@psimona/darkf 0.8.0 → 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.
@@ -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 () => ({}) };
@@ -34,6 +35,7 @@ test("runs a flat workflow to completion via on/next", async () => {
34
35
  states: {
35
36
  Plan: {
36
37
  type: "agent",
38
+ outputType: okOutputType,
37
39
  agent: { model: "test-model" },
38
40
  prompt: "plan",
39
41
  on: { approved: "Build" },
@@ -41,6 +43,7 @@ test("runs a flat workflow to completion via on/next", async () => {
41
43
  Build: { type: "script", file: "build.ts", next: "Review" },
42
44
  Review: {
43
45
  type: "agent",
46
+ outputType: okOutputType,
44
47
  agent: { model: "test-model" },
45
48
  prompt: "review",
46
49
  on: { done: EXIT, changes_requested: "Build" },
@@ -69,6 +72,7 @@ test("supports a cross-scope edge directly out of a composite", async () => {
69
72
  states: {
70
73
  Plan: {
71
74
  type: "agent",
75
+ outputType: okOutputType,
72
76
  agent: { model: "test-model" },
73
77
  prompt: "plan",
74
78
  on: { approved: "Implement" },
@@ -82,6 +86,7 @@ test("supports a cross-scope edge directly out of a composite", async () => {
82
86
  },
83
87
  Review: {
84
88
  type: "agent",
89
+ outputType: okOutputType,
85
90
  agent: { model: "test-model" },
86
91
  prompt: "review",
87
92
  on: { done: EXIT },
@@ -115,6 +120,7 @@ test("supports re-entry into a leaf inside a different composite", async () => {
115
120
  },
116
121
  Review: {
117
122
  type: "agent",
123
+ outputType: okOutputType,
118
124
  agent: { model: "test-model" },
119
125
  prompt: "review",
120
126
  on: { done: EXIT, changes_requested: "Write" },
@@ -232,6 +238,31 @@ test("crashes eagerly with invalid-workflow for a composite initial-cycle, inste
232
238
  }
233
239
  });
234
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
+
235
266
  test("crashes with a duplicate-id reason instead of throwing", async () => {
236
267
  const workflow: WorkflowDef = {
237
268
  initial: "A",
@@ -611,6 +642,7 @@ test("threads agentSessionId and previousFailure into DispatchInfo across nudges
611
642
  states: {
612
643
  Agentic: {
613
644
  type: "agent",
645
+ outputType: okOutputType,
614
646
  agent: { model: "test-model" },
615
647
  prompt: "do it",
616
648
  retries: 1,
@@ -663,6 +695,7 @@ test("threads step and rootDir into DispatchInfo, step constant across retries o
663
695
  states: {
664
696
  Plan: {
665
697
  type: "agent",
698
+ outputType: okOutputType,
666
699
  agent: { model: "test-model" },
667
700
  prompt: "plan",
668
701
  retries: 1,
@@ -821,7 +854,13 @@ test("an agent runner's onProgress is folded into the running heartbeat as agent
821
854
  const workflow: WorkflowDef = {
822
855
  initial: "Agentic",
823
856
  states: {
824
- Agentic: { type: "agent", agent: { model: "test-model" }, prompt: "go", next: EXIT },
857
+ Agentic: {
858
+ type: "agent",
859
+ outputType: okOutputType,
860
+ agent: { model: "test-model" },
861
+ prompt: "go",
862
+ next: EXIT,
863
+ },
825
864
  },
826
865
  };
827
866
  const agentRunner: AgentRunner = {
@@ -884,7 +923,13 @@ test("nudges a handoff-missing AgentFailure in-session, consuming no retry", asy
884
923
  const workflow: WorkflowDef = {
885
924
  initial: "Agentic",
886
925
  states: {
887
- Agentic: { type: "agent", agent: { model: "test-model" }, prompt: "do it", on: { ok: EXIT } },
926
+ Agentic: {
927
+ type: "agent",
928
+ outputType: okOutputType,
929
+ agent: { model: "test-model" },
930
+ prompt: "do it",
931
+ on: { ok: EXIT },
932
+ },
888
933
  },
889
934
  };
890
935
 
@@ -923,7 +968,13 @@ test("nudges no-transition when an unroutable event comes back in a returned Nod
923
968
  const workflow: WorkflowDef = {
924
969
  initial: "Agentic",
925
970
  states: {
926
- Agentic: { type: "agent", agent: { model: "test-model" }, prompt: "do it", on: { ok: EXIT } },
971
+ Agentic: {
972
+ type: "agent",
973
+ outputType: okOutputType,
974
+ agent: { model: "test-model" },
975
+ prompt: "do it",
976
+ on: { ok: EXIT },
977
+ },
927
978
  },
928
979
  };
929
980
 
@@ -953,6 +1004,7 @@ test("exhausts the 3-nudge budget, then falls through and spends one def.retries
953
1004
  states: {
954
1005
  Agentic: {
955
1006
  type: "agent",
1007
+ outputType: okOutputType,
956
1008
  agent: { model: "test-model" },
957
1009
  prompt: "do it",
958
1010
  retries: 1,
@@ -989,7 +1041,13 @@ test("a nudge that fails with runner-error ends the nudge loop without spending
989
1041
  const workflow: WorkflowDef = {
990
1042
  initial: "Agentic",
991
1043
  states: {
992
- Agentic: { type: "agent", agent: { model: "test-model" }, prompt: "do it", on: { ok: EXIT } },
1044
+ Agentic: {
1045
+ type: "agent",
1046
+ outputType: okOutputType,
1047
+ agent: { model: "test-model" },
1048
+ prompt: "do it",
1049
+ on: { ok: EXIT },
1050
+ },
993
1051
  },
994
1052
  };
995
1053
 
@@ -1020,7 +1078,13 @@ test("nudge-start is emitted immediately with nudgeAttempt/nudgeReason, not gate
1020
1078
  const workflow: WorkflowDef = {
1021
1079
  initial: "Agentic",
1022
1080
  states: {
1023
- Agentic: { type: "agent", agent: { model: "test-model" }, prompt: "do it", on: { ok: EXIT } },
1081
+ Agentic: {
1082
+ type: "agent",
1083
+ outputType: okOutputType,
1084
+ agent: { model: "test-model" },
1085
+ prompt: "do it",
1086
+ on: { ok: EXIT },
1087
+ },
1024
1088
  },
1025
1089
  };
1026
1090
 
@@ -1058,6 +1122,7 @@ test("a shared per-attempt deadline bounds the whole nudge sequence, not a fresh
1058
1122
  states: {
1059
1123
  Agentic: {
1060
1124
  type: "agent",
1125
+ outputType: okOutputType,
1061
1126
  agent: { model: "test-model" },
1062
1127
  prompt: "do it",
1063
1128
  timeoutMs: 100,
@@ -1155,6 +1220,7 @@ test("a failing validate nudges in-session with the script's message in previous
1155
1220
  states: {
1156
1221
  Picky: {
1157
1222
  type: "agent",
1223
+ outputType: okOutputType,
1158
1224
  agent: { model: "test-model" },
1159
1225
  prompt: "be good",
1160
1226
  retries: 1,
@@ -1207,6 +1273,7 @@ test("regression: a validate-only leaf still rejects the same inputs, now nudgin
1207
1273
  states: {
1208
1274
  Picky: {
1209
1275
  type: "agent",
1276
+ outputType: okOutputType,
1210
1277
  agent: { model: "test-model" },
1211
1278
  prompt: "be good",
1212
1279
  retries: 1,
@@ -1306,6 +1373,7 @@ test("a usage-limit failure waits the interval, then re-dispatches the SAME atte
1306
1373
  states: {
1307
1374
  Agentic: {
1308
1375
  type: "agent",
1376
+ outputType: okOutputType,
1309
1377
  agent: { model: "test-model" },
1310
1378
  prompt: "do it",
1311
1379
  usageLimitRetryMs: 100,
@@ -1348,6 +1416,7 @@ test("while waiting, running heartbeats carry limitBackoff (probe, nextAttemptAt
1348
1416
  states: {
1349
1417
  Agentic: {
1350
1418
  type: "agent",
1419
+ outputType: okOutputType,
1351
1420
  agent: { model: "test-model" },
1352
1421
  prompt: "do it",
1353
1422
  usageLimitRetryMs: 100,
@@ -1401,6 +1470,7 @@ test("exhausting usageLimitMaxWaitMs crashes with the last usage-limit reason; p
1401
1470
  states: {
1402
1471
  Agentic: {
1403
1472
  type: "agent",
1473
+ outputType: okOutputType,
1404
1474
  agent: { model: "test-model" },
1405
1475
  prompt: "do it",
1406
1476
  usageLimitRetryMs: 50,
@@ -1455,6 +1525,7 @@ test("the attempt deadline is recomputed after each usage-limit wait, so a slept
1455
1525
  states: {
1456
1526
  Agentic: {
1457
1527
  type: "agent",
1528
+ outputType: okOutputType,
1458
1529
  agent: { model: "test-model" },
1459
1530
  prompt: "do it",
1460
1531
  timeoutMs: 80,
@@ -1492,7 +1563,13 @@ test("wraps an event-less agent result as { output: payload }", async () => {
1492
1563
  const workflow: WorkflowDef = {
1493
1564
  initial: "Work",
1494
1565
  states: {
1495
- Work: { type: "agent", agent: { model: "test-model" }, prompt: "work", next: "Gate" },
1566
+ Work: {
1567
+ type: "agent",
1568
+ outputType: okOutputType,
1569
+ agent: { model: "test-model" },
1570
+ prompt: "work",
1571
+ next: "Gate",
1572
+ },
1496
1573
  Gate: { type: "script", file: "gate.ts", next: EXIT },
1497
1574
  },
1498
1575
  };
@@ -1519,7 +1596,13 @@ test("hybrid payload: flat claim plus nested output survives to handoff.output",
1519
1596
  const workflow: WorkflowDef = {
1520
1597
  initial: "Verify",
1521
1598
  states: {
1522
- Verify: { type: "agent", agent: { model: "test-model" }, prompt: "verify", next: "Gate" },
1599
+ Verify: {
1600
+ type: "agent",
1601
+ outputType: okOutputType,
1602
+ agent: { model: "test-model" },
1603
+ prompt: "verify",
1604
+ next: "Gate",
1605
+ },
1523
1606
  Gate: { type: "script", file: "gate.ts", next: EXIT },
1524
1607
  },
1525
1608
  };
@@ -1551,7 +1634,13 @@ test("envelope results (event present) are passed through untouched", async () =
1551
1634
  const workflow: WorkflowDef = {
1552
1635
  initial: "Work",
1553
1636
  states: {
1554
- Work: { type: "agent", agent: { model: "test-model" }, 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
+ },
1555
1644
  Gate: { type: "script", file: "gate.ts", next: EXIT },
1556
1645
  },
1557
1646
  };
@@ -1615,12 +1704,19 @@ test("dispatches each state to the runner it resolves to, with that state's agen
1615
1704
  states: {
1616
1705
  Plan: {
1617
1706
  type: "agent",
1707
+ outputType: okOutputType,
1618
1708
  prompt: "plan",
1619
1709
  runner: "other",
1620
1710
  agent: { model: "model-a" },
1621
1711
  next: "Build",
1622
1712
  },
1623
- Build: { type: "agent", prompt: "build", agent: { model: "model-b" }, next: EXIT },
1713
+ Build: {
1714
+ type: "agent",
1715
+ outputType: okOutputType,
1716
+ prompt: "build",
1717
+ agent: { model: "model-b" },
1718
+ next: EXIT,
1719
+ },
1624
1720
  },
1625
1721
  };
1626
1722
  const calls: Parameters<typeof recordingRunner>[1] = [];
@@ -1644,6 +1740,7 @@ test("a workflow whose agent states all name a runner needs no agentRunner", asy
1644
1740
  states: {
1645
1741
  Plan: {
1646
1742
  type: "agent",
1743
+ outputType: okOutputType,
1647
1744
  agent: { model: "test-model" },
1648
1745
  prompt: "plan",
1649
1746
  runner: "other",
@@ -1663,6 +1760,7 @@ test.each(["typo", "toString"])("runner %p never falls back to the default", asy
1663
1760
  states: {
1664
1761
  Plan: {
1665
1762
  type: "agent",
1763
+ outputType: okOutputType,
1666
1764
  agent: { model: "test-model" },
1667
1765
  prompt: "plan",
1668
1766
  runner: name,
@@ -1692,9 +1790,16 @@ test("an unknown runner on a later state crashes before the first dispatch", asy
1692
1790
  const workflow: WorkflowDef = {
1693
1791
  initial: "Plan",
1694
1792
  states: {
1695
- Plan: { type: "agent", agent: { model: "test-model" }, prompt: "p", next: "Build" },
1793
+ Plan: {
1794
+ type: "agent",
1795
+ outputType: okOutputType,
1796
+ agent: { model: "test-model" },
1797
+ prompt: "p",
1798
+ next: "Build",
1799
+ },
1696
1800
  Build: {
1697
1801
  type: "agent",
1802
+ outputType: okOutputType,
1698
1803
  agent: { model: "test-model" },
1699
1804
  prompt: "p",
1700
1805
  runner: "typo",
@@ -1723,13 +1828,21 @@ test("without a default runner, a state naming none crashes before any dispatch"
1723
1828
  states: {
1724
1829
  Plan: {
1725
1830
  type: "agent",
1831
+ outputType: okOutputType,
1726
1832
  agent: { model: "test-model" },
1727
1833
  prompt: "p",
1728
1834
  runner: "other",
1729
1835
  retries: 2,
1730
1836
  next: "Build",
1731
1837
  },
1732
- Build: { type: "agent", agent: { model: "test-model" }, prompt: "p", retries: 2, next: EXIT },
1838
+ Build: {
1839
+ type: "agent",
1840
+ outputType: okOutputType,
1841
+ agent: { model: "test-model" },
1842
+ prompt: "p",
1843
+ retries: 2,
1844
+ next: EXIT,
1845
+ },
1733
1846
  },
1734
1847
  };
1735
1848
  const calls: Parameters<typeof recordingRunner>[1] = [];
@@ -1753,15 +1866,17 @@ test("merges the workflow's agent bag under default-runner states only, without
1753
1866
  initial: "Inherit",
1754
1867
  agent: shared,
1755
1868
  states: {
1756
- Inherit: { type: "agent", prompt: "p", next: "Override" },
1869
+ Inherit: { type: "agent", outputType: okOutputType, prompt: "p", next: "Override" },
1757
1870
  Override: {
1758
1871
  type: "agent",
1872
+ outputType: okOutputType,
1759
1873
  prompt: "p",
1760
1874
  agent: { model: "own-model", extra: { b: 2 } },
1761
1875
  next: "Named",
1762
1876
  },
1763
1877
  Named: {
1764
1878
  type: "agent",
1879
+ outputType: okOutputType,
1765
1880
  prompt: "p",
1766
1881
  runner: "same",
1767
1882
  agent: { model: "named-model" },
@@ -1769,7 +1884,13 @@ test("merges the workflow's agent bag under default-runner states only, without
1769
1884
  },
1770
1885
  },
1771
1886
  };
1772
- const before = structuredClone(workflow);
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);
1773
1894
  const calls: Parameters<typeof recordingRunner>[1] = [];
1774
1895
  // The named runner is the very same object as the default one.
1775
1896
  const runner = recordingRunner("same", calls);
@@ -1782,7 +1903,7 @@ test("merges the workflow's agent bag under default-runner states only, without
1782
1903
  { model: "own-model", auto: true, extra: { b: 2 } },
1783
1904
  { model: "named-model" },
1784
1905
  ]);
1785
- expect(workflow).toStrictEqual(before);
1906
+ expect(snapshot(workflow)).toBe(before);
1786
1907
  });
1787
1908
 
1788
1909
  test("a runner writing to its def.agent reaches neither the workflow nor the next attempt", async () => {
@@ -1792,6 +1913,7 @@ test("a runner writing to its def.agent reaches neither the workflow nor the nex
1792
1913
  states: {
1793
1914
  Plan: {
1794
1915
  type: "agent",
1916
+ outputType: okOutputType,
1795
1917
  prompt: "p",
1796
1918
  runner: "other",
1797
1919
  agent: { model: "own" },
@@ -1823,12 +1945,19 @@ test("a session id from one runner never reaches the next state's runner", async
1823
1945
  states: {
1824
1946
  Plan: {
1825
1947
  type: "agent",
1948
+ outputType: okOutputType,
1826
1949
  agent: { model: "test-model" },
1827
1950
  prompt: "p",
1828
1951
  runner: "first",
1829
1952
  next: "Build",
1830
1953
  },
1831
- Build: { type: "agent", agent: { model: "test-model" }, prompt: "p", next: EXIT },
1954
+ Build: {
1955
+ type: "agent",
1956
+ outputType: okOutputType,
1957
+ agent: { model: "test-model" },
1958
+ prompt: "p",
1959
+ next: EXIT,
1960
+ },
1832
1961
  },
1833
1962
  };
1834
1963
  const calls: Parameters<typeof recordingRunner>[1] = [];
@@ -1851,8 +1980,21 @@ test.each([undefined, "", 7])("a model of %p crashes before any dispatch", async
1851
1980
  const workflow: WorkflowDef = {
1852
1981
  initial: "Plan",
1853
1982
  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 },
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
+ },
1856
1998
  },
1857
1999
  };
1858
2000
  const calls: Parameters<typeof recordingRunner>[1] = [];
@@ -1872,8 +2014,8 @@ test("the default bag's model satisfies default-runner states only", async () =>
1872
2014
  initial: "Plan",
1873
2015
  agent: { model: "default-model" },
1874
2016
  states: {
1875
- Plan: { type: "agent", prompt: "p", next: "Build" },
1876
- Build: { type: "agent", prompt: "p", runner: "other", next: EXIT },
2017
+ Plan: { type: "agent", outputType: okOutputType, prompt: "p", next: "Build" },
2018
+ Build: { type: "agent", outputType: okOutputType, prompt: "p", runner: "other", next: EXIT },
1877
2019
  },
1878
2020
  };
1879
2021
  const calls: Parameters<typeof recordingRunner>[1] = [];
@@ -1893,9 +2035,23 @@ test("every running status of an agent dispatch records the selected runner and
1893
2035
  const workflow: WorkflowDef = {
1894
2036
  initial: "Default",
1895
2037
  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" },
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
+ },
1899
2055
  BareDefault: { type: "script", file: "a.ts", next: EXIT },
1900
2056
  },
1901
2057
  };
@@ -54,7 +54,7 @@ describe("scanStepFinish", () => {
54
54
  test("ignores lines that aren't step-finish or aren't parseable JSON", () => {
55
55
  const stdout = ndjson([
56
56
  { type: "message", part: { sessionID: "sess-x" } },
57
- { type: "step-finish", part: { reason: "stop", sessionID: "sess-1" } },
57
+ { type: "step_finish", part: { reason: "stop", sessionID: "sess-1" } },
58
58
  ]);
59
59
  const scan = scanStepFinish(`not json\n${stdout}\n`);
60
60
  expect(scan.stepCount).toBe(1);
@@ -64,9 +64,9 @@ describe("scanStepFinish", () => {
64
64
 
65
65
  test("counts every step-finish event and keeps the LAST reason/sessionID seen", () => {
66
66
  const stdout = ndjson([
67
- { type: "step-finish", part: { reason: "tool-calls", sessionID: "sess-1" } },
68
- { type: "step-finish", part: { reason: "stop", sessionID: "sess-1" } },
69
- { type: "step-finish", part: { reason: "length" } }, // no sessionID on this one
67
+ { type: "step_finish", part: { reason: "tool-calls", sessionID: "sess-1" } },
68
+ { type: "step_finish", part: { reason: "stop", sessionID: "sess-1" } },
69
+ { type: "step_finish", part: { reason: "length" } }, // no sessionID on this one
70
70
  ]);
71
71
  const scan = scanStepFinish(stdout);
72
72
  expect(scan.stepCount).toBe(3);
@@ -75,11 +75,13 @@ describe("scanStepFinish", () => {
75
75
  expect(scan.sessionId).toBe("sess-1");
76
76
  });
77
77
 
78
- test("does not match the underscore spelling step_finish", () => {
79
- const stdout = ndjson([{ type: "step_finish", part: { reason: "stop", sessionID: "sess-1" } }]);
80
- const scan = scanStepFinish(stdout);
81
- expect(scan.stepCount).toBe(0);
82
- expect(scan.sessionId).toBeUndefined();
78
+ test("reads a captured opencode 2.3.0 event: outer step_finish, inner part.type step-finish", () => {
79
+ const captured =
80
+ '{"type":"step_finish","timestamp":1795852863406,"sessionID":"ses_dded41c03ffe0K2HQarugg1jY8","part":{"id":"prt_2212c07ae001VpNCu4zfPKY2pA","sessionID":"ses_dded41c03ffe0K2HQarugg1jY8","messageID":"msg_2212be8c80010pl7Mc2TCmrkp5","type":"step-finish","reason":"tool-calls","cost":0,"tokens":{"input":11196,"output":106,"reasoning":93,"cache":{"read":0,"write":0}}}}';
81
+ const scan = scanStepFinish(captured);
82
+ expect(scan.stepCount).toBe(1);
83
+ expect(scan.sessionId).toBe("ses_dded41c03ffe0K2HQarugg1jY8");
84
+ expect(scan.reason).toBe("tool-calls");
83
85
  });
84
86
 
85
87
  test("stepCount is the positive-evidence gate: zero means no session id should ever be attached", () => {
@@ -181,7 +183,7 @@ describe("matchUsageLimit", () => {
181
183
 
182
184
  test("an ordinary non-limit API error does not match", () => {
183
185
  expect(matchUsageLimit("opencode: API error 500: backend exploded\n", "")).toBeUndefined();
184
- expect(matchUsageLimit("", '{"type":"step-finish","part":{"reason":"stop"}}')).toBeUndefined();
186
+ expect(matchUsageLimit("", '{"type":"step_finish","part":{"reason":"stop"}}')).toBeUndefined();
185
187
  });
186
188
  });
187
189
 
@@ -250,7 +252,7 @@ describe("opencodeRunner usage-limit classification + resume", () => {
250
252
  const handoffFile = handoffPath(rootDir, RUN_ID, 1, "N");
251
253
  return [
252
254
  `printf '%s\\0' "$@" > "${argvFile}"`,
253
- `echo '{"type":"step-finish","part":{"reason":"stop","sessionID":"${sessionId}"}}'`,
255
+ `echo '{"type":"step_finish","part":{"reason":"stop","sessionID":"${sessionId}"}}'`,
254
256
  `mkdir -p "$(dirname "${handoffFile}")"`,
255
257
  `echo '{"event":"ok"}' > "${handoffFile}"`,
256
258
  ].join("\n");
@@ -260,7 +262,7 @@ describe("opencodeRunner usage-limit classification + resume", () => {
260
262
  const { rootDir } = install((_rootDir, _argvFile) =>
261
263
  [
262
264
  `echo 'Usage limit reached for 5 hour. Your limit will reset at 2026-11-01 06:58:19' 1>&2`,
263
- `echo '{"type":"step-finish","part":{"reason":"stop","sessionID":"sess-wall"}}'`,
265
+ `echo '{"type":"step_finish","part":{"reason":"stop","sessionID":"sess-wall"}}'`,
264
266
  `exit 3`,
265
267
  ].join("\n"),
266
268
  );
@@ -303,6 +305,81 @@ describe("opencodeRunner usage-limit classification + resume", () => {
303
305
  rmSync(rootDir, { recursive: true, force: true });
304
306
  });
305
307
 
308
+ async function failWith(script: string): Promise<AgentFailure> {
309
+ const { rootDir } = install(() => script);
310
+ let thrown: unknown;
311
+ try {
312
+ await opencodeRunner.run("N", def, {}, info(rootDir));
313
+ } catch (err) {
314
+ thrown = err;
315
+ }
316
+ rmSync(rootDir, { recursive: true, force: true });
317
+ expect(thrown).toBeInstanceOf(AgentFailure);
318
+ return thrown as AgentFailure;
319
+ }
320
+
321
+ const errorEvent = (type: string, message: string, status?: number) =>
322
+ `echo '${JSON.stringify({ type: "error", sessionID: "ses_1", error: { type, message, ...(status ? { status } : {}) } })}'\nexit 1`;
323
+
324
+ test("captured provider.auth event: message in the crash reason, runner-error", async () => {
325
+ const failure = await failWith(
326
+ `echo '{"type":"step_start"}'\n${errorEvent("provider.auth", "This API key has expired.", 401)}`,
327
+ );
328
+ expect(failure.crashReason).toEqual({
329
+ kind: "runner-error",
330
+ error: "opencode exited 1: provider.auth (401): This API key has expired.",
331
+ });
332
+ });
333
+
334
+ test("captured provider.no-route event: message in the crash reason, runner-error", async () => {
335
+ const failure = await failWith(errorEvent("provider.no-route", "Model unavailable: foo"));
336
+ expect(failure.crashReason).toEqual({
337
+ kind: "runner-error",
338
+ error: "opencode exited 1: provider.no-route: Model unavailable: foo",
339
+ });
340
+ });
341
+
342
+ for (const type of [
343
+ "provider.quota",
344
+ "provider.rate-limit",
345
+ "provider.go-limit",
346
+ "provider.chatgpt-limit",
347
+ "provider.chatgpt-workspace-limit",
348
+ "provider.budget-limit",
349
+ ]) {
350
+ test(`${type} error event classifies usage-limit`, async () => {
351
+ const failure = await failWith(
352
+ `echo '{"type":"step_finish","part":{"reason":"stop","sessionID":"sess-q"}}'\n${errorEvent(type, "Plan exhausted, wait.")}`,
353
+ );
354
+ expect(failure.crashReason).toEqual({ kind: "usage-limit", detail: "Plan exhausted, wait." });
355
+ expect(failure.agentSessionId).toBe("sess-q");
356
+ });
357
+ }
358
+
359
+ for (const type of ["provider.free-limit", "provider.model-budget-limit", "provider.mystery"]) {
360
+ test(`${type} error event does not wait`, async () => {
361
+ const failure = await failWith(errorEvent(type, "usage limit hit, switch models"));
362
+ expect(failure.crashReason?.kind).toBe("runner-error");
363
+ });
364
+ }
365
+
366
+ test("an untyped error event with a limit marker classifies usage-limit", async () => {
367
+ const failure = await failWith(
368
+ `echo '{"type":"error","error":{"message":"Usage limit reached for 5 hour."}}'\nexit 1`,
369
+ );
370
+ expect(failure.crashReason).toEqual({
371
+ kind: "usage-limit",
372
+ detail: "Usage limit reached for 5 hour.",
373
+ });
374
+ });
375
+
376
+ test("a non-error JSON event mentioning a limit never matches on a non-zero exit", async () => {
377
+ const failure = await failWith(
378
+ `echo '{"type":"text","part":{"text":"usage limit reached"}}'\nexit 1`,
379
+ );
380
+ expect(failure.crashReason?.kind).toBe("runner-error");
381
+ });
382
+
306
383
  test("an exit-0 + missing-handoff output with a limit string inside an agent JSON line stays handoff-missing", async () => {
307
384
  const { rootDir } = install((_rootDir, _argvFile) =>
308
385
  [