@psimona/darkf 0.5.0 → 0.7.1

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 (69) hide show
  1. package/docs/concepts/failure-modes.md +177 -0
  2. package/docs/concepts/handoff.md +91 -0
  3. package/docs/concepts/journal.md +92 -0
  4. package/docs/concepts/node-types.md +177 -0
  5. package/docs/guides/pick-an-agent-runner.md +57 -0
  6. package/docs/guides/plan-with-a-task-queue.md +411 -0
  7. package/docs/guides/render-to-mermaid.md +22 -0
  8. package/docs/guides/run-detached-and-monitor.md +76 -0
  9. package/docs/guides/test-your-workflow.md +20 -0
  10. package/docs/guides/write-your-first-workflow.md +101 -0
  11. package/docs/index.mdx +68 -0
  12. package/docs/reference/cli-commands.md +105 -0
  13. package/docs/reference/core-types.md +128 -0
  14. package/docs/reference/package-map.md +39 -0
  15. package/docs/start-here/install.md +71 -0
  16. package/docs/start-here/quickstart.md +248 -0
  17. package/package.json +15 -2
  18. package/skills/darkf-usage/SKILL.md +92 -0
  19. package/skills/darkf-writing-workflows/SKILL.md +213 -0
  20. package/src/cli/doctor.test.ts +338 -0
  21. package/src/cli/index.ts +139 -15
  22. package/src/cli/setup.test.ts +384 -0
  23. package/src/cli/setup.ts +281 -0
  24. package/src/cli/state-file.ts +14 -0
  25. package/src/cli/tarball.test.ts +127 -0
  26. package/src/cli/update.test.ts +82 -0
  27. package/src/cli/update.ts +24 -9
  28. package/src/core/tasks.ts +1 -1
  29. package/src/core/types.test.ts +52 -0
  30. package/src/core/types.ts +33 -16
  31. package/src/engine/run.test.ts +462 -3
  32. package/src/engine/run.ts +114 -8
  33. package/src/exports.ts +4 -0
  34. package/src/kit/agents/claude-code.ts +4 -34
  35. package/src/kit/agents/handoff.test.ts +12 -0
  36. package/src/kit/agents/handoff.ts +32 -8
  37. package/src/kit/agents/opencode.test.ts +232 -2
  38. package/src/kit/agents/opencode.ts +80 -31
  39. package/src/kit/specs.test.ts +247 -0
  40. package/src/kit/specs.ts +344 -0
  41. package/src/kit/status.test.ts +67 -0
  42. package/src/kit/status.ts +29 -4
  43. package/src/kit/validation/zod.test.ts +42 -0
  44. package/src/kit/validation/zod.ts +20 -0
  45. package/workflows/dev-loop/scripts/begin.ts +9 -6
  46. package/workflows/dev-loop/scripts/dispatch.ts +50 -0
  47. package/workflows/dev-loop/scripts/finish.ts +26 -21
  48. package/workflows/dev-loop/scripts/mark-done.ts +22 -0
  49. package/workflows/dev-loop/scripts/validate-plan.ts +30 -0
  50. package/workflows/dev-loop/workflow.ts +171 -64
  51. package/bunfig.toml +0 -13
  52. package/examples/article-classifier/data/articles.ts +0 -38
  53. package/examples/article-classifier/scripts/next.ts +0 -7
  54. package/examples/article-classifier/scripts/queue.ts +0 -33
  55. package/examples/article-classifier/scripts/record.ts +0 -9
  56. package/examples/article-classifier/workflow.ts +0 -44
  57. package/examples/haiku-commit/scripts/check.ts +0 -24
  58. package/examples/haiku-commit/workflow.ts +0 -28
  59. package/examples/haiku-readme/scripts/test.ts +0 -13
  60. package/examples/haiku-readme/scripts/write.ts +0 -10
  61. package/examples/haiku-readme/workflow.ts +0 -41
  62. package/examples/task-queue/scripts/next.ts +0 -20
  63. package/examples/task-queue/scripts/record.ts +0 -20
  64. package/examples/task-queue/supervise.ts +0 -57
  65. package/examples/task-queue/workflow.ts +0 -33
  66. package/feedback/2026-10-30-self-update.md +0 -29
  67. package/feedback/2026-10-31-task-list-json.md +0 -26
  68. package/tsconfig.json +0 -4
  69. /package/workflows/dev-loop/scripts/{check.ts → test.ts} +0 -0
@@ -0,0 +1,411 @@
1
+ ---
2
+ title: Plan with a task queue
3
+ description: A durable Next/Work/Record loop, watched by hand, then by a supervisor.
4
+ ---
5
+
6
+ The quickstart's `haiku-commit` is two states that run once each. Most real
7
+ work isn't that — it's a list of things to do, worked one at a time until
8
+ the list is empty. This guide builds that shape as its own example,
9
+ `task-queue`, then watches a real run of it two ways: by hand with the CLI,
10
+ and by a small script that reacts to what the CLI only warns about.
11
+
12
+ If you haven't yet, read [Write your first workflow](../write-your-first-workflow/)
13
+ first — this guide assumes you already know what `on`/`next`, `retries`,
14
+ and a script's `(handoff, info)` signature mean.
15
+
16
+ ## The pattern
17
+
18
+ A queue needs three states that keep looping until there's nothing left:
19
+
20
+ - **Next** — script. Picks the next pending item. Nothing left → exit.
21
+ Found one → hand it to `Work`.
22
+ - **Work** — agent. Does the item.
23
+ - **Record** — script. Marks the item done, then loops back to `Next`.
24
+
25
+ the `article-classifier` example in `darkf/examples/` already has this exact shape,
26
+ but tracks its queue in plain module-scope variables — fine for one
27
+ blocking `darkf run`, since the whole run lives in one process. The moment
28
+ you want to `darkf start` a queue and check on it later from a *different*
29
+ process (which is the entire point of the next section), in-memory state
30
+ doesn't survive — the queue has to be a file. `task-queue` is that version:
31
+ the queue lives in `tasks.json`, in the target project's own directory, not
32
+ in memory.
33
+
34
+ ## The example
35
+
36
+ ```ts
37
+ // workflow.ts
38
+ import { EXIT, type WorkflowDef } from "@psimona/darkf/src/core/index.ts";
39
+ import { claudeCodeRunner } from "../../src/kit/agents/claude-code.ts";
40
+
41
+ export const agentRunner = claudeCodeRunner;
42
+
43
+ export const workflow: WorkflowDef = {
44
+ initial: "Next",
45
+ states: {
46
+ Next: {
47
+ type: "script",
48
+ file: "next.ts",
49
+ on: { found: "Work", empty: EXIT },
50
+ },
51
+ Work: {
52
+ type: "agent",
53
+ prompt:
54
+ 'Do the task named in the handoff\'s "title" field: append one ' +
55
+ "sentence to notes.md describing what you did, creating the file " +
56
+ "if it doesn't exist yet. Don't overwrite what's already there.",
57
+ next: "Record",
58
+ },
59
+ Record: {
60
+ type: "script",
61
+ file: "record.ts",
62
+ next: "Next",
63
+ },
64
+ },
65
+ };
66
+ ```
67
+
68
+ `next.ts` and `record.ts` are the only two places that touch `tasks.json`,
69
+ and they agree on one convention: `"in-progress"` means "the item `Next`
70
+ handed out, that `Record` hasn't closed out yet" — so a crash between the
71
+ two is visible as a stuck `in-progress` row, not a silently lost item.
72
+
73
+ This project-level `tasks.json` is the workflow's own durable, committed
74
+ queue. It is not the file the OpenCode plugin renders. The plugin's runs
75
+ tab reads `.darkf/run/<runId>/tasks.json`, colocated with the run's
76
+ journal, and shows it as the run detail view's task pane once the file
77
+ exists. If you want that pane, have `Record` write a copy there too —
78
+ `info.runId` carries the run id. The plugin is experimental: bundled in
79
+ this repo, no public release.
80
+
81
+ ```ts
82
+ // scripts/next.ts
83
+ import { readFileSync, writeFileSync } from "node:fs";
84
+ import { join } from "node:path";
85
+ import type { DispatchInfo, NodeResult } from "@psimona/darkf/src/core/index.ts";
86
+
87
+ interface Task {
88
+ id: string;
89
+ title: string;
90
+ status: "pending" | "in-progress" | "done";
91
+ }
92
+
93
+ export default function next(_handoff: NodeResult, info: DispatchInfo): NodeResult {
94
+ const path = join(info.rootDir, "tasks.json");
95
+ const tasks: Task[] = JSON.parse(readFileSync(path, "utf8"));
96
+ const task = tasks.find((t) => t.status === "pending");
97
+ if (!task) return { event: "empty" };
98
+
99
+ task.status = "in-progress";
100
+ writeFileSync(path, JSON.stringify(tasks, null, 2));
101
+ return { event: "found", output: { title: task.title } };
102
+ }
103
+ ```
104
+
105
+ ```ts
106
+ // scripts/record.ts
107
+ import { readFileSync, writeFileSync } from "node:fs";
108
+ import { join } from "node:path";
109
+ import type { DispatchInfo, NodeResult } from "@psimona/darkf/src/core/index.ts";
110
+
111
+ interface Task {
112
+ id: string;
113
+ title: string;
114
+ status: "pending" | "in-progress" | "done";
115
+ }
116
+
117
+ export default function record(_handoff: NodeResult, info: DispatchInfo): NodeResult {
118
+ const path = join(info.rootDir, "tasks.json");
119
+ const tasks: Task[] = JSON.parse(readFileSync(path, "utf8"));
120
+ const task = tasks.find((t) => t.status === "in-progress");
121
+ if (!task) throw new Error("no in-progress task to record");
122
+
123
+ task.status = "done";
124
+ writeFileSync(path, JSON.stringify(tasks, null, 2));
125
+ return { event: "recorded" };
126
+ }
127
+ ```
128
+
129
+ `Work`'s own result is never consulted to decide whether the item is
130
+ done — same rule as the quickstart's `check.ts`. Only `Record`'s read of
131
+ `tasks.json` does. Nothing here bounds the loop with `maxCycles`: a
132
+ finite, file-backed queue already terminates on its own once every item is
133
+ `"done"`. A loop with no such ground truth (two agents going back and
134
+ forth, e.g. an implement/review cycle) is the case `maxCycles` exists for
135
+ instead — wrap that kind of loop in a composite, as the reference docs
136
+ on [core types](../../reference/core-types/) describe.
137
+
138
+ Render it:
139
+
140
+ ```sh
141
+ bunx darkf render darkf/examples/task-queue
142
+ ```
143
+
144
+ ```
145
+ stateDiagram-v2
146
+ [*] --> Next
147
+ Next : ▸ Next
148
+ Next --> Work : found
149
+ Next --> [*] : empty
150
+ Work : 🤖 Work
151
+ Work --> Record
152
+ Record : ▸ Record
153
+ Record --> Next
154
+ classDef darkfAgent fill:#6d8dfa
155
+ classDef darkfScript fill:#3fae6a
156
+ class Work darkfAgent
157
+ class Next,Record darkfScript
158
+ ```
159
+
160
+ ```mermaid
161
+ stateDiagram-v2
162
+ [*] --> Next
163
+ Next : ▸ Next
164
+ Next --> Work : found
165
+ Next --> [*] : empty
166
+ Work : 🤖 Work
167
+ Work --> Record
168
+ Record : ▸ Record
169
+ Record --> Next
170
+ classDef darkfAgent fill:#6d8dfa
171
+ classDef darkfScript fill:#3fae6a
172
+ class Work darkfAgent
173
+ class Next,Record darkfScript
174
+ ```
175
+
176
+ ## Run it
177
+
178
+ Same deal as the quickstart: a real agent dispatch per item, real cost.
179
+ Seed a queue with two items in a scratch git repo first:
180
+
181
+ ```sh
182
+ mkdir my-project && cd my-project
183
+ git init
184
+ cat > tasks.json <<'EOF'
185
+ [
186
+ { "id": "welcome", "title": "Write a one-sentence welcome note for new contributors", "status": "pending" },
187
+ { "id": "changelog", "title": "Write a one-sentence changelog entry for today", "status": "pending" }
188
+ ]
189
+ EOF
190
+ git add tasks.json && git commit -m "seed tasks"
191
+ ```
192
+
193
+ `darkf validate` is worth running first here — it checks the workflow
194
+ resolves and loads. A missing `tasks.json` or bad JSON in it fails fast
195
+ as a script crash rather than silently doing nothing. `darkf doctor`
196
+ checks the machine side — the target is a git repo, among other things:
197
+
198
+ ```sh
199
+ bunx darkf validate darkf/examples/task-queue
200
+ ```
201
+
202
+ ## Watch it manually
203
+
204
+ This is the part a single `darkf run` doesn't show: the point of
205
+ `darkf start` is that nothing is blocking, so you check in on your own
206
+ schedule.
207
+
208
+ ```sh
209
+ bunx darkf start darkf/examples/task-queue
210
+ ```
211
+
212
+ ```json
213
+ {"runId":"49d35b19-c9c5-436c-8dbc-8cc737420c3b","pid":712518}
214
+ ```
215
+
216
+ ```sh
217
+ bunx darkf status --run 49d35b19-c9c5-436c-8dbc-8cc737420c3b
218
+ ```
219
+
220
+ ```
221
+ run: 49d35b19-c9c5-436c-8dbc-8cc737420c3b
222
+ root: /path/to/my-project
223
+ state: running
224
+ current node: Work (running)
225
+ visits: {"Next":1,"Work":1}
226
+ elapsed: 3.7s (node Work)
227
+ runtime: 1ms
228
+ ```
229
+
230
+ Caught mid-`Work`: `Next` already ran once (found the first item), `Work`
231
+ is 3.7s into actually doing it. Tail it instead of polling by hand:
232
+
233
+ ```sh
234
+ bunx darkf monitor --run 49d35b19-c9c5-436c-8dbc-8cc737420c3b --window 90
235
+ ```
236
+
237
+ ```
238
+ started with: {}
239
+ Work succeeded event=done (dwell 20079ms)
240
+ Record running attempt=1 (dwell 0ms)
241
+ Record succeeded event=recorded (dwell 1ms)
242
+ Next running attempt=1 (dwell 0ms)
243
+ Next succeeded event=found (dwell 0ms)
244
+ Work running attempt=1 (dwell 0ms)
245
+ Work succeeded event=done (dwell 17770ms)
246
+ Record running attempt=1 (dwell 0ms)
247
+ Record succeeded event=recorded (dwell 0ms)
248
+ Next running attempt=1 (dwell 0ms)
249
+ Next succeeded event=empty (dwell 0ms)
250
+ [*] succeeded event=empty (dwell 0ms)
251
+ succeeded
252
+ ```
253
+
254
+ Two full `Next → Work → Record` laps, then `Next` finds nothing left and
255
+ the run exits. `tasks.json` and `notes.md` on disk now agree with that:
256
+
257
+ ```json
258
+ [
259
+ { "id": "welcome", "title": "Write a one-sentence welcome note for new contributors", "status": "done" },
260
+ { "id": "changelog", "title": "Write a one-sentence changelog entry for today", "status": "done" }
261
+ ]
262
+ ```
263
+
264
+ ```
265
+ Wrote a one-sentence welcome note for new contributors: "Welcome aboard — we're glad you're here, and we can't wait to see what you'll build with us!"
266
+
267
+ 2026-10-13: Added a one-sentence changelog entry summarizing today's work.
268
+ ```
269
+
270
+ ```sh
271
+ bunx darkf report --run 49d35b19-c9c5-436c-8dbc-8cc737420c3b
272
+ ```
273
+
274
+ ```
275
+ run: 49d35b19-c9c5-436c-8dbc-8cc737420c3b
276
+ message: (none)
277
+ span: 37.9s, 7 dispatches
278
+ visits: {"Next":3,"Work":2,"Record":2}
279
+
280
+ dispatches:
281
+ Next 0.0s -> found
282
+ Work 20.1s -> done
283
+ Record 0.0s -> recorded
284
+ Next 0.0s -> found
285
+ Work 17.8s -> done
286
+ Record 0.0s -> recorded
287
+ Next 0.0s -> empty
288
+ usage (2 agent dispatches): $0.22, 369441 in (334480 cache-read, 34941 cache-write), 1724 out
289
+ ```
290
+
291
+ Two items, two agent dispatches, $0.22. `status`'s `visits` counts distinct
292
+ states touched; `report`'s counts actual dispatches — that's why they
293
+ disagree (`{"Next":1,...}` mid-run vs. `{"Next":3,...}` after two full laps
294
+ plus the final empty check).
295
+
296
+ ## The supervisor pattern
297
+
298
+ Everything above is a human doing the checking. darkf's own CLI output
299
+ already assumes something else might be doing it instead —
300
+ `status`/`monitor` describe a stalled heartbeat as "stall suspected ...
301
+ supervisor decides" and never act on it themselves. That something is
302
+ called a **supervisor**: whatever started the run and is now watching it,
303
+ ready to do something darkf itself won't — restart a worker that died
304
+ without a clean exit, escalate a real crash, keep polling a slow one.
305
+
306
+ A supervisor can be as small as a loop around the same three commands you
307
+ just ran by hand:
308
+
309
+ ```ts
310
+ // examples/task-queue/supervise.ts
311
+ const WORKFLOW = new URL(".", import.meta.url).pathname;
312
+ const POLL_MS = 5_000;
313
+
314
+ async function darkf(...args: string[]): Promise<number> {
315
+ const proc = Bun.spawn(["bunx", "darkf", ...args], { stdout: "inherit", stderr: "inherit" });
316
+ return await proc.exited;
317
+ }
318
+
319
+ async function start(): Promise<string> {
320
+ const proc = Bun.spawn(["bunx", "darkf", "start", WORKFLOW], {
321
+ stdout: "pipe",
322
+ stderr: "inherit",
323
+ });
324
+ const stdout = await new Response(proc.stdout).text();
325
+ await proc.exited;
326
+ return (JSON.parse(stdout) as { runId: string }).runId;
327
+ }
328
+
329
+ let runId = await start();
330
+ console.log(`started run ${runId}`);
331
+
332
+ for (;;) {
333
+ const code = await darkf("monitor", "--run", runId, "--once");
334
+ if (code === 0) {
335
+ console.log("succeeded");
336
+ await darkf("report", "--run", runId);
337
+ break;
338
+ }
339
+ if (code === 1) {
340
+ console.log("crashed — see report before deciding whether to resume");
341
+ await darkf("report", "--run", runId);
342
+ break;
343
+ }
344
+ if (code === 2) {
345
+ console.log("orphaned — worker died without a terminal journal entry; restarting");
346
+ runId = await start();
347
+ continue;
348
+ }
349
+ // 6: still running. A real supervisor would check `darkf status --run`
350
+ // here too, for the stall warning the plain exit code doesn't carry.
351
+ await Bun.sleep(POLL_MS);
352
+ }
353
+ ```
354
+
355
+ Run for real, against a queue with one item:
356
+
357
+ ```
358
+ started run 6f0179e9-4c53-4498-8b51-4c59545d4cfd
359
+ started with: {}
360
+ still running
361
+ started with: {}
362
+ still running
363
+ started with: {}
364
+ still running
365
+ succeeded
366
+ succeeded
367
+ run: 6f0179e9-4c53-4498-8b51-4c59545d4cfd
368
+ message: (none)
369
+ span: 27.1s, 4 dispatches
370
+ visits: {"Next":2,"Work":1,"Record":1}
371
+
372
+ dispatches:
373
+ Next 0.0s -> found
374
+ Work 27.1s -> done
375
+ Record 0.0s -> recorded
376
+ Next 0.0s -> empty
377
+ usage (1 agent dispatches): $0.11, 186088 in (170902 cache-read, 15176 cache-write), 1219 out
378
+ ```
379
+
380
+ (The two `succeeded` lines aren't a bug in the capture above — one is
381
+ `darkf monitor`'s own stdout, inherited straight through; the other is this
382
+ script's own `console.log` right after. A quieter version would capture
383
+ `monitor`'s output instead of inheriting it.)
384
+
385
+ This script has to exist as *something* calling `darkf start` from outside
386
+ — but that something doesn't have to be a hand-rolled loop. It can equally
387
+ be an agent harness itself, mid-session, deciding to kick off a workflow
388
+ and check on it between other turns. darkf already distinguishes that
389
+ case: `darkf start`/`run` check `process.env` for a short table of
390
+ "invoked from inside harness X" signals (`SUPERVISOR_ENV_SIGNALS` in
391
+ @psimona/darkf/src/kit/registry.ts` — today just `CLAUDE_CODE_SESSION_ID`) and, if
392
+ one matches, record which session is supervising the run. The experimental
393
+ OpenCode plugin (bundled in this repo, no public release) reads that back to
394
+ show which session owns a run in its sidebar.
395
+ Nothing about the exit-code loop above changes because of it — the
396
+ difference is purely who's watching.
397
+
398
+ If you're packaging this as something a Claude Code agent reaches for
399
+ repeatedly rather than something a human runs once, this is the shape a
400
+ Claude Code **Skill** is for: the polling loop and its reactions become
401
+ the skill's instructions, and the agent supplies the judgment calls
402
+ (`darkf report` says the crash was a timeout — retry once? escalate?) that
403
+ this script's `if` statements hard-code instead.
404
+
405
+ ## Next
406
+
407
+ [Test your workflow](../test-your-workflow/) covers exercising this same
408
+ `Next`/`Work`/`Record` routing without spending anything, by swapping in a
409
+ stub `agentRunner` for `Work`. [Journal](../../concepts/journal/)
410
+ explains exactly what `status`/`monitor`/`report` are reading to produce
411
+ everything shown above.
@@ -0,0 +1,22 @@
1
+ ---
2
+ title: Render a workflow to mermaid
3
+ description: See a workflow's shape before — or while — it runs.
4
+ ---
5
+
6
+ ```sh
7
+ bunx darkf render my-workflow
8
+ ```
9
+
10
+ prints a mermaid `stateDiagram-v2` block straight to stdout — paste it into
11
+ any mermaid-aware viewer (GitHub renders mermaid code blocks natively) to
12
+ see the graph. This reads only `workflow.ts`; it never runs anything, so
13
+ it's safe to use on a workflow that calls a real paid agent.
14
+
15
+ Every `darkf run` also writes `.darkf/last-run.mmd` next to the workflow —
16
+ the same render, with the trace's last-visited state highlighted. That
17
+ file is what the quickstart's diagram in this site was generated from.
18
+
19
+ The mermaid definition is a render target, never a parsed input — darkf
20
+ never reads a `.mmd` file back as a workflow. The `WorkflowDef` object in
21
+ `workflow.ts` is the only source of truth; mermaid is strictly one-way,
22
+ generated from it.
@@ -0,0 +1,76 @@
1
+ ---
2
+ title: Run detached and monitor it
3
+ description: Start a long-running workflow in the background, then watch it.
4
+ ---
5
+
6
+ `darkf run` blocks until the workflow finishes — fine for a quick demo,
7
+ not for a workflow that runs longer than you want to keep a terminal open.
8
+ `darkf start` launches the same workflow as a detached worker that
9
+ survives the launching process exiting, and returns immediately.
10
+
11
+ ```sh
12
+ bunx darkf start my-workflow "optional seed message"
13
+ ```
14
+
15
+ ```json
16
+ {"runId":"7e1c9b2a-...","pid":48213}
17
+ ```
18
+
19
+ If the worker dies during its own startup (bad import, missing workflow
20
+ export), `start` catches that and reports failure instead of leaving a
21
+ stale "running" entry behind.
22
+
23
+ ### Checking in
24
+
25
+ ```sh
26
+ bunx darkf status
27
+ ```
28
+
29
+ Reads the journal directly — no connection to the worker process is
30
+ needed. Exit code doubles as the answer: `0` succeeded, `1` crashed, `2`
31
+ orphaned (worker PID is gone, journal has no terminal marker), `3`
32
+ running. Pass `--run <runId>` when more than one run is live.
33
+
34
+ While a node is waiting out a provider usage limit, the digest prints a
35
+ wait line — `warn: usage limit — probe 3/48, next attempt in 22m` — so
36
+ an overnight quota wall reads as a wait, not silence. The run is alive;
37
+ the exit code stays `3` ("running") until the limit lifts or the wait
38
+ budget runs out.
39
+
40
+ ### Watching live
41
+
42
+ ```sh
43
+ bunx darkf monitor --window 60
44
+ ```
45
+
46
+ Tails the journal and prints each transition as it's written, for up to
47
+ `--window` seconds (default 30) before returning `6` ("still running") so
48
+ a calling script can decide whether to keep waiting. `--once` checks and
49
+ returns immediately instead of polling. Exit codes match `status`'s.
50
+ During a usage-limit wait, the wait line re-prints each time the probe
51
+ count advances.
52
+
53
+ ### After it's done
54
+
55
+ ```sh
56
+ bunx darkf report
57
+ ```
58
+
59
+ Prints a timeline of every dispatch (state, duration, outcome), visit
60
+ counts, any crash reasons, and — if any agent dispatch wrote a usage
61
+ sidecar — a token/cost summary. Useful after the fact, whether the run
62
+ succeeded or crashed.
63
+
64
+ ### Before any of this
65
+
66
+ ```sh
67
+ bunx darkf validate my-workflow
68
+ ```
69
+
70
+ Checks that the workflow resolves and that `workflow.ts` loads and exports
71
+ `workflow` (and `agentRunner`, if any state needs one). For the machine
72
+ side, run `bunx darkf doctor`: the `claude` and `opencode` binaries on
73
+ `PATH`, and that you're not accidentally running from inside the darkf
74
+ checkout itself (a workflow's agent runner uses the invocation `cwd` as
75
+ its working directory — a common mistake when first wiring a workflow
76
+ against another repo).
@@ -0,0 +1,20 @@
1
+ ---
2
+ title: Test your workflow
3
+ description: Exercising a workflow's routing and retry logic without a real agent.
4
+ ---
5
+
6
+ :::note[Coming soon]
7
+ Not written yet — a placeholder for a planned advanced guide.
8
+ :::
9
+
10
+ The [Quickstart](../../start-here/quickstart/)'s `haiku-commit` example
11
+ wires a real `agentRunner` (`claudeCodeRunner`) directly into
12
+ `workflow.ts`, so every `bunx darkf run` costs something. Testing a
13
+ workflow's actual logic — does `Check` really route `missing` back to
14
+ `Write`, does the loop terminate, does `validate` reject what it should —
15
+ shouldn't require a real LLM call every time.
16
+
17
+ This guide will cover swapping in a stub `AgentRunner`/`ScriptRunner` for
18
+ tests (the same pattern the `haiku-readme` example's bundled stub shows),
19
+ and what's worth asserting on: the run's `trace`, the final `NodeStatus`,
20
+ or the journal itself.
@@ -0,0 +1,101 @@
1
+ ---
2
+ title: Write your first workflow
3
+ description: The minimum shape darkf needs to run.
4
+ ---
5
+
6
+ A workflow is a directory containing a `workflow.ts` that exports a
7
+ `WorkflowDef`, plus a `scripts/` directory holding anything your `script`
8
+ states reference.
9
+
10
+ ```ts
11
+ // darkf/my-workflow/workflow.ts
12
+ import { EXIT, type WorkflowDef } from "@psimona/darkf/src/core/index.ts";
13
+
14
+ export const workflow: WorkflowDef = {
15
+ initial: "Greet",
16
+ states: {
17
+ Greet: {
18
+ type: "script",
19
+ file: "greet.ts",
20
+ next: EXIT,
21
+ },
22
+ },
23
+ };
24
+ ```
25
+
26
+ ```ts
27
+ // darkf/my-workflow/scripts/greet.ts
28
+ import type { NodeResult } from "@psimona/darkf/src/core/index.ts";
29
+
30
+ export default function greet(handoff: NodeResult): NodeResult {
31
+ return { output: { greeting: "hello" } };
32
+ }
33
+ ```
34
+
35
+ `darkf/` (no dot) is where this lives in your project — commit it like any
36
+ other source. It's a different directory from `.darkf/` (dot-prefixed),
37
+ which darkf writes its own run artifacts into (journal, handoff files,
38
+ registry) and which you gitignore wholesale.
39
+
40
+ Render it before running anything — this catches unresolved targets and
41
+ duplicate ids without spending an agent call:
42
+
43
+ ```sh
44
+ bunx darkf render my-workflow
45
+ ```
46
+
47
+ Run it:
48
+
49
+ ```sh
50
+ bunx darkf run my-workflow
51
+ ```
52
+
53
+ A workflow module can also export `input` (the seed `NodeResult` for the
54
+ first dispatch), `agentRunner` (required if any state is `type: "agent"`),
55
+ and `scriptRunner` (optional — only needed if a script needs run-aware
56
+ `DispatchInfo`, not just the plain handoff).
57
+
58
+ ### Adding an agent state
59
+
60
+ An `agent` state needs a `prompt` and a configured `agentRunner`:
61
+
62
+ ```ts
63
+ Greet: {
64
+ type: "agent",
65
+ prompt: "Write a one-sentence greeting.",
66
+ on: { done: EXIT },
67
+ },
68
+ ```
69
+
70
+ ```ts
71
+ export const agentRunner: AgentRunner = claudeCodeRunner; // or opencodeRunner
72
+ ```
73
+
74
+ See [Pick an agent runner](../pick-an-agent-runner/) for the two
75
+ built-in options.
76
+
77
+ ### Branching and retrying
78
+
79
+ - `on: { eventName: "NextState" }` routes by the event a node's result
80
+ carries; `next: "NextState"` is the unconditional shorthand for a node
81
+ that always has exactly one way forward.
82
+ - `retries: 2` on a leaf bounds reattempts after a crash (default: 0, no
83
+ retry).
84
+ - `timeoutMs` caps one dispatch attempt (default: 1 hour).
85
+ - `usageLimitRetryMs` / `usageLimitMaxWaitMs` tune how a leaf waits out
86
+ a provider usage limit (defaults: re-probe every 30 minutes, give up
87
+ after 24 hours). A wait consumes no retry attempt and no `timeoutMs`
88
+ budget; see [Failure modes](../../concepts/failure-modes/).
89
+ - `outputType` checks the shape of an agent's `output` payload before
90
+ `validate` runs — build one from a Zod schema with `zodOutputType()`
91
+ (`@psimona/darkf/src/kit/validation/zod.ts`); see
92
+ [Node types](../../concepts/node-types/).
93
+ - `validate(result, handoff)` runs a plain code check against a result
94
+ before it's accepted as `succeeded`; returning a string rejects it as
95
+ `validation-failed` — an agent leaf gets up to three corrective nudges
96
+ in its live session before a fresh attempt spends a retry; see
97
+ [Failure modes](../../concepts/failure-modes/).
98
+
99
+ There is no condition-expression or YAML config layer here on purpose — see
100
+ the repo's `PHILOSOPHY.md` for why. Anything more conditional than a plain
101
+ `on` map belongs in a script's own code.
package/docs/index.mdx ADDED
@@ -0,0 +1,68 @@
1
+ ---
2
+ title: darkf
3
+ description: A typed workflow engine for deterministic automation with LLM agents. Build your dark factory on agent + script state graphs.
4
+ hero:
5
+ tagline: Deterministic automation with LLM agents. The engine for your dark factory.
6
+ actions:
7
+ - text: Quickstart
8
+ link: start-here/quickstart/
9
+ icon: right-arrow
10
+ - text: Concepts
11
+ link: concepts/node-types/
12
+ icon: right-arrow
13
+ variant: minimal
14
+ ---
15
+
16
+ import { Card, CardGrid } from '@astrojs/starlight/components';
17
+
18
+ darkf runs your automation as a workflow: a simple description of steps
19
+ and what happens next.
20
+
21
+ Some steps ask an AI agent to do the work. Some steps are plain code.
22
+ You decide which is which.
23
+
24
+ Everything the run does is written to a file as it happens. Finished,
25
+ failed, or stuck — the answer is on disk, not in a terminal you forgot
26
+ to watch. You can check yesterday's run the same way as today's.
27
+
28
+ When a step fails, the run stops right there. You see which step and
29
+ why. The engine handles the boring parts of failure: trying again,
30
+ waiting out rate limits, stopping before a loop runs away. You never
31
+ write that code.
32
+
33
+ Writing a workflow needs no setup. Describe the steps in plain words,
34
+ and a skill writes the workflow for you — best practices included. Every
35
+ workflow shows as a diagram, so the whole path stays visible and you
36
+ stay in control.
37
+
38
+ Lights out when a run is clean. Lights on the moment it isn't — the
39
+ same stop-the-line principle (jidoka) a lean factory runs on, applied
40
+ to agent steps.
41
+
42
+ Start with one workflow. Grow it into your dark factory.
43
+
44
+ <CardGrid>
45
+ <Card title="Install" icon="download">
46
+ `bun install -g @psimona/darkf`. See [Install](start-here/install/).
47
+ </Card>
48
+ <Card title="Quickstart" icon="rocket">
49
+ One real agent call, a rendered mermaid graph, a finished run.
50
+ See [Quickstart](start-here/quickstart/).
51
+ </Card>
52
+ <Card title="Two node types" icon="puzzle">
53
+ Every leaf is `agent` or `script`; the engine dispatches both the same
54
+ way. See [Node types](concepts/node-types/).
55
+ </Card>
56
+ <Card title="The journal" icon="file-text">
57
+ Every step lands on disk. Replay, inspect, and audit after the fact.
58
+ See [The journal](concepts/journal/).
59
+ </Card>
60
+ <Card title="Failure modes" icon="shield">
61
+ What happens when an agent fails, times out, or hits a usage limit.
62
+ See [Failure modes](concepts/failure-modes/).
63
+ </Card>
64
+ <Card title="CLI + tooling" icon="seti:shell">
65
+ `setup`, `render`, `run`, `start`, `status`, `monitor`, `doctor`, `validate`, `report`.
66
+ See [CLI commands](reference/cli-commands/).
67
+ </Card>
68
+ </CardGrid>