Skip to content

fix(session): an emptied workspace still survives the reconnect merge - #19944

Merged
nwparker merged 2 commits into
mainfrom
nwparker/fix-emptied-workspace-reconnect-survival
Sep 19, 2026
Merged

nwparker merged 2 commits into
mainfrom
nwparker/fix-emptied-workspace-reconnect-survival

Conversation

@nwparker

@nwparker nwparker commented Sep 10, 2026 •

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

Defect

Reconnect an SSH workspace after closing its last terminal and you land on the home screen.

mergeDirectSshRemoteWorkspaceSession decides whether to honour the host's activeWorktreeId verbatim or keep the local one. A null from the host is not evidence that no workspace is active — it is what a snapshot carries when the host never named the workspace or its path did not resolve — so the local value is preserved when the workspace the user is standing in still exists in the merged result. That existence check counted tabs:

(tabsByWorktree[current.activeWorktreeId]?.length ?? 0) > 0

A workspace the user has just emptied has a present, empty row. It reads as zero, so it "did not survive", the host's null is taken literally, and the user is thrown out of the workspace — for closing a tab.

Why this is an oversight, not a design choice

The correct predicate already exists 200 lines above in this same function, with the reasoning spelled out:

// Why presence and not length: an explicit empty row is the record that the user closed the last
// terminal (initial-terminal.ts), and localTabsFor cannot tell it from an absent one. ...
const hasLocalTabsRow = (worktreeId: string): boolean =>
  Object.hasOwn(liveTabsByWorktree, worktreeId) || Object.hasOwn(current.tabsByWorktree, worktreeId)

The tombstone distinction — a missing row means never initialized, an explicit empty row means the user closed the last terminal (initial-terminal.ts) — was applied to the row-assembly pass and missed on the active-workspace line. This PR applies the same reading in the one place it was skipped.

Fix

Object.hasOwn(tabsByWorktree, current.activeWorktreeId)

One line. No new concept: it is hasLocalTabsRow's rule, one level up, on the map this function has already assembled.

Tests

Two cases in remote-workspace-session-merge-local-survival.test.ts, in the local rows the snapshot carries no answer for block that already pins the tombstone's survival in tabsByWorktree. That block asserted the row survived but never that the user did, which is the assertion gap this defect lived in.

  • keeps the user standing in the workspace they emptied — emptied active workspace, host names no active worktree; activeWorktreeId, activeWorkspaceKey and activeRepoId all still describe it.
  • still lets the host move the user off a workspace it does name one for — the counterweight, so presence does not become "never follow the host".

Mutation testing

Mutation Result
Revert the predicate to (tabsByWorktree[id]?.length ?? 0) > 0 killed — 1 failure across all 155 hooks/ files, exactly keeps the user standing in the workspace they emptied

Not a whole-change revert: the assertion the mutation targets is the specific regression, and nothing else in the suite moves.

How this was found

Adversarial testing of a fourteen-PR integration stack, looking for interactions no single PR's suite could cover. Two PRs read the same empty tabsByWorktree row with opposite meanings — one writes [] as a "user closed the last terminal" tombstone, the other reads an empty row as a gap to fill. Walking the readers of that row, three decision-making ones still collapse absent into empty. I verified each: this one and the publish gate below by test, the activation gate structurally. This is the only one of the three that is both unambiguously a defect and safe to fix in isolation. (A prior pass reported six such readers with three already fixed; I did not re-derive that count, so it is not claimed here.)

The other two are deliberately not fixed here, for stated reasons:

  • The revision-zero remote-sync publish gate (remote-workspace-target-sync.ts) never publishes an emptied workspace. Measured: a populated row publishes, a tombstoned row and an absent row both push nothing. Publishing [] into a revision-0 host is exactly the SSH host session partitions are written but never read — tab lists round-trip as empty and remote sync permanently deletes them #12721 shape, and per docs/reference/remote-wire-compatibility.md changing what we publish reaches old clients with no wire change. That needs a negotiated capability, not a predicate swap.
  • ensureWebRuntimeWorktreeTerminalAfterWake is called unconditionally from activateAndRevealWorktree and cannot receive the caller's providesInitialSurface signal. worktree-activation.ts already documents this as a known caveat; changing it affects six call sites and is a product decision rather than a bug fix.

Gates

pnpm tc clean. pnpm test src/renderer/src/hooks — 155 files, 1004 passing, 0 failures (log grepped, not trusted to the wrapper exit code). pnpm run check:code-quality:changed — 0 new findings across 2 changed files. pnpm format.

Rebase record (2026-09-11)

Rebased onto main at 5fa62feda7c264c6eda99ba4788ffebab9aae26c. No conflicts; all 1 commit(s) replayed as-is.

SHA
old head a5d6bd21fc419ce1a24be6454adb5141b2e50cc9
new head 5925d0b7e4bdac1766c4e6cf3fdc3c25d0602366

@coderabbitai

coderabbitai Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: 058a362a-c8af-4498-9121-c60fd36590ca

📥 Commits

Reviewing files that changed from the base of the PR and between 0737d45 and eccd9ac.

📒 Files selected for processing (1)
  • src/renderer/src/hooks/remote-workspace-session-merge.ts

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


📝 Walkthrough

Walkthrough

mergeDirectSshRemoteWorkspaceSession now treats an explicit empty tab row as an existing worktree. The merge preserves the local active worktree, workspace key, and repository when the host provides no active values. Tests also confirm host precedence and null active state when neither side has a matching row.

Priority: ⬇️ Low

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 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 and concisely describes the primary fix: preserving an emptied workspace during reconnect merge.
Description check ✅ Passed The description provides a detailed defect explanation, rationale, implementation details, tests, validation results, and scope boundaries. It does not use the repository template headings and does no…
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.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch

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.

@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

Corrects mergeDirectSshRemoteWorkspaceSession so that a workspace the user has just emptied (its last terminal closed, leaving an explicit empty tabsByWorktree row) still survives the reconnect merge as the active workspace.

  • Presence over length in localActiveWorkspaceSurvives — the predicate switches from (tabsByWorktree[current.activeWorktreeId]?.length ?? 0) > 0 to Object.hasOwn(tabsByWorktree, current.activeWorktreeId), so an explicitly-empty row (the "user closed the last terminal" tombstone) no longer reads as "did not survive", which was dropping the user onto the home screen.
  • Comment capturing the reasoning — documents that the same presence-not-length reading hasLocalTabsRow already applies is now applied to the assembled map.
  • Two tests pin the boundary — keeps the user standing in the workspace they emptied (the regression) and a counterweight still lets the host move the user off a workspace it does name one for, so presence does not become "never follow the host".

Verified the fix is sound: I mutated the predicate back to the length check and confirmed the single new regression test fails while nothing else in the merge suite moves, and the surviving merge test files stay green. The new predicate checks tabsByWorktree (the merged map), which is exactly the "demonstrably still exists in the merged result" target the surrounding comment already describes.

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

@nwparker
nwparker force-pushed the nwparker/fix-emptied-workspace-reconnect-survival branch 2 times, most recently from 5925d0b to 0737d45 Compare September 18, 2026 07:53

@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

Re-reviewed the delta since the prior pullfrog review (a5d6bd21). The only PR-authored change is a test-only commit; the production fix is unchanged.

  • Boundary test for the presence rule — 0737d45b adds does not stand the user in a workspace that survived in neither side, asserting that when neither side carries a row for the local active worktree, the merge does not preserve it. This is the case the commit message calls out: widening localActiveWorkspaceSurvives to "always preserve" previously passed the whole file, because the existing counterweight rides on host-precedence ordering rather than the predicate itself.
  • Fix unchanged since approval — the predicate change and its comment are identical to the previously reviewed head; the source file's only other difference is main's recovery-ledger code picked up in the rebase, which this PR does not author.

Verified locally: all 24 tests in the file pass, and mutating the predicate to drop the Object.hasOwn check fails exactly this new test — real coverage, not a tautology. No open pullfrog threads remained to retire.

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

localActiveWorkspaceSurvives decided whether the workspace the user is
standing in still exists by counting its tabs. A workspace they had just
closed the last terminal in reads as zero, so it "did not survive", the
host's null activeWorktreeId was taken literally, and the reconnect
dropped them onto the home screen -- for closing a tab.

An explicit empty tabsByWorktree row is the record that the user closed
the last terminal, not the absence of a workspace (initial-terminal.ts).
hasLocalTabsRow two hundred lines up in this same function already draws
that distinction with Object.hasOwn; this line was simply missed.

The counterweight is pinned too: presence must not turn into "never
follow the host", so a host that does name an active worktree still wins
over the emptied local one.
Widening the predicate to "always survives" passed the whole file — the
counterweight rides on the host-precedence ordering, not on the predicate, so
nothing objected to dropping the rule entirely. An emptied row is evidence the
workspace exists; no row on either side is not.
@nwparker
nwparker force-pushed the nwparker/fix-emptied-workspace-reconnect-survival branch from 0737d45 to eccd9ac Compare September 19, 2026 06:54
@nwparker
nwparker merged commit 5d3a979 into main Sep 19, 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.

1 participant