From dc08a1cdc458200057bd0fc70b3fe7f263f70d51 Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Mon, 22 Jun 2026 22:46:24 -0700 Subject: [PATCH 1/4] refactor(onboard): declare resume compatibility states --- .../onboard/machine/core-flow-phases.test.ts | 10 +++++++++- src/lib/onboard/machine/core-flow-phases.ts | 8 +++++++- .../onboard/machine/final-flow-phases.test.ts | 10 +++++++++- src/lib/onboard/machine/final-flow-phases.ts | 2 +- src/lib/onboard/machine/initial-flow-phases.ts | 2 +- src/lib/onboard/machine/live-flow-slice.test.ts | 5 +++++ src/lib/onboard/machine/live-flow-slice.ts | 17 +++++++++-------- 7 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/lib/onboard/machine/core-flow-phases.test.ts b/src/lib/onboard/machine/core-flow-phases.test.ts index a9bbf29b9e2..8ccc72a77cf 100644 --- a/src/lib/onboard/machine/core-flow-phases.test.ts +++ b/src/lib/onboard/machine/core-flow-phases.test.ts @@ -292,7 +292,15 @@ describe("core onboard flow phases", () => { await runCoreOnboardFlowSlice({ context: context({ resume: true }), runtime: { - session: async () => createSession(), + session: async () => + createSession({ + machine: { + version: 1, + state: "provider_selection", + stateEnteredAt: "2026-06-09T00:00:00.000Z", + revision: 1, + }, + }), applyResult: async () => createSession(), }, phases, diff --git a/src/lib/onboard/machine/core-flow-phases.ts b/src/lib/onboard/machine/core-flow-phases.ts index af4a72b7734..10fcacbfdc1 100644 --- a/src/lib/onboard/machine/core-flow-phases.ts +++ b/src/lib/onboard/machine/core-flow-phases.ts @@ -160,7 +160,13 @@ export async function runCoreOnboardFlowSlice { await runFinalOnboardFlowSlice({ context: context({ resume: true }), runtime: { - session: async () => createSession(), + session: async () => + createSession({ + machine: { + version: 1, + state: "openclaw", + stateEnteredAt: "2026-06-09T00:00:00.000Z", + revision: 1, + }, + }), applyResult: async () => createSession(), }, phases, diff --git a/src/lib/onboard/machine/final-flow-phases.ts b/src/lib/onboard/machine/final-flow-phases.ts index 10d33d5266f..d82719e44bc 100644 --- a/src/lib/onboard/machine/final-flow-phases.ts +++ b/src/lib/onboard/machine/final-flow-phases.ts @@ -177,7 +177,7 @@ export async function runFinalOnboardFlowSlice { await options.recordStateResult(stateResult); diff --git a/src/lib/onboard/machine/initial-flow-phases.ts b/src/lib/onboard/machine/initial-flow-phases.ts index 051ced46fca..b9dd5ddf3c3 100644 --- a/src/lib/onboard/machine/initial-flow-phases.ts +++ b/src/lib/onboard/machine/initial-flow-phases.ts @@ -200,7 +200,7 @@ export async function runInitialOnboardFlowSlice { ], resume: true, runWhenState: ["preflight"], + compatibilityWhenState: ["provider_selection"], runSlice, applyCompatibleResult, }); @@ -142,6 +143,7 @@ describe("runLiveOnboardFlowSlice", () => { phases: [phase("preflight", 2)], resume: true, runWhenState: ["preflight"], + compatibilityWhenState: ["preflight"], runSlice, applyCompatibleResult, }); @@ -209,6 +211,7 @@ describe("runLiveOnboardFlowSlice", () => { phases: [first, second], resume: true, runWhenState: ["preflight"], + compatibilityWhenState: ["provider_selection"], runSlice: vi.fn(), applyCompatibleResult, }), @@ -230,6 +233,7 @@ describe("runLiveOnboardFlowSlice", () => { phases: [phase("preflight", 2, [])], resume: true, runWhenState: ["preflight"], + compatibilityWhenState: ["provider_selection"], runSlice: vi.fn(), applyCompatibleResult, }), @@ -252,6 +256,7 @@ describe("runLiveOnboardFlowSlice", () => { phases: [phase("preflight", 2), later], resume: true, runWhenState: ["preflight"], + compatibilityWhenState: ["provider_selection"], runSlice: vi.fn(), applyCompatibleResult, }), diff --git a/src/lib/onboard/machine/live-flow-slice.ts b/src/lib/onboard/machine/live-flow-slice.ts index 78eedd17c9b..1294b2bf488 100644 --- a/src/lib/onboard/machine/live-flow-slice.ts +++ b/src/lib/onboard/machine/live-flow-slice.ts @@ -64,13 +64,14 @@ function asResultArray( /** * Run a live onboard flow slice through the strict runner when the current - * machine state is exactly at the slice entry point. Resume/ahead-state flows - * use the compatibility path so repair/backstop phase bodies still execute even - * when a saved session has already advanced beyond the slice. Non-resume - * compatibility is limited to caller-declared ahead states so earlier machine - * states fail before running slice side effects out of order. Callers supply the - * compatibility recorder so each live slice keeps using the runtime boundary - * that validates or intentionally skips stale legacy step results. + * machine state is exactly at the slice entry point. Declared compatibility + * states use the compatibility path so repair/backstop phase bodies still + * execute during resume or when a saved session has already advanced beyond the + * slice. Compatibility is limited to caller-declared states so earlier or + * unexpected machine states fail before running slice side effects out of order. + * Callers supply the compatibility recorder so each live slice keeps using the + * runtime boundary that validates or intentionally skips stale legacy step + * results. */ export async function runLiveOnboardFlowSlice({ context, @@ -86,7 +87,7 @@ export async function runLiveOnboardFlowSlice({ if (!resume && runWhenState.includes(current.machine.state)) { return runSlice({ context, runtime, phases }); } - if (!resume && !compatibilityWhenState.includes(current.machine.state)) { + if (!compatibilityWhenState.includes(current.machine.state)) { throw new UnexpectedLiveOnboardFlowSliceStateError( current.machine.state, runWhenState, From 6c6812f43bf117238931c9ff5f77fce47ddf6669 Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Mon, 22 Jun 2026 22:50:56 -0700 Subject: [PATCH 2/4] refactor(onboard): remove resume flag from live slices --- src/lib/onboard/machine/core-flow-phases.ts | 11 +++-------- src/lib/onboard/machine/final-flow-phases.ts | 5 +++-- src/lib/onboard/machine/initial-flow-phases.ts | 5 +++-- src/lib/onboard/machine/live-flow-slice.test.ts | 8 -------- src/lib/onboard/machine/live-flow-slice.ts | 7 ++++--- 5 files changed, 13 insertions(+), 23 deletions(-) diff --git a/src/lib/onboard/machine/core-flow-phases.ts b/src/lib/onboard/machine/core-flow-phases.ts index 10fcacbfdc1..114deb00ca0 100644 --- a/src/lib/onboard/machine/core-flow-phases.ts +++ b/src/lib/onboard/machine/core-flow-phases.ts @@ -158,15 +158,10 @@ export async function runCoreOnboardFlowSlice { await options.recordStateResult(stateResult); diff --git a/src/lib/onboard/machine/initial-flow-phases.ts b/src/lib/onboard/machine/initial-flow-phases.ts index b9dd5ddf3c3..a7660c10bbe 100644 --- a/src/lib/onboard/machine/initial-flow-phases.ts +++ b/src/lib/onboard/machine/initial-flow-phases.ts @@ -198,9 +198,10 @@ export async function runInitialOnboardFlowSlice { context: { value: 1 }, runtime: runtime("preflight").runtime, phases: [phase("preflight", 2)], - resume: false, runWhenState: ["preflight"], compatibilityWhenState: ["provider_selection"], runSlice, @@ -117,7 +116,6 @@ describe("runLiveOnboardFlowSlice", () => { })), }, ], - resume: true, runWhenState: ["preflight"], compatibilityWhenState: ["provider_selection"], runSlice, @@ -141,7 +139,6 @@ describe("runLiveOnboardFlowSlice", () => { context: { value: 1 }, runtime: liveRuntime.runtime, phases: [phase("preflight", 2)], - resume: true, runWhenState: ["preflight"], compatibilityWhenState: ["preflight"], runSlice, @@ -163,7 +160,6 @@ describe("runLiveOnboardFlowSlice", () => { context: { value: 1 }, runtime: liveRuntime.runtime, phases: [phase("preflight", 2)], - resume: false, runWhenState: ["preflight"], compatibilityWhenState: ["provider_selection"], runSlice, @@ -185,7 +181,6 @@ describe("runLiveOnboardFlowSlice", () => { context: { value: 1 }, runtime: liveRuntime.runtime, phases: [blocked], - resume: false, runWhenState: ["provider_selection"], compatibilityWhenState: ["inference", "sandbox"], runSlice, @@ -209,7 +204,6 @@ describe("runLiveOnboardFlowSlice", () => { context: { value: 1 }, runtime: liveRuntime.runtime, phases: [first, second], - resume: true, runWhenState: ["preflight"], compatibilityWhenState: ["provider_selection"], runSlice: vi.fn(), @@ -231,7 +225,6 @@ describe("runLiveOnboardFlowSlice", () => { context: { value: 1 }, runtime: liveRuntime.runtime, phases: [phase("preflight", 2, [])], - resume: true, runWhenState: ["preflight"], compatibilityWhenState: ["provider_selection"], runSlice: vi.fn(), @@ -254,7 +247,6 @@ describe("runLiveOnboardFlowSlice", () => { context: { value: 1 }, runtime: liveRuntime.runtime, phases: [phase("preflight", 2), later], - resume: true, runWhenState: ["preflight"], compatibilityWhenState: ["provider_selection"], runSlice: vi.fn(), diff --git a/src/lib/onboard/machine/live-flow-slice.ts b/src/lib/onboard/machine/live-flow-slice.ts index 1294b2bf488..883f507de77 100644 --- a/src/lib/onboard/machine/live-flow-slice.ts +++ b/src/lib/onboard/machine/live-flow-slice.ts @@ -14,7 +14,6 @@ export interface LiveOnboardFlowSliceOptions { context: Context; runtime: OnboardMachineRunnerRuntime; phases: readonly OnboardSequencePhase[]; - resume: boolean; runWhenState: readonly OnboardMachineState[]; compatibilityWhenState?: readonly OnboardMachineState[]; runSlice(options: { @@ -77,14 +76,16 @@ export async function runLiveOnboardFlowSlice({ context, runtime, phases, - resume, runWhenState, compatibilityWhenState = [], runSlice, applyCompatibleResult, }: LiveOnboardFlowSliceOptions): Promise> { const current = await runtime.session(); - if (!resume && runWhenState.includes(current.machine.state)) { + if ( + runWhenState.includes(current.machine.state) && + !compatibilityWhenState.includes(current.machine.state) + ) { return runSlice({ context, runtime, phases }); } if (!compatibilityWhenState.includes(current.machine.state)) { From 07543b8b44b937dfc05d2dd467c42fc12ebf7f60 Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Mon, 22 Jun 2026 23:08:56 -0700 Subject: [PATCH 3/4] test(onboard): cover undeclared resume slice states --- .../onboard/machine/live-flow-slice.test.ts | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/lib/onboard/machine/live-flow-slice.test.ts b/src/lib/onboard/machine/live-flow-slice.test.ts index 80a70ceba62..ed0e231e7d3 100644 --- a/src/lib/onboard/machine/live-flow-slice.test.ts +++ b/src/lib/onboard/machine/live-flow-slice.test.ts @@ -198,6 +198,30 @@ describe("runLiveOnboardFlowSlice", () => { expect(applyCompatibleResult).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 applyCompatibleResult = vi.fn(async () => undefined); + + await expect( + runLiveOnboardFlowSlice({ + context: { value: 1 }, + runtime: liveRuntime.runtime, + phases: [blocked], + resume: true, + runWhenState: ["preflight"], + compatibilityWhenState: ["sandbox"], + runSlice, + applyCompatibleResult, + }), + ).rejects.toBeInstanceOf(UnexpectedLiveOnboardFlowSliceStateError); + + expect(runSlice).not.toHaveBeenCalled(); + expect(blocked.run).not.toHaveBeenCalled(); + expect(applyCompatibleResult).not.toHaveBeenCalled(); + }); + it("rejects duplicate compatibility phases before running side effects", async () => { const liveRuntime = runtime("provider_selection"); const first = phase("preflight", 2); From 3c9351aeca3c3f7d31384302e29d9db8474880e9 Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Tue, 23 Jun 2026 15:11:39 -0700 Subject: [PATCH 4/4] test(onboard): cover fresh slice entry strict paths --- .../onboard/machine/core-flow-phases.test.ts | 70 ++++++++++++++++++- .../machine/initial-flow-phases.test.ts | 55 +++++++++++++++ 2 files changed, 124 insertions(+), 1 deletion(-) diff --git a/src/lib/onboard/machine/core-flow-phases.test.ts b/src/lib/onboard/machine/core-flow-phases.test.ts index 7048349d83f..431891000b6 100644 --- a/src/lib/onboard/machine/core-flow-phases.test.ts +++ b/src/lib/onboard/machine/core-flow-phases.test.ts @@ -11,7 +11,7 @@ import { } from "./core-flow-phases"; import type { OnboardFlowContext } from "./flow-context"; import type { OnboardStateResult } from "./result"; -import { advanceTo } from "./result"; +import { advanceTo, branchTo } from "./result"; import type { OnboardSequencePhase } from "./sequence-runner"; type Agent = { name: string }; @@ -274,6 +274,74 @@ describe("core onboard flow phases", () => { }); }); + it("uses the strict runner for fresh provider selection sessions", async () => { + const calls: string[] = []; + const applied: string[] = []; + let runtimeSession = createSession({ + machine: { + version: 1, + state: "provider_selection", + stateEnteredAt: "2026-06-09T00:00:00.000Z", + revision: 1, + }, + }); + const phases: readonly OnboardSequencePhase[] = [ + { + state: "provider_selection", + run: (ctx) => { + calls.push("provider_selection"); + return { + context: ctx, + result: [ + advanceTo("inference", { metadata: { state: "provider_selection" } }), + advanceTo("sandbox", { metadata: { state: "inference" } }), + ], + }; + }, + }, + { + 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({ model: "nvidia/test", provider: "nim" }), + runtime: { + session: async () => runtimeSession, + applyResult: async (stateResult) => { + const next = (stateResult as ReturnType).next; + applied.push(next); + runtimeSession = createSession({ + machine: { + version: 1, + state: next, + stateEnteredAt: "2026-06-09T00:03:00.000Z", + revision: runtimeSession.machine.revision + 1, + }, + }); + return runtimeSession; + }, + }, + phases, + resume: false, + recordStateResult: async () => { + throw new Error("compatibility recorder should not run"); + }, + }); + + expect(calls).toEqual(["provider_selection", "sandbox"]); + expect(applied).toEqual(["inference", "sandbox", "openclaw"]); + expect(result.context.sandboxName).toBe("created-sandbox"); + expect(result.session.machine.state).toBe("openclaw"); + }); + it("records each phase result on the resume compatibility path", async () => { const recorded: string[] = []; const phases: readonly OnboardSequencePhase< diff --git a/src/lib/onboard/machine/initial-flow-phases.test.ts b/src/lib/onboard/machine/initial-flow-phases.test.ts index 342f26e6ebb..7801c0f2f80 100644 --- a/src/lib/onboard/machine/initial-flow-phases.test.ts +++ b/src/lib/onboard/machine/initial-flow-phases.test.ts @@ -493,6 +493,61 @@ describe("initial onboard flow phases", () => { expect(phase.run).not.toHaveBeenCalled(); }); + it("uses the strict runner for fresh preflight sessions", async () => { + const order: string[] = []; + const applied: string[] = []; + const session = createSession({ + machine: { + version: 1, + state: "preflight", + stateEnteredAt: "2026-06-09T00:00:00.000Z", + revision: 1, + }, + }); + const phases: readonly OnboardSequencePhase[] = [ + { + state: "preflight", + run: (ctx) => { + order.push("preflight"); + return { context: ctx, result: advanceTo("gateway") }; + }, + }, + { + state: "gateway", + run: (ctx) => { + order.push("gateway"); + return { context: ctx, result: advanceTo("provider_selection") }; + }, + }, + ]; + + const result = await runInitialOnboardFlowSlice({ + context: context(), + runtime: { + session: async () => session, + applyResult: async (stateResult) => { + const next = (stateResult as ReturnType).next; + applied.push(next); + session.machine = { + ...session.machine, + state: next, + revision: session.machine.revision + 1, + }; + return session; + }, + }, + phases, + resume: false, + recordStateResult: async () => { + throw new Error("compatibility recorder should not run"); + }, + }); + + expect(order).toEqual(["preflight", "gateway"]); + expect(applied).toEqual(["gateway", "provider_selection"]); + expect(result.session.machine.state).toBe("provider_selection"); + }); + it("uses the strict runner for fresh init sessions", async () => { const order: string[] = []; const session = createSession();