Skip to content

Preserve local SSH terminal splits during reconnect - #22991

Merged
OrcaWin merged 1 commit into
mainfrom
OrcaWin/np-ssh-layout-review-finish
Sep 26, 2026
Merged

OrcaWin merged 1 commit into
mainfrom
OrcaWin/np-ssh-layout-review-finish

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Sep 26, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 5 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​197 $\color{#cf222e}{\Huge{\mathbf{−}}}$​5 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​192
Prod 13 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​134 $\color{#cf222e}{\Huge{\mathbf{−}}}$​10 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​124

ELI5

When a remote workspace reconnects while you are splitting a terminal, the reconnect can briefly report the older one-pane layout. Orca now keeps the local split until that exact edit has been uploaded to the SSH host.

What Changed

Before, a same-generation SSH snapshot could replace a local structural terminal layout before the normal 150ms session upload, reopening the tab as one pane while the remote shells remained alive. The renderer now records each local structural SSH layout edit with its target and a fresh entry identity, preserves those tabs during snapshot merge, acknowledges successful uploads from both normal persistence and blank-host seeding, and drops entries when tabs, worktrees, or SSH targets are removed. Host snapshots resume normal ownership after the matching upload acknowledgement.

Why

The fix stays in the existing client-side snapshot merge and upload acknowledgement paths, so it requires no new wire fields or broad sync redesign. Target-scoped entry identity prevents a delayed acknowledgement from clearing a newer edit, including reused roots and null roots, while still allowing legitimate host replacements after acknowledgement.

Linked Issue

Fixes #2323

Visual Proof

N/A — this is a state-recovery fix with no visual styling or interaction change.

Testing

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

Focused Vitest: remote-workspace-target-sync.test.ts, remote-workspace-snapshot-apply-deferred-session-write.test.ts, terminal-write-identity-bailouts.test.ts, and ssh-target-cleanup.test.ts (43 tests passed).

pnpm tc:web and pnpm run check:code-quality:changed passed.

Review

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

The change preserves current process authority and remote mixed-version wire contracts. It does not change resource bounds or claim to fix unrelated PTY-count regressions.

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)

@OrcaWin
OrcaWin force-pushed the OrcaWin/np-ssh-layout-review-finish branch from 66bfe29 to 6d4b493 Compare September 26, 2026 02:16

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

ℹ️ Minor suggestions only — one guard gap and two small follow-ups.

Reviewed changes

  • Pending local-edit ledger — pendingDirectSshLayoutEditsByTabId records { targetId, root } for each local structural terminal-layout edit on an SSH-hosted workspace, and is cleared on tab/worktree/target teardown (terminal-layout-state.ts, terminal-state.ts, and the teardown paths).
  • Preserve through snapshot merge — direct-SSH snapshot apply passes the pending-layout tab ids into mergeDirectSshRemoteWorkspaceSession, so a local split wins over the host's older layout (remote-workspace-snapshot-apply.ts, remote-workspace-session-merge.ts).
  • Acknowledge on upload — both upload paths clear a pending entry once the host echoes back a matching root, returning ownership to the host (use-app-session-persistence.ts, remote-workspace-target-sync.ts).
  • Tests — merge preservation with a missing/outdated host layout, reference- and target-guarded acknowledgement, and target-scoped cleanup.

ℹ️ Nitpicks

  • buildOrphanTerminalCleanupPatch (store/slices/terminal-orphan-helpers.ts) deletes tab rows and terminalLayoutsByTabId entries but not pendingDirectSshLayoutEditsByTabId, unlike the teardown paths this PR updates. It is a session-bounded leak with no behavioral impact (the preserve set is live-tab-scoped), so it is optional — but adding the same omission keeps the sweep families in lockstep.
  • No test exercises the acknowledgement round-trip end to end (upload echoes the root → entry clears → a later host layout can win). The slice-level test pins the identity/target guard, but the two ack call sites and the clear itself are untested, so a regression there would not trip CI.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@OrcaWin
OrcaWin force-pushed the OrcaWin/np-ssh-layout-review-finish branch from 6d4b493 to 24ada45 Compare September 26, 2026 02:21

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

✅ No new issues found.

Reviewed changes (delta since the prior pullfrog review at 66bfe29)

  • Target-scoped preservation — the snapshot-apply preserve set now filters on pendingDirectSshLayoutEditsByTabId[tab.id]?.targetId === authority.targetId, matching the two acknowledgement checks. This closes the stale-host-entry veto raised in the previous review.
  • Pre-push capture in the blank-host acknowledgement — remote-workspace-target-sync.ts now captures pendingDirectSshLayoutEditsByTabId before the upload and acknowledges the captured references, so a newer same-root (e.g. null-root) edit arriving during the async push is no longer cleared by the delayed ack.
  • Coverage — new test remote-workspace-target-sync.test.ts:48 pins the captured-identity acknowledgement; the harness now seeds pendingDirectSshLayoutEditsByTabId and acknowledgeDirectSshLayoutEdits.

All four focused test files pass locally (44 tests). The two non-blocking notes from the prior review (capture-time leaf normalization and the orphan-cleanup omission) still stand but are unchanged by this delta.

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

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

✅ No new issues found.

Reviewed changes (delta since the prior pullfrog review at 6d4b493)

  • Test-only refinement — remote-workspace-target-sync.test.ts now declares its own acknowledgeDirectSshLayoutEdits = vi.fn() and asserts the acknowledged call argument (firstEdit) directly, rather than relying on the harness default mock. Behavior under test is unchanged; the assertion is marginally more explicit.
  • Production code is unchanged from the previous review, where the target-scoped preserve filter and the pre-push capture of pendingDirectSshLayoutEditsByTabId were verified.

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@coderabbitai

coderabbitai Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

📝 Walkthrough

Walkthrough

The renderer now tracks structural terminal layout edits by tab and SSH target. Snapshot merging preserves layouts for tabs with pending edits. Successful uploads acknowledge captured edits only when the target and uploaded layout match. Target, tab, and worktree cleanup removes associated pending edits. Tests cover upload races and reconnects with missing or outdated remote layouts.

Priority: ➖ Normal

Merge Risk: 🔵 Low · up to 24ada

A narrow reconnect case can leave a local terminal layout protected after it has been uploaded. The issue is recoverable through another structural edit or tab deletion, but should be fixed or accepted before merging.

Security Architecture Review

Security architecture risk: 🔵 Low · up to 24ada

The change is limited to terminal layout synchronization and does not show a new remotely reachable operation or a verified security finding. Its protection appears to depend on state that may not survive an application restart, so recovery remains uncertain.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The evidenced change affects a renderer’s target-scoped terminal layout authority. The inspected acknowledgement action does not itself expose a network operation or change credentials or host execution authority.

Trust Boundaries and Controls

  • observed — Snapshot application checks current arrival and preparation authority before merge; upload acknowledgement requires successful target-matching results and leaves newer pending entries intact.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 6.25% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 16 functions across 18 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the primary change: preserving local SSH terminal splits during reconnect.
Description check ✅ Passed The description includes all required sections, links issue #2323, explains the user impact and implementation, documents testing, and marks visual proof as not applicable. The final checklist leaves …
Linked Issues check ✅ Passed For issue #2323, this PR addresses the relevant snapshot-conflict and stale-view reliability objective. It records structural Direct SSH layout edits, preserves affected tab layouts during snapshot me…
Out of Scope Changes check ✅ Passed The changed production files implement the issue #2323 snapshot reliability behavior. The changed tests verify preservation, acknowledgement, and cleanup behavior. The state initialization and teardow…
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR

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: cabc8c39-afda-4a63-a269-5b5ed8271018

📥 Commits

Reviewing files that changed from the base of the PR and between fa018fe and 24ada45.

📒 Files selected for processing (18)
  • src/renderer/src/app-shell/use-app-session-persistence.ts
  • src/renderer/src/hooks/__tests__/remote-workspace-target-sync-test-harness.ts
  • src/renderer/src/hooks/remote-workspace-session-merge.ts
  • src/renderer/src/hooks/remote-workspace-snapshot-apply-deferred-session-write.test.ts
  • src/renderer/src/hooks/remote-workspace-snapshot-apply.ts
  • src/renderer/src/hooks/remote-workspace-target-sync.test.ts
  • src/renderer/src/hooks/remote-workspace-target-sync.ts
  • src/renderer/src/store/slices/ssh-target-cleanup.test.ts
  • src/renderer/src/store/slices/ssh-target-cleanup.ts
  • src/renderer/src/store/slices/terminal-write-identity-bailouts.test.ts
  • src/renderer/src/store/slices/terminals.ts
  • src/renderer/src/store/slices/worktrees/teardown/remove-worktree-store-cleanup.ts
  • src/renderer/src/store/slices/worktrees/teardown/worktree-purge-state.ts
  • src/renderer/src/store/terminals/terminal-actions.ts
  • src/renderer/src/store/terminals/terminal-layout-state.ts
  • src/renderer/src/store/terminals/terminal-state.ts
  • src/renderer/src/store/terminals/terminal-tab-close.ts
  • src/renderer/src/store/terminals/workspace-terminal-hydration-patch.ts

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

Comment on lines +124 to +140
const structuralEdit = !terminalLayoutNodeEqual(existing?.root, normalized.snapshot.root)
const workspaceId = structuralEdit
? Object.keys(s.tabsByWorktree).find((id) =>
s.tabsByWorktree[id].some((tab) => tab.id === tabId)
)
: undefined
const tracksRemoteEdit = workspaceId && getConnectionIdFromState(s, workspaceId)
return {
terminalLayoutsByTabId: { ...s.terminalLayoutsByTabId, [tabId]: normalized.snapshot }
terminalLayoutsByTabId: { ...s.terminalLayoutsByTabId, [tabId]: normalized.snapshot },
...(tracksRemoteEdit
? {
pendingDirectSshLayoutEditsByTabId: {
...s.pendingDirectSshLayoutEditsByTabId,
[tabId]: { targetId: tracksRemoteEdit, root: normalized.snapshot.root }
}
}
: {})

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 | 🟡 Minor | ⚡ Quick win

🔎 Supported by static analysis

🏁 Script executed:

sed -n '70,155p' src/renderer/src/store/terminals/terminal-layout-state.ts
rg -n 'normalizeTerminalLayoutSnapshot|pendingDirectSshLayoutEditsByTabId|hydrateWorkspaceSession' src/renderer/src/store/terminals src/renderer/src/hooks/remote-workspace-snapshot-apply.ts src/renderer/src/app-shell/use-app-session-persistence.ts

Repository: stablyai/orca

Length of output: 7003


🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- terminal layout state imports and definitions ---'
sed -n '1,155p' src/renderer/src/store/terminals/terminal-layout-state.ts
printf '%s\n' '--- normalization definitions and usages ---'
rg -n -C 8 'function normalizeTerminalLayoutSnapshot|const normalizeTerminalLayoutSnapshot|normalizeTerminalLayoutSnapshot|normalizeTerminalLayoutPtyOwnership' src/renderer/src/store/terminals
printf '%s\n' '--- remote snapshot application ---'
sed -n '145,225p' src/renderer/src/hooks/remote-workspace-snapshot-apply.ts
printf '%s\n' '--- hydration implementation ---'
sed -n '1,240p' src/renderer/src/store/terminals/workspace-terminal-hydration.ts
printf '%s\n' '--- hydration patch ---'
sed -n '1,230p' src/renderer/src/store/terminals/workspace-terminal-hydration-patch.ts
printf '%s\n' '--- acknowledgement callers ---'
rg -n -C 10 'acknowledgeDirectSshLayoutEdits|pendingDirectSshLayoutEditsByTabId' src/renderer/src --glob '!**/*.test.*'
printf '%s\n' '--- PR diff summary and changed file diff ---'
git diff --stat e785cacb67ac0fbc69cc9aa41ddee3af6fa08ce3 24ada4517df1dfae93c5115febb3efdfc0c109bb -- src/renderer/src/store/terminals/terminal-layout-state.ts src/renderer/src/hooks/remote-workspace-snapshot-apply.ts src/renderer/src/store/terminals/workspace-terminal-hydration.ts src/renderer/src/store/terminals/workspace-terminal-hydration-patch.ts
git diff --unified=35 e785cacb67ac0fbc69cc9aa41ddee3af6fa08ce3 24ada4517df1dfae93c5115febb3efdfc0c109bb -- src/renderer/src/store/terminals/terminal-layout-state.ts src/renderer/src/hooks/remote-workspace-snapshot-apply.ts

Repository: stablyai/orca

Length of output: 41793


🤖 get_repo_knowledge executed:

get_repo_knowledge stablyai/orca /tmp/coderabbit-repo-knowledge/stablyai-orca-89dc44e4/learnings /tmp/coderabbit-repo-knowledge/stablyai-orca-89dc44e4/conventions

Length of output: 36929


🏁 Script executed:

#!/bin/bash
set -e
sed -n '1,155p' src/renderer/src/store/terminals/terminal-layout-state.ts
rg -n -C 12 'normalizeTerminalLayoutSnapshot|normalizeTerminalLayoutPtyOwnership|pendingDirectSshLayoutEditsByTabId|acknowledgeDirectSshLayoutEdits' src/renderer/src/store/terminals src/renderer/src/hooks/remote-workspace-snapshot-apply.ts
sed -n '150,220p' src/renderer/src/hooks/remote-workspace-snapshot-apply.ts
sed -n '1,230p' src/renderer/src/store/terminals/workspace-terminal-hydration.ts
sed -n '1,220p' src/renderer/src/store/terminals/workspace-terminal-hydration-patch.ts
rg -n -C 12 'acknowledgeDirectSshLayoutEdits' src/renderer/src
git diff --stat e785cacb67ac0fbc69cc9aa41ddee3af6fa08ce3 24ada4517df1dfae93c5115febb3efdfc0c109bb -- src/renderer/src/store/terminals/terminal-layout-state.ts src/renderer/src/hooks/remote-workspace-snapshot-apply.ts src/renderer/src/store/terminals/workspace-terminal-hydration.ts src/renderer/src/store/terminals/workspace-terminal-hydration-patch.ts
git diff --unified=35 e785cacb67ac0fbc69cc9aa41ddee3af6fa08ce3 24ada4517df1dfae93c5115febb3efdfc0c109bb -- src/renderer/src/store/terminals/terminal-layout-state.ts src/renderer/src/hooks/remote-workspace-snapshot-apply.ts

Repository: stablyai/orca

Length of output: 42485


🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- merge definition and callers ---'
rg -n -C 18 'function mergeDirectSshRemoteWorkspaceSession|const mergeDirectSshRemoteWorkspaceSession|mergeDirectSshRemoteWorkspaceSession' src/renderer/src
printf '%s\n' '--- leaf-id normalization definition ---'
rg -n -C 25 'normalizeTerminalLayoutSnapshot' src/renderer/src/components src/renderer/src/lib src/shared
printf '%s\n' '--- layout plan full relevant range ---'
sed -n '1,150p' src/renderer/src/store/terminals/workspace-terminal-layout-plan.ts
printf '%s\n' '--- hydration patch relevant ranges ---'
sed -n '130,205p' src/renderer/src/store/terminals/workspace-terminal-hydration-patch.ts
printf '%s\n' '--- upload capture and both acknowledgement paths ---'
sed -n '80,185p' src/renderer/src/app-shell/use-app-session-persistence.ts
sed -n '175,230p' src/renderer/src/hooks/remote-workspace-target-sync.ts
printf '%s\n' '--- relevant type definitions ---'
rg -n -C 10 'DirectSshLayoutEdit|TerminalLayoutSnapshot|leafId' src/shared/terminal-tab-types.ts src/renderer/src/store/terminals/terminal-state.ts src/renderer/src/components/terminal-pane/terminal-layout-leaf-ids.ts

Repository: stablyai/orca

Length of output: 42045


🏁 Script executed:

#!/bin/bash
set -e
rg -n -C 18 'function mergeDirectSshRemoteWorkspaceSession|const mergeDirectSshRemoteWorkspaceSession|mergeDirectSshRemoteWorkspaceSession' src/renderer/src
rg -n -C 25 'normalizeTerminalLayoutSnapshot' src/renderer/src/components src/renderer/src/lib src/shared
sed -n '1,150p' src/renderer/src/store/terminals/workspace-terminal-layout-plan.ts
sed -n '130,205p' src/renderer/src/store/terminals/workspace-terminal-hydration-patch.ts
sed -n '80,185p' src/renderer/src/app-shell/use-app-session-persistence.ts
sed -n '175,230p' src/renderer/src/hooks/remote-workspace-target-sync.ts
rg -n -C 10 'DirectSshLayoutEdit|TerminalLayoutSnapshot|leafId' src/shared/terminal-tab-types.ts src/renderer/src/store/terminals/terminal-state.ts src/renderer/src/components/terminal-pane/terminal-layout-leaf-ids.ts

Repository: stablyai/orca

Length of output: 42647


🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- direct SSH merge body ---'
sed -n '29,240p' src/renderer/src/hooks/remote-workspace-session-merge.ts
printf '%s\n' '--- layout plan ---'
sed -n '12,100p' src/renderer/src/store/terminals/workspace-terminal-layout-plan.ts
printf '%s\n' '--- hydration layout-plan invocation and patch ---'
rg -n -C 12 'buildWorkspaceTerminalLayoutPlan|targetScopedWorkspaceHydrationPatch|terminalLayoutsByTabId' src/renderer/src/store/terminals/workspace-terminal-hydration.ts src/renderer/src/store/terminals/workspace-terminal-hydration-patch.ts
printf '%s\n' '--- persistence capture helper and upload path ---'
rg -n -C 18 'captureUploadedDirectSshLayoutEdits|pendingLayoutEdits|setForConnectedTargets|acknowledgeDirectSshLayoutEdits' src/renderer/src/app-shell/use-app-session-persistence.ts src/renderer/src/hooks/remote-workspace-target-sync.ts

Repository: stablyai/orca

Length of output: 42109


🏁 Script executed:

#!/bin/bash
set -e
sed -n '29,240p' src/renderer/src/hooks/remote-workspace-session-merge.ts
sed -n '12,100p' src/renderer/src/store/terminals/workspace-terminal-layout-plan.ts
rg -n -C 12 'buildWorkspaceTerminalLayoutPlan|targetScopedWorkspaceHydrationPatch|terminalLayoutsByTabId' src/renderer/src/store/terminals/workspace-terminal-hydration.ts src/renderer/src/store/terminals/workspace-terminal-hydration-patch.ts
rg -n -C 18 'captureUploadedDirectSshLayoutEdits|pendingLayoutEdits|setForConnectedTargets|acknowledgeDirectSshLayoutEdits' src/renderer/src/app-shell/use-app-session-persistence.ts src/renderer/src/hooks/remote-workspace-target-sync.ts

Repository: stablyai/orca

Length of output: 41868


🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- remaining merge layout/session sweeps ---'
sed -n '240,430p' src/renderer/src/hooks/remote-workspace-session-merge.ts
printf '%s\n' '--- setTabLayout callers ---'
rg -n -C 8 'setTabLayout\(' src/renderer/src --glob '!**/*.test.*'

Repository: stablyai/orca

Length of output: 42041


🏁 Script executed:

#!/bin/bash
set -e
sed -n '240,430p' src/renderer/src/hooks/remote-workspace-session-merge.ts
rg -n -C 8 'setTabLayout\(' src/renderer/src --glob '!**/*.test.*'

Repository: stablyai/orca

Length of output: 41238


Normalize leaf IDs before recording the pending edit.

setTabLayout normalizes PTY ownership but not leaf IDs. Hydration later normalizes the preserved layout and can rewrite duplicate or invalid leaf IDs. The pending root then differs from the uploaded root, so both acknowledgement paths retain the pending entry. Later snapshots can continue preserving the local layout until another structural edit or tab deletion.

Suggested fix
+import { normalizeTerminalLayoutSnapshot } from '`@/components/terminal-pane/terminal-layout-leaf-ids`'
 import {
-  normalizeTerminalLayoutPtyOwnership,
   resolveTerminalLayoutPtyOwnershipTransfers
 } from '`@/components/terminal-pane/terminal-layout-pty-ownership`'
...
-        const normalized = normalizeTerminalLayoutPtyOwnership(layout)
+        const normalized = normalizeTerminalLayoutSnapshot(layout)

This correction is separate from the target-scoping fix. Target scoping preserves the local layout during merge; it does not update the pending root after hydration normalization.

@OrcaWin
OrcaWin merged commit a00c424 into main Sep 26, 2026
35 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.

[Feature]: SSH remote workspace sync - follow-up work from the original PR #1690 / rewrite PR #1876

1 participant