claude-mem-lite 4.0.4 → 5.1.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.
Files changed (64) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/README.md +18 -72
  4. package/README.zh-CN.md +24 -74
  5. package/adopt-content.mjs +0 -1
  6. package/cli/common.mjs +0 -17
  7. package/cli.mjs +19 -3
  8. package/commands/mem.md +3 -3
  9. package/commands/update.md +5 -12
  10. package/format-utils.mjs +0 -21
  11. package/haiku-client.mjs +36 -5
  12. package/hash-utils.mjs +21 -0
  13. package/hook-episode.mjs +7 -1
  14. package/hook-optimize.mjs +103 -50
  15. package/hook-shared.mjs +8 -2
  16. package/hook-update.mjs +56 -14
  17. package/hook.mjs +21 -29
  18. package/hooks/hooks.json +0 -10
  19. package/install.mjs +100 -678
  20. package/lib/atomic-write.mjs +16 -0
  21. package/lib/deferred-work.mjs +7 -3
  22. package/lib/doctor-drift.mjs +0 -1
  23. package/lib/frontmatter.mjs +1 -2
  24. package/lib/get-core.mjs +27 -1
  25. package/lib/hook-prune.mjs +133 -0
  26. package/lib/hook-stdin.mjs +1 -1
  27. package/lib/hook-telemetry.mjs +1 -1
  28. package/lib/install-shape.mjs +45 -4
  29. package/lib/maintain-core.mjs +9 -0
  30. package/lib/metrics.mjs +2 -2
  31. package/lib/proc-lock.mjs +111 -13
  32. package/lib/proxy-fetch.mjs +26 -2
  33. package/lib/scrub-record.mjs +8 -0
  34. package/lib/shard-gc.mjs +11 -7
  35. package/lib/tmp-fixture-sweep.mjs +9 -0
  36. package/mem-cli.mjs +46 -434
  37. package/nlp.mjs +1 -1
  38. package/npm-shrinkwrap.json +6 -2
  39. package/package.json +9 -16
  40. package/project-utils.mjs +43 -15
  41. package/schema.mjs +112 -50
  42. package/scripts/hook-launcher.mjs +9 -3
  43. package/scripts/prompt-search-utils.mjs +0 -30
  44. package/scripts/setup.sh +5 -0
  45. package/scripts/user-prompt-search.js +3 -125
  46. package/secret-scrub.mjs +32 -3
  47. package/server.mjs +19 -451
  48. package/source-files.mjs +12 -29
  49. package/synonyms.mjs +2 -1
  50. package/tool-schemas.mjs +23 -91
  51. package/utils.mjs +5 -13
  52. package/commands/tools.md +0 -67
  53. package/install-metadata.mjs +0 -2193
  54. package/lib/registry-core.mjs +0 -264
  55. package/registry/preinstalled.json +0 -2419
  56. package/registry-enricher.mjs +0 -124
  57. package/registry-github.mjs +0 -86
  58. package/registry-importer.mjs +0 -569
  59. package/registry-recommend.mjs +0 -503
  60. package/registry-retriever.mjs +0 -611
  61. package/registry-scanner.mjs +0 -261
  62. package/registry.mjs +0 -665
  63. package/resource-discovery.mjs +0 -199
  64. package/scripts/pre-skill-bridge.js +0 -146
@@ -17,6 +17,8 @@ import {
17
17
  lstatSync,
18
18
  realpathSync,
19
19
  unlinkSync,
20
+ statSync,
21
+ chmodSync,
20
22
  } from 'node:fs';
21
23
  import { dirname } from 'node:path';
22
24
 
@@ -64,9 +66,23 @@ export function atomicWriteFileSync(filePath, data, { backup = false } = {}) {
64
66
  // (CLAUDE.md), where the residue shows up in their `git status` and nothing sweeps it:
65
67
  // the orphan sweep only walks RUNTIME_DIR. Cleaning here covers every caller rather than
66
68
  // asking each one to remember, which is the mistake the private twins made.
69
+ // R10 P2-8: carry the target's permission bits across the rename. A temp file is created
70
+ // with the default 0666 & ~umask, so tmp+rename silently WIDENS a private file — measured
71
+ // 0600 -> 0644. The targets here are ~/.claude.json (0600 as Claude Code writes it,
72
+ // holding mcpServers[*].env and OAuth account data) and ~/.claude/settings.json, both
73
+ // rewritten on every install / uninstall / self-update prune. chmod the temp BEFORE the
74
+ // rename, not writeFileSync's `mode` option, which is masked by umask.
75
+ let mode = null;
76
+ try {
77
+ mode = statSync(target).mode & 0o7777;
78
+ } catch {
79
+ /* new file — let umask decide, as before */
80
+ }
81
+
67
82
  const tmp = `${target}.tmp-${process.pid}`;
68
83
  try {
69
84
  writeFileSync(tmp, data);
85
+ if (mode !== null) chmodSync(tmp, mode);
70
86
  renameSync(tmp, target);
71
87
  } catch (e) {
72
88
  try {
@@ -1,4 +1,5 @@
1
1
  import { DAY_MS } from './time-constants.mjs';
2
+ import { scrubRecord } from './scrub-record.mjs';
2
3
  // claude-mem-lite — deferred_work data layer
3
4
  // Pure-data CRUD + ordinal resolver + transactional closure helper.
4
5
  // Decoupled from observations table: different lifecycle, different scoring.
@@ -31,6 +32,9 @@ export function insertDeferred(db, args) {
31
32
  if (!project || typeof project !== 'string') throw new Error('project required');
32
33
  if (!title || typeof title !== 'string') throw new Error('title required');
33
34
  if (![1, 2, 3].includes(priority)) throw new Error('priority must be 1, 2, or 3');
35
+ // R10 P1-5: scrub at the single choke point, after validation so an all-secret title
36
+ // still fails the required-string check on the caller's own input rather than on '***'.
37
+ const clean = scrubRecord('deferred_work', { title, detail });
34
38
  const stmt = db.prepare(`
35
39
  INSERT INTO deferred_work
36
40
  (project, title, detail, priority, status, created_at_epoch,
@@ -39,8 +43,8 @@ export function insertDeferred(db, args) {
39
43
  `);
40
44
  const r = stmt.run(
41
45
  project,
42
- title,
43
- detail,
46
+ clean.title,
47
+ clean.detail,
44
48
  priority,
45
49
  Date.now(),
46
50
  source_session_id,
@@ -139,7 +143,7 @@ export function dropDeferred(db, id, reason) {
139
143
  WHERE id=? AND status='open'
140
144
  `,
141
145
  )
142
- .run(Date.now(), reason.trim(), id);
146
+ .run(Date.now(), scrubRecord('deferred_work', { drop_reason: reason.trim() }).drop_reason, id);
143
147
  return { changed: r.changes };
144
148
  }
145
149
 
@@ -103,7 +103,6 @@ export const HOOK_SCRIPT_ENTRY_POINTS = new Set([
103
103
  'user-prompt-search.js',
104
104
  'pre-tool-recall.js',
105
105
  'post-tool-recall.js',
106
- 'pre-skill-bridge.js',
107
106
  'pre-agent-inject.sh',
108
107
  'hook-launcher.mjs',
109
108
  ]);
@@ -2,7 +2,6 @@
2
2
  //
3
3
  // Audit 2026-09-02 P1-16. There were three, and they were not all the same:
4
4
  //
5
- // registry-importer.mjs the shipped one, full
6
5
  // scripts/index-managed.mjs byte-identical to it apart from the `export` keyword —
7
6
  // 30 lines, the largest duplicate block in the tree
8
7
  // scripts/convert-commands.mjs a SIMPLIFIED cut with no `|` / `>` block support and no
@@ -13,7 +12,7 @@
13
12
  // scripts reading the same files with different parsers produce different registry rows
14
13
  // depending on which one last ran.
15
14
  //
16
- // Zero dependencies on purpose — it is imported by a shipped module (registry-importer)
15
+ // Zero dependencies on purpose — it is imported by a shipped module
17
16
  // and by two dev scripts, so it must not drag anything into either.
18
17
 
19
18
  /**
package/lib/get-core.mjs CHANGED
@@ -7,6 +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
11
 
11
12
  /** Every observation column `get --fields` accepts, in render order. */
12
13
  export const OBS_FIELDS = [
@@ -32,6 +33,9 @@ export const OBS_FIELDS = [
32
33
  'branch',
33
34
  'superseded_at',
34
35
  'superseded_by',
36
+ // R10 P2-5: without this column the retraction notice below has nothing to read, and a
37
+ // row that dedup merged or compression folded away renders as an ordinary live one.
38
+ 'compressed_into',
35
39
  'last_accessed_at',
36
40
  ];
37
41
 
@@ -137,7 +141,29 @@ export function fetchEventDetail(db, ids) {
137
141
  * @returns {object[]} full observation rows (SELECT *), created order
138
142
  */
139
143
  export function supersededNotice(row) {
140
- if (!row || !row.superseded_at) return null;
144
+ if (!row) return null;
145
+ if (!row.superseded_at) {
146
+ // R10 P2-5: `superseded_at` is only ONE of the two ways a row stops being live.
147
+ // liveObsFilterSql is `COALESCE(compressed_into,0) = 0 AND superseded_at IS NULL`, and
148
+ // every list surface applies both — so a merged or compressed row is reachable only by
149
+ // naming its id, which is exactly what a stale citation in a transcript does. `get`
150
+ // rendered it with no marker at all, bumped its access_count, fed auto-boost, and let
151
+ // mem_update write to it. Three states, three different instructions to the reader.
152
+ const c = row.compressed_into;
153
+ if (typeof c === 'number' && c > 0) {
154
+ return `⚠ MERGED — folded into #${c}. Read #${c} instead; the fields below are the absorbed copy.`;
155
+ }
156
+ if (c === COMPRESSED_AUTO) {
157
+ return '⚠ COMPRESSED — auto-hidden as low-value or summarized into a digest. Hidden from every search and list.';
158
+ }
159
+ if (c === COMPRESSED_PENDING_PURGE) {
160
+ return '⚠ PENDING PURGE — queued for permanent deletion by the next purge_stale run. Hidden from every search and list.';
161
+ }
162
+ if (typeof c === 'number' && c < 0) {
163
+ return '⚠ HIDDEN — this row is not live. Hidden from every search and list.';
164
+ }
165
+ return null;
166
+ }
141
167
  // Every LIST surface (search / recent / timeline / browse / injection) filters
142
168
  // superseded rows out, so the only way to reach one is to name its id — which is
143
169
  // exactly what a stale citation in a transcript, a note, or an old handoff does.
@@ -0,0 +1,133 @@
1
+ // lib/hook-prune.mjs — settings.json hook-entry classification and reconciliation.
2
+ //
3
+ // Extracted here because TWO faces need it and a direct import would close a cycle:
4
+ // `install.mjs` owns hook registration, and `hook-update.mjs` (which install.mjs
5
+ // already imports) has to reconcile after a swap. Per CLAUDE.md's rule — logic two
6
+ // faces share moves to `lib/`, the big file keeps only wiring — rather than
7
+ // documenting `install.mjs -> hook-update.mjs -> install.mjs` as an allowed cycle.
8
+ //
9
+ // Zero local imports on purpose: `hook-update.mjs` reaches this from the auto-update
10
+ // path, which must stay cheap and must not drag install.mjs's dependency graph in.
11
+
12
+ import { existsSync } from 'node:fs';
13
+ import { join, dirname } from 'node:path';
14
+
15
+ /**
16
+ * Identify a settings hook as one of OURS (to replace on install / strip on uninstall).
17
+ *
18
+ * Must be tight: the old `hook.mjs` + event-word test matched a user's OWN generic hook
19
+ * (`node ~/.config/hook.mjs session-start`) and install/uninstall silently deleted it.
20
+ * The launcher marker (`hook-launcher.mjs`, which every Node hook routes through since
21
+ * v2.84) replaces that clause; the product-name substring (our install-dir / legacy-direct
22
+ * hooks) and the bash prefilters round out the real markers.
23
+ */
24
+ export function isMemHook(cfg) {
25
+ // `cfg?.hooks`, not `cfg.hooks`: a null entry in a hand-edited settings.json threw a
26
+ // TypeError here, and hook-update.mjs swallows it — so ONE malformed entry silently
27
+ // cancelled the whole reconcile and the dangling hook survived. Degraded, never
28
+ // destructive, but the reconcile is the point. (Found by adversarial review; the same
29
+ // exposure predates this module in `configureHooks`, which now shares this function.)
30
+ if (!cfg?.hooks) return false;
31
+ return cfg.hooks.some((h) => {
32
+ const cmd = h.command || '';
33
+ return (
34
+ cmd.includes('claude-mem-lite') ||
35
+ cmd.includes('hook-launcher.mjs') ||
36
+ cmd.includes('scripts/post-tool-use.sh') ||
37
+ // Same reason post-tool-use.sh is named here: a bash prefilter routes through
38
+ // NO launcher, and the product-name clause only fires when the install dir
39
+ // happens to contain it — which CLAUDE_MEM_DIR can relocate. Without this line
40
+ // an Agent|Task hook in a relocated install survives uninstall and duplicates
41
+ // on reinstall (audit 2026-08-22 P2-5 added the second prefilter).
42
+ cmd.includes('scripts/pre-agent-inject.sh')
43
+ );
44
+ });
45
+ }
46
+
47
+ /**
48
+ * The launcher's ENTRY argument, resolved — or null when this command is not a
49
+ * launcher invocation we own.
50
+ *
51
+ * `nodeHook` writes `node "<INSTALL_DIR>/scripts/hook-launcher.mjs" scripts/<entry>.js`,
52
+ * and the launcher resolves that bare relative token against its own INSTALL_DIR
53
+ * (`hook-launcher.mjs:125`). Both halves matter for orphan detection: the quoted
54
+ * launcher path exists on a healthy install, so a scanner that stops at the first
55
+ * quoted token declares the entry healthy without ever looking at the file that
56
+ * actually runs. That is precisely how the `Skill` hook removal went unseen.
57
+ *
58
+ * Plugin-channel commands are excluded: `${CLAUDE_PLUGIN_ROOT}` is expanded by Claude
59
+ * Code, not by us, so nothing here can resolve them — and hooks/hooks.json owns them.
60
+ */
61
+ export function launcherEntryPath(cmd, installDir) {
62
+ if (typeof cmd !== 'string' || cmd.includes('${CLAUDE_PLUGIN_ROOT}')) return null;
63
+ if (!cmd.includes('hook-launcher.mjs')) return null;
64
+ // Everything after the quoted launcher path; the entry is its first bare token.
65
+ const tail = cmd.split(/hook-launcher\.mjs"?\s*/)[1];
66
+ if (!tail) return null;
67
+ const entry = tail.split(/\s+/).find((t) => t && !t.startsWith('-'));
68
+ if (!entry) return null;
69
+ if (entry.startsWith('/')) return entry;
70
+ // Resolve against the base the LAUNCHER NAMED IN THIS COMMAND would use
71
+ // (`hook-launcher.mjs:36` — `join(dirname(launcherFile), '..')`), not against the
72
+ // caller's idea of the install dir. The two coincide in production today because both
73
+ // INSTALL_DIR constants are homedir-hardcoded — which is exactly why the coupling is
74
+ // worth removing rather than documenting: the day a caller passes a different
75
+ // targetDir, resolving against it deletes hooks that run fine. `installDir` stays the
76
+ // fallback for a command whose launcher path we could not parse.
77
+ const quoted = cmd.match(/"([^"]*hook-launcher\.mjs)"/);
78
+ const base = quoted ? dirname(dirname(quoted[1])) : installDir;
79
+ return join(base, entry);
80
+ }
81
+
82
+ /**
83
+ * Drop settings.json hook entries whose launcher target no longer exists.
84
+ *
85
+ * Auto-update only ever handled hook ADDITIONS. `configureHooks()` strips stale mem
86
+ * entries and rewrites every event, but its only caller is `install()`;
87
+ * `hook-update.mjs` swaps `scripts/` wholesale, so an upgrade that REMOVES a hook
88
+ * script leaves settings.json pointing at a file it just deleted. The launcher then
89
+ * treats each fire as a broken install: two stderr lines and the self-heal marker,
90
+ * for a file that is never coming back. First hit by the `PreToolUse:Skill`
91
+ * removal (R9 review P1-1); plugin-channel installs are unaffected because
92
+ * hooks/hooks.json is replaced wholesale.
93
+ *
94
+ * Narrow by construction — an entry is dropped only when it is ours (`isMemHook`),
95
+ * routes through our launcher, and its resolved target is absent. An entry that
96
+ * cannot be parsed, or whose target exists, is left exactly as found; a foreign
97
+ * tool's dead hook is not ours to remove.
98
+ *
99
+ * Pure: returns a new settings object plus the entry tokens removed, so the caller
100
+ * decides whether to write and can log what changed.
101
+ *
102
+ * @returns {{settings: object, removed: string[]}}
103
+ */
104
+ export function pruneDanglingMemHooks(settings, installDir) {
105
+ if (!settings?.hooks) return { settings, removed: [] };
106
+ const removed = [];
107
+ const hooks = {};
108
+ for (const [event, configs] of Object.entries(settings.hooks)) {
109
+ if (!Array.isArray(configs)) {
110
+ hooks[event] = configs;
111
+ continue;
112
+ }
113
+ const kept = [];
114
+ for (const cfg of configs) {
115
+ if (!isMemHook(cfg)) {
116
+ kept.push(cfg);
117
+ continue;
118
+ }
119
+ const liveHooks = (cfg.hooks || []).filter((h) => {
120
+ const target = launcherEntryPath(h.command, installDir);
121
+ if (!target || existsSync(target)) return true;
122
+ // Record the token as written, not the resolved path — that is what a
123
+ // reader has to match against settings.json and the release manifest.
124
+ const tail = String(h.command).split(/hook-launcher\.mjs"?\s*/)[1] || '';
125
+ removed.push(tail.split(/\s+/).find((t) => t && !t.startsWith('-')) || target);
126
+ return false;
127
+ });
128
+ if (liveHooks.length > 0) kept.push({ ...cfg, hooks: liveHooks });
129
+ }
130
+ hooks[event] = kept;
131
+ }
132
+ return { settings: { ...settings, hooks }, removed };
133
+ }
@@ -8,7 +8,7 @@
8
8
  // (1.5 s / 262144 / never rejects).
9
9
  //
10
10
  // UNBOUNDED, three copies of `for await (const chunk of process.stdin) input += chunk`:
11
- // pre-tool-recall.js, pre-skill-bridge.js, post-tool-recall.js. No cap and no timeout.
11
+ // pre-tool-recall.js, post-tool-recall.js. No cap and no timeout.
12
12
  // That matters most on `PreToolUse:Write`, whose `tool_input.content` is the ENTIRE file
13
13
  // being written: writing a multi-megabyte file made pre-tool-recall buffer all of it and
14
14
  // `JSON.parse` all of it, to read `file_path`. The only bound was the host's own 3 s
@@ -113,7 +113,7 @@ export function recordHookError(scope, err, runtimeDir, ctx) {
113
113
  pruneOldShards(dir);
114
114
 
115
115
  // Every hook script funnels its failures through here — including the
116
- // STANDALONE ones (scripts/pre-tool-recall.js, scripts/pre-skill-bridge.js)
116
+ // STANDALONE ones (scripts/pre-tool-recall.js, scripts/post-tool-recall.js)
117
117
  // that never import hook.mjs and so never reach its dispatch catch. That gap
118
118
  // is why the 2026-08-13 outage stayed invisible: 78 of that day's 79 entries
119
119
  // were `pre-recall:db-open`, i.e. the ONE path whose errors nothing but this
@@ -30,7 +30,7 @@
30
30
  //
31
31
  // So: enumerate the roots, probe each, and name the one that is broken.
32
32
 
33
- import { existsSync, readdirSync, realpathSync } from 'node:fs';
33
+ import { existsSync, readdirSync, realpathSync, readFileSync } from 'node:fs';
34
34
  import { join } from 'node:path';
35
35
  import { homedir } from 'node:os';
36
36
 
@@ -55,6 +55,36 @@ function cacheBaseFor({ home = homedir(), marketplace = DEFAULT_MARKETPLACE, plu
55
55
  return join(home, '.claude', 'plugins', 'cache', marketplace, plugin);
56
56
  }
57
57
 
58
+ /**
59
+ * The installPath Claude Code recorded for THIS plugin, or null. R10 P2-9.
60
+ *
61
+ * ~/.claude/plugins/installed_plugins.json is Claude Code's own record of which cached
62
+ * version is installed. detectInstallShape used to consult only CLAUDE_PLUGIN_ROOT, which
63
+ * Claude Code sets for hooks and the MCP launcher but NOT for a terminal — so
64
+ * `self-update`, `doctor`, `status` and `rebuild-binding` all fell through to "newest
65
+ * cache directory". Claude Code never prunes old version dirs, so after any rollback or
66
+ * any manual cache copy the newest directory is not the live one, and prunePluginCache
67
+ * then deleted the tree the user's sessions actually load. Best-effort by construction:
68
+ * every failure returns null and the caller keeps the old newest-wins behaviour.
69
+ * @param {string} home
70
+ * @param {string} marketplace
71
+ * @param {string} plugin
72
+ * @returns {string|null}
73
+ */
74
+ function recordedInstallPath(home, marketplace, plugin) {
75
+ try {
76
+ const raw = readFileSync(join(home, '.claude', 'plugins', 'installed_plugins.json'), 'utf8');
77
+ const entries = JSON.parse(raw)?.plugins?.[`${plugin}@${marketplace}`];
78
+ if (!Array.isArray(entries)) return null;
79
+ for (const e of entries) {
80
+ if (e && typeof e.installPath === 'string' && e.installPath) return e.installPath;
81
+ }
82
+ return null;
83
+ } catch {
84
+ return null;
85
+ }
86
+ }
87
+
58
88
  // Leading integer per dot-segment, so a prerelease dir (`3.70.0-rc1`) orders by its
59
89
  // numeric part instead of collapsing to "equal": `Number('0-rc1')` is NaN, and a NaN
60
90
  // difference is falsy, which silently made the comparator return 0 and left ordering
@@ -143,10 +173,21 @@ export function detectInstallShape({
143
173
  // upgrade left every never-started version permanently stale: doctor red forever
144
174
  // about trees nothing loads, and rebuild-binding — which clears the breakage marker
145
175
  // only when EVERY target succeeds — could never clear it, reproducing the
146
- // "launcher re-spawns npm every 6h forever" state from 2026-08-13. Prefer the
147
- // version this process was actually launched from; else the newest.
176
+ // "launcher re-spawns npm every 6h forever" state from 2026-08-13.
177
+ //
178
+ // Three candidates, most-authoritative first (R10 P2-9):
179
+ // 1. CLAUDE_PLUGIN_ROOT — the version dir THIS process was launched from. Only set
180
+ // inside a hook or the MCP launcher, and unbeatable when it is.
181
+ // 2. installed_plugins.json — what Claude Code recorded. This is the one a TERMINAL
182
+ // has: self-update / doctor / status / rebuild-binding all run without the env var,
183
+ // and used to fall straight through to (3).
184
+ // 3. newest cache directory — a guess, and after a rollback the wrong one.
185
+ const recorded = recordedInstallPath(home, marketplace ?? DEFAULT_MARKETPLACE, plugin ?? DEFAULT_PLUGIN);
148
186
  const activePluginVersion =
149
- pluginVersions.find((v) => pluginRoot && resolvesSame(v.root, pluginRoot)) || pluginVersions[0] || null;
187
+ pluginVersions.find((v) => pluginRoot && resolvesSame(v.root, pluginRoot)) ||
188
+ pluginVersions.find((v) => recorded && resolvesSame(v.root, recorded)) ||
189
+ pluginVersions[0] ||
190
+ null;
150
191
 
151
192
  const runtimeRoots = [];
152
193
  const byKey = new Map();
@@ -585,6 +585,12 @@ export function mergeDuplicates(db, groups) {
585
585
  // (all of which filter superseded_at) with no recovery path, because recoverOrphanedChildren
586
586
  // only resurfaces children whose keeper row is GONE, and a tombstone still exists.
587
587
  const isLive = db.prepare(`SELECT 1 FROM observations WHERE id = ? AND ${liveObsFilterSql('')}`);
588
+ // R10 P2-7: merge_ids arrives as bare integers with no project constraint, so a group
589
+ // spanning two projects hid project B's row behind project A's keeper. Every read face B
590
+ // has — search, recent, browse, timeline — filters by project, so the row simply
591
+ // disappears from B, and nothing anywhere says where it went. A merge is a claim that
592
+ // two rows are the same memory; two projects' rows are not, whoever typed the ids.
593
+ const projectOf = db.prepare('SELECT project FROM observations WHERE id = ?');
588
594
  const mergeStmt = db.prepare(
589
595
  `UPDATE observations SET compressed_into = ? WHERE id = ? AND ${liveObsFilterSql('')}`,
590
596
  );
@@ -593,6 +599,9 @@ export function mergeDuplicates(db, groups) {
593
599
  const keeper = resolveKeeper(removeId);
594
600
  if (keeper === removeId) continue; // cycle canonical: this row survives
595
601
  if (!isLive.get(keeper)) continue; // keeper not live -> skip, never orphan
602
+ const kp = projectOf.get(keeper);
603
+ const rp = projectOf.get(removeId);
604
+ if (!kp || !rp || kp.project !== rp.project) continue; // cross-project -> never merge
596
605
  merged += mergeStmt.run(keeper, removeId).changes;
597
606
  }
598
607
  return merged;
package/lib/metrics.mjs CHANGED
@@ -83,8 +83,8 @@ export function timed(dbDir, event, fn, extra = {}) {
83
83
  * grows the dir unbounded. 90d keeps a full quarter for aggregate windows while
84
84
  * bounding the dir. Runs regardless of the enable flag so a user who toggles metrics
85
85
  * OFF still gets old shards cleaned. Best-effort, never throws — called from the
86
- * SessionStart GC sweep. The sweep itself is `lib/shard-gc.mjs`, shared with
87
- * registry-recommend's shadow log; this function is the metrics dir resolution.
86
+ * SessionStart GC sweep. The sweep itself is `lib/shard-gc.mjs`; this function is
87
+ * the metrics dir resolution.
88
88
  * @returns {number} shards removed
89
89
  */
90
90
  export function gcOldMetricShards(dbDir, retainDays = 90) {
package/lib/proc-lock.mjs CHANGED
@@ -16,7 +16,7 @@
16
16
  // write). Release unlinks the file. Crash-safe: a crashed holder's lock ages
17
17
  // out via staleMs so the next session reclaims it.
18
18
 
19
- import { writeFileSync, readFileSync, unlinkSync, mkdirSync } from 'node:fs';
19
+ import { writeFileSync, readFileSync, unlinkSync, mkdirSync, renameSync, linkSync } from 'node:fs';
20
20
  import { dirname } from 'node:path';
21
21
 
22
22
  // 5 min: comfortably longer than any install/update write phase (npm install in
@@ -49,12 +49,60 @@ function isStale(lockPath, staleMs, now) {
49
49
  }
50
50
  }
51
51
 
52
- function makeRelease(lockPath) {
52
+ /**
53
+ * Create `lockPath` with `payload`, atomically and never visible EMPTY.
54
+ * @returns {'ok'|'exists'|'error'}
55
+ *
56
+ * R10 P1-7, second defect. `writeFileSync(path, data, { flag: 'wx' })` is two syscalls:
57
+ * O_CREAT|O_EXCL makes the file, the write fills it. A peer that reads in between gets an
58
+ * empty string — and isStale() treats an unparseable lock as stale, by design, so it stole
59
+ * a lock whose owner was mid-create and already believed it held it. Both then held it.
60
+ * Instrumented under load: one stealer's `sampled` was literally "".
61
+ *
62
+ * Fix: fill a private temp first, then link() it into place. link() is atomic and fails
63
+ * EEXIST exactly like O_EXCL, but the name only ever appears with its full contents. The
64
+ * temp carries a `.lock` suffix so hook.mjs's sweeper reclaims it if we die in between.
65
+ * Filesystems without hard links (some network / FAT mounts) fall back to the old form —
66
+ * they keep the narrow window, which is still better than never acquiring a lock at all.
67
+ */
68
+ function createExclusive(lockPath, payload) {
69
+ const tmp = `${lockPath}.new-${process.pid}-${Math.random().toString(36).slice(2)}.lock`;
70
+ try {
71
+ writeFileSync(tmp, payload, { flag: 'wx' });
72
+ } catch {
73
+ return 'error';
74
+ }
75
+ try {
76
+ linkSync(tmp, lockPath);
77
+ return 'ok';
78
+ } catch (e) {
79
+ if (e.code === 'EEXIST') return 'exists';
80
+ // No hard-link support: fall back to the single-call form.
81
+ try {
82
+ writeFileSync(lockPath, payload, { flag: 'wx' });
83
+ return 'ok';
84
+ } catch (e2) {
85
+ return e2.code === 'EEXIST' ? 'exists' : 'error';
86
+ }
87
+ } finally {
88
+ try {
89
+ unlinkSync(tmp);
90
+ } catch {
91
+ /* best-effort */
92
+ }
93
+ }
94
+ }
95
+
96
+ function makeRelease(lockPath, payload) {
53
97
  let released = false;
54
98
  return function release() {
55
99
  if (released) return;
56
100
  released = true;
57
101
  try {
102
+ // Only remove OUR OWN lock. A release that unlinks whatever is at the path can hand
103
+ // the lock to a third process when a stale-steal has already replaced the file —
104
+ // the same class of bug as the steal race below, one step later.
105
+ if (readFileSync(lockPath, 'utf8') !== payload) return;
58
106
  unlinkSync(lockPath);
59
107
  } catch {
60
108
  /* already gone — fine */
@@ -76,25 +124,75 @@ export function acquireLock(lockPath, { staleMs = DEFAULT_STALE_MS, now = Date.n
76
124
  } catch {
77
125
  /* best-effort */
78
126
  }
79
- const payload = JSON.stringify({ pid: process.pid, ts: now() });
80
- try {
81
- writeFileSync(lockPath, payload, { flag: 'wx' }); // O_EXCL — atomic create
82
- return makeRelease(lockPath);
83
- } catch (e) {
84
- if (e.code !== 'EEXIST') return null; // permission / fs error → fail closed
127
+ // `nonce` makes the payload identify THIS acquisition, not just this process: two
128
+ // acquires from one pid inside the same millisecond would otherwise be indistinguishable,
129
+ // and release() below compares payloads. isStale() and hook.mjs's lock sweeper read only
130
+ // `pid` and `ts`, so the extra field is inert to both.
131
+ const payload = JSON.stringify({
132
+ pid: process.pid,
133
+ ts: now(),
134
+ nonce: Math.random().toString(36).slice(2),
135
+ });
136
+ const first = createExclusive(lockPath, payload);
137
+ if (first === 'ok') return makeRelease(lockPath, payload);
138
+ if (first === 'error') return null; // permission / fs error → fail closed
139
+ {
140
+ let sampled;
141
+ try {
142
+ sampled = readFileSync(lockPath, 'utf8');
143
+ } catch {
144
+ // The file vanished between EEXIST and this read — a peer released, or a peer is
145
+ // mid-steal. We have no bytes to verify against, so we must NOT enter the steal path:
146
+ // it would move a peer's freshly created lock aside with nothing to compare it to,
147
+ // which is the double-acquire this whole protocol exists to prevent. Just retry the
148
+ // plain exclusive create once and take the answer.
149
+ return createExclusive(lockPath, payload) === 'ok' ? makeRelease(lockPath, payload) : null;
150
+ }
85
151
  if (!isStale(lockPath, staleMs, now)) return null; // live peer holds it
86
- // Stale: steal it. unlink + re-create exclusively; lose the race → null.
152
+ // ── Stealing a stale lock (R10 P1-7) ──────────────────────────────────────────────
153
+ // This used to be unlink-then-create, and the comment "lose the race → null" was only
154
+ // true of the create. The losing interleave: A unlinks, A creates, B unlinks A's BRAND
155
+ // NEW lock, B creates. Both hold it, and both then run installExtractedRelease's rename
156
+ // loop over the same tree — the torn install this module exists to prevent. Measured on
157
+ // the old code with two worker threads leaving an Atomics barrier together: 35 double
158
+ // acquisitions in 200 rounds.
159
+ //
160
+ // Protocol now: rename the stale file to a private tombstone. rename is atomic and
161
+ // single-winner, so a second stealer gets ENOENT and stands down. Then verify the
162
+ // tombstone still holds the bytes we judged stale — if it does not, a peer stole and
163
+ // re-created between our check and our rename, and what we just moved aside is THEIR
164
+ // live lock. Put it back with link() (which refuses to clobber) and stand down.
165
+ const tombstone = `${lockPath}.steal-${process.pid}-${Math.random().toString(36).slice(2)}.lock`;
87
166
  try {
88
- unlinkSync(lockPath);
167
+ renameSync(lockPath, tombstone);
89
168
  } catch {
90
- /* raced */
169
+ return null; // another stealer won the rename; it owns the re-create
91
170
  }
171
+ let stolen = null;
92
172
  try {
93
- writeFileSync(lockPath, payload, { flag: 'wx' });
94
- return makeRelease(lockPath);
173
+ stolen = readFileSync(tombstone, 'utf8');
95
174
  } catch {
175
+ /* unreadable — treat as ours to discard */
176
+ }
177
+ if (stolen !== null && stolen !== sampled) {
178
+ try {
179
+ linkSync(tombstone, lockPath); // EEXIST → someone already owns the path; leave it
180
+ } catch {
181
+ /* a third party holds it now */
182
+ }
183
+ try {
184
+ unlinkSync(tombstone);
185
+ } catch {
186
+ /* best-effort */
187
+ }
96
188
  return null;
97
189
  }
190
+ try {
191
+ unlinkSync(tombstone);
192
+ } catch {
193
+ /* best-effort — the .lock suffix keeps it sweepable either way */
194
+ }
195
+ return createExclusive(lockPath, payload) === 'ok' ? makeRelease(lockPath, payload) : null;
98
196
  }
99
197
  }
100
198
 
@@ -47,6 +47,30 @@ export function httpConnectProxyFor(targetUrl) {
47
47
  }
48
48
  }
49
49
 
50
+ /**
51
+ * Proxy-Authorization header for a proxy URL that carries credentials, or {} when it does
52
+ * not. R10 P2-14.
53
+ *
54
+ * redactProxyUrl below exists precisely because HTTP(S)_PROXY legitimately carries
55
+ * `user:pass@` — yet neither CONNECT call site ever sent those credentials. Behind an
56
+ * authenticating proxy every CONNECT came back 407, the caller treated it as unreachable
57
+ * and silently fell back to spawning `claude -p` (about 10x slower), and doctor reported
58
+ * "unreachable", which points at the network rather than at the missing header. The same
59
+ * module carries auto-update, so that path degraded too.
60
+ *
61
+ * decodeURIComponent because a password with `@` or `:` MUST be percent-encoded in the URL
62
+ * and the proxy expects the decoded bytes. The header value is a credential: it must never
63
+ * be logged, and redactProxyUrl stays the only thing that produces printable proxy text.
64
+ * @param {URL} p
65
+ * @returns {Record<string,string>}
66
+ */
67
+ function proxyAuthHeader(p) {
68
+ if (!p.username) return {};
69
+ const user = decodeURIComponent(p.username);
70
+ const pass = decodeURIComponent(p.password || '');
71
+ return { 'Proxy-Authorization': `Basic ${Buffer.from(`${user}:${pass}`).toString('base64')}` };
72
+ }
73
+
50
74
  /**
51
75
  * A proxy URL safe to print. HTTP(S)_PROXY legitimately carries `user:pass@`,
52
76
  * and this string reaches doctor's stdout and `doctor --json` — the text users
@@ -101,7 +125,7 @@ export function connectProbeViaProxy(proxy, host, { timeout = 4000, port = 443 }
101
125
  port: Number(p.port) || 80,
102
126
  method: 'CONNECT',
103
127
  path: `${host}:${port}`,
104
- headers: { Host: `${host}:${port}` },
128
+ headers: { Host: `${host}:${port}`, ...proxyAuthHeader(p) },
105
129
  });
106
130
  req.setTimeout(timeout, () => {
107
131
  req.destroy();
@@ -185,7 +209,7 @@ export function onceViaConnectProxy(
185
209
  port: Number(p.port) || 80,
186
210
  method: 'CONNECT',
187
211
  path: `${t.hostname}:${port}`,
188
- headers: { Host: `${t.hostname}:${port}` },
212
+ headers: { Host: `${t.hostname}:${port}`, ...proxyAuthHeader(p) },
189
213
  });
190
214
  connReq.setTimeout(remaining(), () => connReq.destroy(new Error('proxy CONNECT timeout')));
191
215
  connReq.on('error', (e) => finish(reject, e));
@@ -57,6 +57,14 @@ export const TEXT_FIELDS_BY_TABLE = {
57
57
  // decision titles can carry secrets verbatim (LLM output).
58
58
  'key_decisions',
59
59
  ],
60
+ // deferred_work: the `mem_defer` free-text surface. R10 P1-5 — this table was absent,
61
+ // and insertDeferred never called scrubRecord at all, so neither the listed path nor
62
+ // the unknown-table failsafe ever ran. title/detail are written verbatim by the agent
63
+ // ("rotate ghp_… before release", a connection string in detail) and replayed into
64
+ // model context by the SessionStart dashboard, mem_defer_list and mem_get D#N.
65
+ // files — JSON.stringify(array); pre-scrub elements at the call site if ever needed.
66
+ // project / status — identifiers and an enum.
67
+ deferred_work: ['title', 'detail', 'drop_reason'],
60
68
  };
61
69
 
62
70
  /**
package/lib/shard-gc.mjs CHANGED
@@ -1,12 +1,16 @@
1
1
  // lib/shard-gc.mjs — retention sweep for daily JSONL shard directories.
2
2
  //
3
- // Two sinks in this tree append one `YYYY-MM-DD.jsonl` per day with no GC of their
4
- // own — `lib/metrics.mjs` (CLAUDE_MEM_METRICS) and `registry-recommend.mjs` (the
5
- // shadow log) — so a long-lived install grows both dirs unbounded. The sweep was
6
- // written twice, line for line, differing only in how the directory is resolved;
7
- // audit 2026-09-05 P2-3 found it as the largest non-intentional cross-file duplicate.
8
- // That is the twin-drift class CLAUDE.md names: the retention rule (90 days, the
9
- // `YYYY-MM-DD.jsonl` name filter) had two homes and could disagree with itself.
3
+ // `lib/metrics.mjs` (CLAUDE_MEM_METRICS) appends one `YYYY-MM-DD.jsonl` per day with
4
+ // no GC of its own, so a long-lived install grows that dir unbounded.
5
+ //
6
+ // This module exists because there used to be TWO such sinks — the second was
7
+ // `registry-recommend.mjs`'s shadow log — and the sweep was written twice, line for
8
+ // line, differing only in how the directory is resolved; audit 2026-09-05 P2-3 found
9
+ // it as the largest non-intentional cross-file duplicate. That is the twin-drift class
10
+ // CLAUDE.md names: the retention rule (90 days, the `YYYY-MM-DD.jsonl` name filter) had
11
+ // two homes and could disagree with itself. The skill-registry subsystem was removed in
12
+ // 2026-09 (docs/audits/20260906-145304.md), leaving one caller; the extraction stays
13
+ // because the retention rule is worth owning in one place, not because of the caller count.
10
14
  //
11
15
  // Directory resolution stays with each caller — it is the only part that differed.
12
16