@pareto4/pp-core 1.12.1 → 1.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/agents/pp-code-reviewer.md +1 -1
  3. package/agents/pp-executor.md +2 -13
  4. package/agents/pp-planner.md +1 -1
  5. package/agents/pp-vault-reviewer.md +51 -0
  6. package/commands/pp-ns-review.md +2 -1
  7. package/commands/pp-vault-review.md +33 -0
  8. package/gemini-extension.json +1 -1
  9. package/package.json +1 -1
  10. package/pp-core/bin/lib/capability-registry.cjs +200 -10
  11. package/pp-core/bin/lib/check-command-router.cjs +1242 -37
  12. package/pp-core/bin/lib/clusters.cjs +1 -0
  13. package/pp-core/bin/lib/command-aliases.cjs +7 -0
  14. package/pp-core/bin/lib/compose-bindings.cjs +41 -27
  15. package/pp-core/bin/lib/decisions.cjs +104 -27
  16. package/pp-core/bin/lib/dispatch-plan.cjs +12 -1
  17. package/pp-core/bin/lib/frontmatter.cjs +37 -0
  18. package/pp-core/bin/lib/git-base-branch.cjs +136 -10
  19. package/pp-core/bin/lib/init-contract.cjs +180 -66
  20. package/pp-core/bin/lib/init-interactive.cjs +4 -20
  21. package/pp-core/bin/lib/loop-gates.cjs +213 -0
  22. package/pp-core/bin/lib/loop-resolver.cjs +107 -45
  23. package/pp-core/bin/lib/org-profile.cjs +218 -44
  24. package/pp-core/bin/lib/phase-command-router.cjs +7 -1
  25. package/pp-core/bin/lib/phase-manifest.cjs +527 -0
  26. package/pp-core/bin/lib/phase-tier.cjs +15 -2
  27. package/pp-core/bin/lib/phase.cjs +51 -1
  28. package/pp-core/bin/lib/vault-audit.cjs +2070 -0
  29. package/pp-core/bin/lib/vault-command-router.cjs +460 -1
  30. package/pp-core/bin/lib/vault-practices.cjs +1142 -104
  31. package/pp-core/bin/lib/vault-review.cjs +2359 -0
  32. package/pp-core/bin/lib/vault-trace.cjs +2831 -0
  33. package/pp-core/bin/lib/vault-update.cjs +266 -0
  34. package/pp-core/bin/lib/vault-write-back.cjs +19 -0
  35. package/pp-core/bin/lib/vault.cjs +277 -12
  36. package/pp-core/bin/pp-tools.cjs +30 -13
  37. package/pp-core/bin/shared/model-catalog.json +2 -1
  38. package/pp-core/references/executor-vault-trace.md +47 -0
  39. package/pp-core/references/loop-hook-dispatch.md +13 -2
  40. package/pp-core/references/planner-vault-practices.md +46 -1
  41. package/pp-core/references/planning-config.md +1 -1
  42. package/pp-core/templates/practices/README.md +140 -11
  43. package/pp-core/templates/project-charter.md +226 -0
  44. package/pp-core/workflows/add-tests.md +4 -0
  45. package/pp-core/workflows/audit-fix.md +2 -0
  46. package/pp-core/workflows/check-todos.md +1 -1
  47. package/pp-core/workflows/code-review-fix.md +2 -0
  48. package/pp-core/workflows/debug.md +4 -0
  49. package/pp-core/workflows/discovery-sprint/prefill.md +9 -0
  50. package/pp-core/workflows/discovery-sprint/prototype.md +2 -1
  51. package/pp-core/workflows/discovery-sprint/vault-fetch.md +37 -0
  52. package/pp-core/workflows/discovery-sprint.md +1 -1
  53. package/pp-core/workflows/discovery-to-build.md +1 -1
  54. package/pp-core/workflows/execute-phase/steps/serial-group-dispatch.md +1 -0
  55. package/pp-core/workflows/execute-phase/steps/vault-door-gate.md +19 -0
  56. package/pp-core/workflows/execute-phase/steps/vault-fetch.md +1 -1
  57. package/pp-core/workflows/execute-phase/steps/vault-load-gate.md +10 -8
  58. package/pp-core/workflows/execute-phase.md +22 -5
  59. package/pp-core/workflows/execute-plan.md +4 -0
  60. package/pp-core/workflows/fast.md +4 -0
  61. package/pp-core/workflows/help/modes/full.md +3 -2
  62. package/pp-core/workflows/init.md +2 -2
  63. package/pp-core/workflows/new-project.md +1 -1
  64. package/pp-core/workflows/plan-phase/steps/phase-tier.md +9 -0
  65. package/pp-core/workflows/plan-phase.md +7 -4
  66. package/pp-core/workflows/quick.md +4 -0
  67. package/pp-core/workflows/ship.md +5 -1
  68. package/pp-core/workflows/undo.md +1 -1
  69. package/pp-core/workflows/validate-phase.md +4 -0
  70. package/pp-core/workflows/vault-review.md +65 -0
  71. package/pp-core/workflows/verify-work.md +3 -0
  72. package/scripts/gen-capability-registry.cjs +11 -0
  73. package/scripts/lint-test-file-count.allowlist.json +10 -1
  74. package/scripts/prompt-injection-scan.sh +2 -0
@@ -9,18 +9,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
9
9
  *
10
10
  * Responsibility (Phase 24-01 — parse + discover half of the module):
11
11
  * `parsePracticeFile(filePath, raw)` validates a single practice `.md` file's
12
- * frontmatter (id/scope/severity/applies-to/schemaVersion) against the D-03/
13
- * D-06/D-08/D-14 rules from 24-CONTEXT.md, returning skip-with-warning
14
- * results — it NEVER throws. `discoverPracticeFiles(cwd)` lists `.md` files
12
+ * frontmatter (id/scope/severity/applies-to/schemaVersion/check) against the
13
+ * D-03/D-06/D-08/D-14 rules from 24-CONTEXT.md, returning parsed,
14
+ * DEGRADED-with-warnings, or skip-with-warning results (CHECK-02, 05-01) —
15
+ * it NEVER throws. `discoverPracticeFiles(cwd)` lists `.md` files
15
16
  * under the cached vault tier's `practices/` directory, excludes secret-named
16
17
  * files, and parses each survivor, degrading to an empty/benign result when
17
18
  * no vault cache is present.
18
19
  *
19
20
  * Responsibility (Phase 24-02 — matcher + global-bucket + formatter half):
20
- * `globMatch`/`fallbackGlobToRegex` provide deterministic glob matching,
21
- * feature-detecting `path.posix.matchesGlob` (Node >=22.5.0) with a hand-
22
- * rolled, linear-time regex fallback for the 22.0–22.4 gap (engines floor
23
- * is >=22.0.0). `matchPractices` partitions discovered practices into a
21
+ * `globMatch`/`fallbackGlobToRegex` provide deterministic glob matching
22
+ * through ONE hand-rolled, polynomial-time matcher on every runtime (the
23
+ * native `path.posix.matchesGlob` backtracks on many-star segments and is
24
+ * used only as the tests' oracle). `matchPractices` partitions discovered practices into a
24
25
  * glob-matched `matched` bucket and an always-surfaced `global` bucket (a
25
26
  * bounded, fenced excerpt per global practice — the matcher itself makes
26
27
  * ZERO calls to any hosted model or scoring service; relevance of globals
@@ -34,8 +35,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
34
35
  * - `./vault.cjs` (`usableFiles`, `isSecretFile`, `readManifest`,
35
36
  * `readHeadBounded`) — the existing vault-tier discovery/secret/manifest/
36
37
  * bounded-read helpers.
37
- * - `node:path`'s `path.posix.matchesGlob` (feature-detected) — no
38
- * `minimatch`/`picomatch` dependency (RESEARCH STACK.md).
38
+ * - no `minimatch`/`picomatch` dependency (RESEARCH STACK.md), and no
39
+ * native `path.posix.matchesGlob` on the matching path (see `globMatch`).
39
40
  *
40
41
  * Out of scope here (later plans): the CLI `practices match` router wiring
41
42
  * and cross-tier client-beats-shared id dedup (explicitly DEFERRED to
@@ -58,6 +59,14 @@ const node_fs_1 = __importDefault(require("node:fs"));
58
59
  const fmMod = require("./frontmatter.cjs");
59
60
  // eslint-disable-next-line @typescript-eslint/no-require-imports
60
61
  const vaultMod = require("./vault.cjs");
62
+ // FIX-3: the ONE version resolver — the same function and
63
+ // candidate ladder `project.createdWith` is written from — so the block's
64
+ // status stamp and the bindings can never name different engine versions.
65
+ // compose-bindings imports nothing from vault-practices, so no cycle.
66
+ const compose_bindings_cjs_1 = require("./compose-bindings.cjs");
67
+ // The one source/non-source predicate (plan graph). A leaf with no imports of
68
+ // its own, so no cycle.
69
+ const plan_graph_cjs_1 = require("./plan-graph.cjs");
61
70
  const { extractFrontmatter } = fmMod;
62
71
  // ─── Constants ─────────────────────────────────────────────────────────────────
63
72
  // DoS-guard cap (T-24-04) mirroring vault.cts's SUMMARY_FILE_CAP discipline —
@@ -68,9 +77,36 @@ const VALID_SEVERITIES = ['critical', 'warning', 'info'];
68
77
  const VALID_SCOPES = ['scoped', 'global'];
69
78
  const PRACTICE_SCHEMA_VERSION = 1;
70
79
  const PRACTICES_SUBDIR = 'practices/';
71
- // The exactly-five documented frontmatter keys (README "five frontmatter
72
- // keys" table) — any other key present is rejected at parse time (IN-05).
73
- const DOCUMENTED_FRONTMATTER_KEYS = new Set(['id', 'scope', 'severity', 'applies-to', 'schemaVersion']);
80
+ // The exactly-seven documented frontmatter keys — any other key present
81
+ // DEGRADES the practice at parse time (IN-05 as inverted by CHECK-02, 05-01:
82
+ // retained + warned, never dropped).
83
+ //
84
+ // `check` joined the set in 05-01 and `enforced-by` in 10-03 (MECH-03,
85
+ // D-10-04). Both are step 1 of the degrade-loudly rule's fixed rollout order (engine
86
+ // release → verified rollout → vault-repo merge): the engine admits the key
87
+ // first, the rollout is verified, and only then does the vault repo merge
88
+ // practices that use it. Neither addition bumped PRACTICE_SCHEMA_VERSION
89
+ // (D-10-17) — an optional key an older engine still retains-and-warns on is
90
+ // not a breaking schema change, and bumping would make every older engine
91
+ // shout about a practice it can read correctly.
92
+ //
93
+ // EXPORTED deliberately: this set has two consumers (`parsePracticeFile` and
94
+ // the promote preflight in src/vault-write-back.cts) and a conformance test
95
+ // that must DERIVE its iteration set from the shipped constant rather than
96
+ // hand-list it (T-4-42 — a hand-listed guard cannot notice the set changing
97
+ // underneath it). BOTH CONSUMERS MOVE IN THE ONE CHANGE THAT WIDENS THIS SET
98
+ // (T-4-49, restated by D-10-14): the promote preflight refuses on any non-empty
99
+ // `degradedWarnings`, so widening the reader alone would quietly make a
100
+ // previously-refused key promotable into the fleet-wide vault.
101
+ const DOCUMENTED_FRONTMATTER_KEYS = new Set([
102
+ 'id',
103
+ 'scope',
104
+ 'severity',
105
+ 'applies-to',
106
+ 'schemaVersion',
107
+ 'check',
108
+ 'enforced-by',
109
+ ]);
74
110
  // Glob-pattern length guard (T-24-04 DoS mitigation) — a client-authored
75
111
  // applies-to glob longer than this is rejected by fallbackGlobToRegex (never
76
112
  // matches) rather than compiled, bounding worst-case compile+match cost.
@@ -78,9 +114,22 @@ const GLOB_PATTERN_MAX = 200;
78
114
  // Bounded markdown-body excerpt size for scope:global practices (D-15) —
79
115
  // mirrors vault.cts's SUMMARY_READ_BYTES bounded-read discipline.
80
116
  const PRACTICE_EXCERPT_BYTES = 1024;
117
+ // Over-read factor for the excerpt's bounded read (FIX-1).
118
+ // The excerpt is normalised to LF before it is capped, and every carriage
119
+ // return that normalisation removes consumed a raw byte as half of a CRLF pair.
120
+ // Reading `PRACTICE_EXCERPT_BYTES * 2` body bytes therefore guarantees the cap
121
+ // can be filled from normalised text even when every line is CRLF-terminated.
122
+ // The read stays bounded — the whole file is still never read.
123
+ const EXCERPT_CRLF_OVERREAD_FACTOR = 2;
81
124
  // Matched-file list cap per practice (T-24-04) — matchedCount still carries
82
125
  // the true total so callers know the cap was hit.
83
126
  const MATCHED_FILES_CAP = 20;
127
+ // The unmatched-source-file note's candidate set (FIX-2, QD-2) is the plan
128
+ // graph's own source predicate (`isSourceFile`, src/plan-graph.cts) — the one
129
+ // owner of "is this a source file" — not a second list of extensions. When a
130
+ // source file matched no scoped practice the block says so in one plain line,
131
+ // because a vault laid out for a different folder structure otherwise drops its
132
+ // scoped practices with no word at all.
84
133
  // Bounded prefix read for practice files during DISCOVERY (CR-03) — frontmatter
85
134
  // + bodyOffset comfortably fit well inside this; the whole file is NEVER read.
86
135
  // Mirrors vault.cts's SUMMARY_READ_BYTES bounded-read discipline.
@@ -90,6 +139,188 @@ const PRACTICE_FILE_READ_BYTES = 8192;
90
139
  // bounds worst-case warning size and prevents multi-line injection into a
91
140
  // human/agent-visible report.
92
141
  const WARNING_TOKEN_MAX = 80;
142
+ // ─── Matching cost bound (CHECK-04 / T-5-10, T-5-11, 05-04) ──────────────────
143
+ //
144
+ // `matchPractices` walks (scoped practices × their globs × candidate files).
145
+ // The complexity class is polynomial and the pattern language compiles no
146
+ // `RegExp` — both true, and both irrelevant to wall clock.
147
+ //
148
+ // MEASURED, and REPRODUCIBLE by running tests/vault-practices.slow.test.cjs,
149
+ // on the six-axis worst-case corpus at the shipped caps (100 practices × 39
150
+ // globs derived from PRACTICE_FILE_READ_BYTES × a POSIX-maximal subject path
151
+ // = 3,900 pairs) — Apple M5 Pro, macOS 26.5.2, **Node v25.9.0**, 2026-09-01:
152
+ //
153
+ // native `path.posix.matchesGlob` 1.76 ms / (glob, file) pair
154
+ // pre-22.5 fallback matcher 0.098 ms / pair (same corpus, feature
155
+ // detect deleted before require)
156
+ //
157
+ // (The fallback matcher is now the only shipped engine — see `globMatch`.
158
+ // The native figure is kept as the measurement the caps below were sized on.)
159
+ //
160
+ // 05-RESEARCH § OQ1 measured 1.47 ms/pair native over three points that were
161
+ // linear in pair count, so `cost ≈ pairs × rate` is a measured model rather
162
+ // than a hoped-for one. Its 5.0 ms/pair fallback figure did NOT reproduce on
163
+ // this corpus; nothing here is derived from it (the slow suite records the
164
+ // discrepancy rather than dropping it).
165
+ //
166
+ // That makes the SHIPPED bound a multi-hour hang: 100 practices ×
167
+ // 39 globs × `SHIP_DIFF_FILE_CAP` (2000) = 7,800,000 pairs ⇒ ≈3.8 h at the
168
+ // native rate above (a projection from a measured rate, labelled as such).
169
+ // Both already-shipped call sites — `vault practices match` at plan:pre and
170
+ // `check ship.practices` at ship:pre — reach this function with no budget of
171
+ // any kind, which is why the bound lives HERE and not only in a later
172
+ // evaluator: a bound placed downstream would ship the gate and leave the hole.
173
+ //
174
+ // A number recorded without the machine it was measured on is the warning
175
+ // sign this project has learned to distrust, so every figure above names its
176
+ // runtime. Node-25 numbers are never recorded as Node-22 numbers; the Node-22
177
+ // confirmation is carried separately (05-09) and appends its own numbers to
178
+ // the slow suite's record.
179
+ // PRIMARY, DETERMINISTIC guard. Evaluated BEFORE the loop, so exhaustion is
180
+ // the same verdict on a laptop and on a CI runner (T-5-11 — a time-only budget
181
+ // makes the verdict machine-dependent, which is exactly the non-determinism
182
+ // VERIFY-03 bars from a deterministic gate).
183
+ //
184
+ // Sized above the legitimate ceiling and well below the hostile one: a
185
+ // measured production vault's 92 globs against the 2000-file ship diff cap is 184,000
186
+ // pairs, so this leaves ~5× headroom over any honest corpus while sitting
187
+ // ~7.8× below the 7,800,000-pair hostile saturation bound.
188
+ //
189
+ // BE CLEAR ABOUT WHAT THIS DOES AND DOES NOT BOUND. At the worst-case rate
190
+ // above, 1,000,000 pairs still extrapolates to ~1,760 s. This cap is NOT a
191
+ // wall-clock bound; it is the deterministic, machine-independent refusal of
192
+ // the pathological fan-out, and it is the arm a reproducible gate can rest on.
193
+ // Wall clock is bounded by MATCH_BUDGET_MS below.
194
+ const MATCH_PAIR_CAP = 1000000;
195
+ // SECONDARY guard, and the one that actually bounds wall clock. Machine-
196
+ // dependent by nature, which is precisely why the pair cap is primary and this
197
+ // one can only ever NARROW what the pair cap already permitted.
198
+ //
199
+ // Derivation: the real client vault's plain `**/…` globs cost microseconds per
200
+ // pair (05-RESEARCH § OQ2), so its 184,000-pair worst case runs in well under
201
+ // a second — this sits more than an order of magnitude above that legitimate
202
+ // ceiling, while turning the pathological corpus from hours into ten seconds.
203
+ const MATCH_BUDGET_MS = 10000;
204
+ // Every note this bound emits carries this marker, so a consumer channel can
205
+ // forward exactly those notes without re-composing the wording and without
206
+ // matching on prose (one emission, N channels — T-4-49).
207
+ const MATCH_BUDGET_NOTE_PREFIX = 'practice matching bounded: ';
208
+ /**
209
+ * The single sentence every executor-facing channel states when the client
210
+ * vault itself could not be READ (05-13, G-4).
211
+ *
212
+ * ONE constant, two channels, EXPORTED so their pins harvest it rather than
213
+ * copy it — the same discipline `AUDIT_VAULT_REPAIR_SENTENCE` follows in
214
+ * src/vault-audit.cts. A refusal whose wording drifts between the block a
215
+ * code-writing agent reads and the message an operator reads is how one fact
216
+ * ends up meaning two things (T-4-49), and this pair of channels is exactly
217
+ * where that already happened once.
218
+ *
219
+ * It names no path and no errno: the counted note beside it carries the
220
+ * diagnostic, already bounded to an errno code by discovery (T-5-36).
221
+ */
222
+ const PRACTICES_VAULT_UNREADABLE_SENTENCE = 'Practices were NOT checked — the client vault could not be read.';
223
+ /**
224
+ * The THIRD member of the family (CR-01), and the one a fresh checkout hits.
225
+ *
226
+ * `vaultPresent: false` and `readFailed: true` are different facts and have been
227
+ * since 05-10, but only `readFailed` was carried past the matcher. An absent
228
+ * vault therefore fell through to `practicesProcessed === 0` and was answered
229
+ * with "none found in the vault" — a statement about a vault that WAS read,
230
+ * made about one that was never fetched.
231
+ *
232
+ * `.planning/` is gitignored on this fork, so "never fetched" is the ordinary
233
+ * state of a fresh clone rather than an exotic one. Told the client documented
234
+ * no standards, a planner writes a plan governed by nothing, and `check
235
+ * vault.audit` then halts the build for the vault that was never there.
236
+ *
237
+ * Names the remedy, because unlike an unreadable vault this one has a single
238
+ * obvious repair. Names no path: the same T-5-36 discipline as its sibling.
239
+ */
240
+ const PRACTICES_VAULT_ABSENT_SENTENCE = 'Practices were NOT checked — no client vault has been fetched. Run the vault fetch first.';
241
+ /**
242
+ * The FOURTH member of the family (FIX-4, QD-5).
243
+ *
244
+ * A fetch that failed every tier with no prior copy leaves a manifest recording
245
+ * a miss: no tier, no sha, no files. The vault directory is present, nothing
246
+ * failed to READ, and discovery yields zero practices — so the ladder fell
247
+ * through to "none found in the vault", telling a code-writing agent its client
248
+ * documented no standards about a fetch that reached nothing.
249
+ *
250
+ * The documented "could not be checked: <reason>." form, with the reason
251
+ * phrase copied from the practice trace's own miss reason (`resolvePin` in
252
+ * src/vault-trace.cts) so the two surfaces say the same thing.
253
+ */
254
+ const PRACTICES_VAULT_MISS_SENTENCE = 'Practices could not be checked: the vault manifest records a miss — no vault content was reached.';
255
+ /**
256
+ * The status stamp's fixed opening (FIX-3, QD-4).
257
+ *
258
+ * Every rendered `<vault_practices>` block carries ONE HTML comment as its
259
+ * first inner line, written by this module, naming the vault ref, short sha,
260
+ * either the number of fenced practice entries or a degraded-state token, and
261
+ * the engine version. A distinct prefix — not `vault:external-content` — so no
262
+ * fence parser can mistake it for a marker, and it never begins with `id:`.
263
+ */
264
+ const PRACTICES_STAMP_PREFIX = '<!-- vault-practices-stamp:';
265
+ /**
266
+ * The stamp's degraded-state vocabulary: exactly one token per sentence the
267
+ * block can state instead of rendering entries (FIX-3). A new rung cannot ship
268
+ * without a token — the stamp test harvests this map and pins its size.
269
+ */
270
+ const PRACTICES_STAMP_STATES = Object.freeze({
271
+ COULD_NOT_BE_CHECKED: 'could-not-be-checked',
272
+ NOT_CHECKED_PAIR_CAP: 'not-checked-pair-cap',
273
+ VAULT_UNREADABLE: 'vault-unreadable',
274
+ VAULT_ABSENT: 'vault-absent',
275
+ VAULT_MISS: 'vault-miss',
276
+ ALL_REFUSED: 'all-refused',
277
+ NONE_FOUND: 'none-found',
278
+ NONE_APPLY: 'none-apply',
279
+ });
280
+ // ─── check: block caps (T-5-05, CHECK-02 / D-05-05) ──────────────────────────
281
+ //
282
+ // Each cap below copies the PRACTICE_COUNT_CAP discipline exactly: it bounds a
283
+ // client-authored fan-out BEFORE any work is done with it, and exhaustion is a
284
+ // COUNTED REFUSAL — the block is dropped whole and `checkInvalid` says so —
285
+ // never a silent truncation that would leave a check quietly asserting less
286
+ // than its author wrote.
287
+ // Max path globs one check block may declare. The largest applies-to fan-out
288
+ // observed in the real client vault is 4; this doubles it, so a legitimate
289
+ // block never meets the cap and a padded one is refused before matching.
290
+ const CHECK_FILES_CAP = 8;
291
+ // Max literal content tokens across `require` and `forbid` COMBINED. Bounds the
292
+ // per-file substring work the evaluator does, which is the product of this and
293
+ // the matched-file count.
294
+ const CHECK_TOKEN_COUNT_CAP = 16;
295
+ // Max length (chars) of a single literal content token. Mirrors
296
+ // GLOB_PATTERN_MAX deliberately: the two assertion kinds bound their
297
+ // client-authored payload at the same size, so neither becomes the cheap way
298
+ // to smuggle a large one past the other.
299
+ const CHECK_TOKEN_MAX = 200;
300
+ // ─── enforced-by: cap (T-10-11, MECH-03 / D-10-04) ───────────────────────────
301
+ //
302
+ // Max analyzer rules one practice may name. Set to 8 to MATCH CHECK_FILES_CAP —
303
+ // the number is chosen to match rather than invented, because there is no
304
+ // observed fan-out to double here (the key is new and the client vault carries
305
+ // zero occurrences of it) and a second, differently-sized bound on the same
306
+ // file would be a number a reader has to justify twice.
307
+ //
308
+ // The treatment DIFFERS from the check caps above and the difference is
309
+ // deliberate: a check cap is a counted REFUSAL because truncating an assertion
310
+ // would leave it quietly asserting less than its author wrote. `enforced-by:`
311
+ // asserts nothing and governs nothing — it is provenance data — so exhaustion
312
+ // TRUNCATES to the first ENFORCED_BY_COUNT_CAP entries and STATES the cap in
313
+ // one degraded warning. Nothing is lost silently: the warning is loud on the
314
+ // read side and, because the promote preflight refuses on any degraded parse,
315
+ // an over-cap practice cannot be promoted at all.
316
+ //
317
+ // EXPORTED for the same reason the check caps are: tests HARVEST the shipped
318
+ // value and size their boundary fixtures from it, never copying the number.
319
+ const ENFORCED_BY_COUNT_CAP = 8;
320
+ // The exactly-four keys a `check:` mapping may carry. Anything else invalidates
321
+ // the block — the same closed-vocabulary posture the frontmatter allowlist
322
+ // takes, for the same reason (a typo must be loud, never silently ignored).
323
+ const CHECK_BLOCK_KEYS = new Set(['files', 'require', 'forbid', 'forbidMatch']);
93
324
  /**
94
325
  * Safe stringify for warning messages — `extractFrontmatter` can yield
95
326
  * string | string[] | Record<string, unknown> for any key, and a bare
@@ -127,12 +358,6 @@ function boundedToken(v) {
127
358
  const s = safeStringify(v).replace(/[\r\n]+/g, ' ');
128
359
  return s.length > WARNING_TOKEN_MAX ? `${s.slice(0, WARNING_TOKEN_MAX)}…` : s;
129
360
  }
130
- // ─── Glob matching (PRAC-02) ───────────────────────────────────────────────────
131
- // Feature-detect once at module scope: `path.posix.matchesGlob` landed in
132
- // Node 22.5.0 and stabilized in 22.20.0 (RESEARCH STACK.md), but this repo's
133
- // engines floor is >=22.0.0 — the 22.0–22.4 gap must fall back to the
134
- // hand-rolled matcher below rather than throwing `TypeError: not a function`.
135
- const hasMatchesGlob = typeof node_path_1.default.posix?.matchesGlob === 'function';
136
361
  const NEVER_MATCHES = { test: () => false };
137
362
  // Bounds the total number of concrete patterns a `{a,b,c}` expansion can
138
363
  // produce (T-24-04) — client-authored globs never legitimately need more.
@@ -270,6 +495,15 @@ function globSegmentsMatch(patternSegs, subjectSegs) {
270
495
  * two-pointer algorithm (`globSegmentsMatch` + `segmentMatches`) — a
271
496
  * `.test()`-shaped duck type keeps `globMatch`'s call site unchanged.
272
497
  * Patterns over `GLOB_PATTERN_MAX` are rejected outright (never matches).
498
+ *
499
+ * ON THE NAME (05-04). Despite it, this function constructs NO `RegExp` — it
500
+ * returns an object literal whose `test` runs the two-pointer matcher above.
501
+ * the declarative-data rule says the same thing ("despite its name") and the confusion has
502
+ * now cost more than one verification pass, so it is stated here at the
503
+ * definition rather than only in the ADR. A rename to `compileGlobMatcher`
504
+ * was considered and REJECTED: the rename guard's 2331-file baseline is not
505
+ * re-cut for a readability win, and this note delivers the same correction at
506
+ * zero churn. Recorded so the option is not silently re-opened.
273
507
  */
274
508
  function fallbackGlobToRegex(pattern) {
275
509
  if (typeof pattern !== 'string' || pattern.length === 0 || pattern.length > GLOB_PATTERN_MAX) {
@@ -287,23 +521,18 @@ function fallbackGlobToRegex(pattern) {
287
521
  }
288
522
  /**
289
523
  * True when `filePath` matches `pattern`. Normalizes `\` → `/` (Windows path
290
- * input) before matching. Uses `path.posix.matchesGlob` when the runtime
291
- * supports it (feature-detected once at module scope), else the linear/
292
- * polynomial-time `fallbackGlobToRegex` matcher. Deterministic: identical
293
- * inputs always yield the identical boolean (PRAC-02).
524
+ * input) before matching, then runs the polynomial-time `fallbackGlobToRegex`
525
+ * matcher on every runtime (see the note above `CompiledGlob` for why the
526
+ * native engine is not used). Deterministic: identical inputs always yield the
527
+ * identical boolean (PRAC-02).
294
528
  */
295
529
  function globMatch(filePath, pattern) {
296
- // Pre-dispatch length guard (T-24-04, gap #1 verifier ask b): apply
297
- // GLOB_PATTERN_MAX BEFORE choosing an engine so the cap holds for the
298
- // native `path.posix.matchesGlob` path too, not only the fallback
299
- // compiler's internal check.
530
+ // Length guard (T-24-04): GLOB_PATTERN_MAX holds here as well as inside the
531
+ // compiler, so an over-long pattern never matches on any path.
300
532
  if (typeof pattern !== 'string' || pattern.length === 0 || pattern.length > GLOB_PATTERN_MAX) {
301
533
  return false;
302
534
  }
303
535
  const normalized = filePath.replace(/\\/g, '/');
304
- if (hasMatchesGlob) {
305
- return node_path_1.default.posix.matchesGlob(normalized, pattern);
306
- }
307
536
  return fallbackGlobToRegex(pattern).test(normalized);
308
537
  }
309
538
  // Unsupported glob metacharacters (gap #1 closure option a, D-14/D-07):
@@ -380,6 +609,174 @@ function isSupportedGlob(pattern) {
380
609
  return false;
381
610
  return bracesSupported(pattern);
382
611
  }
612
+ /**
613
+ * Normalize a frontmatter value that must be a list of non-empty strings.
614
+ * `extractFrontmatter` yields `string | string[] | Record<string, unknown>` for
615
+ * any key; a lone scalar is normalized to a one-element list exactly as
616
+ * `applies-to` already is, so both assertion kinds read the same way for an
617
+ * author. Returns `null` when the value is present but is not a usable string
618
+ * list — the caller turns that into a stated defect, never a silent drop.
619
+ */
620
+ function normalizeStringList(v) {
621
+ if (Array.isArray(v)) {
622
+ return v.every((e) => typeof e === 'string' && e.length > 0) ? v : null;
623
+ }
624
+ if (typeof v === 'string' && v.trim())
625
+ return [v.trim()];
626
+ return null;
627
+ }
628
+ /**
629
+ * Normalize a frontmatter boolean. The shared frontmatter parser is line-based
630
+ * and yields the STRING `'true'`, not a boolean, so both forms are accepted and
631
+ * anything else is a stated defect (a `forbidMatch: yes` must not silently read
632
+ * as false).
633
+ */
634
+ function normalizeCheckBoolean(v) {
635
+ if (typeof v === 'boolean')
636
+ return v;
637
+ if (v === 'true')
638
+ return true;
639
+ if (v === 'false')
640
+ return false;
641
+ return null;
642
+ }
643
+ /**
644
+ * Parse and validate a practice's `check:` block. THE ONLY parser for this
645
+ * shape (the one-parser rule): 05-05's evaluator receives a validated record and
646
+ * validates nothing itself, so a rule that is not enforced here is not
647
+ * enforced anywhere.
648
+ *
649
+ * A violation never skips the practice. The block is dropped whole — never
650
+ * half-built — and one bounded, single-line `checkInvalid` records the defect,
651
+ * because the practice's prose still applies (the degrade-loudly rule). 05-05 maps
652
+ * `checkInvalid` to a fail-closed FAIL so a broken block can never read as a
653
+ * pass, nor be mistaken for "this practice was never checkable".
654
+ *
655
+ * The glob half is gated by `isSupportedGlob` BY REFERENCE — the grammar is
656
+ * never restated here and there is never a second parser, which is what stops
657
+ * the evaluator and the reader drifting apart. The literal half is bounded by
658
+ * CHECK_TOKEN_COUNT_CAP and CHECK_TOKEN_MAX, and is compared only by plain
659
+ * substring containment: no client-authored byte reaches a RegExp constructor
660
+ * or any other pattern compiler (the declarative-data rule, T-5-05).
661
+ */
662
+ function parseCheckBlock(id, rawCheck) {
663
+ const invalid = (defect) => ({
664
+ checkInvalid: `check block on practice "${boundedToken(id)}" was dropped: ${defect}. ` +
665
+ `The practice itself still applies; fix the block to make it mechanically checkable again.`,
666
+ });
667
+ if (typeof rawCheck !== 'object' || rawCheck === null || Array.isArray(rawCheck)) {
668
+ return invalid('check must be a mapping of files/require/forbid/forbidMatch');
669
+ }
670
+ const block = rawCheck;
671
+ const unknownKey = Object.keys(block).find((k) => !CHECK_BLOCK_KEYS.has(k));
672
+ if (unknownKey !== undefined) {
673
+ return invalid(`unrecognized key "${boundedToken(unknownKey)}" inside check ` +
674
+ `(expected only: ${Array.from(CHECK_BLOCK_KEYS).join(', ')})`);
675
+ }
676
+ const files = normalizeStringList(block.files);
677
+ if (files === null || files.length === 0) {
678
+ return invalid('files is required and must be a non-empty list of path globs');
679
+ }
680
+ // Count-before-work: refuse an over-wide fan-out BEFORE any glob is examined.
681
+ if (files.length > CHECK_FILES_CAP) {
682
+ return invalid(`files declares ${files.length} globs, over the cap of ${CHECK_FILES_CAP}`);
683
+ }
684
+ const unsupportedGlob = files.find((g) => !isSupportedGlob(g));
685
+ if (unsupportedGlob !== undefined) {
686
+ return invalid(`files glob "${boundedToken(unsupportedGlob)}" uses unsupported syntax ` +
687
+ `(see the practices README "Supported glob syntax" table)`);
688
+ }
689
+ // `globMatch` already refuses an over-long pattern by never matching. Refusing
690
+ // it here too turns a silent never-matches into a stated defect, so an author
691
+ // is told rather than left with a check that looks live and is not.
692
+ if (files.some((g) => g.length > GLOB_PATTERN_MAX)) {
693
+ return invalid(`a files glob is longer than the ${GLOB_PATTERN_MAX}-character pattern cap`);
694
+ }
695
+ const requireTokens = block.require === undefined ? undefined : normalizeStringList(block.require);
696
+ if (requireTokens !== undefined && (requireTokens === null || requireTokens.length === 0)) {
697
+ return invalid('require must be a non-empty list of literal content tokens');
698
+ }
699
+ const forbidTokens = block.forbid === undefined ? undefined : normalizeStringList(block.forbid);
700
+ if (forbidTokens !== undefined && (forbidTokens === null || forbidTokens.length === 0)) {
701
+ return invalid('forbid must be a non-empty list of literal content tokens');
702
+ }
703
+ const forbidMatch = block.forbidMatch === undefined ? undefined : normalizeCheckBoolean(block.forbidMatch);
704
+ if (block.forbidMatch !== undefined && forbidMatch === null) {
705
+ return invalid('forbidMatch must be true or false');
706
+ }
707
+ if (forbidMatch !== undefined && forbidMatch !== null && (requireTokens || forbidTokens)) {
708
+ return invalid('forbidMatch cannot combine with require or forbid — a forbidden match has no content to inspect');
709
+ }
710
+ if (requireTokens === undefined && forbidTokens === undefined && forbidMatch === undefined) {
711
+ return invalid('a check block must declare at least one of require, forbid or forbidMatch');
712
+ }
713
+ const tokens = [...(requireTokens ?? []), ...(forbidTokens ?? [])];
714
+ if (tokens.length > CHECK_TOKEN_COUNT_CAP) {
715
+ return invalid(`require and forbid declare ${tokens.length} tokens combined, over the cap of ${CHECK_TOKEN_COUNT_CAP}`);
716
+ }
717
+ // The offending token is deliberately NOT echoed: it is client-authored and
718
+ // over-long by definition, and the cap is what the author needs to be told.
719
+ if (tokens.some((t) => t.length > CHECK_TOKEN_MAX)) {
720
+ return invalid(`a content token is longer than the ${CHECK_TOKEN_MAX}-character cap`);
721
+ }
722
+ const check = { files };
723
+ if (requireTokens !== undefined && requireTokens !== null)
724
+ check.require = requireTokens;
725
+ if (forbidTokens !== undefined && forbidTokens !== null)
726
+ check.forbid = forbidTokens;
727
+ if (forbidMatch !== undefined && forbidMatch !== null)
728
+ check.forbidMatch = forbidMatch;
729
+ return { check };
730
+ }
731
+ /**
732
+ * Read a practice's `enforced-by:` value. Never throws; never drops a practice.
733
+ *
734
+ * `enforced-by:` names stock analyzer rules whose authors already enforce this
735
+ * practice. pp-core RECORDS the claim and runs nothing (D-10-05) — so this
736
+ * parser's whole job is to turn a client-authored value into a bounded, capped
737
+ * list of tokens, or into a stated defect.
738
+ *
739
+ * Both authored shapes are accepted through the shipped `normalizeStringList`,
740
+ * which turns a lone scalar into a one-element list exactly as `applies-to` is
741
+ * handled, and returns `null` when the value is present but not a usable string
742
+ * list. That `null` becomes a STATED defect here — never a silent drop, which
743
+ * would let a typo'd value read as "this practice names no analyzer".
744
+ *
745
+ * @param safePath The already-bounded path prefix every warning interpolates.
746
+ * @param rawValue The frontmatter value; callers pass only a defined value.
747
+ */
748
+ function parseEnforcedBy(safePath, rawValue) {
749
+ const entries = normalizeStringList(rawValue);
750
+ if (entries === null || entries.length === 0) {
751
+ // The offending value is deliberately NOT echoed: it is client-authored and
752
+ // by definition not a string list, so what the author needs to be told is
753
+ // the shape, not their own bytes back.
754
+ return {
755
+ warning: `${safePath} degraded: enforced-by is present but unusable — ` +
756
+ `expected an analyzer rule name or a list of them (e.g. "enforced-by: VSTHRD002") — ` +
757
+ `the practice still applies; the enforced-by claim was dropped (D-10-04)`,
758
+ };
759
+ }
760
+ // Count-before-work, and ONE warning whose length is independent of the
761
+ // input (T-10-11): the entries themselves are never enumerated into it, so a
762
+ // giant list cannot grow a human/agent-visible line with attacker-chosen
763
+ // bytes. The PRACTICE_FILE_READ_BYTES bounded prefix read stays the primary
764
+ // bound on how much of that input is ever seen at all.
765
+ const overCap = entries.length > ENFORCED_BY_COUNT_CAP;
766
+ const kept = overCap ? entries.slice(0, ENFORCED_BY_COUNT_CAP) : entries;
767
+ // boundedToken on EVERY retained entry (T-10-10 / T-24-07): the value reaches
768
+ // an agent-visible surface in 10-06 and 10-08, and a CR/LF in it would forge a
769
+ // line there. Bounding at parse time means no renderer has to remember to.
770
+ const enforcedBy = kept.map((e) => boundedToken(e));
771
+ return overCap
772
+ ? {
773
+ enforcedBy,
774
+ warning: `${safePath} degraded: enforced-by names ${entries.length} rules, over the cap of ` +
775
+ `${ENFORCED_BY_COUNT_CAP} — the practice still applies and the first ` +
776
+ `${ENFORCED_BY_COUNT_CAP} are recorded; the rest were dropped (D-10-04)`,
777
+ }
778
+ : { enforcedBy };
779
+ }
383
780
  // ─── Parsing + validation ───────────────────────────────────────────────────────
384
781
  /**
385
782
  * Parse and validate a single practice file's frontmatter. Never throws —
@@ -387,14 +784,24 @@ function isSupportedGlob(pattern) {
387
784
  * never-throws discipline modeled by `vaultSummary`/`readHeadBounded` in
388
785
  * src/vault.cts).
389
786
  *
390
- * Validation order (each rule its own warning string, D-03/D-06/D-08/D-14):
391
- * 1. null byte in filePath → invalid path (path-traversal guard)
392
- * 2. missing string `id` in frontmatter
393
- * 3. `id` must equal the file's basename (sans `.md`)
394
- * 4. `severity` must be one of VALID_SEVERITIES (case-insensitive, normalized)
395
- * 5. `scope` must be exactly 'scoped' or 'global'
396
- * 6. scope↔applies-to consistency (scoped needs non-empty globs; global needs none)
397
- * 7. `schemaVersion`: absent → 1; a value > 1 is "authored for a newer pp-core"
787
+ * Validation order (each rule its own warning string, D-03/D-06/D-08/D-14).
788
+ * The order is LOAD-BEARING: the two degrade rules must not short-circuit any
789
+ * hard rule below them, or a practice with an unrecognized key would bypass the
790
+ * id/basename check that neutralizes the CRLF-poisoned-id shape (T-5-02).
791
+ * 1. null byte in filePath → SKIP (path-traversal guard)
792
+ * 2. a frontmatter key outside DOCUMENTED_FRONTMATTER_KEYS → DEGRADE (IN-05)
793
+ * 3. missing string `id` in frontmatter → SKIP
794
+ * 4. `id` must equal the file's basename (sans `.md`) → SKIP
795
+ * 5. `severity` must be one of VALID_SEVERITIES (case-insensitive, normalized) → SKIP
796
+ * 6. `scope` must be exactly 'scoped' or 'global' → SKIP
797
+ * 7. scope↔applies-to consistency (scoped needs non-empty globs; global needs none) → SKIP
798
+ * 8. every applies-to glob must pass `isSupportedGlob` → SKIP
799
+ * 9. `schemaVersion`: absent → 1; malformed (`< 1` / non-integer) → SKIP;
800
+ * a well-formed value > 1 → DEGRADE ("authored for a newer pp-core")
801
+ * 10. `check:` and `enforced-by:` are read LAST, below every hard rule above.
802
+ * Neither can skip a practice: an unusable value is stated (on
803
+ * `checkInvalid` and on `degradedWarnings` respectively) and the prose
804
+ * still applies.
398
805
  *
399
806
  * @param filePath Repo-relative (or synthetic, in unit tests) path — used only
400
807
  * for the id/basename check and to stamp `PracticeRecord.filePath`. Never
@@ -413,18 +820,40 @@ function parsePracticeFile(filePath, raw) {
413
820
  return { ok: false, warning: `${safePath} skipped: invalid path (contains null byte)` };
414
821
  }
415
822
  const fm = extractFrontmatter(raw);
416
- // Unknown-key rejection (IN-05): a practice frontmatter carrying a key
417
- // outside the documented five is skipped-with-warning rather than
418
- // silently ignored — this both catches author typos (a misspelled
419
- // `applies_to`) and closes off a giant-frontmatter DoS surface (T-24-04)
420
- // where extra keys could otherwise be padded arbitrarily.
823
+ // Collects the warnings that DEGRADE this practice without discarding it.
824
+ // Populated below; attached to the success result at the very end, so a
825
+ // practice that later fails a hard rule discards these along with itself.
826
+ const degradedWarnings = [];
827
+ // Unknown-key DEGRADE (IN-05, inverted by CHECK-02 in 05-01).
828
+ //
829
+ // This branch used to `return { ok: false }`, dropping the whole practice.
830
+ // the degrade-loudly rule requires the opposite: an engine that meets a key it does
831
+ // not recognize must degrade LOUDLY — retain the practice, apply its prose
832
+ // through the normal path, and warn naming the key. Dropping it meant the
833
+ // practice silently governed nothing, which is the "governance check that
834
+ // quietly does nothing" the ADR calls worse than no check at all.
835
+ //
836
+ // The typo case (a misspelled `applies_to`) takes this SAME path
837
+ // deliberately. A typo'd key silently dropping the whole practice is
838
+ // exactly the governance loss CHECK-02 removes; the warning is what makes
839
+ // the typo loud, and the practice's prose is still worth applying.
840
+ //
841
+ // Only the FIRST unknown key is named (T-5-03). Naming every key would
842
+ // re-open the giant-frontmatter surface the original IN-05 comment cites
843
+ // (T-24-04) by making the warning grow with attacker-chosen input; the
844
+ // PRACTICE_FILE_READ_BYTES bounded prefix read stays the primary bound.
845
+ //
846
+ // VALIDATION ORDER IS LOAD-BEARING: this degrade must not short-circuit
847
+ // anything below it. id-equals-basename, the severity and scope
848
+ // vocabularies, the scope↔applies-to rules, glob support and a malformed
849
+ // schemaVersion all still hard-skip exactly as before — which is what keeps
850
+ // the CRLF-poisoned-id shape neutralized (T-5-02) now that IN-05 no longer
851
+ // discards it.
421
852
  const unexpectedKey = Object.keys(fm).find((k) => !DOCUMENTED_FRONTMATTER_KEYS.has(k));
422
853
  if (unexpectedKey !== undefined) {
423
- return {
424
- ok: false,
425
- warning: `${safePath} skipped: unexpected frontmatter key "${boundedToken(unexpectedKey)}" ` +
426
- `(expected only: ${Array.from(DOCUMENTED_FRONTMATTER_KEYS).join(', ')}) (IN-05)`,
427
- };
854
+ degradedWarnings.push(`${safePath} degraded: unexpected frontmatter key "${boundedToken(unexpectedKey)}" ` +
855
+ `(expected only: ${Array.from(DOCUMENTED_FRONTMATTER_KEYS).join(', ')}) — ` +
856
+ `the practice still applies; this key was not understood (IN-05)`);
428
857
  }
429
858
  const id = typeof fm.id === 'string' && fm.id.trim() ? fm.id.trim() : null;
430
859
  if (!id) {
@@ -506,31 +935,57 @@ function parsePracticeFile(filePath, raw) {
506
935
  warning: `${safePath} skipped: malformed schemaVersion "${boundedToken(schemaVersionRaw)}"`,
507
936
  };
508
937
  }
938
+ // Forward-compatibility DEGRADE (D-08, inverted by CHECK-02 in 05-01).
939
+ // Note the asymmetry with the `< 1` / non-integer arm just above, which
940
+ // stays a HARD skip: a malformed value is not a forward-compat signal,
941
+ // it is a broken file. Only a well-formed version this engine is too old
942
+ // to understand degrades — that is the case the degrade-loudly rule's rollout order
943
+ // exists to make legible rather than silent.
509
944
  if (parsed > PRACTICE_SCHEMA_VERSION) {
510
- return {
511
- ok: false,
512
- warning: `${safePath} skipped: schemaVersion ${parsed} is newer than this pp-core supports ` +
513
- `(max ${PRACTICE_SCHEMA_VERSION}) — authored for a newer pp-core (D-08)`,
514
- };
945
+ degradedWarnings.push(`${safePath} degraded: schemaVersion ${parsed} is newer than this pp-core supports ` +
946
+ `(max ${PRACTICE_SCHEMA_VERSION}) — authored for a newer pp-core (D-08)`);
515
947
  }
516
948
  schemaVersion = parsed;
517
949
  }
518
950
  // bodyOffset: byte index right after the closing `---` frontmatter fence.
519
951
  const fmMatch = raw.match(/^---\r?\n[\s\S]+?\r?\n---/);
520
952
  const bodyOffset = fmMatch ? fmMatch[0].length : 0;
521
- return {
522
- ok: true,
523
- practice: {
524
- id,
525
- scope,
526
- severity,
527
- appliesTo,
528
- schemaVersion,
529
- tier: null, // stamped by discoverPracticeFiles from the manifest
530
- filePath,
531
- bodyOffset,
532
- },
953
+ // The check: block is validated HERE, alongside every other parse-time
954
+ // rule, so there is exactly one parser for it (the one-parser rule). An invalid
955
+ // block never skips the practice — see `parseCheckBlock`.
956
+ const checkOutcome = fm.check === undefined ? null : parseCheckBlock(id, fm.check);
957
+ // `enforced-by:` is read HERE, in the position parseCheckBlock occupies —
958
+ // BELOW every hard-skip rule above (10-03, MECH-03). The placement is the
959
+ // same load-bearing constraint the validation-order comment states: this
960
+ // key's own validation must not short-circuit id-equals-basename, the
961
+ // severity and scope vocabularies, the scope↔applies-to rules, glob support
962
+ // or a malformed schemaVersion, all of which still hard-skip exactly as
963
+ // before.
964
+ const enforcedByRaw = fm['enforced-by'];
965
+ const enforcedByOutcome = enforcedByRaw === undefined ? null : parseEnforcedBy(safePath, enforcedByRaw);
966
+ if (enforcedByOutcome?.warning !== undefined)
967
+ degradedWarnings.push(enforcedByOutcome.warning);
968
+ const practice = {
969
+ id,
970
+ scope,
971
+ severity,
972
+ appliesTo,
973
+ schemaVersion,
974
+ tier: null, // stamped by discoverPracticeFiles from the manifest
975
+ filePath,
976
+ bodyOffset,
533
977
  };
978
+ if (checkOutcome !== null && 'check' in checkOutcome)
979
+ practice.check = checkOutcome.check;
980
+ if (checkOutcome !== null && 'checkInvalid' in checkOutcome)
981
+ practice.checkInvalid = checkOutcome.checkInvalid;
982
+ if (enforcedByOutcome?.enforcedBy !== undefined)
983
+ practice.enforcedBy = enforcedByOutcome.enforcedBy;
984
+ // The degraded warnings ride on the SUCCESS result — the practice is
985
+ // returned, its prose applies, and the caller decides how to surface the
986
+ // warning. The key is omitted entirely when there is nothing to say, so a
987
+ // healthy practice's result shape is unchanged from before this plan.
988
+ return degradedWarnings.length > 0 ? { ok: true, practice, degradedWarnings } : { ok: true, practice };
534
989
  }
535
990
  catch (e) {
536
991
  return {
@@ -540,6 +995,49 @@ function parsePracticeFile(filePath, raw) {
540
995
  }
541
996
  }
542
997
  // ─── Discovery ───────────────────────────────────────────────────────────────────
998
+ /**
999
+ * The ERRNO code of a caught filesystem error, never its message.
1000
+ *
1001
+ * A Node fs error message interpolates the absolute path it failed on
1002
+ * (`EACCES: permission denied, scandir '/home/u/proj/.planning/vault/practices'`).
1003
+ * 05-10 routes discovery's `notes[]` into the audit's `message`, which
1004
+ * `check-query-gate.md` prints verbatim into an orchestrating agent's context
1005
+ * on EVERY run including passes — so these notes reach an agent-read channel
1006
+ * for the first time. CLAUDE.md § Logging forbids surfacing user file paths
1007
+ * unredacted and records that there is no shared redactor: a call site that
1008
+ * would surface one redacts locally. The errno is the whole diagnostic value
1009
+ * of the message anyway; the path is the part the reader already knows.
1010
+ */
1011
+ function errnoOf(e) {
1012
+ const code = e?.code;
1013
+ return typeof code === 'string' && code.length > 0 ? boundedToken(code) : 'unknown error';
1014
+ }
1015
+ /**
1016
+ * Probe the `practices/` directory's own readability, reporting its refusal as
1017
+ * a BOOLEAN alongside the note (T-24-08a; typed for 05-10).
1018
+ *
1019
+ * The probe runs BEFORE `usableFiles()` because that function's internal
1020
+ * `walk()` (src/vault.cts) catches a `readdirSync` failure itself and returns
1021
+ * an empty list with no throw — so wrapping the `usableFiles` call in a
1022
+ * try/catch could never detect an unreadable directory (it would look
1023
+ * identical to a genuinely empty vault).
1024
+ *
1025
+ * An ABSENT `practices/` directory is a genuinely empty vault and stays silent
1026
+ * (`failed: false`, no note). A failure does NOT short-circuit discovery: any
1027
+ * practices that ARE independently readable still surface, because the signal
1028
+ * is a signal and not a data replacement.
1029
+ */
1030
+ function probePracticesDir(practicesDirPath) {
1031
+ if (!node_fs_1.default.existsSync(practicesDirPath))
1032
+ return { failed: false, note: null };
1033
+ try {
1034
+ node_fs_1.default.readdirSync(practicesDirPath);
1035
+ return { failed: false, note: null };
1036
+ }
1037
+ catch (e) {
1038
+ return { failed: true, note: `practices directory unreadable: ${errnoOf(e)}` };
1039
+ }
1040
+ }
543
1041
  /**
544
1042
  * Discover and parse practice files from the single cached vault tier.
545
1043
  *
@@ -563,32 +1061,28 @@ function parsePracticeFile(filePath, raw) {
563
1061
  * degrades to an empty result, a `skipped` entry, or a bounded `notes` entry.
564
1062
  *
565
1063
  * @param cwd Project root (parent of `.planning/`).
1064
+ * @param vaultDir Optional cache directory to read instead of
1065
+ * `<cwd>/.planning/vault` (worktree executors). Defaults to the cwd
1066
+ * join, so the absent-versus-unreadable distinction below is unchanged for
1067
+ * every caller that does not pass it.
566
1068
  */
567
- function discoverPracticeFiles(cwd) {
1069
+ function discoverPracticeFiles(cwd, vaultDir) {
568
1070
  try {
569
- const vaultDirPath = node_path_1.default.join(cwd, '.planning', 'vault');
1071
+ const vaultDirPath = typeof vaultDir === 'string' && vaultDir !== ''
1072
+ ? vaultDir
1073
+ : node_path_1.default.join(cwd, '.planning', 'vault');
570
1074
  if (!node_fs_1.default.existsSync(vaultDirPath)) {
571
- return { practices: [], skipped: [], tier: null, notes: [] };
572
- }
573
- // T-24-08a: probe the practices/ directory's own readability BEFORE
574
- // calling usableFiles() — usableFiles's internal walk() (src/vault.cts)
575
- // catches a readdirSync failure itself and returns an empty list with no
576
- // throw, so wrapping the usableFiles call below in try/catch can never
577
- // detect an unreadable directory (it would look identical to a genuinely
578
- // empty vault). An ABSENT practices/ directory is a genuinely empty vault
579
- // and stays silent (pushes nothing). The probe does NOT short-circuit
580
- // discovery on failure — any practices that ARE independently readable
581
- // still surface below; the note is the signal, not a data replacement.
582
- const notes = [];
583
- const practicesDirPath = node_path_1.default.join(vaultDirPath, PRACTICES_SUBDIR.slice(0, -1));
584
- if (node_fs_1.default.existsSync(practicesDirPath)) {
585
- try {
586
- node_fs_1.default.readdirSync(practicesDirPath);
587
- }
588
- catch (e) {
589
- notes.push(`practices directory unreadable: ${boundedToken(e.message)}`);
590
- }
1075
+ // ABSENT is its own fact: nothing refused to be read, there was simply
1076
+ // nothing there. `runVaultAudit` blocks on it all the same (05-10) — a
1077
+ // gate that was never given a vault verified nothing — but the two
1078
+ // grounds stay separable for whoever has to repair it.
1079
+ return {
1080
+ practices: [], skipped: [], degraded: [], tier: null, notes: [], vaultPresent: false, readFailed: false,
1081
+ vaultRef: null, vaultSha: null, vaultMiss: false,
1082
+ };
591
1083
  }
1084
+ const probe = probePracticesDir(node_path_1.default.join(vaultDirPath, PRACTICES_SUBDIR.slice(0, -1)));
1085
+ const notes = probe.note ? [probe.note] : [];
592
1086
  const allFiles = vaultMod.usableFiles(vaultDirPath);
593
1087
  const practiceFiles = allFiles
594
1088
  .filter((rel) => rel.startsWith(PRACTICES_SUBDIR) && rel.toLowerCase().endsWith('.md'))
@@ -601,8 +1095,15 @@ function discoverPracticeFiles(cwd) {
601
1095
  }
602
1096
  const manifest = vaultMod.readManifest(vaultDirPath);
603
1097
  const tier = manifest && manifest.tier ? manifest.tier : null;
1098
+ // FIX-3/FIX-4: the coordinates the status stamp carries,
1099
+ // and the recorded-miss fact the sentence ladder states. Typed facts on the
1100
+ // result, never sniffed out of notes prose.
1101
+ const vaultRef = manifest && typeof manifest.ref === 'string' ? manifest.ref : null;
1102
+ const vaultSha = manifest && typeof manifest.resolvedSha === 'string' ? manifest.resolvedSha : null;
1103
+ const vaultMiss = vaultMod.manifestRecordsMiss(manifest);
604
1104
  const practices = [];
605
1105
  const skipped = [];
1106
+ const degraded = [];
606
1107
  for (const rel of shown) {
607
1108
  // Bounded prefix read (CR-03) — the whole file is NEVER read here;
608
1109
  // frontmatter + bodyOffset comfortably fit inside PRACTICE_FILE_READ_BYTES.
@@ -620,24 +1121,53 @@ function discoverPracticeFiles(cwd) {
620
1121
  }
621
1122
  const result = parsePracticeFile(rel, raw);
622
1123
  if (result.ok) {
1124
+ // A degraded practice stays in `practices[]` — that is the whole point
1125
+ // of the degrade (its prose still applies). Its warnings ALSO land in
1126
+ // `degraded[]` so the rendered channels can surface them; a warning
1127
+ // recorded on a result nobody renders is the T-4-64 defect.
623
1128
  practices.push({ ...result.practice, tier });
1129
+ const warnings = [...(result.degradedWarnings ?? [])];
1130
+ // An invalid `check:` block is a degradation of the same class: the
1131
+ // practice applies, one thing about it was not usable. It travels the
1132
+ // same channel so an author learns the block was dropped without having
1133
+ // to read a different surface.
1134
+ if (result.practice.checkInvalid)
1135
+ warnings.push(result.practice.checkInvalid);
1136
+ if (warnings.length > 0)
1137
+ degraded.push({ id: result.practice.id, path: rel, warnings });
624
1138
  }
625
1139
  else {
626
1140
  skipped.push({ path: rel, warning: result.warning });
627
1141
  }
628
1142
  }
629
- return { practices, skipped, tier, notes };
1143
+ return {
1144
+ practices, skipped, degraded, tier, notes, vaultPresent: true, readFailed: probe.failed,
1145
+ vaultRef, vaultSha, vaultMiss,
1146
+ };
630
1147
  }
631
1148
  catch (e) {
632
1149
  // T-24-08a: bind and bound the caught error rather than discarding it —
633
1150
  // the bare `catch {}` this replaced is exactly the shape
634
1151
  // pp-core/templates/practices/error-handling.md forbids ("never an empty
635
1152
  // catch block that hides the failure").
1153
+ //
1154
+ // On every fs path below, the vault directory was proved to exist before
1155
+ // anything could throw (the `existsSync` guard returns early), so reaching
1156
+ // here means it was there and something about READING it failed:
1157
+ // vaultPresent stays true and readFailed goes true. The two fields drive
1158
+ // the same blocking outcome in `runVaultAudit` either way — they differ
1159
+ // only in which repair they point an operator at.
636
1160
  return {
637
1161
  practices: [],
638
1162
  skipped: [],
1163
+ degraded: [],
639
1164
  tier: null,
640
- notes: [`practice discovery failed: ${boundedToken(e.message)}`],
1165
+ notes: [`practice discovery failed: ${errnoOf(e)}`],
1166
+ vaultPresent: true,
1167
+ readFailed: true,
1168
+ vaultRef: null,
1169
+ vaultSha: null,
1170
+ vaultMiss: false,
641
1171
  };
642
1172
  }
643
1173
  }
@@ -654,18 +1184,32 @@ const { EXTERNAL_CONTENT_FENCE_START, EXTERNAL_CONTENT_FENCE_END, defangFenceMar
654
1184
  * reference preamble (T-24-05: the excerpt is the exact untrusted payload a
655
1185
  * later phase injects into a code-writing agent's context). Uses
656
1186
  * `vaultMod.readHeadBounded` — a single bounded read of `bodyOffset +
657
- * PRACTICE_EXCERPT_BYTES` bytes — the whole file is NEVER read. The bounded
658
- * body has any planted fence-marker strings neutralized via
659
- * `defangFenceMarkers` BEFORE being placed between the real markers (CR-01),
660
- * so an untrusted body can never spoof an early fence closer.
1187
+ * PRACTICE_EXCERPT_BYTES * EXCERPT_CRLF_OVERREAD_FACTOR` bytes — the whole file
1188
+ * is NEVER read. The bounded body has any planted fence-marker strings
1189
+ * neutralized via `defangFenceMarkers` BEFORE being placed between the real
1190
+ * markers (CR-01), so an untrusted body can never spoof an early fence closer.
1191
+ *
1192
+ * LINE ENDINGS (FIX-1). The body is sliced at `bodyOffset`
1193
+ * FIRST — it is an index into the raw text, computed by `parsePracticeFile` on
1194
+ * the same bytes — and THEN normalised by deleting every carriage return, which
1195
+ * turns each CRLF pair into LF and drops any lone carriage return (a control
1196
+ * character that must not reach an agent context). Only then is it trimmed and capped, so the
1197
+ * `PRACTICE_EXCERPT_BYTES` cap is measured on normalised text and a CRLF
1198
+ * checkout renders the same excerpt as its LF twin. Before this, the cap was
1199
+ * applied to raw text: a `core.autocrlf=true` checkout was measured losing
1200
+ * 3–18 chars from 8 of 55 practices, with 610 carriage returns reaching the
1201
+ * executor-facing block.
661
1202
  *
662
1203
  * @param filePath Absolute path to the practice `.md` file on disk.
663
1204
  * @param bodyOffset Byte index where the markdown body begins (from the
664
1205
  * practice's `PracticeRecord.bodyOffset`, set by `parsePracticeFile`).
665
1206
  */
666
1207
  function boundedFencedExcerpt(filePath, bodyOffset) {
667
- const raw = vaultMod.readHeadBounded(filePath, bodyOffset + PRACTICE_EXCERPT_BYTES);
668
- const body = raw.slice(bodyOffset).trim();
1208
+ const raw = vaultMod.readHeadBounded(filePath, bodyOffset + PRACTICE_EXCERPT_BYTES * EXCERPT_CRLF_OVERREAD_FACTOR);
1209
+ // ONE replace, not two: deleting every carriage return turns each CRLF pair
1210
+ // into LF AND drops any lone carriage return, so a separate CRLF→LF pass
1211
+ // would be a step no test could tell apart from its absence.
1212
+ const body = raw.slice(bodyOffset).replace(/\r/g, '').trim();
669
1213
  const bounded = body.length > PRACTICE_EXCERPT_BYTES ? body.slice(0, PRACTICE_EXCERPT_BYTES) : body;
670
1214
  const defanged = defangFenceMarkers(bounded);
671
1215
  return [
@@ -679,6 +1223,19 @@ function boundedFencedExcerpt(filePath, bodyOffset) {
679
1223
  EXTERNAL_CONTENT_FENCE_END,
680
1224
  ].join('\n');
681
1225
  }
1226
+ /**
1227
+ * Is this file one the unmatched-source-file note reports when no scoped
1228
+ * practice matched it? (FIX-2, QD-2.)
1229
+ *
1230
+ * Exactly the plan graph's source predicate: tests, docs, baselines,
1231
+ * changesets and `.planning/` files are never reported, and everything else
1232
+ * is. Windows separators are normalised first.
1233
+ *
1234
+ * @param filePath A project-relative file path, either separator.
1235
+ */
1236
+ function isUnmatchedNoteCandidate(filePath) {
1237
+ return (0, plan_graph_cjs_1.isSourceFile)(String(filePath).replace(/\\/g, '/'));
1238
+ }
682
1239
  /**
683
1240
  * Match discovered practices against a candidate file list, partitioning
684
1241
  * results into two buckets (D-13):
@@ -702,12 +1259,84 @@ function boundedFencedExcerpt(filePath, bodyOffset) {
702
1259
  */
703
1260
  function matchPractices(opts) {
704
1261
  const { cwd, files } = opts;
705
- const discovery = discoverPracticeFiles(cwd);
706
- const vaultDirPath = node_path_1.default.join(cwd, '.planning', 'vault');
1262
+ const clock = opts.now ?? (() => Number(process.hrtime.bigint()) / 1e6);
1263
+ const vaultDirPath = typeof opts.vaultDir === 'string' && opts.vaultDir !== ''
1264
+ ? opts.vaultDir
1265
+ : node_path_1.default.join(cwd, '.planning', 'vault');
1266
+ const discovery = discoverPracticeFiles(cwd, vaultDirPath);
707
1267
  const normalizedFiles = files.map((f) => f.replace(/\\/g, '/'));
1268
+ // COST BOUND, PRIMARY ARM (CHECK-04 / T-5-10). Counted BEFORE any matching,
1269
+ // so the refusal is arithmetic rather than a race: identical inputs refuse
1270
+ // identically on every machine (T-5-11). Global practices carry no globs and
1271
+ // so contribute no pairs.
1272
+ const scopedGlobCount = discovery.practices.reduce((n, p) => (p.scope === 'scoped' ? n + p.appliesTo.length : n), 0);
1273
+ const pairs = scopedGlobCount * normalizedFiles.length;
1274
+ if (pairs > MATCH_PAIR_CAP) {
1275
+ // A COUNTED refusal, never a silent empty result — the discipline
1276
+ // `PRACTICE_COUNT_CAP` and `MATCHED_FILES_CAP` already keep. An empty
1277
+ // `matched` with no note would read as "nothing applies", which is
1278
+ // compliance claimed for work that was never done (T-5-12). The note
1279
+ // states what was measured and asks for nothing destructive.
1280
+ return {
1281
+ matched: [],
1282
+ global: [],
1283
+ skipped: discovery.skipped,
1284
+ degraded: discovery.degraded,
1285
+ notes: [
1286
+ ...discovery.notes,
1287
+ `${MATCH_BUDGET_NOTE_PREFIX}this vault and change set produce ${pairs} (glob, file) pair(s), over the ${MATCH_PAIR_CAP}-pair matching cap — no practice was evaluated against these files`,
1288
+ ],
1289
+ // `practicesProcessed` keeps its established meaning (how many practices
1290
+ // discovery yielded), so a renderer can still tell "the vault holds no
1291
+ // practices" from "the vault holds practices that were not evaluated".
1292
+ practicesProcessed: discovery.practices.length,
1293
+ filesConsidered: files.length,
1294
+ // Threaded on BOTH return sites (05-13): a refusal here does not stop
1295
+ // the vault-read fact from being the one an unreadable vault needs
1296
+ // stated, and a field set on one path only is a field a reader cannot
1297
+ // trust on either.
1298
+ vaultReadFailed: discovery.readFailed === true,
1299
+ // Threaded on BOTH return sites for the same reason (CR-01): a field set
1300
+ // on one path only is a field a reader cannot trust on either.
1301
+ vaultAbsent: discovery.vaultPresent === false,
1302
+ // Nothing was evaluated, so no file can be said to have matched nothing
1303
+ // (QD-3) — the refusal note above states what happened instead.
1304
+ unmatchedSourceFiles: [],
1305
+ // Threaded on BOTH return sites (05-13 rule).
1306
+ vaultRef: discovery.vaultRef,
1307
+ vaultSha: discovery.vaultSha,
1308
+ vaultMiss: discovery.vaultMiss,
1309
+ pairBudget: {
1310
+ pairs,
1311
+ cap: MATCH_PAIR_CAP,
1312
+ exceeded: true,
1313
+ budgetMs: MATCH_BUDGET_MS,
1314
+ // The loop never started, so the wall-clock arm never had a chance to
1315
+ // fire. Reporting it as exhausted too would name a guard that did not
1316
+ // act.
1317
+ budgetExceeded: false,
1318
+ },
1319
+ };
1320
+ }
708
1321
  const matched = [];
709
1322
  const global = [];
710
- for (const practice of discovery.practices) {
1323
+ // Every normalised file ANY scoped practice matched — uncapped, unlike each
1324
+ // entry's `matchedFiles` slice — for the unmatched-source-file note (FIX-2).
1325
+ const anyScopedMatch = new Set();
1326
+ const startedAt = clock();
1327
+ let practicesUnprocessed = 0;
1328
+ let budgetExceeded = false;
1329
+ for (const [index, practice] of discovery.practices.entries()) {
1330
+ // COST BOUND, SECONDARY ARM. Checked between whole practices: the unit
1331
+ // skipped over is one practice's full glob × file scan, which the pair cap
1332
+ // above has already bounded, so the granularity is bounded too. Verdicts
1333
+ // already computed are KEPT — but the omission is counted, so a partial
1334
+ // result can never be read as a complete one.
1335
+ if (clock() - startedAt > MATCH_BUDGET_MS) {
1336
+ budgetExceeded = true;
1337
+ practicesUnprocessed = discovery.practices.length - index;
1338
+ break;
1339
+ }
711
1340
  if (practice.scope === 'scoped') {
712
1341
  const winningGlobs = new Set();
713
1342
  const matchedFilesSet = new Set();
@@ -716,6 +1345,7 @@ function matchPractices(opts) {
716
1345
  if (globMatch(file, glob)) {
717
1346
  winningGlobs.add(glob);
718
1347
  matchedFilesSet.add(file);
1348
+ anyScopedMatch.add(file);
719
1349
  }
720
1350
  }
721
1351
  }
@@ -749,15 +1379,135 @@ function matchPractices(opts) {
749
1379
  }
750
1380
  matched.sort((a, b) => a.id.localeCompare(b.id));
751
1381
  global.sort((a, b) => a.id.localeCompare(b.id));
1382
+ const notes = budgetExceeded
1383
+ ? [
1384
+ ...discovery.notes,
1385
+ `${MATCH_BUDGET_NOTE_PREFIX}${practicesUnprocessed} of ${discovery.practices.length} practice(s) were not matched because the ${MATCH_BUDGET_MS}ms matching budget was exhausted — the practices reported were evaluated, the rest were not`,
1386
+ ]
1387
+ : discovery.notes;
1388
+ // QD-3: only a claim about practices that WERE evaluated. With no practice
1389
+ // discovered (absent, unreadable, missed or empty vault) or the wall-clock
1390
+ // budget exhausted part-way, "matched no scoped practice" would state
1391
+ // something nobody measured; the sentence ladder and the budget note already
1392
+ // say what happened there.
1393
+ const unmatchedSourceFiles = discovery.practices.length > 0 && !budgetExceeded
1394
+ ? Array.from(new Set(normalizedFiles))
1395
+ .filter((f) => isUnmatchedNoteCandidate(f) && !anyScopedMatch.has(f))
1396
+ .sort()
1397
+ : [];
752
1398
  return {
753
1399
  matched,
754
1400
  global,
755
1401
  skipped: discovery.skipped,
756
- notes: discovery.notes,
1402
+ // Passed straight through — the matcher makes no judgement about a
1403
+ // degradation, it only carries it to the renderers (CHECK-02d).
1404
+ degraded: discovery.degraded,
1405
+ notes,
757
1406
  practicesProcessed: discovery.practices.length,
758
1407
  filesConsidered: files.length,
1408
+ // The typed vault-read fact, carried verbatim from discovery — never
1409
+ // re-derived here and never sniffed out of `notes[]` prose (05-13).
1410
+ vaultReadFailed: discovery.readFailed === true,
1411
+ // The typed vault-absence fact, carried verbatim from discovery (CR-01).
1412
+ vaultAbsent: discovery.vaultPresent === false,
1413
+ unmatchedSourceFiles,
1414
+ // The manifest coordinates and recorded-miss fact, carried verbatim from
1415
+ // discovery (FIX-3/FIX-4).
1416
+ vaultRef: discovery.vaultRef,
1417
+ vaultSha: discovery.vaultSha,
1418
+ vaultMiss: discovery.vaultMiss,
1419
+ pairBudget: {
1420
+ pairs,
1421
+ cap: MATCH_PAIR_CAP,
1422
+ exceeded: false,
1423
+ budgetMs: MATCH_BUDGET_MS,
1424
+ budgetExceeded,
1425
+ },
759
1426
  };
760
1427
  }
1428
+ /**
1429
+ * The cost bound's notes, selected out of a result's `notes[]` by the marker
1430
+ * the matcher stamped on them.
1431
+ *
1432
+ * NO LONGER THE SLOT FILTER FOR THE TWO RENDERED CHANNELS (05-13, G-4). The
1433
+ * `<vault_practices>` block and the ship gate's message now forward EVERY note
1434
+ * the matcher counted, because this filter's prefix test also dropped the
1435
+ * discovery-failure and cap-omission notes, and a note computed and rendered
1436
+ * nowhere governs nothing. What survives from the original rationale is the
1437
+ * part that was always the point: whatever a channel forwards, it forwards the
1438
+ * sentence the matcher COMPOSED, never one re-composed from `pairBudget`'s
1439
+ * numbers (one emission, N channels — T-4-49).
1440
+ *
1441
+ * Retained and exported as the SELECTOR for a caller that wants exactly the
1442
+ * cost bound's subset — the slow corpus counts refusals with it, and a test
1443
+ * uses it to prove which notes the old filter would have dropped.
1444
+ */
1445
+ function matchBudgetNotes(notes) {
1446
+ return notes.filter((n) => n.startsWith(MATCH_BUDGET_NOTE_PREFIX));
1447
+ }
1448
+ /**
1449
+ * Bound the text of a degradation warning before it is rendered (T-5-02).
1450
+ *
1451
+ * The warning is ENGINE-composed around tokens `parsePracticeFile` already put
1452
+ * through `boundedToken`, so it is not truncated again here — truncating a
1453
+ * composed sentence would cut the defect off mid-word. What it still does is
1454
+ * collapse any CR/LF (so a warning can never become two rendered lines) and
1455
+ * defang planted fence markers (so a client-authored filename carrying the
1456
+ * marker string cannot open or close a fence in a rendered channel).
1457
+ */
1458
+ function boundedDegradationText(warning) {
1459
+ return defangFenceMarkers(String(warning).replace(/[\r\n]+/g, ' '));
1460
+ }
1461
+ /**
1462
+ * The per-practice degradation lines, shared VERBATIM by both rendered
1463
+ * channels. One warning renders exactly one line, and every client-derived
1464
+ * token on it is bounded: the practice `id` is vault-authored, and the warning
1465
+ * is engine-composed around bounded tokens.
1466
+ */
1467
+ function renderDegradationLines(degraded) {
1468
+ return degraded.flatMap((d) => d.warnings.map((w) => `- **${defangFenceMarkers(boundedToken(d.id))}** — ${boundedDegradationText(w)}`));
1469
+ }
1470
+ /**
1471
+ * The per-REFUSED-file lines, shared VERBATIM by both executor-facing channels
1472
+ * (05-REVIEW WR-02).
1473
+ *
1474
+ * `skipped[]` is a structurally different fact from `degraded[]`: a degraded
1475
+ * practice still applies with one part not understood, a skipped one was
1476
+ * REFUSED WHOLE and nothing in it applies. Discovery has always carried both,
1477
+ * `renderPracticesReport` has always rendered both, and the two channels an
1478
+ * executor actually reads rendered only the degraded half — so a vault whose
1479
+ * practice file the parser rejected looked, to a planner and to the ship gate,
1480
+ * exactly like a vault that never held it. `vault.audit` meanwhile emits
1481
+ * `FAIL(practice_unloadable)` and blocks on that identical state, so the three
1482
+ * channels disagreed about one vault.
1483
+ *
1484
+ * Each warning already embeds its own path and is composed by the engine, but
1485
+ * it interpolates vault-authored tokens, so it goes through the same
1486
+ * bounding/defanging as a degradation line and renders inside the fence.
1487
+ */
1488
+ function renderSkippedLines(skipped) {
1489
+ return skipped.map((s) => `- ${boundedDegradationText(s.warning)}`);
1490
+ }
1491
+ /**
1492
+ * The one-line note naming candidate files that matched no scoped practice
1493
+ * (FIX-2). `null` when there are none.
1494
+ *
1495
+ * The paths are project-authored — the plan's `files_modified` or the
1496
+ * operator's argv, the same trust class `matched_files` already renders — so
1497
+ * the line may sit outside a fence, but each path is still bounded and
1498
+ * defanged so none can open or close one. The list is capped at
1499
+ * `MATCHED_FILES_CAP`; the raw array carries every file.
1500
+ *
1501
+ * @param files `PracticeMatchResult.unmatchedSourceFiles`.
1502
+ */
1503
+ function renderUnmatchedSourceNote(files) {
1504
+ const all = Array.isArray(files) ? files : [];
1505
+ if (all.length === 0)
1506
+ return null;
1507
+ const shown = all.slice(0, MATCHED_FILES_CAP).map((p) => defangFenceMarkers(boundedToken(p)));
1508
+ const capped = all.length > MATCHED_FILES_CAP ? ` (showing ${MATCHED_FILES_CAP} of ${all.length})` : '';
1509
+ return `Note: ${all.length} file(s) matched no scoped practice: ${shown.join(', ')}${capped} — check the practices' applies-to globs against this layout.`;
1510
+ }
761
1511
  /**
762
1512
  * Format a `PracticeMatchResult` as plain English (COMM-01 / D-12) — the
763
1513
  * default human-readable output (a later CLI wiring emits `--raw` JSON from
@@ -807,6 +1557,31 @@ function renderPracticesReport(result) {
807
1557
  lines.push(`- ${s.warning}`);
808
1558
  lines.push('');
809
1559
  }
1560
+ // CHECK-02d — the report's half of the degradation channel, carrying the SAME
1561
+ // lines the block carries (T-4-49: both rendered channels move together, or a
1562
+ // reader of one is told something a reader of the other never learns).
1563
+ // Deliberately a section of its own, beside "Skipped" rather than inside it:
1564
+ // a skipped practice did not apply, a degraded one DID.
1565
+ //
1566
+ // Unfenced here, fenced in the block, and that asymmetry is deliberate: this
1567
+ // report is human-facing and its neighbouring `Skipped` section already
1568
+ // prints bounded warnings unfenced, whereas the block is read by a
1569
+ // code-WRITING agent and holds the stricter invariant that nothing
1570
+ // vault-authored appears outside a fence.
1571
+ const degraded = result.degraded ?? [];
1572
+ if (degraded.length > 0) {
1573
+ lines.push(`## Degraded practices (${degraded.length})`);
1574
+ lines.push('_These practices still applied — each line names what this pp-core did not understand._');
1575
+ for (const line of renderDegradationLines(degraded))
1576
+ lines.push(line);
1577
+ lines.push('');
1578
+ }
1579
+ // FIX-2: the same line the block carries, as its own paragraph.
1580
+ const unmatchedNote = renderUnmatchedSourceNote(result.unmatchedSourceFiles);
1581
+ if (unmatchedNote !== null) {
1582
+ lines.push(unmatchedNote);
1583
+ lines.push('');
1584
+ }
810
1585
  if (result.notes.length > 0) {
811
1586
  lines.push('## Notes');
812
1587
  for (const n of result.notes)
@@ -885,6 +1660,86 @@ function renderPracticeEntry(entry) {
885
1660
  lines.push(EXTERNAL_CONTENT_FENCE_END);
886
1661
  return lines.join('\n');
887
1662
  }
1663
+ /** Stamp `ref`: charset-and-length bounded like the load gate's ref (T-08-39). */
1664
+ function stampRef(ref) {
1665
+ const kept = typeof ref === 'string' ? ref.replace(/[^A-Za-z0-9._/@+~-]/g, '').slice(0, 100) : '';
1666
+ return kept === '' ? '(none)' : kept;
1667
+ }
1668
+ /** Stamp `sha`: lowercase hex only, first 8; `(none)` when fewer than 8 remain. */
1669
+ function stampSha(sha) {
1670
+ const hex = typeof sha === 'string' ? sha.toLowerCase().replace(/[^0-9a-f]/g, '') : '';
1671
+ return hex.length >= 8 ? hex.slice(0, 8) : '(none)';
1672
+ }
1673
+ /**
1674
+ * Render the block's status stamp — ONE line, an HTML comment (FIX-3, QD-4).
1675
+ *
1676
+ * `ref=` and `sha=` name the vault read, `entries=<n>` counts the fenced
1677
+ * practice entries rendered OR `state=<token>` names the degraded state —
1678
+ * never both — and `engine=` is the `createdWith` string. Every value is
1679
+ * charset-bounded so no `<` or `>` survives: the comment cannot be closed early
1680
+ * and nothing in it can read as a fence marker or an `id:` line (T-jby-04/05).
1681
+ *
1682
+ * @param parts.ref Manifest ref (project-configured; bounded here).
1683
+ * @param parts.sha Manifest resolvedSha (bounded to 8 lowercase hex).
1684
+ * @param parts.entries Number of fenced entries, when the block renders entries.
1685
+ * @param parts.state A `PRACTICES_STAMP_STATES` token, when it renders a sentence.
1686
+ * @param parts.engine Override for the engine string (tests); defaults to
1687
+ * `resolveCreatedWithFrom(__dirname)`.
1688
+ */
1689
+ function renderPracticesStamp(parts) {
1690
+ const engineRaw = typeof parts.engine === 'string' ? parts.engine : (0, compose_bindings_cjs_1.resolveCreatedWithFrom)(__dirname);
1691
+ const engine = engineRaw.replace(/[^A-Za-z0-9.@_-]/g, '') || '(none)';
1692
+ const tail = typeof parts.state === 'string'
1693
+ ? `state=${parts.state.replace(/[^a-z-]/g, '')}`
1694
+ : `entries=${Math.max(0, Math.trunc(Number(parts.entries) || 0))}`;
1695
+ return `${PRACTICES_STAMP_PREFIX} ref=${stampRef(parts.ref)} sha=${stampSha(parts.sha)} ${tail} engine=${engine} -->`;
1696
+ }
1697
+ /**
1698
+ * Choose the sentence an entry-less block states AND its stamp token, in ONE
1699
+ * place so the two cannot drift (FIX-3/FIX-4).
1700
+ *
1701
+ * ORDER IS LOAD-BEARING and pinned rung by rung. The pair-cap refusal stays
1702
+ * FIRST: it is a statement about work this engine declined to do. Read
1703
+ * failure, absence and a recorded miss follow — each is a fact about a vault
1704
+ * nothing could be read from, and each sits above the all-refused and
1705
+ * none-found rungs, which are facts about a vault that WAS read. The miss rung
1706
+ * (FIX-4) sits after absence (an absent directory has no manifest to record a
1707
+ * miss in, so absence is the more basic fact) and before all-refused.
1708
+ */
1709
+ function selectEmptyBlockSentence(result, skippedCount) {
1710
+ if (result?.pairBudget?.exceeded === true) {
1711
+ return { sentence: "Practices were NOT checked against this plan's files.", state: PRACTICES_STAMP_STATES.NOT_CHECKED_PAIR_CAP };
1712
+ }
1713
+ // THE THIRD RUNG (05-13, G-4). `practicesProcessed === 0` is true of a vault
1714
+ // that holds no practices AND of one nothing could read; the none-found
1715
+ // sentence is a fact about a vault that WAS read. Reads the typed boolean,
1716
+ // never `notes[]` prose.
1717
+ if (result?.vaultReadFailed === true) {
1718
+ return { sentence: PRACTICES_VAULT_UNREADABLE_SENTENCE, state: PRACTICES_STAMP_STATES.VAULT_UNREADABLE };
1719
+ }
1720
+ if (result?.vaultAbsent === true) {
1721
+ return { sentence: PRACTICES_VAULT_ABSENT_SENTENCE, state: PRACTICES_STAMP_STATES.VAULT_ABSENT };
1722
+ }
1723
+ // FIX-4: a manifest that records a miss — the fetch ran
1724
+ // and reached nothing.
1725
+ if (result?.vaultMiss === true) {
1726
+ return { sentence: PRACTICES_VAULT_MISS_SENTENCE, state: PRACTICES_STAMP_STATES.VAULT_MISS };
1727
+ }
1728
+ if (result && result.practicesProcessed === 0 && skippedCount > 0) {
1729
+ // 05-REVIEW WR-02, the fifth rung. `practicesProcessed === 0` is true of a
1730
+ // THIRD kind of vault as well: one that holds practice files and had every
1731
+ // one of them refused. "None found" is false here — the files were found,
1732
+ // read, and rejected.
1733
+ return {
1734
+ sentence: 'Practices were NOT applied — every practice file found in the vault was refused. See “Refused practices”.',
1735
+ state: PRACTICES_STAMP_STATES.ALL_REFUSED,
1736
+ };
1737
+ }
1738
+ if (result && result.practicesProcessed === 0) {
1739
+ return { sentence: 'Practices checked — none found in the vault.', state: PRACTICES_STAMP_STATES.NONE_FOUND };
1740
+ }
1741
+ return { sentence: "Practices checked — none apply to this plan's files.", state: PRACTICES_STAMP_STATES.NONE_APPLY };
1742
+ }
888
1743
  /**
889
1744
  * Render the complete `<vault_practices>` block for a plan, ready to embed
890
1745
  * VERBATIM — no renaming, reshaping or re-fencing by the caller.
@@ -904,18 +1759,130 @@ function renderPracticeEntry(entry) {
904
1759
  function renderPracticesBlock(result, reason) {
905
1760
  const open = '<vault_practices>';
906
1761
  const close = '</vault_practices>';
907
- const head = [open, '', BLOCK_PREAMBLE, ''];
1762
+ // FIX-3: the status stamp is the first line inside the
1763
+ // block on all three return paths, then the blank line, then the preamble.
1764
+ const engine = (0, compose_bindings_cjs_1.resolveCreatedWithFrom)(__dirname);
1765
+ const headWith = (stamp) => [
1766
+ open,
1767
+ renderPracticesStamp({ ref: result?.vaultRef, sha: result?.vaultSha, engine, ...stamp }),
1768
+ '',
1769
+ BLOCK_PREAMBLE,
1770
+ '',
1771
+ ];
1772
+ // CHECK-02d — the executor-facing degradation slot.
1773
+ //
1774
+ // This is the channel the code-writing agent actually reads, so it is the
1775
+ // channel the warning has to reach. A degradation recorded on a parse result
1776
+ // and rendered nowhere repeats T-4-64 exactly: the record exists, nobody sees
1777
+ // it, and the practice governs nothing while appearing to govern.
1778
+ //
1779
+ // FENCED, because this block's standing invariant is that nothing
1780
+ // vault-authored is emitted outside an external-content fence — the practice
1781
+ // `id` on each line is vault-authored. The lines are defanged first, so a
1782
+ // planted marker cannot close the fence early and smuggle text out.
1783
+ //
1784
+ // The slot ADDS to the rendering, never replaces: degraded means "applies
1785
+ // with a warning", so the practice's own entry below is untouched. When
1786
+ // nothing is degraded the slot contributes zero bytes, leaving every healthy
1787
+ // vault's block byte-identical to its pre-plan output.
1788
+ const degraded = result?.degraded ?? [];
1789
+ const degradedSection = degraded.length > 0
1790
+ ? [
1791
+ '## Degraded practices',
1792
+ '',
1793
+ 'These practices still apply. Each line below names something in the practice file that this pp-core did not understand.',
1794
+ '',
1795
+ EXTERNAL_CONTENT_FENCE_START,
1796
+ ...FENCE_NOTICE,
1797
+ '',
1798
+ ...renderDegradationLines(degraded),
1799
+ EXTERNAL_CONTENT_FENCE_END,
1800
+ '',
1801
+ ]
1802
+ : [];
1803
+ // CHECK-04 / T-5-12 — the cost bound's slot (05-04).
1804
+ //
1805
+ // Before this the block rendered no `notes[]` at all, so a refusal computed
1806
+ // by the matcher reached the human report and the --raw JSON and STOPPED —
1807
+ // the one channel a code-writing agent actually reads stayed silent and, on
1808
+ // a total refusal, went on to print "none apply to this plan's files" about
1809
+ // files nothing had been evaluated against. That is T-4-64 with a worse
1810
+ // consequence: not an invisible note, an actively false one.
1811
+ //
1812
+ // UNFENCED, unlike the degradation slot beside it: every byte here is
1813
+ // engine-composed. That was argued for the cost bound's two sentences and
1814
+ // it holds for the WHOLE set (05-13) — the cap-omission note is an integer
1815
+ // and a fixed phrase, and the discovery-failure notes were bounded to an
1816
+ // errno code by 05-10 precisely so no path text could ride one into an
1817
+ // agent-read channel. No vault-authored token reaches this section on any
1818
+ // path, so the block's nothing-vault-authored-outside-a-fence invariant is
1819
+ // untouched.
1820
+ //
1821
+ // WIDENED from `matchBudgetNotes` (05-13, G-4). That prefix filter admitted
1822
+ // the cost bound's notes and silently dropped every other note the matcher
1823
+ // counted — the discovery failure and the cap omission among them. A note
1824
+ // computed and rendered nowhere is T-4-64, and here it was worse than
1825
+ // invisible: the sentence chosen below claimed a clean, empty vault while
1826
+ // the note explaining that nothing could be read was being discarded one
1827
+ // line above it. Every note is still forwarded VERBATIM as the matcher
1828
+ // composed it, never re-composed from `pairBudget`'s numbers, so this
1829
+ // channel and the ship gate's message cannot drift apart (T-4-49).
1830
+ const resultNotes = result?.notes ?? [];
1831
+ const notesSection = resultNotes.length > 0
1832
+ ? ['## Practices not checked', '', ...resultNotes.map((n) => `- ${n}`), '']
1833
+ : [];
1834
+ // 05-REVIEW WR-02 — the refused half of discovery, which this channel dropped.
1835
+ // FENCED like the degradation slot beside it and for the same reason: the
1836
+ // warnings interpolate vault-authored tokens. Rendered on BOTH returns below,
1837
+ // because the all-refused vault lands on the early one.
1838
+ const skipped = result?.skipped ?? [];
1839
+ const refusedSection = skipped.length > 0
1840
+ ? [
1841
+ '## Refused practices',
1842
+ '',
1843
+ 'These practice files were found in the vault and refused whole — nothing in them was applied. Each line names why.',
1844
+ '',
1845
+ EXTERNAL_CONTENT_FENCE_START,
1846
+ ...FENCE_NOTICE,
1847
+ '',
1848
+ ...renderSkippedLines(skipped),
1849
+ EXTERNAL_CONTENT_FENCE_END,
1850
+ '',
1851
+ ]
1852
+ : [];
1853
+ // FIX-2: candidate files no scoped practice matched. The
1854
+ // matcher owns the gating (QD-3); the renderer renders whenever the array is
1855
+ // non-empty. Engine-composed text around project-authored, bounded and
1856
+ // defanged paths — nothing vault-authored — so it sits outside a fence.
1857
+ const unmatchedNote = renderUnmatchedSourceNote(result?.unmatchedSourceFiles);
1858
+ const noteSection = unmatchedNote !== null ? [unmatchedNote, ''] : [];
908
1859
  if (reason) {
1860
+ const head = headWith({ state: PRACTICES_STAMP_STATES.COULD_NOT_BE_CHECKED });
909
1861
  return [...head, `Practices could not be checked: ${boundedToken(reason)}.`, '', close].join('\n');
910
1862
  }
911
1863
  if (!result || (result.matched.length === 0 && result.global.length === 0)) {
912
1864
  // D-14 (vault holds no practices at all) vs D-13 (checked, none applied).
913
- const sentence = result && result.practicesProcessed === 0
914
- ? 'Practices checked — none found in the vault.'
915
- : "Practices checked — none apply to this plan's files.";
916
- return [...head, sentence, '', close].join('\n');
1865
+ //
1866
+ // A degraded SCOPED practice whose globs matched nothing lands in neither
1867
+ // bucket and reaches this early return — which is precisely where a warning
1868
+ // would otherwise vanish, so the slot is rendered here too.
1869
+ //
1870
+ // A TOTAL refusal (the pair cap fired before the loop) lands here too, and
1871
+ // neither of the two pre-existing sentences is true of it: nothing was
1872
+ // checked, so "checked — none apply" claims a result that was never
1873
+ // computed. A partial refusal (the wall-clock budget) keeps its sentence,
1874
+ // because the practices that WERE evaluated genuinely did not apply — the
1875
+ // note above states how many were not reached.
1876
+ //
1877
+ // The rungs themselves — and their order — live in
1878
+ // `selectEmptyBlockSentence`, which picks the sentence and the stamp token
1879
+ // together so the two cannot drift.
1880
+ const { sentence, state } = selectEmptyBlockSentence(result, skipped.length);
1881
+ const head = headWith({ state });
1882
+ return [...head, ...notesSection, ...degradedSection, ...refusedSection, ...noteSection, sentence, '', close].join('\n');
917
1883
  }
918
- const body = [];
1884
+ const head = headWith({ entries: result.matched.length + result.global.length });
1885
+ const body = [...notesSection, ...degradedSection, ...refusedSection, ...noteSection];
919
1886
  if (result.matched.length > 0) {
920
1887
  body.push('## Scoped practices', '');
921
1888
  for (const m of result.matched)
@@ -937,6 +1904,19 @@ module.exports = {
937
1904
  matchPractices,
938
1905
  renderPracticesReport,
939
1906
  renderPracticesBlock,
1907
+ // 06-04 (VERIFY-02, CR-02): exported so the retroactive review arm hands the
1908
+ // agent EXACTLY the entries `renderPracticesBlock` hands a planner — one
1909
+ // fencing idiom, one defang, one reference-data preamble. The alternative was
1910
+ // for that arm to render a per-practice entry of its own, which is how CR-02's
1911
+ // class comes back: a second renderer is a second place a client-authored byte
1912
+ // can reach an agent context unfenced. Purely additive; no call site changed.
1913
+ renderPracticeEntry,
1914
+ // D-05-07: exported so the trace scaffold digests the SAME
1915
+ // text `renderPracticeEntry` places under `ask:`/`excerpt:` — one unwrap, two
1916
+ // readers. A second unwrap in src/vault-trace.cts would be a second place the
1917
+ // fence-stripping rule could drift, and the digest would then disagree with
1918
+ // the block it was written to verify.
1919
+ unwrapFencedExcerpt,
940
1920
  boundedFencedExcerpt,
941
1921
  PRACTICE_COUNT_CAP,
942
1922
  VALID_SEVERITIES,
@@ -949,4 +1929,62 @@ module.exports = {
949
1929
  boundedToken,
950
1930
  PRACTICE_FILE_READ_BYTES,
951
1931
  WARNING_TOKEN_MAX,
1932
+ // 05-01 (CHECK-02, T-4-42): exported so the allowlist's conformance test can
1933
+ // DERIVE its iteration set from the shipped constant instead of hand-listing
1934
+ // it, and so the promote path's mirror can be checked against the same set.
1935
+ DOCUMENTED_FRONTMATTER_KEYS,
1936
+ // 05-01 (D-05-05): the check block's three caps. Exported so tests HARVEST
1937
+ // them at run time and size their boundary fixtures from the shipped values —
1938
+ // never copying the numbers, which is how a fixture rots into asserting a cap
1939
+ // the code no longer has.
1940
+ CHECK_FILES_CAP,
1941
+ CHECK_TOKEN_COUNT_CAP,
1942
+ CHECK_TOKEN_MAX,
1943
+ // 10-03 (MECH-03 / D-10-04): the enforced-by list cap, exported for the same
1944
+ // harvest-never-copy reason as the three check caps above.
1945
+ ENFORCED_BY_COUNT_CAP,
1946
+ // 05-04 (CHECK-04, T-5-10): the matching cost bound. Exported so the slow
1947
+ // corpus can extrapolate its measured per-pair rate to the cap the running
1948
+ // code actually holds, and so a consumer channel can name the cap it hit.
1949
+ MATCH_PAIR_CAP,
1950
+ MATCH_BUDGET_MS,
1951
+ matchBudgetNotes,
1952
+ // 05-13 (CHECK-02, G-4): the read-failure sentence both executor-facing
1953
+ // channels state. Exported so `cmdShipPractices` and every pin HARVEST the
1954
+ // wording instead of copying it — a copy is a wording free to drift.
1955
+ PRACTICES_VAULT_UNREADABLE_SENTENCE,
1956
+ PRACTICES_VAULT_ABSENT_SENTENCE,
1957
+ // 05-REVIEW CR-02: exported so the audit's gate message fences its
1958
+ // vault-authored fail items with the SAME idiom this module already uses —
1959
+ // one marker pair, one defang, one reference-data notice. Authoring a second
1960
+ // fence or preamble in the audit module is how CR-02's class returns (same
1961
+ // argument as the renderPracticeEntry export above).
1962
+ defangFenceMarkers,
1963
+ EXTERNAL_CONTENT_FENCE_START,
1964
+ EXTERNAL_CONTENT_FENCE_END,
1965
+ FENCE_NOTICE,
1966
+ // 05-REVIEW WR-02: the refused-file lines, exported for the SAME reason
1967
+ // `renderPracticeEntry` is — the ship gate renders the identical lines from
1968
+ // this one composer, so the two executor-facing channels cannot describe one
1969
+ // refused practice differently (T-4-49).
1970
+ renderSkippedLines,
1971
+ // 05-REVIEW CR-03: exported so the audit's bounded reader reports a caught
1972
+ // filesystem error by ERRNO only, through the one redactor written for this
1973
+ // exact hazard (see its doc comment) — never a raw Node error message, which
1974
+ // interpolates the absolute path CLAUDE.md § Logging forbids surfacing.
1975
+ errnoOf,
1976
+ // FIX-1: the excerpt's CRLF over-read factor, exported so
1977
+ // the bounded-read test and the contract page HARVEST it.
1978
+ EXCERPT_CRLF_OVERREAD_FACTOR,
1979
+ // FIX-2: the unmatched-source-file note's predicate and one-line composer —
1980
+ // exported so tests and both rendered channels compose the line in one place.
1981
+ isUnmatchedNoteCandidate,
1982
+ renderUnmatchedSourceNote,
1983
+ // FIX-3/FIX-4: the status stamp's prefix, state vocabulary
1984
+ // and renderer, and the recorded-miss sentence — exported so tests and the
1985
+ // contract page harvest them rather than copy them.
1986
+ PRACTICES_VAULT_MISS_SENTENCE,
1987
+ PRACTICES_STAMP_PREFIX,
1988
+ PRACTICES_STAMP_STATES,
1989
+ renderPracticesStamp,
952
1990
  };