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
+24 -9
View File
@@ -770,12 +770,14 @@ func TestChatMessagesRejectsInvalidSession(t *testing.T) {
}
}
// The stream of a session whose turn is still in flight outlives the client
// (so its reply still notifies), but no goroutine outlives the server.
func TestChatDisconnectCancelsStream(t *testing.T) {
broker := scriptedChat()
broker.block = true
broker.started = make(chan struct{})
broker.done = make(chan struct{})
ctx, _, conn := dialChat(t, broker)
ctx, cancel, conn := dialChat(t, broker)
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hi"}}); err != nil {
t.Fatal(err)
}
@@ -789,12 +791,17 @@ func TestChatDisconnectCancelsStream(t *testing.T) {
t.Fatal("stream never started")
}
conn.CloseNow()
// The server's read loop returns on disconnect, canceling the connection
// context the subscription observes; no goroutine may keep running.
// The turn is still in flight, so the session stays observed unattended.
select {
case <-broker.done:
t.Fatal("chat stream of an in-flight turn was canceled on disconnect")
case <-time.After(300 * time.Millisecond):
}
cancel()
select {
case <-broker.done:
case <-time.After(3 * time.Second):
t.Fatal("chat stream was not canceled on disconnect")
t.Fatal("chat stream was not canceled on shutdown")
}
if prompts, _, _ := broker.counts(); prompts != 1 {
t.Fatalf("prompt forwarded %d times, want 1", prompts)
@@ -2205,9 +2212,9 @@ func TestChatPermissionDedupesRepeatedPolls(t *testing.T) {
}
}
// TestChatDisconnectCancelsPermissionPoller pins that the permission poller is
// canceled on disconnect (reusing the stream's context), so it cannot leak a
// goroutine after the client goes away.
// TestChatDisconnectCancelsPermissionPoller pins that the permission poller
// shares the room's context: it survives a disconnect while the turn is in
// flight and cannot leak a goroutine past the server.
func TestChatDisconnectCancelsPermissionPoller(t *testing.T) {
broker := scriptedChat()
broker.block = true
@@ -2215,7 +2222,7 @@ func TestChatDisconnectCancelsPermissionPoller(t *testing.T) {
broker.done = make(chan struct{})
broker.permBlock = true
broker.permDone = make(chan struct{})
ctx, _, conn := dialChat(t, broker)
ctx, cancel, conn := dialChat(t, broker)
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hi"}}); err != nil {
t.Fatal(err)
}
@@ -2234,10 +2241,18 @@ func TestChatDisconnectCancelsPermissionPoller(t *testing.T) {
default:
}
conn.CloseNow()
// An in-flight turn keeps its poller (an approval it raises still
// notifies); shutting the server down cancels it.
select {
case <-broker.permDone:
t.Fatal("permission poller of an in-flight turn was canceled on disconnect")
case <-time.After(300 * time.Millisecond):
}
cancel()
select {
case <-broker.permDone:
case <-time.After(3 * time.Second):
t.Fatal("permission poller was not canceled on disconnect")
t.Fatal("permission poller was not canceled on shutdown")
}
select {
case <-broker.done: