@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.
- package/docs/concepts/failure-modes.md +177 -0
- package/docs/concepts/handoff.md +91 -0
- package/docs/concepts/journal.md +92 -0
- package/docs/concepts/node-types.md +177 -0
- package/docs/guides/pick-an-agent-runner.md +57 -0
- package/docs/guides/plan-with-a-task-queue.md +411 -0
- package/docs/guides/render-to-mermaid.md +22 -0
- package/docs/guides/run-detached-and-monitor.md +76 -0
- package/docs/guides/test-your-workflow.md +20 -0
- package/docs/guides/write-your-first-workflow.md +101 -0
- package/docs/index.mdx +68 -0
- package/docs/reference/cli-commands.md +105 -0
- package/docs/reference/core-types.md +128 -0
- package/docs/reference/package-map.md +39 -0
- package/docs/start-here/install.md +71 -0
- package/docs/start-here/quickstart.md +248 -0
- package/package.json +15 -2
- package/skills/darkf-usage/SKILL.md +92 -0
- package/skills/darkf-writing-workflows/SKILL.md +213 -0
- package/src/cli/doctor.test.ts +338 -0
- package/src/cli/index.ts +139 -15
- package/src/cli/setup.test.ts +384 -0
- package/src/cli/setup.ts +281 -0
- package/src/cli/state-file.ts +14 -0
- package/src/cli/tarball.test.ts +127 -0
- package/src/cli/update.test.ts +82 -0
- package/src/cli/update.ts +24 -9
- package/src/core/tasks.ts +1 -1
- package/src/core/types.test.ts +52 -0
- package/src/core/types.ts +33 -16
- package/src/engine/run.test.ts +462 -3
- package/src/engine/run.ts +114 -8
- package/src/exports.ts +4 -0
- package/src/kit/agents/claude-code.ts +4 -34
- package/src/kit/agents/handoff.test.ts +12 -0
- package/src/kit/agents/handoff.ts +32 -8
- package/src/kit/agents/opencode.test.ts +232 -2
- package/src/kit/agents/opencode.ts +80 -31
- package/src/kit/specs.test.ts +247 -0
- package/src/kit/specs.ts +344 -0
- package/src/kit/status.test.ts +67 -0
- package/src/kit/status.ts +29 -4
- 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 +26 -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 +171 -64
- package/bunfig.toml +0 -13
- package/examples/article-classifier/data/articles.ts +0 -38
- package/examples/article-classifier/scripts/next.ts +0 -7
- package/examples/article-classifier/scripts/queue.ts +0 -33
- package/examples/article-classifier/scripts/record.ts +0 -9
- package/examples/article-classifier/workflow.ts +0 -44
- package/examples/haiku-commit/scripts/check.ts +0 -24
- package/examples/haiku-commit/workflow.ts +0 -28
- package/examples/haiku-readme/scripts/test.ts +0 -13
- package/examples/haiku-readme/scripts/write.ts +0 -10
- package/examples/haiku-readme/workflow.ts +0 -41
- package/examples/task-queue/scripts/next.ts +0 -20
- package/examples/task-queue/scripts/record.ts +0 -20
- package/examples/task-queue/supervise.ts +0 -57
- package/examples/task-queue/workflow.ts +0 -33
- package/feedback/2026-10-30-self-update.md +0 -29
- package/feedback/2026-10-31-task-list-json.md +0 -26
- package/tsconfig.json +0 -4
- /package/workflows/dev-loop/scripts/{check.ts → test.ts} +0 -0
package/src/kit/specs.ts
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
// The backplane spec workspace contract (D15, D16). One spec per folder under
|
|
2
|
+
// `docs/specs/`, a single ordered `backlog.json`, and a `project.json` for
|
|
3
|
+
// harness and sandbox defaults. Every file is typed here, and validators reject
|
|
4
|
+
// anything the format does not name — so a workflow that imports this module
|
|
5
|
+
// sees the same shape the backplane does.
|
|
6
|
+
//
|
|
7
|
+
// Format rules live in the backplane repo at
|
|
8
|
+
// `docs/specs/0001-spec-format/spec.md`. Keep this module in sync with it.
|
|
9
|
+
|
|
10
|
+
export const SPEC_FORMAT_VERSION = 1;
|
|
11
|
+
|
|
12
|
+
/** Frontmatter of a `spec.md`. Unknown fields are rejected. */
|
|
13
|
+
export interface SpecFrontmatter {
|
|
14
|
+
/** UUIDv4 identity. Generated at creation, never changes. */
|
|
15
|
+
id: string;
|
|
16
|
+
/** Human key for URLs, search, and chat. Safe to change. */
|
|
17
|
+
slug: string;
|
|
18
|
+
/** The one person accountable for this spec. */
|
|
19
|
+
owner: string;
|
|
20
|
+
/** UUIDs of specs this one waits for. Satisfied when those ship. */
|
|
21
|
+
depends_on?: string[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** `docs/specs/backlog.json` — the only planning state stored (D16). */
|
|
25
|
+
export interface BacklogFile {
|
|
26
|
+
version: 1;
|
|
27
|
+
/** Spec UUIDs in priority order. Unlisted specs sort after these. */
|
|
28
|
+
order: string[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** An allowlisted pod volume mount for a project's sandbox (D3). */
|
|
32
|
+
export interface SandboxMount {
|
|
33
|
+
name: string;
|
|
34
|
+
mountPath: string;
|
|
35
|
+
/** Optional size hint, e.g. "10Gi". */
|
|
36
|
+
size?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** `project.json` — harness and sandbox defaults (D2, D3). */
|
|
40
|
+
export interface ProjectConfig {
|
|
41
|
+
version: 1;
|
|
42
|
+
title?: string;
|
|
43
|
+
harness?: {
|
|
44
|
+
/** Agent runner kind, e.g. "claude-code" or "opencode". */
|
|
45
|
+
runner: string;
|
|
46
|
+
/** Optional model hint passed to the runner. */
|
|
47
|
+
model?: string;
|
|
48
|
+
};
|
|
49
|
+
sandbox?: {
|
|
50
|
+
/** Project-supplied image, normally the CI image (D3). */
|
|
51
|
+
image?: string;
|
|
52
|
+
/** Isolation runtime from the allowlist, e.g. "kata" or "gvisor". */
|
|
53
|
+
runtimeClass?: string;
|
|
54
|
+
mounts?: SandboxMount[];
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A parsed `spec.md`: frontmatter plus the untouched body. */
|
|
59
|
+
export interface SpecDocument {
|
|
60
|
+
frontmatter: SpecFrontmatter;
|
|
61
|
+
body: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Raised when a backplane file does not match the format. */
|
|
65
|
+
export class SpecFormatError extends Error {
|
|
66
|
+
constructor(readonly issues: string[]) {
|
|
67
|
+
super(`invalid spec workspace file: ${issues.join("; ")}`);
|
|
68
|
+
this.name = "SpecFormatError";
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const UUID_V4 = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
73
|
+
const SLUG = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
74
|
+
|
|
75
|
+
const SPEC_FIELDS = new Set(["id", "slug", "owner", "depends_on"]);
|
|
76
|
+
const BACKLOG_FIELDS = new Set(["version", "order"]);
|
|
77
|
+
const PROJECT_FIELDS = new Set(["version", "title", "harness", "sandbox"]);
|
|
78
|
+
const HARNESS_FIELDS = new Set(["runner", "model"]);
|
|
79
|
+
const SANDBOX_FIELDS = new Set(["image", "runtimeClass", "mounts"]);
|
|
80
|
+
const MOUNT_FIELDS = new Set(["name", "mountPath", "size"]);
|
|
81
|
+
|
|
82
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
83
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function unknownFields(
|
|
87
|
+
value: Record<string, unknown>,
|
|
88
|
+
allowed: Set<string>,
|
|
89
|
+
where: string,
|
|
90
|
+
): string[] {
|
|
91
|
+
return Object.keys(value)
|
|
92
|
+
.filter((key) => !allowed.has(key))
|
|
93
|
+
.map((key) => `${where}: unknown field "${key}"`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function isUuid(value: unknown): value is string {
|
|
97
|
+
return typeof value === "string" && UUID_V4.test(value);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function collectUnique(values: string[], label: string): string[] {
|
|
101
|
+
const seen = new Set<string>();
|
|
102
|
+
const issues: string[] = [];
|
|
103
|
+
for (const value of values) {
|
|
104
|
+
if (seen.has(value)) issues.push(`${label}: duplicate "${value}"`);
|
|
105
|
+
seen.add(value);
|
|
106
|
+
}
|
|
107
|
+
return issues;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Parse and validate `spec.md` frontmatter. Throws {@link SpecFormatError}. */
|
|
111
|
+
export function parseSpecFrontmatter(value: unknown): SpecFrontmatter {
|
|
112
|
+
if (!isRecord(value)) throw new SpecFormatError(["frontmatter must be a YAML object"]);
|
|
113
|
+
|
|
114
|
+
const issues = unknownFields(value, SPEC_FIELDS, "frontmatter");
|
|
115
|
+
const { id, slug, owner, depends_on } = value;
|
|
116
|
+
|
|
117
|
+
if (!isUuid(id)) issues.push('frontmatter: "id" must be a UUIDv4 string');
|
|
118
|
+
if (typeof slug !== "string" || !SLUG.test(slug)) {
|
|
119
|
+
issues.push('frontmatter: "slug" must be lowercase kebab-case');
|
|
120
|
+
}
|
|
121
|
+
if (typeof owner !== "string" || owner.trim() === "") {
|
|
122
|
+
issues.push('frontmatter: "owner" must be a non-empty string');
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
let deps: string[] | undefined;
|
|
126
|
+
if (depends_on !== undefined) {
|
|
127
|
+
if (!Array.isArray(depends_on)) {
|
|
128
|
+
issues.push('frontmatter: "depends_on" must be an array of spec UUIDs');
|
|
129
|
+
} else {
|
|
130
|
+
for (const dep of depends_on) {
|
|
131
|
+
if (!isUuid(dep))
|
|
132
|
+
issues.push(`frontmatter: depends_on entry "${String(dep)}" is not a UUIDv4`);
|
|
133
|
+
}
|
|
134
|
+
issues.push(...collectUnique(depends_on as string[], "frontmatter: depends_on"));
|
|
135
|
+
if (depends_on.includes(id as string))
|
|
136
|
+
issues.push("frontmatter: a spec cannot depend on itself");
|
|
137
|
+
deps = [...(depends_on as string[])];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (issues.length > 0) throw new SpecFormatError(issues);
|
|
142
|
+
const spec: SpecFrontmatter = { id: id as string, slug: slug as string, owner: owner as string };
|
|
143
|
+
if (deps !== undefined) spec.depends_on = deps;
|
|
144
|
+
return spec;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Parse a `spec.md` file: YAML frontmatter plus body. Throws {@link SpecFormatError}. */
|
|
148
|
+
export function parseSpecDocument(text: string): SpecDocument {
|
|
149
|
+
const match = /^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)/.exec(text);
|
|
150
|
+
if (!match) {
|
|
151
|
+
throw new SpecFormatError(["spec.md must start with a --- frontmatter block"]);
|
|
152
|
+
}
|
|
153
|
+
let raw: unknown;
|
|
154
|
+
try {
|
|
155
|
+
raw = Bun.YAML.parse(match[1] ?? "");
|
|
156
|
+
} catch (error) {
|
|
157
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
158
|
+
throw new SpecFormatError([`frontmatter is not valid YAML: ${reason}`]);
|
|
159
|
+
}
|
|
160
|
+
return { frontmatter: parseSpecFrontmatter(raw), body: text.slice(match[0].length) };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Parse and validate `docs/specs/backlog.json`. Throws {@link SpecFormatError}. */
|
|
164
|
+
export function parseBacklog(value: unknown): BacklogFile {
|
|
165
|
+
if (!isRecord(value)) throw new SpecFormatError(["backlog must be a JSON object"]);
|
|
166
|
+
|
|
167
|
+
const issues = unknownFields(value, BACKLOG_FIELDS, "backlog");
|
|
168
|
+
if (value.version !== SPEC_FORMAT_VERSION) {
|
|
169
|
+
issues.push(`backlog: "version" must be ${SPEC_FORMAT_VERSION}`);
|
|
170
|
+
}
|
|
171
|
+
if (!Array.isArray(value.order)) {
|
|
172
|
+
issues.push('backlog: "order" must be an array of spec UUIDs');
|
|
173
|
+
} else {
|
|
174
|
+
for (const id of value.order) {
|
|
175
|
+
if (!isUuid(id)) issues.push(`backlog: order entry "${String(id)}" is not a UUIDv4`);
|
|
176
|
+
}
|
|
177
|
+
issues.push(...collectUnique(value.order as string[], "backlog: order"));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (issues.length > 0) throw new SpecFormatError(issues);
|
|
181
|
+
return { version: SPEC_FORMAT_VERSION, order: [...(value.order as string[])] };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Parse backlog.json text. Throws {@link SpecFormatError}. */
|
|
185
|
+
export function parseBacklogJson(text: string): BacklogFile {
|
|
186
|
+
return parseBacklog(parseJson(text, "backlog.json"));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Parse and validate project.json. Throws {@link SpecFormatError}. */
|
|
190
|
+
export function parseProjectConfig(value: unknown): ProjectConfig {
|
|
191
|
+
if (!isRecord(value)) throw new SpecFormatError(["project config must be a JSON object"]);
|
|
192
|
+
|
|
193
|
+
const issues = unknownFields(value, PROJECT_FIELDS, "project");
|
|
194
|
+
if (value.version !== SPEC_FORMAT_VERSION) {
|
|
195
|
+
issues.push(`project: "version" must be ${SPEC_FORMAT_VERSION}`);
|
|
196
|
+
}
|
|
197
|
+
if (value.title !== undefined && typeof value.title !== "string") {
|
|
198
|
+
issues.push('project: "title" must be a string');
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (value.harness !== undefined) {
|
|
202
|
+
if (!isRecord(value.harness)) {
|
|
203
|
+
issues.push('project: "harness" must be an object');
|
|
204
|
+
} else {
|
|
205
|
+
const harness = value.harness;
|
|
206
|
+
issues.push(...unknownFields(harness, HARNESS_FIELDS, "project.harness"));
|
|
207
|
+
if (typeof harness.runner !== "string" || harness.runner.trim() === "") {
|
|
208
|
+
issues.push('project.harness: "runner" must be a non-empty string');
|
|
209
|
+
}
|
|
210
|
+
if (harness.model !== undefined && typeof harness.model !== "string") {
|
|
211
|
+
issues.push('project.harness: "model" must be a string');
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (value.sandbox !== undefined) {
|
|
217
|
+
if (!isRecord(value.sandbox)) {
|
|
218
|
+
issues.push('project: "sandbox" must be an object');
|
|
219
|
+
} else {
|
|
220
|
+
const sandbox = value.sandbox;
|
|
221
|
+
issues.push(...unknownFields(sandbox, SANDBOX_FIELDS, "project.sandbox"));
|
|
222
|
+
for (const key of ["image", "runtimeClass"] as const) {
|
|
223
|
+
if (sandbox[key] !== undefined && typeof sandbox[key] !== "string") {
|
|
224
|
+
issues.push(`project.sandbox: "${key}" must be a string`);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (sandbox.mounts !== undefined) {
|
|
228
|
+
if (!Array.isArray(sandbox.mounts)) {
|
|
229
|
+
issues.push('project.sandbox: "mounts" must be an array');
|
|
230
|
+
} else {
|
|
231
|
+
sandbox.mounts.forEach((mount, index) => {
|
|
232
|
+
if (!isRecord(mount)) {
|
|
233
|
+
issues.push(`project.sandbox.mounts[${index}] must be an object`);
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
issues.push(...unknownFields(mount, MOUNT_FIELDS, `project.sandbox.mounts[${index}]`));
|
|
237
|
+
if (typeof mount.name !== "string" || mount.name.trim() === "") {
|
|
238
|
+
issues.push(`project.sandbox.mounts[${index}]: "name" must be a non-empty string`);
|
|
239
|
+
}
|
|
240
|
+
if (typeof mount.mountPath !== "string" || !mount.mountPath.startsWith("/")) {
|
|
241
|
+
issues.push(`project.sandbox.mounts[${index}]: "mountPath" must be an absolute path`);
|
|
242
|
+
}
|
|
243
|
+
if (mount.size !== undefined && typeof mount.size !== "string") {
|
|
244
|
+
issues.push(`project.sandbox.mounts[${index}]: "size" must be a string`);
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (issues.length > 0) throw new SpecFormatError(issues);
|
|
253
|
+
|
|
254
|
+
const project: ProjectConfig = { version: SPEC_FORMAT_VERSION };
|
|
255
|
+
if (typeof value.title === "string") project.title = value.title;
|
|
256
|
+
if (isRecord(value.harness)) {
|
|
257
|
+
const harness: NonNullable<ProjectConfig["harness"]> = {
|
|
258
|
+
runner: value.harness.runner as string,
|
|
259
|
+
};
|
|
260
|
+
if (typeof value.harness.model === "string") harness.model = value.harness.model;
|
|
261
|
+
project.harness = harness;
|
|
262
|
+
}
|
|
263
|
+
if (isRecord(value.sandbox)) {
|
|
264
|
+
const sandbox: NonNullable<ProjectConfig["sandbox"]> = {};
|
|
265
|
+
if (typeof value.sandbox.image === "string") sandbox.image = value.sandbox.image;
|
|
266
|
+
if (typeof value.sandbox.runtimeClass === "string") {
|
|
267
|
+
sandbox.runtimeClass = value.sandbox.runtimeClass;
|
|
268
|
+
}
|
|
269
|
+
if (Array.isArray(value.sandbox.mounts)) {
|
|
270
|
+
sandbox.mounts = value.sandbox.mounts.map((mount) => {
|
|
271
|
+
const raw = mount as Record<string, unknown>;
|
|
272
|
+
const parsed: SandboxMount = {
|
|
273
|
+
name: raw.name as string,
|
|
274
|
+
mountPath: raw.mountPath as string,
|
|
275
|
+
};
|
|
276
|
+
if (typeof raw.size === "string") parsed.size = raw.size;
|
|
277
|
+
return parsed;
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
project.sandbox = sandbox;
|
|
281
|
+
}
|
|
282
|
+
return project;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** Parse project.json text. Throws {@link SpecFormatError}. */
|
|
286
|
+
export function parseProjectJson(text: string): ProjectConfig {
|
|
287
|
+
return parseProjectConfig(parseJson(text, "project.json"));
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Validate a set of specs as a graph: unique ids and slugs, existing
|
|
292
|
+
* dependency targets, and no cycles. Throws {@link SpecFormatError}.
|
|
293
|
+
*/
|
|
294
|
+
export function validateSpecGraph(specs: SpecFrontmatter[]): void {
|
|
295
|
+
const issues: string[] = [];
|
|
296
|
+
issues.push(
|
|
297
|
+
...collectUnique(
|
|
298
|
+
specs.map((spec) => spec.id),
|
|
299
|
+
"spec graph: id",
|
|
300
|
+
),
|
|
301
|
+
);
|
|
302
|
+
issues.push(
|
|
303
|
+
...collectUnique(
|
|
304
|
+
specs.map((spec) => spec.slug),
|
|
305
|
+
"spec graph: slug",
|
|
306
|
+
),
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
const byId = new Map(specs.map((spec) => [spec.id, spec]));
|
|
310
|
+
for (const spec of specs) {
|
|
311
|
+
for (const dep of spec.depends_on ?? []) {
|
|
312
|
+
if (!byId.has(dep))
|
|
313
|
+
issues.push(`spec ${spec.slug}: depends_on target "${dep}" does not exist`);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const state = new Map<string, "visiting" | "done">();
|
|
318
|
+
const visit = (id: string, path: string[]): void => {
|
|
319
|
+
const mark = state.get(id);
|
|
320
|
+
if (mark === "done") return;
|
|
321
|
+
if (mark === "visiting") {
|
|
322
|
+
const cycle = [...path.slice(path.indexOf(id)), id]
|
|
323
|
+
.map((specId) => byId.get(specId)?.slug ?? specId)
|
|
324
|
+
.join(" -> ");
|
|
325
|
+
issues.push(`spec graph: dependency cycle ${cycle}`);
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
state.set(id, "visiting");
|
|
329
|
+
for (const dep of byId.get(id)?.depends_on ?? []) visit(dep, [...path, id]);
|
|
330
|
+
state.set(id, "done");
|
|
331
|
+
};
|
|
332
|
+
for (const spec of specs) visit(spec.id, []);
|
|
333
|
+
|
|
334
|
+
if (issues.length > 0) throw new SpecFormatError(issues);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function parseJson(text: string, file: string): unknown {
|
|
338
|
+
try {
|
|
339
|
+
return JSON.parse(text);
|
|
340
|
+
} catch (error) {
|
|
341
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
342
|
+
throw new SpecFormatError([`${file} is not valid JSON: ${reason}`]);
|
|
343
|
+
}
|
|
344
|
+
}
|
package/src/kit/status.test.ts
CHANGED
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
foldJournal,
|
|
14
14
|
foldReport,
|
|
15
15
|
foldTaskList,
|
|
16
|
+
limitWarning,
|
|
16
17
|
nudgeWarning,
|
|
17
18
|
stallWarning,
|
|
18
19
|
} from "./status.ts";
|
|
@@ -296,6 +297,72 @@ test("nudgeWarning fires only for a running node with an active nudge", () => {
|
|
|
296
297
|
expect(nudgeWarning(done)).toBeNull();
|
|
297
298
|
});
|
|
298
299
|
|
|
300
|
+
function runningWithLimitBackoff(
|
|
301
|
+
state: string,
|
|
302
|
+
heartbeatTs: number,
|
|
303
|
+
backoff: { probe: number; nextAttemptAt: number; waitedMs: number },
|
|
304
|
+
): NodeEvent {
|
|
305
|
+
return {
|
|
306
|
+
runId: R,
|
|
307
|
+
state,
|
|
308
|
+
scopePath: [],
|
|
309
|
+
status: {
|
|
310
|
+
phase: "running",
|
|
311
|
+
attempt: 1,
|
|
312
|
+
startedAt: heartbeatTs,
|
|
313
|
+
lastHeartbeat: heartbeatTs,
|
|
314
|
+
limitBackoff: backoff,
|
|
315
|
+
},
|
|
316
|
+
timestamp: heartbeatTs,
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
test("foldJournal surfaces a running node's limitBackoff, omits it when absent", () => {
|
|
321
|
+
const waiting = foldJournal(
|
|
322
|
+
journalText([
|
|
323
|
+
begin(),
|
|
324
|
+
runningWithLimitBackoff("Write", 2000, { probe: 1, nextAttemptAt: 2600, waitedMs: 600 }),
|
|
325
|
+
]),
|
|
326
|
+
);
|
|
327
|
+
expect(waiting.current?.limitBackoff).toEqual({ probe: 1, nextAttemptAt: 2600, waitedMs: 600 });
|
|
328
|
+
|
|
329
|
+
const plain = foldJournal(journalText([begin(), running("Write", 2000)]));
|
|
330
|
+
expect(plain.current?.limitBackoff).toBeUndefined();
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
test("limitWarning renders the probe countdown from the latest waiting heartbeat", () => {
|
|
334
|
+
const NOW = 3_600_000;
|
|
335
|
+
// 22 minutes until the next attempt; defaults give a 24h/30min = 48 total.
|
|
336
|
+
const digest = foldJournal(
|
|
337
|
+
journalText([
|
|
338
|
+
begin(),
|
|
339
|
+
running("Write", NOW - 8 * 60_000),
|
|
340
|
+
runningWithLimitBackoff("Write", NOW - 1_000, {
|
|
341
|
+
probe: 3,
|
|
342
|
+
nextAttemptAt: NOW + 22 * 60_000,
|
|
343
|
+
waitedMs: 90 * 60_000,
|
|
344
|
+
}),
|
|
345
|
+
]),
|
|
346
|
+
);
|
|
347
|
+
expect(limitWarning(digest, NOW)).toBe("usage limit — probe 3/48, next attempt in 22m");
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
test("limitWarning is null for a plain running heartbeat, and once the wait ends (latest running status has no limitBackoff)", () => {
|
|
351
|
+
const plain = foldJournal(journalText([begin(), running("Write", 2000)]));
|
|
352
|
+
expect(limitWarning(plain)).toBeNull();
|
|
353
|
+
|
|
354
|
+
// The post-wait re-dispatch's running status carries no limitBackoff — the
|
|
355
|
+
// warning must clear while the probe dispatch itself is running.
|
|
356
|
+
const probing = foldJournal(
|
|
357
|
+
journalText([
|
|
358
|
+
begin(),
|
|
359
|
+
runningWithLimitBackoff("Write", 2000, { probe: 1, nextAttemptAt: 2600, waitedMs: 600 }),
|
|
360
|
+
running("Write", 2600),
|
|
361
|
+
]),
|
|
362
|
+
);
|
|
363
|
+
expect(limitWarning(probing)).toBeNull();
|
|
364
|
+
});
|
|
365
|
+
|
|
299
366
|
test("foldJournal counts distinct visits, not retry attempts", () => {
|
|
300
367
|
// Write visited once (two attempts), then Test visited once
|
|
301
368
|
const events = [
|
package/src/kit/status.ts
CHANGED
|
@@ -22,15 +22,19 @@ export interface JournalDigest {
|
|
|
22
22
|
* don't report one. */
|
|
23
23
|
agentSessionId?: string;
|
|
24
24
|
/** Present while the engine is nudging this attempt (1..3) — see
|
|
25
|
-
* `nudgeWarning` and docs/specs/session-nudge/spec.md. */
|
|
25
|
+
* `nudgeWarning` and docs/specs/_built/session-nudge/spec.md. */
|
|
26
26
|
nudgeAttempt?: number;
|
|
27
27
|
/** The nudgeable `CrashReason` kind that triggered the current nudge. */
|
|
28
28
|
nudgeReason?: CrashReason["kind"];
|
|
29
29
|
/** Latest agent-reported step count, when the runner reports progress —
|
|
30
|
-
* see `agentStallWarning` and docs/specs/agent-liveness/spec.md. */
|
|
30
|
+
* see `agentStallWarning` and docs/specs/_built/agent-liveness/spec.md. */
|
|
31
31
|
agentStepCount?: number;
|
|
32
32
|
/** Milliseconds idle as of the last heartbeat — see `agentStallWarning`. */
|
|
33
33
|
agentIdleMs?: number;
|
|
34
|
+
/** Present while the engine is waiting out a provider usage-limit
|
|
35
|
+
* backoff for the current attempt — see `limitWarning` and
|
|
36
|
+
* docs/specs/_built/usage-limit-backoff/spec.md. */
|
|
37
|
+
limitBackoff?: { probe: number; nextAttemptAt: number; waitedMs: number };
|
|
34
38
|
} | null;
|
|
35
39
|
crash?: { attempt: number; reason: CrashReason };
|
|
36
40
|
visits: Record<string, number>;
|
|
@@ -88,6 +92,7 @@ export function foldJournal(journalText: string): JournalDigest {
|
|
|
88
92
|
nudgeReason: lastStatus.nudgeReason,
|
|
89
93
|
agentStepCount: lastStatus.agentStepCount,
|
|
90
94
|
agentIdleMs: lastStatus.agentIdleMs,
|
|
95
|
+
limitBackoff: lastStatus.limitBackoff,
|
|
91
96
|
}
|
|
92
97
|
: {}),
|
|
93
98
|
};
|
|
@@ -115,7 +120,7 @@ export function foldJournal(journalText: string): JournalDigest {
|
|
|
115
120
|
return digest;
|
|
116
121
|
}
|
|
117
122
|
|
|
118
|
-
// --- task lists (docs/specs/task-list-json/spec.md) ---
|
|
123
|
+
// --- task lists (docs/specs/_built/task-list-json/spec.md) ---
|
|
119
124
|
|
|
120
125
|
export interface TaskListDigest {
|
|
121
126
|
/** Strict count: only `done` tasks — pending, in-progress, and failed all
|
|
@@ -285,7 +290,7 @@ export function stallWarning(digest: JournalDigest, now: number = Date.now()): s
|
|
|
285
290
|
}
|
|
286
291
|
|
|
287
292
|
// Placeholder default, not yet measured against real runs — see
|
|
288
|
-
// docs/specs/agent-liveness/spec.md's open questions (a long tool-execution
|
|
293
|
+
// docs/specs/_built/agent-liveness/spec.md's open questions (a long tool-execution
|
|
289
294
|
// gap, e.g. a 10-minute build, looks identical to a hung provider; the
|
|
290
295
|
// warning's step count is what lets the supervisor tell them apart).
|
|
291
296
|
export const AGENT_STALL_MS = 15 * 60_000;
|
|
@@ -321,3 +326,23 @@ export function nudgeWarning(digest: JournalDigest): string | null {
|
|
|
321
326
|
if (digest.current?.phase !== "running" || nudgeAttempt === undefined) return null;
|
|
322
327
|
return `nudging attempt ${nudgeAttempt}/3 (${nudgeReason}) — supervisor decides`;
|
|
323
328
|
}
|
|
329
|
+
|
|
330
|
+
// Display-only usage-limit config defaults (docs/specs/_built/usage-limit-backoff/
|
|
331
|
+
// spec.md), mirroring how this module already treats other engine constants
|
|
332
|
+
// as display knowledge, not a shared dependency. The denominator assumes the
|
|
333
|
+
// defaults; a leaf overriding usageLimitRetryMs/usageLimitMaxWaitMs renders
|
|
334
|
+
// a best-effort total until leaf config is recorded in the journal.
|
|
335
|
+
const USAGE_LIMIT_RETRY_MS = 30 * 60_000;
|
|
336
|
+
const USAGE_LIMIT_MAX_WAIT_MS = 24 * 60 * 60_000;
|
|
337
|
+
|
|
338
|
+
/** Warn-only usage-limit wait note, same contract as `stallWarning`: folded
|
|
339
|
+
* from the latest running status's `limitBackoff` — present only while the
|
|
340
|
+
* engine sleeps between usage-limit re-probes, so the supervisor sees the
|
|
341
|
+
* wait instead of silence. Purely a display string, never acts. */
|
|
342
|
+
export function limitWarning(digest: JournalDigest, now: number = Date.now()): string | null {
|
|
343
|
+
const backoff = digest.current?.limitBackoff;
|
|
344
|
+
if (digest.current?.phase !== "running" || backoff === undefined) return null;
|
|
345
|
+
const totalProbes = Math.ceil(USAGE_LIMIT_MAX_WAIT_MS / USAGE_LIMIT_RETRY_MS);
|
|
346
|
+
const minutes = Math.max(0, Math.round((backoff.nextAttemptAt - now) / 60_000));
|
|
347
|
+
return `usage limit — probe ${backoff.probe}/${totalProbes}, next attempt in ${minutes}m`;
|
|
348
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { zodOutputType } from "./zod.ts";
|
|
4
|
+
|
|
5
|
+
function expectFailure(result: { success: true } | { success: false; message: string }): string {
|
|
6
|
+
if (result.success) throw new Error("expected a failing parse");
|
|
7
|
+
return result.message;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
describe("zodOutputType", () => {
|
|
11
|
+
test("a passing schema returns { success: true }, with no message field", () => {
|
|
12
|
+
const outputType = zodOutputType(z.object({ title: z.string() }));
|
|
13
|
+
const result = outputType.safeParse({ title: "hi" });
|
|
14
|
+
expect(result).toEqual({ success: true });
|
|
15
|
+
expect("message" in result).toBe(false);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("a failing schema with one bad field returns one issue, its path joined by dots", () => {
|
|
19
|
+
const outputType = zodOutputType(z.object({ tasks: z.array(z.object({ title: z.string() })) }));
|
|
20
|
+
const message = expectFailure(
|
|
21
|
+
outputType.safeParse({ tasks: [{ title: "a" }, { title: "b" }, {}] }),
|
|
22
|
+
);
|
|
23
|
+
// path ["tasks", 2, "title"] -> "tasks.2.title", one issue only (no "; " join)
|
|
24
|
+
expect(message.startsWith("tasks.2.title: ")).toBe(true);
|
|
25
|
+
expect(message).not.toContain("; ");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("a root-level failure, with no path, falls back to the word `output`", () => {
|
|
29
|
+
const outputType = zodOutputType(z.string());
|
|
30
|
+
const message = expectFailure(outputType.safeParse(42));
|
|
31
|
+
expect(message.startsWith("output: ")).toBe(true);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("a schema with two failing fields returns every issue, joined together, not just the first", () => {
|
|
35
|
+
const outputType = zodOutputType(z.object({ summary: z.string(), count: z.number() }));
|
|
36
|
+
const message = expectFailure(outputType.safeParse({ summary: 1, count: "two" }));
|
|
37
|
+
const parts = message.split("; ");
|
|
38
|
+
expect(parts).toHaveLength(2);
|
|
39
|
+
expect(parts[0].startsWith("summary: ")).toBe(true);
|
|
40
|
+
expect(parts[1].startsWith("count: ")).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
import type { OutputSchema } from "../../core/index.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Adapter from a Zod schema to the engine's `OutputSchema` interface
|
|
6
|
+
* (`AgentStateDef.outputType`). Joins every issue from one failed parse
|
|
7
|
+
* into a single message, so one nudge can name all failing fields.
|
|
8
|
+
*/
|
|
9
|
+
export function zodOutputType<T>(schema: z.ZodType<T>): OutputSchema {
|
|
10
|
+
return {
|
|
11
|
+
safeParse: (value) => {
|
|
12
|
+
const result = schema.safeParse(value);
|
|
13
|
+
if (result.success) return { success: true };
|
|
14
|
+
const message = result.error.issues
|
|
15
|
+
.map((issue) => `${issue.path.join(".") || "output"}: ${issue.message}`)
|
|
16
|
+
.join("; ");
|
|
17
|
+
return { success: false, message };
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -5,17 +5,20 @@ import type { DispatchInfo } from "@psimona/darkf/src/core/index.ts";
|
|
|
5
5
|
// rides along on the branch — Finish commits it there before the squash.
|
|
6
6
|
// The slug is a workflow-file constant for the pilot; editing this file per
|
|
7
7
|
// feature is the contract until engine-side input plumbing exists.
|
|
8
|
-
const FEATURE = "
|
|
8
|
+
const FEATURE = "opencode-overlay-tiles";
|
|
9
9
|
|
|
10
|
-
function sh(...args: string[]): [string, number] {
|
|
11
|
-
const proc = Bun.spawnSync(args, { stdout: "pipe", stderr: "pipe" });
|
|
10
|
+
function sh(cwd: string, ...args: string[]): [string, number] {
|
|
11
|
+
const proc = Bun.spawnSync(args, { cwd, stdout: "pipe", stderr: "pipe" });
|
|
12
12
|
return [`${proc.stdout?.toString() ?? ""}${proc.stderr?.toString() ?? ""}`, proc.exitCode ?? 1];
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export default function begin(_handoff: unknown,
|
|
15
|
+
export default function begin(_handoff: unknown, info: DispatchInfo) {
|
|
16
16
|
const target = `feature/${FEATURE}`;
|
|
17
|
-
const [, exists] = sh("git", "rev-parse", "--verify", target);
|
|
18
|
-
const co =
|
|
17
|
+
const [, exists] = sh(info.rootDir, "git", "rev-parse", "--verify", target);
|
|
18
|
+
const co =
|
|
19
|
+
exists === 0
|
|
20
|
+
? sh(info.rootDir, "git", "checkout", target)
|
|
21
|
+
: sh(info.rootDir, "git", "checkout", "-b", target);
|
|
19
22
|
return co[1] === 0
|
|
20
23
|
? { event: "started", output: { branch: target, reused: exists === 0 } }
|
|
21
24
|
: { event: "branch_failed", output: { reason: co[0] } };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import {
|
|
4
|
+
type DispatchInfo,
|
|
5
|
+
type NodeResult,
|
|
6
|
+
parseTaskList,
|
|
7
|
+
} from "@psimona/darkf/src/core/index.ts";
|
|
8
|
+
|
|
9
|
+
// The node that walks the plan — darkf's item-loop-convention shape
|
|
10
|
+
// (docs/specs/item-loop-convention/spec.md), hand-rolled here because that
|
|
11
|
+
// spec's own darkf-core/kit helper isn't shipped yet; this follows its
|
|
12
|
+
// algorithm and vocabulary directly (pending → in-progress, picked once
|
|
13
|
+
// and re-verified by Review rather than trusted, status written back
|
|
14
|
+
// before the real work starts) without importing anything from it.
|
|
15
|
+
//
|
|
16
|
+
// Picks the first "pending" task out of this run's tasks.json, flips it
|
|
17
|
+
// "in-progress", and hands it to Implement as the one thing to build this
|
|
18
|
+
// pass. Re-entered once per task — by Review approving one, via MarkDone
|
|
19
|
+
// — until the plan is exhausted.
|
|
20
|
+
export default async function dispatch(
|
|
21
|
+
_handoff: NodeResult,
|
|
22
|
+
info: DispatchInfo,
|
|
23
|
+
): Promise<NodeResult> {
|
|
24
|
+
const path = join(info.rootDir, ".darkf", "run", info.runId, "tasks.json");
|
|
25
|
+
const parsed = parseTaskList(JSON.parse(await readFile(path, "utf8")));
|
|
26
|
+
if ("error" in parsed) {
|
|
27
|
+
return { event: "blocked", output: { reason: `tasks.json is invalid: ${parsed.error}` } };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const next = parsed.tasks.find((t) => t.status === "pending");
|
|
31
|
+
if (!next) {
|
|
32
|
+
const failed = parsed.tasks.filter((t) => t.status === "failed");
|
|
33
|
+
if (failed.length > 0) {
|
|
34
|
+
return {
|
|
35
|
+
event: "blocked",
|
|
36
|
+
output: { reason: `task(s) failed: ${failed.map((t) => t.id).join(", ")}` },
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
return { event: "plan_exhausted", output: {} };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
next.status = "in-progress";
|
|
43
|
+
await writeFile(path, JSON.stringify(parsed, null, 2));
|
|
44
|
+
|
|
45
|
+
const done = parsed.tasks.filter((t) => t.status === "done").length;
|
|
46
|
+
return {
|
|
47
|
+
event: "next_item",
|
|
48
|
+
output: { task: next, itemIndex: done + 1, itemsTotal: parsed.tasks.length },
|
|
49
|
+
};
|
|
50
|
+
}
|