Skip to content

Main repairs a terminal stored in two panes once at startup (dark, layout-owner build only) - #30178

Merged
Jinwoo-H merged 3 commits into
mainfrom
e2-6/p2-load-time-duplicate-pty-repair
Oct 18, 2026
Merged

Jinwoo-H merged 3 commits into
mainfrom
e2-6/p2-load-time-duplicate-pty-repair

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​400 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​400
Prod 5 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​178 $\color{#cf222e}{\Huge{\mathbf{−}}}$​26 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​152

ELI5

Sometimes a saved terminal tab ends up describing one terminal as two side-by-side panes. Every time Orca opens, it quietly fixes this in memory, but it never fixes the saved copy, so the bad layout stays on disk forever. In the build where the main process owns the layout, this change has main fix the saved copy once at startup. Before it does, it moves everything that was filed under the extra pane (the agent status and any orchestration record) over to the pane that keeps the terminal. Only then does it write the fixed layout to disk.

What Changed

Before (layout-owner build): a tab stored with one terminal in two panes kept that shape on disk across every restart. The window showed one pane because it repairs on load, but anything that reads the stored layout directly saw two: recovering an orphaned terminal for that pane failed with terminal_orphan_competing_owner, and the phone's list of a desktop with no window open showed the terminal twice.

After (layout-owner build): the first startup with a running runtime rewrites the stored tab to one pane per terminal. The disk shows one pane right after launch and after quit; orphan recovery replays the kept pane; the phone list shows one row.

Mechanism:

  • New planDuplicatePtyPaneRepair runs the existing one-pane-per-terminal repair (normalizeTerminalLayoutPtyOwnership) over each stored tab and lists the pane transfers it implies (dropped pane, kept pane, terminal id). It skips paired-server partitions (runtime:), which are display copies that their own server repairs.
  • New Store method repairDuplicatePtyPanes(transfer) runs as one durable write: it calls transfer for every dropped pane first, then writes the repaired partitions. If an owner move fails, nothing is written and the next start tries again. A frozen or imported Store does nothing.
  • Everything the window files under the dropped pane moves to the kept pane in the same write, as the window's own transfer does: its sleep record (so a sleeping agent still resumes), its read marks (acknowledged, activity cleared, manually unread, so an acknowledged agent does not show unread again), its SSH lease leaf, and its terminal incarnation (the kept pane's own incarnation wins when it has one). This reuses the pane move's record rekey, generalized from "same leaf, new tab" to any pane to any pane.
  • The window's other rule is mirrored too: when the dropped pane's agent-status owner already resolves to a pane in another tab, that owner and its records stay where they are.
  • A local workspace whose session a paused WSL move has frozen is skipped: the write keeps its stored tree, so its owners must not move.
  • The transfers go through the same code a pane move already uses (rekeyMovedPaneOwners): its body is now a shared rekeyPaneOwners(from pane, to pane) that both the move and the repair call. It moves agent status (transferPaneAuthority) and the orchestration row (rekeyWorkerTerminalResourcePaneKey). It still logs a failed step and carries on, as before, and now also reports it; the move ignores the report, and the repair skips its write.
  • Desktop and headless serve call the repair right after the runtime is created, only in the layout-owner build. A load with no runtime (a CLI-only read, an imported or frozen profile) never writes it, so disk keeps the old pane key until the owners can move with it; the next full start does both.

With the switch off nothing calls the new code: shipped builds keep the duplicate on disk exactly as today.

Why

The window already repairs this on every load, but its save cannot reach disk: main's save rule keeps its stored pane tree when a window save without a newer revision changes the pane set. Repairing in main, once, at the place that owns the stored tree, fixes every reader at once, instead of teaching each reader that skips the load repair (orphan recovery, the phone list) to repair again.

Alternatives considered:

  • Repair inside the Store constructor and write immediately: this would drop the old pane key from disk before agent status and orchestration records moved, and a CLI-only or imported load would write too. Holding the write until a runtime can apply the transfers avoids both.
  • Write through setWorkspaceSession: that path rebases the pane tree back to the stored one, which is the bug. The repair uses the same partition write as layout commands instead.
  • Not advancing the topology revision: the window already shows the one-pane tree, so from its side the stored tree now simply matches what it saves.

Linked Issue

Internal layout-owner work (phase E2-6, prerequisite P2).

Visual Proof

N/A: no UI change. With the switch off nothing runs; with it on, the window already showed one pane and now the stored layout matches it. Disk evidence is below.

Testing

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

New tests (failing first: with the repair stubbed to a no-op, the two runtime-load cases failed with transferPaneAuthority called 0 times):

  • duplicate-pty-pane-repair.test.ts (real Store on SQLite; the four record, owner-rule and failure cases were added in round 2 and all failed against the first commit): a runtime start moves the dropped pane's agent status and orchestration row, and the spy records that disk still held both panes at the moment of the transfer, then disk holds one pane. A load with no runtime leaves the stored layouts byte-identical and runs no transfer; a later runtime start transfers once and writes the session domain once. A frozen Store and an imported Store neither transfer nor write. A second start is a no-op (no transfer, no write). A terminal another tab also binds is repaired without moving its owner, matching the window's rule. The dropped pane's sleep record, three read marks, incarnation and SSH lease end on the kept pane, and the kept pane's own incarnation wins over the dropped one. A status owner already in another tab is not moved and its records stay. A failing orchestration rekey leaves disk unchanged. A frozen WSL-move workspace is not repaired.
  • runtime-terminal-orphan-adoption-duplicate-pty.test.ts: the stored duplicate is refused as terminal_orphan_competing_owner; the repaired tree replays the kept pane.
  • mobile-session-tabs-duplicate-pty-repair.spec.ts: the windowless phone list shows 2 terminal rows for the stored duplicate and 1 after the repair.

Gates (macOS): pnpm tc pass; pnpm run check:code-quality:changed 0 new findings; oxlint on changed files clean; pnpm run audit:anti-slop pass; check:layout-writer-ratchets, check:focus-read-ratchet, check:max-lines-ratchet pass. Vitest (4 workers) over src/main/persistence/terminal-topology, src/main/startup, src/main/agent-hooks, leaf-move, orphan adoption and orca-runtime.test.ts: 3677 passed, 1 failed (preserves existing badgeColor on runtime cloneRepo folder->git dedupe upgrade, a macOS /private/var temp path mismatch that fails the same way on main).

End to end (terminal-duplicate-pty-renderer-reveal.spec.ts, plus a local copy that reads the profile database after each step):

  • Switch on, main: spec passes; disk holds 2 panes after quit; save-divergence detector logs 5 "pane tree kept over the save's" reports.
  • Switch on, this branch: spec passes; disk holds 1 pane from the first read after launch through quit; 0 detector reports.
  • Switch off, this branch: spec passes; disk holds 2 panes after quit, as shipped builds do today.

Switch-off bundle (unminified, main vs this branch): renderer and preload identical apart from file hashes and the commit stamp. Main bundle changes in two places:

  • Added, never called with the switch off: the one-pane-per-terminal repair module (and the shared repair helpers it pulls in), the repair write, the Store method repairDuplicatePtyPanes, and the hook server's resolvePaneAuthorityOwner.
  • rekeyMovedPaneOwners now calls the shared rekeyPaneOwners with the same pane keys, terminal id and order of steps; it now also returns whether both steps succeeded, which the move ignores.
  • rekeyMovedLeafProfileRecords now calls the shared rekeyPaneProfileRecords with the same keys; a moved SSH lease also gets its leaf id written, which is the same leaf id it already had. moveRecordKey gained a keepTarget option that defaults off. The pane move behaves the same.

AI Disclosure

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • SSH: SSH partitions are repaired the same way as local ones; paired-server (runtime:) partitions are display copies and are not repaired here.
  • MUST FIX BEFORE THE FLIP: the standalone server (orcad) does not run this repair yet. Its startup file sits at the 300-line limit, so wiring it needs a small extraction first. Until then a paired server keeps its own stored duplicate, as it does today.
  • Folder workspaces: covered; the repair keys on tabs, not on git worktrees.
  • Mixed versions: no wire change. No new layout.apply field or command, so nothing for the flip capability list.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

…t startup, after moving the dropped pane's owners (dark, layout-owner build only)
@coderabbitai

coderabbitai Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

📝 Walkthrough

The change adds planning and persistence for duplicate PTY panes in tab layouts. Eligible repairs transfer pane ownership before the repaired session is written. Main-process startup starts the repair when the main process owns the layout. Tests cover persistence conditions, terminal listings, and orphan adoption.

Priority: ➖ Normal

Merge Risk: 🟡 Moderate · up to 44e13

In the layout-owner build, the startup repair of duplicated terminal panes can attach another pane's agent status in the same tab to the kept pane, so status ends up on the wrong terminal. The repair runs only in that build, but this misattribution should be fixed before merging.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 48.28% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 29 functions across 11 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
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 summarizes the main startup repair and identifies its layout-owner scope.
Description check Passed The description is detailed and covers the required sections, behavior, rationale, testing, scope, risks, and checklist. The linked issue entry is informal, but the overall description is complete.
  • Fix all pre-merge checks with AI
✨ 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
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 565611ee-edfd-4bd3-8cf5-db4a8213806d
📥 Commits

Reviewing files that changed from the base of the PR and between 6c45958 and 47baae3.

📒 Files selected for processing: 9
  • src/main/persistence/terminal-topology/duplicate-pty-pane-repair.test.ts
  • src/main/persistence/terminal-topology/duplicate-pty-pane-repair.ts
  • src/main/persistence/terminal-topology/layout-command-persistence.ts
  • src/main/persistence/terminal-topology/terminal-topology-commit.ts
  • src/main/runtime/moved-pane-owner-rekey.ts
  • src/main/runtime/orca-runtime-tests/mobile-session-tabs-duplicate-pty-repair.spec.ts
  • src/main/runtime/orca-runtime.test.ts
  • src/main/runtime/runtime-terminal-orphan-adoption-duplicate-pty.test.ts
  • src/main/startup/main-process-runtime-service.ts

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

…nd skips the write when an owner move fails (dark, layout-owner build only)
…ce untouched (dark, layout-owner build only)

@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
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a3e06f63-c33b-4aaf-9d0d-17dca582389c
📥 Commits

Reviewing files that changed from the base of the PR and between 47baae3 and 44e13ee.

📒 Files selected for processing: 6
  • src/main/agent-hooks/server/server-authority-aliases.ts
  • src/main/persistence/terminal-topology/duplicate-pty-pane-repair.test.ts
  • src/main/persistence/terminal-topology/duplicate-pty-pane-repair.ts
  • src/main/persistence/terminal-topology/layout-command-persistence.ts
  • src/main/persistence/terminal-topology/terminal-leaf-move.ts
  • src/main/runtime/moved-pane-owner-rekey.ts

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

Comment on lines +74 to +75
if (owner && owner.tabId !== move.tabId) {
return false

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.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Reject a same-tab authority owner on a third leaf.

If the dropped pane aliases to a different leaf in the same tab, this guard permits the transfer. transferPaneAuthority then moves that third leaf’s agent status to the retained leaf, although the repair removes only removedLeafId. Accept an existing owner only when its key is the dropped or retained pane key; otherwise leave its ownership unchanged.

@Jinwoo-H
Jinwoo-H merged commit b8fbfce into main Oct 18, 2026
45 checks passed
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.

1 participant