Skip to content

fix(server): detach the attached runtime when a --listen TUI tab closes - #4930

Draft
pcbeingused333 wants to merge 3 commits into
docker:mainfrom
pcbeingused333:fix/listen-tab-close-detach
Draft

pcbeingused333 wants to merge 3 commits into
docker:mainfrom
pcbeingused333:fix/listen-tab-close-detach

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

Fixes #4909

Root cause. Under --listen, every TUI tab's session is attached to the serving SessionManager via AttachRuntime, but only finishSessionDeletion ever removed that state. Closing a tab ran the spawner's cleanup (stop toolsets) and nothing else, so the runtime, session, event log and follow-up injector stayed retained and the session kept being listed as active for the life of the process.

Fix. Add SessionManager.DetachRuntime, the inverse of AttachRuntime: it cancels the attach context and drops the runtime registration, event log, follow-up injector/keys and pending safety defaults, without deleting the persisted session (so it can still be resumed). The spawner's cleanup calls it when f.listenSM != nil.

Test. TestSessionManager_DetachRuntimeReleasesAttachedSession: Attach → Detach → GetActiveSessions is empty, injector released, stored session intact, second detach is a no-op. Fails before the change, passes after; go test ./pkg/server, go vet and gofmt are clean.

This change was prepared with the help of an AI coding assistant (Claude) and submitted by a human contributor.

Sessions spawned from TUI tabs under --listen are attached to the serving
SessionManager, but nothing released them when the tab was closed, so the
runtime, session, event log and follow-up injector stayed retained and the
session kept showing up as active until the process exited.

Add SessionManager.DetachRuntime (the inverse of AttachRuntime, keeping the
persisted session) and call it from the spawner's cleanup.

Signed-off-by: Alex Castillo <alex.castillog33@gmail.com>
@pcbeingused333
pcbeingused333 requested a review from a team as a code owner November 14, 2026 01:41
@aheritier aheritier added area/runtime Runtime engine, agent loop execution, tool dispatch, loop detection kind/fix PR fixes a bug (maps to fix:). Use on PRs only. labels Nov 14, 2026

@aheritier aheritier left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spawned tabs now leave the active-session list without deleting the stored session, but the fix does not yet cover the full lifecycle in #4909. #4931 addresses the same issue and is a useful comparison for the detach wiring.

Validation: On this commit, the server, TUI, supervisor, and root command package tests passed. Server and supervisor race tests and targeted go vet also passed. Two focused regression tests reproduced the server findings below. The initial-tab finding was verified by tracing the cleanup callbacks, not by an interactive TUI run.

Prior threads: No reviews or review threads; there is no prior human-reviewed commit to compare against.

Comment thread cmd/root/run.go
// (see recallCoordinatorOpt); release it when the tab goes away so
// the runtime, event log and injector are not retained.
if f.listenSM != nil {
f.listenSM.DetachRuntime(newSess.ID)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[blocking] This only detaches tabs created by createSessionSpawner. The initial tab is attached separately by startSessionCoordinator (run_listen.go:75–80), but runOrExec still passes the backend's cleanup directly to the TUI (run.go:559–616). Open a second tab under --listen and close the initial one: the supervisor removes the tab, but its runtime and follow-up injector remain registered and /api/sessions still lists it as active. Wire detachment into the initial tab's cleanup too, and cover both paths with a tab-close regression test.

if rs.cancel != nil {
rs.cancel()
}
sm.runtimeSessions.Delete(sessionID)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[blocking] Removing the runtime from the manager does not undo the recall handler installed by AttachRuntime (session_manager.go:459–460). An embedder that keeps the detached runtime still holds a callback into this manager; an idle recall is routed back to the detached session and rejected instead of falling back to the runtime's steer queue (runtime.go:1492–1514). A focused test with a runtime that implements SetRecallHandler confirmed the handler remains set after detach. Clear the handler installed for that attachment.

Comment thread pkg/server/session_manager.go Outdated
rs.cancel()
}
sm.runtimeSessions.Delete(sessionID)
sm.dropEventLog(sessionID)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[blocking] dropEventLog permanently tombstones the session ID (session_manager.go:329–341). The stored session survives, but reattaching that same ID succeeds while RegisterEventSource silently refuses to register its new source (session_manager.go:256–261), so /api/sessions/:id/events returns 404. A focused attach → detach → reload → reattach test reproduced the rejected registration. Tear down the attachment's event log without marking the session deleted, and add a same-ID reattachment test.

@aheritier
aheritier marked this pull request as draft November 21, 2026 06:41
Clear the recall handler AttachRuntime installed, tear down the event log
without tombstoning the session ID so it can be reattached, and detach the
initial --listen tab when it is closed, not only spawned tabs.

Signed-off-by: Alex Castillo <alex.castillog33@gmail.com>
@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review. Pushed b59a330 (on top of a merge of current main), addressing the three blocking points:

  • Initial tab: runOrExec now wraps the TUI cleanup with detachOnCleanup, so closing the initial --listen tab detaches it like spawned tabs. Covered by TestDetachOnCleanupReleasesListenSession.
  • Recall handler: DetachRuntime now clears the handler installed by AttachRuntime (TestSessionManager_DetachRuntimeClearsRecallHandler).
  • Event log tombstone: detach now uses a new releaseEventLog that tears down the log without tombstoning the ID, so the same session ID can be reattached and register a new source (TestSessionManager_DetachRuntimeAllowsReattach).

go test ./pkg/server/ -race passes; in ./cmd/root/, TestBannerAnimationCapability fails identically without our change, everything else passes.

This comment was drafted with the help of an AI assistant (Claude Code) and the change was tested locally.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/runtime Runtime engine, agent loop execution, tool dispatch, loop detection kind/fix PR fixes a bug (maps to fix:). Use on PRs only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Closing a TUI tab leaks its runtime, session and context in --listen mode

2 participants