@psimona/darkf 0.7.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/concepts/failure-modes.md +18 -5
- package/docs/concepts/handoff.md +8 -4
- package/docs/concepts/node-types.md +45 -21
- package/docs/guides/pick-an-agent-runner.md +143 -4
- package/docs/guides/plan-with-a-task-queue.md +18 -2
- package/docs/guides/run-detached-and-monitor.md +4 -3
- package/docs/guides/write-your-first-workflow.md +24 -5
- package/docs/reference/cli-commands.md +24 -6
- package/docs/reference/core-types.md +41 -6
- package/docs/start-here/quickstart.md +22 -2
- package/package.json +1 -1
- package/skills/darkf-usage/SKILL.md +17 -0
- package/skills/darkf-writing-workflows/SKILL.md +25 -18
- package/src/cli/detached.test.ts +92 -2
- package/src/cli/doctor.test.ts +224 -9
- package/src/cli/index.ts +83 -12
- package/src/cli/tarball.test.ts +8 -2
- package/src/core/graph.test.ts +266 -20
- package/src/core/graph.ts +61 -3
- package/src/core/types.ts +21 -0
- package/src/engine/run.test.ts +581 -17
- package/src/engine/run.ts +50 -7
- package/src/kit/agents/claude-code.test.ts +38 -2
- package/src/kit/agents/claude-code.ts +24 -3
- package/src/kit/agents/opencode.test.ts +89 -12
- package/src/kit/agents/opencode.ts +103 -8
- package/src/kit/status.test.ts +24 -0
- package/src/kit/status.ts +25 -8
- package/src/test/ok-output.ts +11 -0
- package/workflows/dev-loop/output-types.ts +43 -0
- package/workflows/dev-loop/scripts/begin.ts +2 -2
- package/workflows/dev-loop/scripts/finish.ts +2 -36
- package/workflows/dev-loop/workflow.ts +18 -4
- package/workflows/series-ordering/output-types.ts +9 -0
- package/workflows/series-ordering/workflow.ts +4 -0
|
@@ -11,7 +11,8 @@ executing."
|
|
|
11
11
|
```ts
|
|
12
12
|
export type NodeStatus =
|
|
13
13
|
| { phase: "pending" }
|
|
14
|
-
| { phase: "running"; attempt: number; startedAt: number; lastHeartbeat: number;
|
|
14
|
+
| { phase: "running"; attempt: number; startedAt: number; lastHeartbeat: number; agentSessionId?: string;
|
|
15
|
+
runner?: string; runnerKind?: AgentRunnerKind;
|
|
15
16
|
limitBackoff?: { probe: number; nextAttemptAt: number; waitedMs: number } }
|
|
16
17
|
| { phase: "succeeded"; result: NodeResult }
|
|
17
18
|
| { phase: "crashed"; reason: CrashReason; attempt: number };
|
|
@@ -26,9 +27,11 @@ all of them are required:
|
|
|
26
27
|
| `running` | `attempt` | Yes | Which attempt number is in flight — `1` for the first try. |
|
|
27
28
|
| `running` | `startedAt` | Yes | Timestamp this attempt began. |
|
|
28
29
|
| `running` | `lastHeartbeat` | Yes | Timestamp of the latest heartbeat, updated every 30s. |
|
|
29
|
-
| `running` | `
|
|
30
|
+
| `running` | `agentSessionId` | No | Only present once some earlier attempt's runner returned one. |
|
|
31
|
+
| `running` | `runner` | No | The runner name an agent state selected. Absent for the default runner and for script dispatches. |
|
|
32
|
+
| `running` | `runnerKind` | No | The resolved agent runner's `kind`. Absent when it declares none, and for script dispatches. |
|
|
30
33
|
| `running` | `limitBackoff` | No | Present only while the engine is waiting out a provider usage limit — which probe this is, when the next one fires, and the total waited so far. |
|
|
31
|
-
| `crashed` | `reason` | Yes | A `CrashReason` — which of the
|
|
34
|
+
| `crashed` | `reason` | Yes | A `CrashReason` — which of the eleven shapes below. |
|
|
32
35
|
| `crashed` | `attempt` | Yes | Which attempt number crashed. |
|
|
33
36
|
|
|
34
37
|
A **crash** means the mechanism itself failed. It doesn't mean the
|
|
@@ -45,7 +48,8 @@ export type CrashReason =
|
|
|
45
48
|
| { kind: "unresolved-target"; from: string; target: string }
|
|
46
49
|
| { kind: "duplicate-id"; id: string }
|
|
47
50
|
| { kind: "max-steps"; steps: number; state: string }
|
|
48
|
-
| { kind: "cycle-limit"; composite: string; cycles: number }
|
|
51
|
+
| { kind: "cycle-limit"; composite: string; cycles: number }
|
|
52
|
+
| { kind: "invalid-workflow"; errors: WorkflowValidationError[] };
|
|
49
53
|
```
|
|
50
54
|
|
|
51
55
|
Every variant's `kind` is required — it's the discriminant. The rest
|
|
@@ -63,6 +67,7 @@ of each variant's payload is required too, with one exception:
|
|
|
63
67
|
| `duplicate-id` | `id` (required) | `buildRegistry` found two states sharing an id. |
|
|
64
68
|
| `max-steps` | `steps` (required), `state` (required) | The engine-wide `maxSteps` backstop tripped. |
|
|
65
69
|
| `cycle-limit` | `composite` (required), `cycles` (required) | A composite's own `maxCycles` tripped. |
|
|
70
|
+
| `invalid-workflow` | `errors` (required) | `validateWorkflow` rejected the workflow before the first dispatch — the run crashes on the initial state at attempt 0, and no leaf ever runs. `errors` holds one entry per problem, each naming a state — e.g. `missing-output-type` for an agent leaf whose `outputType` is absent or has no `safeParse` function. |
|
|
66
71
|
|
|
67
72
|
`validation-failed` nudges before it retries. When a result fails one
|
|
68
73
|
of these checks, the engine keeps the same session alive and sends a
|
|
@@ -162,7 +167,15 @@ alive the whole time — the supervisor can kill it at any moment, and
|
|
|
162
167
|
the journal says exactly where the wait stopped. Classification is
|
|
163
168
|
runner-side: the opencode runner recognizes limit-shaped errors
|
|
164
169
|
(`usage limit`, `rate limit`, `limit exceeded`, `429`) on a non-zero
|
|
165
|
-
exit
|
|
170
|
+
exit, and it reads the provider's `error` event that opencode prints on
|
|
171
|
+
stdout. Quota and throttle types (`provider.quota`, `provider.rate-limit`,
|
|
172
|
+
`provider.go-limit`, `provider.chatgpt-limit`,
|
|
173
|
+
`provider.chatgpt-workspace-limit`, `provider.budget-limit`) start the wait.
|
|
174
|
+
Other provider errors (`provider.auth`, `provider.no-route`,
|
|
175
|
+
`provider.free-limit`, `provider.model-budget-limit`) are not waited on: the
|
|
176
|
+
dispatch crashes as `runner-error` with the provider's message in `error`,
|
|
177
|
+
for example `opencode exited 1: provider.auth (401): This API key has
|
|
178
|
+
expired.` A script that calls an LLM itself owns its error handling.
|
|
166
179
|
|
|
167
180
|
**A thrash, not (yet) a `cycle-limit` crash.** If one state's visit
|
|
168
181
|
count is the run's highest and reaches 4 or more, `status` prints a
|
package/docs/concepts/handoff.md
CHANGED
|
@@ -44,7 +44,7 @@ export interface DispatchInfo {
|
|
|
44
44
|
validEvents: string[];
|
|
45
45
|
hasFallback: boolean;
|
|
46
46
|
attempt: number;
|
|
47
|
-
|
|
47
|
+
agentSessionId?: string;
|
|
48
48
|
previousFailure?: CrashReason;
|
|
49
49
|
step: number;
|
|
50
50
|
rootDir: string;
|
|
@@ -57,7 +57,7 @@ export interface DispatchInfo {
|
|
|
57
57
|
| `validEvents` | Yes | Derived straight from this state's own `on` keys — the events it can route on. |
|
|
58
58
|
| `hasFallback` | Yes | Whether `next` is also set, meaning the node may omit `event` entirely and still route. |
|
|
59
59
|
| `attempt` | Yes | Which attempt of this dispatch is in flight — `1` the first time. |
|
|
60
|
-
| `
|
|
60
|
+
| `agentSessionId` | No | Only present once a prior attempt's own `NodeResult` returned one. |
|
|
61
61
|
| `previousFailure` | No | Only present on a retry — the [`CrashReason`](../failure-modes/) that rejected the attempt before this one. |
|
|
62
62
|
| `step` | Yes | The engine's own step counter for this node's current visit, constant across every retry attempt of that visit. |
|
|
63
63
|
| `rootDir` | Yes | The run's root directory — the base a runner resolves its own `.darkf/run/<runId>/...` paths against. |
|
|
@@ -69,7 +69,11 @@ artifacts. No in-memory visit map of its own required.
|
|
|
69
69
|
## What's needed, what's handled
|
|
70
70
|
|
|
71
71
|
Your own code decides, every single time, what belongs in `output` for
|
|
72
|
-
the next node to see. darkf never infers this.
|
|
72
|
+
the next node to see. darkf never infers this. For an agent leaf, the
|
|
73
|
+
shape you decided on is declared up front as the state's `outputType` —
|
|
74
|
+
required, checked by `darkf validate` — so the next node can read
|
|
75
|
+
`handoff.output` as that schema's type instead of guessing; see
|
|
76
|
+
[Node types](../node-types/). A value not re-emitted
|
|
73
77
|
this hop simply isn't there next hop — even a value the node itself
|
|
74
78
|
just received.
|
|
75
79
|
|
|
@@ -82,7 +86,7 @@ one, or either into a composite's first child.
|
|
|
82
86
|
`DispatchInfo` is automatic too. `step`/`attempt`/`runId` are the
|
|
83
87
|
engine's own bookkeeping. `validEvents`/`hasFallback` are derived
|
|
84
88
|
straight from that state's own `on`/`next` — nothing to redeclare.
|
|
85
|
-
`
|
|
89
|
+
`agentSessionId`/`previousFailure` only appear once a retry has actually
|
|
86
90
|
happened.
|
|
87
91
|
|
|
88
92
|
The gap in between is deliberate. Anything durable for the whole run
|
|
@@ -3,8 +3,22 @@ title: Node types
|
|
|
3
3
|
description: Every leaf is agent or script, dispatched the same way.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
|
-
A `WorkflowDef` is a plain object. It has one `initial` state
|
|
7
|
-
`states` map.
|
|
6
|
+
A `WorkflowDef` is a plain object. It has one `initial` state, a
|
|
7
|
+
`states` map, and an optional default `agent` bag.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
export interface WorkflowDef {
|
|
11
|
+
initial: string;
|
|
12
|
+
states: Record<string, StateDef>;
|
|
13
|
+
agent?: Record<string, unknown>;
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
| Field | Required | Default | What it does |
|
|
18
|
+
|---|---|---|---|
|
|
19
|
+
| `initial` | Yes | — | The state the run starts in. |
|
|
20
|
+
| `states` | Yes | — | The top-level state map. |
|
|
21
|
+
| `agent` | No | `{}` | Default `agent` bag for agent states on the default runner. A state with no `runner` receives this bag with its own `agent` keys on top. The merge is shallow: each state key replaces the default key. A state that sets `runner` never receives it. Composites have no such field. |
|
|
8
22
|
|
|
9
23
|
Every state is one of three shapes: `agent`, `script`, or `composite`.
|
|
10
24
|
The first two are leaves — the engine actually dispatches them. The
|
|
@@ -17,7 +31,8 @@ export interface AgentStateDef {
|
|
|
17
31
|
type: "agent";
|
|
18
32
|
prompt: string;
|
|
19
33
|
agent?: Record<string, unknown>;
|
|
20
|
-
|
|
34
|
+
runner?: string;
|
|
35
|
+
outputType?: OutputSchema; // optional to the compiler; required by validateWorkflow
|
|
21
36
|
on?: Record<string, string>;
|
|
22
37
|
next?: string;
|
|
23
38
|
retries?: number;
|
|
@@ -32,17 +47,18 @@ export interface AgentStateDef {
|
|
|
32
47
|
|---|---|---|---|
|
|
33
48
|
| `type` | Yes | — | `"agent"`. The discriminant — how darkf tells this state apart from a `script` or a `composite`. |
|
|
34
49
|
| `prompt` | Yes | — | darkf builds the full prompt a runner sees by combining it with the incoming handoff and `validEvents` — computed straight from `on`'s own keys, so there's nothing to keep in sync by hand. |
|
|
35
|
-
| `agent` | No | `{}` | An untyped bag
|
|
50
|
+
| `agent` | No | `{}` | An untyped bag each runner interprets its own way: `claudeCodeRunner` reads `model`/`permissionMode`, `opencodeRunner` reads `model`/`auto`. The engine reads one key itself: the effective bag (after the `WorkflowDef.agent` merge) must hold a non-empty string `model`, whatever the runner, custom runners included. A state without one fails `darkf validate` and run start as `missing-model`. |
|
|
51
|
+
| `runner` | No | the default `agentRunner` | Key into the workflow's `agentRunners` export, selecting the harness for this state. The name is the workflow's own, not the runner's `kind`. An unknown name fails `darkf validate` and run start as `unknown-runner`; it never falls back to the default. See [Pick an agent runner](../../guides/pick-an-agent-runner/). |
|
|
36
52
|
| `on` | No | — | Routes by the reply's event — `{ approved: "Next" }` means "go to `Next` when the event is `approved`." |
|
|
37
53
|
| `next` | No | — | Unconditional alternative to `on`: always go there, regardless of what event comes back. Also sets `DispatchInfo.hasFallback`, telling the agent it may omit `event` entirely. |
|
|
38
54
|
| `retries` | No | `0` | Reattempts of this one dispatch after a crash — a malformed reply, a failed `validate`, a runner throwing. Each retry's prompt includes `DispatchInfo.previousFailure`, naming what went wrong last time. |
|
|
39
55
|
| `timeoutMs` | No | 1 hour | Ceiling on a single dispatch attempt. Exceeding it is a `timeout` crash, consuming a retry attempt like any other. |
|
|
40
56
|
| `usageLimitRetryMs` | No | 30 minutes | Interval between re-probes after a dispatch fails on the provider's usage limit. The wait runs outside `timeoutMs` — each re-probe starts with a fresh per-attempt budget — and consumes no retry attempt. |
|
|
41
57
|
| `usageLimitMaxWaitMs` | No | 24 hours | Total wait budget per node visit for usage-limit backoff. Spending it crashes the run with a `usage-limit` crash — see [Failure modes](../failure-modes/). |
|
|
42
|
-
| `outputType` |
|
|
58
|
+
| `outputType` | Yes | — | Shape check on the reply's `output`, run before `validate`. Any object with a `safeParse(value)` method returning `{ success: true }` or `{ success: false, message }` — build one from a Zod schema with `zodOutputType()` (`src/kit/validation/zod.ts`) and import only that built value, never Zod itself. Required with a validation error: `validateWorkflow` rejects an agent leaf without a usable `outputType` as `missing-output-type`, so `darkf validate` fails and a run crashes `invalid-workflow` before the first dispatch — even though the TypeScript field stays optional. Agent leaves only — an agent's `output` comes from text an LLM wrote, a script's from code you already control. A failure rejects the dispatch exactly like a failed `validate` — see [Failure modes](../failure-modes/). |
|
|
43
59
|
| `validate` | No | — | `(result, handoff) => string \| undefined`, run after the reply already parses as a well-formed `NodeResult` — and after `outputType`, when set. Return a string to reject it as `validation-failed`, or `undefined` to accept. A rejection nudges before it retries — up to three corrective prompts in the live session, none costing a retry — then falls back to a fresh attempt like any crash; see [Failure modes](../failure-modes/). |
|
|
44
60
|
|
|
45
|
-
|
|
61
|
+
A workflow author must always supply `prompt`, a `model` — in the state's own `agent` bag or, for a state with no `runner`, in `WorkflowDef.agent` — and an `outputType` declaring the shape of the handoff output. Everything else is safe to omit.
|
|
46
62
|
|
|
47
63
|
## Script
|
|
48
64
|
|
|
@@ -68,10 +84,10 @@ export interface ScriptStateDef {
|
|
|
68
84
|
| `next` | No | — | Same as the agent case: unconditional alternative to `on`. |
|
|
69
85
|
| `retries` | No | `0` | Reattempts after a crash — the function throwing, returning something malformed, or failing `validate`. Each retry calls the function again from scratch; no conversation to repair, but `DispatchInfo.previousFailure` still carries what went wrong, if the script wants to branch on it. |
|
|
70
86
|
| `timeoutMs` | No | 1 hour | Same as the agent case — a hang past this is a `timeout` crash. |
|
|
71
|
-
| `usageLimitRetryMs` / `usageLimitMaxWaitMs` | No | 30 min / 24 h | Present on both leaf kinds, but only a failure classified as `usage-limit` starts a wait —
|
|
87
|
+
| `usageLimitRetryMs` / `usageLimitMaxWaitMs` | No | 30 min / 24 h | Present on both leaf kinds, but only a failure classified as `usage-limit` starts a wait — only `opencodeRunner` does that classification (`claudeCodeRunner` does not, so its quota failures are `runner-error`), and a script's own throws are `runner-error`. A script that calls an LLM itself owns its error handling. |
|
|
72
88
|
| `validate` | No | — | Same contract as the agent case, minus the nudging — a script has no session to resume, so a rejection spends a retry attempt directly. Rarely needed, since a script's own result is usually correct by construction — but useful for confirming a side effect (a file actually got written) before the dispatch counts as done. |
|
|
73
89
|
|
|
74
|
-
`type` and `file` are the only two a script state can't do without
|
|
90
|
+
`type` and `file` are the only two a script state can't do without — a script leaves every other field optional. An agent state additionally requires `prompt` and an `outputType`, as the agent table above shows.
|
|
75
91
|
|
|
76
92
|
## Composite
|
|
77
93
|
|
|
@@ -103,19 +119,27 @@ a top-level state. `Implement` is nested inside `Fix`. `Review`'s
|
|
|
103
119
|
`changes_requested` edge still names `Implement` directly:
|
|
104
120
|
|
|
105
121
|
```ts
|
|
106
|
-
// darkf/workflows/series-ordering/workflow.ts (prompts omitted)
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
122
|
+
// darkf/workflows/series-ordering/workflow.ts (prompts and retries omitted)
|
|
123
|
+
import { implementOutput, reviewOutput } from "./output-types.ts";
|
|
124
|
+
export const workflow: WorkflowDef = {
|
|
125
|
+
initial: "Fix",
|
|
126
|
+
agent: { model: "claude-sonnet-5-5" },
|
|
110
127
|
states: {
|
|
111
|
-
|
|
112
|
-
|
|
128
|
+
Fix: {
|
|
129
|
+
initial: "Implement",
|
|
130
|
+
maxCycles: 8,
|
|
131
|
+
states: {
|
|
132
|
+
Implement: { type: "agent", outputType: implementOutput, on: { implemented: "Verify", blocked: DONE, disputed: DONE } },
|
|
133
|
+
Verify: { type: "script", file: "verify.ts", on: { green: "Review", red: "Implement" } },
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
Review: {
|
|
137
|
+
type: "agent",
|
|
138
|
+
outputType: reviewOutput,
|
|
139
|
+
on: { done: DONE, changes_requested: "Implement" },
|
|
140
|
+
},
|
|
113
141
|
},
|
|
114
|
-
}
|
|
115
|
-
Review: {
|
|
116
|
-
type: "agent",
|
|
117
|
-
on: { done: EXIT, changes_requested: "Implement" },
|
|
118
|
-
},
|
|
142
|
+
};
|
|
119
143
|
```
|
|
120
144
|
|
|
121
145
|
`Review` doesn't name `Fix`. It doesn't need to know `Implement` is
|
|
@@ -152,7 +176,7 @@ Both leaf kinds return the same shape:
|
|
|
152
176
|
export interface NodeResult {
|
|
153
177
|
event?: string;
|
|
154
178
|
output?: unknown;
|
|
155
|
-
|
|
179
|
+
agentSessionId?: string;
|
|
156
180
|
}
|
|
157
181
|
```
|
|
158
182
|
|
|
@@ -160,7 +184,7 @@ export interface NodeResult {
|
|
|
160
184
|
|---|---|---|
|
|
161
185
|
| `event` | No | What `on`/`next` route on. Omit it and only `next` can match. |
|
|
162
186
|
| `output` | No | This node's payload for whatever node runs next. See [Handoff](../handoff/) for the rule governing it — nothing carries further than one hop unless a node re-emits it. |
|
|
163
|
-
| `
|
|
187
|
+
| `agentSessionId` | No | Opaque, runner-native session id, relayed back into `DispatchInfo` on this same node's next retry attempt, so a stateless runner can resume instead of starting cold. core never interprets it. |
|
|
164
188
|
|
|
165
189
|
All three are optional — every field on `NodeResult` carries a `?`. A leaf can return `{}` and still be valid, as long as some edge's `next` is there to catch it.
|
|
166
190
|
|
|
@@ -3,7 +3,7 @@ title: Pick an agent runner
|
|
|
3
3
|
description: The two built-in AgentRunner adapters, and the contract they implement.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
|
-
Any `agent` state needs a workflow-supplied `AgentRunner
|
|
6
|
+
Any `agent` state needs a workflow-supplied `AgentRunner` and a model:
|
|
7
7
|
|
|
8
8
|
```ts
|
|
9
9
|
export interface AgentRunner {
|
|
@@ -12,7 +12,13 @@ export interface AgentRunner {
|
|
|
12
12
|
}
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
darkf ships two concrete adapters in
|
|
15
|
+
darkf ships two concrete adapters in `@psimona/darkf/src/kit/agents/`.
|
|
16
|
+
Both read the model from the state's effective `agent` bag and reject a
|
|
17
|
+
dispatch without one. There is no fallback to the CLI's own default model.
|
|
18
|
+
A model id belongs to its harness: `claude-sonnet-5-5` is a Claude Code id,
|
|
19
|
+
`zai-coding-plan/glm-5.3-flash` an opencode id. `darkf validate` warns about
|
|
20
|
+
an id its runner does not recognize (see [Model warnings](#model-warnings)).
|
|
21
|
+
A run does not check it.
|
|
16
22
|
|
|
17
23
|
### `claudeCodeRunner`
|
|
18
24
|
|
|
@@ -20,12 +26,25 @@ Shells out to the `claude` CLI. Builds a prompt from the state's `prompt`,
|
|
|
20
26
|
the handoff, and the set of valid events, writes a handoff-file contract
|
|
21
27
|
the agent is instructed to fill in, and reads that file back as the
|
|
22
28
|
`NodeResult`. Reuses the CLI's own `session_id` across retries via
|
|
23
|
-
`
|
|
29
|
+
`agentSessionId` so a repair turn resumes context instead of starting cold.
|
|
24
30
|
|
|
25
31
|
```ts
|
|
32
|
+
import { DONE, type WorkflowDef } from "@psimona/darkf/src/core/index.ts";
|
|
26
33
|
import { claudeCodeRunner } from "@psimona/darkf/src/kit/agents/claude-code.ts";
|
|
34
|
+
import { zodOutputType } from "@psimona/darkf/src/kit/validation/zod.ts";
|
|
35
|
+
import { z } from "zod";
|
|
36
|
+
|
|
37
|
+
const greetOutput = zodOutputType(z.object({ greeting: z.string() }));
|
|
27
38
|
|
|
28
39
|
export const agentRunner = claudeCodeRunner;
|
|
40
|
+
|
|
41
|
+
export const workflow: WorkflowDef = {
|
|
42
|
+
initial: "Greet",
|
|
43
|
+
agent: { model: "claude-sonnet-5-5" },
|
|
44
|
+
states: {
|
|
45
|
+
Greet: { type: "agent", outputType: greetOutput, prompt: "Write a one-sentence greeting.", next: DONE },
|
|
46
|
+
},
|
|
47
|
+
};
|
|
29
48
|
```
|
|
30
49
|
|
|
31
50
|
### `opencodeRunner`
|
|
@@ -39,11 +58,128 @@ rather than letting a silently-incomplete turn look like a clean exit with
|
|
|
39
58
|
a missing handoff file.
|
|
40
59
|
|
|
41
60
|
```ts
|
|
61
|
+
import { DONE, type WorkflowDef } from "@psimona/darkf/src/core/index.ts";
|
|
42
62
|
import { opencodeRunner } from "@psimona/darkf/src/kit/agents/opencode.ts";
|
|
63
|
+
import { zodOutputType } from "@psimona/darkf/src/kit/validation/zod.ts";
|
|
64
|
+
import { z } from "zod";
|
|
65
|
+
|
|
66
|
+
const greetOutput = zodOutputType(z.object({ greeting: z.string() }));
|
|
43
67
|
|
|
44
68
|
export const agentRunner = opencodeRunner;
|
|
69
|
+
|
|
70
|
+
export const workflow: WorkflowDef = {
|
|
71
|
+
initial: "Greet",
|
|
72
|
+
agent: { model: "zai-coding-plan/glm-5.3-flash" },
|
|
73
|
+
states: {
|
|
74
|
+
Greet: { type: "agent", outputType: greetOutput, prompt: "Write a one-sentence greeting.", next: DONE },
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Per-state selection
|
|
80
|
+
|
|
81
|
+
The `agentRunner` export is the default harness. To run some states on
|
|
82
|
+
another one, register it by name in an `agentRunners` export and set
|
|
83
|
+
`runner` on those states:
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
import { DONE, type WorkflowDef } from "@psimona/darkf/src/core/index.ts";
|
|
87
|
+
import { claudeCodeRunner } from "@psimona/darkf/src/kit/agents/claude-code.ts";
|
|
88
|
+
import { opencodeRunner } from "@psimona/darkf/src/kit/agents/opencode.ts";
|
|
89
|
+
import { zodOutputType } from "@psimona/darkf/src/kit/validation/zod.ts";
|
|
90
|
+
import { z } from "zod";
|
|
91
|
+
|
|
92
|
+
export const agentRunner = opencodeRunner; // default
|
|
93
|
+
export const agentRunners = { claude: claudeCodeRunner }; // selectable by name
|
|
94
|
+
|
|
95
|
+
// One outputType per agent state — shapes elided, this guide is about runners.
|
|
96
|
+
const planOutput = zodOutputType(z.unknown());
|
|
97
|
+
const buildOutput = zodOutputType(z.unknown());
|
|
98
|
+
const reviewOutput = zodOutputType(z.unknown());
|
|
99
|
+
|
|
100
|
+
export const workflow: WorkflowDef = {
|
|
101
|
+
initial: "plan",
|
|
102
|
+
agent: { model: "zai-coding-plan/glm-5.3-flash", auto: true }, // default
|
|
103
|
+
states: {
|
|
104
|
+
plan: {
|
|
105
|
+
type: "agent",
|
|
106
|
+
runner: "claude",
|
|
107
|
+
agent: { model: "claude-sonnet-5-5" },
|
|
108
|
+
outputType: planOutput,
|
|
109
|
+
prompt: "Plan the change.",
|
|
110
|
+
next: "build",
|
|
111
|
+
},
|
|
112
|
+
build: {
|
|
113
|
+
type: "agent", // inherits the default runner, model and auto
|
|
114
|
+
outputType: buildOutput,
|
|
115
|
+
prompt: "Implement the plan.",
|
|
116
|
+
next: "review",
|
|
117
|
+
},
|
|
118
|
+
review: {
|
|
119
|
+
type: "agent",
|
|
120
|
+
agent: { model: "zai-coding-plan/glm-5.3" }, // new model, keeps auto
|
|
121
|
+
outputType: reviewOutput,
|
|
122
|
+
prompt: "Review the change.",
|
|
123
|
+
next: DONE,
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
};
|
|
45
127
|
```
|
|
46
128
|
|
|
129
|
+
The name is your own key in `agentRunners`, not the runner's `kind`, so you
|
|
130
|
+
can register two configurations of one harness. Only leaf states take
|
|
131
|
+
`runner`; composites pass nothing down.
|
|
132
|
+
|
|
133
|
+
### Which bag a state receives
|
|
134
|
+
|
|
135
|
+
- No `runner`: `{ ...workflow.agent, ...state.agent }`. The merge is
|
|
136
|
+
shallow, so each state key replaces the default key.
|
|
137
|
+
- `runner` set: the state's own `agent` bag only, even when the named runner
|
|
138
|
+
is the same object as the default. `model`, `auto` and `permissionMode`
|
|
139
|
+
never carry over to another harness, so such a state pins its own `model`.
|
|
140
|
+
|
|
141
|
+
Permission settings differ between the built-in runners.
|
|
142
|
+
`claudeCodeRunner` defaults to `permissionMode: "bypassPermissions"`, while
|
|
143
|
+
`opencodeRunner` approves asks only with `auto: true`. Moving a state to
|
|
144
|
+
another runner can change what its agent may do.
|
|
145
|
+
|
|
146
|
+
### What fails before any dispatch
|
|
147
|
+
|
|
148
|
+
`darkf validate` and run start both reject:
|
|
149
|
+
|
|
150
|
+
| Error | Cause |
|
|
151
|
+
|---|---|
|
|
152
|
+
| `unknown-runner` | A state names a runner that is not an own key of `agentRunners`. It never falls back to the default. |
|
|
153
|
+
| `missing-runner` | A state names no runner and the workflow has no `agentRunner`. A workflow whose agent states all name one needs no default. |
|
|
154
|
+
| `missing-model` | A state's effective bag has no non-empty string `model`. This applies to custom runners too. |
|
|
155
|
+
| `missing-output-type` | An agent state has no `outputType`, or its value has no `safeParse` method. Scripts don't need one. |
|
|
156
|
+
|
|
157
|
+
`darkf validate` also fails on an `agentRunner` or `agentRunners` entry with
|
|
158
|
+
no `run` function. If you call `run()` yourself, pass both as
|
|
159
|
+
`RunOptions.agentRunner` and `RunOptions.agentRunners`.
|
|
160
|
+
|
|
161
|
+
### Model warnings
|
|
162
|
+
|
|
163
|
+
`darkf validate` prints a `warn agent models` line for each state whose model
|
|
164
|
+
its runner does not recognize. The exit code is unchanged.
|
|
165
|
+
|
|
166
|
+
| Runner | Warns when |
|
|
167
|
+
|---|---|
|
|
168
|
+
| `opencodeRunner` | `opencode models` does not list the id (a `#variant` suffix is ignored), or the list cannot be read. |
|
|
169
|
+
| `claudeCodeRunner` | The id is neither a Claude Code alias nor starts with `claude-`. A misspelled `claude-` id passes. |
|
|
170
|
+
|
|
171
|
+
A custom runner opts in with the optional
|
|
172
|
+
`checkModels(models, rootDir)` method, returning `{ model, message }` for each
|
|
173
|
+
id it does not recognize. It must not call a model.
|
|
174
|
+
|
|
175
|
+
### What a run records
|
|
176
|
+
|
|
177
|
+
Each agent dispatch's `running` status in the journal carries `runner` (the
|
|
178
|
+
selected name, absent for the default) and `runnerKind` (the resolved
|
|
179
|
+
runner's `kind`, absent when it declares none). `darkf report` prints both.
|
|
180
|
+
Session ids stay inside one node visit, and a state keeps its runner for the
|
|
181
|
+
whole visit, so one harness never receives another's session id.
|
|
182
|
+
|
|
47
183
|
### Writing your own
|
|
48
184
|
|
|
49
185
|
Both adapters follow the same two moves: build a prompt that includes the
|
|
@@ -54,4 +190,7 @@ in core or engine is specific to either shipped adapter.
|
|
|
54
190
|
|
|
55
191
|
For demos and tests, skip a real harness entirely — a plain object
|
|
56
192
|
satisfying `AgentRunner` is enough, as the `haiku-readme` example's stub
|
|
57
|
-
runner shows (see [Quickstart](../../start-here/quickstart/)).
|
|
193
|
+
runner shows (see [Quickstart](../../start-here/quickstart/)). Its states still
|
|
194
|
+
need a `model` value, such as `agent: { model: "stub" }`, and an
|
|
195
|
+
`outputType` on every agent state — validateWorkflow doesn't care that
|
|
196
|
+
the runner is a stub.
|
|
@@ -37,11 +37,13 @@ in memory.
|
|
|
37
37
|
// workflow.ts
|
|
38
38
|
import { EXIT, type WorkflowDef } from "@psimona/darkf/src/core/index.ts";
|
|
39
39
|
import { claudeCodeRunner } from "../../src/kit/agents/claude-code.ts";
|
|
40
|
+
import { workOutput } from "./output-types.ts";
|
|
40
41
|
|
|
41
42
|
export const agentRunner = claudeCodeRunner;
|
|
42
43
|
|
|
43
44
|
export const workflow: WorkflowDef = {
|
|
44
45
|
initial: "Next",
|
|
46
|
+
agent: { model: "claude-haiku-5-5" },
|
|
45
47
|
states: {
|
|
46
48
|
Next: {
|
|
47
49
|
type: "script",
|
|
@@ -50,6 +52,7 @@ export const workflow: WorkflowDef = {
|
|
|
50
52
|
},
|
|
51
53
|
Work: {
|
|
52
54
|
type: "agent",
|
|
55
|
+
outputType: workOutput,
|
|
53
56
|
prompt:
|
|
54
57
|
'Do the task named in the handoff\'s "title" field: append one ' +
|
|
55
58
|
"sentence to notes.md describing what you did, creating the file " +
|
|
@@ -65,6 +68,19 @@ export const workflow: WorkflowDef = {
|
|
|
65
68
|
};
|
|
66
69
|
```
|
|
67
70
|
|
|
71
|
+
`Work` is an agent state, so it needs an `outputType` — every agent leaf
|
|
72
|
+
does; `darkf validate` rejects the workflow without one. `Record` reads
|
|
73
|
+
`tasks.json` from disk, not the handoff, so the declaration stays
|
|
74
|
+
permissive on purpose:
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
// output-types.ts
|
|
78
|
+
import { zodOutputType } from "@psimona/darkf/src/kit/validation/zod.ts";
|
|
79
|
+
import { z } from "zod";
|
|
80
|
+
|
|
81
|
+
export const workOutput = zodOutputType(z.unknown());
|
|
82
|
+
```
|
|
83
|
+
|
|
68
84
|
`next.ts` and `record.ts` are the only two places that touch `tasks.json`,
|
|
69
85
|
and they agree on one convention: `"in-progress"` means "the item `Next`
|
|
70
86
|
handed out, that `Record` hasn't closed out yet" — so a crash between the
|
|
@@ -279,10 +295,10 @@ visits: {"Next":3,"Work":2,"Record":2}
|
|
|
279
295
|
|
|
280
296
|
dispatches:
|
|
281
297
|
Next 0.0s -> found
|
|
282
|
-
Work 20.1s -> done
|
|
298
|
+
Work 20.1s -> done kind=claude-code
|
|
283
299
|
Record 0.0s -> recorded
|
|
284
300
|
Next 0.0s -> found
|
|
285
|
-
Work 17.8s -> done
|
|
301
|
+
Work 17.8s -> done kind=claude-code
|
|
286
302
|
Record 0.0s -> recorded
|
|
287
303
|
Next 0.0s -> empty
|
|
288
304
|
usage (2 agent dispatches): $0.22, 369441 in (334480 cache-read, 34941 cache-write), 1724 out
|
|
@@ -67,9 +67,10 @@ succeeded or crashed.
|
|
|
67
67
|
bunx darkf validate my-workflow
|
|
68
68
|
```
|
|
69
69
|
|
|
70
|
-
Checks that the workflow resolves
|
|
71
|
-
`workflow
|
|
72
|
-
|
|
70
|
+
Checks that the workflow resolves, that `workflow.ts` loads and exports
|
|
71
|
+
`workflow`, that the state graph is sound, and that every agent state
|
|
72
|
+
resolves to a runner and a model and declares an `outputType` for its
|
|
73
|
+
handoff output. For the machine side, run `bunx darkf doctor`: the `claude` and `opencode` binaries on
|
|
73
74
|
`PATH`, and that you're not accidentally running from inside the darkf
|
|
74
75
|
checkout itself (a workflow's agent runner uses the invocation `cwd` as
|
|
75
76
|
its working directory — a common mistake when first wiring a workflow
|
|
@@ -51,18 +51,36 @@ bunx darkf run my-workflow
|
|
|
51
51
|
```
|
|
52
52
|
|
|
53
53
|
A workflow module can also export `input` (the seed `NodeResult` for the
|
|
54
|
-
first dispatch), `agentRunner` (required if any
|
|
54
|
+
first dispatch), `agentRunner` (the default runner, required if any
|
|
55
|
+
`type: "agent"` state names no `runner`), `agentRunners` (runners a state
|
|
56
|
+
selects by name),
|
|
55
57
|
and `scriptRunner` (optional — only needed if a script needs run-aware
|
|
56
58
|
`DispatchInfo`, not just the plain handoff).
|
|
57
59
|
|
|
58
60
|
### Adding an agent state
|
|
59
61
|
|
|
60
|
-
An `agent` state needs a `prompt` and a
|
|
62
|
+
An `agent` state needs a `prompt`, a model, an `outputType`, and a
|
|
63
|
+
configured `agentRunner`. Define the schema first — the `outputType`
|
|
64
|
+
declares the shape of the state's handoff output. Put it in its own
|
|
65
|
+
file, built with `zodOutputType()`, and import only the built value:
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
// output-types.ts
|
|
69
|
+
import { z } from "zod";
|
|
70
|
+
import { zodOutputType } from "@psimona/darkf/src/kit/validation/zod.ts";
|
|
71
|
+
|
|
72
|
+
const GreetOutputSchema = z.object({ greeting: z.string() });
|
|
73
|
+
export const greetOutput = zodOutputType(GreetOutputSchema);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Then add the agent state, pointing `outputType` at the built value:
|
|
61
77
|
|
|
62
78
|
```ts
|
|
63
79
|
Greet: {
|
|
64
80
|
type: "agent",
|
|
81
|
+
agent: { model: "claude-haiku-5-5" }, // required: every agent state needs a model
|
|
65
82
|
prompt: "Write a one-sentence greeting.",
|
|
83
|
+
outputType: greetOutput, // required: validateWorkflow rejects an agent leaf without one
|
|
66
84
|
on: { done: EXIT },
|
|
67
85
|
},
|
|
68
86
|
```
|
|
@@ -72,7 +90,7 @@ export const agentRunner: AgentRunner = claudeCodeRunner; // or opencodeRunner
|
|
|
72
90
|
```
|
|
73
91
|
|
|
74
92
|
See [Pick an agent runner](../pick-an-agent-runner/) for the two
|
|
75
|
-
built-in options.
|
|
93
|
+
built-in options and for choosing a runner per state.
|
|
76
94
|
|
|
77
95
|
### Branching and retrying
|
|
78
96
|
|
|
@@ -87,8 +105,9 @@ built-in options.
|
|
|
87
105
|
after 24 hours). A wait consumes no retry attempt and no `timeoutMs`
|
|
88
106
|
budget; see [Failure modes](../../concepts/failure-modes/).
|
|
89
107
|
- `outputType` checks the shape of an agent's `output` payload before
|
|
90
|
-
`validate` runs —
|
|
91
|
-
|
|
108
|
+
`validate` runs — every agent leaf must declare one (`darkf validate`
|
|
109
|
+
rejects the workflow otherwise); build one from a Zod schema with
|
|
110
|
+
`zodOutputType()` (`@psimona/darkf/src/kit/validation/zod.ts`); see
|
|
92
111
|
[Node types](../../concepts/node-types/).
|
|
93
112
|
- `validate(result, handoff)` runs a plain code check against a result
|
|
94
113
|
before it's accepted as `succeeded`; returning a string rejects it as
|
|
@@ -18,7 +18,9 @@ anything.
|
|
|
18
18
|
|
|
19
19
|
### `darkf run <dir> [message]`
|
|
20
20
|
|
|
21
|
-
Runs the workflow in-process, blocking until it finishes.
|
|
21
|
+
Runs the workflow in-process, blocking until it finishes. Agent states
|
|
22
|
+
dispatch to the module's `agentRunner` export, or to the `agentRunners`
|
|
23
|
+
entry their `runner` field names. `start` does the same. `message`
|
|
22
24
|
overrides the workflow module's own `input` export, becoming
|
|
23
25
|
`{ output: { message } }` for the first dispatch. Prints each dispatch's
|
|
24
26
|
event, the final status, and the journal path. Writes `.darkf/last-run.mmd`.
|
|
@@ -60,9 +62,11 @@ without the run finishing; otherwise the same exit codes as `status`.
|
|
|
60
62
|
|
|
61
63
|
### `darkf report [--run <runId>]`
|
|
62
64
|
|
|
63
|
-
Prints a full dispatch timeline (state, duration, outcome per step
|
|
65
|
+
Prints a full dispatch timeline (state, duration, outcome per step, and
|
|
66
|
+
for an agent dispatch `runner=<name>` and `kind=<runnerKind>`), visit
|
|
64
67
|
counts, crash reasons, and a token/cost summary if any agent dispatch
|
|
65
|
-
wrote a usage sidecar.
|
|
68
|
+
wrote a usage sidecar. `runner=` is absent for the default runner, and
|
|
69
|
+
`kind=` for a runner that declares no `kind`. Works on any resolvable run, finished or not — but,
|
|
66
70
|
like `status`, needs `--run <runId>` once that run is no longer cached as
|
|
67
71
|
`running`.
|
|
68
72
|
|
|
@@ -82,9 +86,23 @@ check passes, `1` otherwise.
|
|
|
82
86
|
### `darkf validate <ref>`
|
|
83
87
|
|
|
84
88
|
Workflow checks, no environment: the workflow resolves, `workflow.ts`
|
|
85
|
-
loads, it exports `workflow
|
|
86
|
-
|
|
87
|
-
|
|
89
|
+
loads, it exports `workflow`, every `agentRunner` or `agentRunners` entry
|
|
90
|
+
it exports has a `run` function (the `agent runners` check), and the state
|
|
91
|
+
graph structure validates. Structure includes, for each agent state:
|
|
92
|
+
|
|
93
|
+
- its `runner` is a key of `agentRunners`
|
|
94
|
+
- without a `runner`, the module exports `agentRunner`
|
|
95
|
+
- its effective `agent` bag has a non-empty string `model`
|
|
96
|
+
- it declares a usable `outputType` — an object with a `safeParse`
|
|
97
|
+
method — naming the schema of its handoff output (`missing-output-type`)
|
|
98
|
+
|
|
99
|
+
A runner can also report models it does not recognize, as `warn agent models`
|
|
100
|
+
lines. Warnings never change the exit code. `opencodeRunner` compares against
|
|
101
|
+
`opencode models`; `claudeCodeRunner` only checks that the id is an alias or
|
|
102
|
+
starts with `claude-`.
|
|
103
|
+
|
|
104
|
+
See [Pick an agent runner](../../guides/pick-an-agent-runner/). Exits `0`
|
|
105
|
+
if every check passes, `1` otherwise.
|
|
88
106
|
|
|
89
107
|
### `darkf setup`
|
|
90
108
|
|