Repository navigation
fix(remote-workspace): keep local tabs when a pulled snapshot reports none for a populated worktree - #12751
fix(remote-workspace): keep local tabs when a pulled snapshot reports none for a populated worktree#12751robertnisipeanu wants to merge 4 commits into
Conversation
📝 WalkthroughWalkthroughThe change narrows Direct SSH workspace replacement to worktrees with remote tabs or without local tabs. Remote tabs and related metadata are filtered to the narrowed scope. Snapshot application uses this scope for merging, recovery-tab selection, hydration, and terminal reconnection. Tests cover local-state preservation, remote replacement, empty or omitted worktrees, PTY mappings, and independent multi-worktree behavior. Merge Risk: ⚪ Minimal · up to The change prevents a zero-tab remote snapshot from deleting visible local tabs. Remaining risk is limited to strengthening the regression test to assert the narrowed replacement scope directly; no actionable merge-blocking product risk remains. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
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.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/renderer/src/hooks/remote-workspace-session-merge.ts (1)
17-25: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winCondense the rationale comment.
Keep the data-loss rationale. Reduce this comment to one or two non-obvious sentences.
As per coding guidelines, TypeScript comments must be concise, non-obvious, and brief.
Source: Coding guidelines
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: f7f16fa4-645f-4d03-9557-51a6eb5cdb33
📥 Commits
Reviewing files that changed from the base of the PR and between eea0bb6 and 9cde77be0af4e9b752d1a9b4f768f3314aa0bdb7.
📒 Files selected for processing (3)
src/renderer/src/hooks/remote-workspace-session-merge.test.tssrc/renderer/src/hooks/remote-workspace-session-merge.tssrc/renderer/src/hooks/remote-workspace-snapshot-apply.ts
|
Reviewed reproduce-first. Verdict: merge with two commits on top. The direction and the implementation are both right — this is a real P0 and this patch fixes it. Reproduced red on main firstTwo levels deliberately, because the issue makes two claims and a merge-level unit can only express one of them. The second describe drives the real I tried to find a better design and the field evidence killed it
But the poisoned snapshot in #12721 held explicit Two gaps, both fixed on top1. The tests don't cover the hydration path this PR fixes. Revert the single 2. The merge boundary stays individually unsafe. Call Splitting narrowing into an exported pure function was the right factoring, since the caller genuinely needs the narrowed set as a value for One thing the PR body overstates"A genuinely emptied worktree converges once the remote reports any tab for it" is true only for the non-empty case. Multi-client — two laptops against one SSH box, which is the point of the shared snapshot — if client B closes every tab in W while A still holds them, A protects, A pushes, and B's tabs resurrect. That never converges. I think that's the right trade (visible and one-click recoverable beats silent and permanent), but the honest framing is a bounded regression, not convergence. The real fix is tombstones plus a monotonic per-scope revision, already tracked as #12447. Sibling paths, named not fixedThe paired remote runtime already does this correctly and is the model to copy:
Folder workspaces are structurally exempt: Note on CIOnly |
|
Thanks for the reproduce-first review — and for recording the I've pushed both commits myself, so no need to duplicate the work:
I also condensed the rationale block above On the convergence claim: you're right, and the PR body now frames it as a bounded regression. Your multi-client case holds as stated — A protects, A pushes, B's tabs come back, and nothing in this patch breaks that loop. One refinement rather than a pushback: the loop ends as soon as the tabs are closed on the holding client too, so it is "does not converge on its own" rather than "unrecoverable". That doesn't change the trade or the framing, and #12447 is still the real fix. On the CodeRabbit thread at CI: locally |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/renderer/src/hooks/remote-workspace-snapshot-empty-worktree-guard.test.ts (1)
1-11: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueReduce the file header comment.
Keep only the non-obvious reason that this test uses the full snapshot application path. The remaining text repeats the test behavior.
Proposed change
-/** - * Drives the real snapshot apply against a real store. A pulled snapshot that - * reports no tabs for a worktree the renderer is showing tabs in must leave that - * worktree's live terminals untouched, while a worktree the snapshot does carry - * tabs for still replaces. - * - * The merge alone cannot express this: a merge that protects the worktree still - * loses its live PTY bindings if the worktree stays in the hydration replace - * scope, so the assertions below read post-apply store state and the scope handed - * to reconnect rather than the merge result. - */ +/** + * Test full snapshot application because merge-only tests cannot verify + * hydration and terminal reconnection scope. + */Source: Coding guidelines
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 0fb680e0-f35b-4c55-8f9b-ed98a7410f7c
📥 Commits
Reviewing files that changed from the base of the PR and between 9cde77be0af4e9b752d1a9b4f768f3314aa0bdb7 and d6a45d7a41142b9abc0cf1e8ec2a1dfab7c0250a.
📒 Files selected for processing (3)
src/renderer/src/hooks/remote-workspace-session-merge.test.tssrc/renderer/src/hooks/remote-workspace-session-merge.tssrc/renderer/src/hooks/remote-workspace-snapshot-empty-worktree-guard.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- src/renderer/src/hooks/remote-workspace-session-merge.ts
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: bb17a925-fcb8-4205-bab2-32bedc29da0b
📥 Commits
Reviewing files that changed from the base of the PR and between d6a45d7a41142b9abc0cf1e8ec2a1dfab7c0250a and 13798e4a94934c3d4ce13dd358b705abb9b9bfc3.
📒 Files selected for processing (4)
new-pr-body.mdreply-to-coderabbit-active-selection.mdreply-to-jinwoo.mdsrc/renderer/src/hooks/remote-workspace-session-merge.test.ts
|
|
||
| **`test(remote-workspace): cover the snapshot apply path…`** — drives `applyDirectSshRemoteWorkspaceSnapshot` against a real store and asserts on post-apply state: the protected worktree keeps its live PTY bindings, stays out of `pendingReconnectWorktreeIds`, and is absent from the workspace keys handed to `reconnectPersistedTerminals`, while the worktree the snapshot does carry tabs for still replaces. Reverting the single `replaceWorkspaceKeys` line fails it, with the merge tests all still green — your point exactly. | ||
|
|
||
| **`fix(remote-workspace): narrow the replace scope inside the merge`** — `mergeDirectSshRemoteWorkspaceSession` re-applies the narrowing itself. It is filter idempotence rather than a happy accident: the predicate depends only on `(worktreeId, current, remote, liveTabsByWorktree)` and never on the input set, so `narrow(narrow(X)) === narrow(X)` for any `X`. The caller still computes the narrowed set once because it genuinely needs it as a value for `replaceWorkspaceKeys`, and pays nothing for the second application. |
There was a problem hiding this comment.
🚀 Performance & Scalability | 🟡 Minor | ⚡ Quick win
Correct the statement about the second narrowing pass.
Idempotence removes the second pass's semantic effect. It does not make the pass free. The caller still incurs filtering and possible allocation costs unless the implementation explicitly short-circuits the operation.
Use “has no additional semantic effect” instead of “pays nothing.”
Suggested wording
- The caller still computes the narrowed set once because it genuinely needs it as a value for `replaceWorkspaceKeys`, and pays nothing for the second application.
+ The caller still computes the narrowed set once because it genuinely needs it as a value for `replaceWorkspaceKeys`; the second application has no additional semantic effect.📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| **`fix(remote-workspace): narrow the replace scope inside the merge`** — `mergeDirectSshRemoteWorkspaceSession` re-applies the narrowing itself. It is filter idempotence rather than a happy accident: the predicate depends only on `(worktreeId, current, remote, liveTabsByWorktree)` and never on the input set, so `narrow(narrow(X)) === narrow(X)` for any `X`. The caller still computes the narrowed set once because it genuinely needs it as a value for `replaceWorkspaceKeys`, and pays nothing for the second application. | |
| **`fix(remote-workspace): narrow the replace scope inside the merge`** — `mergeDirectSshRemoteWorkspaceSession` re-applies the narrowing itself. It is filter idempotence rather than a happy accident: the predicate depends only on `(worktreeId, current, remote, liveTabsByWorktree)` and never on the input set, so `narrow(narrow(X)) === narrow(X)` for any `X`. The caller still computes the narrowed set once because it genuinely needs it as a value for `replaceWorkspaceKeys`; the second application has no additional semantic effect. |
… none for a populated worktree Co-Authored-By: Claude Fable 5 <[email protected]>
…the remote reports no tabs for Drives applyDirectSshRemoteWorkspaceSnapshot against a real store and asserts on post-apply state: the protected worktree keeps its live PTY bindings, stays out of pendingReconnectWorktreeIds, and is absent from the workspace keys handed to reconnectPersistedTerminals, while the worktree the snapshot does carry tabs for still replaces. The merge-level tests cannot express this. They exercise the merge in isolation, so a hydration scope that still contains the protected worktree resets its tabs and sweeps them into PTY reconnect with every merge assertion green. Co-Authored-By: Claude Fable 5 <[email protected]>
mergeDirectSshRemoteWorkspaceSession narrows the requested replace scope itself rather than relying on callers to have narrowed it. Narrowing is idempotent, so the production caller — which still needs the narrowed set as a value for replaceWorkspaceKeys — is unaffected, and no caller can delete a populated worktree's tabs by handing the merge a wider scope. Co-Authored-By: Claude Fable 5 <[email protected]>
…ent snapshots Co-Authored-By: Claude Fable 5 <[email protected]>
13798e4 to
306573a
Compare
|
Note GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer. |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 0ce3a933-3a31-496b-9e1e-a8de7f163ac0
📒 Files selected for processing (4)
src/renderer/src/hooks/remote-workspace-session-merge.test.tssrc/renderer/src/hooks/remote-workspace-session-merge.tssrc/renderer/src/hooks/remote-workspace-snapshot-apply.tssrc/renderer/src/hooks/remote-workspace-snapshot-empty-worktree-guard.test.ts
🚧 Files skipped from review as they are similar to previous changes (3)
- src/renderer/src/hooks/remote-workspace-snapshot-apply.ts
- src/renderer/src/hooks/remote-workspace-snapshot-empty-worktree-guard.test.ts
- src/renderer/src/hooks/remote-workspace-session-merge.ts
| it('keeps local tabs for an in-scope worktree the remote snapshot omits entirely', () => { | ||
| const localTabs = [tab('tab-1', WT)] | ||
| const expectedTabs = structuredClone(localTabs) | ||
| const current = session({ tabsByWorktree: { [WT]: localTabs } }) | ||
| const remote = session() | ||
|
|
||
| const { merged } = narrowAndMerge(current, remote, new Set([WT]), { [WT]: localTabs }) | ||
|
|
||
| expect(merged.tabsByWorktree[WT]).toEqual(expectedTabs) |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Assert the narrowed scope for an omitted remote worktree.
This test only checks the final merge result. mergeDirectSshRemoteWorkspaceSession reapplies narrowing internally. If narrowDirectSshReplaceWorktreeIds fails to remove WT, this test can still pass. Capture replaceWorktreeIds and assert that it is empty.
The PR objective requires omitted remote worktree records to preserve local state.
Proposed test change
- const { merged } = narrowAndMerge(current, remote, new Set([WT]), { [WT]: localTabs })
+ const { merged, replaceWorktreeIds } = narrowAndMerge(current, remote, new Set([WT]), {
+ [WT]: localTabs
+ })
+ expect([...replaceWorktreeIds]).toEqual([])
expect(merged.tabsByWorktree[WT]).toEqual(expectedTabs)📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| it('keeps local tabs for an in-scope worktree the remote snapshot omits entirely', () => { | |
| const localTabs = [tab('tab-1', WT)] | |
| const expectedTabs = structuredClone(localTabs) | |
| const current = session({ tabsByWorktree: { [WT]: localTabs } }) | |
| const remote = session() | |
| const { merged } = narrowAndMerge(current, remote, new Set([WT]), { [WT]: localTabs }) | |
| expect(merged.tabsByWorktree[WT]).toEqual(expectedTabs) | |
| it('keeps local tabs for an in-scope worktree the remote snapshot omits entirely', () => { | |
| const localTabs = [tab('tab-1', WT)] | |
| const expectedTabs = structuredClone(localTabs) | |
| const current = session({ tabsByWorktree: { [WT]: localTabs } }) | |
| const remote = session() | |
| const { merged, replaceWorktreeIds } = narrowAndMerge(current, remote, new Set([WT]), { | |
| [WT]: localTabs | |
| }) | |
| expect([...replaceWorktreeIds]).toEqual([]) | |
| expect(merged.tabsByWorktree[WT]).toEqual(expectedTabs) |
🧰 Tools
🪛 ast-grep (0.45.1)
[warning] 113-113: Enforce overriding default config
Context: session({ tabsByWorktree: { [WT]: localTabs } })
Note: [CWE-1188] Insecure Default Initialization of Resource (default session cookie name not overridden).
(default-session-config-typescript)
|
Superseded by #14844: the session merge on main now unions the remote snapshot's worktree keys with replace-scope worktrees that hold local tabs, and its host-unknown branch grafts local tabs the snapshot does not carry — so a pulled snapshot reporting zero tabs for a populated worktree no longer erases the local tabs. remote-workspace-session-merge-local-survival.test.ts and remote-workspace-snapshot-local-tab-survival.test.ts pin that behavior. |
Fixes #12748
A pulled direct-SSH workspace snapshot that carries zero tabs for a worktree the renderer is showing tabs in leaves those tabs in place.
narrowDirectSshReplaceWorktreeIdsremoves such worktrees from the replace scope: local state wins until the remote reports at least one tab for the worktree. The snapshot carries no tombstones, so "no tabs" is indistinguishable from a regressed or corrupted snapshot — honoring it deletes terminal surfaces the user can see, unrecoverably, while keeping local state leaves surfaces the user can still close.The narrowed scope drives every step of the apply: the merge (remote per-worktree records are filtered to the scope, so a spread cannot undo the narrowing),
replaceWorkspaceKeysforhydrateWorkspaceSession/hydrateTabsSession, andreconnectPersistedTerminals— a protected worktree left in the hydration scope would still get its live tabs reset and swept into PTY reconnect.mergeDirectSshRemoteWorkspaceSessionapplies the narrowing itself rather than relying on the caller to have done it. The narrowing predicate depends only on the worktree id and the three session inputs, never on the input set, so applying it twice is a no-op: the caller — which still needs the narrowed set as a value forreplaceWorkspaceKeys— pays nothing, and no caller can delete a populated worktree's tabs by handing the merge a wider scope.Worktrees with tabs on both sides are unaffected; the remote stays authoritative for them.
Bounded regression, not convergence
Single-client this converges: the next push re-exports the protected worktree, and a genuinely emptied worktree replaces once the remote reports any tab for it.
Multi-client it does not. Two clients against one SSH box is the point of a shared snapshot: if client B closes every tab in worktree W while client A still holds them, A protects, A pushes, and B's tabs come back. That repeats until the tabs are closed on the holding client too.
The trade is deliberate — resurrected tabs are visible and one click to close, deleted tabs are silent and permanent — but it is a bounded regression, not convergence. Real convergence needs tombstones plus a monotonic per-scope revision, tracked in #12447.
Testing
pnpm run typecheck:webandnpx oxlintare clean; the renderer hook, store, and lib vitest suites pass.Coverage runs at two levels.
remote-workspace-session-merge.test.tspins the merge, including that it narrows a replace scope handed to it un-narrowed.remote-workspace-snapshot-empty-worktree-guard.test.tsdrivesapplyDirectSshRemoteWorkspaceSnapshotagainst a real store and asserts on post-apply state — live PTY bindings,pendingReconnectWorktreeIds, and the workspace keys handed toreconnectPersistedTerminals— so the hydration scope is pinned and not only the merge.🤖 Generated with Claude Code