Skip to content

Commit d3355ea

Browse files
committed
fix(workboard): recover interrupted worker launches
Persist prepared, accepted, and failed launch phases so Gateway restart reconciliation cannot leave cards permanently running between launch preparation and worker acceptance.
1 parent 6c26bc7 commit d3355ea

10 files changed

Lines changed: 978 additions & 185 deletions

File tree

‎extensions/workboard/src/dispatcher-ownership.test.ts‎

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ describe("Workboard dispatcher ownership", () => {
567567
status: "ready",
568568
workspaceAccess: { unrestricted: true },
569569
});
570-
vi.spyOn(store, "enrichExecutionAssociation").mockRejectedValue(
570+
vi.spyOn(store, "acceptExecutionLaunch").mockRejectedValue(
571571
new Error("execution enrichment unavailable"),
572572
);
573573
let provisionalRunId = "";
@@ -584,6 +584,15 @@ describe("Workboard dispatcher ownership", () => {
584584
sessionKey: input.sessionKey,
585585
runId: provisionalRunId,
586586
},
587+
metadata: {
588+
automation: {
589+
launch: {
590+
phase: "prepared",
591+
requestedSessionKey: input.sessionKey,
592+
provisionalRunId,
593+
},
594+
},
595+
},
587596
});
588597
return { sessionKey: canonicalSessionKey, runId: "accepted-run" };
589598
});
@@ -619,6 +628,7 @@ describe("Workboard dispatcher ownership", () => {
619628
runId: provisionalRunId,
620629
execution: { status: "running", runId: provisionalRunId },
621630
metadata: {
631+
automation: { launch: { phase: "prepared", provisionalRunId } },
622632
claim: { ownerId: "workboard-dispatcher" },
623633
workerLogs: [expect.objectContaining({ runId: "accepted-run" })],
624634
},
@@ -642,6 +652,56 @@ describe("Workboard dispatcher ownership", () => {
642652
},
643653
);
644654

655+
it("marks a prepared launch accepted after Gateway acceptance", async () => {
656+
const store = new WorkboardStore(createMemoryStore());
657+
const card = await store.create({
658+
title: "Worker with durable acceptance",
659+
status: "ready",
660+
workspaceAccess: { unrestricted: true },
661+
});
662+
const canonicalSessionKey = `agent:worker:subagent:workboard-default-${card.id}`;
663+
let provisionalRunId = "";
664+
const run = vi.fn().mockImplementation(async (input) => {
665+
provisionalRunId = input.idempotencyKey;
666+
await expect(store.get(card.id)).resolves.toMatchObject({
667+
sessionKey: input.sessionKey,
668+
runId: provisionalRunId,
669+
metadata: {
670+
automation: {
671+
launch: {
672+
phase: "prepared",
673+
requestedSessionKey: input.sessionKey,
674+
provisionalRunId,
675+
},
676+
},
677+
},
678+
});
679+
return { sessionKey: canonicalSessionKey, runId: "accepted-run" };
680+
});
681+
682+
await dispatchAndStartWorkboardCards({
683+
store,
684+
subagent: { run },
685+
options: { maxStarts: 1 },
686+
});
687+
688+
await expect(store.get(card.id)).resolves.toMatchObject({
689+
sessionKey: canonicalSessionKey,
690+
runId: "accepted-run",
691+
metadata: {
692+
automation: {
693+
launch: {
694+
phase: "accepted",
695+
requestedSessionKey: expect.any(String),
696+
provisionalRunId,
697+
acceptedSessionKey: canonicalSessionKey,
698+
acceptedRunId: "accepted-run",
699+
},
700+
},
701+
},
702+
});
703+
});
704+
645705
it.each(["backlog", "todo", "ready"] as const)(
646706
"starts an exact dashboard card from %s",
647707
async (status) => {

‎extensions/workboard/src/dispatcher.ts‎

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from "node:path";
33
import type {
44
WorkboardCard,
55
WorkboardExecution,
6+
WorkboardLaunchState,
67
WorkboardWorkspace,
78
} from "@openclaw/workboard-contract";
89
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
@@ -71,6 +72,8 @@ type WorkboardDispatchAndStartResult = WorkboardDispatchResult & {
7172
startFailures: WorkboardStartFailure[];
7273
};
7374

75+
type WorkboardPreparedLaunch = Extract<WorkboardLaunchState, { phase: "prepared" }>;
76+
7477
type WorkboardDispatchStartParams = {
7578
store: WorkboardStore;
7679
subagent: WorkboardSubagentRuntime;
@@ -364,8 +367,8 @@ async function runWorkboardDispatch(
364367
let materializedWorkspace: WorkboardWorkspace | undefined;
365368
let implicitWorkspaceCwd: string | undefined;
366369
let runStarted = false;
367-
let launchIntentPersisted = false;
368370
let workspaceMutation: { before: WorkboardCard; after: WorkboardCard } | undefined;
371+
let preparedLaunch: WorkboardPreparedLaunch | undefined;
369372
const requestedWorkspace = card.metadata?.automation?.workspace;
370373
let workspaceAccess: WorkboardWorkspaceAccess;
371374
let targetWorkspace: string | undefined;
@@ -502,28 +505,14 @@ async function runWorkboardDispatch(
502505
);
503506
workspaceMutation = { before: workspaceBase, after: materializedCard };
504507
}
505-
const launchBase = await params.store.get(card.id);
506-
if (!launchBase) {
507-
throw new Error(`card not found: ${card.id}`);
508-
}
509-
const runId = `workboard:${card.id}:${launchBase.updatedAt}`;
510-
const launched = await params.store.update(
511-
card.id,
512-
{
513-
sessionKey,
514-
runId,
515-
execution: buildExecution({
516-
card: launchBase,
517-
sessionKey,
518-
runId,
519-
runtime: undefined,
520-
now,
521-
}),
522-
...(materializedWorkspace ? { workspace: materializedWorkspace } : {}),
523-
},
524-
{ expectedUpdatedAt: launchBase.updatedAt },
525-
);
526-
launchIntentPersisted = true;
508+
const prepared = await params.store.prepareExecutionLaunch(card.id, {
509+
requestedSessionKey: sessionKey,
510+
now,
511+
scope: { ownerId, token: claimValue },
512+
});
513+
const launched = prepared.card;
514+
preparedLaunch = prepared.launch;
515+
const runId = prepared.launch.provisionalRunId;
527516
const run = await params.subagent.run({
528517
sessionKey,
529518
message: buildWorkerPrompt({
@@ -556,15 +545,18 @@ async function runWorkboardDispatch(
556545
runId: run.runId,
557546
execution: acceptedExecution,
558547
};
559-
const updated = await params.store
560-
.enrichExecutionAssociation(card.id, {
561-
expectedSessionKey: sessionKey,
562-
expectedRunId: runId,
563-
sessionKey: acceptedSessionKey,
564-
runId: run.runId,
565-
execution: acceptedExecution,
566-
})
567-
.catch(() => acceptedCard);
548+
const updated =
549+
(await params.store
550+
.acceptExecutionLaunch(card.id, {
551+
expectedLaunch: prepared.launch,
552+
acceptedAt: Math.max(Date.now(), prepared.launch.preparedAt),
553+
expectedSessionKey: sessionKey,
554+
expectedRunId: runId,
555+
sessionKey: acceptedSessionKey,
556+
runId: run.runId,
557+
execution: acceptedExecution,
558+
})
559+
.catch(() => undefined)) ?? acceptedCard;
568560
acceptedStarts += 1;
569561
startedOwners.add(ownerId);
570562
started.push({
@@ -614,16 +606,20 @@ async function runWorkboardDispatch(
614606
continue;
615607
}
616608
try {
617-
await params.store.block(
618-
card.id,
619-
{
620-
ownerId,
621-
token: claimValue,
622-
reason: `Dispatcher could not start worker: ${message}`,
623-
},
624-
{ ownerId, token: claimValue },
625-
{ clearExecutionAssociation: launchIntentPersisted },
626-
);
609+
const reason = `Dispatcher could not start worker: ${message}`;
610+
if (preparedLaunch) {
611+
await params.store.failPreparedLaunch(card.id, {
612+
expectedLaunch: preparedLaunch,
613+
reason,
614+
failedAt: Date.now(),
615+
});
616+
} else {
617+
await params.store.block(
618+
card.id,
619+
{ ownerId, token: claimValue, reason },
620+
{ ownerId, token: claimValue },
621+
);
622+
}
627623
} catch {
628624
// Leave the original start failure visible; dispatch will diagnose stale claims later.
629625
}

0 commit comments

Comments
 (0)