Chat subscribe: queue the live-turn marker as the client attaches

TestChatFanoutToSeveralClients flaked (tablet frame 0 = step, want text):
chat_subscribe decided whether to replay a `step started` marker only after
writing its reply and attaching the stream, then sent it. A turn another
client started in between was already being fanned out live to the new
subscriber, so the marker arrived after that turn's user frame, and could
even arrive after its `step ended`, leaving the client showing a turn that
had finished. The marker is now decided and queued in the same critical
section that makes the client a fan-out target, and the new subscriber's
writer starts only after the subscribe reply is written, so the reply comes
first and the marker precedes every live frame of the turn it stands for.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 00:51:03 +00:00
parent 026942b9b7
commit 243e5e7b7b
2 changed files with 60 additions and 50 deletions
+16 -15
View File
@@ -246,17 +246,29 @@ func handleChatSubscribe(request protocol.Message, broker chatBroker, hub *chatH
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
return
}
sub := hub.attach(query.SessionID, clientID, "", broker, write)
// A turn already in flight when this client attaches has no `step started`
// frame in its future, so attaching queues the live marker for it. The
// composer's Stop control and the transcript's one activity row read it;
// the later `step ended` frame settles them normally. The harness is asked
// off the hub mutex; the room's own state is read as the client attaches.
reporter, ok := broker.(chatTurnReporter)
busy := ok && reporter.TurnRunning(query.SessionID)
sub, fresh := hub.attachSub(query.SessionID, clientID, "", broker, write, true, busy)
if sub == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "could not attach to session " + query.SessionID})
return
}
// Acknowledge before the stream is allowed to fan out: otherwise an immediately
// available retained shell can overtake the subscribe reply on this connection.
// The reply carries the prompts the server still holds for the session, so
// a reloaded client draws them as Queued without any device storage.
// available retained shell (or the live-turn marker, or a frame of a turn
// another client starts) can overtake the subscribe reply on this
// connection. The reply carries the prompts the server still holds for the
// session, so a reloaded client draws them as Queued without any device
// storage.
queue := hub.queueFrame(query.SessionID)
_ = write(protocol.Message{Type: protocol.ChatSubscribe, ID: request.ID, ChatQueue: &queue, ChatShown: hub.shownFrame(query.SessionID)})
if fresh {
go sub.run()
}
if err := hub.ensureObserving(sub.room, broker); err != nil {
sub.close()
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "could not observe session " + query.SessionID})
@@ -265,17 +277,6 @@ func handleChatSubscribe(request protocol.Message, broker chatBroker, hub *chatH
// Observe requests from CLI-originated turns and recover after reload; do
// not require the browser to send another prompt just to start polling.
hub.startRoomPoller(sub.room, broker)
// A turn already in flight when this client attaches has no `step started`
// frame in its future, so replay the live marker. The composer's Stop control
// and the transcript's one activity row read it; the later `step ended` frame
// settles them normally.
if hub.turnRunning(query.SessionID, broker) {
sub.send(protocol.Message{Type: protocol.ChatEvent, ChatEvent: &protocol.ChatEventFrame{
SessionID: query.SessionID,
Kind: protocol.ChatEventStep,
Step: &protocol.ChatStep{Status: protocol.ChatStepStarted},
}})
}
// Replay every still-unresolved approval request the room holds, not only the
// most recent one, so a client that attaches after two requests were raised
// still sees both cards.