Chat queue: wake the runner when a scheduled head is unqueued; end a stale observed turn once the harness is idle

A prompt queued behind a scheduled prompt (the app's auto-continue at a
usage limit's reset) stayed queued after that head was removed: unqueue
never woke the runner, which slept until the removed prompt's send_at.
unqueue now wakes it, and the runner re-checks a scheduled head at least
every 30 s.

Safety net: a due prompt held by a turn the room only observed (its end
lost, or a reattach watcher that gave up) is delivered once the harness
reports itself idle for a settle period and its probe confirms it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 00:56:34 +00:00
parent 18e0e565ca
commit 364e0cb3c9
7 changed files with 280 additions and 17 deletions
+72 -9
View File
@@ -206,6 +206,7 @@ func (h *chatHub) reattachSession(ctx context.Context, broker chatBroker, sessio
room.nativeTurnID = session.NativeTurnID
}
room.observedTurn = true
room.observedIdleSince = time.Time{}
h.mu.Unlock()
go h.awaitReattachedTurn(room, broker, session)
}
@@ -216,7 +217,11 @@ func (h *chatHub) reattachSession(ctx context.Context, broker chatBroker, sessio
func (h *chatHub) awaitReattachedTurn(room *chatRoom, broker chatBroker, session pipeline.ChatLiveSession) {
prober, _ := broker.(chatTurnProber)
started := h.clock()
ticker := time.NewTicker(chatReattachPoll)
poll := h.unattended.reattachPoll
if poll <= 0 {
poll = 2 * time.Second
}
ticker := time.NewTicker(poll)
defer ticker.Stop()
for {
select {
@@ -257,14 +262,7 @@ func (h *chatHub) foldEndedTurn(ctx context.Context, broker chatBroker, session
h.mu.Lock()
defer h.mu.Unlock()
if room != nil && h.rooms[id] == room && room.observedTurn {
end := protocol.ChatEventFrame{SessionID: id, Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded, TurnID: room.nativeTurnID}}
h.applyLocked(end)
h.finishTurnLocked(room)
resync := protocol.ChatEventFrame{SessionID: id, Kind: protocol.ChatEventResync}
for _, sub := range room.subs {
enqueueChatLocked(sub, protocol.Message{Type: protocol.ChatEvent, ChatEvent: &resync})
enqueueChatLocked(sub, protocol.Message{Type: protocol.ChatEvent, ChatEvent: &end})
}
h.endObservedTurnLocked(room)
if room.watching && len(room.subs) == 0 && len(room.queue) == 0 && !h.keepUnattendedLocked(room, false, false) {
room.watching = false
h.releaseRoomLocked(room)
@@ -287,6 +285,71 @@ func (h *chatHub) foldEndedTurn(ctx context.Context, broker chatBroker, session
h.startChatAttention(chatAttention{sessionID: id, reply: true, body: body, id: chatNotificationID(id, "reply", key)})
}
// endObservedTurnLocked ends a turn the room observed but whose end event it
// never received: the turn slot is released (so queued prompts are delivered)
// and attached clients re-read the authoritative history. Callers hold h.mu.
func (h *chatHub) endObservedTurnLocked(room *chatRoom) {
end := protocol.ChatEventFrame{SessionID: room.sessionID, Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded, TurnID: room.nativeTurnID}}
h.applyLocked(end)
h.finishTurnLocked(room)
resync := protocol.ChatEventFrame{SessionID: room.sessionID, Kind: protocol.ChatEventResync}
for _, sub := range room.subs {
enqueueChatLocked(sub, protocol.Message{Type: protocol.ChatEvent, ChatEvent: &resync})
enqueueChatLocked(sub, protocol.Message{Type: protocol.ChatEvent, ChatEvent: &end})
}
}
// settleStaleObservedTurn is the queue's safety net for a turn the room only
// observed (announced by the stream, or reattached after a restart) whose end
// was lost: a replayed or silent result, an end carrying another turn id, or
// a reattach watcher that gave up. The runner calls it, without h.mu, while a
// due prompt waits behind such a turn. Once the harness has reported itself
// idle (TurnRunning false) for the settle period (chatQueueStaleTurnSettle) and its own probe
// confirms no turn is in flight, the observed turn is ended and it reports
// true so the queue proceeds. A harness that cannot be probed is never
// second-guessed.
func (h *chatHub) settleStaleObservedTurn(room *chatRoom, broker chatBroker) bool {
if brokerTurnRunning(broker, room.sessionID) {
h.mu.Lock()
room.observedIdleSince = time.Time{}
h.mu.Unlock()
return false
}
h.mu.Lock()
now := h.clock()
if room.observedIdleSince.IsZero() {
room.observedIdleSince = now
}
idle := now.Sub(room.observedIdleSince)
h.mu.Unlock()
prober, ok := broker.(chatTurnProber)
settle := h.unattended.staleTurn
if settle <= 0 {
settle = 10 * time.Second
}
if !ok || idle < settle {
return false
}
probeCtx, cancel := context.WithTimeout(room.ctx, 5*time.Second)
busy, err := prober.ProbeTurn(probeCtx, room.sessionID)
cancel()
h.mu.Lock()
defer h.mu.Unlock()
if err != nil || busy || room.observedIdleSince.IsZero() {
// Busy, unknown, or a new turn started meanwhile: look again only
// after another settle period (one probe per period).
room.observedIdleSince = h.clock()
return false
}
if h.rooms[room.sessionID] != room || room.ctx.Err() != nil || !room.observedTurn || room.running {
return false
}
log.Printf("chat session %s: the harness reports no turn running for %s; ending the observed turn %q that never reported its end, so the queued prompt is delivered",
room.sessionID, idle.Round(time.Second), room.nativeTurnID)
h.endObservedTurnLocked(room)
return true
}
// lastReply returns the id and bounded text of the assistant reply that ends
// a history page (messages after the last user message), or "" when the page
// ends with the user's own message.