Skip to content

Commit 17adc19

Browse files
committed
feat(workboard): accept durable coordination projections
1 parent 5f59625 commit 17adc19

6 files changed

Lines changed: 480 additions & 0 deletions

File tree

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// Maps Durable Core coordination projections into Workboard card patches.
2+
import type { WorkboardCardPatch } from "./store.js";
3+
import type {
4+
WorkboardDurableChildCounts,
5+
WorkboardDurableMetadata,
6+
WorkboardStatus,
7+
} from "./types.js";
8+
9+
type DurableProjectionLike = {
10+
workflowRunId?: unknown;
11+
workflowId?: unknown;
12+
workflowVersion?: unknown;
13+
status?: unknown;
14+
recoveryState?: unknown;
15+
waitingReason?: unknown;
16+
currentStepId?: unknown;
17+
updatedAt?: unknown;
18+
external?: unknown;
19+
children?: unknown;
20+
};
21+
22+
function stringValue(value: unknown): string | undefined {
23+
return typeof value === "string" && value.trim() ? value.trim() : undefined;
24+
}
25+
26+
function numberValue(value: unknown): number | undefined {
27+
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
28+
}
29+
30+
function recordValue(value: unknown): Record<string, unknown> {
31+
return value && typeof value === "object" && !Array.isArray(value)
32+
? (value as Record<string, unknown>)
33+
: {};
34+
}
35+
36+
function childCounts(value: unknown): WorkboardDurableChildCounts | undefined {
37+
const record = recordValue(value);
38+
const next: WorkboardDurableChildCounts = {};
39+
for (const key of [
40+
"total",
41+
"pending",
42+
"running",
43+
"succeeded",
44+
"failed",
45+
"cancelled",
46+
"lost",
47+
"terminal",
48+
"open",
49+
] as const) {
50+
const count = numberValue(record[key]);
51+
if (count !== undefined) {
52+
next[key] = Math.max(0, Math.trunc(count));
53+
}
54+
}
55+
return Object.keys(next).length ? next : undefined;
56+
}
57+
58+
function statusFromDurable(status: string | undefined): WorkboardStatus | undefined {
59+
switch (status) {
60+
case "received":
61+
case "queued":
62+
case "running":
63+
case "waiting":
64+
case "waiting_signal":
65+
case "waiting_timer":
66+
case "waiting_child":
67+
case "retry_scheduled":
68+
return "running";
69+
case "succeeded":
70+
return "review";
71+
case "failed":
72+
case "cancelled":
73+
case "lost":
74+
return "blocked";
75+
default:
76+
return undefined;
77+
}
78+
}
79+
80+
export function buildWorkboardDurableMetadata(
81+
projection: DurableProjectionLike,
82+
): WorkboardDurableMetadata | undefined {
83+
const external = recordValue(projection.external);
84+
const workflowRunId = stringValue(projection.workflowRunId);
85+
if (!workflowRunId) {
86+
return undefined;
87+
}
88+
const updatedAt = numberValue(projection.updatedAt) ?? Date.now();
89+
const children = childCounts(projection.children);
90+
const workflowId = stringValue(projection.workflowId);
91+
const workflowVersion = stringValue(projection.workflowVersion);
92+
const status = stringValue(projection.status);
93+
const recoveryState = stringValue(projection.recoveryState);
94+
const waitingReason = stringValue(projection.waitingReason);
95+
const currentStepId = stringValue(projection.currentStepId);
96+
const taskId = stringValue(external.taskId);
97+
const taskFlowId = stringValue(external.taskFlowId);
98+
const sessionKey = stringValue(external.sessionKey);
99+
const childSessionKey = stringValue(external.childSessionKey);
100+
const runId = stringValue(external.runId);
101+
const agentId = stringValue(external.agentId);
102+
const requesterAgentId = stringValue(external.requesterAgentId);
103+
return {
104+
workflowRunId,
105+
...(workflowId ? { workflowId } : {}),
106+
...(workflowVersion ? { workflowVersion } : {}),
107+
...(status ? { status } : {}),
108+
...(recoveryState ? { recoveryState } : {}),
109+
...(waitingReason ? { waitingReason } : {}),
110+
...(currentStepId ? { currentStepId } : {}),
111+
...(taskId ? { taskId } : {}),
112+
...(taskFlowId ? { taskFlowId } : {}),
113+
...(sessionKey ? { sessionKey } : {}),
114+
...(childSessionKey ? { childSessionKey } : {}),
115+
...(runId ? { runId } : {}),
116+
...(agentId ? { agentId } : {}),
117+
...(requesterAgentId ? { requesterAgentId } : {}),
118+
timelineCommand: `openclaw durable timeline ${workflowRunId}`,
119+
...(children ? { children } : {}),
120+
updatedAt,
121+
};
122+
}
123+
124+
export function buildWorkboardPatchFromDurableProjection(
125+
projection: DurableProjectionLike,
126+
): WorkboardCardPatch {
127+
const durable = buildWorkboardDurableMetadata(projection);
128+
if (!durable) {
129+
return {};
130+
}
131+
const status = statusFromDurable(durable.status);
132+
return {
133+
...(status ? { status } : {}),
134+
...((durable.sessionKey ?? durable.childSessionKey)
135+
? { sessionKey: durable.sessionKey ?? durable.childSessionKey }
136+
: {}),
137+
...(durable.runId ? { runId: durable.runId } : {}),
138+
...(durable.taskId ? { taskId: durable.taskId } : {}),
139+
metadata: {
140+
durable,
141+
lifecycleStatusSourceUpdatedAt: durable.updatedAt,
142+
},
143+
};
144+
}

‎extensions/workboard/src/gateway.test.ts‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ describe("workboard gateway methods", () => {
4848
"workboard.cards.list",
4949
"workboard.cards.create",
5050
"workboard.cards.update",
51+
"workboard.cards.applyDurableProjection",
5152
"workboard.cards.move",
5253
"workboard.cards.delete",
5354
"workboard.cards.comment",
@@ -97,6 +98,9 @@ describe("workboard gateway methods", () => {
9798
});
9899
expect(methods.get("workboard.cards.export")?.opts).toEqual({ scope: "operator.read" });
99100
expect(methods.get("workboard.cards.create")?.opts).toEqual({ scope: "operator.write" });
101+
expect(methods.get("workboard.cards.applyDurableProjection")?.opts).toEqual({
102+
scope: "operator.write",
103+
});
100104
expect(methods.get("workboard.cards.runs")?.opts).toEqual({ scope: "operator.read" });
101105
expect(methods.get("workboard.cards.attachments.get")?.opts).toEqual({
102106
scope: "operator.read",
@@ -184,6 +188,72 @@ describe("workboard gateway methods", () => {
184188
});
185189
});
186190

191+
it("applies durable projection updates through gateway", async () => {
192+
type RegisteredMethod = {
193+
handler: Parameters<OpenClawPluginApi["registerGatewayMethod"]>[1];
194+
opts: Parameters<OpenClawPluginApi["registerGatewayMethod"]>[2];
195+
};
196+
const methods = new Map<string, RegisteredMethod>();
197+
const api = {
198+
runtime: {
199+
state: {
200+
openKeyedStore: vi.fn(() => createMemoryStore()),
201+
},
202+
},
203+
registerGatewayMethod: vi.fn(
204+
(method: string, handler: RegisteredMethod["handler"], opts: RegisteredMethod["opts"]) => {
205+
methods.set(method, { handler, opts });
206+
},
207+
),
208+
} as unknown as OpenClawPluginApi;
209+
210+
registerWorkboardGatewayMethods({ api, store: new WorkboardStore(createMemoryStore()) });
211+
212+
const createRespond = vi.fn();
213+
await methods.get("workboard.cards.create")?.handler({
214+
params: { title: "Durable card", status: "ready" },
215+
respond: createRespond,
216+
} as never);
217+
const cardId = createRespond.mock.calls[0]?.[1]?.card.id;
218+
219+
const applyRespond = vi.fn();
220+
await methods.get("workboard.cards.applyDurableProjection")?.handler({
221+
params: {
222+
id: cardId,
223+
projection: {
224+
workflowRunId: "wfr-card",
225+
workflowId: "openclaw.subagent.run",
226+
status: "succeeded",
227+
recoveryState: "terminal",
228+
updatedAt: Date.now() + 1_000,
229+
external: {
230+
taskId: "task-card",
231+
childSessionKey: "agent:bo:subagent:workboard-card",
232+
runId: "run-card",
233+
},
234+
},
235+
},
236+
respond: applyRespond,
237+
} as never);
238+
239+
expect(applyRespond.mock.calls[0]?.[0]).toBe(true);
240+
expect(applyRespond.mock.calls[0]?.[1]).toMatchObject({
241+
card: {
242+
status: "review",
243+
taskId: "task-card",
244+
sessionKey: "agent:bo:subagent:workboard-card",
245+
runId: "run-card",
246+
metadata: {
247+
durable: {
248+
workflowRunId: "wfr-card",
249+
status: "succeeded",
250+
recoveryState: "terminal",
251+
},
252+
},
253+
},
254+
});
255+
});
256+
187257
it("validates labels from comma-separated gateway input", async () => {
188258
type RegisteredMethod = {
189259
handler: Parameters<OpenClawPluginApi["registerGatewayMethod"]>[1];

‎extensions/workboard/src/gateway.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
33
import type { OpenClawPluginApi } from "../api.js";
44
import { dispatchAndStartWorkboardCards } from "./dispatcher.js";
5+
import { buildWorkboardPatchFromDurableProjection } from "./durable-adapter.js";
56
import { WorkboardStore } from "./store.js";
67
import { WORKBOARD_STATUSES, type WorkboardCard } from "./types.js";
78

@@ -116,6 +117,25 @@ export function registerWorkboardGatewayMethods(params: {
116117
{ scope: WRITE_SCOPE },
117118
);
118119

120+
api.registerGatewayMethod(
121+
"workboard.cards.applyDurableProjection",
122+
async ({ params: requestParams, respond }) => {
123+
try {
124+
const projection = requestParams.projection ?? requestParams;
125+
const patch = buildWorkboardPatchFromDurableProjection(projection);
126+
if (!patch.metadata) {
127+
throw new Error("durable projection with workflowRunId is required.");
128+
}
129+
respond(true, {
130+
card: redactClaimToken(await store.update(readId(requestParams), patch)),
131+
});
132+
} catch (error) {
133+
respondError(respond, error);
134+
}
135+
},
136+
{ scope: WRITE_SCOPE },
137+
);
138+
119139
api.registerGatewayMethod(
120140
"workboard.cards.move",
121141
async ({ params: requestParams, respond }) => {

‎extensions/workboard/src/store.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from "node:path";
55
import { DatabaseSync } from "node:sqlite";
66
import { MAX_DATE_TIMESTAMP_MS } from "openclaw/plugin-sdk/number-runtime";
77
import { describe, expect, it, vi } from "vitest";
8+
import { buildWorkboardPatchFromDurableProjection } from "./durable-adapter.js";
89
import { createWorkboardSqliteStores } from "./sqlite-store.js";
910
import {
1011
WorkboardStore,
@@ -258,6 +259,71 @@ describe("WorkboardStore", () => {
258259
});
259260
});
260261

262+
it("preserves durable projection metadata when updating cards", async () => {
263+
const store = new WorkboardStore(createMemoryStore());
264+
const card = await store.create({ title: "Durable child", status: "ready" });
265+
const durableUpdatedAt = Date.now() + 1_000;
266+
267+
const updated = await store.update(
268+
card.id,
269+
buildWorkboardPatchFromDurableProjection({
270+
workflowRunId: "wfr_child",
271+
workflowId: "openclaw.subagent.run",
272+
workflowVersion: "1",
273+
status: "waiting_child",
274+
recoveryState: "waiting_child",
275+
waitingReason: "child",
276+
currentStepId: "subagents",
277+
updatedAt: durableUpdatedAt,
278+
external: {
279+
taskId: "task-child",
280+
taskFlowId: "flow-child",
281+
childSessionKey: "agent:bo:subagent:workboard-default-card",
282+
runId: "run-child",
283+
agentId: "bo",
284+
},
285+
children: {
286+
total: 2,
287+
running: 1,
288+
succeeded: 1,
289+
terminal: 1,
290+
open: 1,
291+
},
292+
}),
293+
);
294+
295+
expect(updated).toMatchObject({
296+
status: "running",
297+
sessionKey: "agent:bo:subagent:workboard-default-card",
298+
runId: "run-child",
299+
taskId: "task-child",
300+
metadata: {
301+
durable: {
302+
workflowRunId: "wfr_child",
303+
workflowId: "openclaw.subagent.run",
304+
status: "waiting_child",
305+
recoveryState: "waiting_child",
306+
waitingReason: "child",
307+
currentStepId: "subagents",
308+
taskId: "task-child",
309+
taskFlowId: "flow-child",
310+
childSessionKey: "agent:bo:subagent:workboard-default-card",
311+
runId: "run-child",
312+
agentId: "bo",
313+
timelineCommand: "openclaw durable timeline wfr_child",
314+
children: {
315+
total: 2,
316+
running: 1,
317+
succeeded: 1,
318+
terminal: 1,
319+
open: 1,
320+
},
321+
updatedAt: durableUpdatedAt,
322+
},
323+
},
324+
});
325+
});
326+
261327
it("ignores dependency links from generic metadata writes", async () => {
262328
const store = new WorkboardStore(createMemoryStore());
263329
const parent = await store.create({ title: "Parent" });

0 commit comments

Comments
 (0)