@psimona/darkf 0.0.0-stage → 0.4.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/README.md +28 -2
- package/bunfig.toml +13 -0
- package/examples/article-classifier/data/articles.ts +38 -0
- package/examples/article-classifier/scripts/next.ts +7 -0
- package/examples/article-classifier/scripts/queue.ts +33 -0
- package/examples/article-classifier/scripts/record.ts +9 -0
- package/examples/article-classifier/workflow.ts +44 -0
- package/examples/haiku-commit/scripts/check.ts +24 -0
- package/examples/haiku-commit/workflow.ts +28 -0
- package/examples/haiku-readme/scripts/test.ts +13 -0
- package/examples/haiku-readme/scripts/write.ts +10 -0
- package/examples/haiku-readme/workflow.ts +41 -0
- package/examples/task-queue/scripts/next.ts +20 -0
- package/examples/task-queue/scripts/record.ts +20 -0
- package/examples/task-queue/supervise.ts +57 -0
- package/examples/task-queue/workflow.ts +33 -0
- package/feedback/2026-10-30-self-update.md +29 -0
- package/package.json +30 -4
- package/src/cli/detached.test.ts +560 -0
- package/src/cli/index.ts +1040 -0
- package/src/cli/update.test.ts +486 -0
- package/src/cli/update.ts +191 -0
- package/src/core/graph.test.ts +172 -0
- package/src/core/graph.ts +148 -0
- package/src/core/index.ts +4 -0
- package/src/core/journal.ts +21 -0
- package/src/core/mermaid.test.ts +34 -0
- package/src/core/mermaid.ts +95 -0
- package/src/core/types.ts +279 -0
- package/src/engine/eventLog.ts +17 -0
- package/src/engine/index.ts +2 -0
- package/src/engine/run.test.ts +1068 -0
- package/src/engine/run.ts +359 -0
- package/src/exports.ts +2 -0
- package/src/kit/agents/claude-code.test.ts +37 -0
- package/src/kit/agents/claude-code.ts +180 -0
- package/src/kit/agents/handoff.test.ts +128 -0
- package/src/kit/agents/handoff.ts +69 -0
- package/src/kit/agents/opencode.test.ts +136 -0
- package/src/kit/agents/opencode.ts +327 -0
- package/src/kit/registry.test.ts +386 -0
- package/src/kit/registry.ts +296 -0
- package/src/kit/status.test.ts +423 -0
- package/src/kit/status.ts +282 -0
- package/tsconfig.json +4 -0
- package/workflows/dev-loop/scripts/begin.ts +22 -0
- package/workflows/dev-loop/scripts/check.ts +19 -0
- package/workflows/dev-loop/scripts/finish.ts +114 -0
- package/workflows/dev-loop/workflow.ts +135 -0
- package/workflows/series-ordering/scripts/verify.ts +18 -0
- package/workflows/series-ordering/workflow.ts +80 -0
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import type { WorkflowValidationError } from "./graph.ts";
|
|
2
|
+
|
|
3
|
+
// What a leaf node's dispatch produces when it succeeds. This is also the
|
|
4
|
+
// ONLY data a node ever receives from its predecessor — see `handoff` on
|
|
5
|
+
// DispatchInfo. There is no separate accumulating context: if a value needs
|
|
6
|
+
// to survive past the very next node, some node's own code must choose to
|
|
7
|
+
// re-emit it in its own `output`. See PHILOSOPHY.md.
|
|
8
|
+
export interface NodeResult {
|
|
9
|
+
event?: string;
|
|
10
|
+
output?: unknown;
|
|
11
|
+
/**
|
|
12
|
+
* Opaque harness-native session/thread id, meaningful only to the runner
|
|
13
|
+
* that returned it. The engine carries it forward into `DispatchInfo` on
|
|
14
|
+
* the next retry attempt of this same node, so a stateless runner can
|
|
15
|
+
* resume the same session instead of starting fresh — darkf-core never
|
|
16
|
+
* interprets or persists it beyond that.
|
|
17
|
+
*/
|
|
18
|
+
agentSessionId?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function isNodeResult(value: unknown): value is NodeResult {
|
|
22
|
+
if (typeof value !== "object" || value === null) return false;
|
|
23
|
+
const v = value as Record<string, unknown>;
|
|
24
|
+
if ("event" in v && v.event !== undefined && typeof v.event !== "string") return false;
|
|
25
|
+
if (
|
|
26
|
+
"agentSessionId" in v &&
|
|
27
|
+
v.agentSessionId !== undefined &&
|
|
28
|
+
typeof v.agentSessionId !== "string"
|
|
29
|
+
)
|
|
30
|
+
return false;
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Sentinel target meaning "the whole workflow completed its work". Renders
|
|
35
|
+
// as mermaid's own end pseudostate.
|
|
36
|
+
export const DONE = "[*]" as const;
|
|
37
|
+
// Sentinel target meaning "the graph gave up — it could not proceed". Mirrors
|
|
38
|
+
// DONE's pseudostate notation; `mermaid.ts` special-cases it the same way,
|
|
39
|
+
// since `[-]` has no built-in meaning to stateDiagram-v2 the way `[*]` does.
|
|
40
|
+
export const FAILED = "[-]" as const;
|
|
41
|
+
// Deprecated alias — kept only for a consumer repo's give-up edges, which still
|
|
42
|
+
// route to EXIT on its own schedule (docs/specs/failed-terminal/spec.md).
|
|
43
|
+
// Removed in the same change that lands the `run_failure` node + retarget
|
|
44
|
+
// table there; any surviving `EXIT` import then fails to compile.
|
|
45
|
+
export { DONE as EXIT };
|
|
46
|
+
|
|
47
|
+
export type Target = string;
|
|
48
|
+
|
|
49
|
+
export interface BaseLeafDef {
|
|
50
|
+
on?: Record<string, Target>;
|
|
51
|
+
next?: Target;
|
|
52
|
+
/** Bounded re-dispatch attempts after a crash, before giving up. Default 0 (no retry). Leaf-only. */
|
|
53
|
+
retries?: number;
|
|
54
|
+
/** Engine-enforced ceiling on one dispatch attempt. Default 1h. */
|
|
55
|
+
timeoutMs?: number;
|
|
56
|
+
/**
|
|
57
|
+
* Plain code check run against a well-formed NodeResult before it's accepted
|
|
58
|
+
* as `succeeded`. Return a failure message to reject it, or undefined to
|
|
59
|
+
* accept. A rejection consumes a retry attempt exactly like a runner crash.
|
|
60
|
+
*/
|
|
61
|
+
validate?: (result: NodeResult, handoff: NodeResult) => string | undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface AgentStateDef extends BaseLeafDef {
|
|
65
|
+
type: "agent";
|
|
66
|
+
prompt: string;
|
|
67
|
+
agent?: Record<string, unknown>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface ScriptStateDef extends BaseLeafDef {
|
|
71
|
+
type: "script";
|
|
72
|
+
file: string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Pure organizational nesting — no on/next of its own. The engine only ever executes leaves.
|
|
76
|
+
export interface CompositeStateDef {
|
|
77
|
+
initial: string;
|
|
78
|
+
states: Record<string, StateDef>;
|
|
79
|
+
/**
|
|
80
|
+
* Opt-in circuit breaker: max leaf dispatches anywhere in this composite's
|
|
81
|
+
* subtree during one continuous pass through it. Unlike a leaf's `retries`
|
|
82
|
+
* (bounded reattempts of one dispatch), this bounds a runaway loop that
|
|
83
|
+
* spans multiple leaves (e.g. two nodes flapping between each other)
|
|
84
|
+
* without ever tripping a per-dispatch timeout. Resets whenever execution
|
|
85
|
+
* re-enters this composite after having left it (dispatched some leaf
|
|
86
|
+
* outside its subtree in between) — a workflow that's re-entered once per
|
|
87
|
+
* item processed (e.g. once per scope in a multi-scope build phase) gets
|
|
88
|
+
* this budget fresh each time, not accumulated across unrelated passes.
|
|
89
|
+
* Undefined = no scoped limit; the engine-wide `maxSteps` remains the
|
|
90
|
+
* unconditional backstop regardless.
|
|
91
|
+
*/
|
|
92
|
+
maxCycles?: number;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export type StateDef = AgentStateDef | ScriptStateDef | CompositeStateDef;
|
|
96
|
+
|
|
97
|
+
export interface WorkflowDef {
|
|
98
|
+
initial: string;
|
|
99
|
+
states: Record<string, StateDef>;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function isComposite(def: StateDef): def is CompositeStateDef {
|
|
103
|
+
return "states" in def;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// What a leaf gets alongside its own def/ctx on every dispatch attempt.
|
|
107
|
+
// `validEvents` + `hasFallback` are how a runner does options-injection/
|
|
108
|
+
// classification — e.g. an LLM harness can constrain its structured output or
|
|
109
|
+
// list choices in the prompt. `agentSessionId` is never generated or stored by
|
|
110
|
+
// darkf-core — it's only ever a value a runner itself returned via a prior
|
|
111
|
+
// attempt's NodeResult, relayed back so a stateless runner can resume the
|
|
112
|
+
// same harness-native session rather than starting fresh. `previousFailure`
|
|
113
|
+
// is what rejected the previous attempt, letting a runner build a repair
|
|
114
|
+
// turn (e.g. resume the session and add a corrective prompt) on its own.
|
|
115
|
+
export interface DispatchInfo {
|
|
116
|
+
runId: string;
|
|
117
|
+
validEvents: string[];
|
|
118
|
+
hasFallback: boolean;
|
|
119
|
+
attempt: number;
|
|
120
|
+
agentSessionId?: string;
|
|
121
|
+
previousFailure?: CrashReason;
|
|
122
|
+
/**
|
|
123
|
+
* The engine's own step counter for this node's current visit — constant
|
|
124
|
+
* across every retry attempt of that visit. Combined with `rootDir` and
|
|
125
|
+
* `runId`, this is what a runner needs to compute a deterministic path
|
|
126
|
+
* under `.darkf/run/<runId>/` for its own artifacts (e.g. `darkf-cli`'s
|
|
127
|
+
* agent-handoff file convention), with no in-memory visit map of its own.
|
|
128
|
+
*/
|
|
129
|
+
step: number;
|
|
130
|
+
/**
|
|
131
|
+
* Mirrors `RunOptions.rootDir` — the root a runner should resolve its own
|
|
132
|
+
* `.darkf/run/<runId>/...` paths against. `ScriptRunner` gets `baseDir` as
|
|
133
|
+
* a dedicated param for an unrelated reason (locating script files);
|
|
134
|
+
* `AgentRunner` has no equivalent, so it's carried here instead.
|
|
135
|
+
*/
|
|
136
|
+
rootDir: string;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Open literal union (`LiteralUnion` idiom): the `(string & {})` branch
|
|
140
|
+
// stops TypeScript collapsing this back down to plain `string` the way
|
|
141
|
+
// `"claude-code" | "opencode" | string` would on its own — an editor still
|
|
142
|
+
// autocompletes/typo-catches the two built-ins, but a third-party runner's
|
|
143
|
+
// own string still typechecks.
|
|
144
|
+
export type AgentRunnerKind = "claude-code" | "opencode" | (string & {});
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Incremental liveness signal a runner may report via `AgentRunner.run`'s
|
|
148
|
+
* optional `onProgress` callback, throttled to roughly once per 30s —
|
|
149
|
+
* see docs/specs/agent-liveness/spec.md. The engine owns the journal: a
|
|
150
|
+
* runner only ever reports its latest snapshot, never writes anything
|
|
151
|
+
* itself.
|
|
152
|
+
*/
|
|
153
|
+
export interface AgentProgress {
|
|
154
|
+
/** When this snapshot was taken. */
|
|
155
|
+
at: number;
|
|
156
|
+
/** Cumulative step-level events seen so far this dispatch attempt. */
|
|
157
|
+
stepCount: number;
|
|
158
|
+
/** The most recent event's raw type string — runner-defined (e.g.
|
|
159
|
+
* opencode's `"step-finish"`), display-only. */
|
|
160
|
+
lastEvent: string;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export interface AgentRunner {
|
|
164
|
+
/** Self-identification (e.g. `"opencode"`, `"claude-code"`), set once per
|
|
165
|
+
* runner implementation. Read by darkf-cli at run-start time to record
|
|
166
|
+
* which runner executed a run; absent for runners that don't identify
|
|
167
|
+
* themselves. */
|
|
168
|
+
kind?: AgentRunnerKind;
|
|
169
|
+
run(
|
|
170
|
+
id: string,
|
|
171
|
+
def: AgentStateDef,
|
|
172
|
+
handoff: NodeResult,
|
|
173
|
+
info: DispatchInfo,
|
|
174
|
+
onProgress?: (progress: AgentProgress) => void,
|
|
175
|
+
): Promise<NodeResult>;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export interface ScriptRunner {
|
|
179
|
+
run(
|
|
180
|
+
id: string,
|
|
181
|
+
def: ScriptStateDef,
|
|
182
|
+
handoff: NodeResult,
|
|
183
|
+
baseDir: string,
|
|
184
|
+
info: DispatchInfo,
|
|
185
|
+
): Promise<NodeResult>;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// --- Node execution state machine ---
|
|
189
|
+
//
|
|
190
|
+
// One shape, used recursively: a leaf dispatching a runner, a composite whose
|
|
191
|
+
// current descendant leaf is running, and the whole run itself (the root
|
|
192
|
+
// composite's execution) are all "a node executing". Same four phases at
|
|
193
|
+
// every depth. A crash at any depth propagates straight to the top — nothing
|
|
194
|
+
// in between can catch or reroute it, because composites don't own transitions.
|
|
195
|
+
export type NodeStatus =
|
|
196
|
+
| { phase: "pending" }
|
|
197
|
+
| {
|
|
198
|
+
phase: "running";
|
|
199
|
+
attempt: number;
|
|
200
|
+
startedAt: number;
|
|
201
|
+
lastHeartbeat: number;
|
|
202
|
+
agentSessionId?: string;
|
|
203
|
+
/** Latest step count from the agent's own event stream, when the
|
|
204
|
+
* runner reports progress (`AgentRunner.run`'s optional
|
|
205
|
+
* `onProgress`) — see docs/specs/agent-liveness/spec.md. Absent for
|
|
206
|
+
* script dispatches or runners that don't report progress. */
|
|
207
|
+
agentStepCount?: number;
|
|
208
|
+
/** Milliseconds since the last agent-reported progress event, as of
|
|
209
|
+
* this heartbeat's own timestamp (not live at read time — `status`/
|
|
210
|
+
* `monitor` add elapsed-since-heartbeat themselves, the same way
|
|
211
|
+
* they already do for `lastHeartbeat`). Absent under the same
|
|
212
|
+
* conditions as `agentStepCount`. */
|
|
213
|
+
agentIdleMs?: number;
|
|
214
|
+
/** Set only while the engine is nudging this attempt (1..3) — see
|
|
215
|
+
* docs/specs/session-nudge/spec.md. Absent on an ordinary dispatch. */
|
|
216
|
+
nudgeAttempt?: number;
|
|
217
|
+
/** The nudgeable `CrashReason` kind that triggered this nudge. */
|
|
218
|
+
nudgeReason?: CrashReason["kind"];
|
|
219
|
+
}
|
|
220
|
+
| { phase: "succeeded"; result: NodeResult }
|
|
221
|
+
| { phase: "failed"; result: NodeResult }
|
|
222
|
+
| { phase: "crashed"; reason: CrashReason; attempt: number };
|
|
223
|
+
|
|
224
|
+
export type CrashReason =
|
|
225
|
+
| { kind: "runner-error"; error: string }
|
|
226
|
+
| { kind: "timeout"; elapsedMs: number }
|
|
227
|
+
| { kind: "handoff-missing" }
|
|
228
|
+
| { kind: "handoff-unparseable"; parseError: string }
|
|
229
|
+
| { kind: "malformed-result" }
|
|
230
|
+
| { kind: "validation-failed"; message: string }
|
|
231
|
+
| { kind: "no-transition"; state: string; event?: string }
|
|
232
|
+
| { kind: "unresolved-target"; from: string; target: string }
|
|
233
|
+
| { kind: "duplicate-id"; id: string }
|
|
234
|
+
| { kind: "max-steps"; steps: number; state: string }
|
|
235
|
+
| { kind: "cycle-limit"; composite: string; cycles: number }
|
|
236
|
+
| { kind: "invalid-workflow"; errors: WorkflowValidationError[] };
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Thrown by an `AgentRunner` in place of a plain `Error` when the harness
|
|
240
|
+
* session may still be resumable despite the failure. Carries what a plain
|
|
241
|
+
* `Error` can't: the session id to resume, and which `CrashReason` kind this
|
|
242
|
+
* is — so the engine can route the correct nudge template without
|
|
243
|
+
* inspecting the error message string. See
|
|
244
|
+
* docs/specs/session-nudge/spec.md.
|
|
245
|
+
*/
|
|
246
|
+
export class AgentFailure extends Error {
|
|
247
|
+
agentSessionId?: string;
|
|
248
|
+
crashReason?: CrashReason;
|
|
249
|
+
constructor(message: string, opts?: { agentSessionId?: string; crashReason?: CrashReason }) {
|
|
250
|
+
super(message);
|
|
251
|
+
this.agentSessionId = opts?.agentSessionId;
|
|
252
|
+
this.crashReason = opts?.crashReason;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export interface StepTrace {
|
|
257
|
+
state: string;
|
|
258
|
+
scopePath: string[];
|
|
259
|
+
result: NodeResult;
|
|
260
|
+
timestamp: number;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// One of these per transition of ANY node's execution machine — a dispatch
|
|
264
|
+
// starting, a retry re-entering `running`, a terminal succeeded/crashed. This
|
|
265
|
+
// is the full recording; `trace` (below) is just the succeeded-leaf summary
|
|
266
|
+
// used for routing/debugging, `onEvent` is the complete stream.
|
|
267
|
+
export interface NodeEvent {
|
|
268
|
+
runId: string;
|
|
269
|
+
state: string;
|
|
270
|
+
scopePath: string[];
|
|
271
|
+
status: NodeStatus;
|
|
272
|
+
timestamp: number;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export interface RunResult {
|
|
276
|
+
runId: string;
|
|
277
|
+
trace: StepTrace[];
|
|
278
|
+
node: NodeStatus;
|
|
279
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { appendFileSync, mkdirSync } from "node:fs";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import type { NodeEvent } from "../core/index.ts";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The default event sink: appends one JSON line per NodeEvent to `path`.
|
|
7
|
+
* Synchronous by design — events are emitted sequentially within a single
|
|
8
|
+
* run (the engine awaits each dispatch before the next), so a sync append
|
|
9
|
+
* is enough to guarantee line order without a queue. Pair with
|
|
10
|
+
* `parseEventLog` from darkf-core to read one back (e.g. in a web viewer).
|
|
11
|
+
*/
|
|
12
|
+
export function createJsonlEventLogger(path: string): (event: NodeEvent) => void {
|
|
13
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
14
|
+
return (event: NodeEvent) => {
|
|
15
|
+
appendFileSync(path, `${JSON.stringify(event)}\n`);
|
|
16
|
+
};
|
|
17
|
+
}
|