Skip to content

fix(orcad): every activation fence holder carries a generation token its steps and release must match - #25834

Merged
OrcaWin merged 10 commits into
OrcaWin/node-rt-phase3from
OrcaWin/node-rt-p3-fence-generation
Oct 6, 2026
Merged

OrcaWin merged 10 commits into
OrcaWin/node-rt-phase3from
OrcaWin/node-rt-p3-fence-generation

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 14 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​729 $\color{#cf222e}{\Huge{\mathbf{−}}}$​27 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​702
Prod 22 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​609 $\color{#cf222e}{\Huge{\mathbf{−}}}$​188 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​421

ELI5

Only one run at a time may change a managed Orca server's host, and that run holds a lock on the host (the "fence"). If a desktop holding the fence goes to sleep for more than 20 minutes, Orca treats the fence as abandoned, and another desktop can take it over. When the first desktop woke up, it still believed it held the fence: it could keep starting its server version, and when it finished it deleted the other desktop's fence and its recovery record mid-update. Now every holder stamps the fence with its own random token, and the host refuses any step or release from a holder whose token is no longer there.

This PR also includes #25802 (BUG-21): Orca clears a stale fence that has no interrupted run behind it by itself, instead of telling the user to run Recover. It is only safe because of the token check.

What Changed

  • Every holder writes a generation token. Activation, rollback, stop, recover and wake each write a random token into .install-lock/.orca-fence-owner. The same command that creates the lock writes it, building on fix(ssh): a wake's fence carries its owner token from the moment the lock exists #25825's token-at-creation, and so does the stale-takeover command. The file name is one constant, ORCAD_FENCE_OWNER_FILENAME, in orcad-activation-fence-scope.ts.
  • Every step of the holder checks it on the host. The holder's run executes inside an AsyncLocalStorage fence scope (runWithOrcadFence). Inside that scope:
    • On POSIX, execOrcadRemote prefixes each command with [ "$(cat …/.orca-fence-owner)" = '<token>' ] || { echo __ORCAD_FENCE_LOST__; exit 75; }, so the check and the step are one remote command.
    • On Windows, orcadWindowsHostOpCommand passes --fence <lockDir> <token>, and the host script checks the token before it dispatches the operation.
    • A Windows command that isn't a host-script operation (the breakaway launch) gets a fence-check host op immediately before it. That leaves one exec of check-then-step gap on Windows. It is accepted, because a holder would have to be suspended past the 20-minute stale window exactly inside that gap.
    • A failed check raises OrcadFenceLostError. The run aborts, execOrcadRemoteOr never turns it into a fallback, and the readiness wait doesn't retry it.
  • The journal is stamped with its generation. writeOrcadActivationTransaction adds the writing run's fenceToken; older readers ignore the extra field. A recovery takeover re-stamps the journal it adopts, so its own release removes that journal.
  • Release is conditional, piece by piece (Astra pass 9). After the token check, release moves the journal aside and deletes it only if it carries this run's stamp. It then moves the lock aside and deletes it only if its owner file is this run's token. Anything that isn't ours goes straight back, using a no-clobber move. A release that stalls after its token check, while a takeover and a successor come and go, therefore leaves the successor's journal and lock in place. The Windows fence-release op does the same.
  • Release is conditional. Release proceeds only if the token is still ours. It removes the journal first, then moves the lock aside and deletes it, and removes the transaction folder only if it is empty. A successor's fresh lock is therefore never what gets removed, and a superseded holder's release just answers SUPERSEDED. On Windows this is the new fence-release host op.
  • An ownerless fence is marked only by its own holder. The ownerless mark from fix(orcad): a retained activation fence is ownerless, so recovery can always take it over (BUG-17) #25698 is guarded the same way, so a superseded run can't age its successor's fence.
  • Wake: a wake passes its existing token as the fence token, and it releases an interrupted wake fence through the same conditional release.
  • Folded in from fix(orcad): clear a bare stale activation fence instead of asking for Recover; report a restarting update from status (BUG-21) #25802 (BUG-21):
    • a stale fence with no journal is taken over and released, and the update retries once;
    • "Recover it first" appears only when there is a journal to reconcile;
    • the CLI reports a restarting update or rollback from the host's status instead of failing on the closed connection.

Wired into #25811's state mutations (snapshot capture, restore and clear):

  • Order inside the command: the fence check, then the mutation lock, then the work.
    • On POSIX the guard is the outermost part of serializedStateMutationCommand, so a superseded run never takes the mutation lock or touches state.
    • On Windows the host script checks --fence before the op runs.
  • Heartbeat: the heartbeat refreshes the fence only while its token is still this run's. That replaces the old "skip a wake's fence" rule. On POSIX beat_fence checks the token on every beat. On Windows refreshFence compares the --fence token, and without one it refreshes nothing.
  • Lost fence: for a state mutation it is a refusal (OrcadFenceLostError), never a FAILED fallback.
  • One constant: the separate wake-owner constants are removed, so everything uses ORCAD_FENCE_OWNER_FILENAME.
  • p3-autoupdate is reviewing this part.

Stacked on #25825 and #25811. This branch contains #25825's commit and a merge of #25811's head. Both merge first; this PR then rebases, and their commits drop out.

Why

Linked Issue

Astra review pass 8, P1 #1, plus BUG-21. Internal. Supersedes #25802, which closes when this merges. Built on #25825.

Visual Proof

N/A. No UI changes.

Testing

  • I manually tested these changes locally

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

  • orcad-activation-fence-generation.test.ts is Astra's suspend-and-resume scenario, run in a real shell:

    • a holder is suspended past the stale window and its fence is cleared;
    • a successor takes the fence and writes its journal;
    • the resumed holder's next step throws OrcadFenceLostError, and the command it tried to run never happens;
    • the successor's fence and journal survive until the successor releases them.

    This test fails without the fix.

  • In the same file, the Astra pass 9 cases:

    • a release stalled right after its token check, followed by a takeover and a successor, resumes and leaves the successor's journal and lock. This fails without the fix.
    • a superseded run's state mutation (clearOrcadStateSnapshotMembersCommand) is refused with OrcadFenceLostError, and profile state is untouched.
  • orcad-windows-host-fence-ops.test.ts runs the real Windows host script under Node. It runs an operation only for the current token holder, and its release removes only its own generation.

  • ssh-remote-commands.test.ts: the stale takeover writes the new holder's token.

  • orcad-state-mutation-fence-heartbeat.test.ts, real shell:

    • a mutation whose fence was taken over mid-run stops refreshing it and leaves the successor's token;
    • a superseded run never starts its mutation or takes the mutation lock.
  • orcad-windows-host-state-ops.test.ts: the Windows heartbeat refreshes only an owned fence, and a superseded run is refused with exit 75.

  • The fake hosts in the activation, crash-recovery, wake, decommission and Windows lifecycle tests now follow the guard and the conditional release.

Ran locally:

  • pnpm tc:node, typecheck:cli and the changed-code gate pass.
  • In src/main/ssh, the remaining failures are 27 tests across six files. All of them fail the same way without this change, in my local environment. Two of the files fail with "AppEnvironment not initialized".

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

  • SSH execution boundary: a lost fence is a refusal, never a liveness verdict.
  • Mixed versions: an older client writes no token, so its fence never matches a newer holder's check.
  • Windows: the token check stays in the existing host script, with no new interpreter spawn. The one-exec gap described above applies only to commands that aren't host-script operations.

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 left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of the #25811 wiring in bfa24a19dc. It looks right to me: no blockers, and two nits.

I checked:

  • The order is guard, then the mutation process group and lock, then the work. The guard runs outermost, before orca_state_group, so a superseded holder never takes the mutation lock or reaches tar or rm.
  • A fence-lost answer becomes OrcadFenceLostError, and execOrcadStateMutationOr rethrows it instead of falling back to FAILED. The lock wrapper's release and orphan paths are token-guarded (posixReleaseFenceCommand answers SUPERSEDED, and the Windows fence-release op checks the token), so that throw can't delete a successor's journal or fence.
  • The heartbeat refreshes only while the owned-test passes, and never with no fence. On Windows, FENCE_TOKEN comes from --fence.
  • The wake-owner constant is gone. One owner file is used everywhere.

I ran the heartbeat, state-snapshot, Windows state-ops and crash-recovery suites at this head: 84 tests pass. The heartbeat and real-shell snapshot tests also pass with dash as both the outer and inner sh.

Nits, both optional:

  1. The heartbeat tests ownership against owned.lockDir but touches $fence, which is rebuilt from baseDir. On Windows, refreshFence likewise reads FENCE, built from __dirname, rather than the --fence lock dir. They name the same directory today. Touching the directory you just checked (owned.lockDir, or the --fence argument) would rule out a path-spelling mismatch.
  2. owned defaults to currentOrcadFence(), so a future caller outside a fence scope silently gets no guard and no heartbeat. Every current caller (activation capture, rollback rescue capture and restore, recovery restore) is fenced. A one-line comment or a dev-only warning when owned is null in a non-test build would make a regression visible.

m4air added 10 commits October 6, 2026 02:32
… Recover, and report a restarting update from status

BUG-21: a wake cut short leaves a stale fence with no journal. Every update then answered
'Recover it first' while Recover answered 'none'. The fence-hold check now takes such a fence
over and drops it, and the update retries once; Recover is asked for only over a journal.
The CLI also treats a connection closed by the server's own restart during update or rollback
as expected and reports what status shows once the runtime answers.
…lock exists

The idle-exit e2e still read 'fenced' on reconnect: the launch-time wake's
lock landed on the host but its connection dropped before the client saw OK,
so the wake body never ran and never wrote its owner token, leaving a fence
nothing could prove. The token is now claimed before the lock and written by
the same command that creates it, and a wake registers itself before any
remote step so a reconnected wake waits for it instead of racing it.
…its steps and release must still match

Astra pass 8: a holder suspended past the stale window resumed, kept acting, and its
unconditional release deleted the successor's fence and recovery journal mid-update. Every
holder (activation, rollback, stop, recover, wake) now writes a token into the lock it creates or
takes over. Each remote step it issues checks that token on the host, in the same command on
POSIX and inside the host script for Windows host ops; release is conditional on the token and
moves the lock aside instead of removing the root. A superseded holder aborts with
OrcadFenceLostError and its release is a no-op.
…and refresh only a fence they still own

On POSIX the fence guard runs outermost in serializedStateMutationCommand, before the mutation
lock and the work, and the heartbeat touches the fence only while the token is still this run's.
The Windows host script records the --fence token and refreshFence compares it. A fence-lost
answer from a state mutation is a refusal, never a FAILED fallback. One owner-file constant
replaces the wake-owner copies.
… its own generation's

Astra pass 9: a release that passed its token check and stalled before deleting could, once a
takeover and a successor came and went, delete the successor's journal and lock. The journal is
now stamped with the writing run's fence token (a recovery takeover re-stamps the journal it
adopts), and the release renames the journal and the lock aside, deletes each only if it carries
this run's token, and otherwise moves it straight back.
…ver the error message

The real exec error quotes the command, and every fenced command carries the
guard's marker text, so any failed or timed-out fenced step read as a lost
fence and dropped its unconfirmed flag. execCommand now attaches exitCode and
stdout to its exit error; execOrcadRemote rethrows unconfirmed terminations
before any reclassification.
A disconnect fails a wake's next step without the unconfirmed flag, and the
fence release then fails over the dead connection. Forgetting the token on
that error left a fence the reconnected wake could not prove its own, so it
reported the host as held by an update.
@OrcaWin
OrcaWin force-pushed the OrcaWin/node-rt-p3-fence-generation branch from 6f54cac to c1896cb Compare October 6, 2026 09:41
@OrcaWin
OrcaWin merged commit 6ad6881 into OrcaWin/node-rt-phase3 Oct 6, 2026
64 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