agent-recall-core 3.4.48 → 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 (215) 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 +21 -5
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +38 -4
  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.map +1 -1
  70. package/dist/retrieval/candidates.js +17 -2
  71. package/dist/retrieval/candidates.js.map +1 -1
  72. package/dist/retrieval/query-memory.d.ts +65 -7
  73. package/dist/retrieval/query-memory.d.ts.map +1 -1
  74. package/dist/retrieval/query-memory.js +518 -78
  75. package/dist/retrieval/query-memory.js.map +1 -1
  76. package/dist/retrieval/semantic-leg.d.ts +74 -0
  77. package/dist/retrieval/semantic-leg.d.ts.map +1 -0
  78. package/dist/retrieval/semantic-leg.js +180 -0
  79. package/dist/retrieval/semantic-leg.js.map +1 -0
  80. package/dist/storage/archive-write.d.ts.map +1 -1
  81. package/dist/storage/archive-write.js +29 -8
  82. package/dist/storage/archive-write.js.map +1 -1
  83. package/dist/storage/capture-router.d.ts +1 -1
  84. package/dist/storage/capture-router.d.ts.map +1 -1
  85. package/dist/storage/capture-router.js +26 -9
  86. package/dist/storage/capture-router.js.map +1 -1
  87. package/dist/storage/consolidation-queue.d.ts +11 -2
  88. package/dist/storage/consolidation-queue.d.ts.map +1 -1
  89. package/dist/storage/consolidation-queue.js +45 -16
  90. package/dist/storage/consolidation-queue.js.map +1 -1
  91. package/dist/storage/corrections.d.ts +53 -5
  92. package/dist/storage/corrections.d.ts.map +1 -1
  93. package/dist/storage/corrections.js +412 -124
  94. package/dist/storage/corrections.js.map +1 -1
  95. package/dist/storage/durable-intent.d.ts.map +1 -1
  96. package/dist/storage/durable-intent.js +24 -1
  97. package/dist/storage/durable-intent.js.map +1 -1
  98. package/dist/storage/filelock.d.ts +84 -12
  99. package/dist/storage/filelock.d.ts.map +1 -1
  100. package/dist/storage/filelock.js +309 -47
  101. package/dist/storage/filelock.js.map +1 -1
  102. package/dist/storage/fs-utils.d.ts +20 -0
  103. package/dist/storage/fs-utils.d.ts.map +1 -1
  104. package/dist/storage/fs-utils.js +27 -0
  105. package/dist/storage/fs-utils.js.map +1 -1
  106. package/dist/storage/paths.d.ts +34 -0
  107. package/dist/storage/paths.d.ts.map +1 -1
  108. package/dist/storage/paths.js +71 -0
  109. package/dist/storage/paths.js.map +1 -1
  110. package/dist/storage/pending.d.ts +154 -0
  111. package/dist/storage/pending.d.ts.map +1 -0
  112. package/dist/storage/pending.js +454 -0
  113. package/dist/storage/pending.js.map +1 -0
  114. package/dist/storage/project.d.ts.map +1 -1
  115. package/dist/storage/project.js +68 -16
  116. package/dist/storage/project.js.map +1 -1
  117. package/dist/storage/session-card.d.ts +13 -0
  118. package/dist/storage/session-card.d.ts.map +1 -1
  119. package/dist/storage/session-card.js +34 -3
  120. package/dist/storage/session-card.js.map +1 -1
  121. package/dist/storage/unclaimed.d.ts +165 -0
  122. package/dist/storage/unclaimed.d.ts.map +1 -0
  123. package/dist/storage/unclaimed.js +442 -0
  124. package/dist/storage/unclaimed.js.map +1 -0
  125. package/dist/storage/working-memory.d.ts.map +1 -1
  126. package/dist/storage/working-memory.js +19 -1
  127. package/dist/storage/working-memory.js.map +1 -1
  128. package/dist/supabase/recall-backend.d.ts +28 -2
  129. package/dist/supabase/recall-backend.d.ts.map +1 -1
  130. package/dist/supabase/recall-backend.js +80 -23
  131. package/dist/supabase/recall-backend.js.map +1 -1
  132. package/dist/supabase/sync.d.ts.map +1 -1
  133. package/dist/supabase/sync.js +13 -0
  134. package/dist/supabase/sync.js.map +1 -1
  135. package/dist/tools-logic/alignment-check.js +1 -1
  136. package/dist/tools-logic/alignment-check.js.map +1 -1
  137. package/dist/tools-logic/awareness-update.d.ts.map +1 -1
  138. package/dist/tools-logic/awareness-update.js +25 -16
  139. package/dist/tools-logic/awareness-update.js.map +1 -1
  140. package/dist/tools-logic/check-action.js +1 -1
  141. package/dist/tools-logic/check-action.js.map +1 -1
  142. package/dist/tools-logic/check.d.ts +55 -1
  143. package/dist/tools-logic/check.d.ts.map +1 -1
  144. package/dist/tools-logic/check.js +230 -57
  145. package/dist/tools-logic/check.js.map +1 -1
  146. package/dist/tools-logic/confidence.d.ts +11 -6
  147. package/dist/tools-logic/confidence.d.ts.map +1 -1
  148. package/dist/tools-logic/confidence.js +11 -6
  149. package/dist/tools-logic/confidence.js.map +1 -1
  150. package/dist/tools-logic/context-synthesize.js +2 -2
  151. package/dist/tools-logic/context-synthesize.js.map +1 -1
  152. package/dist/tools-logic/digest-read.js +2 -2
  153. package/dist/tools-logic/digest-read.js.map +1 -1
  154. package/dist/tools-logic/digest-recall.js +1 -1
  155. package/dist/tools-logic/digest-recall.js.map +1 -1
  156. package/dist/tools-logic/insight-promotion.d.ts +1 -1
  157. package/dist/tools-logic/insight-promotion.d.ts.map +1 -1
  158. package/dist/tools-logic/insight-promotion.js +17 -6
  159. package/dist/tools-logic/insight-promotion.js.map +1 -1
  160. package/dist/tools-logic/journal-capture.js +3 -3
  161. package/dist/tools-logic/journal-capture.js.map +1 -1
  162. package/dist/tools-logic/journal-write.js +3 -3
  163. package/dist/tools-logic/journal-write.js.map +1 -1
  164. package/dist/tools-logic/knowledge-write.js +2 -2
  165. package/dist/tools-logic/knowledge-write.js.map +1 -1
  166. package/dist/tools-logic/palace-lint.js +1 -1
  167. package/dist/tools-logic/palace-lint.js.map +1 -1
  168. package/dist/tools-logic/palace-read.js +1 -1
  169. package/dist/tools-logic/palace-read.js.map +1 -1
  170. package/dist/tools-logic/palace-search.js +1 -1
  171. package/dist/tools-logic/palace-search.js.map +1 -1
  172. package/dist/tools-logic/palace-walk.js +3 -3
  173. package/dist/tools-logic/palace-walk.js.map +1 -1
  174. package/dist/tools-logic/palace-write.js +4 -4
  175. package/dist/tools-logic/palace-write.js.map +1 -1
  176. package/dist/tools-logic/predict-correction.js +1 -1
  177. package/dist/tools-logic/predict-correction.js.map +1 -1
  178. package/dist/tools-logic/recall-backend.d.ts +11 -2
  179. package/dist/tools-logic/recall-backend.d.ts.map +1 -1
  180. package/dist/tools-logic/recall-backend.js +2 -2
  181. package/dist/tools-logic/recall-backend.js.map +1 -1
  182. package/dist/tools-logic/resurrect.d.ts.map +1 -1
  183. package/dist/tools-logic/resurrect.js +9 -1
  184. package/dist/tools-logic/resurrect.js.map +1 -1
  185. package/dist/tools-logic/safety-consolidation.d.ts.map +1 -1
  186. package/dist/tools-logic/safety-consolidation.js +32 -30
  187. package/dist/tools-logic/safety-consolidation.js.map +1 -1
  188. package/dist/tools-logic/session-end.d.ts +8 -0
  189. package/dist/tools-logic/session-end.d.ts.map +1 -1
  190. package/dist/tools-logic/session-end.js +51 -13
  191. package/dist/tools-logic/session-end.js.map +1 -1
  192. package/dist/tools-logic/session-start.d.ts +23 -0
  193. package/dist/tools-logic/session-start.d.ts.map +1 -1
  194. package/dist/tools-logic/session-start.js +44 -1
  195. package/dist/tools-logic/session-start.js.map +1 -1
  196. package/dist/tools-logic/smart-recall.d.ts +185 -10
  197. package/dist/tools-logic/smart-recall.d.ts.map +1 -1
  198. package/dist/tools-logic/smart-recall.js +363 -85
  199. package/dist/tools-logic/smart-recall.js.map +1 -1
  200. package/dist/tools-logic/store-doctor.d.ts.map +1 -1
  201. package/dist/tools-logic/store-doctor.js +7 -4
  202. package/dist/tools-logic/store-doctor.js.map +1 -1
  203. package/dist/tools-logic/store-repair.js +1 -1
  204. package/dist/tools-logic/store-repair.js.map +1 -1
  205. package/dist/tools-logic/supersession.d.ts +1 -1
  206. package/dist/tools-logic/supersession.d.ts.map +1 -1
  207. package/dist/tools-logic/supersession.js +2 -2
  208. package/dist/tools-logic/supersession.js.map +1 -1
  209. package/dist/types.d.ts +1 -1
  210. package/dist/types.js +1 -1
  211. package/dist/vector/embedding.d.ts +8 -0
  212. package/dist/vector/embedding.d.ts.map +1 -1
  213. package/dist/vector/embedding.js +8 -0
  214. package/dist/vector/embedding.js.map +1 -1
  215. 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
@@ -410,6 +448,18 @@ export function effectiveConfidenceOf(record) {
410
448
  export function splitSentences(text) {
411
449
  // Boundary = one of . ! ? OR a newline, that is followed by whitespace or EOT.
412
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.
413
463
  const out = [];
414
464
  let buf = "";
415
465
  for (let i = 0; i < text.length; i++) {
@@ -417,11 +467,14 @@ export function splitSentences(text) {
417
467
  buf += ch;
418
468
  const next = text[i + 1];
419
469
  const isPunct = ch === "." || ch === "!" || ch === "?";
470
+ const isCjkPunct = ch === "。" || ch === "!" || ch === "?";
420
471
  const isNewline = ch === "\n" || ch === "\r";
421
472
  // Sentence boundary: terminal punctuation at end OR followed by whitespace;
422
- // newline is always a boundary. A `.` between non-whitespace chars is NOT
423
- // 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.
424
476
  const atBoundary = isNewline ||
477
+ isCjkPunct ||
425
478
  (isPunct && (next === undefined || /\s/.test(next)));
426
479
  if (atBoundary) {
427
480
  const frag = buf.trim();
@@ -446,18 +499,157 @@ export function splitSentences(text) {
446
499
  // when the fragment is NOT a hedged/reporting frame. This is recall-safe: every
447
500
  // genuine fixture correction carries a STRONG marker, a preference shape, or a
448
501
  // non-hedged weak verb (verified by scripts/eval/capture-gate-confusion.mjs).
449
- 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;
450
- 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;
451
629
  // Tentative / reporting frame at the START of a fragment — the speaker is musing
452
630
  // or reporting, not issuing a rule. A WEAK marker inside such a frame is NOT a
453
631
  // directive. Anchored at ^ so it only catches the OPENER, never a directive
454
- // sentence that merely follows a hedge.
455
- 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;
456
635
  // Preference / corrective-fact statement. Includes user-preference verbs (CJK
457
636
  // equivalents) AND the "X not Y" / "wrong … not" corrective-fact shape that
458
637
  // carries real intent without an imperative verb (e.g. "Product names are
459
- // novada-search (not novada-mcp)"). Scanned per-fragment.
460
- 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;
461
653
  /**
462
654
  * Capture-quality gate — rejects context-free fragments, pure acknowledgments,
463
655
  * and text that carries no actionable signal.
@@ -569,19 +761,27 @@ export function isLikelyRealCorrection(rule, _context) {
569
761
  // truncated slice), so a directive in sentence 2+ is now seen and can RESCUE
570
762
  // a text that opens with an acknowledgment.
571
763
  const fragments = [r, ...splitSentences(r)];
572
- // (a) STRONG directive marker in any fragment → accept unconditionally.
573
- 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))) {
574
771
  return { ok: true };
575
772
  }
576
773
  // (a2) WEAK directive marker → accept only in a fragment that is NOT a hedged/
577
- // reporting frame. Closes the Loop-14 filler-prose false-accept ("I think we
578
- // should use it") while still accepting a direct weak-verb correction ("stop
579
- // making it full width") and a directive sentence that merely FOLLOWS a hedge.
580
- 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))) {
581
779
  return { ok: true };
582
780
  }
583
- // (b) preference / corrective-fact statement in any fragment
584
- 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))) {
585
785
  return { ok: true };
586
786
  }
587
787
  // ── SOFT ACKNOWLEDGMENT GATE — runs AFTER the actionable scan ──────────────
@@ -590,28 +790,78 @@ export function isLikelyRealCorrection(rule, _context) {
590
790
  // found NO directive, so anything matching here is a genuine content-free ack
591
791
  // ("ok sure", "no that's not what I meant", "confirmed"). NO length cap on the
592
792
  // anchor — only the trailing budget — matching the v2 behavior for true acks.
593
- 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;
594
799
  if (acknowledgmentPattern.test(r)) {
595
800
  return { ok: false, reason: "pure acknowledgment or fragment — no rule content" };
596
801
  }
597
802
  return { ok: false, reason: "no actionable signal — rule lacks imperative/modal marker, preference statement, or substantive content" };
598
803
  }
599
804
  /**
600
- * P1 consolidation match key. A new correction folds into an existing ACTIVE one
601
- * only when their rule titles are IDENTICAL after normalization (lowercase, all
602
- * 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.
828
+ *
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.
603
835
  *
604
- * Deliberately VERBATIM-only. The dominant duplicate source is the SAME correction
605
- * captured again across sessions, and exact-match is the ONE gate with ZERO risk
606
- * of folding two DISTINCT rules into one. Fuzzy/semantic matching is unsafe on the
607
- * zero-LLM storage path because it cannot tell a duplicate from a contradiction:
608
- * "use proxy.ts" vs "use middleware.ts" and a "P0" vs "P1" variant differ by a
609
- * short/numeric token that any local matcher either inflates (char-trigram) or
610
- * drops (sub-3-char token filter) — so it would wrongly merge them. Paraphrase-
611
- * level consolidation is left to the optional semantic/LLM path, never here.
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).
612
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. */
613
863
  function normalizeRule(rule) {
614
- return (rule ?? "").toLowerCase().replace(/[^\p{L}\p{N}]+/gu, " ").trim();
864
+ return distillRuleIdentity(rule);
615
865
  }
616
866
  /**
617
867
  * Write a correction to persistent storage.
@@ -622,7 +872,7 @@ function normalizeRule(rule) {
622
872
  * are unaffected (the return value was void, now it is an object; ignoring it
623
873
  * still compiles and runs correctly).
624
874
  */
625
- export function writeCorrection(project, correction) {
875
+ export async function writeCorrection(project, correction) {
626
876
  // Capture-quality gate — reject noise before touching disk.
627
877
  // v3 (Loop 8): classify on the FULL correction text, not the truncated rule.
628
878
  // `rule` is a first-sentence title slice (set by check.ts) that hid the
@@ -688,7 +938,10 @@ export function writeCorrection(project, correction) {
688
938
  // higher severity/authority/weight. High-precision LOCAL gate — no key, no
689
939
  // network — so this never runs an LLM on the storage hot path.
690
940
  const normNew = normalizeRule(record.rule);
691
- 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) : []) {
692
945
  if (existing.id === record.id)
693
946
  continue; // never merge into self (same-day re-slug)
694
947
  if ((existing.kind ?? "correction") !== (record.kind ?? "correction"))
@@ -700,6 +953,12 @@ export function writeCorrection(project, correction) {
700
953
  proof_count: (existing.proof_count ?? 1) + 1,
701
954
  merged_from: [...(existing.merged_from ?? []), record.id],
702
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
+ : {}),
703
962
  // keep the STRONGER signal on every axis
704
963
  severity: existing.severity === "p0" || record.severity === "p0" ? "p0" : "p1",
705
964
  weight: Math.max(existing.weight ?? 0, record.weight ?? 0),
@@ -796,8 +1055,18 @@ export function readCorrections(project) {
796
1055
  if (!fs.existsSync(dir))
797
1056
  return [];
798
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.
799
1068
  const files = fs.readdirSync(dir)
800
- .filter((f) => f.endsWith(".json"))
1069
+ .filter((f) => f.endsWith(".json") && !f.startsWith("_"))
801
1070
  .sort()
802
1071
  .reverse();
803
1072
  const records = [];
@@ -842,7 +1111,7 @@ export function readP0Corrections(project, preloaded) {
842
1111
  * The file is rewritten atomically — never deleted. The record remains in
843
1112
  * _outcomes.jsonl history and can be manually reactivated by editing the JSON.
844
1113
  */
845
- export function retractCorrection(project, id, reason, supersededBy) {
1114
+ export async function retractCorrection(project, id, reason, supersededBy) {
846
1115
  const dir = correctionsDir(project);
847
1116
  // ── LOCKED critical section (P0 data-loss fix, 2026-07-25) ────────────────
848
1117
  // read-all→find→mutate→atomic-rewrite→index-regen, unlocked, is exactly the
@@ -891,7 +1160,7 @@ export function retractCorrection(project, id, reason, supersededBy) {
891
1160
  * the core-level enforcement of the single-producer contract (the CLI's
892
1161
  * `ar outcomes record` is the one caller that adds the prefix).
893
1162
  */
894
- export function recordOutcome(outcome) {
1163
+ export async function recordOutcome(outcome) {
895
1164
  // C3b single-producer gate: not_triggered without the dream-audit evidence
896
1165
  // prefix indicates an unauthorized producer — fail loudly, never silently.
897
1166
  if (outcome.kind === "not_triggered" &&
@@ -930,89 +1199,108 @@ export function recordOutcome(outcome) {
930
1199
  // DIFFERENT corrections' index writes race). See the matching note in
931
1200
  // writeCorrection/retractCorrection above; same lock name, same pattern as
932
1201
  // `palace-index-${project}` / `digest-${project}` elsewhere in this codebase.
933
- withLock(`corrections-${outcome.project}`, () => {
934
- // Update the per-correction file's counters.
935
- const target = readCorrections(outcome.project).find((r) => r.id === outcome.correction_id);
936
- if (!target)
937
- return; // outcome can still be replayed later if record is restored
938
- const updated = {
939
- ...target,
940
- retrieved_count: target.retrieved_count ?? 0,
941
- heeded_count: target.heeded_count ?? 0,
942
- recurrence_count: target.recurrence_count ?? 0,
943
- };
944
- if (outcome.kind === "retrieved") {
945
- updated.retrieved_count = (updated.retrieved_count ?? 0) + 1;
946
- updated.last_retrieved = outcome.at;
947
- }
948
- else if (outcome.kind === "heeded") {
949
- updated.heeded_count = (updated.heeded_count ?? 0) + 1;
950
- updated.last_outcome = outcome.at;
951
- }
952
- else if (outcome.kind === "recurred") {
953
- updated.recurrence_count = (updated.recurrence_count ?? 0) + 1;
954
- updated.last_outcome = outcome.at;
955
- }
956
- else if (outcome.kind === "predicted") {
957
- // Wave 5: prediction fired — instrument the predict-the-correction loop.
958
- updated.predicted_count = (updated.predicted_count ?? 0) + 1;
959
- updated.last_predicted = outcome.at;
960
- }
961
- else if (outcome.kind === "predict_hit") {
962
- updated.predict_hits = (updated.predict_hits ?? 0) + 1;
963
- }
964
- else if (outcome.kind === "not_violated") {
965
- // Heed-rate credit model Option A (2026-08-29): its OWN counter, seeded
966
- // only on its own first event (mirrors predicted_count/predict_hits'
967
- // pattern, NOT the unconditional retrieved/heeded/recurrence baseline
968
- // seed above) — deliberately kept out of precision/proof_confidence
969
- // below, which read ONLY heeded_count/recurrence_count.
970
- updated.not_violated_count = (updated.not_violated_count ?? 0) + 1;
971
- 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;
972
1301
  }
973
- const r = updated.retrieved_count ?? 0;
974
- // Clamp to [0,1]: `retrieved` is guarded 1/day but `heeded` can fire on every
975
- // session_end, so raw heeded/retrieved can exceed 1.0 ("150% heeded" is
976
- // nonsense). min(1, …) keeps the metric honest. (Root-cause follow-up: apply
977
- // the same 1/day guard to heeded as retrieved has, for finer resolution.)
978
- // NB (Wave 5): `precision` is heeded/retrieved ONLY — predict_* never touch it.
979
- updated.precision = r > 0 ? Math.min(1, Number(((updated.heeded_count ?? 0) / r).toFixed(3))) : undefined;
980
- // Wave 5: predict_precision = predict_hits / predicted_count, kept SEPARATE
981
- // from `precision`. Undefined until at least one prediction has fired.
982
- // A predict_hit implies a prior prediction. If data is inconsistent (hits
983
- // recorded without a matching predicted_count — e.g. migrated/corrupt records),
984
- // floor the denominator at predict_hits so the metric stays VISIBLE and bounded
985
- // rather than silently undefined while hits exist.
986
- const pc = updated.predicted_count ?? 0;
987
- const ph = updated.predict_hits ?? 0;
988
- const predictDenom = Math.max(pc, ph);
989
- updated.predict_precision = predictDenom > 0
990
- ? Math.min(1, Number((ph / predictDenom).toFixed(3)))
991
- : undefined;
992
- // P3: evidence-grounded proof_confidence. With NO outcome evidence yet, keep the
993
- // authority prior (weight); once heeded/recurrence accrue, move to the Beta
994
- // posterior so a rule that keeps being honored strengthens and one whose bug
995
- // keeps recurring weakens. Kept SEPARATE from `precision` (heeded/retrieved) and
996
- // from `weight` (static authority) — this is the evidence axis.
997
- const heededC = updated.heeded_count ?? 0;
998
- const recurC = updated.recurrence_count ?? 0;
999
- updated.proof_confidence = (heededC + recurC) > 0
1000
- ? Number(betaPosterior(heededC, recurC).toFixed(3))
1001
- : (updated.weight ?? defaultWeight(updated.severity));
1002
- // Re-write the JSON file atomically (tmp + rename — prevents truncation on
1003
- // SIGTERM). Reuse the EXISTING filename (see findExistingCorrectionFile
1004
- // doc) — never recompute via slugify, or a v1-named file would orphan.
1005
- const filename = findExistingCorrectionFile(dir, updated.id)
1006
- ?? `${updated.date}--${slugify(updated.rule || updated.id)}.json`;
1007
- const filepath = path.join(dir, filename);
1008
- writeRecordAtomic(filepath, updated);
1009
- // W2-1: regenerate the materialized index on every corrections mutation.
1010
- // (The ledger-only early-returns above for triggered/not_triggered/unknown
1011
- // touch no per-record field the index renders — severity/status/date/rule/
1012
- // failure_class are all unchanged — so they deliberately skip this call,
1013
- // preserving the existing hot-path optimization documented above.)
1014
- regenerateCorrectionsIndex(outcome.project);
1015
- });
1302
+ throw err;
1303
+ }
1016
1304
  }
1017
1305
  /**
1018
1306
  * Outcome kinds that affect per-correction counters (mirrors recordOutcome's
@@ -1316,7 +1604,7 @@ function toPublicDiffs(entries) {
1316
1604
  * 4. Only per-correction JSON files that actually differ are rewritten; the
1317
1605
  * shared _index.md is regenerated ONCE at the end, only if anything wrote.
1318
1606
  */
1319
- export function runOutcomesRebuild(project, opts = {}) {
1607
+ export async function runOutcomesRebuild(project, opts = {}) {
1320
1608
  const apply = opts.apply === true;
1321
1609
  if (!apply) {
1322
1610
  const plan = computeLedgerDivergence(project);
@@ -1807,13 +2095,13 @@ export function getCorrectionKPIs(project, preloaded) {
1807
2095
  * posture: deleting belief is a deliberate act, so the default never mutates;
1808
2096
  * an explicit human (or opt-in flag) triggers the retraction.
1809
2097
  */
1810
- export function reviewNoiseCorrections(project, opts) {
2098
+ export async function reviewNoiseCorrections(project, opts) {
1811
2099
  const auto = opts?.auto ?? (process.env.AR_CONSOLIDATE_AUTO === "1");
1812
2100
  const suggestions = getCorrectionKPIs(project).noise_candidates;
1813
2101
  const pruned = [];
1814
2102
  if (auto) {
1815
2103
  for (const c of suggestions) {
1816
- 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)");
1817
2105
  if (res.success)
1818
2106
  pruned.push(c.id);
1819
2107
  }