@psimona/darkf 0.7.1 → 0.9.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.
Files changed (35) hide show
  1. package/docs/concepts/failure-modes.md +18 -5
  2. package/docs/concepts/handoff.md +8 -4
  3. package/docs/concepts/node-types.md +45 -21
  4. package/docs/guides/pick-an-agent-runner.md +143 -4
  5. package/docs/guides/plan-with-a-task-queue.md +18 -2
  6. package/docs/guides/run-detached-and-monitor.md +4 -3
  7. package/docs/guides/write-your-first-workflow.md +24 -5
  8. package/docs/reference/cli-commands.md +24 -6
  9. package/docs/reference/core-types.md +41 -6
  10. package/docs/start-here/quickstart.md +22 -2
  11. package/package.json +1 -1
  12. package/skills/darkf-usage/SKILL.md +17 -0
  13. package/skills/darkf-writing-workflows/SKILL.md +25 -18
  14. package/src/cli/detached.test.ts +92 -2
  15. package/src/cli/doctor.test.ts +224 -9
  16. package/src/cli/index.ts +83 -12
  17. package/src/cli/tarball.test.ts +8 -2
  18. package/src/core/graph.test.ts +266 -20
  19. package/src/core/graph.ts +61 -3
  20. package/src/core/types.ts +21 -0
  21. package/src/engine/run.test.ts +581 -17
  22. package/src/engine/run.ts +50 -7
  23. package/src/kit/agents/claude-code.test.ts +38 -2
  24. package/src/kit/agents/claude-code.ts +24 -3
  25. package/src/kit/agents/opencode.test.ts +89 -12
  26. package/src/kit/agents/opencode.ts +103 -8
  27. package/src/kit/status.test.ts +24 -0
  28. package/src/kit/status.ts +25 -8
  29. package/src/test/ok-output.ts +11 -0
  30. package/workflows/dev-loop/output-types.ts +43 -0
  31. package/workflows/dev-loop/scripts/begin.ts +2 -2
  32. package/workflows/dev-loop/scripts/finish.ts +2 -36
  33. package/workflows/dev-loop/workflow.ts +18 -4
  34. package/workflows/series-ordering/output-types.ts +9 -0
  35. package/workflows/series-ordering/workflow.ts +4 -0
@@ -14,6 +14,7 @@ import {
14
14
  type ScriptRunner,
15
15
  type WorkflowDef,
16
16
  } from "../core/index.ts";
17
+ import { okOutputType } from "../test/ok-output.ts";
17
18
  import { run as engineRun, type RunOptions } from "./run.ts";
18
19
 
19
20
  const noopAgent: AgentRunner = { run: async () => ({}) };
@@ -32,9 +33,21 @@ test("runs a flat workflow to completion via on/next", async () => {
32
33
  const workflow: WorkflowDef = {
33
34
  initial: "Plan",
34
35
  states: {
35
- Plan: { type: "agent", prompt: "plan", on: { approved: "Build" } },
36
+ Plan: {
37
+ type: "agent",
38
+ outputType: okOutputType,
39
+ agent: { model: "test-model" },
40
+ prompt: "plan",
41
+ on: { approved: "Build" },
42
+ },
36
43
  Build: { type: "script", file: "build.ts", next: "Review" },
37
- Review: { type: "agent", prompt: "review", on: { done: EXIT, changes_requested: "Build" } },
44
+ Review: {
45
+ type: "agent",
46
+ outputType: okOutputType,
47
+ agent: { model: "test-model" },
48
+ prompt: "review",
49
+ on: { done: EXIT, changes_requested: "Build" },
50
+ },
38
51
  },
39
52
  };
40
53
 
@@ -57,7 +70,13 @@ test("supports a cross-scope edge directly out of a composite", async () => {
57
70
  const workflow: WorkflowDef = {
58
71
  initial: "Plan",
59
72
  states: {
60
- Plan: { type: "agent", prompt: "plan", on: { approved: "Implement" } },
73
+ Plan: {
74
+ type: "agent",
75
+ outputType: okOutputType,
76
+ agent: { model: "test-model" },
77
+ prompt: "plan",
78
+ on: { approved: "Implement" },
79
+ },
61
80
  Implement: {
62
81
  initial: "Write",
63
82
  states: {
@@ -65,7 +84,13 @@ test("supports a cross-scope edge directly out of a composite", async () => {
65
84
  Test: { type: "script", file: "test.ts", on: { passed: "Review" } },
66
85
  },
67
86
  },
68
- Review: { type: "agent", prompt: "review", on: { done: EXIT } },
87
+ Review: {
88
+ type: "agent",
89
+ outputType: okOutputType,
90
+ agent: { model: "test-model" },
91
+ prompt: "review",
92
+ on: { done: EXIT },
93
+ },
69
94
  },
70
95
  };
71
96
 
@@ -93,7 +118,13 @@ test("supports re-entry into a leaf inside a different composite", async () => {
93
118
  Test: { type: "script", file: "test.ts", on: { passed: "Review", failed: "Write" } },
94
119
  },
95
120
  },
96
- Review: { type: "agent", prompt: "review", on: { done: EXIT, changes_requested: "Write" } },
121
+ Review: {
122
+ type: "agent",
123
+ outputType: okOutputType,
124
+ agent: { model: "test-model" },
125
+ prompt: "review",
126
+ on: { done: EXIT, changes_requested: "Write" },
127
+ },
97
128
  },
98
129
  };
99
130
 
@@ -207,6 +238,31 @@ test("crashes eagerly with invalid-workflow for a composite initial-cycle, inste
207
238
  }
208
239
  });
209
240
 
241
+ test("crashes eagerly with invalid-workflow for an agent leaf without an outputType, before any dispatch", async () => {
242
+ const workflow: WorkflowDef = {
243
+ initial: "Solo",
244
+ states: { Solo: { type: "agent", agent: { model: "test-model" }, prompt: "p", next: EXIT } },
245
+ };
246
+ let calls = 0;
247
+ const agentRunner: AgentRunner = {
248
+ run: async () => {
249
+ calls++;
250
+ return {};
251
+ },
252
+ };
253
+
254
+ const result = await run({ workflow, agentRunner, scriptRunner: noopScript });
255
+
256
+ expect(calls).toBe(0); // never dispatched — caught before the first enter()
257
+ expect(result.node.phase).toBe("crashed");
258
+ if (result.node.phase === "crashed") {
259
+ expect(result.node.reason).toEqual({
260
+ kind: "invalid-workflow",
261
+ errors: [{ kind: "missing-output-type", state: "Solo" }],
262
+ });
263
+ }
264
+ });
265
+
210
266
  test("crashes with a duplicate-id reason instead of throwing", async () => {
211
267
  const workflow: WorkflowDef = {
212
268
  initial: "A",
@@ -586,6 +642,8 @@ test("threads agentSessionId and previousFailure into DispatchInfo across nudges
586
642
  states: {
587
643
  Agentic: {
588
644
  type: "agent",
645
+ outputType: okOutputType,
646
+ agent: { model: "test-model" },
589
647
  prompt: "do it",
590
648
  retries: 1,
591
649
  on: { ok: EXIT },
@@ -637,6 +695,8 @@ test("threads step and rootDir into DispatchInfo, step constant across retries o
637
695
  states: {
638
696
  Plan: {
639
697
  type: "agent",
698
+ outputType: okOutputType,
699
+ agent: { model: "test-model" },
640
700
  prompt: "plan",
641
701
  retries: 1,
642
702
  on: { approved: "Build" },
@@ -793,7 +853,15 @@ test("script nodes receive DispatchInfo (rootDir) as their second argument", asy
793
853
  test("an agent runner's onProgress is folded into the running heartbeat as agentStepCount/agentIdleMs", async () => {
794
854
  const workflow: WorkflowDef = {
795
855
  initial: "Agentic",
796
- states: { Agentic: { type: "agent", prompt: "go", next: EXIT } },
856
+ states: {
857
+ Agentic: {
858
+ type: "agent",
859
+ outputType: okOutputType,
860
+ agent: { model: "test-model" },
861
+ prompt: "go",
862
+ next: EXIT,
863
+ },
864
+ },
797
865
  };
798
866
  const agentRunner: AgentRunner = {
799
867
  run: async (_id, _def, _handoff, _info, onProgress) => {
@@ -854,7 +922,15 @@ test("open dispatches heartbeat the journal (live runtime)", async () => {
854
922
  test("nudges a handoff-missing AgentFailure in-session, consuming no retry", async () => {
855
923
  const workflow: WorkflowDef = {
856
924
  initial: "Agentic",
857
- states: { Agentic: { type: "agent", prompt: "do it", on: { ok: EXIT } } },
925
+ states: {
926
+ Agentic: {
927
+ type: "agent",
928
+ outputType: okOutputType,
929
+ agent: { model: "test-model" },
930
+ prompt: "do it",
931
+ on: { ok: EXIT },
932
+ },
933
+ },
858
934
  };
859
935
 
860
936
  const infos: DispatchInfo[] = [];
@@ -891,7 +967,15 @@ test("nudges a handoff-missing AgentFailure in-session, consuming no retry", asy
891
967
  test("nudges no-transition when an unroutable event comes back in a returned NodeResult", async () => {
892
968
  const workflow: WorkflowDef = {
893
969
  initial: "Agentic",
894
- states: { Agentic: { type: "agent", prompt: "do it", on: { ok: EXIT } } },
970
+ states: {
971
+ Agentic: {
972
+ type: "agent",
973
+ outputType: okOutputType,
974
+ agent: { model: "test-model" },
975
+ prompt: "do it",
976
+ on: { ok: EXIT },
977
+ },
978
+ },
895
979
  };
896
980
 
897
981
  const infos: DispatchInfo[] = [];
@@ -917,7 +1001,16 @@ test("nudges no-transition when an unroutable event comes back in a returned Nod
917
1001
  test("exhausts the 3-nudge budget, then falls through and spends one def.retries slot", async () => {
918
1002
  const workflow: WorkflowDef = {
919
1003
  initial: "Agentic",
920
- states: { Agentic: { type: "agent", prompt: "do it", retries: 1, on: { ok: EXIT } } },
1004
+ states: {
1005
+ Agentic: {
1006
+ type: "agent",
1007
+ outputType: okOutputType,
1008
+ agent: { model: "test-model" },
1009
+ prompt: "do it",
1010
+ retries: 1,
1011
+ on: { ok: EXIT },
1012
+ },
1013
+ },
921
1014
  };
922
1015
 
923
1016
  const infos: DispatchInfo[] = [];
@@ -947,7 +1040,15 @@ test("exhausts the 3-nudge budget, then falls through and spends one def.retries
947
1040
  test("a nudge that fails with runner-error ends the nudge loop without spending further nudges", async () => {
948
1041
  const workflow: WorkflowDef = {
949
1042
  initial: "Agentic",
950
- states: { Agentic: { type: "agent", prompt: "do it", on: { ok: EXIT } } },
1043
+ states: {
1044
+ Agentic: {
1045
+ type: "agent",
1046
+ outputType: okOutputType,
1047
+ agent: { model: "test-model" },
1048
+ prompt: "do it",
1049
+ on: { ok: EXIT },
1050
+ },
1051
+ },
951
1052
  };
952
1053
 
953
1054
  let calls = 0;
@@ -976,7 +1077,15 @@ test("a nudge that fails with runner-error ends the nudge loop without spending
976
1077
  test("nudge-start is emitted immediately with nudgeAttempt/nudgeReason, not gated behind a heartbeat", async () => {
977
1078
  const workflow: WorkflowDef = {
978
1079
  initial: "Agentic",
979
- states: { Agentic: { type: "agent", prompt: "do it", on: { ok: EXIT } } },
1080
+ states: {
1081
+ Agentic: {
1082
+ type: "agent",
1083
+ outputType: okOutputType,
1084
+ agent: { model: "test-model" },
1085
+ prompt: "do it",
1086
+ on: { ok: EXIT },
1087
+ },
1088
+ },
980
1089
  };
981
1090
 
982
1091
  let calls = 0;
@@ -1010,7 +1119,16 @@ test("nudge-start is emitted immediately with nudgeAttempt/nudgeReason, not gate
1010
1119
  test("a shared per-attempt deadline bounds the whole nudge sequence, not a fresh timeout per nudge", async () => {
1011
1120
  const workflow: WorkflowDef = {
1012
1121
  initial: "Agentic",
1013
- states: { Agentic: { type: "agent", prompt: "do it", timeoutMs: 100, on: { ok: EXIT } } },
1122
+ states: {
1123
+ Agentic: {
1124
+ type: "agent",
1125
+ outputType: okOutputType,
1126
+ agent: { model: "test-model" },
1127
+ prompt: "do it",
1128
+ timeoutMs: 100,
1129
+ on: { ok: EXIT },
1130
+ },
1131
+ },
1014
1132
  };
1015
1133
 
1016
1134
  let calls = 0;
@@ -1048,6 +1166,7 @@ test("a failing outputType nudges in-session with the message in previousFailure
1048
1166
  states: {
1049
1167
  Shaped: {
1050
1168
  type: "agent",
1169
+ agent: { model: "test-model" },
1051
1170
  prompt: "shape it",
1052
1171
  retries: 1,
1053
1172
  on: { ok: EXIT },
@@ -1101,6 +1220,8 @@ test("a failing validate nudges in-session with the script's message in previous
1101
1220
  states: {
1102
1221
  Picky: {
1103
1222
  type: "agent",
1223
+ outputType: okOutputType,
1224
+ agent: { model: "test-model" },
1104
1225
  prompt: "be good",
1105
1226
  retries: 1,
1106
1227
  on: { ok: EXIT },
@@ -1152,6 +1273,8 @@ test("regression: a validate-only leaf still rejects the same inputs, now nudgin
1152
1273
  states: {
1153
1274
  Picky: {
1154
1275
  type: "agent",
1276
+ outputType: okOutputType,
1277
+ agent: { model: "test-model" },
1155
1278
  prompt: "be good",
1156
1279
  retries: 1,
1157
1280
  on: { ok: EXIT },
@@ -1248,7 +1371,14 @@ test("a usage-limit failure waits the interval, then re-dispatches the SAME atte
1248
1371
  const workflow: WorkflowDef = {
1249
1372
  initial: "Agentic",
1250
1373
  states: {
1251
- Agentic: { type: "agent", prompt: "do it", usageLimitRetryMs: 100, on: { ok: EXIT } },
1374
+ Agentic: {
1375
+ type: "agent",
1376
+ outputType: okOutputType,
1377
+ agent: { model: "test-model" },
1378
+ prompt: "do it",
1379
+ usageLimitRetryMs: 100,
1380
+ on: { ok: EXIT },
1381
+ },
1252
1382
  },
1253
1383
  };
1254
1384
 
@@ -1284,7 +1414,14 @@ test("while waiting, running heartbeats carry limitBackoff (probe, nextAttemptAt
1284
1414
  const workflow: WorkflowDef = {
1285
1415
  initial: "Agentic",
1286
1416
  states: {
1287
- Agentic: { type: "agent", prompt: "do it", usageLimitRetryMs: 100, on: { ok: EXIT } },
1417
+ Agentic: {
1418
+ type: "agent",
1419
+ outputType: okOutputType,
1420
+ agent: { model: "test-model" },
1421
+ prompt: "do it",
1422
+ usageLimitRetryMs: 100,
1423
+ on: { ok: EXIT },
1424
+ },
1288
1425
  },
1289
1426
  };
1290
1427
 
@@ -1333,6 +1470,8 @@ test("exhausting usageLimitMaxWaitMs crashes with the last usage-limit reason; p
1333
1470
  states: {
1334
1471
  Agentic: {
1335
1472
  type: "agent",
1473
+ outputType: okOutputType,
1474
+ agent: { model: "test-model" },
1336
1475
  prompt: "do it",
1337
1476
  usageLimitRetryMs: 50,
1338
1477
  usageLimitMaxWaitMs: 120,
@@ -1386,6 +1525,8 @@ test("the attempt deadline is recomputed after each usage-limit wait, so a slept
1386
1525
  states: {
1387
1526
  Agentic: {
1388
1527
  type: "agent",
1528
+ outputType: okOutputType,
1529
+ agent: { model: "test-model" },
1389
1530
  prompt: "do it",
1390
1531
  timeoutMs: 80,
1391
1532
  usageLimitRetryMs: 50,
@@ -1422,7 +1563,13 @@ test("wraps an event-less agent result as { output: payload }", async () => {
1422
1563
  const workflow: WorkflowDef = {
1423
1564
  initial: "Work",
1424
1565
  states: {
1425
- Work: { type: "agent", prompt: "work", next: "Gate" },
1566
+ Work: {
1567
+ type: "agent",
1568
+ outputType: okOutputType,
1569
+ agent: { model: "test-model" },
1570
+ prompt: "work",
1571
+ next: "Gate",
1572
+ },
1426
1573
  Gate: { type: "script", file: "gate.ts", next: EXIT },
1427
1574
  },
1428
1575
  };
@@ -1449,7 +1596,13 @@ test("hybrid payload: flat claim plus nested output survives to handoff.output",
1449
1596
  const workflow: WorkflowDef = {
1450
1597
  initial: "Verify",
1451
1598
  states: {
1452
- Verify: { type: "agent", prompt: "verify", next: "Gate" },
1599
+ Verify: {
1600
+ type: "agent",
1601
+ outputType: okOutputType,
1602
+ agent: { model: "test-model" },
1603
+ prompt: "verify",
1604
+ next: "Gate",
1605
+ },
1453
1606
  Gate: { type: "script", file: "gate.ts", next: EXIT },
1454
1607
  },
1455
1608
  };
@@ -1481,7 +1634,13 @@ test("envelope results (event present) are passed through untouched", async () =
1481
1634
  const workflow: WorkflowDef = {
1482
1635
  initial: "Work",
1483
1636
  states: {
1484
- Work: { type: "agent", prompt: "work", on: { done: "Gate" } },
1637
+ Work: {
1638
+ type: "agent",
1639
+ outputType: okOutputType,
1640
+ agent: { model: "test-model" },
1641
+ prompt: "work",
1642
+ on: { done: "Gate" },
1643
+ },
1485
1644
  Gate: { type: "script", file: "gate.ts", next: EXIT },
1486
1645
  },
1487
1646
  };
@@ -1525,3 +1684,408 @@ test("script results are never coerced", async () => {
1525
1684
  expect(result.node.phase).toBe("succeeded");
1526
1685
  expect(seen).toEqual({ probe: 1 }); // raw handoff, not wrapped
1527
1686
  });
1687
+
1688
+ // Records what each dispatch received, tagged with the stub runner that got it.
1689
+ function recordingRunner(
1690
+ tag: string,
1691
+ calls: { tag: string; id: string; agent: unknown; sessionId?: string }[],
1692
+ ): AgentRunner {
1693
+ return {
1694
+ run: async (id, def, _handoff, info) => {
1695
+ calls.push({ tag, id, agent: def.agent, sessionId: info.agentSessionId });
1696
+ return {};
1697
+ },
1698
+ };
1699
+ }
1700
+
1701
+ test("dispatches each state to the runner it resolves to, with that state's agent bag", async () => {
1702
+ const workflow: WorkflowDef = {
1703
+ initial: "Plan",
1704
+ states: {
1705
+ Plan: {
1706
+ type: "agent",
1707
+ outputType: okOutputType,
1708
+ prompt: "plan",
1709
+ runner: "other",
1710
+ agent: { model: "model-a" },
1711
+ next: "Build",
1712
+ },
1713
+ Build: {
1714
+ type: "agent",
1715
+ outputType: okOutputType,
1716
+ prompt: "build",
1717
+ agent: { model: "model-b" },
1718
+ next: EXIT,
1719
+ },
1720
+ },
1721
+ };
1722
+ const calls: Parameters<typeof recordingRunner>[1] = [];
1723
+
1724
+ const result = await run({
1725
+ workflow,
1726
+ agentRunner: recordingRunner("default", calls),
1727
+ agentRunners: { other: recordingRunner("other", calls) },
1728
+ });
1729
+
1730
+ expect(result.node.phase).toBe("succeeded");
1731
+ expect(calls).toMatchObject([
1732
+ { tag: "other", id: "Plan", agent: { model: "model-a" } },
1733
+ { tag: "default", id: "Build", agent: { model: "model-b" } },
1734
+ ]);
1735
+ });
1736
+
1737
+ test("a workflow whose agent states all name a runner needs no agentRunner", async () => {
1738
+ const workflow: WorkflowDef = {
1739
+ initial: "Plan",
1740
+ states: {
1741
+ Plan: {
1742
+ type: "agent",
1743
+ outputType: okOutputType,
1744
+ agent: { model: "test-model" },
1745
+ prompt: "plan",
1746
+ runner: "other",
1747
+ next: EXIT,
1748
+ },
1749
+ },
1750
+ };
1751
+
1752
+ const result = await run({ workflow, agentRunners: { other: noopAgent } });
1753
+
1754
+ expect(result.node.phase).toBe("succeeded");
1755
+ });
1756
+
1757
+ test.each(["typo", "toString"])("runner %p never falls back to the default", async (name) => {
1758
+ const workflow: WorkflowDef = {
1759
+ initial: "Plan",
1760
+ states: {
1761
+ Plan: {
1762
+ type: "agent",
1763
+ outputType: okOutputType,
1764
+ agent: { model: "test-model" },
1765
+ prompt: "plan",
1766
+ runner: name,
1767
+ next: EXIT,
1768
+ },
1769
+ },
1770
+ };
1771
+ const calls: Parameters<typeof recordingRunner>[1] = [];
1772
+
1773
+ const result = await run({
1774
+ workflow,
1775
+ agentRunner: recordingRunner("default", calls),
1776
+ agentRunners: {},
1777
+ });
1778
+
1779
+ expect(result.node).toMatchObject({
1780
+ phase: "crashed",
1781
+ reason: {
1782
+ kind: "invalid-workflow",
1783
+ errors: [{ kind: "unknown-runner", state: "Plan", runner: name, known: [] }],
1784
+ },
1785
+ });
1786
+ expect(calls).toEqual([]);
1787
+ });
1788
+
1789
+ test("an unknown runner on a later state crashes before the first dispatch", async () => {
1790
+ const workflow: WorkflowDef = {
1791
+ initial: "Plan",
1792
+ states: {
1793
+ Plan: {
1794
+ type: "agent",
1795
+ outputType: okOutputType,
1796
+ agent: { model: "test-model" },
1797
+ prompt: "p",
1798
+ next: "Build",
1799
+ },
1800
+ Build: {
1801
+ type: "agent",
1802
+ outputType: okOutputType,
1803
+ agent: { model: "test-model" },
1804
+ prompt: "p",
1805
+ runner: "typo",
1806
+ next: EXIT,
1807
+ },
1808
+ },
1809
+ };
1810
+ const calls: Parameters<typeof recordingRunner>[1] = [];
1811
+ const runner = recordingRunner("default", calls);
1812
+
1813
+ const result = await run({ workflow, agentRunner: runner, agentRunners: { other: runner } });
1814
+
1815
+ expect(result.node).toMatchObject({
1816
+ phase: "crashed",
1817
+ reason: {
1818
+ kind: "invalid-workflow",
1819
+ errors: [{ kind: "unknown-runner", state: "Build", runner: "typo", known: ["other"] }],
1820
+ },
1821
+ });
1822
+ expect(calls).toEqual([]);
1823
+ });
1824
+
1825
+ test("without a default runner, a state naming none crashes before any dispatch", async () => {
1826
+ const workflow: WorkflowDef = {
1827
+ initial: "Plan",
1828
+ states: {
1829
+ Plan: {
1830
+ type: "agent",
1831
+ outputType: okOutputType,
1832
+ agent: { model: "test-model" },
1833
+ prompt: "p",
1834
+ runner: "other",
1835
+ retries: 2,
1836
+ next: "Build",
1837
+ },
1838
+ Build: {
1839
+ type: "agent",
1840
+ outputType: okOutputType,
1841
+ agent: { model: "test-model" },
1842
+ prompt: "p",
1843
+ retries: 2,
1844
+ next: EXIT,
1845
+ },
1846
+ },
1847
+ };
1848
+ const calls: Parameters<typeof recordingRunner>[1] = [];
1849
+
1850
+ const result = await run({
1851
+ workflow,
1852
+ agentRunners: { other: recordingRunner("other", calls) },
1853
+ });
1854
+
1855
+ expect(result.node).toMatchObject({
1856
+ phase: "crashed",
1857
+ attempt: 0,
1858
+ reason: { kind: "invalid-workflow", errors: [{ kind: "missing-runner", state: "Build" }] },
1859
+ });
1860
+ expect(calls).toEqual([]);
1861
+ });
1862
+
1863
+ test("merges the workflow's agent bag under default-runner states only, without modifying the workflow", async () => {
1864
+ const shared = { model: "default-model", auto: true, extra: { a: 1 } };
1865
+ const workflow: WorkflowDef = {
1866
+ initial: "Inherit",
1867
+ agent: shared,
1868
+ states: {
1869
+ Inherit: { type: "agent", outputType: okOutputType, prompt: "p", next: "Override" },
1870
+ Override: {
1871
+ type: "agent",
1872
+ outputType: okOutputType,
1873
+ prompt: "p",
1874
+ agent: { model: "own-model", extra: { b: 2 } },
1875
+ next: "Named",
1876
+ },
1877
+ Named: {
1878
+ type: "agent",
1879
+ outputType: okOutputType,
1880
+ prompt: "p",
1881
+ runner: "same",
1882
+ agent: { model: "named-model" },
1883
+ next: EXIT,
1884
+ },
1885
+ },
1886
+ };
1887
+ // structuredClone cannot carry outputType's safeParse function — the one
1888
+ // function every agent leaf now declares. Serialize with functions stubbed
1889
+ // instead: the assertion is about def.agent mutation, and any other change
1890
+ // to the def still changes the string.
1891
+ const snapshot = (value: unknown) =>
1892
+ JSON.stringify(value, (_key, v) => (typeof v === "function" ? "fn" : v));
1893
+ const before = snapshot(workflow);
1894
+ const calls: Parameters<typeof recordingRunner>[1] = [];
1895
+ // The named runner is the very same object as the default one.
1896
+ const runner = recordingRunner("same", calls);
1897
+
1898
+ const result = await run({ workflow, agentRunner: runner, agentRunners: { same: runner } });
1899
+
1900
+ expect(result.node.phase).toBe("succeeded");
1901
+ expect(calls.map((c) => c.agent)).toStrictEqual([
1902
+ shared,
1903
+ { model: "own-model", auto: true, extra: { b: 2 } },
1904
+ { model: "named-model" },
1905
+ ]);
1906
+ expect(snapshot(workflow)).toBe(before);
1907
+ });
1908
+
1909
+ test("a runner writing to its def.agent reaches neither the workflow nor the next attempt", async () => {
1910
+ const workflow: WorkflowDef = {
1911
+ initial: "Plan",
1912
+ agent: { model: "default-model" },
1913
+ states: {
1914
+ Plan: {
1915
+ type: "agent",
1916
+ outputType: okOutputType,
1917
+ prompt: "p",
1918
+ runner: "other",
1919
+ agent: { model: "own" },
1920
+ retries: 1,
1921
+ next: EXIT,
1922
+ },
1923
+ },
1924
+ };
1925
+ const seen: unknown[] = [];
1926
+ const mutating: AgentRunner = {
1927
+ run: async (_id, def, _handoff, info) => {
1928
+ seen.push(def.agent?.model);
1929
+ if (def.agent) def.agent.model = "mutated";
1930
+ if (info.attempt === 1) throw new Error("boom");
1931
+ return {};
1932
+ },
1933
+ };
1934
+
1935
+ const result = await run({ workflow, agentRunners: { other: mutating } });
1936
+
1937
+ expect(result.node.phase).toBe("succeeded");
1938
+ expect(seen).toEqual(["own", "own"]);
1939
+ expect((workflow.states.Plan as { agent: unknown }).agent).toEqual({ model: "own" });
1940
+ });
1941
+
1942
+ test("a session id from one runner never reaches the next state's runner", async () => {
1943
+ const workflow: WorkflowDef = {
1944
+ initial: "Plan",
1945
+ states: {
1946
+ Plan: {
1947
+ type: "agent",
1948
+ outputType: okOutputType,
1949
+ agent: { model: "test-model" },
1950
+ prompt: "p",
1951
+ runner: "first",
1952
+ next: "Build",
1953
+ },
1954
+ Build: {
1955
+ type: "agent",
1956
+ outputType: okOutputType,
1957
+ agent: { model: "test-model" },
1958
+ prompt: "p",
1959
+ next: EXIT,
1960
+ },
1961
+ },
1962
+ };
1963
+ const calls: Parameters<typeof recordingRunner>[1] = [];
1964
+
1965
+ const result = await run({
1966
+ workflow,
1967
+ agentRunner: recordingRunner("default", calls),
1968
+ agentRunners: {
1969
+ first: { run: async () => ({ event: "planned", agentSessionId: "sess-first" }) },
1970
+ },
1971
+ });
1972
+
1973
+ expect(result.node.phase).toBe("succeeded");
1974
+ expect(result.trace[0]?.result.agentSessionId).toBe("sess-first");
1975
+ expect(calls).toMatchObject([{ id: "Build" }]);
1976
+ expect(calls[0]?.sessionId).toBeUndefined();
1977
+ });
1978
+
1979
+ test.each([undefined, "", 7])("a model of %p crashes before any dispatch", async (model) => {
1980
+ const workflow: WorkflowDef = {
1981
+ initial: "Plan",
1982
+ states: {
1983
+ Plan: {
1984
+ type: "agent",
1985
+ outputType: okOutputType,
1986
+ agent: { model: "test-model" },
1987
+ prompt: "p",
1988
+ next: "Build",
1989
+ },
1990
+ Build: {
1991
+ type: "agent",
1992
+ outputType: okOutputType,
1993
+ agent: { model },
1994
+ prompt: "p",
1995
+ retries: 2,
1996
+ next: EXIT,
1997
+ },
1998
+ },
1999
+ };
2000
+ const calls: Parameters<typeof recordingRunner>[1] = [];
2001
+
2002
+ const result = await run({ workflow, agentRunner: recordingRunner("default", calls) });
2003
+
2004
+ expect(result.node).toMatchObject({
2005
+ phase: "crashed",
2006
+ attempt: 0,
2007
+ reason: { kind: "invalid-workflow", errors: [{ kind: "missing-model", state: "Build" }] },
2008
+ });
2009
+ expect(calls).toEqual([]);
2010
+ });
2011
+
2012
+ test("the default bag's model satisfies default-runner states only", async () => {
2013
+ const workflow: WorkflowDef = {
2014
+ initial: "Plan",
2015
+ agent: { model: "default-model" },
2016
+ states: {
2017
+ Plan: { type: "agent", outputType: okOutputType, prompt: "p", next: "Build" },
2018
+ Build: { type: "agent", outputType: okOutputType, prompt: "p", runner: "other", next: EXIT },
2019
+ },
2020
+ };
2021
+ const calls: Parameters<typeof recordingRunner>[1] = [];
2022
+ const runner = recordingRunner("same", calls);
2023
+
2024
+ const result = await run({ workflow, agentRunner: runner, agentRunners: { other: runner } });
2025
+
2026
+ expect(result.node).toMatchObject({
2027
+ phase: "crashed",
2028
+ reason: { kind: "invalid-workflow", errors: [{ kind: "missing-model", state: "Build" }] },
2029
+ });
2030
+ expect(calls).toEqual([]);
2031
+ });
2032
+
2033
+ test("every running status of an agent dispatch records the selected runner and its kind", async () => {
2034
+ const agent = { model: "test-model" };
2035
+ const workflow: WorkflowDef = {
2036
+ initial: "Default",
2037
+ states: {
2038
+ Default: { type: "agent", outputType: okOutputType, agent, prompt: "p", next: "Named" },
2039
+ Named: {
2040
+ type: "agent",
2041
+ outputType: okOutputType,
2042
+ agent,
2043
+ prompt: "p",
2044
+ runner: "claude",
2045
+ next: "Kindless",
2046
+ },
2047
+ Kindless: {
2048
+ type: "agent",
2049
+ outputType: okOutputType,
2050
+ agent,
2051
+ prompt: "p",
2052
+ runner: "custom",
2053
+ next: "BareDefault",
2054
+ },
2055
+ BareDefault: { type: "script", file: "a.ts", next: EXIT },
2056
+ },
2057
+ };
2058
+ const slow: AgentRunner["run"] = async () => {
2059
+ await new Promise((r) => setTimeout(r, 120));
2060
+ return {};
2061
+ };
2062
+ const events: NodeEvent[] = [];
2063
+
2064
+ const result = await run({
2065
+ workflow,
2066
+ agentRunner: { kind: "opencode", run: slow },
2067
+ agentRunners: { claude: { kind: "claude-code", run: slow }, custom: { run: slow } },
2068
+ scriptRunner: noopScript,
2069
+ heartbeatMs: 25,
2070
+ onEvent: (e) => events.push(e),
2071
+ });
2072
+
2073
+ expect(result.node.phase).toBe("succeeded");
2074
+ const recorded = (state: string) =>
2075
+ events.flatMap((e) =>
2076
+ e.state === state && e.status.phase === "running"
2077
+ ? [{ runner: e.status.runner, runnerKind: e.status.runnerKind }]
2078
+ : [],
2079
+ );
2080
+ const expected = {
2081
+ Default: { runner: undefined, runnerKind: "opencode" },
2082
+ Named: { runner: "claude", runnerKind: "claude-code" },
2083
+ Kindless: { runner: "custom", runnerKind: undefined },
2084
+ };
2085
+ for (const [state, record] of Object.entries(expected)) {
2086
+ const statuses = recorded(state);
2087
+ expect(statuses.length).toBeGreaterThanOrEqual(2); // the open plus heartbeats
2088
+ for (const status of statuses) expect(status).toEqual(record);
2089
+ }
2090
+ expect(recorded("BareDefault")[0]).toEqual({ runner: undefined, runnerKind: undefined });
2091
+ });