@dzhechkov/harness-cli 0.3.262 → 0.4.2

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/src/cli.ts CHANGED
@@ -4,21 +4,23 @@
4
4
  * @packageDocumentation
5
5
  */
6
6
 
7
- import { appendFileSync, chmodSync, closeSync, existsSync, fstatSync, lstatSync, mkdirSync, mkdtempSync, openSync, readFileSync, readSync, readdirSync, readlinkSync, realpathSync, renameSync, rmdirSync, rmSync, statSync, symlinkSync, unlinkSync, writeFileSync } from 'node:fs';
8
- import { basename, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
7
+ import { appendFileSync, chmodSync, closeSync, cpSync, existsSync, fstatSync, lstatSync, mkdirSync, mkdtempSync, openSync, readFileSync, readSync, readdirSync, readlinkSync, realpathSync, renameSync, rmdirSync, rmSync, statSync, symlinkSync, unlinkSync, writeFileSync } from 'node:fs';
8
+ import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from 'node:path';
9
9
  import { fileURLToPath } from 'node:url';
10
10
  import { execFileSync, execSync, spawn } from 'node:child_process';
11
+ import { createHash } from 'node:crypto';
11
12
  import { homedir, tmpdir } from 'node:os';
12
13
  import { createRequire } from 'node:module';
13
14
 
14
15
  import {
15
16
  createSkill,
16
17
  getSkillInfo,
17
- getWorkflow,
18
18
  isTargetName,
19
19
  listSkills,
20
20
  runDoctor,
21
21
  runInit,
22
+ resolvePackageSkillRoots,
23
+ PACKAGE_SKILL_LAYOUTS,
22
24
  benchmarkSkill,
23
25
  benchmarkSkills,
24
26
  scanMcp,
@@ -51,7 +53,22 @@ import {
51
53
  buildParityMatrix,
52
54
  TARGET_CAPABILITIES,
53
55
  TARGET_SHORT_LABELS,
54
- WORKFLOW_NAMES,
56
+ WORKFLOW_TEMPLATES_RETIRED_MESSAGE,
57
+ parsePlan,
58
+ isParseErrors,
59
+ validatePlan,
60
+ normalizePlan,
61
+ planDigest,
62
+ toTraceProjection,
63
+ renderPlan,
64
+ mergeRender,
65
+ lint,
66
+ lintExitCode,
67
+ LOOP_BLOBS,
68
+ parseTrace,
69
+ assembleTimeline,
70
+ runInvariants,
71
+ renderTimelineHtml,
55
72
  importEcc,
56
73
  recordPattern,
57
74
  resolveLearningBackend,
@@ -128,6 +145,7 @@ import {
128
145
  guardExitCode,
129
146
  DEFAULT_RULES,
130
147
  parsePnpmLockImporters,
148
+ scannableStubPath,
131
149
  // guard-promotion (feature guard-promotion, scout idea #1)
132
150
  assembleCandidates,
133
151
  renderPromotionReport,
@@ -264,6 +282,10 @@ import {
264
282
  isSafeId,
265
283
  alignIdea,
266
284
  mirrorIdeaVector,
285
+ ensureBacklogEmbedForm,
286
+ readBacklogEmbedFormVersion,
287
+ recordAbsorption,
288
+ DEDUP_EMBED_FORM_VERSION,
267
289
  snapshotIdeas,
268
290
  spinRoulette,
269
291
  rankRoulette,
@@ -274,8 +296,18 @@ import {
274
296
  resolveJiraAdapter,
275
297
  makeBacklogIO,
276
298
  harmonizeBacklog,
299
+ transitionIdeas,
277
300
  BACKLOG_BACKENDS,
278
301
  applyDomainBoost,
302
+ DZ_OWNED_TASK_TYPES,
303
+ applyExportHoldout,
304
+ DEFAULT_HELD_OUT_DOMAINS,
305
+ canonicalDomainKey,
306
+ readAgentdbRowsByTaskType,
307
+ heldOutAfterOptIn,
308
+ renderHoldoutNote,
309
+ renderSharedStoreAdvice,
310
+ decideVectorExport,
279
311
  countDisplacedByCut,
280
312
  renderDomainBoostNote,
281
313
  renderDomainCutNote,
@@ -284,7 +316,18 @@ import {
284
316
  settleReqeDebt,
285
317
  renderReqeList,
286
318
  REQE_SCOPE,
319
+ // Mutation gate (feature ha-mutation-gate) — break each named protection, run the suite, require red.
320
+ parseMutationRegistry,
321
+ applyMutationToText,
322
+ countFailingTests,
323
+ classifyBaseline,
324
+ classifyRunFailure,
325
+ classifyMutationOutcome,
326
+ mutationGateExitCode,
327
+ summarizeMutationResults,
328
+ renderMutationReport,
287
329
  } from '@dzhechkov/harness-core';
330
+ import type { MutationEntryResult, MutationObservation, MutationRegistryEntry } from '@dzhechkov/harness-core';
288
331
  import type { ReqeDebt } from '@dzhechkov/harness-core';
289
332
  import type { IdeaRecord, IdeaStatus } from '@dzhechkov/harness-core';
290
333
  import type { Family, ModelRung, Candidate as BtoCandidate, DimScores } from '@dzhechkov/harness-core';
@@ -306,7 +349,12 @@ Usage:
306
349
  dz migrate [--project <dir>]
307
350
  dz create-skill --name <id> [--description <text>] [--skills-dir <dir>] [--tier <1-3>] [--with-references] [--no-evals] [--bto]
308
351
  dz scout [--topics <list>] [--since <date>] [--deep] [--output <file>] [--diff] [--report]
309
- dz workflow <task> [--dry-run]
352
+ dz workflow init --name <n> [--pattern pipeline|barrier|fanout|gate] [--o <plan.json>] (scaffold a loop-plan/1 plan)
353
+ dz workflow validate <plan.json> [--json] (schema + INV-1..8 checks; CI-runnable, non-zero on failure)
354
+ dz workflow render <plan.json> --o <script.js> [--check] [--force] (plan → region-delimited loop script; USER regions preserved)
355
+ dz workflow blobs [--check] (list/self-check the subsystem blob registry)
356
+ dz workflow-lint <script.js> [--plan <plan.json>] [--require-plan|--legacy] [--json] (layer-1 gate; exit 0/1/3 — inconclusive is never a pass)
357
+ dz workflow-trace <runDir|--slug <s>|--run <id>> [--invariants <plan.json>] [--html <out.html>] [--json] (timeline + SEQ invariant runner)
310
358
  dz install <npm-pkg> [--target <name>] [--project <dir>] [--force]
311
359
  dz bundle [--preset <name> | --select id,id,...] [--out <dir>] [--skills-dir <dir>] [--force] (portable self-contained skill bundles for a generic/LangGraph consumer)
312
360
  dz doctor [--project <dir>] [--pubkey <path>] [--require-signing] (health + signature check of installed packs)
@@ -325,11 +373,15 @@ Usage:
325
373
  dz epoch-replay --score <judgments.json> --work-order <file> [--slice <name>] [--json] (un-blind against the pre-registered assignment → SUPPORTED only when the two 95% Wilson CIs are DISJOINT, else FALSIFIED / INCONCLUSIVE)
326
374
  dz score --slug <feature> [--project <dir>] [--json] (process scorecard for ONE feature-adr run, from its artifacts: ADR confirmation, discrimination, cross-model QE grade, live verification, README-first, learning loop, amendments — descriptive-only, a low score exits 0)
327
375
  dz reqe [--slug <feature> [--done --report <f>]] [--json] (the re-QE debt ledger: a usage-switched run whose Step-8 QE ran on the coder's OWN family records a debt; list debts, print the cross-family review brief, settle FAIL-CLOSED against a graded report — the settlement lands in 08_qe_report.md)
376
+ dz mutation-gate [--package <dir>] [--registry <file>] [--test-cmd "<cmd>"] [--only <id[,id]>] [--timeout <ms>] [--rebaseline per-entry|final] [--keep-scratch] [--json] (prove each NAMED protection has a test that DISCRIMINATES: copy the package to a scratch dir, verify the baseline suite is green, apply each registry mutation, run the suite, REQUIRE red, restore. The red must be BEHAVIOURAL: a mutation that no longer parses is MUTATION_UNPARSEABLE; a red run whose OWN output reports a test FILE failing to load (node --test file-level not-ok with exitCode, vitest Failed Suites) is MUTATION_LOAD_FATAL — the signal comes from the same run as the failing count, never from a separate isolated import; red output whose shape matches no known runner is INCONCLUSIVE (a runner-coverage gap, loud, never PROVEN); a count far above the entry's bound is OVER_FAILING; a restored tree that does not reproduce green makes the entry INCONCLUSIVE (flaky). Mutation writes are realpath-contained to the scratch copy: a symlink escape or a node_modules/ target is refused (exit 2), the real tree is never written. A mutation that does not apply, a green suite, or an inconclusive run is a FAILURE — never a skip. exit 0 all proven / 1 gate failed / 2 setup error)
328
377
  dz backlog add "<idea>" [--effort 1-5] [--proposal <text>] [--dry-run] [--project <dir>] [--json] (capture an idea: semantic dedup against existing ideas via the Brain vector engine (DUPLICATE>=0.92 merges, RELATED links, NEW creates) + GoalMap alignment; --dry-run classifies without writing)
329
378
  dz backlog list [--status <s>] [--goal <id>] [--project <dir>] [--json] (list captured ideas, filterable by status/goal)
330
379
  dz backlog show <id> [--project <dir>] [--json] (full record for one idea)
331
380
  dz backlog goals [--validate] [--project <dir>] [--json] (list/validate the compass at .dz/backlog/goals.json)
332
- dz backlog roulette [--pick <N>] [--seed <n>] [--commit] [--project <dir>] [--json] (WEIGHTED draw over eligible ideas: alignment^alpha * recencyDecay * 1/effort, seeded; --pick N = ranked shortlist; --commit flips the pick to in-progress (the only mutation))
381
+ dz backlog roulette [--pick <N>] [--seed <n>] [--commit] [--project <dir>] [--json] (WEIGHTED draw over eligible ideas: alignment^alpha * recencyDecay * 1/effort, seeded; --pick N = ranked shortlist; --commit flips the pick to in-progress)
382
+ dz backlog ship <id> [<id>…] [--reason <t>] [--dry-run] [--project <dir>] [--json] (mark work DONE: new|enriched|in-progress → shipped, removing it from the roulette pool — run it after finishing a task; short id prefixes ok, ambiguous = loud error)
383
+ dz backlog drop <id> [<id>…] [--reason <t>] [--dry-run] [--project <dir>] [--json] (retire an idea: new|enriched|in-progress → dropped)
384
+ dz backlog reopen <id> [<id>…] [--reason <t>] [--dry-run] [--project <dir>] [--json] (back to the pool: shipped|dropped|in-progress → new)
333
385
  dz backlog enrich <id> [--project <dir>] [--json] (stage the idea2prd input scaffold in features/<slug>/ and hand off to the idea2prd-manual skill — the CLI never fabricates a PRD)
334
386
  dz backlog jira <id> [--project <dir>] [--json] (draft a Jira issue via the configurable adapter (backlog.jira.adapter: jira-mcp|copilot-mcp|none); none writes an auditable jira-outbox/<id>.json stub)
335
387
  dz backlog harmonize [--apply] [--threshold <0-1>] [--project <dir>] [--json] (batch semantic dedup of the backlog ideas; --dry-run default, --apply snapshots first)
@@ -354,7 +406,7 @@ Usage:
354
406
  dz brain expand <kuId> [--source <slug>] [--json] (full-content lookup for a citation kuId; --json emits the full KU object)
355
407
  dz brain init [--project <dir>] [--k <N>] (wire the grounding hook into .claude/settings.json — opt-in)
356
408
  dz statusline [--json] [--install] [--project <dir>] (live self-learning panel for Claude Code's status bar; reads the CC JSON payload from STDIN)
357
- dz statusline --fa-record --slug <s> --step "<label>" [--recalled <n>] [--stored <n>] [--mode <m>] (feature-adr: record live per-run learning state → 📐 panel segment)
409
+ dz statusline --fa-record --slug <s> --step "<label>" [--kind <feature-adr|loop>] [--recalled <n>] [--stored <n>] [--mode <m>] (feature-adr: record live per-run learning state → 📐 panel segment)
358
410
  dz usage [--json] [--project <dir>] | dz usage --calibrate --session <pct> --weekly <pct> [--model fable=<pct>] [--project <dir>] (ESTIMATE Claude usage from fixed reset windows; optional per-model weekly binding; exit 0 ALWAYS; pct=null when limits unconfigured)
359
411
  dz usage --by-stage [--run <runId> | --slug <slug>] [--epsilon <0..1>] [--write <file.jsonl>] [--json] (per-stage cost ledger for ONE feature-adr run + the reconciliation invariant: accounted + unaccounted = run total; verdict BALANCED | DEFECT | INSUFFICIENT_DATA; local transcript ESTIMATES — catches ATTRIBUTION errors, not pricing errors)
360
412
  dz claim-check [paths...] [--json] [--fail-on high|medium|none] [--project <dir>] (enforce the Integrity Rule: flag untagged/overstated accuracy claims; default scan = root README.md + every discovered package's README.md + features/*/08_qe_report.md + docs/**/*.md (historical feature artifacts are NOT scanned — pass paths explicitly); exit 1 only at/above --fail-on, default high)
@@ -379,7 +431,7 @@ Usage:
379
431
  dz import-ecc [--local-path <dir>] [--select id,id,...] [--limit N] [--output <dir>] [--force]
380
432
  dz help
381
433
 
382
- Workflows: coverage-lift, mutation-kill, canonicalize, security-audit
434
+ Workflows: author loop-plan/1 plans with dz workflow init/validate/render; gate them with dz workflow-lint; read runs with dz workflow-trace (the ADR-005 templates are retired)
383
435
 
384
436
  Targets: ${TARGET_NAMES.join(', ')}
385
437
  Presets: ${PRESET_NAMES.join(', ')}`;
@@ -401,6 +453,13 @@ export interface CliIo {
401
453
  * without spawning anything.
402
454
  */
403
455
  readonly releaseRunner?: ReleaseExecRunner;
456
+ /**
457
+ * Test seam for `dz install`: overrides the `npm install` subprocess (production leaves
458
+ * it unset → real `execSync`, stdio piped). A stub runner that pre-stages a fixture
459
+ * package under `node_modules/` makes `cmdInstall`'s layout resolution testable
460
+ * offline, hermetically — mirrors the {@link CliIo.releaseRunner} idiom.
461
+ */
462
+ readonly installRunner?: (command: string, cwd: string) => void;
404
463
  }
405
464
 
406
465
  /** Injected subprocess runner used by `dz release` (see {@link CliIo.releaseRunner}). */
@@ -840,28 +899,310 @@ async function cmdScout(options: Map<string, string>, flags: Set<string>, cwd: s
840
899
  }
841
900
  }
842
901
 
902
+ /** Scaffold plans for `dz workflow init` — one per pattern (each validates + renders lint-clean:
903
+ * the ADR-002 property "the generator cannot emit a script its own lint rejects" is enforced by
904
+ * workflow-init-lint-clean.test.ts, not convention). */
905
+ function workflowInitPlan(name: string, pattern: string): object {
906
+ const base = {
907
+ schema: 'loop-plan/1',
908
+ name,
909
+ description: `TODO: describe the ${name} loop`, // no-stubs: workflow-init scaffold sentinel the author replaces (deliberate authoring cue, not unfinished code)
910
+ whenToUse: `TODO: when to invoke ${name}`, // no-stubs: workflow-init scaffold sentinel the author replaces (deliberate authoring cue, not unfinished code)
911
+ checkpointing: { enabled: false },
912
+ subsystems: { checkpoints: false, trainingPairs: false, usageAdaptive: false, challengePanel: false, codexDispatch: false },
913
+ trace: { emit: true },
914
+ };
915
+ if (pattern === 'pipeline') {
916
+ return {
917
+ ...base,
918
+ steps: [
919
+ { stepId: 'fan', kind: 'fanout', phase: 'Work', concurrency: 'pipeline', budget: { maxAgents: 8 } },
920
+ { stepId: 'a', kind: 'agent', phase: 'Work', prompt: 'TODO: stage A per item', budget: { maxAgents: 4 } }, // no-stubs: workflow-init scaffold sentinel the author replaces (deliberate authoring cue, not unfinished code)
921
+ { stepId: 'b', kind: 'agent', phase: 'Work', prompt: 'TODO: stage B per item', budget: { maxAgents: 4 } }, // no-stubs: workflow-init scaffold sentinel the author replaces (deliberate authoring cue, not unfinished code)
922
+ { stepId: 'jn', kind: 'join', phase: 'Work', deps: ['fan'] },
923
+ ],
924
+ fanouts: [{ stage: 'fan', registry: ['item1', 'item2', 'item3'], maxFanout: 3, chain: ['a', 'b'] }],
925
+ joins: [{ stage: 'jn', forStage: 'fan', joinPolicy: 'all-activated', onInvalid: 'named-failure' }],
926
+ };
927
+ }
928
+ if (pattern === 'fanout' || pattern === 'barrier') {
929
+ return {
930
+ ...base,
931
+ steps: [
932
+ { stepId: 'fan', kind: 'fanout', phase: 'Lanes', concurrency: 'barrier', budget: { maxAgents: 6 } },
933
+ { stepId: 'lane', kind: 'agent', phase: 'Lanes', prompt: 'TODO: one lane', budget: { maxAgents: 6 } }, // no-stubs: workflow-init scaffold sentinel the author replaces (deliberate authoring cue, not unfinished code)
934
+ { stepId: 'jn', kind: 'join', phase: 'Lanes', deps: ['fan'] },
935
+ // the consumer hangs off the BARRIER (jn), never the fork — barrier-postdominates teaches this
936
+ { stepId: 'synthesize', kind: 'agent', phase: 'Synthesize', deps: ['jn'], prompt: 'TODO: synthesize across lanes', budget: { maxAgents: 1 } }, // no-stubs: workflow-init scaffold sentinel the author replaces (deliberate authoring cue, not unfinished code)
937
+ ],
938
+ fanouts: [{ stage: 'fan', registry: ['lane1', 'lane2', 'lane3'], maxFanout: 3, chain: ['lane'] }],
939
+ joins: [{ stage: 'jn', forStage: 'fan', joinPolicy: 'all-activated', onInvalid: 'named-failure' }],
940
+ };
941
+ }
942
+ if (pattern === 'gate') {
943
+ return {
944
+ ...base,
945
+ steps: [
946
+ { stepId: 'work', kind: 'agent', phase: 'Work', prompt: 'TODO: produce the artifact', budget: { maxAgents: 2 } }, // no-stubs: workflow-init scaffold sentinel the author replaces (deliberate authoring cue, not unfinished code)
947
+ { stepId: 'gate', kind: 'gate', phase: 'Gate', deps: ['work'], prompt: 'TODO: gate check (parse the verdict, never synthesize one)', budget: { maxAgents: 1 } }, // no-stubs: workflow-init scaffold sentinel the author replaces (deliberate authoring cue, not unfinished code)
948
+ ],
949
+ gates: [{ stepId: 'gate', kind: 'parse-verdict', failRoute: 'work', maxRedos: 1 }],
950
+ };
951
+ }
952
+ // minimal default: one agent step
953
+ return {
954
+ ...base,
955
+ steps: [{ stepId: 'main', kind: 'agent', phase: 'Work', prompt: 'TODO: the one step', budget: { maxAgents: 1 } }], // no-stubs: workflow-init scaffold sentinel the author replaces (deliberate authoring cue, not unfinished code)
956
+ };
957
+ }
958
+
959
+ /**
960
+ * `dz workflow` — the loop-designer authoring verbs (ADR-002; the ADR-005 template emitter is
961
+ * RETIRED — AM-6). Subverbs: init | validate | render | blobs. Any other invocation (including
962
+ * every legacy `--task <name>` / `--name <name>` / bare positional template spelling) prints the
963
+ * pinned retirement message and exits 1 — no legacy format is silently reachable.
964
+ */
843
965
  function cmdWorkflow(options: Map<string, string>, flags: Set<string>, cwd: string, write: Write): number {
844
- const taskName = options.get('task') ?? options.get('name') ?? '';
845
- if (taskName === '' || !WORKFLOW_NAMES.includes(taskName)) {
846
- write(`dz workflow: specify a task — one of: ${WORKFLOW_NAMES.join(', ')}`);
966
+ const sub = options.get('_positional_0') ?? '';
967
+ const legacyTask = options.get('task') ?? options.get('name');
968
+
969
+ if (sub === 'init') {
970
+ const name = options.get('name') ?? options.get('_positional_1') ?? 'my-loop';
971
+ const pattern = options.get('pattern') ?? 'pipeline';
972
+ const outPath = resolve(cwd, options.get('o') ?? options.get('out') ?? `${name}.plan.json`);
973
+ const planObj = workflowInitPlan(name, pattern);
974
+ const parsed = parsePlan(planObj);
975
+ if (isParseErrors(parsed)) {
976
+ write(`dz workflow init: internal scaffold error — ${parsed.map((e) => e.message).join('; ')}`);
977
+ return 1;
978
+ }
979
+ const diags = validatePlan(parsed);
980
+ if (diags.length > 0) {
981
+ write(`dz workflow init: internal scaffold failed validation — ${diags.map((d) => `${d.invariant} ${d.message}`).join('; ')}`);
982
+ return 1;
983
+ }
984
+ writeFileSync(outPath, JSON.stringify(normalizePlan(parsed), null, 2) + '\n');
985
+ write(`dz workflow init: wrote ${outPath} (pattern: ${pattern})`);
986
+ write('Next: edit the TODO prompts, then `dz workflow validate` + `dz workflow render`.'); // no-stubs: workflow-init scaffold sentinel the author replaces (deliberate authoring cue, not unfinished code)
987
+ return 0;
988
+ }
989
+
990
+ if (sub === 'validate') {
991
+ const planPath = options.get('_positional_1') ?? options.get('plan') ?? '';
992
+ if (planPath === '') {
993
+ write('dz workflow validate: usage — dz workflow validate <plan.json> [--json]');
994
+ return 1;
995
+ }
996
+ const abs = resolve(cwd, planPath);
997
+ if (!existsSync(abs)) {
998
+ write(`dz workflow validate: no such plan file: ${abs}`);
999
+ return 1;
1000
+ }
1001
+ let raw: unknown;
1002
+ try {
1003
+ raw = JSON.parse(readFileSync(abs, 'utf8'));
1004
+ } catch (e) {
1005
+ write(`dz workflow validate: unparseable JSON — ${e instanceof Error ? e.message : String(e)}`);
1006
+ return 1;
1007
+ }
1008
+ const parsed = parsePlan(raw);
1009
+ if (isParseErrors(parsed)) {
1010
+ if (flags.has('json')) write(JSON.stringify({ ok: false, parseErrors: parsed }, null, 2));
1011
+ else for (const e of parsed) write(`PARSE ${e.path}: ${e.message}`);
1012
+ return 1;
1013
+ }
1014
+ const diags = validatePlan(parsed);
1015
+ if (flags.has('json')) {
1016
+ write(JSON.stringify({ ok: diags.length === 0, digest: planDigest(parsed), diagnostics: diags }, null, 2));
1017
+ } else {
1018
+ for (const d of diags) write(`${d.invariant} ${d.path}: ${d.message}`);
1019
+ write(diags.length === 0 ? `dz workflow validate: OK (digest sha256:${planDigest(parsed).slice(0, 16)}…)` : `dz workflow validate: ${diags.length} invariant violation(s)`);
1020
+ }
1021
+ return diags.length === 0 ? 0 : 1;
1022
+ }
1023
+
1024
+ if (sub === 'render') {
1025
+ const planPath = options.get('_positional_1') ?? options.get('plan') ?? '';
1026
+ const outPath = options.get('o') ?? options.get('out') ?? '';
1027
+ if (planPath === '' || outPath === '') {
1028
+ write('dz workflow render: usage — dz workflow render <plan.json> -o <script.js> [--check] [--force]');
1029
+ return 1;
1030
+ }
1031
+ const absPlan = resolve(cwd, planPath);
1032
+ if (!existsSync(absPlan)) {
1033
+ write(`dz workflow render: no such plan file: ${absPlan}`);
1034
+ return 1;
1035
+ }
1036
+ const parsed = parsePlan(JSON.parse(readFileSync(absPlan, 'utf8')));
1037
+ if (isParseErrors(parsed)) {
1038
+ for (const e of parsed) write(`PARSE ${e.path}: ${e.message}`);
1039
+ return 1;
1040
+ }
1041
+ const diags = validatePlan(parsed);
1042
+ if (diags.length > 0) {
1043
+ for (const d of diags) write(`${d.invariant} ${d.path}: ${d.message}`);
1044
+ write('dz workflow render: refusing to render an invalid plan');
1045
+ return 1;
1046
+ }
1047
+ const rendered = renderPlan(parsed);
1048
+ const absOut = resolve(cwd, outPath);
1049
+ const sidecar = absOut.replace(/\.js$/, '') + '.plan.json';
1050
+ const prev = existsSync(absOut) ? readFileSync(absOut, 'utf8') : '';
1051
+ const merged = prev === '' ? { text: rendered.text, conflicts: [], refused: false } : mergeRender(prev, rendered, { force: flags.has('force') });
1052
+ if (flags.has('check')) {
1053
+ const same = prev === merged.text && !merged.refused;
1054
+ write(same ? 'dz workflow render --check: up to date' : 'dz workflow render --check: DRIFT — a fresh render differs from the file on disk');
1055
+ return same ? 0 : 1;
1056
+ }
1057
+ if (merged.refused) {
1058
+ const proposed = absOut + '.proposed.js';
1059
+ writeFileSync(proposed, merged.proposedText ?? rendered.text);
1060
+ write(`dz workflow render: ${absOut} carries NO region markers (hand-written loop) — REFUSING to overwrite.`);
1061
+ write(`Proposed render written to ${proposed}; re-run with --force to replace the target.`);
1062
+ return 1;
1063
+ }
1064
+ // sidecar plan FIRST, independently of the script (FR-4.1 — the oracle diffs against it)
1065
+ writeFileSync(sidecar, rendered.planJson);
1066
+ writeFileSync(absOut, merged.text);
1067
+ for (const c of merged.conflicts) write(`CONFLICT step ${c.stepId}: ${c.reason}`);
1068
+ write(`dz workflow render: wrote ${sidecar} then ${absOut} (exec-fp sha256:${rendered.execFingerprint.slice(0, 16)}…, blobs: ${rendered.manifest.blobs.map((b) => b.name).join(', ') || 'none'})`);
1069
+ write('Gate it: dz workflow-lint ' + outPath + ' --plan ' + sidecar + ' --require-plan');
1070
+ return merged.conflicts.length > 0 ? 1 : 0;
1071
+ }
1072
+
1073
+ if (sub === 'blobs') {
1074
+ let bad = 0;
1075
+ for (const b of Object.values(LOOP_BLOBS)) {
1076
+ const actual = createHash('sha256').update(b.code, 'utf8').digest('hex');
1077
+ const ok = actual === b.contentHash;
1078
+ if (!ok) bad++;
1079
+ write(`${b.name}@${b.version} sha256:${b.contentHash.slice(0, 16)}… ${ok ? 'OK' : 'HASH MISMATCH (loop-blobs.generated.ts was hand-edited — regenerate: node scripts/gen-loop-blobs.mjs)'} (requires: [${b.requires.join(', ')}])`);
1080
+ }
1081
+ if (flags.has('check')) {
1082
+ write(bad === 0 ? 'dz workflow blobs --check: registry self-consistent (authoritative canon-vs-committed diff runs in CI via loop-blobs-regen.test.ts / scripts/gen-loop-blobs.mjs --check)' : `dz workflow blobs --check: ${bad} blob(s) inconsistent`);
1083
+ return bad === 0 ? 0 : 1;
1084
+ }
1085
+ return 0;
1086
+ }
1087
+
1088
+ // Everything else — the retired ADR-005 surface, BOTH spellings (bare positional task name and
1089
+ // --task/--name) — prints the pinned shim message. AM-6: never silently reachable.
1090
+ void legacyTask;
1091
+ void flags;
1092
+ write(WORKFLOW_TEMPLATES_RETIRED_MESSAGE);
1093
+ return 1;
1094
+ }
1095
+
1096
+ /** `dz workflow-lint` — layer-1 gate; exit 0/1/3 (pass/fail/inconclusive — INV-13: inconclusive is
1097
+ * never a pass; the exit convention mirrors consult-gate). */
1098
+ function cmdWorkflowLint(options: Map<string, string>, flags: Set<string>, cwd: string, write: Write): number {
1099
+ const scriptPath = options.get('_positional_0') ?? '';
1100
+ if (scriptPath === '') {
1101
+ write('dz workflow-lint: usage — dz workflow-lint <script.js> [--plan <plan.json>] [--require-plan|--legacy] [--json]');
847
1102
  return 1;
848
1103
  }
849
- const wf = getWorkflow(taskName);
850
- if (wf === undefined) {
851
- write(`dz workflow: unknown task ${JSON.stringify(taskName)}`);
1104
+ const absScript = resolve(cwd, scriptPath);
1105
+ if (!existsSync(absScript)) {
1106
+ write(`dz workflow-lint: no such script: ${absScript}`);
852
1107
  return 1;
853
1108
  }
854
- const projectRoot = resolve(cwd, options.get('project') ?? '.');
855
- const script = wf.generate({ projectRoot, dryRun: flags.has('dry-run') });
1109
+ const scriptText = readFileSync(absScript, 'utf8');
1110
+ const planPath = options.get('plan');
1111
+ let plan = null;
1112
+ let digestValue: string | null = null;
1113
+ if (planPath !== undefined) {
1114
+ const absPlan = resolve(cwd, planPath);
1115
+ if (!existsSync(absPlan)) {
1116
+ write(`dz workflow-lint: no such plan: ${absPlan}`);
1117
+ return 1;
1118
+ }
1119
+ const parsed = parsePlan(JSON.parse(readFileSync(absPlan, 'utf8')));
1120
+ if (isParseErrors(parsed)) {
1121
+ for (const e of parsed) write(`PARSE ${e.path}: ${e.message}`);
1122
+ return 1;
1123
+ }
1124
+ plan = parsed;
1125
+ digestValue = planDigest(parsed);
1126
+ }
1127
+ const mode = flags.has('require-plan') ? 'require-plan' : flags.has('legacy') ? 'legacy' : 'default';
1128
+ const run = lint(scriptText, { plan, planDigestValue: digestValue, blobRegistry: LOOP_BLOBS, mode });
1129
+ if (flags.has('json')) {
1130
+ write(JSON.stringify(run, null, 2));
1131
+ } else {
1132
+ for (const f of run.findings) {
1133
+ write(`${f.severity.toUpperCase().padEnd(12)} ${f.rule}: ${f.message}${f.anchor ? ` [anchor: ${f.anchor}]` : ''}`);
1134
+ }
1135
+ const counts = { fail: 0, warn: 0, inconclusive: 0 };
1136
+ for (const f of run.findings) if (f.severity in counts) counts[f.severity as keyof typeof counts]++;
1137
+ write(`dz workflow-lint: ${run.verdict.toUpperCase()} (mode=${run.mode}; ${counts.fail} fail, ${counts.warn} warn, ${counts.inconclusive} inconclusive over ${Object.keys(run.rules).length} rules)`);
1138
+ if (run.verdict === 'inconclusive') write('inconclusive is NOT a pass (exit 3) — bind a plan (--plan … --require-plan) or acknowledge a legacy script with --legacy');
1139
+ }
1140
+ return lintExitCode(run);
1141
+ }
856
1142
 
857
- write(`// Dynamic Workflow: ${wf.name}`);
858
- write(`// ${wf.description}`);
859
- write('');
860
- write(script);
861
- write('');
862
- write(`// To execute: paste the above into Claude Code with Opus 4.8+ and "ultracode" enabled.`);
863
- write(`// Or save to a .js file and run: claude "Execute this workflow" < workflow.js`);
864
- return 0;
1143
+ /** `dz workflow-trace` — timeline + invariant runner over a run's trace.jsonl. Scope is CAPPED
1144
+ * (AM-8): <runDir|--slug|--run>, --invariants, --html, --json. NO watch/filter/compare/search/
1145
+ * retention/access-control — adding one needs an ADR amendment (the surface test pins this). */
1146
+ function cmdWorkflowTrace(options: Map<string, string>, flags: Set<string>, cwd: string, write: Write): number {
1147
+ const runDirArg = options.get('_positional_0');
1148
+ const slug = options.get('slug');
1149
+ const runId = options.get('run');
1150
+ let runDir: string;
1151
+ if (runDirArg !== undefined && runDirArg !== '') runDir = resolve(cwd, runDirArg);
1152
+ else if (slug !== undefined) runDir = resolve(cwd, 'features', slug);
1153
+ else if (runId !== undefined) runDir = resolve(cwd, '.dz', 'loop-trace', runId);
1154
+ else {
1155
+ write('dz workflow-trace: usage — dz workflow-trace <runDir|--slug <s>|--run <id>> [--invariants <plan.json>] [--html <out.html>] [--json]');
1156
+ return 1;
1157
+ }
1158
+ const traceFile = join(runDir, 'trace.jsonl');
1159
+ if (!existsSync(traceFile)) {
1160
+ write(`dz workflow-trace: no trace.jsonl under ${runDir} (the loop writes its own trace only when the plan sets trace.emit: true)`);
1161
+ return 1;
1162
+ }
1163
+ const traceText = readFileSync(traceFile, 'utf8');
1164
+ const ckptFile = join(runDir, '.fa-state', 'checkpoints.jsonl');
1165
+ const ledgerFile = resolve(cwd, '.dz', 'feature-adr', 'run-cost-ledger.jsonl');
1166
+ const journalFile = join(runDir, 'journal.jsonl');
1167
+ const timeline = assembleTimeline({
1168
+ trace: traceText,
1169
+ checkpoints: existsSync(ckptFile) ? readFileSync(ckptFile, 'utf8') : null,
1170
+ ledger: existsSync(ledgerFile) ? readFileSync(ledgerFile, 'utf8') : null,
1171
+ journal: existsSync(journalFile) ? readFileSync(journalFile, 'utf8') : null,
1172
+ });
1173
+ let verdicts: ReturnType<typeof runInvariants> = [];
1174
+ let projection: ReturnType<typeof toTraceProjection> | null = null;
1175
+ const invariantsPlan = options.get('invariants');
1176
+ if (invariantsPlan !== undefined) {
1177
+ const absPlan = resolve(cwd, invariantsPlan);
1178
+ if (!existsSync(absPlan)) {
1179
+ write(`dz workflow-trace: no such plan: ${absPlan}`);
1180
+ return 1;
1181
+ }
1182
+ const parsed = parsePlan(JSON.parse(readFileSync(absPlan, 'utf8')));
1183
+ if (isParseErrors(parsed)) {
1184
+ for (const e of parsed) write(`PARSE ${e.path}: ${e.message}`);
1185
+ return 1;
1186
+ }
1187
+ projection = toTraceProjection(parsed);
1188
+ verdicts = runInvariants(projection, parseTrace(traceText));
1189
+ }
1190
+ const htmlOut = options.get('html');
1191
+ if (htmlOut !== undefined) {
1192
+ const absHtml = resolve(cwd, htmlOut);
1193
+ writeFileSync(absHtml, renderTimelineHtml(timeline, projection, verdicts));
1194
+ write(`dz workflow-trace: wrote ${absHtml} (self-contained: mermaid topology + HTML waterfall)`);
1195
+ }
1196
+ if (flags.has('json')) {
1197
+ write(JSON.stringify({ timeline, verdicts }, null, 2));
1198
+ return verdicts.some((v) => v.status === 'fail') ? 1 : 0;
1199
+ }
1200
+ write(`run ${timeline.runId ?? '(unknown)'}${timeline.incomplete ? ' — INCOMPLETE (no run.closed; unflushed tail may be lost)' : ''}; sources: ${timeline.sources.join(', ')}`);
1201
+ for (const r of timeline.rows.filter((x) => x.kind === 'trace')) {
1202
+ write(` ${String(r.seq).padStart(5)} ${r.label} ${r.detail}${r.wallTime ? ` [wall ${r.wallTime} — diagnostic only]` : ''}`);
1203
+ }
1204
+ for (const v of verdicts) write(`INVARIANT ${v.status.toUpperCase().padEnd(12)} ${v.id}: ${v.message}`);
1205
+ return verdicts.some((v) => v.status === 'fail') ? 1 : 0;
865
1206
  }
866
1207
 
867
1208
  function cmdMigrate(options: Map<string, string>, cwd: string, write: Write): number {
@@ -1015,7 +1356,13 @@ function cmdBundle(options: Map<string, string>, flags: Set<string>, cwd: string
1015
1356
  return 0;
1016
1357
  }
1017
1358
 
1018
- async function cmdInstall(options: Map<string, string>, flags: Set<string>, cwd: string, write: Write): Promise<number> {
1359
+ async function cmdInstall(
1360
+ options: Map<string, string>,
1361
+ flags: Set<string>,
1362
+ cwd: string,
1363
+ write: Write,
1364
+ installRunner?: (command: string, cwd: string) => void,
1365
+ ): Promise<number> {
1019
1366
  const pkg = options.get('_positional_0');
1020
1367
  if (!pkg) {
1021
1368
  write('dz install: package name required (e.g., dz install @dzhechkov/skills-devops)');
@@ -1030,10 +1377,12 @@ async function cmdInstall(options: Map<string, string>, flags: Set<string>, cwd:
1030
1377
 
1031
1378
  const projectRoot = resolve(cwd, options.get('project') ?? '.');
1032
1379
 
1033
- // Step 1: npm install the package
1380
+ // Step 1: npm install the package (installRunner is the CliIo test seam — unset in production)
1034
1381
  write(`Installing ${pkg}...`);
1382
+ const installCmd = `npm install ${pkg} --save-dev --no-fund --no-audit`;
1035
1383
  try {
1036
- execSync(`npm install ${pkg} --save-dev --no-fund --no-audit`, { cwd: projectRoot, stdio: 'pipe', encoding: 'utf-8' });
1384
+ if (installRunner) installRunner(installCmd, projectRoot);
1385
+ else execSync(installCmd, { cwd: projectRoot, stdio: 'pipe', encoding: 'utf-8' });
1037
1386
  } catch (err) {
1038
1387
  write(`dz install: npm install failed — ${err instanceof Error ? err.message : String(err)}`);
1039
1388
  return 1;
@@ -1046,19 +1395,22 @@ async function cmdInstall(options: Map<string, string>, flags: Set<string>, cwd:
1046
1395
  return 1;
1047
1396
  }
1048
1397
 
1049
- // Discover skills look for dirs containing SKILL.md
1050
- const entries = readdirSync(pkgDir, { withFileTypes: true })
1051
- .filter((e) => e.isDirectory() && existsSync(join(pkgDir, e.name, 'SKILL.md')));
1052
-
1053
- if (entries.length === 0) {
1054
- write(`dz install: no SKILL.md files found in ${pkg}. Package may not contain agentskills.io skills.`);
1055
- return 0;
1398
+ // Resolve the package's skills root across the known layouts (flat / npx-template /
1399
+ // skills-dir) feature dz-install-npx-init. Total resolution failure is an ERROR
1400
+ // (exit 1), not the old advisory exit 0: a user asking to install skills that were
1401
+ // not installed must not see success.
1402
+ const roots = resolvePackageSkillRoots(pkgDir);
1403
+ if (roots.length === 0) {
1404
+ write(`dz install: no SKILL.md found in ${pkg}. Probed: ${PACKAGE_SKILL_LAYOUTS.map((l) => l.rel).join(', ')}.`);
1405
+ write(`If this package installs itself, try: npx -y ${pkg} init`);
1406
+ return 1;
1056
1407
  }
1408
+ const root = roots[0]!;
1057
1409
 
1058
- // Step 3: Use dz init with the package as skills source
1410
+ // Step 3: Use dz init with the resolved skills root as source
1059
1411
  const report = await runInit({
1060
1412
  target: targetOpt,
1061
- skillsDir: pkgDir,
1413
+ skillsDir: root.dir,
1062
1414
  projectRoot,
1063
1415
  force: flags.has('force'),
1064
1416
  });
@@ -1066,10 +1418,16 @@ async function cmdInstall(options: Map<string, string>, flags: Set<string>, cwd:
1066
1418
  const totalWritten = report.skills.reduce((sum, s) => sum + s.written.length, 0);
1067
1419
  const totalSkipped = report.skills.reduce((sum, s) => sum + s.skipped.length, 0);
1068
1420
 
1069
- write(`dz install ${pkg}: ${report.skills.length} skill(s), ${totalWritten} file(s) written, ${totalSkipped} skipped`);
1421
+ // Non-flat resolutions are tagged so a mis-resolution is legible in a user's paste;
1422
+ // flat output stays byte-identical to the pre-feature behavior (NFR-1 / test T5.6).
1423
+ const layoutTag = root.layout === 'flat' ? '' : ` [layout: ${root.layout}]`;
1424
+ write(`dz install ${pkg}: ${report.skills.length} skill(s), ${totalWritten} file(s) written, ${totalSkipped} skipped${layoutTag}`);
1070
1425
  for (const skill of report.skills) {
1071
1426
  write(` ${skill.id}: ${skill.written.length} written, ${skill.skipped.length} skipped`);
1072
1427
  }
1428
+ if (root.layout === 'npx-template' && root.hasCompanionAssets) {
1429
+ write(` note: ${pkg} also ships commands/hooks/agents — \`npx -y ${pkg} init\` installs the full kit.`);
1430
+ }
1073
1431
  return 0;
1074
1432
  }
1075
1433
 
@@ -1263,11 +1621,13 @@ function cmdStatuslineInstall(options: Map<string, string>, cwd: string, write:
1263
1621
  /**
1264
1622
  * `dz statusline --fa-record` — the entry point the `/feature-adr` pipeline calls at Steps 0/8/9 to
1265
1623
  * record its LIVE learning state (Pattern memory loop: pool / recalled / stored). Unlike the render
1266
- * path this WRITES `.dz/feature-adr/learning-state.json` (via harness-core's best-effort
1267
- * `writeFeatureAdrState`, which itself computes `pool` from the learned-pattern count).
1624
+ * path this WRITES a per-slug `.dz/feature-adr/learning-state/*.json` slot (via harness-core's
1625
+ * best-effort `writeFeatureAdrState`, which itself computes `pool` from the learned-pattern count).
1268
1626
  *
1269
1627
  * GUARD (feature-adr flag discipline): `--slug` and `--step` are required; a missing one exits 1
1270
1628
  * with a copy-paste example. `--recalled`/`--stored` default to 0 and reject non-numeric input.
1629
+ * `--kind <feature-adr|loop>` identifies the producer, defaults to `feature-adr`, and rejects any
1630
+ * other value rather than silently weakening panel arbitration.
1271
1631
  */
1272
1632
  function cmdStatuslineFaRecord(options: Map<string, string>, cwd: string, write: Write): number {
1273
1633
  const slug = (options.get('slug') ?? '').trim();
@@ -1297,10 +1657,17 @@ function cmdStatuslineFaRecord(options: Map<string, string>, cwd: string, write:
1297
1657
  const reinforced = parseCount('reinforced');
1298
1658
  if (reinforced === undefined) return 1;
1299
1659
 
1660
+ const kindRaw = options.get('kind') ?? 'feature-adr';
1661
+ if (kindRaw !== 'feature-adr' && kindRaw !== 'loop') {
1662
+ write(`dz statusline --fa-record: --kind must be feature-adr or loop (got "${kindRaw}")`);
1663
+ write(' Example: dz statusline --fa-record --slug add-user-auth --step "Step 0" --kind feature-adr --recalled 5 --stored 2');
1664
+ return 1;
1665
+ }
1666
+
1300
1667
  const mode = options.get('mode');
1301
1668
  const projectRoot = resolve(cwd, options.get('project') ?? '.');
1302
1669
  const state = writeFeatureAdrState(projectRoot, {
1303
- slug, step, recalled, stored,
1670
+ kind: kindRaw, slug, step, recalled, stored,
1304
1671
  ...(reinforced > 0 ? { reinforced } : {}),
1305
1672
  ...(mode !== undefined && mode.trim() !== '' ? { mode: mode.trim() } : {}),
1306
1673
  });
@@ -1351,7 +1718,12 @@ function cmdStatusline(
1351
1718
  // Live /feature-adr run in flight → PREPEND the pipeline learning segment to the base dz line.
1352
1719
  const fa = data.featureAdr;
1353
1720
  if (fa !== undefined) {
1354
- line = `📐 feature-adr ${fa.step} · 🎓 ${fa.pool} pool · ↑${fa.recalled} used · +${fa.stored} new · ↻${fa.reinforced ?? 0} reinforced · ${line}`;
1721
+ // The producer marker reached data and arbitration in a previous round but not this label, so the bar asserted a pipeline that was not running.
1722
+ if (fa.kind === 'loop') {
1723
+ line = `🔁 loop ${fa.step} · ${line}`;
1724
+ } else {
1725
+ line = `📐 feature-adr ${fa.step} · 🎓 ${fa.pool} pool · ↑${fa.recalled} used · +${fa.stored} new · ↻${fa.reinforced ?? 0} reinforced · ${line}`;
1726
+ }
1355
1727
  }
1356
1728
 
1357
1729
  write(line);
@@ -1778,6 +2150,22 @@ async function cmdTeach(options: Map<string, string>, flags: Set<string>, cwd: s
1778
2150
  }
1779
2151
  write(`Imported ${imported} pattern(s) from ${fromJson}`);
1780
2152
  write(` Skipped ${skipped} (duplicates already in the store, or invalid entries)`);
2153
+ // Bulk import preserves the DOMAIN of every record, so it can put medical lessons in
2154
+ // a shared store as silently as a hand-typed teach — and it returned before the
2155
+ // advice single-teach prints. The same advice, at the same point in the flow: after
2156
+ // the write, naming the choice, blocking nothing.
2157
+ let resolvedImportRoot = projectRoot;
2158
+ try { resolvedImportRoot = realpathSync(projectRoot); } catch { /* unresolvable is not the brain */ }
2159
+ const importedMedical = importedRecs.filter(
2160
+ (r) => DEFAULT_HELD_OUT_DOMAINS.map(canonicalDomainKey).includes(canonicalDomainKey(r.domain)),
2161
+ );
2162
+ if (importedMedical.length > 0) {
2163
+ const advice = renderSharedStoreAdvice(importedMedical[0]?.domain, resolvedImportRoot);
2164
+ if (advice !== '') {
2165
+ write(` ⚠ ${importedMedical.length} of the imported lesson(s) carry a medical domain.`);
2166
+ write(advice);
2167
+ }
2168
+ }
1781
2169
  write(` Backend: memory (@dzhechkov/memory) Total now: ${loadStorePatternsSync(projectRoot).length}`);
1782
2170
  // ONE batched mirror call through the same seam as single-teach (QR-6 — no bespoke path).
1783
2171
  await emitMirror(projectRoot, importedRecs, 'dz-teach-import');
@@ -1876,6 +2264,16 @@ async function cmdTeach(options: Map<string, string>, flags: Set<string>, cwd: s
1876
2264
  write(`Learned: "${pattern.slice(0, 60)}${pattern.length > 60 ? '...' : ''}"`);
1877
2265
  write(` Domain: ${domain} Reward: ${reward} Backend: memory (@dzhechkov/memory)`);
1878
2266
  write(` Total patterns: ${count}`);
2267
+ // ADVICE, not a gate. Someone putting medical lessons in a shared store owns both
2268
+ // directories and this binary; refusing would be defending a user against themselves,
2269
+ // which this design does not attempt. Making the choice INFORMED is the part that is
2270
+ // ours to do — the write has already happened when this prints.
2271
+ // RESOLVE before deciding. A symlink named `.health-brain` pointing at a shared project
2272
+ // silenced this advice in exactly the case it exists for.
2273
+ let resolvedRoot = projectRoot;
2274
+ try { resolvedRoot = realpathSync(projectRoot); } catch { /* a path we cannot resolve is not the brain */ }
2275
+ const sharedAdvice = renderSharedStoreAdvice(domain, resolvedRoot);
2276
+ if (sharedAdvice !== '') write(sharedAdvice);
1879
2277
  if (quarantineOn) {
1880
2278
  write(' ⚠ quarantined: excluded from auto-inject, damped in recall — promote by confirming it (dz teach --reinforce "<text>") or dz recall --promote <dzId> --apply');
1881
2279
  }
@@ -2029,13 +2427,41 @@ function fmtUsageRow(row: RecallUsagePatternRow): string {
2029
2427
  }
2030
2428
 
2031
2429
  function cmdRecallUsage(options: Map<string, string>, flags: Set<string>, projectRoot: string, write: Write): number {
2032
- const report = readRecallUsageReport(projectRoot);
2430
+ const raw = readRecallUsageReport(projectRoot);
2431
+ // The hold-out applies to EVERY surface that hands out lesson TEXT, not only to the
2432
+ // one labelled "export". `--usage --json` prints whole lessons in its `top` and
2433
+ // `neverRead` lists, so it is an export by behaviour whatever it is called — a
2434
+ // self-audit before review found health-research lesson text sitting in both. A
2435
+ // guarantee that covers only the surface you were thinking about is not a guarantee.
2436
+ const heldOut = heldOutAfterOptIn(options.get('include-domain'));
2437
+ // EVERY list of records, not the two that were obvious. A first pass held out `top`
2438
+ // and `neverRead` and a health lesson still came out — in `all`, a third list further
2439
+ // down the same object. Enumerating the lists you remembered is how the surface leaks.
2440
+ const top = applyExportHoldout(raw.top, heldOut);
2441
+ const neverRead = applyExportHoldout(raw.neverRead, heldOut);
2442
+ // `unknown` is deliberately NOT held out: RecallUsageStat carries counters and
2443
+ // timestamps only — no domain, no lesson text — so there is nothing in it to withhold.
2444
+ // Filtering it would be theatre, and theatre in a privacy control is worse than a gap
2445
+ // because it looks like coverage.
2446
+ const every = applyExportHoldout(raw.all ?? [], heldOut);
2447
+ const withheld = top.withheld.length + neverRead.withheld.length + every.withheld.length;
2448
+ const report = {
2449
+ ...raw,
2450
+ top: top.exported,
2451
+ neverRead: neverRead.exported,
2452
+ ...(raw.all === undefined ? {} : { all: every.exported }),
2453
+ };
2454
+ const holdoutNote = renderHoldoutNote([top, neverRead, every]
2455
+ .reduce((a, b) => (b.withheld.length > a.withheld.length ? b : a)));
2033
2456
  if (flags.has('json')) {
2034
2457
  write(JSON.stringify({
2035
2458
  ...report,
2036
2459
  log: join(projectRoot, RECALL_USAGE_LOG_RELATIVE),
2037
2460
  retention: { maxBytes: RECALL_USAGE_LOG_MAX_BYTES },
2461
+ withheld,
2462
+ withheldDomains: [...new Set([...top.domains, ...neverRead.domains, ...every.domains])].sort(),
2038
2463
  }));
2464
+ if (withheld > 0 && holdoutNote !== '') process.stderr.write(`${holdoutNote}\n`);
2039
2465
  return 0;
2040
2466
  }
2041
2467
 
@@ -2060,6 +2486,9 @@ function cmdRecallUsage(options: Map<string, string>, flags: Set<string>, projec
2060
2486
  for (const row of report.neverRead.slice(0, displayLimit)) write(` ${row.dzId}${row.domain !== undefined ? ` (${row.domain})` : ''} ${row.pattern.slice(0, 100)}`);
2061
2487
  if (report.neverRead.length > displayLimit) write(` ... ${report.neverRead.length - displayLimit} more (use --json for the full list)`);
2062
2488
  }
2489
+ if (withheld > 0 && holdoutNote !== '') {
2490
+ write(holdoutNote);
2491
+ }
2063
2492
  return 0;
2064
2493
  }
2065
2494
 
@@ -2174,15 +2603,57 @@ async function cmdRecall(options: Map<string, string>, flags: Set<string>, cwd:
2174
2603
  // --all: dump the entire learned store (backend-agnostic, via loadStorePatternsSync).
2175
2604
  // With --json this is the portable export the agentdb-memory MCP bridge consumes.
2176
2605
  if (all) {
2177
- const patterns = loadStorePatternsSync(projectRoot);
2606
+ // ISOLATION (health-advisor slice H, ADR-003). This command is THE portable sharing
2607
+ // form — it is documented as such and the agentdb bridge consumes it — so it is the
2608
+ // realistic path by which a learned store leaves this machine. `health-research`
2609
+ // carries lessons drawn from one person's investigations and is held back unless the
2610
+ // caller names it. This replaced a text-inspecting privacy guard that seven rounds of
2611
+ // review could not make correct: a tag set by the writer is decidable, prose is not.
2612
+ const allPatterns = loadStorePatternsSync(projectRoot);
2613
+ const holdout = applyExportHoldout(allPatterns, heldOutAfterOptIn(options.get('include-domain')));
2614
+ const patterns = holdout.exported;
2615
+ const holdoutNote = renderHoldoutNote(holdout);
2616
+ // The opt-in is honoured without argument — and named out loud. A flag that silently
2617
+ // includes medical lessons in a portable export is a flag whose consequence the user
2618
+ // has to remember; one that says what it just handed over is a flag they can act on.
2619
+ const optedIn = options.get('include-domain');
2620
+ if (optedIn !== undefined && optedIn.trim() !== '') {
2621
+ // Count against the SPLIT list, the same way heldOutAfterOptIn parses it. Comparing
2622
+ // each record to the whole unsplit string reported `0 of 2` for a comma-separated
2623
+ // opt-in that had just exported a medical lesson — a warning that undercounts the
2624
+ // thing it warns about is worse than none.
2625
+ const optedKeys = new Set(optedIn.split(',').map((d) => canonicalDomainKey(d)).filter((d) => d !== ''));
2626
+ const medicalKeys = new Set(DEFAULT_HELD_OUT_DOMAINS.map(canonicalDomainKey));
2627
+ const releasedKeys = [...optedKeys].filter((k) => medicalKeys.has(k));
2628
+ const releasedCount = allPatterns.filter((p) => releasedKeys.includes(canonicalDomainKey(p.domain))).length;
2629
+ if (releasedKeys.length > 0) {
2630
+ process.stderr.write(
2631
+ ` ⚠ --include-domain ${releasedKeys.join(',')}: this export CONTAINS lessons from a medical `
2632
+ + `domain (${releasedCount} of ${allPatterns.length}). They travel with the file wherever it `
2633
+ + `goes. Nothing was blocked — this is your call.\n`,
2634
+ );
2635
+ }
2636
+ }
2178
2637
  if (flags.has('stats')) {
2179
- const stats = storeStats(projectRoot);
2638
+ // `stats` is computed from the UNFILTERED store, and `topUses` carries whole
2639
+ // lesson text. Filtering `patterns` two lines above and serialising this untouched
2640
+ // exported exactly what had just been withheld — found by review immediately after
2641
+ // I had written the lesson "cover every surface", in the same function. The
2642
+ // per-domain histogram goes too: it names the domain and its size.
2643
+ const rawStats = storeStats(projectRoot);
2644
+ const topHoldout = applyExportHoldout(rawStats.topUses, heldOutAfterOptIn(options.get('include-domain')));
2645
+ const perDomain = Object.fromEntries(
2646
+ Object.entries(rawStats.perDomain).filter(([d]) => !holdout.domains.includes(canonicalDomainKey(d))),
2647
+ );
2648
+ const stats = { ...rawStats, topUses: topHoldout.exported, perDomain };
2180
2649
  const backendStats = resolveLearningBackend(projectRoot).getStats();
2181
2650
  if (asJson) {
2182
- write(JSON.stringify({ patterns, stats, learning: backendStats }));
2651
+ write(JSON.stringify({ patterns, stats, learning: backendStats, withheld: holdout.withheld.length, withheldDomains: holdout.domains }));
2652
+ if (holdoutNote !== '') process.stderr.write(`${holdoutNote}\n`);
2183
2653
  return 0;
2184
2654
  }
2185
2655
  write(`dz recall --all --stats — ${patterns.length} learned pattern(s)`);
2656
+ if (holdoutNote !== '') write(holdoutNote);
2186
2657
  write(` backend: ${backendStats.backend}${backendStats.advisory !== undefined ? ` (${backendStats.advisory})` : ''}`);
2187
2658
  write(` domains: ${Object.entries(stats.perDomain).map(([k, v]) => `${k}=${v}`).join(', ') || 'none'}`);
2188
2659
  write(` exact-dup groups: ${stats.exactDupGroups}`);
@@ -2193,10 +2664,15 @@ async function cmdRecall(options: Map<string, string>, flags: Set<string>, cwd:
2193
2664
  return 0;
2194
2665
  }
2195
2666
  if (asJson) {
2667
+ // The note goes to STDERR so the JSON on stdout stays machine-parsable — but it is
2668
+ // still SAID. A silent hold-out would let the reader believe they exported the whole
2669
+ // store, and would make a broken hold-out look exactly like an empty domain.
2196
2670
  write(JSON.stringify(patterns));
2671
+ if (holdoutNote !== '') process.stderr.write(`${holdoutNote}\n`);
2197
2672
  } else {
2198
2673
  write(`dz recall --all — ${patterns.length} learned pattern(s)`);
2199
2674
  for (const p of patterns) write(` [${p.reward.toFixed(2)}] (${p.domain}) ${p.pattern.slice(0, 80)}`);
2675
+ if (holdoutNote !== '') write(holdoutNote);
2200
2676
  }
2201
2677
  return 0;
2202
2678
  }
@@ -2282,6 +2758,14 @@ async function cmdRecall(options: Map<string, string>, flags: Set<string>, cwd:
2282
2758
  // Portable contract UNCHANGED (I-7/AC-6): a plain PatternRecord[] — round-trips through
2283
2759
  // `dz teach --from-json` regardless of which backend ranked each hit.
2284
2760
  write(JSON.stringify(hits.map((h) => h.pattern)));
2761
+ // The honesty notes go to STDERR here rather than being skipped: the JSON branch
2762
+ // used to return before them, so a scripted caller was told nothing about a boost
2763
+ // that had promoted a match and pushed a visible hit past the --limit cut.
2764
+ if (boost !== null && shownDomain !== undefined) {
2765
+ process.stderr.write(`${renderDomainBoostNote(boost, shownDomain)}\n`);
2766
+ const cutNoteJson = renderDomainCutNote(displaced, limit);
2767
+ if (cutNoteJson !== '') process.stderr.write(`${cutNoteJson}\n`);
2768
+ }
2285
2769
  return 0;
2286
2770
  }
2287
2771
 
@@ -2467,6 +2951,32 @@ async function cmdVector(options: Map<string, string>, flags: Set<string>, cwd:
2467
2951
  write(' Patterns themselves stay portable via: dz recall --all --json (import with: dz teach --from-json)');
2468
2952
  return 0;
2469
2953
  }
2954
+ // The vector checkpoint carries EMBEDDINGS of the lesson text, and the RVF adapter
2955
+ // copies the whole store — there is no per-record filter to apply. So this path
2956
+ // fails CLOSED: if the store holds a held-out domain, the export is refused unless
2957
+ // the caller names that domain. An embedding is not plaintext, but ADR-003's
2958
+ // question is "does patient data leave this machine?", and a checkpoint that
2959
+ // silently carried it would answer yes while the documentation said no.
2960
+ // JUDGE THE FILE THIS COMMAND EXPORTS. The decision itself lives in harness-core as
2961
+ // a pure function WITH TESTS: the RVF engine is opt-in and absent on most machines,
2962
+ // so this branch could not be exercised in a normal checkout — reasoning about a
2963
+ // safety check I could not run is exactly what this project calls unverified.
2964
+ const optIn = options.get('include-domain');
2965
+ const vecHoldout = applyExportHoldout(
2966
+ loadStorePatternsSync(projectRoot).map((p) => ({ domain: p.domain })),
2967
+ heldOutAfterOptIn(optIn),
2968
+ );
2969
+ const decision = decideVectorExport({
2970
+ rvfExists: existsSync(join(projectRoot, '.dz', 'memory', 'patterns.rvf')),
2971
+ heldOutLexicalCount: vecHoldout.withheld.length,
2972
+ heldOutDomains: vecHoldout.domains,
2973
+ optedIn: optIn,
2974
+ });
2975
+ if (!decision.allow) {
2976
+ write(`dz vector export: REFUSED — ${decision.reason}.`);
2977
+ write(` A forgotten lesson's embedding outlives its lexical record. To export anyway, naming what travels: dz vector export ${dest} --include-domain ${decision.optInHint}`);
2978
+ return 1;
2979
+ }
2470
2980
  const r = await exporter(resolve(cwd, dest));
2471
2981
  if (r.error !== undefined) {
2472
2982
  write(`dz vector export: ${r.error}`);
@@ -3437,7 +3947,7 @@ async function cmdAutoCanonicalize(options: Map<string, string>, cwd: string, wr
3437
3947
  write(` ${skillName.padEnd(30)} ${path}`);
3438
3948
  }
3439
3949
 
3440
- write(`\nTo canonicalize, run for each skill:`);
3950
+ write(`\nTo bring each skill into the canonical tree, run:`);
3441
3951
  const packDir = resolve(cwd, pack);
3442
3952
  for (const path of skillMds) {
3443
3953
  const parts = path.split('/');
@@ -3781,6 +4291,7 @@ function cmdPublish(options: Map<string, string>, flags: Set<string>, cwd: strin
3781
4291
  if (guardResult.verdict === 'block') write(`dz publish: ⚠ guard BLOCK overridden via --no-guard: ${noGuard} (logged)`);
3782
4292
  else if (guardResult.verdict === 'warn') for (const v of guardResult.violations) write(`dz publish: ⚠ guard warn — ${v.rule}: ${v.detail}`);
3783
4293
  else write('dz publish: ✓ guard pre-flight passed');
4294
+ for (const n of guardResult.notes ?? []) write(`dz publish: ℹ guard note — ${n}`); // FN-7: on the record, never blocking
3784
4295
  }
3785
4296
 
3786
4297
  // ADR-001 (publish-provenance): decide BEFORE any work — flag validation, then a pre-flight that
@@ -4989,8 +5500,15 @@ function cmdDriftCheck(options: Map<string, string>, flags: Set<string>, cwd: st
4989
5500
 
4990
5501
  const DEFAULT_STORE_CAP = 5000;
4991
5502
 
4992
- /** Read the optional `.dz/guard.json` — `{ rules?: [...], storeCap?: number }`. Missing/broken ⇒ defaults. */
4993
- function loadGuardConfig(root: string): { rules?: unknown[]; storeCap?: number } {
5503
+ /**
5504
+ * Ceiling on how many changed files the no-stubs scan reads per evaluation. Deterministic (the file
5505
+ * list is `git status` order) and fail-open: files beyond it simply have no gathered contents, so
5506
+ * the rule reports nothing for them — a pre-flight must never become a filesystem sweep.
5507
+ */
5508
+ const MAX_STUB_SCAN_FILES = 400;
5509
+
5510
+ /** Read the optional `.dz/guard.json` — `{ rules?: [...], storeCap?: number, stubWaivers?: [...] }`. Missing/broken ⇒ defaults. */
5511
+ function loadGuardConfig(root: string): { rules?: unknown[]; storeCap?: number; stubWaivers?: unknown[] } {
4994
5512
  const p = join(root, '.dz', 'guard.json');
4995
5513
  if (!existsSync(p)) return {};
4996
5514
  try {
@@ -5044,7 +5562,7 @@ function gatherGuardFacts(op: string, root: string, text: string | undefined, st
5044
5562
  // a resolvable workspace dep becomes its real semver (safe → the rule passes); an UNRESOLVABLE one (points at
5045
5563
  // no workspace package, or not a pnpm workspace) stays `workspace:*` so the rule catches a dep that WOULD ship
5046
5564
  // raw. That is the genuinely dangerous case the rule exists for.
5047
- type Manifest = { name?: string; version?: string; private?: boolean; dependencies?: Record<string, string>; devDependencies?: Record<string, string> };
5565
+ type Manifest = { name?: string; version?: string; private?: boolean; license?: string; licenseHold?: unknown; dependencies?: Record<string, string>; devDependencies?: Record<string, string> };
5048
5566
  const manifests: Manifest[] = [];
5049
5567
  const located: { dir: string; m: Manifest }[] = [];
5050
5568
  try {
@@ -5072,6 +5590,28 @@ function gatherGuardFacts(op: string, root: string, text: string | undefined, st
5072
5590
  packages.push({ name: m.name ?? '(unnamed)', deps });
5073
5591
  }
5074
5592
  facts['packages'] = packages;
5593
+
5594
+ // licence-hold (ADR-001, hermes-claude-adaptation): for each pack DECLARING a hold via a
5595
+ // `licenseHold` field, hand the raw evidence to the pure checker. Best-effort: an unreadable
5596
+ // LICENSE reads as absent (null), which the checker treats as a violation for a publishable
5597
+ // pack — the fail direction that protects the hold, never the publish.
5598
+ try {
5599
+ const holds: { name: string; privateFlag: boolean; licenseText: string | null; noticesText: string | null; licenseField: string | null }[] = [];
5600
+ for (const { dir, m } of located) {
5601
+ if (!m || m.licenseHold === undefined || m.licenseHold === null) continue;
5602
+ const read = (rel: string): string | null => {
5603
+ try { return readFileSync(join(root, dir, rel), 'utf8'); } catch { return null; }
5604
+ };
5605
+ holds.push({
5606
+ name: m.name ?? dir,
5607
+ privateFlag: m.private === true,
5608
+ licenseText: read('LICENSE'),
5609
+ noticesText: read('THIRD_PARTY_NOTICES.md') ?? read('THIRD_PARTY_NOTICES'),
5610
+ licenseField: typeof m.license === 'string' ? m.license : null,
5611
+ });
5612
+ }
5613
+ facts['licenceHold'] = holds;
5614
+ } catch { /* unreadable tree — the rule reports nothing rather than inventing a violation */ }
5075
5615
  try { facts['drift'] = sweepSkillDrift(root, { scope: 'packages', allowlist: readDriftAllowlist(root) }).drifted.map((d) => d.name); } catch { /* skip */ }
5076
5616
  facts['counts'] = gatherReadmeCounts(root);
5077
5617
  // readme-first: from the WORKING-TREE diff (publishes happen pre-commit here), per package: does the
@@ -5127,11 +5667,26 @@ function gatherGuardFacts(op: string, root: string, text: string | undefined, st
5127
5667
  }
5128
5668
  } catch { facts['lockfile'] = { parsed: false }; /* no lockfile (not a pnpm workspace) — rule stays silent */ }
5129
5669
 
5130
- // change: the working-tree diff, for PROMOTED (template) rules. Without this fact a rule written
5131
- // by `dz guard promote --apply` would be INERT present in the config and enforcing nothing.
5132
- // Contents are read only for the globs an active `format-match` rule actually asks about.
5670
+ // change: the working-tree diff the notion of "changed" shared by PROMOTED (template) rules
5671
+ // and the no-stubs rule. (readme-first gathers its OWN pathspec-scoped porcelain call above and
5672
+ // does not read this fact — so the -uall widening below does not alter readme-first at all;
5673
+ // its collapsed-untracked-dir blind spot for a brand-new package dir is a separate, documented
5674
+ // limit of THAT gatherer.) Without this fact a rule written by
5675
+ // `dz guard promote --apply` would be INERT — present in the config and enforcing nothing.
5676
+ // Contents are read for the globs an active `format-match` rule asks about PLUS the changed
5677
+ // files the no-stubs scan reads (its explicit extension allowlist, capped: a pathological
5678
+ // change-set must not turn a pre-flight into a filesystem sweep — beyond the cap the rule
5679
+ // simply sees no contents for the excess files, the standing fail-open contract).
5133
5680
  try {
5134
- const status = execSync('git status --porcelain', { cwd: root, encoding: 'utf-8' });
5681
+ // -uall (FN-1): without it, a brand-new DIRECTORY reports as one collapsed `?? newdir/` line
5682
+ // and every file INSIDE it is invisible to the change fact — and a fresh module directory is
5683
+ // the most stub-prone artifact there is (REPRODUCED: newdir/stub.ts with a live marker ⇒
5684
+ // PASS/0 findings). -uall lists the individual files; `.gitignore` semantics are unchanged
5685
+ // (git status never lists ignored paths, -uall or not — tested live). maxBuffer is raised
5686
+ // (default 1MB) because -uall can expand a huge untracked tree into a long listing; KNOWN
5687
+ // LIMIT: a listing beyond even this bound throws, the catch below drops the whole `change`
5688
+ // fact, and no-stubs + every template rule go silently fail-open together for that run.
5689
+ const status = execSync('git status --porcelain -uall', { cwd: root, encoding: 'utf-8', maxBuffer: 32 * 1024 * 1024 });
5135
5690
  const files = status
5136
5691
  .split('\n')
5137
5692
  .map((l) => l.slice(3).trim())
@@ -5140,28 +5695,40 @@ function gatherGuardFacts(op: string, root: string, text: string | undefined, st
5140
5695
  const formatGlobs = (Array.isArray(loadGuardConfig(root).rules) ? (loadGuardConfig(root).rules as { template?: unknown; params?: { file?: unknown } }[]) : [])
5141
5696
  .filter((r) => r?.template === 'format-match' && typeof r?.params?.file === 'string')
5142
5697
  .map((r) => r.params!.file as string);
5698
+ const stubScannable = files.filter((f) => scannableStubPath(f));
5699
+ const stubWanted = new Set(stubScannable.slice(0, MAX_STUB_SCAN_FILES));
5700
+ // FN-7: fail-open must not be fail-SILENT. Count every stub-scannable changed file whose
5701
+ // contents we do NOT gather (beyond the cap here; deleted/non-regular/oversize/read-error
5702
+ // below) — the no-stubs rule surfaces the count as ONE aggregate note, never a violation.
5703
+ let stubSkipped = stubScannable.length - stubWanted.size;
5143
5704
  const contents: Record<string, string> = {};
5144
- if (formatGlobs.length > 0) {
5705
+ if (formatGlobs.length > 0 || stubWanted.size > 0) {
5145
5706
  for (const f of files) {
5146
- if (!formatGlobs.some((g) => globMatch(g, f))) continue;
5707
+ const wanted = stubWanted.has(f);
5708
+ if (!wanted && !formatGlobs.some((g) => globMatch(g, f))) continue;
5147
5709
  const abs = resolve(root, f);
5148
5710
  // Containment: a `git status` path is repo-relative, but `..` in one must never let the
5149
5711
  // LIVE reader step outside the repo the HISTORICAL reader is confined to.
5150
- if (abs !== root && !abs.startsWith(root + sep)) continue;
5712
+ if (abs !== root && !abs.startsWith(root + sep)) { if (wanted) stubSkipped++; continue; }
5151
5713
  try {
5152
5714
  // lstat, NOT stat (Codex QE MED-3). `git show <sha>:<path>` yields the SYMLINK TARGET
5153
5715
  // TEXT, never the file it points at, so a live reader that follows links answers a
5154
5716
  // different question than the replay — and `/dev/zero` behind a symlink hangs the read.
5155
5717
  // Skipping non-regular files restores replay/live equivalence and closes the DoS.
5156
5718
  const st = lstatSync(abs);
5157
- if (!st.isFile()) continue;
5158
- if (st.size > MAX_CONTENT_BYTES) continue; // too large to be a spec file — undecidable, never guessed
5719
+ if (!st.isFile()) { if (wanted) stubSkipped++; continue; }
5720
+ if (st.size > MAX_CONTENT_BYTES) { if (wanted) stubSkipped++; continue; } // too large to be a spec file — undecidable, never guessed
5159
5721
  contents[f] = readFileSync(abs, 'utf8');
5160
- } catch { /* deleted — leave it undecidable, never guess */ }
5722
+ } catch { if (wanted) stubSkipped++; /* deleted — leave it undecidable, never guess */ }
5161
5723
  }
5162
5724
  }
5163
- facts['change'] = { files, ...(Object.keys(contents).length > 0 ? { contents } : {}) };
5725
+ facts['change'] = { files, ...(Object.keys(contents).length > 0 ? { contents } : {}), ...(stubSkipped > 0 ? { stubSkipped } : {}) };
5164
5726
  } catch { /* not a git repo — every template rule stays silent (fail-open) */ }
5727
+
5728
+ // no-stubs config waivers: `.dz/guard.json` `stubWaivers: [{path, reason}]` — path-keyed, reason
5729
+ // MANDATORY (the feature-adr-setup --guards shape; the pure checker refuses a reasonless entry).
5730
+ const stubWaivers = loadGuardConfig(root).stubWaivers;
5731
+ if (Array.isArray(stubWaivers)) facts['stubWaivers'] = stubWaivers;
5165
5732
  }
5166
5733
  if (op === 'consolidate') {
5167
5734
  try { facts['drift'] = sweepSkillDrift(root, { scope: 'packages', allowlist: readDriftAllowlist(root) }).drifted.map((d) => d.name); } catch { /* skip */ }
@@ -5584,6 +6151,10 @@ function cmdGuard(options: Map<string, string>, flags: Set<string>, cwd: string,
5584
6151
  const scaffold = {
5585
6152
  storeCap: DEFAULT_STORE_CAP,
5586
6153
  rules: DEFAULT_RULES.map((r) => ({ id: r.id, severity: r.severity, enabled: true, description: r.description })),
6154
+ // The scaffold QUOTES the stub marker names in the no-stubs rule description, and this file is
6155
+ // itself a scannable changed file the moment it is written — so it carries its own reasoned
6156
+ // waiver (explicit and justified, never a silent path skip).
6157
+ stubWaivers: [{ path: '.dz/guard.json', reason: 'the guard config quotes the stub marker names in the no-stubs rule description' }],
5587
6158
  };
5588
6159
  mkdirSync(dirname(p), { recursive: true });
5589
6160
  writeFileSync(p, JSON.stringify(scaffold, null, 2) + '\n');
@@ -5631,6 +6202,7 @@ function cmdGuard(options: Map<string, string>, flags: Set<string>, cwd: string,
5631
6202
  const glyph = result.verdict === 'block' ? '✗' : result.verdict === 'warn' ? '⚠' : '✓';
5632
6203
  write(`dz guard (${op}): ${glyph} ${result.verdict.toUpperCase()} [checked: ${result.checked.join(', ') || 'no rules for this op'}]`);
5633
6204
  for (const v of result.violations) write(` [${v.severity === 'hard' ? 'BLOCK' : 'warn'}] ${v.rule}: ${v.detail}`);
6205
+ for (const n of result.notes ?? []) write(` [note] ${n}`); // information, never a verdict input (FN-7)
5634
6206
  if (result.verdict === 'block' && forced) write(` → forced through: ${force} (logged to .dz/guard-audit.jsonl)`);
5635
6207
  else if (result.verdict === 'block') write(' → blocked. Fix the HARD violation(s), or override with --force "<reason>" (logged).');
5636
6208
  return guardExitCode(result, forced);
@@ -6271,6 +6843,352 @@ function nameFor(t: { file: string; name?: string }, outcome: 'pass' | 'fail' |
6271
6843
  return t.name !== undefined ? { file: t.file, name: t.name, outcome } : { file: t.file, outcome };
6272
6844
  }
6273
6845
 
6846
+ /**
6847
+ * `dz mutation-gate` — the mutation gate (feature ha-mutation-gate, SPEC at
6848
+ * features/ha-mutation-gate/SPEC.md). A green test proves the code works; it does NOT prove the
6849
+ * test would notice the protection being DELETED. For each entry in a declarative registry this
6850
+ * gate applies the entry's exact {find, replace} mutation to a SCRATCH COPY of the package, runs
6851
+ * the package's test command there, and REQUIRES a non-zero exit (red). All logic is in
6852
+ * harness-core's pure engine (mutation-gate.ts); this is the I/O executor.
6853
+ *
6854
+ * --package <dir> the package to gate (default: cwd; must contain package.json)
6855
+ * --registry <file> registry path (default: <pkg>/test/mutation-registry.json, then
6856
+ * <pkg>/mutation-registry.json)
6857
+ * --test-cmd '<cmd>' suite command run in the scratch copy (default: registry.testCommand,
6858
+ * then `npm test`)
6859
+ * --only <id[,id]> run a subset of entries (unknown id = usage error, never a silent skip)
6860
+ * --timeout <ms> per-suite-run timeout (default 300000). A timed-out run is INCONCLUSIVE —
6861
+ * a FAILURE, never a pass.
6862
+ * --rebaseline <m> route-b guard mode: 'per-entry' (default — every red entry re-runs the
6863
+ * suite on the restored tree; not green ⇒ that entry is INCONCLUSIVE) or
6864
+ * 'final' (one re-run at the end; not green ⇒ every red-based verdict is
6865
+ * downgraded). The gate's PROVEN now means the redness was ATTRIBUTABLE.
6866
+ * --keep-scratch keep the scratch copy for inspection (default: removed in a finally)
6867
+ * --json machine contract {packageDir, registryPath, testCommand, rebaselineMode,
6868
+ * baseline, results, summary, warnings, exitCode}
6869
+ *
6870
+ * The four rules (SPEC §"Four rules") and where each is enforced:
6871
+ * 1. does-not-apply = FAILURE → core classifyMutationOutcome (occurrences !== 1 ⇒ NOT_APPLIED);
6872
+ * 2. green suite = FAILURE → core (exit 0 ⇒ UNDEFENDED, names the property);
6873
+ * 3. never mutate the working tree → HERE: every write targets the scratch copy under tmpdir();
6874
+ * the repo tree is opened read-only, and a crashed run leaves at worst a stale tmp dir;
6875
+ * 4. the gate's own discrimination proof → harness-cli/test/fixtures/mutation-gate-undefended
6876
+ * (the gate MUST fail on it; asserted by test/mutation-gate-cli.test.ts).
6877
+ *
6878
+ * Exit codes: 0 every entry PROVEN · 1 the gate ran and failed (undefended / not-applied /
6879
+ * below-min / unparseable / load-fatal / over-failing / inconclusive entry) · 2 usage or setup
6880
+ * error (missing registry, red BASELINE — a red unmutated copy proves nothing and must not be
6881
+ * read as a mutation result — or an entry whose file RESOLVES outside the scratch copy: a
6882
+ * symlink escape is refused before anything is written, SPEC rule 3).
6883
+ */
6884
+ /**
6885
+ * Route-a guard for `dz mutation-gate`: parse-check a MUTATED file as its own language BEFORE the
6886
+ * suite runs. A registry mutation must delete the protection while keeping the file loadable — a
6887
+ * file that no longer parses kills the whole suite (or its import chain), and that STRUCTURAL
6888
+ * redness says nothing about the named protection. Returns `{error}` when a parser ran and the
6889
+ * text does not parse; `{skipped}` (reported loudly, never silently) when no parser is available.
6890
+ */
6891
+ function parseCheckMutatedFile(absFile: string, text: string): { error?: string; skipped?: string } {
6892
+ interface TsLike {
6893
+ transpileModule(t: string, o: { reportDiagnostics: boolean; compilerOptions: Record<string, unknown> }): { diagnostics?: { category: number; code: number; messageText: unknown }[] };
6894
+ flattenDiagnosticMessageText(m: unknown, s: string): string;
6895
+ DiagnosticCategory: { Error: number };
6896
+ ScriptTarget: { Latest: number };
6897
+ }
6898
+ const ext = extname(absFile).toLowerCase();
6899
+ try {
6900
+ if (ext === '.ts' || ext === '.tsx' || ext === '.mts' || ext === '.cts') {
6901
+ let ts: TsLike | null = null;
6902
+ for (const from of [absFile, import.meta.url]) {
6903
+ try { ts = (createRequire(from)('typescript') as TsLike); break; } catch { /* try the next resolution root */ }
6904
+ }
6905
+ if (ts === null) return { skipped: 'no TypeScript parser resolvable (typescript installed neither near the package nor near the CLI)' };
6906
+ const out = ts.transpileModule(text, { reportDiagnostics: true, compilerOptions: { target: ts.ScriptTarget.Latest } });
6907
+ const first = (out.diagnostics ?? []).find((d) => d.category === (ts as TsLike).DiagnosticCategory.Error);
6908
+ if (first === undefined) return {};
6909
+ return { error: `TS${first.code}: ${ts.flattenDiagnosticMessageText(first.messageText, ' ')}` };
6910
+ }
6911
+ if (ext === '.json') {
6912
+ try { JSON.parse(text); return {}; } catch (e) { return { error: String((e as Error).message).slice(0, 200) }; }
6913
+ }
6914
+ if (ext === '.js' || ext === '.cjs' || ext === '.mjs' || ext === '') {
6915
+ try {
6916
+ // `node --check` on the file IN PLACE, so the nearest package.json decides the module goal.
6917
+ execFileSync(process.execPath, ['--check', absFile], { stdio: 'pipe' });
6918
+ return {};
6919
+ } catch (e) {
6920
+ const err = e as { stderr?: Buffer | string };
6921
+ const stderrLines = String(err.stderr ?? '').split('\n').map((l) => l.trim()).filter((l) => l !== '');
6922
+ // prefer the actual `SyntaxError: …` line over node's trailing version footer.
6923
+ const msg = [...stderrLines].reverse().find((l) => l.includes('Error')) ?? stderrLines.at(-1) ?? 'node --check failed';
6924
+ return { error: msg.slice(0, 200) };
6925
+ }
6926
+ }
6927
+ return { skipped: `no parser for '${ext}' files — parse-check unavailable` };
6928
+ } catch (e) {
6929
+ return { skipped: `parse-check errored: ${String((e as Error).message).slice(0, 120)}` };
6930
+ }
6931
+ }
6932
+
6933
+ function cmdMutationGate(options: Map<string, string>, flags: Set<string>, cwd: string, write: Write): number {
6934
+ const json = flags.has('json');
6935
+ const fail = (what: string): number => {
6936
+ write(json ? JSON.stringify({ error: what, exitCode: 2 }) : `dz mutation-gate: ${what}`);
6937
+ return 2;
6938
+ };
6939
+
6940
+ const pkgDir = resolve(cwd, options.get('package') ?? '.');
6941
+ if (!existsSync(join(pkgDir, 'package.json'))) {
6942
+ return fail(`no package.json at ${pkgDir} — pass --package <dir>`);
6943
+ }
6944
+
6945
+ const registryOpt = options.get('registry');
6946
+ const registryPath = registryOpt !== undefined
6947
+ ? resolve(cwd, registryOpt)
6948
+ : [join(pkgDir, 'test', 'mutation-registry.json'), join(pkgDir, 'mutation-registry.json')].find((p) => existsSync(p));
6949
+ if (registryPath === undefined || !existsSync(registryPath)) {
6950
+ return fail(`no mutation registry found (looked for test/mutation-registry.json and mutation-registry.json under ${pkgDir}) — pass --registry <file>`);
6951
+ }
6952
+
6953
+ const parsed = parseMutationRegistry(readFileSync(registryPath, 'utf-8'));
6954
+ if (parsed.registry === null) {
6955
+ return fail(`registry ${registryPath} is invalid:\n - ${parsed.errors.join('\n - ')}`);
6956
+ }
6957
+
6958
+ let entries: readonly MutationRegistryEntry[] = parsed.registry.entries;
6959
+ const only = options.get('only');
6960
+ if (only !== undefined) {
6961
+ const ids = only.split(',').map((s) => s.trim()).filter(Boolean);
6962
+ const known = new Set(entries.map((e) => e.id));
6963
+ const unknown = ids.filter((id) => !known.has(id));
6964
+ if (unknown.length > 0) return fail(`--only names unknown entry id(s): ${unknown.join(', ')}`);
6965
+ entries = entries.filter((e) => ids.includes(e.id));
6966
+ }
6967
+
6968
+ const testCmdRaw = options.get('test-cmd') ?? parsed.registry.testCommand ?? 'npm test';
6969
+ if (/[\0\n\r]/.test(testCmdRaw)) return fail('--test-cmd may not contain NUL or newline characters');
6970
+ const testCmd = testCmdRaw;
6971
+
6972
+ const timeoutOpt = Number(options.get('timeout') ?? '300000');
6973
+ const timeout = Number.isFinite(timeoutOpt) && timeoutOpt > 0 ? timeoutOpt : 300000;
6974
+
6975
+ // Route-b guard mode: `per-entry` (default, strongest — each red entry re-baselines the restored
6976
+ // tree, so a flaky neighbour flips THAT entry to INCONCLUSIVE) or `final` (cheap — one re-run at
6977
+ // the end; if it is not green, every red-based verdict of the run is downgraded, because any of
6978
+ // them may have been the flake). MEASURED on the 18-entry health-advisor registry (~15s/suite
6979
+ // run): per-entry ≈ 37 runs, final ≈ 20 runs vs 19 pre-fix. An unknown mode is a usage error.
6980
+ const rebaselineMode = options.get('rebaseline') ?? 'per-entry';
6981
+ if (rebaselineMode !== 'per-entry' && rebaselineMode !== 'final') {
6982
+ return fail(`--rebaseline must be 'per-entry' or 'final', got '${rebaselineMode}'`);
6983
+ }
6984
+
6985
+ // Rule 3 — NEVER mutate the working tree: the package is copied into a scratch dir we own and
6986
+ // mutated THERE. The copy must actually be RUNNABLE (SPEC rule 3's note), which took three
6987
+ // measured layers on the seed package:
6988
+ // • the package's own node_modules is symlinked back (absolute), so deps + .bin resolve;
6989
+ // • the copy lives inside a SHADOW of the package's repo — every ancestor level mirrors the
6990
+ // real one with SYMLINKED siblings (root node_modules for hoisted deps, sibling packages
6991
+ // for repo-relative test paths like `../../harness-core/dist`); only the package under test
6992
+ // is a real, mutable copy (MEASURED: without this, 30 health-advisor tests failed at
6993
+ // baseline on ERR_MODULE_NOT_FOUND / a missing sibling dist);
6994
+ // • the copy is `git init`-ed and committed, because hygiene tests take `git status` before
6995
+ // and after the run — they compare before WITH after, so a pre-mutation commit keeps them
6996
+ // discriminating (MEASURED: without it, 2 tests failed at baseline on "not a git repository").
6997
+ const scratchParent = mkdtempSync(join(tmpdir(), 'dz-mutgate-'));
6998
+ let gitTop: string | null = null;
6999
+ try { gitTop = execSync('git rev-parse --show-toplevel', { cwd: pkgDir, stdio: 'pipe', encoding: 'utf-8' }).trim() || null; } catch { /* not in a git repo */ }
7000
+ let copyDir = join(scratchParent, 'pkg');
7001
+ const results: MutationEntryResult[] = [];
7002
+ const observations: MutationObservation[] = [];
7003
+ const warnings: string[] = [];
7004
+ let baseline: ReturnType<typeof classifyBaseline>;
7005
+ try {
7006
+ if (gitTop !== null && gitTop !== pkgDir && resolve(pkgDir).startsWith(resolve(gitTop) + sep)) {
7007
+ // shadow tree: mirror <gitTop>/…/<pkg> under scratch, symlinking every sibling entry.
7008
+ let realCursor = gitTop;
7009
+ let shadowCursor = join(scratchParent, 'root');
7010
+ mkdirSync(shadowCursor, { recursive: true });
7011
+ const segs = relative(gitTop, pkgDir).split(sep);
7012
+ segs.forEach((seg, i) => {
7013
+ for (const entry of readdirSync(realCursor)) {
7014
+ if (entry === seg || entry === '.git') continue;
7015
+ try { symlinkSync(join(realCursor, entry), join(shadowCursor, entry)); } catch { /* best effort */ }
7016
+ }
7017
+ realCursor = join(realCursor, seg);
7018
+ shadowCursor = join(shadowCursor, seg);
7019
+ if (i < segs.length - 1) mkdirSync(shadowCursor, { recursive: true });
7020
+ });
7021
+ copyDir = shadowCursor;
7022
+ }
7023
+ cpSync(pkgDir, copyDir, {
7024
+ recursive: true,
7025
+ filter: (src) => {
7026
+ const rel = relative(pkgDir, src);
7027
+ return rel === '' || !rel.split(sep).some((seg) => seg === 'node_modules' || seg === '.git');
7028
+ },
7029
+ });
7030
+ const srcNm = join(pkgDir, 'node_modules');
7031
+ if (existsSync(srcNm) && !existsSync(join(copyDir, 'node_modules'))) {
7032
+ symlinkSync(srcNm, join(copyDir, 'node_modules'), 'dir');
7033
+ }
7034
+ try {
7035
+ execSync('git init -q && git add -A -f . && git -c user.email=mutation-gate@dz -c user.name=mutation-gate -c commit.gpgsign=false commit -qm scratch-baseline', { cwd: copyDir, stdio: 'pipe' });
7036
+ } catch { /* no git available → a suite that needs it fails the BASELINE loudly, never silently */ }
7037
+
7038
+ // F-2 — rule-3 containment root: the scratch copy AS THE FILESYSTEM sees it. Every mutation
7039
+ // write below is asserted to RESOLVE inside this root before it happens.
7040
+ const realScratchRoot = realpathSync(copyDir);
7041
+
7042
+ const runSuite = (): { exitCode: number | null; output: string } => {
7043
+ try {
7044
+ const out = execSync(testCmd, { cwd: copyDir, stdio: 'pipe', encoding: 'utf-8', timeout, maxBuffer: 64 * 1024 * 1024, env: { ...process.env, FORCE_COLOR: '0' } });
7045
+ return { exitCode: 0, output: out };
7046
+ } catch (e) {
7047
+ const err = e as { status?: number | null; stdout?: string; stderr?: string };
7048
+ return {
7049
+ exitCode: typeof err.status === 'number' ? err.status : null,
7050
+ output: `${String(err.stdout ?? '')}\n${String(err.stderr ?? '')}`,
7051
+ };
7052
+ }
7053
+ };
7054
+
7055
+ // Baseline BEFORE any mutation: a red copy proves nothing, and reading it as a mutation
7056
+ // result would be this gate shipping the defect class it exists to catch.
7057
+ if (!json) write(`mutation-gate: baseline suite in scratch copy of ${pkgDir} …`);
7058
+ const base = runSuite();
7059
+ baseline = classifyBaseline(base.exitCode);
7060
+ if (!baseline.ok) {
7061
+ if (json) { write(JSON.stringify({ packageDir: pkgDir, registryPath, testCommand: testCmd, baseline, results: [], exitCode: 2 }, null, 2)); return 2; }
7062
+ write(renderMutationReport([], baseline, pkgDir));
7063
+ return 2;
7064
+ }
7065
+
7066
+ for (const entry of entries) {
7067
+ const filePath = join(copyDir, entry.file);
7068
+ let sourceText: string | null = null;
7069
+ try { sourceText = readFileSync(filePath, 'utf-8'); } catch { /* missing file ⇒ occurrences 0 ⇒ NOT_APPLIED */ }
7070
+ if (sourceText === null) {
7071
+ const obs: MutationObservation = { entry, occurrences: 0, exitCode: null, failingCount: null };
7072
+ observations.push(obs);
7073
+ results.push(classifyMutationOutcome(obs));
7074
+ continue;
7075
+ }
7076
+ const applied = applyMutationToText(sourceText, entry.mutation.find, entry.mutation.replace);
7077
+ if (!applied.ok || applied.text === undefined) {
7078
+ const obs: MutationObservation = { entry, occurrences: applied.occurrences, exitCode: null, failingCount: null };
7079
+ observations.push(obs);
7080
+ results.push(classifyMutationOutcome(obs));
7081
+ continue;
7082
+ }
7083
+ // F-2 — rule-3 containment (SPEC "Never mutate the working tree"): `join(copyDir, file)` is
7084
+ // LEXICAL; a symlink cpSync preserved inside the package (or the intentionally symlinked
7085
+ // node_modules) makes it RESOLVE outside the scratch tree, and the "scratch" write would
7086
+ // follow the link and mutate the REAL working tree for the whole suite run — restored only
7087
+ // by the finally, so a SIGKILL mid-run leaves the real tree permanently mutated (MEASURED
7088
+ // pre-fix: a registry file behind a package-local symlink; the suite-run witness read the
7089
+ // mutated text from the REAL file). Same primitive as health-advisor lock.js's
7090
+ // realCaseDir/assertLockRootIsItself: decide on realpaths, refuse an escape — exit 2, a
7091
+ // registry/setup error, never a mutation.
7092
+ let realTarget: string | null = null;
7093
+ try { realTarget = realpathSync(filePath); } catch { /* vanished between read and here → refuse below */ }
7094
+ if (realTarget === null || (realTarget !== realScratchRoot && !realTarget.startsWith(realScratchRoot + sep))) {
7095
+ return fail(`entry '${entry.id}': ${entry.file} resolves to ${realTarget ?? '<unresolvable>'} — OUTSIDE the scratch copy (${realScratchRoot}). A path component is a symlink escaping the scratch tree, so writing the mutation would mutate the REAL working tree (SPEC rule 3). Refused; nothing was written.`);
7096
+ }
7097
+ if (!json) write(`mutation-gate: ${entry.id} — mutating ${entry.file}, running suite …`);
7098
+ let run: { exitCode: number | null; output: string } | null = null;
7099
+ let parseError: string | undefined;
7100
+ try {
7101
+ writeFileSync(filePath, applied.text);
7102
+ // Route-a guard: the mutated file must still PARSE — a load failure reddens the whole
7103
+ // suite for structural, not behavioural, reasons, and must never read as discrimination.
7104
+ const check = parseCheckMutatedFile(filePath, applied.text);
7105
+ if (check.skipped !== undefined) {
7106
+ warnings.push(`${entry.id}: parse-check SKIPPED — ${check.skipped}`);
7107
+ if (!json) write(`mutation-gate: WARNING ${entry.id}: parse-check skipped — ${check.skipped}`);
7108
+ }
7109
+ if (check.error !== undefined) {
7110
+ parseError = check.error; // no suite run: the verdict is MUTATION_UNPARSEABLE regardless
7111
+ } else {
7112
+ run = runSuite();
7113
+ }
7114
+ } finally {
7115
+ writeFileSync(filePath, sourceText); // restore the COPY so the next entry starts pristine
7116
+ }
7117
+ // Route-a′ guard (round-6 rework): the file-load-vs-assertion signal is derived from THE
7118
+ // SAME RUN that produced the failing count — no isolated child, no environment mismatch,
7119
+ // nothing to disagree with itself (the round-5 isolated `import()` had three measured
7120
+ // false-PASS routes, all artifacts of the isolation environment differing from the runner).
7121
+ // 'file-load' ⇒ MUTATION_LOAD_FATAL (structural); 'unrecognised' ⇒ INCONCLUSIVE (a
7122
+ // runner-coverage gap of this tool, loud, never PROVEN); 'assertions' ⇒ behavioural, the
7123
+ // count-based verdicts apply.
7124
+ let fileLoadFailure: string | undefined;
7125
+ let outputUnrecognised: string | undefined;
7126
+ if (run !== null && run.exitCode !== null && run.exitCode !== 0) {
7127
+ const cls = classifyRunFailure(run.output);
7128
+ if (cls.kind === 'file-load') {
7129
+ fileLoadFailure = cls.evidence ?? 'test file failed to load (no evidence line)';
7130
+ } else if (cls.kind === 'unrecognised') {
7131
+ outputUnrecognised = cls.evidence ?? `no classifier for runner '${cls.runner}'`;
7132
+ }
7133
+ }
7134
+ // Route-b guard (per-entry mode): a red mutated run is attributable only if the RESTORED
7135
+ // tree reproduces green — otherwise a flaky neighbour may be what went red. Skipped when the
7136
+ // classification already failed the entry structurally (file-load / unrecognised): those
7137
+ // verdicts outrank the rebaseline check, so the extra suite run would buy nothing.
7138
+ let rebaselineExitCode: number | null | undefined;
7139
+ if (rebaselineMode === 'per-entry' && run !== null && run.exitCode !== null && run.exitCode !== 0
7140
+ && fileLoadFailure === undefined && outputUnrecognised === undefined) {
7141
+ if (!json) write(`mutation-gate: ${entry.id} — re-baselining the restored tree …`);
7142
+ rebaselineExitCode = runSuite().exitCode;
7143
+ }
7144
+ const obs: MutationObservation = {
7145
+ entry,
7146
+ occurrences: 1,
7147
+ exitCode: run === null ? null : run.exitCode,
7148
+ failingCount: run === null ? null : countFailingTests(run.output),
7149
+ ...(parseError !== undefined ? { parseError } : {}),
7150
+ ...(fileLoadFailure !== undefined ? { fileLoadFailure } : {}),
7151
+ ...(outputUnrecognised !== undefined ? { outputUnrecognised } : {}),
7152
+ ...(rebaselineExitCode !== undefined ? { rebaselineExitCode } : {}),
7153
+ };
7154
+ observations.push(obs);
7155
+ results.push(classifyMutationOutcome(obs));
7156
+ }
7157
+
7158
+ // Route-b guard (final mode): one re-run after all entries. Not green ⇒ EVERY red-based
7159
+ // verdict of this run is downgraded (any of them may have been the flake, and there is no
7160
+ // per-entry evidence to say which) — re-classifying with the final exit turns them
7161
+ // INCONCLUSIVE while leaving NOT_APPLIED / UNDEFENDED / MUTATION_UNPARSEABLE /
7162
+ // MUTATION_LOAD_FATAL untouched.
7163
+ if (rebaselineMode === 'final') {
7164
+ if (!json) write('mutation-gate: final re-baseline of the restored tree …');
7165
+ const finalExit = runSuite().exitCode;
7166
+ if (finalExit !== 0) {
7167
+ const what = finalExit === null ? 'no exit code' : `exit ${finalExit}`;
7168
+ warnings.push(`final re-baseline NOT green (${what}) — the suite is flaky; red-based verdicts downgraded to INCONCLUSIVE`);
7169
+ if (!json) write(`mutation-gate: final re-baseline NOT green (${what}) — red-based verdicts downgraded to INCONCLUSIVE`);
7170
+ const reclassified = observations.map((obs) => classifyMutationOutcome({ ...obs, rebaselineExitCode: finalExit }));
7171
+ results.length = 0;
7172
+ results.push(...reclassified);
7173
+ }
7174
+ }
7175
+ } finally {
7176
+ if (flags.has('keep-scratch')) {
7177
+ write(`mutation-gate: scratch copy kept at ${copyDir}`);
7178
+ } else {
7179
+ try { rmSync(scratchParent, { recursive: true, force: true }); } catch { /* best effort */ }
7180
+ }
7181
+ }
7182
+
7183
+ const exitCode = mutationGateExitCode(results, baseline.ok);
7184
+ if (json) {
7185
+ write(JSON.stringify({ packageDir: pkgDir, registryPath, testCommand: testCmd, rebaselineMode, baseline, results, summary: summarizeMutationResults(results), warnings, exitCode }, null, 2));
7186
+ return exitCode;
7187
+ }
7188
+ write(renderMutationReport(results, baseline, pkgDir));
7189
+ return exitCode;
7190
+ }
7191
+
6274
7192
  /**
6275
7193
  * `dz delivery-check` — the portable Step-10 Delivery Gate (feature portable-gates). The `manual` form that
6276
7194
  * travels to every `shell` target: the deterministic parts (artifact probes, hand-off arithmetic,
@@ -7350,6 +8268,21 @@ async function cmdBacklog(options: Map<string, string>, flags: Set<string>, cwd:
7350
8268
  const eff = parseEffort(options.get('effort'), cfg.roulette.defaultEffort);
7351
8269
  if (eff.adjusted && !json && eff.note !== undefined) write(`dz backlog: ${eff.note}`);
7352
8270
  const dryRun = flags.has('dry-run');
8271
+ // Embed-form migration (register-inflation fix): v1 vectors are FULL-TEXT embeds, v2 queries are
8272
+ // bounded excerpts — comparing across the forms is a query-vs-row space split. Re-mirror once
8273
+ // (idempotent upsert), before the dedup search. Dry-run writes nothing, so it only WARNS.
8274
+ if (dryRun) {
8275
+ if (readBacklogEmbedFormVersion(projectRoot) < DEDUP_EMBED_FORM_VERSION && readIdeas(projectRoot).length > 0 && !json) {
8276
+ write(`dz backlog: ⚠ idea vectors are in the old (full-text) embed form — dedup may be unreliable until a non-dry add or \`dz backlog harmonize\` migrates them`);
8277
+ }
8278
+ } else {
8279
+ const form = await ensureBacklogEmbedForm(projectRoot);
8280
+ if (form.action === 'migrated' && !json) {
8281
+ write(`dz backlog: re-embedded ${form.remirrored} idea vector(s) into the bounded dedup embed form (v${form.version})`);
8282
+ } else if (form.action === 'deferred' && !json) {
8283
+ write(`dz backlog: ⚠ embed-form migration deferred (${form.error ?? 'unknown error'}) — semantic dedup may compare against stale full-text vectors`);
8284
+ }
8285
+ }
7353
8286
  const verdict = await dedupIdea(projectRoot, text, cfg);
7354
8287
  // The TOP-MATCH pair (id @ cosine) is the ADR-002 calibration surface (idea ce914ac2) — observational
7355
8288
  // only: the band itself is unchanged, but a RELATED verdict now shows WHICH idea produced the cosine.
@@ -7362,16 +8295,35 @@ async function cmdBacklog(options: Map<string, string>, flags: Set<string>, cwd:
7362
8295
  // DUPLICATE ⇒ snapshot + reinforce the existing root; NO new record (ADR-002 T-002b).
7363
8296
  const ideas = readIdeas(projectRoot);
7364
8297
  const match = ideas.find((i) => i.id === verdict.matchedId);
8298
+ let absorbErr: string | undefined;
7365
8299
  if (!dryRun && match !== undefined) {
7366
8300
  const snap = snapshotIdeas(projectRoot, join(projectRoot, '.dz', 'backlog', `ideas.pre-merge-${Date.now()}.jsonl`));
7367
8301
  if (snap.error !== undefined) return emitErr(snap.error);
8302
+ // The absorbed TEXT is preserved (absorbed.jsonl) — a duplicate verdict must never destroy
8303
+ // user text: two documented false absorptions (2026-08-05, 2026-08-11) were unrecoverable.
8304
+ absorbErr = recordAbsorption(projectRoot, {
8305
+ ts: new Date().toISOString(),
8306
+ matchedId: match.id,
8307
+ cosine: verdict.cosine,
8308
+ ...(verdict.containment !== undefined ? { containment: verdict.containment } : {}),
8309
+ ...(verdict.subsetMatch === true ? { subsetMatch: true } : {}),
8310
+ text,
8311
+ }).error;
7368
8312
  match.uses += 1;
7369
8313
  writeIdeas(projectRoot, ideas);
7370
8314
  }
7371
- if (json) write(JSON.stringify({ action: 'duplicate', matchedId: verdict.matchedId, cosine: verdict.cosine, ...(topMatch !== undefined ? { topMatch } : {}), ...(eff.note !== undefined ? { effortNote: eff.note } : {}), exitCode: 0 }, null, 2));
8315
+ if (json) write(JSON.stringify({ action: 'duplicate', matchedId: verdict.matchedId, cosine: verdict.cosine, ...(verdict.containment !== undefined ? { containment: verdict.containment } : {}), ...(verdict.subsetMatch === true ? { subsetMatch: true } : {}), ...(topMatch !== undefined ? { topMatch } : {}), ...(eff.note !== undefined ? { effortNote: eff.note } : {}), ...(dryRun ? {} : { absorbedLogged: absorbErr === undefined, ...(absorbErr !== undefined ? { absorbedLogError: absorbErr } : {}) }), exitCode: 0 }, null, 2));
7372
8316
  else {
7373
- write(`dz backlog: DUPLICATE of ${verdict.matchedId} (cosine ${verdict.cosine.toFixed(3)}${verdict.exactTextOnly ? ', exact-text' : ''}) — reinforced, no new record`);
8317
+ const via = verdict.subsetMatch === true
8318
+ ? `subset match: containment ${(verdict.containment ?? 0).toFixed(3)} ≥ ${cfg.dedup.subsetContainment}, cosine ${verdict.cosine.toFixed(3)}`
8319
+ : `cosine ${verdict.cosine.toFixed(3)}${verdict.exactTextOnly ? ', exact-text' : ''}`;
8320
+ write(`dz backlog: DUPLICATE of ${verdict.matchedId} (${via}) — reinforced, no new record`);
7374
8321
  if (topMatch !== undefined) write(` top match ${topMatch.id} @ cosine ${topMatch.cosine.toFixed(3)} (DUPLICATE band ≥ ${cfg.dedup.duplicateThreshold})`);
8322
+ if (!dryRun) {
8323
+ write(absorbErr === undefined
8324
+ ? ' absorbed text kept in .dz/backlog/absorbed.jsonl (re-add it from there if this verdict was wrong)'
8325
+ : ` ⚠ could NOT log the absorbed text (${absorbErr}) — if this verdict is wrong, the wording above is the only copy`);
8326
+ }
7375
8327
  }
7376
8328
  return 0;
7377
8329
  }
@@ -7392,13 +8344,19 @@ async function cmdBacklog(options: Map<string, string>, flags: Set<string>, cwd:
7392
8344
  };
7393
8345
  const proposal = options.get('proposal');
7394
8346
  if (proposal !== undefined) rec.proposal = proposal; // agent prose ONLY — the CLI never fabricates it
8347
+ // A demotion (≥-threshold cosine that failed lexical corroboration) is NEVER silent — it is the
8348
+ // register-only false-duplicate surface (the 2026-08-05 zombie x publish-gate absorption).
8349
+ const demotedLine = verdict.demoted !== undefined
8350
+ ? ` near-duplicate demoted: ${verdict.demoted.id} @ cosine ${verdict.demoted.cosine.toFixed(3)} cleared the band but shares no subject vocabulary (containment ${verdict.demoted.containment.toFixed(3)} < ${cfg.dedup.corroborationFloor}) — kept as related`
8351
+ : undefined;
7395
8352
  if (dryRun) {
7396
- if (json) write(JSON.stringify({ action: verdict.action, dryRun: true, idea: rec, ...(topMatch !== undefined ? { topMatch } : {}), ...(eff.note !== undefined ? { effortNote: eff.note } : {}), exitCode: 0 }, null, 2));
8353
+ if (json) write(JSON.stringify({ action: verdict.action, dryRun: true, idea: rec, ...(verdict.demoted !== undefined ? { demoted: verdict.demoted } : {}), ...(topMatch !== undefined ? { topMatch } : {}), ...(eff.note !== undefined ? { effortNote: eff.note } : {}), exitCode: 0 }, null, 2));
7397
8354
  else {
7398
8355
  write(`dz backlog (dry-run): ${verdict.action.toUpperCase()} — would create ${rec.id}; align ${rec.goalAlignment.toFixed(3)}${rec.goalId !== null ? ` → ${rec.goalId}` : ''}`);
7399
8356
  // The calibration surface belongs on the dry-run too (QE LOW-8) — a dry-run is exactly where a
7400
8357
  // user checks whether a near-duplicate should have crossed the band.
7401
8358
  if (topMatch !== undefined) write(` top match ${topMatch.id} @ cosine ${topMatch.cosine.toFixed(3)} (DUPLICATE band ≥ ${cfg.dedup.duplicateThreshold})`);
8359
+ if (demotedLine !== undefined) write(demotedLine);
7402
8360
  }
7403
8361
  return 0;
7404
8362
  }
@@ -7409,12 +8367,13 @@ async function cmdBacklog(options: Map<string, string>, flags: Set<string>, cwd:
7409
8367
  ideas.push(rec);
7410
8368
  writeIdeas(projectRoot, ideas);
7411
8369
  const mirror = await mirrorIdeaVector(projectRoot, rec); // best-effort — never blocks capture
7412
- if (json) write(JSON.stringify({ action: verdict.action, idea: rec, related: verdict.relatedIds, ...(topMatch !== undefined ? { topMatch } : {}), ...(eff.note !== undefined ? { effortNote: eff.note } : {}), gitignore: ignore, exitCode: 0 }, null, 2));
8370
+ if (json) write(JSON.stringify({ action: verdict.action, idea: rec, related: verdict.relatedIds, ...(verdict.demoted !== undefined ? { demoted: verdict.demoted } : {}), ...(topMatch !== undefined ? { topMatch } : {}), ...(eff.note !== undefined ? { effortNote: eff.note } : {}), gitignore: ignore, exitCode: 0 }, null, 2));
7413
8371
  else {
7414
8372
  write(`dz backlog: ${verdict.action.toUpperCase()} — captured ${rec.id}`);
7415
8373
  if (verdict.action === 'related' && topMatch !== undefined) {
7416
8374
  write(` top match ${topMatch.id} @ cosine ${topMatch.cosine.toFixed(3)} (DUPLICATE band ≥ ${cfg.dedup.duplicateThreshold})`);
7417
8375
  }
8376
+ if (demotedLine !== undefined) write(demotedLine);
7418
8377
  if (rec.goalId !== null) write(` top goal: ${rec.goalId} (alignment ${rec.goalAlignment.toFixed(3)})`);
7419
8378
  if (verdict.relatedIds.length > 0) write(` related: ${verdict.relatedIds.join(', ')}`);
7420
8379
  if (ignore.action === 'created' || ignore.action === 'appended') {
@@ -7587,6 +8546,37 @@ async function cmdBacklog(options: Map<string, string>, flags: Set<string>, cwd:
7587
8546
  return 0;
7588
8547
  }
7589
8548
 
8549
+ // ── ship | drop | reopen — the status-transition surface (the missing verb that let the roulette
8550
+ // keep re-drawing already-shipped work: without it, work finished WITHOUT `roulette --commit` —
8551
+ // the normal flow — stayed `new` forever). ALL logic lives in transitionIdeas (harness-core):
8552
+ // short-prefix resolution (unique or a loud error), the IDEA_TRANSITIONS legality table,
8553
+ // idempotent no-ops, all-or-nothing fail-closed batches, line-preserving atomic writes.
8554
+ if (sub === 'ship' || sub === 'drop' || sub === 'reopen') {
8555
+ const prefixes: string[] = [];
8556
+ for (let i = 1; ; i += 1) {
8557
+ const p = options.get(`_positional_${i}`);
8558
+ if (p === undefined) break;
8559
+ prefixes.push(p);
8560
+ }
8561
+ if (prefixes.length === 0) return emitErr(`an idea id is required: dz backlog ${sub} <id> [<id>…]`);
8562
+ const report = transitionIdeas(projectRoot, sub, prefixes, {
8563
+ ...(options.get('reason') !== undefined ? { reason: options.get('reason')! } : {}),
8564
+ dryRun: flags.has('dry-run'),
8565
+ });
8566
+ if (json) {
8567
+ write(JSON.stringify({ verb: sub, ...report, exitCode: report.ok ? 0 : 1 }, null, 2));
8568
+ return report.ok ? 0 : 1;
8569
+ }
8570
+ for (const e of report.errors) write(`dz backlog ${sub}: ${e}`);
8571
+ for (const c of report.changes) {
8572
+ if (c.action === 'noop') write(`dz backlog ${sub}: ${c.id} is already ${c.to} — no-op`);
8573
+ else write(`dz backlog ${sub}${report.dryRun ? ' (dry-run)' : ''}: ${c.id} ${c.from} → ${c.to} ${c.text}`);
8574
+ }
8575
+ if (!report.ok) write(` nothing was written (all-or-nothing: fix the batch and re-run)`);
8576
+ else if (report.dryRun && report.changes.some((c) => c.action === 'transitioned')) write(' (dry-run — nothing written; re-run without --dry-run to apply)');
8577
+ return report.ok ? 0 : 1;
8578
+ }
8579
+
7590
8580
  if (sub === 'enrich') {
7591
8581
  const id = options.get('_positional_1');
7592
8582
  if (id === undefined) return emitErr('an idea id is required: dz backlog enrich <id>');
@@ -7632,6 +8622,11 @@ async function cmdBacklog(options: Map<string, string>, flags: Set<string>, cwd:
7632
8622
  if (sub === 'harmonize') {
7633
8623
  const apply = flags.has('apply');
7634
8624
  const thr = options.get('threshold');
8625
+ // Harmonize is the batch maintenance surface — migrate the mirrored vectors to the current
8626
+ // bounded embed form here too (idempotent; a deferral is warned, never fatal).
8627
+ const form = await ensureBacklogEmbedForm(projectRoot);
8628
+ if (form.action === 'migrated' && !json) write(`dz backlog: re-embedded ${form.remirrored} idea vector(s) into the bounded dedup embed form (v${form.version})`);
8629
+ else if (form.action === 'deferred' && !json) write(`dz backlog: ⚠ embed-form migration deferred (${form.error ?? 'unknown error'})`);
7635
8630
  const report = await harmonizeBacklog(projectRoot, { apply, ...(thr !== undefined ? { threshold: Number(thr) } : {}) });
7636
8631
  if (json) {
7637
8632
  write(JSON.stringify({ ...report, exitCode: 0 }, null, 2));
@@ -7652,6 +8647,9 @@ async function cmdBacklog(options: Map<string, string>, flags: Set<string>, cwd:
7652
8647
  write(' show <id> full record');
7653
8648
  write(' goals [--validate] the compass (.dz/backlog/goals.json)');
7654
8649
  write(' roulette [--pick N][--seed n][--commit <id>] weighted draw; --commit takes the id you saw');
8650
+ write(' ship <id> [<id>…] [--reason <t>][--dry-run] mark done (new|enriched|in-progress → shipped) — run it after finishing a task, or the roulette re-draws it forever');
8651
+ write(' drop <id> [<id>…] [--reason <t>][--dry-run] retire an idea (→ dropped)');
8652
+ write(' reopen <id> [<id>…] [--reason <t>][--dry-run] back to the pool (shipped|dropped|in-progress → new)');
7655
8653
  write(' enrich <id> stage the idea2prd hand-off (agent expands)');
7656
8654
  write(` jira <id> draft a Jira issue (adapter: ${[...BACKLOG_BACKENDS].join('|')})`);
7657
8655
  write(' harmonize [--apply][--threshold 0-1] batch semantic dedup of the backlog');
@@ -7998,12 +8996,16 @@ export async function runCli(argv: string[], io: CliIo = {}): Promise<number> {
7998
8996
  return await cmdScout(options, flags, cwd, write);
7999
8997
  case 'workflow':
8000
8998
  return cmdWorkflow(options, flags, cwd, write);
8999
+ case 'workflow-lint':
9000
+ return cmdWorkflowLint(options, flags, cwd, write);
9001
+ case 'workflow-trace':
9002
+ return cmdWorkflowTrace(options, flags, cwd, write);
8001
9003
  case 'migrate':
8002
9004
  return cmdMigrate(options, cwd, write);
8003
9005
  case 'doctor':
8004
9006
  return await cmdDoctor(options, flags, cwd, write);
8005
9007
  case 'install':
8006
- return await cmdInstall(options, flags, cwd, write);
9008
+ return await cmdInstall(options, flags, cwd, write, io.installRunner);
8007
9009
  case 'bundle':
8008
9010
  return cmdBundle(options, flags, cwd, write);
8009
9011
  case 'teach':
@@ -8082,6 +9084,8 @@ export async function runCli(argv: string[], io: CliIo = {}): Promise<number> {
8082
9084
  return cmdChallenge(options, flags, cwd, write);
8083
9085
  case 'discrimination-check':
8084
9086
  return cmdDiscriminationCheck(options, flags, cwd, write);
9087
+ case 'mutation-gate':
9088
+ return cmdMutationGate(options, flags, cwd, write);
8085
9089
  case 'delivery-check':
8086
9090
  return cmdDeliveryCheck(options, flags, cwd, write);
8087
9091
  case 'skills-verify':