Repository navigation
fix(agents): stop forking a duplicate Pi tab for a live background session - #9729
Conversation
📝 WalkthroughWalkthroughThe change adds live PTY detection for stable panes using runtime tab-to-PTY bindings, including single-leaf layouts without per-leaf bindings. Pane ownership is recorded immediately when a live PTY is found. A Vitest regression test initializes an existing Pi tab and sleeping session, resumes the worktree, and verifies that no duplicate tab is created and the session record remains unchanged. 🚥 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 |
|
@shaharmor +1, I have that problem very frequent - switching between worktrees with pi creates a lot of forks of active sessions which is totally frustrating. |
|
@nwparker can we please get this reviewed? It's really making the Orca experience frustrating. |
…ssion Pi's live-recovery record keeps a resumable session alive after a turn completes (agent_end -> done). On worktree activation, recordPaneIsOwnedByPreservedPane only treated a pane as owned when it was the pane that reconnects on activation (the active tab), so a done-but-alive Pi session in a background tab was misclassified as un-owned and resume opened a second tab pointing at the session still running in the original tab. Treat a pane as owning its session whenever it currently has a live PTY, regardless of active state — a live session must never be forked. Liveness reads from the runtime live-PTY map (ptyIdsByTabId), not the layout's ptyIdsByLeafId snapshot (which persists stale across sleep/restart), and is leaf-precise so split-pane sleeping agents on dead leaves still resume.
a8a41ef to
c4ee2e8
Compare
|
@shaharmor yeah doing it now And sorry my GH volume has been too high so I miss mentions 😢 |
|
Thanks so much for this, @shaharmor — stop forking a duplicate Pi tab for a live background session. Reviewed, rebased on latest |
…esktop promotion (#12666) STA-1716 reported that a packaged `orca serve` could become the single-instance owner after the desktop app exits, leaving Dock/Finder unable to restore a window — and that forcing a reopen made the headless process hydrate a renderer that interrupted and DUPLICATED live agent sessions. Verification against main found every criterion already fixed (#8646 for desktop promotion and the fail-closed CLI, #12212 for duplicate serve activation, #12574 + #9729 for the resume/ownership guards). The genuine gap was criterion 6: the ticket's own automated regression never existed. An existing reliability gate asserted PTY identity survives promotion, but nothing asserted what the incident was actually about — how many agents the promoted renderer resumes. This adds that coverage: a unit/service-level journey that drives the real single-instance lock, activation gate, settle and focus paths, then runs the real resume logic against a store seeded as a renderer freshly mounted inside the serve process, asserting zero duplicate resumes. `settleServeDesktopActivation` moved from `index.ts` into its own module with identical semantics, so the test drives the real decision rather than re-implementing it — the earlier repro had to mirror that logic locally, which is the "test passes without running the scenario" failure mode. Proven to be a real oracle: breaking each guard individually turns it red, and reverting the pre-#12574 pane form reproduces the incident exactly (two duplicate `codex resume` tabs).
…esktop promotion (stablyai#12666) STA-1716 reported that a packaged `orca serve` could become the single-instance owner after the desktop app exits, leaving Dock/Finder unable to restore a window — and that forcing a reopen made the headless process hydrate a renderer that interrupted and DUPLICATED live agent sessions. Verification against main found every criterion already fixed (stablyai#8646 for desktop promotion and the fail-closed CLI, stablyai#12212 for duplicate serve activation, stablyai#12574 + stablyai#9729 for the resume/ownership guards). The genuine gap was criterion 6: the ticket's own automated regression never existed. An existing reliability gate asserted PTY identity survives promotion, but nothing asserted what the incident was actually about — how many agents the promoted renderer resumes. This adds that coverage: a unit/service-level journey that drives the real single-instance lock, activation gate, settle and focus paths, then runs the real resume logic against a store seeded as a renderer freshly mounted inside the serve process, asserting zero duplicate resumes. `settleServeDesktopActivation` moved from `index.ts` into its own module with identical semantics, so the test drives the real decision rather than re-implementing it — the earlier repro had to mirror that logic locally, which is the "test passes without running the scenario" failure mode. Proven to be a real oracle: breaking each guard individually turns it red, and reverting the pre-stablyai#12574 pane form reproduces the incident exactly (two duplicate `codex resume` tabs).
Summary
Fixes a bug where finishing a Pi turn in a background tab and then reactivating the worktree could pop open a duplicate Pi tab resuming a session that was still alive in the original tab.
ELI5
If an AI assistant finished talking in a tab you weren't looking at, coming back to that project could open a second copy of it. Now it stays as one tab.
Root cause & fix
Pi's live-recovery records keep a session resumable after its turn completes, but worktree activation's
recordPaneIsOwnedByPreservedPane()only treated a pane as owning its session when that pane was the one reconnecting on activation (the active tab). A done-but-alive Pi pane sitting in a background tab was misread as un-owned, so resume forked a duplicate tab. The fix addsstablePaneHasLivePty(): a pane owns its running session whenever it currently has a live PTY, regardless of focus, and returns early before the activation gate. Liveness is read from the runtimeptyIdsByTabIdmap rather than the staleptyIdsByLeafIdlayout snapshot.Evidence
Validated & rebased onto latest main during a bug-bash triage on 2026-07-23. Rebase onto
origin/mainwas clean.pi-live-session-no-duplicate-tab.test.tspasses on branch: 1 file, 1 test passed.sleeping-agent-pane-ownership.ts) toorigin/mainwhile keeping the test makes it FAIL — resume forks a duplicate tab (launched === 1).oxlinton the changed file reports no findings.Trade-offs
None — pure fix.
Regression risk
Zero-regression by construction: only the ownership branch for panes with a live PTY changes. Genuinely hibernated panes (no live PTY) keep the existing cold-restore-and-fork behavior, so non-affected paths are unchanged. The reproduction test proves both the fix and the preserved behavior.
Credit to @shaharmor for the original fix. Validated, rebased, and (where applicable) hardened via automated bug-bash review; original fix design preserved.