doctrina-cli 0.18.0 → 0.20.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.
Files changed (42) hide show
  1. package/README.md +3 -2
  2. package/package.json +1 -1
  3. package/src/commands/adapter.js +74 -0
  4. package/src/commands/analyze.js +14 -3
  5. package/src/commands/change.js +73 -4
  6. package/src/commands/close.js +18 -1
  7. package/src/commands/context.js +49 -8
  8. package/src/commands/delivery.js +238 -0
  9. package/src/commands/index-rebuild.js +23 -0
  10. package/src/commands/init.js +15 -0
  11. package/src/commands/intake.js +47 -1
  12. package/src/commands/metrics.js +28 -3
  13. package/src/commands/prime.js +76 -7
  14. package/src/commands/upgrade.js +15 -0
  15. package/src/commands/work.js +86 -2
  16. package/src/commands/workspace.js +409 -0
  17. package/src/index.js +5 -7
  18. package/src/lib/actions.js +1 -1
  19. package/src/lib/adapters.js +80 -0
  20. package/src/lib/code-inventory.js +297 -0
  21. package/src/lib/commands.js +44 -5
  22. package/src/lib/context-slice.js +118 -0
  23. package/src/lib/delivery.js +208 -0
  24. package/src/lib/metrics-model.js +23 -1
  25. package/src/lib/playbook.js +1 -1
  26. package/src/lib/skill-export.js +95 -0
  27. package/src/lib/snapshot.js +39 -2
  28. package/src/lib/task-card.js +102 -0
  29. package/src/lib/views.js +51 -2
  30. package/src/lib/work-model.js +58 -0
  31. package/src/lib/workspace-agents.js +259 -0
  32. package/src/lib/workspace-tools.js +113 -0
  33. package/src/lib/workspace.js +371 -0
  34. package/templates/AGENTS.md.template +17 -14
  35. package/templates/README.md +9 -0
  36. package/templates/adapters/claude/.claude/settings.json.template +18 -0
  37. package/templates/adapters/cursor/.cursor/hooks.json.template +8 -0
  38. package/templates/adapters/cursor/.cursor/rules/00-doctrina.mdc.template +14 -9
  39. package/templates/delivery.md.template +39 -0
  40. package/templates/playbooks/absorb.md.template +40 -0
  41. package/templates/playbooks/delivery.md.template +38 -0
  42. package/templates/playbooks/work.md.template +1 -1
@@ -8,6 +8,8 @@ import * as idx from "../lib/index-json.js";
8
8
  import { c } from "../lib/colors.js";
9
9
  import { assessBrief } from "../lib/clarity.js";
10
10
  import { locateTemplatesDir, substitute } from "../lib/templates.js";
11
+ import { findWorkspace, projectAt, readWorkspace } from "../lib/workspace.js";
12
+ import { readDelivery, resolveDeliveryId } from "../lib/delivery.js";
11
13
  import { EXIT, notADoctrinaProject } from "../lib/exit-codes.js";
12
14
  import { changeNew, reindexChange, addAffectedSpec } from "../lib/change-ops.js";
13
15
  import { classify } from "../lib/triage-model.js";
@@ -15,7 +17,7 @@ import { printPlaybookTemplate } from "../lib/playbook.js";
15
17
  import { specShape } from "../lib/spec-ops.js";
16
18
  import { changedFiles } from "../lib/git.js";
17
19
  import { slugFromPrompt, fold, CONFIDENT_MARGIN } from "../lib/lexicon.js";
18
- import { rankCapabilities, rankCapabilitiesByDiff } from "../lib/work-model.js";
20
+ import { rankCapabilities, rankCapabilitiesByDiff, similarChanges, SIMILAR_CHANGE_DAYS } from "../lib/work-model.js";
19
21
  import { isChangeId, refuseChangeRef } from "../lib/project.js";
20
22
  export { rankCapabilities, rankCapabilitiesByDiff } from "../lib/work-model.js";
21
23
 
@@ -30,7 +32,7 @@ export { rankCapabilities, rankCapabilitiesByDiff } from "../lib/work-model.js";
30
32
  // Flags this command accepts. Declared HERE, with the command, so
31
33
  // adding a command never requires editing the entrypoint — the gap that
32
34
  // let six flags ship undeclared and silently swallow a positional (C3).
33
- export const flags = { boolean: ["json", "chore", "design", "force", "from-diff", "no-spec", "quiet"], string: ["capability", "consolidate", "id", "resume", "title"] };
35
+ export const flags = { boolean: ["json", "chore", "design", "force", "from-diff", "no-spec", "quiet"], string: ["capability", "consolidate", "delivery", "id", "resume", "title"] };
34
36
 
35
37
  export async function run(positional, flags) {
36
38
  const projectRoot = process.cwd();
@@ -61,6 +63,20 @@ export async function run(positional, flags) {
61
63
  ? `Consolidate the ${consolidate} spec: merge requirements and criteria that say the same thing, drop what a later one superseded — no behaviour changes.`
62
64
  : positional.join(" ").trim();
63
65
 
66
+ // --delivery <id> (change 0222): the change is this repository's part of a
67
+ // workspace delivery. Checked BEFORE anything is scaffolded, so a typo or a
68
+ // repository the delivery was never routed to opens nothing.
69
+ let delivery = null;
70
+ if (flags.has("delivery")) {
71
+ const resolved = resolveDeliveryFor(projectRoot, flagString(flags, "delivery"));
72
+ if (resolved.error) {
73
+ console.error(c.red("error:") + ` ${resolved.error}`);
74
+ if (resolved.hint) console.error(c.gray("hint: ") + resolved.hint);
75
+ return resolved.code;
76
+ }
77
+ delivery = resolved.id;
78
+ }
79
+
64
80
  // --resume <id>: reprint the playbook for an existing open change rather than
65
81
  // open a new one (review G1 — `work "continue"` used to slug the word and
66
82
  // fabricate a junk `NNNN-continue` change the operator had to delete).
@@ -101,6 +117,29 @@ export async function run(positional, flags) {
101
117
  }
102
118
  }
103
119
 
120
+ // DUPLICATE CHECK (change 0215). A request that mostly repeats a change
121
+ // already open, or archived in the last two weeks, is held before a second
122
+ // copy is scaffolded: an archived change is out of every default read, and
123
+ // a field review found the person asking for "the doctrina work" of work
124
+ // already closed. A term match can be wrong, so --force proceeds.
125
+ if (!fromDiff && !consolidate && prompt && !flagBool(flags, "force", false)) {
126
+ const similar = similarChanges(projectRoot, prompt);
127
+ if (similar.length > 0) {
128
+ console.error(c.yellow("hold:") + " this request reads like a change that already exists:");
129
+ for (const s of similar.slice(0, 3)) {
130
+ console.error(` ${c.cyan(s.id)} ${s.title}` + c.gray(` — ${s.where} (${s.path}); shared: ${s.shared.join(", ")}`));
131
+ }
132
+ console.error("");
133
+ const top = similar[0];
134
+ console.error(top.where === "archived"
135
+ ? `If it is the same work, it is done: tell the human it is archived at ${top.path}.`
136
+ : `If it is the same work, continue it: ${c.cyan(`doctrina work --resume ${top.id}`)}`);
137
+ console.error(c.gray("If it is new work, open it anyway: ") + c.cyan(`doctrina work --force "<request>"`) +
138
+ c.gray(` (open changes and the last ${SIMILAR_CHANGE_DAYS} days of the archive are compared)`));
139
+ return EXIT.PRECONDITION;
140
+ }
141
+ }
142
+
104
143
  // LANE CHECK (change 0029). `work` was the default answer to every
105
144
  // request, so a broken workflow or a suite that ran nothing became a
106
145
  // change with a proposal, tasks and a spec delta — ceremony spent on a
@@ -195,6 +234,9 @@ export async function run(positional, flags) {
195
234
  if (pinned) {
196
235
  updated = updated.replace(/^(-\s+\*\*Affects specs:\*\*).*$/m, `$1 ${pinned}`);
197
236
  }
237
+ if (delivery) {
238
+ updated = updated.replace(/^(-[ \t]+\*\*Lane:\*\*[^\r\n]*)/m, `$1\n- **Delivery:** ${delivery}`);
239
+ }
198
240
  if (updated !== txt) write(proposalPath, updated, { force: true });
199
241
  }
200
242
 
@@ -262,6 +304,12 @@ export async function run(positional, flags) {
262
304
  fromDiff, diffMatches: fromDiff ? [] : diffMatches,
263
305
  });
264
306
  }
307
+ // The sentence the human needs (change 0214). A field review: the change
308
+ // existed only in the terminal, so the person asking saw code appear with
309
+ // no Doctrina trace — and later asked for "the doctrina work" of work
310
+ // already closed. The agent repeats this line; the CLI writes it.
311
+ console.log("");
312
+ console.log(c.bold("Tell the human: ") + `opened change ${id} — "${title}" (.doctrina/changes/${id}/)`);
265
313
  return 0;
266
314
  }
267
315
 
@@ -332,6 +380,28 @@ function laneRecord(verdict, { forced, chore, fromDiff }) {
332
380
  return line;
333
381
  }
334
382
 
383
+ // The delivery a `--delivery` names, from this repository's workspace: it
384
+ // must exist, and this repository must be one of the projects it was routed to.
385
+ function resolveDeliveryFor(projectRoot, ref) {
386
+ const found = findWorkspace(projectRoot);
387
+ if (!found) {
388
+ return { error: "--delivery needs a workspace, and this repository belongs to none", hint: "register it: `doctrina workspace add <path>` from the workspace", code: EXIT.PRECONDITION };
389
+ }
390
+ const id = resolveDeliveryId(found.dir, ref);
391
+ if (!id) return { error: `no delivery "${ref ?? ""}" in the workspace at ${found.dir}`, hint: "`doctrina delivery list` from the workspace", code: EXIT.USAGE };
392
+ const ws = readWorkspace(found.dir);
393
+ const project = projectAt(ws, projectRoot);
394
+ const d = readDelivery(found.dir, id);
395
+ if (!project || !d.projects.includes(project.name)) {
396
+ return {
397
+ error: `delivery ${id} is not routed to ${project ? project.name : "this repository"} (routed: ${d.projects.join(", ") || "none"})`,
398
+ hint: `route it first, from the workspace: \`doctrina delivery route ${id} ${[...d.projects, project?.name ?? "<name>"].join(",")}\``,
399
+ code: EXIT.PRECONDITION,
400
+ };
401
+ }
402
+ return { id };
403
+ }
404
+
335
405
  function writeLane(projectRoot, id, verdict, opts) {
336
406
  const line = laneRecord(verdict, opts);
337
407
  if (!line) return;
@@ -496,6 +566,9 @@ function printPlaybook(projectRoot, { id, prompt, pinned, matches, capability, c
496
566
  CHANGE_ID: id,
497
567
  PROMPT: prompt,
498
568
  CAPABILITY: capability ?? "<capability>",
569
+ // The read pack already cut to the task (change 0217): `--for` is what
570
+ // slices the spec, and an agent copying a step should copy the cut.
571
+ FOR: forQuery(prompt),
499
572
  FROM_DIFF_NOTE: fromDiff
500
573
  ? c.gray("Code-first: write the spec that describes what the working tree already does.")
501
574
  : "",
@@ -534,6 +607,13 @@ function printPlaybook(projectRoot, { id, prompt, pinned, matches, capability, c
534
607
  });
535
608
  }
536
609
 
610
+ // The prompt as a `context --for` argument: one line, no double quotes, short
611
+ // enough to read — the slicer only needs its content words.
612
+ export function forQuery(prompt) {
613
+ const flat = String(prompt ?? "").replace(/["`]/g, "").replace(/\s+/g, " ").trim();
614
+ return flat.length > 80 ? flat.slice(0, 80).replace(/\s+\S*$/, "") : flat;
615
+ }
616
+
537
617
  // Which capability the change is about, as far as the CLI can tell: pinned by
538
618
  // the operator, ranked by term overlap, or nothing — a hint in every case,
539
619
  // never a decision (ADR 0005).
@@ -601,6 +681,10 @@ Options:
601
681
  existing change folder, and proceed past the lane hold
602
682
  below
603
683
 
684
+ A request that mostly repeats a change already open, or archived in the
685
+ last 14 days, is HELD the same way (exit 3), naming it: the same work is
686
+ resumed, or reported as done where it is archived; --force opens it anyway.
687
+
604
688
  A prompt that reads as a RUNTIME problem — a workflow, an env value, a run
605
689
  that executed nothing — is HELD before anything is scaffolded, and pointed
606
690
  at \`doctrina triage\` instead (exit 3: a precondition, not a bad prompt).
@@ -0,0 +1,409 @@
1
+ // @ts-check
2
+ import path from "node:path";
3
+ import process from "node:process";
4
+ import { spawnSync } from "node:child_process";
5
+ import { readFileSync } from "node:fs";
6
+ import { fileURLToPath } from "node:url";
7
+ import { exists, isDir, mkdirp, relPath, write } from "../lib/fs-ops.js";
8
+ import { flagString } from "../lib/args.js";
9
+ import { c } from "../lib/colors.js";
10
+ import { suggest } from "../lib/suggest.js";
11
+ import { EXIT, PreconditionError } from "../lib/exit-codes.js";
12
+ import {
13
+ PROJECT_MODES, PROJECT_NAME, WORKSPACE_FILE, applyLinks, blankWorkspace, excludeEntries,
14
+ findWorkspace, isWorkspaceDir, linkPlan, projectAt, projectState, readWorkspace, removeLinks,
15
+ shadowDirOf, writeExclude, writePointer, writeWorkspace, gitInit,
16
+ } from "../lib/workspace.js";
17
+ import { TOOL_PRESETS, guardDecision, splitClaudeToolName, toolEntry } from "../lib/workspace-tools.js";
18
+ import { writeOrchestratorFiles } from "../lib/workspace-agents.js";
19
+
20
+ // `doctrina workspace` — the context of several repositories in one place
21
+ // (ADR 0029). The decisions live in lib/workspace.js; this module renders
22
+ // them and runs the one child process it needs (`init` inside a shadow).
23
+
24
+ const SUBCOMMANDS = ["init", "add", "attach", "detach", "list", "tool", "guard"];
25
+ const CLI_ENTRY = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", "index.js");
26
+
27
+ export const flags = {
28
+ boolean: ["json", "no-git", "remove"],
29
+ string: ["name", "mode", "role", "about", "depends-on", "agent", "path", "server", "access", "for", "purpose", "hook"],
30
+ };
31
+
32
+ export async function run(positional, flags) {
33
+ const sub = positional[0];
34
+ if (!SUBCOMMANDS.includes(sub)) {
35
+ console.error(c.red("error:") + ` unknown workspace subcommand "${sub ?? ""}"`);
36
+ const guess = suggest(sub, SUBCOMMANDS);
37
+ console.error(c.gray("hint: ") + (guess ? `did you mean \`doctrina workspace ${guess}\`?` : `available: ${SUBCOMMANDS.join(", ")}`));
38
+ return EXIT.USAGE;
39
+ }
40
+ if (sub === "init") return workspaceInit(positional[1], flags);
41
+ const found = findWorkspace(process.cwd());
42
+ // The guard answers an agent's pre-call hook, so it never fails the call
43
+ // for want of a workspace: no workspace, no opinion.
44
+ if (sub === "guard") return workspaceGuard(found?.dir ?? null, flagString(flags, "hook"));
45
+ if (!found) {
46
+ throw new PreconditionError(
47
+ "no workspace here (no workspace.json in this directory or above, and $DOCTRINA_WORKSPACE is unset)",
48
+ "doctrina workspace init <dir>",
49
+ );
50
+ }
51
+ let code;
52
+ if (sub === "add") code = workspaceAdd(found.dir, positional[1], flags);
53
+ else if (sub === "attach") code = workspaceAttach(found.dir, positional[1], flags);
54
+ else if (sub === "detach") code = workspaceDetach(found.dir, positional[1]);
55
+ else if (sub === "tool") code = workspaceTool(found.dir, positional[1], flags);
56
+ else return workspaceList(found.dir);
57
+ if (code === EXIT.OK) regenerate(found.dir);
58
+ return code;
59
+ }
60
+
61
+ function workspaceInit(dirArg, flags) {
62
+ const dir = path.resolve(process.cwd(), dirArg ?? ".");
63
+ if (isWorkspaceDir(dir)) {
64
+ console.error(c.red("error:") + ` ${dir} is already a workspace`);
65
+ console.error(c.gray("hint: ") + "add a project to it: `doctrina workspace add <path>`");
66
+ return EXIT.PRECONDITION;
67
+ }
68
+ const name = flagString(flags, "name") ?? path.basename(dir);
69
+ mkdirp(path.join(dir, "projects"));
70
+ mkdirp(path.join(dir, "deliveries"));
71
+ writeWorkspace(dir, blankWorkspace(name));
72
+ if (!exists(path.join(dir, ".gitignore"))) {
73
+ write(path.join(dir, ".gitignore"), "node_modules/\n.env\n.env.*\n*.log\n");
74
+ }
75
+ for (const keep of ["projects", "deliveries"]) {
76
+ if (!exists(path.join(dir, keep, ".gitkeep"))) write(path.join(dir, keep, ".gitkeep"), "");
77
+ }
78
+ console.log(c.green("created") + ` workspace "${name}" at ${dir}`);
79
+ if (!flags.get("no-git") && !exists(path.join(dir, ".git"))) {
80
+ console.log(gitInit(dir)
81
+ ? c.green("created") + " a git repository for it — keep it private: it versions the context of repositories that cannot carry their own"
82
+ : c.yellow("warn:") + " git is not available; the workspace is not versioned yet (`git init` it yourself)");
83
+ }
84
+ writePointer(dir);
85
+ regenerate(dir);
86
+ console.log("");
87
+ console.log("Next: register each repository the work touches —");
88
+ console.log(" " + c.cyan("doctrina workspace add <path> --role front|back|app|qa --about \"<what it is>\""));
89
+ console.log(c.gray(" A repository with its own .doctrina/ is registered in-repo; one without is shadowed: its"));
90
+ console.log(c.gray(" artifacts live here and are linked into it, invisible to its git."));
91
+ return EXIT.OK;
92
+ }
93
+
94
+ function workspaceAdd(wsDir, pathArg, flags) {
95
+ if (!pathArg) {
96
+ console.error(c.red("error:") + " workspace add needs the repository's path");
97
+ console.error(c.gray("hint: ") + "doctrina workspace add ../front-core --role front --about \"design system and shell\"");
98
+ return EXIT.USAGE;
99
+ }
100
+ const repo = path.resolve(process.cwd(), pathArg);
101
+ if (!isDir(repo)) {
102
+ console.error(c.red("error:") + ` not a directory: ${pathArg}`);
103
+ return EXIT.PRECONDITION;
104
+ }
105
+ const ws = readWorkspace(wsDir);
106
+ const name = flagString(flags, "name") ?? path.basename(repo).toLowerCase();
107
+ if (!PROJECT_NAME.test(name)) {
108
+ console.error(c.red("error:") + ` project name "${name}" must match ${PROJECT_NAME} — pass --name`);
109
+ return EXIT.USAGE;
110
+ }
111
+ if (ws.projects.some((p) => p.name === name)) {
112
+ console.error(c.red("error:") + ` a project named "${name}" is already registered`);
113
+ console.error(c.gray("hint: ") + `relink it with \`doctrina workspace attach ${name}\`, or pass another --name`);
114
+ return EXIT.PRECONDITION;
115
+ }
116
+ const already = projectAt(ws, repo);
117
+ if (already) {
118
+ console.error(c.red("error:") + ` ${repo} is already registered as "${already.name}"`);
119
+ return EXIT.PRECONDITION;
120
+ }
121
+ const hasOwn = isDir(path.join(repo, ".doctrina"));
122
+ const mode = flagString(flags, "mode") ?? (hasOwn ? "in-repo" : "shadow");
123
+ if (!PROJECT_MODES.includes(mode)) {
124
+ console.error(c.red("error:") + ` --mode must be one of ${PROJECT_MODES.join(", ")} (got "${mode}")`);
125
+ return EXIT.USAGE;
126
+ }
127
+ if (mode === "shadow" && hasOwn) {
128
+ console.error(c.red("error:") + " this repository already carries its own .doctrina/ — register it in-repo");
129
+ return EXIT.PRECONDITION;
130
+ }
131
+ const dependsOn = (flagString(flags, "depends-on") ?? "").split(",").map((s) => s.trim()).filter(Boolean);
132
+ const unknown = dependsOn.filter((d) => !ws.projects.some((p) => p.name === d));
133
+ if (unknown.length > 0) {
134
+ console.error(c.red("error:") + ` --depends-on names no registered project: ${unknown.join(", ")}`);
135
+ return EXIT.USAGE;
136
+ }
137
+ const project = {
138
+ name, path: repo, mode,
139
+ role: flagString(flags, "role") ?? "",
140
+ about: flagString(flags, "about") ?? "",
141
+ dependsOn,
142
+ };
143
+ if (mode === "shadow") {
144
+ const shadow = shadowDirOf(wsDir, name);
145
+ if (!isDir(path.join(shadow, ".doctrina"))) {
146
+ mkdirp(shadow);
147
+ const agent = flagString(flags, "agent") ?? "cursor";
148
+ const r = spawnSync(process.execPath, [CLI_ENTRY, "init", "--non-interactive",
149
+ "--project-name", name, "--project-description", project.about || `${name} (absorbed into a workspace)`,
150
+ "--agent", agent], { cwd: shadow, encoding: "utf8", env: { ...process.env, NO_COLOR: "1" } });
151
+ if (r.status !== 0) {
152
+ console.error(c.red("error:") + ` could not scaffold the shadow at ${shadow}`);
153
+ console.error(r.stderr.trim());
154
+ return EXIT.GATE;
155
+ }
156
+ console.log(c.green("created") + ` shadow ${relPath(wsDir, shadow)}/ (AGENTS.md, .doctrina/, ${agent} adapter)`);
157
+ }
158
+ }
159
+ ws.projects.push(project);
160
+ writeWorkspace(wsDir, ws);
161
+ console.log(c.green("registered") + ` ${name} (${mode}) → ${repo}`);
162
+ if (mode === "shadow") attachShadow(wsDir, project);
163
+ printProjectNext(wsDir, project);
164
+ return EXIT.OK;
165
+ }
166
+
167
+ function attachShadow(wsDir, project) {
168
+ const shadow = shadowDirOf(wsDir, project.name);
169
+ const plan = linkPlan(shadow, project.path);
170
+ const done = applyLinks(plan, shadow, project.path);
171
+ for (const d of done) {
172
+ console.log(c.green(d.how.padEnd(11)) + ` ${d.rel}` + (d.how === "copied" ? c.yellow(" (a copy: rerun attach after the shadow changes)") : ""));
173
+ }
174
+ for (const e of plan.filter((p) => p.action === "conflict")) {
175
+ console.log(c.yellow("kept ") + ` ${e.rel} — the repository already has its own; the shadow's copy is not linked`);
176
+ }
177
+ const excludeFile = writeExclude(project.path, excludeEntries(plan, project.path));
178
+ console.log(excludeFile
179
+ ? c.gray(`hidden from the repository's git by ${relPath(project.path, excludeFile)} (never committed)`)
180
+ : c.yellow("warn:") + " not a git repository — nothing hides the links from version control");
181
+ }
182
+
183
+ function printProjectNext(wsDir, project) {
184
+ const st = projectState(wsDir, project);
185
+ if (st.state === "pending") {
186
+ console.log("");
187
+ console.log(`Next: ${project.name} has no specs yet (${st.reason}) — absorb it from its code:`);
188
+ console.log(" " + c.cyan(`cd ${project.path} && doctrina intake --from-code`));
189
+ }
190
+ }
191
+
192
+ function findProject(ws, name) {
193
+ const project = ws.projects.find((p) => p.name === name);
194
+ if (project) return project;
195
+ const guess = suggest(name ?? "", ws.projects.map((p) => p.name));
196
+ throw Object.assign(new Error(`no project named "${name ?? ""}" in this workspace` + (guess ? ` — did you mean "${guess}"?` : "")), { exitCode: EXIT.USAGE });
197
+ }
198
+
199
+ function workspaceAttach(wsDir, name, flags) {
200
+ const ws = readWorkspace(wsDir);
201
+ const project = findProject(ws, name);
202
+ const newPath = flagString(flags, "path");
203
+ if (newPath) {
204
+ const repo = path.resolve(process.cwd(), newPath);
205
+ if (!isDir(repo)) {
206
+ console.error(c.red("error:") + ` not a directory: ${newPath}`);
207
+ return EXIT.PRECONDITION;
208
+ }
209
+ project.path = repo;
210
+ writeWorkspace(wsDir, ws);
211
+ console.log(c.green("moved") + ` ${project.name} → ${repo}`);
212
+ }
213
+ if (project.mode !== "shadow") {
214
+ console.log(c.gray(`${project.name} is in-repo — its .doctrina/ is its own; nothing to link`));
215
+ return EXIT.OK;
216
+ }
217
+ if (!isDir(project.path)) {
218
+ console.error(c.red("error:") + ` ${project.name}'s repository is not at ${project.path}`);
219
+ console.error(c.gray("hint: ") + `point it at the clone: \`doctrina workspace attach ${project.name} --path <dir>\``);
220
+ return EXIT.PRECONDITION;
221
+ }
222
+ attachShadow(wsDir, project);
223
+ return EXIT.OK;
224
+ }
225
+
226
+ function workspaceDetach(wsDir, name) {
227
+ const ws = readWorkspace(wsDir);
228
+ const project = findProject(ws, name);
229
+ if (project.mode !== "shadow") {
230
+ console.log(c.gray(`${project.name} is in-repo — nothing was linked`));
231
+ return EXIT.OK;
232
+ }
233
+ const removed = isDir(project.path) ? removeLinks(shadowDirOf(wsDir, project.name), project.path) : [];
234
+ for (const r of removed) console.log(c.green("unlinked") + ` ${r}`);
235
+ if (isDir(project.path)) writeExclude(project.path, []);
236
+ console.log(c.gray(`the shadow stays in ${relPath(wsDir, shadowDirOf(wsDir, project.name))}/ — \`doctrina workspace attach ${project.name}\` links it again`));
237
+ return EXIT.OK;
238
+ }
239
+
240
+ // The orchestrator's files follow the registry on every write (change 0223).
241
+ function regenerate(wsDir) {
242
+ const ws = readWorkspace(wsDir);
243
+ const states = new Map(ws.projects.map((p) => [p.name, projectState(wsDir, p).state]));
244
+ const written = writeOrchestratorFiles(ws, wsDir, states);
245
+ if (written.length > 0) console.log(c.gray(`orchestrator files: ${written.join(", ")}`));
246
+ }
247
+
248
+ function workspaceTool(wsDir, name, flags) {
249
+ if (!name) {
250
+ console.error(c.red("error:") + " workspace tool needs the tool's name");
251
+ console.error(c.gray("hint: ") + `presets: ${Object.keys(TOOL_PRESETS).join(", ")} — or --server <name in your agent's MCP config>`);
252
+ return EXIT.USAGE;
253
+ }
254
+ const ws = readWorkspace(wsDir);
255
+ const at = ws.tools.findIndex((t) => t.name === name);
256
+ if (flags.get("remove")) {
257
+ if (at < 0) {
258
+ console.error(c.red("error:") + ` no tool named "${name}"`);
259
+ return EXIT.USAGE;
260
+ }
261
+ ws.tools.splice(at, 1);
262
+ writeWorkspace(wsDir, ws);
263
+ console.log(c.green("removed") + ` tool ${name}`);
264
+ return EXIT.OK;
265
+ }
266
+ let entry;
267
+ try {
268
+ entry = toolEntry(name, {
269
+ server: flagString(flags, "server"), access: flagString(flags, "access"),
270
+ for: flagString(flags, "for"), purpose: flagString(flags, "purpose"),
271
+ }, at >= 0 ? ws.tools[at] : null);
272
+ } catch (err) {
273
+ console.error(c.red("error:") + ` ${err.message}`);
274
+ return EXIT.USAGE;
275
+ }
276
+ if (at >= 0) ws.tools[at] = entry;
277
+ else ws.tools.push(entry);
278
+ writeWorkspace(wsDir, ws);
279
+ console.log(c.green(at >= 0 ? "updated" : "declared") + ` ${entry.name} → MCP server "${entry.server}", ${entry.access === "write" ? "read/write (each write asks the human)" : "read-only"}, for ${(entry.for ?? []).join(", ") || "the planner"}`);
280
+ console.log(c.gray(`the server itself is configured in your agent (Cursor: .cursor/mcp.json; Claude Code: .mcp.json) under the name "${entry.server}"`));
281
+ return EXIT.OK;
282
+ }
283
+
284
+ // Reads the agent's pre-call payload on stdin and answers in the agent's
285
+ // own format. Never exits non-zero and never blocks on bad input: a broken
286
+ // guard must not take the agent's tools away.
287
+ function workspaceGuard(wsDir, hook) {
288
+ if (hook !== "cursor" && hook !== "claude") {
289
+ console.error(c.red("error:") + ` --hook needs cursor or claude (got "${hook ?? ""}")`);
290
+ return EXIT.USAGE;
291
+ }
292
+ let call = {};
293
+ try {
294
+ call = JSON.parse(readFileSync(0, "utf8") || "{}");
295
+ } catch {
296
+ call = {};
297
+ }
298
+ let server = null;
299
+ let tool = null;
300
+ if (hook === "cursor") {
301
+ server = call.mcp_server_name ?? null;
302
+ tool = call.tool_name ?? null;
303
+ } else {
304
+ const split = splitClaudeToolName(call.tool_name ?? "");
305
+ if (split) ({ server, tool } = split);
306
+ }
307
+ const tools = wsDir ? readWorkspace(wsDir).tools : [];
308
+ const verdict = server && tool ? guardDecision(tools, server, tool, call.tool_input) : { decision: "none", reason: "" };
309
+ if (hook === "cursor") {
310
+ const permission = verdict.decision === "none" ? "allow" : verdict.decision;
311
+ const out = { permission };
312
+ if (permission !== "allow") Object.assign(out, { user_message: verdict.reason, agent_message: verdict.reason });
313
+ console.log(JSON.stringify(out));
314
+ } else if (verdict.decision !== "none") {
315
+ console.log(JSON.stringify({ hookSpecificOutput: {
316
+ hookEventName: "PreToolUse", permissionDecision: verdict.decision, permissionDecisionReason: verdict.reason,
317
+ } }));
318
+ }
319
+ return EXIT.OK;
320
+ }
321
+
322
+ function workspaceList(wsDir) {
323
+ const ws = readWorkspace(wsDir);
324
+ console.log(c.bold(`Workspace "${ws.name}"`) + c.gray(` — ${wsDir}`));
325
+ if (ws.projects.length === 0) {
326
+ console.log("");
327
+ console.log(c.gray("no project yet — `doctrina workspace add <path>`"));
328
+ return EXIT.OK;
329
+ }
330
+ console.log("");
331
+ for (const p of ws.projects) {
332
+ const st = projectState(wsDir, p);
333
+ const colour = st.state === "ready" ? c.green : st.state === "pending" ? c.yellow : c.red;
334
+ const open = st.open.length ? `, ${st.open.length} open change${st.open.length === 1 ? "" : "s"}` : "";
335
+ console.log(` ${c.cyan(p.name.padEnd(16))} ${p.mode.padEnd(8)} ${colour(st.state.padEnd(8))} ${st.reason}${open}`);
336
+ const meta = [p.role && `role ${p.role}`, p.dependsOn?.length && `depends on ${p.dependsOn.join(", ")}`].filter(Boolean).join(" · ");
337
+ console.log(c.gray(` ${"".padEnd(16)} ${p.path}${meta ? " · " + meta : ""}`));
338
+ if (p.about) console.log(c.gray(` ${"".padEnd(16)} ${p.about}`));
339
+ }
340
+ const pending = ws.projects.filter((p) => projectState(wsDir, p).state === "pending");
341
+ if (pending.length > 0) {
342
+ console.log("");
343
+ console.log(`Next: absorb ${pending.map((p) => p.name).join(", ")} — ` + c.cyan(`cd ${pending[0].path} && doctrina intake --from-code`));
344
+ }
345
+ console.log("");
346
+ if (ws.tools.length === 0) {
347
+ console.log(c.gray("no tool declared — `doctrina workspace tool jira` (presets: " + Object.keys(TOOL_PRESETS).join(", ") + ")"));
348
+ } else {
349
+ console.log(c.bold("Tools"));
350
+ for (const t of ws.tools) {
351
+ console.log(` ${c.cyan(t.name.padEnd(12))} ${t.server.padEnd(12)} ${t.access.padEnd(6)} ${(t.for ?? []).join(", ").padEnd(16)} ${c.gray(t.purpose ?? "")}`);
352
+ }
353
+ }
354
+ return EXIT.OK;
355
+ }
356
+
357
+ export const help = `
358
+ Usage: doctrina workspace <init|add|attach|detach|list> [options]
359
+
360
+ The context of several repositories in one place (ADR 0029). Keep the
361
+ workspace directory as a PRIVATE git repository: it versions the
362
+ artifacts of repositories that cannot carry .doctrina/ themselves.
363
+
364
+ init [<dir>] [--name <n>] [--no-git]
365
+ Create the workspace (workspace.json, projects/, deliveries/) and
366
+ \`git init\` it. Records it in $DOCTRINA_HOME (default ~/.doctrina)
367
+ so an in-repo project can find it.
368
+
369
+ add <path> [--name <n>] [--mode in-repo|shadow] [--role <r>]
370
+ [--about "<what it is>"] [--depends-on <a,b>] [--agent cursor]
371
+ Register a repository. One with its own .doctrina/ is in-repo. One
372
+ without is a SHADOW: its AGENTS.md, .doctrina/ and agent adapter are
373
+ scaffolded under projects/<name>/, linked into the repository, and
374
+ hidden from its git through .git/info/exclude.
375
+
376
+ attach <name> [--path <dir>]
377
+ Re-create a shadow's links (after cloning the workspace on another
378
+ machine, or with --path after the repository moved).
379
+
380
+ detach <name>
381
+ Remove a shadow's links and its exclude block; the shadow stays.
382
+
383
+ list
384
+ Every project with its mode, state (ready / pending / missing) and
385
+ open changes, then the declared tools.
386
+
387
+ tool <name> [--server <mcp-name>] [--access read|write] [--for <roles>]
388
+ [--purpose "<what for>"] [--remove]
389
+ Declare (or update, or --remove) a tool the work uses. Presets:
390
+ jira, confluence, figma, playwright, datadog, databricks, sql.
391
+ Roles: planner, executor, qa, observer, reviewer. Access defaults
392
+ to read: the guard refuses a write through a read-only tool and
393
+ makes a declared write wait for the human.
394
+
395
+ guard --hook cursor|claude
396
+ The pre-call hook the workspace installs (Cursor beforeMCPExecution,
397
+ Claude Code PreToolUse): reads the call on stdin and answers allow,
398
+ deny or ask from the declared access. Never blocks on bad input.
399
+
400
+ Every write (init, add, attach, detach, tool) regenerates the orchestrator
401
+ files at the workspace root: AGENTS.md, .cursor/rules/00-doctrina-
402
+ workspace.mdc, .cursor/hooks.json and .claude/settings.json (merged),
403
+ .claude/agents/doctrina-*.md (read by Claude Code and Cursor) and
404
+ <name>.code-workspace, which opens every repository in one window.
405
+
406
+ The workspace is found from $DOCTRINA_WORKSPACE, the current directory
407
+ or an ancestor holding ${WORKSPACE_FILE}, a shadow's .doctrina link, or
408
+ the pointer \`init\` wrote.
409
+ `;
package/src/index.js CHANGED
@@ -48,6 +48,8 @@ import * as upgrade from "./commands/upgrade.js";
48
48
  import * as adapter from "./commands/adapter.js";
49
49
  import * as triage from "./commands/triage.js";
50
50
  import * as ci from "./commands/ci.js";
51
+ import * as workspace from "./commands/workspace.js";
52
+ import * as delivery from "./commands/delivery.js";
51
53
 
52
54
  const COMMANDS = {
53
55
  init, spec, change, decision, validate, hooks, analyze, clarify,
@@ -56,18 +58,14 @@ const COMMANDS = {
56
58
  status, close, review, watch, why,
57
59
  prime, handoff, show, doctor, report, completion,
58
60
  intent, upgrade, adapter, triage, ci,
61
+ workspace, delivery,
59
62
  };
60
63
 
61
- const TOP_HELP = `
62
- Usage: doctrina <command> [options] (\`doctrina <command> --help\` for its flags)
64
+ const TOP_HELP = `Usage: doctrina <command> [options] (\`doctrina <command> --help\` for its flags)
63
65
 
64
66
  ${surfaceHelp()}
65
67
 
66
- Global flags:
67
- --help, -h Show this message (or per-command help if after a command)
68
- --version, -v Print the version
69
- --debug On an unexpected error, also print the stack trace
70
-
68
+ Global flags: --help/-h (this, or a command's own) · --version/-v · --debug (stack traces)
71
69
  Exit codes (a contract — an agent reads these to decide what to do next):
72
70
  ${exitCodeHelp()}
73
71
  `;
@@ -151,7 +151,7 @@ export function computeActions(projectRoot, gates = {}) {
151
151
  severity: "blocking",
152
152
  why: "no capability is specced yet, so nothing states what this project must do",
153
153
  text: 'doctrina intake --text "<what this project is>" — no capability is specced yet; ' +
154
- "for an existing codebase, `doctrina work --from-diff` backfills from the code instead",
154
+ "for an existing codebase, `doctrina intake --from-code` takes its inventory as the intake (`doctrina work --from-diff` backfills one change from code already written)",
155
155
  }));
156
156
  }
157
157