Repository navigation
fix(terminal): don't resume an agent whose spawned PTY is still live - #33380
Conversation
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]>
Walkthrough
Priority: Severity of issue fixed: Medium Merge Risk:
|
There was a problem hiding this comment.
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
Files selected for processing: 12-
src/main/daemon/daemon-pty-session-inventory.tssrc/main/daemon/daemon-session-info.tssrc/main/daemon/terminal-host-session-create.tssrc/main/daemon/terminal-host-session-listing.tssrc/main/daemon/terminal-host.test.tssrc/main/daemon/types.tssrc/main/ipc/pty/ipc/session-listing.tssrc/main/providers/pty-process-info.tssrc/main/providers/pty-process-list-admission.tssrc/renderer/src/lib/wake-sleeping-agents-live-unmounted-pane.test.tssrc/renderer/src/lib/worktree-agent-activation-gate.tssrc/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.
-
| const livePtyId = | ||
| persistedPtyId && livePtyIds.has(persistedPtyId) | ||
| ? persistedPtyId | ||
| : livePtyIdBySpawnPaneKey.get(record.paneKey) |
There was a problem hiding this comment.
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.
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 --resumeon 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_KEYexported 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'sptyIdsByTabId. When that state no longer names the PTY, the record looks dead:claude --resume) by another client on workspace selection #21235: the host re-keyed the original PTY onto a new tab id.orca terminal listshows it under a different tab, while theclaudeenv still has the launch tab'sORCA_PANE_KEY. The daemon loggedmisses: tab_missing,layout_missingin the same second as theautomatic-restorespawn.In both cases a fresh
claudelaunch has no agent-session owner claim, so the census lists the PTY asagentOwnership: 'absent', and theblockedsafety 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
ORCA_PANE_KEYperSessionin a side table interminal-host-session-listing.ts, becausesession.tsis at its line cap. It lists the key as optionalpaneKey, and reattaching from another pane never rewrites it. The key travels throughSessionInfo, thenPtyProcessInfo(byte-accounted in the admission), thenpty:listSessions, and arrives asPtyListedSession.paneKey.liveSleepingAgentClaimsfalls 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.SessionInfomoved todaemon-session-info.ts, becausetypes.tswas also at its cap. It is re-exported fromtypes.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
paneKey, and the gate keeps today's behaviour for that daemon. A newer daemon talking to an older app has the field dropped by the old admission, which rebuilds each row.resolveActivationPtyListScopereturnsundefined), which is unchanged. The fix applies wherever the gate runs against a local daemon. That is the host desktop in [Bug]: A tab closed on one paired client is auto-resumed (claude --resume) by another client on workspace selection #21235 and [Bug]: Activation resumes an agent a paired client launched while it is still running, forking it onto a second tab #24385, including when a phone or a second Mac opens the workspace.How verified
does not resume an agent whose PTY the host moved to another tabinwake-sleeping-agents-live-unmounted-pane.test.tsdrives the real phone wake, gate and resume sweep. On main it fails because the sweep queues--resume <same session>. With the fix it passes.still resumes a dead agent next to a shell spawned for another pane.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.claude2.x: a dev build in the background with an isolated profile, CDP only.pty:listSessionsrow listsagentOwnership: "absent"andpaneKey: "a3a9…:0246…". That is identical to the hook-capturedorigin: 'live'record'spaneKey, which confirms the join key on the real path.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.wakeSleepingAgentsForWorktreeInBackgroundgaveadopted, one tab, no queued resume, and no newclaude --resumeprocess.pnpm tcpasses, andcheck:code-quality:changedpasses. 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.tsandrepro-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
tabsByWorktreerecord shares the liveptyIdand haspendingActivationSpawn: true. That is a tab-record cleanup bug, not a second agent.orca servewith a desktop client. The client takes no census of a runtime-owned workspace, and the server-side trigger was not reproduced here.Fixes #21235
Fixes #24385
Refs #20461, #27355, #19882, #29431
Supersedes #24386
Co-authored-by: Minidoracat [email protected]