@psimona/darkf 0.4.0 → 0.6.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 +164 -0
- package/docs/concepts/handoff.md +91 -0
- package/docs/concepts/journal.md +92 -0
- package/docs/concepts/node-types.md +175 -0
- package/docs/guides/pick-an-agent-runner.md +57 -0
- package/docs/guides/plan-with-a-task-queue.md +410 -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 +95 -0
- package/docs/index.mdx +64 -0
- package/docs/reference/cli-commands.md +105 -0
- package/docs/reference/core-types.md +123 -0
- package/docs/reference/package-map.md +35 -0
- package/docs/start-here/install.md +71 -0
- package/docs/start-here/quickstart.md +241 -0
- package/package.json +7 -1
- package/skills/darkf-usage/SKILL.md +92 -0
- package/skills/darkf-writing-workflows/SKILL.md +88 -0
- package/src/cli/doctor.test.ts +338 -0
- package/src/cli/index.ts +123 -9
- package/src/cli/setup.test.ts +384 -0
- package/src/cli/setup.ts +286 -0
- package/src/cli/tarball.test.ts +125 -0
- package/src/cli/update.test.ts +82 -0
- package/src/cli/update.ts +20 -0
- package/src/core/index.ts +1 -0
- package/src/core/tasks.test.ts +146 -0
- package/src/core/tasks.ts +158 -0
- package/src/core/types.ts +42 -0
- package/src/engine/run.test.ts +177 -0
- package/src/engine/run.ts +66 -2
- package/src/kit/agents/opencode.test.ts +232 -2
- package/src/kit/agents/opencode.ts +69 -6
- package/src/kit/status.test.ts +118 -1
- package/src/kit/status.ts +67 -1
- package/workflows/dev-loop/scripts/begin.ts +1 -1
- package/workflows/dev-loop/scripts/finish.ts +13 -2
- package/workflows/dev-loop/workflow.ts +57 -23
- 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/tsconfig.json +0 -4
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { expect, test } from "bun:test";
|
|
2
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join, resolve } from "node:path";
|
|
5
|
+
import { todayLocal, writeProbeState } from "./setup.ts";
|
|
6
|
+
|
|
7
|
+
// Tarball smoke test (skill-setup spec: Testing): npm pack the package, then
|
|
8
|
+
// install the tarball into a temp prefix and run the installed CLI — proving
|
|
9
|
+
// the files allowlist ships every runtime dir (src, workflows, skills, docs)
|
|
10
|
+
// and nothing the CLI reads was dropped. Skipped when npm is absent.
|
|
11
|
+
|
|
12
|
+
const pkgDir = resolve(import.meta.dir, "..", "..");
|
|
13
|
+
|
|
14
|
+
test("tarball smoke: allowlist ships the runtime dirs and the installed CLI works", async () => {
|
|
15
|
+
const npm = Bun.which("npm");
|
|
16
|
+
if (!npm) {
|
|
17
|
+
console.warn("npm not on PATH — skipping tarball smoke test");
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const root = mkdtempSync(join(tmpdir(), "darkf-tarball-"));
|
|
22
|
+
try {
|
|
23
|
+
// Pack and inspect the tarball's contents.
|
|
24
|
+
const pack = Bun.spawnSync([npm, "pack", "--json", "--pack-destination", root], {
|
|
25
|
+
cwd: pkgDir,
|
|
26
|
+
stdout: "pipe",
|
|
27
|
+
stderr: "pipe",
|
|
28
|
+
});
|
|
29
|
+
expect(pack.exitCode).toBe(0);
|
|
30
|
+
const filename = (
|
|
31
|
+
JSON.parse(new TextDecoder().decode(pack.stdout)) as Array<{ filename: string }>
|
|
32
|
+
)[0]?.filename;
|
|
33
|
+
expect(filename).toBeTruthy();
|
|
34
|
+
const tgz = join(root, filename!);
|
|
35
|
+
const list = Bun.spawnSync(["tar", "-tzf", tgz], { stdout: "pipe", stderr: "pipe" });
|
|
36
|
+
expect(list.exitCode).toBe(0);
|
|
37
|
+
const entries = new TextDecoder()
|
|
38
|
+
.decode(list.stdout)
|
|
39
|
+
.split("\n")
|
|
40
|
+
.filter((l) => l.startsWith("package/"));
|
|
41
|
+
const top = new Set(entries.map((l) => l.split("/")[1]).filter(Boolean));
|
|
42
|
+
// Exactly the allowlist plus the auto-included package.json and README.
|
|
43
|
+
expect(top).toEqual(
|
|
44
|
+
new Set(["README.md", "docs", "package.json", "skills", "src", "workflows"]),
|
|
45
|
+
);
|
|
46
|
+
const all = entries.join("\n");
|
|
47
|
+
expect(all).not.toContain("package/examples/");
|
|
48
|
+
expect(all).not.toContain("bunfig.toml");
|
|
49
|
+
expect(all).not.toContain("tsconfig.json");
|
|
50
|
+
expect(all).not.toContain("package/feedback/");
|
|
51
|
+
|
|
52
|
+
// Install into a temp prefix — the offline global-install shape.
|
|
53
|
+
const prefix = join(root, "prefix");
|
|
54
|
+
const install = Bun.spawnSync(
|
|
55
|
+
[npm, "install", "-g", "--prefix", prefix, "--no-audit", "--no-fund", tgz],
|
|
56
|
+
{ stdout: "pipe", stderr: "pipe" },
|
|
57
|
+
);
|
|
58
|
+
expect(install.exitCode).toBe(0);
|
|
59
|
+
const libRoot = join(prefix, "lib", "node_modules", "@psimona", "darkf");
|
|
60
|
+
expect(existsSync(join(libRoot, "src", "cli", "index.ts"))).toBe(true);
|
|
61
|
+
expect(existsSync(join(prefix, "bin", "darkf"))).toBe(true);
|
|
62
|
+
|
|
63
|
+
// Temp HOME and state; the probe stamped so its block stays out of the way.
|
|
64
|
+
const home = join(root, "home");
|
|
65
|
+
mkdirSync(home);
|
|
66
|
+
const stateHome = join(root, "state");
|
|
67
|
+
mkdirSync(stateHome, { recursive: true });
|
|
68
|
+
writeProbeState(join(stateHome, "darkf", "last-skills-check.json"), {
|
|
69
|
+
lastProbe: todayLocal(),
|
|
70
|
+
});
|
|
71
|
+
const env = {
|
|
72
|
+
...process.env,
|
|
73
|
+
HOME: home,
|
|
74
|
+
XDG_STATE_HOME: stateHome,
|
|
75
|
+
DARKF_NO_UPDATE_CHECK: "1",
|
|
76
|
+
};
|
|
77
|
+
const runBin = (args: string[]) => {
|
|
78
|
+
const proc = Bun.spawnSync(
|
|
79
|
+
[process.execPath, join(libRoot, "src", "cli", "index.ts"), ...args],
|
|
80
|
+
{
|
|
81
|
+
cwd: root,
|
|
82
|
+
stdout: "pipe",
|
|
83
|
+
stderr: "pipe",
|
|
84
|
+
env,
|
|
85
|
+
},
|
|
86
|
+
);
|
|
87
|
+
return {
|
|
88
|
+
code: proc.exitCode,
|
|
89
|
+
out: new TextDecoder().decode(proc.stdout),
|
|
90
|
+
err: new TextDecoder().decode(proc.stderr),
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// `setup` installs both skills into a detected host.
|
|
95
|
+
const setup = runBin(["setup"]);
|
|
96
|
+
expect(setup.code).toBe(0);
|
|
97
|
+
expect(setup.out).toContain("host claude (~/.claude/skills)");
|
|
98
|
+
expect(existsSync(join(home, ".claude", "skills", "darkf-usage", "SKILL.md"))).toBe(true);
|
|
99
|
+
expect(existsSync(join(home, ".claude", "skills", "darkf-writing-workflows", "SKILL.md"))).toBe(
|
|
100
|
+
true,
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
// `doctor` renders the skills section from the installed package's bundle.
|
|
104
|
+
const doctor = runBin(["doctor"]);
|
|
105
|
+
expect(doctor.out).toContain("skills (claude) — 2/2 installed");
|
|
106
|
+
|
|
107
|
+
// `render` loads a built-in workflow from the packaged workflows/.
|
|
108
|
+
const render = runBin(["render", "series-ordering"]);
|
|
109
|
+
expect(render.code).toBe(0);
|
|
110
|
+
expect(render.out).toContain("stateDiagram-v2");
|
|
111
|
+
|
|
112
|
+
// Every skill-referenced docs path resolves inside the installed package,
|
|
113
|
+
// offline — the deep-reference pointers work for a global install.
|
|
114
|
+
for (const skill of ["darkf-usage", "darkf-writing-workflows"]) {
|
|
115
|
+
const text = readFileSync(join(libRoot, "skills", skill, "SKILL.md"), "utf8");
|
|
116
|
+
const refs = [...text.matchAll(/docs\/[A-Za-z0-9][A-Za-z0-9/._-]*\.md/g)].map((m) => m[0]!);
|
|
117
|
+
expect(refs.length).toBeGreaterThan(0);
|
|
118
|
+
for (const ref of refs) {
|
|
119
|
+
expect(existsSync(join(libRoot, ref))).toBe(true);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
} finally {
|
|
123
|
+
rmSync(root, { recursive: true, force: true });
|
|
124
|
+
}
|
|
125
|
+
}, 240_000);
|
package/src/cli/update.test.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { afterAll, expect, test } from "bun:test";
|
|
|
2
2
|
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import { dirname, join, resolve } from "node:path";
|
|
5
|
+
import type { SkillsDeps } from "./setup.ts";
|
|
5
6
|
import {
|
|
6
7
|
checkRegistry,
|
|
7
8
|
isDue,
|
|
@@ -10,6 +11,7 @@ import {
|
|
|
10
11
|
type UpdateDeps,
|
|
11
12
|
updateGate,
|
|
12
13
|
upgradeCmd,
|
|
14
|
+
upgradeWithSkills,
|
|
13
15
|
writeState,
|
|
14
16
|
} from "./update.ts";
|
|
15
17
|
|
|
@@ -484,3 +486,83 @@ test("real CLI with a fresh not-due record: the notice is the first stdout line,
|
|
|
484
486
|
`darkf 99.0.0 available (installed ${installedVersion}) — run 'darkf upgrade'`,
|
|
485
487
|
);
|
|
486
488
|
});
|
|
489
|
+
|
|
490
|
+
// --- upgrade + skills sync (skill-setup spec) ---
|
|
491
|
+
|
|
492
|
+
const SKILL_BUNDLED = "bundled skill body\n";
|
|
493
|
+
const SKILL_STALE = "a stale installed copy\n";
|
|
494
|
+
|
|
495
|
+
/** Fixture skills bundle + a home with a stale claude copy installed. */
|
|
496
|
+
function skillsFixture(): { skills: SkillsDeps; installed: string } {
|
|
497
|
+
const bundledDir = tempDir();
|
|
498
|
+
mkdirSync(join(bundledDir, "darkf-usage"), { recursive: true });
|
|
499
|
+
writeFileSync(join(bundledDir, "darkf-usage", "SKILL.md"), SKILL_BUNDLED);
|
|
500
|
+
const home = tempDir();
|
|
501
|
+
const installed = join(home, ".claude", "skills", "darkf-usage", "SKILL.md");
|
|
502
|
+
mkdirSync(dirname(installed), { recursive: true });
|
|
503
|
+
writeFileSync(installed, SKILL_STALE);
|
|
504
|
+
return {
|
|
505
|
+
installed,
|
|
506
|
+
skills: { home, bundledDir, existsBinary: () => false },
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
test("upgrade runs the sync after the install, under the skills: header", async () => {
|
|
511
|
+
const file = tempStateFile();
|
|
512
|
+
const { skills, installed } = skillsFixture();
|
|
513
|
+
const { spawnImpl } = spawnStub((cmd) => {
|
|
514
|
+
if (cmd[1] === "pm") return { exitCode: 0, stdout: "└── @psimona/darkf@0.3.0" };
|
|
515
|
+
if (cmd[1] === "install") return { exitCode: 0 };
|
|
516
|
+
return undefined;
|
|
517
|
+
});
|
|
518
|
+
const { fetchImpl } = fetchReturning('{"latest":"9.9.9"}');
|
|
519
|
+
|
|
520
|
+
const { result, out } = await captured(() =>
|
|
521
|
+
upgradeWithSkills(deps({ stateFile: file, spawnImpl, fetchImpl }), skills),
|
|
522
|
+
);
|
|
523
|
+
|
|
524
|
+
expect(result).toBe(0);
|
|
525
|
+
const skillsIdx = out.indexOf("skills:");
|
|
526
|
+
expect(skillsIdx).toBeGreaterThanOrEqual(0);
|
|
527
|
+
expect(out.slice(skillsIdx).some((l) => l.includes("updated (was stale)"))).toBe(true);
|
|
528
|
+
expect(readFileSync(installed, "utf8")).toBe(SKILL_BUNDLED); // stale copy replaced
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
test("a failed upgrade skips the sync — never a skills: block, stale copy untouched", async () => {
|
|
532
|
+
const { skills, installed } = skillsFixture();
|
|
533
|
+
const { spawnImpl } = spawnStub((cmd) => {
|
|
534
|
+
if (cmd[1] === "pm") return { exitCode: 0, stdout: "└── @psimona/darkf@0.3.0" };
|
|
535
|
+
if (cmd[1] === "install") return { exitCode: 7 };
|
|
536
|
+
return undefined;
|
|
537
|
+
});
|
|
538
|
+
const { fetchImpl } = fetchReturning('{"latest":"9.9.9"}');
|
|
539
|
+
|
|
540
|
+
const { result, out } = await captured(() =>
|
|
541
|
+
upgradeWithSkills(deps({ spawnImpl, fetchImpl }), skills),
|
|
542
|
+
);
|
|
543
|
+
|
|
544
|
+
expect(result).toBe(7); // the upgrade's own exit code, unmasked
|
|
545
|
+
expect(out).not.toContain("skills:");
|
|
546
|
+
expect(readFileSync(installed, "utf8")).toBe(SKILL_STALE);
|
|
547
|
+
});
|
|
548
|
+
|
|
549
|
+
test("a sync failure after a good upgrade is reported and exits non-zero", async () => {
|
|
550
|
+
const { skills } = skillsFixture();
|
|
551
|
+
// The claude skills "dir" is a file — the sync cannot write.
|
|
552
|
+
rmSync(join(skills.home, ".claude", "skills"), { recursive: true });
|
|
553
|
+
writeFileSync(join(skills.home, ".claude", "skills"), "a file, not a dir");
|
|
554
|
+
const { spawnImpl } = spawnStub((cmd) => {
|
|
555
|
+
if (cmd[1] === "pm") return { exitCode: 0, stdout: "└── @psimona/darkf@0.3.0" };
|
|
556
|
+
if (cmd[1] === "install") return { exitCode: 0 };
|
|
557
|
+
return undefined;
|
|
558
|
+
});
|
|
559
|
+
const { fetchImpl } = fetchReturning('{"latest":"9.9.9"}');
|
|
560
|
+
|
|
561
|
+
const { result, out, err } = await captured(() =>
|
|
562
|
+
upgradeWithSkills(deps({ spawnImpl, fetchImpl }), skills),
|
|
563
|
+
);
|
|
564
|
+
|
|
565
|
+
expect(result).toBe(1); // best-effort: reported, non-zero, upgrade unmasked
|
|
566
|
+
expect(out).toContain("skills:");
|
|
567
|
+
expect(err.join("\n")).not.toContain("skills"); // the failure is in the sync lines, not stderr noise
|
|
568
|
+
});
|
package/src/cli/update.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
// manager. No module-level mutable state.
|
|
5
5
|
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
6
6
|
import { dirname } from "node:path";
|
|
7
|
+
import { type SkillsDeps, syncAll } from "./setup.ts";
|
|
7
8
|
|
|
8
9
|
const PACKAGE_SPEC = "@psimona/darkf";
|
|
9
10
|
const REGISTRY_URL = "https://registry.npmjs.org/-/package/@psimona%2Fdarkf/dist-tags";
|
|
@@ -189,3 +190,22 @@ export async function upgradeCmd(deps: UpdateDeps): Promise<number> {
|
|
|
189
190
|
}
|
|
190
191
|
return code;
|
|
191
192
|
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* `darkf upgrade` end-to-end (skill-setup spec): install the new package
|
|
196
|
+
* version, then sync the skills it now ships — after the install, the
|
|
197
|
+
* running process resolves the new bundled copies on disk, so skills can
|
|
198
|
+
* never be stale relative to the engine they teach. Same code path as
|
|
199
|
+
* `darkf setup`. Best-effort: a sync failure is reported and exits non-zero,
|
|
200
|
+
* but never masks the upgrade's own exit code.
|
|
201
|
+
*/
|
|
202
|
+
export async function upgradeWithSkills(
|
|
203
|
+
update: UpdateDeps,
|
|
204
|
+
skills: SkillsDeps,
|
|
205
|
+
log: (line: string) => void = console.log,
|
|
206
|
+
): Promise<number> {
|
|
207
|
+
const code = await upgradeCmd(update);
|
|
208
|
+
if (code !== 0) return code;
|
|
209
|
+
log("skills:");
|
|
210
|
+
return syncAll({ ...skills, log });
|
|
211
|
+
}
|
package/src/core/index.ts
CHANGED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { parseTaskList } from "./tasks.ts";
|
|
3
|
+
import type { TaskList } from "./types.ts";
|
|
4
|
+
|
|
5
|
+
const VALID: TaskList = {
|
|
6
|
+
id: "ips-csv-load",
|
|
7
|
+
spec: { prd: "docs/specs/x/prd.md", design: "docs/specs/x/design.md" },
|
|
8
|
+
requirements: [
|
|
9
|
+
{ id: "REQ-1", kind: "req", text: "load the csv", priority: "must" },
|
|
10
|
+
{ id: "NFR-2", kind: "nfr", text: "under 10s", priority: "should" },
|
|
11
|
+
],
|
|
12
|
+
tasks: [
|
|
13
|
+
{ id: "T-1", title: "wire the loader", status: "done", satisfies: ["REQ-1"], priority: "must" },
|
|
14
|
+
{ id: "T-2", title: "bench it", status: "in-progress", satisfies: ["NFR-2"] },
|
|
15
|
+
{ id: "T-3", title: "write it up", status: "pending" },
|
|
16
|
+
],
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
test("parseTaskList on a valid list returns it narrowed, unknown extra fields ignored", () => {
|
|
20
|
+
const parsed = parseTaskList({
|
|
21
|
+
...VALID,
|
|
22
|
+
tasks: [...VALID.tasks, { id: "T-9", title: "future", status: "failed", note: "extra" }],
|
|
23
|
+
});
|
|
24
|
+
expect(parsed).not.toHaveProperty("error");
|
|
25
|
+
expect(parsed).toEqual({
|
|
26
|
+
...VALID,
|
|
27
|
+
tasks: [...VALID.tasks, { id: "T-9", title: "future", status: "failed" }],
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("parseTaskList accepts the minimal shape: only id and tasks, everything else optional", () => {
|
|
32
|
+
expect(parseTaskList({ id: "x", tasks: [] })).toEqual({ id: "x", tasks: [] });
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe("parseTaskList narrowing", () => {
|
|
36
|
+
test("non-object roots are rejected", () => {
|
|
37
|
+
expect(parseTaskList(null)).toHaveProperty("error");
|
|
38
|
+
expect(parseTaskList("nope")).toHaveProperty("error");
|
|
39
|
+
expect(parseTaskList([VALID])).toHaveProperty("error");
|
|
40
|
+
expect(parseTaskList(undefined)).toHaveProperty("error");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("missing or wrong-typed id", () => {
|
|
44
|
+
const { id: _id, ...noId } = VALID;
|
|
45
|
+
expect(parseTaskList(noId)).toHaveProperty("error");
|
|
46
|
+
expect(parseTaskList({ ...VALID, id: 7 })).toHaveProperty("error");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("wrong-typed spec fields", () => {
|
|
50
|
+
expect(parseTaskList({ ...VALID, spec: "docs" })).toHaveProperty("error");
|
|
51
|
+
expect(parseTaskList({ ...VALID, spec: { prd: 3 } })).toHaveProperty("error");
|
|
52
|
+
expect(parseTaskList({ ...VALID, spec: { design: [] } })).toHaveProperty("error");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("missing or wrong-typed fields on a task", () => {
|
|
56
|
+
const missingTitle = { id: "T-1", status: "pending" };
|
|
57
|
+
expect(parseTaskList({ id: "x", tasks: [missingTitle] })).toHaveProperty("error");
|
|
58
|
+
expect(
|
|
59
|
+
parseTaskList({ id: "x", tasks: [{ id: 1, title: "t", status: "pending" }] }),
|
|
60
|
+
).toHaveProperty("error");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("bad status union", () => {
|
|
64
|
+
const parsed = parseTaskList({
|
|
65
|
+
id: "x",
|
|
66
|
+
tasks: [{ id: "T-1", title: "t", status: "archived" }],
|
|
67
|
+
});
|
|
68
|
+
expect(parsed).toHaveProperty("error");
|
|
69
|
+
expect((parsed as { error: string }).error).toContain("tasks[0].status");
|
|
70
|
+
expect((parsed as { error: string }).error).toContain(
|
|
71
|
+
'"pending" | "in-progress" | "done" | "failed"',
|
|
72
|
+
);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("bad priority union, on both tasks and requirements", () => {
|
|
76
|
+
const taskSide = parseTaskList({
|
|
77
|
+
id: "x",
|
|
78
|
+
tasks: [{ id: "T-1", title: "t", status: "pending", priority: "Must" }],
|
|
79
|
+
});
|
|
80
|
+
expect((taskSide as { error: string }).error).toContain("tasks[0].priority");
|
|
81
|
+
expect((taskSide as { error: string }).error).toContain(
|
|
82
|
+
'"must" | "should" | "could" | "won\'t"',
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const requirementSide = parseTaskList({
|
|
86
|
+
id: "x",
|
|
87
|
+
requirements: [{ id: "REQ-1", kind: "req", text: "t", priority: "SHOULD" }],
|
|
88
|
+
tasks: [],
|
|
89
|
+
});
|
|
90
|
+
expect((requirementSide as { error: string }).error).toContain("requirements[0].priority");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("bad requirement kind union and wrong-typed requirement fields", () => {
|
|
94
|
+
const badKind = parseTaskList({
|
|
95
|
+
id: "x",
|
|
96
|
+
requirements: [{ id: "REQ-1", kind: "story", text: "t" }],
|
|
97
|
+
tasks: [],
|
|
98
|
+
});
|
|
99
|
+
expect((badKind as { error: string }).error).toContain("requirements[0].kind");
|
|
100
|
+
|
|
101
|
+
const noText = parseTaskList({
|
|
102
|
+
id: "x",
|
|
103
|
+
requirements: [{ id: "REQ-1", kind: "req" }],
|
|
104
|
+
tasks: [],
|
|
105
|
+
});
|
|
106
|
+
expect(noText).toHaveProperty("error");
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("satisfies must be an array of strings", () => {
|
|
110
|
+
const notArray = parseTaskList({
|
|
111
|
+
id: "x",
|
|
112
|
+
tasks: [{ id: "T-1", title: "t", status: "pending", satisfies: "REQ-1" }],
|
|
113
|
+
});
|
|
114
|
+
expect((notArray as { error: string }).error).toContain("tasks[0].satisfies");
|
|
115
|
+
|
|
116
|
+
const notStrings = parseTaskList({
|
|
117
|
+
id: "x",
|
|
118
|
+
tasks: [{ id: "T-1", title: "t", status: "pending", satisfies: ["REQ-1", 2] }],
|
|
119
|
+
});
|
|
120
|
+
expect((notStrings as { error: string }).error).toContain("tasks[0].satisfies[1]");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("tasks not an array", () => {
|
|
124
|
+
const parsed = parseTaskList({ id: "x", tasks: "T-1,T-2" });
|
|
125
|
+
expect(parsed).toHaveProperty("error");
|
|
126
|
+
expect((parsed as { error: string }).error).toContain("tasks:");
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("requirements not an array", () => {
|
|
130
|
+
expect(parseTaskList({ id: "x", requirements: {}, tasks: [] })).toHaveProperty("error");
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("the legacy consumer shape has no tolerance: task-side requirements are not read", () => {
|
|
134
|
+
// Pre-migration consumer tasks carry `requirements` instead of `satisfies`
|
|
135
|
+
// — the validator narrows to the standard only; migration is the writer's
|
|
136
|
+
// one-shot job (Decisions: Consumer repo migrates to the standard).
|
|
137
|
+
const legacy = parseTaskList({
|
|
138
|
+
id: "x",
|
|
139
|
+
tasks: [{ id: "T-1", requirements: ["REQ-1"], title: "t", status: "pending" }],
|
|
140
|
+
});
|
|
141
|
+
expect(legacy).toEqual({
|
|
142
|
+
id: "x",
|
|
143
|
+
tasks: [{ id: "T-1", title: "t", status: "pending" }],
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
});
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import type { Task, TaskList, TaskPriority, TaskRequirement, TaskStatus } from "./types.ts";
|
|
2
|
+
|
|
3
|
+
// Hand-written narrowing of unknown JSON to a TaskList (docs/specs/task-list-json/spec.md,
|
|
4
|
+
// Decisions: Validation) — zero deps, no dual-schema tolerance: the legacy
|
|
5
|
+
// names (`moscow`, task-side `requirements`) are simply not read, and the
|
|
6
|
+
// result is either a TaskList or `{ error }`. The item-loop path and the
|
|
7
|
+
// OpenCode plugin share this one validator, so a malformed list can never
|
|
8
|
+
// crash a consumer.
|
|
9
|
+
//
|
|
10
|
+
// Unknown extra fields are ignored — the schema is narrowed to, not
|
|
11
|
+
// enforced as a closed record.
|
|
12
|
+
|
|
13
|
+
const TASK_STATUSES: readonly TaskStatus[] = ["pending", "in-progress", "done", "failed"];
|
|
14
|
+
const TASK_PRIORITIES: readonly TaskPriority[] = ["must", "should", "could", "won't"];
|
|
15
|
+
const REQUIREMENT_KINDS: readonly TaskRequirement["kind"][] = ["req", "nfr", "ng"];
|
|
16
|
+
|
|
17
|
+
function isObject(value: unknown): value is Record<string, unknown> {
|
|
18
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function describe(value: unknown): string {
|
|
22
|
+
if (value === null) return "null";
|
|
23
|
+
if (value === undefined) return "undefined";
|
|
24
|
+
if (Array.isArray(value)) return "an array";
|
|
25
|
+
return `a ${typeof value}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// The failure half of every sub-parse below — never checked with `in` on the
|
|
29
|
+
// value itself, which throws on the string primitives the success half is.
|
|
30
|
+
function isError(x: unknown): x is { error: string } {
|
|
31
|
+
return typeof x === "object" && x !== null && "error" in x;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function oneOf(value: unknown, allowed: readonly string[], at: string): string | undefined {
|
|
35
|
+
if (typeof value === "string" && (allowed as readonly string[]).includes(value)) return undefined;
|
|
36
|
+
const quoted = allowed.map((a) => JSON.stringify(a)).join(" | ");
|
|
37
|
+
return `${at}: expected one of ${quoted}, got ${JSON.stringify(value) ?? describe(value)}`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function parsePriority(value: unknown, at: string): TaskPriority | { error: string } {
|
|
41
|
+
const err = oneOf(value, TASK_PRIORITIES, at);
|
|
42
|
+
return err === undefined ? (value as TaskPriority) : { error: err };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function parseStringArray(value: unknown, at: string): string[] | { error: string } {
|
|
46
|
+
if (!Array.isArray(value)) return { error: `${at}: expected an array, got ${describe(value)}` };
|
|
47
|
+
for (const [i, entry] of value.entries()) {
|
|
48
|
+
if (typeof entry !== "string") {
|
|
49
|
+
return { error: `${at}[${i}]: expected a string, got ${describe(entry)}` };
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function parseRequirement(value: unknown, index: number): TaskRequirement | { error: string } {
|
|
56
|
+
const at = `requirements[${index}]`;
|
|
57
|
+
if (!isObject(value)) return { error: `${at}: expected an object, got ${describe(value)}` };
|
|
58
|
+
if (typeof value.id !== "string") {
|
|
59
|
+
return { error: `${at}.id: expected a string, got ${describe(value.id)}` };
|
|
60
|
+
}
|
|
61
|
+
const kindErr = oneOf(value.kind, REQUIREMENT_KINDS, `${at}.kind`);
|
|
62
|
+
if (kindErr !== undefined) return { error: kindErr };
|
|
63
|
+
if (typeof value.text !== "string") {
|
|
64
|
+
return { error: `${at}.text: expected a string, got ${describe(value.text)}` };
|
|
65
|
+
}
|
|
66
|
+
const requirement: TaskRequirement = {
|
|
67
|
+
id: value.id,
|
|
68
|
+
kind: value.kind as TaskRequirement["kind"],
|
|
69
|
+
text: value.text,
|
|
70
|
+
};
|
|
71
|
+
if (value.priority !== undefined) {
|
|
72
|
+
const priority = parsePriority(value.priority, `${at}.priority`);
|
|
73
|
+
if (isError(priority)) return priority;
|
|
74
|
+
requirement.priority = priority;
|
|
75
|
+
}
|
|
76
|
+
return requirement;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function parseTask(value: unknown, index: number): Task | { error: string } {
|
|
80
|
+
const at = `tasks[${index}]`;
|
|
81
|
+
if (!isObject(value)) return { error: `${at}: expected an object, got ${describe(value)}` };
|
|
82
|
+
if (typeof value.id !== "string") {
|
|
83
|
+
return { error: `${at}.id: expected a string, got ${describe(value.id)}` };
|
|
84
|
+
}
|
|
85
|
+
if (typeof value.title !== "string") {
|
|
86
|
+
return { error: `${at}.title: expected a string, got ${describe(value.title)}` };
|
|
87
|
+
}
|
|
88
|
+
const statusErr = oneOf(value.status, TASK_STATUSES, `${at}.status`);
|
|
89
|
+
if (statusErr !== undefined) return { error: statusErr };
|
|
90
|
+
const task: Task = { id: value.id, title: value.title, status: value.status as TaskStatus };
|
|
91
|
+
if (value.satisfies !== undefined) {
|
|
92
|
+
const satisfies = parseStringArray(value.satisfies, `${at}.satisfies`);
|
|
93
|
+
if (isError(satisfies)) return satisfies;
|
|
94
|
+
task.satisfies = satisfies;
|
|
95
|
+
}
|
|
96
|
+
if (value.priority !== undefined) {
|
|
97
|
+
const priority = parsePriority(value.priority, `${at}.priority`);
|
|
98
|
+
if (isError(priority)) return priority;
|
|
99
|
+
task.priority = priority;
|
|
100
|
+
}
|
|
101
|
+
return task;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function parseTaskList(json: unknown): TaskList | { error: string } {
|
|
105
|
+
if (!isObject(json)) {
|
|
106
|
+
return { error: `task list: expected an object, got ${describe(json)}` };
|
|
107
|
+
}
|
|
108
|
+
if (typeof json.id !== "string") {
|
|
109
|
+
return { error: `id: expected a string, got ${describe(json.id)}` };
|
|
110
|
+
}
|
|
111
|
+
const list: TaskList = { id: json.id, tasks: [] };
|
|
112
|
+
|
|
113
|
+
if (json.spec !== undefined) {
|
|
114
|
+
if (!isObject(json.spec)) {
|
|
115
|
+
return { error: `spec: expected an object, got ${describe(json.spec)}` };
|
|
116
|
+
}
|
|
117
|
+
const spec: TaskList["spec"] = {};
|
|
118
|
+
if (json.spec.prd !== undefined) {
|
|
119
|
+
if (typeof json.spec.prd !== "string") {
|
|
120
|
+
return { error: `spec.prd: expected a string, got ${describe(json.spec.prd)}` };
|
|
121
|
+
}
|
|
122
|
+
spec.prd = json.spec.prd;
|
|
123
|
+
}
|
|
124
|
+
if (json.spec.design !== undefined) {
|
|
125
|
+
if (typeof json.spec.design !== "string") {
|
|
126
|
+
return { error: `spec.design: expected a string, got ${describe(json.spec.design)}` };
|
|
127
|
+
}
|
|
128
|
+
spec.design = json.spec.design;
|
|
129
|
+
}
|
|
130
|
+
list.spec = spec;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (json.requirements !== undefined) {
|
|
134
|
+
if (!Array.isArray(json.requirements)) {
|
|
135
|
+
return { error: `requirements: expected an array, got ${describe(json.requirements)}` };
|
|
136
|
+
}
|
|
137
|
+
const requirements: TaskRequirement[] = [];
|
|
138
|
+
for (const [i, entry] of json.requirements.entries()) {
|
|
139
|
+
const requirement = parseRequirement(entry, i);
|
|
140
|
+
if (isError(requirement)) return requirement;
|
|
141
|
+
requirements.push(requirement);
|
|
142
|
+
}
|
|
143
|
+
list.requirements = requirements;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (!Array.isArray(json.tasks)) {
|
|
147
|
+
return { error: `tasks: expected an array, got ${describe(json.tasks)}` };
|
|
148
|
+
}
|
|
149
|
+
const tasks: Task[] = [];
|
|
150
|
+
for (const [i, entry] of json.tasks.entries()) {
|
|
151
|
+
const task = parseTask(entry, i);
|
|
152
|
+
if (isError(task)) return task;
|
|
153
|
+
tasks.push(task);
|
|
154
|
+
}
|
|
155
|
+
list.tasks = tasks;
|
|
156
|
+
|
|
157
|
+
return list;
|
|
158
|
+
}
|
package/src/core/types.ts
CHANGED
|
@@ -53,6 +53,10 @@ export interface BaseLeafDef {
|
|
|
53
53
|
retries?: number;
|
|
54
54
|
/** Engine-enforced ceiling on one dispatch attempt. Default 1h. */
|
|
55
55
|
timeoutMs?: number;
|
|
56
|
+
/** Fixed interval between usage-limit re-probes after a provider quota wall. Default 30 min. See docs/specs/usage-limit-backoff/spec.md. */
|
|
57
|
+
usageLimitRetryMs?: number;
|
|
58
|
+
/** Total wait budget per node visit for usage-limit backoff. At the cap the run crashes with the last usage-limit reason. Default 24h. */
|
|
59
|
+
usageLimitMaxWaitMs?: number;
|
|
56
60
|
/**
|
|
57
61
|
* Plain code check run against a well-formed NodeResult before it's accepted
|
|
58
62
|
* as `succeeded`. Return a failure message to reject it, or undefined to
|
|
@@ -216,6 +220,9 @@ export type NodeStatus =
|
|
|
216
220
|
nudgeAttempt?: number;
|
|
217
221
|
/** The nudgeable `CrashReason` kind that triggered this nudge. */
|
|
218
222
|
nudgeReason?: CrashReason["kind"];
|
|
223
|
+
/** Present only while the engine is waiting out a provider usage-limit
|
|
224
|
+
* backoff for this attempt — see docs/specs/usage-limit-backoff/spec.md. */
|
|
225
|
+
limitBackoff?: { probe: number; nextAttemptAt: number; waitedMs: number };
|
|
219
226
|
}
|
|
220
227
|
| { phase: "succeeded"; result: NodeResult }
|
|
221
228
|
| { phase: "failed"; result: NodeResult }
|
|
@@ -223,6 +230,7 @@ export type NodeStatus =
|
|
|
223
230
|
|
|
224
231
|
export type CrashReason =
|
|
225
232
|
| { kind: "runner-error"; error: string }
|
|
233
|
+
| { kind: "usage-limit"; detail: string }
|
|
226
234
|
| { kind: "timeout"; elapsedMs: number }
|
|
227
235
|
| { kind: "handoff-missing" }
|
|
228
236
|
| { kind: "handoff-unparseable"; parseError: string }
|
|
@@ -277,3 +285,37 @@ export interface RunResult {
|
|
|
277
285
|
trace: StepTrace[];
|
|
278
286
|
node: NodeStatus;
|
|
279
287
|
}
|
|
288
|
+
|
|
289
|
+
// --- Task lists ---
|
|
290
|
+
//
|
|
291
|
+
// One shared vocabulary for plan/dispatch task lists (docs/specs/task-list-json/spec.md):
|
|
292
|
+
// workflows write it, `parseTaskList` narrows it, the OpenCode plugin renders it.
|
|
293
|
+
// Lowercase MoSCoW and `satisfies` are the locked names — the validator has no
|
|
294
|
+
// tolerance for the legacy shapes (`moscow`, task-side `requirements`).
|
|
295
|
+
|
|
296
|
+
export type TaskStatus = "pending" | "in-progress" | "done" | "failed";
|
|
297
|
+
export type TaskPriority = "must" | "should" | "could" | "won't";
|
|
298
|
+
|
|
299
|
+
export interface Task {
|
|
300
|
+
id: string;
|
|
301
|
+
title: string;
|
|
302
|
+
status: TaskStatus;
|
|
303
|
+
/** Requirement ids this task satisfies, when the list carries requirements. */
|
|
304
|
+
satisfies?: string[];
|
|
305
|
+
priority?: TaskPriority;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export interface TaskRequirement {
|
|
309
|
+
id: string; // "REQ-1", "NFR-2", "NG-1"
|
|
310
|
+
kind: "req" | "nfr" | "ng";
|
|
311
|
+
text: string;
|
|
312
|
+
priority?: TaskPriority;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export interface TaskList {
|
|
316
|
+
id: string;
|
|
317
|
+
/** Paths to the spec documents the tasks implement, when present. */
|
|
318
|
+
spec?: { prd?: string; design?: string };
|
|
319
|
+
requirements?: TaskRequirement[];
|
|
320
|
+
tasks: Task[];
|
|
321
|
+
}
|