claude-db 0.9.1 → 0.10.1

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 (161) hide show
  1. package/CHANGELOG.md +248 -0
  2. package/README.md +74 -113
  3. package/dist/capture/flush.d.ts +1 -0
  4. package/dist/capture/flush.js +4 -3
  5. package/dist/capture/git.js +2 -1
  6. package/dist/capture/index.d.ts +5 -1
  7. package/dist/capture/index.js +4 -1
  8. package/dist/capture/manual.js +1 -1
  9. package/dist/capture/progress.js +1 -0
  10. package/dist/capture/redact.d.ts +1 -0
  11. package/dist/capture/redact.js +17 -0
  12. package/dist/capture/reingest.d.ts +19 -0
  13. package/dist/capture/reingest.js +56 -0
  14. package/dist/capture/scrub.d.ts +11 -0
  15. package/dist/capture/scrub.js +48 -0
  16. package/dist/capture/summarize.js +3 -9
  17. package/dist/capture/transcript.d.ts +1 -0
  18. package/dist/capture/transcript.js +12 -3
  19. package/dist/capture/turn-extractor.d.ts +0 -1
  20. package/dist/capture/turn-extractor.js +10 -19
  21. package/dist/cli/commands/adoption.js +3 -3
  22. package/dist/cli/commands/distill.d.ts +1 -0
  23. package/dist/cli/commands/distill.js +67 -0
  24. package/dist/cli/commands/doctor.js +11 -3
  25. package/dist/cli/commands/insight.js +13 -4
  26. package/dist/cli/commands/maintain.d.ts +2 -1
  27. package/dist/cli/commands/maintain.js +53 -17
  28. package/dist/cli/commands/pick.d.ts +3 -0
  29. package/dist/cli/commands/pick.js +25 -0
  30. package/dist/cli/commands/setup.js +11 -2
  31. package/dist/cli/commands/sync.js +73 -12
  32. package/dist/cli/commands/transfer.d.ts +0 -5
  33. package/dist/cli/commands/transfer.js +1 -24
  34. package/dist/cli/commands/use.js +1 -1
  35. package/dist/cli/commands/view.js +30 -28
  36. package/dist/cli/index.js +13 -2
  37. package/dist/cli/install.d.ts +2 -0
  38. package/dist/cli/install.js +44 -21
  39. package/dist/cli/instructions.js +4 -2
  40. package/dist/cli/refresh.js +5 -1
  41. package/dist/cli/usage.js +4 -0
  42. package/dist/config/dir.d.ts +1 -0
  43. package/dist/config/dir.js +3 -0
  44. package/dist/config/index.d.ts +1 -1
  45. package/dist/config/index.js +1 -1
  46. package/dist/config/load.d.ts +4 -1
  47. package/dist/config/load.js +30 -13
  48. package/dist/config/schema.d.ts +19 -0
  49. package/dist/config/schema.js +17 -2
  50. package/dist/embed/index.d.ts +1 -1
  51. package/dist/embed/index.js +1 -1
  52. package/dist/embed/local.js +2 -1
  53. package/dist/facts/budget.d.ts +4 -0
  54. package/dist/facts/budget.js +12 -0
  55. package/dist/facts/claude-memory.d.ts +19 -0
  56. package/dist/facts/claude-memory.js +100 -0
  57. package/dist/facts/distill.d.ts +25 -0
  58. package/dist/facts/distill.js +141 -0
  59. package/dist/facts/model.d.ts +28 -0
  60. package/dist/facts/model.js +82 -0
  61. package/dist/facts/notice.d.ts +2 -0
  62. package/dist/facts/notice.js +22 -0
  63. package/dist/facts/ops.d.ts +23 -0
  64. package/dist/facts/ops.js +91 -0
  65. package/dist/facts/render.d.ts +4 -0
  66. package/dist/facts/render.js +13 -0
  67. package/dist/facts/start.d.ts +6 -0
  68. package/dist/facts/start.js +57 -0
  69. package/dist/graph/index.d.ts +2 -6
  70. package/dist/graph/index.js +2 -3
  71. package/dist/graph/languages/basic.js +1 -1
  72. package/dist/graph/languages/index.d.ts +1 -1
  73. package/dist/graph/parser.js +1 -1
  74. package/dist/graph/query/index.d.ts +0 -2
  75. package/dist/graph/query/index.js +0 -1
  76. package/dist/graph/scan/extract.js +1 -1
  77. package/dist/graph/scan/files.js +1 -1
  78. package/dist/graph/scan/index.d.ts +1 -2
  79. package/dist/graph/scan/index.js +1 -1
  80. package/dist/hooks/background.d.ts +3 -0
  81. package/dist/hooks/background.js +43 -0
  82. package/dist/hooks/compact.d.ts +8 -0
  83. package/dist/hooks/compact.js +42 -0
  84. package/dist/hooks/payload.d.ts +3 -0
  85. package/dist/hooks/payload.js +18 -1
  86. package/dist/hooks/pick-deliver.d.ts +2 -0
  87. package/dist/hooks/pick-deliver.js +16 -0
  88. package/dist/hooks/prompt-recall.d.ts +21 -0
  89. package/dist/hooks/prompt-recall.js +36 -0
  90. package/dist/hooks/relevance.d.ts +11 -3
  91. package/dist/hooks/relevance.js +56 -26
  92. package/dist/hooks/session-end.js +7 -1
  93. package/dist/hooks/session-start.js +31 -59
  94. package/dist/hooks/shown.d.ts +1 -0
  95. package/dist/hooks/shown.js +5 -2
  96. package/dist/hooks/start-legacy.d.ts +6 -0
  97. package/dist/hooks/start-legacy.js +47 -0
  98. package/dist/hooks/user-prompt.js +29 -25
  99. package/dist/mcp/server.js +13 -1
  100. package/dist/pick/pending.d.ts +17 -0
  101. package/dist/pick/pending.js +106 -0
  102. package/dist/pick/prompt.d.ts +14 -0
  103. package/dist/pick/prompt.js +80 -0
  104. package/dist/pick/run.d.ts +22 -0
  105. package/dist/pick/run.js +36 -0
  106. package/dist/pick/worker.d.ts +2 -0
  107. package/dist/pick/worker.js +32 -0
  108. package/dist/store/adapter.d.ts +1 -0
  109. package/dist/store/each.d.ts +5 -0
  110. package/dist/store/each.js +25 -0
  111. package/dist/store/mongo/docs.d.ts +2 -0
  112. package/dist/store/mongo/docs.js +4 -0
  113. package/dist/store/mongo/driver.js +3 -2
  114. package/dist/store/mongo/filters.d.ts +1 -0
  115. package/dist/store/mongo/filters.js +8 -0
  116. package/dist/store/mongo/index.d.ts +1 -0
  117. package/dist/store/mongo/index.js +5 -2
  118. package/dist/store/mongo/observations.d.ts +2 -1
  119. package/dist/store/mongo/observations.js +11 -1
  120. package/dist/store/mongo/search.js +12 -3
  121. package/dist/store/mongo/sessions.js +7 -1
  122. package/dist/store/mongo/vectorSearch.js +9 -11
  123. package/dist/store/postgres/driver.js +3 -2
  124. package/dist/store/postgres/filters.js +5 -0
  125. package/dist/store/postgres/index.d.ts +1 -0
  126. package/dist/store/postgres/index.js +3 -0
  127. package/dist/store/postgres/meta.js +2 -1
  128. package/dist/store/postgres/observations.d.ts +1 -0
  129. package/dist/store/postgres/observations.js +14 -3
  130. package/dist/store/postgres/rows.js +4 -0
  131. package/dist/store/postgres/schema.d.ts +2 -2
  132. package/dist/store/postgres/schema.js +4 -1
  133. package/dist/store/postgres/sessions.js +14 -7
  134. package/dist/store/session-time.d.ts +2 -0
  135. package/dist/store/session-time.js +5 -0
  136. package/dist/store/sqlite/filters.js +5 -0
  137. package/dist/store/sqlite/index.d.ts +1 -0
  138. package/dist/store/sqlite/index.js +27 -5
  139. package/dist/store/sqlite/observations.d.ts +1 -0
  140. package/dist/store/sqlite/observations.js +14 -1
  141. package/dist/store/sqlite/rows.js +4 -0
  142. package/dist/store/sqlite/schema.sql +3 -1
  143. package/dist/store/sqlite/search.js +7 -3
  144. package/dist/store/sqlite/sessions.js +13 -8
  145. package/dist/types.d.ts +5 -1
  146. package/dist/usages/find.js +5 -3
  147. package/dist/usages/index.d.ts +0 -1
  148. package/dist/util/claude-cli.d.ts +6 -0
  149. package/dist/util/claude-cli.js +38 -0
  150. package/dist/util/daily-budget.d.ts +7 -0
  151. package/dist/util/daily-budget.js +38 -0
  152. package/dist/util/day.d.ts +1 -0
  153. package/dist/util/day.js +11 -0
  154. package/dist/util/job-lock.d.ts +4 -0
  155. package/dist/util/job-lock.js +56 -0
  156. package/dist/util/prompt.d.ts +5 -0
  157. package/dist/util/prompt.js +31 -0
  158. package/docs/how-it-works.md +69 -3
  159. package/docs/setup-guide.md +339 -0
  160. package/docs/with-and-without.md +43 -36
  161. package/package.json +23 -7
@@ -17,17 +17,17 @@ export async function cmdSync(argv) {
17
17
  const remote = await createStore(url);
18
18
  try {
19
19
  await remote.init();
20
- const localIds = new Set();
20
+ const localStatus = new Map();
21
21
  await eachRemoteObservation(local.store, (batch) => {
22
22
  for (const obs of batch)
23
- localIds.add(obs.id);
23
+ localStatus.set(obs.id, obs.status ?? 'done');
24
24
  });
25
- const remoteIds = new Set();
25
+ const remoteStatus = new Map();
26
26
  let pulled = 0;
27
27
  await eachRemoteObservation(remote, async (batch) => {
28
28
  for (const obs of batch)
29
- remoteIds.add(obs.id);
30
- const fresh = batch.filter((obs) => !localIds.has(obs.id));
29
+ remoteStatus.set(obs.id, obs.status ?? 'done');
30
+ const fresh = batch.filter((obs) => !localStatus.has(obs.id));
31
31
  if (fresh.length === 0)
32
32
  return;
33
33
  pulled += fresh.length;
@@ -36,26 +36,62 @@ export async function cmdSync(argv) {
36
36
  });
37
37
  let pushed = 0;
38
38
  await eachRemoteObservation(local.store, async (batch) => {
39
- const fresh = batch.filter((obs) => !remoteIds.has(obs.id));
39
+ const fresh = batch.filter((obs) => !remoteStatus.has(obs.id));
40
40
  if (fresh.length === 0)
41
41
  return;
42
42
  pushed += fresh.length;
43
43
  if (confirmed)
44
44
  await remote.insertObservations(fresh);
45
45
  });
46
+ const localBehind = statusBehind(localStatus, remoteStatus);
47
+ const remoteBehind = statusBehind(remoteStatus, localStatus);
48
+ const moved = count(localBehind) + count(remoteBehind);
46
49
  if (!confirmed) {
47
- console.log(`This would pull ${pulled} and push ${pushed} observation(s).`);
50
+ console.log(`This would pull ${pulled} and push ${pushed} observation(s), ` +
51
+ `and bring ${moved} status(es) up to date.`);
48
52
  console.log('\nNothing was transferred. Re-run with --yes to confirm.');
49
53
  return;
50
54
  }
55
+ await applyStatus(local.store, localBehind);
56
+ await applyStatus(remote, remoteBehind);
51
57
  const sessions = await syncSessions(local.store, remote);
52
- console.log(`Pulled ${pulled}, pushed ${pushed}, and reconciled ${sessions} session(s).`);
58
+ console.log(`Pulled ${pulled}, pushed ${pushed}, brought ${moved} status(es) up to date, ` +
59
+ `and reconciled ${sessions} session(s).`);
53
60
  }
54
61
  finally {
55
62
  await remote.close();
56
63
  await local.close();
57
64
  }
58
65
  }
66
+ const STATUS_ORDER = { open: 0, done: 1, replaced: 2 };
67
+ function statusBehind(mine, theirs) {
68
+ const updates = { done: [], replaced: [] };
69
+ for (const [id, status] of theirs) {
70
+ const own = mine.get(id);
71
+ if (own === undefined)
72
+ continue;
73
+ const ownRank = STATUS_ORDER[own] ?? -1;
74
+ const theirRank = STATUS_ORDER[status] ?? -1;
75
+ if (ownRank < 0 || theirRank <= ownRank)
76
+ continue;
77
+ if (status === 'done')
78
+ updates.done.push(id);
79
+ if (status === 'replaced')
80
+ updates.replaced.push(id);
81
+ }
82
+ return updates;
83
+ }
84
+ function count(updates) {
85
+ return updates.done.length + updates.replaced.length;
86
+ }
87
+ async function applyStatus(store, updates) {
88
+ for (let start = 0; start < updates.done.length; start += BATCH) {
89
+ await store.closeObservations(updates.done.slice(start, start + BATCH));
90
+ }
91
+ for (let start = 0; start < updates.replaced.length; start += BATCH) {
92
+ await store.markReplaced(updates.replaced.slice(start, start + BATCH));
93
+ }
94
+ }
59
95
  async function syncSessions(local, remote) {
60
96
  const projects = new Set();
61
97
  for (const store of [local, remote]) {
@@ -69,15 +105,40 @@ async function syncSessions(local, remote) {
69
105
  [remote, local],
70
106
  ]) {
71
107
  for (const session of await from.recentSessions(project, 1000)) {
72
- if (await to.getSession(session.id))
73
- continue;
74
- await to.upsertSession(session);
75
- moved += 1;
108
+ if (await reconcileSession(session, from, to))
109
+ moved += 1;
76
110
  }
77
111
  }
78
112
  }
79
113
  return moved;
80
114
  }
115
+ async function reconcileSession(session, from, to) {
116
+ const other = await to.getSession(session.id);
117
+ if (!other) {
118
+ await to.upsertSession(session);
119
+ return true;
120
+ }
121
+ const ours = session.updatedAt ?? 0;
122
+ const theirs = other.updatedAt ?? 0;
123
+ if (other.summary !== session.summary && ours > theirs) {
124
+ await to.upsertSession(session);
125
+ return true;
126
+ }
127
+ if (other.summary === undefined && theirs > ours) {
128
+ await from.clearSummary(session.id);
129
+ return true;
130
+ }
131
+ if (session.distilledAt !== undefined && other.distilledAt === undefined) {
132
+ await to.upsertSession({
133
+ id: other.id,
134
+ project: other.project,
135
+ startedAt: other.startedAt,
136
+ distilledAt: session.distilledAt,
137
+ });
138
+ return true;
139
+ }
140
+ return false;
141
+ }
81
142
  async function eachRemoteObservation(store, visit) {
82
143
  let after;
83
144
  let afterId;
@@ -1,8 +1,3 @@
1
- import type { Observation } from '../../types.js';
2
- import type { RecallContext } from '../../context.js';
3
- export declare function eachObservation(ctx: RecallContext, filter: {
4
- project?: string;
5
- }, visit: (batch: Observation[]) => Promise<void> | void): Promise<number>;
6
1
  export declare function cmdExport(argv: (string | undefined)[]): Promise<void>;
7
2
  export declare function cmdImport(path: string | undefined): Promise<void>;
8
3
  export declare function cmdPrune(argv: (string | undefined)[]): Promise<void>;
@@ -6,30 +6,7 @@ import { loadConfig } from '../../config/index.js';
6
6
  import { readFileSync } from 'node:fs';
7
7
  import { resolveProject } from '../../util/project.js';
8
8
  import { valueOf } from '../args.js';
9
- export async function eachObservation(ctx, filter, visit) {
10
- let after;
11
- let afterId;
12
- let total = 0;
13
- for (;;) {
14
- const batch = await ctx.store.list({
15
- ...filter,
16
- ...(after === undefined ? {} : { after }),
17
- ...(afterId === undefined ? {} : { afterId }),
18
- limit: BATCH,
19
- });
20
- if (batch.length === 0)
21
- return total;
22
- await visit(batch);
23
- total += batch.length;
24
- const last = batch[batch.length - 1];
25
- if (!last)
26
- return total;
27
- after = last.createdAt;
28
- afterId = last.id;
29
- if (batch.length < BATCH)
30
- return total;
31
- }
32
- }
9
+ import { eachObservation } from '../../store/each.js';
33
10
  export async function cmdExport(argv) {
34
11
  const all = argv.includes('--all');
35
12
  const ctx = await createContext();
@@ -54,7 +54,7 @@ async function countObservations(store) {
54
54
  async function warnAboutStranding(previous, uri) {
55
55
  if (!previous || previous === uri)
56
56
  return;
57
- let stranded = 0;
57
+ let stranded;
58
58
  try {
59
59
  const store = await createStore(previous);
60
60
  try {
@@ -6,9 +6,11 @@ import { createContext } from '../../context.js';
6
6
  import { resolveProject } from '../../util/project.js';
7
7
  import { toShortId } from '../../util/shortid.js';
8
8
  import { renderPage } from './view-page.js';
9
- import { redact } from '../../capture/turn-extractor.js';
9
+ import { redact } from '../../capture/redact.js';
10
10
  async function collect(ctx, project) {
11
- const all = (await ctx.store.list({ project, limit: 500, newest: true })).sort((a, b) => b.createdAt - a.createdAt);
11
+ const all = (await ctx.store.list({ project, limit: 500, newest: true }))
12
+ .filter((obs) => obs.status !== 'replaced')
13
+ .sort((a, b) => b.createdAt - a.createdAt);
12
14
  const kinds = {};
13
15
  for (const obs of all)
14
16
  kinds[obs.kind] = (kinds[obs.kind] ?? 0) + 1;
@@ -40,6 +42,24 @@ async function collect(ctx, project) {
40
42
  })),
41
43
  };
42
44
  }
45
+ async function respond(ctx, project, rawUrl) {
46
+ const url = new URL(rawUrl, 'http://localhost');
47
+ if (url.pathname === '/api/data') {
48
+ return { type: 'application/json', body: JSON.stringify(await collect(ctx, project)) };
49
+ }
50
+ if (url.pathname === '/api/search') {
51
+ const query = url.searchParams.get('q') ?? '';
52
+ const entries = await ctx.search.search({ text: query, project, limit: 20 });
53
+ const found = entries.map((entry) => ({
54
+ id: toShortId(entry.id),
55
+ kind: entry.kind,
56
+ when: entry.createdAt,
57
+ title: entry.title,
58
+ }));
59
+ return { type: 'application/json', body: JSON.stringify(found) };
60
+ }
61
+ return { type: 'text/html; charset=utf-8', body: renderPage(await collect(ctx, project), true) };
62
+ }
43
63
  export async function cmdView(args) {
44
64
  const project = resolveProject(undefined);
45
65
  const ctx = await createContext();
@@ -51,33 +71,15 @@ export async function cmdView(args) {
51
71
  console.log(`Wrote snapshot to ${target}`);
52
72
  return;
53
73
  }
54
- const server = createServer(async (req, res) => {
55
- try {
56
- const url = new URL(req.url ?? '/', 'http://localhost');
57
- if (url.pathname === '/api/data') {
58
- res.writeHead(200, { 'content-type': 'application/json' });
59
- res.end(JSON.stringify(await collect(ctx, project)));
60
- return;
61
- }
62
- if (url.pathname === '/api/search') {
63
- const query = url.searchParams.get('q') ?? '';
64
- const entries = await ctx.search.search({ text: query, project, limit: 20 });
65
- res.writeHead(200, { 'content-type': 'application/json' });
66
- res.end(JSON.stringify(entries.map((entry) => ({
67
- id: toShortId(entry.id),
68
- kind: entry.kind,
69
- when: entry.createdAt,
70
- title: entry.title,
71
- }))));
72
- return;
73
- }
74
- res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
75
- res.end(renderPage(await collect(ctx, project), true));
76
- }
77
- catch (error) {
78
- res.writeHead(500, { 'content-type': 'text/plain' });
74
+ const server = createServer((req, res) => {
75
+ respond(ctx, project, req.url ?? '/').then(({ type, body }) => {
76
+ res.writeHead(200, { 'content-type': type });
77
+ res.end(body);
78
+ }, (error) => {
79
+ if (!res.headersSent)
80
+ res.writeHead(500, { 'content-type': 'text/plain' });
79
81
  res.end(error instanceof Error ? error.message : String(error));
80
- }
82
+ });
81
83
  });
82
84
  server.listen(0, '127.0.0.1', () => {
83
85
  const address = server.address();
package/dist/cli/index.js CHANGED
@@ -8,7 +8,9 @@ import { cmdSync } from './commands/sync.js';
8
8
  import { cmdForget, cmdRemember, cmdSearch } from './commands/memory.js';
9
9
  import { cmdExport, cmdImport, cmdPrune, cmdReembed } from './commands/transfer.js';
10
10
  import { cmdMerge, cmdProjects, cmdStats } from './commands/insight.js';
11
- import { cmdFlush, cmdReset, cmdUpdate } from './commands/maintain.js';
11
+ import { cmdFlush, cmdRedact, cmdReset, cmdUpdate } from './commands/maintain.js';
12
+ import { cmdDistill } from './commands/distill.js';
13
+ import { cmdPick } from './commands/pick.js';
12
14
  import { cmdScan, cmdUsages } from './commands/graph.js';
13
15
  import { cmdSeed } from './commands/seed.js';
14
16
  import { cmdAdoption } from './commands/adoption.js';
@@ -73,8 +75,17 @@ async function run() {
73
75
  case 'update':
74
76
  await cmdUpdate(args);
75
77
  break;
78
+ case 'distill':
79
+ await cmdDistill(args);
80
+ break;
81
+ case 'pick':
82
+ cmdPick(args);
83
+ break;
84
+ case 'redact':
85
+ await cmdRedact(args);
86
+ break;
76
87
  case 'flush':
77
- await cmdFlush();
88
+ await cmdFlush(args);
78
89
  break;
79
90
  case 'reset':
80
91
  await cmdReset(args);
@@ -1,4 +1,6 @@
1
1
  import { Scope } from './paths.js';
2
2
  export declare function assertStableLocation(distDir: string): void;
3
+ export declare function refreshHooks(distDir: string, path: string): boolean;
3
4
  export declare function install(distDir: string, scope: Scope, project: string): string;
4
5
  export declare function uninstall(distDir: string, scope: Scope, project: string): string | null;
6
+ export declare function ourHookFile(command: string): string | null;
@@ -20,6 +20,7 @@ const HOOKS = [
20
20
  ['UserPromptSubmit', 'user-prompt.js'],
21
21
  ['SessionEnd', 'session-end.js'],
22
22
  ['PreToolUse', 'prefer-usages.js', 'Bash|Grep', 10],
23
+ ['PreToolUse', 'pick-deliver.js', undefined, 5],
23
24
  ];
24
25
  export function assertStableLocation(distDir) {
25
26
  const ephemeral = ['_npx', '_cacache'];
@@ -32,18 +33,23 @@ export function assertStableLocation(distDir) {
32
33
  ` npm install -g claude-db\n` +
33
34
  ` claude-db install --project\n`);
34
35
  }
35
- export function install(distDir, scope, project) {
36
- const path = settingsPathFor(scope, project);
37
- const settings = readJson(path);
38
- const hooks = (settings['hooks'] ?? {});
36
+ function withoutOurHooks(entries) {
37
+ return entries
38
+ .map((entry) => ({
39
+ ...entry,
40
+ hooks: entry.hooks.filter((hook) => !isOurHook(hook.command)),
41
+ }))
42
+ .filter((entry) => entry.hooks.length > 0);
43
+ }
44
+ function withOurHooks(hooks, distDir) {
45
+ const merged = {};
46
+ for (const [event, entries] of Object.entries(hooks)) {
47
+ const kept = withoutOurHooks(entries);
48
+ if (kept.length > 0)
49
+ merged[event] = kept;
50
+ }
39
51
  for (const [event, file, matcher, timeout] of HOOKS) {
40
- const kept = (hooks[event] ?? [])
41
- .map((entry) => ({
42
- ...entry,
43
- hooks: entry.hooks.filter((hook) => !isOurHook(hook.command)),
44
- }))
45
- .filter((entry) => entry.hooks.length > 0);
46
- kept.push({
52
+ (merged[event] ??= []).push({
47
53
  ...(matcher ? { matcher } : {}),
48
54
  hooks: [
49
55
  {
@@ -53,9 +59,28 @@ export function install(distDir, scope, project) {
53
59
  },
54
60
  ],
55
61
  });
56
- hooks[event] = kept;
57
62
  }
58
- settings['hooks'] = hooks;
63
+ return merged;
64
+ }
65
+ export function refreshHooks(distDir, path) {
66
+ const settings = readJson(path);
67
+ const hooks = (settings['hooks'] ?? {});
68
+ const ours = Object.values(hooks)
69
+ .flatMap((entries) => entries.flatMap((entry) => entry.hooks.map((hook) => hook.command)))
70
+ .filter(isOurHook);
71
+ const here = `${hookCommand(distDir, '')}/`;
72
+ if (ours.length === 0 || !ours.every((command) => command.startsWith(here)))
73
+ return false;
74
+ const merged = withOurHooks(hooks, distDir);
75
+ if (JSON.stringify(merged) === JSON.stringify(hooks))
76
+ return false;
77
+ writeJson(path, { ...settings, hooks: merged });
78
+ return true;
79
+ }
80
+ export function install(distDir, scope, project) {
81
+ const path = settingsPathFor(scope, project);
82
+ const settings = readJson(path);
83
+ settings['hooks'] = withOurHooks((settings['hooks'] ?? {}), distDir);
59
84
  delete settings['mcpServers'];
60
85
  writeJson(path, settings);
61
86
  const server = resolve(distDir, 'mcp', 'server.js');
@@ -91,12 +116,7 @@ export function uninstall(distDir, scope, project) {
91
116
  return null;
92
117
  const hooks = (settings['hooks'] ?? {});
93
118
  for (const [event, entries] of Object.entries(hooks)) {
94
- const kept = entries
95
- .map((entry) => ({
96
- ...entry,
97
- hooks: entry.hooks.filter((hook) => !isOurHook(hook.command)),
98
- }))
99
- .filter((entry) => entry.hooks.length > 0);
119
+ const kept = withoutOurHooks(entries);
100
120
  if (kept.length > 0)
101
121
  hooks[event] = kept;
102
122
  else
@@ -128,7 +148,10 @@ export function uninstall(distDir, scope, project) {
128
148
  function hookCommand(distDir, file) {
129
149
  return `node ${toPosix(resolve(distDir, 'hooks', file))}`;
130
150
  }
131
- function isOurHook(command) {
151
+ export function ourHookFile(command) {
132
152
  const path = toPosix(command);
133
- return HOOKS.some(([, file]) => path.endsWith(`/hooks/${file}`));
153
+ return HOOKS.find(([, file]) => path.endsWith(`/hooks/${file}`))?.[1] ?? null;
154
+ }
155
+ function isOurHook(command) {
156
+ return ourHookFile(command) !== null;
134
157
  }
@@ -8,8 +8,10 @@ export const INSTRUCTIONS = [
8
8
  '',
9
9
  'You have persistent memory of this project: your past sessions, decisions',
10
10
  'and their reasoning, served by the `memory` MCP server. Session summaries',
11
- 'are injected at startup and the best matching observation is injected above',
12
- 'each prompt, but that is only ever a slice of what you know.',
11
+ 'are injected at startup, and a `<memory>` block, added with a prompt or with',
12
+ 'your first tool call after it, lists earlier work that fits the prompt, each',
13
+ 'line ending in an id `get_observations` expands.',
14
+ 'That is only ever a slice of what you know.',
13
15
  '',
14
16
  'Recall the rest *before* re-deriving something you already learned. That',
15
17
  'means before grepping or reading git history to reconstruct why something is',
@@ -1,10 +1,14 @@
1
1
  import { BLOCK_START, INSTRUCTIONS, writeInstructions } from './instructions.js';
2
- import { instructionsPathFor, skillPathFor } from './paths.js';
2
+ import { refreshHooks } from './install.js';
3
+ import { instructionsPathFor, settingsPathFor, skillPathFor } from './paths.js';
3
4
  import { readText, writeAtomic } from './files.js';
4
5
  import { resolve } from 'node:path';
5
6
  export function refreshInstalled(distDir, project) {
6
7
  const refreshed = [];
7
8
  for (const scope of ['project', 'global']) {
9
+ const settingsPath = settingsPathFor(scope, project);
10
+ if (refreshHooks(distDir, settingsPath))
11
+ refreshed.push(settingsPath);
8
12
  const skillPath = skillPathFor(scope, project);
9
13
  const current = readText(skillPath);
10
14
  if (current.length > 0) {
package/dist/cli/usage.js CHANGED
@@ -18,7 +18,11 @@ export function usage() {
18
18
  stats What this project's memory is made of
19
19
  adoption How often sessions grep vs use the memory tools
20
20
  view [--export <file>] See this project's memory live in the browser
21
+ distill [on|off] Turn chats into facts with Haiku; no argument shows status
22
+ pick [on|off] Let Haiku pick the memory shown with each prompt
23
+ redact Re-apply secret redaction to memory already saved
21
24
  flush Re-ingest every transcript for this project
25
+ [--repair] ...only chats that already have memory
22
26
  seed --from-git [--limit n] Fill a cold memory from this repo's history
23
27
  scan [--force] Map this repo's symbols and how they connect
24
28
  usages [--mode m] <symbol> Who uses a symbol; --mode usages|explain|path
@@ -0,0 +1 @@
1
+ export declare const CONFIG_DIR: string;
@@ -0,0 +1,3 @@
1
+ import { homedir } from 'node:os';
2
+ import { join } from 'node:path';
3
+ export const CONFIG_DIR = join(homedir(), '.claude-memory');
@@ -1,3 +1,3 @@
1
1
  export type { Config } from './schema.js';
2
2
  export { ConfigSchema } from './schema.js';
3
- export { CONFIG_DIR, CONFIG_PATH, DEFAULT_DB_PATH, loadConfig, saveConfig } from './load.js';
3
+ export { CONFIG_DIR, CONFIG_PATH, loadConfig, saveConfig, setConfigValue } from './load.js';
@@ -1,2 +1,2 @@
1
1
  export { ConfigSchema } from './schema.js';
2
- export { CONFIG_DIR, CONFIG_PATH, DEFAULT_DB_PATH, loadConfig, saveConfig } from './load.js';
2
+ export { CONFIG_DIR, CONFIG_PATH, loadConfig, saveConfig, setConfigValue } from './load.js';
@@ -1,6 +1,9 @@
1
+ import { CONFIG_DIR } from './dir.js';
1
2
  import type { Config } from './schema.js';
2
- export declare const CONFIG_DIR: string;
3
+ export { CONFIG_DIR };
3
4
  export declare const CONFIG_PATH: string;
4
5
  export declare const DEFAULT_DB_PATH: string;
6
+ export declare function dropSupersededDefaults(raw: unknown): unknown;
5
7
  export declare function loadConfig(): Config;
8
+ export declare function setConfigValue(section: string, key: string, value: unknown): void;
6
9
  export declare function saveConfig(config: Config): void;
@@ -1,23 +1,28 @@
1
1
  import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
2
2
  import { dirname, join } from 'node:path';
3
- import { homedir } from 'node:os';
3
+ import { CONFIG_DIR } from './dir.js';
4
4
  import { ConfigSchema } from './schema.js';
5
- export const CONFIG_DIR = join(homedir(), '.claude-memory');
5
+ export { CONFIG_DIR };
6
6
  export const CONFIG_PATH = join(CONFIG_DIR, 'config.json');
7
7
  export const DEFAULT_DB_PATH = join(CONFIG_DIR, 'memory.db');
8
- const SUPERSEDED_INJECT = { expandTop: 1, promptMaxChars: 500 };
9
- function dropSupersededDefaults(raw) {
8
+ const SUPERSEDED_INJECT = [
9
+ { expandTop: 1, promptMaxChars: 500 },
10
+ { promptResults: 4, minOverlap: 1 },
11
+ { promptResults: 3, minOverlap: 2 },
12
+ ];
13
+ export function dropSupersededDefaults(raw) {
10
14
  const file = raw;
11
- const inject = file?.inject;
12
- if (!inject)
15
+ const saved = file?.inject;
16
+ if (!saved)
13
17
  return raw;
14
- const untouched = Object.entries(SUPERSEDED_INJECT).every(([key, value]) => inject[key] === value);
15
- if (!untouched)
16
- return raw;
17
- const trimmed = { ...inject };
18
- for (const key of Object.keys(SUPERSEDED_INJECT))
19
- delete trimmed[key];
20
- return { ...file, inject: trimmed };
18
+ let inject = saved;
19
+ for (const superseded of SUPERSEDED_INJECT) {
20
+ const untouched = Object.entries(superseded).every(([key, value]) => inject[key] === value);
21
+ if (!untouched)
22
+ continue;
23
+ inject = Object.fromEntries(Object.entries(inject).filter(([key]) => !(key in superseded)));
24
+ }
25
+ return inject === saved ? raw : { ...file, inject };
21
26
  }
22
27
  export function loadConfig() {
23
28
  let fileConfig = {};
@@ -32,6 +37,18 @@ export function loadConfig() {
32
37
  database: envUrl && envUrl.length > 0 ? envUrl : config.database || DEFAULT_DB_PATH,
33
38
  };
34
39
  }
40
+ export function setConfigValue(section, key, value) {
41
+ let file = {};
42
+ try {
43
+ file = JSON.parse(readFileSync(CONFIG_PATH, 'utf8'));
44
+ }
45
+ catch { }
46
+ const current = file[section];
47
+ const block = current && typeof current === 'object' ? current : {};
48
+ file[section] = { ...block, [key]: value };
49
+ mkdirSync(dirname(CONFIG_PATH), { recursive: true });
50
+ writeFileSync(CONFIG_PATH, `${JSON.stringify(file, null, 2)}\n`, 'utf8');
51
+ }
35
52
  export function saveConfig(config) {
36
53
  mkdirSync(dirname(CONFIG_PATH), { recursive: true });
37
54
  writeFileSync(CONFIG_PATH, `${JSON.stringify(config, null, 2)}\n`, 'utf8');
@@ -32,6 +32,25 @@ export declare const ConfigSchema: z.ZodObject<{
32
32
  haiku: "haiku";
33
33
  }>>;
34
34
  }, z.core.$strip>>;
35
+ distill: z.ZodPrefault<z.ZodObject<{
36
+ enabled: z.ZodDefault<z.ZodBoolean>;
37
+ model: z.ZodDefault<z.ZodEnum<{
38
+ opus: "opus";
39
+ sonnet: "sonnet";
40
+ haiku: "haiku";
41
+ }>>;
42
+ dailyLimit: z.ZodDefault<z.ZodNumber>;
43
+ backfillDays: z.ZodDefault<z.ZodNumber>;
44
+ }, z.core.$strip>>;
45
+ pick: z.ZodPrefault<z.ZodObject<{
46
+ enabled: z.ZodDefault<z.ZodBoolean>;
47
+ model: z.ZodDefault<z.ZodEnum<{
48
+ opus: "opus";
49
+ sonnet: "sonnet";
50
+ haiku: "haiku";
51
+ }>>;
52
+ dailyLimit: z.ZodDefault<z.ZodNumber>;
53
+ }, z.core.$strip>>;
35
54
  inject: z.ZodPrefault<z.ZodObject<{
36
55
  sessions: z.ZodDefault<z.ZodNumber>;
37
56
  maxChars: z.ZodDefault<z.ZodNumber>;
@@ -20,13 +20,28 @@ export const ConfigSchema = z.object({
20
20
  summarizeModel: z.enum(['opus', 'sonnet', 'haiku']).default('opus'),
21
21
  })
22
22
  .prefault({}),
23
+ distill: z
24
+ .object({
25
+ enabled: z.boolean().default(true),
26
+ model: z.enum(['haiku', 'sonnet', 'opus']).default('haiku'),
27
+ dailyLimit: z.number().int().nonnegative().default(30),
28
+ backfillDays: z.number().int().positive().default(90),
29
+ })
30
+ .prefault({}),
31
+ pick: z
32
+ .object({
33
+ enabled: z.boolean().default(true),
34
+ model: z.enum(['haiku', 'sonnet', 'opus']).default('haiku'),
35
+ dailyLimit: z.number().int().nonnegative().default(150),
36
+ })
37
+ .prefault({}),
23
38
  inject: z
24
39
  .object({
25
40
  sessions: z.number().int().nonnegative().default(5),
26
41
  maxChars: z.number().int().positive().default(6000),
27
42
  perPrompt: z.boolean().default(true),
28
- promptResults: z.number().int().positive().max(10).default(4),
29
- minOverlap: z.number().int().min(0).default(1),
43
+ promptResults: z.number().int().positive().max(10).default(2),
44
+ minOverlap: z.number().int().min(0).default(2),
30
45
  promptMaxChars: z.number().int().positive().default(700),
31
46
  expandTop: z.number().int().min(0).max(3).default(0),
32
47
  expandMaxChars: z.number().int().positive().default(900),
@@ -1,6 +1,6 @@
1
1
  import type { Embedder } from './embedder.js';
2
2
  export type { Embedder } from './embedder.js';
3
- export { NoopEmbedder, normalize } from './embedder.js';
3
+ export { NoopEmbedder } from './embedder.js';
4
4
  export { BuiltinEmbedder } from './builtin.js';
5
5
  export type EmbeddingProvider = 'auto' | 'local' | 'builtin' | 'none';
6
6
  export declare function createEmbedder(provider: EmbeddingProvider): Promise<Embedder>;
@@ -1,6 +1,6 @@
1
1
  import { NoopEmbedder } from './embedder.js';
2
2
  import { BuiltinEmbedder } from './builtin.js';
3
- export { NoopEmbedder, normalize } from './embedder.js';
3
+ export { NoopEmbedder } from './embedder.js';
4
4
  export { BuiltinEmbedder } from './builtin.js';
5
5
  export async function createEmbedder(provider) {
6
6
  switch (provider) {
@@ -1,4 +1,5 @@
1
1
  import { normalize } from './embedder.js';
2
+ const TRANSFORMERS = '@xenova/transformers';
2
3
  export class LocalEmbedder {
3
4
  id = 'Xenova/all-MiniLM-L6-v2';
4
5
  dimensions = 384;
@@ -15,7 +16,7 @@ export class LocalEmbedder {
15
16
  this.pipelinePromise ??= (async () => {
16
17
  let transformers;
17
18
  try {
18
- transformers = (await import('@xenova/transformers'));
19
+ transformers = (await import(TRANSFORMERS));
19
20
  }
20
21
  catch {
21
22
  throw new Error('Local embeddings need @xenova/transformers. ' +
@@ -0,0 +1,4 @@
1
+ import type { BudgetUsage } from '../util/daily-budget.js';
2
+ export declare function distillUsage(now?: number): BudgetUsage;
3
+ export declare function takeDistillCalls(limit: number, count: number, now?: number): boolean;
4
+ export declare function pauseDistill(now?: number): void;
@@ -0,0 +1,12 @@
1
+ import { budgetUsage, pauseBudget, takeBudget } from '../util/daily-budget.js';
2
+ const BUDGET = 'distill';
3
+ const PAUSE_MS = 24 * 60 * 60 * 1000;
4
+ export function distillUsage(now = Date.now()) {
5
+ return budgetUsage(BUDGET, now);
6
+ }
7
+ export function takeDistillCalls(limit, count, now = Date.now()) {
8
+ return takeBudget(BUDGET, limit, count, now);
9
+ }
10
+ export function pauseDistill(now = Date.now()) {
11
+ pauseBudget(BUDGET, PAUSE_MS, now);
12
+ }