Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6b753bf
docs(onboard): document FSM migration target
cv May 27, 2026
fb1b32d
refactor(onboard): centralize machine state metadata
cv May 27, 2026
c3e4ad6
refactor(onboard): derive session step mapping from FSM metadata
cv May 27, 2026
603832c
refactor(onboard): derive progress labels from FSM metadata
cv May 27, 2026
4fad8e7
fix(onboard): emit lifecycle events for onboarding start
cv May 28, 2026
f99e9cb
fix(onboard): emit machine events for resume conflicts
cv May 28, 2026
2b60df4
refactor(onboard): introduce explicit state result types
cv May 28, 2026
30341b0
refactor(onboard): apply explicit state results through runtime
cv May 28, 2026
d4ad2d9
refactor(onboard): make finalization return FSM result
cv May 28, 2026
356c947
refactor(onboard): make agent setup return FSM result
cv May 28, 2026
2296519
refactor(onboard): make policy setup return FSM result
cv May 28, 2026
67a9a1e
refactor(onboard): make preflight and gateway return FSM results
cv May 28, 2026
46f4a49
refactor(onboard): make sandbox return branch FSM result
cv May 28, 2026
9cc15f5
refactor(onboard): return FSM results from provider inference
cv May 28, 2026
dbbb273
refactor(onboard): add FSM runner shell
cv May 28, 2026
6b27a0b
refactor(onboard): consume handler FSM results compatibly
cv May 28, 2026
44009ad
refactor(onboard): allow step recording without machine transitions
cv May 28, 2026
cd6e5f7
refactor(onboard): plumb step mutation options through runtime
cv May 28, 2026
e266e3b
refactor(onboard): add record-only FSM runner adapter
cv May 28, 2026
bf4da0b
refactor(onboard): return ordered provider FSM results
cv May 28, 2026
212ff4d
refactor(onboard): run live sequence with record-only steps
cv May 28, 2026
f69f60a
refactor(onboard): let FSM handlers return result sequences
cv May 29, 2026
727ac69
refactor(onboard): add sequence runner adapter
cv May 29, 2026
aeced21
merge(onboard): resolve sequence adapter conflicts
cv Jun 8, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
212 changes: 212 additions & 0 deletions src/lib/onboard/machine/sequence-runner.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { describe, expect, it } from "vitest";

import {
createSession,
filterSafeUpdates,
normalizeSession,
sanitizeFailure,
type Session,
type SessionUpdates,
} from "../../state/onboard-session";
import { advanceTo, branchTo, completeOnboardMachine, retryTo } from "./result";
import { OnboardRuntime, type OnboardRuntimeDeps } from "./runtime";
import {
buildOnboardSequenceHandlers,
DuplicateOnboardSequencePhaseError,
runOnboardSequenceWithRunner,
type OnboardSequencePhase,
} from "./sequence-runner";

interface SequenceContext {
attempt: number;
log: string[];
}

function cloneSession(session: Session): Session {
return normalizeSession(JSON.parse(JSON.stringify(session))) ?? session;
}

function createRuntime(initialSession: Session = createSession()) {
let session = cloneSession(initialSession);
const updateSession = (mutator: (value: Session) => Session | void): Session => {
session = cloneSession(mutator(cloneSession(session)) ?? session);
return cloneSession(session);
};
const deps: OnboardRuntimeDeps = {
loadSession: () => cloneSession(session),
createSession,
saveSession: (next) => {
session = cloneSession(next);
return cloneSession(session);
},
updateSession,
markStepStarted: () => cloneSession(session),
markStepComplete: (_stepName, updates: SessionUpdates = {}) =>
updateSession((current) => {
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" });
return current;
}),
markStepFailedRecordOnly: () => cloneSession(session),
completeSession: (updates: SessionUpdates = {}) =>
updateSession((current) => {
Object.assign(current, filterSafeUpdates(updates));
current.status = "complete";
current.resumable = false;
return current;
}),
filterSafeUpdates,
emitEvent: () => undefined,
now: () => "2026-05-29T00:00:00.000Z",
};
return new OnboardRuntime(deps);
}

function phase(
state: OnboardSequencePhase<SequenceContext>["state"],
run: OnboardSequencePhase<SequenceContext>["run"],
): OnboardSequencePhase<SequenceContext> {
return { state, run };
}

describe("onboard sequence runner", () => {
it("runs sequence phases through the strict FSM runner", async () => {
const phases: OnboardSequencePhase<SequenceContext>[] = [
phase("init", (context) => ({
context: { ...context, log: [...context.log, "init"] },
result: advanceTo("preflight"),
})),
phase("preflight", (context) => ({
context: { ...context, log: [...context.log, "preflight"] },
result: advanceTo("gateway"),
})),
phase("gateway", (context) => ({
context: { ...context, log: [...context.log, "gateway"] },
result: advanceTo("provider_selection"),
})),
phase("provider_selection", (context) => {
if (context.attempt === 0) {
return {
context: { attempt: 1, log: [...context.log, "provider:first"] },
result: [
advanceTo("inference", { metadata: { state: "provider_selection" } }),
retryTo("provider_selection", { metadata: { state: "inference" } }),
],
};
}
return {
context: { ...context, log: [...context.log, "provider:second"] },
result: [
advanceTo("inference", { metadata: { state: "provider_selection" } }),
advanceTo("sandbox", { metadata: { state: "inference" } }),
],
};
}),
phase("sandbox", (context) => ({
context: { ...context, log: [...context.log, "sandbox"] },
result: branchTo("openclaw"),
})),
phase("openclaw", (context) => ({
context: { ...context, log: [...context.log, "openclaw"] },
result: advanceTo("policies"),
})),
phase("policies", (context) => ({
context: { ...context, log: [...context.log, "policies"] },
result: advanceTo("finalizing"),
})),
phase("finalizing", (context) => ({
context: { ...context, log: [...context.log, "finalizing"] },
result: advanceTo("post_verify"),
})),
phase("post_verify", (context) => ({
context: { ...context, log: [...context.log, "post_verify"] },
result: completeOnboardMachine({ sandboxName: "my-assistant" }),
})),
];

const result = await runOnboardSequenceWithRunner({
context: { attempt: 0, log: [] },
runtime: createRuntime(),
phases,
});

expect(result.session).toMatchObject({
status: "complete",
sandboxName: "my-assistant",
machine: { state: "complete" },
});
expect(result.context).toEqual({
attempt: 1,
log: [
"init",
"preflight",
"gateway",
"provider:first",
"provider:second",
"sandbox",
"openclaw",
"policies",
"finalizing",
"post_verify",
],
});
});

it("passes custom sequence ownership through to the runner", async () => {
const result = await runOnboardSequenceWithRunner({
context: { attempt: 0, log: [] },
runtime: createRuntime(
createSession({
machine: {
version: 1,
state: "finalizing",
stateEnteredAt: "2026-05-29T00:00:00.000Z",
revision: 0,
},
}),
),
sequenceOwnership: { finalizing: ["post_verify"] },
phases: [
phase("finalizing", (context) => ({
context: { ...context, log: ["finalizing"] },
result: [
advanceTo("post_verify", { metadata: { state: "finalizing" } }),
completeOnboardMachine({}, { state: "post_verify" }),
],
})),
],
});

expect(result.session).toMatchObject({
status: "complete",
machine: { state: "complete" },
});
expect(result.context.log).toEqual(["finalizing"]);
});

it("rejects duplicate phases before running", () => {
expect(() =>
buildOnboardSequenceHandlers(
[
phase("preflight", (context) => ({ context, result: advanceTo("gateway") })),
phase("preflight", (context) => ({ context, result: advanceTo("gateway") })),
],
() => undefined,
),
).toThrow(DuplicateOnboardSequencePhaseError);
});
});
78 changes: 78 additions & 0 deletions src/lib/onboard/machine/sequence-runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import type { OnboardMachineRunnerOptions, OnboardStateHandlerResult } from "./runner";
import { runOnboardMachine, type OnboardMachineRunnerRuntime, type OnboardStateHandlers } from "./runner";
import type { OnboardNonTerminalMachineState } from "./types";

export interface OnboardSequencePhaseResult<Context> {
context: Context;
result: OnboardStateHandlerResult;
}

export interface OnboardSequencePhase<Context> {
state: OnboardNonTerminalMachineState;
run(context: Context): Promise<OnboardSequencePhaseResult<Context>> | OnboardSequencePhaseResult<Context>;
}

export interface OnboardSequenceRunnerOptions<Context> {
context: Context;
runtime: OnboardMachineRunnerRuntime;
phases: readonly OnboardSequencePhase<Context>[];
maxTransitions?: OnboardMachineRunnerOptions<Context>["maxTransitions"];
sequenceOwnership?: OnboardMachineRunnerOptions<Context>["sequenceOwnership"];
}

export class DuplicateOnboardSequencePhaseError extends Error {
readonly state: OnboardNonTerminalMachineState;

constructor(state: OnboardNonTerminalMachineState) {
super(`Duplicate onboarding sequence phase for state: ${state}`);
this.name = "DuplicateOnboardSequencePhaseError";
this.state = state;
}
}

export function buildOnboardSequenceHandlers<Context>(
phases: readonly OnboardSequencePhase<Context>[],
setPendingContext: (context: Context) => void,
): OnboardStateHandlers<Context> {
const handlers: OnboardStateHandlers<Context> = {};
for (const phase of phases) {
if (handlers[phase.state]) throw new DuplicateOnboardSequencePhaseError(phase.state);
handlers[phase.state] = async (context) => {
const phaseResult = await phase.run(context);
setPendingContext(phaseResult.context);
return phaseResult.result;
};
}
return handlers;
}

/**
* Adapter for migrating the existing manual onboard sequence onto the strict
* FSM runner.
*
* Each phase can keep constructing its rich next context while returning one or
* more explicit FSM results. The generic runner remains responsible for
* applying those results and validating transitions.
*/
export async function runOnboardSequenceWithRunner<Context>({
context: initialContext,
runtime,
phases,
maxTransitions,
sequenceOwnership,
}: OnboardSequenceRunnerOptions<Context>) {
let pendingContext = initialContext;
return runOnboardMachine({
context: initialContext,
runtime,
maxTransitions,
sequenceOwnership,
handlers: buildOnboardSequenceHandlers(phases, (context) => {
pendingContext = context;
}),
updateContext: () => pendingContext,
});
}
Loading