@psimona/darkf 0.6.0 → 0.8.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 (49) hide show
  1. package/docs/concepts/failure-modes.md +23 -7
  2. package/docs/concepts/handoff.md +3 -3
  3. package/docs/concepts/node-types.md +44 -20
  4. package/docs/guides/pick-an-agent-runner.md +122 -4
  5. package/docs/guides/plan-with-a-task-queue.md +9 -7
  6. package/docs/guides/run-detached-and-monitor.md +3 -3
  7. package/docs/guides/write-your-first-workflow.md +14 -5
  8. package/docs/index.mdx +4 -0
  9. package/docs/reference/cli-commands.md +22 -6
  10. package/docs/reference/core-types.md +29 -4
  11. package/docs/reference/package-map.md +12 -8
  12. package/docs/start-here/quickstart.md +15 -7
  13. package/package.json +9 -2
  14. package/skills/darkf-writing-workflows/SKILL.md +141 -7
  15. package/src/cli/detached.test.ts +88 -2
  16. package/src/cli/doctor.test.ts +225 -10
  17. package/src/cli/index.ts +106 -27
  18. package/src/cli/setup.ts +4 -9
  19. package/src/cli/state-file.ts +14 -0
  20. package/src/cli/tarball.test.ts +3 -1
  21. package/src/cli/update.ts +4 -9
  22. package/src/core/graph.test.ts +124 -19
  23. package/src/core/graph.ts +51 -3
  24. package/src/core/tasks.ts +1 -1
  25. package/src/core/types.test.ts +52 -0
  26. package/src/core/types.ts +48 -18
  27. package/src/engine/run.test.ts +708 -18
  28. package/src/engine/run.ts +101 -16
  29. package/src/exports.ts +4 -0
  30. package/src/kit/agents/claude-code.test.ts +38 -2
  31. package/src/kit/agents/claude-code.ts +28 -37
  32. package/src/kit/agents/handoff.test.ts +12 -0
  33. package/src/kit/agents/handoff.ts +32 -8
  34. package/src/kit/agents/opencode.test.ts +1 -1
  35. package/src/kit/agents/opencode.ts +44 -28
  36. package/src/kit/specs.test.ts +247 -0
  37. package/src/kit/specs.ts +344 -0
  38. package/src/kit/status.test.ts +24 -0
  39. package/src/kit/status.ts +31 -14
  40. package/src/kit/validation/zod.test.ts +42 -0
  41. package/src/kit/validation/zod.ts +20 -0
  42. package/workflows/dev-loop/scripts/begin.ts +9 -6
  43. package/workflows/dev-loop/scripts/dispatch.ts +50 -0
  44. package/workflows/dev-loop/scripts/finish.ts +23 -21
  45. package/workflows/dev-loop/scripts/mark-done.ts +22 -0
  46. package/workflows/dev-loop/scripts/validate-plan.ts +30 -0
  47. package/workflows/dev-loop/workflow.ts +164 -68
  48. package/workflows/series-ordering/workflow.ts +1 -0
  49. /package/workflows/dev-loop/scripts/{check.ts → test.ts} +0 -0
@@ -32,9 +32,19 @@ test("runs a flat workflow to completion via on/next", async () => {
32
32
  const workflow: WorkflowDef = {
33
33
  initial: "Plan",
34
34
  states: {
35
- Plan: { type: "agent", prompt: "plan", on: { approved: "Build" } },
35
+ Plan: {
36
+ type: "agent",
37
+ agent: { model: "test-model" },
38
+ prompt: "plan",
39
+ on: { approved: "Build" },
40
+ },
36
41
  Build: { type: "script", file: "build.ts", next: "Review" },
37
- Review: { type: "agent", prompt: "review", on: { done: EXIT, changes_requested: "Build" } },
42
+ Review: {
43
+ type: "agent",
44
+ agent: { model: "test-model" },
45
+ prompt: "review",
46
+ on: { done: EXIT, changes_requested: "Build" },
47
+ },
38
48
  },
39
49
  };
40
50
 
@@ -57,7 +67,12 @@ test("supports a cross-scope edge directly out of a composite", async () => {
57
67
  const workflow: WorkflowDef = {
58
68
  initial: "Plan",
59
69
  states: {
60
- Plan: { type: "agent", prompt: "plan", on: { approved: "Implement" } },
70
+ Plan: {
71
+ type: "agent",
72
+ agent: { model: "test-model" },
73
+ prompt: "plan",
74
+ on: { approved: "Implement" },
75
+ },
61
76
  Implement: {
62
77
  initial: "Write",
63
78
  states: {
@@ -65,7 +80,12 @@ test("supports a cross-scope edge directly out of a composite", async () => {
65
80
  Test: { type: "script", file: "test.ts", on: { passed: "Review" } },
66
81
  },
67
82
  },
68
- Review: { type: "agent", prompt: "review", on: { done: EXIT } },
83
+ Review: {
84
+ type: "agent",
85
+ agent: { model: "test-model" },
86
+ prompt: "review",
87
+ on: { done: EXIT },
88
+ },
69
89
  },
70
90
  };
71
91
 
@@ -93,7 +113,12 @@ test("supports re-entry into a leaf inside a different composite", async () => {
93
113
  Test: { type: "script", file: "test.ts", on: { passed: "Review", failed: "Write" } },
94
114
  },
95
115
  },
96
- Review: { type: "agent", prompt: "review", on: { done: EXIT, changes_requested: "Write" } },
116
+ Review: {
117
+ type: "agent",
118
+ agent: { model: "test-model" },
119
+ prompt: "review",
120
+ on: { done: EXIT, changes_requested: "Write" },
121
+ },
97
122
  },
98
123
  };
99
124
 
@@ -580,12 +605,13 @@ test("crashes with validation-failed after exhausting retries", async () => {
580
605
  }
581
606
  });
582
607
 
583
- test("threads agentSessionId and previousFailure into DispatchInfo across retries", async () => {
608
+ test("threads agentSessionId and previousFailure into DispatchInfo across nudges and retries", async () => {
584
609
  const workflow: WorkflowDef = {
585
610
  initial: "Agentic",
586
611
  states: {
587
612
  Agentic: {
588
613
  type: "agent",
614
+ agent: { model: "test-model" },
589
615
  prompt: "do it",
590
616
  retries: 1,
591
617
  on: { ok: EXIT },
@@ -611,7 +637,20 @@ test("threads agentSessionId and previousFailure into DispatchInfo across retrie
611
637
  agentSessionId: undefined,
612
638
  previousFailure: undefined,
613
639
  });
614
- expect(infos[1]).toMatchObject({
640
+ // A validate rejection with a session nudges in-session (up to three,
641
+ // docs/specs/output-validation): the attempt stays 1 while previousFailure
642
+ // and the carried session id thread through every nudge dispatch.
643
+ expect(infos).toHaveLength(5); // fresh + 3 nudges + the capped fallback
644
+ for (const info of infos.slice(1, 4)) {
645
+ expect(info).toMatchObject({
646
+ attempt: 1,
647
+ agentSessionId: "sess-123",
648
+ previousFailure: { kind: "validation-failed", message: "expected event 'ok'" },
649
+ });
650
+ }
651
+ // Only after the three-nudge cap does the fresh attempt run, still
652
+ // threaded with the failure and the session id.
653
+ expect(infos[4]).toMatchObject({
615
654
  attempt: 2,
616
655
  agentSessionId: "sess-123",
617
656
  previousFailure: { kind: "validation-failed", message: "expected event 'ok'" },
@@ -624,6 +663,7 @@ test("threads step and rootDir into DispatchInfo, step constant across retries o
624
663
  states: {
625
664
  Plan: {
626
665
  type: "agent",
666
+ agent: { model: "test-model" },
627
667
  prompt: "plan",
628
668
  retries: 1,
629
669
  on: { approved: "Build" },
@@ -780,7 +820,9 @@ test("script nodes receive DispatchInfo (rootDir) as their second argument", asy
780
820
  test("an agent runner's onProgress is folded into the running heartbeat as agentStepCount/agentIdleMs", async () => {
781
821
  const workflow: WorkflowDef = {
782
822
  initial: "Agentic",
783
- states: { Agentic: { type: "agent", prompt: "go", next: EXIT } },
823
+ states: {
824
+ Agentic: { type: "agent", agent: { model: "test-model" }, prompt: "go", next: EXIT },
825
+ },
784
826
  };
785
827
  const agentRunner: AgentRunner = {
786
828
  run: async (_id, _def, _handoff, _info, onProgress) => {
@@ -836,12 +878,14 @@ test("open dispatches heartbeat the journal (live runtime)", async () => {
836
878
  rmSync(rootDir, { recursive: true, force: true });
837
879
  });
838
880
 
839
- // --- session-nudge (docs/specs/session-nudge/spec.md) ---
881
+ // --- session-nudge (docs/specs/_built/session-nudge/spec.md) ---
840
882
 
841
883
  test("nudges a handoff-missing AgentFailure in-session, consuming no retry", async () => {
842
884
  const workflow: WorkflowDef = {
843
885
  initial: "Agentic",
844
- states: { Agentic: { type: "agent", prompt: "do it", on: { ok: EXIT } } },
886
+ states: {
887
+ Agentic: { type: "agent", agent: { model: "test-model" }, prompt: "do it", on: { ok: EXIT } },
888
+ },
845
889
  };
846
890
 
847
891
  const infos: DispatchInfo[] = [];
@@ -878,7 +922,9 @@ test("nudges a handoff-missing AgentFailure in-session, consuming no retry", asy
878
922
  test("nudges no-transition when an unroutable event comes back in a returned NodeResult", async () => {
879
923
  const workflow: WorkflowDef = {
880
924
  initial: "Agentic",
881
- states: { Agentic: { type: "agent", prompt: "do it", on: { ok: EXIT } } },
925
+ states: {
926
+ Agentic: { type: "agent", agent: { model: "test-model" }, prompt: "do it", on: { ok: EXIT } },
927
+ },
882
928
  };
883
929
 
884
930
  const infos: DispatchInfo[] = [];
@@ -904,7 +950,15 @@ test("nudges no-transition when an unroutable event comes back in a returned Nod
904
950
  test("exhausts the 3-nudge budget, then falls through and spends one def.retries slot", async () => {
905
951
  const workflow: WorkflowDef = {
906
952
  initial: "Agentic",
907
- states: { Agentic: { type: "agent", prompt: "do it", retries: 1, on: { ok: EXIT } } },
953
+ states: {
954
+ Agentic: {
955
+ type: "agent",
956
+ agent: { model: "test-model" },
957
+ prompt: "do it",
958
+ retries: 1,
959
+ on: { ok: EXIT },
960
+ },
961
+ },
908
962
  };
909
963
 
910
964
  const infos: DispatchInfo[] = [];
@@ -934,7 +988,9 @@ test("exhausts the 3-nudge budget, then falls through and spends one def.retries
934
988
  test("a nudge that fails with runner-error ends the nudge loop without spending further nudges", async () => {
935
989
  const workflow: WorkflowDef = {
936
990
  initial: "Agentic",
937
- states: { Agentic: { type: "agent", prompt: "do it", on: { ok: EXIT } } },
991
+ states: {
992
+ Agentic: { type: "agent", agent: { model: "test-model" }, prompt: "do it", on: { ok: EXIT } },
993
+ },
938
994
  };
939
995
 
940
996
  let calls = 0;
@@ -963,7 +1019,9 @@ test("a nudge that fails with runner-error ends the nudge loop without spending
963
1019
  test("nudge-start is emitted immediately with nudgeAttempt/nudgeReason, not gated behind a heartbeat", async () => {
964
1020
  const workflow: WorkflowDef = {
965
1021
  initial: "Agentic",
966
- states: { Agentic: { type: "agent", prompt: "do it", on: { ok: EXIT } } },
1022
+ states: {
1023
+ Agentic: { type: "agent", agent: { model: "test-model" }, prompt: "do it", on: { ok: EXIT } },
1024
+ },
967
1025
  };
968
1026
 
969
1027
  let calls = 0;
@@ -997,7 +1055,15 @@ test("nudge-start is emitted immediately with nudgeAttempt/nudgeReason, not gate
997
1055
  test("a shared per-attempt deadline bounds the whole nudge sequence, not a fresh timeout per nudge", async () => {
998
1056
  const workflow: WorkflowDef = {
999
1057
  initial: "Agentic",
1000
- states: { Agentic: { type: "agent", prompt: "do it", timeoutMs: 100, on: { ok: EXIT } } },
1058
+ states: {
1059
+ Agentic: {
1060
+ type: "agent",
1061
+ agent: { model: "test-model" },
1062
+ prompt: "do it",
1063
+ timeoutMs: 100,
1064
+ on: { ok: EXIT },
1065
+ },
1066
+ },
1001
1067
  };
1002
1068
 
1003
1069
  let calls = 0;
@@ -1027,6 +1093,165 @@ test("a shared per-attempt deadline bounds the whole nudge sequence, not a fresh
1027
1093
  }
1028
1094
  });
1029
1095
 
1096
+ // --- output-validation (docs/specs/output-validation/spec.md) ---
1097
+
1098
+ test("a failing outputType nudges in-session with the message in previousFailure, capped at three before the fresh retry", async () => {
1099
+ const workflow: WorkflowDef = {
1100
+ initial: "Shaped",
1101
+ states: {
1102
+ Shaped: {
1103
+ type: "agent",
1104
+ agent: { model: "test-model" },
1105
+ prompt: "shape it",
1106
+ retries: 1,
1107
+ on: { ok: EXIT },
1108
+ outputType: {
1109
+ safeParse: (value) =>
1110
+ value === "fixed"
1111
+ ? { success: true }
1112
+ : { success: false, message: "output: expected the string 'fixed'" },
1113
+ },
1114
+ },
1115
+ },
1116
+ };
1117
+
1118
+ const infos: DispatchInfo[] = [];
1119
+ const agentRunner: AgentRunner = {
1120
+ run: async (_id, _def, _handoff, info) => {
1121
+ infos.push(info);
1122
+ return infos.length <= 4
1123
+ ? { event: "retry_me", output: "bad", agentSessionId: "sess-shape" }
1124
+ : { event: "ok", output: "fixed" };
1125
+ },
1126
+ };
1127
+
1128
+ const result = await run({ workflow, agentRunner, scriptRunner: noopScript });
1129
+
1130
+ expect(result.node.phase).toBe("succeeded");
1131
+ // 1 fresh + exactly 3 nudges, all inside outer attempt 1; the 5th call is
1132
+ // the capped fallback to a fresh attempt (retries: 1 affords exactly one).
1133
+ expect(infos).toHaveLength(5);
1134
+ expect(infos.slice(0, 4).map((i) => i.attempt)).toEqual([1, 1, 1, 1]);
1135
+ expect(infos[4]).toMatchObject({ attempt: 2 });
1136
+ // The first dispatch is not a nudge. Each nudge dispatch carries the
1137
+ // outputType message in previousFailure — the text the corrective prompt
1138
+ // quotes (nudgeMessage's job, covered in kit/agents/handoff.test.ts;
1139
+ // engine/ must not import kit/ — engine-does-not-import-harness).
1140
+ expect(infos[0]).toMatchObject({ previousFailure: undefined });
1141
+ for (const info of infos.slice(1, 4)) {
1142
+ expect(info).toMatchObject({
1143
+ agentSessionId: "sess-shape",
1144
+ previousFailure: {
1145
+ kind: "validation-failed",
1146
+ message: "output: expected the string 'fixed'",
1147
+ },
1148
+ });
1149
+ }
1150
+ });
1151
+
1152
+ test("a failing validate nudges in-session with the script's message in previousFailure, capped at three before the fresh retry", async () => {
1153
+ const workflow: WorkflowDef = {
1154
+ initial: "Picky",
1155
+ states: {
1156
+ Picky: {
1157
+ type: "agent",
1158
+ agent: { model: "test-model" },
1159
+ prompt: "be good",
1160
+ retries: 1,
1161
+ on: { ok: EXIT },
1162
+ validate: (result) =>
1163
+ result.output === "good" ? undefined : "output must be the string 'good'",
1164
+ },
1165
+ },
1166
+ };
1167
+
1168
+ const infos: DispatchInfo[] = [];
1169
+ const agentRunner: AgentRunner = {
1170
+ run: async (_id, _def, _handoff, info) => {
1171
+ infos.push(info);
1172
+ return infos.length <= 4
1173
+ ? { event: "retry_me", output: "bad", agentSessionId: "sess-picky" }
1174
+ : { event: "ok", output: "good" };
1175
+ },
1176
+ };
1177
+
1178
+ const result = await run({ workflow, agentRunner, scriptRunner: noopScript });
1179
+
1180
+ expect(result.node.phase).toBe("succeeded");
1181
+ // 1 fresh + exactly 3 nudges, then the capped fresh retry — the nudge
1182
+ // count stops at three, and no retry was spent on the nudges themselves.
1183
+ expect(infos).toHaveLength(5);
1184
+ expect(infos[0]).toMatchObject({ previousFailure: undefined });
1185
+ for (const info of infos.slice(1, 4)) {
1186
+ expect(info).toMatchObject({
1187
+ agentSessionId: "sess-picky",
1188
+ previousFailure: {
1189
+ kind: "validation-failed",
1190
+ message: "output must be the string 'good'",
1191
+ },
1192
+ });
1193
+ }
1194
+ });
1195
+
1196
+ test("regression: a validate-only leaf still rejects the same inputs, now nudging before it retries", async () => {
1197
+ // The Compatibility contract, checked directly
1198
+ // (docs/specs/output-validation/spec.md): a leaf with `validate` and no
1199
+ // `outputType` — the pre-spec shape — rejects the same inputs with the
1200
+ // same message and ends in the same `validation-failed` crash shape as
1201
+ // before. Only the path there changed: with a session, up to three
1202
+ // same-session nudges run before the fresh retry spends its
1203
+ // `def.retries` slot.
1204
+ const MESSAGE = "rejected: output was not 'good'";
1205
+ const workflow: WorkflowDef = {
1206
+ initial: "Picky",
1207
+ states: {
1208
+ Picky: {
1209
+ type: "agent",
1210
+ agent: { model: "test-model" },
1211
+ prompt: "be good",
1212
+ retries: 1,
1213
+ on: { ok: EXIT },
1214
+ validate: (result) => (result.output === "good" ? undefined : MESSAGE),
1215
+ },
1216
+ },
1217
+ };
1218
+
1219
+ const infos: DispatchInfo[] = [];
1220
+ const agentRunner: AgentRunner = {
1221
+ run: async (_id, _def, _handoff, info) => {
1222
+ infos.push(info);
1223
+ // The same bad input every time — what the leaf rejected before the
1224
+ // feature, it must keep rejecting now.
1225
+ return { event: "retry_me", output: "bad", agentSessionId: "sess-regress" };
1226
+ },
1227
+ };
1228
+
1229
+ const result = await run({ workflow, agentRunner, scriptRunner: noopScript });
1230
+
1231
+ // Same rejection as before, end to end: the identical crash shape the
1232
+ // no-session test above asserts, reached only after the fresh retry
1233
+ // spent the one retries: 1 slot (the nudges spent none).
1234
+ expect(result.node.phase).toBe("crashed");
1235
+ if (result.node.phase === "crashed") {
1236
+ expect(result.node.reason).toEqual({ kind: "validation-failed", message: MESSAGE });
1237
+ expect(result.node.attempt).toBe(2);
1238
+ }
1239
+ // Nudges before it retries: fresh + 3 in-session nudges on attempt 1,
1240
+ // then the fresh attempt 2 — which nudges again within its own attempt
1241
+ // (the per-attempt budget the other nudgeable kinds share) before the
1242
+ // crash. Every dispatch after the first threads the same rejection
1243
+ // message and the carried session id.
1244
+ expect(infos).toHaveLength(8);
1245
+ expect(infos.map((i) => i.attempt)).toEqual([1, 1, 1, 1, 2, 2, 2, 2]);
1246
+ expect(infos[0]).toMatchObject({ previousFailure: undefined });
1247
+ for (const info of infos.slice(1)) {
1248
+ expect(info).toMatchObject({
1249
+ agentSessionId: "sess-regress",
1250
+ previousFailure: { kind: "validation-failed", message: MESSAGE },
1251
+ });
1252
+ }
1253
+ });
1254
+
1030
1255
  test("a workflow routing to FAILED ends with phase 'failed', never crashed/unresolved-target", async () => {
1031
1256
  const workflow: WorkflowDef = {
1032
1257
  initial: "RunFailure",
@@ -1067,7 +1292,7 @@ test("FAILED's terminal journal entry carries the failing node's result as its e
1067
1292
  }
1068
1293
  });
1069
1294
 
1070
- // --- usage-limit-backoff (docs/specs/usage-limit-backoff/spec.md) ---
1295
+ // --- usage-limit-backoff (docs/specs/_built/usage-limit-backoff/spec.md) ---
1071
1296
 
1072
1297
  const usageLimitFailure = () =>
1073
1298
  new AgentFailure("Usage limit reached for 5 hour.", {
@@ -1079,7 +1304,13 @@ test("a usage-limit failure waits the interval, then re-dispatches the SAME atte
1079
1304
  const workflow: WorkflowDef = {
1080
1305
  initial: "Agentic",
1081
1306
  states: {
1082
- Agentic: { type: "agent", prompt: "do it", usageLimitRetryMs: 100, on: { ok: EXIT } },
1307
+ Agentic: {
1308
+ type: "agent",
1309
+ agent: { model: "test-model" },
1310
+ prompt: "do it",
1311
+ usageLimitRetryMs: 100,
1312
+ on: { ok: EXIT },
1313
+ },
1083
1314
  },
1084
1315
  };
1085
1316
 
@@ -1115,7 +1346,13 @@ test("while waiting, running heartbeats carry limitBackoff (probe, nextAttemptAt
1115
1346
  const workflow: WorkflowDef = {
1116
1347
  initial: "Agentic",
1117
1348
  states: {
1118
- Agentic: { type: "agent", prompt: "do it", usageLimitRetryMs: 100, on: { ok: EXIT } },
1349
+ Agentic: {
1350
+ type: "agent",
1351
+ agent: { model: "test-model" },
1352
+ prompt: "do it",
1353
+ usageLimitRetryMs: 100,
1354
+ on: { ok: EXIT },
1355
+ },
1119
1356
  },
1120
1357
  };
1121
1358
 
@@ -1164,6 +1401,7 @@ test("exhausting usageLimitMaxWaitMs crashes with the last usage-limit reason; p
1164
1401
  states: {
1165
1402
  Agentic: {
1166
1403
  type: "agent",
1404
+ agent: { model: "test-model" },
1167
1405
  prompt: "do it",
1168
1406
  usageLimitRetryMs: 50,
1169
1407
  usageLimitMaxWaitMs: 120,
@@ -1217,6 +1455,7 @@ test("the attempt deadline is recomputed after each usage-limit wait, so a slept
1217
1455
  states: {
1218
1456
  Agentic: {
1219
1457
  type: "agent",
1458
+ agent: { model: "test-model" },
1220
1459
  prompt: "do it",
1221
1460
  timeoutMs: 80,
1222
1461
  usageLimitRetryMs: 50,
@@ -1243,3 +1482,454 @@ test("the attempt deadline is recomputed after each usage-limit wait, so a slept
1243
1482
  expect(calls).toBe(2);
1244
1483
  expect(result.node.phase).toBe("succeeded");
1245
1484
  });
1485
+
1486
+ // Bare-payload coercion (run 0a1b2ca3): an agent result without an `event`
1487
+ // field is a payload, not an envelope — the engine wraps it so the payload
1488
+ // always rides `handoff.output` and a payload-internal `output` key can
1489
+ // never hijack the channel downstream.
1490
+
1491
+ test("wraps an event-less agent result as { output: payload }", async () => {
1492
+ const workflow: WorkflowDef = {
1493
+ initial: "Work",
1494
+ states: {
1495
+ Work: { type: "agent", agent: { model: "test-model" }, prompt: "work", next: "Gate" },
1496
+ Gate: { type: "script", file: "gate.ts", next: EXIT },
1497
+ },
1498
+ };
1499
+ let seen: NodeResult | undefined;
1500
+ const scriptRunner: ScriptRunner = {
1501
+ run: async (_id, _def, handoff) => {
1502
+ seen = handoff;
1503
+ return { event: undefined };
1504
+ },
1505
+ };
1506
+ const agentRunner: AgentRunner = {
1507
+ run: async () => ({ behavior_ok: true, gaps: [] }) as unknown as NodeResult,
1508
+ };
1509
+
1510
+ const result = await run({ workflow, agentRunner, scriptRunner });
1511
+
1512
+ expect(result.node.phase).toBe("succeeded");
1513
+ expect(seen?.output).toEqual({ behavior_ok: true, gaps: [] });
1514
+ });
1515
+
1516
+ test("hybrid payload: flat claim plus nested output survives to handoff.output", async () => {
1517
+ // The exact run 0a1b2ca3 shape: the nested summary used to shadow the
1518
+ // passing verdict when the raw object rode the handoff unwrapped.
1519
+ const workflow: WorkflowDef = {
1520
+ initial: "Verify",
1521
+ states: {
1522
+ Verify: { type: "agent", agent: { model: "test-model" }, prompt: "verify", next: "Gate" },
1523
+ Gate: { type: "script", file: "gate.ts", next: EXIT },
1524
+ },
1525
+ };
1526
+ let seen: NodeResult | undefined;
1527
+ const scriptRunner: ScriptRunner = {
1528
+ run: async (_id, _def, handoff) => {
1529
+ seen = handoff;
1530
+ return { event: undefined };
1531
+ },
1532
+ };
1533
+ const agentRunner: AgentRunner = {
1534
+ run: async () =>
1535
+ ({
1536
+ behavior_ok: true,
1537
+ gaps: [],
1538
+ output: { summary: "All 7 MUST requirements verified" },
1539
+ }) as unknown as NodeResult,
1540
+ };
1541
+
1542
+ const result = await run({ workflow, agentRunner, scriptRunner });
1543
+
1544
+ expect(result.node.phase).toBe("succeeded");
1545
+ const output = seen?.output as { behavior_ok?: boolean; output?: { summary?: string } };
1546
+ expect(output.behavior_ok).toBe(true);
1547
+ expect(output.output?.summary).toBe("All 7 MUST requirements verified");
1548
+ });
1549
+
1550
+ test("envelope results (event present) are passed through untouched", async () => {
1551
+ const workflow: WorkflowDef = {
1552
+ initial: "Work",
1553
+ states: {
1554
+ Work: { type: "agent", agent: { model: "test-model" }, prompt: "work", on: { done: "Gate" } },
1555
+ Gate: { type: "script", file: "gate.ts", next: EXIT },
1556
+ },
1557
+ };
1558
+ let seen: NodeResult | undefined;
1559
+ const scriptRunner: ScriptRunner = {
1560
+ run: async (_id, _def, handoff) => {
1561
+ seen = handoff;
1562
+ return { event: undefined };
1563
+ },
1564
+ };
1565
+ const payload = { verdict: "clean" };
1566
+ const agentRunner: AgentRunner = {
1567
+ run: async () => ({ event: "done", output: payload }),
1568
+ };
1569
+
1570
+ const result = await run({ workflow, agentRunner, scriptRunner });
1571
+
1572
+ expect(result.node.phase).toBe("succeeded");
1573
+ expect(seen?.output).toEqual(payload);
1574
+ });
1575
+
1576
+ test("script results are never coerced", async () => {
1577
+ const workflow: WorkflowDef = {
1578
+ initial: "Prep",
1579
+ states: {
1580
+ Prep: { type: "script", file: "prep.ts", next: "Gate" },
1581
+ Gate: { type: "script", file: "gate.ts", next: EXIT },
1582
+ },
1583
+ };
1584
+ let seen: unknown; // asserted as a raw shape, not a typed envelope
1585
+ const scriptRunner: ScriptRunner = {
1586
+ run: async (id, _def, handoff) => {
1587
+ if (id === "Prep") return { probe: 1 } as unknown as NodeResult; // no event, no output
1588
+ seen = handoff;
1589
+ return { event: undefined };
1590
+ },
1591
+ };
1592
+
1593
+ const result = await run({ workflow, agentRunner: noopAgent, scriptRunner });
1594
+
1595
+ expect(result.node.phase).toBe("succeeded");
1596
+ expect(seen).toEqual({ probe: 1 }); // raw handoff, not wrapped
1597
+ });
1598
+
1599
+ // Records what each dispatch received, tagged with the stub runner that got it.
1600
+ function recordingRunner(
1601
+ tag: string,
1602
+ calls: { tag: string; id: string; agent: unknown; sessionId?: string }[],
1603
+ ): AgentRunner {
1604
+ return {
1605
+ run: async (id, def, _handoff, info) => {
1606
+ calls.push({ tag, id, agent: def.agent, sessionId: info.agentSessionId });
1607
+ return {};
1608
+ },
1609
+ };
1610
+ }
1611
+
1612
+ test("dispatches each state to the runner it resolves to, with that state's agent bag", async () => {
1613
+ const workflow: WorkflowDef = {
1614
+ initial: "Plan",
1615
+ states: {
1616
+ Plan: {
1617
+ type: "agent",
1618
+ prompt: "plan",
1619
+ runner: "other",
1620
+ agent: { model: "model-a" },
1621
+ next: "Build",
1622
+ },
1623
+ Build: { type: "agent", prompt: "build", agent: { model: "model-b" }, next: EXIT },
1624
+ },
1625
+ };
1626
+ const calls: Parameters<typeof recordingRunner>[1] = [];
1627
+
1628
+ const result = await run({
1629
+ workflow,
1630
+ agentRunner: recordingRunner("default", calls),
1631
+ agentRunners: { other: recordingRunner("other", calls) },
1632
+ });
1633
+
1634
+ expect(result.node.phase).toBe("succeeded");
1635
+ expect(calls).toMatchObject([
1636
+ { tag: "other", id: "Plan", agent: { model: "model-a" } },
1637
+ { tag: "default", id: "Build", agent: { model: "model-b" } },
1638
+ ]);
1639
+ });
1640
+
1641
+ test("a workflow whose agent states all name a runner needs no agentRunner", async () => {
1642
+ const workflow: WorkflowDef = {
1643
+ initial: "Plan",
1644
+ states: {
1645
+ Plan: {
1646
+ type: "agent",
1647
+ agent: { model: "test-model" },
1648
+ prompt: "plan",
1649
+ runner: "other",
1650
+ next: EXIT,
1651
+ },
1652
+ },
1653
+ };
1654
+
1655
+ const result = await run({ workflow, agentRunners: { other: noopAgent } });
1656
+
1657
+ expect(result.node.phase).toBe("succeeded");
1658
+ });
1659
+
1660
+ test.each(["typo", "toString"])("runner %p never falls back to the default", async (name) => {
1661
+ const workflow: WorkflowDef = {
1662
+ initial: "Plan",
1663
+ states: {
1664
+ Plan: {
1665
+ type: "agent",
1666
+ agent: { model: "test-model" },
1667
+ prompt: "plan",
1668
+ runner: name,
1669
+ next: EXIT,
1670
+ },
1671
+ },
1672
+ };
1673
+ const calls: Parameters<typeof recordingRunner>[1] = [];
1674
+
1675
+ const result = await run({
1676
+ workflow,
1677
+ agentRunner: recordingRunner("default", calls),
1678
+ agentRunners: {},
1679
+ });
1680
+
1681
+ expect(result.node).toMatchObject({
1682
+ phase: "crashed",
1683
+ reason: {
1684
+ kind: "invalid-workflow",
1685
+ errors: [{ kind: "unknown-runner", state: "Plan", runner: name, known: [] }],
1686
+ },
1687
+ });
1688
+ expect(calls).toEqual([]);
1689
+ });
1690
+
1691
+ test("an unknown runner on a later state crashes before the first dispatch", async () => {
1692
+ const workflow: WorkflowDef = {
1693
+ initial: "Plan",
1694
+ states: {
1695
+ Plan: { type: "agent", agent: { model: "test-model" }, prompt: "p", next: "Build" },
1696
+ Build: {
1697
+ type: "agent",
1698
+ agent: { model: "test-model" },
1699
+ prompt: "p",
1700
+ runner: "typo",
1701
+ next: EXIT,
1702
+ },
1703
+ },
1704
+ };
1705
+ const calls: Parameters<typeof recordingRunner>[1] = [];
1706
+ const runner = recordingRunner("default", calls);
1707
+
1708
+ const result = await run({ workflow, agentRunner: runner, agentRunners: { other: runner } });
1709
+
1710
+ expect(result.node).toMatchObject({
1711
+ phase: "crashed",
1712
+ reason: {
1713
+ kind: "invalid-workflow",
1714
+ errors: [{ kind: "unknown-runner", state: "Build", runner: "typo", known: ["other"] }],
1715
+ },
1716
+ });
1717
+ expect(calls).toEqual([]);
1718
+ });
1719
+
1720
+ test("without a default runner, a state naming none crashes before any dispatch", async () => {
1721
+ const workflow: WorkflowDef = {
1722
+ initial: "Plan",
1723
+ states: {
1724
+ Plan: {
1725
+ type: "agent",
1726
+ agent: { model: "test-model" },
1727
+ prompt: "p",
1728
+ runner: "other",
1729
+ retries: 2,
1730
+ next: "Build",
1731
+ },
1732
+ Build: { type: "agent", agent: { model: "test-model" }, prompt: "p", retries: 2, next: EXIT },
1733
+ },
1734
+ };
1735
+ const calls: Parameters<typeof recordingRunner>[1] = [];
1736
+
1737
+ const result = await run({
1738
+ workflow,
1739
+ agentRunners: { other: recordingRunner("other", calls) },
1740
+ });
1741
+
1742
+ expect(result.node).toMatchObject({
1743
+ phase: "crashed",
1744
+ attempt: 0,
1745
+ reason: { kind: "invalid-workflow", errors: [{ kind: "missing-runner", state: "Build" }] },
1746
+ });
1747
+ expect(calls).toEqual([]);
1748
+ });
1749
+
1750
+ test("merges the workflow's agent bag under default-runner states only, without modifying the workflow", async () => {
1751
+ const shared = { model: "default-model", auto: true, extra: { a: 1 } };
1752
+ const workflow: WorkflowDef = {
1753
+ initial: "Inherit",
1754
+ agent: shared,
1755
+ states: {
1756
+ Inherit: { type: "agent", prompt: "p", next: "Override" },
1757
+ Override: {
1758
+ type: "agent",
1759
+ prompt: "p",
1760
+ agent: { model: "own-model", extra: { b: 2 } },
1761
+ next: "Named",
1762
+ },
1763
+ Named: {
1764
+ type: "agent",
1765
+ prompt: "p",
1766
+ runner: "same",
1767
+ agent: { model: "named-model" },
1768
+ next: EXIT,
1769
+ },
1770
+ },
1771
+ };
1772
+ const before = structuredClone(workflow);
1773
+ const calls: Parameters<typeof recordingRunner>[1] = [];
1774
+ // The named runner is the very same object as the default one.
1775
+ const runner = recordingRunner("same", calls);
1776
+
1777
+ const result = await run({ workflow, agentRunner: runner, agentRunners: { same: runner } });
1778
+
1779
+ expect(result.node.phase).toBe("succeeded");
1780
+ expect(calls.map((c) => c.agent)).toStrictEqual([
1781
+ shared,
1782
+ { model: "own-model", auto: true, extra: { b: 2 } },
1783
+ { model: "named-model" },
1784
+ ]);
1785
+ expect(workflow).toStrictEqual(before);
1786
+ });
1787
+
1788
+ test("a runner writing to its def.agent reaches neither the workflow nor the next attempt", async () => {
1789
+ const workflow: WorkflowDef = {
1790
+ initial: "Plan",
1791
+ agent: { model: "default-model" },
1792
+ states: {
1793
+ Plan: {
1794
+ type: "agent",
1795
+ prompt: "p",
1796
+ runner: "other",
1797
+ agent: { model: "own" },
1798
+ retries: 1,
1799
+ next: EXIT,
1800
+ },
1801
+ },
1802
+ };
1803
+ const seen: unknown[] = [];
1804
+ const mutating: AgentRunner = {
1805
+ run: async (_id, def, _handoff, info) => {
1806
+ seen.push(def.agent?.model);
1807
+ if (def.agent) def.agent.model = "mutated";
1808
+ if (info.attempt === 1) throw new Error("boom");
1809
+ return {};
1810
+ },
1811
+ };
1812
+
1813
+ const result = await run({ workflow, agentRunners: { other: mutating } });
1814
+
1815
+ expect(result.node.phase).toBe("succeeded");
1816
+ expect(seen).toEqual(["own", "own"]);
1817
+ expect((workflow.states.Plan as { agent: unknown }).agent).toEqual({ model: "own" });
1818
+ });
1819
+
1820
+ test("a session id from one runner never reaches the next state's runner", async () => {
1821
+ const workflow: WorkflowDef = {
1822
+ initial: "Plan",
1823
+ states: {
1824
+ Plan: {
1825
+ type: "agent",
1826
+ agent: { model: "test-model" },
1827
+ prompt: "p",
1828
+ runner: "first",
1829
+ next: "Build",
1830
+ },
1831
+ Build: { type: "agent", agent: { model: "test-model" }, prompt: "p", next: EXIT },
1832
+ },
1833
+ };
1834
+ const calls: Parameters<typeof recordingRunner>[1] = [];
1835
+
1836
+ const result = await run({
1837
+ workflow,
1838
+ agentRunner: recordingRunner("default", calls),
1839
+ agentRunners: {
1840
+ first: { run: async () => ({ event: "planned", agentSessionId: "sess-first" }) },
1841
+ },
1842
+ });
1843
+
1844
+ expect(result.node.phase).toBe("succeeded");
1845
+ expect(result.trace[0]?.result.agentSessionId).toBe("sess-first");
1846
+ expect(calls).toMatchObject([{ id: "Build" }]);
1847
+ expect(calls[0]?.sessionId).toBeUndefined();
1848
+ });
1849
+
1850
+ test.each([undefined, "", 7])("a model of %p crashes before any dispatch", async (model) => {
1851
+ const workflow: WorkflowDef = {
1852
+ initial: "Plan",
1853
+ states: {
1854
+ Plan: { type: "agent", agent: { model: "test-model" }, prompt: "p", next: "Build" },
1855
+ Build: { type: "agent", agent: { model }, prompt: "p", retries: 2, next: EXIT },
1856
+ },
1857
+ };
1858
+ const calls: Parameters<typeof recordingRunner>[1] = [];
1859
+
1860
+ const result = await run({ workflow, agentRunner: recordingRunner("default", calls) });
1861
+
1862
+ expect(result.node).toMatchObject({
1863
+ phase: "crashed",
1864
+ attempt: 0,
1865
+ reason: { kind: "invalid-workflow", errors: [{ kind: "missing-model", state: "Build" }] },
1866
+ });
1867
+ expect(calls).toEqual([]);
1868
+ });
1869
+
1870
+ test("the default bag's model satisfies default-runner states only", async () => {
1871
+ const workflow: WorkflowDef = {
1872
+ initial: "Plan",
1873
+ agent: { model: "default-model" },
1874
+ states: {
1875
+ Plan: { type: "agent", prompt: "p", next: "Build" },
1876
+ Build: { type: "agent", prompt: "p", runner: "other", next: EXIT },
1877
+ },
1878
+ };
1879
+ const calls: Parameters<typeof recordingRunner>[1] = [];
1880
+ const runner = recordingRunner("same", calls);
1881
+
1882
+ const result = await run({ workflow, agentRunner: runner, agentRunners: { other: runner } });
1883
+
1884
+ expect(result.node).toMatchObject({
1885
+ phase: "crashed",
1886
+ reason: { kind: "invalid-workflow", errors: [{ kind: "missing-model", state: "Build" }] },
1887
+ });
1888
+ expect(calls).toEqual([]);
1889
+ });
1890
+
1891
+ test("every running status of an agent dispatch records the selected runner and its kind", async () => {
1892
+ const agent = { model: "test-model" };
1893
+ const workflow: WorkflowDef = {
1894
+ initial: "Default",
1895
+ states: {
1896
+ Default: { type: "agent", agent, prompt: "p", next: "Named" },
1897
+ Named: { type: "agent", agent, prompt: "p", runner: "claude", next: "Kindless" },
1898
+ Kindless: { type: "agent", agent, prompt: "p", runner: "custom", next: "BareDefault" },
1899
+ BareDefault: { type: "script", file: "a.ts", next: EXIT },
1900
+ },
1901
+ };
1902
+ const slow: AgentRunner["run"] = async () => {
1903
+ await new Promise((r) => setTimeout(r, 120));
1904
+ return {};
1905
+ };
1906
+ const events: NodeEvent[] = [];
1907
+
1908
+ const result = await run({
1909
+ workflow,
1910
+ agentRunner: { kind: "opencode", run: slow },
1911
+ agentRunners: { claude: { kind: "claude-code", run: slow }, custom: { run: slow } },
1912
+ scriptRunner: noopScript,
1913
+ heartbeatMs: 25,
1914
+ onEvent: (e) => events.push(e),
1915
+ });
1916
+
1917
+ expect(result.node.phase).toBe("succeeded");
1918
+ const recorded = (state: string) =>
1919
+ events.flatMap((e) =>
1920
+ e.state === state && e.status.phase === "running"
1921
+ ? [{ runner: e.status.runner, runnerKind: e.status.runnerKind }]
1922
+ : [],
1923
+ );
1924
+ const expected = {
1925
+ Default: { runner: undefined, runnerKind: "opencode" },
1926
+ Named: { runner: "claude", runnerKind: "claude-code" },
1927
+ Kindless: { runner: "custom", runnerKind: undefined },
1928
+ };
1929
+ for (const [state, record] of Object.entries(expected)) {
1930
+ const statuses = recorded(state);
1931
+ expect(statuses.length).toBeGreaterThanOrEqual(2); // the open plus heartbeats
1932
+ for (const status of statuses) expect(status).toEqual(record);
1933
+ }
1934
+ expect(recorded("BareDefault")[0]).toEqual({ runner: undefined, runnerKind: undefined });
1935
+ });