claude-mem-lite 6.32.0 → 6.33.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/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
 
@@ -121,15 +121,73 @@ function bodiesSimilar(a, b, threshold) {
121
121
  return jaccardSimilarity(ba, bb) >= threshold;
122
122
  }
123
123
 
124
+ // A lesson as the rest of the code reads one: blank and the enrichers' 'none' carry nothing.
125
+ const hasLesson = (v) => typeof v === 'string' && v.trim() !== '' && v.trim().toLowerCase() !== 'none';
126
+
127
+ /**
128
+ * Which of two duplicate rows survives a merge or an auto-dedup: the one carrying a lesson,
129
+ * then the higher importance, then the more accessed, then `a`. Every dedup path keeps a row
130
+ * whole and hides or tombstones the other, nothing carried over, so keeping the bare copy put
131
+ * the lesson out of every search, recall and injection face (converge R28, E2E 2026-11-10:
132
+ * maintain scan's suggested merge, and both hook channels). Shared so the four paths agree.
133
+ * @param {{lesson?: string|null, importance?: number, accessCount?: number}} a
134
+ * @param {{lesson?: string|null, importance?: number, accessCount?: number}} b
135
+ * @returns {object} `a` or `b`
136
+ */
137
+ export function pickDedupKeeper(a, b) {
138
+ const la = hasLesson(a.lesson);
139
+ const lb = hasLesson(b.lesson);
140
+ if (la !== lb) return la ? a : b;
141
+ const ia = a.importance ?? 1;
142
+ const ib = b.importance ?? 1;
143
+ if (ia !== ib) return ia > ib ? a : b;
144
+ return (b.accessCount ?? 0) > (a.accessCount ?? 0) ? b : a;
145
+ }
146
+
147
+ /**
148
+ * The hook's exact auto-dedup channel: rows with the identical title in one project written
149
+ * within an hour of each other (the same hook firing twice, a race). Both sides live (audit
150
+ * 2026-08-14 H-1: a tombstone could otherwise come back as the keeper). The older row wins a
151
+ * tie, as it always did; a row carrying a lesson wins over one that does not.
152
+ * @returns {number[]} ids to stamp superseded
153
+ */
154
+ export function selectExactDedupeIds(db) {
155
+ const pairs = db
156
+ .prepare(
157
+ `
158
+ SELECT a.id AS a_id, a.importance AS a_imp, a.access_count AS a_acc, a.lesson_learned AS a_lesson,
159
+ b.id AS b_id, b.importance AS b_imp, b.access_count AS b_acc, b.lesson_learned AS b_lesson
160
+ FROM observations a
161
+ JOIN observations b ON a.title = b.title AND a.project = b.project
162
+ AND a.id < b.id
163
+ AND ABS(a.created_at_epoch - b.created_at_epoch) < 3600000
164
+ AND ${liveObsFilterSql('a')}
165
+ AND ${liveObsFilterSql('b')}
166
+ ORDER BY a.id, b.id
167
+ LIMIT 20
168
+ `,
169
+ )
170
+ .all();
171
+ // pickDedupKeeper orders rows totally (lesson, importance, access, then the older id), so the
172
+ // best row of a group loses no pair and the rest each lose at least one.
173
+ const removed = new Set();
174
+ for (const p of pairs) {
175
+ const a = { id: p.a_id, importance: p.a_imp, accessCount: p.a_acc, lesson: p.a_lesson };
176
+ const b = { id: p.b_id, importance: p.b_imp, accessCount: p.b_acc, lesson: p.b_lesson };
177
+ removed.add(pickDedupKeeper(a, b) === a ? b.id : a.id);
178
+ }
179
+ return [...removed];
180
+ }
181
+
124
182
  /**
125
183
  * Pick which near-duplicate observation ids to supersede in the hook fuzzy-dedup
126
184
  * pass. Pure (no DB) so it is unit-testable. A pair must clear BOTH the title
127
185
  * thresholds (MinHash prefilter → exact title Jaccard) AND the body Jaccard floor
128
- * before the lower-importance row is marked for superseding (audit #8 — title-only
186
+ * before the losing row (pickDedupKeeper) is marked for superseding (audit #8 — title-only
129
187
  * matching collapsed observations with the same title token-set but different bodies).
130
- * @param {Array<{id:number,project?:string,title:string,body:string,importance:number}>} rows
188
+ * @param {Array<{id:number,project?:string,title:string,body:string,importance:number,lesson?:string|null}>} rows
131
189
  * Candidate rows in scan order (caller decides ordering / recency window).
132
- * @returns {number[]} ids to supersede (lower-importance member of each kept pair).
190
+ * @returns {number[]} ids to supersede (the losing member of each kept pair).
133
191
  */
134
192
  export function selectFuzzyDedupeIds(
135
193
  rows,
@@ -155,8 +213,8 @@ export function selectFuzzyDedupeIds(
155
213
  if (estimateJaccardFromMinHash(minhashes[i], minhashes[j]) < minhashPrefilter) continue;
156
214
  if (jaccardSimilarity(titles[i], titles[j]) < titleThreshold) continue;
157
215
  if (!bodiesSimilar(rows[i].body, rows[j].body, bodyThreshold)) continue;
158
- // Keep the higher-importance row; tiebreak by earlier scan position (kept as i).
159
- const keep = (rows[i].importance ?? 1) >= (rows[j].importance ?? 1) ? rows[i] : rows[j];
216
+ // pickDedupKeeper: the lesson, then importance; a tie keeps the earlier scan position (i).
217
+ const keep = pickDedupKeeper(rows[i], rows[j]);
160
218
  const remove = keep === rows[i] ? rows[j] : rows[i];
161
219
  removeIds.push(remove.id);
162
220
  removed.add(remove.id);
@@ -826,7 +884,12 @@ export function reviewRows(db, ids) {
826
884
 
827
885
  /**
828
886
  * Near-duplicate title detection: MinHash pre-filter → exact Jaccard. Returns
829
- * [{ a:{id,title,importance}, b:{…}, similarity:'0.NN' }, …].
887
+ * [{ a:{id,title,importance,lesson,accessCount}, b:{…}, similarity:'0.NN' }, …]; callers pick
888
+ * the keeper with pickDedupKeeper.
889
+ *
890
+ * Rows a compression wrote (`compress-` writer: weekly summaries, cluster-merge keepers) are not
891
+ * candidates: a weekly summary is titled by its count and dominant type alone, so two different
892
+ * weeks scored 1.00 and the suggested merge hid one week's content (converge R28).
830
893
  */
831
894
  export function findDuplicates(
832
895
  db,
@@ -835,7 +898,7 @@ export function findDuplicates(
835
898
  const recent = db
836
899
  .prepare(
837
900
  `
838
- SELECT id, title, project, importance, access_count, created_at_epoch
901
+ SELECT id, title, project, importance, access_count, lesson_learned, created_at_epoch
839
902
  FROM observations
840
903
  -- liveObsFilterSql, not compressed_into alone (audit 2026-09-02 P0-2): this scan renders a
841
904
  -- ready-to-paste "maintain execute --ops dedup --merge-ids A,B", and the keeper rule is
@@ -843,6 +906,7 @@ export function findDuplicates(
843
906
  -- be nominated keeper. Same predicate the hook-side fuzzy dedup candidate query uses
844
907
  -- (hook.mjs), and the same one mergeDuplicates now enforces at write time.
845
908
  WHERE ${liveObsFilterSql('')} ${projectFilter}
909
+ AND COALESCE(memory_session_id, '') NOT LIKE 'compress-%'
846
910
  ORDER BY created_at_epoch DESC, id DESC LIMIT ${limit}
847
911
  `,
848
912
  )
@@ -861,9 +925,16 @@ export function findDuplicates(
861
925
  if (estimateJaccardFromMinHash(minhashes[i], minhashes[j]) < MINHASH_PRE_THRESHOLD) continue;
862
926
  const sim = jaccardSimilarity(titles[i], titles[j]);
863
927
  if (sim > SIMILARITY_THRESHOLD) {
928
+ const side = (r) => ({
929
+ id: r.id,
930
+ title: r.title,
931
+ importance: r.importance,
932
+ lesson: r.lesson_learned,
933
+ accessCount: r.access_count,
934
+ });
864
935
  duplicates.push({
865
- a: { id: recent[i].id, title: recent[i].title, importance: recent[i].importance },
866
- b: { id: recent[j].id, title: recent[j].title, importance: recent[j].importance },
936
+ a: side(recent[i]),
937
+ b: side(recent[j]),
867
938
  similarity: sim.toFixed(2),
868
939
  });
869
940
  }
@@ -93,7 +93,8 @@ export function computeStatsFeed(
93
93
  const daily = db
94
94
  .prepare(
95
95
  `
96
- SELECT date(created_at) as day, COUNT(*) as c FROM observations
96
+ -- The local day (D18), as every displayed date and the --from/--to filters read one.
97
+ SELECT date(created_at_epoch / 1000, 'unixepoch', 'localtime') as day, COUNT(*) as c FROM observations
97
98
  WHERE created_at_epoch >= ? ${projectFilter}
98
99
  GROUP BY day ORDER BY day DESC LIMIT 7
99
100
  `,