@dzhechkov/harness-cli 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/cli.ts CHANGED
@@ -33,6 +33,7 @@ import {
33
33
  RECONCILE_BANNER,
34
34
  buildRegistry,
35
35
  discoverSkillPackDirs,
36
+ discoverVerifiablePackDirs,
36
37
  checkUpstream,
37
38
  compareSkills,
38
39
  checkAllUpstream,
@@ -155,6 +156,7 @@ import {
155
156
  bundleSkills,
156
157
  brainHome,
157
158
  listBrain,
159
+ bookKbPath,
158
160
  promoteProjectToBrain,
159
161
  updateBrainSource,
160
162
  queryBrain,
@@ -400,7 +402,21 @@ import {
400
402
  parseBundle,
401
403
  planImport,
402
404
  decideCheckpointWrite,
405
+ amendmentSection,
406
+ planSaysNoAmendments,
407
+ parseAmendments,
408
+ resolveAmendments,
409
+ decideAmendmentOutcome,
410
+ amendmentVerdictLine,
411
+ amendmentsMissingFromPlan,
412
+ AMENDMENT_VACUITY_NOTE,
413
+ decideSignableSet,
414
+ signableSetLine,
415
+ decideRecordWrite,
416
+ decideReadBack,
417
+ recordVerdictLine,
403
418
  } from '@dzhechkov/harness-core';
419
+ import type { RecordKind, RecordDecision } from '@dzhechkov/harness-core';
404
420
  import type { MutationEntryResult, MutationObservation, MutationRegistryEntry } from '@dzhechkov/harness-core';
405
421
  import type { SkillApplyFailure, SkillLoadFailure } from '@dzhechkov/harness-core';
406
422
  import type { ReqeDebt } from '@dzhechkov/harness-core';
@@ -450,6 +466,8 @@ Usage:
450
466
  dz epoch-replay --judge <filled-work-order.json> [--out <file>] (blind judge prompts from the filled plans)
451
467
  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)
452
468
  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)
469
+ dz amendment-check --slug <slug> | --feature-dir <dir> | --all [--json] (the deterministic Step-8 amendment gate: every AM-N row must resolve to a test found INSIDE the file the row names; the PLAN is authoritative when it carries rows, and an ideation amendment the plan drops is a failure. exit 0 pass/skip, 1 fail, 3 NOT-ESTABLISHED — a section that parsed ZERO rows is never a pass. --all is a CENSUS and always exits 0. Does NOT prove non-vacuity — that is dz discrimination-check)
470
+ dz feature-adr-record --kind ledger|training-pair --stage <s> [--slug <s>] [--row|--pair <json>] [--mark <n>] [--once] [--json] (the witnessed writer for the run-cost ledger and training pairs: the payload arrives as an ARGUMENT, never as shell; a malformed or wrong-kind payload is REFUSED before any write; the timestamp is stamped before serialising; the append is verified by re-reading the tail. exit 0 written|duplicate|skipped, 2 refused, 3 not-verified — a record failure is never blocking)
453
471
  dz feature-adr-checkpoint (--slug <feature> | --feature-dir <abs>) --stage <s> --input-hash <h> --result <json> [--artifact a,b] [--json] (record a pipeline stage ONLY after measuring its artifacts on disk; refuses a null result, an absent artifact, or a stage that declares none — the subagent runs a COMMAND instead of hand-writing durable state)
454
472
  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)
455
473
  dz qe-bridge --family claude --slug <feature> [--coder-family codex|claude] [--model <id>] [--files a,b] [--out <f>] [--timeout <s>] [--allow-same-family] [--json] (the REVERSE QE bridge: run an INDEPENDENT Claude reviewer over a feature's Step-8 artifacts from ANY host — a Codex session included, plain shell, no Claude agent plane needed — and land a PARSED signoff. The reviewer runs ISOLATED: an EMPTY temp cwd plus --safe-mode --strict-mcp-config --tools '' --no-session-persistence, so no CLAUDE.md/skills/plugins/hooks/MCP load, and the verdict is read from the --output-format json RESULT ENVELOPE — text a session customization printed onto the same stdout can never become a signoff. Probes the model before trusting it; sends SCOPED extracts with a loud 200k-char ceiling (never silent truncation); the grade must AGREE across three LAST-anchored channels (terminal marker line, fenced qe-bridge-signoff JSON, the report's own GRADE line) AND the marker must be the FINAL content — empty, gradeless, self-contradicting or miscounted output is one of 18 NAMED failures with an audit record under features/<slug>/.fa-state/qe-bridge/ (runId, resolved executable + binOverride, prompt sha256, channel offsets, requestedOut, reportWritten, retained raw stdout; 0600 files in a 0700 dir), never a clean review. A --coder-family that contradicts the recorded reqe debt is refused. Writes features/<slug>/08b_reqe_report.md, which dz reqe --done settles unchanged. DISCLOSURE: the extracts you scope are sent to the Claude runtime; the bridge cannot classify secrets. DZ_QE_BRIDGE_CLAUDE_BIN is a TEST SEAM, not a flag. exit 0 signoff parsed (ANY grade — it reports, it does not gate) / 1 named failure / 2 usage)
@@ -2664,6 +2682,13 @@ async function cmdTeach(options: Map<string, string>, flags: Set<string>, cwd: s
2664
2682
  }
2665
2683
  write(`Imported ${imported} pattern(s) from ${fromJson}`);
2666
2684
  write(` Skipped ${skipped} (duplicates already in the store, or invalid entries)`);
2685
+ // Carrying a brain to a new machine goes through this path, and the mirror gate is the SAME one
2686
+ // teach uses — so without a config the whole import lands unindexed while `vector status` still
2687
+ // prints `pending: 0`. Say it here, where the user can act on it (FR-6).
2688
+ if (imported > 0 && !flags.has('no-mirror') && !vectorMirrorEnabled(projectRoot)) {
2689
+ write(` ⚠ the vector mirror writer is OFF — these ${imported} pattern(s) are LEXICAL ONLY`);
2690
+ write(` enable it in .dz/config.json (memory.backend=agentdb), then run: dz vector reindex`);
2691
+ }
2667
2692
  // Bulk import preserves the DOMAIN of every record, so it can put medical lessons in
2668
2693
  // a shared store as silently as a hand-typed teach — and it returned before the
2669
2694
  // advice single-teach prints. The same advice, at the same point in the flow: after
@@ -3066,6 +3091,45 @@ async function cmdRecallForget(
3066
3091
  return 0;
3067
3092
  }
3068
3093
 
3094
+
3095
+ /**
3096
+ * Run `fn` with anything written to STDOUT by code we do not own routed to STDERR instead.
3097
+ *
3098
+ * Used to keep `--json` output parseable: a dependency that greets stdout on first load (currently
3099
+ * transformers.js) would otherwise sit in front of the JSON array. Nothing is swallowed — the text
3100
+ * still reaches the terminal, on the stream diagnostics belong on. Restoration is in `finally`, so a
3101
+ * throwing `fn` cannot leave stdout redirected.
3102
+ */
3103
+ export async function withForeignStdoutOnStderr<T>(fn: () => Promise<T>): Promise<T> {
3104
+ // Re-entrant: a nested call must not restore stdout when the INNER scope ends, or the outer scope
3105
+ // silently loses its guard. Depth-counted, and only the outermost exit restores (found by
3106
+ // independent review). Backpressure is not proxied — every writer here emits short diagnostic
3107
+ // lines, and returning stderr's own boolean is closer to the truth than inventing one.
3108
+ // Counted for EVERY caller, nested or concurrent. The first version only incremented when it
3109
+ // installed the patch, so an overlapping call that arrived second was not counted — and when the
3110
+ // FIRST finished it restored stdout while the second was still running, leaking exactly what the
3111
+ // guard exists to catch (found by cross-family review). The original `write` is captured once, by
3112
+ // the caller that installs the patch, and restored by the last one to leave.
3113
+ if (stdoutRedirectDepth === 0) {
3114
+ originalStdoutWrite = process.stdout.write;
3115
+ process.stdout.write = ((chunk: string | Uint8Array, ...rest: unknown[]): boolean =>
3116
+ (process.stderr.write as unknown as (c: string | Uint8Array, ...r: unknown[]) => boolean)(chunk, ...rest)) as typeof process.stdout.write;
3117
+ }
3118
+ stdoutRedirectDepth += 1;
3119
+ try {
3120
+ return await fn();
3121
+ } finally {
3122
+ stdoutRedirectDepth -= 1;
3123
+ // the EXACT original function, not a fresh binding of it
3124
+ if (stdoutRedirectDepth === 0 && originalStdoutWrite !== undefined) {
3125
+ process.stdout.write = originalStdoutWrite;
3126
+ originalStdoutWrite = undefined;
3127
+ }
3128
+ }
3129
+ }
3130
+
3131
+ let stdoutRedirectDepth = 0;
3132
+ let originalStdoutWrite: typeof process.stdout.write | undefined;
3069
3133
  /**
3070
3134
  * `dz recall --promote <dzId>[,<dzId>…] [--apply]` — lift quarantine from NAMED records
3071
3135
  * (lesson-quarantine FR-6b). Dry-run by default, the --forget symmetry. Also clears the
@@ -3212,10 +3276,13 @@ async function cmdRecall(options: Map<string, string>, flags: Set<string>, cwd:
3212
3276
  }
3213
3277
 
3214
3278
  if (flags.has('books')) {
3215
- const { hits, error } = await queryBookKnowledge(projectRoot, query, {
3279
+ // Same guard as the hybrid path: book knowledge is a vector search, so it loads the same
3280
+ // embedder and greeted stdout ahead of the JSON array (found by independent review).
3281
+ const runBooks = (): ReturnType<typeof queryBookKnowledge> => queryBookKnowledge(projectRoot, query, {
3216
3282
  limit,
3217
3283
  ...(bookFilter !== undefined ? { book: bookFilter } : {}),
3218
3284
  });
3285
+ const { hits, error } = asJson ? await withForeignStdoutOnStderr(runBooks) : await runBooks();
3219
3286
  if (asJson) { write(JSON.stringify(hits)); return 0; }
3220
3287
  write(`dz recall "${query}" --books${bookFilter !== undefined ? ` --book ${bookFilter}` : ''} — ${hits.length} KU hit(s)`);
3221
3288
  if (error !== undefined) write(` (${error})`);
@@ -3223,6 +3290,34 @@ async function cmdRecall(options: Map<string, string>, flags: Set<string>, cwd:
3223
3290
  const src = h.chapter !== undefined ? ` [${h.book} гл.${h.chapter}${h.pages ? ` с.${h.pages[0]}-${h.pages[1]}` : ''}]` : ` [${h.book}]`;
3224
3291
  write(` (${h.type}) ${h.name}${src}`);
3225
3292
  }
3293
+ // A zero-hit search must say WHERE it looked, and — only then — whether the other shelf has
3294
+ // anything. `--books` reads THIS PROJECT's store; digitised books are promoted to a machine-wide
3295
+ // brain. MEASURED: the same query gives 3 hits in this repository, 0 in any other directory, and
3296
+ // 2 through `dz brain query` from that same other directory — with no sign that the knowledge
3297
+ // was one command away (features/books-names-the-brain).
3298
+ // A FAILED search is not an empty one. With `error` set, `hits` is empty because the store could
3299
+ // not be read — claiming it was searched, and pointing elsewhere, would turn a fault into a
3300
+ // "nothing here" (found by cross-family review; the error itself is already printed above).
3301
+ if (hits.length === 0 && error === undefined) {
3302
+ write(` searched this project's book store: ${bookKbPath(projectRoot)}`);
3303
+ // Read the brain ONLY here: the happy path must not pay for the empty one. A brain that
3304
+ // cannot be read says NOTHING — an unreadable shelf is not an empty shelf.
3305
+ let sources: { slug: string }[] | undefined;
3306
+ try {
3307
+ sources = listBrain();
3308
+ } catch {
3309
+ sources = undefined;
3310
+ }
3311
+ if (sources !== undefined && sources.length > 0) {
3312
+ // Deliberately NOT asserting that `--book <slug>` exists in the brain — nothing here checked
3313
+ // that, and advising a filter that will also miss is the same defect wearing a hat.
3314
+ // POSIX single-quoting: the query is USER text and lands in a command the reader will paste.
3315
+ // Interpolating it into double quotes breaks on a `"` and invites `$(…)`/backticks to be
3316
+ // read by their shell (found by cross-family review).
3317
+ const quoted = `'${query.replace(/'/g, `'\\''`)}'`;
3318
+ write(` the machine-wide brain holds ${sources.length} source(s) — search it with: dz brain query ${quoted}`);
3319
+ }
3320
+ }
3226
3321
  return 0;
3227
3322
  }
3228
3323
 
@@ -3250,11 +3345,25 @@ async function cmdRecall(options: Map<string, string>, flags: Set<string>, cwd:
3250
3345
  const shownQuery = oneLine(query);
3251
3346
  const shownDomain = wantedDomain === undefined ? undefined : oneLine(wantedDomain);
3252
3347
  const fetchLimit = wantedDomain !== undefined ? Math.min(limit * 3, limit + 20) : limit;
3253
- const result = await recallHybrid(projectRoot, query, { limit: fetchLimit, mode });
3348
+ // `--json` promises MACHINE-READABLE stdout, and transformers.js writes `Transformers.js loaded:
3349
+ // <model>` straight to stdout when the embedding model loads — so the machine mode was unparseable
3350
+ // in exactly the mode that makes it machine-readable (MEASURED 2026-08-22: it broke this project's
3351
+ // own measurement script and produced a false result). The same noise already forced
3352
+ // `.claude/helpers/agentdb-mcp-shim.mjs` to exist for the MCP stdio channel; this is that class,
3353
+ // second occurrence. Foreign stdout is routed to stderr for the duration of the engine call — our
3354
+ // own output is written after it returns.
3355
+ const result = asJson
3356
+ ? await withForeignStdoutOnStderr(() => recallHybrid(projectRoot, query, { limit: fetchLimit, mode }))
3357
+ : await recallHybrid(projectRoot, query, { limit: fetchLimit, mode });
3254
3358
 
3255
3359
  if (mode === 'semantic' && result.vectorEngine === 'none') {
3256
3360
  // --semantic is an explicit ask — degrading it silently would be dishonest (FR-3).
3257
- write(`dz recall --semantic: ${result.vectorReason ?? 'no vector engine available — run: dz setup --memory agentdb'}`);
3361
+ const why = result.vectorReason ?? 'no vector engine available — run: dz setup --memory agentdb';
3362
+ // …and under --json the refusal must itself be JSON. This branch wrote PROSE to stdout, so the
3363
+ // one mode that promises machine-readable output broke exactly where the feature is loudest
3364
+ // (found by cross-family review; MEASURED: `--semantic --json` with no engine printed a
3365
+ // sentence). An error the caller cannot parse is not an honest refusal, only a different lie.
3366
+ write(asJson ? JSON.stringify({ error: 'semantic-unavailable', reason: why, hits: [] }) : `dz recall --semantic: ${why}`);
3258
3367
  return 1;
3259
3368
  }
3260
3369
  // Domain-aware re-ranking (health-advisor slice H): `--domain <name>` lifts lessons
@@ -3262,6 +3371,15 @@ async function cmdRecall(options: Map<string, string>, flags: Set<string>, cwd:
3262
3371
  // shared store keeps the cross-domain transfers that make it worth more than two stores.
3263
3372
  const boost = wantedDomain !== undefined ? applyDomainBoost(result.hits, wantedDomain) : null;
3264
3373
  const hits = (boost ? boost.hits : result.hits).slice(0, limit);
3374
+ // Computed ONCE, honoured by EVERY return path. It used to live only on the text tail, so the two
3375
+ // paths that return earlier — `--json` and the zero-hits branch — still reported success. That
3376
+ // made the contract change invisible to exactly the caller the ADR justifies it by: a script
3377
+ // (MEASURED: text mode exited 1, `--json` exited 0 on the same query).
3378
+ // `vectorError` is EXCLUDED on purpose: an engine that was asked and failed/timed out is the
3379
+ // documented degraded path (exit 0, 05 §2.3) and stays that way. This code is for a tier that had
3380
+ // nothing to give, not for one that broke — conflating them would make the exit status depend on a
3381
+ // timeout and so vary run to run (found by independent review).
3382
+ const semanticUnserved = mode === 'semantic' && result.vectorError === undefined && result.semanticRanked === 0;
3265
3383
  // The boost never drops a hit, but the CUT still can: promoting a match into the top
3266
3384
  // `limit` pushes the last one out, so a lesson visible WITHOUT --domain can vanish
3267
3385
  // WITH it. Cross-model review called this out as a lie by omission — the note said
@@ -3271,7 +3389,19 @@ async function cmdRecall(options: Map<string, string>, flags: Set<string>, cwd:
3271
3389
  if (asJson) {
3272
3390
  // Portable contract UNCHANGED (I-7/AC-6): a plain PatternRecord[] — round-trips through
3273
3391
  // `dz teach --from-json` regardless of which backend ranked each hit.
3274
- write(JSON.stringify(hits.map((h) => h.pattern)));
3392
+ // The RRF relevance that ranked these very records was computed and then dropped, so no
3393
+ // automated consumer could threshold on it (MEASURED: keys were exactly
3394
+ // pattern,type,reward,domain,ts,source). It rides as a COMPANION key: `dz teach --from-json`
3395
+ // ignores unknown keys, so the round-trip is preserved — PROVEN by running, not assumed.
3396
+ // `relevance` is null under `--domain`: the domain boost REORDERS the list, so the RRF score no
3397
+ // longer explains the order shown, and printing it beside a boosted ranking would be a number
3398
+ // that contradicts its own list. Null means "not applicable here", never "zero relevance".
3399
+ // The condition is the BOOST, not `'score' in h`: boosted hits carry a score too, so the first
3400
+ // version emitted the number while its own comment promised null (found by independent review).
3401
+ write(JSON.stringify(hits.map((h) => ({
3402
+ ...h.pattern,
3403
+ relevance: boost === null && 'score' in h && typeof h.score === 'number' ? h.score : null,
3404
+ }))));
3275
3405
  // The honesty notes go to STDERR here rather than being skipped: the JSON branch
3276
3406
  // used to return before them, so a scripted caller was told nothing about a boost
3277
3407
  // that had promoted a match and pushed a visible hit past the --limit cut.
@@ -3280,7 +3410,7 @@ async function cmdRecall(options: Map<string, string>, flags: Set<string>, cwd:
3280
3410
  const cutNoteJson = renderDomainCutNote(displaced, limit);
3281
3411
  if (cutNoteJson !== '') process.stderr.write(`${cutNoteJson}\n`);
3282
3412
  }
3283
- return 0;
3413
+ return semanticUnserved ? 1 : 0;
3284
3414
  }
3285
3415
 
3286
3416
  if (hits.length === 0) {
@@ -3290,11 +3420,29 @@ async function cmdRecall(options: Map<string, string>, flags: Set<string>, cwd:
3290
3420
  // silently said nothing about the domain, so the reader could not tell whether the
3291
3421
  // boost had been applied and found nothing, or had not run at all.
3292
3422
  if (boost !== null && shownDomain !== undefined) write(renderDomainBoostNote(boost, shownDomain));
3293
- return 0;
3294
- }
3295
- const vectorOn = result.vectorEngine !== 'none' && result.vectorError === undefined && mode !== 'lexical';
3423
+ return semanticUnserved ? 1 : 0;
3424
+ }
3425
+ // `vectorOn` used to mean "an engine RESOLVED", so the header claimed vector ranking over a store
3426
+ // with zero vectors while each hit's own label honestly read ⟨sqlite⟩ (MEASURED 2026-08-22).
3427
+ // It now means what it says: a vector actually ranked something (ADR-001).
3428
+ const vectorOn = result.semanticRanked > 0 && result.vectorError === undefined && mode !== 'lexical';
3429
+ const engineUp = result.vectorEngine !== 'none' && result.vectorError === undefined;
3430
+ // Whether a tier EXISTS, regardless of whether this query's search succeeded. The advice below
3431
+ // must key on existence: gated on `engineUp`, a timed-out but installed tier was told to install
3432
+ // itself, one line under "vector search degraded" (found by independent review).
3433
+ const engineInstalled = result.vectorEngine !== 'none';
3296
3434
  const lexLabel = result.lexicalBackend === 'sqlite' ? 'SQLite FTS5' : 'keyword (JSON)';
3297
- const ranking = vectorOn ? `${lexLabel} + vector (${result.vectorEngine}) ranking` : `${lexLabel} ranking (lexical)`;
3435
+ const ranking = vectorOn
3436
+ ? `${lexLabel} + vector (${result.vectorEngine}) ranking`
3437
+ : engineUp && mode !== 'lexical'
3438
+ // the engine is up and returned nothing usable — name the state and the fix, do not claim a
3439
+ // ranking that did not happen and do not advise installing what is already installed
3440
+ // `semanticCandidates` earns its place here: an engine that returned candidates which were ALL
3441
+ // orphans is a different problem from an engine with nothing in it, and the fix differs too.
3442
+ ? result.semanticCandidates > 0
3443
+ ? `${lexLabel} only (the semantic tier returned ${result.semanticCandidates} stale id(s) — run: dz consolidate)`
3444
+ : `${lexLabel} only (semantic tier empty — run: dz vector reindex)`
3445
+ : `${lexLabel} ranking (lexical)`;
3298
3446
  write(`dz recall "${shownQuery}" — ${hits.length} hit(s), ${ranking}`);
3299
3447
  let sawQuarantined = false;
3300
3448
  for (const h of hits) {
@@ -3331,10 +3479,20 @@ async function cmdRecall(options: Map<string, string>, flags: Set<string>, cwd:
3331
3479
  // FR-8 hint swap: with an engine the old MCP-only hint is gone; without one, the SAME
3332
3480
  // conditional position carries an actionable enablement line instead — the only permitted
3333
3481
  // output change on the degraded path.
3334
- if (!vectorOn && result.vectorError === undefined && existsSync(join(projectRoot, '.dz', 'agentdb.db'))) {
3482
+ // ... and NOT when the tier is already installed: in the repo (539 vectors, a 2.9 MB agentdb.db)
3483
+ // `--no-semantic` advised installing the tier it was deliberately not using (MEASURED 2026-08-22).
3484
+ // ... and NOT when the user explicitly asked for lexical recall. Under `--no-semantic` the result
3485
+ // reports `vectorEngine: 'none'` BY CONSTRUCTION, so this line fired over a tier that was installed
3486
+ // and full — in this repo, 539 vectors and a 2.9 MB agentdb.db, advising the user to install it
3487
+ // (MEASURED 2026-08-22). Someone who passed --no-semantic has opted out; advice is noise there.
3488
+ if (!vectorOn && !engineInstalled && mode !== 'lexical' && existsSync(join(projectRoot, '.dz', 'agentdb.db'))) {
3335
3489
  write(` ℹ semantic (vector) recall needs the agentdb vector tier — run: dz setup --memory agentdb`);
3336
3490
  }
3337
- return 0;
3491
+ // An EXPLICIT --semantic that no vector could serve is not a success. The caller who most needs to
3492
+ // know is the one that cannot read the prose above it (ADR-001; this is a contract change).
3493
+ // Exit 1, the SAME code the documented sibling case uses ("--semantic … exit 1 if no engine"):
3494
+ // an explicit ask that could not be served is one failure class, not two.
3495
+ return semanticUnserved ? 1 : 0;
3338
3496
  }
3339
3497
 
3340
3498
  /* ------------------------------------------------------------------ */
@@ -3419,11 +3577,36 @@ async function cmdVector(options: Map<string, string>, flags: Set<string>, cwd:
3419
3577
  write(` Mode: ${st.mode} (.dz/config.json → memory.vector.engine)`);
3420
3578
  if (st.embeddingModel !== undefined) write(` Embedding model: ${st.embeddingModel}`);
3421
3579
  write(` Lexical patterns: ${st.lexicalMirrorable} mirrorable (${st.lexicalTotal} total)`);
3422
- write(` Mirrored vectors: ${st.mirrored !== undefined ? st.mirrored : 'n/a (no engine)'}`);
3580
+ // Each line NAMES its scope. `Mirrored vectors` used to count three task types and sit directly
3581
+ // under a one-task-type lexical count, and a reader took the pair at face value: 547 vs 274 read
3582
+ // as half the index orphaned, and a task was filed to prune it. MEASURED: 273 of those were
3583
+ // `dz-backlog` idea ids and there were ZERO orphans (ADR-001, features/mirror-counts-comparable).
3584
+ write(` Mirrored vectors (learned patterns): ${st.mirrored !== undefined ? st.mirrored : 'n/a (no engine)'}`);
3585
+ if (st.mirroredOther !== undefined && st.mirroredOther > 0) {
3586
+ write(` Other dz-owned vectors (backlog ideas): ${st.mirroredOther} — counted separately, not part of the pair above`);
3587
+ }
3588
+ if (st.orphaned !== undefined && st.orphaned > 0) {
3589
+ write(` Orphan vectors (no lexical record): ${st.orphaned} — run: dz vector reindex`);
3590
+ }
3423
3591
  write(` Pending mirror queue: ${st.pending}`);
3424
- if (st.available && st.mirrored !== undefined && st.mirrored < st.lexicalMirrorable) {
3592
+ // `pending: 0` used to stand alone, and it reads as "no debt" when it actually means "no queue
3593
+ // was ever opened" — an unconfigured project printed the same line as a fully-mirrored store
3594
+ // (MEASURED: two projects differing by one config file, 0 vs 1 for the same record).
3595
+ write(` Mirror writer: ${st.mirrorWriterEnabled ? 'ON' : 'OFF (.dz/config.json has no memory.backend=agentdb — teach is NOT queueing)'}`);
3596
+ // "not in the mirror" is ALL the set difference proves — a vector written and later deleted is
3597
+ // indistinguishable from one never offered, so the label must not claim "never queued".
3598
+ // `undefined` has two causes and they are different advice, so they are printed differently.
3599
+ const unknownReason = st.available ? 'unknown (the engine failed to list its ids)' : 'unknown (no engine to ask)';
3600
+ write(` Not in the mirror: ${st.unmirrored !== undefined ? st.unmirrored : unknownReason}`);
3601
+ // The old advice compared `mirrored < lexicalMirrorable`, two counts of different things — so
3602
+ // backlog ideas inflating `mirrored` could SILENCE it while patterns really were missing.
3603
+ // `unmirrored` is a set difference over ids and answers the same question correctly.
3604
+ if (st.unmirrored !== undefined && st.unmirrored > 0) {
3425
3605
  write(` ℹ mirror behind the lexical store — run: dz consolidate (backfill)`);
3426
3606
  }
3607
+ if (st.unmirrored !== undefined && st.unmirrored > 0) {
3608
+ write(` ℹ ${st.unmirrored} mirrorable pattern(s) are not in the vector mirror — run: dz vector reindex`);
3609
+ }
3427
3610
  return 0;
3428
3611
  }
3429
3612
 
@@ -4548,7 +4731,10 @@ function verifyInstalledPacks(cwd: string, explicitPubkey?: string | undefined):
4548
4731
  packaged: packagedTrustRootPath(),
4549
4732
  });
4550
4733
 
4551
- const packs = discoverSkillPackDirs(cwd);
4734
+ // ADR-001: verification asks "which packs carry a signature?", which is NOT the question
4735
+ // `discoverSkillPackDirs` answers. MEASURED 2026-08-21 — the prefix filter left 26 of 52 signed
4736
+ // packs invisible, `keysarium` drifted unnoticed, and the summary line read as coverage.
4737
+ const packs = discoverVerifiablePackDirs(cwd);
4552
4738
 
4553
4739
  // Cross-model review: `--pubkey <pack>/evil.pub` would let the artifact supply its own verifying key
4554
4740
  // through the caller. The tool must never verify a pack against a key that lives inside it.
@@ -4587,6 +4773,32 @@ function verifyInstalledPacks(cwd: string, explicitPubkey?: string | undefined):
4587
4773
  checks.push({ pack, verdict: 'no-trust-root', failures: [] });
4588
4774
  continue;
4589
4775
  }
4776
+ // A SOURCE tree legitimately holds files the tarball never ships (tests, coverage, CHANGELOG), so
4777
+ // the added-file sweep is meaningless there — scoping it to the manifest's own list disables it.
4778
+ // An INSTALLED pack under node_modules IS the extracted artifact, and there the sweep is the whole
4779
+ // point: it is what catches a file an attacker added. Same function, two honest modes.
4780
+ //
4781
+ // This distinction had to be drawn the moment the SIGNER started covering only the shipped set
4782
+ // (2026-08-21). Leaving it undrawn made every source pack report TAMPERED — the third time in one
4783
+ // day that a scope change on one side was not mirrored on the other.
4784
+ // An ARTIFACT is an extracted tarball, and extracted tarballs live in `node_modules`. Anything
4785
+ // else is a checkout. Deliberately NOT keyed on `cwd`: packs are also discovered from the CLI's
4786
+ // own install location, which is outside the project being checked — keying on cwd made the
4787
+ // repo's own source packs look like artifacts and report TAMPERED from a temp-dir fixture.
4788
+ // Resolve the link FIRST: pnpm links workspace packages into `node_modules`, so a source checkout
4789
+ // is reachable by a path that looks like an artifact. Judging by the given path made the repo's
4790
+ // own packs verify as tarballs and report TAMPERED (measured while wiring this).
4791
+ let realDir = dir;
4792
+ try { realDir = realpathSync(dir); } catch { /* keep the given path */ }
4793
+ const isSourceTree = !realDir.split(sep).includes('node_modules');
4794
+ if (isSourceTree) {
4795
+ // The manifest describes the PUBLISHED TARBALL, and a source checkout is a different object —
4796
+ // `pnpm publish` re-serialises package.json and rewrites `workspace:*`. Hash-verifying a
4797
+ // checkout against it produces a guaranteed false TAMPERED, so this reports a state of its own
4798
+ // instead of an alarm. `dz verify-pack` packs and checks the real artifact.
4799
+ checks.push({ pack, verdict: 'source-tree', failures: [] });
4800
+ continue;
4801
+ }
4590
4802
  const res = verifyManifest(dir, signed as never, keyPem);
4591
4803
  checks.push({
4592
4804
  pack,
@@ -4597,6 +4809,36 @@ function verifyInstalledPacks(cwd: string, explicitPubkey?: string | undefined):
4597
4809
  return { trustRoot, checks };
4598
4810
  }
4599
4811
 
4812
+ /**
4813
+ * The signature verdicts as DATA. The text reporter and the `--json` output both render this, so the
4814
+ * two cannot disagree about what was found — the failure this feature removes is exactly a verdict
4815
+ * that exists in one surface and not the other.
4816
+ */
4817
+ function collectPackVerification(
4818
+ cwd: string,
4819
+ explicitPubkey: string | undefined,
4820
+ ): {
4821
+ trustRoot: { source: string; path: string } | null;
4822
+ counts: Record<PackVerdict, number>;
4823
+ packs: { pack: string; verdict: PackVerdict; failures: { path: string; reason: string }[] }[];
4824
+ } {
4825
+ let trustRoot: ReturnType<typeof resolveTrustRoot> = null;
4826
+ let checks: PackCheck[] = [];
4827
+ try {
4828
+ ({ trustRoot, checks } = verifyInstalledPacks(cwd, explicitPubkey));
4829
+ } catch {
4830
+ // A refusal is a result: an empty listing with no trust root, not a crash and not a silent pass.
4831
+ return { trustRoot: null, counts: { verified: 0, unsigned: 0, tampered: 0, 'no-trust-root': 0, 'source-tree': 0 }, packs: [] };
4832
+ }
4833
+ const counts: Record<PackVerdict, number> = { verified: 0, unsigned: 0, tampered: 0, 'no-trust-root': 0, 'source-tree': 0 };
4834
+ for (const c of checks) counts[c.verdict]++;
4835
+ return {
4836
+ trustRoot: trustRoot === null ? null : { source: trustRoot.source, path: trustRoot.path },
4837
+ counts,
4838
+ packs: checks.map((c) => ({ pack: c.pack, verdict: c.verdict, failures: [...c.failures] })),
4839
+ };
4840
+ }
4841
+
4600
4842
  /** Print the pack verdicts and return 1 iff the policy says any of them is fatal. */
4601
4843
  function reportPackVerification(
4602
4844
  cwd: string,
@@ -4615,7 +4857,7 @@ function reportPackVerification(
4615
4857
  }
4616
4858
  if (checks.length === 0) return 0;
4617
4859
 
4618
- const counts = { verified: 0, unsigned: 0, tampered: 0, 'no-trust-root': 0 } as Record<PackVerdict, number>;
4860
+ const counts = { verified: 0, unsigned: 0, tampered: 0, 'no-trust-root': 0, 'source-tree': 0 } as Record<PackVerdict, number>;
4619
4861
  let fatal = 0;
4620
4862
  for (const c of checks) {
4621
4863
  counts[c.verdict]++;
@@ -4631,7 +4873,7 @@ function reportPackVerification(
4631
4873
  const root = trustRoot ? `${trustRoot.source} (${trustRoot.path})` : 'none';
4632
4874
  write(
4633
4875
  ` signatures: ${counts.verified} verified, ${counts.unsigned} unsigned, ` +
4634
- `${counts.tampered} TAMPERED, ${counts['no-trust-root']} unverifiable; trust root: ${root}`,
4876
+ `${counts.tampered} TAMPERED, ${counts['no-trust-root']} unverifiable, ${counts['source-tree']} source-tree (not an artifact); trust root: ${root}`,
4635
4877
  );
4636
4878
  // A signature proves the bytes are unmodified. It never proves the skill is any good.
4637
4879
  return fatal > 0 ? 1 : 0;
@@ -4719,13 +4961,40 @@ function cmdSign(options: Map<string, string>, flags: Set<string>, cwd: string,
4719
4961
  }
4720
4962
  if (!existsSync(resolve(cwd, key))) { write(`dz sign: private key not found: ${resolve(cwd, key)}`); return 1; }
4721
4963
 
4722
- const files = packFiles(packDir);
4723
- if (files.length === 0) { write('dz sign: the pack contains no files refusing to sign nothing'); return 1; }
4964
+ // The manifest covers what the CONSUMER receives. `dz sign` and the publish-time re-sign MUST use
4965
+ // the same rule, or the two produce different manifests for the same pack a second, divergent
4966
+ // answer beside the real one, which is the class of defect this change removes.
4967
+ // Hash the EXTRACTED TARBALL, not the working tree. `pnpm publish` re-serialises package.json and
4968
+ // rewrites `workspace:*`, so a hash taken from disk is stale before the tarball exists — MEASURED
4969
+ // 2026-08-21: the published `skills-news` package.json is 1050 bytes where the tree's is 1051, and
4970
+ // that single missing newline made six freshly re-signed packs report TAMPERED to every consumer.
4971
+ // `dz sign` and the publish-time re-sign use this same path, or they produce different manifests for
4972
+ // the same pack.
4973
+ let hashRoot = packDir;
4974
+ let cleanupPack: (() => void) | null = null;
4975
+ let files: string[] = packFiles(packDir);
4976
+ try {
4977
+ const extracted = extractPublishTarball(packDir);
4978
+ hashRoot = extracted.dir;
4979
+ cleanupPack = extracted.cleanup;
4980
+ files = packFiles(hashRoot);
4981
+ write(`dz sign: hashing the packed tarball (${files.length} file(s)) — the bytes a recipient receives`);
4982
+ } catch (err) {
4983
+ // Not an npm package, or no pnpm: sign the tree and SAY SO. A silent fallback would restore the
4984
+ // divergence this change closes.
4985
+ write(`dz sign: could not pack this directory (${(err as Error).message.split('\n')[0]}) — signing the working tree instead`);
4986
+ }
4987
+ if (files.length === 0) {
4988
+ cleanupPack?.();
4989
+ write('dz sign: the pack contains no files — refusing to sign nothing');
4990
+ return 1;
4991
+ }
4724
4992
 
4725
- const manifest = buildManifest(packDir, basename(packDir), files);
4993
+ const manifest = buildManifest(hashRoot, basename(packDir), files);
4726
4994
  const signed = signManifest(manifest, readFileSync(resolve(cwd, key), 'utf8'));
4727
4995
  writeFileSync(join(packDir, MANIFEST_NAME), JSON.stringify(signed, null, 2) + '\n');
4728
4996
  writeFileSync(join(packDir, SBOM_NAME), JSON.stringify(buildSbom(manifest), null, 2) + '\n');
4997
+ cleanupPack?.();
4729
4998
  write(`dz sign: signed ${files.length} file(s) in ${packDir}`);
4730
4999
  write(` ${MANIFEST_NAME} + ${SBOM_NAME} written. Ed25519 gives tamper-evidence, never truthfulness.`);
4731
5000
  return 0;
@@ -4749,7 +5018,11 @@ function cmdVerifyPack(options: Map<string, string>, flags: Set<string>, cwd: st
4749
5018
  try { signed = JSON.parse(readFileSync(manifestPath, 'utf8')); }
4750
5019
  catch { write(`dz verify-pack: ${MANIFEST_NAME} is not valid JSON`); return 1; }
4751
5020
 
4752
- const res = verifyManifest(packDir, signed as never, readFileSync(pubPath, 'utf8'));
5021
+ // Same rule as the signer: a working-tree file `files[]` excludes was never "added to the pack".
5022
+ // Unscoped when npm cannot answer — an extracted tarball verifies exactly as before.
5023
+ let shippedForVerify: string[] | undefined;
5024
+ try { shippedForVerify = npmPackedPaths(packDir); } catch { shippedForVerify = undefined; }
5025
+ const res = verifyManifest(packDir, signed as never, readFileSync(pubPath, 'utf8'), shippedForVerify);
4753
5026
  if (res.ok) { write(`dz verify-pack: OK — ${packDir} matches its signed manifest`); return 0; }
4754
5027
  write(`dz verify-pack: FAILED — ${packDir}`);
4755
5028
  for (const f of res.failures) write(` ${f.path}: ${f.reason}`);
@@ -4791,7 +5064,7 @@ function cmdPublish(options: Map<string, string>, flags: Set<string>, cwd: strin
4791
5064
  // Reject unknown flags/options so a typo (e.g. `--dry-rum`) can NEVER be
4792
5065
  // silently swallowed and flip the command into live-publish mode.
4793
5066
  const allowedFlags = new Set(['dry-run', 'no-dry-run', 'yes', 'confirm', 'bump-only', 'help', 'require-signing', 'provenance', 'no-provenance']);
4794
- const allowedOptions = new Set(['filter', 'claim-check', 'no-guard']);
5067
+ const allowedOptions = new Set(['filter', 'claim-check', 'no-guard', 'sign-key']);
4795
5068
  const allowedHelp = ' allowed: --dry-run (default), --yes/--confirm/--no-dry-run (go live), --bump-only, --filter <substr>, --claim-check <off|warn|error>, --no-guard "<reason>" (skip the guard pre-flight; logged)';
4796
5069
  for (const flag of flags) {
4797
5070
  if (!allowedFlags.has(flag)) {
@@ -4922,7 +5195,9 @@ function cmdPublish(options: Map<string, string>, flags: Set<string>, cwd: strin
4922
5195
  if (trustRootPresent && manifestPresent) {
4923
5196
  try {
4924
5197
  const signed = JSON.parse(readFileSync(manifestPath, 'utf8'));
4925
- verifyOk = verifyManifest(pk.dir, signed, readFileSync(trustRoot, 'utf8')).ok;
5198
+ let shippedForGate: string[] | undefined;
5199
+ try { shippedForGate = npmPackedPaths(pk.dir); } catch { shippedForGate = undefined; }
5200
+ verifyOk = verifyManifest(pk.dir, signed, readFileSync(trustRoot, 'utf8'), shippedForGate).ok;
4926
5201
  } catch {
4927
5202
  verifyOk = false;
4928
5203
  }
@@ -4941,7 +5216,75 @@ function cmdPublish(options: Map<string, string>, flags: Set<string>, cwd: strin
4941
5216
  }
4942
5217
  }
4943
5218
 
4944
- const report = publishPackages(cwd, { provenance, dryRun, filter, bumpOnly, claimGate: claimCheckOpt });
5219
+ // A signed pack must be RE-SIGNED after publish's own bump and README sync (feature
5220
+ // `sign-after-bump`): publish mutates the pack, so any earlier signature describes files that no
5221
+ // longer exist. Default to the same path `dz sign --init` writes, so the ordinary operator needs no
5222
+ // new flag; `--sign-key` overrides it.
5223
+ const signKey = (options.get('sign-key') ?? join(homedir(), '.dz', 'keys', 'dz.key')).trim();
5224
+ const report = publishPackages(cwd, {
5225
+ provenance,
5226
+ dryRun,
5227
+ filter,
5228
+ bumpOnly,
5229
+ claimGate: claimCheckOpt,
5230
+ signKey: signKey === '' ? undefined : resolve(cwd, signKey),
5231
+ verifyAfterSign: (packDir: string): { ok: boolean; trustRootPresent: boolean; pack?: string } => {
5232
+ // Verify the OUTCOME against the trust root a CONSUMER would use — an existing key may be the
5233
+ // WRONG key, and enumerating that state is a losing game (round-1 review). The pack NAME travels
5234
+ // with the verdict so a pass about a different artifact cannot be mistaken for this one.
5235
+ const trustRoot = resolve(cwd, TRUST_ROOT_REL);
5236
+ if (!existsSync(trustRoot)) return { ok: false, trustRootPresent: false, pack: basename(packDir) };
5237
+ try {
5238
+ const signed = JSON.parse(readFileSync(join(packDir, MANIFEST_NAME), 'utf8')) as never;
5239
+ let shipped: string[] | undefined;
5240
+ try { shipped = npmPackedPaths(packDir); } catch { shipped = undefined; }
5241
+ const res = verifyManifest(packDir, signed, readFileSync(trustRoot, 'utf8'), shipped);
5242
+ return { ok: res.ok, trustRootPresent: true, pack: basename(packDir) };
5243
+ } catch {
5244
+ return { ok: false, trustRootPresent: true, pack: basename(packDir) };
5245
+ }
5246
+ },
5247
+ reSign: (packDir: string, keyPath: string): void => {
5248
+ // The same three steps `dz sign` performs, including the SBOM — a manifest refreshed without
5249
+ // its SBOM would leave the two describing different trees.
5250
+ //
5251
+ // Path A: the manifest must cover what the CONSUMER receives, not what the author has on disk.
5252
+ // MEASURED 2026-08-21 by a live install of the published 0.6.1 — with the trust root restored,
5253
+ // six packs reported TAMPERED, and only half of that was the version bump. The rest was
5254
+ // `CHANGELOG.md: listed in the manifest but absent`: signed on disk, excluded by `files[]`,
5255
+ // therefore missing for every recipient forever. So the file list comes from `npm pack`, which
5256
+ // is the authority on what ships — we do not reimplement its globbing.
5257
+ // Hash the EXTRACTED TARBALL, not the working tree. `pnpm publish` re-serialises package.json and
5258
+ // rewrites `workspace:*`, so a hash taken from disk is stale before the tarball exists — MEASURED
5259
+ // 2026-08-21: the published `skills-news` package.json is 1050 bytes where the tree's is 1051,
5260
+ // and that one missing newline is what made six freshly re-signed packs report TAMPERED.
5261
+ const { dir: shippedDir, cleanup } = extractPublishTarball(packDir);
5262
+ try {
5263
+ const onDisk = packFiles(shippedDir);
5264
+ const packed = packFiles(shippedDir);
5265
+ const setDecision = decideSignableSet({ signable: onDisk, packed });
5266
+ write(signableSetLine(basename(packDir), setDecision));
5267
+ if (setDecision.publishedButUnsigned.length > 0) {
5268
+ // A shipped file no signature covers is WORSE than an unsigned pack: the badge says verified
5269
+ // while part of the payload is unchecked. Refuse rather than sign a partial claim.
5270
+ throw new Error(
5271
+ `refusing to sign a pack with ${setDecision.publishedButUnsigned.length} SHIPPED BUT UNSIGNED file(s): ${setDecision.publishedButUnsigned.slice(0, 5).join(', ')}`,
5272
+ );
5273
+ }
5274
+ const files = [...setDecision.sign];
5275
+ if (files.length === 0) throw new Error(`refusing to sign an empty pack: ${packDir}`);
5276
+ // Hashes come from the extracted tarball; the manifest is WRITTEN to the source dir so the next
5277
+ // pack carries it. The re-pack normalises package.json identically (deterministic — MEASURED by
5278
+ // packing twice and comparing hashes), so the entries still describe what ships.
5279
+ const manifest = buildManifest(shippedDir, basename(packDir), files);
5280
+ const signed = signManifest(manifest, readFileSync(keyPath, 'utf-8'));
5281
+ writeFileSync(join(packDir, MANIFEST_NAME), `${JSON.stringify(signed, null, 2)}\n`);
5282
+ writeFileSync(join(packDir, SBOM_NAME), `${JSON.stringify(buildSbom(manifest), null, 2)}\n`);
5283
+ } finally {
5284
+ cleanup();
5285
+ }
5286
+ },
5287
+ });
4945
5288
 
4946
5289
  write(`\ndz publish${dryRun ? ' --dry-run' : ''}${bumpOnly ? ' --bump-only' : ''}${claimCheckOpt !== 'warn' ? ` --claim-check ${claimCheckOpt}` : ''}`);
4947
5290
  write(` Published: ${report.published} Skipped: ${report.skipped} Errors: ${report.errors}\n`);
@@ -6392,8 +6735,16 @@ function cmdDriftCheck(options: Map<string, string>, flags: Set<string>, cwd: st
6392
6735
  const r = sweepSkillDrift(root, { scope, allowlist });
6393
6736
 
6394
6737
  if (flags.has('json')) {
6395
- write(JSON.stringify({ ...r, scope, allowlist }));
6396
- return r.drifted.length > 0 ? 1 : 0;
6738
+ // The signature verdicts were printed in the TEXT output and absent from the JSON, so a CI job
6739
+ // parsing `--json` saw `drifted: 0` and concluded all was well while packs were TAMPERED. A gate
6740
+ // silent in the form CI reads is not a gate (MEASURED 2026-08-21: keys were duplicated, drifted,
6741
+ // allowlisted, scope, allowlist — and nothing else).
6742
+ const sig = collectPackVerification(root, options.get('pubkey'));
6743
+ const sigBlocking = sig.packs.filter(
6744
+ (c) => decideVerifyPolicy(c.verdict, flags.has('require-signing')).action === 'fail',
6745
+ ).length;
6746
+ write(JSON.stringify({ ...r, scope, allowlist, signatures: sig }));
6747
+ return r.drifted.length > 0 || sigBlocking > 0 ? 1 : 0;
6397
6748
  }
6398
6749
 
6399
6750
  write(`Skills duplicated across ≥2 ${scope === 'packages' ? 'package' : ''} locations: ${r.duplicated}`);
@@ -6447,7 +6798,7 @@ const DEFAULT_STORE_CAP = 5000;
6447
6798
  const MAX_STUB_SCAN_FILES = 400;
6448
6799
 
6449
6800
  /** Read the optional `.dz/guard.json` — `{ rules?: [...], storeCap?: number, stubWaivers?: [...] }`. Missing/broken ⇒ defaults. */
6450
- function loadGuardConfig(root: string): { rules?: unknown[]; storeCap?: number; stubWaivers?: unknown[] } {
6801
+ function loadGuardConfig(root: string): { rules?: unknown[]; storeCap?: number; stubWaivers?: unknown[]; reviewRound?: { minGrade?: unknown } } {
6451
6802
  const p = join(root, '.dz', 'guard.json');
6452
6803
  if (!existsSync(p)) return {};
6453
6804
  try {
@@ -6594,6 +6945,51 @@ function gatherGuardFacts(op: string, root: string, text: string | undefined, st
6594
6945
  facts['readmeFirst'] = [...perPack.entries()].map(([name, e]) => ({ name: '@dzhechkov/' + name, versionBumped: e.pkgJson, readmeChanged: e.readme }));
6595
6946
  } catch { /* not a git repo — rule skips */ }
6596
6947
 
6948
+ // review-round: the same WORKING-TREE diff, asked a different question — does a package that
6949
+ // bumps its version and changes SOURCE bring a GRADED QE report with it? Scoped to source so a
6950
+ // docs-only republish is never blocked (ADR-001, features/publish-needs-a-review). A throw here
6951
+ // leaves the whole fact undefined, and the rule then reports NOTHING: absence of a report is an
6952
+ // accusation, absence of facts is ignorance, and they must not render the same.
6953
+ try {
6954
+ const status = execSync('git status --porcelain -uall', { cwd: root, encoding: 'utf-8' });
6955
+ const changed = status.split('\n').map((l) => l.slice(3).trim()).filter(Boolean);
6956
+ const perPack = new Map<string, { versionBumped: boolean; sourceChanged: boolean }>();
6957
+ for (const rel of changed) {
6958
+ const m = rel.match(/^packages\/@dzhechkov\/([^/]+)\/(.+)$/);
6959
+ if (!m || !m[1] || !m[2]) continue;
6960
+ const e = perPack.get(m[1]) ?? { versionBumped: false, sourceChanged: false };
6961
+ if (m[2] === 'package.json') e.versionBumped = true;
6962
+ // SOURCE = what ships and can be wrong at runtime. Tests, docs and fixtures are excluded:
6963
+ // a test-only change still ships, but it is not the class the review gate is about, and
6964
+ // widening the scope is what makes a HARD gate get switched off.
6965
+ if (/^(src|lib|bin|skills)\//.test(m[2]) && !/\.(md|json|txt)$/.test(m[2])) e.sourceChanged = true;
6966
+ perPack.set(m[1], e);
6967
+ }
6968
+ const grades: { report: string; grade: string }[] = [];
6969
+ for (const rel of changed) {
6970
+ if (!/^features\/[^/]+\/08_qe_report\.md$/.test(rel)) continue;
6971
+ let text: string;
6972
+ try { text = readFileSync(join(root, rel), 'utf-8'); } catch { continue; }
6973
+ // The grade the report itself STATES — `grade C`, `GRADE: B`, `**grade: A**`. A report that
6974
+ // never states one is not evidence (AM-2), so nothing is pushed for it.
6975
+ const m = text.match(/\bgrade\s*:?\s*\**\s*([ABCDF])\b/i);
6976
+ if (m && m[1]) grades.push({ report: rel, grade: m[1] });
6977
+ }
6978
+ const cfgMin = loadGuardConfig(root).reviewRound?.minGrade;
6979
+ const minGrade = typeof cfgMin === 'string' ? cfgMin : undefined;
6980
+ facts['reviewRound'] = {
6981
+ packages: [...perPack.entries()].map(([name, e]) => ({ name: '@dzhechkov/' + name, ...e })),
6982
+ grades,
6983
+ gathered: true,
6984
+ ...(minGrade !== undefined ? { minGrade } : {}),
6985
+ };
6986
+ } catch {
6987
+ // TRIED and could not read the tree. Say so on the record rather than passing silently: a HARD
6988
+ // gate that is quiet about ungathered evidence cannot be told from one that checked (raised by
6989
+ // cross-family review). It still does not BLOCK — ignorance is not an accusation.
6990
+ facts['reviewRound'] = { packages: [], grades: [], gathered: false };
6991
+ }
6992
+
6597
6993
  // skills-registrable: every skill dir in a skill pack must carry a depth-1 SKILL.md, or it ships
6598
6994
  // registering nowhere (the health-advisor 1.2.0 class). Pure-toolkit packages yield nothing.
6599
6995
  try {
@@ -8958,6 +9354,260 @@ function scanOneReqeRoot(
8958
9354
  * It measures `--artifact` paths on disk itself. A caller cannot assert presence; it can only name
8959
9355
  * what must be there.
8960
9356
  */
9357
+ /**
9358
+ * `dz amendment-check` — the deterministic half of the Step-8 amendment gate (ADR-001).
9359
+ *
9360
+ * The gate used to be prompt text asking the QE agent to confirm every `AM-N` row names a real test.
9361
+ * That is layer 4 on the cost-of-detection ladder, and `features/qe-scoped-review` shipped with five
9362
+ * dangling ids and a plan recording `## Amendments: None`. This command owns I/O and the exit code;
9363
+ * `harness-core/src/amendment-trace.ts` owns the grammar and the rules.
9364
+ */
9365
+ /**
9366
+ * `dz feature-adr-record` — the witnessed writer for the run-cost ledger and training pairs
9367
+ * (ADR-001 … ADR-003). The subagent stops being a COURIER handed a shell pipeline and becomes a
9368
+ * CALLER handed arguments: this command owns the paths, the refusal, the append, the READ-BACK and
9369
+ * the exit code. A courier can neither refuse nor verify, which is how four workflow runs finished
9370
+ * with no cost row at all.
9371
+ */
9372
+ /**
9373
+ * The file list of the tarball `npm pack` would produce, `package/` prefix stripped. npm is the
9374
+ * authority on what `files[]` ships; reimplementing its globbing would put a second, divergent answer
9375
+ * next to the real one — which is the class of defect this whole change exists to remove.
9376
+ */
9377
+ function npmPackedPaths(packDir: string): string[] {
9378
+ // `pnpm`, not `npm`: the PUBLISHER is `pnpm publish` (see `publishArgv`), and the two packers do not
9379
+ // agree. MEASURED 2026-08-21 on `skills-news`: `npm pack` emits a 1051-byte package.json identical
9380
+ // to the working tree, `pnpm pack` emits 1050 — pnpm re-serialises it (dropping the trailing
9381
+ // newline, and expanding `workspace:*`). Asking one tool what ships while a different tool ships it
9382
+ // is how a signature ends up describing a file nobody receives.
9383
+ const out = execFileSync('pnpm', ['pack', '--pack-destination', mkdtempSync(join(tmpdir(), 'dz-pack-probe-')), '--json'], {
9384
+ cwd: packDir,
9385
+ encoding: 'utf-8',
9386
+ maxBuffer: 64 * 1024 * 1024,
9387
+ });
9388
+ const parsed = JSON.parse(out) as { files?: { path: string }[] } | { files?: { path: string }[] }[];
9389
+ const entry = Array.isArray(parsed) ? parsed[0] : parsed;
9390
+ const files = entry?.files ?? [];
9391
+ return files.map((f) => f.path.replace(/^package\//, '')).sort();
9392
+ }
9393
+
9394
+ /**
9395
+ * Pack the package with the SAME tool that publishes it, extract the tarball, and return the directory
9396
+ * holding its contents. Hashing THAT is the only way a manifest can describe what a recipient gets:
9397
+ * `pnpm publish` re-serialises package.json and rewrites `workspace:*`, so any hash taken from the
9398
+ * working tree is stale before the tarball exists.
9399
+ */
9400
+ function extractPublishTarball(packDir: string): { dir: string; cleanup: () => void } {
9401
+ const tmp = mkdtempSync(join(tmpdir(), 'dz-sign-pack-'));
9402
+ const out = execFileSync('pnpm', ['pack', '--pack-destination', tmp, '--json'], {
9403
+ cwd: packDir,
9404
+ encoding: 'utf-8',
9405
+ maxBuffer: 64 * 1024 * 1024,
9406
+ });
9407
+ const parsed = JSON.parse(out) as { filename?: string } | { filename?: string }[];
9408
+ const entry = Array.isArray(parsed) ? parsed[0] : parsed;
9409
+ const tgz = entry?.filename;
9410
+ if (tgz === undefined) throw new Error(`pnpm pack did not name a tarball for ${packDir}`);
9411
+ // pnpm reports an ABSOLUTE filename (it already contains --pack-destination); joining again would
9412
+ // double the directory. npm reports a bare name. Accept both rather than assuming either.
9413
+ const tgzPath = isAbsolute(tgz) ? tgz : join(tmp, tgz);
9414
+ execFileSync('tar', ['-xzf', tgzPath, '-C', tmp]);
9415
+ return { dir: join(tmp, 'package'), cleanup: (): void => { try { rmSync(tmp, { recursive: true, force: true }); } catch { /* best-effort */ } } };
9416
+ }
9417
+
9418
+ function cmdFeatureAdrRecord(options: Map<string, string>, flags: Set<string>, cwd: string, write: Write): number {
9419
+ const json = flags.has('json');
9420
+ const kind = (options.get('kind') ?? '').trim() as RecordKind;
9421
+ const stage = (options.get('stage') ?? '').trim();
9422
+ const slug = (options.get('slug') ?? '').trim();
9423
+ const payloadRaw = options.get('row') ?? options.get('pair') ?? '';
9424
+
9425
+ const emit = (d: RecordDecision, extra: Record<string, unknown> = {}): number => {
9426
+ if (json) {
9427
+ write(JSON.stringify({ ok: d.exit === 0, kind, stage, verdict: d.verdict, exit: d.exit, reason: d.reason, blocking: d.blocking, ...extra }));
9428
+ } else {
9429
+ write(recordVerdictLine(kind === 'ledger' || kind === 'training-pair' ? kind : 'ledger', stage, d));
9430
+ }
9431
+ return d.exit;
9432
+ };
9433
+
9434
+ if (kind !== 'ledger' && kind !== 'training-pair') {
9435
+ write('dz feature-adr-record: --kind must be ledger or training-pair');
9436
+ return 2;
9437
+ }
9438
+ if (payloadRaw === '') {
9439
+ write('dz feature-adr-record: --row (ledger) or --pair (training-pair) is required');
9440
+ return 2;
9441
+ }
9442
+
9443
+ const repo = (options.get('project') ?? cwd).trim() || cwd;
9444
+ const target = kind === 'ledger'
9445
+ ? join(repo, '.dz', 'feature-adr', 'run-cost-ledger.jsonl')
9446
+ : join(repo, '.dz', 'fa-training', slug === '' ? 'unknown' : slug, `${stage.replace(/[^\w.-]/g, '_')}.jsonl`);
9447
+ const markDir = join(repo, '.dz', 'fa-training', '.backfill-marks');
9448
+ const markName = (options.get('mark') ?? '').trim();
9449
+ const markPath = markName === '' ? null : join(markDir, markName.replace(/[^\w.-]/g, '_'));
9450
+
9451
+ const decision = decideRecordWrite({
9452
+ kind,
9453
+ payloadRaw,
9454
+ stage,
9455
+ stageProducedResult: flags.has('no-result') ? false : true,
9456
+ markExists: markPath !== null && existsSync(markPath),
9457
+ targetExists: existsSync(target),
9458
+ targetHasPair: flags.has('once') && existsSync(target),
9459
+ timestamp: new Date().toISOString(),
9460
+ });
9461
+ if (decision.line === null) return emit(decision);
9462
+
9463
+ // MEASURE, never assume: an unwritable target is a LOUD refusal, because a swallowed mkdir failure
9464
+ // is exactly how a write becomes a silent no-op (acid case A4).
9465
+ try {
9466
+ mkdirSync(dirname(target), { recursive: true });
9467
+ if (markPath !== null) {
9468
+ mkdirSync(markDir, { recursive: true });
9469
+ mkdirSync(markPath);
9470
+ }
9471
+ } catch (err) {
9472
+ const code = (err as NodeJS.ErrnoException).code;
9473
+ if (markPath !== null && code === 'EEXIST') {
9474
+ // A stale mark (target absent) was already decided as writable above; re-taking it is a no-op,
9475
+ // not a duplicate. Only a mark WITH its target means another run got here first.
9476
+ if (decision.staleMark !== true) {
9477
+ return emit({ verdict: 'duplicate', exit: 0, reason: 'a mark for this record already exists — another run captured it first', blocking: false, line: null });
9478
+ }
9479
+ }
9480
+ return emit({ verdict: 'refused', exit: 2, reason: `the target could not be prepared: ${(err as Error).message}`, blocking: false, line: null });
9481
+ }
9482
+
9483
+ try {
9484
+ appendFileSync(target, `${decision.line}\n`, 'utf-8');
9485
+ } catch (err) {
9486
+ return emit({ verdict: 'refused', exit: 2, reason: `the append failed: ${(err as Error).message}`, blocking: false, line: null });
9487
+ }
9488
+
9489
+ // ADR-002: the write is verified against the disk, never inferred from the absence of an error.
9490
+ let lastLine: string | null = null;
9491
+ try {
9492
+ const body = readFileSync(target, 'utf-8');
9493
+ const lines = body.split('\n').filter((l) => l !== '');
9494
+ lastLine = lines.length > 0 ? (lines[lines.length - 1] as string) : null;
9495
+ } catch {
9496
+ lastLine = null;
9497
+ }
9498
+ return emit(decideReadBack(decision.line, lastLine), { target });
9499
+ }
9500
+
9501
+ function cmdAmendmentCheck(options: Map<string, string>, flags: Set<string>, cwd: string, write: Write): number {
9502
+ const json = flags.has('json');
9503
+ const readOr = (abs: string): string | null => {
9504
+ try {
9505
+ return readFileSync(abs, 'utf-8');
9506
+ } catch {
9507
+ return null;
9508
+ }
9509
+ };
9510
+
9511
+ const checkOne = (featureDir: string): { decision: ReturnType<typeof decideAmendmentOutcome>; resolutions: ReturnType<typeof resolveAmendments>; slug: string } => {
9512
+ const slug = basename(featureDir);
9513
+ const ideation = readOr(join(featureDir, '03.5_ideation_report.md'));
9514
+ const plan = readOr(join(featureDir, '06_implementation_plan.md'));
9515
+ if (ideation === null) {
9516
+ return {
9517
+ slug,
9518
+ resolutions: [],
9519
+ decision: decideAmendmentOutcome({ sectionPresent: false, rows: [], resolutions: [], planSaysNone: false }),
9520
+ };
9521
+ }
9522
+ const sectionPresent = amendmentSection(ideation) !== null;
9523
+ const ideationRows = parseAmendments(ideation);
9524
+ const planRows = plan === null ? [] : parseAmendments(plan);
9525
+ // The PLAN is authoritative when it carries rows: Step 6 owes "carry AM-N into the plan
9526
+ // verbatim", and the ideation report is the historical record — rewriting its rows to match
9527
+ // tests named later would close the trail by falsifying it. Coverage keeps that honest below.
9528
+ const rows = planRows.length > 0 ? planRows : ideationRows;
9529
+ const missingFromPlan = planRows.length > 0 ? amendmentsMissingFromPlan(ideationRows, planRows) : [];
9530
+ // Paths in an amendment row are repo-relative, so they resolve against the repo root — not
9531
+ // against the feature directory, and not against wherever the caller happened to stand.
9532
+ const resolutions = resolveAmendments(rows, { readFile: (rel) => readOr(resolve(cwd, rel)) });
9533
+ const decision = decideAmendmentOutcome({
9534
+ sectionPresent,
9535
+ rows,
9536
+ resolutions,
9537
+ planSaysNone: plan !== null && planSaysNoAmendments(plan),
9538
+ missingFromPlan,
9539
+ });
9540
+ return { slug, decision, resolutions };
9541
+ };
9542
+
9543
+ // --all is a CENSUS, not a gate (ADR-003): a gate that is red on arrival over 21 historical
9544
+ // features gets disabled, and then the mechanism is gone along with the debt it was to surface.
9545
+ if (flags.has('all')) {
9546
+ const featuresDir = join(cwd, 'features');
9547
+ let slugs: string[] = [];
9548
+ try {
9549
+ slugs = readdirSync(featuresDir, { withFileTypes: true }).filter((e) => e.isDirectory()).map((e) => e.name).sort();
9550
+ } catch {
9551
+ if (json) write(JSON.stringify({ ok: true, mode: 'all', features: [], note: 'no features/ directory' }));
9552
+ else write('amendment traceability census: no features/ directory');
9553
+ return 0;
9554
+ }
9555
+ const rowsOut: Record<string, unknown>[] = [];
9556
+ for (const slug of slugs) {
9557
+ try {
9558
+ const r = checkOne(join(featuresDir, slug));
9559
+ rowsOut.push({ slug, outcome: r.decision.outcome, counts: r.decision.counts, reasons: r.decision.reasons.length });
9560
+ } catch (err) {
9561
+ // AM-6: one unreadable feature becomes its own row. Dropping it silently would make the
9562
+ // census read like coverage it does not have.
9563
+ rowsOut.push({ slug, outcome: 'read-error', error: (err as Error).message });
9564
+ }
9565
+ }
9566
+ if (json) {
9567
+ write(JSON.stringify({ ok: true, mode: 'all', note: AMENDMENT_VACUITY_NOTE, features: rowsOut }));
9568
+ } else {
9569
+ const tally = new Map<string, number>();
9570
+ for (const r of rowsOut) tally.set(String(r['outcome']), (tally.get(String(r['outcome'])) ?? 0) + 1);
9571
+ for (const r of rowsOut) {
9572
+ if (r['outcome'] !== 'pass' && r['outcome'] !== 'skip') write(` [${String(r['outcome'])}] ${String(r['slug'])}`);
9573
+ }
9574
+ write(`amendment traceability census over ${rowsOut.length} feature(s): ${[...tally].map(([k, v]) => `${v} ${k}`).join(', ')}`);
9575
+ write(AMENDMENT_VACUITY_NOTE);
9576
+ }
9577
+ return 0;
9578
+ }
9579
+
9580
+ const explicitDir = (options.get('feature-dir') ?? '').trim();
9581
+ const slug = (options.get('slug') ?? '').trim();
9582
+ if (explicitDir === '' && slug === '') {
9583
+ write('dz amendment-check: one of --slug / --feature-dir is required (or --all for a census)');
9584
+ return 2;
9585
+ }
9586
+ const featureDir = explicitDir !== '' ? resolve(cwd, explicitDir) : join(cwd, 'features', slug);
9587
+ const { decision, resolutions } = checkOne(featureDir);
9588
+ if (json) {
9589
+ write(JSON.stringify({
9590
+ ok: decision.outcome === 'pass' || decision.outcome === 'skip',
9591
+ slug: basename(featureDir),
9592
+ outcome: decision.outcome,
9593
+ exit: decision.exit,
9594
+ counts: decision.counts,
9595
+ reasons: decision.reasons,
9596
+ rows: resolutions,
9597
+ note: AMENDMENT_VACUITY_NOTE,
9598
+ }));
9599
+ } else {
9600
+ for (const r of resolutions) {
9601
+ if (r.verdict !== 'resolved') write(` [${r.verdict}] ${r.id}${r.testId === null ? '' : ` \`${r.testId}\``} — ${r.detail}`);
9602
+ }
9603
+ write(AMENDMENT_VACUITY_NOTE);
9604
+ // The verdict is the LAST line, in the K2 gate's own shape, so a caller that reads the tail of
9605
+ // the output reads a verdict rather than a finding.
9606
+ write(amendmentVerdictLine(decision));
9607
+ }
9608
+ return decision.exit;
9609
+ }
9610
+
8961
9611
  function cmdFeatureAdrCheckpoint(options: Map<string, string>, flags: Set<string>, cwd: string, write: Write): number {
8962
9612
  const json = flags.has('json');
8963
9613
  const emit = (payload: Record<string, unknown>, human: string, code: number): number => {
@@ -11775,6 +12425,10 @@ export async function runCli(argv: string[], io: CliIo = {}): Promise<number> {
11775
12425
  return cmdEpochReplay(options, flags, cwd, write);
11776
12426
  case 'score':
11777
12427
  return cmdScore(options, flags, cwd, write);
12428
+ case 'feature-adr-record':
12429
+ return cmdFeatureAdrRecord(options, flags, cwd, write);
12430
+ case 'amendment-check':
12431
+ return cmdAmendmentCheck(options, flags, cwd, write);
11778
12432
  case 'feature-adr-checkpoint':
11779
12433
  return cmdFeatureAdrCheckpoint(options, flags, cwd, write);
11780
12434
  case 'reqe':