@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
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
type ProjectConfig,
|
|
4
|
+
parseBacklog,
|
|
5
|
+
parseBacklogJson,
|
|
6
|
+
parseProjectConfig,
|
|
7
|
+
parseProjectJson,
|
|
8
|
+
parseSpecDocument,
|
|
9
|
+
parseSpecFrontmatter,
|
|
10
|
+
SPEC_FORMAT_VERSION,
|
|
11
|
+
SpecFormatError,
|
|
12
|
+
validateSpecGraph,
|
|
13
|
+
} from "./specs.ts";
|
|
14
|
+
|
|
15
|
+
const ID = "a8169a9c-1a53-4e0b-8a55-1318fef44bce";
|
|
16
|
+
const DEP = "0b9f8c2e-51a7-4c3d-9a11-7f6e5d4c3b2a";
|
|
17
|
+
|
|
18
|
+
function issuesOf(run: () => unknown): string[] {
|
|
19
|
+
try {
|
|
20
|
+
run();
|
|
21
|
+
} catch (error) {
|
|
22
|
+
expect(error).toBeInstanceOf(SpecFormatError);
|
|
23
|
+
return (error as SpecFormatError).issues;
|
|
24
|
+
}
|
|
25
|
+
throw new Error("expected a SpecFormatError");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe("parseSpecFrontmatter", () => {
|
|
29
|
+
test("accepts the minimal shape", () => {
|
|
30
|
+
expect(parseSpecFrontmatter({ id: ID, slug: "billing-v2", owner: "alice" })).toEqual({
|
|
31
|
+
id: ID,
|
|
32
|
+
slug: "billing-v2",
|
|
33
|
+
owner: "alice",
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("accepts dependencies", () => {
|
|
38
|
+
const spec = parseSpecFrontmatter({
|
|
39
|
+
id: ID,
|
|
40
|
+
slug: "cutover",
|
|
41
|
+
owner: "alice",
|
|
42
|
+
depends_on: [DEP],
|
|
43
|
+
});
|
|
44
|
+
expect(spec.depends_on).toEqual([DEP]);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("rejects unknown fields", () => {
|
|
48
|
+
expect(
|
|
49
|
+
issuesOf(() => parseSpecFrontmatter({ id: ID, slug: "x", owner: "a", priority: 1 })),
|
|
50
|
+
).toEqual(['frontmatter: unknown field "priority"']);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("rejects a non-object", () => {
|
|
54
|
+
expect(issuesOf(() => parseSpecFrontmatter("nope"))).toEqual([
|
|
55
|
+
"frontmatter must be a YAML object",
|
|
56
|
+
]);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("rejects a missing or non-v4 id", () => {
|
|
60
|
+
expect(issuesOf(() => parseSpecFrontmatter({ slug: "x", owner: "a" }))).toContain(
|
|
61
|
+
'frontmatter: "id" must be a UUIDv4 string',
|
|
62
|
+
);
|
|
63
|
+
expect(
|
|
64
|
+
issuesOf(() => parseSpecFrontmatter({ id: "not-a-uuid", slug: "x", owner: "a" })),
|
|
65
|
+
).toContain('frontmatter: "id" must be a UUIDv4 string');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("rejects a bad slug and an empty owner", () => {
|
|
69
|
+
const issues = issuesOf(() =>
|
|
70
|
+
parseSpecFrontmatter({ id: ID, slug: "Not_A_Slug", owner: " " }),
|
|
71
|
+
);
|
|
72
|
+
expect(issues).toContain('frontmatter: "slug" must be lowercase kebab-case');
|
|
73
|
+
expect(issues).toContain('frontmatter: "owner" must be a non-empty string');
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("rejects duplicate and self dependencies", () => {
|
|
77
|
+
expect(
|
|
78
|
+
issuesOf(() =>
|
|
79
|
+
parseSpecFrontmatter({ id: ID, slug: "x", owner: "a", depends_on: [DEP, DEP] }),
|
|
80
|
+
),
|
|
81
|
+
).toContain(`frontmatter: depends_on: duplicate "${DEP}"`);
|
|
82
|
+
expect(
|
|
83
|
+
issuesOf(() => parseSpecFrontmatter({ id: ID, slug: "x", owner: "a", depends_on: [ID] })),
|
|
84
|
+
).toContain("frontmatter: a spec cannot depend on itself");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("rejects non-uuid dependency entries", () => {
|
|
88
|
+
expect(
|
|
89
|
+
issuesOf(() => parseSpecFrontmatter({ id: ID, slug: "x", owner: "a", depends_on: ["nope"] })),
|
|
90
|
+
).toContain('frontmatter: depends_on entry "nope" is not a UUIDv4');
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe("parseSpecDocument", () => {
|
|
95
|
+
test("splits frontmatter and body", () => {
|
|
96
|
+
const text = `---\nid: ${ID}\nslug: billing-v2\nowner: alice\n---\n\n# Billing\n\nBody.\n`;
|
|
97
|
+
const doc = parseSpecDocument(text);
|
|
98
|
+
expect(doc.frontmatter).toEqual({ id: ID, slug: "billing-v2", owner: "alice" });
|
|
99
|
+
expect(doc.body.trim()).toBe("# Billing\n\nBody.");
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("rejects a missing frontmatter block", () => {
|
|
103
|
+
expect(issuesOf(() => parseSpecDocument("# No frontmatter\n"))).toEqual([
|
|
104
|
+
"spec.md must start with a --- frontmatter block",
|
|
105
|
+
]);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("wraps YAML errors", () => {
|
|
109
|
+
const issues = issuesOf(() => parseSpecDocument("---\nkey: [unclosed\n---\n"));
|
|
110
|
+
expect(issues[0]).toStartWith("frontmatter is not valid YAML:");
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe("parseBacklog", () => {
|
|
115
|
+
test("accepts an ordered list", () => {
|
|
116
|
+
expect(parseBacklog({ version: 1, order: [ID, DEP] })).toEqual({
|
|
117
|
+
version: 1,
|
|
118
|
+
order: [ID, DEP],
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test("accepts an empty order", () => {
|
|
123
|
+
expect(parseBacklog({ version: 1, order: [] })).toEqual({ version: 1, order: [] });
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("rejects a version mismatch and unknown fields", () => {
|
|
127
|
+
const issues = issuesOf(() => parseBacklog({ version: 2, order: [], extra: true }));
|
|
128
|
+
expect(issues).toContain(`backlog: "version" must be ${SPEC_FORMAT_VERSION}`);
|
|
129
|
+
expect(issues).toContain('backlog: unknown field "extra"');
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test("rejects duplicates and non-uuid entries", () => {
|
|
133
|
+
const issues = issuesOf(() => parseBacklog({ version: 1, order: [ID, ID, "nope"] }));
|
|
134
|
+
expect(issues).toContain(`backlog: order: duplicate "${ID}"`);
|
|
135
|
+
expect(issues).toContain('backlog: order entry "nope" is not a UUIDv4');
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test("parseBacklogJson wraps JSON errors", () => {
|
|
139
|
+
expect(issuesOf(() => parseBacklogJson("{"))[0]).toStartWith("backlog.json is not valid JSON:");
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
describe("parseProjectConfig", () => {
|
|
144
|
+
const full: ProjectConfig = {
|
|
145
|
+
version: 1,
|
|
146
|
+
title: "darkf backplane",
|
|
147
|
+
harness: { runner: "opencode", model: "openai/gpt-5" },
|
|
148
|
+
sandbox: {
|
|
149
|
+
image: "ghcr.io/psimona/backplane-ci:latest",
|
|
150
|
+
runtimeClass: "gvisor",
|
|
151
|
+
mounts: [{ name: "bun-cache", mountPath: "/root/.bun", size: "10Gi" }],
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
test("accepts the full shape", () => {
|
|
156
|
+
expect(parseProjectConfig(full)).toEqual(full);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test("accepts the minimal shape", () => {
|
|
160
|
+
expect(parseProjectConfig({ version: 1 })).toEqual({ version: 1 });
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test("rejects unknown fields at every level", () => {
|
|
164
|
+
const issues = issuesOf(() =>
|
|
165
|
+
parseProjectConfig({
|
|
166
|
+
version: 1,
|
|
167
|
+
extra: true,
|
|
168
|
+
harness: { runner: "opencode", apiKey: "x" },
|
|
169
|
+
sandbox: {
|
|
170
|
+
image: "i",
|
|
171
|
+
privileged: true,
|
|
172
|
+
mounts: [{ name: "a", mountPath: "/a", mode: "rw" }],
|
|
173
|
+
},
|
|
174
|
+
}),
|
|
175
|
+
);
|
|
176
|
+
expect(issues).toContain('project: unknown field "extra"');
|
|
177
|
+
expect(issues).toContain('project.harness: unknown field "apiKey"');
|
|
178
|
+
expect(issues).toContain('project.sandbox: unknown field "privileged"');
|
|
179
|
+
expect(issues).toContain('project.sandbox.mounts[0]: unknown field "mode"');
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test("requires a runner when a harness is present", () => {
|
|
183
|
+
expect(issuesOf(() => parseProjectConfig({ version: 1, harness: {} }))).toContain(
|
|
184
|
+
'project.harness: "runner" must be a non-empty string',
|
|
185
|
+
);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test("requires absolute mount paths", () => {
|
|
189
|
+
const issues = issuesOf(() =>
|
|
190
|
+
parseProjectConfig({
|
|
191
|
+
version: 1,
|
|
192
|
+
sandbox: { mounts: [{ name: "a", mountPath: "relative" }] },
|
|
193
|
+
}),
|
|
194
|
+
);
|
|
195
|
+
expect(issues).toContain('project.sandbox.mounts[0]: "mountPath" must be an absolute path');
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test("parseProjectJson wraps JSON errors", () => {
|
|
199
|
+
expect(issuesOf(() => parseProjectJson("nope"))[0]).toStartWith(
|
|
200
|
+
"project.json is not valid JSON:",
|
|
201
|
+
);
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
describe("validateSpecGraph", () => {
|
|
206
|
+
const base = { owner: "alice" };
|
|
207
|
+
|
|
208
|
+
test("accepts a valid graph", () => {
|
|
209
|
+
expect(() =>
|
|
210
|
+
validateSpecGraph([
|
|
211
|
+
{ ...base, id: ID, slug: "parent" },
|
|
212
|
+
{ ...base, id: DEP, slug: "child", depends_on: [ID] },
|
|
213
|
+
]),
|
|
214
|
+
).not.toThrow();
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test("rejects duplicate ids and slugs", () => {
|
|
218
|
+
const issues = issuesOf(() =>
|
|
219
|
+
validateSpecGraph([
|
|
220
|
+
{ ...base, id: ID, slug: "same" },
|
|
221
|
+
{ ...base, id: ID, slug: "same" },
|
|
222
|
+
]),
|
|
223
|
+
);
|
|
224
|
+
expect(issues).toContain(`spec graph: id: duplicate "${ID}"`);
|
|
225
|
+
expect(issues).toContain('spec graph: slug: duplicate "same"');
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
test("rejects missing dependency targets", () => {
|
|
229
|
+
expect(
|
|
230
|
+
issuesOf(() => validateSpecGraph([{ ...base, id: ID, slug: "child", depends_on: [DEP] }])),
|
|
231
|
+
).toContain(
|
|
232
|
+
'spec child: depends_on target "0b9f8c2e-51a7-4c3d-9a11-7f6e5d4c3b2a" does not exist',
|
|
233
|
+
);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
test("rejects cycles", () => {
|
|
237
|
+
const third = "11111111-1111-4111-8111-111111111111";
|
|
238
|
+
const issues = issuesOf(() =>
|
|
239
|
+
validateSpecGraph([
|
|
240
|
+
{ ...base, id: ID, slug: "a", depends_on: [DEP] },
|
|
241
|
+
{ ...base, id: DEP, slug: "b", depends_on: [third] },
|
|
242
|
+
{ ...base, id: third, slug: "c", depends_on: [ID] },
|
|
243
|
+
]),
|
|
244
|
+
);
|
|
245
|
+
expect(issues).toContain("spec graph: dependency cycle a -> b -> c -> a");
|
|
246
|
+
});
|
|
247
|
+
});
|
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
|
@@ -478,6 +478,30 @@ describe("foldReport", () => {
|
|
|
478
478
|
expect(report.visits).toEqual({ Implement: 1, Verify: 1 });
|
|
479
479
|
});
|
|
480
480
|
|
|
481
|
+
test("copies the running event's runner fields onto succeeded and crashed rows", () => {
|
|
482
|
+
const ran = (state: string, ts: number, fields: { runner?: string; runnerKind?: string }) => {
|
|
483
|
+
const event = running(state, ts);
|
|
484
|
+
return { ...event, status: { ...event.status, ...fields } } as NodeEvent;
|
|
485
|
+
};
|
|
486
|
+
const journal = journalText([
|
|
487
|
+
begin(),
|
|
488
|
+
ran("Plan", 100, { runner: "claude", runnerKind: "claude-code" }),
|
|
489
|
+
succeeded("Plan", 200),
|
|
490
|
+
ran("Build", 200, { runnerKind: "opencode" }),
|
|
491
|
+
succeeded("Build", 300),
|
|
492
|
+
ran("Review", 300, { runner: "custom" }),
|
|
493
|
+
crashed("Review", 400, { kind: "malformed-result" }),
|
|
494
|
+
]);
|
|
495
|
+
|
|
496
|
+
expect(
|
|
497
|
+
foldReport(journal).dispatches.map((d) => [d.state, d.runner, d.runnerKind, d.durationMs]),
|
|
498
|
+
).toEqual([
|
|
499
|
+
["Plan", "claude", "claude-code", 100],
|
|
500
|
+
["Build", undefined, "opencode", 100],
|
|
501
|
+
["Review", "custom", undefined, 100],
|
|
502
|
+
]);
|
|
503
|
+
});
|
|
504
|
+
|
|
481
505
|
test("a FAILED-tagged end marker is its own dispatch row, not silently dropped", () => {
|
|
482
506
|
const journal = journalText([
|
|
483
507
|
begin(),
|