Skip to content

A pane closed while its terminal starts stays closed (dark, layout-owner build only) - #30560

Merged
Jinwoo-H merged 1 commit into
mainfrom
e2-6/e1c2-cli-split-followups-2
Oct 19, 2026
Merged

Jinwoo-H merged 1 commit into
mainfrom
e2-6/e1c2-cli-split-followups-2

Conversation

@Jinwoo-H

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

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​327 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​327
Prod 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​222 $\color{#cf222e}{\Huge{\mathbf{−}}}$​22 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​200

ELI5

In the build where the background process owns the window layout (off in every shipped build), closing a terminal pane while its shell was still starting could bring the pane right back. This change makes the background process remember, for a few minutes, which panes it closed, and refuse a late "this pane's terminal has started" record for one of them. The closed pane stays closed, and the terminal that started too late is shut down.

What Changed

  • Before: when a terminal starts, the background process saves which terminal runs in which pane. If that pane is missing from its layout, the save adds it back, because historically a terminal could start before the window's layout reached the background process. So when a pane was closed while its terminal was still starting, that late save put it back, with its startup command running. This covers a user's close, a close from the command line or a phone, or the background process taking back a pane whose terminal did not start in time.
  • After (layout-owner build only): every close or take-back the background process commits records the panes it removed, in memory, for 5 minutes. A late terminal save for one of those panes is refused when the pane is no longer in the layout. A refused save already shuts down the terminal that just started. A real create of a pane under the same id lifts the record, so that pane works as usual. A write that fails and rolls back puts the records back as they were.
  • Mechanism:
    • The record lives in closed-pane-binding-fence.ts. It compares the pane ids before and after each terminal-layout commit in terminal-topology-commit.ts: pane closes, tab closes, window close and create commands, admissions, and take-backs.
    • The refusal is one new check in pty-binding-refusals.ts, next to the existing closed-tab check.

Why

  • Why the background process: it is the one doing the close, so it is the place that knows a pane is gone for good. The existing "a pane is missing, add it" rule stays for every other case, because several window start paths still depend on it. Those move to main-first creates in a later phase, and then the rule can go entirely.
  • Why not a durable marker: a marker written to disk would outlive restarts and need cleanup. A late start can only arrive within one app session, so memory is enough.
  • Why 5 minutes: that is longer than the longest wait a start can have before it records itself:
    • an SSH credential prompt (120 s), plus the connect (30 s), plus the relay request (30 s);
    • the local terminal service admits a start within 12 s.
      Pane ids are never reused, so a longer window refuses nothing real; the limit only bounds memory.

Linked Issue

Internal layout-owner follow-up (no issue).

Visual Proof

N/A: no UI change, and the change is in a build that is not shipped.

Testing

  • Failing first: persistence-closed-pane-binding-fence.test.ts runs against a real store with the layout-owner switch on. With the record not wired in, these four cases fail on the base commit:

    • the background process takes back a split whose terminal did not start;
    • the window closes a pane while its terminal is starting;
    • the background process closes a pane, through the same close the command line and phones use;
    • the window closes a tab while its terminal is starting.

    All pass with the fix. Three more cases pass on both: a pane created again under the same id (by the background process or the window) binds as usual, and after 5 minutes a late save behaves as before. closed-pane-binding-fence.test.ts covers two rules: a write in another partition that keeps the pane does not lift its record, and a rolled-back write puts the records back.

  • Gates: pnpm tc passes. The changed-code quality gate, layout-writer ratchets (212 main, 657 renderer), max-lines ratchet, and anti-slop audit all pass. Related store tests: 23 files, 147 tests pass.

  • Full test suite (this change touches code shipped builds load): 16 files failed locally. 13 fail the same way on the base commit; they are local-environment failures in packaging, browser-manager and dev-rebuild tests. The other 3 pass when run alone, so they were load flakes.

  • Switch-off bundle: I compared an unminified build of the base with this head. Preload and renderer are identical apart from chunk hashes. In main, everything that differs:

    • a new fencedIfLayoutOwner that returns the mutation it was given, so the four commit paths pass the same mutation as before. The order of evaluation is unchanged and the mutation is still built and run when it was before.
    • two small helpers, partitionsFence and layoutCommandFence, built only inside a callback that is never called with the switch off;
    • one comment edit;
    • the stamped source commit.

    The record, the refusal check and the pane-id comparison are compiled out entirely.

  • Layout save-divergence detector, switch on, with the same 6 specs before and after: terminal-pane-close-layout-consistency, terminal-parked-cli-split, terminal-cli-split-closed-before-mount, golden-core-flows, terminal-closed-pane-quit-relaunch, cli-first-worktree-single-terminal. Base: 13 passed, 0 reports. Head: 13 passed, 0 reports.

  • macOS only (local). No platform-specific code.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

Review

An independent fresh review found five issues: a record lifted by a write that only kept the pane, a rollback that did not restore records, missing tests for three close paths, test isolation, and the length of the window. All five are fixed in this commit, and a re-review of the fixes found none left.

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: the record is keyed by the pane, not by partition, so a late save that lands in a different partition than the close is refused too. No wire change, no new layout command or field. Shipped builds: the new check and the record are compiled out; see the bundle comparison.

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)

@coderabbitai

coderabbitai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

📝 Walkthrough

Adds an in-memory, five-minute fence for pane keys removed by close or withdrawal operations when main layout ownership is enabled. Terminal topology commits apply fence handling to close, admission, withdrawal, and selected layout commands. Rollback restores fence changes. PTY binding refusal now rejects a late binding for a fenced pane that is absent from the tab layout. Tests cover pane recreation, fence expiration, partition writes, rollback, and layout-owner behavior.

Priority: ⬇️ Low

Merge Risk: 🔵 Low · up to e7691

In a legacy multi-partition tab, a late terminal binding can restore a closed pane. Fix the cross-partition fence collision before relying on this protection for those tabs.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 18 functions across 5 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary behavior change: closed panes remain closed when late terminal startup occurs in the layout-owner build.
Description check Passed The description is comprehensive. It covers the user impact, implementation, rationale, testing, platform scope, compatibility, and checklist items. The linked issue section does not provide an issue …
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.
Full details: Description check

Explanation

The description is comprehensive. It covers the user impact, implementation, rationale, testing, platform scope, compatibility, and checklist items. The linked issue section does not provide an issue number, but it identifies this as an internal follow-up and the rest of the required content 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: 983a00e9-e2e1-4268-b982-60fc2e69420b
📥 Commits

Reviewing files that changed from the base of the PR and between e37d527 and e769163.

📒 Files selected for processing: 5
  • src/main/persistence-closed-pane-binding-fence.test.ts
  • src/main/persistence/loading-store/pty-binding-refusals.ts
  • src/main/persistence/terminal-topology/closed-pane-binding-fence.test.ts
  • src/main/persistence/terminal-topology/closed-pane-binding-fence.ts
  • src/main/persistence/terminal-topology/terminal-topology-commit.ts

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

Comment on lines +24 to +47
type SessionsRead = {
hostIds: () => readonly ExecutionHostId[]
getSession: (hostId: ExecutionHostId) => WorkspaceSessionState | null | undefined
}

type Fence = { sessions: SessionsRead; fences: boolean }

function paneKeysIn(
hostIds: readonly ExecutionHostId[],
getSession: SessionsRead['getSession']
): Set<string> {
const keys = new Set<string>()
for (const hostId of hostIds) {
const layouts = getSession(hostId)?.terminalLayoutsByTabId ?? {}
for (const [tabId, layout] of Object.entries(layouts)) {
if (layout?.root) {
for (const leafId of collectLayoutLeafIdsInOrder(layout.root)) {
keys.add(`${tabId}:${leafId}`)
}
}
}
}
return keys
}

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.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🔎 Supported by static analysis

🏁 Script executed:

sed -n '1,115p' src/main/persistence/loading-store/pty-binding-refusals.ts
sed -n '130,215p' src/main/persistence/loading-store/pty-binding-persistence.ts
sed -n '35,95p' src/main/runtime/runtime-layout-apply.ts
sed -n '48,85p' src/main/persistence/terminal-topology/closed-pane-binding-fence.test.ts

Repository: stablyai/orca

Length of output: 12394


🏁 Script executed:

set -e
printf '%s\n' '--- host resolution and refusal/fence references ---'
rg -n -F --glob '*.ts' -e 'function resolveHostId' -e 'const resolveHostId' -e 'resolveHostId(' -e 'ptyBindingIsRefused(' -e 'isClosedPaneFenced(' -e 'fencedIfLayoutOwner(' -e 'partitionsFence(' src/main | head -240
printf '%s\n' '--- complete fence implementation ---'
nl -ba src/main/persistence/terminal-topology/closed-pane-binding-fence.ts | sed -n '1,180p'
printf '%s\n' '--- persistence host-resolution context ---'
rg -n -F --glob '*.ts' -e 'resolveHostId' src/main/persistence/loading-store src/main | head -80
nl -ba src/main/persistence/loading-store/pty-binding-persistence.ts | sed -n '1,90p'
printf '%s\n' '--- refusal-focused tests and callers ---'
rg -n -F --glob '*.test.ts' -e 'different host' -e 'legacy relay' -e 'SSH' -e 'ptyBindingIsRefused' -e 'isClosedPaneFenced' src/main/persistence | head -160
printf '%s\n' '--- relevant current/base diff paths ---'
git diff --no-ext-diff --unified=20 51530bb86cb2f799fb5162840654c64d9ff48fb2 e7691636da67006737d83f6bb67b78315b2324f2 -- src/main/persistence/terminal-topology/closed-pane-binding-fence.ts src/main/persistence/loading-store/pty-binding-refusals.ts src/main/persistence/loading-store/pty-binding-persistence.ts

Repository: stablyai/orca

Length of output: 44209


🏁 Script executed:

set -e
printf '%s\n' '--- resolveHostId and partition operations ---'
nl -ba src/main/persistence/loading-store/session-host-partitions.ts | sed -n '1,155p'
printf '%s\n' '--- topology fence producer ---'
nl -ba src/main/persistence/terminal-topology/terminal-topology-commit.ts | sed -n '1,175p'
printf '%s\n' '--- binding callers and host arguments ---'
rg -n -F --glob '*.ts' -e 'persistPtyBinding(' -e 'mayCreate: false' -e 'origin:' src/main | head -240
printf '%s\n' '--- focused binding refusal tests ---'
rg -n -F --glob '*.test.ts' -e 'pty-binding-refusals' -e 'binding is refused' -e 'late' -e 'mayCreate' -e 'legacy' src/main/persistence/loading-store src/main/persistence/terminal-topology | head -220

Repository: stablyai/orca

Length of output: 41528


🏁 Script executed:

set -e
printf '%s\n' '--- focused fence persistence tests ---'
nl -ba src/main/persistence/persistence-closed-pane-binding-fence.test.ts | sed -n '1,260p'
printf '%s\n' '--- fence unit tests after the supplied excerpt ---'
nl -ba src/main/persistence/terminal-topology/closed-pane-binding-fence.test.ts | sed -n '1,145p'
printf '%s\n' '--- partition type and binding persistence continuation ---'
rg -n -F --glob '*.ts' -e 'export type TerminalSessionPartition' -e 'type TerminalSessionPartition' src
nl -ba src/main/persistence/loading-store/pty-binding-persistence.ts | sed -n '145,190p'

Repository: stablyai/orca

Length of output: 7638


Scope fence entries by host, but check all host partitions during refusal.

A close in one host can record tabId:leafId, and an admission in another partition can remove that shared entry. A later binding in the closed partition can then pass isClosedPaneFenced and recreate the pane.

Do not limit refusal lookup to the binding host. Legacy relay reattach can place the binding in a different host from the close, so the lookup must check the scoped fence for every partition.

Suggested fix
diff --git a/src/main/persistence/terminal-topology/closed-pane-binding-fence.ts b/src/main/persistence/terminal-topology/closed-pane-binding-fence.ts
@@
 const closedAtByPaneKey = new Map<string, number>()
 
+function scopedPaneKey(hostId: ExecutionHostId, paneKey: string): string {
+  return JSON.stringify([hostId, paneKey])
+}
+
@@
-          keys.add(`${tabId}:${leafId}`)
+          keys.add(scopedPaneKey(hostId, `${tabId}:${leafId}`))
@@
-export function isClosedPaneFenced(paneKey: string): boolean {
-  const closedAt = closedAtByPaneKey.get(paneKey)
+export function isClosedPaneFenced(hostId: ExecutionHostId, paneKey: string): boolean {
+  const closedAt = closedAtByPaneKey.get(scopedPaneKey(hostId, paneKey))
   if (closedAt === undefined) {
     return false
   }
   if (Date.now() - closedAt >= CLOSED_PANE_BINDING_FENCE_MS) {
-    closedAtByPaneKey.delete(paneKey)
+    closedAtByPaneKey.delete(scopedPaneKey(hostId, paneKey))
     return false
   }
   return true
 }
diff --git a/src/main/persistence/loading-store/pty-binding-refusals.ts b/src/main/persistence/loading-store/pty-binding-refusals.ts
@@
-    isClosedPaneFenced(paneKey)
+    partitions.some(({ hostId }) => isClosedPaneFenced(hostId, paneKey))

@Jinwoo-H
Jinwoo-H merged commit f6cc8f6 into main Oct 19, 2026
56 of 57 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