Skip to content

fix(terminal): don't resume an agent whose spawned PTY is still live - #33380

Merged
nwparker merged 1 commit into
mainfrom
nwparker/agint-paired-duplicate-agent-resume
Oct 25, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/agint-paired-duplicate-agent-resume

Conversation

@nwparker

@nwparker nwparker commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​68 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​68
Prod 10 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​68 $\color{#cf222e}{\Huge{\mathbf{−}}}$​27 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​41

ELI5

When you open a workspace, or open it from your phone, Orca restarts agents it thinks were interrupted. If the desktop had moved a running Claude to a new tab, or a paired Orca had started it, the desktop could no longer tell that the agent was still running. It then started a second claude --resume on the same conversation, and both copies wrote to one transcript. With this change, the terminal daemon remembers which pane each terminal was started for. The desktop can now see that the agent is still running and leaves it alone.

Root cause

A sleeping-agent record is keyed by the pane the agent reports from. That is the ORCA_PANE_KEY exported into the PTY when it was spawned, and Claude's hooks send it (src/main/claude/hook-script.ts). The activation gate (liveSleepingAgentClaims) could link a record to a live PTY only through this renderer's own state: the record pane's layout leaf, or the record tab's ptyIdsByTabId. When that state no longer names the PTY, the record looks dead:

In both cases a fresh claude launch has no agent-session owner claim, so the census lists the PTY as agentOwnership: 'absent', and the blocked safety net does not fire either. The sweep then resumes the session into another tab. The phone wake has gone through this same gate since #29431, so both entry points produced the duplicate.

What changed

  • The daemon records the spawn ORCA_PANE_KEY per Session in a side table in terminal-host-session-listing.ts, because session.ts is at its line cap. It lists the key as optional paneKey, and reattaching from another pane never rewrites it. The key travels through SessionInfo, then PtyProcessInfo (byte-accounted in the admission), then pty:listSessions, and arrives as PtyListedSession.paneKey.
  • liveSleepingAgentClaims falls back to "a live PTY spawned for this record's exact pane" when the layout and tab links don't resolve. Matching is exact, so a shell in a sibling pane cannot claim a dead agent.
  • SessionInfo moved to daemon-session-info.ts, because types.ts was also at its cap. It is re-exported from types.ts, so no import sites change.

This is a minimal rewrite of #24386 by @Minidoracat on current main. Since that PR was opened, #29431 already routes the phone wake through the gate, and the SSH relay (src/relay) has been deleted, so those parts are no longer needed. The approach and the daemon lifecycle test are theirs.

Mixed-version / remote

How verified

  • Repro on main: the new test does not resume an agent whose PTY the host moved to another tab in wake-sleeping-agents-live-unmounted-pane.test.ts drives the real phone wake, gate and resume sweep. On main it fails because the sweep queues --resume <same session>. With the fix it passes.
  • Boundary test: still resumes a dead agent next to a shell spawned for another pane.
  • Daemon test: lists the pane key a session was spawned for, not the pane that reattaches it, from fix(terminal): don't resume an agent whose spawned PTY is still live #24386.
  • Real app with real claude 2.x: a dev build in the background with an isolated profile, CDP only.
    • The live PTY's pty:listSessions row lists agentOwnership: "absent" and paneKey: "a3a9…:0246…". That is identical to the hook-captured origin: 'live' record's paneKey, which confirms the join key on the real path.
    • I re-keyed the tab the way [Bug]: A tab closed on one paired client is auto-resumed (claude --resume) by another client on workspace selection #21235 describes and reloaded the renderer. The persisted state then showed the PTY under the new tab and leaf, with the record still naming the old pane. The daemon still reported the old spawn key.
    • Running the real wakeSleepingAgentsForWorktreeInBackground gave adopted, one tab, no queued resume, and no new claude --resume process.
  • pnpm tc passes, and check:code-quality:changed passes. Gate, seam, wake, host-mirror, pty IPC, provider admission and daemon suites pass: 4291 tests. The only failures were real-shell PTY timing tests (shell-ready.test.ts and repro-13767-…) that time out on this machine because PTYs are exhausted, and they don't touch this code.

Risk

Low. The gate claims more records only on exact spawn-pane evidence from a live PTY. Without the key, behaviour is unchanged.

Not covered

Fixes #21235
Fixes #24385
Refs #20461, #27355, #19882, #29431
Supersedes #24386

Co-authored-by: Minidoracat [email protected]

The terminal daemon now lists the ORCA_PANE_KEY each PTY was spawned with,
and the activation gate (also used by the phone wake) claims a sleeping
record through that key when this renderer's layout no longer links the
record's pane to the live PTY. Covers a host that re-keyed the live PTY onto
a new tab and an agent a paired client launched for its own pane; both
previously forked a second `claude --resume` onto the live transcript.

Rewritten on current main from #24386 (the phone wake already routes
through the gate since #29431; the SSH relay it also touched is gone).

Co-authored-by: Minidoracat <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Walkthrough

PTY session listings now include the pane key used when each PTY was spawned. The activation gate uses live session IDs and pane keys to match sleeping records to live PTYs. If the persisted PTY ID is not live, the gate checks for a live PTY with the record’s pane key. Added tests cover reattachment under another tab and a shell associated with a different pane.

Priority: Normal

Severity of issue fixed: Medium

Merge Risk: Moderate · up to 7f35f

An agent may remain asleep when a shell replaces it in the same pane. Distinguish the agent from the replacement shell before merging.

Pre-merge checks: Passed 3 · Failed 2

Failed Failed checks: 2 warnings

Check name Status Explanation Resolution
Linked Issues check Warning The PR implements the live-PTY case for #21235: liveSleepingAgentClaims matches the record's exact paneKey and blocks a duplicate resume when the PTY remains live. The linked issue requires a user… Implement and test close handling for #21235. When the client applies a host snapshot that removes a mirrored tab, retire the sleeping-agent records for that tab, or use host close information to distinguish a user close from a lost PTY bef…
Docstring Coverage Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 12 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 3 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary change: preventing duplicate agent resumes when the spawned PTY is still live.
Description check Passed The description is detailed and directly explains the problem, root cause, implementation, compatibility behavior, linked issues, testing, risks, and scope. It omits several template headings, includi…
Out of Scope Changes check Passed The changed daemon listing, optional paneKey wire field, provider admission, renderer gate, and tests all support the linked duplicate-resume behavior in #21235 and #24385. The type move is a suppor…
Full details: Description check

Explanation

The description is detailed and directly explains the problem, root cause, implementation, compatibility behavior, linked issues, testing, risks, and scope. It omits several template headings, including Visual Proof, Review, Notes, and Checklist, but the required technical context is mostly complete.

Full details: Out of Scope Changes check

Explanation

The changed daemon listing, optional paneKey wire field, provider admission, renderer gate, and tests all support the linked duplicate-resume behavior in #21235 and #24385. The type move is a supporting refactor for the same data path. No unrelated production behavior is established by the reviewed diff.

Full details: Linked Issues check

Explanation

The PR implements the live-PTY case for #21235: liveSleepingAgentClaims matches the record's exact paneKey and blocks a duplicate resume when the PTY remains live. The linked issue requires a user-closed tab to remain retired and not be auto-resumed. The changed code does not retire the sleeping record when a host snapshot removes a mirrored tab, and the added #21235 test covers a re-keyed live PTY, not the closed-tab case described by the issue. The implementation satisfies #24385: it records the spawn pane key, carries it through PTY listing, and claims a live PTY launched by a paired client without a host layout. The added boundary test preserves resume behavior for a dead agent.

Resolution

Implement and test close handling for #21235. When the client applies a host snapshot that removes a mirrored tab, retire the sleeping-agent records for that tab, or use host close information to distinguish a user close from a lost PTY before the activation sweep resumes the record.

Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1


Review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: aea1edc4-eba6-4e2e-a208-0f5c7bc287f9
Commits

Reviewing files that changed from the base of the PR and between 407e9eb and 7f35ffd.

Files selected for processing: 12
  • src/main/daemon/daemon-pty-session-inventory.ts
  • src/main/daemon/daemon-session-info.ts
  • src/main/daemon/terminal-host-session-create.ts
  • src/main/daemon/terminal-host-session-listing.ts
  • src/main/daemon/terminal-host.test.ts
  • src/main/daemon/types.ts
  • src/main/ipc/pty/ipc/session-listing.ts
  • src/main/providers/pty-process-info.ts
  • src/main/providers/pty-process-list-admission.ts
  • src/renderer/src/lib/wake-sleeping-agents-live-unmounted-pane.test.ts
  • src/renderer/src/lib/worktree-agent-activation-gate.ts
  • src/shared/pty-listed-session.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

Comment on lines +140 to +143
const livePtyId =
persistedPtyId && livePtyIds.has(persistedPtyId)
? persistedPtyId
: livePtyIdBySpawnPaneKey.get(record.paneKey)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Major · Functional Correctness · Heavy lift

Do not treat a matching pane key as proof that the agent is live.

If the old agent’s PTY exits and a replacement shell starts with the same pane key, this fallback claims the sleeping record for the shell. The gate then skips the agent’s resume even when the shell reports agentOwnership: 'absent'. The adjacent shell test covers only a different pane key. Require evidence that the matched PTY runs the recorded agent before suppressing recovery, and cover a replacement shell in the same pane.

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

Labels

None yet

Projects

None yet

1 participant