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.
package/hook.mjs CHANGED
@@ -153,6 +153,7 @@ import {
153
153
  resolveDefaultMaintainOps,
154
154
  markAutoCompressible,
155
155
  selectFuzzyDedupeIds,
156
+ selectExactDedupeIds,
156
157
  stampDedupSuperseded,
157
158
  hardDeleteCandidateCount,
158
159
  purgeStale,
@@ -178,6 +179,7 @@ import {
178
179
  } from './lib/citation-tracker.mjs';
179
180
  import { resolveEdgeAttribution, readPreRecallFileEdges } from './lib/edge-attribution.mjs';
180
181
  import { extractTailAssistantText, extractStructuredSummary } from './lib/summary-extractor.mjs';
182
+ import { recordTurnReply, TURN_REPLIES_PREFIX } from './lib/turn-replies.mjs';
181
183
  import { searchRelevantMemories, formatMemoryLine, selectImperativeLesson } from './hook-memory.mjs';
182
184
  import { searchInjectableEvents, renderInjectableEvent } from './lib/events-injection.mjs';
183
185
  import { upsFtsQuery } from './lib/ups-query.mjs';
@@ -1256,6 +1258,9 @@ async function readStopHookInput() {
1256
1258
  // collides across parallel sessions for the same project (see docs/bug.txt).
1257
1259
  let ccSessionId = null;
1258
1260
  let transcriptPath = null;
1261
+ // The host's own copy of this turn's final reply (Claude Code's Stop input, optional). Kept as
1262
+ // given, '' included: an empty one means the turn ended without text, not that it is unknown.
1263
+ let lastAssistantMessage = null;
1259
1264
  try {
1260
1265
  const raw = await readStdin();
1261
1266
  const hookData = JSON.parse(raw.text);
@@ -1265,10 +1270,39 @@ async function readStopHookInput() {
1265
1270
  if (typeof hookData?.transcript_path === 'string' && hookData.transcript_path.length > 0) {
1266
1271
  transcriptPath = hookData.transcript_path;
1267
1272
  }
1273
+ if (typeof hookData?.last_assistant_message === 'string') {
1274
+ lastAssistantMessage = hookData.last_assistant_message;
1275
+ }
1268
1276
  } catch {
1269
1277
  /* stdin unavailable — fall back to local session id */
1270
1278
  }
1271
- return { ccSessionId, transcriptPath };
1279
+ return { ccSessionId, transcriptPath, lastAssistantMessage };
1280
+ }
1281
+
1282
+ /**
1283
+ * D#825 — record this turn's final reply under this Stop's memory session and its current prompt
1284
+ * number, for the summary worker (lib/turn-replies.mjs). The host's `last_assistant_message`
1285
+ * when it sends one; from a host that does not, the transcript's last assistant text, the reply
1286
+ * the Stop baseline reads. A turn answering a task notice stores no prompt, so its reply lands
1287
+ * on the prompt before it and replaces that reply: a turn's last reply wins. Never throws.
1288
+ */
1289
+ function recordStopTurnReply(db, { sessionId, transcriptPath, lastAssistantMessage }) {
1290
+ try {
1291
+ const reply =
1292
+ lastAssistantMessage !== null
1293
+ ? lastAssistantMessage
1294
+ : transcriptPath
1295
+ ? extractTailAssistantText(transcriptPath)
1296
+ : null;
1297
+ if (!reply || !reply.trim()) return;
1298
+ const n =
1299
+ db
1300
+ .prepare('SELECT MAX(prompt_number) AS n FROM user_prompts WHERE content_session_id = ?')
1301
+ .get(sessionId)?.n ?? 0;
1302
+ recordTurnReply(RUNTIME_DIR, sessionId, n, reply);
1303
+ } catch (e) {
1304
+ debugCatch(e, 'stop-turn-reply');
1305
+ }
1272
1306
  }
1273
1307
 
1274
1308
  /** Flush whatever is left in the episode buffer, under the PostToolUse lock. */
@@ -1864,7 +1898,7 @@ function trackCitationsAtStop(db, { sessionId, project, ccSessionId, transcriptP
1864
1898
 
1865
1899
  async function handleStop() {
1866
1900
  // Read Claude Code's real session_id from hook stdin for parallel-session scoping.
1867
- const { ccSessionId, transcriptPath } = await readStopHookInput();
1901
+ const { ccSessionId, transcriptPath, lastAssistantMessage } = await readStopHookInput();
1868
1902
 
1869
1903
  // Capture session info BEFORE cleanup. All DB lookups use the mem-internal id
1870
1904
  // (that's what handleUserPrompt wrote into user_prompts / sdk_sessions / observations
@@ -1899,6 +1933,9 @@ async function handleStop() {
1899
1933
  if (!isDbBusyError(e)) throw e;
1900
1934
  debugLog('WARN', 'stop', 'database locked by another process; this turn was not recorded');
1901
1935
  } finally {
1936
+ // A read and a file write, so it records even when the writes above lost the lock (pre-ship
1937
+ // review of abeac529, F2); before the worker below is spawned, which reads these replies.
1938
+ recordStopTurnReply(db, { sessionId, transcriptPath, lastAssistantMessage });
1902
1939
  db.close();
1903
1940
  }
1904
1941
  }
@@ -1979,7 +2016,8 @@ function gcStalePreRecallCooldowns() {
1979
2016
  name.startsWith('.claude-mem-injected-') ||
1980
2017
  name.startsWith('.claude-mem-keyctx-') || // D#123 Key Context marker — same per-session growth, same 24h GC
1981
2018
  name.startsWith(BUGFIX_NUDGE_MARKER_PREFIX) || // §9-B once-per-session nudge claim — same shape
1982
- name.startsWith(RECALL_SHOWN_PREFIX); // injection budget C1/C3 per-thread marker — same shape
2019
+ name.startsWith(RECALL_SHOWN_PREFIX) || // injection budget C1/C3 per-thread marker — same shape
2020
+ name.startsWith(TURN_REPLIES_PREFIX); // D#825 per-session turn replies; its workers read it within minutes
1983
2021
  if (!isCooldown && !isInjectedMarker) continue;
1984
2022
  try {
1985
2023
  const p = join(RUNTIME_DIR, name);
@@ -2173,34 +2211,13 @@ function runSessionStartAutoMaintain(db, project) {
2173
2211
  debugLog('DEBUG', 'auto-maintain', `boosted ${boosted} frequently-accessed observations`);
2174
2212
 
2175
2213
  // Auto-dedup (exact): merge identical-title observations within 1h.
2176
- // Catches rapid duplicate writes (same hook firing twice, race conditions).
2177
- // BOTH join sides must be live (audit 2026-08-14 H-1): without the
2178
- // superseded_at filters (which the fuzzy channel below always had), a row the
2179
- // fuzzy pass had tombstoned could come back as `a` (a.id < b.id) and tombstone
2180
- // the LIVE keeper `b` — both copies gone from every read path. Worse, a user
2181
- // correction saved with supersedes=[#A] (A.superseded_by = B's NUMERIC id) has
2182
- // the same title as A, so the pair (A, B) tombstoned the correction B itself
2183
- // and the string 'auto-dedup' write clobbered numeric supersession chains that
2184
- // citation-tracker decay hand-off and timeline re-anchoring both follow. The
2185
- // UPDATE repeats the guard so a concurrent writer can't re-stamp a chain.
2186
- const dupPairs = db
2187
- .prepare(
2188
- `
2189
- SELECT a.id as keep_id, b.id as remove_id
2190
- FROM observations a
2191
- JOIN observations b ON a.title = b.title AND a.project = b.project
2192
- AND a.id < b.id
2193
- AND ABS(a.created_at_epoch - b.created_at_epoch) < 3600000
2194
- AND ${liveObsFilterSql('a')}
2195
- AND ${liveObsFilterSql('b')}
2196
- LIMIT 20
2197
- `,
2198
- )
2199
- .all();
2200
- if (dupPairs.length > 0) {
2201
- const removeIds = dupPairs.map((p) => p.remove_id);
2214
+ // Catches rapid duplicate writes (same hook firing twice, race conditions). Both join
2215
+ // sides live (audit 2026-08-14 H-1); the keeper is the copy carrying a lesson, then the
2216
+ // older one (converge R28) — lib/maintain-core.mjs selectExactDedupeIds.
2217
+ const removeIds = selectExactDedupeIds(db);
2218
+ if (removeIds.length > 0) {
2202
2219
  stampDedupSuperseded(db, removeIds, 'auto-dedup');
2203
- debugLog('DEBUG', 'auto-maintain', `auto-deduped ${dupPairs.length} near-identical observations`);
2220
+ debugLog('DEBUG', 'auto-maintain', `auto-deduped ${removeIds.length} near-identical observations`);
2204
2221
  }
2205
2222
 
2206
2223
  // Auto-dedup (fuzzy): catches near-identical titles that exact-match
@@ -2222,7 +2239,7 @@ function runSessionStartAutoMaintain(db, project) {
2222
2239
  const recent = db
2223
2240
  .prepare(
2224
2241
  `
2225
- SELECT id, project, title, importance, created_at_epoch, narrative, text
2242
+ SELECT id, project, title, importance, created_at_epoch, narrative, text, lesson_learned
2226
2243
  FROM observations
2227
2244
  WHERE ${liveObsFilterSql('')}
2228
2245
  AND created_at_epoch > ?
@@ -2240,6 +2257,7 @@ function runSessionStartAutoMaintain(db, project) {
2240
2257
  project: r.project,
2241
2258
  title: r.title,
2242
2259
  importance: r.importance,
2260
+ lesson: r.lesson_learned,
2243
2261
  body: (r.narrative && r.narrative.trim()) || (r.text && r.text.trim()) || '',
2244
2262
  }));
2245
2263
  const fuzzyRemoveIds = selectFuzzyDedupeIds(rows, { maxMerges: FUZZY_MAX_MERGES });
package/install.mjs CHANGED
@@ -14,6 +14,7 @@ import {
14
14
  renameSync,
15
15
  symlinkSync,
16
16
  unlinkSync,
17
+ rmdirSync,
17
18
  readdirSync,
18
19
  statSync,
19
20
  lstatSync,
@@ -643,6 +644,26 @@ export function deployCodeTree(IS_DEV) {
643
644
  } else {
644
645
  log('Installing to ~/.claude-mem-lite/...');
645
646
  const scriptsDir = join(DATA_DIR, 'scripts');
647
+ // An earlier `install --dev` left scripts/ and node_modules/ as symlinks into the developer's
648
+ // checkout. existsSync is true for a link, so the hook-script copies below went THROUGH
649
+ // scripts/ — overwriting the checkout's uncommitted edits, with hooks then running out of
650
+ // the checkout (R32 E2E install F2, P0) — and npm writes through a linked node_modules when
651
+ // its `force` option is set (it replaces the link otherwise; R32 review). Drop the links
652
+ // themselves. The per-file source links need nothing: atomicCopyFileSync renames over a
653
+ // link, it does not write through it.
654
+ for (const name of ['scripts', 'node_modules']) {
655
+ const p = join(DATA_DIR, name);
656
+ let isLink = false;
657
+ try {
658
+ isLink = lstatSync(p).isSymbolicLink();
659
+ } catch {
660
+ /* absent */
661
+ }
662
+ if (isLink) {
663
+ unlinkSync(p);
664
+ log(`Removed the dev-mode ${name}/ link into the checkout; installing a real copy`);
665
+ }
666
+ }
646
667
  if (!existsSync(scriptsDir)) mkdirSync(scriptsDir, { recursive: true });
647
668
  for (const f of SOURCE_FILES) {
648
669
  const src = join(PROJECT_DIR, f);
@@ -1949,6 +1970,7 @@ async function uninstall() {
1949
1970
  }
1950
1971
 
1951
1972
  // 6. Purge data if requested
1973
+ let purgeIncomplete = false; // reported, and not followed by "Done!" and exit 0
1952
1974
  if (flags.has('--purge')) {
1953
1975
  const homeDir = join(homedir(), '.claude-mem-lite');
1954
1976
  // Always remove the homedir code/install dir (guarded to the canonical path).
@@ -1958,12 +1980,74 @@ async function uninstall() {
1958
1980
  } else if (existsSync(DATA_DIR)) {
1959
1981
  fail('DATA_DIR path mismatch, refusing to purge for safety: ' + DATA_DIR);
1960
1982
  }
1961
- // Also remove the relocated data dir — but ONLY if it's genuinely our data dir
1962
- // (contains claude-mem-lite.db), so a mistyped CLAUDE_MEM_DIR is never rm'd.
1983
+ // Also purge the relocated data dir — ONLY if it's genuinely our data dir (contains
1984
+ // claude-mem-lite.db), so a mistyped CLAUDE_MEM_DIR is never touched. Even then only what
1985
+ // claude-mem-lite writes there goes: the directory may be one the user keeps other files in,
1986
+ // and removing it whole took those too (R32 E2E install F1, P0). It is removed only once
1987
+ // nothing else is left in it.
1963
1988
  if (MEM_DATA_DIR !== homeDir) {
1964
- if (existsSync(join(MEM_DATA_DIR, 'claude-mem-lite.db'))) {
1965
- rmSync(MEM_DATA_DIR, { recursive: true, force: true });
1966
- ok(`Relocated data purged (${MEM_DATA_DIR})`);
1989
+ // Not existsSync: it is false on EACCES too, so a store it could not look for was reported
1990
+ // absent, then "Done!" (R32 depth-2 review M1).
1991
+ let storeLookup = null;
1992
+ try {
1993
+ statSync(join(MEM_DATA_DIR, 'claude-mem-lite.db'));
1994
+ } catch (e) {
1995
+ storeLookup = e.code || e.message;
1996
+ }
1997
+ if (storeLookup !== null && storeLookup !== 'ENOENT' && storeLookup !== 'ENOTDIR') {
1998
+ warn(
1999
+ `Could not look for claude-mem-lite.db in ${MEM_DATA_DIR} (${storeLookup}) — nothing there was removed.`,
2000
+ );
2001
+ purgeIncomplete = true;
2002
+ } else if (storeLookup === null) {
2003
+ const r = purgeOwnedDataEntries(MEM_DATA_DIR);
2004
+ // Up to 50 names, then how many more: a bare "…" after 10 hid a user's notes.txt among
2005
+ // 19 kept entries (R33 review L2).
2006
+ const list = (xs) =>
2007
+ `${xs.slice(0, 50).join(', ')}${xs.length > 50 ? ` and ${xs.length - 50} more` : ''}`;
2008
+ if (r.unreadable) {
2009
+ warn(`Could not list ${MEM_DATA_DIR} (${r.unreadable}) — nothing there was removed.`);
2010
+ purgeIncomplete = true;
2011
+ } else {
2012
+ let removedDir = false;
2013
+ if (r.kept.length === 0 && r.failed.length === 0) {
2014
+ try {
2015
+ rmdirSync(MEM_DATA_DIR); // never recursive: something written meanwhile stays
2016
+ removedDir = true;
2017
+ } catch {
2018
+ /* not empty after all, or not removable — reported below */
2019
+ }
2020
+ }
2021
+ if (r.failed.length > 0) {
2022
+ warn(
2023
+ `Could not remove ${r.failed.length} of its own entries in ${MEM_DATA_DIR}: ${list(r.failed)}` +
2024
+ (r.storeKept
2025
+ ? ' — claude-mem-lite.db stays until they go, so running the purge again finishes it'
2026
+ : ''),
2027
+ );
2028
+ purgeIncomplete = true;
2029
+ }
2030
+ if (r.left.length > 0) {
2031
+ warn(
2032
+ `Left ${r.left.join(', ')} in ${MEM_DATA_DIR}: claude-mem-lite's own, but the directory also holds ` +
2033
+ `entries that are not, so a file of yours may be inside. Remove ${r.left.length === 1 ? 'it' : 'them'} yourself once nothing of yours is.`,
2034
+ );
2035
+ purgeIncomplete = true;
2036
+ }
2037
+ if (r.failed.length === 0 && r.left.length === 0) {
2038
+ ok(
2039
+ removedDir
2040
+ ? `Relocated data purged (${MEM_DATA_DIR})`
2041
+ : `Relocated data purged from ${MEM_DATA_DIR}`,
2042
+ );
2043
+ }
2044
+ if (r.kept.length > 0) {
2045
+ warn(
2046
+ `Kept ${r.kept.length} entr${r.kept.length === 1 ? 'y' : 'ies'} there that it does not recognise as claude-mem-lite's: ` +
2047
+ `${list(r.kept)}. Remove them yourself if intended.`,
2048
+ );
2049
+ }
2050
+ }
1967
2051
  } else if (existsSync(MEM_DATA_DIR)) {
1968
2052
  warn(
1969
2053
  `CLAUDE_MEM_DIR (${MEM_DATA_DIR}) has no claude-mem-lite.db — left untouched. Remove manually if intended.`,
@@ -2003,9 +2087,154 @@ async function uninstall() {
2003
2087
  process.exitCode = EXIT_MCP_UNREGISTERED;
2004
2088
  return;
2005
2089
  }
2090
+ if (purgeIncomplete) {
2091
+ console.log('\n ✗ Uninstalled, but the purge is incomplete: see the lines above.\n');
2092
+ process.exitCode = 1;
2093
+ return;
2094
+ }
2006
2095
  console.log('\n Done!\n');
2007
2096
  }
2008
2097
 
2098
+ // What claude-mem-lite writes at the top of a relocated data dir, in the exact formats its writers
2099
+ // use, so a user file that merely starts with the same letters is never taken for one (R32
2100
+ // reviews). STORE is the store and its pre-rename name; ISO is toISOString() with [:.] → '-'.
2101
+ const STORE_RE = 'claude-mem(?:-lite)?\\.db';
2102
+ const ISO_RE = '\\d{4}-\\d{2}-\\d{2}T\\d{2}-\\d{2}-\\d{2}-\\d{3}Z';
2103
+ const OWNED_TOP_RE = new RegExp(
2104
+ `^(?:${[
2105
+ `${STORE_RE}(?:-wal|-shm|-journal)?`, // the store; SQLite's own sidecars
2106
+ // snapshots (lib/db-backup.mjs); VACUUM INTO's own journal stays behind if it is killed
2107
+ `${STORE_RE}\\.[a-z][a-z0-9-]*-${ISO_RE}-\\d+-\\d+\\.bak(?:-journal)?`,
2108
+ `${STORE_RE}\\.legacy-backup-\\d+(?:-wal|-shm)?`, // legacy migration backup (here, scripts/setup.sh)
2109
+ `${STORE_RE}-(?:wal|shm)\\.legacy-backup-\\d+`, // its log under the pre-fix name
2110
+ // a set-aside damaged store (lib/db-unusable.mjs); bare `.corrupt` is the name an earlier
2111
+ // release's remedy told the user to `mv` it to
2112
+ `${STORE_RE}\\.corrupt(?:-${ISO_RE}(?:-\\d+)?(?:-wal|-shm)?)?`,
2113
+ 'resource-registry\\.db(?:-wal|-shm|-journal)?', // the skill registry releases before 5.0.0 kept
2114
+ 'plugin_claude_mem_lite\\.md(?:\\.tmp-\\d+)?', // steering detail doc (lib/local-steering.mjs)
2115
+ '\\.legacy-backup-(?:copy|setup)-\\d+-\\d+(?:-wal|-shm)?', // backup copy temps (here, setup.sh)
2116
+ '\\.snapshot-check-\\d+-[A-Za-z0-9]{6}', // lib/db-unusable.mjs CHECK_DIR_RE
2117
+ '\\.update-(?:staging|backup)-(?:s\\d+-)?\\d+-\\d+', // update residue (hook-update.mjs)
2118
+ ].join('|')})$`,
2119
+ );
2120
+ // Directories it creates there, and the only files it writes in each: daily shards
2121
+ // (lib/metrics.mjs, lib/err-sampler.mjs) and verify-apply's backups (lib/verify-apply-core.mjs).
2122
+ // A file of any other name in them is the user's.
2123
+ const OWNED_DIR_FILES = Object.freeze({
2124
+ metrics: /^\d{4}-\d{2}-\d{2}\.jsonl$/,
2125
+ errors: /^\d{4}-\d{2}-\d{2}\.jsonl$/,
2126
+ // verify-<ISO>-<6 hex>.json, the only name it has ever written there (v6.12.0 on); anything
2127
+ // looser took a user's backups/verify-settings.json (R32 depth-2 review M2).
2128
+ backups: new RegExp(`^verify-${ISO_RE}-[0-9a-f]{6}\\.json(?:\\.tmp-\\d+)?$`),
2129
+ });
2130
+
2131
+ // Its directories whose files it cannot tell from a user's by name: `runtime/`, the working
2132
+ // directory every hook writes dozens of kinds of state into (lib/resolve-data-dir.mjs's default),
2133
+ // and `managed/`, the resource clones releases before 5.0.0 installed. Each goes whole, and only from a
2134
+ // directory that holds nothing that is not claude-mem-lite's; beside a user's files it stays and
2135
+ // is named (R33 review H1, H2: runtime/my-notes.txt went with it).
2136
+ const OWNED_WHOLE_DIRS = new Set(['runtime', 'managed']);
2137
+ // The store goes last, and only once nothing of its own failed: it is how a re-run knows the
2138
+ // directory is claude-mem-lite's (R33 review M2: removed first, the re-run the failure message
2139
+ // asks for found no store, left the rest and said Done!).
2140
+ const LIVE_STORE_RE = /^claude-mem-lite\.db(?:-wal|-shm|-journal)?$/;
2141
+
2142
+ /**
2143
+ * Remove what claude-mem-lite wrote into a relocated data dir, and nothing else (R32 E2E
2144
+ * install F1 and its review). A name it does not recognise stays and is reported: a leftover
2145
+ * can be removed later, a deleted user file cannot be recovered. Never throws.
2146
+ * @param {string} dir
2147
+ * @returns {{unreadable?: string, kept: string[], failed: string[], left: string[], storeKept?: boolean}}
2148
+ * `kept` = entries left because they are not its own (paths relative to `dir`); `failed` = its
2149
+ * own entries it could not remove, with the reason; `left` = its own whole directories left
2150
+ * because `kept` is not empty; `storeKept` = the store stayed because something failed
2151
+ */
2152
+ function purgeOwnedDataEntries(dir) {
2153
+ const kept = [];
2154
+ const failed = [];
2155
+ const whole = [];
2156
+ const store = [];
2157
+ const remove = (rel) => {
2158
+ try {
2159
+ rmSync(join(dir, rel), { recursive: true, force: true });
2160
+ } catch (e) {
2161
+ failed.push(`${rel} (${e.code || e.message})`);
2162
+ }
2163
+ };
2164
+ let names;
2165
+ try {
2166
+ names = readdirSync(dir);
2167
+ } catch (e) {
2168
+ return { unreadable: e.code || e.message, kept, failed, left: [] };
2169
+ }
2170
+ for (const name of names) {
2171
+ if (LIVE_STORE_RE.test(name)) {
2172
+ store.push(name);
2173
+ continue;
2174
+ }
2175
+ if (OWNED_WHOLE_DIRS.has(name)) {
2176
+ whole.push(name);
2177
+ continue;
2178
+ }
2179
+ if (OWNED_TOP_RE.test(name)) {
2180
+ remove(name);
2181
+ continue;
2182
+ }
2183
+ const inner = OWNED_DIR_FILES[name];
2184
+ let isDir = false;
2185
+ try {
2186
+ isDir = inner !== undefined && lstatSync(join(dir, name)).isDirectory();
2187
+ } catch {
2188
+ /* raced away */
2189
+ }
2190
+ if (!isDir) {
2191
+ kept.push(name); // a symlink under one of its names is listed, never followed
2192
+ continue;
2193
+ }
2194
+ let children;
2195
+ try {
2196
+ children = readdirSync(join(dir, name));
2197
+ } catch (e) {
2198
+ failed.push(`${name} (${e.code || e.message})`);
2199
+ continue;
2200
+ }
2201
+ for (const c of children) {
2202
+ if (!inner.test(c)) {
2203
+ kept.push(join(name, c));
2204
+ continue;
2205
+ }
2206
+ // A file only: a directory carrying a shard's name is not one of its files. When unlink
2207
+ // fails, what is there decides, not the code: a directory is EISDIR on Linux but EPERM on
2208
+ // macOS, EPERM on Linux is a file it may not remove, and nothing there means a live session
2209
+ // pruned the shard meanwhile, which is what was wanted (R32 depth-2 review L3, L4).
2210
+ const path = join(dir, name, c);
2211
+ try {
2212
+ unlinkSync(path);
2213
+ } catch (e) {
2214
+ let isDir = false;
2215
+ let gone = false;
2216
+ try {
2217
+ isDir = lstatSync(path).isDirectory();
2218
+ } catch (le) {
2219
+ gone = le.code === 'ENOENT';
2220
+ }
2221
+ if (isDir) kept.push(join(name, c));
2222
+ else if (!gone) failed.push(`${join(name, c)} (${e.code || e.message})`);
2223
+ }
2224
+ }
2225
+ try {
2226
+ rmdirSync(join(dir, name));
2227
+ } catch {
2228
+ /* not empty: what is left is listed above */
2229
+ }
2230
+ }
2231
+ const left = kept.length === 0 ? [] : whole.sort();
2232
+ if (left.length === 0) for (const name of whole) remove(name);
2233
+ const storeKept = failed.length > 0 && store.length > 0;
2234
+ if (!storeKept) for (const name of store) remove(name);
2235
+ return { kept: kept.sort(), failed: failed.sort(), left, storeKept };
2236
+ }
2237
+
2009
2238
  /**
2010
2239
  * Split what a non-purge uninstall leaves behind into the two halves a user cares about:
2011
2240
  * the memories, and everything else.
@@ -3685,20 +3914,19 @@ async function doctor() {
3685
3914
  }
3686
3915
  }
3687
3916
 
3688
- // Protections the operator has switched off. `doctor` is the channel because the surface
3689
- // that would otherwise carry it cannot: the daily normalize runs in a worker spawned by
3690
- // hook-shared.mjs::spawnBackground with `stdio: 'ignore'`, so its `console.error` warning
3691
- // reaches /dev/null. That warning is still correct for the foreground CLI path; this is
3692
- // the unattended one. Same shape as the CLAUDE_MEM_SKIP_SIG_VERIFY notice.
3917
+ // Protections the operator has switched off, said where the user looks. The flag reaches a
3918
+ // normalize run without --project from the CLI, which warns in the user's own terminal, or
3919
+ // from `mem_optimize`, whose warning lands in the host's MCP log; the daily pass runs no
3920
+ // normalize since D20 (2026-11-11). Same shape as the CLAUDE_MEM_SKIP_SIG_VERIFY notice.
3693
3921
  // dwarn, not fail: the flag is set deliberately, so it must be VISIBLE without pushing
3694
3922
  // doctor to exit 1 — a diagnostic that fails on a supported configuration stops being run.
3695
3923
  // `=== '1'` mirrors executeNormalize exactly; warning on `true` would describe a machine
3696
3924
  // that is in fact still fanning out.
3697
3925
  if (String(process.env.CLAUDE_MEM_NORMALIZE_CROSS_PROJECT || '') === '1') {
3698
3926
  dwarn(
3699
- 'CLAUDE_MEM_NORMALIZE_CROSS_PROJECT=1: the daily normalize runs over every project at ' +
3700
- "once, so one project's stored content can steer the synonym groups applied to all of " +
3701
- 'them (R10-P3-21). Unset it for the per-project default.',
3927
+ 'CLAUDE_MEM_NORMALIZE_CROSS_PROJECT=1: a normalize run without --project goes over every ' +
3928
+ "project at once, so one project's stored content can steer the synonym groups applied " +
3929
+ 'to all of them (R10-P3-21). Unset it for the per-project default.',
3702
3930
  );
3703
3931
  }
3704
3932
 
package/lib/activity.mjs CHANGED
@@ -62,6 +62,8 @@ export function saveEvent(
62
62
  git_sha = null,
63
63
  importance = 1,
64
64
  created_at_epoch = Date.now(),
65
+ // D21: the session an episode came from; null for a writer with no session (CLI activity).
66
+ memory_session_id = null,
65
67
  },
66
68
  ) {
67
69
  // Scrub secrets at the single write choke-point so BOTH the auto-capture
@@ -77,8 +79,8 @@ export function saveEvent(
77
79
  const info = db
78
80
  .prepare(
79
81
  `
80
- INSERT INTO events (project, event_type, title, body, file_paths, git_sha, importance, created_at_epoch, search_terms)
81
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
82
+ INSERT INTO events (project, event_type, title, body, file_paths, git_sha, importance, created_at_epoch, search_terms, memory_session_id)
83
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
82
84
  `,
83
85
  )
84
86
  .run(
@@ -92,6 +94,7 @@ export function saveEvent(
92
94
  created_at_epoch,
93
95
  // Derived from the SCRUBBED text, so a redacted secret cannot reach the index this way.
94
96
  eventSearchTerms(safe.title, safe.body),
97
+ memory_session_id,
95
98
  );
96
99
  return Number(info.lastInsertRowid);
97
100
  }
package/lib/db-backup.mjs CHANGED
@@ -7,7 +7,7 @@
7
7
  //
8
8
  // MUST be called OUTSIDE any transaction — VACUUM cannot run inside one, which is
9
9
  // why the maintenance entry points snapshot before opening their db.transaction().
10
- import { existsSync, readdirSync, unlinkSync, statSync } from 'fs';
10
+ import { closeSync, openSync, readdirSync, unlinkSync, statSync } from 'fs';
11
11
  import { dirname, basename, join } from 'path';
12
12
  import { debugLog } from '../utils.mjs';
13
13
 
@@ -149,17 +149,23 @@ export function snapshotDb(db, { tag = 'pre-maintain', retain = 3 } = {}) {
149
149
  const out = `${dbPath}.${tag}-${stamp}.bak`;
150
150
  // Path is internal (db.name from our own config), but escape single quotes
151
151
  // defensively since VACUUM INTO takes a string literal, not a bound param.
152
- const fresh = !existsSync(out);
152
+ // Created empty and owner-only first, then filled: VACUUM INTO writes into an existing empty
153
+ // file and keeps its mode. Left to SQLite the copy took the umask's 0644 while the store is
154
+ // 0600, so under a data directory that already existed as 0755 any local user could read the
155
+ // pre-delete snapshot of what had just been deleted (converge R28, E2E 2026-11-10).
156
+ let created = false;
153
157
  try {
158
+ closeSync(openSync(out, 'wx', 0o600));
159
+ created = true;
154
160
  db.exec(`VACUUM INTO '${out.replace(/'/g, "''")}'`);
155
161
  } catch (e) {
156
162
  // A failed VACUUM INTO can leave its target behind: 0 bytes on a damaged store, newer than
157
163
  // every real snapshot, so a restore command named it (D#551). Only a file it created goes.
158
- if (fresh)
164
+ if (created)
159
165
  try {
160
166
  unlinkSync(out);
161
167
  } catch {
162
- /* never created */
168
+ /* already gone */
163
169
  }
164
170
  throw e;
165
171
  }
@@ -1,4 +1,5 @@
1
1
  import { DAY_MS } from './time-constants.mjs';
2
+ import { localIsoTime } from '../format-utils.mjs';
2
3
  import { isBlankText } from './blank-text.mjs';
3
4
  import { scrubRecord, scrubFilePaths } from './scrub-record.mjs';
4
5
  // claude-mem-lite — deferred_work data layer
@@ -89,8 +90,15 @@ export function listOpenWithOrdinal(db, project, limit = 10) {
89
90
 
90
91
  /**
91
92
  * One open row's ordinal, numbered as listOpenWithOrdinal numbers it; null when the row is not
92
- * open. `defer add` / `mem_defer` read it from a 50-row page before, so an item added past 50
93
- * open printed `(item ?)` (D#123).
93
+ * open.
94
+ *
95
+ * Exported though nothing in this release imports it: v6.32.0's server.mjs and mem-cli.mjs do
96
+ * (their `defer add` / `mem_defer` receipts printed `(item N)`, dropped in D17), and an update
97
+ * swaps this file before them, so for that window (and after a killed swap, until a repair) the
98
+ * previous release's entry points load against it. Removed in D17, every MCP and CLI entry point
99
+ * failed to load at 21-22 of 169 split points from v6.32.0 (scripts/swap-mixed-tree.mjs).
100
+ *
101
+ * @public
94
102
  * @param {Database} db
95
103
  * @param {string} project
96
104
  * @param {number} id
@@ -215,6 +223,29 @@ export function dropDeferred(db, id, reason) {
215
223
  return { changed: r.changes };
216
224
  }
217
225
 
226
+ /**
227
+ * Drop several items in ONE immediate transaction: every token (D#N or ordinal) resolves
228
+ * against one snapshot of the open list, then each row is dropped. Shared by CLI `defer drop`
229
+ * and MCP `mem_defer_drop` (D17). A failure part-way rolls back the drops before it: otherwise
230
+ * the caller sees only the error, retries the same ordinals against a renumbered list and
231
+ * drops the wrong items — the hazard D17 removed (R31 review L3). Throws what
232
+ * resolveDeferredIds or dropDeferred throws.
233
+ * @returns {{dropped: number[], noop: number[]}} row ids; `noop` = no longer open at the UPDATE
234
+ */
235
+ export function dropDeferredMany(db, project, tokens, reason) {
236
+ return db
237
+ .transaction(() => {
238
+ const dropped = [];
239
+ const noop = [];
240
+ for (const id of resolveDeferredIds(db, project, tokens)) {
241
+ if (dropDeferred(db, id, reason).changed === 0) noop.push(id);
242
+ else dropped.push(id);
243
+ }
244
+ return { dropped, noop };
245
+ })
246
+ .immediate();
247
+ }
248
+
218
249
  /**
219
250
  * Reasons that mean "this item was FIXED", for which `defer drop` is the wrong
220
251
  * verb — dropping loses the closed_by_obs_id link that `save --closes-deferred`
@@ -309,7 +340,9 @@ export function formatDeferredDetail(row) {
309
340
  /* legacy non-JSON files value — skip rather than render garbage */
310
341
  }
311
342
  }
312
- if (row.created_at_epoch) lines.push(`created: ${new Date(row.created_at_epoch).toISOString()}`);
343
+ // Local with its offset: the UTC string's date is the next day for an evening item west of
344
+ // Greenwich (D18).
345
+ if (row.created_at_epoch) lines.push(`created: ${localIsoTime(row.created_at_epoch)}`);
313
346
  if (row.status === 'dropped' && row.drop_reason) lines.push(`drop_reason: ${row.drop_reason}`);
314
347
  if (row.status === 'done' && row.closed_by_obs_id) lines.push(`closed_by: #${row.closed_by_obs_id}`);
315
348
  // D#195: a row re-closed out of 'dropped' keeps its drop_reason. Render it under
package/lib/get-core.mjs CHANGED
@@ -7,7 +7,7 @@
7
7
  // keeps its own header/label rendering conventions.
8
8
 
9
9
  import { autoBoostIfNeeded } from '../search-scoring.mjs';
10
- import { COMPRESSED_AUTO, COMPRESSED_PENDING_PURGE } from '../utils.mjs';
10
+ import { COMPRESSED_AUTO, COMPRESSED_PENDING_PURGE, localYmd } from '../utils.mjs';
11
11
 
12
12
  /** Every observation column `get --fields` accepts, in render order. */
13
13
  export const OBS_FIELDS = [
@@ -197,7 +197,7 @@ export function supersededNotice(row) {
197
197
  */
198
198
  export function supersededEventNotice(row) {
199
199
  if (!row || !row.superseded_at_epoch) return null;
200
- const when = new Date(row.superseded_at_epoch).toISOString().slice(0, 10);
200
+ const when = localYmd(row.superseded_at_epoch);
201
201
  return `⚠ SUPERSEDED ${when} — retired by a later correction, or promoted into an observation. Hidden from every search and from the injected lessons; the fields below may be the withdrawn version.`;
202
202
  }
203
203