Skip to content

fix: close task terminals after completion - #121070

Merged
steipete merged 4 commits into
mainfrom
codex/terminal-task-owner
Aug 9, 2026
Merged

steipete merged 4 commits into
mainfrom
codex/terminal-task-owner

Conversation

@steipete

@steipete steipete commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

Related: #121055

What Problem This Solves

Fixes an issue where terminals opened by a task could remain alive after that task completed when its agent session key did not use a recognized cron, subagent, or ACP naming pattern. Those leaked PTYs kept the Gateway's active-work blocker set non-empty after otherwise successful tasks.

This follows #121055 after independent source-blind validation found that a generic CLI task still leaked both of its terminals.

Why This Change Was Made

Terminal ownership now records the exact active task ID at creation time. Task lifecycle cleanup closes only terminals carrying that task ID, while normal conversation terminals and connection-owned terminals remain persistent. Ambiguous shared-session cases stay conversation-scoped rather than guessing ownership.

The repair replaces session-key syntax inference with the task registry's authoritative ownership relationship. Production code is +34/-25 (net +9); the added lines keep task binding private while preserving the existing public terminal-manager contract.

User Impact

When a terminal task finishes, all terminals that task opened are closed and attached viewers receive terminal.exit. Unrelated normal-chat and browser-attached terminals continue running.

Evidence

  • Source-blind AWS behavior validation: 5/5 clauses passed at implementation commit de5ada41e23b (implementation rebased as 6a3faee59616): running-task retention, exact task-owned cleanup, normal-chat persistence, connection-owned persistence, viewer exit delivery, and PTY drain.
  • Focused Testbox proof: 19/19 tests passed across terminal-tool, Gateway task subscriptions, and terminal task lifecycle (tbx_01kzk9b03srt4t78d3ehrhqw40; run 31314398390).
  • Task access-boundary follow-up: 13/13 focused tests passed after routing the lookup through the approved task read seam (tbx_01kzkcfpte5rp4fk3kwkwpeyxq; run 31316755916).
  • Final compatibility-preserving shape: Plugin SDK baseline and 16/16 task-boundary/Gateway lifecycle tests passed in run 31318374042; the updated terminal-tool caller passed 8/8 in run 31318564503, and targeted formatting passed in run 31318739874.
  • The full changed gate, core/core-test typechecks, lint, and architecture guards passed before the final private-only refactor; targeted formatting and git diff --check passed on the current shape.
  • Final Codex autoreview: clean, no accepted/actionable findings, confidence 0.98.

@clawsweeper

clawsweeper Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime agents Agent runtime and tooling size: S maintainer Maintainer-authored PR labels Aug 9, 2026
@steipete
steipete force-pushed the codex/terminal-task-owner branch from 2c5d240 to 836965b Compare August 9, 2026 13:36
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. labels Aug 9, 2026
@clawsweeper

clawsweeper Bot commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge. Reviewed August 9, 2026, 10:44 AM ET / 14:44 UTC.

ClawSweeper review

What this changes

This PR replaces session-name-based terminal cleanup with private task-ID ownership so completed tasks close their own PTYs while persistent conversation and connection terminals remain open.

Merge readiness

⛔ Blocked by patch quality or review findings - 7 items remain

Keep this PR open: two prior P1 lifecycle blockers remain on the current head, so task terminals can still escape completion cleanup.

Priority: P1
Reviewed head: 58df17af210c18c7560b114d3a4e981ec1c7c385

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) The intended repair is well-scoped, but two P1 lifecycle holes mean the current patch is not ready to merge.
Proof confidence 🌊 off-meta tidepool Not applicable: This maintainer-associated PR is exempt from the external-contributor proof gate; its claimed linked runtime evidence could not be independently opened because GitHub DNS was unavailable.
Patch quality 🧂 unranked krab (1/6) 2 actionable review findings remain.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This maintainer-associated PR is exempt from the external-contributor proof gate; its claimed linked runtime evidence could not be independently opened because GitHub DNS was unavailable.
Evidence reviewed 4 items Shared-session ownership remains ambiguous: The new resolver binds a terminal only when exactly one queued or running task has the child session key; two active tasks sharing that session leave the terminal unbound even though the active run ID is available upstream.
Pending opens escape task cleanup: Completion cleanup scans registered sessions only; a task terminalizing while its backend spawn is pending can close zero sessions, then register a leaked PTY after the spawn resolves.
Current main still needs this follow-up: The merged predecessor is contained in current main, while this PR head is not; main retains session-key-pattern cleanup rather than task-ID binding.
Findings 2 actionable findings [P1] Bind ownership to the active run rather than session cardinality
[P1] Abort task-owned pending opens on task completion
Security None None.

How this fits together

Agent tasks open terminals through the terminal tool, and the Gateway observes task completion to tear down task-owned PTYs. The terminal manager also emits viewer exit events and must handle both registered and still-opening sessions.

flowchart LR
  A[Agent task run] --> B[Terminal tool]
  B --> C[Terminal manager]
  A --> D[Task registry]
  D --> E[Gateway task completion]
  E --> C
  C --> F[PTY closure and viewer exit]
Loading

Before merge

  • Bind ownership to the active run rather than session cardinality (P1) - The resolver returns no task ID whenever two queued/running tasks share a child session, so terminals from either task remain conversation-scoped and completion cannot close them. The tool construction path already has the active run ID; resolve that exact task and verify its child session instead. This repeats the earlier P1 blocker on the current head.
  • Abort task-owned pending opens on task completion (P1) - Cleanup only searches registered sessions. If a task becomes terminal while its backend spawn is pending, cleanup finds nothing and the delayed open registers an unowned live PTY afterward. Track task identity on pending opens and abort the matching open before it registers; add a deferred-spawn regression test. This repeats the earlier P1 blocker on the current head.
  • Resolve merge risk (P1) - Merging now can still leave an idle task PTY registered after a stalled open, keeping Gateway restart blockers active.
  • Resolve merge risk (P1) - Exact-head CI is still in progress; GitHub DNS also prevented independent inspection of the linked Action artifacts and filtered PR comments.
  • Improve patch quality - Pass the active run ID through terminal-tool construction and cover two active tasks sharing one child session.
  • Improve patch quality - Abort pending task-owned opens on completion and add a deferred-spawn regression test.
  • Improve patch quality - Refresh exact-head CI after the repair.

Findings

  • [P1] Bind ownership to the active run rather than session cardinality — src/agents/tools/terminal-tool.ts:81-90
  • [P1] Abort task-owned pending opens on task completion — src/gateway/terminal/session-manager.ts:395-403
Agent review details

Security

None.

PR surface

Source +9, Tests -2. Total +7 across 6 files.

View PR surface stats
Area Files Added Removed Net
Source 3 34 25 +9
Tests 3 24 26 -2
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 6 58 51 +7

Review metrics

Metric Value Why it matters
Production versus test delta production +34/-25 (net +9); tests +24/-26 (net -2) The production growth is justified by task-ownership metadata, but the two uncovered lifecycle races require regression coverage before merge.

Stored data model

Persistent data-model change detected: serialized state: src/gateway/terminal/session-manager.ts. Confirm migration or upgrade compatibility proof before merge.

Merge-risk options

Maintainer options:

  1. Repair exact-run binding and pending-open cleanup (recommended)
    Pass the active run ID into terminal ownership resolution and add task-ID metadata to pending opens so completion aborts them before registration.
  2. Pause the PR
    Do not merge if the task-run contract cannot be carried to the terminal tool without a broader lifecycle design decision.

Technical review

Best possible solution:

Bind terminals through the exact active run ID and make task completion abort matching pending opens before they can register.

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

Yes, from source: two active tasks sharing a child session leave a terminal unbound, and a task terminalizing during a deferred backend spawn registers a PTY after cleanup.

Is this the best way to solve the issue?

No: task IDs are a better boundary than session-key syntax, but the implementation must use the already-available active run ID and cover pending opens.

Full review comments:

  • [P1] Bind ownership to the active run rather than session cardinality — src/agents/tools/terminal-tool.ts:81-90
    The resolver returns no task ID whenever two queued/running tasks share a child session, so terminals from either task remain conversation-scoped and completion cannot close them. The tool construction path already has the active run ID; resolve that exact task and verify its child session instead. This repeats the earlier P1 blocker on the current head.
    Confidence: 0.99
  • [P1] Abort task-owned pending opens on task completion — src/gateway/terminal/session-manager.ts:395-403
    Cleanup only searches registered sessions. If a task becomes terminal while its backend spawn is pending, cleanup finds nothing and the delayed open registers an unowned live PTY afterward. Track task identity on pending opens and abort the matching open before it registers; add a deferred-spawn regression test. This repeats the earlier P1 blocker on the current head.
    Confidence: 0.99

Overall correctness: patch is incorrect
Overall confidence: 0.99

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

  • add rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🌊 off-meta tidepool and patch quality is 🧂 unranked krab.
  • remove rating: 🦪 silver shellfish: Current PR rating is rating: 🧂 unranked krab, so this older rating label is no longer current.

Label justifications:

  • P1: Completed tasks can still leak PTYs that hold Gateway restart blockers after otherwise successful work.
  • merge-risk: 🚨 availability: The patch changes PTY cleanup timing and can leave pending task terminals alive after completion.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🌊 off-meta tidepool and patch quality is 🧂 unranked krab.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Not applicable: This maintainer-associated PR is exempt from the external-contributor proof gate; its claimed linked runtime evidence could not be independently opened because GitHub DNS was unavailable.

Evidence

Acceptance criteria:

  • [P1] node scripts/run-vitest.mjs src/agents/tools/terminal-tool.test.ts src/gateway/terminal/session-manager.task-lifecycle.test.ts src/gateway/server-runtime-subscriptions.test.ts.
  • [P1] git diff --check.

What I checked:

Likely related people:

  • steipete: Authored the merged terminal lifecycle repair and the follow-up task-ownership work. (role: recent area contributor; confidence: high; commits: 8dad2bf0ce3e, 6a3faee59616, 58df17af210c; files: src/gateway/server-runtime-subscriptions.ts, src/gateway/terminal/session-manager.ts, src/agents/tools/terminal-tool.ts)

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-08-09T13:43:31.406Z sha 836965b66d2f9369815b434e33921c9fb9773f58 :: found issues before merge. :: [P1] Use run identity rather than session cardinality | [P1] Abort task-owned opens before their PTY registers

@steipete
steipete force-pushed the codex/terminal-task-owner branch 2 times, most recently from ccf4ea7 to 38c5bde Compare August 9, 2026 14:15
@openclaw-barnacle openclaw-barnacle Bot removed the docs Improvements or additions to documentation label Aug 9, 2026
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Aug 9, 2026
@steipete
steipete force-pushed the codex/terminal-task-owner branch from 58df17a to 4f6c5db Compare August 9, 2026 14:49
@steipete
steipete merged commit 01ca754 into main Aug 9, 2026
120 checks passed
@steipete
steipete deleted the codex/terminal-task-owner branch August 9, 2026 15:02
@steipete

steipete commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 10, 2026
* fix(gateway): bind terminals to task owners

* fix(tasks): use task access seam for terminal ownership

* refactor(gateway): keep terminal task binding private

* refactor(gateway): preserve terminal manager contract
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling gateway Gateway runtime maintainer Maintainer-authored PR merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: S status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant