@psimona/darkf 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -2
- package/bunfig.toml +13 -0
- package/examples/article-classifier/data/articles.ts +38 -0
- package/examples/article-classifier/scripts/next.ts +7 -0
- package/examples/article-classifier/scripts/queue.ts +33 -0
- package/examples/article-classifier/scripts/record.ts +9 -0
- package/examples/article-classifier/workflow.ts +44 -0
- package/examples/haiku-commit/scripts/check.ts +24 -0
- package/examples/haiku-commit/workflow.ts +28 -0
- package/examples/haiku-readme/scripts/test.ts +13 -0
- package/examples/haiku-readme/scripts/write.ts +10 -0
- package/examples/haiku-readme/workflow.ts +41 -0
- package/examples/task-queue/scripts/next.ts +20 -0
- package/examples/task-queue/scripts/record.ts +20 -0
- package/examples/task-queue/supervise.ts +57 -0
- package/examples/task-queue/workflow.ts +33 -0
- package/package.json +30 -4
- package/src/cli/detached.test.ts +557 -0
- package/src/cli/index.ts +1012 -0
- package/src/core/graph.test.ts +172 -0
- package/src/core/graph.ts +148 -0
- package/src/core/index.ts +4 -0
- package/src/core/journal.ts +21 -0
- package/src/core/mermaid.test.ts +34 -0
- package/src/core/mermaid.ts +95 -0
- package/src/core/types.ts +279 -0
- package/src/engine/eventLog.ts +17 -0
- package/src/engine/index.ts +2 -0
- package/src/engine/run.test.ts +1068 -0
- package/src/engine/run.ts +359 -0
- package/src/exports.ts +2 -0
- package/src/kit/agents/claude-code.test.ts +37 -0
- package/src/kit/agents/claude-code.ts +180 -0
- package/src/kit/agents/handoff.test.ts +128 -0
- package/src/kit/agents/handoff.ts +69 -0
- package/src/kit/agents/opencode.test.ts +136 -0
- package/src/kit/agents/opencode.ts +327 -0
- package/src/kit/registry.test.ts +386 -0
- package/src/kit/registry.ts +296 -0
- package/src/kit/status.test.ts +423 -0
- package/src/kit/status.ts +282 -0
- package/tsconfig.json +4 -0
- package/workflows/series-ordering/scripts/verify.ts +18 -0
- package/workflows/series-ordering/workflow.ts +79 -0
|
@@ -0,0 +1,1068 @@
|
|
|
1
|
+
import { afterAll, expect, test } from "bun:test";
|
|
2
|
+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { pathToFileURL } from "node:url";
|
|
6
|
+
import {
|
|
7
|
+
AgentFailure,
|
|
8
|
+
type AgentRunner,
|
|
9
|
+
type DispatchInfo,
|
|
10
|
+
EXIT,
|
|
11
|
+
FAILED,
|
|
12
|
+
type NodeEvent,
|
|
13
|
+
type NodeResult,
|
|
14
|
+
type ScriptRunner,
|
|
15
|
+
type WorkflowDef,
|
|
16
|
+
} from "../core/index.ts";
|
|
17
|
+
import { run as engineRun, type RunOptions } from "./run.ts";
|
|
18
|
+
|
|
19
|
+
const noopAgent: AgentRunner = { run: async () => ({}) };
|
|
20
|
+
const noopScript: ScriptRunner = { run: async () => ({}) };
|
|
21
|
+
|
|
22
|
+
// Every run journals unconditionally now — keep that out of the repo by
|
|
23
|
+
// rooting every test run under a scratch temp dir instead of cwd.
|
|
24
|
+
const testRootDir = mkdtempSync(join(tmpdir(), "darkf-engine-test-"));
|
|
25
|
+
afterAll(() => rmSync(testRootDir, { recursive: true, force: true }));
|
|
26
|
+
|
|
27
|
+
function run(opts: Omit<RunOptions, "rootDir">) {
|
|
28
|
+
return engineRun({ ...opts, rootDir: testRootDir });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
test("runs a flat workflow to completion via on/next", async () => {
|
|
32
|
+
const workflow: WorkflowDef = {
|
|
33
|
+
initial: "Plan",
|
|
34
|
+
states: {
|
|
35
|
+
Plan: { type: "agent", prompt: "plan", on: { approved: "Build" } },
|
|
36
|
+
Build: { type: "script", file: "build.ts", next: "Review" },
|
|
37
|
+
Review: { type: "agent", prompt: "review", on: { done: EXIT, changes_requested: "Build" } },
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
let reviewCalls = 0;
|
|
42
|
+
const agentRunner: AgentRunner = {
|
|
43
|
+
run: async (id) => {
|
|
44
|
+
if (id === "Plan") return { event: "approved" };
|
|
45
|
+
reviewCalls++;
|
|
46
|
+
return { event: reviewCalls === 1 ? "changes_requested" : "done" };
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const result = await run({ workflow, agentRunner, scriptRunner: noopScript });
|
|
51
|
+
|
|
52
|
+
expect(result.node.phase).toBe("succeeded");
|
|
53
|
+
expect(result.trace.map((t) => t.state)).toEqual(["Plan", "Build", "Review", "Build", "Review"]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("supports a cross-scope edge directly out of a composite", async () => {
|
|
57
|
+
const workflow: WorkflowDef = {
|
|
58
|
+
initial: "Plan",
|
|
59
|
+
states: {
|
|
60
|
+
Plan: { type: "agent", prompt: "plan", on: { approved: "Implement" } },
|
|
61
|
+
Implement: {
|
|
62
|
+
initial: "Write",
|
|
63
|
+
states: {
|
|
64
|
+
Write: { type: "script", file: "write.ts", next: "Test" },
|
|
65
|
+
Test: { type: "script", file: "test.ts", on: { passed: "Review" } },
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
Review: { type: "agent", prompt: "review", on: { done: EXIT } },
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const agentRunner: AgentRunner = {
|
|
73
|
+
run: async (id) => (id === "Plan" ? { event: "approved" } : { event: "done" }),
|
|
74
|
+
};
|
|
75
|
+
const scriptRunner: ScriptRunner = {
|
|
76
|
+
run: async (id) => (id === "Test" ? { event: "passed" } : {}),
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const result = await run({ workflow, agentRunner, scriptRunner });
|
|
80
|
+
|
|
81
|
+
expect(result.node.phase).toBe("succeeded");
|
|
82
|
+
expect(result.trace.map((t) => t.state)).toEqual(["Plan", "Write", "Test", "Review"]);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("supports re-entry into a leaf inside a different composite", async () => {
|
|
86
|
+
const workflow: WorkflowDef = {
|
|
87
|
+
initial: "Implement",
|
|
88
|
+
states: {
|
|
89
|
+
Implement: {
|
|
90
|
+
initial: "Write",
|
|
91
|
+
states: {
|
|
92
|
+
Write: { type: "script", file: "write.ts", next: "Test" },
|
|
93
|
+
Test: { type: "script", file: "test.ts", on: { passed: "Review", failed: "Write" } },
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
Review: { type: "agent", prompt: "review", on: { done: EXIT, changes_requested: "Write" } },
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
let testAttempts = 0;
|
|
101
|
+
let reviewCalls = 0;
|
|
102
|
+
const scriptRunner: ScriptRunner = {
|
|
103
|
+
run: async (id) => {
|
|
104
|
+
if (id === "Test") {
|
|
105
|
+
testAttempts++;
|
|
106
|
+
return { event: testAttempts === 1 ? "failed" : "passed" };
|
|
107
|
+
}
|
|
108
|
+
return {};
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
const agentRunner: AgentRunner = {
|
|
112
|
+
run: async () => {
|
|
113
|
+
reviewCalls++;
|
|
114
|
+
return { event: reviewCalls === 1 ? "changes_requested" : "done" };
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const result = await run({ workflow, agentRunner, scriptRunner });
|
|
119
|
+
|
|
120
|
+
expect(result.node.phase).toBe("succeeded");
|
|
121
|
+
expect(result.trace.map((t) => t.state)).toEqual([
|
|
122
|
+
"Write",
|
|
123
|
+
"Test",
|
|
124
|
+
"Write",
|
|
125
|
+
"Test",
|
|
126
|
+
"Review",
|
|
127
|
+
"Write",
|
|
128
|
+
"Test",
|
|
129
|
+
"Review",
|
|
130
|
+
]);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("crashes with a no-transition reason when a returned event matches no route", async () => {
|
|
134
|
+
const workflow: WorkflowDef = {
|
|
135
|
+
initial: "Solo",
|
|
136
|
+
states: { Solo: { type: "script", file: "solo.ts", on: { ok: EXIT } } },
|
|
137
|
+
};
|
|
138
|
+
const scriptRunner: ScriptRunner = { run: async () => ({ event: "not_ok" }) };
|
|
139
|
+
|
|
140
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner });
|
|
141
|
+
|
|
142
|
+
expect(result.node.phase).toBe("crashed");
|
|
143
|
+
if (result.node.phase === "crashed") {
|
|
144
|
+
expect(result.node.reason).toEqual({ kind: "no-transition", state: "Solo", event: "not_ok" });
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("crashes eagerly with invalid-workflow for a dead-end leaf, before any dispatch", async () => {
|
|
149
|
+
const workflow: WorkflowDef = {
|
|
150
|
+
initial: "Solo",
|
|
151
|
+
states: { Solo: { type: "script", file: "solo.ts" } }, // no on, no next
|
|
152
|
+
};
|
|
153
|
+
let calls = 0;
|
|
154
|
+
const scriptRunner: ScriptRunner = {
|
|
155
|
+
run: async () => {
|
|
156
|
+
calls++;
|
|
157
|
+
return {};
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner });
|
|
162
|
+
|
|
163
|
+
expect(calls).toBe(0); // never dispatched — caught before the first enter()
|
|
164
|
+
expect(result.node.phase).toBe("crashed");
|
|
165
|
+
if (result.node.phase === "crashed") {
|
|
166
|
+
expect(result.node.reason).toEqual({
|
|
167
|
+
kind: "invalid-workflow",
|
|
168
|
+
errors: [{ kind: "dead-end-leaf", state: "Solo" }],
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test("crashes eagerly with invalid-workflow for an unresolved on/next target", async () => {
|
|
174
|
+
const workflow: WorkflowDef = {
|
|
175
|
+
initial: "Solo",
|
|
176
|
+
states: { Solo: { type: "script", file: "solo.ts", next: "Nope" } },
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner: noopScript });
|
|
180
|
+
|
|
181
|
+
expect(result.node.phase).toBe("crashed");
|
|
182
|
+
if (result.node.phase === "crashed") {
|
|
183
|
+
expect(result.node.reason).toEqual({
|
|
184
|
+
kind: "invalid-workflow",
|
|
185
|
+
errors: [{ kind: "unresolved-target", from: "Solo", target: "Nope" }],
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
test("crashes eagerly with invalid-workflow for a composite initial-cycle, instead of hanging", async () => {
|
|
191
|
+
const workflow: WorkflowDef = {
|
|
192
|
+
initial: "A",
|
|
193
|
+
states: {
|
|
194
|
+
A: { initial: "B", states: { LeafA: { type: "script", file: "leaf.ts", next: EXIT } } },
|
|
195
|
+
B: { initial: "A", states: { LeafB: { type: "script", file: "leaf.ts", next: EXIT } } },
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner: noopScript });
|
|
200
|
+
|
|
201
|
+
expect(result.node.phase).toBe("crashed");
|
|
202
|
+
if (result.node.phase === "crashed") {
|
|
203
|
+
expect(result.node.reason).toEqual({
|
|
204
|
+
kind: "invalid-workflow",
|
|
205
|
+
errors: [{ kind: "initial-cycle", cycle: ["A", "B"] }],
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test("crashes with a duplicate-id reason instead of throwing", async () => {
|
|
211
|
+
const workflow: WorkflowDef = {
|
|
212
|
+
initial: "A",
|
|
213
|
+
states: {
|
|
214
|
+
A: { type: "script", file: "a.ts", next: "B" },
|
|
215
|
+
Wrapper: {
|
|
216
|
+
initial: "A",
|
|
217
|
+
states: { A: { type: "script", file: "a.ts" } },
|
|
218
|
+
},
|
|
219
|
+
},
|
|
220
|
+
} as unknown as WorkflowDef;
|
|
221
|
+
|
|
222
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner: noopScript });
|
|
223
|
+
|
|
224
|
+
expect(result.node.phase).toBe("crashed");
|
|
225
|
+
if (result.node.phase === "crashed") {
|
|
226
|
+
expect(result.node.reason).toEqual({ kind: "duplicate-id", id: "A" });
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test("retries a crashing leaf up to `retries` times then succeeds", async () => {
|
|
231
|
+
const workflow: WorkflowDef = {
|
|
232
|
+
initial: "Flaky",
|
|
233
|
+
states: { Flaky: { type: "script", file: "flaky.ts", retries: 2, on: { ok: EXIT } } },
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
let calls = 0;
|
|
237
|
+
const scriptRunner: ScriptRunner = {
|
|
238
|
+
run: async () => {
|
|
239
|
+
calls++;
|
|
240
|
+
if (calls < 3) throw new Error("transient boom");
|
|
241
|
+
return { event: "ok" };
|
|
242
|
+
},
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner });
|
|
246
|
+
|
|
247
|
+
expect(result.node.phase).toBe("succeeded");
|
|
248
|
+
expect(calls).toBe(3);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
test("crashes with runner-error after exhausting retries", async () => {
|
|
252
|
+
const workflow: WorkflowDef = {
|
|
253
|
+
initial: "Flaky",
|
|
254
|
+
states: { Flaky: { type: "script", file: "flaky.ts", retries: 1, on: { ok: EXIT } } },
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
let calls = 0;
|
|
258
|
+
const scriptRunner: ScriptRunner = {
|
|
259
|
+
run: async () => {
|
|
260
|
+
calls++;
|
|
261
|
+
throw new Error("always boom");
|
|
262
|
+
},
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner });
|
|
266
|
+
|
|
267
|
+
expect(calls).toBe(2); // 1 initial attempt + 1 retry
|
|
268
|
+
expect(result.node.phase).toBe("crashed");
|
|
269
|
+
if (result.node.phase === "crashed") {
|
|
270
|
+
expect(result.node.reason).toEqual({ kind: "runner-error", error: "always boom" });
|
|
271
|
+
expect(result.node.attempt).toBe(2);
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
test("crashes with a timeout reason when a runner exceeds timeoutMs", async () => {
|
|
276
|
+
const workflow: WorkflowDef = {
|
|
277
|
+
initial: "Slow",
|
|
278
|
+
states: { Slow: { type: "script", file: "slow.ts", timeoutMs: 10, on: { ok: EXIT } } },
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
const scriptRunner: ScriptRunner = {
|
|
282
|
+
run: async () => {
|
|
283
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
284
|
+
return { event: "ok" };
|
|
285
|
+
},
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner });
|
|
289
|
+
|
|
290
|
+
expect(result.node.phase).toBe("crashed");
|
|
291
|
+
if (result.node.phase === "crashed") {
|
|
292
|
+
expect(result.node.reason).toEqual({ kind: "timeout", elapsedMs: 10 });
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
test("onEvent fires for every transition, including retries", async () => {
|
|
297
|
+
const workflow: WorkflowDef = {
|
|
298
|
+
initial: "Flaky",
|
|
299
|
+
states: { Flaky: { type: "script", file: "flaky.ts", retries: 1, on: { ok: EXIT } } },
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
let calls = 0;
|
|
303
|
+
const scriptRunner: ScriptRunner = {
|
|
304
|
+
run: async () => {
|
|
305
|
+
calls++;
|
|
306
|
+
if (calls === 1) throw new Error("boom");
|
|
307
|
+
return { event: "ok" };
|
|
308
|
+
},
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
const events: NodeEvent[] = [];
|
|
312
|
+
await run({ workflow, agentRunner: noopAgent, scriptRunner, onEvent: (e) => events.push(e) });
|
|
313
|
+
|
|
314
|
+
// The run's own begin/end markers (state EXIT) bookend the leaf's events.
|
|
315
|
+
expect(events.map((e) => e.status.phase)).toEqual([
|
|
316
|
+
"succeeded", // begin marker (EXIT)
|
|
317
|
+
"running", // attempt 1
|
|
318
|
+
"running", // attempt 2 (retry)
|
|
319
|
+
"succeeded", // leaf success (Flaky)
|
|
320
|
+
"succeeded", // end marker (EXIT)
|
|
321
|
+
]);
|
|
322
|
+
expect(events.filter((e) => e.state === "Flaky").length).toBe(3);
|
|
323
|
+
expect(events[0]!.state).toBe(EXIT);
|
|
324
|
+
expect(events.at(-1)!.state).toBe(EXIT);
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
test("always journals to .darkf/run/<runId>/journal.jsonl, unconditionally", async () => {
|
|
328
|
+
const workflow: WorkflowDef = {
|
|
329
|
+
initial: "Solo",
|
|
330
|
+
states: { Solo: { type: "script", file: "solo.ts", on: { ok: EXIT } } },
|
|
331
|
+
};
|
|
332
|
+
const scriptRunner: ScriptRunner = { run: async () => ({ event: "ok" }) };
|
|
333
|
+
|
|
334
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner });
|
|
335
|
+
|
|
336
|
+
const journalPath = join(testRootDir, ".darkf", "run", result.runId, "journal.jsonl");
|
|
337
|
+
const lines = readFileSync(journalPath, "utf8").trim().split("\n");
|
|
338
|
+
const parsed = lines.map((l) => JSON.parse(l) as NodeEvent);
|
|
339
|
+
// begin (EXIT) + running + succeeded (Solo) + end (EXIT)
|
|
340
|
+
expect(parsed.map((e) => e.status.phase)).toEqual([
|
|
341
|
+
"succeeded",
|
|
342
|
+
"running",
|
|
343
|
+
"succeeded",
|
|
344
|
+
"succeeded",
|
|
345
|
+
]);
|
|
346
|
+
expect(parsed[0]!.state).toBe(EXIT); // begin marker is the first line
|
|
347
|
+
expect(parsed.at(-1)!.state).toBe(EXIT); // end marker is the last line
|
|
348
|
+
expect(parsed.every((e) => e.runId === result.runId)).toBe(true);
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
test("validate rejects a well-formed but semantically bad result, consuming a retry", async () => {
|
|
352
|
+
const workflow: WorkflowDef = {
|
|
353
|
+
initial: "Picky",
|
|
354
|
+
states: {
|
|
355
|
+
Picky: {
|
|
356
|
+
type: "script",
|
|
357
|
+
file: "picky.ts",
|
|
358
|
+
retries: 1,
|
|
359
|
+
on: { ok: EXIT },
|
|
360
|
+
validate: (result) =>
|
|
361
|
+
result.output === "good" ? undefined : "expected output to be 'good'",
|
|
362
|
+
},
|
|
363
|
+
},
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
let calls = 0;
|
|
367
|
+
const scriptRunner: ScriptRunner = {
|
|
368
|
+
run: async () => {
|
|
369
|
+
calls++;
|
|
370
|
+
return { event: "ok", output: calls === 1 ? "bad" : "good" };
|
|
371
|
+
},
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner });
|
|
375
|
+
|
|
376
|
+
expect(calls).toBe(2);
|
|
377
|
+
expect(result.node.phase).toBe("succeeded");
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
test("crashes with cycle-limit when a multi-leaf loop exceeds a composite's maxCycles", async () => {
|
|
381
|
+
const workflow: WorkflowDef = {
|
|
382
|
+
initial: "Implement",
|
|
383
|
+
states: {
|
|
384
|
+
Implement: {
|
|
385
|
+
initial: "Write",
|
|
386
|
+
maxCycles: 3,
|
|
387
|
+
states: {
|
|
388
|
+
Write: { type: "script", file: "write.ts", next: "Test" },
|
|
389
|
+
Test: { type: "script", file: "test.ts", on: { failed: "Write" } },
|
|
390
|
+
},
|
|
391
|
+
},
|
|
392
|
+
},
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
const scriptRunner: ScriptRunner = {
|
|
396
|
+
run: async (id) => (id === "Test" ? { event: "failed" } : {}),
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner });
|
|
400
|
+
|
|
401
|
+
expect(result.node.phase).toBe("crashed");
|
|
402
|
+
if (result.node.phase === "crashed") {
|
|
403
|
+
expect(result.node.reason).toEqual({ kind: "cycle-limit", composite: "Implement", cycles: 3 });
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
test("maxCycles does not fire when the loop finishes within budget", async () => {
|
|
408
|
+
const workflow: WorkflowDef = {
|
|
409
|
+
initial: "Implement",
|
|
410
|
+
states: {
|
|
411
|
+
Implement: {
|
|
412
|
+
initial: "Write",
|
|
413
|
+
maxCycles: 10,
|
|
414
|
+
states: {
|
|
415
|
+
Write: { type: "script", file: "write.ts", next: "Test" },
|
|
416
|
+
Test: { type: "script", file: "test.ts", on: { passed: EXIT, failed: "Write" } },
|
|
417
|
+
},
|
|
418
|
+
},
|
|
419
|
+
},
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
let calls = 0;
|
|
423
|
+
const scriptRunner: ScriptRunner = {
|
|
424
|
+
run: async (id) => {
|
|
425
|
+
if (id !== "Test") return {};
|
|
426
|
+
calls++;
|
|
427
|
+
return { event: calls === 1 ? "failed" : "passed" };
|
|
428
|
+
},
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner });
|
|
432
|
+
|
|
433
|
+
expect(result.node.phase).toBe("succeeded");
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
test("maxCycles resets when a composite is re-entered after leaving it", async () => {
|
|
437
|
+
// Router dispatches Implement twice; each pass is exactly 4 leaf dispatches
|
|
438
|
+
// (Write, Test-failed, Write, Test-passed) under maxCycles: 4 — individually
|
|
439
|
+
// within budget, but 8 combined would exceed it if the counter never reset
|
|
440
|
+
// between passes (the exact bug cluster-tdd-loop-port reproduced: a cap
|
|
441
|
+
// sized for one scope's dispatch count tripping on a second, healthy scope).
|
|
442
|
+
const workflow: WorkflowDef = {
|
|
443
|
+
initial: "Router",
|
|
444
|
+
states: {
|
|
445
|
+
Router: { type: "script", file: "router.ts", on: { go: "Implement", done: EXIT } },
|
|
446
|
+
Implement: {
|
|
447
|
+
initial: "Write",
|
|
448
|
+
maxCycles: 4,
|
|
449
|
+
states: {
|
|
450
|
+
Write: { type: "script", file: "write.ts", next: "Test" },
|
|
451
|
+
Test: { type: "script", file: "test.ts", on: { failed: "Write", passed: "Router" } },
|
|
452
|
+
},
|
|
453
|
+
},
|
|
454
|
+
},
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
let routerCalls = 0;
|
|
458
|
+
let testCalls = 0;
|
|
459
|
+
const scriptRunner: ScriptRunner = {
|
|
460
|
+
run: async (id) => {
|
|
461
|
+
if (id === "Router") {
|
|
462
|
+
routerCalls++;
|
|
463
|
+
return { event: routerCalls <= 2 ? "go" : "done" };
|
|
464
|
+
}
|
|
465
|
+
if (id === "Test") {
|
|
466
|
+
testCalls++;
|
|
467
|
+
return { event: testCalls % 2 === 1 ? "failed" : "passed" };
|
|
468
|
+
}
|
|
469
|
+
return {};
|
|
470
|
+
},
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner });
|
|
474
|
+
|
|
475
|
+
expect(result.node.phase).toBe("succeeded");
|
|
476
|
+
expect(result.trace.map((t) => t.state)).toEqual([
|
|
477
|
+
"Router",
|
|
478
|
+
"Write",
|
|
479
|
+
"Test",
|
|
480
|
+
"Write",
|
|
481
|
+
"Test",
|
|
482
|
+
"Router",
|
|
483
|
+
"Write",
|
|
484
|
+
"Test",
|
|
485
|
+
"Write",
|
|
486
|
+
"Test",
|
|
487
|
+
"Router",
|
|
488
|
+
]);
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
test("maxCycles still fires for a genuinely runaway single pass, unaffected by the reset", async () => {
|
|
492
|
+
// Same shape as the re-entry test above, but Test never passes — one
|
|
493
|
+
// continuous pass through Implement must still trip its own maxCycles,
|
|
494
|
+
// proving the fix didn't weaken the breaker for its original job.
|
|
495
|
+
const workflow: WorkflowDef = {
|
|
496
|
+
initial: "Implement",
|
|
497
|
+
states: {
|
|
498
|
+
Implement: {
|
|
499
|
+
initial: "Write",
|
|
500
|
+
maxCycles: 4,
|
|
501
|
+
states: {
|
|
502
|
+
Write: { type: "script", file: "write.ts", next: "Test" },
|
|
503
|
+
Test: { type: "script", file: "test.ts", on: { failed: "Write" } },
|
|
504
|
+
},
|
|
505
|
+
},
|
|
506
|
+
},
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
const scriptRunner: ScriptRunner = {
|
|
510
|
+
run: async (id) => (id === "Test" ? { event: "failed" } : {}),
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner });
|
|
514
|
+
|
|
515
|
+
expect(result.node.phase).toBe("crashed");
|
|
516
|
+
if (result.node.phase === "crashed") {
|
|
517
|
+
expect(result.node.reason).toEqual({ kind: "cycle-limit", composite: "Implement", cycles: 4 });
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
test("a composite's cycle counter is not reset just because a nested composite is freshly entered", async () => {
|
|
522
|
+
// A (inside Outer) -> Inner (nested inside Outer, generous maxCycles) -> back
|
|
523
|
+
// to A. Outer's own counter must keep accumulating across that hop into
|
|
524
|
+
// Inner and back — proving composites reset independently by their own
|
|
525
|
+
// presence in the previous dispatch's scopePath, not by a descendant's.
|
|
526
|
+
const workflow: WorkflowDef = {
|
|
527
|
+
initial: "Outer",
|
|
528
|
+
states: {
|
|
529
|
+
Outer: {
|
|
530
|
+
initial: "A",
|
|
531
|
+
maxCycles: 2,
|
|
532
|
+
states: {
|
|
533
|
+
A: { type: "script", file: "a.ts", on: { toInner: "Inner" } },
|
|
534
|
+
Inner: {
|
|
535
|
+
initial: "B",
|
|
536
|
+
maxCycles: 10,
|
|
537
|
+
states: {
|
|
538
|
+
B: { type: "script", file: "b.ts", on: { back: "A" } },
|
|
539
|
+
},
|
|
540
|
+
},
|
|
541
|
+
},
|
|
542
|
+
},
|
|
543
|
+
},
|
|
544
|
+
};
|
|
545
|
+
|
|
546
|
+
const scriptRunner: ScriptRunner = {
|
|
547
|
+
run: async (id) => (id === "A" ? { event: "toInner" } : { event: "back" }),
|
|
548
|
+
};
|
|
549
|
+
|
|
550
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner });
|
|
551
|
+
|
|
552
|
+
expect(result.node.phase).toBe("crashed");
|
|
553
|
+
if (result.node.phase === "crashed") {
|
|
554
|
+
expect(result.node.reason).toEqual({ kind: "cycle-limit", composite: "Outer", cycles: 2 });
|
|
555
|
+
}
|
|
556
|
+
expect(result.trace.map((t) => t.state)).toEqual(["A", "B"]);
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
test("crashes with validation-failed after exhausting retries", async () => {
|
|
560
|
+
const workflow: WorkflowDef = {
|
|
561
|
+
initial: "Picky",
|
|
562
|
+
states: {
|
|
563
|
+
Picky: {
|
|
564
|
+
type: "script",
|
|
565
|
+
file: "picky.ts",
|
|
566
|
+
on: { ok: EXIT },
|
|
567
|
+
validate: () => "always rejected",
|
|
568
|
+
},
|
|
569
|
+
},
|
|
570
|
+
};
|
|
571
|
+
|
|
572
|
+
const scriptRunner: ScriptRunner = { run: async () => ({ event: "ok" }) };
|
|
573
|
+
|
|
574
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner });
|
|
575
|
+
|
|
576
|
+
expect(result.node.phase).toBe("crashed");
|
|
577
|
+
if (result.node.phase === "crashed") {
|
|
578
|
+
expect(result.node.reason).toEqual({ kind: "validation-failed", message: "always rejected" });
|
|
579
|
+
expect(result.node.attempt).toBe(1);
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
test("threads agentSessionId and previousFailure into DispatchInfo across retries", async () => {
|
|
584
|
+
const workflow: WorkflowDef = {
|
|
585
|
+
initial: "Agentic",
|
|
586
|
+
states: {
|
|
587
|
+
Agentic: {
|
|
588
|
+
type: "agent",
|
|
589
|
+
prompt: "do it",
|
|
590
|
+
retries: 1,
|
|
591
|
+
on: { ok: EXIT },
|
|
592
|
+
validate: (result) => (result.event === "ok" ? undefined : "expected event 'ok'"),
|
|
593
|
+
},
|
|
594
|
+
},
|
|
595
|
+
};
|
|
596
|
+
|
|
597
|
+
const infos: DispatchInfo[] = [];
|
|
598
|
+
const agentRunner: AgentRunner = {
|
|
599
|
+
run: async (_id, _def, _handoff, info) => {
|
|
600
|
+
infos.push(info);
|
|
601
|
+
if (info.attempt === 1) return { event: "retry_me", agentSessionId: "sess-123" };
|
|
602
|
+
return { event: "ok" };
|
|
603
|
+
},
|
|
604
|
+
};
|
|
605
|
+
|
|
606
|
+
const result = await run({ workflow, agentRunner, scriptRunner: noopScript });
|
|
607
|
+
|
|
608
|
+
expect(result.node.phase).toBe("succeeded");
|
|
609
|
+
expect(infos[0]).toMatchObject({
|
|
610
|
+
attempt: 1,
|
|
611
|
+
agentSessionId: undefined,
|
|
612
|
+
previousFailure: undefined,
|
|
613
|
+
});
|
|
614
|
+
expect(infos[1]).toMatchObject({
|
|
615
|
+
attempt: 2,
|
|
616
|
+
agentSessionId: "sess-123",
|
|
617
|
+
previousFailure: { kind: "validation-failed", message: "expected event 'ok'" },
|
|
618
|
+
});
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
test("threads step and rootDir into DispatchInfo, step constant across retries of one visit", async () => {
|
|
622
|
+
const workflow: WorkflowDef = {
|
|
623
|
+
initial: "Plan",
|
|
624
|
+
states: {
|
|
625
|
+
Plan: {
|
|
626
|
+
type: "agent",
|
|
627
|
+
prompt: "plan",
|
|
628
|
+
retries: 1,
|
|
629
|
+
on: { approved: "Build" },
|
|
630
|
+
validate: (result) => (result.event === "approved" ? undefined : "expected 'approved'"),
|
|
631
|
+
},
|
|
632
|
+
Build: { type: "script", file: "build.ts", on: { done: EXIT } },
|
|
633
|
+
},
|
|
634
|
+
};
|
|
635
|
+
|
|
636
|
+
const infos: DispatchInfo[] = [];
|
|
637
|
+
let planCalls = 0;
|
|
638
|
+
const agentRunner: AgentRunner = {
|
|
639
|
+
run: async (_id, _def, _handoff, info) => {
|
|
640
|
+
infos.push(info);
|
|
641
|
+
planCalls++;
|
|
642
|
+
if (planCalls === 1) return { event: "retry_me" };
|
|
643
|
+
return { event: "approved" };
|
|
644
|
+
},
|
|
645
|
+
};
|
|
646
|
+
const scriptRunner: ScriptRunner = {
|
|
647
|
+
run: async (_id, _def, _handoff, _baseDir, info) => {
|
|
648
|
+
infos.push(info);
|
|
649
|
+
return { event: "done" };
|
|
650
|
+
},
|
|
651
|
+
};
|
|
652
|
+
|
|
653
|
+
const result = await run({ workflow, agentRunner, scriptRunner });
|
|
654
|
+
|
|
655
|
+
expect(result.node.phase).toBe("succeeded");
|
|
656
|
+
expect(infos.map((i) => i.step)).toEqual([1, 1, 2]);
|
|
657
|
+
expect(infos.every((i) => i.rootDir === testRootDir)).toBe(true);
|
|
658
|
+
});
|
|
659
|
+
|
|
660
|
+
test("begin marker is the journal's first line and carries the run's seed input", async () => {
|
|
661
|
+
const workflow: WorkflowDef = {
|
|
662
|
+
initial: "Solo",
|
|
663
|
+
states: { Solo: { type: "script", file: "solo.ts", on: { ok: EXIT } } },
|
|
664
|
+
};
|
|
665
|
+
const scriptRunner: ScriptRunner = { run: async () => ({ event: "ok" }) };
|
|
666
|
+
const seed = { output: { hello: "world" } };
|
|
667
|
+
|
|
668
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner, input: seed });
|
|
669
|
+
|
|
670
|
+
const journalPath = join(testRootDir, ".darkf", "run", result.runId, "journal.jsonl");
|
|
671
|
+
const parsed = readFileSync(journalPath, "utf8")
|
|
672
|
+
.trim()
|
|
673
|
+
.split("\n")
|
|
674
|
+
.map((l) => JSON.parse(l) as NodeEvent);
|
|
675
|
+
expect(parsed[0]!.state).toBe(EXIT);
|
|
676
|
+
expect(parsed[0]!.status).toEqual({ phase: "succeeded", result: seed });
|
|
677
|
+
});
|
|
678
|
+
|
|
679
|
+
test("end marker is the journal's last line and carries the final NodeResult on success", async () => {
|
|
680
|
+
const workflow: WorkflowDef = {
|
|
681
|
+
initial: "Solo",
|
|
682
|
+
states: { Solo: { type: "script", file: "solo.ts", on: { ok: EXIT } } },
|
|
683
|
+
};
|
|
684
|
+
const finalResult = { event: "ok", output: "done" };
|
|
685
|
+
const scriptRunner: ScriptRunner = { run: async () => finalResult };
|
|
686
|
+
|
|
687
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner });
|
|
688
|
+
|
|
689
|
+
const journalPath = join(testRootDir, ".darkf", "run", result.runId, "journal.jsonl");
|
|
690
|
+
const parsed = readFileSync(journalPath, "utf8")
|
|
691
|
+
.trim()
|
|
692
|
+
.split("\n")
|
|
693
|
+
.map((l) => JSON.parse(l) as NodeEvent);
|
|
694
|
+
const last = parsed.at(-1)!;
|
|
695
|
+
expect(last.state).toBe(EXIT);
|
|
696
|
+
expect(last.status).toEqual({ phase: "succeeded", result: finalResult });
|
|
697
|
+
});
|
|
698
|
+
|
|
699
|
+
test("a crashed run has no end marker, only the crashing leaf's own crashed line", async () => {
|
|
700
|
+
const workflow: WorkflowDef = {
|
|
701
|
+
initial: "Solo",
|
|
702
|
+
states: { Solo: { type: "script", file: "solo.ts" } }, // no on/next -> no-transition
|
|
703
|
+
};
|
|
704
|
+
const scriptRunner: ScriptRunner = { run: async () => ({}) };
|
|
705
|
+
|
|
706
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner });
|
|
707
|
+
|
|
708
|
+
expect(result.node.phase).toBe("crashed");
|
|
709
|
+
const journalPath = join(testRootDir, ".darkf", "run", result.runId, "journal.jsonl");
|
|
710
|
+
const parsed = readFileSync(journalPath, "utf8")
|
|
711
|
+
.trim()
|
|
712
|
+
.split("\n")
|
|
713
|
+
.map((l) => JSON.parse(l) as NodeEvent);
|
|
714
|
+
expect(parsed[0]!.state).toBe(EXIT); // begin marker still present
|
|
715
|
+
const last = parsed.at(-1)!;
|
|
716
|
+
expect(last.state).toBe("Solo");
|
|
717
|
+
expect(last.status.phase).toBe("crashed"); // no EXIT end marker after the crash
|
|
718
|
+
});
|
|
719
|
+
|
|
720
|
+
test("a duplicate-id crash still journals the begin marker as its first line", async () => {
|
|
721
|
+
const workflow: WorkflowDef = {
|
|
722
|
+
initial: "A",
|
|
723
|
+
states: {
|
|
724
|
+
A: { type: "script", file: "a.ts", next: "B" },
|
|
725
|
+
Wrapper: { initial: "A", states: { A: { type: "script", file: "a.ts" } } },
|
|
726
|
+
},
|
|
727
|
+
} as unknown as WorkflowDef;
|
|
728
|
+
|
|
729
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner: noopScript });
|
|
730
|
+
|
|
731
|
+
expect(result.node.phase).toBe("crashed");
|
|
732
|
+
const journalPath = join(testRootDir, ".darkf", "run", result.runId, "journal.jsonl");
|
|
733
|
+
const parsed = readFileSync(journalPath, "utf8")
|
|
734
|
+
.trim()
|
|
735
|
+
.split("\n")
|
|
736
|
+
.map((l) => JSON.parse(l) as NodeEvent);
|
|
737
|
+
expect(parsed[0]!.state).toBe(EXIT); // emitted before buildRegistry
|
|
738
|
+
expect(parsed[0]!.status.phase).toBe("succeeded");
|
|
739
|
+
const last = parsed.at(-1)!;
|
|
740
|
+
expect(last.status.phase).toBe("crashed"); // no end marker
|
|
741
|
+
});
|
|
742
|
+
|
|
743
|
+
test("script nodes receive DispatchInfo (rootDir) as their second argument", async () => {
|
|
744
|
+
const workflow: WorkflowDef = {
|
|
745
|
+
initial: "S",
|
|
746
|
+
states: {
|
|
747
|
+
S: { type: "script", file: "probe.ts", next: EXIT },
|
|
748
|
+
},
|
|
749
|
+
};
|
|
750
|
+
const dir = mkdtempSync(join(tmpdir(), "darkf-script-info-"));
|
|
751
|
+
await writeFileSync(
|
|
752
|
+
join(dir, "probe.ts"),
|
|
753
|
+
"export default function (handoff, info) {\n return { output: { rootDir: info?.rootDir ?? null } };\n}\n",
|
|
754
|
+
);
|
|
755
|
+
const rootDir = mkdtempSync(join(tmpdir(), "darkf-script-root-"));
|
|
756
|
+
const opts: RunOptions = {
|
|
757
|
+
workflow,
|
|
758
|
+
scriptRunner: {
|
|
759
|
+
async run(_id, def, handoff, baseDir, info) {
|
|
760
|
+
const url = pathToFileURL(`${baseDir}/${def.file}`).href;
|
|
761
|
+
const mod = await import(url);
|
|
762
|
+
const fn = mod.default as (h: NodeResult, i: DispatchInfo) => NodeResult;
|
|
763
|
+
return await fn(handoff, info);
|
|
764
|
+
},
|
|
765
|
+
},
|
|
766
|
+
baseDir: dir,
|
|
767
|
+
rootDir,
|
|
768
|
+
};
|
|
769
|
+
const result = await engineRun(opts);
|
|
770
|
+
const finalStatus = result.node;
|
|
771
|
+
const out =
|
|
772
|
+
finalStatus.phase === "succeeded"
|
|
773
|
+
? (finalStatus.result?.output as { rootDir?: string } | undefined)
|
|
774
|
+
: undefined;
|
|
775
|
+
expect(out?.rootDir).toBe(rootDir);
|
|
776
|
+
rmSync(dir, { recursive: true, force: true });
|
|
777
|
+
rmSync(rootDir, { recursive: true, force: true });
|
|
778
|
+
});
|
|
779
|
+
|
|
780
|
+
test("an agent runner's onProgress is folded into the running heartbeat as agentStepCount/agentIdleMs", async () => {
|
|
781
|
+
const workflow: WorkflowDef = {
|
|
782
|
+
initial: "Agentic",
|
|
783
|
+
states: { Agentic: { type: "agent", prompt: "go", next: EXIT } },
|
|
784
|
+
};
|
|
785
|
+
const agentRunner: AgentRunner = {
|
|
786
|
+
run: async (_id, _def, _handoff, _info, onProgress) => {
|
|
787
|
+
onProgress?.({ at: Date.now(), stepCount: 7, lastEvent: "step-finish" });
|
|
788
|
+
await new Promise((r) => setTimeout(r, 250));
|
|
789
|
+
return {};
|
|
790
|
+
},
|
|
791
|
+
};
|
|
792
|
+
|
|
793
|
+
const result = await run({ workflow, agentRunner, scriptRunner: noopScript, heartbeatMs: 50 });
|
|
794
|
+
expect(result.node.phase).toBe("succeeded");
|
|
795
|
+
|
|
796
|
+
const journalPath = join(testRootDir, ".darkf", "run", result.runId, "journal.jsonl");
|
|
797
|
+
const events = readFileSync(journalPath, "utf8")
|
|
798
|
+
.trim()
|
|
799
|
+
.split("\n")
|
|
800
|
+
.map((l) => JSON.parse(l) as NodeEvent);
|
|
801
|
+
const heartbeats = events.filter((e) => e.state === "Agentic" && e.status.phase === "running");
|
|
802
|
+
expect(heartbeats.length).toBeGreaterThanOrEqual(2);
|
|
803
|
+
const last = heartbeats.at(-1)!;
|
|
804
|
+
expect(last.status.phase === "running" && last.status.agentStepCount).toBe(7);
|
|
805
|
+
expect(last.status.phase === "running" && typeof last.status.agentIdleMs).toBe("number");
|
|
806
|
+
});
|
|
807
|
+
|
|
808
|
+
test("open dispatches heartbeat the journal (live runtime)", async () => {
|
|
809
|
+
const workflow: WorkflowDef = {
|
|
810
|
+
initial: "S",
|
|
811
|
+
states: { S: { type: "script", file: "slow.ts", next: EXIT } },
|
|
812
|
+
};
|
|
813
|
+
const dir = mkdtempSync(join(tmpdir(), "darkf-hb-scripts-"));
|
|
814
|
+
await writeFileSync(
|
|
815
|
+
join(dir, "slow.ts"),
|
|
816
|
+
"export default async function () { await new Promise((r) => setTimeout(r, 700)); return {}; }\n",
|
|
817
|
+
);
|
|
818
|
+
const rootDir = mkdtempSync(join(tmpdir(), "darkf-hb-root-"));
|
|
819
|
+
const result = await engineRun({ workflow, baseDir: dir, rootDir, heartbeatMs: 200 });
|
|
820
|
+
expect(result.node.phase).toBe("succeeded");
|
|
821
|
+
|
|
822
|
+
const journal = readFileSync(
|
|
823
|
+
join(rootDir, ".darkf", "run", result.runId, "journal.jsonl"),
|
|
824
|
+
"utf8",
|
|
825
|
+
);
|
|
826
|
+
const events = journal
|
|
827
|
+
.trim()
|
|
828
|
+
.split("\n")
|
|
829
|
+
.map((l) => JSON.parse(l) as NodeEvent);
|
|
830
|
+
const running = events.filter((e) => e.status.phase === "running");
|
|
831
|
+
// initial dispatch + >= 2 heartbeats during a 700ms node at 200ms cadence
|
|
832
|
+
expect(running.length).toBeGreaterThanOrEqual(3);
|
|
833
|
+
const beats = running.map((e) => (e.status.phase === "running" ? e.status.lastHeartbeat : 0));
|
|
834
|
+
expect(beats.at(-1)!).toBeGreaterThan(beats[0]!);
|
|
835
|
+
rmSync(dir, { recursive: true, force: true });
|
|
836
|
+
rmSync(rootDir, { recursive: true, force: true });
|
|
837
|
+
});
|
|
838
|
+
|
|
839
|
+
// --- session-nudge (docs/specs/session-nudge/spec.md) ---
|
|
840
|
+
|
|
841
|
+
test("nudges a handoff-missing AgentFailure in-session, consuming no retry", async () => {
|
|
842
|
+
const workflow: WorkflowDef = {
|
|
843
|
+
initial: "Agentic",
|
|
844
|
+
states: { Agentic: { type: "agent", prompt: "do it", on: { ok: EXIT } } },
|
|
845
|
+
};
|
|
846
|
+
|
|
847
|
+
const infos: DispatchInfo[] = [];
|
|
848
|
+
const agentRunner: AgentRunner = {
|
|
849
|
+
run: async (_id, _def, _handoff, info) => {
|
|
850
|
+
infos.push(info);
|
|
851
|
+
if (infos.length === 1) {
|
|
852
|
+
throw new AgentFailure("handoff not written", {
|
|
853
|
+
agentSessionId: "sess-1",
|
|
854
|
+
crashReason: { kind: "handoff-missing" },
|
|
855
|
+
});
|
|
856
|
+
}
|
|
857
|
+
return { event: "ok" };
|
|
858
|
+
},
|
|
859
|
+
};
|
|
860
|
+
|
|
861
|
+
const result = await run({ workflow, agentRunner, scriptRunner: noopScript });
|
|
862
|
+
|
|
863
|
+
expect(result.node.phase).toBe("succeeded");
|
|
864
|
+
expect(infos.length).toBe(2);
|
|
865
|
+
// Both calls belong to the same outer attempt — no `def.retries` was spent.
|
|
866
|
+
expect(infos[0]).toMatchObject({
|
|
867
|
+
attempt: 1,
|
|
868
|
+
agentSessionId: undefined,
|
|
869
|
+
previousFailure: undefined,
|
|
870
|
+
});
|
|
871
|
+
expect(infos[1]).toMatchObject({
|
|
872
|
+
attempt: 1,
|
|
873
|
+
agentSessionId: "sess-1",
|
|
874
|
+
previousFailure: { kind: "handoff-missing" },
|
|
875
|
+
});
|
|
876
|
+
});
|
|
877
|
+
|
|
878
|
+
test("nudges no-transition when an unroutable event comes back in a returned NodeResult", async () => {
|
|
879
|
+
const workflow: WorkflowDef = {
|
|
880
|
+
initial: "Agentic",
|
|
881
|
+
states: { Agentic: { type: "agent", prompt: "do it", on: { ok: EXIT } } },
|
|
882
|
+
};
|
|
883
|
+
|
|
884
|
+
const infos: DispatchInfo[] = [];
|
|
885
|
+
const agentRunner: AgentRunner = {
|
|
886
|
+
run: async (_id, _def, _handoff, info) => {
|
|
887
|
+
infos.push(info);
|
|
888
|
+
if (infos.length === 1) return { event: "not_a_route", agentSessionId: "sess-2" };
|
|
889
|
+
return { event: "ok" };
|
|
890
|
+
},
|
|
891
|
+
};
|
|
892
|
+
|
|
893
|
+
const result = await run({ workflow, agentRunner, scriptRunner: noopScript });
|
|
894
|
+
|
|
895
|
+
expect(result.node.phase).toBe("succeeded");
|
|
896
|
+
expect(infos.length).toBe(2);
|
|
897
|
+
expect(infos[1]).toMatchObject({
|
|
898
|
+
attempt: 1,
|
|
899
|
+
agentSessionId: "sess-2",
|
|
900
|
+
previousFailure: { kind: "no-transition", state: "Agentic", event: "not_a_route" },
|
|
901
|
+
});
|
|
902
|
+
});
|
|
903
|
+
|
|
904
|
+
test("exhausts the 3-nudge budget, then falls through and spends one def.retries slot", async () => {
|
|
905
|
+
const workflow: WorkflowDef = {
|
|
906
|
+
initial: "Agentic",
|
|
907
|
+
states: { Agentic: { type: "agent", prompt: "do it", retries: 1, on: { ok: EXIT } } },
|
|
908
|
+
};
|
|
909
|
+
|
|
910
|
+
const infos: DispatchInfo[] = [];
|
|
911
|
+
const agentRunner: AgentRunner = {
|
|
912
|
+
run: async (_id, _def, _handoff, info) => {
|
|
913
|
+
infos.push(info);
|
|
914
|
+
if (infos.length <= 4) {
|
|
915
|
+
throw new AgentFailure("handoff not written", {
|
|
916
|
+
agentSessionId: "sess-3",
|
|
917
|
+
crashReason: { kind: "handoff-missing" },
|
|
918
|
+
});
|
|
919
|
+
}
|
|
920
|
+
return { event: "ok" };
|
|
921
|
+
},
|
|
922
|
+
};
|
|
923
|
+
|
|
924
|
+
const result = await run({ workflow, agentRunner, scriptRunner: noopScript });
|
|
925
|
+
|
|
926
|
+
expect(result.node.phase).toBe("succeeded");
|
|
927
|
+
// 1 initial + 3 nudges, all on outer attempt 1; the 5th call is a fresh
|
|
928
|
+
// outer attempt 2 (retries: 1 affords exactly one).
|
|
929
|
+
expect(infos.length).toBe(5);
|
|
930
|
+
expect(infos.slice(0, 4).map((i) => i.attempt)).toEqual([1, 1, 1, 1]);
|
|
931
|
+
expect(infos[4]).toMatchObject({ attempt: 2 });
|
|
932
|
+
});
|
|
933
|
+
|
|
934
|
+
test("a nudge that fails with runner-error ends the nudge loop without spending further nudges", async () => {
|
|
935
|
+
const workflow: WorkflowDef = {
|
|
936
|
+
initial: "Agentic",
|
|
937
|
+
states: { Agentic: { type: "agent", prompt: "do it", on: { ok: EXIT } } },
|
|
938
|
+
};
|
|
939
|
+
|
|
940
|
+
let calls = 0;
|
|
941
|
+
const agentRunner: AgentRunner = {
|
|
942
|
+
run: async () => {
|
|
943
|
+
calls++;
|
|
944
|
+
if (calls === 1) {
|
|
945
|
+
throw new AgentFailure("handoff not written", {
|
|
946
|
+
agentSessionId: "sess-4",
|
|
947
|
+
crashReason: { kind: "handoff-missing" },
|
|
948
|
+
});
|
|
949
|
+
}
|
|
950
|
+
throw new Error("provider died mid-nudge");
|
|
951
|
+
},
|
|
952
|
+
};
|
|
953
|
+
|
|
954
|
+
const result = await run({ workflow, agentRunner, scriptRunner: noopScript });
|
|
955
|
+
|
|
956
|
+
expect(calls).toBe(2); // initial dispatch + exactly one nudge, which crashes non-nudgeably
|
|
957
|
+
expect(result.node.phase).toBe("crashed");
|
|
958
|
+
if (result.node.phase === "crashed") {
|
|
959
|
+
expect(result.node.reason).toEqual({ kind: "runner-error", error: "provider died mid-nudge" });
|
|
960
|
+
}
|
|
961
|
+
});
|
|
962
|
+
|
|
963
|
+
test("nudge-start is emitted immediately with nudgeAttempt/nudgeReason, not gated behind a heartbeat", async () => {
|
|
964
|
+
const workflow: WorkflowDef = {
|
|
965
|
+
initial: "Agentic",
|
|
966
|
+
states: { Agentic: { type: "agent", prompt: "do it", on: { ok: EXIT } } },
|
|
967
|
+
};
|
|
968
|
+
|
|
969
|
+
let calls = 0;
|
|
970
|
+
const agentRunner: AgentRunner = {
|
|
971
|
+
run: async () => {
|
|
972
|
+
calls++;
|
|
973
|
+
if (calls === 1) {
|
|
974
|
+
throw new AgentFailure("handoff not written", {
|
|
975
|
+
agentSessionId: "sess-5",
|
|
976
|
+
crashReason: { kind: "handoff-missing" },
|
|
977
|
+
});
|
|
978
|
+
}
|
|
979
|
+
return { event: "ok" };
|
|
980
|
+
},
|
|
981
|
+
};
|
|
982
|
+
|
|
983
|
+
const events: NodeEvent[] = [];
|
|
984
|
+
await run({ workflow, agentRunner, scriptRunner: noopScript, onEvent: (e) => events.push(e) });
|
|
985
|
+
|
|
986
|
+
const running = events.filter((e) => e.state === "Agentic" && e.status.phase === "running");
|
|
987
|
+
expect(running.length).toBe(2);
|
|
988
|
+
expect(running[0]!.status).toMatchObject({ attempt: 1 });
|
|
989
|
+
expect((running[0]!.status as { nudgeAttempt?: number }).nudgeAttempt).toBeUndefined();
|
|
990
|
+
expect(running[1]!.status).toMatchObject({
|
|
991
|
+
attempt: 1,
|
|
992
|
+
nudgeAttempt: 1,
|
|
993
|
+
nudgeReason: "handoff-missing",
|
|
994
|
+
});
|
|
995
|
+
});
|
|
996
|
+
|
|
997
|
+
test("a shared per-attempt deadline bounds the whole nudge sequence, not a fresh timeout per nudge", async () => {
|
|
998
|
+
const workflow: WorkflowDef = {
|
|
999
|
+
initial: "Agentic",
|
|
1000
|
+
states: { Agentic: { type: "agent", prompt: "do it", timeoutMs: 100, on: { ok: EXIT } } },
|
|
1001
|
+
};
|
|
1002
|
+
|
|
1003
|
+
let calls = 0;
|
|
1004
|
+
const agentRunner: AgentRunner = {
|
|
1005
|
+
run: async () => {
|
|
1006
|
+
calls++;
|
|
1007
|
+
if (calls === 1) {
|
|
1008
|
+
throw new AgentFailure("handoff not written", {
|
|
1009
|
+
agentSessionId: "sess-6",
|
|
1010
|
+
crashReason: { kind: "handoff-missing" },
|
|
1011
|
+
});
|
|
1012
|
+
}
|
|
1013
|
+
// The nudge itself runs past the attempt's remaining budget (~100ms
|
|
1014
|
+
// total, already spent on call 1 plus engine overhead) — this must
|
|
1015
|
+
// surface as `timeout`, not get a fresh 100ms of its own.
|
|
1016
|
+
await new Promise((r) => setTimeout(r, 300));
|
|
1017
|
+
return { event: "ok" };
|
|
1018
|
+
},
|
|
1019
|
+
};
|
|
1020
|
+
|
|
1021
|
+
const result = await run({ workflow, agentRunner, scriptRunner: noopScript });
|
|
1022
|
+
|
|
1023
|
+
expect(calls).toBe(2);
|
|
1024
|
+
expect(result.node.phase).toBe("crashed");
|
|
1025
|
+
if (result.node.phase === "crashed") {
|
|
1026
|
+
expect(result.node.reason).toEqual({ kind: "timeout", elapsedMs: 100 });
|
|
1027
|
+
}
|
|
1028
|
+
});
|
|
1029
|
+
|
|
1030
|
+
test("a workflow routing to FAILED ends with phase 'failed', never crashed/unresolved-target", async () => {
|
|
1031
|
+
const workflow: WorkflowDef = {
|
|
1032
|
+
initial: "RunFailure",
|
|
1033
|
+
states: { RunFailure: { type: "script", file: "run_failure.ts", on: { gave_up: FAILED } } },
|
|
1034
|
+
};
|
|
1035
|
+
const scriptRunner: ScriptRunner = {
|
|
1036
|
+
run: async () => ({ event: "gave_up", output: { kind: "stuck_signature" } }),
|
|
1037
|
+
};
|
|
1038
|
+
|
|
1039
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner });
|
|
1040
|
+
|
|
1041
|
+
expect(result.node.phase).toBe("failed");
|
|
1042
|
+
if (result.node.phase === "failed") {
|
|
1043
|
+
expect(result.node.result.output).toEqual({ kind: "stuck_signature" });
|
|
1044
|
+
}
|
|
1045
|
+
});
|
|
1046
|
+
|
|
1047
|
+
test("FAILED's terminal journal entry carries the failing node's result as its end marker", async () => {
|
|
1048
|
+
const workflow: WorkflowDef = {
|
|
1049
|
+
initial: "RunFailure",
|
|
1050
|
+
states: { RunFailure: { type: "script", file: "run_failure.ts", on: { gave_up: FAILED } } },
|
|
1051
|
+
};
|
|
1052
|
+
const scriptRunner: ScriptRunner = {
|
|
1053
|
+
run: async () => ({ event: "gave_up", output: { kind: "cap_exceeded" } }),
|
|
1054
|
+
};
|
|
1055
|
+
|
|
1056
|
+
const result = await run({ workflow, agentRunner: noopAgent, scriptRunner });
|
|
1057
|
+
|
|
1058
|
+
const journalPath = join(testRootDir, ".darkf", "run", result.runId, "journal.jsonl");
|
|
1059
|
+
const lines = readFileSync(journalPath, "utf8").trim().split("\n");
|
|
1060
|
+
const parsed = lines.map((l) => JSON.parse(l) as NodeEvent);
|
|
1061
|
+
expect(parsed[0]!.state).toBe(EXIT); // begin marker, unchanged
|
|
1062
|
+
const last = parsed.at(-1)!;
|
|
1063
|
+
expect(last.state).toBe(FAILED);
|
|
1064
|
+
expect(last.status.phase).toBe("failed");
|
|
1065
|
+
if (last.status.phase === "failed") {
|
|
1066
|
+
expect(last.status.result.output).toEqual({ kind: "cap_exceeded" });
|
|
1067
|
+
}
|
|
1068
|
+
});
|