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
+15 -1
View File
@@ -149,6 +149,10 @@ type chatRoom struct {
// 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
// observedIdleSince is when the runner, holding a due prompt behind an
// observed turn, first saw the harness report itself idle (zero while it
// reports a turn running). See settleStaleObservedTurn.
observedIdleSince time.Time
// 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.
@@ -473,6 +477,7 @@ func (h *chatHub) emitRoom(source *chatRoom, frame protocol.ChatEventFrame) {
}
if room != nil && frame.Kind == protocol.ChatEventStep && frame.Step != nil && frame.Step.Status == protocol.ChatStepStarted && !frame.Step.Intermediate {
room.observedTurn = true
room.observedIdleSince = time.Time{}
}
var priorStep *protocol.ChatStep
if frame.Kind == protocol.ChatEventStep && frame.SessionID != "" {
@@ -603,6 +608,7 @@ func (h *chatHub) finishTurnLocked(room *chatRoom) {
h.clearCompactionLocked(room)
room.running = false
room.observedTurn = false
room.observedIdleSince = time.Time{}
room.nativeTurnID = ""
if room.turnDone != nil {
close(room.turnDone)
@@ -800,9 +806,17 @@ func (h *chatHub) nextPrompt(room *chatRoom) (*chatQueuedPrompt, chan struct{},
case room.queue[0].sendAt.After(now):
// A scheduled head waits for its time; the queue is FIFO, so
// later prompts wait behind it. A timer wakes the runner then.
wait = room.queue[0].sendAt.Sub(now)
wait = min(room.queue[0].sendAt.Sub(now), chatQueueScheduleRecheck)
case room.observedTurn:
wait = chatQueueBusyPoll
if !room.running {
broker := room.broker
h.mu.Unlock()
if h.settleStaleObservedTurn(room, broker) {
continue
}
h.mu.Lock()
}
case time.Until(room.notBefore) > 0:
wait = time.Until(room.notBefore)
default: