Merge: chat list shows running turns and background tasks for every chat
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
|
||||
/// One chat's live activity as the server observes it: a turn in flight and
|
||||
/// how many background tasks (shells, background commands, subagents) still
|
||||
/// run. It rides on each `chat_sessions` row as `activity` and is pushed to
|
||||
/// every connection as `chat_activity` on each change. [seq] orders one
|
||||
/// chat's snapshots across the list and the pushes; the larger one is newer.
|
||||
typedef ChatListActivity = ({
|
||||
String sessionId,
|
||||
bool turnRunning,
|
||||
int backgroundTasks,
|
||||
int seq,
|
||||
});
|
||||
|
||||
/// Reads an `activity` row field or a `chat_activity` body. [sessionId] names
|
||||
/// the row when the body omits it. Anything malformed is null, so an older
|
||||
/// server (which sends neither) leaves the list exactly as before.
|
||||
ChatListActivity? chatListActivityOf(Object? value, {String? sessionId}) {
|
||||
if (value is! Map) return null;
|
||||
final named = value['session_id'];
|
||||
final id = sessionId ?? (named is String ? named : '');
|
||||
if (id.isEmpty) return null;
|
||||
final tasks = value['background_tasks'];
|
||||
final seq = value['seq'];
|
||||
return (
|
||||
sessionId: id,
|
||||
turnRunning: value['turn_running'] == true,
|
||||
backgroundTasks: tasks is num && tasks > 0 ? tasks.toInt() : 0,
|
||||
seq: seq is num ? seq.toInt() : 0,
|
||||
);
|
||||
}
|
||||
|
||||
/// The row keys the list controller derives from the activity.
|
||||
JsonMap chatListActivityRowFields(ChatListActivity activity) => {
|
||||
'running': activity.turnRunning,
|
||||
'background_tasks': activity.backgroundTasks,
|
||||
};
|
||||
|
||||
/// Running background tasks of a list row; 0 when unknown.
|
||||
int chatSessionBackgroundTasks(JsonMap session) {
|
||||
final tasks = session['background_tasks'];
|
||||
return tasks is int && tasks > 0 ? tasks : 0;
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import '../../../../core/servers/server_scope.dart';
|
||||
import '../../../../core/transport/connection_state.dart';
|
||||
import '../../../attention/runtime/attention_ack_sync.dart';
|
||||
import '../../data/chat_repository.dart';
|
||||
import '../../domain/chat_list_activity.dart';
|
||||
import '../../domain/chat_sessions.dart';
|
||||
import 'chat_session_local.dart';
|
||||
import 'session_attention.dart';
|
||||
@@ -65,6 +66,9 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
Future<void> _persistTail = Future<void>.value();
|
||||
int _runningRevision = 0;
|
||||
final Map<String, ({int revision, bool running})> _runningFrames = {};
|
||||
// The newest server activity applied per chat (list row or push), so an
|
||||
// older snapshot that arrives later never replaces it.
|
||||
final Map<String, ChatListActivity> _activity = {};
|
||||
StreamSubscription<JsonMap>? _frameSubscription;
|
||||
String? activeSessionId;
|
||||
|
||||
@@ -75,6 +79,9 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
|
||||
void _onFrame(JsonMap frame) {
|
||||
if (frame['type'] == 'chat_session_changed') return _sessionChanged(frame);
|
||||
if (frame['type'] == 'chat_activity') {
|
||||
return _activityChanged(frame['chat_activity']);
|
||||
}
|
||||
final effect = chatSessionFrameEffect(frame);
|
||||
if (effect == null) return;
|
||||
final id = effect.sessionId;
|
||||
@@ -130,6 +137,58 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
if (found) _notify();
|
||||
}
|
||||
|
||||
/// The server pushed a chat's turn state and background-task count. It
|
||||
/// reaches every connection, so every listed chat's spinner and
|
||||
/// background-task badge follow it, not only the chat open here. A push
|
||||
/// older than the snapshot already applied is ignored.
|
||||
void _activityChanged(Object? body) {
|
||||
final activity = chatListActivityOf(body);
|
||||
if (activity == null || _disposed) return;
|
||||
final id = activity.sessionId;
|
||||
if (activity.seq <= (_activity[id]?.seq ?? 0)) return;
|
||||
_activity[id] = activity;
|
||||
_runningFrames[id] = (
|
||||
revision: ++_runningRevision,
|
||||
running: activity.turnRunning,
|
||||
);
|
||||
final fields = chatListActivityRowFields(activity);
|
||||
var changed = false;
|
||||
_sessions = [
|
||||
for (final row in _sessions)
|
||||
if ('${row['id'] ?? ''}' == id &&
|
||||
(row['running'] != fields['running'] ||
|
||||
row['background_tasks'] != fields['background_tasks']))
|
||||
() {
|
||||
changed = true;
|
||||
return {...row, ...fields};
|
||||
}()
|
||||
else
|
||||
row,
|
||||
];
|
||||
if (changed) _notify();
|
||||
}
|
||||
|
||||
/// A listed row with the newest activity known for it: the row's own
|
||||
/// `activity` unless a newer push was already applied, then a turn frame
|
||||
/// this connection saw during the read. An older server sends no
|
||||
/// `activity`, so only the turn frames apply, as before.
|
||||
JsonMap _withActivity(JsonMap row, int runningRevision) {
|
||||
final id = '${row['id'] ?? ''}';
|
||||
var next = row;
|
||||
final listed = chatListActivityOf(row['activity'], sessionId: id);
|
||||
if (listed != null) {
|
||||
final known = _activity[id];
|
||||
final activity = known != null && known.seq > listed.seq ? known : listed;
|
||||
_activity[id] = activity;
|
||||
next = {...row, ...chatListActivityRowFields(activity)};
|
||||
}
|
||||
final frame = _runningFrames[id];
|
||||
if ((frame?.revision ?? 0) > runningRevision) {
|
||||
next = {...next, 'running': frame!.running};
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
void _bindAckSync(AttentionAckSync? sync) {
|
||||
if (identical(sync, _ackSync)) return;
|
||||
_ackSync?.removeListener(_ackChanged);
|
||||
@@ -246,6 +305,7 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
if (identical(next.gateway, repository.gateway)) return;
|
||||
repository = next;
|
||||
_runningFrames.clear();
|
||||
_activity.clear();
|
||||
_listenFrames();
|
||||
_bindAckSync(attentionAckSyncOf(next.gateway) ?? _ackSync);
|
||||
refresh();
|
||||
@@ -265,12 +325,7 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
final sessions = await repository.sessions();
|
||||
if (generation != _generation) return;
|
||||
_sessions = [
|
||||
for (final row in sessions)
|
||||
if ((_runningFrames['${row['id'] ?? ''}']?.revision ?? 0) >
|
||||
runningRevision)
|
||||
{...row, 'running': _runningFrames['${row['id'] ?? ''}']!.running}
|
||||
else
|
||||
row,
|
||||
for (final row in sessions) _withActivity(row, runningRevision),
|
||||
];
|
||||
_seedLocal();
|
||||
_loading = false;
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// The corner badge a chat's avatar wears for its live activity: the
|
||||
/// platform's activity indicator while a turn runs; otherwise, while
|
||||
/// background tasks still run, a quiet muted glyph (never a second spinner).
|
||||
/// Null when the chat is idle.
|
||||
Widget? chatSessionActivityBadge({
|
||||
required bool cupertino,
|
||||
required bool running,
|
||||
required int backgroundTasks,
|
||||
}) {
|
||||
if (running) {
|
||||
return Semantics(
|
||||
key: const Key('chat-row-working'),
|
||||
label: 'Chat is working',
|
||||
child: _WorkingBadge(cupertino: cupertino),
|
||||
);
|
||||
}
|
||||
if (backgroundTasks <= 0) return null;
|
||||
final label = 'Background tasks running ($backgroundTasks)';
|
||||
// Hover shows the tooltip on a pointer device; a touch long press stays the
|
||||
// row's own gesture.
|
||||
return Tooltip(
|
||||
key: const Key('chat-row-background'),
|
||||
message: label,
|
||||
triggerMode: TooltipTriggerMode.manual,
|
||||
excludeFromSemantics: true,
|
||||
child: Semantics(
|
||||
label: label,
|
||||
child: _BackgroundBadge(cupertino: cupertino),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// The badge's own disc in the surface colour, so it reads over any avatar.
|
||||
class _BadgeDisc extends StatelessWidget {
|
||||
const _BadgeDisc({required this.cupertino, required this.child});
|
||||
|
||||
final bool cupertino;
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final background = cupertino
|
||||
? CupertinoColors.systemBackground.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.surface;
|
||||
return DecoratedBox(
|
||||
decoration: BoxDecoration(color: background, shape: BoxShape.circle),
|
||||
child: Padding(padding: const EdgeInsets.all(2), child: child),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _WorkingBadge extends StatelessWidget {
|
||||
const _WorkingBadge({required this.cupertino});
|
||||
|
||||
final bool cupertino;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return _BadgeDisc(
|
||||
cupertino: cupertino,
|
||||
child: cupertino
|
||||
? const CupertinoActivityIndicator(radius: 6)
|
||||
: const SizedBox.square(
|
||||
dimension: 12,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _BackgroundBadge extends StatelessWidget {
|
||||
const _BackgroundBadge({required this.cupertino});
|
||||
|
||||
final bool cupertino;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final color = cupertino
|
||||
? CupertinoColors.secondaryLabel.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.onSurfaceVariant;
|
||||
return _BadgeDisc(
|
||||
cupertino: cupertino,
|
||||
child: Icon(
|
||||
cupertino ? CupertinoIcons.ellipsis_circle_fill : Icons.pending,
|
||||
size: 12,
|
||||
color: color,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -6,10 +6,12 @@ import '../../../../ui/layout/app_list_avatar.dart';
|
||||
import '../../../../ui/layout/app_list_row.dart';
|
||||
import '../../../../ui/text/relative_time_label.dart';
|
||||
import '../../domain/chat_colors.dart';
|
||||
import '../../domain/chat_list_activity.dart';
|
||||
import '../../domain/chat_sessions.dart';
|
||||
import 'chat_adaptive.dart';
|
||||
import 'chat_color_palette.dart';
|
||||
import 'session_actions.dart';
|
||||
import 'session_activity_badge.dart';
|
||||
import 'session_harness_icon.dart';
|
||||
import 'session_row_style.dart';
|
||||
|
||||
@@ -68,6 +70,9 @@ class ChatSessionRow extends StatelessWidget {
|
||||
leading: ChatSessionAvatar(
|
||||
session: session,
|
||||
running: session['running'] == true && !unseen,
|
||||
backgroundTasks: session['running'] == true
|
||||
? 0
|
||||
: chatSessionBackgroundTasks(session),
|
||||
selected: selected,
|
||||
),
|
||||
// The star rides in the title's own text, so a narrow row ellipsises
|
||||
@@ -149,17 +154,20 @@ class _Star extends StatelessWidget {
|
||||
|
||||
/// The chat's harness mark in a filled circle, like a contact avatar. A
|
||||
/// running chat carries a small activity badge on the avatar's corner instead
|
||||
/// of a line of its own; a selected chat's avatar takes the accent fill.
|
||||
/// of a line of its own, and an idle chat whose background tasks still run a
|
||||
/// quieter one; a selected chat's avatar takes the accent fill.
|
||||
class ChatSessionAvatar extends StatelessWidget {
|
||||
const ChatSessionAvatar({
|
||||
super.key,
|
||||
required this.session,
|
||||
this.running = false,
|
||||
this.backgroundTasks = 0,
|
||||
this.selected = false,
|
||||
});
|
||||
|
||||
final JsonMap session;
|
||||
final bool running;
|
||||
final int backgroundTasks;
|
||||
final bool selected;
|
||||
|
||||
@override
|
||||
@@ -192,39 +200,12 @@ class ChatSessionAvatar extends StatelessWidget {
|
||||
return AppListAvatar(
|
||||
foreground: mark,
|
||||
background: fill,
|
||||
badge: running
|
||||
? Semantics(
|
||||
key: const Key('chat-row-working'),
|
||||
label: 'Chat is working',
|
||||
child: _WorkingBadge(cupertino: cupertino),
|
||||
)
|
||||
: null,
|
||||
badge: chatSessionActivityBadge(
|
||||
cupertino: cupertino,
|
||||
running: running,
|
||||
backgroundTasks: backgroundTasks,
|
||||
),
|
||||
child: SessionHarnessIcon(session: session, color: mark),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _WorkingBadge extends StatelessWidget {
|
||||
const _WorkingBadge({required this.cupertino});
|
||||
|
||||
final bool cupertino;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final background = cupertino
|
||||
? CupertinoColors.systemBackground.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.surface;
|
||||
return DecoratedBox(
|
||||
decoration: BoxDecoration(color: background, shape: BoxShape.circle),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(2),
|
||||
child: cupertino
|
||||
? const CupertinoActivityIndicator(radius: 6)
|
||||
: const SizedBox.square(
|
||||
dimension: 12,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +1,58 @@
|
||||
# Session list: background-task indicator (blocked on server data)
|
||||
# Session list: turn and background-task indicators
|
||||
|
||||
Request: show a quieter indicator on a chat list row when the turn is not running
|
||||
but the session still has live background work (shells, Claude background Bash,
|
||||
subagents). The turn spinner would win.
|
||||
Request: besides the turn spinner, show a quieter indicator on a chat list row
|
||||
when the turn is idle but the session still runs background work (shells,
|
||||
Claude Code background Bash and Agent/Task subagents, Codex command executions,
|
||||
OpenCode shells). Also make the spinner reliable for every listed chat, not
|
||||
only chats this connection opened.
|
||||
|
||||
Not implemented. The client cannot know this per listed chat without guessing.
|
||||
Implemented, with an approved server change.
|
||||
|
||||
## What exists
|
||||
- Background work is modelled server-side as shells for all three harnesses
|
||||
(OpenCode shell API, Codex `commandExecution` items, Claude background Bash
|
||||
and Agent/Task in `internal/claude/translate.go`).
|
||||
- `chat_event` kind `shell` frames: sent only to subscribers of that session's
|
||||
room (`chatHub.emitRoom`, `internal/server/chat_room.go`). The app subscribes
|
||||
only the chat it opens; subscriptions end when the socket closes.
|
||||
- `chat_shells` (`handleChatShells`, `internal/server/chat_shell_output.go`):
|
||||
an on-demand per-session readback. It is not pushed, and for Codex/Claude it
|
||||
goes through `chatManager.harness`, which can start the harness.
|
||||
## Server
|
||||
- `protocol.ChatSession.Activity` (`activity`: `turn_running`,
|
||||
`background_tasks`, `seq`) is set on every `chat_sessions` row from the hub's
|
||||
own state (`internal/server/chat_activity.go`, `handleChatSessions`).
|
||||
- `chat_activity` is pushed to every connection (through `chatSessionChanges`)
|
||||
when a session's turn starts or ends or its running-shell count changes. It
|
||||
is computed under the hub mutex at those points only: turn admitted or
|
||||
finished, step frames, shell frames, shell readback, and room release. Text
|
||||
and tool chunks never push. `seq` starts from the wall clock in microseconds,
|
||||
so it orders across a restart.
|
||||
- Keep-alive: `keepUnattendedLocked` keeps an unwatched room while it holds
|
||||
running shells, so their exits are seen and the badge clears. When the last
|
||||
shell exits, the room is released at once (`emitRoom`) or by the reaper.
|
||||
Bounds: the existing 16-room unattended cap, the 6-hour
|
||||
`chatUnattendedTurnLimit` (measured from when the room became unattended),
|
||||
and the harness itself. A Claude Code process exit now reports its running
|
||||
background tasks as exited (`processExit`, `internal/claude`). A released
|
||||
room reports 0 tasks rather than a stale count.
|
||||
|
||||
## What is missing
|
||||
- `protocol.ChatSession` (`internal/protocol/chat.go`) has no background field,
|
||||
and `handleChatSessions` (`internal/server/chat_sessions.go`) sets none.
|
||||
- No push to every connection when a session's running-shell set changes
|
||||
(`chat_session_changed` goes to all connections but carries no such field).
|
||||
- `keepUnattendedLocked` (`internal/server/chat_unattended.go`) does not keep a
|
||||
room alive for running shells, so after the last viewer leaves the server stops
|
||||
observing that session's shell exits.
|
||||
## App
|
||||
- `ChatSessionsController` applies `chat_activity` and row `activity` by `seq`
|
||||
(the larger wins), deriving `running` and `background_tasks` on the row.
|
||||
The list re-reads on reconnect (the existing `syncToken`). A server without
|
||||
`activity` leaves the list as before.
|
||||
- `session_activity_badge.dart`: the turn spinner, or, when idle with tasks,
|
||||
a muted `Icons.pending` / `CupertinoIcons.ellipsis_circle_fill` glyph with
|
||||
tooltip and semantics "Background tasks running (N)". The tooltip shows on
|
||||
hover only (`TooltipTriggerMode.manual`), so a touch long press stays the
|
||||
row's own gesture.
|
||||
- Fixture: `?chats=N&activity=1` in the capture app lists one running chat,
|
||||
one with two background tasks and idle chats (`tool/perf/perf_fixture.dart`).
|
||||
|
||||
A server change that adds a count to the list row, sends it with
|
||||
`chat_session_changed` (or a dedicated frame) on each running-set change, and keeps
|
||||
observing sessions with live shells would let the client draw this accurately
|
||||
through `chatSessionFrameEffect` and `session_row_content.dart`.
|
||||
## Evidence
|
||||
- Go: `chat_activity_test.go` covers the list payload, the pushes on turn start
|
||||
and end and on task start and exit, unattended keep and release on exit, and
|
||||
expiry of a task that never reports an exit. `background_tasks_test.go` covers
|
||||
the Claude process exit.
|
||||
- Flutter: `test/features/chat/ui/sessions/chat_sessions_activity_test.dart`.
|
||||
- Captures (fixture app): Material phone 560x1000, Material desktop
|
||||
1280x900 with the hover tooltip, and Cupertino dark phone.
|
||||
|
||||
The existing turn spinner has the same limit: it follows only rooms this connection
|
||||
subscribed to.
|
||||
## Unverified
|
||||
- Real harness behaviour. Shell start and exit events come from each adapter's
|
||||
existing translation. Whether a real Codex or OpenCode background command
|
||||
always reports its exit was not exercised against live harnesses here.
|
||||
- Background Bash outliving a crashed Claude Code CLI. It is reported exited
|
||||
on process exit, on the assumption that the CLI's tasks end with it.
|
||||
- Device (Android/iOS) rendering; only the browser was captured.
|
||||
|
||||
@@ -0,0 +1,264 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/chat/data/chat_repository.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_list_activity.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/sessions/chat_sessions_controller.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/sessions/chat_sessions_screen.dart';
|
||||
|
||||
import 'chat_test_harness.dart';
|
||||
import 'fake_chat_gateway.dart';
|
||||
|
||||
/// The list shows every chat's live activity from the server: a spinner while
|
||||
/// a turn runs, a quieter badge while only background tasks run, nothing when
|
||||
/// idle. `chat_activity` pushes reach chats this device never opened, and an
|
||||
/// older server (no `activity`) leaves the list as before.
|
||||
JsonMap _row(String id, {JsonMap? activity}) => {
|
||||
'id': id,
|
||||
'title': 'Chat $id',
|
||||
'directory': '/w',
|
||||
'updated_at': 1000,
|
||||
'activity': ?activity,
|
||||
};
|
||||
|
||||
JsonMap _activity({bool turn = false, int tasks = 0, int seq = 1}) => {
|
||||
if (turn) 'turn_running': true,
|
||||
if (tasks > 0) 'background_tasks': tasks,
|
||||
'seq': seq,
|
||||
};
|
||||
|
||||
JsonMap _push(
|
||||
String id, {
|
||||
bool turn = false,
|
||||
int tasks = 0,
|
||||
required int seq,
|
||||
}) => {
|
||||
'type': 'chat_activity',
|
||||
'chat_activity': {
|
||||
'session_id': id,
|
||||
..._activity(turn: turn, tasks: tasks, seq: seq),
|
||||
},
|
||||
};
|
||||
|
||||
/// The row merges its semantics, so the badge's label is part of the row's.
|
||||
Finder _label(int tasks) => find.bySemanticsLabel(
|
||||
RegExp(RegExp.escape('Background tasks running ($tasks)')),
|
||||
);
|
||||
|
||||
Finder _badge(String id) => find.descendant(
|
||||
of: find.byKey(Key('chat-row-$id')),
|
||||
matching: find.byKey(const Key('chat-row-background')),
|
||||
);
|
||||
|
||||
Finder _spinner(String id) => find.descendant(
|
||||
of: find.byKey(Key('chat-row-$id')),
|
||||
matching: find.byKey(const Key('chat-row-working')),
|
||||
);
|
||||
|
||||
Future<void> _pump(
|
||||
WidgetTester tester,
|
||||
FakeChatGateway fake, {
|
||||
bool cupertino = false,
|
||||
}) async {
|
||||
final screen = ChatSessionsScreen(
|
||||
gateway: fake,
|
||||
onOpen: (_) {},
|
||||
localStore: MemoryChatSessionLocalStore(),
|
||||
);
|
||||
await tester.pumpWidget(
|
||||
cupertino
|
||||
? CupertinoApp(home: screen)
|
||||
: MaterialApp(
|
||||
theme: ThemeData(platform: TargetPlatform.android),
|
||||
home: screen,
|
||||
),
|
||||
);
|
||||
// A spinner never settles, so pump a few frames instead.
|
||||
for (var i = 0; i < 5; i++) {
|
||||
await tester.pump(const Duration(milliseconds: 50));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _frames(WidgetTester tester) async {
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets(
|
||||
'listed activity draws a spinner, a background badge or nothing',
|
||||
(tester) async {
|
||||
final fake = FakeChatGateway()
|
||||
..sessionsReply = {
|
||||
'chat_sessions': [
|
||||
_row('run', activity: _activity(turn: true, tasks: 1)),
|
||||
_row('bg', activity: _activity(tasks: 2)),
|
||||
_row('idle', activity: _activity()),
|
||||
],
|
||||
};
|
||||
await _pump(tester, fake);
|
||||
|
||||
expect(_spinner('run'), findsOneWidget);
|
||||
expect(_badge('run'), findsNothing, reason: 'the turn spinner wins');
|
||||
expect(_spinner('bg'), findsNothing);
|
||||
expect(_badge('bg'), findsOneWidget);
|
||||
expect(_spinner('idle'), findsNothing);
|
||||
expect(_badge('idle'), findsNothing);
|
||||
expect(_label(2), findsOneWidget);
|
||||
final tooltip = tester.widget<Tooltip>(_badge('bg'));
|
||||
expect(tooltip.message, 'Background tasks running (2)');
|
||||
expect(tooltip.triggerMode, TooltipTriggerMode.manual);
|
||||
expect(
|
||||
find.descendant(of: _badge('bg'), matching: find.byIcon(Icons.pending)),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(
|
||||
find.descendant(
|
||||
of: _badge('bg'),
|
||||
matching: find.byType(CircularProgressIndicator),
|
||||
),
|
||||
findsNothing,
|
||||
reason: 'the background badge is not a second spinner',
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('pushes move a chat this device never opened', (tester) async {
|
||||
final fake = FakeChatGateway()
|
||||
..sessionsReply = {
|
||||
'chat_sessions': [_row('s1', activity: _activity(seq: 10))],
|
||||
};
|
||||
await _pump(tester, fake);
|
||||
expect(_spinner('s1'), findsNothing);
|
||||
|
||||
fake.pushed.add(_push('s1', turn: true, seq: 11));
|
||||
await _frames(tester);
|
||||
expect(_spinner('s1'), findsOneWidget);
|
||||
|
||||
fake.pushed.add(_push('s1', tasks: 3, seq: 13));
|
||||
await _frames(tester);
|
||||
expect(_spinner('s1'), findsNothing);
|
||||
expect(_label(3), findsOneWidget);
|
||||
|
||||
// A push older than the snapshot already applied is ignored.
|
||||
fake.pushed.add(_push('s1', turn: true, seq: 12));
|
||||
await _frames(tester);
|
||||
expect(_spinner('s1'), findsNothing);
|
||||
expect(_badge('s1'), findsOneWidget);
|
||||
|
||||
fake.pushed.add(_push('s1', seq: 14));
|
||||
await _frames(tester);
|
||||
expect(_badge('s1'), findsNothing);
|
||||
expect(_spinner('s1'), findsNothing);
|
||||
expect(fake.count('chat_sessions'), 1);
|
||||
});
|
||||
|
||||
testWidgets('an older server without activity keeps the old spinner', (
|
||||
tester,
|
||||
) async {
|
||||
final fake = FakeChatGateway()
|
||||
..sessionsReply = {
|
||||
'chat_sessions': [_row('s1')],
|
||||
};
|
||||
await _pump(tester, fake);
|
||||
expect(_badge('s1'), findsNothing);
|
||||
expect(_spinner('s1'), findsNothing);
|
||||
fake.pushed.add({
|
||||
'type': 'chat_event',
|
||||
'chat_event': {
|
||||
'session_id': 's1',
|
||||
'kind': 'step',
|
||||
'step': {'status': 'started'},
|
||||
},
|
||||
});
|
||||
await _frames(tester);
|
||||
expect(_spinner('s1'), findsOneWidget);
|
||||
expect(_badge('s1'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('the Cupertino host draws its own glyph', (tester) async {
|
||||
final fake = FakeChatGateway()
|
||||
..sessionsReply = {
|
||||
'chat_sessions': [_row('bg', activity: _activity(tasks: 1))],
|
||||
};
|
||||
await _pump(tester, fake, cupertino: true);
|
||||
expect(
|
||||
find.descendant(
|
||||
of: _badge('bg'),
|
||||
matching: find.byIcon(CupertinoIcons.ellipsis_circle_fill),
|
||||
),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(_label(1), findsOneWidget);
|
||||
});
|
||||
|
||||
group('controller', () {
|
||||
late FakeChatGateway fake;
|
||||
late ChatSessionsController controller;
|
||||
late List<JsonMap> rows;
|
||||
|
||||
setUp(() {
|
||||
fake = FakeChatGateway();
|
||||
fake.sessionsHandler = () async => {'chat_sessions': rows};
|
||||
controller = ChatSessionsController(
|
||||
ChatRepository(fake),
|
||||
localStore: MemoryChatSessionLocalStore(),
|
||||
);
|
||||
});
|
||||
tearDown(() => controller.dispose());
|
||||
|
||||
JsonMap row() => controller.sessions.single;
|
||||
|
||||
test('a list read older than an applied push keeps the push', () async {
|
||||
rows = [_row('s1', activity: _activity(seq: 5))];
|
||||
await controller.refresh();
|
||||
fake.pushed.add(_push('s1', tasks: 2, seq: 7));
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
expect(chatSessionBackgroundTasks(row()), 2);
|
||||
|
||||
rows = [_row('s1', activity: _activity(seq: 6))];
|
||||
await controller.refresh();
|
||||
expect(chatSessionBackgroundTasks(row()), 2);
|
||||
expect(row()['running'], false);
|
||||
});
|
||||
|
||||
test('a newer list read (a reconnect) replaces the pushed state', () async {
|
||||
rows = [_row('s1', activity: _activity(seq: 5))];
|
||||
await controller.refresh();
|
||||
fake.pushed.add(_push('s1', turn: true, tasks: 2, seq: 7));
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
expect(row()['running'], true);
|
||||
|
||||
rows = [_row('s1', activity: _activity(seq: 90))];
|
||||
await controller.refresh();
|
||||
expect(row()['running'], false);
|
||||
expect(chatSessionBackgroundTasks(row()), 0);
|
||||
});
|
||||
});
|
||||
|
||||
test('chatListActivityOf reads rows and pushes and refuses malformed', () {
|
||||
expect(
|
||||
chatListActivityOf({
|
||||
'turn_running': true,
|
||||
'background_tasks': 2,
|
||||
'seq': 9,
|
||||
}, sessionId: 's1'),
|
||||
(sessionId: 's1', turnRunning: true, backgroundTasks: 2, seq: 9),
|
||||
);
|
||||
expect(chatListActivityOf({'session_id': 's2'}), (
|
||||
sessionId: 's2',
|
||||
turnRunning: false,
|
||||
backgroundTasks: 0,
|
||||
seq: 0,
|
||||
));
|
||||
expect(chatListActivityOf({'background_tasks': 1}), isNull);
|
||||
expect(chatListActivityOf('nope', sessionId: 's1'), isNull);
|
||||
expect(
|
||||
chatListActivityOf({
|
||||
'background_tasks': -4,
|
||||
}, sessionId: 's1')?.backgroundTasks,
|
||||
0,
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -10,6 +10,8 @@
|
||||
// that harness's account quota after a chat subscribes.
|
||||
// `?colors=1` (with `?chats=N`) names the chats, stars and colours some of
|
||||
// them, and keeps colours picked with chat_color;
|
||||
// `?activity=1` (with `?chats=N`) lists a running chat, one with
|
||||
// background tasks and idle ones;
|
||||
// `?msgs=`, `?rich=1`, `?older=1`, `?live=N` vary the chat transcripts for
|
||||
// scroll captures (tool/perf/chat_scroll_fixture.dart).
|
||||
// `?question=` pushes an agent question (tool/perf/chat_question_fixture.dart).
|
||||
@@ -35,6 +37,18 @@ int get _chats => int.tryParse(_params['chats'] ?? '') ?? 0;
|
||||
bool get perfRunGraph => _params['graph'] == '1';
|
||||
bool get _colors => _params['colors'] == '1';
|
||||
|
||||
/// `?activity=1` (with `?chats=N`): the first chat's turn is running, the
|
||||
/// second is idle with two background tasks, the rest are idle, as the
|
||||
/// server's `activity` row field reports them.
|
||||
JsonMap? _activityFor(int i) {
|
||||
if (_params['activity'] != '1') return null;
|
||||
return switch (i) {
|
||||
0 => {'turn_running': true, 'background_tasks': 1, 'seq': 3},
|
||||
1 => {'background_tasks': 2, 'seq': 2},
|
||||
_ => {'seq': 1},
|
||||
};
|
||||
}
|
||||
|
||||
/// `?colors=1`: titles, directories, stars and colours per chat index.
|
||||
const _colorTitles = [
|
||||
'Release notes',
|
||||
@@ -61,6 +75,7 @@ JsonMap _chatRow(int i) {
|
||||
if (_harness != 'opencode') 'backend': _harness,
|
||||
'resume_command': _resumeCommand(id),
|
||||
'native_export': _harness == 'claude',
|
||||
'activity': ?_activityFor(i),
|
||||
};
|
||||
}
|
||||
final color = _pickedColors.containsKey(id)
|
||||
@@ -75,6 +90,7 @@ JsonMap _chatRow(int i) {
|
||||
'updated_at': DateTime.now().millisecondsSinceEpoch - (i + 1) * 600000,
|
||||
if (_colorStarred.contains(i)) 'starred': true,
|
||||
'color': ?color,
|
||||
'activity': ?_activityFor(i),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -95,6 +95,19 @@ Claude Code also accepts `/clear` in `-p --input-format stream-json` mode, but
|
||||
the CLI then picks the new session id itself. Re-keying keeps one path for all
|
||||
three harnesses and lets the server choose the id.
|
||||
|
||||
## Chat list activity
|
||||
|
||||
Every `chat_sessions` row carries `activity` (turn running, running background
|
||||
tasks), and `chat_activity` pushes each change to every connection (see
|
||||
`docs/cli.md`). Background tasks are the same shells all three harnesses already
|
||||
report: OpenCode shells, Codex `commandExecution` items, and Claude Code
|
||||
background Bash and Agent/Task subagents. A chat whose last viewer left stays
|
||||
observed while its tasks run, so their exits are seen. That is bounded by the
|
||||
unattended limits (16 chats, 6 hours) and by the harness: when a Claude Code
|
||||
process exits, its background tasks are reported exited because they ran inside
|
||||
it. After an OpenCode or Codex stream reconnects, the shell set is re-read from
|
||||
the harness.
|
||||
|
||||
## Web interaction contract
|
||||
|
||||
All advertised chat adapters (OpenCode V2, Codex and Claude Code) implement the
|
||||
|
||||
+14
@@ -455,6 +455,20 @@ sessions pipeline runs create. The protocol types are:
|
||||
for the run that owns them. The list is fail-closed: with no registry and no
|
||||
store it returns an empty list, never the endpoint's whole session list. A
|
||||
session is never marked `busy`: any number of clients may watch and write one.
|
||||
Each row carries `activity` (`turn_running`, `background_tasks`, `seq`): the
|
||||
server's live view of the session. An older server omits it.
|
||||
- `chat_activity` is pushed to every open connection, not only the chat's
|
||||
watchers, when a session's turn starts or ends or its count of running
|
||||
background tasks changes (shells, background commands, Claude Code background
|
||||
Bash and subagents, Codex command executions). The body is `chat_activity`
|
||||
(`session_id`, `turn_running`, `background_tasks`, `seq`). It is sent on those
|
||||
transitions only, never per output chunk. `seq` increases with every change
|
||||
and keeps increasing across a restart, so a client keeps the snapshot (list
|
||||
row or push) with the larger `seq`. Background tasks are counted only while
|
||||
the server observes the session: after a chat's last viewer leaves, the
|
||||
server keeps observing it while its tasks run (bounded like an unattended
|
||||
turn: at most 16 such chats and 6 hours). Once released it reports none
|
||||
rather than a stale count.
|
||||
- `chat_subscribe` attaches a connection to one session's shared live stream (body
|
||||
`session_id`) and replays the session's still-pending approval requests, so every
|
||||
client that watches a session sees every frame any client's turn produces — the
|
||||
|
||||
@@ -118,3 +118,35 @@ func TestForegroundCommandsAndAgentsFinishWithTheirResult(t *testing.T) {
|
||||
t.Fatalf("stray notifications list nothing, got %+v", shells)
|
||||
}
|
||||
}
|
||||
|
||||
// Background commands and subagents run inside the CLI process, so a process
|
||||
// that exits (crash, idle release, interrupt) ends them: each is reported as
|
||||
// exited instead of staying "running" with nothing left to report its end.
|
||||
func TestProcessExitEndsBackgroundTasks(t *testing.T) {
|
||||
chat, s := backgroundSession(t)
|
||||
s.handleLine(nil, toolUse(t, "toolu_bg", "Bash", map[string]any{"command": "sleep 600", "run_in_background": true}))
|
||||
s.handleLine(nil, toolResult(t, "toolu_bg", "Command running in background with ID: b9. Output is being written to: /tmp/b9.output"))
|
||||
if shells := running(t, chat); len(shells) != 1 {
|
||||
t.Fatalf("the background command must be running first, got %+v", shells)
|
||||
}
|
||||
subs := attachTestSubs(s, 1)
|
||||
defer detachTestSubs(s, subs)
|
||||
proc := &process{session: s}
|
||||
s.mu.Lock()
|
||||
s.proc = proc
|
||||
s.mu.Unlock()
|
||||
|
||||
s.processExit(proc, nil)
|
||||
|
||||
if shells := running(t, chat); len(shells) != 0 {
|
||||
t.Fatalf("a process exit must end its background tasks, got %+v", shells)
|
||||
}
|
||||
select {
|
||||
case frame := <-subs[0]:
|
||||
if frame.Kind != protocol.ChatEventShell || frame.Shell == nil || frame.Shell.Status != protocol.ChatShellExited || frame.Shell.Command != "sleep 600" {
|
||||
t.Fatalf("the exit is published as a shell exit, got %+v", frame)
|
||||
}
|
||||
default:
|
||||
t.Fatal("no shell exit was published")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -379,7 +379,14 @@ func (s *session) processExit(p *process, err error) {
|
||||
s.steered = 0
|
||||
s.awaitFollowUp = false
|
||||
s.resetStreamLocked()
|
||||
// Background commands and subagents run inside the CLI process, so its
|
||||
// exit ends them: report each as exited, or it would stay "running" (in the
|
||||
// chat list and in chat_shells) with nothing left to report its end.
|
||||
ended := s.endShellsLocked()
|
||||
s.mu.Unlock()
|
||||
for _, shell := range ended {
|
||||
s.emit(protocol.ChatEventFrame{SessionID: s.id, Kind: protocol.ChatEventShell, Shell: shell})
|
||||
}
|
||||
if !running {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -791,6 +791,22 @@ func (s *session) finishNotifiedTask(line []byte) {
|
||||
))
|
||||
}
|
||||
|
||||
// endShellsLocked marks every command still running as exited (with no exit
|
||||
// code: none was reported) and returns their final states, sorted by id, for
|
||||
// the caller to publish once s.mu is released. Callers hold s.mu.
|
||||
func (s *session) endShellsLocked() []*protocol.ChatShell {
|
||||
var ended []*protocol.ChatShell
|
||||
for _, snapshot := range s.shells {
|
||||
if snapshot.shell.Status != protocol.ChatShellRunning {
|
||||
continue
|
||||
}
|
||||
snapshot.shell.Status = protocol.ChatShellExited
|
||||
ended = append(ended, &protocol.ChatShell{ID: snapshot.shell.ID, Status: snapshot.shell.Status, Command: snapshot.shell.Command})
|
||||
}
|
||||
sort.Slice(ended, func(i, j int) bool { return ended[i].ID < ended[j].ID })
|
||||
return ended
|
||||
}
|
||||
|
||||
// finishBackgroundTasks marks the tasks a Claude Code task notification
|
||||
// reports as finished, and publishes each one's final state.
|
||||
func (s *session) finishBackgroundTasks(text string) {
|
||||
|
||||
@@ -85,6 +85,13 @@ const (
|
||||
// shared list metadata (colour, star, title) changes, carrying the updated
|
||||
// registry row in ChatSession, so each device's list updates live.
|
||||
ChatSessionChanged = "chat_session_changed"
|
||||
// ChatActivityChanged is pushed to every open connection (not only the
|
||||
// session's room subscribers) when a chat's live activity changes: its turn
|
||||
// starts or ends, or its count of running background tasks changes. The
|
||||
// body is ChatActivity. It is sent on those transitions only, never per
|
||||
// output chunk; a client applies a push only when its Seq is newer than the
|
||||
// activity it already holds for that session.
|
||||
ChatActivityChanged = "chat_activity"
|
||||
// ChatSubscribe attaches a connection to one session's shared live stream so
|
||||
// it receives every frame any client's turn produces (its own and the other
|
||||
// devices'), not only the frames of a turn it prompted itself. Any number of
|
||||
@@ -320,6 +327,26 @@ type ChatSession struct {
|
||||
// the operator started a fresh session. Clients replace the row with that
|
||||
// id by this one; it is never part of a listed row.
|
||||
PreviousID string `json:"previous_id,omitempty"`
|
||||
// Activity is the server's live view of the session. A server that tracks
|
||||
// it sets it on every chat_sessions row (its presence, even as an empty
|
||||
// object, tells a client the server also pushes chat_activity). Older
|
||||
// servers omit it.
|
||||
Activity *ChatActivity `json:"activity,omitempty"`
|
||||
}
|
||||
|
||||
// ChatActivity is one session's live activity as the server observes it: a
|
||||
// turn in flight, and how many background tasks (shells, background commands,
|
||||
// subagents) the harness reports still running. It rides on chat_sessions rows
|
||||
// and is pushed as chat_activity. Seq orders one session's snapshots across
|
||||
// the list reply and the pushes (a later snapshot has a larger Seq, also
|
||||
// across a server restart); a client keeps the snapshot with the largest Seq.
|
||||
// Background tasks are counted only while the server observes the session's
|
||||
// stream, so an unobserved session reports none rather than a stale count.
|
||||
type ChatActivity struct {
|
||||
SessionID string `json:"session_id,omitempty"`
|
||||
TurnRunning bool `json:"turn_running,omitempty"`
|
||||
BackgroundTasks int `json:"background_tasks,omitempty"`
|
||||
Seq int64 `json:"seq,omitempty"`
|
||||
}
|
||||
|
||||
// Chat export formats accepted by chat_export.
|
||||
|
||||
@@ -198,6 +198,8 @@ type Message struct {
|
||||
// requests themselves ride on ChatQuery (the session id), matching
|
||||
// chat_messages/chat_interrupt.
|
||||
ChatClose *ChatCloseResult `json:"chat_close,omitempty"`
|
||||
// ChatActivity is the chat_activity push: one session's live activity.
|
||||
ChatActivity *ChatActivity `json:"chat_activity,omitempty"`
|
||||
// ChatAnswer is the chat_answer body; the form update arrives through the
|
||||
// normal chat_event stream.
|
||||
ChatAnswer *ChatAnswerRequest `json:"chat_answer,omitempty"`
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// Chat list activity: every listed chat shows whether its turn is running and
|
||||
// whether background tasks (shells, background commands, subagents) are still
|
||||
// running after the turn. Room frames reach only the connections that opened
|
||||
// that chat, so the hub keeps a small per-session summary and pushes each
|
||||
// change to every connection as chat_activity, and chat_sessions carries the
|
||||
// current summary on each row.
|
||||
//
|
||||
// The summary is derived from the hub's own state only (no broker call, no
|
||||
// I/O), under the hub mutex, at the points where that state changes: a turn
|
||||
// admitted or finished, a step announcing or ending a turn, a shell starting
|
||||
// or exiting, a shell readback reconciling the set, and a room released.
|
||||
// Anything else, including every text or tool chunk, never touches it.
|
||||
//
|
||||
// A session's background tasks are counted only while it has a room, i.e.
|
||||
// while the hub observes its stream (a viewer, or unattended observation, see
|
||||
// chat_unattended.go). Once the room is released its shell exits are no
|
||||
// longer seen, so the count drops to zero rather than going stale.
|
||||
|
||||
// chatActivityLocked computes the session's current summary. Callers hold h.mu.
|
||||
func (h *chatHub) chatActivityLocked(sessionID string) protocol.ChatActivity {
|
||||
activity := protocol.ChatActivity{SessionID: sessionID}
|
||||
room := h.rooms[sessionID]
|
||||
if room == nil {
|
||||
return activity
|
||||
}
|
||||
activity.TurnRunning = room.running || room.observedTurn
|
||||
activity.BackgroundTasks = h.backgroundTasksLocked(sessionID)
|
||||
return activity
|
||||
}
|
||||
|
||||
// backgroundTasksLocked counts the session's retained running shells. Callers
|
||||
// hold h.mu.
|
||||
func (h *chatHub) backgroundTasksLocked(sessionID string) int {
|
||||
if turn := h.turns[sessionID]; turn != nil {
|
||||
return len(turn.shells)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// noteActivityLocked recomputes the session's summary and, only when the turn
|
||||
// state or the background-task count changed, records it under a new sequence
|
||||
// number and pushes it to every connection. Callers hold h.mu; the push itself
|
||||
// never blocks (chatSessionChanges writes on its own goroutines).
|
||||
func (h *chatHub) noteActivityLocked(sessionID string) {
|
||||
if sessionID == "" {
|
||||
return
|
||||
}
|
||||
next := h.chatActivityLocked(sessionID)
|
||||
previous := h.activity[sessionID]
|
||||
if previous.TurnRunning == next.TurnRunning && previous.BackgroundTasks == next.BackgroundTasks {
|
||||
return
|
||||
}
|
||||
if h.activity == nil {
|
||||
h.activity = map[string]protocol.ChatActivity{}
|
||||
}
|
||||
next.Seq = h.nextActivitySeqLocked()
|
||||
// The record keeps the last sequence of an idle session too, so a list
|
||||
// row's Seq never moves backwards behind a push a client already applied.
|
||||
// It holds one small entry per session that was ever active.
|
||||
h.activity[sessionID] = next
|
||||
h.sessionChanges.broadcastActivity(next)
|
||||
}
|
||||
|
||||
// nextActivitySeqLocked returns a strictly increasing sequence. It starts from
|
||||
// the wall clock in microseconds (exact in a JavaScript number) so snapshots
|
||||
// after a server restart still order after the ones a client held before.
|
||||
func (h *chatHub) nextActivitySeqLocked() int64 {
|
||||
if h.activitySeq == 0 {
|
||||
h.activitySeq = time.Now().UnixMicro()
|
||||
}
|
||||
h.activitySeq++
|
||||
return h.activitySeq
|
||||
}
|
||||
|
||||
// activitySnapshot returns the current summary of each session in ids, each
|
||||
// with the sequence of its last recorded change. A session that has not
|
||||
// changed since this server started carries the current sequence instead, so
|
||||
// it still orders after anything a client kept from a previous server process
|
||||
// and before any later push.
|
||||
func (h *chatHub) activitySnapshot(ids []string) map[string]protocol.ChatActivity {
|
||||
out := make(map[string]protocol.ChatActivity, len(ids))
|
||||
if h == nil {
|
||||
return out
|
||||
}
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
if h.activitySeq == 0 {
|
||||
h.activitySeq = time.Now().UnixMicro()
|
||||
}
|
||||
for _, id := range ids {
|
||||
activity := h.chatActivityLocked(id)
|
||||
activity.Seq = h.activitySeq
|
||||
if recorded, ok := h.activity[id]; ok {
|
||||
activity.Seq = recorded.Seq
|
||||
}
|
||||
out[id] = activity
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// broadcastActivity pushes one session's activity to every connection,
|
||||
// including the one whose action caused it (it has no reply carrying it).
|
||||
func (c *chatSessionChanges) broadcastActivity(activity protocol.ChatActivity) {
|
||||
c.mu.Lock()
|
||||
targets := make([]func(protocol.Message) error, 0, len(c.clients))
|
||||
for _, write := range c.clients {
|
||||
targets = append(targets, write)
|
||||
}
|
||||
c.mu.Unlock()
|
||||
for _, write := range targets {
|
||||
body := activity
|
||||
go func() {
|
||||
_ = write(protocol.Message{Type: protocol.ChatActivityChanged, ChatActivity: &body})
|
||||
}()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
"github.com/coder/websocket/wsjson"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// listedObservedChat is an observedChat whose endpoint lists ses_1, so
|
||||
// chat_sessions returns its registry row.
|
||||
type listedObservedChat struct{ *observedChat }
|
||||
|
||||
func (c listedObservedChat) Sessions(context.Context) ([]protocol.ChatSession, error) {
|
||||
return []protocol.ChatSession{{ID: "ses_1"}}, nil
|
||||
}
|
||||
|
||||
// readActivity reads raw frames until the next chat_activity push.
|
||||
func readActivity(t *testing.T, ctx context.Context, conn *websocket.Conn) protocol.ChatActivity {
|
||||
t.Helper()
|
||||
for {
|
||||
var message protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &message); err != nil {
|
||||
t.Fatalf("read (waiting for chat_activity): %v", err)
|
||||
}
|
||||
if message.Type == protocol.ChatActivityChanged {
|
||||
if message.ChatActivity == nil {
|
||||
t.Fatal("chat_activity without a body")
|
||||
}
|
||||
return *message.ChatActivity
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func shellFrame(id, status string) protocol.ChatEventFrame {
|
||||
return protocol.ChatEventFrame{SessionID: "ses_1", Kind: protocol.ChatEventShell, Shell: &protocol.ChatShell{ID: id, Status: status, Command: "sleep 600"}}
|
||||
}
|
||||
|
||||
func listActivity(t *testing.T, ctx context.Context, conn *websocket.Conn) *protocol.ChatActivity {
|
||||
t.Helper()
|
||||
writeChat(t, ctx, conn, protocol.Message{Type: protocol.ChatSessions, ID: "list"})
|
||||
reply := readUntil(t, ctx, conn, protocol.ChatSessions)
|
||||
if len(reply.ChatSessions) != 1 || reply.ChatSessions[0].ID != "ses_1" {
|
||||
t.Fatalf("chat_sessions = %+v", reply.ChatSessions)
|
||||
}
|
||||
return reply.ChatSessions[0].Activity
|
||||
}
|
||||
|
||||
// A connection that never opened the chat still learns, from chat_activity,
|
||||
// when its turn starts and ends and how many background tasks run, and the
|
||||
// list carries the same summary with the same sequence. Output chunks push
|
||||
// nothing. After the last viewer leaves, the session stays observed while a
|
||||
// background task runs, and is released (pushing an idle summary) once the
|
||||
// last one exits.
|
||||
func TestChatActivityReachesEveryConnection(t *testing.T) {
|
||||
fastUnattended(t)
|
||||
t.Setenv("PIPELINES_NTFY_URL", "")
|
||||
store := unattendedStore(t, false)
|
||||
broker := newObservedChat()
|
||||
server := startQueueServer(t, listedObservedChat{broker}, store)
|
||||
viewer := dialChatConn(t, server.ctx, server.srv)
|
||||
list := dialChatConn(t, server.ctx, server.srv)
|
||||
|
||||
// A session that never changed still carries a sequence (this server's
|
||||
// current one), so it orders after anything kept from an earlier process.
|
||||
initial := listActivity(t, server.ctx, list)
|
||||
if initial == nil || initial.TurnRunning || initial.BackgroundTasks != 0 || initial.Seq == 0 {
|
||||
t.Fatalf("idle row activity = %+v, want an empty summary with a sequence", initial)
|
||||
}
|
||||
|
||||
sendPrompt(t, server.ctx, viewer, "p1", "build it", "op-1")
|
||||
started := readActivity(t, server.ctx, list)
|
||||
if started.SessionID != "ses_1" || !started.TurnRunning || started.BackgroundTasks != 0 || started.Seq <= initial.Seq {
|
||||
t.Fatalf("turn start push = %+v", started)
|
||||
}
|
||||
broker.push(stepFrameFor(protocol.ChatStepStarted, "turn_1"))
|
||||
broker.push(textFrame("Working on it."))
|
||||
broker.push(shellFrame("sh_1", protocol.ChatShellRunning))
|
||||
withTask := readActivity(t, server.ctx, list)
|
||||
if !withTask.TurnRunning || withTask.BackgroundTasks != 1 || withTask.Seq <= started.Seq {
|
||||
t.Fatalf("background task push = %+v (after %+v); a text chunk or the re-announced turn must push nothing", withTask, started)
|
||||
}
|
||||
broker.push(stepFrameFor(protocol.ChatStepEnded, "turn_1"))
|
||||
ended := readActivity(t, server.ctx, list)
|
||||
if ended.TurnRunning || ended.BackgroundTasks != 1 || ended.Seq <= withTask.Seq {
|
||||
t.Fatalf("turn end push = %+v", ended)
|
||||
}
|
||||
if got := listActivity(t, server.ctx, list); got == nil || got.TurnRunning || got.BackgroundTasks != 1 || got.Seq != ended.Seq {
|
||||
t.Fatalf("listed activity = %+v, want %+v", got, ended)
|
||||
}
|
||||
|
||||
viewer.CloseNow()
|
||||
time.Sleep(100 * time.Millisecond) // let the server notice the disconnect
|
||||
if active, _ := broker.streams(); active != 1 {
|
||||
t.Fatalf("a running background task was not observed after the viewer left: %d streams", active)
|
||||
}
|
||||
broker.push(shellFrame("sh_1", protocol.ChatShellExited))
|
||||
idle := readActivity(t, server.ctx, list)
|
||||
if idle.TurnRunning || idle.BackgroundTasks != 0 || idle.Seq <= ended.Seq {
|
||||
t.Fatalf("task exit push = %+v", idle)
|
||||
}
|
||||
waitFor(t, "observer released", func() bool { active, _ := broker.streams(); return active == 0 })
|
||||
if got := listActivity(t, server.ctx, list); got == nil || got.BackgroundTasks != 0 || got.Seq != idle.Seq {
|
||||
t.Fatalf("listed activity after release = %+v, want %+v", got, idle)
|
||||
}
|
||||
}
|
||||
|
||||
// A background task that never reports its exit does not hold the observer
|
||||
// forever: it is released after chatUnattendedTurnLimit, and the list then
|
||||
// reports no tasks rather than a stale count.
|
||||
func TestChatUnreportedBackgroundTaskIsReleased(t *testing.T) {
|
||||
fastUnattended(t)
|
||||
t.Setenv("PIPELINES_NTFY_URL", "")
|
||||
limit := chatUnattendedTurnLimit
|
||||
chatUnattendedTurnLimit = 300 * time.Millisecond
|
||||
t.Cleanup(func() { chatUnattendedTurnLimit = limit })
|
||||
store := unattendedStore(t, false)
|
||||
broker := newObservedChat()
|
||||
server := startQueueServer(t, listedObservedChat{broker}, store)
|
||||
viewer := dialChatConn(t, server.ctx, server.srv)
|
||||
list := dialChatConn(t, server.ctx, server.srv)
|
||||
// A reply proves the connection reached its read loop, where it is
|
||||
// attached to the pushes.
|
||||
listActivity(t, server.ctx, list)
|
||||
sendPrompt(t, server.ctx, viewer, "p1", "build it", "op-1")
|
||||
readActivity(t, server.ctx, list)
|
||||
broker.push(stepFrameFor(protocol.ChatStepStarted, "turn_1"))
|
||||
broker.push(shellFrame("sh_1", protocol.ChatShellRunning))
|
||||
readActivity(t, server.ctx, list)
|
||||
broker.push(stepFrameFor(protocol.ChatStepEnded, "turn_1"))
|
||||
readActivity(t, server.ctx, list)
|
||||
viewer.CloseNow()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
if active, _ := broker.streams(); active != 1 {
|
||||
t.Fatalf("background task not observed: %d", active)
|
||||
}
|
||||
released := readActivity(t, server.ctx, list)
|
||||
if released.TurnRunning || released.BackgroundTasks != 0 {
|
||||
t.Fatalf("release push = %+v", released)
|
||||
}
|
||||
waitFor(t, "observer released", func() bool { active, _ := broker.streams(); return active == 0 })
|
||||
if got := listActivity(t, server.ctx, list); got == nil || got.BackgroundTasks != 0 {
|
||||
t.Fatalf("listed activity after expiry = %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// The hub keeps an unattended room for background tasks only: an idle room
|
||||
// with none is released when its last client leaves, a room with one is kept.
|
||||
func TestChatBackgroundTasksKeepUnattendedRoom(t *testing.T) {
|
||||
fastUnattended(t)
|
||||
h := newChatHub()
|
||||
defer func() {
|
||||
h.mu.Lock()
|
||||
for _, room := range h.rooms {
|
||||
room.cancel()
|
||||
}
|
||||
h.mu.Unlock()
|
||||
}()
|
||||
for _, tasks := range []int{0, 1} {
|
||||
id := "ses_idle"
|
||||
if tasks > 0 {
|
||||
id = "ses_task"
|
||||
}
|
||||
h.mu.Lock()
|
||||
room := h.roomLocked(id)
|
||||
if tasks > 0 {
|
||||
h.turnLocked(id).applyShell(&protocol.ChatShell{ID: "sh_1", Status: protocol.ChatShellRunning})
|
||||
}
|
||||
sub := &chatSub{hub: h, room: room, clientID: "c", ch: make(chan protocol.Message, 1), done: make(chan struct{})}
|
||||
room.subs["c"] = sub
|
||||
h.mu.Unlock()
|
||||
h.detach(id, sub)
|
||||
h.mu.Lock()
|
||||
kept := h.rooms[id] == room
|
||||
h.mu.Unlock()
|
||||
if kept != (tasks > 0) {
|
||||
t.Fatalf("%d background tasks: kept=%v", tasks, kept)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,7 @@ func TestChatAdoptRecordsTheOwningHarness(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.ChatAdopt || response.ChatSession == nil {
|
||||
|
||||
@@ -42,7 +42,7 @@ func subscribeChat(t *testing.T, ctx context.Context, conn *websocket.Conn, id s
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ack protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &ack); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &ack); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ack.Type != protocol.ChatSubscribe || ack.ID != id {
|
||||
@@ -55,7 +55,7 @@ func readStepStarted(t *testing.T, ctx context.Context, conn *websocket.Conn) {
|
||||
t.Helper()
|
||||
for {
|
||||
var message protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &message); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &message); err != nil {
|
||||
t.Fatalf("no step-started frame: %v", err)
|
||||
}
|
||||
event := message.ChatEvent
|
||||
@@ -89,7 +89,7 @@ func TestChatSubscribeIdleHarnessReplaysNoTurn(t *testing.T) {
|
||||
defer cancel()
|
||||
for {
|
||||
var message protocol.Message
|
||||
err := wsjson.Read(readCtx, conn, &message)
|
||||
err := readTestFrame(readCtx, conn, &message)
|
||||
if err != nil {
|
||||
if !errors.Is(readCtx.Err(), context.DeadlineExceeded) {
|
||||
t.Fatalf("read: %v", err)
|
||||
|
||||
@@ -25,7 +25,7 @@ func TestChatCloseRecordsClosedState(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.ChatClose || response.ID != "cl1" {
|
||||
@@ -56,7 +56,7 @@ func TestChatCloseRecordsClosedState(t *testing.T) {
|
||||
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatSessions, ID: "l1"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.ChatSessions || len(response.ChatSessions) != 1 {
|
||||
@@ -86,7 +86,7 @@ func TestChatCloseStopsRunningTurn(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ack protocol.Message
|
||||
if err := wsjson.Read(ctx, conns[0], &ack); err != nil {
|
||||
if err := readTestFrame(ctx, conns[0], &ack); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ack.Type != protocol.ChatPrompt {
|
||||
@@ -99,7 +99,7 @@ func TestChatCloseStopsRunningTurn(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conns[1], &response); err != nil {
|
||||
if err := readTestFrame(ctx, conns[1], &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.ChatClose || response.ID != "cl1" {
|
||||
@@ -139,7 +139,7 @@ func TestChatCloseKeepsSessionAndMessages(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.ChatClose {
|
||||
@@ -157,7 +157,7 @@ func TestChatCloseKeepsSessionAndMessages(t *testing.T) {
|
||||
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatMessages, ID: "ms1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.ChatMessages || len(response.ChatMessages) != 1 || response.ChatMessages[0].ID != "m1" {
|
||||
@@ -185,7 +185,7 @@ func TestChatReopenClearsClosedState(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.ChatReopen || response.ID != "op1" || response.ChatSession == nil {
|
||||
@@ -220,7 +220,7 @@ func TestChatPromptReopensClosedSession(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ack protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &ack); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &ack); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ack.Type != protocol.ChatPrompt {
|
||||
@@ -247,7 +247,7 @@ func TestChatCloseRefusesNonOperatorSession(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.Error || !strings.Contains(response.Error, "not an operator") {
|
||||
@@ -276,7 +276,7 @@ func TestChatCloseRejectsInvalidInput(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.Error || response.ID != "bad" {
|
||||
@@ -291,7 +291,7 @@ func TestChatCloseRejectsInvalidInput(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(noRegCtx, noRegConn, &response); err != nil {
|
||||
if err := readTestFrame(noRegCtx, noRegConn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.Error || !strings.Contains(response.Error, "registry") {
|
||||
@@ -304,7 +304,7 @@ func TestChatCloseRejectsInvalidInput(t *testing.T) {
|
||||
if err := wsjson.Write(noBrokerCtx, noBrokerConn, protocol.Message{Type: protocol.ChatClose, ID: "nb", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := wsjson.Read(noBrokerCtx, noBrokerConn, &response); err != nil {
|
||||
if err := readTestFrame(noBrokerCtx, noBrokerConn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.Error || !strings.Contains(response.Error, "not configured") {
|
||||
|
||||
@@ -18,7 +18,7 @@ func chatColorRequest(id, session string, color *string) protocol.Message {
|
||||
func readMessage(t *testing.T, ctx context.Context, conn *websocket.Conn) protocol.Message {
|
||||
t.Helper()
|
||||
var message protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &message); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &message); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return message
|
||||
|
||||
@@ -6,8 +6,6 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/coder/websocket/wsjson"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
@@ -59,7 +57,7 @@ func exportFixture(t *testing.T, backend string) (*pagedChat, context.Context, f
|
||||
writeChat(t, ctx, conn, message)
|
||||
for {
|
||||
var reply protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &reply); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &reply); err != nil {
|
||||
t.Fatalf("read reply to %s: %v", message.ID, err)
|
||||
}
|
||||
if reply.ID == message.ID {
|
||||
|
||||
@@ -63,6 +63,10 @@ type chatHub struct {
|
||||
unattended chatUnattendedTimings
|
||||
// sessionChanges pushes shared list metadata changes to every connection.
|
||||
sessionChanges chatSessionChanges
|
||||
// activity/activitySeq are the last chat-list activity pushed per session
|
||||
// and its sequence source (chat_activity.go).
|
||||
activity map[string]protocol.ChatActivity
|
||||
activitySeq int64
|
||||
}
|
||||
|
||||
// chatPart is the retained form of one part. Text and reasoning deltas append to
|
||||
|
||||
@@ -220,7 +220,7 @@ func readChatResponse(t *testing.T, ctx context.Context, ws *websocket.Conn) ([]
|
||||
func readChatMessage(t *testing.T, ctx context.Context, ws *websocket.Conn) protocol.Message {
|
||||
t.Helper()
|
||||
var message protocol.Message
|
||||
if err := wsjson.Read(ctx, ws, &message); err != nil {
|
||||
if err := readTestFrame(ctx, ws, &message); err != nil {
|
||||
t.Fatalf("read: %v", err)
|
||||
}
|
||||
if message.Type == protocol.Error {
|
||||
|
||||
@@ -224,7 +224,7 @@ func readUntil(t *testing.T, ctx context.Context, conn *websocket.Conn, want str
|
||||
t.Helper()
|
||||
for {
|
||||
var message protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &message); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &message); err != nil {
|
||||
t.Fatalf("read (waiting for %s): %v", want, err)
|
||||
}
|
||||
if message.Type == protocol.Error {
|
||||
@@ -242,7 +242,7 @@ func nextEvent(t *testing.T, ctx context.Context, conn *websocket.Conn) protocol
|
||||
t.Helper()
|
||||
for {
|
||||
var message protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &message); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &message); err != nil {
|
||||
t.Fatalf("read event: %v", err)
|
||||
}
|
||||
if message.Type == protocol.Error {
|
||||
@@ -259,7 +259,7 @@ func nextPermission(t *testing.T, ctx context.Context, conn *websocket.Conn, req
|
||||
t.Helper()
|
||||
for {
|
||||
var message protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &message); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &message); err != nil {
|
||||
t.Fatalf("read permission: %v", err)
|
||||
}
|
||||
if message.Type == protocol.Error {
|
||||
@@ -420,7 +420,7 @@ func TestChatApprovalFirstAnswerWins(t *testing.T) {
|
||||
// The tablet's later answer is refused, and it never reaches the agent.
|
||||
writeChat(t, ctx, tablet, protocol.Message{Type: protocol.ChatReply, ID: "a2", ChatReply: &protocol.ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: protocol.ChatDecisionAlways, Client: "Pixel 10 Pro Fold"}})
|
||||
var refused protocol.Message
|
||||
if err := wsjson.Read(ctx, tablet, &refused); err != nil {
|
||||
if err := readTestFrame(ctx, tablet, &refused); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if refused.Type != protocol.Error || !strings.Contains(refused.Error, "already answered by Pixel 9") {
|
||||
@@ -484,7 +484,7 @@ func TestChatQueuedPromptFailureSurfaced(t *testing.T) {
|
||||
|
||||
for {
|
||||
var message protocol.Message
|
||||
if err := wsjson.Read(ctx, tablet, &message); err != nil {
|
||||
if err := readTestFrame(ctx, tablet, &message); err != nil {
|
||||
t.Fatalf("read (waiting for the queued prompt failure): %v", err)
|
||||
}
|
||||
if message.Type != protocol.Error {
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
"github.com/coder/websocket/wsjson"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/opencode"
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
@@ -236,7 +235,7 @@ func TestChatSubscribeListsQueuedPromptsAndUnqueueRemoves(t *testing.T) {
|
||||
sawUser := false
|
||||
for {
|
||||
var message protocol.Message
|
||||
if err := wsjson.Read(server.ctx, reloaded, &message); err != nil {
|
||||
if err := readTestFrame(server.ctx, reloaded, &message); err != nil {
|
||||
t.Fatalf("no delivery reported: %v", err)
|
||||
}
|
||||
if event := message.ChatEvent; event != nil && event.Kind == protocol.ChatEventUser && event.OperationID == "op-3" {
|
||||
@@ -259,7 +258,7 @@ func TestChatSubscribeListsQueuedPromptsAndUnqueueRemoves(t *testing.T) {
|
||||
writeChat(t, server.ctx, reloaded, protocol.Message{Type: protocol.ChatUnqueue, ID: "rm-late", ChatUnqueue: &protocol.ChatUnqueueRequest{SessionID: "ses_1", OperationID: "op-3"}})
|
||||
for {
|
||||
var message protocol.Message
|
||||
if err := wsjson.Read(server.ctx, reloaded, &message); err != nil {
|
||||
if err := readTestFrame(server.ctx, reloaded, &message); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if message.ID == "rm-late" {
|
||||
|
||||
@@ -346,6 +346,8 @@ func (h *chatHub) releaseRoomLocked(room *chatRoom) {
|
||||
h.clearCompactionLocked(room)
|
||||
delete(h.rooms, room.sessionID)
|
||||
room.cancel()
|
||||
// An unobserved session reports no turn and no background tasks.
|
||||
h.noteActivityLocked(room.sessionID)
|
||||
// ReleaseShells is a local identity-cache operation (no broker I/O).
|
||||
// Complete it before a reconnect can acquire this session's new room.
|
||||
if releaser, ok := room.broker.(chatShellReleaser); ok {
|
||||
@@ -477,6 +479,16 @@ func (h *chatHub) emitRoom(source *chatRoom, frame protocol.ChatEventFrame) {
|
||||
h.releaseRoomLocked(room)
|
||||
}
|
||||
}
|
||||
if frame.Kind == protocol.ChatEventStep || frame.Kind == protocol.ChatEventShell {
|
||||
h.noteActivityLocked(frame.SessionID)
|
||||
}
|
||||
// An unwatched room kept only for its background tasks is done once the
|
||||
// last of them exits.
|
||||
if room != nil && frame.Kind == protocol.ChatEventShell && frame.Shell != nil && frame.Shell.Status != protocol.ChatShellRunning &&
|
||||
room.watching && len(room.subs) == 0 && len(room.queue) == 0 && h.rooms[room.sessionID] == room && !h.keepUnattendedLocked(room, false, true) {
|
||||
room.watching = false
|
||||
h.releaseRoomLocked(room)
|
||||
}
|
||||
// Shell replay and live lifecycle delivery must enqueue in the same order
|
||||
// as folding. Otherwise an exit can overtake an older running replay.
|
||||
if (frame.Kind == protocol.ChatEventShell || frame.Kind == protocol.ChatEventCompaction || frame.Kind == protocol.ChatEventUsage) && !buffered {
|
||||
@@ -557,6 +569,7 @@ func (h *chatHub) replaceShellsLocked(sessionID string, shells []protocol.ChatSh
|
||||
for i := range shells {
|
||||
turn.applyShell(&shells[i])
|
||||
}
|
||||
h.noteActivityLocked(sessionID)
|
||||
}
|
||||
|
||||
// finishTurnLocked marks the room's turn finished and releases the turn runner's
|
||||
@@ -570,6 +583,7 @@ func (h *chatHub) finishTurnLocked(room *chatRoom) {
|
||||
close(room.turnDone)
|
||||
room.turnDone = nil
|
||||
}
|
||||
h.noteActivityLocked(room.sessionID)
|
||||
// A queue held behind a turn this room only observed advances now.
|
||||
signalWake(room.wake)
|
||||
}
|
||||
@@ -774,6 +788,7 @@ func (h *chatHub) nextPrompt(room *chatRoom) (*chatQueuedPrompt, chan struct{},
|
||||
room.nativeTurnID = ""
|
||||
room.turnDone = make(chan struct{})
|
||||
done := room.turnDone
|
||||
h.noteActivityLocked(room.sessionID)
|
||||
h.mu.Unlock()
|
||||
return p, done, true
|
||||
} else {
|
||||
|
||||
@@ -86,6 +86,20 @@ func handleChatSessions(ctx context.Context, request protocol.Message, broker ch
|
||||
session.Color = operator.Color
|
||||
response.ChatSessions = append(response.ChatSessions, session)
|
||||
}
|
||||
// Each row carries the hub's live activity (turn running, background
|
||||
// tasks), read once for the whole list after the broker call; later
|
||||
// changes arrive as chat_activity pushes.
|
||||
if hub != nil {
|
||||
ids := make([]string, len(response.ChatSessions))
|
||||
for i := range response.ChatSessions {
|
||||
ids[i] = response.ChatSessions[i].ID
|
||||
}
|
||||
activity := hub.activitySnapshot(ids)
|
||||
for i := range response.ChatSessions {
|
||||
row := activity[response.ChatSessions[i].ID]
|
||||
response.ChatSessions[i].Activity = &row
|
||||
}
|
||||
}
|
||||
_ = write(response)
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
"github.com/coder/websocket/wsjson"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/opencode"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
@@ -273,7 +272,7 @@ func dialShellOutputWebsocket(t *testing.T, ctx context.Context, srv *httptest.S
|
||||
func readShellOutputMessage(t *testing.T, ctx context.Context, ws *websocket.Conn) protocol.Message {
|
||||
t.Helper()
|
||||
var message protocol.Message
|
||||
if err := wsjson.Read(ctx, ws, &message); err != nil {
|
||||
if err := readTestFrame(ctx, ws, &message); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return message
|
||||
|
||||
@@ -47,7 +47,7 @@ func TestChatShellReconciliationScratchServer(t *testing.T) {
|
||||
read := func() protocol.Message {
|
||||
t.Helper()
|
||||
var m protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &m); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return m
|
||||
@@ -215,6 +215,11 @@ func TestChatShellReconciliationBarrier(t *testing.T) {
|
||||
}
|
||||
}
|
||||
sub.close()
|
||||
if status == protocol.ChatShellRunning {
|
||||
// Running background tasks keep the room observing with no
|
||||
// client (chat_unattended.go); release it to end the stream.
|
||||
releaseKeptRoom(t, h, sub.room)
|
||||
}
|
||||
shellBarrierWait(t, b.stopped)
|
||||
})
|
||||
}
|
||||
@@ -257,5 +262,20 @@ func TestChatShellReconciliationCancelReconnectBarrier(t *testing.T) {
|
||||
t.Fatalf("reconnect state = %+v", got)
|
||||
}
|
||||
newSub.close()
|
||||
releaseKeptRoom(t, h, newSub.room)
|
||||
shellBarrierWait(t, next.stopped)
|
||||
}
|
||||
|
||||
// releaseKeptRoom asserts room is still kept observing after its last client
|
||||
// left (it holds running background tasks) and releases it.
|
||||
func releaseKeptRoom(t *testing.T, h *chatHub, room *chatRoom) {
|
||||
t.Helper()
|
||||
h.mu.Lock()
|
||||
kept := h.rooms[room.sessionID] == room && room.watching
|
||||
room.watching = false
|
||||
h.releaseRoomLocked(room)
|
||||
h.mu.Unlock()
|
||||
if !kept {
|
||||
t.Fatal("a room with running background tasks was not kept observing")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -428,7 +428,7 @@ func readChatTurn(t *testing.T, ctx context.Context, conn *websocket.Conn) []pro
|
||||
var frames []protocol.ChatEventFrame
|
||||
for {
|
||||
var message protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &message); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &message); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if message.Type == protocol.Error {
|
||||
@@ -452,7 +452,7 @@ func TestChatModelsAndSwitch(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var reply protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &reply); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &reply); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if reply.Type != protocol.ChatModels || len(reply.ChatModels) != 1 || reply.ChatModels[0].ID != "m1" {
|
||||
@@ -461,7 +461,7 @@ func TestChatModelsAndSwitch(t *testing.T) {
|
||||
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatModelSelect, ID: "r2", ModelRequest: &protocol.ChatModelRequest{SessionID: "ses_1", Provider: "p1", Model: "m1"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := wsjson.Read(ctx, conn, &reply); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &reply); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if reply.Type != protocol.ChatModelSelect || reply.ModelRequest == nil || reply.ModelRequest.Model != "m1" {
|
||||
@@ -487,7 +487,7 @@ func TestChatPromptWritesAttachmentAndForwardsLocalFile(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ack protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &ack); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &ack); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ack.Type != protocol.ChatPrompt || ack.Error != "" {
|
||||
@@ -517,7 +517,7 @@ func TestChatPromptStreamsEvents(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ack protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &ack); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &ack); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ack.Type != protocol.ChatPrompt || ack.ID != "r1" {
|
||||
@@ -558,7 +558,7 @@ func TestChatPromptRejectsInvalidSession(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.Error || response.ID != "bad" {
|
||||
@@ -578,7 +578,7 @@ func TestChatPromptRejectsOversizedText(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.Error || !strings.Contains(response.Error, "64 KiB") {
|
||||
@@ -595,7 +595,7 @@ func TestChatPromptWithoutBroker(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.Error || !strings.Contains(response.Error, "not configured") {
|
||||
@@ -611,7 +611,7 @@ func TestChatMessagesResumesRetainedState(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ack protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &ack); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &ack); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
readChatTurn(t, ctx, conn)
|
||||
@@ -619,7 +619,7 @@ func TestChatMessagesResumesRetainedState(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.ChatMessages || response.ID != "r2" || response.ChatHistory == nil {
|
||||
@@ -692,7 +692,7 @@ func TestChatMessagesReturnsBrokerHistory(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.ChatMessages || response.ID != "r2" {
|
||||
@@ -735,7 +735,7 @@ func TestChatMessagesThroughRealAdapter(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.ChatMessages || response.ID != "r2" {
|
||||
@@ -758,7 +758,7 @@ func TestChatMessagesRejectsInvalidSession(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.Error || response.ID != "bad" {
|
||||
@@ -782,7 +782,7 @@ func TestChatDisconnectCancelsStream(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ack protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &ack); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &ack); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
@@ -815,7 +815,7 @@ func TestChatInterruptAndReply(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.ChatInterrupt || response.ID != "i1" {
|
||||
@@ -824,7 +824,7 @@ func TestChatInterruptAndReply(t *testing.T) {
|
||||
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatReply, ID: "p1", ChatReply: &protocol.ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: protocol.ChatDecisionOnce}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.ChatReply || response.ID != "p1" {
|
||||
@@ -909,7 +909,7 @@ func TestChatInterruptEndsRunningPrompt(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ack protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &ack); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &ack); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ack.Type != protocol.ChatPrompt || ack.ID != "r1" {
|
||||
@@ -917,7 +917,7 @@ func TestChatInterruptEndsRunningPrompt(t *testing.T) {
|
||||
}
|
||||
// The first text delta proves the turn is live and the subscription attached.
|
||||
var first protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &first); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &first); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if first.Type != protocol.ChatEvent || first.ChatEvent == nil || first.ChatEvent.Kind != protocol.ChatEventText || first.ChatEvent.Delta != "working" {
|
||||
@@ -934,7 +934,7 @@ func TestChatInterruptEndsRunningPrompt(t *testing.T) {
|
||||
var acked, ended bool
|
||||
for !acked || !ended {
|
||||
var message protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &message); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &message); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if message.Type == protocol.Error {
|
||||
@@ -974,7 +974,7 @@ func TestChatInterruptRejectsInvalidSession(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.Error || response.ID != "bad" {
|
||||
@@ -994,7 +994,7 @@ func TestChatInterruptWithoutBroker(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.Error || !strings.Contains(response.Error, "not configured") {
|
||||
@@ -1012,7 +1012,7 @@ func TestChatInterruptErrorReported(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.Error || response.ID != "i1" || !strings.Contains(response.Error, "already finished") {
|
||||
@@ -1261,7 +1261,7 @@ func TestChatPromptStreamsShellFrame(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ack protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &ack); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &ack); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
frames := readChatTurn(t, ctx, conn)
|
||||
@@ -1289,7 +1289,7 @@ func TestChatSubscribeReplaysRunningShell(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ack protocol.Message
|
||||
if err := wsjson.Read(ctx, conns[0], &ack); err != nil {
|
||||
if err := readTestFrame(ctx, conns[0], &ack); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
frames := readChatTurn(t, ctx, conns[0])
|
||||
@@ -1307,14 +1307,14 @@ func TestChatSubscribeReplaysRunningShell(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var subscription protocol.Message
|
||||
if err := wsjson.Read(ctx, conns[1], &subscription); err != nil {
|
||||
if err := readTestFrame(ctx, conns[1], &subscription); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if subscription.Type != protocol.ChatSubscribe || subscription.ID != "sub1" {
|
||||
t.Fatalf("subscribe ack = %+v", subscription)
|
||||
}
|
||||
var replayed protocol.Message
|
||||
if err := wsjson.Read(ctx, conns[1], &replayed); err != nil {
|
||||
if err := readTestFrame(ctx, conns[1], &replayed); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if replayed.Type != protocol.ChatEvent || replayed.ChatEvent == nil || replayed.ChatEvent.Kind != protocol.ChatEventShell {
|
||||
@@ -1338,14 +1338,14 @@ func TestChatSubscribeKeepsStreamWhenShellReadbackFails(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ack protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &ack); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &ack); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ack.Type != protocol.ChatSubscribe || ack.ID != "sub1" {
|
||||
t.Fatalf("subscribe ack = %+v", ack)
|
||||
}
|
||||
var limited protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &limited); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &limited); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if limited.Type != protocol.Error || limited.Error != "background shell visibility is limited" {
|
||||
@@ -1369,7 +1369,7 @@ func TestChatPromptSubscribesBeforePrompt(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ack protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &ack); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &ack); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
readChatTurn(t, ctx, conn)
|
||||
@@ -1422,7 +1422,7 @@ func realAdapterChat(t *testing.T, frames []string) []protocol.ChatEventFrame {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ack protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &ack); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &ack); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ack.Type != protocol.ChatPrompt || ack.ID != "r1" {
|
||||
@@ -1509,7 +1509,7 @@ func TestChatPromptRejectsEmptyText(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.Error || response.ID != "empty" {
|
||||
@@ -1536,7 +1536,7 @@ func TestChatCreateRecordsOperatorSession(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.ChatCreate || response.ID != "c1" || response.ChatSession == nil {
|
||||
@@ -1582,7 +1582,7 @@ func TestChatCreateEphemeralSessionIsNeverListed(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// The session is real and usable: it is created, bootstrapped and echoed.
|
||||
@@ -1858,7 +1858,7 @@ func TestChatCreateDirectoryResolution(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if tc.wantErr != "" {
|
||||
@@ -1908,7 +1908,7 @@ func TestChatDirectoriesHomeThenBoundedRecents(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.ChatDirectories || response.ID != "d1" {
|
||||
@@ -1992,7 +1992,7 @@ func TestChatSessionsListsOperatorSessionsOnly(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.ChatSessions || response.ID != "s1" {
|
||||
@@ -2028,7 +2028,7 @@ func TestChatSessionsNeverListsUntagged(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.ChatSessions || len(response.ChatSessions) != 0 {
|
||||
@@ -2061,7 +2061,7 @@ func TestChatSessionsMostRecentlyUsedFirst(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(response.ChatSessions) != 2 {
|
||||
@@ -2086,7 +2086,7 @@ func TestChatSessionsFailsClosedWithoutOwnership(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.ChatSessions || len(response.ChatSessions) != 0 {
|
||||
@@ -2119,14 +2119,14 @@ func TestChatPromptPushesPermissionAndForwardsReply(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ack protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &ack); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &ack); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ack.Type != protocol.ChatPrompt || ack.ID != "r1" {
|
||||
t.Fatalf("prompt ack = %+v", ack)
|
||||
}
|
||||
var permission protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &permission); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &permission); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if permission.Type == protocol.Error {
|
||||
@@ -2143,7 +2143,7 @@ func TestChatPromptPushesPermissionAndForwardsReply(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.ChatReply || response.ID != "p1" {
|
||||
@@ -2227,7 +2227,7 @@ func TestChatDisconnectCancelsPermissionPoller(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ack protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &ack); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &ack); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
@@ -2273,11 +2273,11 @@ func TestChatPermissionPollErrorSurfaced(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ack protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &ack); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &ack); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.Error || !strings.Contains(response.Error, "permission poll") {
|
||||
@@ -2304,7 +2304,7 @@ func TestChatReplyRejectsInvalidInput(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.Error || response.ID != "bad" {
|
||||
@@ -2324,7 +2324,7 @@ func TestChatReplyWithoutBroker(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.Error || !strings.Contains(response.Error, "not configured") {
|
||||
@@ -2346,7 +2346,7 @@ func TestChatPermissionUnsupportedBackendIsQuiet(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ack protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &ack); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &ack); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ack.Type != protocol.ChatPrompt || ack.ID != "r1" {
|
||||
@@ -2356,7 +2356,7 @@ func TestChatPermissionUnsupportedBackendIsQuiet(t *testing.T) {
|
||||
readCtx, cancel := context.WithTimeout(ctx, 300*time.Millisecond)
|
||||
defer cancel()
|
||||
var unexpected protocol.Message
|
||||
if err := wsjson.Read(readCtx, conn, &unexpected); err == nil {
|
||||
if err := readTestFrame(readCtx, conn, &unexpected); err == nil {
|
||||
t.Fatalf("unsupported permission surface must not write a frame, got %+v", unexpected)
|
||||
}
|
||||
if calls, _ := broker.permissionQuery(); calls != 1 {
|
||||
@@ -2379,7 +2379,7 @@ func TestChatAdoptRecordsEndpointSession(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.ChatAdopt || response.ID != "a1" || response.ChatSession == nil {
|
||||
@@ -2410,7 +2410,7 @@ func TestChatAdoptRecordsEndpointSession(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var listed protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &listed); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &listed); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if listed.Type != protocol.ChatSessions || len(listed.ChatSessions) != 1 || listed.ChatSessions[0].ID != "ses_existing" || listed.ChatSessions[0].Directory != "/work/existing" || listed.ChatSessions[0].Tag != protocol.ChatSessionTagOperator {
|
||||
@@ -2422,7 +2422,7 @@ func TestChatAdoptRecordsEndpointSession(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ack protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &ack); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &ack); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ack.Type != protocol.ChatPrompt || ack.ID != "p1" {
|
||||
@@ -2451,7 +2451,7 @@ func TestChatAdoptMissingIDRecordsNothing(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.Error || !strings.Contains(response.Error, "ses_missing") {
|
||||
@@ -2480,7 +2480,7 @@ func TestChatAdoptRefusesEmptyDirectory(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.Error || !strings.Contains(response.Error, "directory") {
|
||||
@@ -2516,7 +2516,7 @@ func TestChatAdoptRejectsInvalidInputBeforeBroker(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Type != protocol.Error {
|
||||
|
||||
@@ -11,15 +11,17 @@ import (
|
||||
// stream while the session has a room. A room normally lives while a client
|
||||
// watches the chat, but the operator relies on these notifications precisely
|
||||
// when no app is open. So a room whose last client leaves stays observing,
|
||||
// unattended, while its turn is in flight or it holds an unanswered question
|
||||
// or approval, and is torn down once that work is done. The same unattended
|
||||
// unattended, while its turn is in flight, it holds an unanswered question or
|
||||
// approval, or background tasks it started are still running (so the chat list
|
||||
// sees them finish), and is torn down once that work is done. The same unattended
|
||||
// observation is started at server start for sessions whose harness reports a
|
||||
// running turn, so a reply that finishes after a restart still notifies.
|
||||
//
|
||||
// Resources are bounded: at most chatUnattendedLimit rooms are kept without a
|
||||
// client (rooms holding queued prompts are not counted: they were always kept
|
||||
// until delivered), a stuck turn is released after chatUnattendedTurnLimit,
|
||||
// and an idle session is never observed.
|
||||
// until delivered), a stuck turn or a background task that never reports an
|
||||
// exit is released after chatUnattendedTurnLimit, and an idle session is never
|
||||
// observed.
|
||||
|
||||
// chatUnattendedLimit bounds the rooms kept observing with no client attached
|
||||
// and no queued prompt.
|
||||
@@ -63,13 +65,21 @@ type chatTurnProber interface {
|
||||
// keepUnattendedLocked reports whether a room with no client still has work
|
||||
// the operator must hear about: a turn in flight (the runner's, or one its
|
||||
// stream announced), an unresolved approval or question, or a harness that
|
||||
// reports its own turn running (busy, read by the caller off the hub mutex).
|
||||
// reports its own turn running (busy, read by the caller off the hub mutex),
|
||||
// or background tasks still running.
|
||||
// hold honours the start-up settle window. Queued prompts are the caller's
|
||||
// concern. Callers hold h.mu.
|
||||
func (h *chatHub) keepUnattendedLocked(room *chatRoom, busy, hold bool) bool {
|
||||
if room.running || room.observedTurn || busy || len(room.pending) > 0 || len(room.forms) > 0 {
|
||||
return true
|
||||
}
|
||||
// Running background tasks keep the stream observed so their exits are
|
||||
// seen and the chat list's indicator clears. This is bounded like a turn:
|
||||
// the unattended room limit, the chatUnattendedTurnLimit expiry, and the
|
||||
// harness itself (a Claude Code process that exits ends its tasks).
|
||||
if h.backgroundTasksLocked(room.sessionID) > 0 {
|
||||
return true
|
||||
}
|
||||
return hold && h.clock().Before(room.holdUntil)
|
||||
}
|
||||
|
||||
@@ -167,7 +177,7 @@ func (h *chatHub) reapUnattended(room *chatRoom) bool {
|
||||
return false
|
||||
}
|
||||
if expired {
|
||||
log.Printf("chat session %s: released the unattended observer after %s without the turn ending", room.sessionID, h.unattended.turnLimit)
|
||||
log.Printf("chat session %s: released the unattended observer after %s without its turn or background tasks ending", room.sessionID, h.unattended.turnLimit)
|
||||
}
|
||||
room.watching = false
|
||||
h.releaseRoomLocked(room)
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
"github.com/coder/websocket/wsjson"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
@@ -68,7 +67,7 @@ func readReply(t *testing.T, ctx context.Context, conn *websocket.Conn, id strin
|
||||
t.Helper()
|
||||
for {
|
||||
var reply protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &reply); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &reply); err != nil {
|
||||
t.Fatalf("read reply %s: %v", id, err)
|
||||
}
|
||||
if reply.ID == id {
|
||||
@@ -140,7 +139,7 @@ func TestChatSearchBudgetNeverBlocksOtherReplies(t *testing.T) {
|
||||
chatSearch(t, ctx, conn, "c1", "flaky")
|
||||
writeChat(t, ctx, conn, protocol.Message{Type: protocol.Ping, ID: "p1"})
|
||||
var first protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &first); err != nil {
|
||||
if err := readTestFrame(ctx, conn, &first); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if first.ID != "p1" {
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
"github.com/coder/websocket/wsjson"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// readTestFrame reads the next websocket frame into v like wsjson.Read, but
|
||||
// skips chat_activity pushes. Those reach every connection whenever any
|
||||
// chat's turn or background-task count changes, so tests that read one
|
||||
// connection's replies in order would otherwise see them interleaved. Tests of
|
||||
// chat_activity itself read the raw stream.
|
||||
func readTestFrame(ctx context.Context, conn *websocket.Conn, v any) error {
|
||||
for {
|
||||
var raw json.RawMessage
|
||||
if err := wsjson.Read(ctx, conn, &raw); err != nil {
|
||||
return err
|
||||
}
|
||||
var head struct {
|
||||
Type string `json:"type"`
|
||||
}
|
||||
if json.Unmarshal(raw, &head) == nil && head.Type == protocol.ChatActivityChanged {
|
||||
continue
|
||||
}
|
||||
return json.Unmarshal(raw, v)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user