Skip to content

Commit 5f59625

Browse files
committed
feat(durable): project runs into taskflow state
1 parent 530df0f commit 5f59625

2 files changed

Lines changed: 343 additions & 0 deletions

File tree

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import fs from "node:fs";
2+
import os from "node:os";
3+
import path from "node:path";
4+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
5+
import {
6+
createManagedTaskFlow,
7+
getTaskFlowById,
8+
resetTaskFlowRegistryForTests,
9+
} from "../tasks/task-flow-runtime-internal.js";
10+
import { openDurableWorkflowSqliteStore } from "./sqlite-store.js";
11+
import { syncDurableRunToTaskFlow } from "./taskflow-bridge.js";
12+
13+
describe("durable taskflow bridge", () => {
14+
beforeEach(() => {
15+
resetTaskFlowRegistryForTests({ persist: false });
16+
});
17+
18+
afterEach(() => {
19+
resetTaskFlowRegistryForTests({ persist: false });
20+
});
21+
22+
it("projects waiting child durable state into TaskFlow state and wait metadata", () => {
23+
const flow = createManagedTaskFlow({
24+
controllerId: "durable-test",
25+
ownerKey: "agent:main:main",
26+
notifyPolicy: "state_changes",
27+
goal: "Coordinate children",
28+
status: "running",
29+
});
30+
expect(flow).toBeTruthy();
31+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-durable-taskflow-"));
32+
const store = openDurableWorkflowSqliteStore({
33+
path: path.join(dir, "openclaw.sqlite"),
34+
});
35+
try {
36+
const parent = store.createRun({
37+
workflowId: "test.parent",
38+
status: "waiting_child",
39+
recoveryState: "waiting_child",
40+
metadata: {
41+
taskFlowId: flow!.flowId,
42+
taskId: "task-parent",
43+
sessionKey: "agent:main:main",
44+
},
45+
now: 100,
46+
});
47+
store.createStep({
48+
workflowRunId: parent.workflowRunId,
49+
stepId: "subagents",
50+
stepType: "fan_in",
51+
status: "waiting",
52+
recoveryState: "waiting_child",
53+
now: 110,
54+
});
55+
const child = store.createRun({
56+
workflowId: "test.child",
57+
status: "running",
58+
recoveryState: "running",
59+
now: 120,
60+
});
61+
store.createLink({
62+
parentWorkflowRunId: parent.workflowRunId,
63+
parentStepId: "subagents",
64+
childWorkflowRunId: child.workflowRunId,
65+
linkType: "subagent",
66+
status: "running",
67+
now: 125,
68+
});
69+
70+
const result = syncDurableRunToTaskFlow({
71+
store,
72+
workflowRunId: parent.workflowRunId,
73+
});
74+
75+
expect(result).toMatchObject({
76+
synced: true,
77+
flowId: flow!.flowId,
78+
status: "waiting",
79+
});
80+
expect(getTaskFlowById(flow!.flowId)).toMatchObject({
81+
status: "waiting",
82+
currentStep: "subagents",
83+
stateJson: {
84+
durable: {
85+
workflowRunId: parent.workflowRunId,
86+
external: {
87+
taskId: "task-parent",
88+
},
89+
children: {
90+
total: 1,
91+
running: 1,
92+
open: 1,
93+
},
94+
},
95+
},
96+
waitJson: {
97+
durable: {
98+
waitingReason: "child",
99+
workflowRunId: parent.workflowRunId,
100+
stepId: "subagents",
101+
},
102+
},
103+
});
104+
} finally {
105+
store.close();
106+
fs.rmSync(dir, { recursive: true, force: true });
107+
}
108+
});
109+
110+
it("projects terminal durable state into TaskFlow completion", () => {
111+
const flow = createManagedTaskFlow({
112+
controllerId: "durable-test",
113+
ownerKey: "agent:main:main",
114+
notifyPolicy: "state_changes",
115+
goal: "Complete durable work",
116+
status: "running",
117+
});
118+
expect(flow).toBeTruthy();
119+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-durable-taskflow-"));
120+
const store = openDurableWorkflowSqliteStore({
121+
path: path.join(dir, "openclaw.sqlite"),
122+
});
123+
try {
124+
const run = store.createRun({
125+
workflowId: "test.task",
126+
status: "running",
127+
recoveryState: "running",
128+
metadata: { taskFlowId: flow!.flowId },
129+
now: 100,
130+
});
131+
store.updateRun({
132+
workflowRunId: run.workflowRunId,
133+
status: "succeeded",
134+
recoveryState: "terminal",
135+
completedAt: 150,
136+
now: 150,
137+
});
138+
139+
const result = syncDurableRunToTaskFlow({
140+
store,
141+
workflowRunId: run.workflowRunId,
142+
});
143+
144+
expect(result).toMatchObject({
145+
synced: true,
146+
status: "succeeded",
147+
});
148+
expect(getTaskFlowById(flow!.flowId)).toMatchObject({
149+
status: "succeeded",
150+
endedAt: 150,
151+
waitJson: null,
152+
});
153+
} finally {
154+
store.close();
155+
fs.rmSync(dir, { recursive: true, force: true });
156+
}
157+
});
158+
});

‎src/durable/taskflow-bridge.ts‎

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
import type { JsonValue, TaskFlowStatus } from "../tasks/task-flow-registry.types.js";
2+
import {
3+
getTaskFlowById,
4+
updateFlowRecordByIdExpectedRevision,
5+
} from "../tasks/task-flow-runtime-internal.js";
6+
import {
7+
buildDurableCoordinationProjection,
8+
buildDurableTaskFlowStateProjection,
9+
mergeDurableProjectionIntoJsonObject,
10+
type DurableCoordinationProjection,
11+
} from "./coordination-projection.js";
12+
import type { DurableWorkflowStore } from "./types.js";
13+
14+
export type DurableTaskFlowSyncResult =
15+
| {
16+
synced: true;
17+
flowId: string;
18+
workflowRunId: string;
19+
status: TaskFlowStatus;
20+
}
21+
| {
22+
synced: false;
23+
reason:
24+
| "run_not_found"
25+
| "flow_not_bound"
26+
| "flow_not_found"
27+
| "revision_conflict"
28+
| "persist_failed";
29+
workflowRunId: string;
30+
flowId?: string;
31+
};
32+
33+
function isJsonValue(value: unknown): value is JsonValue {
34+
if (
35+
value === null ||
36+
typeof value === "string" ||
37+
typeof value === "number" ||
38+
typeof value === "boolean"
39+
) {
40+
return true;
41+
}
42+
if (Array.isArray(value)) {
43+
return value.every(isJsonValue);
44+
}
45+
if (value && typeof value === "object") {
46+
return Object.values(value).every(isJsonValue);
47+
}
48+
return false;
49+
}
50+
51+
function toJsonValue(value: Record<string, unknown>): JsonValue {
52+
return isJsonValue(value) ? value : {};
53+
}
54+
55+
function mapProjectionToFlowStatus(projection: DurableCoordinationProjection): TaskFlowStatus {
56+
switch (projection.status) {
57+
case "received":
58+
case "queued":
59+
return "queued";
60+
case "running":
61+
return "running";
62+
case "waiting":
63+
case "waiting_signal":
64+
case "waiting_timer":
65+
case "waiting_child":
66+
case "retry_scheduled":
67+
return "waiting";
68+
case "succeeded":
69+
return "succeeded";
70+
case "failed":
71+
return "failed";
72+
case "cancelled":
73+
return "cancelled";
74+
case "lost":
75+
return "lost";
76+
}
77+
}
78+
79+
function buildWaitJson(projection: DurableCoordinationProjection): JsonValue | null {
80+
if (!projection.waitingReason) {
81+
return null;
82+
}
83+
return {
84+
durable: {
85+
waitingReason: projection.waitingReason,
86+
workflowRunId: projection.workflowRunId,
87+
...(projection.currentStepId ? { stepId: projection.currentStepId } : {}),
88+
updatedAt: projection.updatedAt,
89+
},
90+
};
91+
}
92+
93+
export function buildDurableTaskFlowPatch(projection: DurableCoordinationProjection): {
94+
status: TaskFlowStatus;
95+
currentStep?: string | null;
96+
stateJson: JsonValue;
97+
waitJson: JsonValue | null;
98+
endedAt?: number | null;
99+
updatedAt: number;
100+
} {
101+
const status = mapProjectionToFlowStatus(projection);
102+
const durableState = buildDurableTaskFlowStateProjection(projection);
103+
return {
104+
status,
105+
currentStep: projection.currentStepId ?? null,
106+
stateJson: toJsonValue(mergeDurableProjectionIntoJsonObject(undefined, durableState)),
107+
waitJson: status === "waiting" ? buildWaitJson(projection) : null,
108+
endedAt:
109+
status === "succeeded" || status === "failed" || status === "cancelled" || status === "lost"
110+
? (projection.completedAt ?? projection.updatedAt)
111+
: null,
112+
updatedAt: projection.updatedAt,
113+
};
114+
}
115+
116+
function tryUpdateTaskFlow(params: {
117+
flowId: string;
118+
projection: DurableCoordinationProjection;
119+
}): DurableTaskFlowSyncResult {
120+
const flow = getTaskFlowById(params.flowId);
121+
if (!flow) {
122+
return {
123+
synced: false,
124+
reason: "flow_not_found",
125+
workflowRunId: params.projection.workflowRunId,
126+
flowId: params.flowId,
127+
};
128+
}
129+
const patch = buildDurableTaskFlowPatch(params.projection);
130+
const result = updateFlowRecordByIdExpectedRevision({
131+
flowId: flow.flowId,
132+
expectedRevision: flow.revision,
133+
patch,
134+
});
135+
if (result.applied) {
136+
return {
137+
synced: true,
138+
flowId: result.flow.flowId,
139+
workflowRunId: params.projection.workflowRunId,
140+
status: result.flow.status,
141+
};
142+
}
143+
return {
144+
synced: false,
145+
reason: result.reason === "not_found" ? "flow_not_found" : result.reason,
146+
workflowRunId: params.projection.workflowRunId,
147+
flowId: params.flowId,
148+
};
149+
}
150+
151+
export function syncDurableRunToTaskFlow(params: {
152+
store: DurableWorkflowStore;
153+
workflowRunId: string;
154+
}): DurableTaskFlowSyncResult {
155+
const run = params.store.getRun(params.workflowRunId);
156+
if (!run) {
157+
return { synced: false, reason: "run_not_found", workflowRunId: params.workflowRunId };
158+
}
159+
const projection = buildDurableCoordinationProjection({
160+
run,
161+
steps: params.store.listSteps(run.workflowRunId),
162+
childLinks: params.store.listChildLinks(run.workflowRunId),
163+
refs: params.store.listRefs(run.workflowRunId),
164+
});
165+
const flowId = projection.external.taskFlowId;
166+
if (!flowId) {
167+
return { synced: false, reason: "flow_not_bound", workflowRunId: run.workflowRunId };
168+
}
169+
const first = tryUpdateTaskFlow({ flowId, projection });
170+
if (first.synced || first.reason !== "revision_conflict") {
171+
return first;
172+
}
173+
return tryUpdateTaskFlow({ flowId, projection });
174+
}
175+
176+
export function syncDurableRunToTaskFlowBestEffort(params: {
177+
store: DurableWorkflowStore;
178+
workflowRunId: string;
179+
}): void {
180+
try {
181+
syncDurableRunToTaskFlow(params);
182+
} catch {
183+
// Projection writes must never break live durable recovery or agent runtime paths.
184+
}
185+
}

0 commit comments

Comments
 (0)