refactor(state): make registry and gateway lifecycle updates atomic
Problem
NemoClaw persists sandbox lifecycle state in the local registry while OpenShell owns live sandbox state on a gateway. Create, remove, recreate, and upgrade operations can currently update those stores at different times. A crash, partial operation, or concurrent gateway transition can therefore leave a live sandbox with no registry row, or a registry row whose sandbox is still live while a recreate path expects it to be absent.
src/lib/onboard/not-ready-recreate.ts currently handles this conservatively by refusing or skipping pre-upgrade backup selection when the registry and gateway disagree. That avoids unsafe restore/deletion, but it is a localized recovery guard rather than an atomic source-of-truth contract.
Scope
- Define one durable lifecycle transaction or idempotent reconciliation protocol spanning registry mutations and gateway create/remove/recreate operations.
- Preserve enough intent to recover safely after process interruption at every mutation boundary.
- Make concurrent status/recovery paths distinguish in-progress lifecycle state from stale drift.
- Keep destructive operations fail-closed when ownership cannot be proven.
- Migrate existing drift guards to the canonical protocol.
Acceptance Criteria
- Fault-injection tests cover interruption before and after each registry and gateway mutation.
- A live sandbox cannot be silently reused or deleted from an ambiguous registry state.
- Upgrade/recreate can recover deterministically without environment breadcrumbs.
- The source-of-truth guards in
not-ready-recreate.tsare removed once the atomic protocol owns those cases. - Multi-gateway and non-default-gateway sandboxes are covered.
Follow-up from #6490 (verified-history replacement for #6250).
✨ Related open PRs:
Before implementation, I would like to align on a narrow first vertical slice rather than introduce a second broad lifecycle abstraction.
Proposed first slice: durable recreate journal
Start with same-name recreate/upgrade, where the registry/gateway split is most destructive. Keep create/remove on their current guarded paths until this contract is proven.
The persisted record would be secret-free and versioned:
type LifecycleOperationV1 = {
schemaVersion: 1;
operationId: string;
kind: "recreate";
phase:
| "planned"
| "deleting"
| "deleted"
| "creating"
| "created"
| "registry_committing"
| "completed"
| "compensating";
sandboxName: string;
gateway: { name: string; port: number };
owner: { sessionId: string; registryRevision: number };
sourceIdentity: { registryFingerprint: string; gatewayFingerprint: string };
targetIntentFingerprint: string;
observedSandboxIdentity?: string;
startedAt: string;
updatedAt: string;
};
Credential values, temporary paths, callbacks, and materialized provider plans would not be persisted. The target fingerprint would cover only normalized, non-secret recreate intent.
Ordering and reconciliation
- Acquire the existing per-sandbox lifecycle lock.
- Read registry and gateway state and prove source ownership.
- Durably write
plannedbefore the first gateway mutation. - Revalidate owner/revision immediately before each destructive effect.
- Record the observed postcondition after delete/create, not merely that the command returned.
- Commit the registry with compare-and-swap semantics against the recorded revision.
- Mark
completedonly after registry and gateway identity agree.
Recovery would be state/postcondition driven:
| Journal phase | Observed gateway state | Recovery |
|---|---|---|
planned |
original identity present | resume delete after ownership revalidation |
deleting |
original identity present | retry the idempotent delete |
deleting / deleted |
absent | advance to create |
creating |
absent | retry create from freshly validated intent |
creating / created |
exact target identity present | advance to registry commit |
| any mutation phase | unknown or mismatched live identity | fail closed; do not reuse or delete |
registry_committing |
exact target present, old registry revision | retry CAS commit |
Fault-injection matrix
Tests would terminate the child process at each durable boundary:
- after
planned, before delete; - after delete command, before deleted postcondition receipt;
- after
deleted, before create; - after create command, before created postcondition receipt;
- after
created, before registry commit; - after registry write, before
completed.
For every boundary, restart must converge to either the original proven sandbox or the exact replacement, never an ambiguous reuse/deletion. The same matrix should run with a non-default gateway name/port.
This deliberately does not serialize SandboxCreateIntent and should share ownership/generation semantics with #6228 rather than defining a competing onboarding checkpoint format.
Would a recreate-first journal with this phase/ownership model be an acceptable implementation direction, or should #6228 establish the shared session/generation contract first?
Tracking for v0.0.97. With #6228 complete, the narrow durable recreate-journal slice can proceed as the first atomic lifecycle update.
Opened #7690 for the resumed-onboard vertical slice. It journals same-name replacement before deletion, binds recovery to source and target identity, and rejects ambiguous registry/OpenShell states before another mutation.
This does not close #6492. Remaining work:
- adopt the journal for rebuild;
- adopt it for non-resumed re-onboard;
- remove the legacy not-ready repair fallback after every same-name replacement caller is journal-bound and crash-point coverage exists.
Reconciliation on current main confirms that the accepted #6492 recreate-journal scope is complete.
Verified revision:
- NemoClaw commit:
5d4ac392c471de5323496a0d1df6d0118851d7d7 - Branch state: clean fresh worktree based on that exact
origin/main
Implemented slices:
- #7690 journals resumed same-name recreation before deletion.
- #7788 journals rebuild and non-resumed same-name recreation.
- #8027 requires a journal for not-ready repair.
- #8073 removes the unbound source-of-truth fallback and makes pre-upgrade backup selection consume journal-bound source proof.
- #10491 makes a void replacement journal recoverable without weakening source, target, registry, or gateway ownership checks.
Current contract evidence:
- The durable transaction records
plannedbefore deletion,deletinganddeletedaround the gateway mutation, the exact created identity before registry commit, andcompletedonly after the live replacement and registry entry agree. - Restart coverage exercises every journal phase and either continues the proven source/target operation, accepts the exact registered replacement, or stops on a changed or ambiguous identity.
- The source registry row remains present until replacement registration commits.
- The transaction is secret-free and binds the sandbox, source and target identities, target intent, lifecycle generation, and exact gateway authority.
- Non-default gateway selection and cross-gateway refusal are covered.
not-ready-recreate.tsremains as the backup-selection policy module, but its former unbound registry/OpenShell drift guard is gone; it now requiresSandboxRecreateSourceProof.
Focused validation:
npx vitest run --project cli \
src/lib/onboard/sandbox-recreate-transaction.test.ts \
src/lib/onboard/onboard-recreate-journal.test.ts \
src/lib/actions/sandbox/rebuild-recreate-journal.test.ts \
src/lib/actions/sandbox/rebuild-flow-recovery.test.ts \
src/lib/onboard/machine/handlers/sandbox-recreate-journal.test.ts \
src/lib/onboard/not-ready-recreate.test.ts
Test Files 6 passed (6)
Tests 222 passed (222)
Exit status 0
No open PR currently claims #6492.
This does not claim that all lifecycle work is complete. True health-before-delete/atomic replacement remains separately tracked by #5801 and design issue #7704. Broader lifecycle receipts, compensation, and backup-policy parity remain separately owned by #7702. Those issues explicitly define separate boundaries and ownership, so they are not folded into this closeout.
Closing #6492 as completed.