agent-recall-core 3.4.50 → 3.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. package/README.md +559 -330
  2. package/dist/embeddings/config.d.ts.map +1 -1
  3. package/dist/embeddings/config.js +17 -4
  4. package/dist/embeddings/config.js.map +1 -1
  5. package/dist/helpers/alignment-patterns.d.ts +46 -0
  6. package/dist/helpers/alignment-patterns.d.ts.map +1 -1
  7. package/dist/helpers/alignment-patterns.js +103 -2
  8. package/dist/helpers/alignment-patterns.js.map +1 -1
  9. package/dist/helpers/journal-files.d.ts +12 -6
  10. package/dist/helpers/journal-files.d.ts.map +1 -1
  11. package/dist/helpers/journal-files.js +13 -7
  12. package/dist/helpers/journal-files.js.map +1 -1
  13. package/dist/helpers/transcript-project.d.ts +69 -0
  14. package/dist/helpers/transcript-project.d.ts.map +1 -0
  15. package/dist/helpers/transcript-project.js +212 -0
  16. package/dist/helpers/transcript-project.js.map +1 -0
  17. package/dist/index.d.ts +28 -7
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +62 -6
  20. package/dist/index.js.map +1 -1
  21. package/dist/palace/awareness.d.ts +6 -0
  22. package/dist/palace/awareness.d.ts.map +1 -1
  23. package/dist/palace/awareness.js +10 -4
  24. package/dist/palace/awareness.js.map +1 -1
  25. package/dist/prompts/consolidation-prompt.js +14 -14
  26. package/dist/retrieval/activation.d.ts +202 -0
  27. package/dist/retrieval/activation.d.ts.map +1 -0
  28. package/dist/retrieval/activation.js +313 -0
  29. package/dist/retrieval/activation.js.map +1 -0
  30. package/dist/retrieval/candidates.d.ts +17 -0
  31. package/dist/retrieval/candidates.d.ts.map +1 -1
  32. package/dist/retrieval/candidates.js +4 -2
  33. package/dist/retrieval/candidates.js.map +1 -1
  34. package/dist/retrieval/contradiction.d.ts +42 -0
  35. package/dist/retrieval/contradiction.d.ts.map +1 -1
  36. package/dist/retrieval/contradiction.js +126 -7
  37. package/dist/retrieval/contradiction.js.map +1 -1
  38. package/dist/retrieval/lexical-index.d.ts +150 -0
  39. package/dist/retrieval/lexical-index.d.ts.map +1 -0
  40. package/dist/retrieval/lexical-index.js +514 -0
  41. package/dist/retrieval/lexical-index.js.map +1 -0
  42. package/dist/retrieval/query-memory.d.ts +17 -7
  43. package/dist/retrieval/query-memory.d.ts.map +1 -1
  44. package/dist/retrieval/query-memory.js +274 -68
  45. package/dist/retrieval/query-memory.js.map +1 -1
  46. package/dist/storage/classification.d.ts.map +1 -1
  47. package/dist/storage/classification.js +4 -1
  48. package/dist/storage/classification.js.map +1 -1
  49. package/dist/storage/content-guard.d.ts +26 -0
  50. package/dist/storage/content-guard.d.ts.map +1 -1
  51. package/dist/storage/content-guard.js +450 -48
  52. package/dist/storage/content-guard.js.map +1 -1
  53. package/dist/storage/corrections.d.ts +140 -2
  54. package/dist/storage/corrections.d.ts.map +1 -1
  55. package/dist/storage/corrections.js +144 -70
  56. package/dist/storage/corrections.js.map +1 -1
  57. package/dist/storage/cwd-allowlist.d.ts +28 -2
  58. package/dist/storage/cwd-allowlist.d.ts.map +1 -1
  59. package/dist/storage/cwd-allowlist.js +61 -13
  60. package/dist/storage/cwd-allowlist.js.map +1 -1
  61. package/dist/storage/filelock.d.ts +26 -8
  62. package/dist/storage/filelock.d.ts.map +1 -1
  63. package/dist/storage/filelock.js +202 -26
  64. package/dist/storage/filelock.js.map +1 -1
  65. package/dist/storage/fs-utils.d.ts +51 -0
  66. package/dist/storage/fs-utils.d.ts.map +1 -1
  67. package/dist/storage/fs-utils.js +123 -2
  68. package/dist/storage/fs-utils.js.map +1 -1
  69. package/dist/storage/heed-tiers.d.ts +206 -0
  70. package/dist/storage/heed-tiers.d.ts.map +1 -0
  71. package/dist/storage/heed-tiers.js +318 -0
  72. package/dist/storage/heed-tiers.js.map +1 -0
  73. package/dist/storage/memory-protocol.js +55 -55
  74. package/dist/storage/paths.d.ts.map +1 -1
  75. package/dist/storage/paths.js +7 -2
  76. package/dist/storage/paths.js.map +1 -1
  77. package/dist/storage/project.d.ts.map +1 -1
  78. package/dist/storage/project.js +9 -2
  79. package/dist/storage/project.js.map +1 -1
  80. package/dist/storage/store-manifest.js +57 -57
  81. package/dist/supabase/embedding.d.ts.map +1 -1
  82. package/dist/supabase/embedding.js +7 -0
  83. package/dist/supabase/embedding.js.map +1 -1
  84. package/dist/supabase/sync.d.ts +1 -1
  85. package/dist/supabase/sync.d.ts.map +1 -1
  86. package/dist/supabase/sync.js +88 -28
  87. package/dist/supabase/sync.js.map +1 -1
  88. package/dist/tools-logic/association.d.ts +260 -0
  89. package/dist/tools-logic/association.d.ts.map +1 -0
  90. package/dist/tools-logic/association.js +502 -0
  91. package/dist/tools-logic/association.js.map +1 -0
  92. package/dist/tools-logic/bootstrap.d.ts.map +1 -1
  93. package/dist/tools-logic/bootstrap.js +4 -2
  94. package/dist/tools-logic/bootstrap.js.map +1 -1
  95. package/dist/tools-logic/check-action.d.ts.map +1 -1
  96. package/dist/tools-logic/check-action.js +14 -0
  97. package/dist/tools-logic/check-action.js.map +1 -1
  98. package/dist/tools-logic/check.d.ts +7 -0
  99. package/dist/tools-logic/check.d.ts.map +1 -1
  100. package/dist/tools-logic/check.js +54 -44
  101. package/dist/tools-logic/check.js.map +1 -1
  102. package/dist/tools-logic/dream-admission.js +29 -29
  103. package/dist/tools-logic/implicit-harvest.d.ts +196 -0
  104. package/dist/tools-logic/implicit-harvest.d.ts.map +1 -0
  105. package/dist/tools-logic/implicit-harvest.js +675 -0
  106. package/dist/tools-logic/implicit-harvest.js.map +1 -0
  107. package/dist/tools-logic/insight-promotion.d.ts.map +1 -1
  108. package/dist/tools-logic/insight-promotion.js +53 -1
  109. package/dist/tools-logic/insight-promotion.js.map +1 -1
  110. package/dist/tools-logic/recall-backend.d.ts.map +1 -1
  111. package/dist/tools-logic/recall-backend.js +3 -1
  112. package/dist/tools-logic/recall-backend.js.map +1 -1
  113. package/dist/tools-logic/retier.d.ts +274 -0
  114. package/dist/tools-logic/retier.d.ts.map +1 -0
  115. package/dist/tools-logic/retier.js +511 -0
  116. package/dist/tools-logic/retier.js.map +1 -0
  117. package/dist/tools-logic/session-end.d.ts +35 -0
  118. package/dist/tools-logic/session-end.d.ts.map +1 -1
  119. package/dist/tools-logic/session-end.js +26 -7
  120. package/dist/tools-logic/session-end.js.map +1 -1
  121. package/dist/tools-logic/session-start.d.ts +11 -0
  122. package/dist/tools-logic/session-start.d.ts.map +1 -1
  123. package/dist/tools-logic/session-start.js +34 -4
  124. package/dist/tools-logic/session-start.js.map +1 -1
  125. package/dist/tools-logic/smart-recall.d.ts +63 -0
  126. package/dist/tools-logic/smart-recall.d.ts.map +1 -1
  127. package/dist/tools-logic/smart-recall.js +213 -31
  128. package/dist/tools-logic/smart-recall.js.map +1 -1
  129. package/dist/tools-logic/smart-remember-vector.d.ts +3 -1
  130. package/dist/tools-logic/smart-remember-vector.d.ts.map +1 -1
  131. package/dist/tools-logic/smart-remember-vector.js +3 -1
  132. package/dist/tools-logic/smart-remember-vector.js.map +1 -1
  133. package/dist/tools-logic/smart-remember.d.ts.map +1 -1
  134. package/dist/tools-logic/smart-remember.js +6 -2
  135. package/dist/tools-logic/smart-remember.js.map +1 -1
  136. package/dist/tools-logic/transcript-audit.d.ts +128 -0
  137. package/dist/tools-logic/transcript-audit.d.ts.map +1 -0
  138. package/dist/tools-logic/transcript-audit.js +428 -0
  139. package/dist/tools-logic/transcript-audit.js.map +1 -0
  140. package/dist/types.d.ts +17 -2
  141. package/dist/types.d.ts.map +1 -1
  142. package/dist/types.js +31 -3
  143. package/dist/types.js.map +1 -1
  144. package/dist/vector/embedding.d.ts +20 -1
  145. package/dist/vector/embedding.d.ts.map +1 -1
  146. package/dist/vector/embedding.js +27 -1
  147. package/dist/vector/embedding.js.map +1 -1
  148. package/dist/vector/local-vector-backend.d.ts +4 -1
  149. package/dist/vector/local-vector-backend.d.ts.map +1 -1
  150. package/dist/vector/local-vector-backend.js +7 -3
  151. package/dist/vector/local-vector-backend.js.map +1 -1
  152. package/package.json +59 -59
@@ -0,0 +1,675 @@
1
+ /**
2
+ * implicit-harvest.ts — evolution p1b (Phase 1b): `ar corrections harvest-implicit`.
3
+ *
4
+ * Capture density is the measured ceiling (p1a: 35.3% capture recall, every
5
+ * miss was hook-no-fire coverage — the human corrected, the hook never ran).
6
+ * Implicit signals raise events/week ~10x over the explicit human_correction
7
+ * channel: most real corrections in a transcript never take the imperative
8
+ * shape check.ts's structured form requires ("No, that's wrong" / "还是不对,
9
+ * 用 X" / repeating the same instruction a second time because the first was
10
+ * ignored). This module mines those THREE conservative, high-precision
11
+ * patterns over USER turns only:
12
+ *
13
+ * (a) negation opener — a turn that OPENS with a negation/correction marker
14
+ * ("no,", "不对", "not that", "i said", "停", ...) followed by ≥4 more
15
+ * words of actual re-instruction. The re-instruction (opener stripped)
16
+ * is the candidate rule.
17
+ * (b) again-marker — a turn containing a recurrence marker ("again", "还是",
18
+ * "第二次", "怎么又") AND a minimal imperative-ish verb ("use", "don't",
19
+ * "应该", "改成", ...). The sentence carrying the verb is the candidate
20
+ * rule (the recurrence marker is corroborating evidence, not the rule
21
+ * text itself).
22
+ * (c) repeat-instruction — two ≥6-word user turns in the SAME session whose
23
+ * token-Jaccard similarity is ≥0.6 (the human said essentially the same
24
+ * thing twice because the first time didn't land). The SECOND
25
+ * occurrence is the candidate rule.
26
+ *
27
+ * Fix round 3 (orchestrator decision, data-driven — real 48-day window):
28
+ * signal (b) again-marker was measured at 0 true positives / 2 false
29
+ * positives over that window, both from the SAME structural mechanism
30
+ * (voice-dictation run-on monologues where "again" is an ordinary temporal
31
+ * adverb, not a recurrence report — see the AGAIN_MARKER gating comment
32
+ * below for why fix round 1's "same sentence" proximity fix doesn't rescue
33
+ * this population). The only genuine true positive across the window came
34
+ * from signal (a) negation-opener. DEFAULT is now **signal (a) ONLY**;
35
+ * signals (b) and (c) both live behind the same `experimentalSignals` opt-in
36
+ * — neither deleted, both structurally available for a caller who wants
37
+ * them (see ImplicitHarvestOptions.experimentalSignals' doc comment).
38
+ *
39
+ * Everything this produces is PROVISIONAL by construction and carries a hard
40
+ * humility contract, enforced structurally (not by convention):
41
+ * - severity is ALWAYS "p1" — literally never derived, never "p0". Because
42
+ * writeCorrection only auto-detects severity when the caller omits it
43
+ * (`correction.severity ?? detectSeverity(...)`), setting it explicitly
44
+ * here means detectSeverity is never even consulted for these records —
45
+ * an implicit record can never enter session_start's P0 section
46
+ * (readP0Corrections filters on severity==="p0") no matter how strong the
47
+ * underlying language looks. See implicit-harvest.test.mjs's watch-tier
48
+ * isolation test.
49
+ * - weight 0.3 / confidence "low" / provenance {source:"transcript-implicit",
50
+ * mode:"observed"} / tags ["implicit"] — every downstream KPI/ranking
51
+ * surface (rankCorrections, getCorrectionKPIs, heed-tiers.ts) reads only
52
+ * severity/weight/proof_confidence/recency/proof_count/outcome-ledger
53
+ * events, NEVER provenance.source — so these records fall exactly where
54
+ * an ordinary low-authority p1 correction would, no special-casing either
55
+ * direction (verified, not assumed — see the same isolation test).
56
+ *
57
+ * REUSE, not a second walker: `scanTranscripts` (day-bucketed line scan,
58
+ * per-file F1 project resolution, mtime-fallback ambiguity handling) and
59
+ * `resolveDayRange` (--date XOR --backfill --since/--until) are p1a's own
60
+ * exported machinery (packages/core/src/tools-logic/transcript-audit.ts),
61
+ * unmodified. This module only adds a DIFFERENT extraction shape over the
62
+ * same day-bucketed lines — ordered user/assistant turn pairing instead of
63
+ * one concatenated blob — built from the SAME exported per-record filters
64
+ * (isBoilerplateRecord/isSystemText/textFromContent,
65
+ * packages/core/src/helpers/transcript-project.ts) transcript-audit.ts's own
66
+ * extractDayText uses internally. That shape difference is required because
67
+ * signals (a)/(b) need the immediately-preceding assistant sentence and
68
+ * signal (c) needs ORDERED, DISCRETE messages to pair — a single flattened
69
+ * string can supply neither.
70
+ *
71
+ * Storage is exclusively through the canonical `writeCorrection` path
72
+ * (packages/core/src/storage/corrections.ts) — never a hand-rolled file
73
+ * write — so on-write consolidation (token-identical rule merge), atomic
74
+ * write, and materialized-index regeneration are shared with every other
75
+ * correction producer. `writeCorrection`'s ONLY gate was previously the fused
76
+ * hard-noise + actionable-imperative-shape + soft-acknowledgment gate
77
+ * (`isLikelyRealCorrection`) with no seam to run the hard-noise floor alone —
78
+ * see `WriteCorrectionOptions.skipActionableGate`'s doc comment in
79
+ * corrections.ts for why a seam (not a fork, not a refactor of the
80
+ * precision-tuned gate internals) was the minimal fix, and why running the
81
+ * FULL gate on implicit candidates would have rejected almost every
82
+ * negation-opener candidate as a "pure acknowledgment" (the exact Loop-7
83
+ * failure the actionable-scan rescue exists to prevent, replayed from the
84
+ * opposite side once that rescue is skipped).
85
+ */
86
+ import * as crypto from "node:crypto";
87
+ import { getRoot } from "../types.js";
88
+ import { resolveProjectDirName } from "../storage/paths.js";
89
+ import { writeCorrection, dropHardNoise, splitSentences, } from "../storage/corrections.js";
90
+ import { tokenizeWords } from "../helpers/tokenize.js";
91
+ import { isBoilerplateRecord, isSystemText, textFromContent } from "../helpers/transcript-project.js";
92
+ import { scanTranscripts, resolveDayRange, defaultClaudeDir, } from "./transcript-audit.js";
93
+ // ---------------------------------------------------------------------------
94
+ // Signal patterns
95
+ // ---------------------------------------------------------------------------
96
+ // (a) — brief-specified verbatim. Anchored at the start of the (trimmed)
97
+ // user-turn text: "sentence starting" reads most naturally, and most
98
+ // defensibly for a first pass, as "the message opens with this marker" —
99
+ // matching this codebase's own STRONG_IMPERATIVE precedent of scanning whole
100
+ // turns rather than requiring the marker to be its own isolated sentence.
101
+ const NEGATION_OPENER = /^(no[,.\s]|不对|不是|错了|not that|that'?s (wrong|not)|我说的是|i said|停|别这样)/i;
102
+ // (b) — Fix round 2 (post-review, precision measured 28% on the real 14-day
103
+ // window; floor 60%): the brief-specified regex had two precision bugs.
104
+ // 1. No word boundaries on the Latin term: "again" matched the "again"
105
+ // substring inside "against" ("I'm against using X" — an OPINION, not a
106
+ // recurrence report). \b added.
107
+ // 2. Chinese "还是" is overwhelmingly used as "still/rather" ("我还是觉得
108
+ // X比较好" — a preference restatement, not "this happened AGAIN") — not a
109
+ // recurrence marker at all in the vast majority of real usage. Measured
110
+ // directly against the real transcript sample this fix round was scored
111
+ // against: every "还是"-triggered would_write candidate sampled was a
112
+ // false positive. Dropped entirely, per the brief's explicit instruction.
113
+ // 仍然/第二次/怎么又 are kept unchanged — they already only ever produce a
114
+ // candidate when a MINIMAL_VERB co-occurs in the SAME sentence (see
115
+ // detectSignalsInSession's `sentences.find(...)` below, itself a prior fix
116
+ // round's own precision fix), so the "only with an imperative in the SAME
117
+ // sentence" requirement already holds structurally for them.
118
+ //
119
+ // Fix round 3 (orchestrator decision, data-driven, real 48-day window):
120
+ // the "same sentence" requirement above does NOT rescue this signal's
121
+ // precision, because it doesn't hold structurally for the population that
122
+ // actually broke it. A voice-dictated run-on monologue (no `.`/`!`/`?`/
123
+ // newline anywhere — see splitSentences' own boundary contract) is, by
124
+ // construction, exactly ONE "sentence" for the whole turn — so an unrelated
125
+ // MINIMAL_VERB match anywhere later in that same monologue trivially
126
+ // "co-occurs" with an earlier, purely-temporal "again" ("we talked about
127
+ // this again last week... and separately I think we should use a different
128
+ // color..."). Measured 0 true positives / 2 false positives over the real
129
+ // window, both this exact mechanism. Regex tightening cannot fix an adverb
130
+ // sense-ambiguity ("again" as recurrence-of-mistake vs. plain temporal
131
+ // adverb is not decidable lexically) — per the same "drop the family, don't
132
+ // re-patch the regex" policy already applied to signal (c), signal (b) is
133
+ // now gated behind `experimentalSignals` too (see detectSignalsInSession
134
+ // below) rather than attempting a fourth regex round on this pattern.
135
+ const AGAIN_MARKER = /\bagain\b|仍然|第二次|怎么又/i;
136
+ // (b)'s "imperative-ish verb" — corrections.ts's STRONG_IMPERATIVE/WEAK_IMPERATIVE
137
+ // are precision-tuned, heavily regression-tested gate-v4 internals and are
138
+ // NOT exported (module-private by design — see corrections.ts's own doc
139
+ // comments on why splitting/exposing them is out of scope for this brief).
140
+ // Per the brief's own fallback clause ("otherwise a minimal verb list"), this
141
+ // is a deliberately independent, minimal, conservative verb list — NOT a
142
+ // copy of the gate's regex, so a future change to gate-v4's precision tuning
143
+ // can never silently change this miner's behavior (or vice versa).
144
+ const MINIMAL_VERB = /\b(use|using|don'?t|do not|stop|avoid|should|must|always|never|change|switch|instead|remove|add|make sure)\b|应该|不要|使用|改成|换成|停止|确保|必须|禁止|不能|换用|改用/i;
145
+ // ---------------------------------------------------------------------------
146
+ // Fix round 2 (post-review, precision measured 28% on the real 14-day window;
147
+ // floor 60%): 52% of ALL candidates in the real sample were mined from
148
+ // `<task-notification>` blocks — a background-task completion report that
149
+ // Claude Code injects as a `role:"user"` record (the harness's own way of
150
+ // delivering an async subagent's result back into the transcript), never
151
+ // something the human typed. Confirmed by a READ-ONLY grep of every
152
+ // `~/.claude/projects/*/*.jsonl` on this machine (2026-09-17): across
153
+ // ~24k user-typed records, exactly one injected-tag family is NOT already
154
+ // covered by transcript-project.ts's own `isSystemText`/`SYSTEM_PREFIXES`
155
+ // (which already catches `<local-command...`, `<command-name`,
156
+ // `<command-message`, `<system-reminder` by prefix match) —
157
+ // `<task-notification` — with 707 occurrences, by far the dominant tag.
158
+ // `<script`/`<instructions` were also observed (1 each) but are real
159
+ // HUMAN-PASTED content (a PostHog snippet, a user-authored prompt template)
160
+ // — never Claude-Code-injected — and must stay eligible; not added here.
161
+ //
162
+ // `<agent-message` is a SECOND member of the exact same class — not seen in
163
+ // this machine's 14-day sample (this user's multi-agent runs happened not to
164
+ // hit it in-window), but this codebase ALREADY treats it as a harness
165
+ // artifact of the identical shape in a sibling module solving the same
166
+ // problem (packages/cli/src/index.ts's hook-ambient/hook-correction
167
+ // `HARNESS_PREFIXES`, which early-exits on `<task-notification>|<agent-
168
+ // message|<local-command-caveat>|<command-name>|<system-reminder>` before
169
+ // ever scanning a prompt for a correction signal) — cli cannot be imported
170
+ // from core (core has no dependency on cli), so the constant itself can't be
171
+ // shared, but the CLASS it documents can: covered here for the same reason.
172
+ //
173
+ // Separately, `isMeta: true` marks a DIFFERENT injection family with no
174
+ // leading `<` tag at all: a slash-command's own skill markdown replayed
175
+ // verbatim as if the user had typed it (e.g. "# /arsave — AgentRecall
176
+ // Save\n\n..."), and the harness's own "[Your previous response had no
177
+ // visible output...]" continuation nudge. Both are assistant/harness-
178
+ // authored, never human speech — confirmed structurally (`rec.isMeta`) and
179
+ // empirically (401 isMeta:true user records sampled; zero were human-typed).
180
+ const INJECTED_BLOCK_TAG_RE = /^(<task-notification\b|<agent-message)/i;
181
+ // ---------------------------------------------------------------------------
182
+ // Fix-round (post-review, 2026-09-17) — image-attachment placeholder noise.
183
+ //
184
+ // Claude Code auto-injects a caption-only text block for every pasted/
185
+ // attached image: "[Image: source: /Users/.../image-cache/<sid>/<n>.png]"
186
+ // (one per file in the local image cache) and "[Image: original WxH,
187
+ // displayed at WxH...]" (paste-resize metadata). Neither carries any
188
+ // human-authored content, yet a session with 2+ pasted screenshots
189
+ // (extremely common in real usage) produces near-identical placeholder
190
+ // strings that otherwise clear signal (c)'s ≥6-word / Jaccard≥0.6 gate on
191
+ // boilerplate alone — measured as 36/52 (69.2%) of every real would_write
192
+ // candidate in the 14-day review sample. Stripped BEFORE any signal test
193
+ // (same altitude as isSystemText's own strip-before-test convention) so
194
+ // placeholder text can never contribute a single character to a candidate.
195
+ //
196
+ // "[Image #N]" is a DIFFERENT, narrower marker Claude Code prefixes onto a
197
+ // turn that pastes an image ALONGSIDE real typed text (e.g. "[Image #1] can
198
+ // you update..."). It is decoration only — stripped so it can never pad
199
+ // word/Jaccard counts — but unlike the caption-only placeholder above it
200
+ // never gates a turn out entirely; the real text after it still matters.
201
+ const IMAGE_PLACEHOLDER_RE = /\[Image:\s*(?:source:[^\]]*|original\s+\d+x\d+[^\]]*)\]/gi;
202
+ const IMAGE_MARKER_RE = /\[Image\s*#\d+\]/gi;
203
+ // Fix round 2 (post-review, precision measured 28% on the real 14-day
204
+ // window): this previously ran `.replace(/\s+/g, " ")` LAST, which flattens
205
+ // every newline into a plain space too — turning a multi-line turn (a
206
+ // perfectly normal shape: one line per thought, no terminal punctuation
207
+ // between them) into one run-on string. `splitSentences()` (storage/
208
+ // corrections.ts) treats a bare newline as an UNCONDITIONAL sentence
209
+ // boundary (its own S-M2 fix) — destroying every newline here silently
210
+ // defeats the same-sentence proximity gates signal (a)/(b) rely on
211
+ // (`firstGateSentence`/`verbSentence` below), because two originally-distinct
212
+ // lines can no longer be told apart from one long "sentence". Fixed by
213
+ // collapsing only HORIZONTAL whitespace (space/tab) per line, never the
214
+ // newline itself that separates lines.
215
+ function stripImageAttachmentNoise(text) {
216
+ const stripped = text.replace(IMAGE_PLACEHOLDER_RE, " ").replace(IMAGE_MARKER_RE, " ");
217
+ return stripped
218
+ .split("\n")
219
+ .map((l) => l.replace(/[^\S\n]+/g, " ").trim())
220
+ .join("\n")
221
+ .trim();
222
+ }
223
+ // Word-counting / Jaccard tokenization — same CJK-aware tokenizer the rest of
224
+ // corrections.ts uses (helpers/tokenize.ts), with the SAME punctuation-strip
225
+ // options distillRuleIdentity uses, so "word"/"token" means the same thing
226
+ // here as it does in the storage layer's own consolidation identity.
227
+ const WORD_OPTS = { minLength: 1, asciiStripRegex: /[^\p{L}\p{N}\s]+/gu };
228
+ function wordCount(text) {
229
+ return tokenizeWords(text, WORD_OPTS).length;
230
+ }
231
+ function jaccard(a, b) {
232
+ const setA = new Set(tokenizeWords(a, WORD_OPTS));
233
+ const setB = new Set(tokenizeWords(b, WORD_OPTS));
234
+ if (setA.size === 0 || setB.size === 0)
235
+ return 0;
236
+ let intersection = 0;
237
+ for (const tok of setA)
238
+ if (setB.has(tok))
239
+ intersection++;
240
+ const union = setA.size + setB.size - intersection;
241
+ return union > 0 ? intersection / union : 0;
242
+ }
243
+ function truncate(text, maxLen) {
244
+ const t = text.trim().replace(/\s+/g, " ");
245
+ if (t.length <= maxLen)
246
+ return t;
247
+ return t.slice(0, maxLen - 1).trimEnd() + "…";
248
+ }
249
+ /** Last sentence of `text` (the sentence immediately preceding a user turn), or "" when text is empty. */
250
+ function lastSentence(text) {
251
+ if (!text)
252
+ return "";
253
+ const sentences = splitSentences(text);
254
+ return sentences.length > 0 ? sentences[sentences.length - 1] : text;
255
+ }
256
+ // Fix-round (post-review): signal (a) previously ran MINIMAL_VERB against
257
+ // the WHOLE remainder (up to 200 chars), with no requirement that the verb
258
+ // relate to the negation opener at all. Demonstrated false positive: "No
259
+ // need to revoke. This is not a dangerous action because it's useless for
260
+ // the people who use these npm access tokens..." — MINIMAL_VERB matched
261
+ // "use" in an unrelated clause about OTHER PEOPLE, nowhere near an actual
262
+ // instruction. `that's (wrong|not)` is itself one of NEGATION_OPENER's own
263
+ // acknowledgment-only alternatives — when it appears chained right after the
264
+ // primary opener match ("No, that's wrong. <real instruction>"), it carries
265
+ // zero instruction content and must not count toward the corrective clause.
266
+ // Stripping any such chained acknowledgment fragment(s), then requiring the
267
+ // verb to appear in the FIRST remaining sentence only (not scanned across
268
+ // arbitrary later, unrelated sentences), keeps the existing "No, that's
269
+ // wrong. Use the blue button..." fixture passing while rejecting the
270
+ // npm-token false positive, whose verb-bearing sentence is not the first.
271
+ // Fix round 2 (post-review, precision measured 28% on the real 14-day
272
+ // window): the ONLY question-exclusion gate that existed was signal (c)'s
273
+ // own `/[??]\s*$/.test(second.text.trim())` — anchored to the very END of
274
+ // the WHOLE raw turn, and only ever checked for signal (c). Two ways that
275
+ // was defeated in the real sample:
276
+ // 1. Signals (a)/(b) had NO question check at all.
277
+ // 2. Even for (c), a question that wasn't the literal last character of
278
+ // the turn — embedded earlier, or followed by trailing decoration
279
+ // (a closing quote, a trailing dash/ellipsis, "right?" mid-sentence
280
+ // before more prose) — never matched the `$`-anchored regex.
281
+ // Fixed by checking every sentence of the CANDIDATE RULE TEXT itself (not
282
+ // the raw turn) for a `?`/`?` anywhere within it — per the brief's own
283
+ // "when in doubt, drop": a repeated/re-stated CORRECTION is essentially
284
+ // never phrased as a question; an open question (or a question-shaped
285
+ // re-instruction) almost always is. Checking per-sentence (rather than
286
+ // requiring the mark to be the rule text's own final character) means
287
+ // trailing decoration after the mark can never hide it.
288
+ function containsQuestionSentence(text) {
289
+ return splitSentences(text).some((s) => /[??]/.test(s));
290
+ }
291
+ function stripChainedAckOpeners(text) {
292
+ let t = text;
293
+ for (let i = 0; i < 3; i++) {
294
+ const m = NEGATION_OPENER.exec(t);
295
+ if (!m)
296
+ break;
297
+ const rest = t.slice(m[0].length).replace(/^[,.\s,。!?]+/, "");
298
+ if (rest === t)
299
+ break;
300
+ t = rest;
301
+ }
302
+ return t;
303
+ }
304
+ function extractUserTurnsWithContext(lines) {
305
+ const turns = [];
306
+ let lastAssistantText = "";
307
+ lines.forEach((d, index) => {
308
+ if (!d || typeof d !== "object")
309
+ return;
310
+ const rec = d;
311
+ if (isBoilerplateRecord(rec))
312
+ return;
313
+ // Fix-round (post-review): Claude Code auto-injects a compaction/
314
+ // continuation summary as a role:"user" record whenever a session
315
+ // compacts ("This session is being continued from a previous
316
+ // conversation..."). That text is assistant-authored recap — never
317
+ // something the human typed in that turn — yet a good summary literally
318
+ // restates prior decisions/corrections in rule-like phrasing, making it
319
+ // a magnet for all three signal types. Claude Code itself flags this
320
+ // shape structurally via `isCompactSummary: true`; skipped before any
321
+ // type-branching so it can never become a user turn OR pollute the
322
+ // "preceding assistant" context text.
323
+ if (rec.isCompactSummary === true)
324
+ return;
325
+ if (rec.type === "user") {
326
+ if ("attachment" in rec)
327
+ return;
328
+ // Fix round 2 — see INJECTED_BLOCK_TAG_RE's doc comment above:
329
+ // isMeta:true skill-injection/continuation-nudge records are never
330
+ // something the human typed and must never become a UserTurn (nor
331
+ // contribute to `precedingAssistant` context — they simply vanish, the
332
+ // same treatment isCompactSummary already gets above).
333
+ if (rec.isMeta === true)
334
+ return;
335
+ const msg = rec.message;
336
+ const rawText = textFromContent(msg?.content);
337
+ if (!rawText || isSystemText(rawText))
338
+ return;
339
+ // Fix round 2 — a `<task-notification>` background-task report opens
340
+ // the turn; checked on the RAW (pre-strip) text, same altitude as the
341
+ // isSystemText check immediately above, so it can never leak a single
342
+ // character into a candidate.
343
+ if (INJECTED_BLOCK_TAG_RE.test(rawText.trimStart()))
344
+ return;
345
+ const text = stripImageAttachmentNoise(rawText);
346
+ if (!text)
347
+ return; // pure image-attachment placeholder turn — never real conversational content
348
+ turns.push({ index, text, precedingAssistant: lastAssistantText });
349
+ }
350
+ else if (rec.type === "assistant") {
351
+ const msg = rec.message;
352
+ const content = msg?.content;
353
+ const pieces = [];
354
+ if (Array.isArray(content)) {
355
+ for (const c of content) {
356
+ const cr = c;
357
+ if (cr.type === "text" && typeof cr.text === "string" && cr.text.length > 0 && !isSystemText(cr.text)) {
358
+ const stripped = stripImageAttachmentNoise(cr.text);
359
+ if (stripped)
360
+ pieces.push(stripped);
361
+ }
362
+ }
363
+ }
364
+ else {
365
+ const text = textFromContent(content);
366
+ if (text && !isSystemText(text)) {
367
+ const stripped = stripImageAttachmentNoise(text);
368
+ if (stripped)
369
+ pieces.push(stripped);
370
+ }
371
+ }
372
+ if (pieces.length > 0)
373
+ lastAssistantText = pieces.join("\n");
374
+ }
375
+ });
376
+ return turns;
377
+ }
378
+ /**
379
+ * Detects all three signals over one session's ordered user turns.
380
+ *
381
+ * A turn that already produced a candidate under a HIGHER-priority signal
382
+ * (negation > again > repeat, the brief's own a/b/c ordering) is skipped by
383
+ * the lower-priority signals — one turn should not mint two overlapping
384
+ * candidates for what is plausibly the same correction event. Returned in
385
+ * chronological (turnIndex) order.
386
+ */
387
+ function detectSignalsInSession(turns, experimentalSignals) {
388
+ const out = [];
389
+ const usedTurns = new Set();
390
+ // (a) negation opener + ≥4 words of re-instruction.
391
+ for (const turn of turns) {
392
+ if (usedTurns.has(turn.index))
393
+ continue;
394
+ const trimmed = turn.text.trim();
395
+ const m = NEGATION_OPENER.exec(trimmed);
396
+ if (!m)
397
+ continue;
398
+ const remainder = trimmed.slice(m[0].length).replace(/^[,.\s,。!?]+/, "").trim();
399
+ if (wordCount(remainder) < 4)
400
+ continue;
401
+ // Precision fix (own fixture-testing, pre-ship): "≥4 more words" alone
402
+ // still captures pure meta-commentary continuations that carry a negation
403
+ // opener but no actual RE-INSTRUCTION — e.g. "No, that's not what I
404
+ // meant" (5-word remainder "that's not what I meant", zero directive
405
+ // content) is corrections.ts's OWN canonical soft-ack fixture
406
+ // (rejected-log.test.mjs: `mk("no, that's not what I meant")` —
407
+ // acknowledgment). "Re-instruction" (the brief's own word) implies an
408
+ // actual instruction — require the SAME minimal verb signal (b) uses so
409
+ // a negation opener is only a candidate when it is followed by content
410
+ // that looks like a re-instruction, not just more hedging.
411
+ // Fix-round: verb must be in the FIRST sentence of the remainder once any
412
+ // chained acknowledgment-only opener fragment is stripped — see
413
+ // stripChainedAckOpeners's doc comment above. Prevents a coincidental
414
+ // verb match deep in an unrelated later clause from qualifying as
415
+ // "re-instruction".
416
+ const gateText = stripChainedAckOpeners(remainder);
417
+ const firstGateSentence = splitSentences(gateText)[0] ?? gateText;
418
+ if (wordCount(firstGateSentence) < 4 || !MINIMAL_VERB.test(firstGateSentence))
419
+ continue;
420
+ // Fix round 2: a question ANYWHERE in the remainder (the candidate rule
421
+ // text) drops the whole candidate — see containsQuestionSentence's doc
422
+ // comment above.
423
+ if (containsQuestionSentence(remainder))
424
+ continue;
425
+ out.push({
426
+ turnIndex: turn.index,
427
+ signal: "negation",
428
+ rule: truncate(remainder, 200),
429
+ context: truncate(lastSentence(turn.precedingAssistant), 150),
430
+ });
431
+ usedTurns.add(turn.index);
432
+ }
433
+ // (b) again-marker anywhere in the turn AND a minimal imperative-ish verb.
434
+ //
435
+ // Fix round 3 (orchestrator decision, data-driven, real 48-day window):
436
+ // DROPPED from the shipped DEFAULT, same as (c) below — 0 true positives /
437
+ // 2 false positives over the window, both the SAME structural mechanism
438
+ // (voice-dictation run-on monologue; see AGAIN_MARKER's own doc comment
439
+ // above for why the "same sentence" fix does not rescue this case — an
440
+ // unpunctuated monologue IS one sentence, so the proximity check is
441
+ // trivially satisfied). Only the negation-opener (a) produced a genuine
442
+ // true positive in-window. Gated behind the SAME `experimentalSignals`
443
+ // opt-in as (c) — kept, not deleted, and not regex-patched a fourth time.
444
+ if (experimentalSignals) {
445
+ for (const turn of turns) {
446
+ if (usedTurns.has(turn.index))
447
+ continue;
448
+ if (!AGAIN_MARKER.test(turn.text) || !MINIMAL_VERB.test(turn.text))
449
+ continue;
450
+ // Fix-round: the marker and the verb previously only had to appear
451
+ // ANYWHERE in the whole turn, independently — no requirement that they
452
+ // relate to each other. Require them to co-occur in the SAME sentence
453
+ // (the brief's own "the sentence carrying the verb" framing, tightened
454
+ // to also carry the recurrence marker) so an unrelated "again" earlier
455
+ // in a long turn can never corroborate an unrelated verb later in it.
456
+ const sentences = splitSentences(turn.text);
457
+ const verbSentence = sentences.find((s) => MINIMAL_VERB.test(s) && AGAIN_MARKER.test(s));
458
+ if (!verbSentence)
459
+ continue;
460
+ // Fix round 2: same per-sentence question exclusion as signal (a)/(c) —
461
+ // the verb-bearing sentence itself must not be a question.
462
+ if (containsQuestionSentence(verbSentence))
463
+ continue;
464
+ out.push({
465
+ turnIndex: turn.index,
466
+ signal: "again",
467
+ rule: truncate(verbSentence, 200),
468
+ context: truncate(lastSentence(turn.precedingAssistant), 150),
469
+ });
470
+ usedTurns.add(turn.index);
471
+ }
472
+ }
473
+ // (c) repeat-instruction — two ≥6-word turns, Jaccard ≥0.6; the SECOND is
474
+ // the candidate. Each candidate (second occurrence) is claimed at most
475
+ // once, by its FIRST sufficiently-similar earlier turn.
476
+ //
477
+ // Fix round 2: DROPPED from the shipped DEFAULT (own-judgment precision
478
+ // review on the real 14-day window: 2 of 3 real repeat-instruction hits
479
+ // were false positives — see ImplicitHarvestOptions.experimentalSignals'
480
+ // doc comment for the two recurring mechanisms found). Only runs when the
481
+ // caller explicitly opts in.
482
+ const eligible = experimentalSignals
483
+ ? turns.filter((t) => !usedTurns.has(t.index) && wordCount(t.text) >= 6)
484
+ : [];
485
+ for (let i = 0; i < eligible.length; i++) {
486
+ for (let j = i + 1; j < eligible.length; j++) {
487
+ const second = eligible[j];
488
+ if (usedTurns.has(second.index))
489
+ continue;
490
+ // Fix-round: signal (c) had no content-shape gate at all — it fired on
491
+ // any two similar ≥6-word turns regardless of whether the content was
492
+ // a correction, a question, or a discussion restatement. Demonstrated:
493
+ // a user re-asking the same open-ended design question twice
494
+ // ("what would be success for cross-project long-term memory...") was
495
+ // captured as a candidate. A repeated CORRECTION is never phrased as a
496
+ // question; a repeated open question almost always is — conservative,
497
+ // precision-first exclusion per the brief's own "when in doubt, drop".
498
+ // Fix round 2: widened from an END-anchored check on the raw turn
499
+ // (`/[??]\s*$/`) to the shared per-sentence containsQuestionSentence
500
+ // helper — catches a question anywhere in the turn, including one
501
+ // followed by trailing decoration after the mark, not just as the
502
+ // turn's own literal last character.
503
+ if (containsQuestionSentence(second.text))
504
+ continue;
505
+ if (jaccard(eligible[i].text, second.text) < 0.6)
506
+ continue;
507
+ out.push({
508
+ turnIndex: second.index,
509
+ signal: "repeat",
510
+ rule: truncate(second.text, 200),
511
+ context: truncate(lastSentence(second.precedingAssistant), 150),
512
+ });
513
+ usedTurns.add(second.index);
514
+ }
515
+ }
516
+ out.sort((a, b) => a.turnIndex - b.turnIndex);
517
+ return out;
518
+ }
519
+ // ---------------------------------------------------------------------------
520
+ // Correction-record construction + write
521
+ // ---------------------------------------------------------------------------
522
+ const MAX_CANDIDATES_PER_SESSION = 5;
523
+ /**
524
+ * Own-fixture-testing bug fix (pre-ship): a content-derived id (hash of the
525
+ * rule text) is WRONG here, not just unnecessary. writeCorrection's on-write
526
+ * consolidation loop explicitly skips `existing.id === record.id` ("never
527
+ * merge into self (same-day re-slug)") — so two INDEPENDENT candidates that
528
+ * normalize to the same rule text on the same day (session-to-session
529
+ * near-duplicates, or a same-day re-run over the same transcripts) would
530
+ * compute the SAME hash-derived id, get treated as "self" by the merge scan,
531
+ * fall through to the brand-new-record branch, collide on filename, and get
532
+ * hash-disambiguated into a SECOND FILE — two on-disk records sharing one id.
533
+ * Verified directly (own fixture: two sessions, identical rule text, same
534
+ * day → 2 files, both `written:true, merged:false`, identical id on disk).
535
+ *
536
+ * Fix: id must be unique PER WRITE ATTEMPT, never derived from content —
537
+ * rule-text consolidation (normalizeRule, content-keyed, id-independent) is
538
+ * the ONLY mechanism this module relies on for merging; id here exists
539
+ * solely for readability/traceability in reports.
540
+ */
541
+ function buildId(day) {
542
+ const rand = crypto.randomBytes(6).toString("hex");
543
+ return `${day}-implicit-${rand}`;
544
+ }
545
+ function buildRecord(day, project, cand) {
546
+ return {
547
+ id: buildId(day),
548
+ date: day,
549
+ // Hard-floored — see this file's header doc. Never derived, never "p0".
550
+ severity: "p1",
551
+ project,
552
+ rule: cand.rule,
553
+ context: cand.context,
554
+ tags: ["implicit"],
555
+ kind: "correction",
556
+ weight: 0.3,
557
+ confidence: "low",
558
+ provenance: { source: "transcript-implicit", mode: "observed" },
559
+ };
560
+ }
561
+ // ---------------------------------------------------------------------------
562
+ // Per-day harvest
563
+ // ---------------------------------------------------------------------------
564
+ async function harvestDay(day, allFiles, targetProjectSlug, write, experimentalSignals) {
565
+ const summary = {
566
+ date: day,
567
+ sessions_scanned: 0,
568
+ candidates_by_signal: { negation: 0, again: 0, repeat: 0 },
569
+ capped: 0,
570
+ gated_out: 0,
571
+ written: 0,
572
+ dedup_merged: 0,
573
+ unresolved_project_sessions: [],
574
+ candidates: [],
575
+ };
576
+ const dayFiles = allFiles
577
+ .filter((f) => f.byDay.has(day))
578
+ .filter((f) => (targetProjectSlug ? f.project === targetProjectSlug : true))
579
+ .sort((a, b) => a.basename.localeCompare(b.basename));
580
+ summary.sessions_scanned = dayFiles.length;
581
+ for (const file of dayFiles) {
582
+ // A session whose project could not be resolved ("auto") has nowhere
583
+ // safe to write a correction — never guessed, always reported.
584
+ if (file.project === "auto") {
585
+ summary.unresolved_project_sessions.push(file.basename);
586
+ continue;
587
+ }
588
+ const lines = file.byDay.get(day) ?? [];
589
+ const turns = extractUserTurnsWithContext(lines);
590
+ const raw = detectSignalsInSession(turns, experimentalSignals);
591
+ for (const cand of raw)
592
+ summary.candidates_by_signal[cand.signal]++;
593
+ for (let position = 0; position < raw.length; position++) {
594
+ const cand = raw[position];
595
+ const base = {
596
+ date: day,
597
+ project: file.project,
598
+ session: file.basename,
599
+ signal: cand.signal,
600
+ rule: cand.rule,
601
+ context: cand.context,
602
+ };
603
+ // Cap: max 5 candidates/session — over-cap candidates are visible in
604
+ // the report (precision sampling needs to see the raw firing rate) but
605
+ // never gate-checked or written.
606
+ if (position >= MAX_CANDIDATES_PER_SESSION) {
607
+ summary.capped++;
608
+ summary.candidates.push({ ...base, outcome: "capped" });
609
+ continue;
610
+ }
611
+ if (!write) {
612
+ // Dry-run: predict the gate outcome with zero disk mutation — never
613
+ // call writeCorrection (which mutates on success) or
614
+ // logRejectedCorrection (which appends to _rejected.jsonl) here.
615
+ const wouldPass = dropHardNoise(cand.rule);
616
+ if (!wouldPass) {
617
+ summary.gated_out++;
618
+ summary.candidates.push({ ...base, outcome: "gated", reason: "implicit-gate" });
619
+ }
620
+ else {
621
+ summary.candidates.push({ ...base, outcome: "would_write" });
622
+ }
623
+ continue;
624
+ }
625
+ const record = buildRecord(day, file.project, cand);
626
+ const result = await writeCorrection(file.project, record, { skipActionableGate: true });
627
+ if (!result.written) {
628
+ summary.gated_out++;
629
+ summary.candidates.push({ ...base, outcome: "gated", reason: result.reason });
630
+ }
631
+ else if (result.merged) {
632
+ summary.dedup_merged++;
633
+ summary.candidates.push({ ...base, outcome: "merged", correction_id: result.id });
634
+ }
635
+ else {
636
+ summary.written++;
637
+ summary.candidates.push({ ...base, outcome: "written", correction_id: result.id });
638
+ }
639
+ }
640
+ }
641
+ return summary;
642
+ }
643
+ // ---------------------------------------------------------------------------
644
+ // Public entry point
645
+ // ---------------------------------------------------------------------------
646
+ /**
647
+ * `ar corrections harvest-implicit` — see this file's header doc.
648
+ *
649
+ * Dry-run by DEFAULT (`options.write` must be explicitly true to touch disk)
650
+ * — matches the brief's flag contract exactly, not the opposite.
651
+ */
652
+ export async function runImplicitHarvest(options) {
653
+ let days;
654
+ try {
655
+ days = resolveDayRange(options);
656
+ }
657
+ catch {
658
+ throw new Error("runImplicitHarvest: either --date <YYYY-MM-DD> or --backfill --since <YYYY-MM-DD> is required");
659
+ }
660
+ const claudeDir = options.claudeDir ?? defaultClaudeDir();
661
+ const { files } = scanTranscripts(claudeDir); // ambiguous transcripts carry no day bucket — never harvested
662
+ const write = !!options.write;
663
+ // Fix round 2 — signal (c) repeat-instruction is opt-in only; see
664
+ // ImplicitHarvestOptions.experimentalSignals' doc comment.
665
+ const experimentalSignals = !!options.experimentalSignals;
666
+ const targetProjectSlug = options.project
667
+ ? resolveProjectDirName(getRoot(), options.project)
668
+ : undefined;
669
+ const dayResults = [];
670
+ for (const day of days) {
671
+ dayResults.push(await harvestDay(day, files, targetProjectSlug, write, experimentalSignals));
672
+ }
673
+ return { dry_run: !write, claude_dir: claudeDir, days: dayResults };
674
+ }
675
+ //# sourceMappingURL=implicit-harvest.js.map