@psimona/darkf 0.8.0 → 0.10.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 (43) hide show
  1. package/docs/concepts/failure-modes.md +13 -3
  2. package/docs/concepts/handoff.md +5 -1
  3. package/docs/concepts/node-types.md +7 -5
  4. package/docs/guides/pick-an-agent-runner.md +24 -3
  5. package/docs/guides/plan-with-a-task-queue.md +15 -0
  6. package/docs/guides/run-detached-and-monitor.md +2 -1
  7. package/docs/guides/write-your-first-workflow.md +19 -3
  8. package/docs/reference/cli-commands.md +19 -0
  9. package/docs/reference/core-types.md +17 -2
  10. package/docs/start-here/quickstart.md +19 -0
  11. package/package.json +1 -1
  12. package/skills/darkf-usage/SKILL.md +17 -0
  13. package/skills/darkf-writing-workflows/SKILL.md +16 -18
  14. package/src/cli/changelog.test.ts +44 -0
  15. package/src/cli/changelog.ts +26 -0
  16. package/src/cli/detached.test.ts +27 -5
  17. package/src/cli/doctor.test.ts +67 -8
  18. package/src/cli/help.test.ts +92 -0
  19. package/src/cli/help.ts +164 -0
  20. package/src/cli/index.ts +119 -3
  21. package/src/cli/tarball.test.ts +8 -2
  22. package/src/cli/update.test.ts +85 -17
  23. package/src/cli/update.ts +41 -7
  24. package/src/core/graph.test.ts +154 -13
  25. package/src/core/graph.ts +13 -3
  26. package/src/core/types.ts +21 -1
  27. package/src/engine/identity-guard.test.ts +47 -0
  28. package/src/engine/run.test.ts +268 -25
  29. package/src/engine/run.ts +4 -4
  30. package/src/kit/agents/handoff.ts +1 -1
  31. package/src/kit/agents/opencode.test.ts +89 -12
  32. package/src/kit/agents/opencode.ts +73 -8
  33. package/src/kit/registry.ts +5 -0
  34. package/src/kit/specs.test.ts +2 -2
  35. package/src/kit/specs.ts +3 -6
  36. package/src/test/ok-output.ts +11 -0
  37. package/workflows/dev-loop/output-types.ts +43 -0
  38. package/workflows/dev-loop/scripts/begin.ts +2 -2
  39. package/workflows/dev-loop/scripts/dispatch.ts +3 -5
  40. package/workflows/dev-loop/scripts/finish.ts +2 -36
  41. package/workflows/dev-loop/workflow.ts +26 -14
  42. package/workflows/series-ordering/output-types.ts +9 -0
  43. package/workflows/series-ordering/workflow.ts +3 -0
@@ -1,6 +1,7 @@
1
1
  import { expect, test } from "bun:test";
2
+ import { okOutputType } from "../test/ok-output.ts";
2
3
  import { buildRegistry, validateWorkflow } from "./graph.ts";
3
- import { DONE, EXIT, FAILED, type WorkflowDef } from "./types.ts";
4
+ import { DONE, EXIT, FAILED, type OutputSchema, type WorkflowDef } from "./types.ts";
4
5
 
5
6
  function registryFor(workflow: WorkflowDef) {
6
7
  return buildRegistry(workflow);
@@ -11,7 +12,13 @@ test("reports no errors for a well-formed workflow", () => {
11
12
  initial: "A",
12
13
  states: {
13
14
  A: { type: "script", file: "a.ts", next: "B" },
14
- B: { type: "agent", agent: { model: "m" }, prompt: "b", on: { ok: DONE, bad: FAILED } },
15
+ B: {
16
+ type: "agent",
17
+ outputType: okOutputType,
18
+ agent: { model: "m" },
19
+ prompt: "b",
20
+ on: { ok: DONE, bad: FAILED },
21
+ },
15
22
  },
16
23
  };
17
24
 
@@ -177,11 +184,24 @@ const runnerWorkflow: WorkflowDef = {
177
184
  Group: {
178
185
  initial: "Named",
179
186
  states: {
180
- Named: { type: "agent", agent: { model: "m" }, prompt: "p", runner: "other", next: "Bare" },
187
+ Named: {
188
+ type: "agent",
189
+ outputType: okOutputType,
190
+ agent: { model: "m" },
191
+ prompt: "p",
192
+ runner: "other",
193
+ next: "Bare",
194
+ },
181
195
  },
182
196
  next: DONE,
183
197
  },
184
- Bare: { type: "agent", agent: { model: "m" }, prompt: "p", next: "Script" },
198
+ Bare: {
199
+ type: "agent",
200
+ outputType: okOutputType,
201
+ agent: { model: "m" },
202
+ prompt: "p",
203
+ next: "Script",
204
+ },
185
205
  Script: { type: "script", file: "a.ts", next: DONE },
186
206
  },
187
207
  };
@@ -208,7 +228,14 @@ test("needs no default when every agent state names a registered runner", () =>
208
228
  const workflow: WorkflowDef = {
209
229
  initial: "A",
210
230
  states: {
211
- A: { type: "agent", agent: { model: "m" }, prompt: "p", runner: "other", next: DONE },
231
+ A: {
232
+ type: "agent",
233
+ outputType: okOutputType,
234
+ agent: { model: "m" },
235
+ prompt: "p",
236
+ runner: "other",
237
+ next: DONE,
238
+ },
212
239
  },
213
240
  };
214
241
 
@@ -219,7 +246,14 @@ test("reports an inherited object key such as toString as unknown", () => {
219
246
  const workflow: WorkflowDef = {
220
247
  initial: "A",
221
248
  states: {
222
- A: { type: "agent", agent: { model: "m" }, prompt: "p", runner: "toString", next: DONE },
249
+ A: {
250
+ type: "agent",
251
+ outputType: okOutputType,
252
+ agent: { model: "m" },
253
+ prompt: "p",
254
+ runner: "toString",
255
+ next: DONE,
256
+ },
223
257
  },
224
258
  };
225
259
 
@@ -242,12 +276,30 @@ test("flags every agent state without a non-empty string model, ignoring scripts
242
276
  states: {
243
277
  Group: {
244
278
  initial: "Bare",
245
- states: { Bare: { type: "agent", prompt: "p", next: "Empty" } },
279
+ states: { Bare: { type: "agent", outputType: okOutputType, prompt: "p", next: "Empty" } },
246
280
  next: DONE,
247
281
  },
248
- Empty: { type: "agent", prompt: "p", agent: { model: "" }, next: "NotString" },
249
- NotString: { type: "agent", prompt: "p", agent: { model: 7 }, next: "Pinned" },
250
- Pinned: { type: "agent", prompt: "p", agent: { model: "m" }, next: "Script" },
282
+ Empty: {
283
+ type: "agent",
284
+ outputType: okOutputType,
285
+ prompt: "p",
286
+ agent: { model: "" },
287
+ next: "NotString",
288
+ },
289
+ NotString: {
290
+ type: "agent",
291
+ outputType: okOutputType,
292
+ prompt: "p",
293
+ agent: { model: 7 },
294
+ next: "Pinned",
295
+ },
296
+ Pinned: {
297
+ type: "agent",
298
+ outputType: okOutputType,
299
+ prompt: "p",
300
+ agent: { model: "m" },
301
+ next: "Script",
302
+ },
251
303
  Script: { type: "script", file: "a.ts", next: DONE },
252
304
  },
253
305
  };
@@ -264,9 +316,21 @@ test("takes the model from the default bag for default-runner states only", () =
264
316
  initial: "Inherit",
265
317
  agent: { model: "m" },
266
318
  states: {
267
- Inherit: { type: "agent", prompt: "p", agent: { auto: true }, next: "Cleared" },
268
- Cleared: { type: "agent", prompt: "p", agent: { model: undefined }, next: "Named" },
269
- Named: { type: "agent", prompt: "p", runner: "other", next: DONE },
319
+ Inherit: {
320
+ type: "agent",
321
+ outputType: okOutputType,
322
+ prompt: "p",
323
+ agent: { auto: true },
324
+ next: "Cleared",
325
+ },
326
+ Cleared: {
327
+ type: "agent",
328
+ outputType: okOutputType,
329
+ prompt: "p",
330
+ agent: { model: undefined },
331
+ next: "Named",
332
+ },
333
+ Named: { type: "agent", outputType: okOutputType, prompt: "p", runner: "other", next: DONE },
270
334
  },
271
335
  };
272
336
 
@@ -275,3 +339,80 @@ test("takes the model from the default bag for default-runner states only", () =
275
339
  { kind: "missing-model", state: "Named" },
276
340
  ]);
277
341
  });
342
+
343
+ // --- mandatory outputType on agent leaves (docs/specs/_built/mandatory-agent-output-types/) ---
344
+
345
+ function outputTypeErrors(workflow: WorkflowDef) {
346
+ return validateWorkflow(registryFor(workflow), workflow);
347
+ }
348
+
349
+ test("flags an agent leaf without an outputType, ignoring scripts", () => {
350
+ const workflow: WorkflowDef = {
351
+ initial: "Script",
352
+ states: {
353
+ Script: { type: "script", file: "a.ts", next: "Bare" },
354
+ Bare: { type: "agent", agent: { model: "m" }, prompt: "p", next: DONE },
355
+ },
356
+ };
357
+
358
+ expect(outputTypeErrors(workflow)).toEqual([{ kind: "missing-output-type", state: "Bare" }]);
359
+ });
360
+
361
+ test("flags an outputType that is not an object with a safeParse function", () => {
362
+ const malformed = (value: unknown) => value as OutputSchema;
363
+ const workflow: WorkflowDef = {
364
+ initial: "NotAnObject",
365
+ states: {
366
+ NotAnObject: {
367
+ type: "agent",
368
+ agent: { model: "m" },
369
+ prompt: "p",
370
+ outputType: malformed("schema"),
371
+ next: "NoSafeParse",
372
+ },
373
+ NoSafeParse: {
374
+ type: "agent",
375
+ agent: { model: "m" },
376
+ prompt: "p",
377
+ outputType: malformed({}),
378
+ next: DONE,
379
+ },
380
+ },
381
+ };
382
+
383
+ expect(outputTypeErrors(workflow)).toEqual([
384
+ { kind: "missing-output-type", state: "NotAnObject" },
385
+ { kind: "missing-output-type", state: "NoSafeParse" },
386
+ ]);
387
+ });
388
+
389
+ test("flags an agent leaf nested in a composite", () => {
390
+ const workflow: WorkflowDef = {
391
+ initial: "Outer",
392
+ states: {
393
+ Outer: {
394
+ initial: "Bare",
395
+ states: { Bare: { type: "agent", agent: { model: "m" }, prompt: "p", next: DONE } },
396
+ next: DONE,
397
+ },
398
+ },
399
+ };
400
+
401
+ expect(outputTypeErrors(workflow)).toEqual([{ kind: "missing-output-type", state: "Bare" }]);
402
+ });
403
+
404
+ test("collects every agent leaf without an outputType in one pass", () => {
405
+ const workflow: WorkflowDef = {
406
+ initial: "A",
407
+ states: {
408
+ A: { type: "agent", agent: { model: "m" }, prompt: "p", next: "B" },
409
+ B: { type: "agent", agent: { model: "m" }, prompt: "p", next: "Script" },
410
+ Script: { type: "script", file: "a.ts", next: DONE },
411
+ },
412
+ };
413
+
414
+ expect(outputTypeErrors(workflow)).toEqual([
415
+ { kind: "missing-output-type", state: "A" },
416
+ { kind: "missing-output-type", state: "B" },
417
+ ]);
418
+ });
package/src/core/graph.ts CHANGED
@@ -65,7 +65,8 @@ export type WorkflowValidationError =
65
65
  | { kind: "dead-end-leaf"; state: string }
66
66
  | { kind: "unknown-runner"; state: string; runner: string; known: string[] }
67
67
  | { kind: "missing-runner"; state: string }
68
- | { kind: "missing-model"; state: string };
68
+ | { kind: "missing-model"; state: string }
69
+ | { kind: "missing-output-type"; state: string };
69
70
 
70
71
  /** The runners a workflow has available, as names only — `core` never sees the objects. */
71
72
  export interface AvailableRunners {
@@ -93,8 +94,9 @@ export function effectiveAgent(
93
94
  * second, cheap pass over the same flat `Map` `buildRegistry` produced, not
94
95
  * a second tree-walk. Collects every problem found; never stops at the
95
96
  * first. `DONE`/`FAILED` are valid targets only for a leaf's `on`/`next`,
96
- * never for any `initial` (composite or top-level). Without `runners`, the
97
- * runner checks are skipped.
97
+ * never for any `initial` (composite or top-level). Every agent leaf must
98
+ * declare an `outputType` (docs/specs/_built/mandatory-agent-output-types/). Without
99
+ * `runners`, the runner checks are skipped.
98
100
  */
99
101
  export function validateWorkflow(
100
102
  registry: Map<string, RegistryEntry>,
@@ -134,6 +136,14 @@ export function validateWorkflow(
134
136
  if (typeof model !== "string" || model === "") {
135
137
  errors.push({ kind: "missing-model", state: id });
136
138
  }
139
+ const { outputType } = def;
140
+ if (
141
+ typeof outputType !== "object" ||
142
+ outputType === null ||
143
+ typeof outputType.safeParse !== "function"
144
+ ) {
145
+ errors.push({ kind: "missing-output-type", state: id });
146
+ }
137
147
  }
138
148
 
139
149
  if (runners && def.type === "agent") {
package/src/core/types.ts CHANGED
@@ -23,7 +23,7 @@ export interface NodeResult {
23
23
  // checking added. Non-strict on purpose: extra fields pass, the same as the
24
24
  // hand-rolled check this replaced. One deliberate behavior change: an array
25
25
  // now fails, where the hand-rolled check let it through
26
- // (docs/specs/output-validation/spec.md, "Extending `isNodeResult` to the
26
+ // (docs/specs/_built/output-validation/spec.md, "Extending `isNodeResult` to the
27
27
  // same pattern").
28
28
  const NodeResultSchema = z.object({
29
29
  event: z.string().optional(),
@@ -273,6 +273,8 @@ export type CrashReason =
273
273
  | { kind: "cycle-limit"; composite: string; cycles: number }
274
274
  | { kind: "invalid-workflow"; errors: WorkflowValidationError[] };
275
275
 
276
+ const AGENT_FAILURE = Symbol.for("darkf.AgentFailure");
277
+
276
278
  /**
277
279
  * Thrown by an `AgentRunner` in place of a plain `Error` when the harness
278
280
  * session may still be resumable despite the failure. Carries what a plain
@@ -284,6 +286,10 @@ export type CrashReason =
284
286
  export class AgentFailure extends Error {
285
287
  agentSessionId?: string;
286
288
  crashReason?: CrashReason;
289
+ /** Brand that every copy of this class shares. `instanceof` fails across
290
+ * module copies (a workflow that resolves its own install of the package),
291
+ * so the engine tests the brand through `isAgentFailure`. */
292
+ readonly [AGENT_FAILURE]: true = true;
287
293
  constructor(message: string, opts?: { agentSessionId?: string; crashReason?: CrashReason }) {
288
294
  super(message);
289
295
  this.agentSessionId = opts?.agentSessionId;
@@ -291,6 +297,20 @@ export class AgentFailure extends Error {
291
297
  }
292
298
  }
293
299
 
300
+ /**
301
+ * True for an `AgentFailure` from any module copy of this package: an error
302
+ * that carries the shared brand, or (for a runner of an earlier release, which
303
+ * has no brand) an `Error` whose constructor is named `AgentFailure`. Use this
304
+ * instead of `instanceof AgentFailure` (docs/specs/_built/runner-failure-identity).
305
+ */
306
+ export function isAgentFailure(err: unknown): err is AgentFailure {
307
+ if (!(err instanceof Error)) return false;
308
+ return (
309
+ (err as unknown as Record<symbol, unknown>)[AGENT_FAILURE] === true ||
310
+ err.constructor.name === "AgentFailure"
311
+ );
312
+ }
313
+
294
314
  export interface StepTrace {
295
315
  state: string;
296
316
  scopePath: string[];
@@ -0,0 +1,47 @@
1
+ import { expect, test } from "bun:test";
2
+ import { readdirSync, readFileSync } from "node:fs";
3
+ import { join } from "node:path";
4
+
5
+ // docs/specs/_built/runner-failure-identity/spec.md, REQ-6: a class that the package
6
+ // exports is not the same class in a second module copy, so the engine must
7
+ // not test it with `instanceof`. Classes private to one engine module are fine.
8
+ const engineDir = import.meta.dir;
9
+ const coreDir = join(engineDir, "..", "core");
10
+
11
+ // Exported classes that the engine throws and catches itself, never across a
12
+ // workflow or runner boundary, so they always share the engine's copy.
13
+ const sameCopyOnly: Record<string, string> = {
14
+ DuplicateStateIdError: "thrown by buildRegistry, which run.ts imports itself",
15
+ };
16
+
17
+ function sourceFiles(dir: string): string[] {
18
+ return readdirSync(dir)
19
+ .filter((n) => n.endsWith(".ts") && !n.endsWith(".test.ts"))
20
+ .map((n) => join(dir, n));
21
+ }
22
+
23
+ function exportedClasses(): string[] {
24
+ const names = new Set<string>();
25
+ for (const file of sourceFiles(coreDir)) {
26
+ for (const m of readFileSync(file, "utf8").matchAll(/^export class (\w+)/gm)) names.add(m[1]!);
27
+ }
28
+ return [...names];
29
+ }
30
+
31
+ test("the engine never tests an exported core class with instanceof", () => {
32
+ const classes = exportedClasses().filter((c) => !(c in sameCopyOnly));
33
+ expect(classes).toContain("AgentFailure"); // the scan finds the class it exists for
34
+ const offenders: string[] = [];
35
+ for (const file of sourceFiles(engineDir)) {
36
+ readFileSync(file, "utf8")
37
+ .split("\n")
38
+ .forEach((line, i) => {
39
+ for (const cls of classes) {
40
+ if (new RegExp(`instanceof\\s+${cls}\\b`).test(line)) {
41
+ offenders.push(`${file.slice(engineDir.length + 1)}:${i + 1} ${cls}`);
42
+ }
43
+ }
44
+ });
45
+ }
46
+ expect(offenders).toEqual([]);
47
+ });