Report a busy chat on subscribe after a reload

The room that records a server-run turn is torn down when its last client
leaves, so a page reload re-subscribed to a new room that no longer knew the
turn was running and never replayed the step-started marker. Claude Code does
not re-announce a running turn to a new stream either, so the activity row and
Stop control vanished until the next streamed delta.

chat_subscribe now also replays the marker when the room's stream announced a
turn (CLI-originated or re-announced on attach) or when the broker reports its
own turn in flight through the optional chatTurnReporter (Claude Code reads its
in-process running flag; the chat manager routes it without I/O).
This commit is contained in:
2026-10-31 22:09:11 +00:00
parent 063c1144a1
commit 429e175b95
7 changed files with 288 additions and 5 deletions
+21 -4
View File
@@ -127,6 +127,11 @@ type chatRoom struct {
queue []*chatQueuedPrompt
running bool
streaming bool
// observedTurn is set by an authoritative step-started frame this room's
// stream delivered (a CLI-originated turn, or one a harness re-announced on
// attach) and cleared when the turn ends, so a later subscriber is told the
// turn is running even though no prompt of this room started it.
observedTurn bool
// observing is non-nil while one caller is attaching the shared broker
// stream. It prevents concurrent subscribers from opening duplicate global
// event streams before streaming can be published.
@@ -407,6 +412,9 @@ func (h *chatHub) emitRoom(source *chatRoom, frame protocol.ChatEventFrame) {
if room != nil && frame.Kind == protocol.ChatEventStep && frame.Step != nil && frame.Step.Status == protocol.ChatStepEnded && !frame.Step.Intermediate {
h.clearCompactionLocked(room)
}
if room != nil && frame.Kind == protocol.ChatEventStep && frame.Step != nil && frame.Step.Status == protocol.ChatStepStarted && !frame.Step.Intermediate {
room.observedTurn = true
}
var priorStep *protocol.ChatStep
if frame.Kind == protocol.ChatEventStep && frame.SessionID != "" {
priorStep = h.turnLocked(frame.SessionID).step
@@ -516,6 +524,7 @@ func (h *chatHub) replaceShellsLocked(sessionID string, shells []protocol.ChatSh
func (h *chatHub) finishTurnLocked(room *chatRoom) {
h.clearCompactionLocked(room)
room.running = false
room.observedTurn = false
room.nativeTurnID = ""
if room.turnDone != nil {
close(room.turnDone)
@@ -596,12 +605,20 @@ func (h *chatHub) submitPrompt(sessionID string, p *chatQueuedPrompt) (queued bo
// turnRunning reports whether the session has a turn in flight. A client that
// subscribes after a turn started (for example the operator's browser while the
// CLI harness is working) needs this to show Stop and the live activity line;
// the room's own frames only cover events after the subscription.
func (h *chatHub) turnRunning(sessionID string) bool {
// the room's own frames only cover events after the subscription. A turn the
// room's stream announced counts too, and so does a turn the broker itself
// reports in flight: the room is rebuilt when its last client reloads, so its
// own record of an earlier prompt does not survive the reload.
func (h *chatHub) turnRunning(sessionID string, broker chatBroker) bool {
h.mu.Lock()
defer h.mu.Unlock()
room := h.rooms[sessionID]
return room != nil && room.running
running := room != nil && (room.running || room.observedTurn)
h.mu.Unlock()
if running {
return true
}
reporter, ok := broker.(chatTurnReporter)
return ok && reporter.TurnRunning(sessionID)
}
// pendingFrames returns a copy of the room's unresolved approval requests so a