Skip to content

fix(ssh): keep the generation floor when a target is removed and recreated - #22146

Merged
nwparker merged 1 commit into
mainfrom
neil/sta-8011-p1continuous-preserve-ssh-generation-fencing-across-runtime
Sep 22, 2026
Merged

nwparker merged 1 commit into
mainfrom
neil/sta-8011-p1continuous-preserve-ssh-generation-fencing-across-runtime

Conversation

@nwparker

@nwparker nwparker commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​113 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​113
Prod 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​25 $\color{#cf222e}{\Huge{\mathbf{−}}}$​8 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​17

ELI5

Every SSH connection carries a ticket number. When the connection is replaced, the number goes up, and any late file change that still shows the old number is refused. Removing a target threw away its counter, so a target created again under the same name started counting from the beginning and handed out the same numbers twice. A slow file change from the old machine could then show a number that looked current. Now the counter never goes back down within a session.

What Changed

Before: Remove a default ephemeral VM target and recreate it. The new target's connection reused the generation numbers the old one had issued. A delayed write or delete that the old VM's session had stamped with its generation passed the stale-host fence and ran against the replacement.

After: The same delayed mutation is rejected with "SSH connection changed; refresh and try again", and the replacement connection keeps working normally.

Mechanism: forgetSshConnectionGeneration still drops the per-target map entry (the memory fix from #21734 stays), but it now raises a single scope-wide floor to the highest forgotten generation. advanceSshConnectionGeneration starts from the max of the target's current value and that floor, so a recreated target skips past everything its prior incarnation issued. The floor resets whenever the session scope rolls, because a new scope already invalidates every prior generation.

Because a recreated target now jumps by more than one, rotateSshProviderAuthority no longer infers a scope roll from next !== previous + 1. It asks the generation module whether the previous generation is still inside the current scope. Only a real scope roll revokes every target's in-flight requests; a recreated target aborts only its own.

Why

Runtime-owned target ids are deterministic (runtime-ssh-<runtimeId>), so remove-and-recreate is a normal path for ephemeral VMs, not an edge case. Alternatives considered:

  • Per-target tombstone map: reintroduces the unbounded growth fix(memory): release stale session registries #21734 removed, and any bound on it brings the reuse bug back once an entry is evicted.
  • One global monotonic sequence for all targets: also fixes it, but shares the per-scope counter budget across every target and makes each reconnect on any host push all hosts toward a scope roll, which aborts every in-flight request. The floor only advances when a target is actually removed.

Linked Issue

Fixes STA-8011 (https://linear.app/stably/issue/STA-8011)

Visual Proof

N/A. Main-process fencing logic with no UI change.

Testing

  • pnpm test src/main/ssh/ src/main/persistence-repo-lifecycle.test.ts src/main/ipc/filesystem-mutations.test.ts src/main/ipc/filesystem-import-ssh-ops.test.ts src/main/ipc/filesystem-import-ssh.test.ts src/main/runtime/orca-runtime-files-rename-authority.test.ts passes (167 files, 2333 tests).

  • pnpm tc:node passes; pnpm run check:code-quality:changed and oxlint are clean on the changed files.

  • New tests: recreated target starts above every generation its removed incarnation issued; floor holds across repeated recreation and across a lower forgotten target; floor drops on scope roll; authority rotation for a recreated target does not abort other targets; real Store remove-and-recreate of a runtime-owned VM target rejects the stale expectation while the replacement passes.

  • Tested on macOS; the code is platform-independent main-process logic.

  • I manually tested these changes locally

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

AI Disclosure

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

No wire change: the generation is still an opaque safe integer on both sides. Old clients holding a stale generation get the same "refresh and try again" error they already handle.

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)

…eated

A removed SSH target released its connection-generation entry, so a target
recreated under the same id (runtime-owned VM targets derive it from the
runtime id) restarted at the session base and reissued the generations the
discarded incarnation had already handed out. A delayed write or delete from
the old VM then matched the replacement's fence.

Keep a scope-wide floor of the highest forgotten generation so a recreated
target always starts above it, and make authority rotation detect a scope
roll by scope membership rather than by a +1 step so the skip does not revoke
every other target's requests.
@coderabbitai

coderabbitai Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

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

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: 8c01a77a-e516-4c50-9b56-3ed387153b2e

📥 Commits

Reviewing files that changed from the base of the PR and between b964336 and 6c04b23.

📒 Files selected for processing (5)
  • src/main/persistence-repo-lifecycle.test.ts
  • src/main/ssh/ssh-connection-generation.test.ts
  • src/main/ssh/ssh-connection-generation.ts
  • src/main/ssh/ssh-provider-authority.test.ts
  • src/main/ssh/ssh-provider-authority.ts

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


📝 Walkthrough

Walkthrough

The SSH generation state now records the highest generation issued by forgotten targets. Recreated targets receive generations above that floor. Session initialization, rollover, and reset update the floor. A reusable session-membership predicate now supports authority rotation. Tests cover recreated targets, repeated forgetting, session rollover, provider authority, and persistence lifecycle behavior.

Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to 6c04b

Recreated SSH targets reject stale mutations while genuine session changes still revoke affected requests. No actionable merge-blocking risk remains.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 22.22% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 9 functions across 5 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 main change: preserving the SSH generation floor when a target is removed and recreated.
Description check ✅ Passed The description follows the required structure and explains the problem, solution, rationale, linked issue, testing, scope, and checklist. Visual proof is correctly marked N/A because there is no UI c…
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 💡
  • 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.

@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

  • Scope-wide forgotten-generation floor (ssh-connection-generation.ts): forgetSshConnectionGeneration now raises a single module-level forgottenGenerationFloor to the highest forgotten generation before dropping the per-target entry (so the #21734 memory fix is preserved), and advanceSshConnectionGeneration starts from Math.max(current, floor) + 1. A recreated deterministic runtime-ssh-<runtimeId> target therefore skips past every generation its prior incarnation issued.
  • Floor lifecycle: the floor resets to sessionGenerationBase on session init, resetSshConnectionGenerations, and every scope roll — a fresh scope already invalidates all prior generations, so dropping the floor is safe.
  • Scope-roll detection rewritten (ssh-provider-authority.ts): rotateSshProviderAuthority no longer infers a roll from nextGeneration !== previousGeneration + 1 (a recreated target legitimately jumps by more than one). It now uses the new isSshConnectionGenerationInCurrentSession(previousGeneration). Only a genuine roll clears authorityByTarget and aborts every target; a recreated target aborts only its own prior authority.
  • Tests: unit coverage for recreation, repeated recreation across a lower forgotten target, floor drop on scope roll, and authority rotation not revoking other targets; plus a real Store remove/re-add integration test in persistence-repo-lifecycle.test.ts.

I verified the rewritten guard myself: a real scope roll always advances sessionGenerationBase by exactly one stride (or wraps to 0), and previousGeneration is captured before advance from the old scope, so it can never land inside the new range — making the new check equivalent to the old one for real rolls while correctly classifying the recreation jump. Each new test asserts an exact value or an exact throw and fails with the old behavior.

Also ran the touched suites locally: ssh-connection-generation, ssh-provider-authority, persistence-repo-lifecycle, filesystem-mutations, filesystem-import-ssh, and orca-runtime-files-rename-authority all pass; oxlint clean on both changed source files. No wire change — the generation remains an opaque safe integer and renderer/web clients echo it rather than doing arithmetic on it.

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

@nwparker
nwparker merged commit 8fb0ba6 into main Sep 22, 2026
37 checks passed
@nwparker
nwparker deleted the neil/sta-8011-p1continuous-preserve-ssh-generation-fencing-across-runtime branch September 22, 2026 05:20
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