Repository navigation
fix(mobile): stop a mobile reveal from minting a duplicate terminal tab - #11259
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📥 CommitsReviewing files that changed from the base of the PR and between 06b0d32d0a034c7fd27c7abf232d2bc271e60e66 and bf33f56. 📒 Files selected for processing (7)
🚧 Files skipped from review as they are similar to previous changes (5)
📝 WalkthroughWalkthroughThe changes add shared PTY-to-tab ownership models and resolution logic covering mounted bindings, persisted layouts, hints, and ambiguity. Mobile tab mounting now consumes the shared ownership state. IPC terminal creation uses the resolver for tab reuse and split reveals. A reusable IPC-events test harness and tests cover mobile reveal adoption, stale layouts, missing hydration, conflicting hints, and ambiguous ownership. 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
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 |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/renderer/src/hooks/mobile-terminal-reveal-tab-adoption.test.ts (1)
10-30: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win
revealSplitPaneFromMobilenever triggers the split-reveal branch.
isSplitRevealinuseIpcEvents.ts(Line 1429) requiresptyId && tabId && leafId && splitFromLeafId. This helper omitssplitFromLeafId, so every test using it takes the non-split path. The reorderedexistingTab ?? splitTargetTabprecedence and the "tab not found" throw at Lines 1433-1436 — the riskiest part of this diff — are unexercised despite the test names referencing split panes.Consider adding
splitFromLeafId: 'leaf-a'to one case (or a dedicated test) so the split path is covered.♻️ Add a split-reveal case
leafId?: string + splitFromLeafId?: string presentation?: 'background' | 'focused' title?: string }) => voidThen a dedicated test asserting the owning tab is reused and
addSplitLeafToLayoutis applied to it rather than to the hinted tab.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: d320648f-d6ed-4fb2-a40e-bf19032705b5
📥 Commits
Reviewing files that changed from the base of the PR and between c8dba6d and 06b0d32d0a034c7fd27c7abf232d2bc271e60e66.
📒 Files selected for processing (7)
src/renderer/src/hooks/ipc-events-test-harness.tssrc/renderer/src/hooks/mobile-terminal-reveal-tab-adoption.test.tssrc/renderer/src/hooks/useIpcEvents.tssrc/renderer/src/lib/mobile-terminal-tab-mount.test.tssrc/renderer/src/lib/mobile-terminal-tab-mount.tssrc/renderer/src/lib/terminal-tab-for-pty-id.test.tssrc/renderer/src/lib/terminal-tab-for-pty-id.ts
Greptile SummaryThis PR fixes a duplicate terminal tab bug (#10486) where a mobile reveal of a split pane that has a second PTY recorded only in the persisted layout (not yet mounted) caused
Confidence Score: 5/5Safe to merge; the ownership resolution is narrow and well-bounded, the ambiguity path degrades gracefully to creating a new tab rather than throwing, and the test suite covers all three tiers bidirectionally. The change is self-contained: a new pure function replaces two ad-hoc ownership checks in one handler. All three binding tiers are exercised by mutation-audited tests, the hook is tested end-to-end through the real IPC handler, and the ambiguity path that previously could have thrown on the un-caught mobile focus path now fails closed silently. Files Needing Attention: No files require special attention; the resolver logic and its call site in useIpcEvents are fully covered by the new test suite.
|
| Filename | Overview |
|---|---|
| src/renderer/src/lib/terminal-tab-for-pty-id.ts | Core ownership resolver: three-tier logic (mounted → recorded → hint) is sound; tiebreak and ambiguity paths are well-covered by unit tests. |
| src/renderer/src/hooks/useIpcEvents.ts | Replaces two-source ownership + hintedPendingTab with resolveTerminalTabPtyOwnership; ambiguity path now creates a tab silently, preventing an unhandled throw on the mobile focus path. |
| src/renderer/src/lib/mobile-terminal-tab-mount.ts | Signature widened to TerminalTabPtyOwnershipState (adds ptyIdsByTabId); resolveTerminalTabIdForPtyId now correctly prefers a live binding over a stale layout row for background-mount planning. |
| src/renderer/src/hooks/ipc-events-test-harness.ts | New harness correctly stubs useEffect synchronously, injects a store proxy, and captures the createTerminal IPC listener to drive the hook at the handler boundary. |
| src/renderer/src/hooks/mobile-terminal-reveal-tab-adoption.test.ts | Seven end-to-end scenarios covering the fixed split-pane path, stale layout vs live map precedence, hint disagreement, and ambiguity-creates-tab behavior; confirms the hook never throws. |
| src/renderer/src/lib/terminal-tab-for-pty-id.test.ts | Extended with livePtyIds parameter and 10 new resolveTerminalTabPtyOwnership tests; covers all three tiers, tiebreak, and ambiguity; also adds a live-binding test for the existing resolveTerminalTabIdForPtyId wrapper. |
| src/renderer/src/lib/mobile-terminal-tab-mount.test.ts | Updated state helper to include ptyIdsByTabId; adds a test that mounted-pane ownership beats a stale layout row during background-mount planning. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[onCreateTerminal IPC] --> B{ptyId present?}
B -- No --> C[kind: none]
B -- Yes --> D[resolveTerminalTabPtyOwnership]
D --> E[Scan worktree tabs]
E --> F{ptyIdsByTabId includes ptyId?}
F -- Yes --> G[mountedOwners.push tab]
F -- No --> H{tab.ptyId eq ptyId or layout row?}
H -- Yes --> I[recordedOwners.push tab]
H -- No --> E2[next tab]
G --> E2
I --> E2
E2 --> K{mountedOwners length GT 0?}
K -- Yes --> L[owners = mountedOwners]
K -- No --> M[owners = recordedOwners]
L --> N{owners.length}
M --> N
N -- equals 1 --> O[kind: owned tabId = owners0]
N -- GT 1 --> P{preferTabId in owners?}
P -- Yes --> Q[kind: owned tabId = preferTabId]
P -- No --> R[kind: ambiguous]
N -- equals 0 --> S{preferTabId exists?}
S -- Yes --> T[kind: owned - hint fallback]
S -- No --> C
O --> U{ownership.kind}
Q --> U
R --> U
T --> U
C --> U
U -- owned --> V[existingTab = tab by tabId]
U -- ambiguous or none --> W[existingTab = undefined]
V --> X{reusedTab = existingTab or splitTargetTab}
W --> X
X -- reusedTab set --> Y[Adopt existing tab]
X -- reusedTab null --> Z[createTab - mint new tab]
Reviews (2): Last reviewed commit: "fix(mobile): stop a mobile reveal from m..." | Re-trigger Greptile
Revealing a live PTY from a paired phone passes the tab id baked into its env, but the create-terminal handler only recognised ownership via tab.ptyId and the live ptyIdsByTabId map. With the worktree closed on the desktop no pane is mounted, so ptyIdsByTabId is empty and tab.ptyId holds at most one leaf's PTY -- a split pane's second PTY lives only in the persisted layout. Ownership missed, and createTab ran with an id that already existed; it mints a fresh uuid on collision, so a second tab appeared on the same session (#10486). Resolve ownership through the persisted layout as well. Every binding consulted is an exact match on the ptyId, so all of them outrank the reveal's tab id hint: that hint is written once when the PTY spawns and is never rewritten, so it goes stale as soon as a pane is dragged to another tab. A mounted pane outranks a recorded one, same-tier conflicts stay ambiguous so the mobile mount planner keeps failing closed, and the hint is what resolves a reveal that nothing records yet -- which is also what keeps paneKey hook attribution intact.
06b0d32 to
bf33f56
Compare
…ab (stablyai#11259) Revealing a live PTY from a paired phone passes the tab id baked into its env, but the create-terminal handler only recognised ownership via tab.ptyId and the live ptyIdsByTabId map. With the worktree closed on the desktop no pane is mounted, so ptyIdsByTabId is empty and tab.ptyId holds at most one leaf's PTY -- a split pane's second PTY lives only in the persisted layout. Ownership missed, and createTab ran with an id that already existed; it mints a fresh uuid on collision, so a second tab appeared on the same session (stablyai#10486). Resolve ownership through the persisted layout as well. Every binding consulted is an exact match on the ptyId, so all of them outrank the reveal's tab id hint: that hint is written once when the PTY spawns and is never rewritten, so it goes stale as soon as a pane is dragged to another tab. A mounted pane outranks a recorded one, same-tier conflicts stay ambiguous so the mobile mount planner keeps failing closed, and the hint is what resolves a reveal that nothing records yet -- which is also what keeps paneKey hook attribution intact.
Summary
Fixes #10486 — viewing a live host terminal from a paired phone no longer opens a second desktop tab showing the same session.
OrcaRuntime.focusTerminalreveals the PTY with the tab id baked into its env (ORCA_TAB_ID/ORCA_PANE_KEY), and the create-terminal handler inuseIpcEventsis supposed to adopt the tab that already owns it. Ownership was only checked againsttab.ptyIdand the liveptyIdsByTabIdmap. When the worktree isn't open on the desktop no pane is mounted, soptyIdsByTabIdis empty andtab.ptyIdholds at most one leaf's PTY — a split pane's second PTY lives only in the persistedterminalLayoutsByTabIdrow. Ownership missed, andcreateTabran with an id that already existed; it mints a fresh uuid on collision, so a second tab appeared on the same session.The issue as filed (a single-pane Codex terminal) was never broken — a single-pane tab matches on
tab.ptyId. The reachable defect is the split-pane / partially-hydrated variant.Changes
resolveTerminalTabPtyOwnershipresolves a PTY through three bindings and ranks them:ptyIdsByTabId— a pane that is actually running ittab.ptyIdor a persisted layout rowtabIdhint, used only when nothing records the PTY, and as the tie-break when several doEvery binding is an exact match on the ptyId, so all of them outrank the hint. The hint is not a binding: main's
pty.tabIdcomes frompersistPtyBinding(written once at spawn, never rewritten) or from a graph sync off already-mounted leaves — so it is always a snapshot of a past renderer state and can never be fresher than the renderer's own records. It goes stale as soon as a pane is dragged to another tab. Same-tier conflicts stayambiguous, soplanMobileTerminalTabMountkeeps failing closed exactly as #8597 intended.onCreateTerminaladopts through that resolver and never throws on ambiguity —focusTerminalawaitsrevealTerminalSessionwith nocatch, so a throw would surface on the phone.hintedPendingTabis gone: the hint fallback subsumes it, and its "adopt only if the hinted tab has no PTY at all" guard was what blocked the split-pane case.Two costs worth naming rather than implying:
mainminted a duplicate. I could not construct a producer for that state (detachTerminalLayoutLeafprunes the source row,setTabLayoutrewrites the whole snapshot, and ptyIds are uuid-suffixed so they can't be reused).Out of scope, pre-existing: when the adopted tab's layout lacks the revealed
leafId, the handler replaces the layout with a single-pane snapshot. That predates this PR and this change makes it strictly less reachable, not more — the new "leaves the hinted tab split intact" test is RED onmain, which does collapse that tab.Test plan
mobile-terminal-reveal-tab-adoption.test.tsdrives the realonCreateTerminalhandler through a newipc-events-test-harness.ts.useIpcEvents.tsturns 6 of the 7 hook tests RED. Per-tier mutation audit over the three test files (baseline 30): hint fallback → 2 RED, layout-row tier → 11, mounted-pane tier → 6,tab.ptyIdtier → 8, hint tie-break → 2. No branch is deletable with the suite green.toBeNull()expectations, with separate positive cases added.--maxWorkers=2,oxlint,oxfmt --check,tsc --noEmit(all three projects).Reviewed adversarially over three rounds; the first two rounds found real defects in earlier revisions of this ordering, both fixed here.
Not verified live. Reproducing end-to-end needs a paired phone focusing a split pane in a worktree closed on the desktop; I validated at the IPC-handler level instead.
Supersedes #10556, which resolved ownership but let a stale layout row outrank a live binding, threw a user-visible "Ambiguous terminal tab ownership" on the un-caught mobile focus path, and had no coverage of its own
useIpcEventschange.ELI5
Your phone asked the desktop to show a terminal that was already open. The desktop couldn't tell which tab it belonged to, so it made a new one. Now it checks every place that records where a session lives, and trusts those over the label the session was born with — because that label goes stale if you drag the pane somewhere else.