diff --git a/app/lib/features/chat/conversation/chat_composer_controller.dart b/app/lib/features/chat/conversation/chat_composer_controller.dart index 1de63bce..83645a68 100644 --- a/app/lib/features/chat/conversation/chat_composer_controller.dart +++ b/app/lib/features/chat/conversation/chat_composer_controller.dart @@ -278,6 +278,7 @@ class ChatComposerController extends ChangeNotifier { ); if (_disposed) return false; final queued = reply['chat_queued'] == true; + final steered = reply['chat_steered'] == true; // Accepted (sent or queued) means the server holds it now: the device // copy is dropped, and a reload reads a queued prompt from the server. _forgetStored(intent.operationId); @@ -285,7 +286,7 @@ class ChatComposerController extends ChangeNotifier { _pending = null; _composerSendsAccepted++; } - deliveries.accepted(intent, queued: queued); + deliveries.accepted(intent, queued: queued, steered: steered); _queued = deliveries.isQueued(intent.operationId) || deliveries.ownQueued; onAccepted?.call(intent, queued); _settle(null); diff --git a/app/lib/features/chat/conversation/chat_deliveries.dart b/app/lib/features/chat/conversation/chat_deliveries.dart index 4da1a97d..a094a8e3 100644 --- a/app/lib/features/chat/conversation/chat_deliveries.dart +++ b/app/lib/features/chat/conversation/chat_deliveries.dart @@ -14,6 +14,9 @@ enum ChatDelivery { /// The server accepted the prompt behind the turn in flight. queued, + /// The server handed the prompt to the turn already running. + steered, + /// The server refused the prompt, or its request was lost. failed, } @@ -48,7 +51,10 @@ class ChatDeliveries extends ChangeNotifier { final operation = _operationOf(message); if (operation == null) return null; final state = _states[operation]; - if (state == ChatDelivery.sent && operation != _latestSent) return null; + if ((state == ChatDelivery.sent || state == ChatDelivery.steered) && + operation != _latestSent) { + return null; + } return state; } @@ -71,7 +77,11 @@ class ChatDeliveries extends ChangeNotifier { _set(send, ChatDelivery.sending); } - void accepted(ChatComposerSend send, {required bool queued}) { + void accepted( + ChatComposerSend send, { + required bool queued, + bool steered = false, + }) { // The server's queue report may already have moved this prompt on (its // frame and the reply race on the socket); never step it back. final current = _states[send.operationId]; @@ -82,7 +92,14 @@ class ChatDeliveries extends ChangeNotifier { return; } if (!queued) _latestSent = send.operationId; - _set(send, queued ? ChatDelivery.queued : ChatDelivery.sent); + _set( + send, + queued + ? ChatDelivery.queued + : steered + ? ChatDelivery.steered + : ChatDelivery.sent, + ); } void failed(ChatComposerSend send) { diff --git a/app/lib/features/chat/conversation/chat_delivery_indicator.dart b/app/lib/features/chat/conversation/chat_delivery_indicator.dart index ddd28dba..b11e59cb 100644 --- a/app/lib/features/chat/conversation/chat_delivery_indicator.dart +++ b/app/lib/features/chat/conversation/chat_delivery_indicator.dart @@ -8,6 +8,7 @@ String chatDeliveryLabel(ChatDelivery state) => switch (state) { ChatDelivery.sending => 'Sending…', ChatDelivery.sent => 'Sent', ChatDelivery.queued => 'Queued', + ChatDelivery.steered => 'Delivered to the running reply', ChatDelivery.failed => 'Not sent', }; @@ -78,6 +79,7 @@ class ChatDeliveryIndicator extends StatelessWidget { ChatDelivery.sent => cupertino ? CupertinoIcons.checkmark : Icons.done, ChatDelivery.queued => cupertino ? CupertinoIcons.hourglass : Icons.hourglass_empty, + ChatDelivery.steered => cupertino ? CupertinoIcons.checkmark : Icons.done, ChatDelivery.failed => cupertino ? CupertinoIcons.exclamationmark_circle : Icons.error_outline, }; diff --git a/app/test/features/chat/conversation/chat_delivery_test.dart b/app/test/features/chat/conversation/chat_delivery_test.dart index 38847ff4..7f012841 100644 --- a/app/test/features/chat/conversation/chat_delivery_test.dart +++ b/app/test/features/chat/conversation/chat_delivery_test.dart @@ -100,6 +100,29 @@ void main() { expect(_state('sent'), findsOneWidget); }); + testWidgets('a steered reply is distinct from Sent ($cupertino)', ( + tester, + ) async { + final gateway = _Gateway(); + addTearDown(gateway.close); + await _host( + tester, + gateway, + MemoryChatDraftStore(), + cupertino: cupertino, + ); + await _send(tester, 'Handle this in the running reply'); + gateway.prompts.single.complete({'chat_steered': true}); + await _pump(tester); + expect(_state('steered'), findsOneWidget); + expect(find.text('Delivered to the running reply'), findsOneWidget); + expect(_state('sent'), findsNothing); + await _send(tester, 'And this one too'); + gateway.prompts.last.complete({'chat_steered': true}); + await _pump(tester); + expect(_state('steered'), findsOneWidget); + }); + testWidgets('a queued reply shows Queued until the turn moves on ' '($cupertino)', (tester) async { final gateway = _Gateway(); diff --git a/app/tool/capture/README.md b/app/tool/capture/README.md index cff65da4..359242da 100644 --- a/app/tool/capture/README.md +++ b/app/tool/capture/README.md @@ -132,3 +132,7 @@ content kind and expects the offset to move: node tool/capture/capture.mjs --build build/probe-app --out /tmp/review/chat-wheel \ --script tool/capture/examples/chat-wheel-scroll.json ``` + +For a prompt delivered to the running reply, use `--params chats=2&steered=1`, +open `/chat/perf-chat-0`, then type and send a prompt. Without `steered=1` +the fixture retains the older-server Sent acknowledgement. diff --git a/app/tool/perf/perf_fixture.dart b/app/tool/perf/perf_fixture.dart index db16c193..125540c3 100644 --- a/app/tool/perf/perf_fixture.dart +++ b/app/tool/perf/perf_fixture.dart @@ -258,7 +258,12 @@ JsonMap? perfChatReply(JsonMap request) { case 'chat_prompt': // Accepted, as the server does; no reply follows, so a capture can // watch the sent message itself. - return {'id': id, 'type': 'chat_prompt', 'chat_queued': false}; + return { + 'id': id, + 'type': 'chat_prompt', + 'chat_queued': false, + if (_params['steered'] == '1') 'chat_steered': true, + }; case 'chat_shells': return { 'id': id, diff --git a/docs/chat-backends.md b/docs/chat-backends.md index 0ddb2008..8ea96ae2 100644 --- a/docs/chat-backends.md +++ b/docs/chat-backends.md @@ -6,6 +6,10 @@ when the `claude` CLI is installed. The selected backend is stored on the operator-session row and returned as `backend` on every `chat_sessions` row, so a restart does not have to guess from the session id. +When a `chat_prompt` is accepted during a running turn, the reply sets +`chat_steered: true` when the backend hands it to that turn; otherwise it sets +`chat_queued: true` while the prompt waits for the turn to finish. + ## Restarts keep sessions running A server restart or stop does not stop chat sessions. Only an operator action diff --git a/docs/cli.md b/docs/cli.md index abdb9c6f..dc5470d9 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -555,7 +555,8 @@ sessions pipeline runs create. The protocol types are: - `chat_prompt` may come from any client. A prompt whose session already has a turn in flight is handed to that turn at once when the harness accepts mid-turn input (Claude Code reads it from stdin at its next step; Codex takes it with - `turn/steer` pinned to the active turn) and is acknowledged as sent. Otherwise + `turn/steer` pinned to the active turn) and is acknowledged with + `chat_steered: true`. Otherwise (OpenCode, or a turn that ended meanwhile) it is written to the session's durable server-side queue (bounded, currently 8) and acknowledged with `chat_queued: true`; the agent never receives two interleaved turns. Queued diff --git a/internal/protocol/chat_test.go b/internal/protocol/chat_test.go index ec3e7dd3..197a3f0e 100644 --- a/internal/protocol/chat_test.go +++ b/internal/protocol/chat_test.go @@ -29,6 +29,7 @@ func chatSamples() []struct { {"query", ChatQuery{SessionID: "ses_1", Limit: 50}}, {"prompt", ChatPromptRequest{SessionID: "ses_1", Text: "hello", Model: "m", Agent: "build", Files: []string{"file:///tmp/a.png"}, Client: "Pixel 9 Pro XL"}}, {"prompt_queued_ack", Message{Type: ChatPrompt, ID: "a1", ChatQueued: true}}, + {"prompt_steered_ack", Message{Type: ChatPrompt, ID: "a2", ChatSteered: true}}, {"subscribe", ChatQuery{SessionID: "ses_1"}}, {"event_user", ChatEventFrame{ SessionID: "ses_1", MessageID: "usr_1", Kind: ChatEventUser, Client: "Pixel 10 Pro Fold", diff --git a/internal/protocol/message.go b/internal/protocol/message.go index 1d7306da..19a4b911 100644 --- a/internal/protocol/message.go +++ b/internal/protocol/message.go @@ -246,6 +246,9 @@ type Message struct { // accepted into the session's queue instead of being sent immediately. ChatSubscribe *ChatQuery `json:"chat_subscribe,omitempty"` ChatQueued bool `json:"chat_queued,omitempty"` + // ChatSteered means the prompt was handed to the turn already running; its + // reply is that turn's continuing output. + ChatSteered bool `json:"chat_steered,omitempty"` // ChatQueue is the session's server-held prompt queue: pushed as a // chat_queue frame when it changes and carried by the chat_subscribe and // chat_unqueue replies. diff --git a/internal/server/chat_hub.go b/internal/server/chat_hub.go index 5fff6f10..5a0e2f76 100644 --- a/internal/server/chat_hub.go +++ b/internal/server/chat_hub.go @@ -59,7 +59,7 @@ type chatHub struct { liveStore chatLiveStore // recentOps/recentOpOrder are the bounded record of accepted prompt // operation ids, so a client retry is acknowledged instead of re-sent. - recentOps map[string]struct{} + recentOps map[string]chatRecentOp recentOpOrder []string // unattended holds the unattended-observation timings (chat_unattended.go), // fixed when the hub is built. diff --git a/internal/server/chat_queue.go b/internal/server/chat_queue.go index ed3f8cae..3576759b 100644 --- a/internal/server/chat_queue.go +++ b/internal/server/chat_queue.go @@ -63,6 +63,10 @@ const ( chatAdmitRefused ) +type chatRecentOp struct { + steered bool +} + type chatStoredFile struct { URI string `json:"uri"` Name string `json:"name"` @@ -405,21 +409,21 @@ func (r *chatRoom) holdsOp(operationID string) bool { // duplicateOp reports whether operationID is a prompt this session already // accepted (queued reports it is still waiting in the queue), so a client retry // is acknowledged again instead of delivered twice. -func (h *chatHub) duplicateOp(sessionID, operationID string) (duplicate, queued bool) { +func (h *chatHub) duplicateOp(sessionID, operationID string) (duplicate, queued, steered bool) { if operationID == "" { - return false, false + return false, false, false } h.mu.Lock() defer h.mu.Unlock() if room := h.rooms[sessionID]; room != nil { for _, p := range room.queue { if p.operationID == operationID { - return true, p.acked + return true, p.acked, false } } } - _, seen := h.recentOps[sessionID+"\x00"+operationID] - return seen, false + recent, seen := h.recentOps[sessionID+"\x00"+operationID] + return seen, false, recent.steered } func (h *chatHub) rememberOp(sessionID, operationID string) { @@ -434,12 +438,12 @@ func (h *chatHub) rememberOpLocked(sessionID, operationID string) { } key := sessionID + "\x00" + operationID if h.recentOps == nil { - h.recentOps = map[string]struct{}{} + h.recentOps = map[string]chatRecentOp{} } if _, ok := h.recentOps[key]; ok { return } - h.recentOps[key] = struct{}{} + h.recentOps[key] = chatRecentOp{} h.recentOpOrder = append(h.recentOpOrder, key) for len(h.recentOpOrder) > chatRecentOpsLimit { delete(h.recentOps, h.recentOpOrder[0]) @@ -447,6 +451,16 @@ func (h *chatHub) rememberOpLocked(sessionID, operationID string) { } } +func (h *chatHub) markOpSteered(sessionID, operationID string) { + h.mu.Lock() + defer h.mu.Unlock() + key := sessionID + "\x00" + operationID + if recent, ok := h.recentOps[key]; ok { + recent.steered = true + h.recentOps[key] = recent + } +} + // forgetOp lets a prompt that never reached the harness be retried. func (h *chatHub) forgetOp(sessionID, operationID string) { h.mu.Lock() diff --git a/internal/server/chat_queue_test.go b/internal/server/chat_queue_test.go index 246f673f..9552156b 100644 --- a/internal/server/chat_queue_test.go +++ b/internal/server/chat_queue_test.go @@ -117,7 +117,7 @@ func TestChatPromptDuringTurnIsSteeredImmediately(t *testing.T) { t.Fatalf("first prompt queued: %+v", ack) } waitFor(t, "first prompt", func() bool { return broker.promptCount() == 1 }) - if ack := sendPrompt(t, server.ctx, conn, "p2", "two", "op-2"); ack.ChatQueued { + if ack := sendPrompt(t, server.ctx, conn, "p2", "two", "op-2"); ack.ChatQueued || !ack.ChatSteered { t.Fatalf("mid-turn prompt was queued instead of forwarded: %+v", ack) } broker.steerMu.Lock() @@ -130,7 +130,7 @@ func TestChatPromptDuringTurnIsSteeredImmediately(t *testing.T) { t.Fatalf("steered prompt was stored: %v", ops) } // A retry of the same operation is acknowledged, never forwarded again. - if ack := sendPrompt(t, server.ctx, conn, "p2-retry", "two", "op-2"); ack.ChatQueued { + if ack := sendPrompt(t, server.ctx, conn, "p2-retry", "two", "op-2"); ack.ChatQueued || !ack.ChatSteered { t.Fatalf("retry ack = %+v", ack) } broker.steerMu.Lock() diff --git a/internal/server/chat_stream.go b/internal/server/chat_stream.go index 43363c3c..223f752a 100644 --- a/internal/server/chat_stream.go +++ b/internal/server/chat_stream.go @@ -64,11 +64,11 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat // Every server-held prompt is addressable (chat_unqueue) and unique in // its session's durable queue, so mint an id for a client that sent none. operationID = "srv-" + strings.TrimPrefix(chatClientToken(), "conn-") - } else if duplicate, queued := hub.duplicateOp(prompt.SessionID, operationID); duplicate { + } else if duplicate, queued, steered := hub.duplicateOp(prompt.SessionID, operationID); duplicate { // A retry of a prompt this session already accepted: acknowledge it the // same way again instead of delivering it a second time. hub.attach(prompt.SessionID, clientID, sanitizeChatClientLabel(prompt.Client), broker, write) - _ = write(protocol.Message{Type: protocol.ChatPrompt, ID: request.ID, ChatQueued: queued}) + _ = write(protocol.Message{Type: protocol.ChatPrompt, ID: request.ID, ChatQueued: queued, ChatSteered: steered}) return } attachmentFiles, attachmentParts, err := prepareChatAttachments(prompt.SessionID, prompt.Attachments) @@ -130,9 +130,10 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat // Publish the user turn to every other client; the sender already has it. hub.broadcastUser(userFrame, clientID) if admission == chatAdmitSteered { - // The harness took the message into its running turn: it is sent, not - // queued, and the turn already in flight carries its reply. - _ = write(protocol.Message{Type: protocol.ChatPrompt, ID: request.ID}) + hub.markOpSteered(sessionID, operationID) + // The harness took the message into its running turn: it is not queued, + // and the turn already in flight carries its reply. + _ = write(protocol.Message{Type: protocol.ChatPrompt, ID: request.ID, ChatSteered: true}) } if queued { // The client that sent it is told the prompt is waiting for the in-flight