claude-mem-lite 6.32.0 → 6.34.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,7 +9,7 @@
9
9
  "plugins": [
10
10
  {
11
11
  "name": "claude-mem-lite",
12
- "version": "6.32.0",
12
+ "version": "6.34.0",
13
13
  "source": "./",
14
14
  "homepage": "https://github.com/sdsrss/claude-mem-lite",
15
15
  "description": "Persistent long-term memory for Claude Code via MCP — captures coding decisions, bugfixes, and context across sessions. FTS5 BM25 keyword search with episode batching. Single SQLite DB, no external services. A lighter, lower-cost alternative to claude-mem (episode batching + a smaller model; cost savings are an internal estimate, not a measured benchmark)."
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-mem-lite",
3
- "version": "6.32.0",
3
+ "version": "6.34.0",
4
4
  "description": "Persistent long-term memory for Claude Code via MCP — captures coding decisions, bugfixes, and context across sessions. FTS5 BM25 keyword search with episode batching. Single SQLite DB, no external services. A lighter, lower-cost alternative to claude-mem (episode batching + a smaller model; cost savings are an internal estimate, not a measured benchmark).",
5
5
  "author": {
6
6
  "name": "sdsrss"
package/README.md CHANGED
@@ -239,6 +239,34 @@ rm -rf ~/claude-mem-lite/ # unhidden data dir from before the first release (n
239
239
  repos/ # Shallow-cloned source repos
240
240
  ```
241
241
 
242
+ ## Upgrading to 6.34.0
243
+
244
+ **The session summary reads every turn's last reply; nothing to run, and the schema version stays
245
+ 49.** To go back, pin `claude-mem-lite@6.33.0` and delete `runtime/turn-replies-*.json` from the
246
+ data directory: 6.33.0 never reads them.
247
+
248
+ - **The session summary model reads each turn's last reply**, not only the observations and
249
+ prompts. Stop keeps each turn's last reply, scrubbed, in `runtime/`.
250
+ - **The secret scrubber erases Hugging Face, DigitalOcean, PyPI, Shopify and Groq tokens.**
251
+ - See CHANGELOG.md.
252
+
253
+ ## Upgrading to 6.33.0
254
+
255
+ **`uninstall --purge` leaves your own files in a relocated data directory; nothing to run, and the
256
+ schema version stays 49.** The first open adds a nullable column to `events`, which an older build
257
+ ignores. To go back, pin `claude-mem-lite@6.32.0`.
258
+
259
+ - **With `CLAUDE_MEM_DIR` set, `uninstall --purge` removes only what claude-mem-lite writes
260
+ there**: its files by each writer's exact file-name format, and its `runtime/` and `managed/`
261
+ directories whole when nothing of yours sits beside them. It names what it leaves. It used to
262
+ remove the whole directory, your files included. The default
263
+ `~/.claude-mem-lite` is removed whole, as before.
264
+ - **The daily unattended maintenance pass no longer runs `normalize`**, which rewrote concept tags
265
+ with model-picked synonym groups and kept no record. Run it yourself with
266
+ `claude-mem-lite optimize --run --task normalize` (`--run-all` skips its weekly gate).
267
+ - **Dates and times are shown in local time**, the zone the date filters already read.
268
+ - Details in CHANGELOG.md.
269
+
242
270
  ## Upgrading to 6.32.0
243
271
 
244
272
  **The steering sync looks above the repository before writing its copy; nothing to run, and the
@@ -1290,7 +1318,7 @@ benchmark and A/B harness are calibrated against — changing them invalidates t
1290
1318
  | `CLAUDE_MEM_AUTO_DEEP` | `0` disables automatic deep-search escalation (one Haiku call rewriting a weak query into keyword/concept/HyDE variants). Explicit `deep: true` still works. | _(auto)_ |
1291
1319
  | `CLAUDE_MEM_DEEP_DISCLOSURE` | `off` suppresses the one-line caveat appended to a multi-variant deep result. The caveat exists because deep search fills the page even when the corpus cannot answer — measured at 10 of 10 slots on queries whose answers had been removed (`benchmark/deep-search-holdout.mjs`) — and `deep` is AUTO by default on the MCP surface, i.e. it escalates precisely when the honest answer is "nothing". It does not change retrieval, ranking, or which rows are returned. | _(on)_ |
1292
1320
  | `CLAUDE_MEM_REACH_DISCLOSURE` | `off` suppresses the one-line note that fires when a search's reported `total` exceeds what its pagination can hand back. The candidate pool is sized from `limit` alone and deliberately does not grow with `offset` (D#30 — an offset-scaled pool re-ranks its own prefix under RRF, so pages overlapped and gapped), while `total` is the full match count. Measured on a 128-row corpus: at the default limit of 20 the last non-empty offset is 59, so 60 of 128 rows are unreachable at any offset. The note reports that; it does not change retrieval, ranking, or which rows are returned. It stays **silent** when a filter you asked for (`tier`, or the CJK precision gate on prompts) removed rows after the count was taken — that gap is your filter, not the pool, and raising the limit would not recover it. | _(on)_ |
1293
- | `CLAUDE_MEM_NORMALIZE_CROSS_PROJECT` | `1` restores the pre-fix behaviour where the daily unattended `normalize` runs ONCE over every project's concepts at the same time. That is how one project's stored content could steer synonym groups applied to another project's rows, so the default is now one scoped pass per project (bounded to 8 per run). The cost of the default is that `k8s` in one project and `kubernetes` in another are no longer unified automatically. Note that EVERY unscoped run fans out, including an explicit `optimize --run --task normalize` with no `--project` — this variable is the only route back to the single cross-project pass. A foreground `optimize` run prints a warning when it is set; the daily unattended pass cannot (its worker is spawned with stderr closed), so `claude-mem-lite doctor` reports it as a ⚠ instead. | _(off)_ |
1321
+ | `CLAUDE_MEM_NORMALIZE_CROSS_PROJECT` | `1` restores the pre-fix behaviour where a `normalize` run without `--project` goes ONCE over every project's concepts at the same time. That is how one project's stored content could steer synonym groups applied to another project's rows, so the default is now one scoped pass per project (bounded to 8 per run). The cost of the default is that `k8s` in one project and `kubernetes` in another are no longer unified automatically. Note that EVERY unscoped run fans out, including an explicit `optimize --run --task normalize` with no `--project` — this variable is the only route back to the single cross-project pass. A foreground `optimize` run prints a warning when it is set, and `claude-mem-lite doctor` reports it as a ⚠ for `mem_optimize`, whose stderr is the MCP log. The daily unattended pass does not run normalize. | _(off)_ |
1294
1322
  | `CLAUDE_MEM_AUTO_DEEP_CLI` | `0` disables the same auto-escalation on the CLI path only. | _(auto)_ |
1295
1323
  | `CLAUDE_MEM_UPS_PROMPT_ECHO` | `0` lets the "Past similar questions" fallback show a stored question identical (or nearly) to the prompt just typed, from an earlier session. The current session's own prompts stay excluded either way: showing the prompt just typed was a bug. | _(on)_ |
1296
1324
  | `CLAUDE_MEM_SCOPE_FILTER` | `1` stops environment-scoped observations from firing on file-triggered recall. They stay reachable via search. **Leave it off**: on the face it gates, `environment` is not the low-relevance class its premise assumes — it cites at least as well as `project` (47.5% vs 44.3%, intervals overlapping), and an earlier measurement left 173 recall groups empty with it on. | _(off)_ |
@@ -1332,7 +1360,7 @@ what is already stored — only whether new work runs.
1332
1360
  | `CLAUDE_MEM_SKIP_SAVE_ENRICH` | Skip the background Haiku call that backfills `lesson_learned` / search aliases after a save. | _(runs)_ |
1333
1361
  | `CLAUDE_MEM_SKIP_COMPRESS` | Skip auto-compression of old observations. | _(runs)_ |
1334
1362
  | `CLAUDE_MEM_SKIP_MAINTAIN` | Skip the 24h auto-maintain pass (decay, purge, backup). | _(runs)_ |
1335
- | `CLAUDE_MEM_SKIP_OPTIMIZE` | Skip the LLM optimization pass (re-enrich, normalize, cluster-merge). | _(runs)_ |
1363
+ | `CLAUDE_MEM_SKIP_OPTIMIZE` | Skip the daily LLM optimization pass (re-enrich, cluster-merge, smart-compress). | _(runs)_ |
1336
1364
  | `CLAUDE_MEM_SKIP_AUTO_DEDUP_FUZZY` | Skip the MinHash near-duplicate pass, keeping exact dedup. | _(runs)_ |
1337
1365
  | `CLAUDE_MEM_SKIP_MARKER_GC` | Skip the runtime-marker sweep. **Must be exactly `1`** — unlike the other `CLAUDE_MEM_SKIP_*` flags, which accept any truthy value, this one compares against the string `1`. That is deliberate: a truthy check makes `=0` mean "skip", which is the opposite of what anyone typing it intends. | _(runs)_ |
1338
1366
  | `CLAUDE_MEM_SKIP_UPDATE` | Skip the 24h update check against GitHub Releases, its session-start notice, and `self-update` / `install.mjs update` (unset it to update). A `repair` triggered by a missing module or dependency still runs. | _(runs)_ |
package/README.zh-CN.md CHANGED
@@ -201,6 +201,23 @@ rm -rf ~/claude-mem-lite/ # 首个发布版之前的非隐藏目录(不是
201
201
  repos/ # 浅克隆的源代码仓库
202
202
  ```
203
203
 
204
+ ## 升级到 6.34.0
205
+
206
+ **会话摘要会读每一轮的最后回复;不用执行任何操作,schema 版本仍是 49。** 想退回,请固定 `claude-mem-lite@6.33.0`,并删掉数据目录里的 `runtime/turn-replies-*.json`:6.33.0 不读它们。
207
+
208
+ - **会话摘要模型除了观察和提示,还读每一轮的最后回复。** Stop 把每一轮的最后回复脱敏后存在 `runtime/`。
209
+ - **密钥脱敏会抹掉 Hugging Face、DigitalOcean、PyPI、Shopify 和 Groq 的令牌。**
210
+ - 详见 CHANGELOG.md。
211
+
212
+ ## 升级到 6.33.0
213
+
214
+ **数据目录改过位置时,`uninstall --purge` 不再删你自己的文件;不用执行任何操作,schema 版本仍是 49。** 第一次打开数据库时会给 `events` 加一个可空列,旧版本会忽略它。想退回,请固定 `claude-mem-lite@6.32.0`。
215
+
216
+ - **设了 `CLAUDE_MEM_DIR` 时,`uninstall --purge` 只删 claude-mem-lite 在那里写的东西**:按每个写入方的确切文件名格式匹配的文件;旁边没有你的东西时,再整个删掉它的 `runtime/` 和 `managed/` 目录。留下的东西会列出来。以前会删掉整个目录,连同你的文件。默认目录 `~/.claude-mem-lite` 仍然整个删除,和以前一样。
217
+ - **每日无人值守的维护不再运行 `normalize`**:它按模型挑的同义词组改写概念标签,而且不留记录。需要时手动运行 `claude-mem-lite optimize --run --task normalize`(用 `--run-all` 可跳过它的每周闸门)。
218
+ - **日期和时间按本地时区显示**,和日期筛选一直使用的时区一致。
219
+ - 详见 CHANGELOG.md。
220
+
204
221
  ## 升级到 6.32.0
205
222
 
206
223
  **写引导副本之前,插件先看仓库之上的文件;不用执行任何操作,schema 版本仍是 49。** 想退回,请固定 `claude-mem-lite@6.31.0`;它的下一个会话会把本地副本写回来。用户或项目设置里有 `claudeMdExcludes` 时,下面两条都不适用。
package/cli/activity.mjs CHANGED
@@ -17,9 +17,11 @@ import { snapshotDb } from '../lib/db-backup.mjs';
17
17
  import { isBlankText } from '../lib/blank-text.mjs';
18
18
  import { CLI_INVOKE } from '../cli-path.mjs';
19
19
 
20
+ // E#N, the form search, get and --supersedes use for an event: a bare #N names an observation
21
+ // everywhere else, so an id copied from this list into `delete` hit an unrelated row (converge R30).
20
22
  function formatActivityResults(rows) {
21
23
  if (!rows || rows.length === 0) return '(no events)';
22
- return rows.map((r) => `#${r.id} [${r.event_type}] ${r.title}`).join('\n');
24
+ return rows.map((r) => `E#${r.id} [${r.event_type}] ${r.title}`).join('\n');
23
25
  }
24
26
 
25
27
  /** An event id as typed: N, #N or E#N (the form search and get print), else null. */
package/cli/common.mjs CHANGED
@@ -9,7 +9,13 @@
9
9
  // consistent.
10
10
 
11
11
  import { editDistance } from '../lib/edit-distance.mjs';
12
- import { neutralizeContextDelimiters, neutralizeSkillDelimiters, truncate } from '../format-utils.mjs';
12
+ import {
13
+ neutralizeContextDelimiters,
14
+ neutralizeSkillDelimiters,
15
+ truncate,
16
+ localYmd,
17
+ localIsoTime,
18
+ } from '../format-utils.mjs';
13
19
  import { CLI_INVOKE } from '../cli-path.mjs';
14
20
 
15
21
  // ─── Argument Parsing ────────────────────────────────────────────────────────
@@ -554,10 +560,10 @@ export function relativeTime(epochMs) {
554
560
  return `${days}d ago`;
555
561
  }
556
562
 
557
- /** ISO date string → "YYYY-MM-DD" prefix. */
563
+ /** ISO date string → its local "YYYY-MM-DD" (D18: the day `--from/--to` read it as). */
558
564
  export function fmtDateShort(iso) {
559
565
  if (!iso) return '';
560
- return iso.slice(0, 10);
566
+ return localYmd(iso) || String(iso).slice(0, 10);
561
567
  }
562
568
 
563
569
  // Integer epoch-ms time fields on the observations table that `get`/`mem_get`
@@ -627,6 +633,10 @@ export function formatObsFieldValue(field, val) {
627
633
  if (OBS_TIME_FIELDS.includes(field) && typeof val === 'number') {
628
634
  return `${val} (${relativeTime(val)})`;
629
635
  }
636
+ // The stored ISO string is UTC; shown raw, its date is the next day for an evening save west of
637
+ // Greenwich, a day the date filters (local) do not find it on (D18). The CLI shows the local
638
+ // day in its header instead and skips the field.
639
+ if (field === 'created_at' && typeof val === 'string') return localIsoTime(val) || val;
630
640
  return val;
631
641
  }
632
642
 
@@ -671,6 +681,6 @@ export function formatReviewSide(row) {
671
681
  row.lesson_learned && row.lesson_learned !== 'none'
672
682
  ? `lesson: ${flat(row.lesson_learned, 100)}`
673
683
  : 'no lesson';
674
- const date = row.created_at_epoch ? new Date(row.created_at_epoch).toISOString().slice(0, 10) : '?';
684
+ const date = row.created_at_epoch ? localYmd(row.created_at_epoch) : '?';
675
685
  return `#${row.id} ${row.type || '?'} · importance ${row.importance ?? '?'} · ${row.project} · ${date} · ${lesson} · ${flat(row.narrative, 120) || '(no narrative)'}`;
676
686
  }
package/format-utils.mjs CHANGED
@@ -309,23 +309,62 @@ export function typeIcon(type) {
309
309
 
310
310
  const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
311
311
 
312
+ // Displayed dates and times are LOCAL (D18, converge R31, user-chosen 2026-11-11): `--from/--to`
313
+ // and date_from/date_to read a bare date as the local day (lib/search-core.mjs parseCalendarBound),
314
+ // and the dates printed in UTC disagreed with them — an evening save west of Greenwich showed as
315
+ // tomorrow and the date on screen did not find it.
316
+
317
+ /**
318
+ * A date (Date, epoch ms or ISO string) as the local calendar day "YYYY-MM-DD"; '' when unparseable.
319
+ * @param {Date|number|string} value
320
+ * @returns {string}
321
+ */
322
+ export function localYmd(value) {
323
+ if (value === null || value === undefined || value === '') return '';
324
+ const d = value instanceof Date ? value : new Date(value);
325
+ if (Number.isNaN(d.getTime())) return '';
326
+ const m = String(d.getMonth() + 1).padStart(2, '0');
327
+ const day = String(d.getDate()).padStart(2, '0');
328
+ return `${d.getFullYear()}-${m}-${day}`;
329
+ }
330
+
331
+ /**
332
+ * An instant (Date, epoch ms or ISO string) as local ISO 8601 with its UTC offset,
333
+ * "2026-11-10T21:52:00.000-08:00": the same instant as the stored UTC string, milliseconds
334
+ * included (writers store them, and a shown time pasted back as an inclusive `date_to` must not
335
+ * fall before its own row), and its date is the local day the date filters read. For detail
336
+ * views that print a full timestamp. '' when unparseable.
337
+ * @param {Date|number|string} value
338
+ * @returns {string}
339
+ */
340
+ export function localIsoTime(value) {
341
+ if (value === null || value === undefined || value === '') return '';
342
+ const d = value instanceof Date ? value : new Date(value);
343
+ if (Number.isNaN(d.getTime())) return '';
344
+ const p = (n) => String(n).padStart(2, '0');
345
+ const off = -d.getTimezoneOffset();
346
+ const zone = `${off < 0 ? '-' : '+'}${p(Math.floor(Math.abs(off) / 60))}:${p(Math.abs(off) % 60)}`;
347
+ const ms = String(d.getMilliseconds()).padStart(3, '0');
348
+ return `${localYmd(d)}T${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}.${ms}${zone}`;
349
+ }
350
+
312
351
  /**
313
- * Format an ISO date string as "Mon DD HH:MM" for compact display.
352
+ * Format an ISO date string as local "Mon DD HH:MM" for compact display.
314
353
  * @param {string} iso ISO 8601 date string
315
354
  * @returns {string} Formatted date or empty string
316
355
  */
317
356
  export function fmtDate(iso) {
318
357
  if (!iso) return '';
319
358
  const d = new Date(iso);
320
- const mon = MONTHS[d.getUTCMonth()];
321
- const day = d.getUTCDate();
322
- const h = String(d.getUTCHours()).padStart(2, '0');
323
- const m = String(d.getUTCMinutes()).padStart(2, '0');
359
+ const mon = MONTHS[d.getMonth()];
360
+ const day = d.getDate();
361
+ const h = String(d.getHours()).padStart(2, '0');
362
+ const m = String(d.getMinutes()).padStart(2, '0');
324
363
  return `${mon} ${day} ${h}:${m}`;
325
364
  }
326
365
 
327
366
  /**
328
- * Format an ISO date string as "HH:MM" for time-only display.
367
+ * Format an ISO date string as local "HH:MM" for time-only display.
329
368
  * @param {string} iso ISO 8601 date string
330
369
  * @returns {string} Formatted time or empty string
331
370
  */
@@ -333,11 +372,11 @@ export function fmtTime(iso) {
333
372
  if (!iso) return '';
334
373
  const d = new Date(iso);
335
374
  // Guard against an unparseable timestamp (e.g. corrupt/imported created_at):
336
- // a bare new Date('garbage') yields Invalid Date → getUTCHours() is NaN →
375
+ // a bare new Date('garbage') yields Invalid Date → getHours() is NaN →
337
376
  // "NaN:NaN" leaking into the SessionStart Recent table. Degrade to '' like the
338
377
  // falsy-input case above.
339
378
  if (Number.isNaN(d.getTime())) return '';
340
- return `${String(d.getUTCHours()).padStart(2, '0')}:${String(d.getUTCMinutes()).padStart(2, '0')}`;
379
+ return `${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`;
341
380
  }
342
381
 
343
382
  // ─── ISO Week ────────────────────────────────────────────────────────────────
package/hook-context.mjs CHANGED
@@ -13,6 +13,7 @@ import {
13
13
  truncate,
14
14
  typeIcon,
15
15
  fmtTime,
16
+ localYmd,
16
17
  inferProject,
17
18
  debugLog,
18
19
  neutralizeContextDelimiters,
@@ -757,7 +758,10 @@ export function buildSessionContextLines(
757
758
  if (recentObs.length > 0) {
758
759
  summaryLines.push('### Recent Activity');
759
760
  for (const o of recentObs) {
760
- summaryLines.push(`- ${truncate(o.title || '(untitled)', 80)}`);
761
+ // A row the top-up borrowed from another project says whose it is, as the Recent table
762
+ // does; bare, the model read it as this project's history (converge R30, E2E hooks F5).
763
+ const proj = o.project && o.project !== project ? ` (${o.project})` : '';
764
+ summaryLines.push(`- ${truncate(o.title || '(untitled)', 80)}${proj}`);
761
765
  }
762
766
  summaryLines.push('');
763
767
  }
@@ -930,17 +934,17 @@ export function buildSessionContextLines(
930
934
  (a, b) => Date.parse(b.created_at) - Date.parse(a.created_at) || b.id - a.id,
931
935
  );
932
936
  if (obsToShow.length > 0) {
933
- const today = now.toISOString().slice(0, 10);
937
+ const today = localYmd(now);
934
938
  obsLines.push(`### Recent (${today})`);
935
939
  obsLines.push('');
936
940
  obsLines.push('| ID | Time | T | Title |');
937
941
  obsLines.push('|----|------|---|-------|');
938
942
  // A row from another day carries its date: under the "Recent (<today>)" heading a bare
939
943
  // HH:MM read as today, and a 5-day-old row looked like this morning's (E2E 2026-10-16).
940
- // Same UTC calendar as the heading.
944
+ // Same local calendar as the heading (D18).
941
945
  const when = (iso) =>
942
- typeof iso === 'string' && fmtTime(iso) && iso.slice(0, 10) !== today
943
- ? `${iso.slice(5, 10)} ${fmtTime(iso)}`
946
+ typeof iso === 'string' && fmtTime(iso) && localYmd(iso) !== today
947
+ ? `${localYmd(iso).slice(5)} ${fmtTime(iso)}`
944
948
  : fmtTime(iso);
945
949
  for (const o of obsToShow) {
946
950
  const proj = o.project && o.project !== project ? ` (${o.project})` : '';
package/hook-llm.mjs CHANGED
@@ -28,6 +28,7 @@ import { BG_LLM_TIMEOUT_MS } from './haiku-client.mjs';
28
28
  import { scrubRecord, scrubFilePaths } from './lib/scrub-record.mjs';
29
29
  import { mergeModelSummary, summarySuperseded } from './lib/fast-summary.mjs';
30
30
  import { recordMetric } from './lib/metrics.mjs';
31
+ import { readTurnReplies } from './lib/turn-replies.mjs';
31
32
  import { DB_DIR } from './schema.mjs';
32
33
  import {
33
34
  insertObservationRow,
@@ -545,6 +546,8 @@ export function persistHaikuSummary(db, summary, ctx) {
545
546
  : null,
546
547
  importance: summary.importance ?? 1,
547
548
  created_at_epoch: Date.now(),
549
+ // D21: so the session summary can read the episodes this session stored as events.
550
+ memory_session_id: ctx.session_id ?? null,
548
551
  });
549
552
 
550
553
  if (ctx.preSavedObsId) {
@@ -1728,6 +1731,125 @@ async function waitForEpisodeFlushes(flushTimeout, superseded) {
1728
1731
  return false;
1729
1732
  }
1730
1733
 
1734
+ /**
1735
+ * D19 — how much of the session's final reply the summary prompt carries: its first
1736
+ * SUMMARY_REPLY_HEAD and last SUMMARY_REPLY_TAIL characters. On this machine's 159 sessions
1737
+ * (2026-11-11) the reply's median length was 2293 chars, p90 3211, max 4704, and the report
1738
+ * convention there puts the open items LAST, so a head-only cut would drop exactly the part
1739
+ * that says what is left (tasks/specs/converge-r31-decisions.md).
1740
+ */
1741
+ export const SUMMARY_REPLY_HEAD = 2000;
1742
+ export const SUMMARY_REPLY_TAIL = 1000;
1743
+
1744
+ /**
1745
+ * D22 — a session of two or more turns gives the summary prompt every turn's last reply, not
1746
+ * only the final one: on 20 multi-turn sessions the final-reply prompt left a main finished
1747
+ * piece of work out of `completed` in 13 (rulers.md, session summary events A/B, reading 2).
1748
+ * An earlier turn's reply is cut to its first SUMMARY_TURN_HEAD and last SUMMARY_TURN_TAIL
1749
+ * characters (the final one keeps the D19 cut), and at most the first SUMMARY_TURNS_FIRST and
1750
+ * the last SUMMARY_TURNS_LAST turns are shown — the cuts the D21/D22 judges read.
1751
+ */
1752
+ export const SUMMARY_TURN_HEAD = 700;
1753
+ export const SUMMARY_TURN_TAIL = 300;
1754
+ export const SUMMARY_TURNS_FIRST = 4;
1755
+ export const SUMMARY_TURNS_LAST = 8;
1756
+
1757
+ /**
1758
+ * The final reply as the summary prompt carries it: scrubbed whole first, so a secret
1759
+ * straddling a cut is never half-kept, then cut to head + tail with the omitted length named.
1760
+ * Cuts land on code-point boundaries. Empty for an empty or missing reply.
1761
+ */
1762
+ export function summaryReplyExcerpt(reply, head = SUMMARY_REPLY_HEAD, tail = SUMMARY_REPLY_TAIL) {
1763
+ const text = scrubSecrets(String(reply ?? '')).trim();
1764
+ const points = Array.from(text);
1765
+ if (points.length <= head + tail) return text;
1766
+ const omitted = points.length - head - tail;
1767
+ return `${points.slice(0, head).join('')}\n[… ${omitted} characters omitted …]\n${points.slice(-tail).join('')}`;
1768
+ }
1769
+
1770
+ /**
1771
+ * D22: the turns block of the summary prompt, oldest first, or '' for fewer than two turns with
1772
+ * text. The last turn is the final reply and keeps the D19 cut.
1773
+ * @param {string[]} replies each turn's last assistant text, oldest first
1774
+ */
1775
+ function summaryTurnsBlock(replies) {
1776
+ const turns = (Array.isArray(replies) ? replies : []).filter((r) => typeof r === 'string' && r.trim());
1777
+ if (turns.length < 2) return '';
1778
+ const n = turns.length;
1779
+ const cut = (r, i) =>
1780
+ i === n - 1 ? summaryReplyExcerpt(r) : summaryReplyExcerpt(r, SUMMARY_TURN_HEAD, SUMMARY_TURN_TAIL);
1781
+ const block = (i) => `[Turn ${i + 1} of ${n}${i === n - 1 ? ', final' : ''}]\n${cut(turns[i], i)}`;
1782
+ const idx = turns.map((_, i) => i);
1783
+ if (n <= SUMMARY_TURNS_FIRST + SUMMARY_TURNS_LAST) return idx.map(block).join('\n\n');
1784
+ const omitted = n - SUMMARY_TURNS_FIRST - SUMMARY_TURNS_LAST;
1785
+ return [
1786
+ ...idx.slice(0, SUMMARY_TURNS_FIRST).map(block),
1787
+ `[… ${omitted} turns omitted …]`,
1788
+ ...idx.slice(-SUMMARY_TURNS_LAST).map(block),
1789
+ ].join('\n\n');
1790
+ }
1791
+
1792
+ /**
1793
+ * D21: one list for the summary prompt, newest first. An event becomes an observation-shaped
1794
+ * row (its body is the lesson or narrative the episode stored); at equal times the observation
1795
+ * comes first, so the order never depends on the two tables' unrelated ids.
1796
+ * @param {{created_at_epoch?: number}[]} observations newest first
1797
+ * @param {{event_type: string, title: string, body: string|null, created_at_epoch: number}[]} events
1798
+ */
1799
+ export function mergeSummaryRows(observations, events) {
1800
+ const rows = [
1801
+ ...observations.map((o) => ({ row: o, at: o.created_at_epoch ?? 0, table: 0 })),
1802
+ ...events.map((e) => ({
1803
+ row: { type: e.event_type, title: e.title, narrative: e.body || '' },
1804
+ at: e.created_at_epoch ?? 0,
1805
+ table: 1,
1806
+ })),
1807
+ ];
1808
+ rows.sort((a, b) => b.at - a.at || a.table - b.table);
1809
+ return rows.map((r) => r.row);
1810
+ }
1811
+
1812
+ /**
1813
+ * The session summary prompt. `observations` are {type, title, narrative} newest first,
1814
+ * `userPrompts` the raw prompt texts in order; both are cut here. `finalReply`, when given, is
1815
+ * the session's last assistant message (D19): it goes into the user part behind the same input
1816
+ * guard, and the system part says the outcome it reports wins. Without one the prompt is the
1817
+ * one shipped before D19, byte for byte. `turnReplies` (D22), each turn's last reply oldest
1818
+ * first, replaces the final reply when it holds two or more turns with text (its last one IS
1819
+ * the final reply); with fewer the prompt is the D19 one.
1820
+ */
1821
+ export function buildSessionSummaryPrompt({ project, observations, userPrompts, finalReply, turnReplies }) {
1822
+ const obsList = observations
1823
+ .map((o, i) => `${i + 1}. [${o.type}] ${o.title}${o.narrative ? ': ' + truncate(o.narrative, 200) : ''}`)
1824
+ .join('\n');
1825
+ const prompts = userPrompts.map((p) => truncate(p, 300));
1826
+ const promptCtx = prompts.length > 0 ? `\nUser requests: ${prompts.join(' → ')}\n` : '';
1827
+ const turns = summaryTurnsBlock(turnReplies);
1828
+ const reply = turns ? '' : summaryReplyExcerpt(finalReply);
1829
+
1830
+ // cso F#4: split system/user. The userPrompts content is the single highest-leakage path
1831
+ // for memory poisoning — putting it in the user role behind an explicit boundary is the
1832
+ // main win here. The final reply is captured content too and goes on the same side.
1833
+ const system = `Summarize this coding session. Return ONLY valid JSON, no markdown fences.
1834
+ ${MEMORY_INPUT_GUARD}
1835
+
1836
+ JSON: {"request":"what the user was working on","completed":"specific items accomplished with file names","remaining_items":"specific unfinished items from the original request — compare investigation scope with actual changes to infer what was NOT yet done; be precise with file:issue format, or empty string if all done","next_steps":"suggested follow-up","lessons":["non-obvious insights discovered during this session"],"key_decisions":["important design choices made and WHY"]}
1837
+ lessons: Only genuinely non-obvious insights (debugging discoveries, gotchas, architectural reasons). Empty array if routine.
1838
+ key_decisions: Only decisions with lasting impact (library choices, architecture, data model). Include reasoning. Empty array if none.${
1839
+ turns
1840
+ ? `
1841
+ Replies: the user message ends with the assistant's last reply of each turn, oldest first; the last one is the session's final reply. These are the assistant's own account of the outcome. completed lists the main work the replies report as done in every turn, not only the last. Work a reply reports as done (tests passed, saved, committed, fixed) is completed — never list it in remaining_items or next_steps. An item's state is the one given by the latest reply that mentions it. What is still unfinished, failed or left for later belongs in remaining_items.`
1842
+ : reply
1843
+ ? `
1844
+ Final reply: the user message ends with the session's final reply, the assistant's own account of the outcome. Work it reports as done (tests passed, saved, committed, fixed) is completed — never list it in remaining_items or next_steps. What it reports as unfinished, failed or left for later belongs in remaining_items.`
1845
+ : ''
1846
+ }`;
1847
+ const user = `Project: ${project}${promptCtx}
1848
+ Observations (${observations.length} total):
1849
+ ${obsList}${turns ? `\n\nReplies, one per turn, oldest first:\n${turns}` : reply ? `\n\nFinal reply:\n${reply}` : ''}`;
1850
+ return { system, user };
1851
+ }
1852
+
1731
1853
  export async function handleLLMSummary() {
1732
1854
  // Seconds. The default spans an episode worker's whole bound (E2E 2026-10-16 P2-4): it was 15,
1733
1855
  // below one CLI-leg episode (2-5 s delay + 8-13 s call, + the lesson retry on a bugfix), so the
@@ -1789,12 +1911,6 @@ export async function handleLLMSummary() {
1789
1911
 
1790
1912
  if (recentObs.length < 1) return outcome('no-obs');
1791
1913
 
1792
- const obsList = recentObs
1793
- .map(
1794
- (o, i) => `${i + 1}. [${o.type}] ${o.title}${o.narrative ? ': ' + truncate(o.narrative, 200) : ''}`,
1795
- )
1796
- .join('\n');
1797
-
1798
1914
  // Include user prompts for richer context
1799
1915
  const userPrompts = db
1800
1916
  .prepare(
@@ -1804,22 +1920,19 @@ export async function handleLLMSummary() {
1804
1920
  `,
1805
1921
  )
1806
1922
  .all(sessionId)
1807
- .map((p) => truncate(p.prompt_text, 300));
1808
- const promptCtx = userPrompts.length > 0 ? `\nUser requests: ${userPrompts.join(' → ')}\n` : '';
1809
-
1810
- // cso F#4: split system/user. The userPrompts content (line 921) is the
1811
- // single highest-leakage path for memory poisoning — putting it in the
1812
- // user role behind an explicit boundary is the main win here.
1813
- const system = `Summarize this coding session. Return ONLY valid JSON, no markdown fences.
1814
- ${MEMORY_INPUT_GUARD}
1815
-
1816
- JSON: {"request":"what the user was working on","completed":"specific items accomplished with file names","remaining_items":"specific unfinished items from the original request — compare investigation scope with actual changes to infer what was NOT yet done; be precise with file:issue format, or empty string if all done","next_steps":"suggested follow-up","lessons":["non-obvious insights discovered during this session"],"key_decisions":["important design choices made and WHY"]}
1817
- lessons: Only genuinely non-obvious insights (debugging discoveries, gotchas, architectural reasons). Empty array if routine.
1818
- key_decisions: Only decisions with lasting impact (library choices, architecture, data model). Include reasoning. Empty array if none.`;
1819
- const user = `Project: ${project}${promptCtx}
1820
- Observations (${recentObs.length} total):
1821
- ${obsList}`;
1822
- const prompt = { system, user };
1923
+ .map((p) => p.prompt_text);
1924
+ // D19 + D22 (D#825): every turn's last reply, as Stop recorded them under this session
1925
+ // (lib/turn-replies.mjs). Two or more show every turn; one is the final reply; none gives the
1926
+ // prompt without a reply. The Stop worker and the /clear or /compact one read the same record,
1927
+ // so neither depends on which transcript it was handed or what was written there since.
1928
+ const turnReplies = readTurnReplies(RUNTIME_DIR, sessionId);
1929
+ const prompt = buildSessionSummaryPrompt({
1930
+ project,
1931
+ observations: recentObs,
1932
+ userPrompts,
1933
+ finalReply: turnReplies.at(-1) ?? null,
1934
+ turnReplies,
1935
+ });
1823
1936
 
1824
1937
  if (!(await acquireLLMSlot())) {
1825
1938
  debugLog('WARN', 'llm-summary', 'semaphore timeout, skipping summary');
package/hook-optimize.mjs CHANGED
@@ -1063,24 +1063,11 @@ export async function executeNormalize(db, force = false, { project } = {}) {
1063
1063
  // was simply still using the legacy unscoped mode.
1064
1064
  if (!project) {
1065
1065
  if (String(process.env.CLAUDE_MEM_NORMALIZE_CROSS_PROJECT || '') === '1') {
1066
- // This reaches a caller that OWNS ITS STDERR, and that bound is the whole story of
1067
- // the line. Three callers reach here: the CLI (`optimize --run --task normalize`,
1068
- // the user's own terminal), the MCP server (`mem_optimize`, server.mjs:1545 — its
1069
- // stderr is the host's MCP log, so this does land somewhere a human can reach), and
1070
- // the daily unattended pass, which is the one that cannot.
1071
- // Second review moved it off `debugLog` (which returns early unless
1072
- // CLAUDE_MEM_DEBUG is set, and the detached worker does not set it) and the test
1073
- // certifying the repair spied on `console.error` IN PROCESS — which proves the
1074
- // function emits, not that anyone receives. Nobody does, on the path that matters:
1075
- // `hook.mjs` reaches this via `spawnBackground('llm-optimize')`, and hook-shared.mjs
1076
- // spawns with `stdio: 'ignore'`, so the child's fd 2 IS /dev/null. Dropping the
1077
- // CLAUDE_MEM_DEBUG gate removed one of two blockers and the remaining one is
1078
- // sufficient on its own.
1079
- // So: useful for `claude-mem-lite optimize --run --task normalize`, silent for the
1080
- // daily unattended pass. The unattended disclosure is carried by `doctor`, which the
1081
- // user runs in their own terminal — same shape and prefix as install.mjs's
1082
- // CLAUDE_MEM_SKIP_SIG_VERIFY notice. Do not delete either half; they cover different
1083
- // paths, and tests/normalize-cross-project-disclosure.test.mjs pins both.
1066
+ // console.error, not debugLog (which returns early unless CLAUDE_MEM_DEBUG is set). Two
1067
+ // callers reach here: the CLI (`optimize --run --task normalize`, the user's own
1068
+ // terminal) and the MCP server (`mem_optimize`, whose stderr is the host's MCP log). The
1069
+ // daily pass does not since D20 (2026-11-11). `doctor` repeats the disclosure where the
1070
+ // user looks; tests/normalize-cross-project-disclosure.test.mjs pins both halves.
1084
1071
  console.error(
1085
1072
  '[claude-mem-lite] WARNING: CLAUDE_MEM_NORMALIZE_CROSS_PROJECT=1 — normalize is ' +
1086
1073
  'running over every project at once, so one project’s stored content can steer the ' +
@@ -2091,6 +2078,12 @@ export async function optimizeRun(
2091
2078
  return results;
2092
2079
  }
2093
2080
 
2081
+ /** What the daily unattended pass runs (D20: no normalize). Exported for its test. */
2082
+ export const DAILY_OPTIMIZE = Object.freeze({
2083
+ reenrichScope: 'wide',
2084
+ tasks: Object.freeze(['re-enrich', 'cluster-merge', 'smart-compress']),
2085
+ });
2086
+
2094
2087
  export async function handleLLMOptimize() {
2095
2088
  const { ensureDb } = await import('./schema.mjs');
2096
2089
  let db;
@@ -2108,10 +2101,18 @@ export async function handleLLMOptimize() {
2108
2101
  // targets bugfix/refactor/feature/decision rows with substantive narrative
2109
2102
  // but missing lesson_learned, which is exactly the audit's 11.2% coverage
2110
2103
  // gap. CLI `mem optimize` keeps narrow as default for explicit invocations.
2111
- const results = await optimizeRun(db, { reenrichScope: 'wide' });
2104
+ //
2105
+ // No normalize here (D20, 2026-11-11). It rewrites observations.concepts with whatever
2106
+ // synonym groups the model returns, and one real call on a copy of the maintainer's store
2107
+ // returned 21 groups, about half of them not synonyms (`fmtDate <- date`,
2108
+ // `CLAUDE.md <- CLAUDE.local.md`); nothing recorded the rewrite and nothing undoes it. The
2109
+ // groups it gets right are mostly case and plural variants, which search already covers:
2110
+ // the FTS tokenizer folds case, wordFormPrefixQuery adds the stem prefix, and the original
2111
+ // word stays in search_aliases. An explicit `optimize --run --task normalize` still runs it.
2112
+ // The three tasks keep the budgets they had: distributeBudget's normalize share is nominal.
2113
+ const results = await optimizeRun(db, { ...DAILY_OPTIMIZE, tasks: [...DAILY_OPTIMIZE.tasks] });
2112
2114
  const parts = [];
2113
2115
  if (results.reenrich?.processed) parts.push(`re-enriched: ${results.reenrich.processed}`);
2114
- if (results.normalize?.processed) parts.push(`normalized: ${results.normalize.processed}`);
2115
2116
  if (results.clusterMerge?.merged) parts.push(`merged: ${results.clusterMerge.merged}`);
2116
2117
  if (results.smartCompress?.compressed) parts.push(`compressed: ${results.smartCompress.compressed}`);
2117
2118
  if (parts.length > 0) debugLog('DEBUG', 'llm-optimize', parts.join(', '));