|
| 1 | +import fs from "node:fs"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | +import { runDurableExecutorOnce } from "./executor.js"; |
| 6 | +import { createDurableWorkflowRegistry } from "./registry.js"; |
| 7 | +import { openDurableWorkflowSqliteStore } from "./sqlite-store.js"; |
| 8 | + |
| 9 | +function tempStore() { |
| 10 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-durable-executor-")); |
| 11 | + const store = openDurableWorkflowSqliteStore({ |
| 12 | + path: path.join(dir, "workflows.sqlite"), |
| 13 | + }); |
| 14 | + return { |
| 15 | + store, |
| 16 | + cleanup: () => { |
| 17 | + store.close(); |
| 18 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 19 | + }, |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +describe("durable workflow executor", () => { |
| 24 | + it("claims a runnable step, runs a handler, records heartbeat, and completes the run", async () => { |
| 25 | + const { store, cleanup } = tempStore(); |
| 26 | + try { |
| 27 | + const registry = createDurableWorkflowRegistry(); |
| 28 | + registry.registerStepHandler("tool", (context) => { |
| 29 | + context.heartbeat({ phase: "testing" }); |
| 30 | + return { |
| 31 | + kind: "succeeded", |
| 32 | + output: { ok: true }, |
| 33 | + completeRun: true, |
| 34 | + }; |
| 35 | + }); |
| 36 | + const run = store.createRun({ |
| 37 | + workflowId: "test.workflow", |
| 38 | + status: "queued", |
| 39 | + recoveryState: "runnable", |
| 40 | + now: 100, |
| 41 | + }); |
| 42 | + const step = store.createStep({ |
| 43 | + workflowRunId: run.workflowRunId, |
| 44 | + stepType: "tool", |
| 45 | + status: "queued", |
| 46 | + recoveryState: "runnable", |
| 47 | + now: 110, |
| 48 | + }); |
| 49 | + let clock = 120; |
| 50 | + |
| 51 | + const result = await runDurableExecutorOnce({ |
| 52 | + store, |
| 53 | + registry, |
| 54 | + workerId: "worker-1", |
| 55 | + workflowId: "test.workflow", |
| 56 | + now: () => { |
| 57 | + clock += 10; |
| 58 | + return clock; |
| 59 | + }, |
| 60 | + }); |
| 61 | + |
| 62 | + expect(result).toEqual({ |
| 63 | + claimed: true, |
| 64 | + workflowRunId: run.workflowRunId, |
| 65 | + stepId: step.stepId, |
| 66 | + outcome: "succeeded", |
| 67 | + }); |
| 68 | + expect(store.getRun(run.workflowRunId)).toMatchObject({ |
| 69 | + status: "succeeded", |
| 70 | + recoveryState: "terminal", |
| 71 | + }); |
| 72 | + expect(store.listSteps(run.workflowRunId)).toMatchObject([ |
| 73 | + { |
| 74 | + stepId: step.stepId, |
| 75 | + status: "succeeded", |
| 76 | + recoveryState: "terminal", |
| 77 | + outputRef: expect.any(String), |
| 78 | + }, |
| 79 | + ]); |
| 80 | + expect(store.getTimeline(run.workflowRunId).map((event) => event.eventType)).toEqual([ |
| 81 | + "workflow.step.running", |
| 82 | + "workflow.step.heartbeat", |
| 83 | + "workflow.step.succeeded", |
| 84 | + ]); |
| 85 | + } finally { |
| 86 | + cleanup(); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + it("schedules retry timers for retryable handler failures", async () => { |
| 91 | + const { store, cleanup } = tempStore(); |
| 92 | + try { |
| 93 | + const registry = createDurableWorkflowRegistry(); |
| 94 | + registry.registerStepHandler("tool", () => ({ |
| 95 | + kind: "failed", |
| 96 | + error: { code: "temporary" }, |
| 97 | + retryAfterMs: 500, |
| 98 | + })); |
| 99 | + const run = store.createRun({ |
| 100 | + workflowId: "test.workflow", |
| 101 | + status: "queued", |
| 102 | + recoveryState: "runnable", |
| 103 | + now: 100, |
| 104 | + }); |
| 105 | + const step = store.createStep({ |
| 106 | + workflowRunId: run.workflowRunId, |
| 107 | + stepType: "tool", |
| 108 | + status: "queued", |
| 109 | + recoveryState: "runnable", |
| 110 | + maxAttempts: 3, |
| 111 | + now: 110, |
| 112 | + }); |
| 113 | + |
| 114 | + const result = await runDurableExecutorOnce({ |
| 115 | + store, |
| 116 | + registry, |
| 117 | + workerId: "worker-1", |
| 118 | + workflowId: "test.workflow", |
| 119 | + now: () => 200, |
| 120 | + }); |
| 121 | + |
| 122 | + expect(result).toMatchObject({ claimed: true, outcome: "failed" }); |
| 123 | + expect(store.getRun(run.workflowRunId)).toMatchObject({ |
| 124 | + status: "retry_scheduled", |
| 125 | + recoveryState: "retry_scheduled", |
| 126 | + }); |
| 127 | + expect(store.listSteps(run.workflowRunId)).toMatchObject([ |
| 128 | + { |
| 129 | + stepId: step.stepId, |
| 130 | + status: "retry_scheduled", |
| 131 | + recoveryState: "retry_scheduled", |
| 132 | + attempt: 2, |
| 133 | + errorRef: expect.any(String), |
| 134 | + }, |
| 135 | + ]); |
| 136 | + expect(store.listTimers(run.workflowRunId)).toMatchObject([ |
| 137 | + { |
| 138 | + timerType: "retry", |
| 139 | + dueAt: 700, |
| 140 | + status: "pending", |
| 141 | + stepId: step.stepId, |
| 142 | + }, |
| 143 | + ]); |
| 144 | + } finally { |
| 145 | + cleanup(); |
| 146 | + } |
| 147 | + }); |
| 148 | + |
| 149 | + it("marks unhandled step types as unknown instead of replaying blindly", async () => { |
| 150 | + const { store, cleanup } = tempStore(); |
| 151 | + try { |
| 152 | + const registry = createDurableWorkflowRegistry(); |
| 153 | + const run = store.createRun({ |
| 154 | + workflowId: "test.workflow", |
| 155 | + status: "queued", |
| 156 | + recoveryState: "runnable", |
| 157 | + now: 100, |
| 158 | + }); |
| 159 | + const step = store.createStep({ |
| 160 | + workflowRunId: run.workflowRunId, |
| 161 | + stepType: "agent", |
| 162 | + status: "queued", |
| 163 | + recoveryState: "runnable", |
| 164 | + now: 110, |
| 165 | + }); |
| 166 | + |
| 167 | + const result = await runDurableExecutorOnce({ |
| 168 | + store, |
| 169 | + registry, |
| 170 | + workerId: "worker-1", |
| 171 | + workflowId: "test.workflow", |
| 172 | + now: () => 200, |
| 173 | + }); |
| 174 | + |
| 175 | + expect(result).toEqual({ |
| 176 | + claimed: true, |
| 177 | + workflowRunId: run.workflowRunId, |
| 178 | + stepId: step.stepId, |
| 179 | + outcome: "no_handler", |
| 180 | + }); |
| 181 | + expect(store.getRun(run.workflowRunId)).toMatchObject({ |
| 182 | + status: "waiting", |
| 183 | + recoveryState: "unknown_after_side_effect", |
| 184 | + }); |
| 185 | + expect(store.listSteps(run.workflowRunId)).toMatchObject([ |
| 186 | + { |
| 187 | + stepId: step.stepId, |
| 188 | + status: "waiting", |
| 189 | + recoveryState: "unknown_after_side_effect", |
| 190 | + }, |
| 191 | + ]); |
| 192 | + } finally { |
| 193 | + cleanup(); |
| 194 | + } |
| 195 | + }); |
| 196 | +}); |
0 commit comments