Files
agentic-dev/scripts/work/cli.mjs
Danijel Martinek 014578c9a8 feat(work): pnpm work decompose subcommand
Closes the gap surfaced by the user: `pnpm work` usage referenced
`decompose` (via docs + the to-prd skill) but the subcommand was
never built. Mirrors `pnpm work dispatch`'s shape.

scripts/work/decompose.mjs (new):
  - validatePrdForDecompose(prdPath) — refuses draft (must go
    through human review first), in-review (review incomplete),
    shipped (epic already exists); accepts only approved
  - printDecomposePlan(prdId, prdPath, frontmatter) — print-mode
    output showing the PRD's eligibility + sandcastle invocation
    plan + auth modes
  - executeDecompose(prdId, prdPath, prdText) — invokes sandcastle
    with .sandcastle/decomposer.prompt.md, passing PRD_FILE_CONTENT
    promptArg. The decomposer agent writes the epic + per-story
    files to disk on a sandcastle branch the human can review
  - runCli(args, { workRoot }) — entry point used by cli.mjs
  - Direct invocation also supported (mirrors dispatch.mjs's
    invokedDirectly guard, NEW pattern after this commit)

scripts/work/decompose.test.mjs (new, 9 tests, all green):
  - validatePrdForDecompose: accepts approved; rejects draft,
    in-review, shipped, unknown status, missing file
  - runCli: writes error + returns 1 on missing PRD; writes error
    + returns 1 on draft PRD; prints plan + returns 0 on approved

scripts/work/cli.mjs:
  - Adds `decompose` subcommand to usage + dispatch
  - Usage formatting realigned for the 3-line subcommand block

scripts/work/dispatch.mjs:
  - **Fix** the bug surfaced by the user: dispatch.mjs's CLI ran
    as a top-level side effect whenever any of its exports was
    imported. decompose.mjs imports resolveClaudeAuth from it, so
    importing decompose.mjs printed "No ready task to dispatch."
    Added an `import.meta.url === \`file://${process.argv[1]}\``
    guard so the CLI only runs when invoked directly. This unblocks
    cross-import without side effects.

Smoke-tested end-to-end:
  - `pnpm work decompose` (no id) prints usage + exits 2
  - `pnpm work decompose 2026-05-13-binder-wrap-helper` prints the
    decompose plan with status: approved (eligible)
  - 9/9 unit tests green
  - dispatch.mjs's existing direct-invocation path unchanged

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-13 17:46:57 +02:00

158 lines
4.8 KiB
JavaScript

#!/usr/bin/env node
/**
* pnpm work — CLI for the local work-system.
*
* Subcommands:
* rebuild-state Rewrites docs/work/_state.json from the current markdown
* status Prints a tree of all epics + their stories
* next Prints the first non-done story in the first non-done epic
*/
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { buildState } from "./state-builder.mjs";
import { runCli as runPrdShip } from "./prd-ship.mjs";
import { runCli as runDecompose } from "./decompose.mjs";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const REPO_ROOT = path.resolve(__dirname, "..", "..");
const WORK_ROOT = path.join(REPO_ROOT, "docs", "work");
const STATE_FILE = path.join(WORK_ROOT, "_state.json");
function rebuildState() {
const state = buildState(WORK_ROOT);
fs.writeFileSync(STATE_FILE, JSON.stringify(state, null, 2) + "\n");
console.log(
`Rebuilt ${path.relative(REPO_ROOT, STATE_FILE)} with ${Object.keys(state.epics).length} epic(s).`,
);
}
function printStatus() {
const state = buildState(WORK_ROOT);
const epicIds = Object.keys(state.epics).sort();
if (epicIds.length === 0) {
console.log("No epics found under docs/work/.");
return;
}
for (const epicId of epicIds) {
const epic = state.epics[epicId];
const storyIds = Object.keys(epic.stories).sort();
const storyTotals = storyIds.reduce(
(acc, sid) => ({
total: acc.total + epic.stories[sid].ac_total,
done: acc.done + epic.stories[sid].ac_completed,
}),
{ total: 0, done: 0 },
);
const epicMark = mark(epic.status);
console.log(
`${epicMark} ${epicId}${epic.title} (${storyTotals.done}/${storyTotals.total} tasks done)`,
);
for (const sid of storyIds) {
const s = epic.stories[sid];
console.log(
` ${mark(s.status)} ${sid} (${s.ac_completed}/${s.ac_total}) — ${s.title}`,
);
}
}
}
function printNext() {
const state = buildState(WORK_ROOT);
if (state.ready.length === 0) {
if (state.blocked.length > 0) {
console.log("No ready stories. Blocked:");
for (const b of state.blocked) {
console.log(
` ${b.epic} / ${b.story} — waiting on: ${b.waiting_on.join(", ")}`,
);
}
} else {
console.log("All epics + stories are done. ✓");
}
return;
}
const r = state.ready[0];
console.log(`${r.epic} / ${r.story}${r.title}`);
console.log(` (use \`pnpm work ready\` to see all ready stories)`);
}
function printReady() {
const state = buildState(WORK_ROOT);
if (state.ready.length === 0) {
console.log(
"No ready stories. Run `pnpm work blocked` to see what's waiting on what.",
);
return;
}
console.log(
`${state.ready.length} ready stor${state.ready.length === 1 ? "y" : "ies"}:`,
);
for (const r of state.ready) {
console.log(` ${r.epic} / ${r.story}${r.title}`);
}
}
function printBlocked() {
const state = buildState(WORK_ROOT);
if (state.blocked.length === 0) {
console.log("No blocked stories.");
return;
}
console.log(
`${state.blocked.length} blocked stor${state.blocked.length === 1 ? "y" : "ies"}:`,
);
for (const b of state.blocked) {
console.log(` ${b.epic} / ${b.story}${b.title}`);
console.log(` waiting on: ${b.waiting_on.join(", ")}`);
}
}
function mark(status) {
if (status === "done") return "✓";
if (status === "in-progress") return "→";
if (status === "blocked") return "✗";
return "○";
}
function usage() {
console.log(
"Usage: pnpm work <rebuild-state|status|next|ready|blocked|dispatch|decompose|prd-ship>",
);
console.log(
" dispatch Print the next dispatch plan (use --execute to invoke sandcastle)",
);
console.log(
" decompose <prd-id> Decompose an approved PRD into epic + stories (use --execute to invoke sandcastle)",
);
console.log(
" prd-ship <id> Flip a PRD's status to `shipped` (run after its epic completes)",
);
process.exit(2);
}
const cmd = process.argv[2];
if (cmd === "rebuild-state") rebuildState();
else if (cmd === "status") printStatus();
else if (cmd === "next") printNext();
else if (cmd === "ready") printReady();
else if (cmd === "blocked") printBlocked();
else if (cmd === "dispatch") {
// Re-dispatch to the dispatch script so it can handle its own --execute flag
import("./dispatch.mjs").catch((e) => {
console.error(e);
process.exit(1);
});
} else if (cmd === "prd-ship") {
const exitCode = runPrdShip(process.argv.slice(3), {
repoRoot: REPO_ROOT,
workRoot: WORK_ROOT,
});
process.exit(exitCode);
} else if (cmd === "decompose") {
runDecompose(process.argv.slice(3), {
repoRoot: REPO_ROOT,
workRoot: WORK_ROOT,
}).then((code) => process.exit(code));
} else usage();