Keep observing chats with an in-flight turn after the last client leaves

Chat attention notifications (questions, approvals, finished replies) come
from a session's event stream, which the hub only read while a room existed;
detach destroyed the room when the last client left, so a turn that finished
or asked with the app closed notified nobody.

A room whose last client leaves now stays observing, unattended, while its
turn is in flight or it holds an unanswered question/approval, and is torn
down when the turn ends (or after 6h for a stuck turn); at most 16 such rooms.
At server start, recently used open sessions whose harness reports a running
turn (OpenCode /api/session/active, Codex thread/turns/list) are observed
again so their reply still notifies. Settings, dedupe and app_notifications
recording are unchanged.
This commit is contained in:
2026-11-02 16:07:42 +00:00
parent 467f3eafa7
commit 80022ea41a
13 changed files with 820 additions and 25 deletions
+34 -12
View File
@@ -167,6 +167,14 @@ type chatRoom struct {
// notBefore holds back a recovered queue until the restarted stream had a
// chance to report a turn the harness is still running.
notBefore time.Time
// watching is set while the unattended watcher (chat_unattended.go) owns
// this room's lifetime: the room has no client but is kept observing so a
// finished turn or a question still notifies. unattendedSince bounds how
// long a stuck turn may hold it; holdUntil keeps a room recovered at start
// alive until its stream has had a chance to report the running turn.
watching bool
unattendedSince time.Time
holdUntil time.Time
}
func newChatRoom(base context.Context, sessionID string) *chatRoom {
@@ -309,11 +317,13 @@ func (h *chatHub) attach(sessionID, clientID, label string, broker chatBroker, w
}
// detach drops one subscription. The room (and its broker stream and queue) is
// released once its last client leaves and it has no prompt left to deliver,
// so a session with no watchers holds no goroutine. A room holding queued
// released once its last client leaves and it has nothing left to do, so an
// idle session with no watchers holds no goroutine. A room holding queued
// prompts stays (with its stream) until its runner has delivered them: the
// queue belongs to the session, not to the clients watching it. A running turn
// with nothing queued behind it needs no room; the harness finishes it alone.
// queue belongs to the session, not to the clients watching it. A room whose
// turn is still in flight, or which holds an unanswered question or approval,
// also stays (bounded, see chat_unattended.go) so the operator is still
// notified when the turn finishes or the agent asks while no app is open.
func (h *chatHub) detach(sessionID string, sub *chatSub) {
h.mu.Lock()
room := h.rooms[sessionID]
@@ -322,8 +332,8 @@ func (h *chatHub) detach(sessionID string, sub *chatSub) {
return
}
delete(room.subs, sub.clientID)
if len(room.subs) == 0 && len(room.queue) == 0 {
h.releaseRoomLocked(room)
if len(room.subs) == 0 {
h.leaveUnattendedLocked(room)
}
h.mu.Unlock()
}
@@ -461,6 +471,11 @@ 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.finishTurnLocked(room)
// An unwatched room kept only for this turn is done once it ends (its
// reply attention was noted above).
if room.watching && len(room.subs) == 0 && len(room.queue) == 0 && !h.keepUnattendedLocked(room, false, false) {
h.releaseRoomLocked(room)
}
}
// Shell replay and live lifecycle delivery must enqueue in the same order
// as folding. Otherwise an exit can overtake an older running replay.
@@ -561,10 +576,12 @@ func (h *chatHub) finishTurnLocked(room *chatRoom) {
// turnInFlight reports whether sessionID has a turn in flight, so chat_close
// knows whether to stop one through the broker interrupt before it records the
// closed state. A room exists only while a client is watching the session, and
// running is set by the turn runner for the whole turn (a prompt queued behind a
// closed state. A room exists only while a client is watching the session or
// it still has work, and running is set by the turn runner for the whole turn (a prompt queued behind a
// live turn leaves it set), so an idle or unwatched session reports false and no
// interrupt is sent. It is one read under the hub mutex and mutates nothing.
// interrupt is sent. (A room also outlives its last client while its turn is in
// flight, see chat_unattended.go.) It is one read under the hub mutex and
// mutates nothing.
func (h *chatHub) turnInFlight(sessionID string) bool {
h.mu.Lock()
defer h.mu.Unlock()
@@ -709,10 +726,15 @@ func (h *chatHub) runRoom(room *chatRoom) {
}
h.runTurn(room, p, done)
// The last client may have left while this turn ran; an unwatched room
// stays only while it still has prompts to deliver.
// stays only while it still has prompts to deliver or something the
// operator must be told about.
h.mu.Lock()
if len(room.subs) == 0 && !room.running && len(room.queue) == 0 {
h.releaseRoomLocked(room)
if len(room.subs) == 0 && len(room.queue) == 0 {
if h.keepUnattendedLocked(room, false, true) {
h.watchUnattendedLocked(room)
} else {
h.releaseRoomLocked(room)
}
}
h.mu.Unlock()
}