Skip to content

fix(agents): stop forking a duplicate Pi tab for a live background session - #9729

Merged
nwparker merged 1 commit into
stablyai:mainfrom
shaharmor:fix/pi-resume-duplicate-live-tab
Jul 24, 2026
Merged

nwparker merged 1 commit into
stablyai:mainfrom
shaharmor:fix/pi-resume-duplicate-live-tab

Conversation

@shaharmor

@shaharmor shaharmor commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

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 adds stablePaneHasLivePty(): 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 runtime ptyIdsByTabId map rather than the stale ptyIdsByLeafId layout snapshot.

Evidence

Validated & rebased onto latest main during a bug-bash triage on 2026-07-23. Rebase onto origin/main was clean.

  • Test pi-live-session-no-duplicate-tab.test.ts passes on branch: 1 file, 1 test passed.
  • Reverting only the fix (sleeping-agent-pane-ownership.ts) to origin/main while keeping the test makes it FAIL — resume forks a duplicate tab (launched === 1).
  • Lint clean: oxlint on the changed file reports no findings.
STEP 2a (on branch): Test Files 1 passed (1); Tests 1 passed (1)

STEP 2b (fix reverted): FAIL
AssertionError: expected 1 to be +0 // Object.is equality
  at pi-live-session-no-duplicate-tab.test.ts:78  expect(launched).toBe(0)

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.

@coderabbitai

coderabbitai Bot commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The 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)

Check name Status Explanation Resolution
Description check ⚠️ Warning It covers summary, root cause, evidence, trade-offs, and regression risk, but several required template sections are missing. Add the missing Screenshots, Testing checklist, AI Review Report, Security Audit, and Notes sections, or fill them with the required details.
✅ Passed checks (4 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly matches the main change: preventing duplicate Pi tabs for live background sessions.

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.

@JakubGrupa36

Copy link
Copy Markdown

@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.

@shaharmor

Copy link
Copy Markdown
Contributor Author

@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.
@nwparker
nwparker force-pushed the fix/pi-resume-duplicate-live-tab branch from a8a41ef to c4ee2e8 Compare July 23, 2026 11:49
@nwparker

Copy link
Copy Markdown
Contributor

@shaharmor yeah doing it now

And sorry my GH volume has been too high so I miss mentions 😢
Feel free to always msg me on X or Discord and I'll take care of stuff nearly instantly!

@nwparker
nwparker merged commit 877bbde into stablyai:main Jul 24, 2026
@nwparker

Copy link
Copy Markdown
Contributor

Thanks so much for this, @shaharmor — stop forking a duplicate Pi tab for a live background session. Reviewed, rebased on latest main, and merged. Really appreciate the contribution! 🙏

Jinwoo-H added a commit that referenced this pull request Aug 5, 2026
…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).
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants