Skip to content
70 changes: 69 additions & 1 deletion src/lib/onboard/machine/core-flow-phases.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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<CoreContext>[] = [
{
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<typeof advanceTo>).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<
Expand Down
23 changes: 12 additions & 11 deletions src/lib/onboard/machine/core-flow-phases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,19 @@ export async function runCoreOnboardFlowSlice<Context extends OnboardFlowContext
context: options.context,
runtime: options.runtime,
phases: options.phases,
resume: options.resume,
runWhenState: ["provider_selection"],
compatibilityWhenState: [
"provider_selection",
"inference",
"sandbox",
"openclaw",
"agent_setup",
"policies",
"finalizing",
"post_verify",
],
compatibilityWhenState: options.resume
? [
"provider_selection",
"inference",
"sandbox",
"openclaw",
"agent_setup",
"policies",
"finalizing",
"post_verify",
]
: ["inference", "sandbox", "openclaw", "agent_setup"],
runSlice: runCoreOnboardFlowSequence,
applyCompatibleResult: options.recordStateResult,
});
Expand Down
5 changes: 3 additions & 2 deletions src/lib/onboard/machine/final-flow-phases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,10 @@ export async function runFinalOnboardFlowSlice<Context extends OnboardFlowContex
context: options.context,
runtime: withAfterPoliciesResultApplied(options.runtime, options.afterPoliciesResultApplied),
phases: withContextObserver(options.phases, options.onContextUpdated),
resume: options.resume,
runWhenState: ["openclaw", "agent_setup"],
compatibilityWhenState: ["openclaw", "agent_setup", "policies", "finalizing", "post_verify"],
compatibilityWhenState: options.resume
? ["openclaw", "agent_setup", "policies", "finalizing", "post_verify"]
: ["policies", "finalizing", "post_verify"],
runSlice: runFinalOnboardFlowSequence,
applyCompatibleResult: async (stateResult) => {
await options.recordStateResult(stateResult);
Expand Down
55 changes: 55 additions & 0 deletions src/lib/onboard/machine/initial-flow-phases.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Context>[] = [
{
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<typeof advanceTo>).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();
Expand Down
29 changes: 15 additions & 14 deletions src/lib/onboard/machine/initial-flow-phases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,22 @@ export async function runInitialOnboardFlowSlice<Context extends OnboardFlowCont
context: options.context,
runtime: options.runtime,
phases: options.phases,
resume: options.resume,
runWhenState: ["init", "preflight"],
compatibilityWhenState: [
"init",
"preflight",
"gateway",
"provider_selection",
"inference",
"sandbox",
"openclaw",
"agent_setup",
"policies",
"finalizing",
"post_verify",
],
compatibilityWhenState: options.resume
? [
"init",
"preflight",
"gateway",
"provider_selection",
"inference",
"sandbox",
"openclaw",
"agent_setup",
"policies",
"finalizing",
"post_verify",
]
: ["gateway", "provider_selection"],
runSlice: runInitialOnboardFlowSequence,
applyCompatibleResult: options.recordStateResult,
});
Expand Down
10 changes: 1 addition & 9 deletions src/lib/onboard/machine/live-flow-slice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ describe("runLiveOnboardFlowSlice", () => {
context: { value: 1 },
runtime: runtime("preflight").runtime,
phases: [phase("preflight", 2)],
resume: false,
runWhenState: ["preflight"],
compatibilityWhenState: ["provider_selection"],
runSlice,
Expand Down Expand Up @@ -117,7 +116,6 @@ describe("runLiveOnboardFlowSlice", () => {
})),
},
],
resume: true,
runWhenState: ["preflight"],
compatibilityWhenState: ["provider_selection"],
runSlice,
Expand All @@ -141,7 +139,6 @@ describe("runLiveOnboardFlowSlice", () => {
context: { value: 1 },
runtime: liveRuntime.runtime,
phases: [phase("preflight", 2)],
resume: true,
runWhenState: ["preflight"],
compatibilityWhenState: ["preflight"],
runSlice,
Expand All @@ -163,7 +160,6 @@ describe("runLiveOnboardFlowSlice", () => {
context: { value: 1 },
runtime: liveRuntime.runtime,
phases: [phase("preflight", 2)],
resume: false,
runWhenState: ["preflight"],
compatibilityWhenState: ["provider_selection"],
runSlice,
Expand All @@ -185,7 +181,6 @@ describe("runLiveOnboardFlowSlice", () => {
context: { value: 1 },
runtime: liveRuntime.runtime,
phases: [blocked],
resume: false,
runWhenState: ["provider_selection"],
compatibilityWhenState: ["inference", "sandbox"],
runSlice,
Expand All @@ -209,7 +204,7 @@ describe("runLiveOnboardFlowSlice", () => {
context: { value: 1 },
runtime: liveRuntime.runtime,
phases: [blocked],
resume: true,

runWhenState: ["preflight"],
compatibilityWhenState: ["sandbox"],
runSlice,
Expand All @@ -233,7 +228,6 @@ describe("runLiveOnboardFlowSlice", () => {
context: { value: 1 },
runtime: liveRuntime.runtime,
phases: [first, second],
resume: true,
runWhenState: ["preflight"],
compatibilityWhenState: ["provider_selection"],
runSlice: vi.fn(),
Expand All @@ -255,7 +249,6 @@ describe("runLiveOnboardFlowSlice", () => {
context: { value: 1 },
runtime: liveRuntime.runtime,
phases: [phase("preflight", 2, [])],
resume: true,
runWhenState: ["preflight"],
compatibilityWhenState: ["provider_selection"],
runSlice: vi.fn(),
Expand All @@ -278,7 +271,6 @@ describe("runLiveOnboardFlowSlice", () => {
context: { value: 1 },
runtime: liveRuntime.runtime,
phases: [phase("preflight", 2), later],
resume: true,
runWhenState: ["preflight"],
compatibilityWhenState: ["provider_selection"],
runSlice: vi.fn(),
Expand Down
7 changes: 4 additions & 3 deletions src/lib/onboard/machine/live-flow-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export interface LiveOnboardFlowSliceOptions<Context> {
context: Context;
runtime: OnboardMachineRunnerRuntime;
phases: readonly OnboardSequencePhase<Context>[];
resume: boolean;
runWhenState: readonly OnboardMachineState[];
compatibilityWhenState?: readonly OnboardMachineState[];
runSlice(options: {
Expand Down Expand Up @@ -77,14 +76,16 @@ export async function runLiveOnboardFlowSlice<Context>({
context,
runtime,
phases,
resume,
runWhenState,
compatibilityWhenState = [],
runSlice,
applyCompatibleResult,
}: LiveOnboardFlowSliceOptions<Context>): Promise<OnboardMachineRunnerResult<Context>> {
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)) {
Expand Down