claude-db 0.12.4 → 0.13.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 (100) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/dist/capture/cursors.d.ts +8 -0
  3. package/dist/capture/cursors.js +99 -0
  4. package/dist/capture/flush-lock.d.ts +11 -0
  5. package/dist/capture/flush-lock.js +108 -0
  6. package/dist/capture/flush.d.ts +4 -4
  7. package/dist/capture/flush.js +19 -80
  8. package/dist/capture/index.d.ts +3 -3
  9. package/dist/capture/index.js +3 -3
  10. package/dist/capture/reingest.d.ts +1 -0
  11. package/dist/capture/reingest.js +4 -1
  12. package/dist/capture/scrub.d.ts +1 -0
  13. package/dist/capture/scrub.js +4 -1
  14. package/dist/capture/vectors.js +2 -1
  15. package/dist/cli/commands/daemon.d.ts +1 -0
  16. package/dist/cli/commands/daemon.js +29 -0
  17. package/dist/cli/commands/distill.js +2 -1
  18. package/dist/cli/commands/graph.js +2 -1
  19. package/dist/cli/commands/maintain.js +3 -1
  20. package/dist/cli/index.js +4 -0
  21. package/dist/cli/usage.js +1 -0
  22. package/dist/config/schema.d.ts +4 -0
  23. package/dist/config/schema.js +6 -0
  24. package/dist/context.d.ts +1 -0
  25. package/dist/context.js +21 -6
  26. package/dist/daemon/client.d.ts +12 -0
  27. package/dist/daemon/client.js +88 -0
  28. package/dist/daemon/paths.d.ts +16 -0
  29. package/dist/daemon/paths.js +19 -0
  30. package/dist/daemon/server.d.ts +2 -0
  31. package/dist/daemon/server.js +225 -0
  32. package/dist/facts/budget.d.ts +1 -0
  33. package/dist/facts/budget.js +4 -0
  34. package/dist/facts/claude-memory.js +1 -1
  35. package/dist/facts/distill.js +2 -2
  36. package/dist/facts/known.js +1 -1
  37. package/dist/facts/model.d.ts +1 -1
  38. package/dist/facts/model.js +18 -7
  39. package/dist/facts/start.js +1 -1
  40. package/dist/graph/query/refresh.js +5 -1
  41. package/dist/graph/scan/cache.d.ts +1 -0
  42. package/dist/graph/scan/cache.js +1 -1
  43. package/dist/graph/scan/persist.js +12 -16
  44. package/dist/hooks/compact.d.ts +2 -1
  45. package/dist/hooks/compact.js +2 -2
  46. package/dist/hooks/detached.d.ts +1 -0
  47. package/dist/hooks/detached.js +6 -0
  48. package/dist/hooks/graph-presence.d.ts +3 -0
  49. package/dist/hooks/graph-presence.js +42 -0
  50. package/dist/hooks/handlers/prefer-usages.d.ts +2 -0
  51. package/dist/hooks/handlers/prefer-usages.js +113 -0
  52. package/dist/hooks/handlers/session-end.d.ts +2 -0
  53. package/dist/hooks/handlers/session-end.js +62 -0
  54. package/dist/hooks/handlers/session-start.d.ts +2 -0
  55. package/dist/hooks/handlers/session-start.js +104 -0
  56. package/dist/hooks/handlers/turn-end.d.ts +2 -0
  57. package/dist/hooks/handlers/turn-end.js +31 -0
  58. package/dist/hooks/handlers/user-prompt.d.ts +2 -0
  59. package/dist/hooks/handlers/user-prompt.js +55 -0
  60. package/dist/hooks/local.d.ts +2 -0
  61. package/dist/hooks/local.js +23 -0
  62. package/dist/hooks/payload.d.ts +9 -4
  63. package/dist/hooks/payload.js +26 -17
  64. package/dist/hooks/prefer-usages.js +2 -118
  65. package/dist/hooks/route.d.ts +2 -0
  66. package/dist/hooks/route.js +28 -0
  67. package/dist/hooks/run.d.ts +14 -0
  68. package/dist/hooks/run.js +12 -0
  69. package/dist/hooks/save-turn.d.ts +2 -0
  70. package/dist/hooks/save-turn.js +11 -0
  71. package/dist/hooks/session-end.js +2 -63
  72. package/dist/hooks/session-start.js +2 -90
  73. package/dist/hooks/turn-end.js +2 -25
  74. package/dist/hooks/turn-save.js +10 -15
  75. package/dist/hooks/user-prompt.js +2 -58
  76. package/dist/search/service.d.ts +1 -0
  77. package/dist/search/service.js +16 -6
  78. package/dist/store/adapter.d.ts +7 -5
  79. package/dist/store/mongo/index.d.ts +3 -6
  80. package/dist/store/mongo/index.js +5 -0
  81. package/dist/store/mongo/observations.js +13 -1
  82. package/dist/store/postgres/driver.d.ts +10 -3
  83. package/dist/store/postgres/driver.js +12 -0
  84. package/dist/store/postgres/graph.d.ts +3 -5
  85. package/dist/store/postgres/graph.js +56 -50
  86. package/dist/store/postgres/index.d.ts +3 -6
  87. package/dist/store/postgres/index.js +5 -4
  88. package/dist/store/postgres/insert.js +98 -50
  89. package/dist/store/project-resolver.d.ts +3 -0
  90. package/dist/store/project-resolver.js +34 -23
  91. package/dist/store/scoped-store.js +1 -1
  92. package/dist/store/sqlite/graph.d.ts +3 -5
  93. package/dist/store/sqlite/graph.js +42 -33
  94. package/dist/store/sqlite/index.d.ts +3 -6
  95. package/dist/store/sqlite/index.js +3 -0
  96. package/dist/store/sqlite/observations.js +6 -2
  97. package/dist/util/job-lock.d.ts +1 -0
  98. package/dist/util/job-lock.js +50 -10
  99. package/docs/setup-guide.md +5 -0
  100. package/package.json +6 -6
@@ -10,6 +10,12 @@ export const ConfigSchema = z.object({
10
10
  })
11
11
  .prefault({}),
12
12
  updates: z.enum(['auto', 'notify', 'off']).default('notify'),
13
+ daemon: z
14
+ .object({
15
+ enabled: z.boolean().default(false),
16
+ idleMinutes: z.number().int().positive().default(15),
17
+ })
18
+ .prefault({}),
13
19
  project: z
14
20
  .object({
15
21
  remote: z
package/dist/context.d.ts CHANGED
@@ -10,3 +10,4 @@ export interface RecallContext {
10
10
  close(): Promise<void>;
11
11
  }
12
12
  export declare function createContext(overrides?: Partial<Config>): Promise<RecallContext>;
13
+ export declare function embedderWithDeadline(load: () => Promise<Embedder>, timeoutMs: number, now?: () => number): () => Promise<Embedder>;
package/dist/context.js CHANGED
@@ -9,17 +9,32 @@ export async function createContext(overrides) {
9
9
  const raw = await createStore(config.database);
10
10
  await raw.init();
11
11
  const store = withProjectScope(raw, createScopeResolver({ store: raw, remote: config.project.remote }));
12
- let pending = null;
13
- const embedder = () => {
14
- pending ??= withTimeout(createEmbedder(config.embeddings.provider).catch(() => new NoopEmbedder()), config.embeddings.timeoutMs);
15
- return pending;
16
- };
12
+ const embedder = embedderWithDeadline(() => createEmbedder(config.embeddings.provider), config.embeddings.timeoutMs);
17
13
  const search = new SearchService(store, embedder, config.embeddings.maxScanCandidates);
18
14
  return { config, store, embedder, search, close: () => store.close() };
19
15
  }
16
+ export function embedderWithDeadline(load, timeoutMs, now = Date.now) {
17
+ let loading = null;
18
+ let ready = null;
19
+ let deadline = 0;
20
+ return () => {
21
+ if (ready)
22
+ return Promise.resolve(ready);
23
+ if (!loading) {
24
+ deadline = now() + timeoutMs;
25
+ loading = load().catch(() => new NoopEmbedder());
26
+ void loading.then((embedder) => {
27
+ ready = embedder;
28
+ });
29
+ }
30
+ if (timeoutMs <= 0)
31
+ return loading;
32
+ return withTimeout(loading, deadline - now());
33
+ };
34
+ }
20
35
  async function withTimeout(pending, ms) {
21
36
  if (ms <= 0)
22
- return pending;
37
+ return Promise.race([pending, Promise.resolve(new NoopEmbedder())]);
23
38
  let timer;
24
39
  const limit = new Promise((settle) => {
25
40
  timer = setTimeout(() => settle(new NoopEmbedder()), ms);
@@ -0,0 +1,12 @@
1
+ import type { DaemonReply, DaemonRequest } from './paths.js';
2
+ export declare const STARTING_PREFIX = "daemon.starting.";
3
+ export type Asked = DaemonReply | {
4
+ kind: 'unreachable';
5
+ } | {
6
+ kind: 'timeout';
7
+ };
8
+ export declare function daemonWanted(env?: NodeJS.ProcessEnv): boolean;
9
+ export declare function forwardedEnv(env?: NodeJS.ProcessEnv): Record<string, string>;
10
+ export declare function workerEnv(env?: NodeJS.ProcessEnv): Record<string, string>;
11
+ export declare function askDaemon(request: DaemonRequest, timeoutMs: number): Promise<Asked>;
12
+ export declare function startDaemon(now?: number): boolean;
@@ -0,0 +1,88 @@
1
+ import { readFileSync, rmSync, writeFileSync } from 'node:fs';
2
+ import { createConnection } from 'node:net';
3
+ import { homedir } from 'node:os';
4
+ import { join } from 'node:path';
5
+ import { CONFIG_DIR } from '../config/dir.js';
6
+ import { runDetached } from '../hooks/detached.js';
7
+ import { DAEMON_SCRIPT, socketPath } from './paths.js';
8
+ const FORWARDED = ['CLAUDE_DB_CAPTURE', 'CLAUDE_CODE_ENTRYPOINT', 'CLAUDE_DB_USAGES_HOOK'];
9
+ const OWN_SETUP = ['CLAUDE_DB_URL', 'CLAUDE_DB_GRAPH_CACHE'];
10
+ const STARTING_FOR_MS = 15_000;
11
+ export const STARTING_PREFIX = 'daemon.starting.';
12
+ export function daemonWanted(env = process.env) {
13
+ if (OWN_SETUP.some((name) => env[name]))
14
+ return false;
15
+ const forced = env['CLAUDE_DB_DAEMON'];
16
+ if (forced === 'on')
17
+ return true;
18
+ if (forced === 'off')
19
+ return false;
20
+ try {
21
+ const config = JSON.parse(readFileSync(join(CONFIG_DIR, 'config.json'), 'utf8'));
22
+ return config.daemon?.enabled === true;
23
+ }
24
+ catch {
25
+ return false;
26
+ }
27
+ }
28
+ export function forwardedEnv(env = process.env) {
29
+ const picked = {};
30
+ for (const name of FORWARDED) {
31
+ const value = env[name];
32
+ if (value !== undefined)
33
+ picked[name] = value;
34
+ }
35
+ return picked;
36
+ }
37
+ export function workerEnv(env = process.env) {
38
+ const picked = {};
39
+ for (const [name, value] of Object.entries(env)) {
40
+ if (value !== undefined)
41
+ picked[name] = value;
42
+ }
43
+ return picked;
44
+ }
45
+ export function askDaemon(request, timeoutMs) {
46
+ return new Promise((settle) => {
47
+ let sent = false;
48
+ let reply = '';
49
+ const socket = createConnection(socketPath());
50
+ const finish = (asked) => {
51
+ clearTimeout(timer);
52
+ socket.destroy();
53
+ settle(asked);
54
+ };
55
+ const timer = setTimeout(() => finish(sent ? { kind: 'timeout' } : { kind: 'unreachable' }), timeoutMs);
56
+ socket.setEncoding('utf8');
57
+ socket.on('connect', () => {
58
+ sent = true;
59
+ socket.write(`${JSON.stringify(request)}\n`);
60
+ });
61
+ socket.on('data', (chunk) => {
62
+ reply += chunk;
63
+ const end = reply.indexOf('\n');
64
+ if (end < 0)
65
+ return;
66
+ try {
67
+ finish(JSON.parse(reply.slice(0, end)));
68
+ }
69
+ catch {
70
+ finish({ kind: 'timeout' });
71
+ }
72
+ });
73
+ socket.on('error', () => finish(sent ? { kind: 'timeout' } : { kind: 'unreachable' }));
74
+ socket.on('close', () => finish(sent ? { kind: 'timeout' } : { kind: 'unreachable' }));
75
+ });
76
+ }
77
+ export function startDaemon(now = Date.now()) {
78
+ const window = Math.floor(now / STARTING_FOR_MS);
79
+ const marker = join(CONFIG_DIR, `${STARTING_PREFIX}${window}`);
80
+ try {
81
+ writeFileSync(marker, String(now), { flag: 'wx' });
82
+ }
83
+ catch {
84
+ return false;
85
+ }
86
+ runDetached(DAEMON_SCRIPT, [], homedir(), () => rmSync(marker, { force: true }));
87
+ return true;
88
+ }
@@ -0,0 +1,16 @@
1
+ export declare const DAEMON_SCRIPT: string;
2
+ export declare function socketPath(): string;
3
+ export declare function buildId(): string;
4
+ export interface DaemonRequest {
5
+ build: string;
6
+ hook: string;
7
+ raw: string;
8
+ env: Record<string, string>;
9
+ workerEnv?: Record<string, string>;
10
+ }
11
+ export type DaemonReply = {
12
+ kind: 'done';
13
+ out: string;
14
+ } | {
15
+ kind: 'stale';
16
+ };
@@ -0,0 +1,19 @@
1
+ import { statSync } from 'node:fs';
2
+ import { dirname, join, resolve } from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { CONFIG_DIR } from '../config/dir.js';
5
+ import { scopeToken } from '../util/scope.js';
6
+ export const DAEMON_SCRIPT = resolve(dirname(fileURLToPath(import.meta.url)), 'server.js');
7
+ export function socketPath() {
8
+ if (process.platform === 'win32')
9
+ return `\\\\.\\pipe\\claude-db-${scopeToken(CONFIG_DIR)}`;
10
+ return join(CONFIG_DIR, 'daemon.sock');
11
+ }
12
+ export function buildId() {
13
+ try {
14
+ return `${DAEMON_SCRIPT}:${statSync(DAEMON_SCRIPT).mtimeMs}`;
15
+ }
16
+ catch {
17
+ return DAEMON_SCRIPT;
18
+ }
19
+ }
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,225 @@
1
+ #!/usr/bin/env node
2
+ import { chmodSync, readdirSync, rmSync, statSync } from 'node:fs';
3
+ import { createConnection, createServer } from 'node:net';
4
+ import { join } from 'node:path';
5
+ import { CONFIG_DIR, CONFIG_PATH, loadConfig } from '../config/index.js';
6
+ import { createContext } from '../context.js';
7
+ import { parsePayload } from '../hooks/payload.js';
8
+ import { isHookName, loadHandler } from '../hooks/run.js';
9
+ import { withWorkerEnv } from '../hooks/detached.js';
10
+ import { silenceSqliteWarning } from '../util/warnings.js';
11
+ import { buildId, socketPath } from './paths.js';
12
+ import { STARTING_PREFIX } from './client.js';
13
+ silenceSqliteWarning();
14
+ const BUILD = buildId();
15
+ const SOCKET = socketPath();
16
+ const MAX_REQUEST_BYTES = 8 * 1024 * 1024;
17
+ const RETIRE_AFTER_MS = 30_000;
18
+ let current = null;
19
+ let running = 0;
20
+ const pending = new Set();
21
+ let idleTimer;
22
+ let server;
23
+ let closing = false;
24
+ const log = (message) => {
25
+ process.stderr.write(`${new Date().toISOString()} daemon ${message}\n`);
26
+ };
27
+ function configStamp() {
28
+ try {
29
+ return statSync(CONFIG_PATH).mtimeMs;
30
+ }
31
+ catch {
32
+ return 0;
33
+ }
34
+ }
35
+ function retire(ctx) {
36
+ setTimeout(() => {
37
+ ctx.then((old) => old.close()).catch(() => undefined);
38
+ }, RETIRE_AFTER_MS).unref();
39
+ }
40
+ function context() {
41
+ const stamp = configStamp();
42
+ if (current && current.stamp === stamp)
43
+ return current.ctx;
44
+ if (current)
45
+ retire(current.ctx);
46
+ const ctx = createContext();
47
+ current = { stamp, ctx };
48
+ ctx.catch(() => {
49
+ if (current?.ctx === ctx)
50
+ current = null;
51
+ });
52
+ return ctx;
53
+ }
54
+ function idleMs() {
55
+ try {
56
+ return loadConfig().daemon.idleMinutes * 60_000;
57
+ }
58
+ catch {
59
+ return 15 * 60_000;
60
+ }
61
+ }
62
+ function touch() {
63
+ clearTimeout(idleTimer);
64
+ idleTimer = setTimeout(() => {
65
+ if (running === 0 && pending.size === 0)
66
+ void shutdown('idle');
67
+ else
68
+ touch();
69
+ }, idleMs());
70
+ }
71
+ function track(task) {
72
+ const done = task()
73
+ .catch((error) => log(`background task failed: ${String(error)}`))
74
+ .finally(() => {
75
+ pending.delete(done);
76
+ touch();
77
+ });
78
+ pending.add(done);
79
+ return true;
80
+ }
81
+ async function answer(request) {
82
+ if (request.build !== BUILD) {
83
+ setImmediate(() => void shutdown('replaced by a newer build'));
84
+ return { kind: 'stale' };
85
+ }
86
+ if (request.hook === '__stop') {
87
+ setImmediate(() => void shutdown('asked to stop'));
88
+ return { kind: 'done', out: '' };
89
+ }
90
+ if (!isHookName(request.hook))
91
+ return { kind: 'done', out: '' };
92
+ const out = [];
93
+ const run = {
94
+ payload: parsePayload(request.raw),
95
+ env: request.env,
96
+ write: (text) => out.push(text),
97
+ withContext: async (work) => work(await context()),
98
+ inDaemon: track,
99
+ };
100
+ try {
101
+ const handle = await loadHandler(request.hook);
102
+ await withWorkerEnv(request.workerEnv, () => handle(run));
103
+ }
104
+ catch (error) {
105
+ log(`${request.hook} failed: ${error instanceof Error ? error.message : String(error)}`);
106
+ }
107
+ return { kind: 'done', out: out.join('') };
108
+ }
109
+ const isStringMap = (value) => typeof value === 'object' &&
110
+ value !== null &&
111
+ Object.values(value).every((entry) => typeof entry === 'string');
112
+ function parseRequest(line) {
113
+ let parsed;
114
+ try {
115
+ parsed = JSON.parse(line);
116
+ }
117
+ catch {
118
+ return null;
119
+ }
120
+ if (typeof parsed !== 'object' || parsed === null)
121
+ return null;
122
+ const { build, hook, raw, env, workerEnv } = parsed;
123
+ if (typeof build !== 'string' || typeof hook !== 'string' || typeof raw !== 'string')
124
+ return null;
125
+ if (!isStringMap(env))
126
+ return null;
127
+ if (workerEnv !== undefined && !isStringMap(workerEnv))
128
+ return null;
129
+ return { build, hook, raw, env, ...(workerEnv ? { workerEnv } : {}) };
130
+ }
131
+ function serve(socket) {
132
+ let buffer = '';
133
+ socket.setEncoding('utf8');
134
+ socket.on('error', () => socket.destroy());
135
+ socket.on('data', (chunk) => {
136
+ buffer += chunk;
137
+ if (buffer.length > MAX_REQUEST_BYTES) {
138
+ socket.destroy();
139
+ return;
140
+ }
141
+ const end = buffer.indexOf('\n');
142
+ if (end < 0)
143
+ return;
144
+ const line = buffer.slice(0, end);
145
+ buffer = '';
146
+ socket.pause();
147
+ running += 1;
148
+ touch();
149
+ const request = parseRequest(line);
150
+ if (!request) {
151
+ running -= 1;
152
+ socket.destroy();
153
+ return;
154
+ }
155
+ void answer(request)
156
+ .then((reply) => socket.end(`${JSON.stringify(reply)}\n`))
157
+ .catch((error) => {
158
+ log(`request failed: ${String(error)}`);
159
+ socket.destroy();
160
+ })
161
+ .finally(() => {
162
+ running -= 1;
163
+ touch();
164
+ });
165
+ });
166
+ }
167
+ async function shutdown(reason) {
168
+ if (closing)
169
+ return;
170
+ closing = true;
171
+ log(`stopping: ${reason}`);
172
+ clearTimeout(idleTimer);
173
+ server?.close();
174
+ await Promise.allSettled([...pending]);
175
+ if (current)
176
+ await current.ctx.then((ctx) => ctx.close()).catch(() => undefined);
177
+ process.exit(0);
178
+ }
179
+ function clearStartMarkers() {
180
+ try {
181
+ for (const name of readdirSync(CONFIG_DIR)) {
182
+ if (name.startsWith(STARTING_PREFIX))
183
+ rmSync(join(CONFIG_DIR, name), { force: true });
184
+ }
185
+ }
186
+ catch {
187
+ return;
188
+ }
189
+ }
190
+ function inUse() {
191
+ return new Promise((settle) => {
192
+ const probe = createConnection(SOCKET);
193
+ probe.on('connect', () => {
194
+ probe.destroy();
195
+ settle(true);
196
+ });
197
+ probe.on('error', () => settle(false));
198
+ });
199
+ }
200
+ async function start() {
201
+ if (await inUse()) {
202
+ log('another daemon is already running');
203
+ return;
204
+ }
205
+ if (process.platform !== 'win32')
206
+ rmSync(SOCKET, { force: true });
207
+ server = createServer(serve);
208
+ server.on('error', (error) => {
209
+ log(`could not listen: ${error.message}`);
210
+ process.exit(0);
211
+ });
212
+ server.listen(SOCKET, () => {
213
+ if (process.platform !== 'win32')
214
+ chmodSync(SOCKET, 0o600);
215
+ clearStartMarkers();
216
+ log(`listening on ${SOCKET}`);
217
+ touch();
218
+ void context()
219
+ .then((ctx) => ctx.embedder())
220
+ .catch((error) => log(`could not open the store: ${String(error)}`));
221
+ });
222
+ process.on('SIGTERM', () => void shutdown('SIGTERM'));
223
+ process.on('SIGINT', () => void shutdown('SIGINT'));
224
+ }
225
+ await start();
@@ -1,5 +1,6 @@
1
1
  import type { BudgetUsage } from '../util/daily-budget.js';
2
2
  export declare function distillUsage(now?: number): BudgetUsage;
3
+ export declare function distillBlocked(limit: number, now?: number): boolean;
3
4
  export declare function takeDistillCalls(limit: number, count: number, now?: number): boolean;
4
5
  export declare function recordDistillFailure(reason: string, now?: number): void;
5
6
  export declare function recordDistillSuccess(now?: number): void;
@@ -4,6 +4,10 @@ const BUDGET = 'distill';
4
4
  export function distillUsage(now = Date.now()) {
5
5
  return budgetUsage(BUDGET, now);
6
6
  }
7
+ export function distillBlocked(limit, now = Date.now()) {
8
+ const { used, pausedUntil } = budgetUsage(BUDGET, now);
9
+ return pausedUntil > now || used >= limit;
10
+ }
7
11
  export function takeDistillCalls(limit, count, now = Date.now()) {
8
12
  return takeBudget(BUDGET, limit, count, now);
9
13
  }
@@ -87,7 +87,7 @@ function memoryFiles(dir) {
87
87
  async function importedFacts(ctx, project) {
88
88
  const mine = [machineTag(), originTag(project)];
89
89
  const rows = [];
90
- for (const owner of [project, youScope()]) {
90
+ for (const owner of [project, youScope(project)]) {
91
91
  const batch = await ctx.store.list({
92
92
  project: owner,
93
93
  sessionId: FACT_SESSION,
@@ -62,7 +62,7 @@ async function distillWindow(ctx, project, chat, source, at, run) {
62
62
  const retiring = ops
63
63
  .filter((op) => op.op === 'retire' && !kept.has(op.key))
64
64
  .flatMap((op) => existing.filter((fact) => fact.key === op.key))
65
- .map((fact) => factId(fact.scope === 'you' ? youScope() : project, fact.key));
65
+ .map((fact) => factId(fact.scope === 'you' ? youScope(project) : project, fact.key));
66
66
  const retired = await ctx.store.markReplaced(retiring);
67
67
  return { ok: true, set: facts.length, retired, texts: sets.map((op) => op.text) };
68
68
  }
@@ -132,5 +132,5 @@ export async function factCounts(ctx, project) {
132
132
  const scope = { project: owner, sessionId: FACT_SESSION };
133
133
  return ((await ctx.store.count(scope)) - (await ctx.store.count({ ...scope, status: 'replaced' })));
134
134
  };
135
- return { project: await count(project), you: await count(youScope()) };
135
+ return { project: await count(project), you: await count(youScope(project)) };
136
136
  }
@@ -5,7 +5,7 @@ const EXISTING_LIMIT = 80;
5
5
  export async function existingFacts(ctx, project) {
6
6
  const owners = [
7
7
  [project, 'project'],
8
- [youScope(), 'you'],
8
+ [youScope(project), 'you'],
9
9
  ];
10
10
  const facts = [];
11
11
  for (const [owner, scope] of owners) {
@@ -17,7 +17,7 @@ export interface Fact {
17
17
  source: string;
18
18
  tags?: string[];
19
19
  }
20
- export declare function youScope(): string;
20
+ export declare function youScope(project?: string): string;
21
21
  export declare function machineTag(): string;
22
22
  export declare function originTag(project: string): string;
23
23
  export declare function scopeProject(scope: FactScope, project: string): string;
@@ -1,5 +1,7 @@
1
1
  import { execFileSync } from 'node:child_process';
2
- import { hostname } from 'node:os';
2
+ import { existsSync } from 'node:fs';
3
+ import { homedir, hostname } from 'node:os';
4
+ import { isAbsolute } from 'node:path';
3
5
  import { currentAuthor, observationId } from '../capture/identity.js';
4
6
  import { redact } from '../capture/redact.js';
5
7
  import { scopeToken } from '../util/scope.js';
@@ -22,20 +24,29 @@ const LABELS = {
22
24
  todo: 'To do',
23
25
  fact: 'Fact',
24
26
  };
25
- let you = null;
26
- export function youScope() {
27
- if (you === null) {
27
+ const you = new Map();
28
+ function folderOf(project) {
29
+ if (project && isAbsolute(project) && existsSync(project))
30
+ return project;
31
+ return homedir();
32
+ }
33
+ export function youScope(project) {
34
+ const folder = folderOf(project);
35
+ let scope = you.get(folder);
36
+ if (scope === undefined) {
28
37
  let email = '';
29
38
  try {
30
39
  email = execFileSync('git', ['config', '--get', 'user.email'], {
40
+ cwd: folder,
31
41
  encoding: 'utf8',
32
42
  stdio: ['ignore', 'pipe', 'ignore'],
33
43
  }).trim();
34
44
  }
35
45
  catch { }
36
- you = `@you:${email || currentAuthor() || 'unknown'}`;
46
+ scope = `@you:${email || currentAuthor() || 'unknown'}`;
47
+ you.set(folder, scope);
37
48
  }
38
- return you;
49
+ return scope;
39
50
  }
40
51
  export function machineTag() {
41
52
  return `machine:${scopeToken(hostname())}`;
@@ -44,7 +55,7 @@ export function originTag(project) {
44
55
  return `origin:${scopeToken(project)}`;
45
56
  }
46
57
  export function scopeProject(scope, project) {
47
- return scope === 'you' ? youScope() : project;
58
+ return scope === 'you' ? youScope(project) : project;
48
59
  }
49
60
  export function factId(owner, key) {
50
61
  return observationId(FACT_SESSION, 0, `${owner}\0${key}`);
@@ -14,7 +14,7 @@ const ORDER = ['rule', 'decision', 'deadend', 'fact'];
14
14
  export async function startFacts(ctx, project, hidden = new Set(), current) {
15
15
  const visible = (obs) => obs.status !== 'replaced' && !hidden.has(obs.id) && !seenByClaude(obs, project);
16
16
  const list = async (owner, sessionId, limit) => (await ctx.store.list({ project: owner, sessionId, newest: true, limit, bodies: false })).filter(visible);
17
- const you = onePerKey(await list(youScope(), FACT_SESSION, 50));
17
+ const you = onePerKey(await list(youScope(project), FACT_SESSION, 50));
18
18
  const facts = onePerKey(await list(project, FACT_SESSION, 300));
19
19
  const handoff = await newestHandoff(ctx, project);
20
20
  const shownHandoff = handoff && visible(handoff) ? handoff : null;
@@ -1,4 +1,5 @@
1
1
  import { currentHashes, saveScan, scanRepository } from '../scan/index.js';
2
+ import { staleFiles } from './stale.js';
2
3
  function changesOnDisk(root, stored) {
3
4
  const current = currentHashes(root);
4
5
  for (const [path, hash] of current)
@@ -10,7 +11,10 @@ function changesOnDisk(root, stored) {
10
11
  return null;
11
12
  }
12
13
  export async function refreshGraph(store, root, project) {
13
- const stored = new Map((await store.scannedFiles(project)).map((file) => [file.path, file.hash]));
14
+ const scanned = await store.scannedFiles(project);
15
+ if (scanned.length > 0 && staleFiles(root, scanned).length === 0)
16
+ return [];
17
+ const stored = new Map(scanned.map((file) => [file.path, file.hash]));
14
18
  const hashes = changesOnDisk(root, stored);
15
19
  if (!hashes)
16
20
  return [];
@@ -7,5 +7,6 @@ export interface CachedFile {
7
7
  edgesHash: string;
8
8
  }
9
9
  export declare const cacheHome: () => string;
10
+ export declare const cachePath: (project: string) => string;
10
11
  export declare function loadCache(project: string): ReadonlyMap<string, CachedFile>;
11
12
  export declare function saveCache(project: string, files: ReadonlyMap<string, CachedFile>): void;
@@ -5,7 +5,7 @@ import { scopeToken } from '../../util/scope.js';
5
5
  import { CACHE_KEY } from './files.js';
6
6
  const MEMO_IDLE_MS = 10 * 60 * 1000;
7
7
  export const cacheHome = () => process.env['CLAUDE_DB_GRAPH_CACHE'] ?? join(CONFIG_DIR, 'graph-cache');
8
- const cachePath = (project) => join(cacheHome(), `${scopeToken(project)}.json`);
8
+ export const cachePath = (project) => join(cacheHome(), `${scopeToken(project)}.json`);
9
9
  function receiverTable() {
10
10
  const list = [];
11
11
  const positions = new Map();
@@ -1,22 +1,18 @@
1
- const REMOVE_BATCH = 500;
2
- async function removeFiles(store, project, files) {
3
- for (let start = 0; start < files.length; start += REMOVE_BATCH) {
4
- const batch = files.slice(start, start + REMOVE_BATCH);
5
- if (batch.length > 0)
6
- await store.removeGraph(project, batch);
7
- }
8
- }
9
1
  export async function saveScan(store, project, scan, force = false) {
10
2
  if (force) {
11
- await store.removeGraph(project);
12
- await store.upsertGraph({ symbols: scan.symbols, edges: scan.edges, files: scan.files });
3
+ await store.replaceGraph(project, null, {
4
+ symbols: scan.symbols,
5
+ edges: scan.edges,
6
+ files: scan.files,
7
+ });
13
8
  return;
14
9
  }
15
- await removeFiles(store, project, [...scan.removed, ...scan.rewrite]);
16
10
  const rewrite = new Set(scan.rewrite);
17
- await store.upsertGraph({
18
- symbols: scan.symbols.filter((symbol) => rewrite.has(symbol.file)),
19
- edges: scan.edges.filter((edge) => rewrite.has(edge.file)),
20
- files: scan.files.filter((file) => rewrite.has(file.path)),
21
- });
11
+ const symbols = scan.symbols.filter((symbol) => rewrite.has(symbol.file));
12
+ const edges = scan.edges.filter((edge) => rewrite.has(edge.file));
13
+ const files = scan.files.filter((file) => rewrite.has(file.path));
14
+ const replaced = [...scan.removed, ...scan.rewrite];
15
+ if (replaced.length === 0 && symbols.length + edges.length + files.length === 0)
16
+ return;
17
+ await store.replaceGraph(project, replaced, { symbols, edges, files });
22
18
  }
@@ -1,8 +1,9 @@
1
1
  import type { RecallContext } from '../context.js';
2
2
  import type { Observation } from '../types.js';
3
+ import type { HookEnv } from './payload.js';
3
4
  export interface EarlierInChat {
4
5
  block: string;
5
6
  ids: ReadonlySet<string>;
6
7
  }
7
- export declare function recoverAfterCompact(ctx: RecallContext, project: string, sessionId: string, transcriptPath: string | undefined): Promise<EarlierInChat | null>;
8
+ export declare function recoverAfterCompact(ctx: RecallContext, project: string, sessionId: string, transcriptPath: string | undefined, env?: HookEnv): Promise<EarlierInChat | null>;
8
9
  export declare function renderEarlierInChat(newestFirst: Observation[]): EarlierInChat | null;
@@ -9,9 +9,9 @@ const LABELS = {
9
9
  deadend: 'Dead end',
10
10
  preference: 'Rule',
11
11
  };
12
- export async function recoverAfterCompact(ctx, project, sessionId, transcriptPath) {
12
+ export async function recoverAfterCompact(ctx, project, sessionId, transcriptPath, env = process.env) {
13
13
  forgetShown(sessionId);
14
- if (!capturingDisabled(ctx.config.capture.scripted)) {
14
+ if (!capturingDisabled(ctx.config.capture.scripted, env)) {
15
15
  await flushSession(ctx, sessionId, project, transcriptPath);
16
16
  }
17
17
  const observations = await ctx.store.list({
@@ -1,5 +1,6 @@
1
1
  export declare const CLI: string;
2
2
  export declare const PICK_WORKER: string;
3
3
  export declare const TURN_WORKER: string;
4
+ export declare function withWorkerEnv<T>(env: NodeJS.ProcessEnv | undefined, work: () => T): T;
4
5
  export declare const backgroundLog: () => string;
5
6
  export declare function runDetached(script: string, args: string[], cwd: string, release: () => void): void;