@psimona/darkf 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/concepts/failure-modes.md +23 -7
- package/docs/concepts/handoff.md +3 -3
- package/docs/concepts/node-types.md +44 -20
- package/docs/guides/pick-an-agent-runner.md +122 -4
- package/docs/guides/plan-with-a-task-queue.md +9 -7
- package/docs/guides/run-detached-and-monitor.md +3 -3
- package/docs/guides/write-your-first-workflow.md +14 -5
- package/docs/index.mdx +4 -0
- package/docs/reference/cli-commands.md +22 -6
- package/docs/reference/core-types.md +29 -4
- package/docs/reference/package-map.md +12 -8
- package/docs/start-here/quickstart.md +15 -7
- package/package.json +9 -2
- package/skills/darkf-writing-workflows/SKILL.md +141 -7
- package/src/cli/detached.test.ts +88 -2
- package/src/cli/doctor.test.ts +225 -10
- package/src/cli/index.ts +106 -27
- package/src/cli/setup.ts +4 -9
- package/src/cli/state-file.ts +14 -0
- package/src/cli/tarball.test.ts +3 -1
- package/src/cli/update.ts +4 -9
- package/src/core/graph.test.ts +124 -19
- package/src/core/graph.ts +51 -3
- package/src/core/tasks.ts +1 -1
- package/src/core/types.test.ts +52 -0
- package/src/core/types.ts +48 -18
- package/src/engine/run.test.ts +708 -18
- package/src/engine/run.ts +101 -16
- package/src/exports.ts +4 -0
- package/src/kit/agents/claude-code.test.ts +38 -2
- package/src/kit/agents/claude-code.ts +28 -37
- package/src/kit/agents/handoff.test.ts +12 -0
- package/src/kit/agents/handoff.ts +32 -8
- package/src/kit/agents/opencode.test.ts +1 -1
- package/src/kit/agents/opencode.ts +44 -28
- package/src/kit/specs.test.ts +247 -0
- package/src/kit/specs.ts +344 -0
- package/src/kit/status.test.ts +24 -0
- package/src/kit/status.ts +31 -14
- package/src/kit/validation/zod.test.ts +42 -0
- package/src/kit/validation/zod.ts +20 -0
- package/workflows/dev-loop/scripts/begin.ts +9 -6
- package/workflows/dev-loop/scripts/dispatch.ts +50 -0
- package/workflows/dev-loop/scripts/finish.ts +23 -21
- package/workflows/dev-loop/scripts/mark-done.ts +22 -0
- package/workflows/dev-loop/scripts/validate-plan.ts +30 -0
- package/workflows/dev-loop/workflow.ts +164 -68
- package/workflows/series-ordering/workflow.ts +1 -0
- /package/workflows/dev-loop/scripts/{check.ts → test.ts} +0 -0
|
@@ -12,22 +12,26 @@ darkf ships two packages:
|
|
|
12
12
|
- **`core/`** — the shared vocabulary: `types.ts` (every type on this
|
|
13
13
|
site's [Core types](../core-types/) page), `graph.ts`
|
|
14
14
|
(registry-building and target resolution), `mermaid.ts` (render-only,
|
|
15
|
-
never parsed back), `journal.ts` (the JSONL event shape).
|
|
16
|
-
runtime
|
|
15
|
+
never parsed back), `journal.ts` (the JSONL event shape). One
|
|
16
|
+
runtime dependency, Zod — `types.ts` uses it for the fixed
|
|
17
|
+
`NodeResult` envelope check; no public type references a schema
|
|
18
|
+
library.
|
|
17
19
|
- **`engine/`** — the interpreter. `run.ts` is the dispatch loop:
|
|
18
|
-
retries, per-attempt timeouts, the `
|
|
19
|
-
backoff, cycle-limit and
|
|
20
|
+
retries, per-attempt timeouts, the `outputType` shape check and
|
|
21
|
+
`validate` hook, usage-limit backoff, cycle-limit and
|
|
20
22
|
`maxSteps` circuit breakers, unconditional journaling. Depends only
|
|
21
23
|
on `core/`.
|
|
22
24
|
- **`kit/`** — the workflow-authoring library: the two built-in
|
|
23
25
|
`AgentRunner` adapters (`claudeCodeRunner`, `opencodeRunner`), the
|
|
24
|
-
handoff contract,
|
|
26
|
+
handoff contract, the Zod adapter for `outputType` shape checks
|
|
27
|
+
(`validation/zod.ts`), and the run registry/status helpers behind
|
|
25
28
|
[CLI commands](../cli-commands/). Depends on `core/` and `engine/`.
|
|
26
29
|
- **`cli/`** — the `darkf` binary itself. Depends on all three.
|
|
27
30
|
- **`darkf-opencode-plugin`** — an experimental OpenCode TUI integration
|
|
28
|
-
(a
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
(a non-modal panel inside the OpenCode editor: workflow definitions and
|
|
32
|
+
run status, with active runs tiled side by side). No public release
|
|
33
|
+
yet. Its own package,
|
|
34
|
+
because its peer dependencies (`@opencode/plugin`, `@opentui/*`,
|
|
31
35
|
`solid-js`) are real and optional — no other consumer needs them.
|
|
32
36
|
|
|
33
37
|
A dependency-cruiser rule enforces the one-way chain
|
|
@@ -59,6 +59,7 @@ export const agentRunner = claudeCodeRunner;
|
|
|
59
59
|
|
|
60
60
|
export const workflow: WorkflowDef = {
|
|
61
61
|
initial: "Write",
|
|
62
|
+
agent: { model: "claude-haiku-5-5" },
|
|
62
63
|
states: {
|
|
63
64
|
Write: {
|
|
64
65
|
type: "agent",
|
|
@@ -189,7 +190,7 @@ line per transition, JSON per line. This run's, in full:
|
|
|
189
190
|
```
|
|
190
191
|
{"runId":"...","state":"[*]","scopePath":[],"status":{"phase":"succeeded","result":{}},"timestamp":...}
|
|
191
192
|
{"runId":"...","state":"Write","scopePath":[],"status":{"phase":"running","attempt":1,...},"timestamp":...}
|
|
192
|
-
{"runId":"...","state":"Write","scopePath":[],"status":{"phase":"succeeded","result":{"event":"done","output":{...},"
|
|
193
|
+
{"runId":"...","state":"Write","scopePath":[],"status":{"phase":"succeeded","result":{"event":"done","output":{...},"agentSessionId":"..."}},"timestamp":...}
|
|
193
194
|
{"runId":"...","state":"Check","scopePath":[],"status":{"phase":"running","attempt":1,...},"timestamp":...}
|
|
194
195
|
{"runId":"...","state":"Check","scopePath":[],"status":{"phase":"succeeded","result":{"event":"committed"}},"timestamp":...}
|
|
195
196
|
{"runId":"...","state":"[*]","scopePath":[],"status":{"phase":"succeeded","result":{"event":"committed"}},"timestamp":...}
|
|
@@ -213,7 +214,7 @@ message: (none)
|
|
|
213
214
|
span: 19.8s, 2 dispatches
|
|
214
215
|
|
|
215
216
|
dispatches:
|
|
216
|
-
Write 19.8s -> done
|
|
217
|
+
Write 19.8s -> done kind=claude-code
|
|
217
218
|
Check 0.0s -> committed
|
|
218
219
|
usage (1 agent dispatches): $0.13, 222581 in (201997 cache-read, 20572 cache-write), 1072 out
|
|
219
220
|
```
|
|
@@ -225,13 +226,20 @@ exercise this same routing logic without spending anything.
|
|
|
225
226
|
|
|
226
227
|
## See it in an editor
|
|
227
228
|
|
|
228
|
-
darkf also ships an experimental OpenCode plugin — a
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
229
|
+
darkf also ships an experimental OpenCode plugin — a panel inside the
|
|
230
|
+
editor, instead of a second terminal for `darkf monitor`. Chat stays
|
|
231
|
+
visible beside it. A `panel │ full` toggle flips the panel full-screen,
|
|
232
|
+
and a narrow terminal always gets full-screen. Two tabs: `workflows`
|
|
233
|
+
catalogs every workflow definition as an expandable state graph, and
|
|
234
|
+
`runs` shows your runs. Active runs tile side by side, as many as the
|
|
235
|
+
width fits, and each drops out of the grid the moment it finishes. With
|
|
236
|
+
none active, `runs` falls back to a plain list — active runs first, each
|
|
237
|
+
with a spinner, with a search box. Selecting a run shows its live graph,
|
|
238
|
+
task tree, and dwell warnings. `Tab` cycles focus across the tab strip,
|
|
239
|
+
the content, and each tile.
|
|
232
240
|
|
|
233
241
|
<div style="border: 2px dashed var(--sl-color-gray-4); border-radius: 0.5rem; padding: 3rem 1.5rem; text-align: center; color: var(--sl-color-gray-3); margin: 1.5rem 0;">
|
|
234
|
-
Screenshot coming soon — OpenCode plugin
|
|
242
|
+
Screenshot coming soon — OpenCode plugin overlay
|
|
235
243
|
</div>
|
|
236
244
|
|
|
237
245
|
It's part of this repo (`darkf-opencode-plugin`), not yet published or
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@psimona/darkf",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "A small, typed workflow engine for agent + script state graphs.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Patrizio Simona <patriziosimona@gmail.com>",
|
|
@@ -30,9 +30,16 @@
|
|
|
30
30
|
"state-graph",
|
|
31
31
|
"claude-code",
|
|
32
32
|
"opencode",
|
|
33
|
-
"bun"
|
|
33
|
+
"bun",
|
|
34
|
+
"dark-factory",
|
|
35
|
+
"software-factory",
|
|
36
|
+
"automation",
|
|
37
|
+
"llm-agents"
|
|
34
38
|
],
|
|
35
39
|
"engines": {
|
|
36
40
|
"bun": ">=1.2"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"zod": "^4.6.7"
|
|
37
44
|
}
|
|
38
45
|
}
|
|
@@ -22,13 +22,15 @@ Every state that can route backward must live inside ONE composite. `maxCycles`
|
|
|
22
22
|
## Skeleton
|
|
23
23
|
|
|
24
24
|
```ts
|
|
25
|
-
import {
|
|
25
|
+
import { DONE, type AgentRunner, type WorkflowDef } from "/abs/path/to/darkf/src/core/index.ts";
|
|
26
26
|
import { claudeCodeRunner } from "/abs/path/to/darkf/src/kit/agents/claude-code.ts";
|
|
27
27
|
|
|
28
28
|
export const agentRunner: AgentRunner = claudeCodeRunner;
|
|
29
29
|
|
|
30
30
|
export const workflow: WorkflowDef = {
|
|
31
31
|
initial: "Loop",
|
|
32
|
+
// Every agent state needs a model: set it here, or in the state's own `agent`.
|
|
33
|
+
agent: { model: "claude-sonnet-5-5" },
|
|
32
34
|
states: {
|
|
33
35
|
Loop: {
|
|
34
36
|
initial: "Implement",
|
|
@@ -39,17 +41,17 @@ export const workflow: WorkflowDef = {
|
|
|
39
41
|
states: {
|
|
40
42
|
Implement: {
|
|
41
43
|
type: "agent", retries: 1, prompt: IMPL,
|
|
42
|
-
on: { implemented: "Verify", disputed:
|
|
44
|
+
on: { implemented: "Verify", disputed: DONE, blocked: DONE },
|
|
43
45
|
},
|
|
44
46
|
Verify: {
|
|
45
47
|
// Review only sees Verify output. Relay what Implement reported:
|
|
46
48
|
// return { event: "green", output: { implemented: handoff.output } }
|
|
47
49
|
type: "script", file: "verify.ts", retries: 1,
|
|
48
|
-
on: { green: "Review", red: "Implement", blocked:
|
|
50
|
+
on: { green: "Review", red: "Implement", blocked: DONE },
|
|
49
51
|
},
|
|
50
52
|
Review: {
|
|
51
53
|
type: "agent", retries: 1, prompt: REVIEW,
|
|
52
|
-
on: { done:
|
|
54
|
+
on: { done: DONE, changes_requested: "Implement" },
|
|
53
55
|
},
|
|
54
56
|
},
|
|
55
57
|
},
|
|
@@ -64,20 +66,152 @@ Workflows outside the darkf package use absolute paths into the package, as in t
|
|
|
64
66
|
## Runners and the handoff contract
|
|
65
67
|
|
|
66
68
|
- Use the built-in runners (`claudeCodeRunner`, `opencodeRunner` from `src/kit/agents/`). Never parse agent stdout. The runner injects the event contract into the prompt and reads the handoff JSON file the agent writes.
|
|
69
|
+
- To run one state on another harness, export `agentRunners = { claude: claudeCodeRunner }` beside `agentRunner` and set `runner: "claude"` on the state. That state inherits nothing from `workflow.agent`, so give it its own `agent: { model }`.
|
|
67
70
|
- The handoff contains only the immediate predecessor result. If Verify must report what Implement did, Verify must relay it in its own output. Nothing carries forward unless each node re-emits it.
|
|
68
71
|
- Do not ask the agent to echo events in prose. Describe the event semantics. The runner appends the mechanics.
|
|
69
72
|
|
|
73
|
+
### Reading an agent claim off the handoff
|
|
74
|
+
|
|
75
|
+
Agents emit the claim in several shapes: flat at the handoff top level,
|
|
76
|
+
nested under `output`, or hybrid — flat claim plus a nested summary block.
|
|
77
|
+
Never read `handoff.output ?? handoff`. A truthy `output` without the claim
|
|
78
|
+
field shadows a correct flat verdict (run 0a1b2ca3: a passing
|
|
79
|
+
`behavior_ok: true` verdict was declared invalid, and the repair loop
|
|
80
|
+
tripped the composite's cycle budget on a healthy tree). Scan the
|
|
81
|
+
candidates and prefer whichever shape actually carries the claim field:
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
const claim = [handoff.output, handoff].find(
|
|
85
|
+
(c) => c != null && typeof (c as Claim).verdict_field === "boolean",
|
|
86
|
+
);
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Pair this with the maxCycles rule above: one false rejection costs a full
|
|
90
|
+
repair round, and the budget counts dispatches, not repairs.
|
|
91
|
+
|
|
70
92
|
## Scripts
|
|
71
93
|
|
|
72
94
|
A script default export receives `(handoff, info)` and returns a `NodeResult`. `info.rootDir` is the invocation cwd (the target repo). Use it for every repo path so the workflow stays location-independent. `file:` resolves against `<workflow-dir>/scripts/`, not the workflow dir.
|
|
73
95
|
|
|
96
|
+
## Validate agent output with `outputType`
|
|
97
|
+
|
|
98
|
+
An agent state accepts an optional `outputType`: a shape check the engine runs against the node's `output` payload, before `validate`. Use it for structure ("does the output have these fields?"), keep `validate` for logic ("is the value acceptable?"). A state may set both. Type runs first, so a `validate` script can read `output.tasks[2].title` without re-checking the shape itself. `outputType` is agent-only: a script's output comes from code you already wrote and control. `validate` stays available on both agent and script states.
|
|
99
|
+
|
|
100
|
+
### The `OutputSchema` interface
|
|
101
|
+
|
|
102
|
+
`outputType` is typed `OutputSchema` (`src/core/types.ts`), deliberately narrow and Zod-free:
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
export interface OutputSchema {
|
|
106
|
+
safeParse(value: unknown): { success: true } | { success: false; message: string };
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Any schema library with a `.safeParse()` can implement it in a few lines. darkf-core's public types never reference a schema library, so your Zod version can never conflict with darkf's.
|
|
111
|
+
|
|
112
|
+
### The shipped Zod adapter
|
|
113
|
+
|
|
114
|
+
darkf ships exactly one adapter, for Zod (`src/kit/validation/zod.ts`):
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
import { zodOutputType } from "@psimona/darkf/src/kit/validation/zod.ts";
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
It joins every issue from one failed parse into a single message — `summary: Invalid input: expected string, received number; count: Invalid input: expected number, received string` — so one corrective round names every bad field, not just the first. Valibot or ArkType follow the same two-method shape; write your own, darkf ships no others.
|
|
121
|
+
|
|
122
|
+
### Schema-first: the schema lives in its own file
|
|
123
|
+
|
|
124
|
+
Define the Zod schema once in its own file, derive the type with `z.infer`, and export the built adapter value. The workflow definition imports only that value — never Zod directly — and stays what it already is, a plain TypeScript object:
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
// workflows/dev-loop/schemas/plan-output.ts
|
|
128
|
+
import { z } from "zod";
|
|
129
|
+
import { zodOutputType } from "@psimona/darkf/src/kit/validation/zod.ts";
|
|
130
|
+
|
|
131
|
+
const PlanOutputSchema = z.object({
|
|
132
|
+
summary: z.string(),
|
|
133
|
+
tasks: z.array(z.object({ id: z.string(), title: z.string() })),
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
export type PlanOutput = z.infer<typeof PlanOutputSchema>;
|
|
137
|
+
export const planOutputType = zodOutputType(PlanOutputSchema);
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
// workflow.ts
|
|
142
|
+
import { planOutputType } from "./schemas/plan-output.ts";
|
|
143
|
+
|
|
144
|
+
Plan: {
|
|
145
|
+
type: "agent",
|
|
146
|
+
agent: { model: "claude-sonnet-5-5" },
|
|
147
|
+
prompt: PLAN_PROMPT,
|
|
148
|
+
outputType: planOutputType,
|
|
149
|
+
on: { planned: "ValidatePlan" },
|
|
150
|
+
},
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
A failed check rejects the dispatch as `validation-failed`, with the joined message as the reason. It nudges before it retries: up to three corrective nudges in the same agent session, each fed the message, then a fresh attempt against `retries`. The whole sequence is one node visit — no extra `retries` consumed, no extra `maxCycles` count — but each nudge is still a full dispatch call.
|
|
154
|
+
|
|
155
|
+
## Three-phase vs. one lane: only plan when planning earns its keep
|
|
156
|
+
|
|
157
|
+
The two sections below (generate a `TaskList`, walk it with a `Dispatch`
|
|
158
|
+
node) describe a three-phase shape: a planning phase that distills a spec
|
|
159
|
+
into a list of items, a loop that works through that list one item at a
|
|
160
|
+
time, and whatever finishes the run. Treat this as the shape to reach for
|
|
161
|
+
when a workflow justifies it, not the default shape for every workflow:
|
|
162
|
+
|
|
163
|
+
- **Use it** when the spec implies more than one independently-buildable,
|
|
164
|
+
independently-verifiable deliverable, and you can't enumerate them by
|
|
165
|
+
hand at authoring time — the count and shape of the work only become
|
|
166
|
+
known once an agent actually reads the spec. A backend feature with an
|
|
167
|
+
unknown number of files/behaviors to add is the motivating case.
|
|
168
|
+
- **Skip it** when the workflow already does one well-defined thing, or a
|
|
169
|
+
small fixed number of known steps you can enumerate yourself while
|
|
170
|
+
writing the workflow. One fix, one script change, one narrowly-scoped
|
|
171
|
+
task — hardcode the lane directly (the Skeleton above: one composite,
|
|
172
|
+
`Implement → Verify → Review`, no `TaskList`, no `Plan`, no `Dispatch`).
|
|
173
|
+
Forcing a planning phase onto a workflow with nothing real to plan adds
|
|
174
|
+
two more dispatches and a loop with nothing to walk, for zero benefit.
|
|
175
|
+
|
|
176
|
+
The tell: if you can write out every state the graph needs just from
|
|
177
|
+
reading the spec's title, skip planning — build that lane directly. If
|
|
178
|
+
you can't say in advance how many deliverables the spec implies, that's
|
|
179
|
+
exactly what a planning phase is for.
|
|
180
|
+
|
|
181
|
+
## Keep prompts feature-agnostic
|
|
182
|
+
|
|
183
|
+
A workflow's `prompt` strings are fixed at authoring time — they cannot read the spec per dispatch. The temptation, once a workflow targets one real feature, is to hand-transcribe spec content straight into the prompt: a deliverables checklist, a review checklist, extracted requirements. Don't. That content drifts the moment the spec changes after the prompt was written, and someone has to hand-rewrite it for every new feature the same workflow file later dispatches.
|
|
184
|
+
|
|
185
|
+
If a node needs a concrete artifact derived from the spec, add a dedicated pair of steps that generates and persists it, and have every downstream node read what was persisted instead of carrying feature-specific prose in the workflow file:
|
|
186
|
+
|
|
187
|
+
- **Generating** is almost always an **agent**, not a script — distilling free-form spec prose into a checklist takes judgment. Have it emit the artifact as structured JSON in its own `output`, not as a file it writes itself.
|
|
188
|
+
- If the artifact is a plan or task breakdown, don't invent a one-off shape: darkf already has a shared `TaskList`/`Task`/`TaskRequirement` vocabulary (`darkf/src/core/types.ts`) with a hand-written, zero-dep validator (`parseTaskList`) and a canonical location the OpenCode plugin already renders live progress from. Emit that shape, not markdown prose — you get free validation, a `satisfies`-linked requirement/task map (a review checklist falls out of it: for each requirement, is there a `done` task that satisfies it?), and live status in the plugin's "tasks" tab if the runner is `opencodeRunner`.
|
|
189
|
+
- **Persisting** is a script, immediately after the generating agent: it validates the candidate (`parseTaskList`, or your own check for a non-TaskList artifact) and writes it to the canonical run-scoped path, `.darkf/run/<runId>/<name>.json` — a script gets `info.rootDir`/`info.runId` to compute that path; a static agent prompt cannot interpolate a per-dispatch `runId`, only a compile-time constant like `FEATURE`. Route an invalid candidate back to the generating step (bounded by the same composite's `maxCycles`, per the Bounded loops rule above), not forward to a node that can't use it.
|
|
190
|
+
- Every downstream node's prompt reads the file back by finding the run's own directory — "the directory under `.darkf/run/` with the latest mtime" (there is exactly one active run per dispatch) — the same pattern a run-retrospective step already needs to locate its own journal. Never by the engine handoff, which only ever carries the immediately preceding node's result, and never by pasting the artifact's content into the prompt by hand.
|
|
191
|
+
- A review checklist is exactly as spec-specific as a deliverables list, and belongs in the same generated artifact, not hand-copied into the Review prompt either — if you used a `TaskList`, it's the `requirements` array, already there.
|
|
192
|
+
- One generating/persisting pair can feed several downstream nodes several hops apart (e.g. the planner feeding both the implementer and a much later reviewer) without anything carrying it through every hop in between — it's a file on disk, not a handoff value, so the "nothing rides along for free" rule doesn't apply to it. `.darkf/` is already gitignored, so nothing here needs its own ignore rule or merge-time cleanup.
|
|
193
|
+
|
|
194
|
+
## Walk a plan one item at a time, with a dedicated Dispatch node
|
|
195
|
+
|
|
196
|
+
Once you have a `TaskList` (previous section), don't hand it whole to one agent and ask it to work through every task in a single dispatch. That hides the per-task state inside one agent's own judgment about what it already finished, has no node any run status can point at ("which task is this run on right now?"), and a crash mid-plan loses track of exactly where it was.
|
|
197
|
+
|
|
198
|
+
This is the `item-loop-convention` shape (a design doc, not shipped `darkf-core`/`kit` code as of this writing; hand-roll its algorithm directly in your own `scripts/` rather than importing a module that doesn't exist yet):
|
|
199
|
+
|
|
200
|
+
- **Dispatch** (a script) is the node that walks the plan. It reads the persisted `TaskList`, picks the first task whose status is not yet `"done"` (`"pending"` normally; an `"in-progress"` one found on resume means a crash interrupted it — dispatch that same one again, no special case needed), flips it `"in-progress"`, writes the file back, and hands that one task to the next node as its `output`. If every task is `"done"`, route to whatever follows the loop. If any are `"failed"` with none left `"pending"`, that's a distinct blocked outcome, not "exhausted."
|
|
201
|
+
- The lane that follows (implement, test, fix, review — whatever one task's own work needs) operates on that single task only. It gets the task directly via the one-hop handoff from Dispatch if it's the very next node; anything further downstream (e.g. a reviewer several hops past Dispatch, past a fix-retry loop) rediscovers the right task by reading the plan file itself and finding the one still `"in-progress"` — never by relying on a value threaded through every intervening node.
|
|
202
|
+
- Only flip a task to `"done"` once its result is independently confirmed, not when the implementing step merely claims it finished. If judgment is needed to confirm that (a reviewer checking the claim against the working tree), split it into the judging agent (which never touches the file) and a small script right after it that does the actual write — same agent/script split as everywhere else in this document.
|
|
203
|
+
- The lane's last node routes back to Dispatch by its state id — a flat edge, which is fine even across composite boundaries (`buildRegistry` is one flat map keyed by state id; routing doesn't care which composite either side lives in). Dispatch gets re-entered once per task until the plan is exhausted.
|
|
204
|
+
- **Keep Dispatch outside the lane's composite.** Wrap only the lane (implement, test, fix, review, mark-done) in its own composite; Dispatch sits as that composite's sibling, not inside it. This is what makes the lane's `maxCycles` budget one task's worst case, not the whole plan's: the engine resets a composite's cycle counter whenever the dispatch immediately before it did not already carry that composite in its scope (`CompositeStateDef.maxCycles`'s doc, `src/core/types.ts`). Every hand-off from Dispatch into the lane is such a reset, because Dispatch itself lives outside it.
|
|
205
|
+
- Size the lane's `maxCycles` by hand for one task's worst case only: `dispatches per task in the worst case`, with slack (roughly ×1.5) for an occasional extra fix round. Never multiply by the expected task count — `WorkflowDef` is static and built before the real task count is known, so a budget sized against an assumed total crashes the whole run the first time a real plan comes out bigger, even when no single task is anywhere near a runaway. Put no `maxCycles` on Dispatch itself; the engine-wide `maxSteps` is the only backstop needed on the item-to-item loop, matching `item-loop-convention`'s own non-goal of not wiring this automatically.
|
|
206
|
+
- This loop and the plan-generation loop from the previous section are two separate composites with two separate `maxCycles` budgets, not one — they bound unrelated things (a small, fixed number of generation retries vs. one task's worst case), per the Bounded loops rule above.
|
|
207
|
+
|
|
74
208
|
## Event design
|
|
75
209
|
|
|
76
|
-
Keep event sets small and semantic: `implemented`/`blocked`, `green`/`red`, `done`/`changes_requested`. Use `
|
|
210
|
+
Keep event sets small and semantic: `implemented`/`blocked`, `green`/`red`, `done`/`changes_requested`. Use `DONE` for terminal human gates. A crash is not a verdict. A blocked agent is not a crash either: exit cleanly and let a human re-enter.
|
|
77
211
|
|
|
78
212
|
### The `disputed` event
|
|
79
213
|
|
|
80
|
-
Give the implementer a reserved `disputed` event. When a review demand contradicts the spec, the implementer objects and routes `disputed →
|
|
214
|
+
Give the implementer a reserved `disputed` event. When a review demand contradicts the spec, the implementer objects and routes `disputed → DONE`. The demand and a spec-grounded rationale go in `output`. The run ends with the disagreement recorded for a human. Compliance must never be the only path.
|
|
81
215
|
|
|
82
216
|
## Review prompts
|
|
83
217
|
|
|
@@ -85,4 +219,4 @@ Agent-authored extra files are invisible to `git diff`. Every review prompt must
|
|
|
85
219
|
|
|
86
220
|
## Verify before you run
|
|
87
221
|
|
|
88
|
-
From the target repo: `darkf validate <ref>`, then `darkf render <ref>`. The packaged guide is the long form: `docs/guides/write-your-first-workflow.md` inside the darkf package.
|
|
222
|
+
From the target repo: `darkf validate <ref>`, then `darkf render <ref>`. Read any `warn agent models` line: it does not fail validation, but the model may be misspelled or belong to another harness. The packaged guide is the long form: `docs/guides/write-your-first-workflow.md` inside the darkf package.
|
package/src/cli/detached.test.ts
CHANGED
|
@@ -134,7 +134,7 @@ export const agentRunner: AgentRunner = {
|
|
|
134
134
|
export const workflow: WorkflowDef = {
|
|
135
135
|
initial: "Plan",
|
|
136
136
|
states: {
|
|
137
|
-
Plan: { type: "agent", prompt: "plan", on: { approved: "Implement" } },
|
|
137
|
+
Plan: { type: "agent", agent: { model: "test-model" }, prompt: "plan", on: { approved: "Implement" } },
|
|
138
138
|
Implement: {
|
|
139
139
|
initial: "Write",
|
|
140
140
|
states: {
|
|
@@ -142,7 +142,7 @@ export const workflow: WorkflowDef = {
|
|
|
142
142
|
Test: { type: "script", file: "test.ts", on: { passed: "Review" } },
|
|
143
143
|
},
|
|
144
144
|
},
|
|
145
|
-
Review: { type: "agent", prompt: "review", on: { done: EXIT } },
|
|
145
|
+
Review: { type: "agent", agent: { model: "test-model" }, prompt: "review", on: { done: EXIT } },
|
|
146
146
|
},
|
|
147
147
|
};
|
|
148
148
|
`,
|
|
@@ -302,6 +302,92 @@ test("status and monitor report succeeded for a run of the haiku-readme example"
|
|
|
302
302
|
expect(mon.stdout).toContain("succeeded");
|
|
303
303
|
});
|
|
304
304
|
|
|
305
|
+
// --- per-state runners ---
|
|
306
|
+
|
|
307
|
+
// Two stubs that report which one ran, and a default `kind` unlike the named one.
|
|
308
|
+
const TWO_RUNNERS: Record<string, string> = {
|
|
309
|
+
"workflow.ts": `import { type AgentRunner, EXIT, type WorkflowDef } from "@psimona/darkf/src/core/index.ts";
|
|
310
|
+
export const agentRunner: AgentRunner = {
|
|
311
|
+
kind: "opencode",
|
|
312
|
+
async run() { return { by: "default" }; },
|
|
313
|
+
};
|
|
314
|
+
export const agentRunners: Record<string, AgentRunner> = {
|
|
315
|
+
other: {
|
|
316
|
+
kind: "claude-code",
|
|
317
|
+
async run(_id, _def, handoff) { return { first: handoff.output, second: "other" }; },
|
|
318
|
+
},
|
|
319
|
+
};
|
|
320
|
+
export const workflow: WorkflowDef = {
|
|
321
|
+
initial: "A",
|
|
322
|
+
states: {
|
|
323
|
+
A: { type: "agent", agent: { model: "test-model" }, prompt: "a", next: "B" },
|
|
324
|
+
B: { type: "agent", runner: "other", agent: { model: "test-model" }, prompt: "b", next: EXIT },
|
|
325
|
+
},
|
|
326
|
+
};
|
|
327
|
+
`,
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
test("run passes agentRunners to the engine and records the default runner's kind", async () => {
|
|
331
|
+
const root = makeProjectRoot();
|
|
332
|
+
writeFixture(root, "two-runners", TWO_RUNNERS);
|
|
333
|
+
|
|
334
|
+
const res = await runCli(["run", "two-runners"], root);
|
|
335
|
+
|
|
336
|
+
expect(res.stdout).toContain("status: succeeded");
|
|
337
|
+
expect(res.stdout).toContain('by: "default"');
|
|
338
|
+
expect(res.stdout).toContain('second: "other"');
|
|
339
|
+
expect(listEntries(root)[0]?.agentRunnerKind).toBe("opencode");
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
test("the detached worker passes agentRunners and records the default runner's kind", async () => {
|
|
343
|
+
const root = makeProjectRoot();
|
|
344
|
+
writeFixture(root, "two-runners", TWO_RUNNERS);
|
|
345
|
+
// The entry exists before the worker starts, so its `kind` merge has a target.
|
|
346
|
+
const runId = randomUUID();
|
|
347
|
+
writeEntry(root, {
|
|
348
|
+
schemaVersion: SCHEMA_VERSION,
|
|
349
|
+
runId,
|
|
350
|
+
pid: process.pid,
|
|
351
|
+
rootDir: root,
|
|
352
|
+
startedAt: Date.now(),
|
|
353
|
+
status: "running",
|
|
354
|
+
finishedAt: null,
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
const worker = await runCli(["__worker", "two-runners", runId], root);
|
|
358
|
+
|
|
359
|
+
expect(worker.code).toBe(0);
|
|
360
|
+
expect(journalTerminalState(root, runId)).toBe("succeeded");
|
|
361
|
+
expect(readEntry(root, runId)?.agentRunnerKind).toBe("opencode");
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
test("report shows the runner name and kind of each agent dispatch", async () => {
|
|
365
|
+
const root = makeProjectRoot();
|
|
366
|
+
writeFixture(root, "two-runners", TWO_RUNNERS);
|
|
367
|
+
await runCli(["run", "two-runners"], root);
|
|
368
|
+
const runId = listEntries(root)[0]!.runId;
|
|
369
|
+
|
|
370
|
+
const res = await runCli(["report", "--run", runId], root);
|
|
371
|
+
|
|
372
|
+
expect(res.code).toBe(0);
|
|
373
|
+
expect(res.stdout).toMatch(/^ {2}A .*-> \(no event\) {2}kind=opencode$/m);
|
|
374
|
+
expect(res.stdout).toMatch(/^ {2}B .*-> \(no event\) {2}runner=other {2}kind=claude-code$/m);
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
test("run crashes on an unknown runner name before any dispatch", async () => {
|
|
378
|
+
const root = makeProjectRoot();
|
|
379
|
+
const workflow = TWO_RUNNERS["workflow.ts"]!.replace('runner: "other"', 'runner: "typo"');
|
|
380
|
+
writeFixture(root, "typo", { "workflow.ts": workflow });
|
|
381
|
+
|
|
382
|
+
const res = await runCli(["run", "typo"], root);
|
|
383
|
+
|
|
384
|
+
expect(res.stdout).toContain("status: crashed");
|
|
385
|
+
expect(res.stdout).toContain(
|
|
386
|
+
'{"kind":"unknown-runner","state":"B","runner":"typo","known":["other"]}',
|
|
387
|
+
);
|
|
388
|
+
expect(res.stdout).not.toContain("A -> event=");
|
|
389
|
+
});
|
|
390
|
+
|
|
305
391
|
// --- crashed ---
|
|
306
392
|
|
|
307
393
|
test("status and monitor report crashed (with reason) for a run that crashes mid-run", async () => {
|