From 7c809c5bca4ae5edc4fe04edb6f734ec33373233 Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Tue, 28 Jul 2026 00:12:51 -0700 Subject: [PATCH 01/19] refactor(onboard): make recovery and sandbox entry strict Signed-off-by: Carlos Villela --- src/lib/onboard.ts | 10 +- src/lib/onboard/agent-resume-state.test.ts | 50 ++++ src/lib/onboard/agent-resume-state.ts | 12 + src/lib/onboard/checkpoint-replay.test.ts | 36 ++- src/lib/onboard/checkpoint-replay.ts | 21 ++ src/lib/onboard/lifecycle-contracts.md | 8 +- src/lib/onboard/machine/README.md | 4 +- .../onboard/machine/core-flow-phases.test.ts | 257 +++++++++--------- src/lib/onboard/machine/core-flow-phases.ts | 116 ++++++-- .../machine/handlers/provider-inference.ts | 4 +- .../machine/initial-flow-phases.test.ts | 139 +++++----- .../onboard/machine/initial-flow-phases.ts | 104 ++++--- .../machine/prerequisite-repair.test.ts | 96 +++++++ .../onboard/machine/prerequisite-repair.ts | 123 +++++++++ 14 files changed, 701 insertions(+), 279 deletions(-) create mode 100644 src/lib/onboard/agent-resume-state.test.ts create mode 100644 src/lib/onboard/machine/prerequisite-repair.test.ts create mode 100644 src/lib/onboard/machine/prerequisite-repair.ts diff --git a/src/lib/onboard.ts b/src/lib/onboard.ts index 31e536b92e3..a0c4457d78d 100644 --- a/src/lib/onboard.ts +++ b/src/lib/onboard.ts @@ -3865,10 +3865,6 @@ const recordStepSkipped = onboardRuntimeBoundary.recordStepSkipped.bind(onboardR const recordStepFailed = onboardRuntimeBoundary.recordStepFailed.bind(onboardRuntimeBoundary); const recordStateSkipped = onboardRuntimeBoundary.recordStateSkipped.bind(onboardRuntimeBoundary); const recordRepairEvent = onboardRuntimeBoundary.recordRepairEvent.bind(onboardRuntimeBoundary); -const recordStateResult = - onboardRuntimeBoundary.recordStateResultWithStepCompatibility.bind(onboardRuntimeBoundary); -const recordInvalidatedStateResult = - onboardRuntimeBoundary.recordInvalidatedStateResult.bind(onboardRuntimeBoundary); /** Run only non-mutating fatal onboard gates while the rebuild target is still intact. */ async function preflightAuthoritativeRebuildTarget( opts: import("./onboard/authoritative-rebuild-target").AuthoritativeRebuildPreflightOptions, @@ -4267,8 +4263,7 @@ async function runOnboard(opts: OnboardOptions = {}): Promise { runtime: onboardRuntimeBoundary.getRuntime(), phases: [preflightPhase, gatewayPhase], resume, - recordStateResult, - recordInvalidatedStateResult, + recordRepairEvent, }); const initialContext = initialFlowResult.context; @@ -4454,8 +4449,7 @@ async function runOnboard(opts: OnboardOptions = {}): Promise { runtime: onboardRuntimeBoundary.getRuntime(), phases: { providerInference: providerInferencePhase, sandbox: sandboxPhase }, resume, - recordStateResult, - recordInvalidatedStateResult, + recordRepairEvent, }); setupInferenceFactory.selectGatewayForFollowupOrExit(GATEWAY_NAME, runOpenshell); const coreContext = coreFlowResult.context; diff --git a/src/lib/onboard/agent-resume-state.test.ts b/src/lib/onboard/agent-resume-state.test.ts new file mode 100644 index 00000000000..0bc5e70fb55 --- /dev/null +++ b/src/lib/onboard/agent-resume-state.test.ts @@ -0,0 +1,50 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { describe, expect, it } from "vitest"; + +import { decisionSelected } from "../state/onboard-checkpoint-decision"; +import { deriveCheckpointFromSession } from "../state/onboard-checkpoint-migrate"; +import { createSession } from "../state/onboard-session"; +import { clearAgentScopedResumeState } from "./agent-resume-state"; + +describe("clearAgentScopedResumeState", () => { + it("invalidates agent-scoped checkpoint decisions and effect receipts", () => { + const session = createSession({ + agent: null, + sandboxName: "my-sandbox", + sandboxPromptProgress: { + sandboxName: true, + webSearch: true, + messaging: true, + resourceProfile: true, + }, + }); + session.checkpoint = { + ...deriveCheckpointFromSession(session), + sandboxIdentity: decisionSelected({ name: "my-sandbox", agent: "openclaw" }), + resourceProfile: decisionSelected({ cpu: "4", memory: "8Gi" }), + effectGroups: { + sandbox_create: { completedAt: session.updatedAt, fingerprint: "create" }, + sandbox_register: { completedAt: session.updatedAt, fingerprint: "register" }, + }, + bindings: { + credentialEnvs: ["SLACK_TOKEN"], + registeredProviders: [ + { name: "my-sandbox-slack", type: "generic", credentialEnv: "SLACK_TOKEN" }, + ], + }, + }; + + clearAgentScopedResumeState(session, "hermes"); + + expect(session.checkpoint).toMatchObject({ + sandboxIdentity: { kind: "unset" }, + webSearch: { kind: "unset" }, + messaging: { kind: "unset" }, + effectGroups: {}, + bindings: { credentialEnvs: [], registeredProviders: [] }, + }); + expect(session.checkpoint?.resourceProfile.kind).not.toBe("unset"); + }); +}); diff --git a/src/lib/onboard/agent-resume-state.ts b/src/lib/onboard/agent-resume-state.ts index 7634ad255b4..6686b97d42a 100644 --- a/src/lib/onboard/agent-resume-state.ts +++ b/src/lib/onboard/agent-resume-state.ts @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { Session } from "../state/onboard-session"; +import { decisionUnset } from "../state/onboard-checkpoint-decision"; export function normalizeAgentNameForResumeState(agentName: string | null | undefined): string { const trimmed = typeof agentName === "string" ? agentName.trim() : ""; @@ -36,6 +37,17 @@ export function clearAgentScopedResumeState(session: Session, selectedAgentName: session.sandboxPromptProgress.webSearch = false; session.sandboxPromptProgress.messaging = false; } + if (session.checkpoint) { + session.checkpoint = { + ...session.checkpoint, + sandboxIdentity: decisionUnset(), + webSearch: decisionUnset(), + messaging: decisionUnset(), + effectGroups: {}, + bindings: { credentialEnvs: [], registeredProviders: [] }, + updatedAt: new Date().toISOString(), + }; + } session.policyPresets = null; const resetSteps = [ diff --git a/src/lib/onboard/checkpoint-replay.test.ts b/src/lib/onboard/checkpoint-replay.test.ts index e2ce29bcc9f..426af9d2fb8 100644 --- a/src/lib/onboard/checkpoint-replay.test.ts +++ b/src/lib/onboard/checkpoint-replay.test.ts @@ -8,7 +8,11 @@ import { CHECKPOINT_SCHEMA_VERSION, type OnboardCheckpoint, } from "../state/onboard-checkpoint-types"; -import { planEffectGroupReplay, planSandboxCreateReplay } from "./checkpoint-replay"; +import { + checkpointSandboxIdentityMatches, + planEffectGroupReplay, + planSandboxCreateReplay, +} from "./checkpoint-replay"; import { bindingRevalidationGuidance, revalidateCheckpointBindings } from "./checkpoint-revalidate"; const ISO = "2026-01-01T00:00:00.000Z"; @@ -31,6 +35,36 @@ function checkpoint(overrides: Partial = {}): OnboardCheckpoi }; } +describe("checkpointSandboxIdentityMatches", () => { + it("uses the checkpoint identity when legacy name progress disagrees", () => { + expect( + checkpointSandboxIdentityMatches( + { + checkpoint: checkpoint({ sandboxIdentity: decisionUnset() }), + machine: { state: "sandbox" }, + sandboxName: "my-sandbox", + sandboxPromptProgress: { sandboxName: true }, + }, + "my-sandbox", + ), + ).toBe(false); + }); + + it("uses legacy name progress only when no checkpoint exists", () => { + expect( + checkpointSandboxIdentityMatches( + { + checkpoint: null, + machine: { state: "sandbox" }, + sandboxName: "my-sandbox", + sandboxPromptProgress: { sandboxName: true }, + }, + "my-sandbox", + ), + ).toBe(true); + }); +}); + describe("planEffectGroupReplay", () => { it("runs an unrecorded effect group", () => { expect(planEffectGroupReplay(checkpoint(), "messaging_providers", true).action).toBe("run"); diff --git a/src/lib/onboard/checkpoint-replay.ts b/src/lib/onboard/checkpoint-replay.ts index fa303e1726b..befc3db76f2 100644 --- a/src/lib/onboard/checkpoint-replay.ts +++ b/src/lib/onboard/checkpoint-replay.ts @@ -15,6 +15,27 @@ export interface CheckpointedMachineSession { readonly machine: { readonly state: OnboardMachineState }; } +export function checkpointSandboxIdentityMatches( + session: + | (CheckpointedMachineSession & { + readonly sandboxName?: string | null; + readonly sandboxPromptProgress?: { readonly sandboxName?: boolean }; + }) + | null + | undefined, + sandboxName: string, +): boolean { + if (session?.checkpoint) { + return ( + isDecisionSelected(session.checkpoint.sandboxIdentity) && + session.checkpoint.sandboxIdentity.value.name === sandboxName + ); + } + return ( + session?.sandboxPromptProgress?.sandboxName === true && session.sandboxName === sandboxName + ); +} + export function checkpointProvesSandboxStepComplete( session: CheckpointedMachineSession | null | undefined, ): boolean { diff --git a/src/lib/onboard/lifecycle-contracts.md b/src/lib/onboard/lifecycle-contracts.md index 544b5170984..3a7fa06c9bf 100644 --- a/src/lib/onboard/lifecycle-contracts.md +++ b/src/lib/onboard/lifecycle-contracts.md @@ -53,7 +53,7 @@ Every nonterminal state has one production owner or an explicit internal designa | `post_verify` | `handlePostVerifyState` in `machine/handlers/finalization.ts` | | `complete`, `failed` | Terminal; no handler | -The final flow gives the strict runner ownership from the durable entry state. If a saved session is at `policies`, `finalizing`, or `post_verify`, the flow runs earlier handlers as prerequisite repairs. Each repair emits a `state.repair.started` event and then a `state.repair.completed` or `state.repair.failed` event. A repair must return one update-free canonical advance and leave the durable state unchanged. The flow validates but does not apply the returned transition. The repaired context then feeds the strict runner at the saved entry state. +The initial, core, and final flows give the strict runner ownership from the durable entry state. If a saved session is later than a slice entry, the flow runs earlier handlers as prerequisite repairs. Each repair emits a `state.repair.started` event and then a `state.repair.completed` or `state.repair.failed` event. A repair must return a legal, update-free transition chain and leave the durable state unchanged. The flow validates but does not apply the repair transitions. The repaired context then feeds the strict runner at an exact state or the next flow slice. FSM transitions remain step-granular. For OpenClaw onboarding, the sandbox handler additionally checkpoints each completed secret-free prompt group: sandbox name, web search selection, messaging selection and non-secret configuration, and resource profile. @@ -150,7 +150,7 @@ Rebuild and non-resumed re-onboard remain under #6492. ## Persisted field ownership -The schema and sanitation authority is `Session` plus `normalizeSession`/`filterSafeUpdates` in `src/lib/state/onboard-session.ts`. `undefined` in an update means “leave unchanged”; accepted `null` means “clear.” On disk, many nullable fields still collapse never selected, explicitly declined, and explicitly cleared into the same `null` representation. `sandboxPromptProgress` records which of the checkpointed sandbox name, web search, messaging, and resource choices completed. The dedicated versioned `checkpoint` field (`src/lib/state/onboard-checkpoint.ts`) resolves the remaining ambiguity for those choices by modelling each as an explicit `unset`/`declined`/`selected` decision (#6228, #6227/#5783); `deriveCheckpointFromSession` reconstructs the same tri-state from legacy sessions using the completion markers. Live decision reads still consult the legacy fields; migrating every consumer onto the checkpoint decisions is a follow-up. +The schema and sanitation authority is `Session` plus `normalizeSession`/`filterSafeUpdates` in `src/lib/state/onboard-session.ts`. `undefined` in an update means “leave unchanged”; accepted `null` means “clear.” On disk, many nullable fields still collapse never selected, explicitly declined, and explicitly cleared into the same `null` representation. `sandboxPromptProgress` records which of the checkpointed sandbox name, web search, messaging, and resource choices completed. The dedicated versioned `checkpoint` field (`src/lib/state/onboard-checkpoint.ts`) resolves the remaining ambiguity for those choices by modelling each as an explicit `unset`/`declined`/`selected` decision (#6228, #6227/#5783). Live decision reads use the checkpoint when it exists. `deriveCheckpointFromSession` uses legacy fields only to migrate a session that has no checkpoint. | Field group | Fields | Writer/owner and state meaning | |---|---|---| @@ -199,14 +199,14 @@ PR #5955 moved the rebuild messaging conflict check before destruction. | Contract | Executable evidence | Uncovered boundary | |---|---|---| -| Fresh, resumed, recreate, successful, and failed machine event order | `machine/transition-traces.test.ts` | None at this boundary. Sandbox-handler replay tests cover effect-group crash recovery. | +| Fresh, resumed, recreate, successful, and failed machine event order | `machine/transition-traces.test.ts`, `machine/prerequisite-repair.test.ts` | None at this boundary. Sandbox-handler replay tests cover effect-group crash recovery. | | Detailed recreate decisions and repair branches | `machine/handlers/sandbox-resume.test.ts`, `machine/handlers/sandbox.test.ts` | Cross-handler effect transaction | | Legal edges, result kinds, runtime event shapes, runner sequencing | `machine/transitions.test.ts`, `machine/runtime.test.ts`, `machine/runner*.test.ts` | None at the unit boundary | | Create intent/provider ordering and fail-closed credential drift | `sandbox-create-plan.test.ts` | Cross-module pre-delete ordering has no behavioral seam | | Messaging conflict validation before recreate | `sandbox-messaging-preflight.test.ts`, rebuild preflight tests | One shared declarative policy across all callers | | Resume identity | `checkpoint-replay.test.ts`, `checkpoint-resume-guard.test.ts`, `machine/handlers/sandbox-checkpoint-crash-recovery.test.ts` | Live process-termination E2E with a real OpenShell sandbox | | Session sanitation, sandbox prompt checkpoints, and no-secret persistence | `src/lib/state/onboard-session-sandbox-prompts.test.ts`, `src/lib/state/onboard-checkpoint.test.ts`, `machine/handlers/sandbox-create-intent-boundary.test.ts` | Tri-state decisions remain scoped to checkpointed sandbox choices. | -| Versioned checkpoint schema, tri-state decisions, migration, and unknown-future fail-safe | `src/lib/state/onboard-checkpoint.test.ts`, `src/lib/state/onboard-checkpoint-migrate.test.ts` | Live decision reads still use legacy fields | +| Versioned checkpoint schema, tri-state decisions, migration, and unknown-future fail-safe | `src/lib/state/onboard-checkpoint.test.ts`, `src/lib/state/onboard-checkpoint-migrate.test.ts`, `checkpoint-replay.test.ts` | Legacy fields remain only as migration input when no checkpoint exists. | | Resumable create replay, durable identity, and stale-binding fail-closed | `src/lib/onboard/checkpoint-replay.test.ts`, `src/lib/onboard/checkpoint-resume-guard.test.ts`, `machine/handlers/sandbox-checkpoint-crash-recovery.test.ts` | None at the sandbox-handler boundary. | When lifecycle behavior changes one of these contracts, update the map and the narrow owning test in that same PR. Do not add source-text scans or production scaffolding solely to preserve current orchestration order. diff --git a/src/lib/onboard/machine/README.md b/src/lib/onboard/machine/README.md index bc5960fd7cf..f728b68fdf7 100644 --- a/src/lib/onboard/machine/README.md +++ b/src/lib/onboard/machine/README.md @@ -19,6 +19,8 @@ The target shape is a machine-driven onboarding runner: In that final shape, `src/lib/onboard.ts` should be a thin entrypoint. State handlers should own state-specific prompts, resume validation, repair decisions, and side effects. +The strict runner owns exact `init`, `preflight`, `provider_selection`, `inference`, and `sandbox` entry. If the durable state is later than a slice entry, earlier phases run as evented prerequisite repairs. A repair must return a legal, update-free transition chain and must not change the durable entry state. + ## State ownership Machine states are coarse user-visible onboarding phases, not every subprocess or probe inside a phase. The current vocabulary is intentionally limited to major boundaries: @@ -91,7 +93,7 @@ without a state transition so a later process can resume the same non-terminal s - applying safe session context updates; - marking terminal states; - emitting redacted lifecycle, state, repair, resume-conflict, and hook events; -- preserving compatibility with normalized older sessions. +- normalizing older sessions before strict execution. The runtime should reject invalid transitions before they can be persisted. diff --git a/src/lib/onboard/machine/core-flow-phases.test.ts b/src/lib/onboard/machine/core-flow-phases.test.ts index e788b51a50f..9480a708471 100644 --- a/src/lib/onboard/machine/core-flow-phases.test.ts +++ b/src/lib/onboard/machine/core-flow-phases.test.ts @@ -4,7 +4,6 @@ import { describe, expect, it, vi } from "vitest"; import { createSession, type Session, type SessionUpdates } from "../../state/onboard-session"; -import { recordInvalidatedTargets } from "../__test-helpers__/machine-recorders"; import { type CoreOnboardFlowPhases, createProviderInferenceOnboardFlowPhase, @@ -15,7 +14,7 @@ import { type SandboxOnboardFlowPhaseOptions, } from "./core-flow-phases"; import type { OnboardFlowContext } from "./flow-context"; -import type { OnboardStateResult } from "./result"; +import type { OnboardPrerequisiteRepairEventRecorder } from "./prerequisite-repair"; import { advanceTo, branchTo } from "./result"; import type { OnboardSequencePhase } from "./sequence-runner"; @@ -74,6 +73,12 @@ function completeStep(): Session["steps"][string] { }; } +function repairRecorder(events: string[] = []): OnboardPrerequisiteRepairEventRecorder { + return async (type, options) => { + events.push(`${type}:${options.state ?? "unknown"}`); + }; +} + function createPhases( overrides: { endpointProvenance?: Partial; @@ -577,13 +582,6 @@ describe("core onboard flow phases", () => { run: runSandbox, }, }; - const recordStateResult = vi.fn(async () => { - throw new Error("compatibility recorder should not run"); - }); - const recordInvalidatedStateResult = vi.fn(async () => { - throw new Error("invalidation recorder should not run on the strict runner path"); - }); - const result = await runCoreOnboardFlowSlice({ context: context({ resume, @@ -609,8 +607,9 @@ describe("core onboard flow phases", () => { }, phases, resume, - recordStateResult, - recordInvalidatedStateResult, + recordRepairEvent: async () => { + throw new Error("repair recorder should not run on the exact-entry path"); + }, }); expect(phaseCalls).toEqual(["provider_selection", "sandbox"]); @@ -621,74 +620,157 @@ describe("core onboard flow phases", () => { expect.objectContaining({ endpointUrl: "https://example.test/v1" }), ); expect(appliedTransitions).toEqual(["advance:inference", "advance:sandbox", "branch:openclaw"]); - expect(recordStateResult).not.toHaveBeenCalled(); - expect(recordInvalidatedStateResult).not.toHaveBeenCalled(); expect(result.context.endpointUrl).toBe("https://example.test/v1"); expect(result.context.sandboxName).toBe("created-sandbox"); expect(result.session.machine.state).toBe("openclaw"); }); it.each([ - "inference", - "sandbox", + ["inference", true, ["advance:sandbox", "branch:openclaw"]], + ["sandbox", true, ["branch:openclaw"]], + ["sandbox", false, ["branch:openclaw"]], + ] as const)("repairs provider context before strict %s entry", async (state, resume, expected) => { + const calls: string[] = []; + const applied: string[] = []; + const repairEvents: string[] = []; + let runtimeSession = createSession({ + machine: { + version: 1, + state, + stateEnteredAt: "2026-06-09T00:02:00.000Z", + revision: 7, + }, + }); + const phases: CoreOnboardFlowPhases = { + providerInference: { + state: "provider_selection", + run: (ctx) => { + calls.push("provider_selection"); + return { + context: { ...ctx, endpointUrl: "https://example.test/v1" }, + result: [ + advanceTo("inference", { metadata: { state: "provider_selection" } }), + advanceTo("sandbox", { metadata: { state: "inference" } }), + ], + }; + }, + }, + sandbox: { + state: "sandbox", + run: (ctx) => { + calls.push("sandbox"); + return { + context: { ...ctx, sandboxName: "created-sandbox" }, + result: branchTo("openclaw", { metadata: { state: "sandbox" } }), + }; + }, + }, + }; + + const result = await runCoreOnboardFlowSlice({ + context: context({ resume }), + runtime: { + session: async () => runtimeSession, + applyResult: async (stateResult) => { + if (stateResult.type === "transition") { + applied.push(`${stateResult.transitionKind}:${stateResult.next}`); + runtimeSession.machine = { + ...runtimeSession.machine, + state: stateResult.next, + revision: runtimeSession.machine.revision + 1, + }; + } + return runtimeSession; + }, + }, + phases, + resume, + recordRepairEvent: repairRecorder(repairEvents), + }); + + expect(calls).toEqual(["provider_selection", "sandbox"]); + expect(applied).toEqual(expected); + expect(repairEvents).toEqual([ + "state.repair.started:provider_selection", + "state.repair.completed:provider_selection", + ]); + expect(result.context.endpointUrl).toBe("https://example.test/v1"); + expect(result.context.sandboxName).toBe("created-sandbox"); + expect(result.session.machine.state).toBe("openclaw"); + }); + + it.each([ "openclaw", "agent_setup", "policies", "finalizing", "post_verify", - ] as const)("lets resume sessions at %s pass through core compatibility", async (state) => { - const recorded: string[] = []; - const applyResult = vi.fn(async () => { - throw new Error("downstream resume compatibility should not use the strict runner"); + ] as const)("repairs core prerequisites without changing resumed %s entry", async (state) => { + const repairEvents: string[] = []; + const branchState = state === "agent_setup" ? "agent_setup" : "openclaw"; + const session = createSession({ + machine: { + version: 1, + state, + stateEnteredAt: "2026-06-09T00:02:00.000Z", + revision: 7, + }, }); const phases: CoreOnboardFlowPhases = { providerInference: { state: "provider_selection", - run: (ctx) => ({ context: ctx, result: advanceTo("sandbox") }), + run: (ctx) => ({ + context: { ...ctx, endpointUrl: "https://example.test/v1" }, + result: [ + advanceTo("inference", { metadata: { state: "provider_selection" } }), + advanceTo("sandbox", { metadata: { state: "inference" } }), + ], + }), }, sandbox: { state: "sandbox", - run: (ctx) => ({ context: ctx, result: advanceTo("openclaw") }), + run: (ctx) => ({ + context: { ...ctx, sandboxName: "created-sandbox" }, + result: branchTo(branchState, { metadata: { state: "sandbox" } }), + }), }, }; - await runCoreOnboardFlowSlice({ + const result = await runCoreOnboardFlowSlice({ context: context({ resume: true }), runtime: { - session: async () => - createSession({ - machine: { - version: 1, - state, - stateEnteredAt: "2026-06-09T00:00:00.000Z", - revision: 7, - }, - }), - applyResult, + session: async () => session, + applyResult: async () => { + throw new Error("prerequisite repair must not apply a machine result"); + }, }, phases, resume: true, - recordStateResult: async (result) => { - recorded.push((result as ReturnType).next); - }, - recordInvalidatedStateResult: recordInvalidatedTargets(recorded), + recordRepairEvent: repairRecorder(repairEvents), }); - expect(recorded).toEqual(["sandbox", "openclaw"]); - expect(applyResult).not.toHaveBeenCalled(); + expect(repairEvents).toHaveLength(4); + expect(result.session.machine.state).toBe(state); + expect(result.context.sandboxName).toBe("created-sandbox"); }); it.each([ "complete", "failed", - ] as const)("rejects terminal %s sessions before core compatibility side effects", async (state) => { - const phase: OnboardSequencePhase = { + ] as const)("rejects terminal %s sessions before core repair effects", async (state) => { + const providerInference: OnboardSequencePhase = { state: "provider_selection", - run: vi.fn((ctx) => ({ context: ctx, result: advanceTo("sandbox") })), + run: vi.fn((ctx) => ({ + context: ctx, + result: advanceTo("sandbox", { metadata: { state: "inference" } }), + })), }; - const sandboxPhase: OnboardSequencePhase = { + const sandbox: OnboardSequencePhase = { state: "sandbox", - run: vi.fn((ctx) => ({ context: ctx, result: advanceTo("openclaw") })), + run: vi.fn((ctx) => ({ + context: ctx, + result: branchTo("openclaw", { metadata: { state: "sandbox" } }), + })), }; await expect( @@ -706,93 +788,12 @@ describe("core onboard flow phases", () => { }), applyResult: async () => createSession(), }, - phases: { providerInference: phase, sandbox: sandboxPhase }, + phases: { providerInference, sandbox }, resume: true, - recordStateResult: async () => undefined, - recordInvalidatedStateResult: recordInvalidatedTargets([]), + recordRepairEvent: repairRecorder(), }), ).rejects.toThrow("Unexpected onboarding live flow state before slice entry"); - expect(phase.run).not.toHaveBeenCalled(); - expect(sandboxPhase.run).not.toHaveBeenCalled(); - }); - - it("keeps non-resume ahead-state sessions on the compatibility path", async () => { - const calls: string[] = []; - const skipped: string[] = []; - const applied: string[] = []; - let runtimeSession = createSession({ - machine: { - version: 1, - state: "sandbox", - stateEnteredAt: "2026-06-09T00:02:00.000Z", - revision: 7, - }, - }); - const phases: CoreOnboardFlowPhases = { - providerInference: { - state: "provider_selection", - run: (ctx) => { - calls.push("provider_selection"); - return { - context: ctx, - result: [ - advanceTo("inference", { - metadata: { state: "provider_selection", provider: "nim", model: "nvidia/test" }, - }), - advanceTo("sandbox", { - metadata: { state: "inference", provider: "nim", model: "nvidia/test" }, - }), - ], - }; - }, - }, - sandbox: { - state: "sandbox", - run: (ctx) => { - calls.push("sandbox"); - return { - context: { ...ctx, sandboxName: "created-sandbox" }, - result: advanceTo("openclaw", { metadata: { state: "sandbox" } }), - }; - }, - }, - }; - - const result = await runCoreOnboardFlowSlice({ - context: context(), - runtime: { - session: async () => runtimeSession, - applyResult: async () => { - throw new Error("ahead-state compatibility path should not use strict applyResult"); - }, - }, - phases, - resume: false, - recordStateResult: async (stateResult: OnboardStateResult) => { - if (stateResult.type !== "transition") return runtimeSession; - const source = stateResult.metadata?.state; - applied.push(`${source}->${stateResult.next}`); - runtimeSession = createSession({ - machine: { - version: 1, - state: stateResult.next, - stateEnteredAt: "2026-06-09T00:03:00.000Z", - revision: runtimeSession.machine.revision + 1, - }, - }); - return runtimeSession; - }, - recordInvalidatedStateResult: async (stateResult, invalidation) => { - if (stateResult.type !== "transition") return runtimeSession; - skipped.push(`${invalidation.sourceState ?? "unknown"}->${stateResult.next}`); - return runtimeSession; - }, - }); - - expect(calls).toEqual(["provider_selection", "sandbox"]); - expect(skipped).toEqual(["provider_selection->inference", "inference->sandbox"]); - expect(applied).toEqual(["sandbox->openclaw"]); - expect(result.context.sandboxName).toBe("created-sandbox"); - expect(result.session.machine.state).toBe("openclaw"); + expect(providerInference.run).not.toHaveBeenCalled(); + expect(sandbox.run).not.toHaveBeenCalled(); }); }); diff --git a/src/lib/onboard/machine/core-flow-phases.ts b/src/lib/onboard/machine/core-flow-phases.ts index a2415ea8441..52587a361d6 100644 --- a/src/lib/onboard/machine/core-flow-phases.ts +++ b/src/lib/onboard/machine/core-flow-phases.ts @@ -23,13 +23,14 @@ import { type ProviderInferenceStateOptions, } from "./handlers/provider-inference"; import { handleSandboxState, type SandboxStateOptions } from "./handlers/sandbox"; +import { UnexpectedLiveOnboardFlowSliceStateError } from "./live-flow-slice"; import { - type InvalidatedOnboardStateResultRecorder, - runLiveOnboardFlowSlice, -} from "./live-flow-slice"; -import type { OnboardStateResult } from "./result"; + type OnboardPrerequisiteRepairEventRecorder, + runOnboardPrerequisiteRepair, +} from "./prerequisite-repair"; import type { OnboardMachineRunnerResult, OnboardMachineRunnerRuntime } from "./runner"; -import type { OnboardSequencePhase } from "./sequence-runner"; +import { runOnboardSequenceWithRunner, type OnboardSequencePhase } from "./sequence-runner"; +import type { OnboardMachineState } from "./types"; export interface EndpointProvenanceOptions { endpointSource?: InferenceEndpointSource | null; @@ -256,30 +257,91 @@ export async function runCoreOnboardFlowSlice; resume: boolean; - recordStateResult(result: OnboardStateResult): Promise; - recordInvalidatedStateResult: InvalidatedOnboardStateResultRecorder; + recordRepairEvent: OnboardPrerequisiteRepairEventRecorder; }): Promise> { - // Exact provider-selection entry is runner-owned for fresh and resumed flows. - // Recompute only when a durable snapshot is already downstream even though - // provider/sandbox repair and backstop checks must still re-run. Those - // ahead-state snapshots can come from legacy/test step mutation that - // explicitly opts into `updateMachine === true` or from repaired-resume replay - // of persisted sessions. Recomputed transition results are explicitly applied - // or invalidated by runLiveOnboardFlowSlice, so stale phase output cannot - // update context or silently advance state. The tolerated downstream family - // includes inference, sandbox branch states, and the final slice handoff - // states. Remove this fallback once those checks are strict FSM recovery states - // and legacy machine step mutation is gone. - return runLiveOnboardFlowSlice({ + const durableEntry = await options.runtime.session(); + const state = durableEntry.machine.state; + const allowedStates: readonly OnboardMachineState[] = options.resume + ? [ + "provider_selection", + "inference", + "sandbox", + "openclaw", + "agent_setup", + "policies", + "finalizing", + "post_verify", + ] + : ["provider_selection", "inference", "sandbox", "openclaw", "agent_setup"]; + if (!allowedStates.includes(state)) { + throw new UnexpectedLiveOnboardFlowSliceStateError( + state, + ["provider_selection", "inference", "sandbox"], + allowedStates.filter( + (candidate) => + candidate !== "provider_selection" && + candidate !== "inference" && + candidate !== "sandbox", + ), + ); + } + if (state === "provider_selection") { + return runCoreOnboardFlowSequence({ + context: options.context, + runtime: options.runtime, + phases: [options.phases.providerInference, options.phases.sandbox], + }); + } + + const providerRepair = await runOnboardPrerequisiteRepair({ context: options.context, + durableEntryState: state, + phase: options.phases.providerInference, + expectedFinalStates: ["sandbox"], + repair: "core-flow-prerequisite", + runtime: options.runtime, + recordRepairEvent: options.recordRepairEvent, + }); + if (state === "inference") { + const inferenceResult = [...providerRepair.results] + .reverse() + .find((result) => result.type === "transition" && result.metadata?.state === "inference"); + if (!inferenceResult) { + throw new Error("Core onboarding inference repair returned no inference result"); + } + const inferencePhase: OnboardSequencePhase = { + state: "inference", + run: () => ({ context: providerRepair.context, result: inferenceResult }), + }; + return runOnboardSequenceWithRunner({ + context: providerRepair.context, + runtime: options.runtime, + phases: [inferencePhase, options.phases.sandbox], + stopStates: ["openclaw", "agent_setup"], + }); + } + if (state === "sandbox") { + return runOnboardSequenceWithRunner({ + context: providerRepair.context, + runtime: options.runtime, + phases: [options.phases.sandbox], + stopStates: ["openclaw", "agent_setup"], + }); + } + + const sandboxRepair = await runOnboardPrerequisiteRepair({ + context: providerRepair.context, + durableEntryState: state, + phase: options.phases.sandbox, + expectedFinalStates: ["openclaw", "agent_setup"], + repair: "core-flow-prerequisite", runtime: options.runtime, - phases: [options.phases.providerInference, options.phases.sandbox], - runWhenState: ["provider_selection"], - compatibilityWhenState: options.resume - ? ["inference", "sandbox", "openclaw", "agent_setup", "policies", "finalizing", "post_verify"] - : ["inference", "sandbox", "openclaw", "agent_setup"], - runSlice: runCoreOnboardFlowSequence, - recordStateResult: options.recordStateResult, - recordInvalidatedStateResult: options.recordInvalidatedStateResult, + recordRepairEvent: options.recordRepairEvent, }); + if ((state === "openclaw" || state === "agent_setup") && sandboxRepair.finalState !== state) { + throw new Error( + `Core onboarding prerequisite repair selected '${sandboxRepair.finalState}' for durable entry '${state}'`, + ); + } + return { context: sandboxRepair.context, session: await options.runtime.session() }; } diff --git a/src/lib/onboard/machine/handlers/provider-inference.ts b/src/lib/onboard/machine/handlers/provider-inference.ts index 5e817fb2669..ef6c6c9cd19 100644 --- a/src/lib/onboard/machine/handlers/provider-inference.ts +++ b/src/lib/onboard/machine/handlers/provider-inference.ts @@ -15,6 +15,7 @@ import type { InferenceEndpointSource } from "../../../inference/selection"; import type { WebSearchConfig } from "../../../inference/web-search"; import type { HermesAuthMethod, Session, SessionUpdates } from "../../../state/onboard-session"; import type { OnboardInferenceCapabilityCache } from "../../inference-capability-cache"; +import { checkpointSandboxIdentityMatches } from "../../checkpoint-replay"; import type { RepairLocalInferenceSystemdOverrideOptions } from "../../local-inference-topology"; import { describeIgnoredReasoningEnv } from "../../reasoning-mode"; import type { @@ -481,8 +482,7 @@ export async function handleProviderInferenceState({ if ( (!selectedAgentName || selectedAgentName === "openclaw") && sandboxName && - session?.sandboxPromptProgress?.sandboxName === true && - session.sandboxName === sandboxName + checkpointSandboxIdentityMatches(session, sandboxName) ) { deps.log(` [resume] Reusing sandbox name: ${sandboxName}.`); } diff --git a/src/lib/onboard/machine/initial-flow-phases.test.ts b/src/lib/onboard/machine/initial-flow-phases.test.ts index 38ee89b09b7..16c8b6ecfb4 100644 --- a/src/lib/onboard/machine/initial-flow-phases.test.ts +++ b/src/lib/onboard/machine/initial-flow-phases.test.ts @@ -4,14 +4,14 @@ import { describe, expect, it, vi } from "vitest"; import { createSession, type Session } from "../../state/onboard-session"; -import { recordInvalidatedTargets } from "../__test-helpers__/machine-recorders"; import { resolveGatewayOwner } from "../gateway-ownership"; import { createInitialOnboardFlowPhases, type InitialOnboardFlowContext, runInitialOnboardFlowSlice, } from "./initial-flow-phases"; -import { advanceTo, type OnboardStateResult } from "./result"; +import type { OnboardPrerequisiteRepairEventRecorder } from "./prerequisite-repair"; +import { advanceTo } from "./result"; import type { OnboardMachineRunnerRuntime } from "./runner"; import type { OnboardSequencePhase } from "./sequence-runner"; @@ -69,7 +69,22 @@ function config(gpu: Gpu): SandboxGpuConfig { function runtime(session: Session = createSession()): OnboardMachineRunnerRuntime { return { session: async () => session, - applyResult: async () => session, + applyResult: async (result) => { + if (result.type === "transition") { + session.machine = { + ...session.machine, + state: result.next, + revision: session.machine.revision + 1, + }; + } + return session; + }, + }; +} + +function repairRecorder(events: string[] = []): OnboardPrerequisiteRepairEventRecorder { + return async (type, options) => { + events.push(`${type}:${options.state ?? "unknown"}`); }; } @@ -194,16 +209,22 @@ describe("initial onboard flow phases", () => { ); }); - it("records each phase result when a resume snapshot is already at gateway", async () => { - const recorded: string[] = []; + it("repairs preflight before strict gateway entry", async () => { + const events: string[] = []; const phases: readonly OnboardSequencePhase[] = [ { state: "preflight", - run: (ctx) => ({ context: ctx, result: advanceTo("gateway") }), + run: (ctx) => ({ + context: ctx, + result: advanceTo("gateway", { metadata: { state: "preflight" } }), + }), }, { state: "gateway", - run: (ctx) => ({ context: ctx, result: advanceTo("provider_selection") }), + run: (ctx) => ({ + context: ctx, + result: advanceTo("provider_selection", { metadata: { state: "gateway" } }), + }), }, ]; @@ -221,16 +242,13 @@ describe("initial onboard flow phases", () => { ), phases, resume: true, - recordStateResult: async (result) => { - if (result.type === "transition") recorded.push(result.next); - }, - recordInvalidatedStateResult: recordInvalidatedTargets(recorded), + recordRepairEvent: repairRecorder(events), }); - expect(recorded).toEqual(["gateway", "provider_selection"]); + expect(events).toEqual(["state.repair.started:preflight", "state.repair.completed:preflight"]); }); - it("returns the runtime session after ahead-state compatibility recording", async () => { + it("returns the runtime session after strict gateway entry", async () => { const phaseSession = createSession({ machine: { version: 1, @@ -252,14 +270,14 @@ describe("initial onboard flow phases", () => { state: "preflight", run: (ctx) => ({ context: { ...ctx, session: phaseSession }, - result: advanceTo("gateway"), + result: advanceTo("gateway", { metadata: { state: "preflight" } }), }), }, { state: "gateway", run: (ctx) => ({ context: { ...ctx, session: phaseSession }, - result: advanceTo("provider_selection"), + result: advanceTo("provider_selection", { metadata: { state: "gateway" } }), }), }, ]; @@ -268,25 +286,20 @@ describe("initial onboard flow phases", () => { context: context({ resume: true, session: phaseSession }), runtime: { session: async () => runtimeSession, - applyResult: async () => { - throw new Error("resume compatibility path should not use strict applyResult"); + applyResult: async (stateResult) => { + if (stateResult.type === "transition") { + runtimeSession.machine = { + ...runtimeSession.machine, + state: stateResult.next, + revision: runtimeSession.machine.revision + 1, + }; + } + return runtimeSession; }, }, phases, resume: true, - recordStateResult: async (stateResult) => { - if (stateResult.type === "transition") { - runtimeSession = createSession({ - machine: { - version: 1, - state: stateResult.next, - stateEnteredAt: "2026-06-09T00:01:00.000Z", - revision: 1, - }, - }); - } - }, - recordInvalidatedStateResult: recordInvalidatedTargets([]), + recordRepairEvent: repairRecorder(), }); expect(result.context.session).toBe(phaseSession); @@ -447,7 +460,7 @@ describe("initial onboard flow phases", () => { }, note: vi.fn(), }); - const recorded: string[] = []; + const repairEvents: string[] = []; const result = await runInitialOnboardFlowSlice({ context: context({ @@ -459,10 +472,7 @@ describe("initial onboard flow phases", () => { runtime: runtime(session), phases, resume: true, - recordStateResult: async (stateResult) => { - if (stateResult.type === "transition") recorded.push(stateResult.next); - }, - recordInvalidatedStateResult: recordInvalidatedTargets(recorded), + recordRepairEvent: repairRecorder(repairEvents), }); expect(result.session.machine.state).toBe("provider_selection"); @@ -489,13 +499,13 @@ describe("initial onboard flow phases", () => { "record-gateway-skipped", "record-gateway-complete", ]); - expect(recorded).toEqual(["gateway", "provider_selection"]); - // Ahead-state resume invalidates the preflight/gateway transitions but the - // recomputed context (sandboxGpuConfig, gpu, gpuPassthrough) must still - // survive so runOnboard's assertion at src/lib/onboard.ts:4397 - // ("Preflight did not produce a sandbox GPU configuration") stays - // satisfied on resume, and downstream sandbox setup can consume the - // freshly detected GPU rather than a stale saved value (#6227). + expect(repairEvents).toEqual([ + "state.repair.started:preflight", + "state.repair.completed:preflight", + "state.repair.started:gateway", + "state.repair.completed:gateway", + ]); + // Repair context must carry the current GPU observation into sandbox setup. expect(result.context.sandboxGpuConfig).toEqual(config(gpu)); expect(result.context.gpu).toEqual(gpu); expect(result.context.gpuPassthrough).toBe(true); @@ -509,16 +519,22 @@ describe("initial onboard flow phases", () => { "policies", "finalizing", "post_verify", - ] as const)("lets resume sessions at %s pass through initial compatibility", async (state) => { - const recorded: string[] = []; + ] as const)("repairs initial prerequisites for resumed %s entry", async (state) => { + const repairEvents: string[] = []; const phases: readonly OnboardSequencePhase[] = [ { state: "preflight", - run: (ctx) => ({ context: ctx, result: advanceTo("gateway") }), + run: (ctx) => ({ + context: ctx, + result: advanceTo("gateway", { metadata: { state: "preflight" } }), + }), }, { state: "gateway", - run: (ctx) => ({ context: ctx, result: advanceTo("provider_selection") }), + run: (ctx) => ({ + context: ctx, + result: advanceTo("provider_selection", { metadata: { state: "gateway" } }), + }), }, ]; @@ -536,22 +552,22 @@ describe("initial onboard flow phases", () => { ), phases, resume: true, - recordStateResult: async (stateResult) => { - recorded.push((stateResult as ReturnType).next); - }, - recordInvalidatedStateResult: recordInvalidatedTargets(recorded), + recordRepairEvent: repairRecorder(repairEvents), }); - expect(recorded).toEqual(["gateway", "provider_selection"]); + expect(repairEvents).toHaveLength(4); }); it.each([ "complete", "failed", - ] as const)("rejects terminal %s sessions before initial compatibility side effects", async (state) => { + ] as const)("rejects terminal %s sessions before initial repair effects", async (state) => { const phase: OnboardSequencePhase = { state: "preflight", - run: vi.fn((ctx) => ({ context: ctx, result: advanceTo("gateway") })), + run: vi.fn((ctx) => ({ + context: ctx, + result: advanceTo("gateway", { metadata: { state: "preflight" } }), + })), }; await expect( @@ -569,8 +585,7 @@ describe("initial onboard flow phases", () => { ), phases: [phase], resume: true, - recordStateResult: async () => undefined, - recordInvalidatedStateResult: recordInvalidatedTargets([]), + recordRepairEvent: repairRecorder(), }), ).rejects.toThrow("Unexpected onboarding live flow state before slice entry"); expect(phase.run).not.toHaveBeenCalled(); @@ -624,11 +639,8 @@ describe("initial onboard flow phases", () => { }, phases, resume, - recordStateResult: async () => { - throw new Error("compatibility recorder should not run"); - }, - recordInvalidatedStateResult: async () => { - throw new Error("invalidation recorder should not run on the strict runner path"); + recordRepairEvent: async () => { + throw new Error("repair recorder should not run on the exact-entry path"); }, }); @@ -679,11 +691,8 @@ describe("initial onboard flow phases", () => { }, phases, resume, - recordStateResult: async () => { - throw new Error("compatibility recorder should not run"); - }, - recordInvalidatedStateResult: async () => { - throw new Error("invalidation recorder should not run on the strict runner path"); + recordRepairEvent: async () => { + throw new Error("repair recorder should not run on the exact-entry path"); }, }); diff --git a/src/lib/onboard/machine/initial-flow-phases.ts b/src/lib/onboard/machine/initial-flow-phases.ts index 685435f4b3e..4a231be6c57 100644 --- a/src/lib/onboard/machine/initial-flow-phases.ts +++ b/src/lib/onboard/machine/initial-flow-phases.ts @@ -13,13 +13,14 @@ import { type PreflightSandboxGpuFlag, type PreflightStateOptions, } from "./handlers/preflight"; +import { UnexpectedLiveOnboardFlowSliceStateError } from "./live-flow-slice"; import { - type InvalidatedOnboardStateResultRecorder, - runLiveOnboardFlowSlice, -} from "./live-flow-slice"; -import type { OnboardStateResult } from "./result"; + type OnboardPrerequisiteRepairEventRecorder, + runOnboardPrerequisiteRepair, +} from "./prerequisite-repair"; import type { OnboardMachineRunnerResult, OnboardMachineRunnerRuntime } from "./runner"; import type { OnboardSequencePhase } from "./sequence-runner"; +import type { OnboardMachineState } from "./types"; export type InitialOnboardFlowContext< Agent, @@ -191,47 +192,64 @@ export async function runInitialOnboardFlowSlice[]; resume: boolean; - recordStateResult(result: OnboardStateResult): Promise; - recordInvalidatedStateResult: InvalidatedOnboardStateResultRecorder; + recordRepairEvent: OnboardPrerequisiteRepairEventRecorder; }): Promise> { - // The strict runner owns both exact entry states (`init` and `preflight`). - // Recompute plan for live resume repair when durable machine snapshots - // are already downstream of those entries even though preflight/gateway host - // backstops must still re-run. Those ahead-state snapshots can come from - // legacy/test step mutation that explicitly opts into `updateMachine === true` - // or from repaired-resume replay of persisted sessions. Recomputed transition - // results are explicitly applied or invalidated by runLiveOnboardFlowSlice, - // so stale phase output cannot update context or silently advance state. - // This slice cannot - // eliminate that source locally because the host backstop checks are still - // modeled as imperative resume work rather than strict FSM recovery states. - // The tolerated family is every nonterminal state after the exact - // `init`/`preflight` entries: gateway, provider_selection, inference, sandbox, - // openclaw/agent_setup, policies, finalizing, and post_verify. Non-resume - // compatibility is limited to gateway and provider_selection. Phase tests - // cover ahead-state resume and terminal-state rejection; remove this fallback - // once those repair/backstop checks are modeled as strict FSM recovery states - // and legacy machine step mutation is gone. - return runLiveOnboardFlowSlice({ + const durableEntry = await options.runtime.session(); + const state = durableEntry.machine.state; + const resumeAheadStates: readonly OnboardMachineState[] = [ + "gateway", + "provider_selection", + "inference", + "sandbox", + "openclaw", + "agent_setup", + "policies", + "finalizing", + "post_verify", + ]; + const allowedStates: readonly OnboardMachineState[] = options.resume + ? ["init", "preflight", ...resumeAheadStates] + : ["init", "preflight", "gateway", "provider_selection"]; + if (!allowedStates.includes(state)) { + throw new UnexpectedLiveOnboardFlowSliceStateError( + state, + ["init", "preflight"], + allowedStates.filter((candidate) => candidate !== "init" && candidate !== "preflight"), + ); + } + if (state === "init" || state === "preflight") { + return runInitialOnboardFlowSequence(options); + } + + const preflight = options.phases.find((phase) => phase.state === "preflight"); + const gateway = options.phases.find((phase) => phase.state === "gateway"); + if (!preflight || !gateway || options.phases.length !== 2) { + throw new Error("Expected one preflight phase and one gateway phase"); + } + const preflightRepair = await runOnboardPrerequisiteRepair({ context: options.context, + durableEntryState: state, + phase: preflight, + expectedFinalStates: ["gateway"], + repair: "initial-flow-prerequisite", + runtime: options.runtime, + recordRepairEvent: options.recordRepairEvent, + }); + if (state === "gateway") { + return runInitialOnboardFlowSequence({ + context: preflightRepair.context, + runtime: options.runtime, + phases: options.phases, + }); + } + const gatewayRepair = await runOnboardPrerequisiteRepair({ + context: preflightRepair.context, + durableEntryState: state, + phase: gateway, + expectedFinalStates: ["provider_selection"], + repair: "initial-flow-prerequisite", runtime: options.runtime, - phases: options.phases, - runWhenState: ["init", "preflight"], - compatibilityWhenState: options.resume - ? [ - "gateway", - "provider_selection", - "inference", - "sandbox", - "openclaw", - "agent_setup", - "policies", - "finalizing", - "post_verify", - ] - : ["gateway", "provider_selection"], - runSlice: runInitialOnboardFlowSequence, - recordStateResult: options.recordStateResult, - recordInvalidatedStateResult: options.recordInvalidatedStateResult, + recordRepairEvent: options.recordRepairEvent, }); + return { context: gatewayRepair.context, session: await options.runtime.session() }; } diff --git a/src/lib/onboard/machine/prerequisite-repair.test.ts b/src/lib/onboard/machine/prerequisite-repair.test.ts new file mode 100644 index 00000000000..68078358ed7 --- /dev/null +++ b/src/lib/onboard/machine/prerequisite-repair.test.ts @@ -0,0 +1,96 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { describe, expect, it, vi } from "vitest"; + +import { createSession } from "../../state/onboard-session"; +import { advanceTo, retryTo } from "./result"; +import { runOnboardPrerequisiteRepair } from "./prerequisite-repair"; + +describe("runOnboardPrerequisiteRepair", () => { + it("accepts a legal update-free chain and preserves the durable entry state", async () => { + const session = createSession({ + machine: { + version: 1, + state: "sandbox", + stateEnteredAt: "2026-07-28T00:00:00.000Z", + revision: 4, + }, + }); + const recordRepairEvent = vi.fn(async (_type: string) => undefined); + + const result = await runOnboardPrerequisiteRepair({ + context: { provider: "nim" }, + durableEntryState: "sandbox", + phase: { + state: "provider_selection", + run: (context) => ({ + context: { ...context, model: "nvidia/test" }, + result: [ + advanceTo("inference", { metadata: { state: "provider_selection" } }), + retryTo("provider_selection", { metadata: { state: "inference" } }), + advanceTo("inference", { metadata: { state: "provider_selection" } }), + advanceTo("sandbox", { metadata: { state: "inference" } }), + ], + }), + }, + expectedFinalStates: ["sandbox"], + repair: "core-flow-prerequisite", + runtime: { + session: async () => session, + applyResult: async () => { + throw new Error("repair must not apply a result"); + }, + }, + recordRepairEvent, + }); + + expect(result.context).toEqual({ provider: "nim", model: "nvidia/test" }); + expect(result.finalState).toBe("sandbox"); + expect(session.machine).toMatchObject({ state: "sandbox", revision: 4 }); + expect(recordRepairEvent.mock.calls.map(([type]) => type)).toEqual([ + "state.repair.started", + "state.repair.completed", + ]); + }); + + it("rejects a repair result that contains a session update", async () => { + const session = createSession({ + machine: { + version: 1, + state: "gateway", + stateEnteredAt: "2026-07-28T00:00:00.000Z", + revision: 2, + }, + }); + const recordRepairEvent = vi.fn(async (_type: string) => undefined); + + await expect( + runOnboardPrerequisiteRepair({ + context: {}, + durableEntryState: "gateway", + phase: { + state: "preflight", + run: (context) => ({ + context, + result: advanceTo("gateway", { + updates: { provider: "nim" }, + metadata: { state: "preflight" }, + }), + }), + }, + expectedFinalStates: ["gateway"], + repair: "initial-flow-prerequisite", + runtime: { + session: async () => session, + applyResult: async () => session, + }, + recordRepairEvent, + }), + ).rejects.toThrow("expected an update-free transition"); + expect(recordRepairEvent).toHaveBeenLastCalledWith( + "state.repair.failed", + expect.objectContaining({ state: "preflight" }), + ); + }); +}); diff --git a/src/lib/onboard/machine/prerequisite-repair.ts b/src/lib/onboard/machine/prerequisite-repair.ts new file mode 100644 index 00000000000..3a832c0ea14 --- /dev/null +++ b/src/lib/onboard/machine/prerequisite-repair.ts @@ -0,0 +1,123 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { createPhaseProgressReporter } from "./phase-progress"; +import type { OnboardStateResult } from "./result"; +import type { OnboardMachineRunnerRuntime, OnboardStateHandlerResult } from "./runner"; +import type { OnboardSequencePhase } from "./sequence-runner"; +import { assertValidOnboardMachineTransition, isOnboardMachineState } from "./transitions"; +import type { OnboardMachineEventType, OnboardMachineState } from "./types"; + +type RepairEventType = Extract< + OnboardMachineEventType, + "state.repair.started" | "state.repair.completed" | "state.repair.failed" +>; + +export type OnboardPrerequisiteRepairEventRecorder = ( + type: RepairEventType, + options: { + state?: OnboardMachineState | null; + error?: string | null; + metadata?: Record | null; + }, +) => Promise; + +export interface OnboardPrerequisiteRepairResult { + readonly context: Context; + readonly results: readonly OnboardStateResult[]; + readonly finalState: OnboardMachineState; +} + +function resultArray( + result: OnboardStateHandlerResult, + state: OnboardSequencePhase["state"], +): readonly OnboardStateResult[] { + const results = Array.isArray(result) + ? (result as readonly OnboardStateResult[]) + : [result as OnboardStateResult]; + if (results.length === 0) { + throw new Error(`Onboarding prerequisite repair for '${state}' returned no results`); + } + return results; +} + +function validateRepairResults( + results: readonly OnboardStateResult[], + phaseState: OnboardSequencePhase["state"], + expectedFinalStates: readonly OnboardMachineState[], +): OnboardMachineState { + let state: OnboardMachineState = phaseState; + for (const result of results) { + const source = result.metadata?.state; + if ( + result.type !== "transition" || + result.updates !== undefined || + !isOnboardMachineState(source) || + source !== state + ) { + throw new Error( + `Invalid onboarding prerequisite repair result for '${phaseState}'; expected an update-free transition from '${state}'`, + ); + } + const transition = assertValidOnboardMachineTransition(state, result.next); + if (result.transitionKind !== transition.kind) { + throw new Error( + `Invalid onboarding prerequisite repair result for '${phaseState}'; expected transition kind '${transition.kind}' from '${state}' to '${result.next}'`, + ); + } + state = result.next; + } + if (!expectedFinalStates.includes(state)) { + throw new Error( + `Invalid onboarding prerequisite repair result for '${phaseState}'; expected final state ${expectedFinalStates.join(" or ")}, got '${state}'`, + ); + } + return state; +} + +function errorMessage(error: unknown): string { + return error instanceof Error ? error.message : String(error); +} + +export async function runOnboardPrerequisiteRepair(options: { + context: Context; + durableEntryState: OnboardMachineState; + phase: OnboardSequencePhase; + expectedFinalStates: readonly OnboardMachineState[]; + repair: string; + runtime: OnboardMachineRunnerRuntime; + recordRepairEvent: OnboardPrerequisiteRepairEventRecorder; +}): Promise> { + const metadata = { + repair: options.repair, + entryState: options.durableEntryState, + }; + const phase = createPhaseProgressReporter().wrap(options.phase); + try { + await options.recordRepairEvent("state.repair.started", { + state: phase.state, + metadata, + }); + const phaseResult = await phase.run(options.context); + const results = resultArray(phaseResult.result, phase.state); + const finalState = validateRepairResults(results, phase.state, options.expectedFinalStates); + const current = await options.runtime.session(); + if (current.machine.state !== options.durableEntryState) { + throw new Error( + `Onboarding prerequisite repair for '${phase.state}' changed durable entry state from '${options.durableEntryState}' to '${current.machine.state}'`, + ); + } + await options.recordRepairEvent("state.repair.completed", { + state: phase.state, + metadata, + }); + return { context: phaseResult.context, results, finalState }; + } catch (error) { + await options.recordRepairEvent("state.repair.failed", { + state: phase.state, + error: errorMessage(error), + metadata, + }); + throw error; + } +} From 6d2a218ffefb0a56a36a676c8b23bb1f013ff0ce Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Tue, 28 Jul 2026 00:40:07 -0700 Subject: [PATCH 02/19] refactor(onboard): make runtime the machine authority Signed-off-by: Carlos Villela --- src/lib/onboard/lifecycle-contracts.md | 4 +- src/lib/onboard/machine/README.md | 6 +- .../onboard/machine/core-flow-phases.test.ts | 2 +- src/lib/onboard/machine/core-flow-phases.ts | 4 +- .../machine/final-flow-phases.runtime.test.ts | 4 +- src/lib/onboard/machine/final-flow-phases.ts | 4 +- src/lib/onboard/machine/flow-slice-error.ts | 15 + .../machine/initial-flow-phases.test.ts | 2 +- .../onboard/machine/initial-flow-phases.ts | 4 +- .../onboard/machine/live-flow-slice.test.ts | 406 ------------------ src/lib/onboard/machine/live-flow-slice.ts | 166 ------- src/lib/onboard/machine/runtime.test.ts | 54 +-- src/lib/onboard/machine/runtime.ts | 88 ++-- src/lib/onboard/resume-machine-repair.test.ts | 1 - .../runtime-boundary-record-only.test.ts | 76 ++-- src/lib/onboard/runtime-boundary.test.ts | 398 +---------------- src/lib/onboard/runtime-boundary.ts | 120 +----- src/lib/state/onboard-session.test.ts | 205 +++------ src/lib/state/onboard-session.ts | 127 +----- src/lib/state/onboard-step-mutation.test.ts | 155 +------ src/lib/state/onboard-step-mutation.ts | 25 -- test/helpers/onboard-legacy-step-mutation.ts | 49 --- test/repro-1751-extra.test.ts | 28 +- 23 files changed, 282 insertions(+), 1661 deletions(-) create mode 100644 src/lib/onboard/machine/flow-slice-error.ts delete mode 100644 src/lib/onboard/machine/live-flow-slice.test.ts delete mode 100644 src/lib/onboard/machine/live-flow-slice.ts delete mode 100644 src/lib/state/onboard-step-mutation.ts delete mode 100644 test/helpers/onboard-legacy-step-mutation.ts diff --git a/src/lib/onboard/lifecycle-contracts.md b/src/lib/onboard/lifecycle-contracts.md index 3a7fa06c9bf..77c09b3add7 100644 --- a/src/lib/onboard/lifecycle-contracts.md +++ b/src/lib/onboard/lifecycle-contracts.md @@ -155,7 +155,7 @@ The schema and sanitation authority is `Session` plus `normalizeSession`/`filter | Field group | Fields | Writer/owner and state meaning | |---|---|---| | Session envelope | `version`, `sessionId`, `mode`, `startedAt`, `updatedAt`, `status`, `resumable` | `createSession`, save/update helpers, and completion/failure paths. Values are always known after creation. | -| Progress and recovery | `lastStepStarted`, `lastCompletedStep`, `failure`, `steps`, `machine`, `sandboxPromptProgress`, `stagedCredentialProviders`, `checkpoint` | Step-mutation helpers and `OnboardRuntime` own whole-step progress; the OpenClaw sandbox handler owns prompt-group completion markers. `stagedCredentialProviders` contains only names registered before sandbox setup so OpenClaw resume can require both durable ownership and an exact live binding. A marker is trusted only when its matching persisted value is present and valid, including an explicit `null` where supported. `checkpoint` is the dedicated versioned resume contract: a secret-free tri-state decision record plus durable sandbox identity, effect-group receipts, and logical web-search and messaging provider bindings, serialized alongside the session under its own `schemaVersion` with fail-closed handling of an unknown future version. The primary inference provider binding remains owned and revalidated by the provider and inference phases instead of entering this checkpoint ledger. | +| Progress and recovery | `lastStepStarted`, `lastCompletedStep`, `failure`, `steps`, `machine`, `sandboxPromptProgress`, `stagedCredentialProviders`, `checkpoint` | Step helpers own step status. `OnboardRuntime` owns normal machine transitions, revisions, terminal state, and machine events. Explicit session recovery and the process-exit failure backstop own their narrow snapshot changes. The OpenClaw sandbox handler owns prompt-group completion markers. `stagedCredentialProviders` contains only names registered before sandbox setup so OpenClaw resume can require both durable ownership and an exact live binding. A marker is trusted only when its matching persisted value is present and valid, including an explicit `null` where supported. `checkpoint` is the dedicated versioned resume contract: a secret-free tri-state decision record plus durable sandbox identity, effect-group receipts, and logical web-search and messaging provider bindings, serialized alongside the session under its own `schemaVersion` with fail-closed handling of an unknown future version. The primary inference provider binding remains owned and revalidated by the provider and inference phases instead of entering this checkpoint ledger. | | Target identity | `agent`, `sandboxName`, `metadata.gatewayName`, `metadata.fromDockerfile` | Onboard selection, sandbox handler/registration, and rebuild session preparation. A completed sandbox step or valid `sandboxPromptProgress.sandboxName` marker is the trust gate for a recorded name. | | Inference intent | `provider`, `model`, `endpointUrl`, `credentialEnv`, `preferredInferenceApi`, `compatibleEndpointReasoning`, `nimContainer`, `webSearchConfig` | Provider/inference handlers and `runInferenceSet`. Known credential state is an environment-variable name or presence metadata, never the value. `redactUrl` masks userinfo and fragments, redacts values under sensitive parameter names, and redacts canonical token-shaped values even under benign parameter names. | | Agent and policy intent | `hermesAuthMethod`, `toolDisclosure`, `hermesToolGateways`, `policyPresets` | Agent setup and policy handling. Channel commands update matching-session `policyPresets` only best-effort. Nullable fields conflate unset, declined, and cleared where the CLI makes those distinctions. | @@ -164,7 +164,7 @@ The schema and sanitation authority is `Session` plus `normalizeSession`/`filter | Runtime metadata | `routerPid`, `routerCredentialHash`, `gpuPassthrough` | Router and sandbox setup/recovery. PID is a live-process hint; credential hash is a digest; GPU is a concrete boolean. | | Legacy migration proof | `migratedLegacyValueHashes` | Onboard legacy migration writes SHA-256 digests keyed by environment name; session filtering guarantees string records but does not independently validate digest shape. | -The registry is separately owned by `src/lib/state/registry.ts`; backup/recovery manifests are owned by their rebuild and recreate modules. Step helpers normally use `RECORD_ONLY_STEP_MUTATION_OPTIONS`; `LEGACY_MACHINE_STEP_MUTATION_OPTIONS` is the compatibility path that also moves the machine snapshot. +The registry is separately owned by `src/lib/state/registry.ts`; backup/recovery manifests are owned by their rebuild and recreate modules. Step helpers own only step status and safe step updates. They cannot move the machine snapshot. Explicit state results pass through `OnboardRuntime` validation. ## Duplicated decision points diff --git a/src/lib/onboard/machine/README.md b/src/lib/onboard/machine/README.md index f728b68fdf7..28e9fb5cd3a 100644 --- a/src/lib/onboard/machine/README.md +++ b/src/lib/onboard/machine/README.md @@ -87,15 +87,15 @@ without a state transition so a later process can resume the same non-terminal s ## Runtime responsibilities -`OnboardRuntime` is the intended authority for: +`OnboardRuntime` is the authority for: -- validating transitions against `transitions.ts`; +- validating result source, target, kind, and graph transitions; - applying safe session context updates; - marking terminal states; - emitting redacted lifecycle, state, repair, resume-conflict, and hook events; - normalizing older sessions before strict execution. -The runtime should reject invalid transitions before they can be persisted. +Step helpers record step status and safe step updates. They cannot change the machine snapshot or emit machine events. Explicit session recovery and the process-exit failure backstop are separate recovery boundaries. They validate their snapshot changes and run before or outside handler execution. ## Event semantics diff --git a/src/lib/onboard/machine/core-flow-phases.test.ts b/src/lib/onboard/machine/core-flow-phases.test.ts index 9480a708471..ec20419a4a6 100644 --- a/src/lib/onboard/machine/core-flow-phases.test.ts +++ b/src/lib/onboard/machine/core-flow-phases.test.ts @@ -792,7 +792,7 @@ describe("core onboard flow phases", () => { resume: true, recordRepairEvent: repairRecorder(), }), - ).rejects.toThrow("Unexpected onboarding live flow state before slice entry"); + ).rejects.toThrow("Unexpected onboarding flow state before slice entry"); expect(providerInference.run).not.toHaveBeenCalled(); expect(sandbox.run).not.toHaveBeenCalled(); }); diff --git a/src/lib/onboard/machine/core-flow-phases.ts b/src/lib/onboard/machine/core-flow-phases.ts index 52587a361d6..87692b6c1c8 100644 --- a/src/lib/onboard/machine/core-flow-phases.ts +++ b/src/lib/onboard/machine/core-flow-phases.ts @@ -23,7 +23,7 @@ import { type ProviderInferenceStateOptions, } from "./handlers/provider-inference"; import { handleSandboxState, type SandboxStateOptions } from "./handlers/sandbox"; -import { UnexpectedLiveOnboardFlowSliceStateError } from "./live-flow-slice"; +import { UnexpectedOnboardFlowSliceStateError } from "./flow-slice-error"; import { type OnboardPrerequisiteRepairEventRecorder, runOnboardPrerequisiteRepair, @@ -274,7 +274,7 @@ export async function runCoreOnboardFlowSlice { phases: createPhases(branchState, order), recordRepairEvent, }), - ).rejects.toBeInstanceOf(UnexpectedLiveOnboardFlowSliceStateError); + ).rejects.toBeInstanceOf(UnexpectedOnboardFlowSliceStateError); expect(order).toEqual([]); expect(recordRepairEvent).not.toHaveBeenCalled(); diff --git a/src/lib/onboard/machine/final-flow-phases.ts b/src/lib/onboard/machine/final-flow-phases.ts index c175ba5b92d..f17880ce764 100644 --- a/src/lib/onboard/machine/final-flow-phases.ts +++ b/src/lib/onboard/machine/final-flow-phases.ts @@ -18,7 +18,7 @@ import { handlePostVerifyState, } from "./handlers/finalization"; import { handlePoliciesState, type PoliciesStateOptions } from "./handlers/policies"; -import { UnexpectedLiveOnboardFlowSliceStateError } from "./live-flow-slice"; +import { UnexpectedOnboardFlowSliceStateError } from "./flow-slice-error"; import { createPhaseProgressReporter } from "./phase-progress"; import type { OnboardStateResult } from "./result"; import type { OnboardMachineRunnerRuntime, OnboardStateHandlerResult } from "./runner"; @@ -336,7 +336,7 @@ export async function runFinalOnboardFlowSlice { resume: true, recordRepairEvent: repairRecorder(), }), - ).rejects.toThrow("Unexpected onboarding live flow state before slice entry"); + ).rejects.toThrow("Unexpected onboarding flow state before slice entry"); expect(phase.run).not.toHaveBeenCalled(); }); diff --git a/src/lib/onboard/machine/initial-flow-phases.ts b/src/lib/onboard/machine/initial-flow-phases.ts index 4a231be6c57..498ac29e84b 100644 --- a/src/lib/onboard/machine/initial-flow-phases.ts +++ b/src/lib/onboard/machine/initial-flow-phases.ts @@ -13,7 +13,7 @@ import { type PreflightSandboxGpuFlag, type PreflightStateOptions, } from "./handlers/preflight"; -import { UnexpectedLiveOnboardFlowSliceStateError } from "./live-flow-slice"; +import { UnexpectedOnboardFlowSliceStateError } from "./flow-slice-error"; import { type OnboardPrerequisiteRepairEventRecorder, runOnboardPrerequisiteRepair, @@ -211,7 +211,7 @@ export async function runInitialOnboardFlowSlice candidate !== "init" && candidate !== "preflight"), diff --git a/src/lib/onboard/machine/live-flow-slice.test.ts b/src/lib/onboard/machine/live-flow-slice.test.ts deleted file mode 100644 index 08b87da7257..00000000000 --- a/src/lib/onboard/machine/live-flow-slice.test.ts +++ /dev/null @@ -1,406 +0,0 @@ -// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -import { afterEach, describe, expect, it, vi } from "vitest"; - -import { createSession, type Session } from "../../state/onboard-session"; -import { - EmptyLiveOnboardFlowSliceResultError, - runLiveOnboardFlowSlice, - UnexpectedLiveOnboardFlowSliceStateError, -} from "./live-flow-slice"; -import { advanceTo, type OnboardStateResult } from "./result"; -import type { OnboardMachineRunnerRuntime } from "./runner"; -import { DuplicateOnboardSequencePhaseError, type OnboardSequencePhase } from "./sequence-runner"; - -interface Context { - value: number; -} - -function runtime(state: Session["machine"]["state"]): { - runtime: OnboardMachineRunnerRuntime; - applyResult(result: OnboardStateResult): Promise; - session(): Session; -} { - let session = createSession({ - machine: { version: 1, state, stateEnteredAt: null, revision: 1 }, - }); - const runtimeApi: OnboardMachineRunnerRuntime = { - async session() { - return session; - }, - async applyResult(result) { - if (result.type === "transition") { - session = { - ...session, - machine: { - ...session.machine, - state: result.next, - revision: session.machine.revision + 1, - }, - }; - } - return session; - }, - }; - return { - runtime: runtimeApi, - applyResult(result) { - return runtimeApi.applyResult(result); - }, - session() { - return session; - }, - }; -} - -function phase( - state: OnboardSequencePhase["state"], - next: number, - result: OnboardStateResult | readonly OnboardStateResult[] = advanceTo("gateway"), -): OnboardSequencePhase { - return { - state, - run: vi.fn((context) => ({ - context: { value: next }, - result, - })), - }; -} - -function invalidatedRecorder() { - return vi.fn(async () => undefined); -} - -describe("runLiveOnboardFlowSlice", () => { - afterEach(() => { - vi.useRealTimers(); - vi.restoreAllMocks(); - }); - - it("uses the strict slice runner for fresh matching entry states", async () => { - const runSlice = vi.fn(async ({ context }) => ({ - context: { value: context.value + 1 }, - session: createSession(), - })); - const recordStateResult = vi.fn(async () => undefined); - - const result = await runLiveOnboardFlowSlice({ - context: { value: 1 }, - runtime: runtime("preflight").runtime, - phases: [phase("preflight", 2)], - runWhenState: ["preflight"], - compatibilityWhenState: ["provider_selection"], - runSlice, - recordStateResult, - recordInvalidatedStateResult: invalidatedRecorder(), - }); - - expect(result.context).toEqual({ value: 2 }); - expect(runSlice).toHaveBeenCalledOnce(); - expect(recordStateResult).not.toHaveBeenCalled(); - }); - - it("applies recomputed results in exact phase order while they match durable state", async () => { - const liveRuntime = runtime("preflight"); - const runSlice = vi.fn(async ({ context }) => ({ context, session: createSession() })); - const results = [ - advanceTo("gateway"), - advanceTo("provider_selection", { metadata: { state: "gateway" } }), - advanceTo("inference", { metadata: { state: "provider_selection" } }), - ]; - const recordStateResult = vi.fn(async (result: OnboardStateResult) => - liveRuntime.applyResult(result), - ); - const recordInvalidatedStateResult = invalidatedRecorder(); - const wrappedStates: string[] = []; - - const result = await runLiveOnboardFlowSlice({ - context: { value: 1 }, - runtime: liveRuntime.runtime, - phases: [ - phase("preflight", 2, results[0]), - { - state: "gateway", - run: vi.fn((context) => ({ - context: { value: context.value + 1 }, - result: [results[1], results[2]], - })), - }, - ], - runWhenState: ["preflight"], - compatibilityWhenState: ["preflight"], - phaseProgress: { - wrap: (candidate) => { - wrappedStates.push(candidate.state); - return candidate; - }, - }, - runSlice, - recordStateResult, - recordInvalidatedStateResult, - }); - - expect(result.context).toEqual({ value: 3 }); - expect(result.session.machine.state).toBe("inference"); - expect(runSlice).not.toHaveBeenCalled(); - expect(wrappedStates).toEqual(["preflight", "gateway"]); - expect(recordStateResult.mock.calls.map(([result]) => result)).toEqual(results); - expect(recordInvalidatedStateResult).not.toHaveBeenCalled(); - }); - - it("keeps resume-at-entry flows on compatibility execution", async () => { - const liveRuntime = runtime("preflight"); - const runSlice = vi.fn(async ({ context }) => ({ context, session: createSession() })); - const recordStateResult = vi.fn(async (result: OnboardStateResult) => - liveRuntime.applyResult(result), - ); - - await runLiveOnboardFlowSlice({ - context: { value: 1 }, - runtime: liveRuntime.runtime, - phases: [phase("preflight", 2)], - runWhenState: ["preflight"], - compatibilityWhenState: ["preflight"], - runSlice, - recordStateResult, - recordInvalidatedStateResult: invalidatedRecorder(), - }); - - expect(runSlice).not.toHaveBeenCalled(); - expect(recordStateResult).toHaveBeenCalledOnce(); - }); - - it("keeps non-resume ahead-state flows on compatibility execution", async () => { - const liveRuntime = runtime("provider_selection"); - const runSlice = vi.fn(async ({ context }) => ({ context, session: createSession() })); - const recordStateResult = vi.fn(async (result: OnboardStateResult) => - liveRuntime.applyResult(result), - ); - const recordInvalidatedStateResult = invalidatedRecorder(); - // Preflight replays advanceTo('gateway', { state: 'preflight' }) while the - // durable machine already stands at 'provider_selection'. The compatibility - // phase body must still execute (backstop side effects) and the recomputed - // transition must be routed through invalidation, not applied. Assert both: - // stalePhase.run fired once, and invalidation carries the correct source / - // current state ownership for #6227. - const stalePhase = phase("preflight", 2); - - await runLiveOnboardFlowSlice({ - context: { value: 1 }, - runtime: liveRuntime.runtime, - phases: [stalePhase], - runWhenState: ["preflight"], - compatibilityWhenState: ["provider_selection"], - runSlice, - recordStateResult, - recordInvalidatedStateResult, - }); - - expect(runSlice).not.toHaveBeenCalled(); - expect(stalePhase.run).toHaveBeenCalledOnce(); - expect(recordStateResult).not.toHaveBeenCalled(); - expect(recordInvalidatedStateResult).toHaveBeenCalledOnce(); - expect(recordInvalidatedStateResult).toHaveBeenCalledWith( - expect.objectContaining({ next: "gateway" }), - { - reason: "source_state_mismatch", - currentState: "provider_selection", - sourceState: "preflight", - }, - ); - }); - - it("keeps compatibility phases visible through the default heartbeat reporter", async () => { - vi.useFakeTimers(); - vi.setSystemTime(0); - const log = vi.spyOn(console, "log").mockImplementation(() => {}); - let markPhaseStarted!: () => void; - let releasePhase!: () => void; - const phaseStarted = new Promise((resolve) => { - markPhaseStarted = resolve; - }); - const phaseReleased = new Promise((resolve) => { - releasePhase = resolve; - }); - const liveRuntime = runtime("provider_selection"); - const pendingGateway: OnboardSequencePhase = { - state: "gateway", - async run(context) { - markPhaseStarted(); - await phaseReleased; - return { context, result: advanceTo("inference") }; - }, - }; - - const running = runLiveOnboardFlowSlice({ - context: { value: 1 }, - runtime: liveRuntime.runtime, - phases: [pendingGateway], - runWhenState: ["gateway"], - compatibilityWhenState: ["provider_selection"], - runSlice: vi.fn(), - recordStateResult: (result) => liveRuntime.applyResult(result), - recordInvalidatedStateResult: invalidatedRecorder(), - }); - await phaseStarted; - try { - await vi.advanceTimersByTimeAsync(30_000); - expect(log).toHaveBeenCalledWith(" ⏳ Still working on Gateway startup… (30s elapsed)"); - } finally { - releasePhase(); - await running; - } - expect(vi.getTimerCount()).toBe(0); - }); - - it("rejects non-resume states before the slice entry before running side effects", async () => { - const liveRuntime = runtime("init"); - const blocked = phase("provider_selection", 2); - const runSlice = vi.fn(async ({ context }) => ({ context, session: createSession() })); - const recordStateResult = vi.fn(async () => undefined); - - await expect( - runLiveOnboardFlowSlice({ - context: { value: 1 }, - runtime: liveRuntime.runtime, - phases: [blocked], - runWhenState: ["provider_selection"], - compatibilityWhenState: ["inference", "sandbox"], - runSlice, - recordStateResult, - recordInvalidatedStateResult: invalidatedRecorder(), - }), - ).rejects.toBeInstanceOf(UnexpectedLiveOnboardFlowSliceStateError); - - expect(runSlice).not.toHaveBeenCalled(); - expect(blocked.run).not.toHaveBeenCalled(); - expect(recordStateResult).not.toHaveBeenCalled(); - }); - - it("rejects undeclared resume states before running side effects", async () => { - const liveRuntime = runtime("provider_selection"); - const blocked = phase("preflight", 2); - const runSlice = vi.fn(async ({ context }) => ({ context, session: createSession() })); - const recordStateResult = vi.fn(async () => undefined); - - await expect( - runLiveOnboardFlowSlice({ - context: { value: 1 }, - runtime: liveRuntime.runtime, - phases: [blocked], - - runWhenState: ["preflight"], - compatibilityWhenState: ["sandbox"], - runSlice, - recordStateResult, - recordInvalidatedStateResult: invalidatedRecorder(), - }), - ).rejects.toBeInstanceOf(UnexpectedLiveOnboardFlowSliceStateError); - - expect(runSlice).not.toHaveBeenCalled(); - expect(blocked.run).not.toHaveBeenCalled(); - expect(recordStateResult).not.toHaveBeenCalled(); - }); - - it("rejects duplicate compatibility phases before running side effects", async () => { - const liveRuntime = runtime("provider_selection"); - const first = phase("preflight", 2); - const second = phase("preflight", 3); - const recordStateResult = vi.fn(async () => undefined); - - await expect( - runLiveOnboardFlowSlice({ - context: { value: 1 }, - runtime: liveRuntime.runtime, - phases: [first, second], - runWhenState: ["preflight"], - compatibilityWhenState: ["provider_selection"], - runSlice: vi.fn(), - recordStateResult, - recordInvalidatedStateResult: invalidatedRecorder(), - }), - ).rejects.toBeInstanceOf(DuplicateOnboardSequencePhaseError); - - expect(first.run).not.toHaveBeenCalled(); - expect(second.run).not.toHaveBeenCalled(); - expect(recordStateResult).not.toHaveBeenCalled(); - }); - - it("rejects empty compatibility phase results", async () => { - const liveRuntime = runtime("provider_selection"); - const recordStateResult = vi.fn(async () => undefined); - - await expect( - runLiveOnboardFlowSlice({ - context: { value: 1 }, - runtime: liveRuntime.runtime, - phases: [phase("preflight", 2, [])], - runWhenState: ["preflight"], - compatibilityWhenState: ["provider_selection"], - runSlice: vi.fn(), - recordStateResult, - recordInvalidatedStateResult: invalidatedRecorder(), - }), - ).rejects.toBeInstanceOf(EmptyLiveOnboardFlowSliceResultError); - - expect(recordStateResult).not.toHaveBeenCalled(); - }); - - it("invalidates a recomputed transition when the machine already stands at its target (#6227)", async () => { - const liveRuntime = runtime("gateway"); - const recordStateResult = vi.fn(async (result: OnboardStateResult) => - liveRuntime.applyResult(result), - ); - const recordInvalidatedStateResult = invalidatedRecorder(); - // Recompute of the preflight phase in ahead-state resume: machine is - // already at 'gateway' when preflight replays advanceTo('gateway', - // { state: 'preflight' }). Recording must route through the invalidated - // path with reason 'already_at_target', not the standard apply path. - await runLiveOnboardFlowSlice({ - context: { value: 1 }, - runtime: liveRuntime.runtime, - phases: [phase("preflight", 2)], - runWhenState: ["preflight"], - compatibilityWhenState: ["gateway"], - runSlice: vi.fn(), - recordStateResult, - recordInvalidatedStateResult, - }); - - expect(recordStateResult).not.toHaveBeenCalled(); - expect(recordInvalidatedStateResult).toHaveBeenCalledOnce(); - expect(recordInvalidatedStateResult).toHaveBeenCalledWith( - expect.objectContaining({ next: "gateway" }), - { - reason: "already_at_target", - currentState: "gateway", - sourceState: "preflight", - }, - ); - }); - - it("propagates recomputed result application failures without running later phases", async () => { - const liveRuntime = runtime("preflight"); - const later = phase("gateway", 3); - const recordStateResult = vi.fn(async () => { - throw new Error("compatibility failed"); - }); - - await expect( - runLiveOnboardFlowSlice({ - context: { value: 1 }, - runtime: liveRuntime.runtime, - phases: [phase("preflight", 2), later], - runWhenState: ["preflight"], - compatibilityWhenState: ["preflight"], - runSlice: vi.fn(), - recordStateResult, - recordInvalidatedStateResult: invalidatedRecorder(), - }), - ).rejects.toThrow("compatibility failed"); - - expect(recordStateResult).toHaveBeenCalledOnce(); - expect(later.run).not.toHaveBeenCalled(); - }); -}); diff --git a/src/lib/onboard/machine/live-flow-slice.ts b/src/lib/onboard/machine/live-flow-slice.ts deleted file mode 100644 index 3c6c6134e16..00000000000 --- a/src/lib/onboard/machine/live-flow-slice.ts +++ /dev/null @@ -1,166 +0,0 @@ -// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -import { createPhaseProgressReporter, type PhaseProgressReporter } from "./phase-progress"; -import type { OnboardStateResult } from "./result"; -import type { - OnboardMachineRunnerResult, - OnboardMachineRunnerRuntime, - OnboardStateHandlerResult, -} from "./runner"; -import { DuplicateOnboardSequencePhaseError, type OnboardSequencePhase } from "./sequence-runner"; -import type { OnboardMachineState } from "./types"; - -export type InvalidatedOnboardStateResultRecorder = ( - result: OnboardStateResult, - options: { - reason: "already_at_target" | "source_state_mismatch"; - currentState: OnboardMachineState; - sourceState?: string | null; - }, -) => Promise; - -export interface LiveOnboardFlowSliceOptions { - context: Context; - runtime: OnboardMachineRunnerRuntime; - phases: readonly OnboardSequencePhase[]; - runWhenState: readonly OnboardMachineState[]; - compatibilityWhenState?: readonly OnboardMachineState[]; - phaseProgress?: PhaseProgressReporter; - runSlice(options: { - context: Context; - runtime: OnboardMachineRunnerRuntime; - phases: readonly OnboardSequencePhase[]; - }): Promise>; - recordStateResult(result: OnboardStateResult): Promise; - recordInvalidatedStateResult: InvalidatedOnboardStateResultRecorder; -} - -export class EmptyLiveOnboardFlowSliceResultError extends Error { - constructor(readonly state: OnboardSequencePhase["state"]) { - super(`Onboarding live flow phase '${state}' returned no results`); - this.name = "EmptyLiveOnboardFlowSliceResultError"; - } -} - -export class UnexpectedLiveOnboardFlowSliceStateError extends Error { - constructor( - readonly state: OnboardMachineState, - readonly runWhenState: readonly OnboardMachineState[], - readonly compatibilityWhenState: readonly OnboardMachineState[], - ) { - super(`Unexpected onboarding live flow state before slice entry: ${state}`); - this.name = "UnexpectedLiveOnboardFlowSliceStateError"; - } -} - -function assertUniquePhases(phases: readonly OnboardSequencePhase[]): void { - const states = new Set["state"]>(); - for (const phase of phases) { - if (states.has(phase.state)) throw new DuplicateOnboardSequencePhaseError(phase.state); - states.add(phase.state); - } -} - -function asResultArray( - result: OnboardStateHandlerResult, - state: OnboardSequencePhase["state"], -): readonly OnboardStateResult[] { - const results = Array.isArray(result) - ? (result as readonly OnboardStateResult[]) - : [result as OnboardStateResult]; - if (results.length === 0) throw new EmptyLiveOnboardFlowSliceResultError(state); - return results; -} - -function resultSourceState(result: OnboardStateResult): string | null { - const source = result.metadata?.state; - return typeof source === "string" ? source : null; -} - -async function recordRecomputedResult( - options: Pick< - LiveOnboardFlowSliceOptions, - "runtime" | "recordStateResult" | "recordInvalidatedStateResult" - > & { phaseState: OnboardSequencePhase["state"]; result: OnboardStateResult }, -): Promise { - if (options.result.type !== "transition") { - await options.recordStateResult(options.result); - return; - } - - const current = await options.runtime.session(); - const sourceState = resultSourceState(options.result) ?? options.phaseState; - if (current.machine.state === options.result.next) { - await options.recordInvalidatedStateResult(options.result, { - reason: "already_at_target", - currentState: current.machine.state, - sourceState, - }); - return; - } - if (sourceState && current.machine.state !== sourceState) { - await options.recordInvalidatedStateResult(options.result, { - reason: "source_state_mismatch", - currentState: current.machine.state, - sourceState, - }); - return; - } - await options.recordStateResult(options.result); -} - -/** - * Run a live onboard flow slice through the strict runner when the current - * machine state is exactly at the slice entry point. Declared compatibility - * states use the recompute path so repair/backstop phase bodies still execute - * during resume or when a saved session has already advanced beyond the slice. - * Recomputed results are applied only when they still match the durable machine - * state; stale transition results are explicitly invalidated with source/target - * diagnostics. Compatibility is limited to caller-declared states so earlier or - * unexpected machine states fail before running slice side effects out of order. - */ -export async function runLiveOnboardFlowSlice({ - context, - runtime, - phases, - runWhenState, - compatibilityWhenState = [], - phaseProgress = createPhaseProgressReporter(), - runSlice, - recordStateResult, - recordInvalidatedStateResult, -}: LiveOnboardFlowSliceOptions): Promise> { - const current = await runtime.session(); - if ( - runWhenState.includes(current.machine.state) && - !compatibilityWhenState.includes(current.machine.state) - ) { - return runSlice({ context, runtime, phases }); - } - if (!compatibilityWhenState.includes(current.machine.state)) { - throw new UnexpectedLiveOnboardFlowSliceStateError( - current.machine.state, - runWhenState, - compatibilityWhenState, - ); - } - - assertUniquePhases(phases); - let nextContext = context; - for (const rawPhase of phases) { - const phase = phaseProgress.wrap(rawPhase); - const phaseResult = await phase.run(nextContext); - for (const result of asResultArray(phaseResult.result, phase.state)) { - await recordRecomputedResult({ - runtime, - recordStateResult, - recordInvalidatedStateResult, - phaseState: phase.state, - result, - }); - } - nextContext = phaseResult.context; - } - return { context: nextContext, session: await runtime.session() }; -} diff --git a/src/lib/onboard/machine/runtime.test.ts b/src/lib/onboard/machine/runtime.test.ts index 332ddebe356..fd82a2e5bc7 100644 --- a/src/lib/onboard/machine/runtime.test.ts +++ b/src/lib/onboard/machine/runtime.test.ts @@ -11,7 +11,6 @@ import { type SessionUpdates, sanitizeFailure, } from "../../state/onboard-session"; -import type { StepMutationOptions } from "../../state/onboard-step-mutation"; import type { OnboardMachineEvent } from "./events"; import { advanceTo, @@ -31,7 +30,7 @@ function cloneSession(session: Session): Session { function createHarness(initialSession: Session | null = createSession()) { let session = initialSession ? cloneSession(initialSession) : null; const events: OnboardMachineEvent[] = []; - const stepOptionCalls: Array<{ method: string; options: StepMutationOptions | undefined }> = []; + const stepCalls: string[] = []; let tick = 0; const updateSession = (mutator: (value: Session) => Session | void): Session => { const current = session ? cloneSession(session) : createSession(); @@ -47,8 +46,8 @@ function createHarness(initialSession: Session | null = createSession()) { return cloneSession(session); }, updateSession, - markStepStarted: (stepName, options) => { - stepOptionCalls.push({ method: "markStepStarted", options }); + markStepStarted: (stepName) => { + stepCalls.push("markStepStarted"); return updateSession((current) => { const step = current.steps[stepName]; if (!step) return current; @@ -58,8 +57,8 @@ function createHarness(initialSession: Session | null = createSession()) { return current; }); }, - markStepComplete: (stepName, updates: SessionUpdates = {}, options) => { - stepOptionCalls.push({ method: "markStepComplete", options }); + markStepComplete: (stepName, updates: SessionUpdates = {}) => { + stepCalls.push("markStepComplete"); return updateSession((current) => { const step = current.steps[stepName]; if (!step) return current; @@ -85,8 +84,8 @@ function createHarness(initialSession: Session | null = createSession()) { step.status = "skipped"; return current; }), - markStepFailed: (stepName, message, options) => { - stepOptionCalls.push({ method: "markStepFailed", options }); + markStepFailed: (stepName, message) => { + stepCalls.push("markStepFailed"); return updateSession((current) => { const step = current.steps[stepName]; if (!step) return current; @@ -118,7 +117,7 @@ function createHarness(initialSession: Session | null = createSession()) { return { runtime: new OnboardRuntime(deps), events, - stepOptionCalls, + stepCalls, getSession: () => { if (!session) throw new Error("Expected runtime session"); return cloneSession(session); @@ -150,36 +149,18 @@ describe("OnboardRuntime", () => { expect(events[1]).toMatchObject({ type: "onboard.resumed", state: "init" }); }); - it("defaults step recording dependencies to record-only machine mutations", async () => { - const { runtime, stepOptionCalls } = createHarness(); + it("uses status-only step dependencies", async () => { + const { runtime, getSession, stepCalls } = createHarness(); await runtime.markStepStarted("preflight"); await runtime.markStepComplete("preflight", { sandboxName: "my-assistant" }); await runtime.markStepFailed("gateway", "boom"); - expect(stepOptionCalls).toEqual([ - { method: "markStepStarted", options: { updateMachine: false } }, - { method: "markStepComplete", options: { updateMachine: false } }, - { method: "markStepFailed", options: { updateMachine: false } }, - ]); - }); - - it("forwards step mutation options to step recording dependencies", async () => { - const { runtime, getSession, stepOptionCalls } = createHarness(); - const recordOnlyOptions = { updateMachine: false }; - - await runtime.markStepStarted("preflight", recordOnlyOptions); - await runtime.markStepComplete("preflight", { sandboxName: "my-assistant" }, recordOnlyOptions); - await runtime.markStepFailed("gateway", "boom", recordOnlyOptions); - - expect(stepOptionCalls).toEqual([ - { method: "markStepStarted", options: recordOnlyOptions }, - { method: "markStepComplete", options: recordOnlyOptions }, - { method: "markStepFailed", options: recordOnlyOptions }, - ]); + expect(stepCalls).toEqual(["markStepStarted", "markStepComplete", "markStepFailed"]); expect(getSession()).toMatchObject({ sandboxName: "my-assistant", status: "failed", + machine: { state: "init", revision: 0 }, steps: { preflight: { status: "complete" }, gateway: { status: "failed" }, @@ -208,6 +189,17 @@ describe("OnboardRuntime", () => { expect(getSession().machine.state).toBe("preflight"); }); + it("rejects a stale result source before changing the machine", async () => { + const { runtime, events, getSession } = createHarness(); + + await expect( + runtime.applyResult(advanceTo("preflight", { metadata: { state: "gateway" } })), + ).rejects.toThrow("Onboarding state result source mismatch: gateway != init"); + + expect(getSession().machine).toMatchObject({ state: "init", revision: 0 }); + expect(events).toHaveLength(0); + }); + it("applies only safe context updates and emits redacted context events", async () => { const { runtime, events, getSession } = createHarness(); diff --git a/src/lib/onboard/machine/runtime.ts b/src/lib/onboard/machine/runtime.ts index e8678d66ca0..0984d0a2b78 100644 --- a/src/lib/onboard/machine/runtime.ts +++ b/src/lib/onboard/machine/runtime.ts @@ -4,10 +4,6 @@ import type { JsonObject } from "../../core/json-types"; import type { CompleteSessionOptions, Session, SessionUpdates } from "../../state/onboard-session"; import * as onboardSession from "../../state/onboard-session"; -import { - RECORD_ONLY_STEP_MUTATION_OPTIONS, - type StepMutationOptions, -} from "../../state/onboard-step-mutation"; import type { ResumeConfigConflict } from "../resume-config"; import { createOnboardMachineEvent, @@ -33,15 +29,11 @@ export interface OnboardRuntimeDeps { createSession(overrides?: Partial): Session; saveSession(session: Session): Session; updateSession(mutator: (session: Session) => Session | void): Session; - markStepStarted(stepName: string, options?: StepMutationOptions): Session; - markStepComplete( - stepName: string, - updates?: SessionUpdates, - options?: StepMutationOptions, - ): Session; + markStepStarted(stepName: string): Session; + markStepComplete(stepName: string, updates?: SessionUpdates): Session; markStepCompleteRecordOnly(stepName: string, updates?: SessionUpdates): Session; markStepSkipped(stepName: string): Session; - markStepFailed(stepName: string, message?: string | null, options?: StepMutationOptions): Session; + markStepFailed(stepName: string, message?: string | null): Session; markStepFailedRecordOnly(stepName: string, message?: string | null): Session; completeSession(updates?: SessionUpdates, options?: CompleteSessionOptions): Session; filterSafeUpdates(updates: SessionUpdates): Partial; @@ -159,38 +151,39 @@ export class OnboardRuntime { return session; } - async markStepStarted( - stepName: string, - options: StepMutationOptions = RECORD_ONLY_STEP_MUTATION_OPTIONS, - ): Promise { - return this.deps.markStepStarted(stepName, options); + async markStepStarted(stepName: string): Promise { + return this.deps.markStepStarted(stepName); } - async markStepComplete( - stepName: string, - updates: SessionUpdates = {}, - options: StepMutationOptions = RECORD_ONLY_STEP_MUTATION_OPTIONS, - ): Promise { - return this.deps.markStepComplete(stepName, updates, options); + async markStepComplete(stepName: string, updates: SessionUpdates = {}): Promise { + const safeUpdates = this.deps.filterSafeUpdates(updates); + const fields = Object.keys(safeUpdates); + const updated = this.deps.markStepComplete(stepName, { + ...(safeUpdates as SessionUpdates), + stationExpressModelIdentity: updates.stationExpressModelIdentity, + }); + if (fields.length > 0) { + this.emit("context.updated", updated, { + step: stepName, + metadata: { fields }, + }); + } + return updated; } async markStepCompleteRecordOnly( stepName: string, updates: SessionUpdates = {}, ): Promise { - return this.deps.markStepCompleteRecordOnly(stepName, updates); + return this.markStepComplete(stepName, updates); } async markStepSkipped(stepName: string): Promise { return this.deps.markStepSkipped(stepName); } - async markStepFailed( - stepName: string, - message: string | null = null, - options: StepMutationOptions = RECORD_ONLY_STEP_MUTATION_OPTIONS, - ): Promise { - return this.deps.markStepFailed(stepName, message, options); + async markStepFailed(stepName: string, message: string | null = null): Promise { + return this.deps.markStepFailed(stepName, message); } async markStepFailedRecordOnly( @@ -289,7 +282,44 @@ export class OnboardRuntime { return updated; } + async assertResultWillApply(result: OnboardStateResult): Promise { + const current = this.ensureSession(); + if (result.type === "failed") { + assertValidOnboardMachineTransition(current.machine.state, "failed"); + return; + } + if (result.type === "complete") { + assertValidOnboardMachineTransition(current.machine.state, "complete"); + return; + } + const sourceState = + result.metadata && typeof result.metadata.state === "string" ? result.metadata.state : null; + if (result.type === "pause") { + if (sourceState && current.machine.state !== sourceState) { + throw new Error( + `Onboarding state result source mismatch: ${sourceState} != ${current.machine.state}`, + ); + } + return; + } + if (current.machine.state === result.next) { + throw new Error(`Onboarding state result already reached target state: ${result.next}`); + } + if (sourceState && current.machine.state !== sourceState) { + throw new Error( + `Onboarding state result source mismatch: ${sourceState} != ${current.machine.state}`, + ); + } + const transition = assertValidOnboardMachineTransition(current.machine.state, result.next); + if (result.transitionKind && transition.kind !== result.transitionKind) { + throw new Error( + `Invalid onboarding machine transition kind: ${current.machine.state} -> ${result.next} expected ${result.transitionKind}, got ${transition.kind}`, + ); + } + } + async applyResult(result: OnboardStateResult): Promise { + await this.assertResultWillApply(result); if (result.type === "pause") { const current = this.ensureSession(); if (isTerminalOnboardMachineState(current.machine.state)) { diff --git a/src/lib/onboard/resume-machine-repair.test.ts b/src/lib/onboard/resume-machine-repair.test.ts index f2741c40d6c..e889544af83 100644 --- a/src/lib/onboard/resume-machine-repair.test.ts +++ b/src/lib/onboard/resume-machine-repair.test.ts @@ -89,7 +89,6 @@ function createBoundaryHarness(initial: Session) { toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, maybeForceE2eStepFailure: () => undefined, createRuntime: () => new OnboardRuntime(deps), - stepMutationOptions: { updateMachine: false }, }); return { boundary, getSession: () => cloneSession(session) }; } diff --git a/src/lib/onboard/runtime-boundary-record-only.test.ts b/src/lib/onboard/runtime-boundary-record-only.test.ts index 340ca7e1341..097831168b8 100644 --- a/src/lib/onboard/runtime-boundary-record-only.test.ts +++ b/src/lib/onboard/runtime-boundary-record-only.test.ts @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -import { describe, expect, it, vi } from "vitest"; +import { describe, expect, it } from "vitest"; import { createSession, @@ -110,41 +110,33 @@ describe("OnboardRuntimeBoundary record-only step/result pairing", () => { expect(events.map((event) => event.type)).toEqual([ "state.exited", "state.entered", + "context.updated", "state.exited", "state.entered", ]); }); - it("applies validated step completion results directly without compatibility diagnostics", async () => { + it("applies validated step completion results directly", async () => { const { boundary, events } = createRuntimeHarness(); - const compatibilitySpy = vi - .spyOn(boundary, "recordStateResultWithStepCompatibility") - .mockRejectedValue(new Error("compatibility bridge should not be used")); - - try { - await boundary.recordStateResult(advanceTo("preflight")); - const completed = await boundary.recordStepCompleteWithStateResult( - "preflight", - { sandboxName: "strict-path-sb" }, - advanceTo("gateway", { metadata: { state: "preflight" } }), - ); - - expect(completed).toMatchObject({ - sandboxName: "strict-path-sb", - machine: { state: "gateway", revision: 2 }, - steps: { preflight: { status: "complete" } }, - }); - expect(compatibilitySpy).not.toHaveBeenCalled(); - expect(events.map((event) => event.type)).toEqual([ - "state.exited", - "state.entered", - "state.exited", - "state.entered", - ]); - expect(events.some((event) => event.type === "state.result.skipped")).toBe(false); - } finally { - compatibilitySpy.mockRestore(); - } + await boundary.recordStateResult(advanceTo("preflight")); + const completed = await boundary.recordStepCompleteWithStateResult( + "preflight", + { sandboxName: "strict-path-sb" }, + advanceTo("gateway", { metadata: { state: "preflight" } }), + ); + + expect(completed).toMatchObject({ + sandboxName: "strict-path-sb", + machine: { state: "gateway", revision: 2 }, + steps: { preflight: { status: "complete" } }, + }); + expect(events.map((event) => event.type)).toEqual([ + "state.exited", + "state.entered", + "context.updated", + "state.exited", + "state.entered", + ]); }); it("pairs record-only step failure with an explicit failure result", async () => { @@ -188,22 +180,14 @@ describe("OnboardRuntimeBoundary record-only step/result pairing", () => { it("rejects stale state results when record-only steps did not advance the machine", async () => { const { boundary, events } = createRuntimeHarness(); - await boundary.recordStateResultWithStepCompatibility( - advanceTo("preflight", { metadata: { state: "init" } }), - ); + await boundary.recordStateResult(advanceTo("preflight", { metadata: { state: "init" } })); await expect( - boundary.recordStateResultWithStepCompatibility( - advanceTo("preflight", { metadata: { state: "init" } }), - ), - ).rejects.toThrow("Record-only step result already reached target state: preflight"); + boundary.recordStateResult(advanceTo("preflight", { metadata: { state: "init" } })), + ).rejects.toThrow("Onboarding state result already reached target state: preflight"); await expect( - boundary.recordStateResultWithStepCompatibility( - advanceTo("gateway", { metadata: { state: "init" } }), - ), - ).rejects.toThrow("Record-only step result source mismatch: init != preflight"); - await boundary.recordStateResultWithStepCompatibility( - advanceTo("gateway", { metadata: { state: "preflight" } }), - ); + boundary.recordStateResult(advanceTo("gateway", { metadata: { state: "init" } })), + ).rejects.toThrow("Onboarding state result source mismatch: init != preflight"); + await boundary.recordStateResult(advanceTo("gateway", { metadata: { state: "preflight" } })); expect(events.map((event) => event.type)).toEqual([ "state.exited", @@ -219,8 +203,8 @@ describe("OnboardRuntimeBoundary record-only step/result pairing", () => { const { boundary } = createRuntimeHarness(); const result = advanceTo("preflight", { metadata: { state: "missing" } }); - await expect(boundary.recordStateResultWithStepCompatibility(result)).rejects.toThrow( - "Record-only step result source mismatch: missing != init", + await expect(boundary.recordStateResult(result)).rejects.toThrow( + "Onboarding state result source mismatch: missing != init", ); }); }); diff --git a/src/lib/onboard/runtime-boundary.test.ts b/src/lib/onboard/runtime-boundary.test.ts index 34f6598e34b..6bdffebbb82 100644 --- a/src/lib/onboard/runtime-boundary.test.ts +++ b/src/lib/onboard/runtime-boundary.test.ts @@ -10,7 +10,6 @@ import { type Session, type SessionUpdates, } from "../state/onboard-session"; -import type { StepMutationOptions } from "../state/onboard-step-mutation"; import type { OnboardMachineEvent } from "./machine/events"; import { advanceTo, @@ -21,7 +20,6 @@ import { retryTo, } from "./machine/result"; import { OnboardRuntime, type OnboardRuntimeDeps } from "./machine/runtime"; -import type { OnboardMachineState } from "./machine/types"; import { OnboardRuntimeBoundary } from "./runtime-boundary"; import { applySessionRecovery } from "./session-recovery"; @@ -29,60 +27,10 @@ function cloneSession(session: Session): Session { return normalizeSession(JSON.parse(JSON.stringify(session))) ?? session; } -const STEP_TO_STATE: Record = { - preflight: "preflight", - gateway: "gateway", - provider_selection: "provider_selection", - inference: "inference", - sandbox: "sandbox", - openclaw: "openclaw", - agent_setup: "agent_setup", - policies: "policies", -}; - -function nextStateAfterCompletedStep( - stepName: string, - session: Pick, -): OnboardMachineState | null { - switch (stepName) { - case "preflight": - return "gateway"; - case "gateway": - return "provider_selection"; - case "provider_selection": - return "inference"; - case "inference": - return "sandbox"; - case "sandbox": - return session.agent ? "agent_setup" : "openclaw"; - case "openclaw": - case "agent_setup": - return "policies"; - case "policies": - return "finalizing"; - default: - return null; - } -} - -function transitionMachine(session: Session, state: OnboardMachineState): void { - session.machine = { - version: session.machine.version, - state, - stateEnteredAt: "2026-05-27T00:00:00.000Z", - revision: - session.machine.state === state ? session.machine.revision : session.machine.revision + 1, - }; -} - -function shouldUpdateMachine(options: StepMutationOptions | undefined): boolean { - return options?.updateMachine !== false; -} - function createRuntimeHarness(overrides: Partial = {}) { let session: Session | null = createSession(overrides); const events: OnboardMachineEvent[] = []; - const stepOptionCalls: Array<{ method: string; options: StepMutationOptions | undefined }> = []; + const stepCalls: string[] = []; const updateSession = (mutator: (value: Session) => Session | void): Session => { const current = session ? cloneSession(session) : createSession(); session = cloneSession(mutator(current) ?? current); @@ -96,8 +44,8 @@ function createRuntimeHarness(overrides: Partial = {}) { return cloneSession(session); }, updateSession, - markStepStarted: (stepName, options) => { - stepOptionCalls.push({ method: "markStepStarted", options }); + markStepStarted: (stepName) => { + stepCalls.push("markStepStarted"); return updateSession((current) => { const step = current.steps[stepName]; if (!step) return current; @@ -108,13 +56,11 @@ function createRuntimeHarness(overrides: Partial = {}) { current.lastStepStarted = stepName; current.status = "in_progress"; current.failure = null; - const state = STEP_TO_STATE[stepName]; - if (state && shouldUpdateMachine(options)) transitionMachine(current, state); return current; }); }, - markStepComplete: (stepName, updates: SessionUpdates = {}, options) => { - stepOptionCalls.push({ method: "markStepComplete", options }); + markStepComplete: (stepName, updates: SessionUpdates = {}) => { + stepCalls.push("markStepComplete"); return updateSession((current) => { const step = current.steps[stepName]; if (!step) return current; @@ -124,8 +70,6 @@ function createRuntimeHarness(overrides: Partial = {}) { current.lastCompletedStep = stepName; current.failure = null; Object.assign(current, filterSafeUpdates(updates)); - const nextState = nextStateAfterCompletedStep(stepName, current); - if (nextState && shouldUpdateMachine(options)) transitionMachine(current, nextState); return current; }); }, @@ -135,13 +79,11 @@ function createRuntimeHarness(overrides: Partial = {}) { current.steps[stepName].status = "skipped"; return current; }), - markStepFailed: (stepName, message, options) => { - stepOptionCalls.push({ method: "markStepFailed", options }); + markStepFailed: (stepName, message) => { + stepCalls.push("markStepFailed"); return updateSession((current) => { current.steps[stepName].status = "failed"; - current.failure = shouldUpdateMachine(options) - ? { step: stepName, message: message ?? null, recordedAt: "now" } - : current.failure; + current.steps[stepName].error = message ?? null; return current; }); }, @@ -159,7 +101,7 @@ function createRuntimeHarness(overrides: Partial = {}) { return { createRuntime: () => new OnboardRuntime(deps), events, - stepOptionCalls, + stepCalls, getSession: () => cloneSession(session ?? createSession()), }; } @@ -234,7 +176,7 @@ describe("OnboardRuntimeBoundary", () => { ); }); - it("defaults boundary step recorders to record-only machine mutations", async () => { + it("keeps step status writes separate from runtime transitions", async () => { const harness = createRuntimeHarness(); const boundary = new OnboardRuntimeBoundary({ toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, @@ -246,113 +188,28 @@ describe("OnboardRuntimeBoundary", () => { await boundary.recordStepComplete("preflight"); await boundary.recordStepFailed("gateway", "boom"); - expect(harness.stepOptionCalls).toEqual([ - { method: "markStepStarted", options: { updateMachine: false } }, - { method: "markStepComplete", options: { updateMachine: false } }, - { method: "markStepFailed", options: { updateMachine: false } }, - ]); - }); - - it("keeps default boundary step recorders from advancing the machine", async () => { - const harness = createRuntimeHarness(); - const boundary = new OnboardRuntimeBoundary({ - toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, - maybeForceE2eStepFailure: () => undefined, - createRuntime: harness.createRuntime, + expect(harness.stepCalls).toEqual(["markStepStarted", "markStepComplete", "markStepFailed"]); + expect(harness.getSession()).toMatchObject({ + machine: { state: "init", revision: 0 }, + steps: { preflight: { status: "complete" }, gateway: { status: "failed" } }, }); - - await boundary.startRecordedStep("preflight"); - expect(harness.getSession().machine.state).toBe("init"); - await boundary.recordStepComplete("preflight"); - expect(harness.getSession().machine.state).toBe("init"); - - await boundary.recordStateResultWithStepCompatibility( - advanceTo("preflight", { metadata: { state: "init" } }), - ); - expect(harness.getSession().machine.state).toBe("preflight"); }); - it("forwards configured step mutation options through boundary recorders", async () => { + it("applies each explicit transition exactly once", async () => { const harness = createRuntimeHarness(); - const legacyOptions = { updateMachine: true }; const boundary = new OnboardRuntimeBoundary({ toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, maybeForceE2eStepFailure: () => undefined, createRuntime: harness.createRuntime, - stepMutationOptions: legacyOptions, }); - await boundary.startRecordedStep("preflight"); - await boundary.recordStepComplete("preflight"); - await boundary.recordStepFailed("gateway", "boom"); + await boundary.recordStateResult(advanceTo("preflight", { metadata: { state: "init" } })); - expect(harness.stepOptionCalls).toEqual([ - { method: "markStepStarted", options: legacyOptions }, - { method: "markStepComplete", options: legacyOptions }, - { method: "markStepFailed", options: legacyOptions }, - ]); - expect(harness.getSession().machine.state).toBe("gateway"); + expect(harness.getSession().machine).toMatchObject({ state: "preflight", revision: 1 }); + expect(harness.events.map((event) => event.type)).toEqual(["state.exited", "state.entered"]); }); - it("emits diagnostics for legacy-compatible stale state results", async () => { - const harness = createRuntimeHarness(); - const boundary = new OnboardRuntimeBoundary({ - toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, - maybeForceE2eStepFailure: () => undefined, - createRuntime: harness.createRuntime, - stepMutationOptions: { updateMachine: true }, - }); - - await boundary.recordStateResultWithStepCompatibility( - advanceTo("gateway", { metadata: { state: "preflight" } }), - ); - - expect(harness.events).toHaveLength(1); - expect(harness.events[0]).toMatchObject({ - type: "state.result.skipped", - state: "init", - metadata: { - reason: "source_state_mismatch", - currentState: "init", - targetState: "gateway", - sourceState: "preflight", - }, - }); - }); - - it("emits diagnostics for explicit repaired resume invalidated results", async () => { - const harness = createRuntimeHarness(); - const boundary = new OnboardRuntimeBoundary({ - toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, - maybeForceE2eStepFailure: () => undefined, - createRuntime: harness.createRuntime, - }); - const result = advanceTo("gateway", { metadata: { state: "preflight" } }); - - await boundary.recordInvalidatedStateResult(result, { - reason: "source_state_mismatch", - currentState: "init", - sourceState: "preflight", - }); - - expect(harness.events[0]).toMatchObject({ - type: "state.result.invalidated", - metadata: { - reason: "source_state_mismatch", - currentState: "init", - sourceState: "preflight", - targetState: "gateway", - }, - }); - }); - - it.each([ - { label: "pause", result: () => pauseOnboardMachine() }, - { label: "complete", result: () => completeOnboardMachine() }, - { label: "failed", result: () => failOnboardMachine("boom") }, - ] as const)("rejects non-transition $label results before emitting invalidation (#6227)", async ({ - result, - }) => { + it("rejects a stale explicit transition before mutation", async () => { const harness = createRuntimeHarness(); const boundary = new OnboardRuntimeBoundary({ toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, @@ -361,223 +218,12 @@ describe("OnboardRuntimeBoundary", () => { }); await expect( - boundary.recordInvalidatedStateResult(result(), { - reason: "already_at_target", - currentState: "init", - sourceState: "init", - }), - ).rejects.toThrow(/Cannot invalidate non-transition/); - + boundary.recordStateResult(advanceTo("gateway", { metadata: { state: "preflight" } })), + ).rejects.toThrow("Onboarding state result source mismatch: preflight != init"); + expect(harness.getSession().machine).toMatchObject({ state: "init", revision: 0 }); expect(harness.events).toHaveLength(0); }); - it("emits diagnostics for explicit invalidated replay of stale default results", async () => { - const harness = createRuntimeHarness(); - const boundary = new OnboardRuntimeBoundary({ - toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, - maybeForceE2eStepFailure: () => undefined, - createRuntime: harness.createRuntime, - }); - - const result = advanceTo("preflight", { metadata: { state: "missing" } }); - await expect( - boundary.recordInvalidatedStateResult(result, { - reason: "source_state_mismatch", - currentState: "init", - sourceState: "missing", - }), - ).resolves.toMatchObject({ - machine: { state: "init" }, - }); - }); - - it("rejects skipped transition results that carry context updates", async () => { - const harness = createRuntimeHarness(); - const boundary = new OnboardRuntimeBoundary({ - toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, - maybeForceE2eStepFailure: () => undefined, - createRuntime: harness.createRuntime, - stepMutationOptions: { updateMachine: true }, - }); - - await expect( - boundary.recordStateResultWithStepCompatibility( - advanceTo("preflight", { metadata: { state: "missing" }, updates: { provider: "nvidia" } }), - ), - ).rejects.toThrow("Cannot skip onboarding state result with context updates"); - }); - - it("allows skipped transition results whose updates are all undefined", async () => { - const harness = createRuntimeHarness(); - const boundary = new OnboardRuntimeBoundary({ - toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, - maybeForceE2eStepFailure: () => undefined, - createRuntime: harness.createRuntime, - stepMutationOptions: { updateMachine: true }, - }); - - const session = await boundary.recordStateResultWithStepCompatibility( - advanceTo("preflight", { - metadata: { state: "missing" }, - updates: { provider: undefined, model: undefined }, - }), - ); - - expect(session.machine.state).toBe("init"); - expect(harness.events[0]).toMatchObject({ - type: "state.result.skipped", - state: "init", - metadata: { - reason: "source_state_mismatch", - currentState: "init", - targetState: "preflight", - sourceState: "missing", - }, - }); - }); - - it("records live legacy step/result compatibility through provider retry and finalization", async () => { - const harness = createRuntimeHarness(); - const boundary = new OnboardRuntimeBoundary({ - toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, - maybeForceE2eStepFailure: () => undefined, - createRuntime: harness.createRuntime, - stepMutationOptions: { updateMachine: true }, - }); - - await boundary.startRecordedStep("preflight"); - await boundary.recordStepComplete("preflight"); - await boundary.recordStateResultWithStepCompatibility( - advanceTo("gateway", { metadata: { state: "preflight" } }), - ); - - await boundary.startRecordedStep("gateway"); - await boundary.recordStepComplete("gateway"); - await boundary.recordStateResultWithStepCompatibility( - advanceTo("provider_selection", { metadata: { state: "gateway" } }), - ); - - await boundary.startRecordedStep("provider_selection"); - await boundary.recordStepComplete("provider_selection", { - provider: "bad", - model: "bad-model", - }); - await boundary.startRecordedStep("inference", { provider: "bad", model: "bad-model" }); - const retryResult = retryTo("provider_selection", { - metadata: { - state: "inference", - provider: "bad", - model: "bad-model", - reason: "selection_retry", - }, - }); - await boundary.startRecordedStep("provider_selection"); - await boundary.recordStepComplete("provider_selection", { - provider: "nvidia", - model: "nemotron", - }); - await boundary.startRecordedStep("inference", { provider: "nvidia", model: "nemotron" }); - await boundary.recordStepComplete("inference", { provider: "nvidia", model: "nemotron" }); - await boundary.recordStateResultWithStepCompatibility(retryResult); - await boundary.recordStateResultWithStepCompatibility( - advanceTo("sandbox", { - metadata: { state: "inference", provider: "nvidia", model: "nemotron" }, - }), - ); - - await boundary.startRecordedStep("sandbox"); - await boundary.recordStepComplete("sandbox", { sandboxName: "openclaw-sb" }); - await boundary.recordStateResultWithStepCompatibility( - branchTo("openclaw", { - metadata: { state: "sandbox", sandboxName: "openclaw-sb", agent: "openclaw" }, - }), - ); - - await boundary.startRecordedStep("openclaw"); - await boundary.recordStepComplete("openclaw"); - await boundary.recordStateResultWithStepCompatibility( - advanceTo("policies", { metadata: { state: "openclaw" } }), - ); - - await boundary.startRecordedStep("policies"); - await boundary.recordStepComplete("policies", { policyPresets: ["github"] }); - await boundary.recordStateResultWithStepCompatibility( - advanceTo("finalizing", { metadata: { state: "policies" } }), - ); - - await boundary.recordStateResultWithStepCompatibility( - advanceTo("post_verify", { metadata: { state: "finalizing" } }), - ); - const completed = await boundary.recordStateResultWithStepCompatibility( - completeOnboardMachine( - { sandboxName: "openclaw-sb", provider: "nvidia", model: "nemotron" }, - { state: "post_verify" }, - ), - ); - - const skipped = harness.events.filter((event) => event.type === "state.result.skipped"); - expect(skipped.map((event) => event.metadata.targetState)).toEqual([ - "gateway", - "provider_selection", - "provider_selection", - "sandbox", - "openclaw", - "policies", - "finalizing", - ]); - expect(skipped[2]).toMatchObject({ - state: "sandbox", - metadata: { - reason: "source_state_mismatch", - currentState: "sandbox", - sourceState: "inference", - targetState: "provider_selection", - }, - }); - expect(completed).toMatchObject({ - status: "complete", - machine: { state: "complete" }, - sandboxName: "openclaw-sb", - provider: "nvidia", - model: "nemotron", - }); - }); - - it("records sandbox branch compatibility for agent setup sandboxes", async () => { - const harness = createRuntimeHarness({ agent: "hermes" }); - const boundary = new OnboardRuntimeBoundary({ - toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, - maybeForceE2eStepFailure: () => undefined, - createRuntime: harness.createRuntime, - stepMutationOptions: { updateMachine: true }, - }); - - await boundary.recordStateResult(advanceTo("preflight")); - await boundary.recordStateResult(advanceTo("gateway")); - await boundary.recordStateResult(advanceTo("provider_selection")); - await boundary.recordStateResult(advanceTo("inference")); - await boundary.recordStateResult(advanceTo("sandbox")); - await boundary.startRecordedStep("sandbox"); - await boundary.recordStepComplete("sandbox", { sandboxName: "hermes-sb" }); - - const session = await boundary.recordStateResultWithStepCompatibility( - branchTo("agent_setup", { - metadata: { state: "sandbox", sandboxName: "hermes-sb", agent: "hermes" }, - }), - ); - - expect(session.machine.state).toBe("agent_setup"); - expect(harness.events.at(-1)).toMatchObject({ - type: "state.result.skipped", - state: "agent_setup", - metadata: { - reason: "already_at_target", - currentState: "agent_setup", - targetState: "agent_setup", - }, - }); - }); - it("records resume conflict diagnostics through the runtime", async () => { const harness = createRuntimeHarness(); const boundary = new OnboardRuntimeBoundary({ diff --git a/src/lib/onboard/runtime-boundary.ts b/src/lib/onboard/runtime-boundary.ts index 84ddd658ad3..c66cea33270 100644 --- a/src/lib/onboard/runtime-boundary.ts +++ b/src/lib/onboard/runtime-boundary.ts @@ -2,14 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 import type { Session, SessionUpdates } from "../state/onboard-session"; -import { - RECORD_ONLY_STEP_MUTATION_OPTIONS, - type StepMutationOptions, - shouldUpdateMachine, -} from "../state/onboard-step-mutation"; import type { OnboardStateFailedResult, OnboardStateResult } from "./machine/result"; import { OnboardRuntime } from "./machine/runtime"; -import { assertValidOnboardMachineTransition } from "./machine/transitions"; import type { OnboardMachineEventType, OnboardMachineState } from "./machine/types"; import type { ResumeConfigConflict } from "./resume-config"; @@ -27,12 +21,6 @@ export interface OnboardRuntimeBoundaryOptions { toSessionUpdates(updates: Record): SessionUpdates; maybeForceE2eStepFailure(stepName: string): void; createRuntime?(): OnboardRuntime; - /** - * Override for legacy/test harnesses. Production boundary writes default to - * status-only step mutations so explicit OnboardStateResult transitions stay - * the durable machine source of truth. - */ - stepMutationOptions?: StepMutationOptions; } export class OnboardRuntimeBoundary { @@ -88,7 +76,7 @@ export class OnboardRuntimeBoundary { } = {}, ): Promise { const runtime = this.getRuntime(); - await runtime.markStepStarted(stepName, this.stepMutationOptions()); + await runtime.markStepStarted(stepName); if (Object.keys(updates).length > 0) { await runtime.updateContext(this.options.toSessionUpdates(updates)); } @@ -96,7 +84,7 @@ export class OnboardRuntimeBoundary { } async recordStepComplete(stepName: string, updates: SessionUpdates = {}): Promise { - return this.getRuntime().markStepComplete(stepName, updates, this.stepMutationOptions()); + return this.getRuntime().markStepComplete(stepName, updates); } async recordStepSkipped(stepName: string): Promise { @@ -104,15 +92,7 @@ export class OnboardRuntimeBoundary { } async recordStepFailed(stepName: string, message: string | null): Promise { - return this.getRuntime().markStepFailed(stepName, message, this.stepMutationOptions()); - } - - private stepMutationOptions(): StepMutationOptions { - return this.options.stepMutationOptions ?? RECORD_ONLY_STEP_MUTATION_OPTIONS; - } - - private usesLegacyMachineStepMutation(): boolean { - return shouldUpdateMachine(this.stepMutationOptions()); + return this.getRuntime().markStepFailed(stepName, message); } async recordStateSkipped( @@ -126,52 +106,12 @@ export class OnboardRuntimeBoundary { return this.getRuntime().applyResult(result); } - private async assertStateResultWillApply(result: OnboardStateResult): Promise { - const current = await this.getRuntime().session(); - if (result.type === "failed") { - assertValidOnboardMachineTransition(current.machine.state, "failed"); - return; - } - if (result.type === "complete") { - assertValidOnboardMachineTransition(current.machine.state, "complete"); - return; - } - - if (result.type === "pause") { - const sourceState = - result.metadata && typeof result.metadata.state === "string" ? result.metadata.state : null; - if (sourceState && current.machine.state !== sourceState) { - throw new Error( - `Paused onboarding state result source mismatch: ${sourceState} != ${current.machine.state}`, - ); - } - return; - } - - const sourceState = - result.metadata && typeof result.metadata.state === "string" ? result.metadata.state : null; - if (current.machine.state === result.next) { - throw new Error(`Record-only step result already reached target state: ${result.next}`); - } - if (sourceState && current.machine.state !== sourceState) { - throw new Error( - `Record-only step result source mismatch: ${sourceState} != ${current.machine.state}`, - ); - } - const transition = assertValidOnboardMachineTransition(current.machine.state, result.next); - if (result.transitionKind && transition.kind !== result.transitionKind) { - throw new Error( - `Invalid onboarding machine transition kind: ${current.machine.state} -> ${result.next} expected ${result.transitionKind}, got ${transition.kind}`, - ); - } - } - async recordStepCompleteWithStateResult( stepName: string, updates: SessionUpdates, result: OnboardStateResult, ): Promise { - await this.assertStateResultWillApply(result); + await this.getRuntime().assertResultWillApply(result); await this.getRuntime().markStepCompleteRecordOnly(stepName, updates); return this.recordStateResult(result); } @@ -181,56 +121,11 @@ export class OnboardRuntimeBoundary { message: string | null, result: OnboardStateFailedResult, ): Promise { - await this.assertStateResultWillApply(result); + await this.getRuntime().assertResultWillApply(result); await this.getRuntime().markStepFailedRecordOnly(stepName, message); return this.recordStateResult(result); } - /** - * Compatibility bridge for legacy/test boundaries explicitly configured with - * `updateMachine === true`. - * - * Default record-only paths reject stale transition results before applying - * them. Skipped legacy results must stay metadata-only so stale results cannot - * become a context source. Live resume replay uses - * `recordInvalidatedStateResult` instead so recomputed-but-stale results are - * explicitly invalidated rather than accepted through this compatibility path. - */ - async recordStateResultWithStepCompatibility(result: OnboardStateResult): Promise { - const runtime = this.getRuntime(); - const current = await runtime.session(); - if (result.type !== "transition") return runtime.applyResult(result); - - if (!this.usesLegacyMachineStepMutation()) { - await this.assertStateResultWillApply(result); - return runtime.applyResult(result); - } - - if (current.machine.state === result.next) { - assertResultHasNoContextUpdates(result, "skip"); - return runtime.emitResultSkipped({ - reason: "already_at_target", - currentState: current.machine.state, - targetState: result.next, - metadata: result.metadata, - }); - } - - const sourceState = - result.metadata && typeof result.metadata.state === "string" ? result.metadata.state : null; - if (sourceState && current.machine.state !== sourceState) { - assertResultHasNoContextUpdates(result, "skip"); - return runtime.emitResultSkipped({ - reason: "source_state_mismatch", - currentState: current.machine.state, - targetState: result.next, - metadata: { ...(result.metadata ?? {}), sourceState }, - }); - } - - return runtime.applyResult(result); - } - async recordInvalidatedStateResult( result: OnboardStateResult, options: { @@ -277,9 +172,6 @@ export class OnboardRuntimeBoundary { await runtime.transition("post_verify"); return runtime.complete(updates); } - if (current.machine.state === "post_verify") { - return runtime.complete(updates); - } - return runtime.completeSession(updates); + return runtime.complete(updates); } } diff --git a/src/lib/state/onboard-session.test.ts b/src/lib/state/onboard-session.test.ts index 0de6c8cb5f7..29d976d157a 100644 --- a/src/lib/state/onboard-session.test.ts +++ b/src/lib/state/onboard-session.test.ts @@ -8,21 +8,13 @@ import os from "node:os"; import path from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import { - markStepCompleteLegacy, - markStepFailedLegacy, - markStepStartedLegacy, -} from "../../../test/helpers/onboard-legacy-step-mutation"; - const require = createRequire(import.meta.url); const distPath = require.resolve("./onboard-session"); const eventsDistPath = require.resolve("../onboard/machine/events"); -const stepMutationDistPath = require.resolve("./onboard-step-mutation"); const originalHome = process.env.HOME; type OnboardSessionModule = typeof import("./onboard-session"); type OnboardMachineEventsModule = typeof import("../onboard/machine/events"); type OnboardMachineEvent = import("../onboard/machine/events").OnboardMachineEvent; -type OnboardStepMutationModule = typeof import("./onboard-step-mutation"); type LoadedSession = NonNullable>; type DebugSummary = NonNullable>; type NullableSessionUpdateKey = import("./onboard-session").NullableSessionUpdateKey; @@ -30,7 +22,6 @@ type MessagingPlan = NonNullable; type MessagingChannelId = MessagingPlan["channels"][number]["channelId"]; let session: OnboardSessionModule; let machineEvents: OnboardMachineEventsModule; -let stepMutation: OnboardStepMutationModule; let tmpDir: string; const _nullableSessionUpdateKeyAcceptsNullableFields: Record< @@ -117,10 +108,8 @@ beforeEach(() => { process.env.HOME = tmpDir; delete require.cache[distPath]; delete require.cache[eventsDistPath]; - delete require.cache[stepMutationDistPath]; session = require("./onboard-session"); machineEvents = require("../onboard/machine/events"); - stepMutation = require("./onboard-step-mutation"); machineEvents.clearOnboardMachineEventListeners(); session.clearSession(); session.releaseOnboardLock(); @@ -198,7 +187,7 @@ describe("onboard session", () => { it("redacts credential-bearing endpoint URLs before persisting them", () => { session.saveSession(session.createSession()); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { endpointUrl: "https://alice:secret@example.com/v1/models?token=abc123&sig=def456&X-Amz-Signature=ghi789&keep=yes#token=frag", }); @@ -218,7 +207,7 @@ describe("onboard session", () => { stationExpressIntent: { version: 1, kind: "spark", sandboxName: "my-assistant" }, }), ); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { provider: "vllm-local", model: "nvidia/Qwen3.6-35B-A3B-NVFP4", sandboxName: "my-assistant", @@ -229,31 +218,28 @@ describe("onboard session", () => { expect(loaded.provider).toBe("vllm-local"); }); - it("marks steps started, completed, and failed", () => { + it("records step status without changing machine or terminal status", () => { session.saveSession(session.createSession()); - markStepStartedLegacy(session, stepMutation, "gateway"); + session.markStepStarted("gateway"); let loaded = requireLoadedSession(session.loadSession()); expect(loaded.steps.gateway.status).toBe("in_progress"); expect(loaded.lastStepStarted).toBe("gateway"); expect(loaded.steps.gateway.completedAt).toBeNull(); - markStepCompleteLegacy(session, stepMutation, "gateway", { sandboxName: "my-assistant" }); + session.markStepComplete("gateway", { sandboxName: "my-assistant" }); loaded = requireLoadedSession(session.loadSession()); expect(loaded.steps.gateway.status).toBe("complete"); expect(loaded.sandboxName).toBe("my-assistant"); expect(loaded.steps.gateway.completedAt).toBeTruthy(); - markStepFailedLegacy(session, stepMutation, "sandbox", "Sandbox creation failed"); + session.markStepFailed("sandbox", "Sandbox creation failed"); loaded = requireLoadedSession(session.loadSession()); expect(loaded.steps.sandbox.status).toBe("failed"); expect(loaded.steps.sandbox.completedAt).toBeNull(); - expect(loaded.failure).not.toBeNull(); - if (!loaded.failure) { - throw new Error("Expected failure metadata after markStepFailed()"); - } - expect(loaded.failure.step).toBe("sandbox"); - expect(loaded.failure.message).toMatch(/Sandbox creation failed/); - expect(loaded.machine.state).toBe("failed"); + expect(loaded.steps.sandbox.error).toBe("Sandbox creation failed"); + expect(loaded.failure).toBeNull(); + expect(loaded.status).toBe("in_progress"); + expect(loaded.machine).toMatchObject({ state: "init", revision: 0 }); }); it("can record step boundaries without mutating the machine snapshot", () => { @@ -261,53 +247,43 @@ describe("onboard session", () => { machineEvents.addOnboardMachineEventListener((event) => emitted.push(event)); session.saveSession(session.createSession()); - session.markStepStarted("preflight", { updateMachine: false }); + session.markStepStarted("preflight"); let loaded = requireLoadedSession(session.loadSession()); expect(loaded.steps.preflight.status).toBe("in_progress"); expect(loaded.status).toBe("in_progress"); expect(loaded.machine).toMatchObject({ state: "init", revision: 0 }); - session.markStepComplete( - "preflight", - { sandboxName: "my-assistant" }, - { updateMachine: false }, - ); + session.markStepComplete("preflight", { sandboxName: "my-assistant" }); loaded = requireLoadedSession(session.loadSession()); expect(loaded.steps.preflight.status).toBe("complete"); expect(loaded.sandboxName).toBe("my-assistant"); expect(loaded.machine).toMatchObject({ state: "init", revision: 0 }); - session.markStepFailed("gateway", "Gateway failed", { updateMachine: false }); + session.markStepFailed("gateway", "Gateway failed"); loaded = requireLoadedSession(session.loadSession()); expect(loaded.steps.gateway.status).toBe("failed"); expect(loaded.status).toBe("in_progress"); expect(loaded.failure).toBeNull(); expect(loaded.machine).toMatchObject({ state: "init", revision: 0 }); - expect(emitted.map((event) => event.type)).toEqual(["context.updated"]); + expect(emitted).toEqual([]); }); - it("persists a compact machine snapshot across step boundaries", () => { + it("leaves machine transition ownership outside step helpers", () => { session.saveSession(session.createSession()); let loaded = requireLoadedSession(session.loadSession()); expect(loaded.machine).toMatchObject({ state: "init", revision: 0 }); - markStepStartedLegacy(session, stepMutation, "preflight"); - loaded = requireLoadedSession(session.loadSession()); - expect(loaded.machine).toMatchObject({ state: "preflight", revision: 1 }); - expect(loaded.machine.stateEnteredAt).toBe(loaded.steps.preflight.startedAt); - - markStepCompleteLegacy(session, stepMutation, "preflight"); + session.markStepStarted("preflight"); loaded = requireLoadedSession(session.loadSession()); - expect(loaded.machine).toMatchObject({ state: "gateway", revision: 2 }); - expect(loaded.machine.stateEnteredAt).toBe(loaded.steps.preflight.completedAt); + expect(loaded.machine).toMatchObject({ state: "init", revision: 0 }); - markStepCompleteLegacy(session, stepMutation, "gateway"); + session.markStepComplete("preflight"); loaded = requireLoadedSession(session.loadSession()); - expect(loaded.machine).toMatchObject({ state: "provider_selection", revision: 3 }); + expect(loaded.machine).toMatchObject({ state: "init", revision: 0 }); - session.completeSession(); + session.markStepComplete("gateway"); loaded = requireLoadedSession(session.loadSession()); - expect(loaded.machine).toMatchObject({ state: "complete", revision: 4 }); + expect(loaded.machine).toMatchObject({ state: "init", revision: 0 }); expect(requireDebugSummary(session.summarizeForDebug()).machine).toEqual(loaded.machine); }); @@ -388,59 +364,22 @@ describe("onboard session", () => { }); }); - it("emits redacted structured machine events for session step mutations", () => { + it("does not emit machine events for direct step mutations", () => { const emitted: OnboardMachineEvent[] = []; machineEvents.addOnboardMachineEventListener((event) => emitted.push(event)); session.saveSession(session.createSession({ sessionId: "session-1" })); - markStepStartedLegacy(session, stepMutation, "gateway"); - markStepCompleteLegacy(session, stepMutation, "gateway", { + session.markStepStarted("gateway"); + session.markStepComplete("gateway", { sandboxName: "my-assistant", endpointUrl: "https://alice:super-secret-token@example.com/v1?token=super-secret-token&keep=yes#token=super-secret-token", credentialEnv: "NVIDIA_INFERENCE_API_KEY", }); session.markStepSkipped("openclaw"); - markStepFailedLegacy( - session, - stepMutation, - "sandbox", - "NVIDIA_INFERENCE_API_KEY=super-secret-token", - ); - session.completeSession({ provider: "ollama-local", credentialEnv: null }); - - expect(emitted.map((event) => event.type)).toEqual([ - "state.entered", - "context.updated", - "state.completed", - "state.skipped", - "state.failed", - "onboard.failed", - "context.updated", - "onboard.completed", - ]); - expect(emitted[0]).toMatchObject({ - version: 1, - sessionId: "session-1", - state: "gateway", - step: "gateway", - error: null, - }); - expect(emitted[1].context).toMatchObject({ - sandboxName: "my-assistant", - credentialEnv: "NVIDIA_INFERENCE_API_KEY", - }); - expect(emitted[1].context.endpointOrigin).toBe("https://example.com"); - expect(emitted[1].metadata.fields).toEqual(["sandboxName", "endpointUrl", "credentialEnv"]); - expect(emitted[4]).toMatchObject({ - type: "state.failed", - state: "sandbox", - step: "sandbox", - error: "NVIDIA_INFERENCE_API_KEY=", - }); - expect(emitted[5]).toMatchObject({ type: "onboard.failed", state: "failed" }); - expect(emitted.at(-1)).toMatchObject({ type: "onboard.completed", state: "complete" }); - expect(JSON.stringify(emitted)).not.toContain("super-secret-token"); + session.markStepFailed("sandbox", "NVIDIA_INFERENCE_API_KEY=super-secret-token"); + + expect(emitted).toEqual([]); const persisted = JSON.parse(fs.readFileSync(session.SESSION_FILE, "utf8")); expect(persisted.events).toBeUndefined(); @@ -452,7 +391,7 @@ describe("onboard session", () => { }); session.saveSession(session.createSession()); - expect(() => markStepStartedLegacy(session, stepMutation, "preflight")).not.toThrow(); + expect(() => session.markStepStarted("preflight")).not.toThrow(); const loaded = requireLoadedSession(session.loadSession()); expect(loaded.steps.preflight.status).toBe("in_progress"); @@ -463,7 +402,7 @@ describe("onboard session", () => { machineEvents.addOnboardMachineEventListener((event) => emitted.push(event)); session.saveSession(session.createSession()); - markStepStartedLegacy(session, stepMutation, "not_a_real_step"); + session.markStepStarted("not_a_real_step"); expect(emitted).toEqual([]); }); @@ -478,8 +417,8 @@ describe("onboard session", () => { session.completeSession(); session.completeSession(); - expect(emitted.map((event) => event.type)).toEqual(["state.skipped", "onboard.completed"]); - expect(emitted).toHaveLength(2); + expect(emitted.map((event) => event.type)).toEqual(["onboard.completed"]); + expect(emitted).toHaveLength(1); }); it("persists safe provider metadata without persisting secrets", () => { @@ -503,7 +442,7 @@ describe("onboard session", () => { token: "secret", }, }; - markStepCompleteLegacy(session, stepMutation, "provider_selection", unsafeProviderUpdate); + session.markStepComplete("provider_selection", unsafeProviderUpdate); const loaded = requireLoadedSession(session.loadSession()); expect(loaded.provider).toBe("nvidia-nim"); @@ -535,7 +474,7 @@ describe("onboard session", () => { it("clears credentialEnv when a provider-selection update passes null (#2625)", () => { // Seed with a prior remote-provider onboard state. session.saveSession(session.createSession()); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { provider: "openai", model: "gpt-4o", endpointUrl: "https://api.openai.com/v1", @@ -548,7 +487,7 @@ describe("onboard session", () => { // User re-runs onboard and picks local Ollama. The wizard emits // credentialEnv=null and nimContainer=null alongside the new provider. - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { provider: "ollama-local", model: "qwen3:14b", endpointUrl: "http://host.docker.internal:11434/v1", @@ -568,12 +507,12 @@ describe("onboard session", () => { // Regression guard: undefined must mean "leave unchanged", distinct from // null ("clear"). Partial updates must not accidentally wipe fields. session.saveSession(session.createSession()); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { provider: "openai", model: "gpt-4o", credentialEnv: "OPENAI_API_KEY", }); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { model: "gpt-4o-mini" }); + session.markStepComplete("provider_selection", { model: "gpt-4o-mini" }); const loaded = requireLoadedSession(session.loadSession()); expect(loaded.model).toBe("gpt-4o-mini"); @@ -595,7 +534,7 @@ describe("onboard session", () => { JSON.parse(fs.readFileSync(session.SESSION_FILE, "utf8")).migratedLegacyValueHashes, ).toBeNull(); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { migratedLegacyValueHashes: { NVIDIA_API_KEY: digest, BROKEN_NUMERIC: 123, @@ -617,10 +556,10 @@ describe("onboard session", () => { session.saveSession(session.createSession()); const unset = JSON.parse(fs.readFileSync(session.SESSION_FILE, "utf8")).credentialEnv; - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { credentialEnv: "NVIDIA_INFERENCE_API_KEY", }); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { credentialEnv: null }); + session.markStepComplete("provider_selection", { credentialEnv: null }); const declined = JSON.parse(fs.readFileSync(session.SESSION_FILE, "utf8")).credentialEnv; expect(unset).toBeNull(); @@ -633,20 +572,20 @@ describe("onboard session", () => { it("only persists known Hermes auth methods", () => { session.saveSession(session.createSession()); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { provider: "hermes-provider", hermesAuthMethod: "oauth", }); let loaded = requireLoadedSession(session.loadSession()); expect(loaded.hermesAuthMethod).toBe("oauth"); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { hermesAuthMethod: "not-a-real-method" as never, }); loaded = requireLoadedSession(session.loadSession()); expect(loaded.hermesAuthMethod).toBe("oauth"); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { hermesAuthMethod: null, }); loaded = requireLoadedSession(session.loadSession()); @@ -701,7 +640,7 @@ describe("onboard session", () => { // support the null-clear contract. If any regresses to the old // string-only guard, the test below catches it. session.saveSession(session.createSession()); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { sandboxName: "stale-sandbox", provider: "openai", model: "gpt-4o", @@ -712,7 +651,7 @@ describe("onboard session", () => { nimContainer: "nim-abc", }); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { sandboxName: null, provider: null, model: null, @@ -739,7 +678,7 @@ describe("onboard session", () => { // finalizes the session for a successful run. A local-provider onboard // must not leave a stale credentialEnv on the "complete" record either. session.saveSession(session.createSession()); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { provider: "openai", credentialEnv: "OPENAI_API_KEY", }); @@ -870,13 +809,13 @@ describe("onboard session", () => { it("filterSafeUpdates passes through messagingPlan and accepts explicit null clear", () => { session.saveSession(session.createSession()); const plan = makeMessagingPlan("my-assistant", ["discord"]); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { messagingPlan: plan }); + session.markStepComplete("provider_selection", { messagingPlan: plan }); expect(requireLoadedSession(session.loadSession()).messagingPlan).toMatchObject({ sandboxName: "my-assistant", channels: [expect.objectContaining({ channelId: "discord", configured: true })], }); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { messagingPlan: null }); + session.markStepComplete("provider_selection", { messagingPlan: null }); expect(requireLoadedSession(session.loadSession()).messagingPlan).toBeNull(); }); @@ -973,7 +912,7 @@ describe("onboard session", () => { it("persists and clears web search config through safe session updates", () => { session.saveSession(session.createSession()); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { webSearchConfig: { fetchEnabled: true }, }); @@ -1030,7 +969,7 @@ describe("onboard session", () => { token: "should-not-persist", }, }; - markStepCompleteLegacy(session, stepMutation, "provider_selection", unsafeMetadataUpdate); + session.markStepComplete("provider_selection", unsafeMetadataUpdate); const loaded = requireLoadedSession(session.loadSession()); expect(loaded.metadata.gatewayName).toBe("nemoclaw"); @@ -1284,9 +1223,7 @@ describe("onboard session", () => { it("redacts sensitive values from persisted failure messages", () => { session.saveSession(session.createSession()); - markStepFailedLegacy( - session, - stepMutation, + session.markStepFailed( "inference", "provider auth failed with NVIDIA_INFERENCE_API_KEY=nvapi-secret Bearer topsecret sk-secret-value-that-is-long-enough ghp_1234567890123456789012345", ); @@ -1298,11 +1235,8 @@ describe("onboard session", () => { expect(loaded.steps.inference.error).not.toContain("topsecret"); expect(loaded.steps.inference.error).not.toContain("sk-secret-value-that-is-long-enough"); expect(loaded.steps.inference.error).not.toContain("ghp_1234567890123456789012345"); - expect(loaded.failure).not.toBeNull(); - if (!loaded.failure) { - throw new Error("Expected failure metadata after markStepFailed()"); - } - expect(loaded.failure.message).toBe(loaded.steps.inference.error); + expect(loaded.failure).toBeNull(); + expect(loaded.machine).toMatchObject({ state: "init", revision: 0 }); }); it("round-trips null messagingPlan through normalizeSession", () => { @@ -1330,7 +1264,7 @@ describe("onboard session", () => { it("filterSafeUpdates preserves messagingPlan field", () => { session.saveSession(session.createSession()); const plan = makeMessagingPlan("my-assistant", ["slack", "discord"]); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { messagingPlan: plan, }); @@ -1346,7 +1280,7 @@ describe("onboard session", () => { it("filterSafeUpdates ignores malformed messagingPlan values", () => { session.saveSession(session.createSession()); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { messagingPlan: { sandboxName: "my-assistant" }, } as unknown as Parameters[1]); @@ -1356,7 +1290,7 @@ describe("onboard session", () => { it("routes telegramConfig through markStepComplete in filterSafeUpdates (#1737)", () => { session.saveSession(session.createSession()); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { telegramConfig: { requireMention: true }, }); @@ -1364,7 +1298,7 @@ describe("onboard session", () => { expect(loaded.telegramConfig).toEqual({ requireMention: true }); // Explicit null (clearing the field) should also round-trip. - markStepCompleteLegacy(session, stepMutation, "provider_selection", { telegramConfig: null }); + session.markStepComplete("provider_selection", { telegramConfig: null }); const cleared = session.loadSession()!; expect(cleared.telegramConfig).toBeNull(); }); @@ -1372,7 +1306,7 @@ describe("onboard session", () => { it("drops malformed telegramConfig values in filterSafeUpdates (#1737)", () => { session.saveSession(session.createSession()); // Non-boolean requireMention — must not leak through. - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { telegramConfig: { requireMention: "yes" } as unknown as { requireMention: boolean }, }); @@ -1382,7 +1316,7 @@ describe("onboard session", () => { it("filterSafeUpdates routes wechatConfig through markStepComplete", () => { session.saveSession(session.createSession()); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { wechatConfig: { accountId: "primary", baseUrl: "https://x", userId: "u" }, }); @@ -1395,14 +1329,14 @@ describe("onboard session", () => { // Explicit null clears the field (used when WeChat is removed from the // enabled channels on a subsequent onboard). - markStepCompleteLegacy(session, stepMutation, "provider_selection", { wechatConfig: null }); + session.markStepComplete("provider_selection", { wechatConfig: null }); const cleared = session.loadSession()!; expect(cleared.wechatConfig).toBeNull(); }); it("filterSafeUpdates drops malformed wechatConfig values", () => { session.saveSession(session.createSession()); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { wechatConfig: { accountId: 9000 } as unknown as { accountId: string }, }); @@ -1427,8 +1361,8 @@ describe("onboard session", () => { it("summarizes the session for debug output", () => { session.saveSession(session.createSession({ sandboxName: "my-assistant" })); - markStepStartedLegacy(session, stepMutation, "preflight"); - markStepCompleteLegacy(session, stepMutation, "preflight"); + session.markStepStarted("preflight"); + session.markStepComplete("preflight"); session.completeSession(); const summary = requireDebugSummary(session.summarizeForDebug()); @@ -1440,12 +1374,15 @@ describe("onboard session", () => { }); it("keeps debug summaries redacted when failures were sanitized", () => { - session.saveSession(session.createSession({ sandboxName: "my-assistant" })); - markStepFailedLegacy( - session, - stepMutation, - "provider_selection", - "Bearer abcdefghijklmnopqrstuvwxyz", + session.saveSession( + session.createSession({ + sandboxName: "my-assistant", + failure: { + step: "provider_selection", + message: "Bearer abcdefghijklmnopqrstuvwxyz", + recordedAt: "2026-04-01T00:00:00.000Z", + }, + }), ); const summary = requireDebugSummary(session.summarizeForDebug()); diff --git a/src/lib/state/onboard-session.ts b/src/lib/state/onboard-session.ts index 089780330a9..12ee73f386e 100644 --- a/src/lib/state/onboard-session.ts +++ b/src/lib/state/onboard-session.ts @@ -48,11 +48,6 @@ import { preserveInvalidSessionToolDisclosure, type ToolDisclosure, } from "./onboard-session-tool-disclosure"; -import { - RECORD_ONLY_STEP_MUTATION_OPTIONS, - type StepMutationOptions, - shouldUpdateMachine, -} from "./onboard-step-mutation"; import { nextMachineStateAfterCompletedStep } from "./onboard-step-state"; import { nemoclawStateRoot } from "./state-root"; @@ -1347,11 +1342,7 @@ export function updateSession(mutator: (session: Session) => Session | void): Se return saveSession(next); } -function markStepStartedWithOptions( - stepName: string, - options: StepMutationOptions = RECORD_ONLY_STEP_MUTATION_OPTIONS, -): Session { - let shouldEmit = false; +export function markStepStarted(stepName: string): Session { const updatedSession = updateSession((session) => { const step = session.steps[stepName]; if (!step) return session; @@ -1363,28 +1354,14 @@ function markStepStartedWithOptions( session.lastStepStarted = stepName; session.failure = null; session.status = "in_progress"; - const state = machineStateFromOnboardSessionStep(stepName); - shouldEmit = Boolean(state && shouldUpdateMachine(options)); - if (state && shouldEmit) transitionMachineSnapshot(session, state, now); return session; }); - if (shouldEmit) { - emitOnboardMachineEvent( - createOnboardMachineEvent({ type: "state.entered", session: updatedSession, step: stepName }), - ); - } return updatedSession; } -function markStepCompleteWithOptions( - stepName: string, - updates: SessionUpdates = {}, - options: StepMutationOptions = RECORD_ONLY_STEP_MUTATION_OPTIONS, -): Session { +export function markStepComplete(stepName: string, updates: SessionUpdates = {}): Session { const safeUpdates = filterSafeUpdates(updates); - const hasUpdates = Object.keys(safeUpdates).length > 0; - let shouldEmit = false; - const updatedSession = updateSession((session) => { + return updateSession((session) => { const step = session.steps[stepName]; if (!step) return session; // Spark managed-vLLM Express intents (#7231) carry no receipt/served state @@ -1410,62 +1387,23 @@ function markStepCompleteWithOptions( Object.assign(session, safeUpdates); if (stationExpressIntent) session.stationExpressIntent = stationExpressIntent; else if (sparkExpressComplete) session.stationExpressIntent = null; - const nextState = nextMachineStateAfterCompletedStep(stepName, session); - shouldEmit = Boolean(nextState && shouldUpdateMachine(options)); - if (nextState && shouldEmit) transitionMachineSnapshot(session, nextState, now); return session; }); - if (hasUpdates) { - emitOnboardMachineEvent( - createOnboardMachineEvent({ - type: "context.updated", - session: updatedSession, - step: stepName, - metadata: { fields: Object.keys(safeUpdates) }, - }), - ); - } - if (shouldEmit) { - emitOnboardMachineEvent( - createOnboardMachineEvent({ - type: "state.completed", - session: updatedSession, - step: stepName, - }), - ); - } - return updatedSession; -} - -export function markStepStarted( - stepName: string, - options: StepMutationOptions = RECORD_ONLY_STEP_MUTATION_OPTIONS, -): Session { - return markStepStartedWithOptions(stepName, options); } export function markStepStartedRecordOnly(stepName: string): Session { - return markStepStartedWithOptions(stepName, RECORD_ONLY_STEP_MUTATION_OPTIONS); -} - -export function markStepComplete( - stepName: string, - updates: SessionUpdates = {}, - options: StepMutationOptions = RECORD_ONLY_STEP_MUTATION_OPTIONS, -): Session { - return markStepCompleteWithOptions(stepName, updates, options); + return markStepStarted(stepName); } export function markStepCompleteRecordOnly( stepName: string, updates: SessionUpdates = {}, ): Session { - return markStepCompleteWithOptions(stepName, updates, RECORD_ONLY_STEP_MUTATION_OPTIONS); + return markStepComplete(stepName, updates); } export function markStepSkipped(stepName: string): Session { - let shouldEmit = false; - const updatedSession = updateSession((session) => { + return updateSession((session) => { const step = session.steps[stepName]; if (!step) return session; if (step.status === "complete" || step.status === "failed" || step.status === "skipped") @@ -1474,70 +1412,23 @@ export function markStepSkipped(stepName: string): Session { step.startedAt = null; step.completedAt = null; step.error = null; - shouldEmit = true; return session; }); - if (shouldEmit) { - emitOnboardMachineEvent( - createOnboardMachineEvent({ type: "state.skipped", session: updatedSession, step: stepName }), - ); - } - return updatedSession; } -function markStepFailedWithOptions( - stepName: string, - message: string | null = null, - options: StepMutationOptions = RECORD_ONLY_STEP_MUTATION_OPTIONS, -): Session { - let shouldEmit = false; - const updatedSession = updateSession((session) => { +export function markStepFailed(stepName: string, message: string | null = null): Session { + return updateSession((session) => { const step = session.steps[stepName]; if (!step) return session; - const now = new Date().toISOString(); step.status = "failed"; step.completedAt = null; step.error = redactSensitiveText(message); - shouldEmit = shouldUpdateMachine(options); - if (shouldEmit) { - session.failure = sanitizeFailure({ step: stepName, message, recordedAt: now }); - session.status = "failed"; - transitionMachineSnapshot(session, "failed", now); - } return session; }); - if (shouldEmit) { - emitOnboardMachineEvent( - createOnboardMachineEvent({ - type: "state.failed", - session: updatedSession, - step: stepName, - error: message, - }), - ); - emitOnboardMachineEvent( - createOnboardMachineEvent({ - type: "onboard.failed", - session: updatedSession, - state: "failed", - step: stepName, - error: message, - }), - ); - } - return updatedSession; -} - -export function markStepFailed( - stepName: string, - message: string | null = null, - options: StepMutationOptions = RECORD_ONLY_STEP_MUTATION_OPTIONS, -): Session { - return markStepFailedWithOptions(stepName, message, options); } export function markStepFailedRecordOnly(stepName: string, message: string | null = null): Session { - return markStepFailedWithOptions(stepName, message, RECORD_ONLY_STEP_MUTATION_OPTIONS); + return markStepFailed(stepName, message); } /** diff --git a/src/lib/state/onboard-step-mutation.test.ts b/src/lib/state/onboard-step-mutation.test.ts index b5593f594c4..4864b221501 100644 --- a/src/lib/state/onboard-step-mutation.test.ts +++ b/src/lib/state/onboard-step-mutation.test.ts @@ -6,162 +6,49 @@ import os from "node:os"; import path from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import type * as eventsModule from "../onboard/machine/events"; import type * as sessionModule from "./onboard-session"; -import type * as stepMutationModule from "./onboard-step-mutation"; const originalHome = process.env.HOME; let session: typeof sessionModule; -let stepMutation: typeof stepMutationModule; -let machineEvents: typeof eventsModule; let tmpDir: string; beforeEach(async () => { - tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-onboard-step-mutation-")); + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-onboard-step-status-")); process.env.HOME = tmpDir; vi.resetModules(); - stepMutation = await import("./onboard-step-mutation"); session = await import("./onboard-session"); - machineEvents = await import("../onboard/machine/events"); - machineEvents.clearOnboardMachineEventListeners(); session.clearSession(); session.releaseOnboardLock(); }); afterEach(() => { - machineEvents.clearOnboardMachineEventListeners(); fs.rmSync(tmpDir, { recursive: true, force: true }); - if (originalHome === undefined) { - delete process.env.HOME; - } else { - process.env.HOME = originalHome; - } + if (originalHome === undefined) delete process.env.HOME; + else process.env.HOME = originalHome; }); -function requireLoadedSession(loaded: ReturnType) { - expect(loaded).not.toBeNull(); - if (!loaded) throw new Error("Expected onboard session to be present"); - return loaded; -} - -describe("record-only onboard step mutation", () => { - it("freezes shared mutation option constants and preserves helper behavior", () => { - expect(Object.isFrozen(stepMutation.LEGACY_MACHINE_STEP_MUTATION_OPTIONS)).toBe(true); - expect(Object.isFrozen(stepMutation.RECORD_ONLY_STEP_MUTATION_OPTIONS)).toBe(true); - expect(() => { - ( - stepMutation.LEGACY_MACHINE_STEP_MUTATION_OPTIONS as { updateMachine?: boolean } - ).updateMachine = false; - }).toThrow(TypeError); - expect(() => { - ( - stepMutation.RECORD_ONLY_STEP_MUTATION_OPTIONS as { updateMachine?: boolean } - ).updateMachine = true; - }).toThrow(TypeError); - - session.saveSession(session.createSession()); - session.markStepStarted("preflight", stepMutation.LEGACY_MACHINE_STEP_MUTATION_OPTIONS); - let loaded = requireLoadedSession(session.loadSession()); - expect(loaded.machine).toMatchObject({ state: "preflight", revision: 1 }); - - session.markStepStartedRecordOnly("gateway"); - loaded = requireLoadedSession(session.loadSession()); - expect(loaded.steps.gateway.status).toBe("in_progress"); - expect(loaded.machine).toMatchObject({ state: "preflight", revision: 1 }); - }); - - it("persists step status and per-step failure errors without mutating the machine snapshot", () => { - const emitted: eventsModule.OnboardMachineEvent[] = []; - machineEvents.addOnboardMachineEventListener((event) => emitted.push(event)); - session.saveSession(session.createSession()); - - session.markStepStartedRecordOnly("preflight"); - let loaded = requireLoadedSession(session.loadSession()); - expect(loaded.steps.preflight.status).toBe("in_progress"); - expect(loaded.status).toBe("in_progress"); - expect(loaded.machine).toMatchObject({ state: "init", revision: 0 }); - - session.markStepCompleteRecordOnly("preflight", { sandboxName: "my-assistant" }); - loaded = requireLoadedSession(session.loadSession()); - expect(loaded.steps.preflight.status).toBe("complete"); - expect(loaded.sandboxName).toBe("my-assistant"); - expect(loaded.machine).toMatchObject({ state: "init", revision: 0 }); - - session.markStepFailedRecordOnly( - "gateway", - "Gateway failed: NVIDIA_INFERENCE_API_KEY=nvapi-secret", - ); - loaded = requireLoadedSession(session.loadSession()); - expect(loaded.steps.gateway.status).toBe("failed"); - expect(loaded.steps.gateway.error).toBe("Gateway failed: NVIDIA_INFERENCE_API_KEY="); - expect(loaded.steps.gateway.error).not.toContain("nvapi-secret"); - expect(loaded.status).toBe("in_progress"); - expect(loaded.failure).toBeNull(); - expect(loaded.machine).toMatchObject({ state: "init", revision: 0 }); - expect(emitted.map((event) => event.type)).toEqual(["context.updated"]); - }); - - it("keeps explicit legacy step helpers on legacy machine mutation", () => { - const emitted: eventsModule.OnboardMachineEvent[] = []; - machineEvents.addOnboardMachineEventListener((event) => emitted.push(event)); - session.saveSession(session.createSession()); - - session.markStepStarted("preflight", stepMutation.LEGACY_MACHINE_STEP_MUTATION_OPTIONS); - let loaded = requireLoadedSession(session.loadSession()); - expect(loaded.machine).toMatchObject({ state: "preflight", revision: 1 }); - - session.markStepComplete( - "preflight", - { sandboxName: "my-assistant" }, - stepMutation.LEGACY_MACHINE_STEP_MUTATION_OPTIONS, - ); - loaded = requireLoadedSession(session.loadSession()); - expect(loaded.sandboxName).toBe("my-assistant"); - expect(loaded.machine).toMatchObject({ state: "gateway", revision: 2 }); - - session.markStepFailed( - "gateway", - "Gateway failed: NVIDIA_INFERENCE_API_KEY=nvapi-secret", - stepMutation.LEGACY_MACHINE_STEP_MUTATION_OPTIONS, - ); - loaded = requireLoadedSession(session.loadSession()); - expect(loaded.status).toBe("failed"); - expect(loaded.failure?.message).toBe("Gateway failed: NVIDIA_INFERENCE_API_KEY="); - expect(loaded.failure?.message).not.toContain("nvapi-secret"); - expect(loaded.machine).toMatchObject({ state: "failed", revision: 3 }); - expect(emitted.map((event) => event.type)).toEqual([ - "state.entered", - "context.updated", - "state.completed", - "state.failed", - "onboard.failed", - ]); - }); - - it("defaults no-options step helpers to record-only machine mutation", () => { - const emitted: eventsModule.OnboardMachineEvent[] = []; - machineEvents.addOnboardMachineEventListener((event) => emitted.push(event)); +describe("onboard step status", () => { + it("persists status and safe updates without changing the machine snapshot", () => { session.saveSession(session.createSession()); session.markStepStarted("preflight"); - let loaded = requireLoadedSession(session.loadSession()); - expect(loaded.steps.preflight.status).toBe("in_progress"); - expect(loaded.machine).toMatchObject({ state: "init", revision: 0 }); - session.markStepComplete("preflight", { sandboxName: "my-assistant" }); - loaded = requireLoadedSession(session.loadSession()); - expect(loaded.steps.preflight.status).toBe("complete"); - expect(loaded.sandboxName).toBe("my-assistant"); - expect(loaded.machine).toMatchObject({ state: "init", revision: 0 }); - session.markStepFailed("gateway", "Gateway failed: NVIDIA_INFERENCE_API_KEY=nvapi-secret"); - loaded = requireLoadedSession(session.loadSession()); - expect(loaded.steps.gateway.status).toBe("failed"); - expect(loaded.steps.gateway.error).toBe("Gateway failed: NVIDIA_INFERENCE_API_KEY="); - expect(loaded.steps.gateway.error).not.toContain("nvapi-secret"); - expect(loaded.status).toBe("in_progress"); - expect(loaded.failure).toBeNull(); - expect(loaded.machine).toMatchObject({ state: "init", revision: 0 }); - expect(emitted.map((event) => event.type)).toEqual(["context.updated"]); + + const loaded = session.loadSession(); + expect(loaded).not.toBeNull(); + expect(loaded).toMatchObject({ + sandboxName: "my-assistant", + status: "in_progress", + failure: null, + machine: { state: "init", revision: 0 }, + steps: { + preflight: { status: "complete" }, + gateway: { + status: "failed", + error: "Gateway failed: NVIDIA_INFERENCE_API_KEY=", + }, + }, + }); }); }); diff --git a/src/lib/state/onboard-step-mutation.ts b/src/lib/state/onboard-step-mutation.ts deleted file mode 100644 index 406fb5c98bb..00000000000 --- a/src/lib/state/onboard-step-mutation.ts +++ /dev/null @@ -1,25 +0,0 @@ -// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -export interface StepMutationOptions { - /** - * Transitional FSM migration escape hatch for paths where legacy step helpers - * still move session.machine. Runtime-owned onboarding should use record-only - * step writes paired with explicit OnboardStateResult transitions so the - * runtime remains the durable machine source of truth. Remove this option once - * direct step helpers no longer own machine transitions. - */ - updateMachine?: boolean; -} - -export const LEGACY_MACHINE_STEP_MUTATION_OPTIONS: Readonly = Object.freeze({ - updateMachine: true, -}); - -export const RECORD_ONLY_STEP_MUTATION_OPTIONS: Readonly = Object.freeze({ - updateMachine: false, -}); - -export function shouldUpdateMachine(options: StepMutationOptions | undefined): boolean { - return options?.updateMachine !== false; -} diff --git a/test/helpers/onboard-legacy-step-mutation.ts b/test/helpers/onboard-legacy-step-mutation.ts deleted file mode 100644 index 164ee3d75d8..00000000000 --- a/test/helpers/onboard-legacy-step-mutation.ts +++ /dev/null @@ -1,49 +0,0 @@ -// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -type LegacyStepMutationModule = { - LEGACY_MACHINE_STEP_MUTATION_OPTIONS: object; -}; - -type LegacyStepSessionModule = { - markStepStarted(stepName: string, options?: object): unknown; - markStepComplete(stepName: string, updates?: unknown, options?: object): unknown; - markStepFailed(stepName: string, message?: string | null, options?: object): unknown; -}; - -export function markStepStartedLegacy( - session: SessionModule, - stepMutation: LegacyStepMutationModule, - stepName: string, -): ReturnType { - return session.markStepStarted( - stepName, - stepMutation.LEGACY_MACHINE_STEP_MUTATION_OPTIONS, - ) as ReturnType; -} - -export function markStepCompleteLegacy( - session: SessionModule, - stepMutation: LegacyStepMutationModule, - stepName: string, - updates?: Parameters[1], -): ReturnType { - return session.markStepComplete( - stepName, - updates, - stepMutation.LEGACY_MACHINE_STEP_MUTATION_OPTIONS, - ) as ReturnType; -} - -export function markStepFailedLegacy( - session: SessionModule, - stepMutation: LegacyStepMutationModule, - stepName: string, - message: string | null = null, -): ReturnType { - return session.markStepFailed( - stepName, - message, - stepMutation.LEGACY_MACHINE_STEP_MUTATION_OPTIONS, - ) as ReturnType; -} diff --git a/test/repro-1751-extra.test.ts b/test/repro-1751-extra.test.ts index 74912a8b917..9d7fe72e27c 100644 --- a/test/repro-1751-extra.test.ts +++ b/test/repro-1751-extra.test.ts @@ -19,12 +19,6 @@ import os from "node:os"; import path from "node:path"; import { afterEach, beforeEach, describe, expect, it } from "vitest"; import * as session from "../src/lib/state/onboard-session"; -import * as stepMutation from "../src/lib/state/onboard-step-mutation"; -import { - markStepCompleteLegacy, - markStepFailedLegacy, - markStepStartedLegacy, -} from "./helpers/onboard-legacy-step-mutation"; const tmpHomes: string[] = []; @@ -43,7 +37,7 @@ afterEach(() => { describe("GPU passthrough session persistence (#1751)", () => { it("filterSafeUpdates: gpuPassthrough=true is propagated to safe", () => { session.saveSession(session.createSession()); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { gpuPassthrough: true }); + session.markStepComplete("provider_selection", { gpuPassthrough: true }); const loaded = session.loadSession()!; expect(loaded.gpuPassthrough).toBe(true); }); @@ -51,7 +45,7 @@ describe("GPU passthrough session persistence (#1751)", () => { it("filterSafeUpdates: gpuPassthrough=false is propagated to safe", () => { const s = session.createSession({ gpuPassthrough: true }); session.saveSession(s); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { gpuPassthrough: false }); + session.markStepComplete("provider_selection", { gpuPassthrough: false }); const loaded = session.loadSession()!; expect(loaded.gpuPassthrough).toBe(false); }); @@ -68,7 +62,7 @@ describe("GPU passthrough session persistence (#1751)", () => { // Garbage shapes: string, number, null. None should clobber the existing true. const garbageValues: unknown[] = ["yes", 1, null, undefined, "true"]; for (const v of garbageValues) { - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { gpuPassthrough: v as unknown as boolean, }); const loaded = session.loadSession()!; @@ -93,12 +87,12 @@ describe("GPU passthrough session persistence (#1751)", () => { it("completeSession persists gpuPassthrough via filterSafeUpdates", () => { session.saveSession(session.createSession()); - markStepStartedLegacy(session, stepMutation, "preflight"); - markStepCompleteLegacy(session, stepMutation, "preflight", { gpuPassthrough: true }); - markStepStartedLegacy(session, stepMutation, "gateway"); - markStepCompleteLegacy(session, stepMutation, "gateway"); - markStepStartedLegacy(session, stepMutation, "sandbox"); - markStepCompleteLegacy(session, stepMutation, "sandbox"); + session.markStepStarted("preflight"); + session.markStepComplete("preflight", { gpuPassthrough: true }); + session.markStepStarted("gateway"); + session.markStepComplete("gateway"); + session.markStepStarted("sandbox"); + session.markStepComplete("sandbox"); session.completeSession({ gpuPassthrough: true }); const loaded = session.loadSession()!; expect(loaded.gpuPassthrough).toBe(true); @@ -113,8 +107,8 @@ describe("GPU passthrough session persistence (#1751)", () => { it("markStepFailed preserves gpuPassthrough", () => { session.saveSession(session.createSession({ gpuPassthrough: true })); - markStepStartedLegacy(session, stepMutation, "gateway"); - markStepFailedLegacy(session, stepMutation, "gateway", "test failure"); + session.markStepStarted("gateway"); + session.markStepFailed("gateway", "test failure"); const loaded = session.loadSession()!; expect(loaded.gpuPassthrough).toBe(true); expect(loaded.steps.gateway?.status).toBe("failed"); From f58ca8824a495c3f35219b07c7ef5bcd39669c69 Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Tue, 28 Jul 2026 07:37:08 -0700 Subject: [PATCH 03/19] test(onboard): cover exact sandbox phase entry Signed-off-by: Carlos Villela --- .../onboard/machine/core-flow-phases.test.ts | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/lib/onboard/machine/core-flow-phases.test.ts b/src/lib/onboard/machine/core-flow-phases.test.ts index 9480a708471..5ba7459bcb7 100644 --- a/src/lib/onboard/machine/core-flow-phases.test.ts +++ b/src/lib/onboard/machine/core-flow-phases.test.ts @@ -625,6 +625,59 @@ describe("core onboard flow phases", () => { expect(result.session.machine.state).toBe("openclaw"); }); + it("runs the sandbox effect once at exact sandbox entry", async () => { + const createSandbox = vi.fn(async () => "created-sandbox"); + const phases = createPhases({ sandboxDeps: { createSandbox } }); + const appliedTransitions: string[] = []; + const repairEvents: string[] = []; + let runtimeSession = createSession({ + machine: { + version: 1, + state: "sandbox", + stateEnteredAt: "2026-06-09T00:02:00.000Z", + revision: 7, + }, + }); + + const result = await runCoreOnboardFlowSlice({ + context: context({ + resume: true, + model: "nvidia/test", + provider: "nim", + endpointUrl: "https://example.test/v1", + credentialEnv: "NVIDIA_INFERENCE_API_KEY", + }), + runtime: { + session: async () => runtimeSession, + applyResult: async (stateResult) => { + const transition = stateResult as ReturnType; + appliedTransitions.push(`${transition.transitionKind}:${transition.next}`); + runtimeSession = createSession({ + machine: { + version: 1, + state: transition.next, + stateEnteredAt: "2026-06-09T00:03:00.000Z", + revision: runtimeSession.machine.revision + 1, + }, + }); + return runtimeSession; + }, + }, + phases, + resume: true, + recordRepairEvent: repairRecorder(repairEvents), + }); + + expect(createSandbox).toHaveBeenCalledOnce(); + expect(appliedTransitions).toEqual(["branch:agent_setup"]); + expect(repairEvents).toEqual([ + "state.repair.started:provider_selection", + "state.repair.completed:provider_selection", + ]); + expect(result.context.sandboxName).toBe("created-sandbox"); + expect(result.session.machine.state).toBe("agent_setup"); + }); + it.each([ ["inference", true, ["advance:sandbox", "branch:openclaw"]], ["sandbox", true, ["branch:openclaw"]], From 03a4341b943cbcb6bddcaa63813212deccc37369 Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Tue, 28 Jul 2026 08:28:22 -0700 Subject: [PATCH 04/19] chore(architecture): ratchet state root files Signed-off-by: Carlos Villela --- ci/source-architecture-budget.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/source-architecture-budget.json b/ci/source-architecture-budget.json index 77396cbc6d2..12b6cd9e659 100644 --- a/ci/source-architecture-budget.json +++ b/ci/source-architecture-budget.json @@ -145,7 +145,7 @@ "src/lib/onboard": 307, "src/lib/actions": 19, "src/lib/actions/sandbox": 182, - "src/lib/state": 38, + "src/lib/state": 37, "src/lib/inference": 62, "scripts": 44 } From 13ba3dffc032e8f1d739bfd6b699f37aee6a62f1 Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Tue, 28 Jul 2026 08:43:59 -0700 Subject: [PATCH 05/19] docs(onboard): state runtime ownership contract Signed-off-by: Carlos Villela --- src/lib/onboard/machine/README.md | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/lib/onboard/machine/README.md b/src/lib/onboard/machine/README.md index 28e9fb5cd3a..8558f85624a 100644 --- a/src/lib/onboard/machine/README.md +++ b/src/lib/onboard/machine/README.md @@ -41,15 +41,12 @@ A state handler may perform many smaller operations, but it should expose only s ## Session steps versus machine state -The persisted onboarding session still tracks step-level progress for resumability. Step recording is older than the FSM and is currently used as a compatibility bridge. +The persisted onboarding session tracks step-level progress for resumability. -Long term: - -- `OnboardRuntime` should own machine transitions and machine revision increments. -- Session step helpers should record only step status (`pending`, `in_progress`, `complete`, `failed`, `skipped`). -- State handlers should return explicit results instead of implicitly moving the machine by calling step helpers. - -Until that migration completes, step helpers may still infer machine snapshots for compatibility with older sessions and tests. +- `OnboardRuntime` owns normal machine transitions, revision increments, terminal state, and machine events. +- Session step helpers record only step status (`pending`, `in_progress`, `complete`, `failed`, `skipped`) and safe step updates. They cannot change the machine snapshot. +- State handlers return explicit results. They do not move the machine through step helpers. +- Explicit session recovery and the process-exit failure backstop are narrow exceptions. ## Handler contract From 6650e85d1b4080554cd569701fc4b660eac3f17b Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Sun, 2 Aug 2026 23:58:01 -0700 Subject: [PATCH 06/19] fix(onboard): resolve runtime authority stack refresh --- .../onboard/machine/core-flow-phases.test.ts | 12 + src/lib/onboard/machine/core-flow-phases.ts | 9 + .../machine/initial-flow-phases.test.ts | 2 + src/lib/onboard/runtime-boundary.test.ts | 510 +----------------- src/lib/onboard/runtime-boundary.ts | 126 +---- .../onboard-session-reasoning-effort.test.ts | 11 +- src/lib/state/onboard-session.ts | 138 +---- 7 files changed, 66 insertions(+), 742 deletions(-) diff --git a/src/lib/onboard/machine/core-flow-phases.test.ts b/src/lib/onboard/machine/core-flow-phases.test.ts index ac8cebb3212..9727408cbd0 100644 --- a/src/lib/onboard/machine/core-flow-phases.test.ts +++ b/src/lib/onboard/machine/core-flow-phases.test.ts @@ -46,6 +46,8 @@ function context( hermesToolGateways: [], preferredInferenceApi: null, compatibleEndpointReasoning: null, + + compatibleEndpointReasoningEffort: null, nimContainer: null, webSearchConfig: null, webSearchSupported: false, @@ -135,6 +137,8 @@ function createPhases( hermesToolGateways: ["local"], preferredInferenceApi: "chat", compatibleEndpointReasoning: null, + + compatibleEndpointReasoningEffort: null, nimContainer: "nim-test", })), setupInference: vi.fn(async () => ({ ok: true as const })), @@ -159,7 +163,11 @@ function createPhases( recordRepairEvent: vi.fn(async () => createSession()), hydrateCredentialEnv: vi.fn(), configureCompatibleEndpointReasoning: vi.fn(async () => "false" as const), + + configureCompatibleEndpointReasoningEffort: vi.fn(async () => null), clearCompatibleEndpointReasoning: vi.fn(() => null), + + clearCompatibleEndpointReasoningEffort: vi.fn(() => null), repairLocalInferenceSystemdOverrideOrExit: vi.fn(), isNonInteractive: () => true, getOpenshellBinary: () => "openshell", @@ -200,6 +208,8 @@ function createPhases( resolvePath: (value) => value, agentSupportsWebSearch: () => true, note: vi.fn(), + + cliName: () => "nemoclaw", updateSession: vi.fn((mutator) => mutator(createSession()) ?? createSession()), getStoredMessagingChannelConfig: () => null, hydrateMessagingChannelConfig: (config) => config, @@ -362,6 +372,8 @@ describe("core onboard flow phases", () => { hermesToolGateways: [], preferredInferenceApi: "chat", compatibleEndpointReasoning: null, + + compatibleEndpointReasoningEffort: null, nimContainer: null, })); const { providerInference: providerPhase } = createPhases({ providerDeps: { setupNim } }); diff --git a/src/lib/onboard/machine/core-flow-phases.ts b/src/lib/onboard/machine/core-flow-phases.ts index 87692b6c1c8..8e03fc98dda 100644 --- a/src/lib/onboard/machine/core-flow-phases.ts +++ b/src/lib/onboard/machine/core-flow-phases.ts @@ -67,6 +67,8 @@ export interface SandboxOnboardFlowPhaseOptions< gatewayName: string; authoritativeResumeConfig?: boolean; authoritativePolicyTier?: string | null; + + recreateJournalTargetIntentFingerprint?: string | null; resumeAgentChanged: boolean; requestedObservabilityEnabled?: boolean | null; requestedDcodeAutoApprovalMode?: DcodeAutoApprovalMode | null; @@ -158,6 +160,8 @@ export function createProviderInferenceOnboardFlowPhase< hermesToolGateways: context.hermesToolGateways, preferredInferenceApi: context.preferredInferenceApi, compatibleEndpointReasoning: context.compatibleEndpointReasoning, + + compatibleEndpointReasoningEffort: context.compatibleEndpointReasoningEffort, nimContainer: context.nimContainer, webSearchConfig: context.webSearchConfig, }, @@ -181,6 +185,9 @@ export function createProviderInferenceOnboardFlowPhase< hermesToolGateways: providerInferenceResult.hermesToolGateways, preferredInferenceApi: providerInferenceResult.preferredInferenceApi, compatibleEndpointReasoning: providerInferenceResult.compatibleEndpointReasoning, + + compatibleEndpointReasoningEffort: + providerInferenceResult.compatibleEndpointReasoningEffort, nimContainer: providerInferenceResult.nimContainer, webSearchConfig: providerInferenceResult.webSearchConfig, }), @@ -210,6 +217,8 @@ export function createSandboxOnboardFlowPhase< gatewayName: options.gatewayName, authoritativeResumeConfig: options.authoritativeResumeConfig, authoritativePolicyTier: options.authoritativePolicyTier, + + recreateJournalTargetIntentFingerprint: options.recreateJournalTargetIntentFingerprint, endpointSource: endpointProvenance.endpointSource, resumeAgentChanged: options.resumeAgentChanged, requestedObservabilityEnabled: options.requestedObservabilityEnabled, diff --git a/src/lib/onboard/machine/initial-flow-phases.test.ts b/src/lib/onboard/machine/initial-flow-phases.test.ts index 76ffb9b59d2..4ae83d80b05 100644 --- a/src/lib/onboard/machine/initial-flow-phases.test.ts +++ b/src/lib/onboard/machine/initial-flow-phases.test.ts @@ -43,6 +43,8 @@ function context(overrides: Partial = {}): Context { hermesToolGateways: [], preferredInferenceApi: null, compatibleEndpointReasoning: null, + + compatibleEndpointReasoningEffort: null, nimContainer: null, webSearchConfig: null, webSearchSupported: false, diff --git a/src/lib/onboard/runtime-boundary.test.ts b/src/lib/onboard/runtime-boundary.test.ts index 160fb372d71..6bdffebbb82 100644 --- a/src/lib/onboard/runtime-boundary.test.ts +++ b/src/lib/onboard/runtime-boundary.test.ts @@ -10,7 +10,6 @@ import { type Session, type SessionUpdates, } from "../state/onboard-session"; -import type { StepMutationOptions } from "../state/onboard-step-mutation"; import type { OnboardMachineEvent } from "./machine/events"; import { advanceTo, @@ -21,11 +20,6 @@ import { retryTo, } from "./machine/result"; import { OnboardRuntime, type OnboardRuntimeDeps } from "./machine/runtime"; -import { - InvalidOnboardMachineTransitionError, - OnboardInterruptedError, -} from "./machine/transitions"; -import type { OnboardMachineState } from "./machine/types"; import { OnboardRuntimeBoundary } from "./runtime-boundary"; import { applySessionRecovery } from "./session-recovery"; @@ -33,60 +27,10 @@ function cloneSession(session: Session): Session { return normalizeSession(JSON.parse(JSON.stringify(session))) ?? session; } -const STEP_TO_STATE: Record = { - preflight: "preflight", - gateway: "gateway", - provider_selection: "provider_selection", - inference: "inference", - sandbox: "sandbox", - openclaw: "openclaw", - agent_setup: "agent_setup", - policies: "policies", -}; - -function nextStateAfterCompletedStep( - stepName: string, - session: Pick, -): OnboardMachineState | null { - switch (stepName) { - case "preflight": - return "gateway"; - case "gateway": - return "provider_selection"; - case "provider_selection": - return "inference"; - case "inference": - return "sandbox"; - case "sandbox": - return session.agent ? "agent_setup" : "openclaw"; - case "openclaw": - case "agent_setup": - return "policies"; - case "policies": - return "finalizing"; - default: - return null; - } -} - -function transitionMachine(session: Session, state: OnboardMachineState): void { - session.machine = { - version: session.machine.version, - state, - stateEnteredAt: "2026-05-27T00:00:00.000Z", - revision: - session.machine.state === state ? session.machine.revision : session.machine.revision + 1, - }; -} - -function shouldUpdateMachine(options: StepMutationOptions | undefined): boolean { - return options?.updateMachine !== false; -} - function createRuntimeHarness(overrides: Partial = {}) { let session: Session | null = createSession(overrides); const events: OnboardMachineEvent[] = []; - const stepOptionCalls: Array<{ method: string; options: StepMutationOptions | undefined }> = []; + const stepCalls: string[] = []; const updateSession = (mutator: (value: Session) => Session | void): Session => { const current = session ? cloneSession(session) : createSession(); session = cloneSession(mutator(current) ?? current); @@ -100,8 +44,8 @@ function createRuntimeHarness(overrides: Partial = {}) { return cloneSession(session); }, updateSession, - markStepStarted: (stepName, options) => { - stepOptionCalls.push({ method: "markStepStarted", options }); + markStepStarted: (stepName) => { + stepCalls.push("markStepStarted"); return updateSession((current) => { const step = current.steps[stepName]; if (!step) return current; @@ -112,13 +56,11 @@ function createRuntimeHarness(overrides: Partial = {}) { current.lastStepStarted = stepName; current.status = "in_progress"; current.failure = null; - const state = STEP_TO_STATE[stepName]; - if (state && shouldUpdateMachine(options)) transitionMachine(current, state); return current; }); }, - markStepComplete: (stepName, updates: SessionUpdates = {}, options) => { - stepOptionCalls.push({ method: "markStepComplete", options }); + markStepComplete: (stepName, updates: SessionUpdates = {}) => { + stepCalls.push("markStepComplete"); return updateSession((current) => { const step = current.steps[stepName]; if (!step) return current; @@ -128,8 +70,6 @@ function createRuntimeHarness(overrides: Partial = {}) { current.lastCompletedStep = stepName; current.failure = null; Object.assign(current, filterSafeUpdates(updates)); - const nextState = nextStateAfterCompletedStep(stepName, current); - if (nextState && shouldUpdateMachine(options)) transitionMachine(current, nextState); return current; }); }, @@ -139,13 +79,11 @@ function createRuntimeHarness(overrides: Partial = {}) { current.steps[stepName].status = "skipped"; return current; }), - markStepFailed: (stepName, message, options) => { - stepOptionCalls.push({ method: "markStepFailed", options }); + markStepFailed: (stepName, message) => { + stepCalls.push("markStepFailed"); return updateSession((current) => { current.steps[stepName].status = "failed"; - current.failure = shouldUpdateMachine(options) - ? { step: stepName, message: message ?? null, recordedAt: "now" } - : current.failure; + current.steps[stepName].error = message ?? null; return current; }); }, @@ -163,7 +101,7 @@ function createRuntimeHarness(overrides: Partial = {}) { return { createRuntime: () => new OnboardRuntime(deps), events, - stepOptionCalls, + stepCalls, getSession: () => cloneSession(session ?? createSession()), }; } @@ -238,7 +176,7 @@ describe("OnboardRuntimeBoundary", () => { ); }); - it("defaults boundary step recorders to record-only machine mutations", async () => { + it("keeps step status writes separate from runtime transitions", async () => { const harness = createRuntimeHarness(); const boundary = new OnboardRuntimeBoundary({ toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, @@ -250,113 +188,28 @@ describe("OnboardRuntimeBoundary", () => { await boundary.recordStepComplete("preflight"); await boundary.recordStepFailed("gateway", "boom"); - expect(harness.stepOptionCalls).toEqual([ - { method: "markStepStarted", options: { updateMachine: false } }, - { method: "markStepComplete", options: { updateMachine: false } }, - { method: "markStepFailed", options: { updateMachine: false } }, - ]); - }); - - it("keeps default boundary step recorders from advancing the machine", async () => { - const harness = createRuntimeHarness(); - const boundary = new OnboardRuntimeBoundary({ - toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, - maybeForceE2eStepFailure: () => undefined, - createRuntime: harness.createRuntime, + expect(harness.stepCalls).toEqual(["markStepStarted", "markStepComplete", "markStepFailed"]); + expect(harness.getSession()).toMatchObject({ + machine: { state: "init", revision: 0 }, + steps: { preflight: { status: "complete" }, gateway: { status: "failed" } }, }); - - await boundary.startRecordedStep("preflight"); - expect(harness.getSession().machine.state).toBe("init"); - await boundary.recordStepComplete("preflight"); - expect(harness.getSession().machine.state).toBe("init"); - - await boundary.recordStateResultWithStepCompatibility( - advanceTo("preflight", { metadata: { state: "init" } }), - ); - expect(harness.getSession().machine.state).toBe("preflight"); }); - it("forwards configured step mutation options through boundary recorders", async () => { + it("applies each explicit transition exactly once", async () => { const harness = createRuntimeHarness(); - const legacyOptions = { updateMachine: true }; const boundary = new OnboardRuntimeBoundary({ toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, maybeForceE2eStepFailure: () => undefined, createRuntime: harness.createRuntime, - stepMutationOptions: legacyOptions, }); - await boundary.startRecordedStep("preflight"); - await boundary.recordStepComplete("preflight"); - await boundary.recordStepFailed("gateway", "boom"); + await boundary.recordStateResult(advanceTo("preflight", { metadata: { state: "init" } })); - expect(harness.stepOptionCalls).toEqual([ - { method: "markStepStarted", options: legacyOptions }, - { method: "markStepComplete", options: legacyOptions }, - { method: "markStepFailed", options: legacyOptions }, - ]); - expect(harness.getSession().machine.state).toBe("gateway"); + expect(harness.getSession().machine).toMatchObject({ state: "preflight", revision: 1 }); + expect(harness.events.map((event) => event.type)).toEqual(["state.exited", "state.entered"]); }); - it("emits diagnostics for legacy-compatible stale state results", async () => { - const harness = createRuntimeHarness(); - const boundary = new OnboardRuntimeBoundary({ - toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, - maybeForceE2eStepFailure: () => undefined, - createRuntime: harness.createRuntime, - stepMutationOptions: { updateMachine: true }, - }); - - await boundary.recordStateResultWithStepCompatibility( - advanceTo("gateway", { metadata: { state: "preflight" } }), - ); - - expect(harness.events).toHaveLength(1); - expect(harness.events[0]).toMatchObject({ - type: "state.result.skipped", - state: "init", - metadata: { - reason: "source_state_mismatch", - currentState: "init", - targetState: "gateway", - sourceState: "preflight", - }, - }); - }); - - it("emits diagnostics for explicit repaired resume invalidated results", async () => { - const harness = createRuntimeHarness(); - const boundary = new OnboardRuntimeBoundary({ - toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, - maybeForceE2eStepFailure: () => undefined, - createRuntime: harness.createRuntime, - }); - const result = advanceTo("gateway", { metadata: { state: "preflight" } }); - - await boundary.recordInvalidatedStateResult(result, { - reason: "source_state_mismatch", - currentState: "init", - sourceState: "preflight", - }); - - expect(harness.events[0]).toMatchObject({ - type: "state.result.invalidated", - metadata: { - reason: "source_state_mismatch", - currentState: "init", - sourceState: "preflight", - targetState: "gateway", - }, - }); - }); - - it.each([ - { label: "pause", result: () => pauseOnboardMachine() }, - { label: "complete", result: () => completeOnboardMachine() }, - { label: "failed", result: () => failOnboardMachine("boom") }, - ] as const)("rejects non-transition $label results before emitting invalidation (#6227)", async ({ - result, - }) => { + it("rejects a stale explicit transition before mutation", async () => { const harness = createRuntimeHarness(); const boundary = new OnboardRuntimeBoundary({ toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, @@ -365,223 +218,12 @@ describe("OnboardRuntimeBoundary", () => { }); await expect( - boundary.recordInvalidatedStateResult(result(), { - reason: "already_at_target", - currentState: "init", - sourceState: "init", - }), - ).rejects.toThrow(/Cannot invalidate non-transition/); - + boundary.recordStateResult(advanceTo("gateway", { metadata: { state: "preflight" } })), + ).rejects.toThrow("Onboarding state result source mismatch: preflight != init"); + expect(harness.getSession().machine).toMatchObject({ state: "init", revision: 0 }); expect(harness.events).toHaveLength(0); }); - it("emits diagnostics for explicit invalidated replay of stale default results", async () => { - const harness = createRuntimeHarness(); - const boundary = new OnboardRuntimeBoundary({ - toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, - maybeForceE2eStepFailure: () => undefined, - createRuntime: harness.createRuntime, - }); - - const result = advanceTo("preflight", { metadata: { state: "missing" } }); - await expect( - boundary.recordInvalidatedStateResult(result, { - reason: "source_state_mismatch", - currentState: "init", - sourceState: "missing", - }), - ).resolves.toMatchObject({ - machine: { state: "init" }, - }); - }); - - it("rejects skipped transition results that carry context updates", async () => { - const harness = createRuntimeHarness(); - const boundary = new OnboardRuntimeBoundary({ - toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, - maybeForceE2eStepFailure: () => undefined, - createRuntime: harness.createRuntime, - stepMutationOptions: { updateMachine: true }, - }); - - await expect( - boundary.recordStateResultWithStepCompatibility( - advanceTo("preflight", { metadata: { state: "missing" }, updates: { provider: "nvidia" } }), - ), - ).rejects.toThrow("Cannot skip onboarding state result with context updates"); - }); - - it("allows skipped transition results whose updates are all undefined", async () => { - const harness = createRuntimeHarness(); - const boundary = new OnboardRuntimeBoundary({ - toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, - maybeForceE2eStepFailure: () => undefined, - createRuntime: harness.createRuntime, - stepMutationOptions: { updateMachine: true }, - }); - - const session = await boundary.recordStateResultWithStepCompatibility( - advanceTo("preflight", { - metadata: { state: "missing" }, - updates: { provider: undefined, model: undefined }, - }), - ); - - expect(session.machine.state).toBe("init"); - expect(harness.events[0]).toMatchObject({ - type: "state.result.skipped", - state: "init", - metadata: { - reason: "source_state_mismatch", - currentState: "init", - targetState: "preflight", - sourceState: "missing", - }, - }); - }); - - it("records live legacy step/result compatibility through provider retry and finalization", async () => { - const harness = createRuntimeHarness(); - const boundary = new OnboardRuntimeBoundary({ - toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, - maybeForceE2eStepFailure: () => undefined, - createRuntime: harness.createRuntime, - stepMutationOptions: { updateMachine: true }, - }); - - await boundary.startRecordedStep("preflight"); - await boundary.recordStepComplete("preflight"); - await boundary.recordStateResultWithStepCompatibility( - advanceTo("gateway", { metadata: { state: "preflight" } }), - ); - - await boundary.startRecordedStep("gateway"); - await boundary.recordStepComplete("gateway"); - await boundary.recordStateResultWithStepCompatibility( - advanceTo("provider_selection", { metadata: { state: "gateway" } }), - ); - - await boundary.startRecordedStep("provider_selection"); - await boundary.recordStepComplete("provider_selection", { - provider: "bad", - model: "bad-model", - }); - await boundary.startRecordedStep("inference", { provider: "bad", model: "bad-model" }); - const retryResult = retryTo("provider_selection", { - metadata: { - state: "inference", - provider: "bad", - model: "bad-model", - reason: "selection_retry", - }, - }); - await boundary.startRecordedStep("provider_selection"); - await boundary.recordStepComplete("provider_selection", { - provider: "nvidia", - model: "nemotron", - }); - await boundary.startRecordedStep("inference", { provider: "nvidia", model: "nemotron" }); - await boundary.recordStepComplete("inference", { provider: "nvidia", model: "nemotron" }); - await boundary.recordStateResultWithStepCompatibility(retryResult); - await boundary.recordStateResultWithStepCompatibility( - advanceTo("sandbox", { - metadata: { state: "inference", provider: "nvidia", model: "nemotron" }, - }), - ); - - await boundary.startRecordedStep("sandbox"); - await boundary.recordStepComplete("sandbox", { sandboxName: "openclaw-sb" }); - await boundary.recordStateResultWithStepCompatibility( - branchTo("openclaw", { - metadata: { state: "sandbox", sandboxName: "openclaw-sb", agent: "openclaw" }, - }), - ); - - await boundary.startRecordedStep("openclaw"); - await boundary.recordStepComplete("openclaw"); - await boundary.recordStateResultWithStepCompatibility( - advanceTo("policies", { metadata: { state: "openclaw" } }), - ); - - await boundary.startRecordedStep("policies"); - await boundary.recordStepComplete("policies", { policyPresets: ["github"] }); - await boundary.recordStateResultWithStepCompatibility( - advanceTo("finalizing", { metadata: { state: "policies" } }), - ); - - await boundary.recordStateResultWithStepCompatibility( - advanceTo("post_verify", { metadata: { state: "finalizing" } }), - ); - const completed = await boundary.recordStateResultWithStepCompatibility( - completeOnboardMachine( - { sandboxName: "openclaw-sb", provider: "nvidia", model: "nemotron" }, - { state: "post_verify" }, - ), - ); - - const skipped = harness.events.filter((event) => event.type === "state.result.skipped"); - expect(skipped.map((event) => event.metadata.targetState)).toEqual([ - "gateway", - "provider_selection", - "provider_selection", - "sandbox", - "openclaw", - "policies", - "finalizing", - ]); - expect(skipped[2]).toMatchObject({ - state: "sandbox", - metadata: { - reason: "source_state_mismatch", - currentState: "sandbox", - sourceState: "inference", - targetState: "provider_selection", - }, - }); - expect(completed).toMatchObject({ - status: "complete", - machine: { state: "complete" }, - sandboxName: "openclaw-sb", - provider: "nvidia", - model: "nemotron", - }); - }); - - it("records sandbox branch compatibility for agent setup sandboxes", async () => { - const harness = createRuntimeHarness({ agent: "hermes" }); - const boundary = new OnboardRuntimeBoundary({ - toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, - maybeForceE2eStepFailure: () => undefined, - createRuntime: harness.createRuntime, - stepMutationOptions: { updateMachine: true }, - }); - - await boundary.recordStateResult(advanceTo("preflight")); - await boundary.recordStateResult(advanceTo("gateway")); - await boundary.recordStateResult(advanceTo("provider_selection")); - await boundary.recordStateResult(advanceTo("inference")); - await boundary.recordStateResult(advanceTo("sandbox")); - await boundary.startRecordedStep("sandbox"); - await boundary.recordStepComplete("sandbox", { sandboxName: "hermes-sb" }); - - const session = await boundary.recordStateResultWithStepCompatibility( - branchTo("agent_setup", { - metadata: { state: "sandbox", sandboxName: "hermes-sb", agent: "hermes" }, - }), - ); - - expect(session.machine.state).toBe("agent_setup"); - expect(harness.events.at(-1)).toMatchObject({ - type: "state.result.skipped", - state: "agent_setup", - metadata: { - reason: "already_at_target", - currentState: "agent_setup", - targetState: "agent_setup", - }, - }); - }); - it("records resume conflict diagnostics through the runtime", async () => { const harness = createRuntimeHarness(); const boundary = new OnboardRuntimeBoundary({ @@ -603,111 +245,3 @@ describe("OnboardRuntimeBoundary", () => { }); }); }); - -describe("onboarding interrupted while a step is in flight (#7982)", () => { - function failedAtSandbox(interrupted: boolean) { - const harness = createRuntimeHarness({ - status: "failed", - lastStepStarted: "sandbox", - failure: { - step: "sandbox", - message: interrupted - ? "Onboarding exited before the step completed." - : "Rebuild recreate failed", - recordedAt: "2026-05-27T00:00:00.000Z", - interrupted, - }, - machine: { - version: 1, - state: "failed", - stateEnteredAt: "2026-05-27T00:00:00.000Z", - revision: 5, - }, - }); - const boundary = new OnboardRuntimeBoundary({ - toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, - maybeForceE2eStepFailure: () => undefined, - createRuntime: harness.createRuntime, - }); - return { boundary, harness }; - } - - function interruptedAtSandbox() { - return failedAtSandbox(true); - } - - it("reports the sandbox branch as interrupted rather than an invalid transition", async () => { - const { boundary, harness } = interruptedAtSandbox(); - - const applied = boundary.recordStateResult( - branchTo("openclaw", { metadata: { state: "sandbox" } }), - ); - - await expect(applied).rejects.toThrow(OnboardInterruptedError); - await expect(applied).rejects.not.toThrow(InvalidOnboardMachineTransitionError); - await expect(applied).rejects.toThrow(/during the sandbox step/); - await expect(applied).rejects.toThrow(/continue to openclaw/); - expect(harness.getSession().machine).toMatchObject({ state: "failed", revision: 5 }); - expect(harness.events).toHaveLength(0); - }); - - it("reports an interrupt recorded by another process without an in-process latch", async () => { - const { boundary, harness } = interruptedAtSandbox(); - - const applied = boundary.recordStateResult( - branchTo("openclaw", { metadata: { state: "sandbox" } }), - ); - - await expect(applied).rejects.toThrow(OnboardInterruptedError); - await expect(applied).rejects.toThrow(/during the sandbox step/); - expect(harness.getSession().machine).toMatchObject({ state: "failed", revision: 5 }); - }); - - it("reports an ordinary failed session as an invalid transition, not an interrupt", async () => { - const { boundary, harness } = failedAtSandbox(false); - - const applied = boundary.recordStateResult( - branchTo("openclaw", { metadata: { state: "sandbox" } }), - ); - - await expect(applied).rejects.toThrow(InvalidOnboardMachineTransitionError); - await expect(applied).rejects.not.toThrow(OnboardInterruptedError); - expect(harness.getSession().machine).toMatchObject({ state: "failed", revision: 5 }); - }); - - it("rejects completing an interrupted session through recordSessionComplete", async () => { - const { boundary, harness } = interruptedAtSandbox(); - - await expect(boundary.recordSessionComplete()).rejects.toThrow(OnboardInterruptedError); - expect(harness.getSession().machine).toMatchObject({ state: "failed", revision: 5 }); - }); - - it("reports an interrupted finalization rather than an invalid completion", async () => { - const { boundary } = interruptedAtSandbox(); - - await expect(boundary.recordStateResult(completeOnboardMachine())).rejects.toThrow( - OnboardInterruptedError, - ); - }); - - it("still applies a legal branch when no interrupt was recorded", async () => { - const harness = createRuntimeHarness({ - lastStepStarted: "sandbox", - machine: { - version: 1, - state: "sandbox", - stateEnteredAt: "2026-05-27T00:00:00.000Z", - revision: 5, - }, - }); - const boundary = new OnboardRuntimeBoundary({ - toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, - maybeForceE2eStepFailure: () => undefined, - createRuntime: harness.createRuntime, - }); - - await boundary.recordStateResult(branchTo("openclaw", { metadata: { state: "sandbox" } })); - - expect(harness.getSession().machine).toMatchObject({ state: "openclaw", revision: 6 }); - }); -}); diff --git a/src/lib/onboard/runtime-boundary.ts b/src/lib/onboard/runtime-boundary.ts index cbd3f9062a3..c66cea33270 100644 --- a/src/lib/onboard/runtime-boundary.ts +++ b/src/lib/onboard/runtime-boundary.ts @@ -2,17 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 import type { Session, SessionUpdates } from "../state/onboard-session"; -import { - RECORD_ONLY_STEP_MUTATION_OPTIONS, - type StepMutationOptions, - shouldUpdateMachine, -} from "../state/onboard-step-mutation"; import type { OnboardStateFailedResult, OnboardStateResult } from "./machine/result"; import { OnboardRuntime } from "./machine/runtime"; -import { - assertOnboardNotInterrupted, - assertValidOnboardMachineTransition, -} from "./machine/transitions"; import type { OnboardMachineEventType, OnboardMachineState } from "./machine/types"; import type { ResumeConfigConflict } from "./resume-config"; @@ -30,12 +21,6 @@ export interface OnboardRuntimeBoundaryOptions { toSessionUpdates(updates: Record): SessionUpdates; maybeForceE2eStepFailure(stepName: string): void; createRuntime?(): OnboardRuntime; - /** - * Override for legacy/test harnesses. Production boundary writes default to - * status-only step mutations so explicit OnboardStateResult transitions stay - * the durable machine source of truth. - */ - stepMutationOptions?: StepMutationOptions; } export class OnboardRuntimeBoundary { @@ -91,7 +76,7 @@ export class OnboardRuntimeBoundary { } = {}, ): Promise { const runtime = this.getRuntime(); - await runtime.markStepStarted(stepName, this.stepMutationOptions()); + await runtime.markStepStarted(stepName); if (Object.keys(updates).length > 0) { await runtime.updateContext(this.options.toSessionUpdates(updates)); } @@ -99,7 +84,7 @@ export class OnboardRuntimeBoundary { } async recordStepComplete(stepName: string, updates: SessionUpdates = {}): Promise { - return this.getRuntime().markStepComplete(stepName, updates, this.stepMutationOptions()); + return this.getRuntime().markStepComplete(stepName, updates); } async recordStepSkipped(stepName: string): Promise { @@ -107,15 +92,7 @@ export class OnboardRuntimeBoundary { } async recordStepFailed(stepName: string, message: string | null): Promise { - return this.getRuntime().markStepFailed(stepName, message, this.stepMutationOptions()); - } - - private stepMutationOptions(): StepMutationOptions { - return this.options.stepMutationOptions ?? RECORD_ONLY_STEP_MUTATION_OPTIONS; - } - - private usesLegacyMachineStepMutation(): boolean { - return shouldUpdateMachine(this.stepMutationOptions()); + return this.getRuntime().markStepFailed(stepName, message); } async recordStateSkipped( @@ -129,54 +106,12 @@ export class OnboardRuntimeBoundary { return this.getRuntime().applyResult(result); } - private async assertStateResultWillApply(result: OnboardStateResult): Promise { - const current = await this.getRuntime().session(); - if (result.type === "failed") { - assertValidOnboardMachineTransition(current.machine.state, "failed"); - return; - } - if (result.type === "complete") { - assertOnboardNotInterrupted(current.machine.state, "complete", current.failure); - assertValidOnboardMachineTransition(current.machine.state, "complete"); - return; - } - - if (result.type === "pause") { - const sourceState = - result.metadata && typeof result.metadata.state === "string" ? result.metadata.state : null; - if (sourceState && current.machine.state !== sourceState) { - throw new Error( - `Paused onboarding state result source mismatch: ${sourceState} != ${current.machine.state}`, - ); - } - return; - } - - assertOnboardNotInterrupted(current.machine.state, result.next, current.failure); - const sourceState = - result.metadata && typeof result.metadata.state === "string" ? result.metadata.state : null; - if (current.machine.state === result.next) { - throw new Error(`Record-only step result already reached target state: ${result.next}`); - } - if (sourceState && current.machine.state !== sourceState) { - throw new Error( - `Record-only step result source mismatch: ${sourceState} != ${current.machine.state}`, - ); - } - const transition = assertValidOnboardMachineTransition(current.machine.state, result.next); - if (result.transitionKind && transition.kind !== result.transitionKind) { - throw new Error( - `Invalid onboarding machine transition kind: ${current.machine.state} -> ${result.next} expected ${result.transitionKind}, got ${transition.kind}`, - ); - } - } - async recordStepCompleteWithStateResult( stepName: string, updates: SessionUpdates, result: OnboardStateResult, ): Promise { - await this.assertStateResultWillApply(result); + await this.getRuntime().assertResultWillApply(result); await this.getRuntime().markStepCompleteRecordOnly(stepName, updates); return this.recordStateResult(result); } @@ -186,56 +121,11 @@ export class OnboardRuntimeBoundary { message: string | null, result: OnboardStateFailedResult, ): Promise { - await this.assertStateResultWillApply(result); + await this.getRuntime().assertResultWillApply(result); await this.getRuntime().markStepFailedRecordOnly(stepName, message); return this.recordStateResult(result); } - /** - * Compatibility bridge for legacy/test boundaries explicitly configured with - * `updateMachine === true`. - * - * Default record-only paths reject stale transition results before applying - * them. Skipped legacy results must stay metadata-only so stale results cannot - * become a context source. Live resume replay uses - * `recordInvalidatedStateResult` instead so recomputed-but-stale results are - * explicitly invalidated rather than accepted through this compatibility path. - */ - async recordStateResultWithStepCompatibility(result: OnboardStateResult): Promise { - const runtime = this.getRuntime(); - const current = await runtime.session(); - if (result.type !== "transition") return runtime.applyResult(result); - - if (!this.usesLegacyMachineStepMutation()) { - await this.assertStateResultWillApply(result); - return runtime.applyResult(result); - } - - if (current.machine.state === result.next) { - assertResultHasNoContextUpdates(result, "skip"); - return runtime.emitResultSkipped({ - reason: "already_at_target", - currentState: current.machine.state, - targetState: result.next, - metadata: result.metadata, - }); - } - - const sourceState = - result.metadata && typeof result.metadata.state === "string" ? result.metadata.state : null; - if (sourceState && current.machine.state !== sourceState) { - assertResultHasNoContextUpdates(result, "skip"); - return runtime.emitResultSkipped({ - reason: "source_state_mismatch", - currentState: current.machine.state, - targetState: result.next, - metadata: { ...(result.metadata ?? {}), sourceState }, - }); - } - - return runtime.applyResult(result); - } - async recordInvalidatedStateResult( result: OnboardStateResult, options: { @@ -278,14 +168,10 @@ export class OnboardRuntimeBoundary { async recordSessionComplete(updates: SessionUpdates = {}): Promise { const runtime = this.getRuntime(); const current = await runtime.session(); - assertOnboardNotInterrupted(current.machine.state, "complete", current.failure); if (current.machine.state === "finalizing") { await runtime.transition("post_verify"); return runtime.complete(updates); } - if (current.machine.state === "post_verify") { - return runtime.complete(updates); - } - return runtime.completeSession(updates); + return runtime.complete(updates); } } diff --git a/src/lib/state/onboard-session-reasoning-effort.test.ts b/src/lib/state/onboard-session-reasoning-effort.test.ts index 74d469c510d..62f78b9a431 100644 --- a/src/lib/state/onboard-session-reasoning-effort.test.ts +++ b/src/lib/state/onboard-session-reasoning-effort.test.ts @@ -6,13 +6,9 @@ import os from "node:os"; import path from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import { markStepCompleteLegacy } from "../../../test/helpers/onboard-legacy-step-mutation"; - type OnboardSessionModule = typeof import("./onboard-session"); -type OnboardStepMutationModule = typeof import("./onboard-step-mutation"); type LoadedSession = NonNullable>; let session: OnboardSessionModule; -let stepMutation: OnboardStepMutationModule; let tmpDir: string; function requireLoadedSession( @@ -27,7 +23,6 @@ beforeEach(async () => { vi.stubEnv("HOME", tmpDir); vi.resetModules(); session = await import("./onboard-session"); - stepMutation = await import("./onboard-step-mutation"); session.clearSession(); session.releaseOnboardLock(); }); @@ -43,19 +38,19 @@ afterEach(() => { describe("onboard session reasoning effort", () => { it("round-trips only valid compatible-endpoint reasoning effort updates", () => { session.saveSession(session.createSession()); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { compatibleEndpointReasoningEffort: "high", }); let loaded = requireLoadedSession(session.loadSession()); expect(loaded.compatibleEndpointReasoningEffort).toBe("high"); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { compatibleEndpointReasoningEffort: "extreme", } as never); loaded = requireLoadedSession(session.loadSession()); expect(loaded.compatibleEndpointReasoningEffort).toBe("high"); - markStepCompleteLegacy(session, stepMutation, "provider_selection", { + session.markStepComplete("provider_selection", { compatibleEndpointReasoningEffort: null, }); expect( diff --git a/src/lib/state/onboard-session.ts b/src/lib/state/onboard-session.ts index fbc240a5466..340c85f32b0 100644 --- a/src/lib/state/onboard-session.ts +++ b/src/lib/state/onboard-session.ts @@ -49,11 +49,6 @@ import { preserveInvalidSessionToolDisclosure, type ToolDisclosure, } from "./onboard-session-tool-disclosure"; -import { - RECORD_ONLY_STEP_MUTATION_OPTIONS, - type StepMutationOptions, - shouldUpdateMachine, -} from "./onboard-step-mutation"; import { nextMachineStateAfterCompletedStep } from "./onboard-step-state"; import { nemoclawStateRoot } from "./state-root"; @@ -1382,11 +1377,7 @@ export function updateSession(mutator: (session: Session) => Session | void): Se return saveSession(next); } -function markStepStartedWithOptions( - stepName: string, - options: StepMutationOptions = RECORD_ONLY_STEP_MUTATION_OPTIONS, -): Session { - let shouldEmit = false; +export function markStepStarted(stepName: string): Session { const updatedSession = updateSession((session) => { const step = session.steps[stepName]; if (!step) return session; @@ -1398,28 +1389,18 @@ function markStepStartedWithOptions( session.lastStepStarted = stepName; session.failure = null; session.status = "in_progress"; - const state = machineStateFromOnboardSessionStep(stepName); - shouldEmit = Boolean(state && shouldUpdateMachine(options)); - if (state && shouldEmit) transitionMachineSnapshot(session, state, now); return session; }); - if (shouldEmit) { - emitOnboardMachineEvent( - createOnboardMachineEvent({ type: "state.entered", session: updatedSession, step: stepName }), - ); - } return updatedSession; } -function markStepCompleteWithOptions( - stepName: string, - updates: SessionUpdates = {}, - options: StepMutationOptions = RECORD_ONLY_STEP_MUTATION_OPTIONS, -): Session { +export function markStepStartedRecordOnly(stepName: string): Session { + return markStepStarted(stepName); +} + +export function markStepComplete(stepName: string, updates: SessionUpdates = {}): Session { const safeUpdates = filterSafeUpdates(updates); - const hasUpdates = Object.keys(safeUpdates).length > 0; - let shouldEmit = false; - const updatedSession = updateSession((session) => { + return updateSession((session) => { const step = session.steps[stepName]; if (!step) return session; // Spark managed-vLLM Express intents (#7231) carry no receipt/served state @@ -1445,62 +1426,19 @@ function markStepCompleteWithOptions( Object.assign(session, safeUpdates); if (stationExpressIntent) session.stationExpressIntent = stationExpressIntent; else if (sparkExpressComplete) session.stationExpressIntent = null; - const nextState = nextMachineStateAfterCompletedStep(stepName, session); - shouldEmit = Boolean(nextState && shouldUpdateMachine(options)); - if (nextState && shouldEmit) transitionMachineSnapshot(session, nextState, now); return session; }); - if (hasUpdates) { - emitOnboardMachineEvent( - createOnboardMachineEvent({ - type: "context.updated", - session: updatedSession, - step: stepName, - metadata: { fields: Object.keys(safeUpdates) }, - }), - ); - } - if (shouldEmit) { - emitOnboardMachineEvent( - createOnboardMachineEvent({ - type: "state.completed", - session: updatedSession, - step: stepName, - }), - ); - } - return updatedSession; -} - -export function markStepStarted( - stepName: string, - options: StepMutationOptions = RECORD_ONLY_STEP_MUTATION_OPTIONS, -): Session { - return markStepStartedWithOptions(stepName, options); -} - -export function markStepStartedRecordOnly(stepName: string): Session { - return markStepStartedWithOptions(stepName, RECORD_ONLY_STEP_MUTATION_OPTIONS); -} - -export function markStepComplete( - stepName: string, - updates: SessionUpdates = {}, - options: StepMutationOptions = RECORD_ONLY_STEP_MUTATION_OPTIONS, -): Session { - return markStepCompleteWithOptions(stepName, updates, options); } export function markStepCompleteRecordOnly( stepName: string, updates: SessionUpdates = {}, ): Session { - return markStepCompleteWithOptions(stepName, updates, RECORD_ONLY_STEP_MUTATION_OPTIONS); + return markStepComplete(stepName, updates); } export function markStepSkipped(stepName: string): Session { - let shouldEmit = false; - const updatedSession = updateSession((session) => { + return updateSession((session) => { const step = session.steps[stepName]; if (!step) return session; if (step.status === "complete" || step.status === "failed" || step.status === "skipped") @@ -1509,75 +1447,23 @@ export function markStepSkipped(stepName: string): Session { step.startedAt = null; step.completedAt = null; step.error = null; - shouldEmit = true; return session; }); - if (shouldEmit) { - emitOnboardMachineEvent( - createOnboardMachineEvent({ type: "state.skipped", session: updatedSession, step: stepName }), - ); - } - return updatedSession; } -function markStepFailedWithOptions( - stepName: string, - message: string | null = null, - options: StepMutationOptions = RECORD_ONLY_STEP_MUTATION_OPTIONS, -): Session { - let shouldEmit = false; - const updatedSession = updateSession((session) => { +export function markStepFailed(stepName: string, message: string | null = null): Session { + return updateSession((session) => { const step = session.steps[stepName]; if (!step) return session; - const now = new Date().toISOString(); step.status = "failed"; step.completedAt = null; step.error = redactSensitiveText(message); - shouldEmit = shouldUpdateMachine(options); - if (shouldEmit) { - session.failure = sanitizeFailure({ - step: stepName, - message, - recordedAt: now, - interrupted: false, - }); - session.status = "failed"; - transitionMachineSnapshot(session, "failed", now); - } return session; }); - if (shouldEmit) { - emitOnboardMachineEvent( - createOnboardMachineEvent({ - type: "state.failed", - session: updatedSession, - step: stepName, - error: message, - }), - ); - emitOnboardMachineEvent( - createOnboardMachineEvent({ - type: "onboard.failed", - session: updatedSession, - state: "failed", - step: stepName, - error: message, - }), - ); - } - return updatedSession; -} - -export function markStepFailed( - stepName: string, - message: string | null = null, - options: StepMutationOptions = RECORD_ONLY_STEP_MUTATION_OPTIONS, -): Session { - return markStepFailedWithOptions(stepName, message, options); } export function markStepFailedRecordOnly(stepName: string, message: string | null = null): Session { - return markStepFailedWithOptions(stepName, message, RECORD_ONLY_STEP_MUTATION_OPTIONS); + return markStepFailed(stepName, message); } /** From dc1ccb3c6c769fcb9deabfa6f8322d2080f2b1e3 Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Mon, 3 Aug 2026 00:17:40 -0700 Subject: [PATCH 07/19] test(onboard): allow gateway reconcile coverage slack --- test/gateway-state-reconcile-2276.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/gateway-state-reconcile-2276.test.ts b/test/gateway-state-reconcile-2276.test.ts index 72adb6ce150..44436b73a74 100644 --- a/test/gateway-state-reconcile-2276.test.ts +++ b/test/gateway-state-reconcile-2276.test.ts @@ -18,7 +18,7 @@ import path from "node:path"; import { afterEach, beforeEach, describe, it } from "vitest"; import { testTimeout } from "./helpers/timeouts"; -const TIMEOUT_MS = testTimeout(20_000); +const TIMEOUT_MS = testTimeout(60_000); const SANDBOX_NAME = "my-assistant"; // Output fixtures that mirror real OpenShell CLI output. From ca084eff201d033df762d9cf024b7db04e18ee70 Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Mon, 3 Aug 2026 00:37:00 -0700 Subject: [PATCH 08/19] refactor(onboard): remove legacy step mutation API --- ci/source-architecture-budget.json | 2 +- src/lib/state/onboard-step-mutation.test.ts | 54 --------------------- src/lib/state/onboard-step-mutation.ts | 25 ---------- 3 files changed, 1 insertion(+), 80 deletions(-) delete mode 100644 src/lib/state/onboard-step-mutation.test.ts delete mode 100644 src/lib/state/onboard-step-mutation.ts diff --git a/ci/source-architecture-budget.json b/ci/source-architecture-budget.json index 8d1def477b4..783ffcf3e2f 100644 --- a/ci/source-architecture-budget.json +++ b/ci/source-architecture-budget.json @@ -58,7 +58,7 @@ "src/lib/onboard": 310, "src/lib/actions": 19, "src/lib/actions/sandbox": 183, - "src/lib/state": 38, + "src/lib/state": 37, "src/lib/inference": 62, "scripts": 44 } diff --git a/src/lib/state/onboard-step-mutation.test.ts b/src/lib/state/onboard-step-mutation.test.ts deleted file mode 100644 index 4864b221501..00000000000 --- a/src/lib/state/onboard-step-mutation.test.ts +++ /dev/null @@ -1,54 +0,0 @@ -// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -import fs from "node:fs"; -import os from "node:os"; -import path from "node:path"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; - -import type * as sessionModule from "./onboard-session"; - -const originalHome = process.env.HOME; -let session: typeof sessionModule; -let tmpDir: string; - -beforeEach(async () => { - tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-onboard-step-status-")); - process.env.HOME = tmpDir; - vi.resetModules(); - session = await import("./onboard-session"); - session.clearSession(); - session.releaseOnboardLock(); -}); - -afterEach(() => { - fs.rmSync(tmpDir, { recursive: true, force: true }); - if (originalHome === undefined) delete process.env.HOME; - else process.env.HOME = originalHome; -}); - -describe("onboard step status", () => { - it("persists status and safe updates without changing the machine snapshot", () => { - session.saveSession(session.createSession()); - - session.markStepStarted("preflight"); - session.markStepComplete("preflight", { sandboxName: "my-assistant" }); - session.markStepFailed("gateway", "Gateway failed: NVIDIA_INFERENCE_API_KEY=nvapi-secret"); - - const loaded = session.loadSession(); - expect(loaded).not.toBeNull(); - expect(loaded).toMatchObject({ - sandboxName: "my-assistant", - status: "in_progress", - failure: null, - machine: { state: "init", revision: 0 }, - steps: { - preflight: { status: "complete" }, - gateway: { - status: "failed", - error: "Gateway failed: NVIDIA_INFERENCE_API_KEY=", - }, - }, - }); - }); -}); diff --git a/src/lib/state/onboard-step-mutation.ts b/src/lib/state/onboard-step-mutation.ts deleted file mode 100644 index 406fb5c98bb..00000000000 --- a/src/lib/state/onboard-step-mutation.ts +++ /dev/null @@ -1,25 +0,0 @@ -// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -export interface StepMutationOptions { - /** - * Transitional FSM migration escape hatch for paths where legacy step helpers - * still move session.machine. Runtime-owned onboarding should use record-only - * step writes paired with explicit OnboardStateResult transitions so the - * runtime remains the durable machine source of truth. Remove this option once - * direct step helpers no longer own machine transitions. - */ - updateMachine?: boolean; -} - -export const LEGACY_MACHINE_STEP_MUTATION_OPTIONS: Readonly = Object.freeze({ - updateMachine: true, -}); - -export const RECORD_ONLY_STEP_MUTATION_OPTIONS: Readonly = Object.freeze({ - updateMachine: false, -}); - -export function shouldUpdateMachine(options: StepMutationOptions | undefined): boolean { - return options?.updateMachine !== false; -} From 8fed8a76d90cb22536f7efea5770b27d9dfd88a7 Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Mon, 3 Aug 2026 00:54:10 -0700 Subject: [PATCH 09/19] refactor(onboard): remove duplicate step mutation API --- ci/source-shape-test-budget.json | 5 +++ .../agent-policy-finalization.test.ts | 11 ++----- src/lib/onboard/machine/flow-sequence.test.ts | 11 ++----- src/lib/onboard/machine/flow-slices.test.ts | 3 -- .../onboard/machine/runner-sequence.test.ts | 13 ++------ src/lib/onboard/machine/runner.test.ts | 13 ++------ src/lib/onboard/machine/runtime.test.ts | 26 +++------------- src/lib/onboard/machine/runtime.ts | 18 ----------- .../onboard/machine/sequence-runner.test.ts | 11 ++----- .../onboard/machine/transition-traces.test.ts | 7 ++--- src/lib/onboard/resume-machine-repair.test.ts | 15 ++++----- ...s => runtime-boundary-step-result.test.ts} | 28 +++++------------ src/lib/onboard/runtime-boundary.test.ts | 2 -- src/lib/onboard/runtime-boundary.ts | 4 +-- src/lib/state/onboard-session.ts | 15 --------- test/helpers/onboard-final-flow-phases.ts | 3 -- test/source-architecture.test.ts | 31 +++++++++++++++++++ 17 files changed, 69 insertions(+), 147 deletions(-) rename src/lib/onboard/{runtime-boundary-record-only.test.ts => runtime-boundary-step-result.test.ts} (85%) diff --git a/ci/source-shape-test-budget.json b/ci/source-shape-test-budget.json index b9c46c21eb8..66de06b294f 100644 --- a/ci/source-shape-test-budget.json +++ b/ci/source-shape-test-budget.json @@ -516,6 +516,11 @@ "test": "keeps migrated provider identities and implementations behind the one bundle composition", "category": "compatibility" }, + { + "file": "test/source-architecture.test.ts", + "test": "keeps removed step mutation APIs out of production source (#7703)", + "category": "compatibility" + }, { "file": "test/pr-limit-policy.test.ts", "test": "keeps contributor guidance aligned with the enforced maintainer exemption", diff --git a/src/lib/onboard/machine/flow-phases/agent-policy-finalization.test.ts b/src/lib/onboard/machine/flow-phases/agent-policy-finalization.test.ts index f5ad29e38a6..6a858175469 100644 --- a/src/lib/onboard/machine/flow-phases/agent-policy-finalization.test.ts +++ b/src/lib/onboard/machine/flow-phases/agent-policy-finalization.test.ts @@ -10,7 +10,6 @@ import { normalizeSession, type Session, type SessionUpdates, - sanitizeFailure, } from "../../../state/onboard-session"; import type { OnboardFlowContext } from "../flow-context"; import { advanceTo, completeOnboardMachine } from "../result"; @@ -77,19 +76,13 @@ function createRuntime(initialSession: Session = createSession()) { Object.assign(current, filterSafeUpdates(updates)); return current; }), - markStepCompleteRecordOnly: (_stepName, updates: SessionUpdates = {}) => - updateSession((current) => { - Object.assign(current, filterSafeUpdates(updates)); - return current; - }), markStepSkipped: () => cloneSession(session), markStepFailed: (stepName, message) => updateSession((current) => { - current.status = "failed"; - current.failure = sanitizeFailure({ step: stepName, message, recordedAt: "now" }); + current.steps[stepName].status = "failed"; + current.steps[stepName].error = message ?? null; return current; }), - markStepFailedRecordOnly: () => cloneSession(session), completeSession: (updates: SessionUpdates = {}) => updateSession((current) => { Object.assign(current, filterSafeUpdates(updates)); diff --git a/src/lib/onboard/machine/flow-sequence.test.ts b/src/lib/onboard/machine/flow-sequence.test.ts index 9ff28f3baa4..a0e8d6c763e 100644 --- a/src/lib/onboard/machine/flow-sequence.test.ts +++ b/src/lib/onboard/machine/flow-sequence.test.ts @@ -10,7 +10,6 @@ import { normalizeSession, type Session, type SessionUpdates, - sanitizeFailure, } from "../../state/onboard-session"; import type { OnboardFlowContext, OnboardFlowPhaseResult } from "./flow-context"; import { onboardFlowPhaseResult } from "./flow-context"; @@ -82,19 +81,13 @@ function createRuntime(initialSession: Session = createSession()) { Object.assign(current, filterSafeUpdates(updates)); return current; }), - markStepCompleteRecordOnly: (_stepName, updates: SessionUpdates = {}) => - updateSession((current) => { - Object.assign(current, filterSafeUpdates(updates)); - return current; - }), markStepSkipped: () => cloneSession(session), markStepFailed: (stepName, message) => updateSession((current) => { - current.status = "failed"; - current.failure = sanitizeFailure({ step: stepName, message, recordedAt: "now" }); + current.steps[stepName].status = "failed"; + current.steps[stepName].error = message ?? null; return current; }), - markStepFailedRecordOnly: () => cloneSession(session), completeSession: (updates: SessionUpdates = {}) => updateSession((current) => { Object.assign(current, filterSafeUpdates(updates)); diff --git a/src/lib/onboard/machine/flow-slices.test.ts b/src/lib/onboard/machine/flow-slices.test.ts index e7e65909d3d..b4b4cafb55a 100644 --- a/src/lib/onboard/machine/flow-slices.test.ts +++ b/src/lib/onboard/machine/flow-slices.test.ts @@ -45,11 +45,8 @@ function runtime(initialSession: Session = createSession()) { markStepStarted: () => cloneSession(session), markStepComplete: (_stepName, updates: SessionUpdates = {}) => updateSession((current) => Object.assign(current, filterSafeUpdates(updates))), - markStepCompleteRecordOnly: (_stepName, updates: SessionUpdates = {}) => - updateSession((current) => Object.assign(current, filterSafeUpdates(updates))), markStepSkipped: () => cloneSession(session), markStepFailed: () => cloneSession(session), - markStepFailedRecordOnly: () => cloneSession(session), completeSession: () => cloneSession(session), filterSafeUpdates, emitEvent: () => undefined, diff --git a/src/lib/onboard/machine/runner-sequence.test.ts b/src/lib/onboard/machine/runner-sequence.test.ts index 031c5fa3bbb..97837bbed3b 100644 --- a/src/lib/onboard/machine/runner-sequence.test.ts +++ b/src/lib/onboard/machine/runner-sequence.test.ts @@ -10,7 +10,6 @@ import { normalizeSession, type Session, type SessionUpdates, - sanitizeFailure, } from "../../state/onboard-session"; import { advanceTo, branchTo, completeOnboardMachine, failOnboardMachine, retryTo } from "./result"; import { @@ -53,19 +52,13 @@ function createRuntime(initialSession: Session = createSession()) { Object.assign(current, filterSafeUpdates(updates)); return current; }), - markStepCompleteRecordOnly: (_stepName, updates: SessionUpdates = {}) => - updateSession((current) => { - Object.assign(current, filterSafeUpdates(updates)); - return current; - }), markStepSkipped: () => cloneSession(session), - markStepFailed: (_stepName, message) => + markStepFailed: (stepName, message) => updateSession((current) => { - current.status = "failed"; - current.failure = sanitizeFailure({ step: _stepName, message, recordedAt: "now" }); + current.steps[stepName].status = "failed"; + current.steps[stepName].error = message ?? null; return current; }), - markStepFailedRecordOnly: () => cloneSession(session), completeSession: (updates: SessionUpdates = {}) => updateSession((current) => { Object.assign(current, filterSafeUpdates(updates)); diff --git a/src/lib/onboard/machine/runner.test.ts b/src/lib/onboard/machine/runner.test.ts index d749b0cc678..f520f480b59 100644 --- a/src/lib/onboard/machine/runner.test.ts +++ b/src/lib/onboard/machine/runner.test.ts @@ -10,7 +10,6 @@ import { normalizeSession, type Session, type SessionUpdates, - sanitizeFailure, } from "../../state/onboard-session"; import { advanceTo, @@ -58,19 +57,13 @@ function createRuntime(initialSession: Session = createSession()) { Object.assign(current, filterSafeUpdates(updates)); return current; }), - markStepCompleteRecordOnly: (_stepName, updates: SessionUpdates = {}) => - updateSession((current) => { - Object.assign(current, filterSafeUpdates(updates)); - return current; - }), markStepSkipped: () => cloneSession(session), - markStepFailed: (_stepName, message) => + markStepFailed: (stepName, message) => updateSession((current) => { - current.status = "failed"; - current.failure = sanitizeFailure({ step: _stepName, message, recordedAt: "now" }); + current.steps[stepName].status = "failed"; + current.steps[stepName].error = message ?? null; return current; }), - markStepFailedRecordOnly: () => cloneSession(session), completeSession: (updates: SessionUpdates = {}) => updateSession((current) => { Object.assign(current, filterSafeUpdates(updates)); diff --git a/src/lib/onboard/machine/runtime.test.ts b/src/lib/onboard/machine/runtime.test.ts index fd82a2e5bc7..706b9ef3260 100644 --- a/src/lib/onboard/machine/runtime.test.ts +++ b/src/lib/onboard/machine/runtime.test.ts @@ -9,7 +9,6 @@ import { normalizeSession, type Session, type SessionUpdates, - sanitizeFailure, } from "../../state/onboard-session"; import type { OnboardMachineEvent } from "./events"; import { @@ -68,15 +67,6 @@ function createHarness(initialSession: Session | null = createSession()) { return current; }); }, - markStepCompleteRecordOnly: (stepName, updates: SessionUpdates = {}) => - updateSession((current) => { - const step = current.steps[stepName]; - if (!step) return current; - step.status = "complete"; - current.lastCompletedStep = stepName; - Object.assign(current, filterSafeUpdates(updates)); - return current; - }), markStepSkipped: (stepName) => updateSession((current) => { const step = current.steps[stepName]; @@ -90,19 +80,10 @@ function createHarness(initialSession: Session | null = createSession()) { const step = current.steps[stepName]; if (!step) return current; step.status = "failed"; - current.status = "failed"; - current.failure = sanitizeFailure({ step: stepName, message, recordedAt: "now" }); + step.error = message ?? null; return current; }); }, - markStepFailedRecordOnly: (stepName, message) => - updateSession((current) => { - const step = current.steps[stepName]; - if (!step) return current; - step.status = "failed"; - step.error = message ?? null; - return current; - }), completeSession: (updates: SessionUpdates = {}) => updateSession((current) => { Object.assign(current, filterSafeUpdates(updates)); @@ -159,11 +140,12 @@ describe("OnboardRuntime", () => { expect(stepCalls).toEqual(["markStepStarted", "markStepComplete", "markStepFailed"]); expect(getSession()).toMatchObject({ sandboxName: "my-assistant", - status: "failed", + status: "in_progress", + failure: null, machine: { state: "init", revision: 0 }, steps: { preflight: { status: "complete" }, - gateway: { status: "failed" }, + gateway: { status: "failed", error: "boom" }, }, }); }); diff --git a/src/lib/onboard/machine/runtime.ts b/src/lib/onboard/machine/runtime.ts index 16ef69a1bda..abbc5afb8d6 100644 --- a/src/lib/onboard/machine/runtime.ts +++ b/src/lib/onboard/machine/runtime.ts @@ -32,10 +32,8 @@ export interface OnboardRuntimeDeps { updateSession(mutator: (session: Session) => Session | void): Session; markStepStarted(stepName: string): Session; markStepComplete(stepName: string, updates?: SessionUpdates): Session; - markStepCompleteRecordOnly(stepName: string, updates?: SessionUpdates): Session; markStepSkipped(stepName: string): Session; markStepFailed(stepName: string, message?: string | null): Session; - markStepFailedRecordOnly(stepName: string, message?: string | null): Session; completeSession(updates?: SessionUpdates, options?: CompleteSessionOptions): Session; filterSafeUpdates(updates: SessionUpdates): Partial; emitEvent(event: OnboardMachineEvent): void; @@ -76,10 +74,8 @@ function defaultDeps(): OnboardRuntimeDeps { updateSession: onboardSession.updateSession, markStepStarted: onboardSession.markStepStarted, markStepComplete: onboardSession.markStepComplete, - markStepCompleteRecordOnly: onboardSession.markStepCompleteRecordOnly, markStepSkipped: onboardSession.markStepSkipped, markStepFailed: onboardSession.markStepFailed, - markStepFailedRecordOnly: onboardSession.markStepFailedRecordOnly, completeSession: onboardSession.completeSession, filterSafeUpdates: onboardSession.filterSafeUpdates, emitEvent: emitOnboardMachineEvent, @@ -172,13 +168,6 @@ export class OnboardRuntime { return updated; } - async markStepCompleteRecordOnly( - stepName: string, - updates: SessionUpdates = {}, - ): Promise { - return this.markStepComplete(stepName, updates); - } - async markStepSkipped(stepName: string): Promise { return this.deps.markStepSkipped(stepName); } @@ -187,13 +176,6 @@ export class OnboardRuntime { return this.deps.markStepFailed(stepName, message); } - async markStepFailedRecordOnly( - stepName: string, - message: string | null = null, - ): Promise { - return this.deps.markStepFailedRecordOnly(stepName, message); - } - async completeSession(updates: SessionUpdates = {}): Promise { return this.deps.completeSession(updates); } diff --git a/src/lib/onboard/machine/sequence-runner.test.ts b/src/lib/onboard/machine/sequence-runner.test.ts index a09c52ecd63..579b2158943 100644 --- a/src/lib/onboard/machine/sequence-runner.test.ts +++ b/src/lib/onboard/machine/sequence-runner.test.ts @@ -9,7 +9,6 @@ import { normalizeSession, type Session, type SessionUpdates, - sanitizeFailure, } from "../../state/onboard-session"; import { advanceTo, branchTo, completeOnboardMachine, retryTo } from "./result"; import { OnboardRuntime, type OnboardRuntimeDeps } from "./runtime"; @@ -49,19 +48,13 @@ function createRuntime(initialSession: Session = createSession()) { Object.assign(current, filterSafeUpdates(updates)); return current; }), - markStepCompleteRecordOnly: (_stepName, updates: SessionUpdates = {}) => - updateSession((current) => { - Object.assign(current, filterSafeUpdates(updates)); - return current; - }), markStepSkipped: () => cloneSession(session), markStepFailed: (stepName, message) => updateSession((current) => { - current.status = "failed"; - current.failure = sanitizeFailure({ step: stepName, message, recordedAt: "now" }); + current.steps[stepName].status = "failed"; + current.steps[stepName].error = message ?? null; return current; }), - markStepFailedRecordOnly: () => cloneSession(session), completeSession: (updates: SessionUpdates = {}) => updateSession((current) => { Object.assign(current, filterSafeUpdates(updates)); diff --git a/src/lib/onboard/machine/transition-traces.test.ts b/src/lib/onboard/machine/transition-traces.test.ts index e4234ac64af..0b45bebcc0d 100644 --- a/src/lib/onboard/machine/transition-traces.test.ts +++ b/src/lib/onboard/machine/transition-traces.test.ts @@ -23,7 +23,6 @@ import { type Session, type SessionUpdates, type StepState, - sanitizeFailure, } from "../../state/onboard-session"; import type { OnboardMachineEvent } from "./events"; import { handleSandboxState } from "./handlers/sandbox"; @@ -72,14 +71,12 @@ function createTracedRuntime(initialSession: Session = createSession()) { updateSession, markStepStarted: () => cloneSession(session), markStepComplete: (_stepName, updates) => applySafeUpdates(updates), - markStepCompleteRecordOnly: (_stepName, updates) => applySafeUpdates(updates), markStepSkipped: () => cloneSession(session), markStepFailed: (stepName, message) => updateSession((current) => { - current.status = "failed"; - current.failure = sanitizeFailure({ step: stepName, message, recordedAt: NOW }); + current.steps[stepName].status = "failed"; + current.steps[stepName].error = message ?? null; }), - markStepFailedRecordOnly: () => cloneSession(session), completeSession: (updates: SessionUpdates = {}) => updateSession((current) => { Object.assign(current, filterSafeUpdates(updates)); diff --git a/src/lib/onboard/resume-machine-repair.test.ts b/src/lib/onboard/resume-machine-repair.test.ts index e889544af83..7e8a6547312 100644 --- a/src/lib/onboard/resume-machine-repair.test.ts +++ b/src/lib/onboard/resume-machine-repair.test.ts @@ -49,7 +49,7 @@ function cloneSession(session: Session): Session { } /** - * Creates a memory-backed runtime boundary with record-only step mutations. + * Creates a memory-backed runtime boundary with step-status mutations. */ function createBoundaryHarness(initial: Session) { let session = cloneSession(initial); @@ -69,11 +69,8 @@ function createBoundaryHarness(initial: Session) { markStepStarted: () => cloneSession(session), markStepComplete: (_stepName, updates: SessionUpdates = {}) => updateSession((current) => Object.assign(current, filterSafeUpdates(updates))), - markStepCompleteRecordOnly: (_stepName, updates: SessionUpdates = {}) => - updateSession((current) => Object.assign(current, filterSafeUpdates(updates))), markStepSkipped: () => cloneSession(session), markStepFailed: () => cloneSession(session), - markStepFailedRecordOnly: () => cloneSession(session), completeSession: (updates: SessionUpdates = {}) => updateSession((current) => { Object.assign(current, filterSafeUpdates(updates)); @@ -96,7 +93,7 @@ function createBoundaryHarness(initial: Session) { /** * Replays the live resume sequence from failed snapshot repair through completion. */ -async function runRecordOnlyResumeSequence(initial: Session): Promise { +async function runResumeSequence(initial: Session): Promise { applySessionRecovery(initial, "2026-06-01T00:01:00.000Z"); initial.failure = null; initial.status = "in_progress"; @@ -316,7 +313,7 @@ describe("resume machine repair", () => { ["preflight", "preflight", null], ["gateway", "gateway", "preflight"], ["inference", "inference", "provider_selection"], - ] as const)("lets record-only resume complete from failed %s", async (_name, failedStep, completedStep) => { + ] as const)("lets resume complete from failed %s", async (_name, failedStep, completedStep) => { const session = createFailedSession((current) => { current.failure = { step: failedStep, @@ -331,7 +328,7 @@ describe("resume machine repair", () => { } }); - const completed = await runRecordOnlyResumeSequence(session); + const completed = await runResumeSequence(session); expect(completed).toMatchObject({ status: "complete", @@ -343,7 +340,7 @@ describe("resume machine repair", () => { it.each([ "gateway", "policies", - ] as const)("lets record-only resume complete from a reopened complete snapshot after %s", async (completedStep) => { + ] as const)("lets resume complete from a reopened complete snapshot after %s", async (completedStep) => { const session = createSession({ resumable: true, status: "in_progress", @@ -357,7 +354,7 @@ describe("resume machine repair", () => { }); session.steps[completedStep].status = "complete"; - const completed = await runRecordOnlyResumeSequence(session); + const completed = await runResumeSequence(session); expect(completed).toMatchObject({ status: "complete", diff --git a/src/lib/onboard/runtime-boundary-record-only.test.ts b/src/lib/onboard/runtime-boundary-step-result.test.ts similarity index 85% rename from src/lib/onboard/runtime-boundary-record-only.test.ts rename to src/lib/onboard/runtime-boundary-step-result.test.ts index 097831168b8..e9ccd1bad7a 100644 --- a/src/lib/onboard/runtime-boundary-record-only.test.ts +++ b/src/lib/onboard/runtime-boundary-step-result.test.ts @@ -45,26 +45,12 @@ function createRuntimeHarness() { Object.assign(current, filterSafeUpdates(updates)); return current; }), - markStepCompleteRecordOnly: (stepName, updates: SessionUpdates = {}) => - updateSession((current) => { - current.steps[stepName].status = "complete"; - Object.assign(current, filterSafeUpdates(updates)); - return current; - }), markStepSkipped: (stepName) => updateSession((current) => { current.steps[stepName].status = "skipped"; return current; }), markStepFailed: (stepName, message) => - updateSession((current) => { - current.steps[stepName].status = "failed"; - current.steps[stepName].error = message ?? null; - current.status = "failed"; - current.failure = { step: stepName, message: message ?? null, recordedAt: "now" }; - return current; - }), - markStepFailedRecordOnly: (stepName, message) => updateSession((current) => { current.steps[stepName].status = "failed"; current.steps[stepName].error = message ?? null; @@ -91,19 +77,19 @@ function createRuntimeHarness() { }; } -describe("OnboardRuntimeBoundary record-only step/result pairing", () => { - it("pairs record-only step completion with an explicit state result", async () => { +describe("OnboardRuntimeBoundary step/result pairing", () => { + it("pairs step completion with an explicit state result", async () => { const { boundary, events } = createRuntimeHarness(); await boundary.recordStateResult(advanceTo("preflight")); const completed = await boundary.recordStepCompleteWithStateResult( "preflight", - { sandboxName: "record-only-sb" }, + { sandboxName: "paired-step-sb" }, advanceTo("gateway", { metadata: { state: "preflight" } }), ); expect(completed).toMatchObject({ - sandboxName: "record-only-sb", + sandboxName: "paired-step-sb", machine: { state: "gateway", revision: 2 }, steps: { preflight: { status: "complete" } }, }); @@ -139,7 +125,7 @@ describe("OnboardRuntimeBoundary record-only step/result pairing", () => { ]); }); - it("pairs record-only step failure with an explicit failure result", async () => { + it("pairs step failure with an explicit failure result", async () => { const { boundary, events } = createRuntimeHarness(); await boundary.recordStateResult(advanceTo("preflight")); @@ -163,7 +149,7 @@ describe("OnboardRuntimeBoundary record-only step/result pairing", () => { ]); }); - it("rejects invalid explicit results before persisting record-only step completion", async () => { + it("rejects invalid explicit results before persisting step completion", async () => { const { boundary, getSession } = createRuntimeHarness(); await boundary.recordStateResult(advanceTo("preflight")); @@ -177,7 +163,7 @@ describe("OnboardRuntimeBoundary record-only step/result pairing", () => { }); }); - it("rejects stale state results when record-only steps did not advance the machine", async () => { + it("rejects stale state results when step updates did not advance the machine", async () => { const { boundary, events } = createRuntimeHarness(); await boundary.recordStateResult(advanceTo("preflight", { metadata: { state: "init" } })); diff --git a/src/lib/onboard/runtime-boundary.test.ts b/src/lib/onboard/runtime-boundary.test.ts index 6bdffebbb82..cd7456e9e3c 100644 --- a/src/lib/onboard/runtime-boundary.test.ts +++ b/src/lib/onboard/runtime-boundary.test.ts @@ -73,7 +73,6 @@ function createRuntimeHarness(overrides: Partial = {}) { return current; }); }, - markStepCompleteRecordOnly: () => cloneSession(session ?? createSession()), markStepSkipped: (stepName) => updateSession((current) => { current.steps[stepName].status = "skipped"; @@ -87,7 +86,6 @@ function createRuntimeHarness(overrides: Partial = {}) { return current; }); }, - markStepFailedRecordOnly: () => cloneSession(session ?? createSession()), completeSession: (updates: SessionUpdates = {}) => updateSession((current) => { Object.assign(current, filterSafeUpdates(updates)); diff --git a/src/lib/onboard/runtime-boundary.ts b/src/lib/onboard/runtime-boundary.ts index c66cea33270..18c9c8443da 100644 --- a/src/lib/onboard/runtime-boundary.ts +++ b/src/lib/onboard/runtime-boundary.ts @@ -112,7 +112,7 @@ export class OnboardRuntimeBoundary { result: OnboardStateResult, ): Promise { await this.getRuntime().assertResultWillApply(result); - await this.getRuntime().markStepCompleteRecordOnly(stepName, updates); + await this.getRuntime().markStepComplete(stepName, updates); return this.recordStateResult(result); } @@ -122,7 +122,7 @@ export class OnboardRuntimeBoundary { result: OnboardStateFailedResult, ): Promise { await this.getRuntime().assertResultWillApply(result); - await this.getRuntime().markStepFailedRecordOnly(stepName, message); + await this.getRuntime().markStepFailed(stepName, message); return this.recordStateResult(result); } diff --git a/src/lib/state/onboard-session.ts b/src/lib/state/onboard-session.ts index 340c85f32b0..84386bc4bb1 100644 --- a/src/lib/state/onboard-session.ts +++ b/src/lib/state/onboard-session.ts @@ -1394,10 +1394,6 @@ export function markStepStarted(stepName: string): Session { return updatedSession; } -export function markStepStartedRecordOnly(stepName: string): Session { - return markStepStarted(stepName); -} - export function markStepComplete(stepName: string, updates: SessionUpdates = {}): Session { const safeUpdates = filterSafeUpdates(updates); return updateSession((session) => { @@ -1430,13 +1426,6 @@ export function markStepComplete(stepName: string, updates: SessionUpdates = {}) }); } -export function markStepCompleteRecordOnly( - stepName: string, - updates: SessionUpdates = {}, -): Session { - return markStepComplete(stepName, updates); -} - export function markStepSkipped(stepName: string): Session { return updateSession((session) => { const step = session.steps[stepName]; @@ -1462,10 +1451,6 @@ export function markStepFailed(stepName: string, message: string | null = null): }); } -export function markStepFailedRecordOnly(stepName: string, message: string | null = null): Session { - return markStepFailed(stepName, message); -} - /** * Single synchronous terminal-failure owner for process-exit / backstop paths. * diff --git a/test/helpers/onboard-final-flow-phases.ts b/test/helpers/onboard-final-flow-phases.ts index 118881eb387..66a7de234fb 100644 --- a/test/helpers/onboard-final-flow-phases.ts +++ b/test/helpers/onboard-final-flow-phases.ts @@ -103,11 +103,8 @@ export function createRuntimeHarness(initialSession: Session) { markStepStarted: () => cloneSession(session), markStepComplete: (_stepName, updates: SessionUpdates = {}) => updateSession((current) => Object.assign(current, filterSafeUpdates(updates))), - markStepCompleteRecordOnly: (_stepName, updates: SessionUpdates = {}) => - updateSession((current) => Object.assign(current, filterSafeUpdates(updates))), markStepSkipped: () => cloneSession(session), markStepFailed: () => cloneSession(session), - markStepFailedRecordOnly: () => cloneSession(session), completeSession: (updates: SessionUpdates = {}) => updateSession((current) => { Object.assign(current, filterSafeUpdates(updates)); diff --git a/test/source-architecture.test.ts b/test/source-architecture.test.ts index da17152e5e0..a905ebcfec2 100644 --- a/test/source-architecture.test.ts +++ b/test/source-architecture.test.ts @@ -20,6 +20,23 @@ function writeModule(root: string, file: string, source: string): void { fs.writeFileSync(destination, source); } +function listProductionTypeScriptFiles(directory: string): string[] { + const files: string[] = []; + for (const entry of fs.readdirSync(directory, { withFileTypes: true })) { + const entryPath = path.join(directory, entry.name); + if (entry.isDirectory()) { + if (entry.name !== "__test-helpers__") { + files.push(...listProductionTypeScriptFiles(entryPath)); + } + continue; + } + if (entry.isFile() && entry.name.endsWith(".ts") && !entry.name.endsWith(".test.ts")) { + files.push(entryPath); + } + } + return files; +} + function budget(overrides: Partial = {}): SourceArchitectureBudget { return { fanIn: { defaultMax: 10 }, @@ -42,6 +59,20 @@ describe("source architecture budget (#7692)", () => { expect(evaluateSourceArchitectureBudget(report, parsed)).toEqual([]); }); + // source-shape-contract: compatibility -- Removed step-mutation APIs must stay absent so OnboardRuntime remains the only persisted machine transition authority + test("keeps removed step mutation APIs out of production source (#7703)", () => { + const productionSources = ["src/lib/onboard", "src/lib/state"] + .flatMap((directory) => listProductionTypeScriptFiles(path.join(REPO_ROOT, directory))) + .map((file) => fs.readFileSync(file, "utf8")); + + expect(productionSources.join("")).not.toMatch( + /LEGACY_MACHINE_STEP_MUTATION_OPTIONS|RecordOnly|onboard-step-mutation/, + ); + expect(fs.existsSync(path.join(REPO_ROOT, "src/lib/state/onboard-step-mutation.ts"))).toBe( + false, + ); + }); + test("rejects a new runtime cycle and names its files", ({ resources }) => { const root = resources.temporaryDirectory("nemoclaw-architecture-cycle-"); writeModule(root, "src/a.ts", 'import "./b";\n'); From 5ea6980b124880984d5d675f25c09de57746abde Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Mon, 3 Aug 2026 00:56:20 -0700 Subject: [PATCH 10/19] test(package): allow policy boundary package slack --- test/package-contract/openshell-policy-boundary.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/package-contract/openshell-policy-boundary.test.ts b/test/package-contract/openshell-policy-boundary.test.ts index 5d24e319db3..fccc7cf079a 100644 --- a/test/package-contract/openshell-policy-boundary.test.ts +++ b/test/package-contract/openshell-policy-boundary.test.ts @@ -212,7 +212,7 @@ describe("OpenShell policy boundary package contract", () => { expect(validation).toEqual([true, false]); }); - it("ships an out-of-tree runtime sandbox-policy schema validator", { timeout: 90_000 }, () => { + it("ships an out-of-tree runtime sandbox-policy schema validator", { timeout: 180_000 }, () => { const productionDependencyTree = spawnSync( "npm", ["ls", "ajv", "--omit=dev", "--all", "--json"], From c85a048dd21e76d4b0851e3c01fd180dc2af83f4 Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Mon, 3 Aug 2026 00:59:31 -0700 Subject: [PATCH 11/19] fix(onboard): complete step mutation cleanup --- src/lib/onboard/lifecycle-contracts.md | 4 ++-- test/source-architecture.test.ts | 23 +++++++++-------------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/lib/onboard/lifecycle-contracts.md b/src/lib/onboard/lifecycle-contracts.md index baa57a14cd5..36c2315fca5 100644 --- a/src/lib/onboard/lifecycle-contracts.md +++ b/src/lib/onboard/lifecycle-contracts.md @@ -191,7 +191,7 @@ The schema and sanitation authority is `Session` plus `normalizeSession`/`filter | Field group | Fields | Writer/owner and state meaning | |---|---|---| | Session envelope | `version`, `sessionId`, `mode`, `startedAt`, `updatedAt`, `status`, `resumable` | `createSession`, save/update helpers, and completion/failure paths. Values are always known after creation. | -| Progress and recovery | `lastStepStarted`, `lastCompletedStep`, `failure`, `steps`, `machine`, `sandboxPromptProgress`, `stagedCredentialProviders`, `checkpoint` | Step-mutation helpers and `OnboardRuntime` own whole-step progress; the OpenClaw sandbox handler owns prompt-group completion markers. `stagedCredentialProviders` contains only names registered before sandbox setup so OpenClaw resume can require both durable ownership and an exact live binding. A marker is trusted only when its matching persisted value is present and valid, including an explicit `null` where supported. `checkpoint` is the dedicated versioned resume contract: a secret-free tri-state decision record plus durable sandbox identity, effect-group receipts, and logical web-search and messaging provider bindings, serialized alongside the session under its own `schemaVersion` with fail-closed handling of an unknown future version. The primary inference provider binding remains owned and revalidated by the provider and inference phases instead of entering this checkpoint ledger. | +| Progress and recovery | `lastStepStarted`, `lastCompletedStep`, `failure`, `steps`, `machine`, `sandboxPromptProgress`, `stagedCredentialProviders`, `checkpoint` | Step helpers record step status, timestamps, and session fields accepted by `filterSafeUpdates`. `OnboardRuntime` owns machine transitions, terminal state, and machine events. Explicit session recovery and the process-exit failure backstop are separate recovery boundaries. The OpenClaw sandbox handler owns prompt-group completion markers. `stagedCredentialProviders` contains only names registered before sandbox setup so OpenClaw resume can require both durable ownership and an exact live binding. A marker is trusted only when its matching persisted value is present and valid, including an explicit `null` where supported. `checkpoint` is the dedicated versioned resume contract: a secret-free tri-state decision record plus durable sandbox identity, effect-group receipts, and logical web-search and messaging provider bindings, serialized alongside the session under its own `schemaVersion` with fail-closed handling of an unknown future version. The primary inference provider binding remains owned and revalidated by the provider and inference phases instead of entering this checkpoint ledger. | | Target identity | `agent`, `sandboxName`, `metadata.gatewayName`, `metadata.fromDockerfile` | Onboard selection, sandbox handler/registration, and rebuild session preparation. A completed sandbox step or valid `sandboxPromptProgress.sandboxName` marker is the trust gate for a recorded name. | | Inference intent | `provider`, `model`, `endpointUrl`, `credentialEnv`, `preferredInferenceApi`, `compatibleEndpointReasoning`, `nimContainer`, `webSearchConfig` | Provider/inference handlers and `runInferenceSet`. Known credential state is an environment-variable name or presence metadata, never the value. `redactUrl` masks userinfo and fragments, redacts values under sensitive parameter names, and redacts canonical token-shaped values even under benign parameter names. | | Agent and policy intent | `hermesAuthMethod`, `toolDisclosure`, `hermesToolGateways`, `policyPresets` | Agent setup and policy handling. Channel commands update matching-session `policyPresets` only best-effort. Nullable fields conflate unset, declined, and cleared where the CLI makes those distinctions. | @@ -200,7 +200,7 @@ The schema and sanitation authority is `Session` plus `normalizeSession`/`filter | Runtime metadata | `routerPid`, `routerCredentialHash`, `gpuPassthrough` | Router and sandbox setup/recovery. PID is a live-process hint; credential hash is a digest; GPU is a concrete boolean. | | Legacy migration proof | `migratedLegacyValueHashes` | Onboard legacy migration writes SHA-256 digests keyed by environment name; session filtering guarantees string records but does not independently validate digest shape. | -The registry is separately owned by `src/lib/state/registry.ts`; backup/recovery manifests are owned by their rebuild and recreate modules. Step helpers normally use `RECORD_ONLY_STEP_MUTATION_OPTIONS`; `LEGACY_MACHINE_STEP_MUTATION_OPTIONS` is the compatibility path that also moves the machine snapshot. +The registry is separately owned by `src/lib/state/registry.ts`; backup and recovery manifests are owned by their rebuild and recreate modules. Step helpers do not change the machine snapshot or emit machine events. ## Duplicated decision points diff --git a/test/source-architecture.test.ts b/test/source-architecture.test.ts index a905ebcfec2..3330b1f6982 100644 --- a/test/source-architecture.test.ts +++ b/test/source-architecture.test.ts @@ -21,20 +21,15 @@ function writeModule(root: string, file: string, source: string): void { } function listProductionTypeScriptFiles(directory: string): string[] { - const files: string[] = []; - for (const entry of fs.readdirSync(directory, { withFileTypes: true })) { - const entryPath = path.join(directory, entry.name); - if (entry.isDirectory()) { - if (entry.name !== "__test-helpers__") { - files.push(...listProductionTypeScriptFiles(entryPath)); - } - continue; - } - if (entry.isFile() && entry.name.endsWith(".ts") && !entry.name.endsWith(".test.ts")) { - files.push(entryPath); - } - } - return files; + return fs + .readdirSync(directory, { encoding: "utf8", recursive: true }) + .filter( + (entry) => + entry.endsWith(".ts") && + !entry.endsWith(".test.ts") && + !entry.split(path.sep).includes("__test-helpers__"), + ) + .map((entry) => path.join(directory, entry)); } function budget(overrides: Partial = {}): SourceArchitectureBudget { From b1263d823fadb95b52a98316d7a92ee1008b0001 Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Mon, 3 Aug 2026 01:01:37 -0700 Subject: [PATCH 12/19] test(architecture): keep mutation absence guard linear --- test/source-architecture.test.ts | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/test/source-architecture.test.ts b/test/source-architecture.test.ts index a905ebcfec2..7fb1cb0e309 100644 --- a/test/source-architecture.test.ts +++ b/test/source-architecture.test.ts @@ -21,20 +21,16 @@ function writeModule(root: string, file: string, source: string): void { } function listProductionTypeScriptFiles(directory: string): string[] { - const files: string[] = []; - for (const entry of fs.readdirSync(directory, { withFileTypes: true })) { + return fs.readdirSync(directory, { withFileTypes: true }).flatMap((entry) => { const entryPath = path.join(directory, entry.name); - if (entry.isDirectory()) { - if (entry.name !== "__test-helpers__") { - files.push(...listProductionTypeScriptFiles(entryPath)); - } - continue; - } - if (entry.isFile() && entry.name.endsWith(".ts") && !entry.name.endsWith(".test.ts")) { - files.push(entryPath); - } - } - return files; + return entry.isDirectory() + ? entry.name === "__test-helpers__" + ? [] + : listProductionTypeScriptFiles(entryPath) + : entry.isFile() && entry.name.endsWith(".ts") && !entry.name.endsWith(".test.ts") + ? [entryPath] + : []; + }); } function budget(overrides: Partial = {}): SourceArchitectureBudget { From a7c3fb96a602d57aaa72c319378da1bd44c38f2a Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Mon, 3 Aug 2026 01:05:47 -0700 Subject: [PATCH 13/19] fix(onboard): preserve interrupted session guidance --- src/lib/onboard/machine/README.md | 4 +- src/lib/onboard/machine/runtime.test.ts | 2 +- src/lib/onboard/machine/runtime.ts | 2 + src/lib/onboard/runtime-boundary.test.ts | 112 +++++++++++++++++++++++ 4 files changed, 117 insertions(+), 3 deletions(-) diff --git a/src/lib/onboard/machine/README.md b/src/lib/onboard/machine/README.md index 8558f85624a..849cc631603 100644 --- a/src/lib/onboard/machine/README.md +++ b/src/lib/onboard/machine/README.md @@ -44,7 +44,7 @@ A state handler may perform many smaller operations, but it should expose only s The persisted onboarding session tracks step-level progress for resumability. - `OnboardRuntime` owns normal machine transitions, revision increments, terminal state, and machine events. -- Session step helpers record only step status (`pending`, `in_progress`, `complete`, `failed`, `skipped`) and safe step updates. They cannot change the machine snapshot. +- Session step helpers record only step status (`pending`, `in_progress`, `complete`, `failed`, `skipped`) and session fields accepted by `filterSafeUpdates`. They cannot change the machine snapshot. - State handlers return explicit results. They do not move the machine through step helpers. - Explicit session recovery and the process-exit failure backstop are narrow exceptions. @@ -92,7 +92,7 @@ without a state transition so a later process can resume the same non-terminal s - emitting redacted lifecycle, state, repair, resume-conflict, and hook events; - normalizing older sessions before strict execution. -Step helpers record step status and safe step updates. They cannot change the machine snapshot or emit machine events. Explicit session recovery and the process-exit failure backstop are separate recovery boundaries. They validate their snapshot changes and run before or outside handler execution. +Step helpers record step status and session fields accepted by `filterSafeUpdates`. They cannot change the machine snapshot or emit machine events. Explicit session recovery and the process-exit failure backstop are separate recovery boundaries. They validate their snapshot changes and run before or outside handler execution. ## Event semantics diff --git a/src/lib/onboard/machine/runtime.test.ts b/src/lib/onboard/machine/runtime.test.ts index 706b9ef3260..b4cb8f654ec 100644 --- a/src/lib/onboard/machine/runtime.test.ts +++ b/src/lib/onboard/machine/runtime.test.ts @@ -130,7 +130,7 @@ describe("OnboardRuntime", () => { expect(events[1]).toMatchObject({ type: "onboard.resumed", state: "init" }); }); - it("uses status-only step dependencies", async () => { + it("keeps step dependencies from changing machine or terminal status", async () => { const { runtime, getSession, stepCalls } = createHarness(); await runtime.markStepStarted("preflight"); diff --git a/src/lib/onboard/machine/runtime.ts b/src/lib/onboard/machine/runtime.ts index abbc5afb8d6..fbb1e6de042 100644 --- a/src/lib/onboard/machine/runtime.ts +++ b/src/lib/onboard/machine/runtime.ts @@ -273,6 +273,7 @@ export class OnboardRuntime { return; } if (result.type === "complete") { + assertOnboardNotInterrupted(current.machine.state, "complete", current.failure); assertValidOnboardMachineTransition(current.machine.state, "complete"); return; } @@ -286,6 +287,7 @@ export class OnboardRuntime { } return; } + assertOnboardNotInterrupted(current.machine.state, result.next, current.failure); if (current.machine.state === result.next) { throw new Error(`Onboarding state result already reached target state: ${result.next}`); } diff --git a/src/lib/onboard/runtime-boundary.test.ts b/src/lib/onboard/runtime-boundary.test.ts index cd7456e9e3c..04b0755e232 100644 --- a/src/lib/onboard/runtime-boundary.test.ts +++ b/src/lib/onboard/runtime-boundary.test.ts @@ -20,6 +20,10 @@ import { retryTo, } from "./machine/result"; import { OnboardRuntime, type OnboardRuntimeDeps } from "./machine/runtime"; +import { + InvalidOnboardMachineTransitionError, + OnboardInterruptedError, +} from "./machine/transitions"; import { OnboardRuntimeBoundary } from "./runtime-boundary"; import { applySessionRecovery } from "./session-recovery"; @@ -243,3 +247,111 @@ describe("OnboardRuntimeBoundary", () => { }); }); }); + +describe("onboarding interrupted while a step is in flight (#7982)", () => { + function failedAtSandbox(interrupted: boolean) { + const harness = createRuntimeHarness({ + status: "failed", + lastStepStarted: "sandbox", + failure: { + step: "sandbox", + message: interrupted + ? "Onboarding exited before the step completed." + : "Rebuild recreate failed", + recordedAt: "2026-05-27T00:00:00.000Z", + interrupted, + }, + machine: { + version: 1, + state: "failed", + stateEnteredAt: "2026-05-27T00:00:00.000Z", + revision: 5, + }, + }); + const boundary = new OnboardRuntimeBoundary({ + toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, + maybeForceE2eStepFailure: () => undefined, + createRuntime: harness.createRuntime, + }); + return { boundary, harness }; + } + + function interruptedAtSandbox() { + return failedAtSandbox(true); + } + + it("reports the sandbox branch as interrupted rather than an invalid transition", async () => { + const { boundary, harness } = interruptedAtSandbox(); + + const applied = boundary.recordStateResult( + branchTo("openclaw", { metadata: { state: "sandbox" } }), + ); + + await expect(applied).rejects.toThrow(OnboardInterruptedError); + await expect(applied).rejects.not.toThrow(InvalidOnboardMachineTransitionError); + await expect(applied).rejects.toThrow(/during the sandbox step/); + await expect(applied).rejects.toThrow(/continue to openclaw/); + expect(harness.getSession().machine).toMatchObject({ state: "failed", revision: 5 }); + expect(harness.events).toHaveLength(0); + }); + + it("reports an interrupt recorded by another process without an in-process latch", async () => { + const { boundary, harness } = interruptedAtSandbox(); + + const applied = boundary.recordStateResult( + branchTo("openclaw", { metadata: { state: "sandbox" } }), + ); + + await expect(applied).rejects.toThrow(OnboardInterruptedError); + await expect(applied).rejects.toThrow(/during the sandbox step/); + expect(harness.getSession().machine).toMatchObject({ state: "failed", revision: 5 }); + }); + + it("reports an ordinary failed session as an invalid transition, not an interrupt", async () => { + const { boundary, harness } = failedAtSandbox(false); + + const applied = boundary.recordStateResult( + branchTo("openclaw", { metadata: { state: "sandbox" } }), + ); + + await expect(applied).rejects.toThrow(InvalidOnboardMachineTransitionError); + await expect(applied).rejects.not.toThrow(OnboardInterruptedError); + expect(harness.getSession().machine).toMatchObject({ state: "failed", revision: 5 }); + }); + + it("rejects completing an interrupted session through recordSessionComplete", async () => { + const { boundary, harness } = interruptedAtSandbox(); + + await expect(boundary.recordSessionComplete()).rejects.toThrow(OnboardInterruptedError); + expect(harness.getSession().machine).toMatchObject({ state: "failed", revision: 5 }); + }); + + it("reports an interrupted finalization rather than an invalid completion", async () => { + const { boundary } = interruptedAtSandbox(); + + await expect(boundary.recordStateResult(completeOnboardMachine())).rejects.toThrow( + OnboardInterruptedError, + ); + }); + + it("still applies a legal branch when no interrupt was recorded", async () => { + const harness = createRuntimeHarness({ + lastStepStarted: "sandbox", + machine: { + version: 1, + state: "sandbox", + stateEnteredAt: "2026-05-27T00:00:00.000Z", + revision: 5, + }, + }); + const boundary = new OnboardRuntimeBoundary({ + toSessionUpdates: (updates) => filterSafeUpdates(updates as SessionUpdates) as SessionUpdates, + maybeForceE2eStepFailure: () => undefined, + createRuntime: harness.createRuntime, + }); + + await boundary.recordStateResult(branchTo("openclaw", { metadata: { state: "sandbox" } })); + + expect(harness.getSession().machine).toMatchObject({ state: "openclaw", revision: 6 }); + }); +}); From b1f9f0c695e09691e2ae0dddfee4a88631774dfd Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Mon, 3 Aug 2026 01:27:33 -0700 Subject: [PATCH 14/19] fix(onboard): preserve terminal transition errors --- src/lib/onboard/machine/runtime.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/lib/onboard/machine/runtime.ts b/src/lib/onboard/machine/runtime.ts index fbb1e6de042..c59eeca131d 100644 --- a/src/lib/onboard/machine/runtime.ts +++ b/src/lib/onboard/machine/runtime.ts @@ -288,6 +288,9 @@ export class OnboardRuntime { return; } assertOnboardNotInterrupted(current.machine.state, result.next, current.failure); + if (isTerminalOnboardMachineState(current.machine.state)) { + assertValidOnboardMachineTransition(current.machine.state, result.next); + } if (current.machine.state === result.next) { throw new Error(`Onboarding state result already reached target state: ${result.next}`); } From 48a2766b2c213a59dd5862cb579bcf6911267865 Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Mon, 3 Aug 2026 06:38:44 -0700 Subject: [PATCH 15/19] docs(onboard): clarify transition bookkeeping Clarify the source-shape contract and session step bookkeeping. Signed-off-by: Carlos Villela --- src/lib/onboard/machine/README.md | 2 +- test/source-architecture.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/onboard/machine/README.md b/src/lib/onboard/machine/README.md index 849cc631603..5f0d140188c 100644 --- a/src/lib/onboard/machine/README.md +++ b/src/lib/onboard/machine/README.md @@ -44,7 +44,7 @@ A state handler may perform many smaller operations, but it should expose only s The persisted onboarding session tracks step-level progress for resumability. - `OnboardRuntime` owns normal machine transitions, revision increments, terminal state, and machine events. -- Session step helpers record only step status (`pending`, `in_progress`, `complete`, `failed`, `skipped`) and session fields accepted by `filterSafeUpdates`. They cannot change the machine snapshot. +- Session step helpers record step-progress bookkeeping and context updates accepted by `filterSafeUpdates`. They cannot change the machine snapshot. - State handlers return explicit results. They do not move the machine through step helpers. - Explicit session recovery and the process-exit failure backstop are narrow exceptions. diff --git a/test/source-architecture.test.ts b/test/source-architecture.test.ts index 7fb1cb0e309..379d3283210 100644 --- a/test/source-architecture.test.ts +++ b/test/source-architecture.test.ts @@ -55,7 +55,7 @@ describe("source architecture budget (#7692)", () => { expect(evaluateSourceArchitectureBudget(report, parsed)).toEqual([]); }); - // source-shape-contract: compatibility -- Removed step-mutation APIs must stay absent so OnboardRuntime remains the only persisted machine transition authority + // source-shape-contract: compatibility -- Removed step-mutation APIs must stay absent so step helpers cannot persist machine transitions test("keeps removed step mutation APIs out of production source (#7703)", () => { const productionSources = ["src/lib/onboard", "src/lib/state"] .flatMap((directory) => listProductionTypeScriptFiles(path.join(REPO_ROOT, directory))) From dfd7278233a27d82c6d00efce36991401212dbc0 Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Mon, 3 Aug 2026 06:45:21 -0700 Subject: [PATCH 16/19] docs(onboard): align step bookkeeping terms Use one term for step helper bookkeeping across the internal authority contracts. Signed-off-by: Carlos Villela --- src/lib/onboard/lifecycle-contracts.md | 2 +- src/lib/onboard/machine/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/onboard/lifecycle-contracts.md b/src/lib/onboard/lifecycle-contracts.md index 36c2315fca5..18de1ed5b4e 100644 --- a/src/lib/onboard/lifecycle-contracts.md +++ b/src/lib/onboard/lifecycle-contracts.md @@ -191,7 +191,7 @@ The schema and sanitation authority is `Session` plus `normalizeSession`/`filter | Field group | Fields | Writer/owner and state meaning | |---|---|---| | Session envelope | `version`, `sessionId`, `mode`, `startedAt`, `updatedAt`, `status`, `resumable` | `createSession`, save/update helpers, and completion/failure paths. Values are always known after creation. | -| Progress and recovery | `lastStepStarted`, `lastCompletedStep`, `failure`, `steps`, `machine`, `sandboxPromptProgress`, `stagedCredentialProviders`, `checkpoint` | Step helpers record step status, timestamps, and session fields accepted by `filterSafeUpdates`. `OnboardRuntime` owns machine transitions, terminal state, and machine events. Explicit session recovery and the process-exit failure backstop are separate recovery boundaries. The OpenClaw sandbox handler owns prompt-group completion markers. `stagedCredentialProviders` contains only names registered before sandbox setup so OpenClaw resume can require both durable ownership and an exact live binding. A marker is trusted only when its matching persisted value is present and valid, including an explicit `null` where supported. `checkpoint` is the dedicated versioned resume contract: a secret-free tri-state decision record plus durable sandbox identity, effect-group receipts, and logical web-search and messaging provider bindings, serialized alongside the session under its own `schemaVersion` with fail-closed handling of an unknown future version. The primary inference provider binding remains owned and revalidated by the provider and inference phases instead of entering this checkpoint ledger. | +| Progress and recovery | `lastStepStarted`, `lastCompletedStep`, `failure`, `steps`, `machine`, `sandboxPromptProgress`, `stagedCredentialProviders`, `checkpoint` | Step helpers record step-progress bookkeeping and context updates accepted by `filterSafeUpdates`. `OnboardRuntime` owns machine transitions, terminal state, and machine events. Explicit session recovery and the process-exit failure backstop are separate recovery boundaries. The OpenClaw sandbox handler owns prompt-group completion markers. `stagedCredentialProviders` contains only names registered before sandbox setup so OpenClaw resume can require both durable ownership and an exact live binding. A marker is trusted only when its matching persisted value is present and valid, including an explicit `null` where supported. `checkpoint` is the dedicated versioned resume contract: a secret-free tri-state decision record plus durable sandbox identity, effect-group receipts, and logical web-search and messaging provider bindings, serialized alongside the session under its own `schemaVersion` with fail-closed handling of an unknown future version. The primary inference provider binding remains owned and revalidated by the provider and inference phases instead of entering this checkpoint ledger. | | Target identity | `agent`, `sandboxName`, `metadata.gatewayName`, `metadata.fromDockerfile` | Onboard selection, sandbox handler/registration, and rebuild session preparation. A completed sandbox step or valid `sandboxPromptProgress.sandboxName` marker is the trust gate for a recorded name. | | Inference intent | `provider`, `model`, `endpointUrl`, `credentialEnv`, `preferredInferenceApi`, `compatibleEndpointReasoning`, `nimContainer`, `webSearchConfig` | Provider/inference handlers and `runInferenceSet`. Known credential state is an environment-variable name or presence metadata, never the value. `redactUrl` masks userinfo and fragments, redacts values under sensitive parameter names, and redacts canonical token-shaped values even under benign parameter names. | | Agent and policy intent | `hermesAuthMethod`, `toolDisclosure`, `hermesToolGateways`, `policyPresets` | Agent setup and policy handling. Channel commands update matching-session `policyPresets` only best-effort. Nullable fields conflate unset, declined, and cleared where the CLI makes those distinctions. | diff --git a/src/lib/onboard/machine/README.md b/src/lib/onboard/machine/README.md index 5f0d140188c..d31769642ec 100644 --- a/src/lib/onboard/machine/README.md +++ b/src/lib/onboard/machine/README.md @@ -92,7 +92,7 @@ without a state transition so a later process can resume the same non-terminal s - emitting redacted lifecycle, state, repair, resume-conflict, and hook events; - normalizing older sessions before strict execution. -Step helpers record step status and session fields accepted by `filterSafeUpdates`. They cannot change the machine snapshot or emit machine events. Explicit session recovery and the process-exit failure backstop are separate recovery boundaries. They validate their snapshot changes and run before or outside handler execution. +Step helpers record step-progress bookkeeping and context updates accepted by `filterSafeUpdates`. They cannot change the machine snapshot or emit machine events. Explicit session recovery and the process-exit failure backstop are separate recovery boundaries. They validate their snapshot changes and run before or outside handler execution. ## Event semantics From e2dcffb09ffe3464fd27a02d5ce7e685b28dcd0d Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Mon, 3 Aug 2026 08:22:53 -0700 Subject: [PATCH 17/19] test(onboard): correct runtime boundary descriptions Signed-off-by: Carlos Villela --- src/lib/onboard/resume-machine-repair.test.ts | 2 +- src/lib/onboard/runtime-boundary-step-result.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/onboard/resume-machine-repair.test.ts b/src/lib/onboard/resume-machine-repair.test.ts index 7e8a6547312..5c911acc52d 100644 --- a/src/lib/onboard/resume-machine-repair.test.ts +++ b/src/lib/onboard/resume-machine-repair.test.ts @@ -49,7 +49,7 @@ function cloneSession(session: Session): Session { } /** - * Creates a memory-backed runtime boundary with step-status mutations. + * Creates a memory-backed runtime boundary that persists safe session updates. */ function createBoundaryHarness(initial: Session) { let session = cloneSession(initial); diff --git a/src/lib/onboard/runtime-boundary-step-result.test.ts b/src/lib/onboard/runtime-boundary-step-result.test.ts index e9ccd1bad7a..8a68669bc2c 100644 --- a/src/lib/onboard/runtime-boundary-step-result.test.ts +++ b/src/lib/onboard/runtime-boundary-step-result.test.ts @@ -185,7 +185,7 @@ describe("OnboardRuntimeBoundary step/result pairing", () => { expect(events[3]).toMatchObject({ state: "gateway" }); }); - it("rejects stale default results before compatibility replay", async () => { + it("rejects a stale result source before runtime application", async () => { const { boundary } = createRuntimeHarness(); const result = advanceTo("preflight", { metadata: { state: "missing" } }); From 26d454904ef8fcd0b38fc47d53d9461b58ec4dfb Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Mon, 3 Aug 2026 09:02:56 -0700 Subject: [PATCH 18/19] fix(onboard): restore skipped-step events Signed-off-by: Carlos Villela --- .../machine/final-flow-phases.runtime.test.ts | 10 ++++ src/lib/onboard/machine/runtime.test.ts | 59 ++++++++++++++++++- src/lib/onboard/machine/runtime.ts | 14 ++++- .../onboard/machine/transition-traces.test.ts | 4 +- test/helpers/onboard-final-flow-phases.ts | 13 +++- 5 files changed, 93 insertions(+), 7 deletions(-) diff --git a/src/lib/onboard/machine/final-flow-phases.runtime.test.ts b/src/lib/onboard/machine/final-flow-phases.runtime.test.ts index 52967fbecdf..52f846caec2 100644 --- a/src/lib/onboard/machine/final-flow-phases.runtime.test.ts +++ b/src/lib/onboard/machine/final-flow-phases.runtime.test.ts @@ -75,6 +75,11 @@ describe("final onboard flow runtime boundary", () => { expect( harness.events.filter((event) => event.type === "state.entered").map((event) => event.state), ).toEqual(["policies", "finalizing", "post_verify", "complete"]); + expect( + harness.events + .filter((event) => event.type === "state.skipped") + .map((event) => `${event.type}:${event.state}`), + ).toEqual(["state.skipped:agent_setup"]); expect(harness.events.some((event) => event.type.startsWith("state.repair."))).toBe(false); }); @@ -234,6 +239,11 @@ describe("final onboard flow runtime boundary", () => { model: "nvidia/test", machine: { state: "complete" }, }); + expect( + harness.events + .filter((event) => event.type === "state.skipped") + .map((event) => `${event.type}:${event.state}`), + ).toEqual(["state.skipped:openclaw"]); expect(harness.events.some((event) => event.type.startsWith("state.repair."))).toBe(false); }); diff --git a/src/lib/onboard/machine/runtime.test.ts b/src/lib/onboard/machine/runtime.test.ts index b4cb8f654ec..84d3add6852 100644 --- a/src/lib/onboard/machine/runtime.test.ts +++ b/src/lib/onboard/machine/runtime.test.ts @@ -67,13 +67,20 @@ function createHarness(initialSession: Session | null = createSession()) { return current; }); }, - markStepSkipped: (stepName) => - updateSession((current) => { + markStepSkipped: (stepName) => { + stepCalls.push("markStepSkipped"); + return updateSession((current) => { const step = current.steps[stepName]; if (!step) return current; + if (step.status === "complete" || step.status === "failed" || step.status === "skipped") + return current; step.status = "skipped"; + step.startedAt = null; + step.completedAt = null; + step.error = null; return current; - }), + }); + }, markStepFailed: (stepName, message) => { stepCalls.push("markStepFailed"); return updateSession((current) => { @@ -150,6 +157,52 @@ describe("OnboardRuntime", () => { }); }); + it.each([ + "pending", + "in_progress", + ] as const)("emits one mapped skip event when a $status step becomes skipped", async (status) => { + const initial = sessionInState("policies"); + initial.endpointUrl = + "https://alice:super-secret@example.com/v1?token=super-secret&keep=yes#token=super-secret"; + initial.steps.agent_setup.status = status; + const { runtime, events, getSession, stepCalls } = createHarness(initial); + + await runtime.markStepSkipped("agent_setup"); + await runtime.markStepSkipped("agent_setup"); + + expect(stepCalls).toEqual(["markStepSkipped", "markStepSkipped"]); + expect(getSession().steps.agent_setup.status).toBe("skipped"); + expect(events).toHaveLength(1); + expect(events[0]).toMatchObject({ + version: 1, + type: "state.skipped", + sessionId: initial.sessionId, + state: "agent_setup", + step: "agent_setup", + context: { endpointOrigin: "https://example.com" }, + error: null, + metadata: {}, + }); + expect(JSON.stringify(events)).not.toContain("super-secret"); + expect(JSON.stringify(events)).not.toContain("alice"); + }); + + it.each([ + { label: "complete", stepName: "openclaw", status: "complete" as const }, + { label: "failed", stepName: "openclaw", status: "failed" as const }, + { label: "skipped", stepName: "openclaw", status: "skipped" as const }, + { label: "unknown", stepName: "not_a_step", status: null }, + ])("does not emit a skip event for a $label step", async ({ stepName, status }) => { + const initial = sessionInState("policies"); + if (status) initial.steps.openclaw.status = status; + const { runtime, events, stepCalls } = createHarness(initial); + + await runtime.markStepSkipped(stepName); + + expect(stepCalls).toEqual(["markStepSkipped"]); + expect(events).toHaveLength(0); + }); + it("validates and persists explicit transitions", async () => { const { runtime, events, getSession } = createHarness(); diff --git a/src/lib/onboard/machine/runtime.ts b/src/lib/onboard/machine/runtime.ts index c59eeca131d..6f197c9f48f 100644 --- a/src/lib/onboard/machine/runtime.ts +++ b/src/lib/onboard/machine/runtime.ts @@ -8,6 +8,7 @@ import type { ResumeConfigConflict } from "../resume-config"; import { createOnboardMachineEvent, emitOnboardMachineEvent, + machineStateFromOnboardSessionStep, type OnboardMachineEvent, } from "./events"; import type { OnboardStateResult } from "./result"; @@ -169,7 +170,18 @@ export class OnboardRuntime { } async markStepSkipped(stepName: string): Promise { - return this.deps.markStepSkipped(stepName); + const current = this.ensureSession(); + const state = machineStateFromOnboardSessionStep(stepName); + const previousStatus = state ? current.steps[stepName]?.status : null; + const updated = this.deps.markStepSkipped(stepName); + if ( + state && + (previousStatus === "pending" || previousStatus === "in_progress") && + updated.steps[stepName]?.status === "skipped" + ) { + this.emit("state.skipped", updated, { state, step: stepName }); + } + return updated; } async markStepFailed(stepName: string, message: string | null = null): Promise { diff --git a/src/lib/onboard/machine/transition-traces.test.ts b/src/lib/onboard/machine/transition-traces.test.ts index 0b45bebcc0d..61d494a2f65 100644 --- a/src/lib/onboard/machine/transition-traces.test.ts +++ b/src/lib/onboard/machine/transition-traces.test.ts @@ -8,8 +8,8 @@ * `runtime.test.ts` pins per-operation event shapes, `runner.test.ts` pins * handler sequencing without observing events). Descriptive, not * aspirational: update a pin in the same PR that changes the ordering. - * Recovery-path semantics (edges leaving terminal `failed`, the legacy - * step-mutation bridge) stay out of scope and are owned by #6227. + * Recovery-path semantics (edges leaving terminal `failed`) stay out of scope + * and are owned by #6227. */ import { describe, expect, it, vi } from "vitest"; diff --git a/test/helpers/onboard-final-flow-phases.ts b/test/helpers/onboard-final-flow-phases.ts index 66a7de234fb..f074bb759e0 100644 --- a/test/helpers/onboard-final-flow-phases.ts +++ b/test/helpers/onboard-final-flow-phases.ts @@ -103,7 +103,18 @@ export function createRuntimeHarness(initialSession: Session) { markStepStarted: () => cloneSession(session), markStepComplete: (_stepName, updates: SessionUpdates = {}) => updateSession((current) => Object.assign(current, filterSafeUpdates(updates))), - markStepSkipped: () => cloneSession(session), + markStepSkipped: (stepName) => + updateSession((current) => { + const step = current.steps[stepName]; + if (!step) return current; + if (step.status === "complete" || step.status === "failed" || step.status === "skipped") + return current; + step.status = "skipped"; + step.startedAt = null; + step.completedAt = null; + step.error = null; + return current; + }), markStepFailed: () => cloneSession(session), completeSession: (updates: SessionUpdates = {}) => updateSession((current) => { From c1552c6d736cd6a7f16bca8dd2a2ce7470c0082f Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Mon, 3 Aug 2026 16:37:40 -0700 Subject: [PATCH 19/19] refactor(onboard): extract flow handoff context (#7717) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Stack 3 of 5; depends on #7716. Move initial-to-core and core-to-final context validation into `flow-handoff.ts`. The entrypoint retains dependency wiring instead of constructing handoff context. ## Related Issue Refs #7706 ## Changes - Validate preflight GPU output before core execution. - Preserve sandbox-name precedence and reserved-name reporting. - Validate sandbox identity and inference output before final execution. - Document the extracted handoff owner. ## Type of Change - [x] Code change with doc updates ## Quality Gates - [x] Tests added or updated for changed behavior - [x] Documentation not applicable — No supported command, configuration, default, workflow, or user-visible behavior changes. The internal ownership README was updated. - [x] Sensitive paths changed (security, policy, credentials, preflight, onboarding, inference, runner, sandbox, or messaging) - [x] Sensitive-path review completed — Independent nine-category review passed for commit SHA `74d024849fb23fca25c339f567220a937298d6c0` against base SHA `7c83651249b7a53a847a317736aa59e46ded8101`: https://github.com/NVIDIA/NemoClaw/pull/7717#issuecomment-5167936273 ## Documentation Writer Review - [x] Documentation writer subagent reviewed the completed changes - Result: `docs-updated` - Evidence: Independent Codex Desktop documentation writer review passed for commit SHA `74d024849fb23fca25c339f567220a937298d6c0` against base SHA `7c83651249b7a53a847a317736aa59e46ded8101`. Reviewed all five changed files against `WRITING.md`, the NemoClaw Controlled Word List, the shared documentation-writing contract, and the documentation contributor guidance. `src/lib/onboard/machine/README.md` accurately states that `flow-handoff.ts` validates required data and constructs context at the initial-to-core and core-to-final boundaries. The entrypoint continues to supply process-bound dependencies and reserved-name output. Changed errors and behavior-oriented test titles accurately describe preflight GPU validation, sandbox-name precedence, required final context, and preservation of runner results and sessions. Issue #7706 defines this internal refactor scope and prohibits a new public API or speculative extension point. The change does not modify a supported command, output, configuration, workflow, default, or other user-visible behavior, so no user-facing `docs/` source change is required. - Agent: Codex Desktop ## DGX Station Hardware Evidence - [ ] Tested on DGX Station - Result: Not applicable; this PR does not change `scripts/prepare-dgx-station-host.sh`. ## Verification - [x] PR description includes a `Signed-off-by:` line and every commit appears as `Verified` in GitHub - [x] Normal `pre-commit`, `commit-msg`, and `pre-push` hooks passed, or `npm run check:diff` passed when hooks were skipped or unavailable - [x] Targeted behavior tests pass for the current change set — CI / Pull Request test jobs passed for commit SHA `74d024849fb23fca25c339f567220a937298d6c0`. - [x] Applicable broad gate passed — Required check `E2E / PR Gate` passed for commit SHA `74d024849fb23fca25c339f567220a937298d6c0`. - [x] Quality Gates section completed with required justifications or waivers - [x] No secrets, API keys, or credentials committed - [x] `npm run docs` builds without warnings (documentation changes only) — Not applicable; no published documentation source changed. - [x] Doc pages follow the style guide (doc changes only) --- Signed-off-by: Carlos Villela --------- Signed-off-by: Carlos Villela --- src/lib/onboard.ts | 76 +---- src/lib/onboard/machine/README.md | 2 + src/lib/onboard/machine/core-flow-phases.ts | 2 + src/lib/onboard/machine/flow-handoff.test.ts | 339 +++++++++++++++++++ src/lib/onboard/machine/flow-handoff.ts | 60 ++++ 5 files changed, 420 insertions(+), 59 deletions(-) create mode 100644 src/lib/onboard/machine/flow-handoff.test.ts create mode 100644 src/lib/onboard/machine/flow-handoff.ts diff --git a/src/lib/onboard.ts b/src/lib/onboard.ts index d2c07e3e537..47b6659ec3c 100644 --- a/src/lib/onboard.ts +++ b/src/lib/onboard.ts @@ -480,7 +480,7 @@ const { restoreDefaultAfterRecreate, }: typeof import("./onboard/cancel-rollback") = require("./onboard/cancel-rollback"); // biome-ignore format: keep src/lib/onboard.ts net-neutral for growth guardrail. -const { createProviderInferenceOnboardFlowPhase, createSandboxOnboardFlowPhase, runCoreOnboardFlowSlice }: typeof import("./onboard/machine/core-flow-phases") = require("./onboard/machine/core-flow-phases"); +const { createProviderInferenceOnboardFlowPhase, createSandboxOnboardFlowPhase, prepareCoreOnboardFlowContext, prepareFinalOnboardFlowContext, runCoreOnboardFlowSlice }: typeof import("./onboard/machine/core-flow-phases") = require("./onboard/machine/core-flow-phases"); const { createFinalOnboardFlowPhases, runFinalOnboardFlowSlice, @@ -4251,35 +4251,23 @@ async function runOnboard(opts: OnboardOptions = {}): Promise { recordRepairEvent, }); - const initialContext = initialFlowResult.context; - if (!initialContext.sandboxGpuConfig) { - throw new Error("Preflight did not produce a sandbox GPU configuration."); - } - session = initialFlowResult.session; - const sandboxGpuConfig = initialContext.sandboxGpuConfig; - const { gpuPassthrough } = initialContext; - const gpu = initialContext.gpu ?? null; - // #2753: for an unfinished sandbox, an explicit requested name precedes // the checkpointed name from the interrupted session. - let sandboxName = - recordedSandboxName || requestedSandboxName || checkpointedSandboxName || null; - if (sandboxName && RESERVED_SANDBOX_NAMES.has(sandboxName)) { - console.error( - ` Reserved name in resumed session: '${sandboxName}' is a ${cliDisplayName()} CLI command.`, - ); - console.error(" Start a fresh onboard with --name to choose a different name."); - process.exit(1); - } - const coreFlowContext: InitialOnboardFlowContext = { - ...initialContext, - session, - sandboxName, + const coreFlowContext = prepareCoreOnboardFlowContext({ + initial: initialFlowResult, + recordedSandboxName, + requestedSandboxName, + checkpointedSandboxName, selectedMessagingChannels, - gpu, - sandboxGpuConfig, - gpuPassthrough, - }; + assertSandboxNameAllowed: (sandboxName) => { + if (!RESERVED_SANDBOX_NAMES.has(sandboxName)) return; + console.error( + ` Reserved name in resumed session: '${sandboxName}' is a ${cliDisplayName()} CLI command.`, + ); + console.error(" Start a fresh onboard with --name to choose a different name."); + process.exit(1); + }, + }); // biome-ignore format: keep src/lib/onboard.ts net-neutral for growth guardrail. const runCoreGatewayOpenshell = setupInferenceFactory.createGatewayScopedOpenshellRunner(runOpenshell, GATEWAY_NAME); // biome-ignore format: keep src/lib/onboard.ts net-neutral for growth guardrail. @@ -4445,38 +4433,8 @@ async function runOnboard(opts: OnboardOptions = {}): Promise { recordRepairEvent, }); setupInferenceFactory.selectGatewayForFollowupOrExit(GATEWAY_NAME, runOpenshell); - const coreContext = coreFlowResult.context; - session = coreContext.session; - sandboxName = coreContext.sandboxName; - if (!sandboxName || !coreContext.model || !coreContext.provider) { - throw new Error("Onboarding state is incomplete after sandbox setup."); - } - const model = coreContext.model; - const provider = coreContext.provider; - const endpointUrl = coreContext.endpointUrl; - const credentialEnv = coreContext.credentialEnv; - const hermesAuthMethod = coreContext.hermesAuthMethod; - const hermesToolGateways = coreContext.hermesToolGateways; - const nimContainer = coreContext.nimContainer; - let webSearchConfig = coreContext.webSearchConfig as WebSearchConfig | null; - const webSearchSupported = coreContext.webSearchSupported; - - const finalFlowContext: InitialOnboardFlowContext = { - ...coreContext, - session, - sandboxName, - model, - provider, - endpointUrl, - credentialEnv, - hermesAuthMethod, - hermesToolGateways, - nimContainer, - webSearchConfig, - selectedMessagingChannels: coreContext.selectedMessagingChannels, - webSearchSupported, - }; - let liveFinalFlowContext = finalFlowContext; + const finalFlowContext = prepareFinalOnboardFlowContext(coreFlowResult); + let liveFinalFlowContext: InitialOnboardFlowContext = finalFlowContext; const finalFlowPhases = createFinalOnboardFlowPhases< InitialOnboardFlowContext, diff --git a/src/lib/onboard/machine/README.md b/src/lib/onboard/machine/README.md index d31769642ec..bd917cf8116 100644 --- a/src/lib/onboard/machine/README.md +++ b/src/lib/onboard/machine/README.md @@ -19,6 +19,8 @@ The target shape is a machine-driven onboarding runner: In that final shape, `src/lib/onboard.ts` should be a thin entrypoint. State handlers should own state-specific prompts, resume validation, repair decisions, and side effects. +`flow-handoff.ts` validates required data and constructs context at the initial-to-core and core-to-final boundaries. The entrypoint supplies process-bound dependencies and reserved-name output. + The strict runner owns exact `init`, `preflight`, `provider_selection`, `inference`, and `sandbox` entry. If the durable state is later than a slice entry, earlier phases run as evented prerequisite repairs. A repair must return a legal, update-free transition chain and must not change the durable entry state. ## State ownership diff --git a/src/lib/onboard/machine/core-flow-phases.ts b/src/lib/onboard/machine/core-flow-phases.ts index d137df113fa..b511baf97bf 100644 --- a/src/lib/onboard/machine/core-flow-phases.ts +++ b/src/lib/onboard/machine/core-flow-phases.ts @@ -32,6 +32,8 @@ import type { OnboardMachineRunnerResult, OnboardMachineRunnerRuntime } from "./ import { runOnboardSequenceWithRunner, type OnboardSequencePhase } from "./sequence-runner"; import type { OnboardMachineState } from "./types"; +export { prepareCoreOnboardFlowContext, prepareFinalOnboardFlowContext } from "./flow-handoff"; + export interface EndpointProvenanceOptions { endpointSource?: InferenceEndpointSource | null; endpointSourceProvider?: string | null; diff --git a/src/lib/onboard/machine/flow-handoff.test.ts b/src/lib/onboard/machine/flow-handoff.test.ts new file mode 100644 index 00000000000..c090faa49e6 --- /dev/null +++ b/src/lib/onboard/machine/flow-handoff.test.ts @@ -0,0 +1,339 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { describe, expect, it, vi } from "vitest"; + +import { createSession, MACHINE_SNAPSHOT_VERSION, type Session } from "../../state/onboard-session"; +import type { OnboardFlowContext } from "./flow-context"; +import { prepareCoreOnboardFlowContext, prepareFinalOnboardFlowContext } from "./flow-handoff"; +import type { OnboardMachineState } from "./types"; + +const TRACE_TIME = "2026-08-03T00:00:00.000Z"; + +interface HandoffResultCase { + trace: string; + resume: boolean; + fresh: boolean; + initialState: OnboardMachineState; + coreState: OnboardMachineState; + status: "in_progress" | "failed"; +} + +const handoffResultCases: readonly HandoffResultCase[] = [ + { + trace: "resumed", + resume: true, + fresh: false, + initialState: "provider_selection", + coreState: "openclaw", + status: "in_progress", + }, + { + trace: "failed", + resume: false, + fresh: true, + initialState: "failed", + coreState: "failed", + status: "failed", + }, + { + trace: "paused", + resume: false, + fresh: true, + initialState: "gateway", + coreState: "sandbox", + status: "in_progress", + }, +]; + +function runnerSession( + state: OnboardMachineState, + status: HandoffResultCase["status"], + failure: Session["failure"], +): Session { + const session = createSession({ + failure, + machine: { + version: MACHINE_SNAPSHOT_VERSION, + state, + stateEnteredAt: TRACE_TIME, + revision: 7, + }, + }); + session.status = status; + return session; +} + +function context(): OnboardFlowContext & { + gpu: string | null; + sandboxGpuConfig: { mode: string } | null; + gpuPassthrough: boolean; +} { + return { + resume: false, + fresh: true, + session: createSession(), + agent: null, + recordedSandboxName: null, + requestedSandboxName: null, + sandboxName: null, + fromDockerfile: null, + model: null, + provider: null, + endpointUrl: null, + credentialEnv: null, + hermesAuthMethod: null, + hermesToolGateways: [], + preferredInferenceApi: null, + compatibleEndpointReasoning: null, + + compatibleEndpointReasoningEffort: null, + nimContainer: null, + webSearchConfig: null, + webSearchSupported: false, + selectedMessagingChannels: [], + gpu: "nvidia", + sandboxGpuConfig: { mode: "cdi" }, + gpuPassthrough: true, + }; +} + +describe("onboard flow handoffs", () => { + it("constructs core context from the initial result and requested name", () => { + const initialContext = context(); + const persisted = createSession(); + const assertSandboxNameAllowed = vi.fn(); + + const result = prepareCoreOnboardFlowContext({ + initial: { context: initialContext, session: persisted }, + recordedSandboxName: null, + requestedSandboxName: "requested", + checkpointedSandboxName: "checkpointed", + selectedMessagingChannels: ["slack"], + assertSandboxNameAllowed, + }); + + expect(result).toMatchObject({ + session: persisted, + sandboxName: "requested", + selectedMessagingChannels: ["slack"], + gpu: "nvidia", + sandboxGpuConfig: { mode: "cdi" }, + gpuPassthrough: true, + }); + expect(assertSandboxNameAllowed).toHaveBeenCalledWith("requested"); + }); + + it.each([ + { + source: "recorded", + recordedSandboxName: "recorded", + requestedSandboxName: "requested", + checkpointedSandboxName: "checkpointed", + }, + { + source: "requested", + recordedSandboxName: null, + requestedSandboxName: "requested", + checkpointedSandboxName: "checkpointed", + }, + { + source: "checkpointed", + recordedSandboxName: null, + requestedSandboxName: null, + checkpointedSandboxName: "checkpointed", + }, + ])("selects the $source sandbox name by precedence", ({ + source, + recordedSandboxName, + requestedSandboxName, + checkpointedSandboxName, + }) => { + const assertSandboxNameAllowed = vi.fn(); + + const result = prepareCoreOnboardFlowContext({ + initial: { context: context(), session: createSession() }, + recordedSandboxName, + requestedSandboxName, + checkpointedSandboxName, + selectedMessagingChannels: [], + assertSandboxNameAllowed, + }); + + expect(result.sandboxName).toBe(source); + expect(assertSandboxNameAllowed).toHaveBeenCalledWith(source); + }); + + it("rejects a missing preflight GPU configuration", () => { + const initialContext = { ...context(), sandboxGpuConfig: null }; + const persisted = createSession(); + + expect(() => + prepareCoreOnboardFlowContext({ + initial: { context: initialContext, session: persisted }, + recordedSandboxName: null, + requestedSandboxName: null, + checkpointedSandboxName: null, + selectedMessagingChannels: [], + assertSandboxNameAllowed: vi.fn(), + }), + ).toThrow("Preflight did not produce a sandbox GPU configuration."); + }); + + it.each( + handoffResultCases, + )("preserves a $trace runner result at the initial-to-core handoff (#7706)", ({ + trace, + resume, + fresh, + initialState, + status, + }) => { + const failure = + status === "failed" + ? { + step: "gateway", + message: "gateway failed", + recordedAt: TRACE_TIME, + } + : null; + const persisted = runnerSession(initialState, status, failure); + const endpointUrl = `https://${trace}.example.test`; + + const result = prepareCoreOnboardFlowContext({ + initial: { + context: { + ...context(), + resume, + fresh, + endpointUrl, + }, + session: persisted, + }, + recordedSandboxName: null, + requestedSandboxName: null, + checkpointedSandboxName: null, + selectedMessagingChannels: ["slack"], + assertSandboxNameAllowed: vi.fn(), + }); + + expect(result.session).toBe(persisted); + expect(result).toMatchObject({ + resume, + fresh, + endpointUrl, + selectedMessagingChannels: ["slack"], + }); + expect(result.session).toMatchObject({ + status, + resumable: true, + failure, + machine: { + state: initialState, + revision: 7, + }, + }); + }); + + it("constructs final context after sandbox identity and inference are complete", () => { + const persisted = createSession(); + const coreContext = { + ...context(), + sandboxName: "ready", + model: "model", + provider: "provider", + endpointUrl: "https://inference.example.test", + selectedMessagingChannels: ["slack"], + }; + + const result = prepareFinalOnboardFlowContext({ + context: coreContext, + session: persisted, + }); + + expect(result.session).toBe(persisted); + expect(result).toMatchObject({ + sandboxName: "ready", + model: "model", + provider: "provider", + endpointUrl: "https://inference.example.test", + selectedMessagingChannels: ["slack"], + }); + }); + + it.each( + handoffResultCases, + )("preserves a $trace runner result at the core-to-final handoff (#7706)", ({ + trace, + resume, + fresh, + coreState, + status, + }) => { + const failure = + status === "failed" + ? { + step: "sandbox", + message: "sandbox failed", + recordedAt: TRACE_TIME, + } + : null; + const persisted = runnerSession(coreState, status, failure); + const endpointUrl = `https://${trace}.example.test`; + + const result = prepareFinalOnboardFlowContext({ + context: { + ...context(), + resume, + fresh, + sandboxName: "ready", + model: "model", + provider: "provider", + endpointUrl, + selectedMessagingChannels: ["slack"], + }, + session: persisted, + }); + + expect(result.session).toBe(persisted); + expect(result).toMatchObject({ + resume, + fresh, + sandboxName: "ready", + model: "model", + provider: "provider", + endpointUrl, + selectedMessagingChannels: ["slack"], + }); + expect(result.session).toMatchObject({ + status, + resumable: true, + failure, + machine: { + state: coreState, + revision: 7, + }, + }); + }); + + it.each([ + "sandboxName", + "model", + "provider", + ] as const)("rejects final context when $field is missing", (field) => { + const coreContext = { + ...context(), + sandboxName: "ready", + model: "model", + provider: "provider", + [field]: null, + }; + + expect(() => + prepareFinalOnboardFlowContext({ + context: coreContext, + session: createSession(), + }), + ).toThrow("Onboarding state is incomplete after sandbox setup."); + }); +}); diff --git a/src/lib/onboard/machine/flow-handoff.ts b/src/lib/onboard/machine/flow-handoff.ts new file mode 100644 index 00000000000..acd607cf48b --- /dev/null +++ b/src/lib/onboard/machine/flow-handoff.ts @@ -0,0 +1,60 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import type { OnboardFlowContext } from "./flow-context"; +import type { OnboardMachineRunnerResult } from "./runner"; + +type InitialHandoffContext = OnboardFlowContext & { + readonly gpu: Gpu | null; + readonly sandboxGpuConfig: SandboxGpuConfig | null; + readonly gpuPassthrough: boolean; +}; + +export function prepareCoreOnboardFlowContext< + Context extends InitialHandoffContext, + Gpu, + SandboxGpuConfig, +>(options: { + initial: OnboardMachineRunnerResult; + recordedSandboxName: string | null; + requestedSandboxName: string | null; + checkpointedSandboxName: string | null; + selectedMessagingChannels: string[]; + assertSandboxNameAllowed(sandboxName: string): void; +}): Context & { sandboxGpuConfig: SandboxGpuConfig } { + const context = options.initial.context; + if (!context.sandboxGpuConfig) { + throw new Error("Preflight did not produce a sandbox GPU configuration."); + } + const sandboxName = + options.recordedSandboxName || + options.requestedSandboxName || + options.checkpointedSandboxName || + null; + if (sandboxName) options.assertSandboxNameAllowed(sandboxName); + return { + ...context, + session: options.initial.session, + sandboxName, + selectedMessagingChannels: options.selectedMessagingChannels, + gpu: context.gpu ?? null, + sandboxGpuConfig: context.sandboxGpuConfig, + gpuPassthrough: context.gpuPassthrough, + }; +} + +export function prepareFinalOnboardFlowContext( + core: OnboardMachineRunnerResult, +): Context & { sandboxName: string; model: string; provider: string } { + const context = core.context; + if (!context.sandboxName || !context.model || !context.provider) { + throw new Error("Onboarding state is incomplete after sandbox setup."); + } + return { + ...context, + session: core.session, + sandboxName: context.sandboxName, + model: context.model, + provider: context.provider, + }; +}