@psimona/darkf 0.3.0 → 0.5.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/feedback/2026-10-30-self-update.md +29 -0
- package/feedback/2026-10-31-task-list-json.md +26 -0
- package/package.json +1 -1
- package/src/cli/detached.test.ts +3 -0
- package/src/cli/index.ts +31 -3
- package/src/cli/update.test.ts +486 -0
- package/src/cli/update.ts +191 -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 +35 -1
- package/src/kit/agents/opencode.ts +5 -5
- package/src/kit/status.test.ts +51 -1
- package/src/kit/status.ts +42 -1
- package/workflows/dev-loop/scripts/begin.ts +22 -0
- package/workflows/dev-loop/scripts/check.ts +19 -0
- package/workflows/dev-loop/scripts/finish.ts +122 -0
- package/workflows/dev-loop/workflow.ts +158 -0
- package/workflows/series-ordering/scripts/verify.ts +1 -1
- package/workflows/series-ordering/workflow.ts +5 -4
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// CLI self-update (docs/specs/self-update): a daily startup version check and
|
|
2
|
+
// the `darkf upgrade` command. Every dependency arrives via UpdateDeps, so the
|
|
3
|
+
// whole module runs in tests without network, a real clock, or a real package
|
|
4
|
+
// manager. No module-level mutable state.
|
|
5
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { dirname } from "node:path";
|
|
7
|
+
|
|
8
|
+
const PACKAGE_SPEC = "@psimona/darkf";
|
|
9
|
+
const REGISTRY_URL = "https://registry.npmjs.org/-/package/@psimona%2Fdarkf/dist-tags";
|
|
10
|
+
const CHECK_INTERVAL_MS = 24 * 60 * 60 * 1000;
|
|
11
|
+
const CHECK_TIMEOUT_MS = 2_000;
|
|
12
|
+
// Design-originated: no REQ covers probe duration; the cap bounds a hung
|
|
13
|
+
// package manager.
|
|
14
|
+
const PROBE_TIMEOUT_MS = 10_000;
|
|
15
|
+
// Trust boundary: the registry response is untrusted input. `latest` must
|
|
16
|
+
// match this before any argv uses it (design: Security Considerations).
|
|
17
|
+
const SEMVER_RE = /^\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?$/;
|
|
18
|
+
|
|
19
|
+
export interface UpdateState {
|
|
20
|
+
lastCheck?: string; // ISO timestamp of the last check attempt
|
|
21
|
+
latestSeen?: string; // newest version known to exist; drives the notice
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface UpdateDeps {
|
|
25
|
+
now(): number; // injectable clock
|
|
26
|
+
fetchImpl: typeof fetch; // injectable registry access
|
|
27
|
+
spawnImpl: typeof Bun.spawnSync; // injectable probes and installer
|
|
28
|
+
stateFile: string; // injectable state location
|
|
29
|
+
installed: string; // version from the package package.json
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Absent or corrupt file returns {}. Never throws. */
|
|
33
|
+
export function readState(file: string): UpdateState {
|
|
34
|
+
try {
|
|
35
|
+
const parsed: unknown = JSON.parse(readFileSync(file, "utf8"));
|
|
36
|
+
if (typeof parsed !== "object" || parsed === null) return {};
|
|
37
|
+
const rec = parsed as Record<string, unknown>;
|
|
38
|
+
const state: UpdateState = {};
|
|
39
|
+
if (typeof rec.lastCheck === "string") state.lastCheck = rec.lastCheck;
|
|
40
|
+
if (typeof rec.latestSeen === "string") state.latestSeen = rec.latestSeen;
|
|
41
|
+
return state;
|
|
42
|
+
} catch {
|
|
43
|
+
return {};
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** mkdir -p on the parent; best-effort — swallow write errors (REQ-4). */
|
|
48
|
+
export function writeState(file: string, state: UpdateState): void {
|
|
49
|
+
try {
|
|
50
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
51
|
+
writeFileSync(file, JSON.stringify(state));
|
|
52
|
+
} catch {
|
|
53
|
+
// An unwritable state location degrades to re-checking next invocation.
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** True when lastCheck is missing, unparseable, or older than 24 h. */
|
|
58
|
+
export function isDue(state: UpdateState, now: number): boolean {
|
|
59
|
+
if (state.lastCheck === undefined) return true;
|
|
60
|
+
const last = Date.parse(state.lastCheck);
|
|
61
|
+
if (Number.isNaN(last)) return true;
|
|
62
|
+
return now - last >= CHECK_INTERVAL_MS;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Bun.semver.order(installed, latest) < 0. */
|
|
66
|
+
export function isNewer(installed: string, latest: string): boolean {
|
|
67
|
+
return Bun.semver.order(installed, latest) < 0;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* GET the dist-tags endpoint with AbortSignal.timeout(2000). Returns `latest`
|
|
72
|
+
* when it is a semver-shaped string, else null. Never throws.
|
|
73
|
+
*/
|
|
74
|
+
export async function checkRegistry(fetchImpl: typeof fetch): Promise<string | null> {
|
|
75
|
+
try {
|
|
76
|
+
const res = await fetchImpl(REGISTRY_URL, { signal: AbortSignal.timeout(CHECK_TIMEOUT_MS) });
|
|
77
|
+
if (res.status !== 200) return null;
|
|
78
|
+
const tags: unknown = await res.json();
|
|
79
|
+
const latest =
|
|
80
|
+
typeof tags === "object" && tags !== null
|
|
81
|
+
? (tags as Record<string, unknown>).latest
|
|
82
|
+
: undefined;
|
|
83
|
+
return typeof latest === "string" && SEMVER_RE.test(latest) ? latest : null;
|
|
84
|
+
} catch {
|
|
85
|
+
// Rejects, aborts, and malformed JSON are all the same silent no-op.
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The startup path, called from main() before command dispatch. Sends at most
|
|
92
|
+
* one registry request per invocation, bounded to one per 24 h across
|
|
93
|
+
* invocations (REQ-1, REQ-2). Silent by contract: every failure continues the
|
|
94
|
+
* started command with no added output (REQ-4).
|
|
95
|
+
*/
|
|
96
|
+
export async function updateGate(cmd: string | undefined, deps: UpdateDeps): Promise<void> {
|
|
97
|
+
// `upgrade` fetches on its own; the opt-out never blocks it.
|
|
98
|
+
if (cmd === "upgrade") return;
|
|
99
|
+
// Opt-out (REQ-5): no state read, no request, no print.
|
|
100
|
+
if (process.env.DARKF_NO_UPDATE_CHECK) return;
|
|
101
|
+
|
|
102
|
+
const state = readState(deps.stateFile);
|
|
103
|
+
if (isDue(state, deps.now())) {
|
|
104
|
+
const latest = await checkRegistry(deps.fetchImpl);
|
|
105
|
+
// null (failed check): leave latestSeen untouched — a stale notice survives.
|
|
106
|
+
if (latest !== null) {
|
|
107
|
+
if (isNewer(deps.installed, latest)) state.latestSeen = latest;
|
|
108
|
+
else delete state.latestSeen;
|
|
109
|
+
}
|
|
110
|
+
// Stamp on attempt (REQ-2): a failed check counts as a check.
|
|
111
|
+
state.lastCheck = new Date(deps.now()).toISOString();
|
|
112
|
+
writeState(deps.stateFile, state);
|
|
113
|
+
}
|
|
114
|
+
// Notice from the record: no registry request (REQ-11), exactly one line,
|
|
115
|
+
// printed before dispatch so it is the first line of stdout (NFR-3).
|
|
116
|
+
if (state.latestSeen !== undefined && isNewer(deps.installed, state.latestSeen)) {
|
|
117
|
+
console.log(
|
|
118
|
+
`darkf ${state.latestSeen} available (installed ${deps.installed}) — run 'darkf upgrade'`,
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Marker shared by `bun pm ls -g` and `npm ls -g --depth=0` output. */
|
|
124
|
+
const INSTALLED_MARKER = `${PACKAGE_SPEC}@`;
|
|
125
|
+
|
|
126
|
+
/** False when the manager fails, times out, or does not list the package. */
|
|
127
|
+
function probeManager(spawnImpl: typeof Bun.spawnSync, cmd: string[]): boolean {
|
|
128
|
+
let out: { exitCode: number; stdout: Buffer };
|
|
129
|
+
try {
|
|
130
|
+
out = spawnImpl(cmd, { timeout: PROBE_TIMEOUT_MS });
|
|
131
|
+
} catch {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
if (out.exitCode !== 0) return false;
|
|
135
|
+
return new TextDecoder().decode(out.stdout).includes(INSTALLED_MARKER);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* bun first (the darkf bin runs under bun by construction), then npm when
|
|
140
|
+
* present — so a pack:local snapshot resolves to bun (design: D-4).
|
|
141
|
+
*/
|
|
142
|
+
function pickManager(spawnImpl: typeof Bun.spawnSync): "bun" | "npm" | null {
|
|
143
|
+
if (probeManager(spawnImpl, ["bun", "pm", "ls", "-g"])) return "bun";
|
|
144
|
+
if (Bun.which("npm") !== null && probeManager(spawnImpl, ["npm", "ls", "-g", "--depth=0"])) {
|
|
145
|
+
return "npm";
|
|
146
|
+
}
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* The `darkf upgrade` command. Loud where the startup gate is silent: registry
|
|
152
|
+
* and provenance failures print a one-line stderr reason and exit non-zero
|
|
153
|
+
* (REQ-9, REQ-10). Returns the process exit code.
|
|
154
|
+
*/
|
|
155
|
+
export async function upgradeCmd(deps: UpdateDeps): Promise<number> {
|
|
156
|
+
const latest = await checkRegistry(deps.fetchImpl);
|
|
157
|
+
if (latest === null) {
|
|
158
|
+
console.error(
|
|
159
|
+
`update check failed: could not read the latest version of ${PACKAGE_SPEC} from the npm registry`,
|
|
160
|
+
);
|
|
161
|
+
return 1;
|
|
162
|
+
}
|
|
163
|
+
// Current, or a local snapshot newer than the published release: no
|
|
164
|
+
// downgrade (REQ-6).
|
|
165
|
+
if (Bun.semver.order(deps.installed, latest) >= 0) {
|
|
166
|
+
console.log(`darkf is current (installed ${deps.installed})`);
|
|
167
|
+
return 0;
|
|
168
|
+
}
|
|
169
|
+
const pm = pickManager(deps.spawnImpl);
|
|
170
|
+
if (pm === null) {
|
|
171
|
+
console.error("cannot determine how this darkf was installed — not managed by bun or npm");
|
|
172
|
+
return 1;
|
|
173
|
+
}
|
|
174
|
+
// The before/after line REQ-7 requires, printed before anything changes.
|
|
175
|
+
console.log(`upgrading ${deps.installed} → ${latest}`);
|
|
176
|
+
if (!SEMVER_RE.test(latest)) {
|
|
177
|
+
console.error(`registry returned a malformed version: ${JSON.stringify(latest)}`);
|
|
178
|
+
return 1;
|
|
179
|
+
}
|
|
180
|
+
// Argv array, never a shell string; stdio inherited so the installer's own
|
|
181
|
+
// output flows through (REQ-8, design: Security Considerations).
|
|
182
|
+
const install = deps.spawnImpl([pm, "install", "-g", `${PACKAGE_SPEC}@${latest}`], {
|
|
183
|
+
stdio: ["inherit", "inherit", "inherit"],
|
|
184
|
+
});
|
|
185
|
+
const code = install.exitCode;
|
|
186
|
+
// On success clear any stale record so no notice survives the upgrade.
|
|
187
|
+
if (code === 0) {
|
|
188
|
+
writeState(deps.stateFile, { lastCheck: new Date(deps.now()).toISOString() });
|
|
189
|
+
}
|
|
190
|
+
return code;
|
|
191
|
+
}
|
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
|
@@ -38,7 +38,7 @@ export const DONE = "[*]" as const;
|
|
|
38
38
|
// DONE's pseudostate notation; `mermaid.ts` special-cases it the same way,
|
|
39
39
|
// since `[-]` has no built-in meaning to stateDiagram-v2 the way `[*]` does.
|
|
40
40
|
export const FAILED = "[-]" as const;
|
|
41
|
-
// Deprecated alias — kept only for
|
|
41
|
+
// Deprecated alias — kept only for a consumer repo's give-up edges, which still
|
|
42
42
|
// route to EXIT on its own schedule (docs/specs/failed-terminal/spec.md).
|
|
43
43
|
// Removed in the same change that lands the `run_failure` node + retarget
|
|
44
44
|
// table there; any surviving `EXIT` import then fails to compile.
|
|
@@ -277,3 +277,37 @@ export interface RunResult {
|
|
|
277
277
|
trace: StepTrace[];
|
|
278
278
|
node: NodeStatus;
|
|
279
279
|
}
|
|
280
|
+
|
|
281
|
+
// --- Task lists ---
|
|
282
|
+
//
|
|
283
|
+
// One shared vocabulary for plan/dispatch task lists (docs/specs/task-list-json/spec.md):
|
|
284
|
+
// workflows write it, `parseTaskList` narrows it, the OpenCode plugin renders it.
|
|
285
|
+
// Lowercase MoSCoW and `satisfies` are the locked names — the validator has no
|
|
286
|
+
// tolerance for the legacy shapes (`moscow`, task-side `requirements`).
|
|
287
|
+
|
|
288
|
+
export type TaskStatus = "pending" | "in-progress" | "done" | "failed";
|
|
289
|
+
export type TaskPriority = "must" | "should" | "could" | "won't";
|
|
290
|
+
|
|
291
|
+
export interface Task {
|
|
292
|
+
id: string;
|
|
293
|
+
title: string;
|
|
294
|
+
status: TaskStatus;
|
|
295
|
+
/** Requirement ids this task satisfies, when the list carries requirements. */
|
|
296
|
+
satisfies?: string[];
|
|
297
|
+
priority?: TaskPriority;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export interface TaskRequirement {
|
|
301
|
+
id: string; // "REQ-1", "NFR-2", "NG-1"
|
|
302
|
+
kind: "req" | "nfr" | "ng";
|
|
303
|
+
text: string;
|
|
304
|
+
priority?: TaskPriority;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export interface TaskList {
|
|
308
|
+
id: string;
|
|
309
|
+
/** Paths to the spec documents the tasks implement, when present. */
|
|
310
|
+
spec?: { prd?: string; design?: string };
|
|
311
|
+
requirements?: TaskRequirement[];
|
|
312
|
+
tasks: Task[];
|
|
313
|
+
}
|
|
@@ -44,7 +44,7 @@ export interface StepFinishScan {
|
|
|
44
44
|
// opencode's `--format json` is NDJSON (one event object per line). The
|
|
45
45
|
// step-finish event carries `part.reason` — the terminal reason for the
|
|
46
46
|
// turn: "stop" (clean), "tool-calls" (ended between tool calls), "length"
|
|
47
|
-
// (output budget exhausted).
|
|
47
|
+
// (output budget exhausted). Unattended runs 2026-10-09: THREE crash classes
|
|
48
48
|
// (silent resume hang, finish=length authoring abort, finish=tool-calls
|
|
49
49
|
// mid-turn death) all surfaced as `exit 0` + missing handoff → a bare
|
|
50
50
|
// ENOENT minutes after the real failure. Capture the reason and make a
|
|
@@ -58,7 +58,7 @@ export interface StepFinishScan {
|
|
|
58
58
|
// and a count of step-finish events seen, in the same single pass:
|
|
59
59
|
// `stepCount` is the positive-evidence gate for ever reporting a session id
|
|
60
60
|
// at all — this is the resumability fix for the session that hung 15
|
|
61
|
-
// minutes on zero persisted messages (
|
|
61
|
+
// minutes on zero persisted messages (2026-10-08):
|
|
62
62
|
// 2026-10-08): the hang's root cause was resuming a session with no
|
|
63
63
|
// evidence it had done anything, not resuming as such.
|
|
64
64
|
export function scanStepFinish(stdout: string): StepFinishScan {
|
|
@@ -170,8 +170,8 @@ export const opencodeRunner: AgentRunner = {
|
|
|
170
170
|
}
|
|
171
171
|
// Opt-in per agent state: agent: { auto: true } passes opencode's --auto,
|
|
172
172
|
// which approves every ask-level permission for the dispatch (explicit
|
|
173
|
-
// deny rules still hold). Deliberately NOT a runner default —
|
|
174
|
-
// run
|
|
173
|
+
// deny rules still hold). Deliberately NOT a runner default — an
|
|
174
|
+
// unattended run (2026-10-13) crashed twice on `rm -rf *` asks that
|
|
175
175
|
// headless mode auto-rejects (finish=tool-calls, handoff not written),
|
|
176
176
|
// but blanket --auto would also silently approve force-push asks in
|
|
177
177
|
// unattended runs. Each workflow chooses.
|
|
@@ -218,7 +218,7 @@ export const opencodeRunner: AgentRunner = {
|
|
|
218
218
|
// (e.g. scratch-dir `rm -rf`) needs --auto or a narrower config allow.
|
|
219
219
|
// Resolve once: spawn can miss PATH lookups in daemonized workers.
|
|
220
220
|
//
|
|
221
|
-
// No -s outside a nudge:
|
|
221
|
+
// No -s outside a nudge: an unattended run (2026-10-08) — the
|
|
222
222
|
// task_plan dispatch resumed req_gate's session via `-s` and hung 15
|
|
223
223
|
// minutes with ZERO messages persisted (opencode.db: session's last row
|
|
224
224
|
// predates the dispatch), then exited 0 without writing the handoff →
|
package/src/kit/status.test.ts
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
type CrashReason,
|
|
4
|
+
EXIT,
|
|
5
|
+
FAILED,
|
|
6
|
+
type NodeEvent,
|
|
7
|
+
type NodeResult,
|
|
8
|
+
type TaskList,
|
|
9
|
+
} from "../core/index.ts";
|
|
3
10
|
import {
|
|
4
11
|
agentStallWarning,
|
|
5
12
|
classifyTerminal,
|
|
6
13
|
foldJournal,
|
|
7
14
|
foldReport,
|
|
15
|
+
foldTaskList,
|
|
8
16
|
nudgeWarning,
|
|
9
17
|
stallWarning,
|
|
10
18
|
} from "./status.ts";
|
|
@@ -421,3 +429,45 @@ describe("foldReport", () => {
|
|
|
421
429
|
});
|
|
422
430
|
});
|
|
423
431
|
});
|
|
432
|
+
|
|
433
|
+
describe("foldTaskList", () => {
|
|
434
|
+
const LIST: TaskList = {
|
|
435
|
+
id: "x",
|
|
436
|
+
tasks: [
|
|
437
|
+
{ id: "T-1", title: "a", status: "done" },
|
|
438
|
+
{ id: "T-2", title: "b", status: "done" },
|
|
439
|
+
{ id: "T-3", title: "c", status: "in-progress" },
|
|
440
|
+
{ id: "T-4", title: "d", status: "pending" },
|
|
441
|
+
{ id: "T-5", title: "e", status: "failed" },
|
|
442
|
+
],
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
test("strict counts: done only; failed/pending/in-progress are all not-done", () => {
|
|
446
|
+
expect(foldTaskList(LIST).done).toBe(2);
|
|
447
|
+
expect(foldTaskList(LIST).total).toBe(5);
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
test("current is the file's in-progress task, null when nothing is in-progress", () => {
|
|
451
|
+
expect(foldTaskList(LIST).current?.id).toBe("T-3");
|
|
452
|
+
expect(
|
|
453
|
+
foldTaskList({ id: "x", tasks: [{ id: "T-1", title: "a", status: "pending" }] }).current,
|
|
454
|
+
).toBeNull();
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
test("no stall when exactly one (or zero) task is in-progress", () => {
|
|
458
|
+
expect(foldTaskList(LIST).stall).toBeNull();
|
|
459
|
+
expect(foldTaskList({ id: "x", tasks: [] }).stall).toBeNull();
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
test("stall when the list names more than one current task", () => {
|
|
463
|
+
const digest = foldTaskList({
|
|
464
|
+
id: "x",
|
|
465
|
+
tasks: [
|
|
466
|
+
{ id: "T-1", title: "a", status: "in-progress" },
|
|
467
|
+
{ id: "T-2", title: "b", status: "in-progress" },
|
|
468
|
+
],
|
|
469
|
+
});
|
|
470
|
+
expect(digest.stall).toContain("2 tasks in-progress");
|
|
471
|
+
expect(digest.current?.id).toBe("T-1");
|
|
472
|
+
});
|
|
473
|
+
});
|