@dzhechkov/harness-cli 0.6.1 → 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,
@@ -408,6 +410,8 @@ import {
408
410
  amendmentVerdictLine,
409
411
  amendmentsMissingFromPlan,
410
412
  AMENDMENT_VACUITY_NOTE,
413
+ decideSignableSet,
414
+ signableSetLine,
411
415
  decideRecordWrite,
412
416
  decideReadBack,
413
417
  recordVerdictLine,
@@ -2678,6 +2682,13 @@ async function cmdTeach(options: Map<string, string>, flags: Set<string>, cwd: s
2678
2682
  }
2679
2683
  write(`Imported ${imported} pattern(s) from ${fromJson}`);
2680
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
+ }
2681
2692
  // Bulk import preserves the DOMAIN of every record, so it can put medical lessons in
2682
2693
  // a shared store as silently as a hand-typed teach — and it returned before the
2683
2694
  // advice single-teach prints. The same advice, at the same point in the flow: after
@@ -3080,6 +3091,45 @@ async function cmdRecallForget(
3080
3091
  return 0;
3081
3092
  }
3082
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;
3083
3133
  /**
3084
3134
  * `dz recall --promote <dzId>[,<dzId>…] [--apply]` — lift quarantine from NAMED records
3085
3135
  * (lesson-quarantine FR-6b). Dry-run by default, the --forget symmetry. Also clears the
@@ -3226,10 +3276,13 @@ async function cmdRecall(options: Map<string, string>, flags: Set<string>, cwd:
3226
3276
  }
3227
3277
 
3228
3278
  if (flags.has('books')) {
3229
- 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, {
3230
3282
  limit,
3231
3283
  ...(bookFilter !== undefined ? { book: bookFilter } : {}),
3232
3284
  });
3285
+ const { hits, error } = asJson ? await withForeignStdoutOnStderr(runBooks) : await runBooks();
3233
3286
  if (asJson) { write(JSON.stringify(hits)); return 0; }
3234
3287
  write(`dz recall "${query}" --books${bookFilter !== undefined ? ` --book ${bookFilter}` : ''} — ${hits.length} KU hit(s)`);
3235
3288
  if (error !== undefined) write(` (${error})`);
@@ -3237,6 +3290,34 @@ async function cmdRecall(options: Map<string, string>, flags: Set<string>, cwd:
3237
3290
  const src = h.chapter !== undefined ? ` [${h.book} гл.${h.chapter}${h.pages ? ` с.${h.pages[0]}-${h.pages[1]}` : ''}]` : ` [${h.book}]`;
3238
3291
  write(` (${h.type}) ${h.name}${src}`);
3239
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
+ }
3240
3321
  return 0;
3241
3322
  }
3242
3323
 
@@ -3264,11 +3345,25 @@ async function cmdRecall(options: Map<string, string>, flags: Set<string>, cwd:
3264
3345
  const shownQuery = oneLine(query);
3265
3346
  const shownDomain = wantedDomain === undefined ? undefined : oneLine(wantedDomain);
3266
3347
  const fetchLimit = wantedDomain !== undefined ? Math.min(limit * 3, limit + 20) : limit;
3267
- 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 });
3268
3358
 
3269
3359
  if (mode === 'semantic' && result.vectorEngine === 'none') {
3270
3360
  // --semantic is an explicit ask — degrading it silently would be dishonest (FR-3).
3271
- 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}`);
3272
3367
  return 1;
3273
3368
  }
3274
3369
  // Domain-aware re-ranking (health-advisor slice H): `--domain <name>` lifts lessons
@@ -3276,6 +3371,15 @@ async function cmdRecall(options: Map<string, string>, flags: Set<string>, cwd:
3276
3371
  // shared store keeps the cross-domain transfers that make it worth more than two stores.
3277
3372
  const boost = wantedDomain !== undefined ? applyDomainBoost(result.hits, wantedDomain) : null;
3278
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;
3279
3383
  // The boost never drops a hit, but the CUT still can: promoting a match into the top
3280
3384
  // `limit` pushes the last one out, so a lesson visible WITHOUT --domain can vanish
3281
3385
  // WITH it. Cross-model review called this out as a lie by omission — the note said
@@ -3285,7 +3389,19 @@ async function cmdRecall(options: Map<string, string>, flags: Set<string>, cwd:
3285
3389
  if (asJson) {
3286
3390
  // Portable contract UNCHANGED (I-7/AC-6): a plain PatternRecord[] — round-trips through
3287
3391
  // `dz teach --from-json` regardless of which backend ranked each hit.
3288
- 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
+ }))));
3289
3405
  // The honesty notes go to STDERR here rather than being skipped: the JSON branch
3290
3406
  // used to return before them, so a scripted caller was told nothing about a boost
3291
3407
  // that had promoted a match and pushed a visible hit past the --limit cut.
@@ -3294,7 +3410,7 @@ async function cmdRecall(options: Map<string, string>, flags: Set<string>, cwd:
3294
3410
  const cutNoteJson = renderDomainCutNote(displaced, limit);
3295
3411
  if (cutNoteJson !== '') process.stderr.write(`${cutNoteJson}\n`);
3296
3412
  }
3297
- return 0;
3413
+ return semanticUnserved ? 1 : 0;
3298
3414
  }
3299
3415
 
3300
3416
  if (hits.length === 0) {
@@ -3304,11 +3420,29 @@ async function cmdRecall(options: Map<string, string>, flags: Set<string>, cwd:
3304
3420
  // silently said nothing about the domain, so the reader could not tell whether the
3305
3421
  // boost had been applied and found nothing, or had not run at all.
3306
3422
  if (boost !== null && shownDomain !== undefined) write(renderDomainBoostNote(boost, shownDomain));
3307
- return 0;
3308
- }
3309
- 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';
3310
3434
  const lexLabel = result.lexicalBackend === 'sqlite' ? 'SQLite FTS5' : 'keyword (JSON)';
3311
- 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)`;
3312
3446
  write(`dz recall "${shownQuery}" — ${hits.length} hit(s), ${ranking}`);
3313
3447
  let sawQuarantined = false;
3314
3448
  for (const h of hits) {
@@ -3345,10 +3479,20 @@ async function cmdRecall(options: Map<string, string>, flags: Set<string>, cwd:
3345
3479
  // FR-8 hint swap: with an engine the old MCP-only hint is gone; without one, the SAME
3346
3480
  // conditional position carries an actionable enablement line instead — the only permitted
3347
3481
  // output change on the degraded path.
3348
- 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'))) {
3349
3489
  write(` ℹ semantic (vector) recall needs the agentdb vector tier — run: dz setup --memory agentdb`);
3350
3490
  }
3351
- 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;
3352
3496
  }
3353
3497
 
3354
3498
  /* ------------------------------------------------------------------ */
@@ -3433,11 +3577,36 @@ async function cmdVector(options: Map<string, string>, flags: Set<string>, cwd:
3433
3577
  write(` Mode: ${st.mode} (.dz/config.json → memory.vector.engine)`);
3434
3578
  if (st.embeddingModel !== undefined) write(` Embedding model: ${st.embeddingModel}`);
3435
3579
  write(` Lexical patterns: ${st.lexicalMirrorable} mirrorable (${st.lexicalTotal} total)`);
3436
- 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
+ }
3437
3591
  write(` Pending mirror queue: ${st.pending}`);
3438
- 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) {
3439
3605
  write(` ℹ mirror behind the lexical store — run: dz consolidate (backfill)`);
3440
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
+ }
3441
3610
  return 0;
3442
3611
  }
3443
3612
 
@@ -4562,7 +4731,10 @@ function verifyInstalledPacks(cwd: string, explicitPubkey?: string | undefined):
4562
4731
  packaged: packagedTrustRootPath(),
4563
4732
  });
4564
4733
 
4565
- 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);
4566
4738
 
4567
4739
  // Cross-model review: `--pubkey <pack>/evil.pub` would let the artifact supply its own verifying key
4568
4740
  // through the caller. The tool must never verify a pack against a key that lives inside it.
@@ -4601,6 +4773,32 @@ function verifyInstalledPacks(cwd: string, explicitPubkey?: string | undefined):
4601
4773
  checks.push({ pack, verdict: 'no-trust-root', failures: [] });
4602
4774
  continue;
4603
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
+ }
4604
4802
  const res = verifyManifest(dir, signed as never, keyPem);
4605
4803
  checks.push({
4606
4804
  pack,
@@ -4611,6 +4809,36 @@ function verifyInstalledPacks(cwd: string, explicitPubkey?: string | undefined):
4611
4809
  return { trustRoot, checks };
4612
4810
  }
4613
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
+
4614
4842
  /** Print the pack verdicts and return 1 iff the policy says any of them is fatal. */
4615
4843
  function reportPackVerification(
4616
4844
  cwd: string,
@@ -4629,7 +4857,7 @@ function reportPackVerification(
4629
4857
  }
4630
4858
  if (checks.length === 0) return 0;
4631
4859
 
4632
- 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>;
4633
4861
  let fatal = 0;
4634
4862
  for (const c of checks) {
4635
4863
  counts[c.verdict]++;
@@ -4645,7 +4873,7 @@ function reportPackVerification(
4645
4873
  const root = trustRoot ? `${trustRoot.source} (${trustRoot.path})` : 'none';
4646
4874
  write(
4647
4875
  ` signatures: ${counts.verified} verified, ${counts.unsigned} unsigned, ` +
4648
- `${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}`,
4649
4877
  );
4650
4878
  // A signature proves the bytes are unmodified. It never proves the skill is any good.
4651
4879
  return fatal > 0 ? 1 : 0;
@@ -4733,13 +4961,40 @@ function cmdSign(options: Map<string, string>, flags: Set<string>, cwd: string,
4733
4961
  }
4734
4962
  if (!existsSync(resolve(cwd, key))) { write(`dz sign: private key not found: ${resolve(cwd, key)}`); return 1; }
4735
4963
 
4736
- const files = packFiles(packDir);
4737
- 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
+ }
4738
4992
 
4739
- const manifest = buildManifest(packDir, basename(packDir), files);
4993
+ const manifest = buildManifest(hashRoot, basename(packDir), files);
4740
4994
  const signed = signManifest(manifest, readFileSync(resolve(cwd, key), 'utf8'));
4741
4995
  writeFileSync(join(packDir, MANIFEST_NAME), JSON.stringify(signed, null, 2) + '\n');
4742
4996
  writeFileSync(join(packDir, SBOM_NAME), JSON.stringify(buildSbom(manifest), null, 2) + '\n');
4997
+ cleanupPack?.();
4743
4998
  write(`dz sign: signed ${files.length} file(s) in ${packDir}`);
4744
4999
  write(` ${MANIFEST_NAME} + ${SBOM_NAME} written. Ed25519 gives tamper-evidence, never truthfulness.`);
4745
5000
  return 0;
@@ -4763,7 +5018,11 @@ function cmdVerifyPack(options: Map<string, string>, flags: Set<string>, cwd: st
4763
5018
  try { signed = JSON.parse(readFileSync(manifestPath, 'utf8')); }
4764
5019
  catch { write(`dz verify-pack: ${MANIFEST_NAME} is not valid JSON`); return 1; }
4765
5020
 
4766
- 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);
4767
5026
  if (res.ok) { write(`dz verify-pack: OK — ${packDir} matches its signed manifest`); return 0; }
4768
5027
  write(`dz verify-pack: FAILED — ${packDir}`);
4769
5028
  for (const f of res.failures) write(` ${f.path}: ${f.reason}`);
@@ -4936,7 +5195,9 @@ function cmdPublish(options: Map<string, string>, flags: Set<string>, cwd: strin
4936
5195
  if (trustRootPresent && manifestPresent) {
4937
5196
  try {
4938
5197
  const signed = JSON.parse(readFileSync(manifestPath, 'utf8'));
4939
- 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;
4940
5201
  } catch {
4941
5202
  verifyOk = false;
4942
5203
  }
@@ -4967,15 +5228,61 @@ function cmdPublish(options: Map<string, string>, flags: Set<string>, cwd: strin
4967
5228
  bumpOnly,
4968
5229
  claimGate: claimCheckOpt,
4969
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
+ },
4970
5247
  reSign: (packDir: string, keyPath: string): void => {
4971
5248
  // The same three steps `dz sign` performs, including the SBOM — a manifest refreshed without
4972
5249
  // its SBOM would leave the two describing different trees.
4973
- const files = packFiles(packDir);
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];
4974
5275
  if (files.length === 0) throw new Error(`refusing to sign an empty pack: ${packDir}`);
4975
- const manifest = buildManifest(packDir, basename(packDir), files);
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);
4976
5280
  const signed = signManifest(manifest, readFileSync(keyPath, 'utf-8'));
4977
5281
  writeFileSync(join(packDir, MANIFEST_NAME), `${JSON.stringify(signed, null, 2)}\n`);
4978
5282
  writeFileSync(join(packDir, SBOM_NAME), `${JSON.stringify(buildSbom(manifest), null, 2)}\n`);
5283
+ } finally {
5284
+ cleanup();
5285
+ }
4979
5286
  },
4980
5287
  });
4981
5288
 
@@ -6428,8 +6735,16 @@ function cmdDriftCheck(options: Map<string, string>, flags: Set<string>, cwd: st
6428
6735
  const r = sweepSkillDrift(root, { scope, allowlist });
6429
6736
 
6430
6737
  if (flags.has('json')) {
6431
- write(JSON.stringify({ ...r, scope, allowlist }));
6432
- 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;
6433
6748
  }
6434
6749
 
6435
6750
  write(`Skills duplicated across ≥2 ${scope === 'packages' ? 'package' : ''} locations: ${r.duplicated}`);
@@ -6483,7 +6798,7 @@ const DEFAULT_STORE_CAP = 5000;
6483
6798
  const MAX_STUB_SCAN_FILES = 400;
6484
6799
 
6485
6800
  /** Read the optional `.dz/guard.json` — `{ rules?: [...], storeCap?: number, stubWaivers?: [...] }`. Missing/broken ⇒ defaults. */
6486
- function loadGuardConfig(root: string): { rules?: unknown[]; storeCap?: number; stubWaivers?: unknown[] } {
6801
+ function loadGuardConfig(root: string): { rules?: unknown[]; storeCap?: number; stubWaivers?: unknown[]; reviewRound?: { minGrade?: unknown } } {
6487
6802
  const p = join(root, '.dz', 'guard.json');
6488
6803
  if (!existsSync(p)) return {};
6489
6804
  try {
@@ -6630,6 +6945,51 @@ function gatherGuardFacts(op: string, root: string, text: string | undefined, st
6630
6945
  facts['readmeFirst'] = [...perPack.entries()].map(([name, e]) => ({ name: '@dzhechkov/' + name, versionBumped: e.pkgJson, readmeChanged: e.readme }));
6631
6946
  } catch { /* not a git repo — rule skips */ }
6632
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
+
6633
6993
  // skills-registrable: every skill dir in a skill pack must carry a depth-1 SKILL.md, or it ships
6634
6994
  // registering nowhere (the health-advisor 1.2.0 class). Pure-toolkit packages yield nothing.
6635
6995
  try {
@@ -9009,6 +9369,52 @@ function scanOneReqeRoot(
9009
9369
  * the exit code. A courier can neither refuse nor verify, which is how four workflow runs finished
9010
9370
  * with no cost row at all.
9011
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
+
9012
9418
  function cmdFeatureAdrRecord(options: Map<string, string>, flags: Set<string>, cwd: string, write: Write): number {
9013
9419
  const json = flags.has('json');
9014
9420
  const kind = (options.get('kind') ?? '').trim() as RecordKind;