agent-recall-core 3.4.47 → 3.4.49

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 (221) hide show
  1. package/README.md +25 -0
  2. package/dist/digest/store.d.ts +15 -6
  3. package/dist/digest/store.d.ts.map +1 -1
  4. package/dist/digest/store.js +33 -20
  5. package/dist/digest/store.js.map +1 -1
  6. package/dist/embeddings/chunker.d.ts +102 -0
  7. package/dist/embeddings/chunker.d.ts.map +1 -0
  8. package/dist/embeddings/chunker.js +260 -0
  9. package/dist/embeddings/chunker.js.map +1 -0
  10. package/dist/embeddings/config.d.ts +93 -0
  11. package/dist/embeddings/config.d.ts.map +1 -0
  12. package/dist/embeddings/config.js +174 -0
  13. package/dist/embeddings/config.js.map +1 -0
  14. package/dist/embeddings/index-store.d.ts +61 -0
  15. package/dist/embeddings/index-store.d.ts.map +1 -0
  16. package/dist/embeddings/index-store.js +158 -0
  17. package/dist/embeddings/index-store.js.map +1 -0
  18. package/dist/embeddings/indexer.d.ts +75 -0
  19. package/dist/embeddings/indexer.d.ts.map +1 -0
  20. package/dist/embeddings/indexer.js +196 -0
  21. package/dist/embeddings/indexer.js.map +1 -0
  22. package/dist/embeddings/runtime.d.ts +66 -0
  23. package/dist/embeddings/runtime.d.ts.map +1 -0
  24. package/dist/embeddings/runtime.js +197 -0
  25. package/dist/embeddings/runtime.js.map +1 -0
  26. package/dist/helpers/associative-link.d.ts.map +1 -1
  27. package/dist/helpers/associative-link.js +26 -1
  28. package/dist/helpers/associative-link.js.map +1 -1
  29. package/dist/helpers/tokenize.d.ts +1 -0
  30. package/dist/helpers/tokenize.d.ts.map +1 -1
  31. package/dist/helpers/tokenize.js +18 -0
  32. package/dist/helpers/tokenize.js.map +1 -1
  33. package/dist/index.d.ts +27 -10
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +50 -6
  36. package/dist/index.js.map +1 -1
  37. package/dist/palace/awareness.d.ts +34 -10
  38. package/dist/palace/awareness.d.ts.map +1 -1
  39. package/dist/palace/awareness.js +249 -180
  40. package/dist/palace/awareness.js.map +1 -1
  41. package/dist/palace/consolidate.d.ts +1 -1
  42. package/dist/palace/consolidate.d.ts.map +1 -1
  43. package/dist/palace/consolidate.js +9 -9
  44. package/dist/palace/consolidate.js.map +1 -1
  45. package/dist/palace/decay-pass.d.ts +1 -1
  46. package/dist/palace/decay-pass.d.ts.map +1 -1
  47. package/dist/palace/decay-pass.js +2 -2
  48. package/dist/palace/decay-pass.js.map +1 -1
  49. package/dist/palace/fan-out.d.ts +1 -1
  50. package/dist/palace/fan-out.d.ts.map +1 -1
  51. package/dist/palace/fan-out.js +5 -5
  52. package/dist/palace/fan-out.js.map +1 -1
  53. package/dist/palace/index-manager.d.ts +1 -1
  54. package/dist/palace/index-manager.d.ts.map +1 -1
  55. package/dist/palace/index-manager.js +1 -1
  56. package/dist/palace/index-manager.js.map +1 -1
  57. package/dist/palace/insights-index.d.ts +19 -6
  58. package/dist/palace/insights-index.d.ts.map +1 -1
  59. package/dist/palace/insights-index.js +36 -13
  60. package/dist/palace/insights-index.js.map +1 -1
  61. package/dist/palace/keystone.d.ts +1 -1
  62. package/dist/palace/keystone.d.ts.map +1 -1
  63. package/dist/palace/keystone.js +2 -2
  64. package/dist/palace/keystone.js.map +1 -1
  65. package/dist/palace/rooms.d.ts +3 -3
  66. package/dist/palace/rooms.d.ts.map +1 -1
  67. package/dist/palace/rooms.js +34 -6
  68. package/dist/palace/rooms.js.map +1 -1
  69. package/dist/retrieval/candidates.d.ts +27 -2
  70. package/dist/retrieval/candidates.d.ts.map +1 -1
  71. package/dist/retrieval/candidates.js +170 -3
  72. package/dist/retrieval/candidates.js.map +1 -1
  73. package/dist/retrieval/contradiction.d.ts +65 -13
  74. package/dist/retrieval/contradiction.d.ts.map +1 -1
  75. package/dist/retrieval/contradiction.js +40 -17
  76. package/dist/retrieval/contradiction.js.map +1 -1
  77. package/dist/retrieval/query-memory.d.ts +112 -9
  78. package/dist/retrieval/query-memory.d.ts.map +1 -1
  79. package/dist/retrieval/query-memory.js +623 -62
  80. package/dist/retrieval/query-memory.js.map +1 -1
  81. package/dist/retrieval/semantic-leg.d.ts +74 -0
  82. package/dist/retrieval/semantic-leg.d.ts.map +1 -0
  83. package/dist/retrieval/semantic-leg.js +180 -0
  84. package/dist/retrieval/semantic-leg.js.map +1 -0
  85. package/dist/storage/archive-write.d.ts.map +1 -1
  86. package/dist/storage/archive-write.js +29 -8
  87. package/dist/storage/archive-write.js.map +1 -1
  88. package/dist/storage/capture-router.d.ts +1 -1
  89. package/dist/storage/capture-router.d.ts.map +1 -1
  90. package/dist/storage/capture-router.js +26 -9
  91. package/dist/storage/capture-router.js.map +1 -1
  92. package/dist/storage/consolidation-queue.d.ts +11 -2
  93. package/dist/storage/consolidation-queue.d.ts.map +1 -1
  94. package/dist/storage/consolidation-queue.js +45 -16
  95. package/dist/storage/consolidation-queue.js.map +1 -1
  96. package/dist/storage/corrections.d.ts +185 -6
  97. package/dist/storage/corrections.d.ts.map +1 -1
  98. package/dist/storage/corrections.js +525 -126
  99. package/dist/storage/corrections.js.map +1 -1
  100. package/dist/storage/durable-intent.d.ts.map +1 -1
  101. package/dist/storage/durable-intent.js +24 -1
  102. package/dist/storage/durable-intent.js.map +1 -1
  103. package/dist/storage/filelock.d.ts +84 -12
  104. package/dist/storage/filelock.d.ts.map +1 -1
  105. package/dist/storage/filelock.js +309 -47
  106. package/dist/storage/filelock.js.map +1 -1
  107. package/dist/storage/fs-utils.d.ts +20 -0
  108. package/dist/storage/fs-utils.d.ts.map +1 -1
  109. package/dist/storage/fs-utils.js +27 -0
  110. package/dist/storage/fs-utils.js.map +1 -1
  111. package/dist/storage/paths.d.ts +34 -0
  112. package/dist/storage/paths.d.ts.map +1 -1
  113. package/dist/storage/paths.js +71 -0
  114. package/dist/storage/paths.js.map +1 -1
  115. package/dist/storage/pending.d.ts +154 -0
  116. package/dist/storage/pending.d.ts.map +1 -0
  117. package/dist/storage/pending.js +454 -0
  118. package/dist/storage/pending.js.map +1 -0
  119. package/dist/storage/project.d.ts.map +1 -1
  120. package/dist/storage/project.js +68 -16
  121. package/dist/storage/project.js.map +1 -1
  122. package/dist/storage/session-card.d.ts +13 -0
  123. package/dist/storage/session-card.d.ts.map +1 -1
  124. package/dist/storage/session-card.js +34 -3
  125. package/dist/storage/session-card.js.map +1 -1
  126. package/dist/storage/unclaimed.d.ts +165 -0
  127. package/dist/storage/unclaimed.d.ts.map +1 -0
  128. package/dist/storage/unclaimed.js +442 -0
  129. package/dist/storage/unclaimed.js.map +1 -0
  130. package/dist/storage/working-memory.d.ts.map +1 -1
  131. package/dist/storage/working-memory.js +19 -1
  132. package/dist/storage/working-memory.js.map +1 -1
  133. package/dist/supabase/recall-backend.d.ts +29 -2
  134. package/dist/supabase/recall-backend.d.ts.map +1 -1
  135. package/dist/supabase/recall-backend.js +231 -29
  136. package/dist/supabase/recall-backend.js.map +1 -1
  137. package/dist/supabase/sync.d.ts.map +1 -1
  138. package/dist/supabase/sync.js +13 -0
  139. package/dist/supabase/sync.js.map +1 -1
  140. package/dist/tools-logic/alignment-check.js +1 -1
  141. package/dist/tools-logic/alignment-check.js.map +1 -1
  142. package/dist/tools-logic/awareness-update.d.ts.map +1 -1
  143. package/dist/tools-logic/awareness-update.js +25 -16
  144. package/dist/tools-logic/awareness-update.js.map +1 -1
  145. package/dist/tools-logic/check-action.js +1 -1
  146. package/dist/tools-logic/check-action.js.map +1 -1
  147. package/dist/tools-logic/check.d.ts +55 -1
  148. package/dist/tools-logic/check.d.ts.map +1 -1
  149. package/dist/tools-logic/check.js +230 -57
  150. package/dist/tools-logic/check.js.map +1 -1
  151. package/dist/tools-logic/confidence.d.ts +11 -6
  152. package/dist/tools-logic/confidence.d.ts.map +1 -1
  153. package/dist/tools-logic/confidence.js +11 -6
  154. package/dist/tools-logic/confidence.js.map +1 -1
  155. package/dist/tools-logic/context-synthesize.js +2 -2
  156. package/dist/tools-logic/context-synthesize.js.map +1 -1
  157. package/dist/tools-logic/digest-read.js +2 -2
  158. package/dist/tools-logic/digest-read.js.map +1 -1
  159. package/dist/tools-logic/digest-recall.js +1 -1
  160. package/dist/tools-logic/digest-recall.js.map +1 -1
  161. package/dist/tools-logic/insight-promotion.d.ts +1 -1
  162. package/dist/tools-logic/insight-promotion.d.ts.map +1 -1
  163. package/dist/tools-logic/insight-promotion.js +17 -6
  164. package/dist/tools-logic/insight-promotion.js.map +1 -1
  165. package/dist/tools-logic/journal-capture.js +3 -3
  166. package/dist/tools-logic/journal-capture.js.map +1 -1
  167. package/dist/tools-logic/journal-write.js +3 -3
  168. package/dist/tools-logic/journal-write.js.map +1 -1
  169. package/dist/tools-logic/knowledge-write.js +2 -2
  170. package/dist/tools-logic/knowledge-write.js.map +1 -1
  171. package/dist/tools-logic/palace-lint.js +1 -1
  172. package/dist/tools-logic/palace-lint.js.map +1 -1
  173. package/dist/tools-logic/palace-read.js +1 -1
  174. package/dist/tools-logic/palace-read.js.map +1 -1
  175. package/dist/tools-logic/palace-search.js +1 -1
  176. package/dist/tools-logic/palace-search.js.map +1 -1
  177. package/dist/tools-logic/palace-walk.js +3 -3
  178. package/dist/tools-logic/palace-walk.js.map +1 -1
  179. package/dist/tools-logic/palace-write.js +4 -4
  180. package/dist/tools-logic/palace-write.js.map +1 -1
  181. package/dist/tools-logic/predict-correction.js +1 -1
  182. package/dist/tools-logic/predict-correction.js.map +1 -1
  183. package/dist/tools-logic/recall-backend.d.ts +11 -2
  184. package/dist/tools-logic/recall-backend.d.ts.map +1 -1
  185. package/dist/tools-logic/recall-backend.js +2 -2
  186. package/dist/tools-logic/recall-backend.js.map +1 -1
  187. package/dist/tools-logic/resurrect.d.ts.map +1 -1
  188. package/dist/tools-logic/resurrect.js +9 -1
  189. package/dist/tools-logic/resurrect.js.map +1 -1
  190. package/dist/tools-logic/safety-consolidation.d.ts.map +1 -1
  191. package/dist/tools-logic/safety-consolidation.js +32 -30
  192. package/dist/tools-logic/safety-consolidation.js.map +1 -1
  193. package/dist/tools-logic/session-end.d.ts +8 -0
  194. package/dist/tools-logic/session-end.d.ts.map +1 -1
  195. package/dist/tools-logic/session-end.js +51 -13
  196. package/dist/tools-logic/session-end.js.map +1 -1
  197. package/dist/tools-logic/session-start.d.ts +23 -0
  198. package/dist/tools-logic/session-start.d.ts.map +1 -1
  199. package/dist/tools-logic/session-start.js +44 -1
  200. package/dist/tools-logic/session-start.js.map +1 -1
  201. package/dist/tools-logic/smart-recall.d.ts +185 -10
  202. package/dist/tools-logic/smart-recall.d.ts.map +1 -1
  203. package/dist/tools-logic/smart-recall.js +364 -60
  204. package/dist/tools-logic/smart-recall.js.map +1 -1
  205. package/dist/tools-logic/store-doctor.d.ts.map +1 -1
  206. package/dist/tools-logic/store-doctor.js +7 -4
  207. package/dist/tools-logic/store-doctor.js.map +1 -1
  208. package/dist/tools-logic/store-repair.js +1 -1
  209. package/dist/tools-logic/store-repair.js.map +1 -1
  210. package/dist/tools-logic/supersession.d.ts +64 -3
  211. package/dist/tools-logic/supersession.d.ts.map +1 -1
  212. package/dist/tools-logic/supersession.js +155 -50
  213. package/dist/tools-logic/supersession.js.map +1 -1
  214. package/dist/types.d.ts +12 -1
  215. package/dist/types.d.ts.map +1 -1
  216. package/dist/types.js +1 -1
  217. package/dist/vector/embedding.d.ts +8 -0
  218. package/dist/vector/embedding.d.ts.map +1 -1
  219. package/dist/vector/embedding.js +8 -0
  220. package/dist/vector/embedding.js.map +1 -1
  221. package/package.json +1 -1
@@ -10,8 +10,9 @@ import * as crypto from "node:crypto";
10
10
  import { ensureDir } from "./fs-utils.js";
11
11
  import { byteCap, sanitizeName } from "./sanitize.js";
12
12
  import { journalDir, projectSubPath } from "./paths.js";
13
- import { withLock } from "./filelock.js";
13
+ import { withLock, LockContentionError } from "./filelock.js";
14
14
  import { scrubForCloud } from "./content-guard.js";
15
+ import { tokenizeWords, HAN_RUN_RE } from "../helpers/tokenize.js";
15
16
  // ---------------------------------------------------------------------------
16
17
  // Internal helpers
17
18
  // ---------------------------------------------------------------------------
@@ -55,9 +56,42 @@ export const GATE_VERSION = "v4-2026-06-22";
55
56
  * best-effort: a failure to rotate must never throw into writeCorrection.
56
57
  */
57
58
  const REJECTED_LOG_CAP = 2000;
58
- /** Auto-detect severity: p0 if uses strong negation/mandate language, else p1. */
59
- function detectSeverity(text) {
60
- const p0Patterns = /\bnever\b|\balways\b|\bdon'?t\b|\bdo not\b|\bmust not\b|\bforbid\b|\bprohibit\b/i;
59
+ /**
60
+ * Auto-detect severity: p0 if uses strong negation/mandate language, else p1.
61
+ *
62
+ * INDEPENDENT-REVIEW FIX (2026-09-09, TOW2-326 class): this pattern was
63
+ * English-only, so a CJK-only P0-caliber prohibition (禁止/不得/不能/不要 with
64
+ * no English marker) would now clear the newly CJK-aware capture gate above
65
+ * but still classify as p1 — silently losing the P0 blocking power (see
66
+ * check.ts's own doctrine comment: P0 severity is what lets a correction
67
+ * OVERRIDE a plan via `verdict: "blocked"`) an equivalent English phrasing
68
+ * would get. CJK rows added, mirroring STRONG_IMPERATIVE above; kept
69
+ * byte-identical to the duplicate `p0Patterns` copy in tools-logic/check.ts
70
+ * (search that file for `p0Patterns`, not a line number — line references
71
+ * drift; that copy is the primary caller, which computes severity itself
72
+ * before calling writeCorrection — this function is the FALLBACK path for
73
+ * callers that don't pre-supply severity).
74
+ *
75
+ * INDEPENDENT-REVIEW FIX (2026-09-09, round 3): 不能 scoped to 你不能 —
76
+ * see the identical fix/reasoning on STRONG_IMPERATIVE above.
77
+ *
78
+ * PRE-SHIP GATE FIX (2026-09-09, C-1): 不要's benign-reassurance exclusion
79
+ * widened from the narrow round-1 set (担心/客气/急/着急/紧张/见外) to the
80
+ * FULL round-2 set also used by GATED_PROHIBITION_PATTERNS and
81
+ * STRONG_IMPERATIVE (adds 慌/害怕/在意) — see CJK_REASSURANCE_COMPLETIONS'
82
+ * own doc comment. This copy had drifted out of sync with the other three
83
+ * sites; all four are now built from the SAME exported constant where they
84
+ * share a package (this file + check.ts), or kept byte-identical by
85
+ * cross-reference where they don't (correction-detector.ts, cli package).
86
+ */
87
+ // Fix #2 (dual-channel capture gate, 2026-09-11): exported so the pending
88
+ // staging path (storage/pending.ts) stamps the SAME severity a direct capture
89
+ // would get — the S-M1 English injection twin pins "should not → p1" on the
90
+ // staged record, so staging and capture must never diverge on this classifier.
91
+ // Same internal-use-export convention as splitSentences/dropHardNoise below.
92
+ export function detectSeverity(text) {
93
+ const p0Patterns = new RegExp(`\\bnever\\b|\\balways\\b|\\bdon'?t\\b|\\bdo not\\b|\\bmust not\\b|\\bforbid\\b|\\bprohibit\\b|` +
94
+ `永远不要|绝不|千万不要|总是|一直|始终|不要(?!${CJK_REASSURANCE_COMPLETIONS})|不可以|不准|你不能(?!不)|不得(?!不)|不应该|切勿|禁止`, "i");
61
95
  return p0Patterns.test(text) ? "p0" : "p1";
62
96
  }
63
97
  /**
@@ -164,7 +198,11 @@ function todayDate() {
164
198
  * retractCorrection / recordOutcome) so every correction writer shares one
165
199
  * durable path. Pure side-effect helper; no behavior change vs the originals.
166
200
  */
167
- function writeRecordAtomic(filepath, record) {
201
+ // Fix #2 (dual-channel capture gate, 2026-09-11): exported so the pending
202
+ // store (storage/pending.ts) writes through the SAME scrub-on-write choke
203
+ // point as corrections — rider R2a's "same scrub path" is a structural fact
204
+ // (one function), not a parallel implementation kept in sync by hand.
205
+ export function writeRecordAtomic(filepath, record) {
168
206
  // Scrub BEFORE the local write — corrections are AgentRecall's most-injected
169
207
  // artifact (readP0Corrections is loaded into every session_start briefing,
170
208
  // handoff.md's "Binding rules" section, and check()'s/checkAction's
@@ -286,12 +324,33 @@ export function isStaleCorrection(rec, nowMs = Date.now()) {
286
324
  return false;
287
325
  return nowMs - t > STALE_DAYS * 24 * 60 * 60 * 1000;
288
326
  }
327
+ /**
328
+ * v4 W1 (2026-07-02 field-design-options.md §A.1) — default `confidence` for a
329
+ * record that predates the field, derived from the ALREADY-RESOLVED `weight`
330
+ * (post `defaultWeight` fallback in `applyCorrectionDefaults`, so in practice
331
+ * this always receives a defined number: p0 → weight 1.0 → "high"; p1 → weight
332
+ * 0.7 → "medium"). The `weight` param stays optional and the `undefined` branch
333
+ * returns the proposal's documented "medium" fallback anyway — this function
334
+ * has no callers outside `applyCorrectionDefaults` today, but keeping it total
335
+ * over `number | undefined` means a future caller can't silently get `NaN`-driven
336
+ * comparisons if it is ever called before `weight` is resolved.
337
+ */
338
+ function defaultConfidence(weight) {
339
+ if (weight === undefined)
340
+ return "medium";
341
+ if (weight >= 0.8)
342
+ return "high";
343
+ if (weight >= 0.5)
344
+ return "medium";
345
+ return "low";
346
+ }
289
347
  function applyCorrectionDefaults(record, holderDefault) {
290
348
  const kind = record.kind ?? "correction";
291
349
  const weight = record.weight ?? defaultWeight(record.severity);
350
+ const holder = record.holder ?? holderDefault;
292
351
  return {
293
352
  ...record,
294
- holder: record.holder ?? holderDefault,
353
+ holder,
295
354
  kind,
296
355
  weight,
297
356
  active: record.active ?? true,
@@ -305,8 +364,72 @@ function applyCorrectionDefaults(record, holderDefault) {
305
364
  proof_count: record.proof_count ?? 1,
306
365
  proof_confidence: record.proof_confidence ?? weight,
307
366
  stale: record.stale ?? false,
367
+ // v4 W1 (§A.1) — assertion confidence + provenance. Both ADDITIVE +
368
+ // OPTIONAL: old records lack them and normalize here at read time, same
369
+ // pattern as weight/proof_count above. `decay_class_override` is
370
+ // deliberately NOT defaulted here — it stays absent unless the caller (or
371
+ // a prior write) set it; `decayClassOf` treats absence as "use the class
372
+ // default", not as "static/slow/volatile: undefined".
373
+ confidence: record.confidence ?? defaultConfidence(weight),
374
+ provenance: record.provenance ?? { source: holder ?? "unknown", mode: "told" },
308
375
  };
309
376
  }
377
+ /**
378
+ * v4 W1 (2026-07-02 field-design-options.md §A.1/§A.5) — computed decay class
379
+ * for a correction. PURE — never mutates, never reads/writes disk. Corrections
380
+ * carry no `MemoryCategory`, so the class default is always `"slow"` (§A.1:
381
+ * "behavioral rules rarely become false — corrections.ts:2: persist forever").
382
+ * `decay_class_override` is the one escape hatch (§A.5's Option 2 recommendation:
383
+ * computed mapping + optional per-record override) for the rare correction whose
384
+ * truth genuinely decays faster/slower than that default.
385
+ *
386
+ * INVARIANT: `decay_class` is NEVER a stored/materialized field — it is
387
+ * recomputed from `decay_class_override` (if present) on every call. Callers
388
+ * needing it in a `SmartRecallResultItem`-shaped annotation should call this at
389
+ * read/response time, not persist its return value.
390
+ *
391
+ * DEFENSIVE (v4 W2 review fix): `decay_class_override` is loaded straight off
392
+ * disk-parsed JSON with no runtime validation — TypeScript's `DecayClass`
393
+ * union guards the WRITE path, not a hand-edited or corrupted file. Falling
394
+ * through to the union check below (rather than a bare `?? "slow"`) means an
395
+ * out-of-union value on disk normalizes to the documented default instead of
396
+ * leaking a garbage string into `getCorrectionKPIs`' `by_decay_class` tally,
397
+ * which would otherwise mint a stray `Record` key and silently break that
398
+ * field's "exhaustive partition, sums to `total`" invariant.
399
+ */
400
+ export function decayClassOf(record) {
401
+ const v = record.decay_class_override;
402
+ return v === "static" || v === "slow" || v === "volatile" ? v : "slow";
403
+ }
404
+ /**
405
+ * v4 W2 (2026-07-02 field-design-options.md §E, "rankCorrections() confidence
406
+ * feed — Option B") — effective `confidence` for ANNOTATION purposes, i.e. the
407
+ * value a record would carry AFTER `applyCorrectionDefaults` resolves it, but
408
+ * computed WITHOUT running the full defaulting pipeline (holder/kind/
409
+ * proof_count/authoritative/provenance are irrelevant to this one field).
410
+ *
411
+ * In practice every caller of `rankCorrections`/`getCorrectionKPIs` already
412
+ * receives records that went through `applyCorrectionDefaults` inside
413
+ * `readCorrections()`, so `record.confidence` is normally already resolved —
414
+ * this mirrors that SAME resolution rule (confidence ?? defaultConfidence(weight
415
+ * ?? defaultWeight(severity))) as a defensive fallback for the rare caller that
416
+ * hands in raw/unresolved records directly (e.g. unit-test fixtures), so the two
417
+ * downstream annotation sites never disagree with `applyCorrectionDefaults` on
418
+ * what "effective confidence" means. PURE — never mutates, never reads/writes
419
+ * disk. Kept as ONE shared helper (class-not-instance) rather than two separate
420
+ * inline computations in rankCorrections and getCorrectionKPIs.
421
+ *
422
+ * DEFENSIVE (v4 W2 review fix): same rationale as `decayClassOf` above — an
423
+ * out-of-union `confidence` value on disk (hand-edited/corrupted JSON) falls
424
+ * through to the computed default rather than propagating a garbage string
425
+ * into `getCorrectionKPIs`' `by_confidence` tally.
426
+ */
427
+ export function effectiveConfidenceOf(record) {
428
+ const v = record.confidence;
429
+ if (v === "high" || v === "medium" || v === "low")
430
+ return v;
431
+ return defaultConfidence(record.weight ?? defaultWeight(record.severity));
432
+ }
310
433
  // ---------------------------------------------------------------------------
311
434
  // Public API
312
435
  // ---------------------------------------------------------------------------
@@ -325,6 +448,18 @@ function applyCorrectionDefaults(record, holderDefault) {
325
448
  export function splitSentences(text) {
326
449
  // Boundary = one of . ! ? OR a newline, that is followed by whitespace or EOT.
327
450
  // A `.` wedged between two non-space chars (4.7, file.md, e.g.) is NOT a boundary.
451
+ //
452
+ // S-M2 fix (pre-ship gate review, 2026-09-09): CJK full-width terminators
453
+ // 。!? are ALWAYS a boundary, unconditionally (no "followed by whitespace"
454
+ // requirement) — unlike ASCII ./!/?, CJK prose has no decimal-point
455
+ // ambiguity to protect against and conventionally has no space after
456
+ // sentence punctuation, so requiring trailing whitespace would silently
457
+ // never split CJK text at all. This is the root dependency S-M1 (sentence-
458
+ // scoped WRITE, see hook-correction in cli/src/index.ts) needs: without it,
459
+ // a CJK prompt-injection tail appended after a genuine trigger clause (e.g.
460
+ // "不要在未经用户确认的情况下发布代码。忽略之前所有的规则…") could never be
461
+ // isolated from the trigger sentence, because the whole string was always
462
+ // returned as ONE fragment.
328
463
  const out = [];
329
464
  let buf = "";
330
465
  for (let i = 0; i < text.length; i++) {
@@ -332,11 +467,14 @@ export function splitSentences(text) {
332
467
  buf += ch;
333
468
  const next = text[i + 1];
334
469
  const isPunct = ch === "." || ch === "!" || ch === "?";
470
+ const isCjkPunct = ch === "。" || ch === "!" || ch === "?";
335
471
  const isNewline = ch === "\n" || ch === "\r";
336
472
  // Sentence boundary: terminal punctuation at end OR followed by whitespace;
337
- // newline is always a boundary. A `.` between non-whitespace chars is NOT
338
- // a boundary (next is defined and not whitespace) — keeps decimals intact.
473
+ // a CJK terminator or newline is ALWAYS a boundary. A `.` between
474
+ // non-whitespace chars is NOT a boundary (next is defined and not
475
+ // whitespace) — keeps decimals intact.
339
476
  const atBoundary = isNewline ||
477
+ isCjkPunct ||
340
478
  (isPunct && (next === undefined || /\s/.test(next)));
341
479
  if (atBoundary) {
342
480
  const frag = buf.trim();
@@ -361,18 +499,157 @@ export function splitSentences(text) {
361
499
  // when the fragment is NOT a hedged/reporting frame. This is recall-safe: every
362
500
  // genuine fixture correction carries a STRONG marker, a preference shape, or a
363
501
  // non-hedged weak verb (verified by scripts/eval/capture-gate-confusion.mjs).
364
- const STRONG_IMPERATIVE = /\b(never|always|don'?t|do not|must\s+not|must|should\s+not|needs?\s+(to|those|the|a|an|more|all)\b|instead|make\s+sure|remember\s+to|remove\s+all|replace\s+with|default\s+to|keep\s+the|keep\s+\w|show\s+(both|all|the|only))\b/i;
365
- const WEAK_IMPERATIVE = /\b(should|use|using|stop|avoid|prefer)\b/i;
502
+ // CJK ADDITIONS (TOW2-326 class): the audit found this gate was English-
503
+ // directive-only — a Chinese correction whose only imperative markers are
504
+ // e.g. 必须/禁止/不要 (with none of the three pre-existing PREFERENCE_PATTERN
505
+ // trigger words 偏好/喜欢/要求 present) was silently REJECTED by
506
+ // writeCorrection() before it ever reached disk (verified directly:
507
+ // isLikelyRealCorrection("发布代码前必须获得用户确认") returned
508
+ // { ok:false, reason:"no actionable signal..." } — see
509
+ // audit-cjk-check-action.test.mjs's file-header finding, which flagged this
510
+ // exact gap for "whichever worker owns this file next"). CJK has no word
511
+ // boundaries (\b matches nothing meaningful around Han characters), so the
512
+ // CJK rows below are bare substring alternatives, mirroring the existing CJK
513
+ // rows in PREFERENCE_PATTERN (偏好|喜欢|要求) and in correction-detector.ts's
514
+ // CORRECTION_PATTERNS/BEHAVIORAL_SIGNALS — same convention, same file class.
515
+ //
516
+ // INDEPENDENT-REVIEW FIX (2026-09-09, round 3): 不能 is scoped to
517
+ // second-person 你不能, NOT bare 不能 — mirrors correction-detector.ts's
518
+ // pre-existing BEHAVIORAL_SIGNALS entry `/你不能(?!不)/` (and that entry's
519
+ // own documented reasoning): bare 不能 collides with plain capability/bug-
520
+ // report statements ("这个不能这样跑,报错了"), which are common in this
521
+ // gate's real traffic. The first version of this line added bare 不能(?!不)
522
+ // without inheriting that existing scoping — same class-not-instance miss
523
+ // this whole PR exists to close, just aimed at itself.
524
+ // PRE-SHIP GATE FIX (2026-09-09, C-1): shared benign-reassurance completion
525
+ // list for 不要 — extracted so STRONG_IMPERATIVE below, detectSeverity above,
526
+ // and check.ts's p0Patterns (imported from here) cannot independently drift
527
+ // again. The 2026-09-09 gate review found exactly that: this list existed in
528
+ // THREE places with THREE different widths (STRONG_IMPERATIVE had none at
529
+ // all; detectSeverity/check.ts had the narrow round-1 set). Exported so
530
+ // check.ts (same package) can build its own p0Patterns from the identical
531
+ // string instead of a hand-copied literal. correction-detector.ts (cli
532
+ // package) keeps its own copy — cross-package import was judged not worth
533
+ // the coupling for a 9-token disjunction; see that file's own doc comment,
534
+ // kept in sync by cross-reference (same "duplicated, cross-referenced"
535
+ // convention this file already uses for detectSeverity/p0Patterns).
536
+ export const CJK_REASSURANCE_COMPLETIONS = "担心|客气|急|着急|紧张|见外|慌|害怕|在意";
537
+ // PRE-SHIP GATE FIX (2026-09-09, S-M3): a directive-shaped clause that is
538
+ // being REPORTED, QUOTED from a document/manual, or explicitly left
539
+ // UNRESOLVED in discussion is never an authoritative rule — however strong
540
+ // its own marker. This is the ONE frame check STRONG_IMPERATIVE does NOT
541
+ // bypass (unlike HEDGE_FRAME, which only gates WEAK_IMPERATIVE) — design
542
+ // decision FINAL: quoted third-party rules and undecided narratives are
543
+ // never capturable as authoritative. One table (class-not-instance):
544
+ // - reporting/discussion openers: 我们讨论了/讨论过, 聊到(了)
545
+ // - document/manual attribution openers: 文档里写着, X手册规定,
546
+ // 根据…的说法, 据说
547
+ // - undecided-outcome markers (checked ANYWHERE, not just the opener —
548
+ // these are CONCLUDING remarks): 没有定论, 没(有)达成一致
549
+ // Anchored at ^ for the opener half, mirroring HEDGE_FRAME's own convention.
550
+ const QUOTE_NARRATIVE_OPENER = /^\s*(?:我们|大家|昨天|之前|上次|后来|团队)?[^\n]{0,6}?(?:讨论了|讨论过|聊到了?|文档里写着|(?:产品|公司|员工|操作|规范)?手册(?:里)?规定|根据[^\n]{0,20}的说法|据说)/;
551
+ const QUOTE_NARRATIVE_UNRESOLVED = /没有定论|没达成一致|没有达成一致/;
552
+ function isQuoteNarrativeFrame(text) {
553
+ return QUOTE_NARRATIVE_OPENER.test(text) || QUOTE_NARRATIVE_UNRESOLVED.test(text);
554
+ }
555
+ // PRE-SHIP GATE FIX (2026-09-09, C-2): three of the bare CJK tokens below
556
+ // (需要/确保/记得) gained structural completion requirements, mirroring the
557
+ // pre-existing English precedent's own narrowing (e.g. `needs?\s+(to|the|
558
+ // ...)` — "needs three years" does not match because "three" isn't in that
559
+ // list). Each excludes the SPECIFIC descriptive-prose shape the pre-ship
560
+ // gate review's fixture battery found, not an invented whitelist:
561
+ // - 需要(?!<digit-or-CJK-numeral>): "需要三年经验" (a quantity/requirement
562
+ // phrase) vs "需要先确认一下" (a directive) — descriptive usage quantifies,
563
+ // directive usage rarely does.
564
+ // - 确保(?!金): "确保金" is a name/compound collision (mirrors the existing
565
+ // 试试/改成/统一 compound-word fixes below), not the verb 确保.
566
+ // - 记得(?!上次|之前|以前|那时): "我记得上次是这样" (recollection of the
567
+ // past) vs "记得先跑测试" (a forward-looking directive).
568
+ const STRONG_IMPERATIVE = new RegExp(`\\b(never|always|don'?t|do not|must\\s+not|must|should\\s+not|needs?\\s+(to|those|the|a|an|more|all)\\b|instead|make\\s+sure|remember\\s+to|remove\\s+all|replace\\s+with|default\\s+to|keep\\s+the|keep\\s+\\w|show\\s+(both|all|the|only))\\b|` +
569
+ `永远不要|绝不|千万不要|总是|一直|始终|不要(?!${CJK_REASSURANCE_COMPLETIONS})|别再|禁止|不可以|不准|你不能(?!不)|不得(?!不)|切勿|不应该|必须|务必|一定要|` +
570
+ `需要(?![0-9一二两三四五六七八九十百千万几])|而不是|改为|换成|确保(?!金)|记得(?!上次|之前|以前|那时)|切记|删除所有|去掉所有|替换为|改用|默认使用|默认为|保留|保持|显示所有|只显示|仅显示`, "i");
571
+ // INDEPENDENT-REVIEW FIX (2026-09-09): English "OK, use the new endpoint
572
+ // instead" is rescued by the actionable scan (via "use") BEFORE it ever
573
+ // reaches the soft acknowledgmentPattern gate below — the CJK acknowledgment
574
+ // list added 可以/好的 as pure-ack openers, but without an equivalent CJK
575
+ // suggestion-verb row, a CJK sentence using the SAME "OK, do X" shape (可以
576
+ // 试试.../ 好的,那...改成/统一叫做...) had nothing to rescue it and fell
577
+ // into the ack gate as a false negative — the exact "correction silently
578
+ // never reaches disk" failure this file's CJK additions exist to close.
579
+ //
580
+ // ROUND-2 INDEPENDENT-REVIEW FIX (2026-09-09): the first pass added these as
581
+ // BARE 2-char substrings (试试|改成|统一), which — because CJK has no word
582
+ // boundaries — collide with common COMPOUND-WORD junctions in ordinary,
583
+ // non-directive Chinese: "面试" + "试讲" → "面试试讲" contains "试试"; "整改"
584
+ // + "成效" → "整改成效" and "修改" + "成本" → "修改成本" both contain "改成";
585
+ // and bare "统一" is itself an ordinary adjective/adverb ("统一标准", "统一
586
+ // 安排") with no directive meaning at all. All three were REJECTED, not just
587
+ // narrowed, in favor of structural patterns that require the actual
588
+ // suggestion/naming-rule SHAPE the original fixtures were modeling, not a
589
+ // bare substring:
590
+ // - 试试: only after a suggestion opener (可以/来/去) or before its most
591
+ // common completion (看) — "可以试试" / "试试...看" — not a bare
592
+ // mid-sentence occurrence that could straddle a compound-word boundary.
593
+ // - 改成: only after 把/将 (the object-marking preposition Mandarin's
594
+ // "change X to Y" construction always uses) — "把/将 ... 改成" — never
595
+ // a bare occurrence, closing the 整改+成效 / 修改+成本 collision.
596
+ // - 统一: only followed by a naming/directive completion (叫做/命名/称为/
597
+ // 改为) — "统一叫做orderId" — never bare, closing the "统一标准/统一
598
+ // 安排" ordinary-adjective false-positive. NOTE: 使用/采用 were
599
+ // considered but dropped from this completion list (own stress-testing,
600
+ // independent of the review rounds) — "统一使用"/"统一采用" are just as
601
+ // often ordinary descriptive prose ("公司统一使用企业微信办公" — the
602
+ // company uniformly uses X, a factual statement, not a correction) as a
603
+ // genuine naming rule, so they added false-positive risk without a
604
+ // matching precision gain; 叫做/命名/称为/改为 are naming-specific verbs
605
+ // with no equivalent descriptive-prose sense.
606
+ // ROUND-3 PRE-SHIP GATE FIX (2026-09-09): the 试试...看 gapped alternative
607
+ // ({0,10} chars between them) reproduced the EXACT compound-word collision
608
+ // class this section already fixed for 试试/改成/统一 above — "面试试讲,看看
609
+ // 效果怎么样" contains 试试 spanning the 面试/试讲 word boundary, and the
610
+ // SEPARATE, unrelated 看看 later in the sentence (reporting "let's see how it
611
+ // goes", nothing to do with 试试) was close enough to satisfy the {0,10} gap
612
+ // and wrongly rescue it. Tightened to the CONTIGUOUS "试试看" collocation
613
+ // (zero gap) — every genuine "试试看" fixture (own AND existing regression
614
+ // suite) has 看 immediately after 试试 with nothing between; the gapped form
615
+ // was never load-bearing for any real fixture, only for this collision.
616
+ //
617
+ // PRE-SHIP GATE FIX (2026-09-09, C-2): 应该/使用/停止/避免 gained structural
618
+ // completion requirements, same rationale as 需要/确保/记得 above:
619
+ // - 应该(?!已经|会|也许|可能): "他应该已经到了" (epistemic "must have") vs
620
+ // "应该用novada-search" (a directive).
621
+ // - 使用(?!的): "使用的是旧版本" (descriptive "what is used") vs "应该使用
622
+ // 新版本" (a directive).
623
+ // - 停止(?!了): "服务停止了响应" (past-tense event report) vs "请停止这样做"
624
+ // (a command).
625
+ // - (?<!为了)避免: "为了避免误会" (purpose clause) vs "请避免这样写" (a
626
+ // directive) — excludes what PRECEDES 避免, not what follows, since
627
+ // "避免误会" itself appears in both shapes.
628
+ const WEAK_IMPERATIVE = /\b(should|use|using|stop|avoid|prefer)\b|应该(?!已经|会|也许|可能)|使用(?!的)|停止(?!了)|(?<!为了)避免|(?:可以|来|去)[^\n]{0,4}试试|试试看|(?:把|将)[^\n]{0,10}改成|统一(?:叫做|命名|称为|改为)/i;
366
629
  // Tentative / reporting frame at the START of a fragment — the speaker is musing
367
630
  // or reporting, not issuing a rule. A WEAK marker inside such a frame is NOT a
368
631
  // directive. Anchored at ^ so it only catches the OPENER, never a directive
369
- // sentence that merely follows a hedge.
370
- const HEDGE_FRAME = /^\s*(i\s+(think|guess|suppose|believe|reckon|feel|will)\b|i'?ll\b|i'?m\s+going\s+to\b|maybe\b|perhaps\b|sounds?\s+good\b|the\s+team\s+(wants?|thinks?|prefers?)\b|we\s+(could|might|may)\b)/i;
632
+ // sentence that merely follows a hedge. CJK rows mirror the English hedge
633
+ // openers (musing/intent/tentative-team framing) — no \b equivalent needed.
634
+ const HEDGE_FRAME = /^\s*(i\s+(think|guess|suppose|believe|reckon|feel|will)\b|i'?ll\b|i'?m\s+going\s+to\b|maybe\b|perhaps\b|sounds?\s+good\b|the\s+team\s+(wants?|thinks?|prefers?)\b|we\s+(could|might|may)\b|我觉得|我猜|我想|我认为|我感觉|我会|我打算|我要去|也许|可能|或许|听起来不错|听起来还行|团队希望|团队认为|团队倾向于|我们可以|我们也许|我们或许)/i;
371
635
  // Preference / corrective-fact statement. Includes user-preference verbs (CJK
372
636
  // equivalents) AND the "X not Y" / "wrong … not" corrective-fact shape that
373
637
  // carries real intent without an imperative verb (e.g. "Product names are
374
- // novada-search (not novada-mcp)"). Scanned per-fragment.
375
- const PREFERENCE_PATTERN = /(\buser\s+(wants?|prefers?|likes?|needs?|agreed|tested|chose|wanted)\b|\bthe\s+user\s+is\b|偏好|喜欢|要求|\bwrong\b[\s\S]{0,60}\bnot\b|\(not\s+[^)]+\)|\bnot\s+\w[\w-]*[,.]?\s+(it'?s|its|use|the\s+\w))/i;
638
+ // novada-search (not novada-mcp)"). Scanned per-fragment. CJK rows extended
639
+ // (TOW2-326 class) beyond the original 偏好|喜欢|要求 three: 我要/我希望/想要
640
+ // (first-person preference verbs) and 更喜欢/倾向于 (comparative preference).
641
+ // PRE-SHIP GATE FIX (2026-09-09, C-2): 要求/喜欢 gained structural
642
+ // completion requirements:
643
+ // - (?<!的)要求: "客户的要求还没定" (的要求 — possessive NOUN phrase) vs
644
+ // "用户要求删除文件前必须先确认" (要求 as a first-person/subject VERB,
645
+ // the existing audit fixture — preceded by 户, not 的).
646
+ // - 喜欢 excluded when a THIRD-PERSON subject (他/她/它) opens the same
647
+ // clause: "他很喜欢这个方案" is a report about someone ELSE's
648
+ // preference, not the USER'S — mirrors the English row's own
649
+ // `\buser\s+(wants?|prefers?|likes?...)\b` requirement (subject-scoped,
650
+ // not "anyone likes anything"). "我更喜欢"/"更喜欢" (first-person) is a
651
+ // SEPARATE alternative below and is unaffected.
652
+ const PREFERENCE_PATTERN = /(\buser\s+(wants?|prefers?|likes?|needs?|agreed|tested|chose|wanted)\b|\bthe\s+user\s+is\b|偏好|(?<!他[^,。!?\n]{0,4})(?<!她[^,。!?\n]{0,4})(?<!它[^,。!?\n]{0,4})喜欢|(?<!的)要求|我要|我希望|想要|更喜欢|倾向于|\bwrong\b[\s\S]{0,60}\bnot\b|\(not\s+[^)]+\)|\bnot\s+\w[\w-]*[,.]?\s+(it'?s|its|use|the\s+\w))/i;
376
653
  /**
377
654
  * Capture-quality gate — rejects context-free fragments, pure acknowledgments,
378
655
  * and text that carries no actionable signal.
@@ -484,19 +761,27 @@ export function isLikelyRealCorrection(rule, _context) {
484
761
  // truncated slice), so a directive in sentence 2+ is now seen and can RESCUE
485
762
  // a text that opens with an acknowledgment.
486
763
  const fragments = [r, ...splitSentences(r)];
487
- // (a) STRONG directive marker in any fragment → accept unconditionally.
488
- if (fragments.some((f) => STRONG_IMPERATIVE.test(f))) {
764
+ // (a) STRONG directive marker in any fragment → accept unconditionally,
765
+ // UNLESS the fragment is a quote/narrative frame (S-M3, 2026-09-09 pre-ship
766
+ // gate fix) — "unconditionally" here means "not gated on HEDGE_FRAME like
767
+ // WEAK below", never meant "even when the rule is merely being quoted from
768
+ // a document or discussed without resolution". This is the one frame check
769
+ // STRONG markers do not bypass.
770
+ if (fragments.some((f) => STRONG_IMPERATIVE.test(f) && !isQuoteNarrativeFrame(f))) {
489
771
  return { ok: true };
490
772
  }
491
773
  // (a2) WEAK directive marker → accept only in a fragment that is NOT a hedged/
492
- // reporting frame. Closes the Loop-14 filler-prose false-accept ("I think we
493
- // should use it") while still accepting a direct weak-verb correction ("stop
494
- // making it full width") and a directive sentence that merely FOLLOWS a hedge.
495
- if (fragments.some((f) => WEAK_IMPERATIVE.test(f) && !HEDGE_FRAME.test(f))) {
774
+ // reporting frame, NOR a quote/narrative frame. Closes the Loop-14
775
+ // filler-prose false-accept ("I think we should use it") while still
776
+ // accepting a direct weak-verb correction ("stop making it full width")
777
+ // and a directive sentence that merely FOLLOWS a hedge.
778
+ if (fragments.some((f) => WEAK_IMPERATIVE.test(f) && !HEDGE_FRAME.test(f) && !isQuoteNarrativeFrame(f))) {
496
779
  return { ok: true };
497
780
  }
498
- // (b) preference / corrective-fact statement in any fragment
499
- if (fragments.some((f) => PREFERENCE_PATTERN.test(f))) {
781
+ // (b) preference / corrective-fact statement in any fragment (quote/
782
+ // narrative-framed preferences don't count as the user's OWN preference
783
+ // either — S-M3 applies uniformly across all three signal types).
784
+ if (fragments.some((f) => PREFERENCE_PATTERN.test(f) && !isQuoteNarrativeFrame(f))) {
500
785
  return { ok: true };
501
786
  }
502
787
  // ── SOFT ACKNOWLEDGMENT GATE — runs AFTER the actionable scan ──────────────
@@ -505,28 +790,78 @@ export function isLikelyRealCorrection(rule, _context) {
505
790
  // found NO directive, so anything matching here is a genuine content-free ack
506
791
  // ("ok sure", "no that's not what I meant", "confirmed"). NO length cap on the
507
792
  // anchor — only the trailing budget — matching the v2 behavior for true acks.
508
- const acknowledgmentPattern = /^(no[,.]?\s*(that'?s\s+wrong[.!]?)?|ok(ay)?\b|good\b|great\b|nice\b|yes\b|yeah\b|right\b|wait\b|hmm+\b|sure\b|thanks?\b|confirmed\b|fair\s+point\b)[\s\S]{0,80}$/i;
793
+ // CJK ack markers added (TOW2-326 class): bare Chinese acknowledgments
794
+ // ("好的,我明白了", "没问题,收到") must be rejected the same way their
795
+ // English counterparts are — only reached when the actionable scan above
796
+ // found NO directive anywhere in the text, so a genuine CJK correction that
797
+ // happens to open with one of these words is unaffected (rescued earlier).
798
+ const acknowledgmentPattern = /^(no[,.]?\s*(that'?s\s+wrong[.!]?)?|ok(ay)?\b|good\b|great\b|nice\b|yes\b|yeah\b|right\b|wait\b|hmm+\b|sure\b|thanks?\b|confirmed\b|fair\s+point\b|不好意思|好的?|嗯+|哦+|噢+|是的?|对的?|没问题|收到|明白了?|知道了?|清楚了?|了解了?|确认了?|谢谢|辛苦了|行吧|可以的?)[\s\S]{0,80}$/i;
509
799
  if (acknowledgmentPattern.test(r)) {
510
800
  return { ok: false, reason: "pure acknowledgment or fragment — no rule content" };
511
801
  }
512
802
  return { ok: false, reason: "no actionable signal — rule lacks imperative/modal marker, preference statement, or substantive content" };
513
803
  }
514
804
  /**
515
- * P1 consolidation match key. A new correction folds into an existing ACTIVE one
516
- * only when their rule titles are IDENTICAL after normalization (lowercase, all
517
- * runs of non-alphanumerics collapsed to a single space, trimmed).
805
+ * Fix #2 (dual-channel capture gate, 2026-09-11) — DISTILLED rule identity,
806
+ * the consolidation match key. Replaces the old char-level normalization
807
+ * (lowercase + collapse non-alphanumerics) with the shared CJK-aware
808
+ * tokenizer (helpers/tokenize.ts): NFKC-normalize, segment Han runs with
809
+ * Intl.Segmenter, lowercase + punctuation-strip the Latin remainder, then
810
+ * join the ORDERED token stream. Same rule REWORDED at the punctuation/
811
+ * case/whitespace/full-width level now merges — critically including CJK
812
+ * internal-whitespace variants ("永远不要 跳过测试" vs "永远不要跳过测试"),
813
+ * which the old char-normalizer treated as two distinct rules because it
814
+ * had no notion of CJK word boundaries.
815
+ *
816
+ * STILL deliberately conservative — token-level identity, ORDER PRESERVED
817
+ * INCLUDING cross-script interleaving (review fix 2026-09-11, code-review
818
+ * MEDIUM: tokenizeWords emits Han tokens first, which would have collapsed
819
+ * "在 staging 测试 deploy" and "在 staging deploy 测试" into one identity —
820
+ * the text is therefore segmented at Han-run boundaries first and each
821
+ * segment tokenized in source order). The old doctrine stands: exact
822
+ * identity is the one gate with ZERO risk of folding two DISTINCT rules
823
+ * into one ("use proxy.ts" vs "use middleware.ts" differ by a real token
824
+ * and never merge; "test before push" vs "push before test" differ by
825
+ * order and never merge). Paraphrase-level consolidation stays out of the
826
+ * zero-LLM storage path. `minLength: 1` keeps short discriminating tokens
827
+ * ("ts", "js", "p0") — dropping them would over-merge.
518
828
  *
519
- * Deliberately VERBATIM-only. The dominant duplicate source is the SAME correction
520
- * captured again across sessions, and exact-match is the ONE gate with ZERO risk
521
- * of folding two DISTINCT rules into one. Fuzzy/semantic matching is unsafe on the
522
- * zero-LLM storage path because it cannot tell a duplicate from a contradiction:
523
- * "use proxy.ts" vs "use middleware.ts" and a "P0" vs "P1" variant differ by a
524
- * short/numeric token that any local matcher either inflates (char-trigram) or
525
- * drops (sub-3-char token filter) — so it would wrongly merge them. Paraphrase-
526
- * level consolidation is left to the optional semantic/LLM path, never here.
829
+ * NON-HAN/NON-LATIN SCRIPTS (review fix 2026-09-11, code-review HIGH-2): the
830
+ * strip class is `\p{L}\p{N}` (all letters/numbers, like the old
831
+ * normalizeRule) — NOT `[a-z0-9]` — so hangul/kana/etc. survive as tokens
832
+ * instead of every such rule distilling to "" and colliding. An EMPTY
833
+ * identity must never match anything — both merge loops (writeCorrection
834
+ * below, pending.ts's dedupe) guard on it.
835
+ *
836
+ * Exported: the pending store (storage/pending.ts) dedupes staged captures
837
+ * by the SAME identity, so a repeat capture can never fan out into N
838
+ * pending rows (the ×34 verbatim-repeat exemplar dedupes to one).
527
839
  */
840
+ const DISTILL_OPTS = {
841
+ minLength: 1,
842
+ asciiStripRegex: /[^\p{L}\p{N}\s]+/gu,
843
+ };
844
+ export function distillRuleIdentity(rule) {
845
+ // NFKC up-front so the Han-run boundary scan below sees the same composed
846
+ // text tokenizeWords normalizes to internally.
847
+ const s = (rule ?? "").normalize("NFKC");
848
+ const out = [];
849
+ const hanRun = new RegExp(HAN_RUN_RE.source, "gu");
850
+ let last = 0;
851
+ let m;
852
+ while ((m = hanRun.exec(s)) !== null) {
853
+ if (m.index > last)
854
+ out.push(...tokenizeWords(s.slice(last, m.index).toLowerCase(), DISTILL_OPTS));
855
+ out.push(...tokenizeWords(m[0], DISTILL_OPTS)); // pure Han run — segmented in place
856
+ last = m.index + m[0].length;
857
+ }
858
+ if (last < s.length)
859
+ out.push(...tokenizeWords(s.slice(last).toLowerCase(), DISTILL_OPTS));
860
+ return out.join(" ");
861
+ }
862
+ /** Consolidation match key — see distillRuleIdentity above. */
528
863
  function normalizeRule(rule) {
529
- return (rule ?? "").toLowerCase().replace(/[^\p{L}\p{N}]+/gu, " ").trim();
864
+ return distillRuleIdentity(rule);
530
865
  }
531
866
  /**
532
867
  * Write a correction to persistent storage.
@@ -537,7 +872,7 @@ function normalizeRule(rule) {
537
872
  * are unaffected (the return value was void, now it is an object; ignoring it
538
873
  * still compiles and runs correctly).
539
874
  */
540
- export function writeCorrection(project, correction) {
875
+ export async function writeCorrection(project, correction) {
541
876
  // Capture-quality gate — reject noise before touching disk.
542
877
  // v3 (Loop 8): classify on the FULL correction text, not the truncated rule.
543
878
  // `rule` is a first-sentence title slice (set by check.ts) that hid the
@@ -603,7 +938,10 @@ export function writeCorrection(project, correction) {
603
938
  // higher severity/authority/weight. High-precision LOCAL gate — no key, no
604
939
  // network — so this never runs an LLM on the storage hot path.
605
940
  const normNew = normalizeRule(record.rule);
606
- for (const existing of readActiveCorrections(project)) {
941
+ // Review fix (2026-09-11, HIGH-2 guard): an empty distilled identity
942
+ // (rule made entirely of stripped symbols) must never match anything —
943
+ // without this, every such record would fold into the first one.
944
+ for (const existing of normNew ? readActiveCorrections(project) : []) {
607
945
  if (existing.id === record.id)
608
946
  continue; // never merge into self (same-day re-slug)
609
947
  if ((existing.kind ?? "correction") !== (record.kind ?? "correction"))
@@ -615,6 +953,12 @@ export function writeCorrection(project, correction) {
615
953
  proof_count: (existing.proof_count ?? 1) + 1,
616
954
  merged_from: [...(existing.merged_from ?? []), record.id],
617
955
  tags: Array.from(new Set([...(existing.tags ?? []), ...(record.tags ?? [])])),
956
+ // Fix #2 review fix (2026-09-11, code-review LOW): absorb the incoming
957
+ // structured form's applies_when like tags — union, and only mint the
958
+ // field when at least one side carries it (old records stay untouched).
959
+ ...(existing.applies_when || record.applies_when
960
+ ? { applies_when: Array.from(new Set([...(existing.applies_when ?? []), ...(record.applies_when ?? [])])) }
961
+ : {}),
618
962
  // keep the STRONGER signal on every axis
619
963
  severity: existing.severity === "p0" || record.severity === "p0" ? "p0" : "p1",
620
964
  weight: Math.max(existing.weight ?? 0, record.weight ?? 0),
@@ -711,8 +1055,18 @@ export function readCorrections(project) {
711
1055
  if (!fs.existsSync(dir))
712
1056
  return [];
713
1057
  readCorrectionsScanLog.push(project);
1058
+ // fix4 S1 (2026-09-11): reserved-name exclusion — every `_`-prefixed entry
1059
+ // in a corrections directory is INFRASTRUCTURE, never a correction record:
1060
+ // `_index.md`, `_outcomes.jsonl`, `_rejected.jsonl`, `_ab_arms.jsonl` (all
1061
+ // already extension-excluded), plus the `_pending/`/`_quarantine/` staging
1062
+ // subtrees (fix2's dual-channel capture gate stages raw captures under
1063
+ // `corrections/_pending/` — those records are BY DESIGN not yet active and
1064
+ // must never be parsed as if they were). The class is excluded BY NAME
1065
+ // (leading underscore), not by extension accident, so a future flat
1066
+ // `_staged.json`-style infra file can never leak into the active ledger.
1067
+ // Class-not-instance: one namespace rule, not one branch per known file.
714
1068
  const files = fs.readdirSync(dir)
715
- .filter((f) => f.endsWith(".json"))
1069
+ .filter((f) => f.endsWith(".json") && !f.startsWith("_"))
716
1070
  .sort()
717
1071
  .reverse();
718
1072
  const records = [];
@@ -757,7 +1111,7 @@ export function readP0Corrections(project, preloaded) {
757
1111
  * The file is rewritten atomically — never deleted. The record remains in
758
1112
  * _outcomes.jsonl history and can be manually reactivated by editing the JSON.
759
1113
  */
760
- export function retractCorrection(project, id, reason, supersededBy) {
1114
+ export async function retractCorrection(project, id, reason, supersededBy) {
761
1115
  const dir = correctionsDir(project);
762
1116
  // ── LOCKED critical section (P0 data-loss fix, 2026-07-25) ────────────────
763
1117
  // read-all→find→mutate→atomic-rewrite→index-regen, unlocked, is exactly the
@@ -806,7 +1160,7 @@ export function retractCorrection(project, id, reason, supersededBy) {
806
1160
  * the core-level enforcement of the single-producer contract (the CLI's
807
1161
  * `ar outcomes record` is the one caller that adds the prefix).
808
1162
  */
809
- export function recordOutcome(outcome) {
1163
+ export async function recordOutcome(outcome) {
810
1164
  // C3b single-producer gate: not_triggered without the dream-audit evidence
811
1165
  // prefix indicates an unauthorized producer — fail loudly, never silently.
812
1166
  if (outcome.kind === "not_triggered" &&
@@ -845,89 +1199,108 @@ export function recordOutcome(outcome) {
845
1199
  // DIFFERENT corrections' index writes race). See the matching note in
846
1200
  // writeCorrection/retractCorrection above; same lock name, same pattern as
847
1201
  // `palace-index-${project}` / `digest-${project}` elsewhere in this codebase.
848
- withLock(`corrections-${outcome.project}`, () => {
849
- // Update the per-correction file's counters.
850
- const target = readCorrections(outcome.project).find((r) => r.id === outcome.correction_id);
851
- if (!target)
852
- return; // outcome can still be replayed later if record is restored
853
- const updated = {
854
- ...target,
855
- retrieved_count: target.retrieved_count ?? 0,
856
- heeded_count: target.heeded_count ?? 0,
857
- recurrence_count: target.recurrence_count ?? 0,
858
- };
859
- if (outcome.kind === "retrieved") {
860
- updated.retrieved_count = (updated.retrieved_count ?? 0) + 1;
861
- updated.last_retrieved = outcome.at;
862
- }
863
- else if (outcome.kind === "heeded") {
864
- updated.heeded_count = (updated.heeded_count ?? 0) + 1;
865
- updated.last_outcome = outcome.at;
866
- }
867
- else if (outcome.kind === "recurred") {
868
- updated.recurrence_count = (updated.recurrence_count ?? 0) + 1;
869
- updated.last_outcome = outcome.at;
870
- }
871
- else if (outcome.kind === "predicted") {
872
- // Wave 5: prediction fired — instrument the predict-the-correction loop.
873
- updated.predicted_count = (updated.predicted_count ?? 0) + 1;
874
- updated.last_predicted = outcome.at;
875
- }
876
- else if (outcome.kind === "predict_hit") {
877
- updated.predict_hits = (updated.predict_hits ?? 0) + 1;
878
- }
879
- else if (outcome.kind === "not_violated") {
880
- // Heed-rate credit model Option A (2026-08-29): its OWN counter, seeded
881
- // only on its own first event (mirrors predicted_count/predict_hits'
882
- // pattern, NOT the unconditional retrieved/heeded/recurrence baseline
883
- // seed above) — deliberately kept out of precision/proof_confidence
884
- // below, which read ONLY heeded_count/recurrence_count.
885
- updated.not_violated_count = (updated.not_violated_count ?? 0) + 1;
886
- updated.last_outcome = outcome.at;
1202
+ //
1203
+ // fix6-locks contention handling: the jsonl ledger line above is ALREADY
1204
+ // appended (append-only, lossless), and the materialized counters below are
1205
+ // fully re-derivable from that ledger via runOutcomesRebuild. So if another
1206
+ // LIVE process holds the lock past the timeout, skipping this counter pass
1207
+ // loses NOTHING durable — log the skip and return instead of stealing the
1208
+ // lock (old behavior, mid-write corruption risk) or failing the caller's
1209
+ // hot path (session_start / session_end / check_action record inline).
1210
+ try {
1211
+ await withLock(`corrections-${outcome.project}`, () => {
1212
+ // Update the per-correction file's counters.
1213
+ const target = readCorrections(outcome.project).find((r) => r.id === outcome.correction_id);
1214
+ if (!target)
1215
+ return; // outcome can still be replayed later if record is restored
1216
+ const updated = {
1217
+ ...target,
1218
+ retrieved_count: target.retrieved_count ?? 0,
1219
+ heeded_count: target.heeded_count ?? 0,
1220
+ recurrence_count: target.recurrence_count ?? 0,
1221
+ };
1222
+ if (outcome.kind === "retrieved") {
1223
+ updated.retrieved_count = (updated.retrieved_count ?? 0) + 1;
1224
+ updated.last_retrieved = outcome.at;
1225
+ }
1226
+ else if (outcome.kind === "heeded") {
1227
+ updated.heeded_count = (updated.heeded_count ?? 0) + 1;
1228
+ updated.last_outcome = outcome.at;
1229
+ }
1230
+ else if (outcome.kind === "recurred") {
1231
+ updated.recurrence_count = (updated.recurrence_count ?? 0) + 1;
1232
+ updated.last_outcome = outcome.at;
1233
+ }
1234
+ else if (outcome.kind === "predicted") {
1235
+ // Wave 5: prediction fired — instrument the predict-the-correction loop.
1236
+ updated.predicted_count = (updated.predicted_count ?? 0) + 1;
1237
+ updated.last_predicted = outcome.at;
1238
+ }
1239
+ else if (outcome.kind === "predict_hit") {
1240
+ updated.predict_hits = (updated.predict_hits ?? 0) + 1;
1241
+ }
1242
+ else if (outcome.kind === "not_violated") {
1243
+ // Heed-rate credit model Option A (2026-08-29): its OWN counter, seeded
1244
+ // only on its own first event (mirrors predicted_count/predict_hits'
1245
+ // pattern, NOT the unconditional retrieved/heeded/recurrence baseline
1246
+ // seed above) — deliberately kept out of precision/proof_confidence
1247
+ // below, which read ONLY heeded_count/recurrence_count.
1248
+ updated.not_violated_count = (updated.not_violated_count ?? 0) + 1;
1249
+ updated.last_outcome = outcome.at;
1250
+ }
1251
+ const r = updated.retrieved_count ?? 0;
1252
+ // Clamp to [0,1]: `retrieved` is guarded 1/day but `heeded` can fire on every
1253
+ // session_end, so raw heeded/retrieved can exceed 1.0 ("150% heeded" is
1254
+ // nonsense). min(1, …) keeps the metric honest. (Root-cause follow-up: apply
1255
+ // the same 1/day guard to heeded as retrieved has, for finer resolution.)
1256
+ // NB (Wave 5): `precision` is heeded/retrieved ONLY — predict_* never touch it.
1257
+ updated.precision = r > 0 ? Math.min(1, Number(((updated.heeded_count ?? 0) / r).toFixed(3))) : undefined;
1258
+ // Wave 5: predict_precision = predict_hits / predicted_count, kept SEPARATE
1259
+ // from `precision`. Undefined until at least one prediction has fired.
1260
+ // A predict_hit implies a prior prediction. If data is inconsistent (hits
1261
+ // recorded without a matching predicted_count — e.g. migrated/corrupt records),
1262
+ // floor the denominator at predict_hits so the metric stays VISIBLE and bounded
1263
+ // rather than silently undefined while hits exist.
1264
+ const pc = updated.predicted_count ?? 0;
1265
+ const ph = updated.predict_hits ?? 0;
1266
+ const predictDenom = Math.max(pc, ph);
1267
+ updated.predict_precision = predictDenom > 0
1268
+ ? Math.min(1, Number((ph / predictDenom).toFixed(3)))
1269
+ : undefined;
1270
+ // P3: evidence-grounded proof_confidence. With NO outcome evidence yet, keep the
1271
+ // authority prior (weight); once heeded/recurrence accrue, move to the Beta
1272
+ // posterior so a rule that keeps being honored strengthens and one whose bug
1273
+ // keeps recurring weakens. Kept SEPARATE from `precision` (heeded/retrieved) and
1274
+ // from `weight` (static authority) — this is the evidence axis.
1275
+ const heededC = updated.heeded_count ?? 0;
1276
+ const recurC = updated.recurrence_count ?? 0;
1277
+ updated.proof_confidence = (heededC + recurC) > 0
1278
+ ? Number(betaPosterior(heededC, recurC).toFixed(3))
1279
+ : (updated.weight ?? defaultWeight(updated.severity));
1280
+ // Re-write the JSON file atomically (tmp + rename — prevents truncation on
1281
+ // SIGTERM). Reuse the EXISTING filename (see findExistingCorrectionFile
1282
+ // doc) — never recompute via slugify, or a v1-named file would orphan.
1283
+ const filename = findExistingCorrectionFile(dir, updated.id)
1284
+ ?? `${updated.date}--${slugify(updated.rule || updated.id)}.json`;
1285
+ const filepath = path.join(dir, filename);
1286
+ writeRecordAtomic(filepath, updated);
1287
+ // W2-1: regenerate the materialized index on every corrections mutation.
1288
+ // (The ledger-only early-returns above for triggered/not_triggered/unknown
1289
+ // touch no per-record field the index renders — severity/status/date/rule/
1290
+ // failure_class are all unchanged — so they deliberately skip this call,
1291
+ // preserving the existing hot-path optimization documented above.)
1292
+ regenerateCorrectionsIndex(outcome.project);
1293
+ });
1294
+ }
1295
+ catch (err) {
1296
+ if (err instanceof LockContentionError) {
1297
+ console.error(`[agent-recall] recordOutcome(${outcome.kind}, ${outcome.correction_id}): ${err.message} ` +
1298
+ `— materialized counter update skipped; the _outcomes.jsonl ledger already holds this ` +
1299
+ `event and \`ar outcomes rebuild\` re-derives the counters.`);
1300
+ return;
887
1301
  }
888
- const r = updated.retrieved_count ?? 0;
889
- // Clamp to [0,1]: `retrieved` is guarded 1/day but `heeded` can fire on every
890
- // session_end, so raw heeded/retrieved can exceed 1.0 ("150% heeded" is
891
- // nonsense). min(1, …) keeps the metric honest. (Root-cause follow-up: apply
892
- // the same 1/day guard to heeded as retrieved has, for finer resolution.)
893
- // NB (Wave 5): `precision` is heeded/retrieved ONLY — predict_* never touch it.
894
- updated.precision = r > 0 ? Math.min(1, Number(((updated.heeded_count ?? 0) / r).toFixed(3))) : undefined;
895
- // Wave 5: predict_precision = predict_hits / predicted_count, kept SEPARATE
896
- // from `precision`. Undefined until at least one prediction has fired.
897
- // A predict_hit implies a prior prediction. If data is inconsistent (hits
898
- // recorded without a matching predicted_count — e.g. migrated/corrupt records),
899
- // floor the denominator at predict_hits so the metric stays VISIBLE and bounded
900
- // rather than silently undefined while hits exist.
901
- const pc = updated.predicted_count ?? 0;
902
- const ph = updated.predict_hits ?? 0;
903
- const predictDenom = Math.max(pc, ph);
904
- updated.predict_precision = predictDenom > 0
905
- ? Math.min(1, Number((ph / predictDenom).toFixed(3)))
906
- : undefined;
907
- // P3: evidence-grounded proof_confidence. With NO outcome evidence yet, keep the
908
- // authority prior (weight); once heeded/recurrence accrue, move to the Beta
909
- // posterior so a rule that keeps being honored strengthens and one whose bug
910
- // keeps recurring weakens. Kept SEPARATE from `precision` (heeded/retrieved) and
911
- // from `weight` (static authority) — this is the evidence axis.
912
- const heededC = updated.heeded_count ?? 0;
913
- const recurC = updated.recurrence_count ?? 0;
914
- updated.proof_confidence = (heededC + recurC) > 0
915
- ? Number(betaPosterior(heededC, recurC).toFixed(3))
916
- : (updated.weight ?? defaultWeight(updated.severity));
917
- // Re-write the JSON file atomically (tmp + rename — prevents truncation on
918
- // SIGTERM). Reuse the EXISTING filename (see findExistingCorrectionFile
919
- // doc) — never recompute via slugify, or a v1-named file would orphan.
920
- const filename = findExistingCorrectionFile(dir, updated.id)
921
- ?? `${updated.date}--${slugify(updated.rule || updated.id)}.json`;
922
- const filepath = path.join(dir, filename);
923
- writeRecordAtomic(filepath, updated);
924
- // W2-1: regenerate the materialized index on every corrections mutation.
925
- // (The ledger-only early-returns above for triggered/not_triggered/unknown
926
- // touch no per-record field the index renders — severity/status/date/rule/
927
- // failure_class are all unchanged — so they deliberately skip this call,
928
- // preserving the existing hot-path optimization documented above.)
929
- regenerateCorrectionsIndex(outcome.project);
930
- });
1302
+ throw err;
1303
+ }
931
1304
  }
932
1305
  /**
933
1306
  * Outcome kinds that affect per-correction counters (mirrors recordOutcome's
@@ -1231,7 +1604,7 @@ function toPublicDiffs(entries) {
1231
1604
  * 4. Only per-correction JSON files that actually differ are rewritten; the
1232
1605
  * shared _index.md is regenerated ONCE at the end, only if anything wrote.
1233
1606
  */
1234
- export function runOutcomesRebuild(project, opts = {}) {
1607
+ export async function runOutcomesRebuild(project, opts = {}) {
1235
1608
  const apply = opts.apply === true;
1236
1609
  if (!apply) {
1237
1610
  const plan = computeLedgerDivergence(project);
@@ -1632,6 +2005,12 @@ export function getCorrectionKPIs(project, preloaded) {
1632
2005
  let notViolated = 0;
1633
2006
  const noise = [];
1634
2007
  const hot = [];
2008
+ // v4 W2 — decay_class/confidence breakdown, ANNOTATION ONLY (see the
2009
+ // CorrectionKPI field docs). Pre-seed every enum key at 0 so callers never
2010
+ // need an `?? 0` guard, then tally alongside the existing single pass over
2011
+ // `all` rather than a second loop.
2012
+ const byDecayClass = { static: 0, slow: 0, volatile: 0 };
2013
+ const byConfidence = { high: 0, medium: 0, low: 0 };
1635
2014
  for (const r of all) {
1636
2015
  retrieved += r.retrieved_count ?? 0;
1637
2016
  heeded += r.heeded_count ?? 0;
@@ -1639,6 +2018,8 @@ export function getCorrectionKPIs(project, preloaded) {
1639
2018
  // Heed-rate credit model Option A (2026-08-29): summed for VISIBILITY
1640
2019
  // only — NEVER folded into heeded/recurred/precision above.
1641
2020
  notViolated += r.not_violated_count ?? 0;
2021
+ byDecayClass[decayClassOf(r)]++;
2022
+ byConfidence[effectiveConfidenceOf(r)]++;
1642
2023
  const p = r.precision ?? null;
1643
2024
  const ret = r.retrieved_count ?? 0;
1644
2025
  if (p !== null && ret >= 3 && p < 0.3) {
@@ -1703,6 +2084,8 @@ export function getCorrectionKPIs(project, preloaded) {
1703
2084
  unknown_count: unknownCount,
1704
2085
  not_triggered_count: notTriggeredCount,
1705
2086
  not_violated_count: notViolated,
2087
+ by_decay_class: byDecayClass,
2088
+ by_confidence: byConfidence,
1706
2089
  };
1707
2090
  }
1708
2091
  /**
@@ -1712,13 +2095,13 @@ export function getCorrectionKPIs(project, preloaded) {
1712
2095
  * posture: deleting belief is a deliberate act, so the default never mutates;
1713
2096
  * an explicit human (or opt-in flag) triggers the retraction.
1714
2097
  */
1715
- export function reviewNoiseCorrections(project, opts) {
2098
+ export async function reviewNoiseCorrections(project, opts) {
1716
2099
  const auto = opts?.auto ?? (process.env.AR_CONSOLIDATE_AUTO === "1");
1717
2100
  const suggestions = getCorrectionKPIs(project).noise_candidates;
1718
2101
  const pruned = [];
1719
2102
  if (auto) {
1720
2103
  for (const c of suggestions) {
1721
- const res = retractCorrection(project, c.id, "auto-pruned: low signal (precision<0.3, retrieved≥3)");
2104
+ const res = await retractCorrection(project, c.id, "auto-pruned: low signal (precision<0.3, retrieved≥3)");
1722
2105
  if (res.success)
1723
2106
  pruned.push(c.id);
1724
2107
  }
@@ -1733,6 +2116,15 @@ export function reviewNoiseCorrections(project, opts) {
1733
2116
  * evidence-backed + recently-relevant rules win the cap:
1734
2117
  * severity (p0 always above p1) ≫ proof_confidence ≫ recency ≫ proof_count.
1735
2118
  * Deterministic and stable; pure (Date.now only for recency decay).
2119
+ *
2120
+ * v4 W2 (2026-07-02 field-design-options.md §E, "rankCorrections() confidence
2121
+ * feed — Option B") — the returned records are additionally annotated with a
2122
+ * computed `decay_class` (`decayClassOf`) and effective `confidence`
2123
+ * (`effectiveConfidenceOf`). Both are attached AFTER `scoreOf`/`sort` have
2124
+ * fully decided the ORDER — neither field is read inside `scoreOf`, so this
2125
+ * is pure annotation on the output, never an input to the ranking formula
2126
+ * (ASSERT_INVARIANT: order is byte-identical to the pre-annotation behavior;
2127
+ * see corrections-rank.test.mjs).
1736
2128
  */
1737
2129
  export function rankCorrections(records, limit) {
1738
2130
  const nowMs = Date.now();
@@ -1748,6 +2140,13 @@ export function rankCorrections(records, limit) {
1748
2140
  return sev * 100 + conf * 10 + recency * 3 + proof;
1749
2141
  };
1750
2142
  const sorted = [...records].sort((a, b) => scoreOf(b) - scoreOf(a));
1751
- return limit !== undefined ? sorted.slice(0, limit) : sorted;
2143
+ const limited = limit !== undefined ? sorted.slice(0, limit) : sorted;
2144
+ // Annotate on a NEW object per record (never mutate the input's elements —
2145
+ // same purity guarantee decayClassOf/effectiveConfidenceOf document).
2146
+ return limited.map((r) => ({
2147
+ ...r,
2148
+ decay_class: decayClassOf(r),
2149
+ confidence: effectiveConfidenceOf(r),
2150
+ }));
1752
2151
  }
1753
2152
  //# sourceMappingURL=corrections.js.map