@dzhechkov/harness-cli 0.6.1 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.dz-manifest.json +20 -264
- package/README.md +55 -4
- package/dist/cli.d.ts +9 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +513 -33
- package/dist/cli.js.map +1 -1
- package/dist/core-compat.d.ts +1 -1
- package/dist/core-compat.d.ts.map +1 -1
- package/dist/core-compat.js +4 -1
- package/dist/core-compat.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/sbom.json +19 -629
- package/src/cli.ts +495 -27
- package/src/core-compat.ts +4 -1
- package/src/index.ts +3 -0
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
4737
|
-
|
|
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(
|
|
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
|
-
|
|
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}`);
|
|
@@ -4933,15 +5192,40 @@ function cmdPublish(options: Map<string, string>, flags: Set<string>, cwd: strin
|
|
|
4933
5192
|
const manifestPath = join(pk.dir, MANIFEST_NAME);
|
|
4934
5193
|
const manifestPresent = existsSync(manifestPath);
|
|
4935
5194
|
let verifyOk = false;
|
|
5195
|
+
let artifactUnavailable = false;
|
|
4936
5196
|
if (trustRootPresent && manifestPresent) {
|
|
5197
|
+
// Verify the ARTIFACT, through the SAME extraction `dz sign` uses. This used to walk the
|
|
5198
|
+
// source TREE narrowed to a shipped-path list — a different object by construction, because
|
|
5199
|
+
// npm synthesises a LICENSE into the pack of a package whose tree has none and pnpm rewrites
|
|
5200
|
+
// package.json at pack time. So the gate named two failures that were both true about the
|
|
5201
|
+
// tree and both wrong about what ships, for EVERY package, and a fail-closed gate no input
|
|
5202
|
+
// can satisfy is a gate that gets routed around — it was: the 0.7.0 release went out through
|
|
5203
|
+
// `pnpm publish` and skipped re-signing (ADR-001, features/publish-gate-verifies-the-tarball).
|
|
5204
|
+
let cleanupGate: (() => void) | null = null;
|
|
4937
5205
|
try {
|
|
4938
5206
|
const signed = JSON.parse(readFileSync(manifestPath, 'utf8'));
|
|
4939
|
-
|
|
5207
|
+
let extracted: { dir: string; cleanup: () => void } | undefined;
|
|
5208
|
+
try {
|
|
5209
|
+
extracted = extractPublishTarball(pk.dir);
|
|
5210
|
+
cleanupGate = extracted.cleanup;
|
|
5211
|
+
} catch (err) {
|
|
5212
|
+
// Cross-family review (codex `gpt-5.6-sol`, 2026-08-22): falling back to the working
|
|
5213
|
+
// TREE here fails the gate OPEN. The gate's whole claim is "what ships matches the
|
|
5214
|
+
// signature"; with no artifact, nothing was compared, and reporting a pass would be a
|
|
5215
|
+
// claim about an object that was never built. Say why, and block.
|
|
5216
|
+
write(`dz publish: could not pack ${pk.name} (${(err as Error).message.split('\n')[0]}) — the artifact was never built, so its signature was not checked`);
|
|
5217
|
+
artifactUnavailable = true;
|
|
5218
|
+
}
|
|
5219
|
+
verifyOk =
|
|
5220
|
+
extracted !== undefined &&
|
|
5221
|
+
verifyManifest(extracted.dir, signed, readFileSync(trustRoot, 'utf8')).ok;
|
|
4940
5222
|
} catch {
|
|
4941
5223
|
verifyOk = false;
|
|
5224
|
+
} finally {
|
|
5225
|
+
cleanupGate?.();
|
|
4942
5226
|
}
|
|
4943
5227
|
}
|
|
4944
|
-
const decision = decidePublishGate({ trustRootPresent, manifestPresent, verifyOk, requireSigning });
|
|
5228
|
+
const decision = decidePublishGate({ trustRootPresent, manifestPresent, verifyOk, requireSigning, artifactUnavailable });
|
|
4945
5229
|
if (decision.action === 'block') {
|
|
4946
5230
|
write(`dz publish: BLOCKED ${pk.name} — ${decision.reason}`);
|
|
4947
5231
|
blocked++;
|
|
@@ -4967,15 +5251,76 @@ function cmdPublish(options: Map<string, string>, flags: Set<string>, cwd: strin
|
|
|
4967
5251
|
bumpOnly,
|
|
4968
5252
|
claimGate: claimCheckOpt,
|
|
4969
5253
|
signKey: signKey === '' ? undefined : resolve(cwd, signKey),
|
|
5254
|
+
verifyAfterSign: (packDir: string): { ok: boolean; trustRootPresent: boolean; pack?: string } => {
|
|
5255
|
+
// Verify the OUTCOME against the trust root a CONSUMER would use — an existing key may be the
|
|
5256
|
+
// WRONG key, and enumerating that state is a losing game (round-1 review). The pack NAME travels
|
|
5257
|
+
// with the verdict so a pass about a different artifact cannot be mistaken for this one.
|
|
5258
|
+
// The identity must be the one the PUBLISHER uses — the npm package name — or the check
|
|
5259
|
+
// compares two vocabularies and can never agree. It did: `basename(packDir)` is `memory`
|
|
5260
|
+
// where the publisher says `@dzhechkov/memory`, so every scoped package (all of them here)
|
|
5261
|
+
// was refused as "a pass about another artifact", and the release routed around the gate
|
|
5262
|
+
// instead. MEASURED 2026-08-22 by running `dz publish --filter memory --yes`.
|
|
5263
|
+
const trustRoot = resolve(cwd, TRUST_ROOT_REL);
|
|
5264
|
+
const identity = packNpmName(packDir);
|
|
5265
|
+
const id = identity === undefined ? {} : { pack: identity };
|
|
5266
|
+
if (!existsSync(trustRoot)) return { ok: false, trustRootPresent: false, ...id };
|
|
5267
|
+
// Verify the SAME OBJECT `reSign` hashed: the extracted tarball. Walking the source tree here
|
|
5268
|
+
// compares a manifest built from the artifact against files npm rewrites at pack time, so it
|
|
5269
|
+
// reported "does not verify — most likely the WRONG signing key" about a correctly signed pack
|
|
5270
|
+
// (MEASURED 2026-08-22, `dz publish --filter memory --yes`). That message sends the operator
|
|
5271
|
+
// hunting for a key problem that does not exist; the object was simply the wrong one.
|
|
5272
|
+
let cleanup: (() => void) | null = null;
|
|
5273
|
+
try {
|
|
5274
|
+
const signed = JSON.parse(readFileSync(join(packDir, MANIFEST_NAME), 'utf8')) as never;
|
|
5275
|
+
const extracted = extractPublishTarball(packDir);
|
|
5276
|
+
cleanup = extracted.cleanup;
|
|
5277
|
+
const res = verifyManifest(extracted.dir, signed, readFileSync(trustRoot, 'utf8'));
|
|
5278
|
+
return { ok: res.ok, trustRootPresent: true, ...id };
|
|
5279
|
+
} catch {
|
|
5280
|
+
return { ok: false, trustRootPresent: true, ...id };
|
|
5281
|
+
} finally {
|
|
5282
|
+
cleanup?.();
|
|
5283
|
+
}
|
|
5284
|
+
},
|
|
4970
5285
|
reSign: (packDir: string, keyPath: string): void => {
|
|
4971
5286
|
// The same three steps `dz sign` performs, including the SBOM — a manifest refreshed without
|
|
4972
5287
|
// its SBOM would leave the two describing different trees.
|
|
4973
|
-
|
|
5288
|
+
//
|
|
5289
|
+
// Path A: the manifest must cover what the CONSUMER receives, not what the author has on disk.
|
|
5290
|
+
// MEASURED 2026-08-21 by a live install of the published 0.6.1 — with the trust root restored,
|
|
5291
|
+
// six packs reported TAMPERED, and only half of that was the version bump. The rest was
|
|
5292
|
+
// `CHANGELOG.md: listed in the manifest but absent`: signed on disk, excluded by `files[]`,
|
|
5293
|
+
// therefore missing for every recipient forever. So the file list comes from `npm pack`, which
|
|
5294
|
+
// is the authority on what ships — we do not reimplement its globbing.
|
|
5295
|
+
// Hash the EXTRACTED TARBALL, not the working tree. `pnpm publish` re-serialises package.json and
|
|
5296
|
+
// rewrites `workspace:*`, so a hash taken from disk is stale before the tarball exists — MEASURED
|
|
5297
|
+
// 2026-08-21: the published `skills-news` package.json is 1050 bytes where the tree's is 1051,
|
|
5298
|
+
// and that one missing newline is what made six freshly re-signed packs report TAMPERED.
|
|
5299
|
+
const { dir: shippedDir, cleanup } = extractPublishTarball(packDir);
|
|
5300
|
+
try {
|
|
5301
|
+
const onDisk = packFiles(shippedDir);
|
|
5302
|
+
const packed = packFiles(shippedDir);
|
|
5303
|
+
const setDecision = decideSignableSet({ signable: onDisk, packed });
|
|
5304
|
+
write(signableSetLine(basename(packDir), setDecision));
|
|
5305
|
+
if (setDecision.publishedButUnsigned.length > 0) {
|
|
5306
|
+
// A shipped file no signature covers is WORSE than an unsigned pack: the badge says verified
|
|
5307
|
+
// while part of the payload is unchecked. Refuse rather than sign a partial claim.
|
|
5308
|
+
throw new Error(
|
|
5309
|
+
`refusing to sign a pack with ${setDecision.publishedButUnsigned.length} SHIPPED BUT UNSIGNED file(s): ${setDecision.publishedButUnsigned.slice(0, 5).join(', ')}`,
|
|
5310
|
+
);
|
|
5311
|
+
}
|
|
5312
|
+
const files = [...setDecision.sign];
|
|
4974
5313
|
if (files.length === 0) throw new Error(`refusing to sign an empty pack: ${packDir}`);
|
|
4975
|
-
|
|
5314
|
+
// Hashes come from the extracted tarball; the manifest is WRITTEN to the source dir so the next
|
|
5315
|
+
// pack carries it. The re-pack normalises package.json identically (deterministic — MEASURED by
|
|
5316
|
+
// packing twice and comparing hashes), so the entries still describe what ships.
|
|
5317
|
+
const manifest = buildManifest(shippedDir, basename(packDir), files);
|
|
4976
5318
|
const signed = signManifest(manifest, readFileSync(keyPath, 'utf-8'));
|
|
4977
5319
|
writeFileSync(join(packDir, MANIFEST_NAME), `${JSON.stringify(signed, null, 2)}\n`);
|
|
4978
5320
|
writeFileSync(join(packDir, SBOM_NAME), `${JSON.stringify(buildSbom(manifest), null, 2)}\n`);
|
|
5321
|
+
} finally {
|
|
5322
|
+
cleanup();
|
|
5323
|
+
}
|
|
4979
5324
|
},
|
|
4980
5325
|
});
|
|
4981
5326
|
|
|
@@ -6428,8 +6773,16 @@ function cmdDriftCheck(options: Map<string, string>, flags: Set<string>, cwd: st
|
|
|
6428
6773
|
const r = sweepSkillDrift(root, { scope, allowlist });
|
|
6429
6774
|
|
|
6430
6775
|
if (flags.has('json')) {
|
|
6431
|
-
|
|
6432
|
-
|
|
6776
|
+
// The signature verdicts were printed in the TEXT output and absent from the JSON, so a CI job
|
|
6777
|
+
// parsing `--json` saw `drifted: 0` and concluded all was well while packs were TAMPERED. A gate
|
|
6778
|
+
// silent in the form CI reads is not a gate (MEASURED 2026-08-21: keys were duplicated, drifted,
|
|
6779
|
+
// allowlisted, scope, allowlist — and nothing else).
|
|
6780
|
+
const sig = collectPackVerification(root, options.get('pubkey'));
|
|
6781
|
+
const sigBlocking = sig.packs.filter(
|
|
6782
|
+
(c) => decideVerifyPolicy(c.verdict, flags.has('require-signing')).action === 'fail',
|
|
6783
|
+
).length;
|
|
6784
|
+
write(JSON.stringify({ ...r, scope, allowlist, signatures: sig }));
|
|
6785
|
+
return r.drifted.length > 0 || sigBlocking > 0 ? 1 : 0;
|
|
6433
6786
|
}
|
|
6434
6787
|
|
|
6435
6788
|
write(`Skills duplicated across ≥2 ${scope === 'packages' ? 'package' : ''} locations: ${r.duplicated}`);
|
|
@@ -6483,7 +6836,7 @@ const DEFAULT_STORE_CAP = 5000;
|
|
|
6483
6836
|
const MAX_STUB_SCAN_FILES = 400;
|
|
6484
6837
|
|
|
6485
6838
|
/** Read the optional `.dz/guard.json` — `{ rules?: [...], storeCap?: number, stubWaivers?: [...] }`. Missing/broken ⇒ defaults. */
|
|
6486
|
-
function loadGuardConfig(root: string): { rules?: unknown[]; storeCap?: number; stubWaivers?: unknown[] } {
|
|
6839
|
+
function loadGuardConfig(root: string): { rules?: unknown[]; storeCap?: number; stubWaivers?: unknown[]; reviewRound?: { minGrade?: unknown } } {
|
|
6487
6840
|
const p = join(root, '.dz', 'guard.json');
|
|
6488
6841
|
if (!existsSync(p)) return {};
|
|
6489
6842
|
try {
|
|
@@ -6630,6 +6983,51 @@ function gatherGuardFacts(op: string, root: string, text: string | undefined, st
|
|
|
6630
6983
|
facts['readmeFirst'] = [...perPack.entries()].map(([name, e]) => ({ name: '@dzhechkov/' + name, versionBumped: e.pkgJson, readmeChanged: e.readme }));
|
|
6631
6984
|
} catch { /* not a git repo — rule skips */ }
|
|
6632
6985
|
|
|
6986
|
+
// review-round: the same WORKING-TREE diff, asked a different question — does a package that
|
|
6987
|
+
// bumps its version and changes SOURCE bring a GRADED QE report with it? Scoped to source so a
|
|
6988
|
+
// docs-only republish is never blocked (ADR-001, features/publish-needs-a-review). A throw here
|
|
6989
|
+
// leaves the whole fact undefined, and the rule then reports NOTHING: absence of a report is an
|
|
6990
|
+
// accusation, absence of facts is ignorance, and they must not render the same.
|
|
6991
|
+
try {
|
|
6992
|
+
const status = execSync('git status --porcelain -uall', { cwd: root, encoding: 'utf-8' });
|
|
6993
|
+
const changed = status.split('\n').map((l) => l.slice(3).trim()).filter(Boolean);
|
|
6994
|
+
const perPack = new Map<string, { versionBumped: boolean; sourceChanged: boolean }>();
|
|
6995
|
+
for (const rel of changed) {
|
|
6996
|
+
const m = rel.match(/^packages\/@dzhechkov\/([^/]+)\/(.+)$/);
|
|
6997
|
+
if (!m || !m[1] || !m[2]) continue;
|
|
6998
|
+
const e = perPack.get(m[1]) ?? { versionBumped: false, sourceChanged: false };
|
|
6999
|
+
if (m[2] === 'package.json') e.versionBumped = true;
|
|
7000
|
+
// SOURCE = what ships and can be wrong at runtime. Tests, docs and fixtures are excluded:
|
|
7001
|
+
// a test-only change still ships, but it is not the class the review gate is about, and
|
|
7002
|
+
// widening the scope is what makes a HARD gate get switched off.
|
|
7003
|
+
if (/^(src|lib|bin|skills)\//.test(m[2]) && !/\.(md|json|txt)$/.test(m[2])) e.sourceChanged = true;
|
|
7004
|
+
perPack.set(m[1], e);
|
|
7005
|
+
}
|
|
7006
|
+
const grades: { report: string; grade: string }[] = [];
|
|
7007
|
+
for (const rel of changed) {
|
|
7008
|
+
if (!/^features\/[^/]+\/08_qe_report\.md$/.test(rel)) continue;
|
|
7009
|
+
let text: string;
|
|
7010
|
+
try { text = readFileSync(join(root, rel), 'utf-8'); } catch { continue; }
|
|
7011
|
+
// The grade the report itself STATES — `grade C`, `GRADE: B`, `**grade: A**`. A report that
|
|
7012
|
+
// never states one is not evidence (AM-2), so nothing is pushed for it.
|
|
7013
|
+
const m = text.match(/\bgrade\s*:?\s*\**\s*([ABCDF])\b/i);
|
|
7014
|
+
if (m && m[1]) grades.push({ report: rel, grade: m[1] });
|
|
7015
|
+
}
|
|
7016
|
+
const cfgMin = loadGuardConfig(root).reviewRound?.minGrade;
|
|
7017
|
+
const minGrade = typeof cfgMin === 'string' ? cfgMin : undefined;
|
|
7018
|
+
facts['reviewRound'] = {
|
|
7019
|
+
packages: [...perPack.entries()].map(([name, e]) => ({ name: '@dzhechkov/' + name, ...e })),
|
|
7020
|
+
grades,
|
|
7021
|
+
gathered: true,
|
|
7022
|
+
...(minGrade !== undefined ? { minGrade } : {}),
|
|
7023
|
+
};
|
|
7024
|
+
} catch {
|
|
7025
|
+
// TRIED and could not read the tree. Say so on the record rather than passing silently: a HARD
|
|
7026
|
+
// gate that is quiet about ungathered evidence cannot be told from one that checked (raised by
|
|
7027
|
+
// cross-family review). It still does not BLOCK — ignorance is not an accusation.
|
|
7028
|
+
facts['reviewRound'] = { packages: [], grades: [], gathered: false };
|
|
7029
|
+
}
|
|
7030
|
+
|
|
6633
7031
|
// skills-registrable: every skill dir in a skill pack must carry a depth-1 SKILL.md, or it ships
|
|
6634
7032
|
// registering nowhere (the health-advisor 1.2.0 class). Pure-toolkit packages yield nothing.
|
|
6635
7033
|
try {
|
|
@@ -9009,6 +9407,76 @@ function scanOneReqeRoot(
|
|
|
9009
9407
|
* the exit code. A courier can neither refuse nor verify, which is how four workflow runs finished
|
|
9010
9408
|
* with no cost row at all.
|
|
9011
9409
|
*/
|
|
9410
|
+
/**
|
|
9411
|
+
* The file list of the tarball `npm pack` would produce, `package/` prefix stripped. npm is the
|
|
9412
|
+
* authority on what `files[]` ships; reimplementing its globbing would put a second, divergent answer
|
|
9413
|
+
* next to the real one — which is the class of defect this whole change exists to remove.
|
|
9414
|
+
*/
|
|
9415
|
+
/**
|
|
9416
|
+
* The npm name of a pack on disk — the identity a publisher, a registry and a consumer all use.
|
|
9417
|
+
* `undefined` when it cannot be read, so an unestablished identity is never asserted as a match.
|
|
9418
|
+
*/
|
|
9419
|
+
function packNpmName(packDir: string): string | undefined {
|
|
9420
|
+
try {
|
|
9421
|
+
const name = (JSON.parse(readFileSync(join(packDir, 'package.json'), 'utf8')) as { name?: unknown }).name;
|
|
9422
|
+
return typeof name === 'string' && name !== '' ? name : undefined;
|
|
9423
|
+
} catch {
|
|
9424
|
+
return undefined;
|
|
9425
|
+
}
|
|
9426
|
+
}
|
|
9427
|
+
|
|
9428
|
+
function npmPackedPaths(packDir: string): string[] {
|
|
9429
|
+
// `pnpm`, not `npm`: the PUBLISHER is `pnpm publish` (see `publishArgv`), and the two packers do not
|
|
9430
|
+
// agree. MEASURED 2026-08-21 on `skills-news`: `npm pack` emits a 1051-byte package.json identical
|
|
9431
|
+
// to the working tree, `pnpm pack` emits 1050 — pnpm re-serialises it (dropping the trailing
|
|
9432
|
+
// newline, and expanding `workspace:*`). Asking one tool what ships while a different tool ships it
|
|
9433
|
+
// is how a signature ends up describing a file nobody receives.
|
|
9434
|
+
const out = execFileSync('pnpm', ['pack', '--pack-destination', mkdtempSync(join(tmpdir(), 'dz-pack-probe-')), '--json'], {
|
|
9435
|
+
cwd: packDir,
|
|
9436
|
+
encoding: 'utf-8',
|
|
9437
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
9438
|
+
});
|
|
9439
|
+
const parsed = JSON.parse(out) as { files?: { path: string }[] } | { files?: { path: string }[] }[];
|
|
9440
|
+
const entry = Array.isArray(parsed) ? parsed[0] : parsed;
|
|
9441
|
+
const files = entry?.files ?? [];
|
|
9442
|
+
return files.map((f) => f.path.replace(/^package\//, '')).sort();
|
|
9443
|
+
}
|
|
9444
|
+
|
|
9445
|
+
/**
|
|
9446
|
+
* Pack the package with the SAME tool that publishes it, extract the tarball, and return the directory
|
|
9447
|
+
* holding its contents. Hashing THAT is the only way a manifest can describe what a recipient gets:
|
|
9448
|
+
* `pnpm publish` re-serialises package.json and rewrites `workspace:*`, so any hash taken from the
|
|
9449
|
+
* working tree is stale before the tarball exists.
|
|
9450
|
+
*/
|
|
9451
|
+
function extractPublishTarball(packDir: string): { dir: string; cleanup: () => void } {
|
|
9452
|
+
const tmp = mkdtempSync(join(tmpdir(), 'dz-sign-pack-'));
|
|
9453
|
+
try {
|
|
9454
|
+
return extractIntoTempDir(packDir, tmp);
|
|
9455
|
+
} catch (err) {
|
|
9456
|
+
// The caller never receives a cleanup for a throw, so the directory this function created must
|
|
9457
|
+
// be removed HERE or it leaks once per failed pack (cross-family review, 2026-08-22).
|
|
9458
|
+
try { rmSync(tmp, { recursive: true, force: true }); } catch { /* best-effort */ }
|
|
9459
|
+
throw err;
|
|
9460
|
+
}
|
|
9461
|
+
}
|
|
9462
|
+
|
|
9463
|
+
function extractIntoTempDir(packDir: string, tmp: string): { dir: string; cleanup: () => void } {
|
|
9464
|
+
const out = execFileSync('pnpm', ['pack', '--pack-destination', tmp, '--json'], {
|
|
9465
|
+
cwd: packDir,
|
|
9466
|
+
encoding: 'utf-8',
|
|
9467
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
9468
|
+
});
|
|
9469
|
+
const parsed = JSON.parse(out) as { filename?: string } | { filename?: string }[];
|
|
9470
|
+
const entry = Array.isArray(parsed) ? parsed[0] : parsed;
|
|
9471
|
+
const tgz = entry?.filename;
|
|
9472
|
+
if (tgz === undefined) throw new Error(`pnpm pack did not name a tarball for ${packDir}`);
|
|
9473
|
+
// pnpm reports an ABSOLUTE filename (it already contains --pack-destination); joining again would
|
|
9474
|
+
// double the directory. npm reports a bare name. Accept both rather than assuming either.
|
|
9475
|
+
const tgzPath = isAbsolute(tgz) ? tgz : join(tmp, tgz);
|
|
9476
|
+
execFileSync('tar', ['-xzf', tgzPath, '-C', tmp]);
|
|
9477
|
+
return { dir: join(tmp, 'package'), cleanup: (): void => { try { rmSync(tmp, { recursive: true, force: true }); } catch { /* best-effort */ } } };
|
|
9478
|
+
}
|
|
9479
|
+
|
|
9012
9480
|
function cmdFeatureAdrRecord(options: Map<string, string>, flags: Set<string>, cwd: string, write: Write): number {
|
|
9013
9481
|
const json = flags.has('json');
|
|
9014
9482
|
const kind = (options.get('kind') ?? '').trim() as RecordKind;
|