Repository navigation
Conversation
|
Reviewer pass complete. I pushed commit What I verified/reproduced:
Validation run:
No Electron screenshot/video here because this is a hidden background PTY lifecycle leak rather than a visible UI state; the regression test exercises the ownership/cleanup seam directly. I did not merge and left draft status unchanged. |
|
Revalidated this bug-fix branch and moved it out of draft. Reproduction/evidence focus:
Fresh validation on head
No merge performed. |
…etion Automation dispatch spawns a hidden background tab + PTY via launchAgentBackgroundSession to run the agent, but the captured result.tabId/result.ptyId were never used after marking the result. Because a hidden tab never mounts a TerminalPane (the only renderer path that issues pty.kill), the tab and its live PTY leaked for the lifetime of the app — accumulating one orphaned PTY per run. On completion (markCompletionResult), process exit (markExitResult), and dispatch failure, reap the launched session: kill the PTY runtime-aware (closeWebRuntimeTerminal for remote-runtime handles, window.api.pty.kill for local/SSH) then closeTab for UI/store cleanup. Guards: - Only the launched background session is reaped — reused sessions belong to the user and are never touched. - Skip if the user has the tab active/open, so a visible automation tab is never yanked out from under them. Covers local, SSH, and remote-runtime PTYs on all platforms. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
6561592 to
408bc1e
Compare
📝 WalkthroughWalkthrough
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Rebased onto latest main and ready for review. Conflicts resolved, and validated locally on a clean install: |
…y-cleanup # Conflicts: # src/renderer/src/hooks/useAutomationDispatchEvents.ts
|
Ready to review this PR? Stage has broken it down into 4 individual chapters for you:
Chapters generated by Stage for commit ce0bdba on Jun 19, 2026 10:00pm UTC. |
|
This fix makes sense to me for truly background automation runs: we don’t want completed automations quietly leaving terminal sessions around forever. I’m curious how you’d expect this to feel as a user, though. If an automation creates a terminal, would you generally expect that terminal to disappear after the run finishes, or would you sometimes want it to stick around so you can inspect what happened? I’m especially thinking about cases where you opened the workspace while it was running, used Run Now almost like starting an agent session, or the run failed and the terminal output might be useful. My instinct is that successful runs that stayed fully in the background should clean themselves up, but sessions the user looked at or failed runs may be worth preserving. Curious if that matches your expectation. |
|
@nwparker this one's drifted out of mergeable again after recent main changes. Before I rebase it another round, is it still something you'd want to take? Glad to resolve conflicts if it's on the path to merge, equally glad to close it if priorities have moved. Just let me know which. |
|
@Jinwoo-H that matches my intent closely, and the PR already encodes most of it:
The one place it currently diverges from your instinct is failed runs. Cleanup fires on every terminal state: agent completion, process exit, and the dispatch The softer edge is "I looked at it earlier, then switched away." Today the guard is active-at-completion, so a tab you viewed but aren't focused on when the run ends still gets reaped. I can extend that to "preserve if ever viewed," but it needs interaction tracking I'd rather only add if the failed-run gate alone isn't enough. So two changes I can fold in:
Want both, or just (1)? |
…y-cleanup # Conflicts: # src/renderer/src/hooks/useAutomationDispatchEvents.test.ts # src/renderer/src/hooks/useAutomationDispatchEvents.ts
|
reviewing it now |
|
Live operational evidence from a recurring Orca watchdog automation confirms this PR's leak path:
One additional parity gap is worth resolving before closeout (or tracking explicitly as a follow-up): the headless Suggested acceptance evidence:
This is not the recognition/transport-close problem in #9414; it is automation ownership cleanup after a known terminal state. |
chsong1
left a comment
There was a problem hiding this comment.
Follow-up code-path audit found several ownership/lifecycle cases that should be resolved before treating this as a complete automation reaper:
reuseSession: trueonly protects an already reused candidate. If no candidate exists, the newly launched session is the future reuse seed, but it is still classified aslaunchedBackgroundSessionand reaped on completion.- The current background launch path mounts hidden automation panes through
background-terminal-worktree-mount; the PR's premise that the hidden tab never mounts a pane is stale. Explicit PTY kill followed by tab close can race/double-teardown with unmount cleanup. - The active-at-completion guard does not preserve a session the user viewed or typed into and then left before completion. Ownership takeover needs durable evidence (activation/input), not only current focus.
- Remote
closeWebRuntimeTerminal()closes the runtime terminal but does not remove the host-authoritative persisted session-tab binding. A later host snapshot can resurrect a dead tab; the hostsession.tabs.close/closeMobileSessionTab()path owns both binding removal and PTY retirement. - Headless/serve and reload-recovery completion paths remain uncovered.
- Failure/nonzero-exit cleanup removes the terminal evidence operators may need. The safest first contract is successful fresh one-shot runs only, after snapshot/result persistence.
A robust contract would make cleanup an idempotent, launch-owned disposer bound to exact tab/pane/PTY/runtime identity; call it once only for successful fresh non-reuse runs after result persistence; and refuse cleanup after reuse, user activation/input takeover, identity drift, or failure. Renderer local/SSH, remote host-authoritative, and headless paths need separate regression coverage.
The operational leak is real, but broad idle/tab cleanup is dangerous because it can delete ordinary interactive agent sessions. I would not merge the current ownership predicate without these guards or an explicitly scoped first slice.
|
Closing as the underlying change is already implemented on |
Problem
Automation dispatch (
useAutomationDispatchEvents) launches a hidden background tab + PTY vialaunchAgentBackgroundSessionto run the scheduled/Run-Now agent. The returnedresult.tabId/result.ptyIdwere only used formarkDispatchResultbookkeeping and focus restoration — they were never closed when the run finished.A hidden automation tab never mounts a
TerminalPane, and the only renderer path that issuespty.killis the pane transport teardown. SocloseTabalone wouldn't have reaped it either. The result: every automation run leaked one tab + one live PTY for the lifetime of the app (local, SSH, and remote-runtime alike).Fix
Reap the launched background session when the run reaches a terminal state — agent completion (
markCompletionResult), process exit (markExitResult), and dispatch failure (catch):closeWebRuntimeTerminal(ptyId)for remote-runtime handles (they aren't reachable viawindow.api.pty.kill— the worktree teardown path explicitly skipsremote:ids).window.api.pty.kill(ptyId)for local / SSH PTYs.store.closeTab(tabId, { recordInteraction: false })for UI/store cleanup.Guards (no regression to visible automation tabs)
launchedBackgroundSessionis only set on thelaunchAgentBackgroundSessionpath.Scope / platforms
src/renderer/src/hooks/useAutomationDispatchEvents.ts.Verification
pnpm run typecheck:web— clean.oxlinton the changed file — 0 warnings / 0 errors.vitest runonstore-session-cascades,store-cascades, andweb-runtime-session(thecloseTab/pty.killcascade + runtime terminal-close behavior this relies on) — 118/118 pass.No unit test exists for this hook (it's IPC/store-coupled and exercised via e2e); behavior was validated by typecheck + lint + the cascade tests above and by tracing the completion/exit/failure paths.
🤖 Generated with Claude Code
Risk assessment
Risk: HIGH
Historic risk metadata backfill based on the existing
risk:highlabel.Risk: high
Historic risk metadata backfill based on the existing
risk:highlabel.ELI5
Finished automations left hidden background terminal tabs open forever. Those tabs/PTYs are closed when the automation run completes.