Skip to content

fix(gateway): stop session terminals when resetting an Incognito session - #159207

Merged
steipete merged 1 commit into
mainfrom
fix/live2-incognito-reset-terminal
Sep 27, 2026
Merged

steipete merged 1 commit into
mainfrom
fix/live2-incognito-reset-terminal

Conversation

@steipete

@steipete steipete commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Maintainer decision: @steipete approved landing this bounded terminal fix now; the reset/delete lifecycle convergence is tracked in #159304.

What Problem This Solves

Incognito sessions promise that nothing outlives the conversation. Calling sessions.reset on an Incognito session deletes it (row, transcript, delivery artifacts), but a terminal opened for that session kept running: the PTY process stayed alive and the existing connection could still send input to it after the session was gone. Delete and archive already stop session-bound terminals; only the Incognito reset branch skipped that.

Why This Change Was Made

The Incognito branch of performGatewaySessionReset (src/gateway/session-reset-service.ts) went straight to deleteSessionEntryLifecycle without the terminal drain that sessions.delete/archive run through sessions-lifecycle-drain.ts. It now fences the exact session's terminals (beginAgentSessionDrain for the session key, session id, and agent), waits for them to exit within SESSION_WORK_ADMISSION_DRAIN_TIMEOUT_MS, then runs the before_reset plugin hook and the unchanged deletion, and releases the drain on every path. If terminals do not drain in time, the reset fails with a typed UNAVAILABLE error and the session is kept. The sequence lives in src/gateway/session-reset-incognito.ts because session-reset-service.ts is at its line-cap ratchet.

Design question for the maintainer

We tried routing Incognito reset through the canonical delete drain instead. It breaks an existing reset contract: that drain clears queues before reset's internal hook can revoke caller authority (one ordering test fails). While comparing the two paths, the Incognito reset turned out to differ from delete in more than terminals:

Side effect sessions.delete Existing Incognito reset
Lifecycle fence, admission interruption Yes Yes
Gateway controller cancellation, partial persistence, settlement Full drain Missing equivalent drain
Queues, reply/embedded runs Full cancellation and settlement Cleanup; incomplete reply settlement
Worker inference and turn-claim drain Explicit Admission interruption only
Exact-owner terminals Canonical drain Parallel terminal-only drain
Subagents, MCP/provider resources, browser tabs, caches Yes Yes
Persistent plugin cleanup Included Explicitly skipped
ACP and harness cleanup Delete reasons Reset reasons
Worker placement Reclaim; retire after deletion Reject unsafe placement; retire before cleanup
Row, transcript, delivery-artifact deletion Yes; no Incognito archive Same storage owner/options
Channel unbinding, watchers/state purge, shutdown tracking Yes Yes
Physical worktree removal Yes Missing; workspace only settled
Hooks and notifications Delete hooks; keyed and broad notification Reset hooks, before_reset, keyed notification

Ordinary (non-Incognito) reset keeps its session id, and a control test confirms its terminal stays reachable through the same owner, so no orphan was demonstrated there. Converging Incognito reset with delete (full controller/worker drains, worktree removal) is a lifecycle design change, tracked in #159304.

User Impact

Resetting an Incognito session now also stops its terminals, instead of leaving a live shell attached to a deleted session.

Evidence

  • src/gateway/server.sessions.archive-terminal.test.ts: new Incognito reset case fails on main (oldPty.killed stays false) and passes; archive control unchanged.
  • On a Blacksmith Testbox via Crabbox: 136 tests across 11 reset, archive, Incognito-create, and terminal session-manager suites passed; node scripts/check-changed.mjs passed.
  • Codex autoreview: scoped-clean ("patch is correct", 0.96).
  • Terminals are driven through a controllable PTY backend, not a live OS shell.

Production LOC: +90 / −29 (includes moving the Incognito deletion sequence into its own file for the line-cap ratchet). Tests: +112 / −66. Docs: +2.

@clawsweeper

clawsweeper Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@steipete
steipete marked this pull request as ready for review September 26, 2026 21:57
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime size: M labels Sep 26, 2026
@clawsweeper clawsweeper Bot added P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Sep 26, 2026
@clawsweeper

clawsweeper Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 26, 2026, 9:38 PM ET / September 27, 2026, 01:38 UTC (Revision 2).

ClawSweeper review

What this changes

The branch drains an Incognito session’s conversation-owned terminals before deleting its state, adds a Gateway RPC regression test, and documents the behavior.

Merge readiness

✅ Ready for maintainer review

Current main still deletes Incognito sessions without stopping their session-owned terminals, so this PR remains useful. The repository admin has approved the bounded repair; broader lifecycle convergence is tracked separately.

Priority: P1
Reviewed head: 87933baccba5e9a796abe1ba603cf5bd1ca012aa

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) The focused patch and RPC regression support a normal good review rating, with no identified correctness or security finding.
Proof confidence 🌊 off-meta tidepool Not applicable: The verified repository admin-authored PR is exempt from the external contributor proof gate. Its supplementary direct-RPC test uses the production terminal manager with a controllable PTY and checks closure and write denial, but does not show a live OS shell or the timeout path; no stored-data contract changes.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The verified repository admin-authored PR is exempt from the external contributor proof gate. Its supplementary direct-RPC test uses the production terminal manager with a controllable PTY and checks closure and write denial, but does not show a live OS shell or the timeout path; no stored-data contract changes.
Evidence reviewed 9 items Current main lacks terminal cleanup: The Incognito reset branch calls session deletion without draining session-owned terminals.
Introduced terminal drain: The helper selects the exact session key, session ID, and agent, waits for terminal exit, and returns an error before deletion on timeout. Its finally block releases the drain.
Production request context: The Gateway request wrapper supplies the runtime context used by the new helper to reach the terminal manager.
Findings None None.
Security None None.

How this fits together

The Gateway receives session reset requests and coordinates cleanup with the terminal manager and session store. A successful Incognito reset deletes the session and notifies clients.

flowchart LR
  A[Incognito reset request] --> B[Gateway reset lifecycle]
  B --> C[Exact session terminal drain]
  C --> D{Terminals exited?}
  D -- Yes --> E[Delete session state]
  D -- No --> F[Keep session and report error]
  E --> G[Notify clients]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +61, Tests +46, Docs +2. Total +109 across 4 files.

View PR surface stats
Area Files Added Removed Net
Source 2 90 29 +61
Tests 1 112 66 +46
Docs 1 2 0 +2
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 4 204 95 +109

Review metrics

Metric Value Why it matters
Regression test cost 2 cases; 21.38s single-worker test time reported The direct-RPC lifecycle test exceeds the repository’s five-second target for a per-PR test file.

Technical review

Best possible solution:

Keep the exact-session terminal drain in the reset lifecycle and handle the wider controller, worker, and worktree teardown in #159304.

Do we have a high-confidence way to reproduce the issue?

Yes. Open an Incognito session’s terminal and call sessions.reset; current-main source deletes the session without a terminal drain. The PR reports a failing baseline test, but this read-only review did not run it.

Is this the best way to solve the issue?

Yes for the terminal leak. The patch uses the existing exact-owner terminal manager and preserves reset hook ordering; broader teardown has its own tracked issue.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 9c2335a2d07e.

Labels

Label changes:

No label changes.

Label justifications:

  • P1: Current main can leave a live shell associated with an Incognito session after reset deletes that session.
  • merge-risk: 🚨 compatibility: A terminal that does not exit within 15 seconds can now make reset fail and require a retry; the verified repository admin accepted this bounded behavior in the PR body.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The verified repository admin-authored PR is exempt from the external contributor proof gate. Its supplementary direct-RPC test uses the production terminal manager with a controllable PTY and checks closure and write denial, but does not show a live OS shell or the timeout path; no stored-data contract changes.

Evidence

What I checked:

  • Current main lacks terminal cleanup: The Incognito reset branch calls session deletion without draining session-owned terminals. (src/gateway/session-reset-service.ts:1154, 9c2335a2d07e)
  • Introduced terminal drain: The helper selects the exact session key, session ID, and agent, waits for terminal exit, and returns an error before deletion on timeout. Its finally block releases the drain. (src/gateway/session-reset-incognito.ts:22, 87933baccba5)
  • Production request context: The Gateway request wrapper supplies the runtime context used by the new helper to reach the terminal manager. (src/gateway/server-methods.ts:522, 87933baccba5)
  • Terminal ownership boundary: The terminal manager aborts matching pending opens and closes matching live terminals; owner matching requires the session key, session ID, and agent ID. (src/gateway/terminal/session-manager.ts:440, 87933baccba5)
  • Gateway regression coverage: The direct-RPC test uses the production terminal manager with a controllable PTY and checks closure, denied writes, row retention during drain, and isolation from replacement and unrelated terminals. The PR reports that the Incognito case failed on unmodified main; this review did not execute it. (src/gateway/server.sessions.archive-terminal.test.ts:29, 87933baccba5)
  • Accepted bounded scope: The captured PR body says steipete approved landing the terminal fix now, describes the 15-second retryable timeout, and links broader teardown work. The live repository permission check identifies steipete as an admin. The prior review’s stuck-PTY test remains an optional rank-up move; the helper’s return-before-delete and finally paths establish its stated outcome in source.

Likely related people:

  • Peter Steinberger: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • Dallin Romney: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • Vincent Koc: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (1 earlier review cycle)
  • reviewed 2026-09-26T22:04:44.173Z sha dc4c1b3 :: blocked before merge. :: none

Incognito reset deleted ephemeral session state without draining its conversation-owned terminals. Reuse the exact-session terminal fence and exit drain before deletion, preserve the row on timeout, and release the fence on every outcome. Extract the deletion phase to respect the reset service line-cap ratchet and document the terminal lifetime.

Validation on Blacksmith Testbox: regression failed on unmodified 9883b72 because the PTY was not killed; final 11 owner suites passed 136 tests. Single-worker regression file passed 2 tests in 21.38s (23.61s wrapper wall). check-changed passed. Independent review found no actionable P0-P2 issues.
@steipete
steipete force-pushed the fix/live2-incognito-reset-terminal branch from dc4c1b3 to 87933ba Compare September 27, 2026 01:32
@steipete
steipete merged commit 247258e into main Sep 27, 2026
197 checks passed
@steipete
steipete deleted the fix/live2-incognito-reset-terminal branch September 27, 2026 02:08
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 27, 2026
…aw#159207)

Incognito reset deleted ephemeral session state without draining its conversation-owned terminals. Reuse the exact-session terminal fence and exit drain before deletion, preserve the row on timeout, and release the fence on every outcome. Extract the deletion phase to respect the reset service line-cap ratchet and document the terminal lifetime.

Validation on Blacksmith Testbox: regression failed on unmodified 73cbb5d because the PTY was not killed; final 11 owner suites passed 136 tests. Single-worker regression file passed 2 tests in 21.38s (23.61s wrapper wall). check-changed passed. Independent review found no actionable P0-P2 issues.

Co-authored-by: Peter Steinberger <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation gateway Gateway runtime merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: M status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant