claude-db 0.13.0 → 0.14.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 (105) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/dist/capture/cursors.d.ts +1 -3
  3. package/dist/capture/cursors.js +16 -35
  4. package/dist/capture/flush.d.ts +1 -3
  5. package/dist/capture/flush.js +34 -34
  6. package/dist/capture/progress.js +4 -2
  7. package/dist/capture/transcript.d.ts +1 -1
  8. package/dist/capture/transcript.js +6 -6
  9. package/dist/catchup/gather.js +1 -1
  10. package/dist/cli/commands/catchup.js +2 -1
  11. package/dist/cli/commands/graph.js +9 -10
  12. package/dist/cli/commands/insight.js +10 -1
  13. package/dist/cli/commands/memory.js +2 -1
  14. package/dist/cli/commands/setup.js +1 -2
  15. package/dist/cli/commands/sync.js +4 -1
  16. package/dist/cli/install.js +1 -2
  17. package/dist/cli/instructions.d.ts +1 -3
  18. package/dist/cli/instructions.js +5 -40
  19. package/dist/cli/refresh.js +2 -6
  20. package/dist/config/load.js +2 -1
  21. package/dist/config/schema.d.ts +0 -2
  22. package/dist/config/schema.js +0 -2
  23. package/dist/context.js +16 -7
  24. package/dist/daemon/client.d.ts +5 -3
  25. package/dist/daemon/client.js +43 -25
  26. package/dist/daemon/paths.d.ts +3 -1
  27. package/dist/daemon/pid.d.ts +3 -0
  28. package/dist/daemon/pid.js +20 -0
  29. package/dist/daemon/server.js +9 -7
  30. package/dist/embed/local.js +4 -1
  31. package/dist/facts/claude-memory.js +2 -2
  32. package/dist/facts/last-chat.d.ts +1 -1
  33. package/dist/facts/last-chat.js +5 -4
  34. package/dist/facts/model.d.ts +1 -1
  35. package/dist/facts/model.js +20 -16
  36. package/dist/facts/start-render.d.ts +13 -0
  37. package/dist/facts/start-render.js +43 -0
  38. package/dist/facts/start.d.ts +3 -6
  39. package/dist/facts/start.js +32 -46
  40. package/dist/graph/grammars.js +2 -1
  41. package/dist/graph/index.d.ts +1 -1
  42. package/dist/graph/index.js +1 -1
  43. package/dist/graph/query/index.d.ts +1 -0
  44. package/dist/graph/query/index.js +1 -0
  45. package/dist/graph/query/stale.js +19 -2
  46. package/dist/graph/query/usages.d.ts +12 -0
  47. package/dist/graph/query/usages.js +37 -0
  48. package/dist/graph/scan/cache.js +2 -1
  49. package/dist/graph/scan/files.js +2 -2
  50. package/dist/hooks/compact.d.ts +1 -0
  51. package/dist/hooks/compact.js +3 -2
  52. package/dist/hooks/detached.d.ts +0 -1
  53. package/dist/hooks/detached.js +2 -6
  54. package/dist/hooks/grep-symbols.d.ts +3 -1
  55. package/dist/hooks/grep-symbols.js +41 -2
  56. package/dist/hooks/handlers/prefer-usages.js +15 -10
  57. package/dist/hooks/handlers/session-end.js +4 -6
  58. package/dist/hooks/handlers/session-start.js +19 -40
  59. package/dist/hooks/handlers/turn-end.js +1 -1
  60. package/dist/hooks/handlers/user-prompt.js +5 -5
  61. package/dist/hooks/local.js +0 -1
  62. package/dist/hooks/payload.js +6 -5
  63. package/dist/hooks/pick-deliver.js +2 -1
  64. package/dist/hooks/prompt-recall.d.ts +4 -2
  65. package/dist/hooks/prompt-recall.js +10 -10
  66. package/dist/hooks/route.js +8 -3
  67. package/dist/hooks/run.d.ts +1 -2
  68. package/dist/mcp/tools/ids.d.ts +3 -0
  69. package/dist/mcp/tools/ids.js +6 -0
  70. package/dist/mcp/tools/memory.js +2 -1
  71. package/dist/mcp/tools/observations.js +3 -2
  72. package/dist/mcp/tools/search.js +4 -9
  73. package/dist/mcp/tools/usages.js +18 -44
  74. package/dist/pick/worker.js +1 -2
  75. package/dist/search/service.d.ts +5 -0
  76. package/dist/search/service.js +11 -5
  77. package/dist/store/postgres/filters.d.ts +1 -0
  78. package/dist/store/postgres/filters.js +3 -0
  79. package/dist/store/postgres/graph.js +3 -2
  80. package/dist/store/postgres/index.js +3 -1
  81. package/dist/store/postgres/meta.d.ts +1 -0
  82. package/dist/store/postgres/meta.js +4 -0
  83. package/dist/store/postgres/observations.js +3 -3
  84. package/dist/store/project-resolver.d.ts +1 -0
  85. package/dist/store/project-resolver.js +1 -1
  86. package/dist/store/scoped-store.js +3 -1
  87. package/dist/store/sqlite/filters.d.ts +1 -0
  88. package/dist/store/sqlite/filters.js +6 -2
  89. package/dist/store/sqlite/graph.js +3 -2
  90. package/dist/store/sqlite/observations.js +6 -7
  91. package/dist/util/claude-binary.js +5 -4
  92. package/dist/util/claude-cli.js +2 -1
  93. package/dist/util/claude-dir.d.ts +2 -0
  94. package/dist/util/claude-dir.js +8 -0
  95. package/dist/util/env.d.ts +2 -0
  96. package/dist/util/env.js +6 -0
  97. package/dist/util/shortid.d.ts +3 -0
  98. package/dist/util/shortid.js +11 -0
  99. package/docs/how-it-works.md +5 -10
  100. package/docs/setup-guide.md +1 -1
  101. package/package.json +7 -6
  102. package/dist/capture/flush-lock.d.ts +0 -11
  103. package/dist/capture/flush-lock.js +0 -108
  104. package/dist/hooks/start-legacy.d.ts +0 -6
  105. package/dist/hooks/start-legacy.js +0 -47
@@ -26,7 +26,6 @@ export const ConfigSchema = z.object({
26
26
  .prefault({}),
27
27
  capture: z
28
28
  .object({
29
- tools: z.array(z.string()).default(['Edit', 'Write', 'Bash', 'NotebookEdit']),
30
29
  exclude: z.array(z.string()).default(['.env', 'secrets', 'node_modules', '.git/']),
31
30
  maxBodyChars: z.number().int().positive().default(4000),
32
31
  scripted: z.boolean().default(false),
@@ -51,7 +50,6 @@ export const ConfigSchema = z.object({
51
50
  .prefault({}),
52
51
  inject: z
53
52
  .object({
54
- sessions: z.number().int().nonnegative().default(5),
55
53
  maxChars: z.number().int().positive().default(6000),
56
54
  perPrompt: z.boolean().default(true),
57
55
  promptResults: z.number().int().positive().max(10).default(2),
package/dist/context.js CHANGED
@@ -4,6 +4,7 @@ import { SearchService } from './search/index.js';
4
4
  import { createStore } from './store/index.js';
5
5
  import { createScopeResolver } from './store/project-resolver.js';
6
6
  import { withProjectScope } from './store/scoped-store.js';
7
+ const RETRY_LOAD_MS = 60_000;
7
8
  export async function createContext(overrides) {
8
9
  const config = { ...loadConfig(), ...overrides };
9
10
  const raw = await createStore(config.database);
@@ -17,16 +18,24 @@ export function embedderWithDeadline(load, timeoutMs, now = Date.now) {
17
18
  let loading = null;
18
19
  let ready = null;
19
20
  let deadline = 0;
21
+ let failedAt = null;
22
+ const startLoading = () => {
23
+ deadline = now() + timeoutMs;
24
+ failedAt = null;
25
+ return load().then((embedder) => {
26
+ ready = embedder;
27
+ return embedder;
28
+ }, () => {
29
+ failedAt = now();
30
+ return new NoopEmbedder();
31
+ });
32
+ };
20
33
  return () => {
21
34
  if (ready)
22
35
  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
- }
36
+ if (failedAt !== null && now() - failedAt >= RETRY_LOAD_MS)
37
+ loading = null;
38
+ loading ??= startLoading();
30
39
  if (timeoutMs <= 0)
31
40
  return loading;
32
41
  return withTimeout(loading, deadline - now());
@@ -2,11 +2,13 @@ import type { DaemonReply, DaemonRequest } from './paths.js';
2
2
  export declare const STARTING_PREFIX = "daemon.starting.";
3
3
  export type Asked = DaemonReply | {
4
4
  kind: 'unreachable';
5
+ } | {
6
+ kind: 'silent';
5
7
  } | {
6
8
  kind: 'timeout';
7
9
  };
8
10
  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>;
11
+ export declare function envForDaemon(env?: NodeJS.ProcessEnv): Record<string, string>;
12
+ export declare function askDaemon(request: DaemonRequest, replyMs: number): Promise<Asked>;
12
13
  export declare function startDaemon(now?: number): boolean;
14
+ export declare function replaceDaemon(asked: number | null): void;
@@ -4,12 +4,14 @@ import { homedir } from 'node:os';
4
4
  import { join } from 'node:path';
5
5
  import { CONFIG_DIR } from '../config/dir.js';
6
6
  import { runDetached } from '../hooks/detached.js';
7
+ import { hookEnv } from '../util/env.js';
8
+ import { clearDaemonPid, readDaemonPid } from './pid.js';
7
9
  import { DAEMON_SCRIPT, socketPath } from './paths.js';
8
- const FORWARDED = ['CLAUDE_DB_CAPTURE', 'CLAUDE_CODE_ENTRYPOINT', 'CLAUDE_DB_USAGES_HOOK'];
9
10
  const OWN_SETUP = ['CLAUDE_DB_URL', 'CLAUDE_DB_GRAPH_CACHE'];
10
11
  const STARTING_FOR_MS = 15_000;
12
+ const ACK_MS = 2_000;
11
13
  export const STARTING_PREFIX = 'daemon.starting.';
12
- export function daemonWanted(env = process.env) {
14
+ export function daemonWanted(env = hookEnv()) {
13
15
  if (OWN_SETUP.some((name) => env[name]))
14
16
  return false;
15
17
  const forced = env['CLAUDE_DB_DAEMON'];
@@ -25,53 +27,54 @@ export function daemonWanted(env = process.env) {
25
27
  return false;
26
28
  }
27
29
  }
28
- export function forwardedEnv(env = process.env) {
30
+ export function envForDaemon(env = hookEnv()) {
29
31
  const picked = {};
30
- for (const name of FORWARDED) {
31
- const value = env[name];
32
+ for (const [name, value] of Object.entries(env)) {
32
33
  if (value !== undefined)
33
34
  picked[name] = value;
34
35
  }
35
36
  return picked;
36
37
  }
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;
38
+ function parseMessage(line) {
39
+ try {
40
+ return JSON.parse(line);
41
+ }
42
+ catch {
43
+ return null;
42
44
  }
43
- return picked;
44
45
  }
45
- export function askDaemon(request, timeoutMs) {
46
+ export function askDaemon(request, replyMs) {
46
47
  return new Promise((settle) => {
47
48
  let sent = false;
48
- let reply = '';
49
+ let buffer = '';
49
50
  const socket = createConnection(socketPath());
50
51
  const finish = (asked) => {
51
52
  clearTimeout(timer);
52
53
  socket.destroy();
53
54
  settle(asked);
54
55
  };
55
- const timer = setTimeout(() => finish(sent ? { kind: 'timeout' } : { kind: 'unreachable' }), timeoutMs);
56
+ const failed = () => (sent ? { kind: 'timeout' } : { kind: 'unreachable' });
57
+ let timer = setTimeout(() => finish({ kind: 'silent' }), ACK_MS);
56
58
  socket.setEncoding('utf8');
57
59
  socket.on('connect', () => {
58
60
  sent = true;
59
61
  socket.write(`${JSON.stringify(request)}\n`);
60
62
  });
61
63
  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' });
64
+ buffer += chunk;
65
+ for (let end = buffer.indexOf('\n'); end >= 0; end = buffer.indexOf('\n')) {
66
+ const message = parseMessage(buffer.slice(0, end));
67
+ buffer = buffer.slice(end + 1);
68
+ if (message?.kind !== 'ack') {
69
+ finish(message ?? { kind: 'timeout' });
70
+ return;
71
+ }
72
+ clearTimeout(timer);
73
+ timer = setTimeout(() => finish({ kind: 'timeout' }), replyMs);
71
74
  }
72
75
  });
73
- socket.on('error', () => finish(sent ? { kind: 'timeout' } : { kind: 'unreachable' }));
74
- socket.on('close', () => finish(sent ? { kind: 'timeout' } : { kind: 'unreachable' }));
76
+ socket.on('error', () => finish(failed()));
77
+ socket.on('close', () => finish(failed()));
75
78
  });
76
79
  }
77
80
  export function startDaemon(now = Date.now()) {
@@ -86,3 +89,18 @@ export function startDaemon(now = Date.now()) {
86
89
  runDetached(DAEMON_SCRIPT, [], homedir(), () => rmSync(marker, { force: true }));
87
90
  return true;
88
91
  }
92
+ export function replaceDaemon(asked) {
93
+ if (asked !== null) {
94
+ try {
95
+ process.kill(asked, 'SIGKILL');
96
+ }
97
+ catch { }
98
+ }
99
+ if (readDaemonPid() === asked) {
100
+ if (asked !== null)
101
+ clearDaemonPid(asked);
102
+ if (process.platform !== 'win32')
103
+ rmSync(socketPath(), { force: true });
104
+ }
105
+ startDaemon();
106
+ }
@@ -6,7 +6,6 @@ export interface DaemonRequest {
6
6
  hook: string;
7
7
  raw: string;
8
8
  env: Record<string, string>;
9
- workerEnv?: Record<string, string>;
10
9
  }
11
10
  export type DaemonReply = {
12
11
  kind: 'done';
@@ -14,3 +13,6 @@ export type DaemonReply = {
14
13
  } | {
15
14
  kind: 'stale';
16
15
  };
16
+ export type DaemonMessage = DaemonReply | {
17
+ kind: 'ack';
18
+ };
@@ -0,0 +1,3 @@
1
+ export declare function readDaemonPid(): number | null;
2
+ export declare function writeDaemonPid(): void;
3
+ export declare function clearDaemonPid(pid: number): void;
@@ -0,0 +1,20 @@
1
+ import { readFileSync, rmSync, writeFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { CONFIG_DIR } from '../config/dir.js';
4
+ const pidPath = () => join(CONFIG_DIR, 'daemon.pid');
5
+ export function readDaemonPid() {
6
+ try {
7
+ const pid = Number(readFileSync(pidPath(), 'utf8'));
8
+ return Number.isInteger(pid) && pid > 1 ? pid : null;
9
+ }
10
+ catch {
11
+ return null;
12
+ }
13
+ }
14
+ export function writeDaemonPid() {
15
+ writeFileSync(pidPath(), String(process.pid));
16
+ }
17
+ export function clearDaemonPid(pid) {
18
+ if (readDaemonPid() === pid)
19
+ rmSync(pidPath(), { force: true });
20
+ }
@@ -6,15 +6,17 @@ import { CONFIG_DIR, CONFIG_PATH, loadConfig } from '../config/index.js';
6
6
  import { createContext } from '../context.js';
7
7
  import { parsePayload } from '../hooks/payload.js';
8
8
  import { isHookName, loadHandler } from '../hooks/run.js';
9
- import { withWorkerEnv } from '../hooks/detached.js';
9
+ import { withHookEnv } from '../util/env.js';
10
10
  import { silenceSqliteWarning } from '../util/warnings.js';
11
11
  import { buildId, socketPath } from './paths.js';
12
+ import { clearDaemonPid, writeDaemonPid } from './pid.js';
12
13
  import { STARTING_PREFIX } from './client.js';
13
14
  silenceSqliteWarning();
14
15
  const BUILD = buildId();
15
16
  const SOCKET = socketPath();
16
17
  const MAX_REQUEST_BYTES = 8 * 1024 * 1024;
17
18
  const RETIRE_AFTER_MS = 30_000;
19
+ const ACK_LINE = `${JSON.stringify({ kind: 'ack' })}\n`;
18
20
  let current = null;
19
21
  let running = 0;
20
22
  const pending = new Set();
@@ -92,14 +94,13 @@ async function answer(request) {
92
94
  const out = [];
93
95
  const run = {
94
96
  payload: parsePayload(request.raw),
95
- env: request.env,
96
97
  write: (text) => out.push(text),
97
98
  withContext: async (work) => work(await context()),
98
99
  inDaemon: track,
99
100
  };
100
101
  try {
101
102
  const handle = await loadHandler(request.hook);
102
- await withWorkerEnv(request.workerEnv, () => handle(run));
103
+ await withHookEnv(request.env, () => handle(run));
103
104
  }
104
105
  catch (error) {
105
106
  log(`${request.hook} failed: ${error instanceof Error ? error.message : String(error)}`);
@@ -119,14 +120,12 @@ function parseRequest(line) {
119
120
  }
120
121
  if (typeof parsed !== 'object' || parsed === null)
121
122
  return null;
122
- const { build, hook, raw, env, workerEnv } = parsed;
123
+ const { build, hook, raw, env } = parsed;
123
124
  if (typeof build !== 'string' || typeof hook !== 'string' || typeof raw !== 'string')
124
125
  return null;
125
126
  if (!isStringMap(env))
126
127
  return null;
127
- if (workerEnv !== undefined && !isStringMap(workerEnv))
128
- return null;
129
- return { build, hook, raw, env, ...(workerEnv ? { workerEnv } : {}) };
128
+ return { build, hook, raw, env };
130
129
  }
131
130
  function serve(socket) {
132
131
  let buffer = '';
@@ -152,6 +151,7 @@ function serve(socket) {
152
151
  socket.destroy();
153
152
  return;
154
153
  }
154
+ socket.write(ACK_LINE);
155
155
  void answer(request)
156
156
  .then((reply) => socket.end(`${JSON.stringify(reply)}\n`))
157
157
  .catch((error) => {
@@ -172,6 +172,7 @@ async function shutdown(reason) {
172
172
  clearTimeout(idleTimer);
173
173
  server?.close();
174
174
  await Promise.allSettled([...pending]);
175
+ clearDaemonPid(process.pid);
175
176
  if (current)
176
177
  await current.ctx.then((ctx) => ctx.close()).catch(() => undefined);
177
178
  process.exit(0);
@@ -212,6 +213,7 @@ async function start() {
212
213
  server.listen(SOCKET, () => {
213
214
  if (process.platform !== 'win32')
214
215
  chmodSync(SOCKET, 0o600);
216
+ writeDaemonPid();
215
217
  clearStartMarkers();
216
218
  log(`listening on ${SOCKET}`);
217
219
  touch();
@@ -24,7 +24,10 @@ export class LocalEmbedder {
24
24
  }
25
25
  transformers.env.allowLocalModels = false;
26
26
  return (await transformers.pipeline('feature-extraction', this.id));
27
- })();
27
+ })().catch((error) => {
28
+ this.pipelinePromise = null;
29
+ throw error;
30
+ });
28
31
  return this.pipelinePromise;
29
32
  }
30
33
  }
@@ -1,11 +1,11 @@
1
1
  import { readdirSync, readFileSync, statSync } from 'node:fs';
2
- import { homedir } from 'node:os';
3
2
  import { join } from 'node:path';
4
3
  import { embedObservations } from '../capture/flush.js';
4
+ import { claudeProjectsDir } from '../util/claude-dir.js';
5
5
  import { CLAUDE_MEMORY_TAG, FACT_SESSION, factToObservation, machineTag, originTag, youScope, } from './model.js';
6
6
  const MAX_TITLE = 200;
7
7
  export function claudeMemoryDir(project) {
8
- return join(homedir(), '.claude', 'projects', project.replace(/[^a-zA-Z0-9]/g, '-'), 'memory');
8
+ return join(claudeProjectsDir(), project.replace(/[^a-zA-Z0-9]/g, '-'), 'memory');
9
9
  }
10
10
  export function parseMemoryFile(text, fallbackTime) {
11
11
  const match = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/.exec(text);
@@ -3,7 +3,7 @@ import type { Observation } from '../types.js';
3
3
  export interface RecentChat {
4
4
  sessionId: string;
5
5
  at: number;
6
- summary: string | null;
6
+ summary: string[];
7
7
  turns: Observation[];
8
8
  }
9
9
  export declare function recentChats(ctx: RecallContext, project: string, current?: string): Promise<RecentChat[]>;
@@ -29,11 +29,12 @@ function toChat(sessionId, newestFirst, session) {
29
29
  }
30
30
  function chatSummary(session, at, turns) {
31
31
  if (!session?.summary || session.endedAt === undefined || session.endedAt < at)
32
- return null;
32
+ return [];
33
33
  const shown = new Set(turns.map((turn) => turn.title));
34
- if (session.summary.split(SUMMARY_PARTS).every((part) => shown.has(part)))
35
- return null;
36
- return toSnippet(session.summary, SUMMARY_CHARS) ?? null;
34
+ return session.summary
35
+ .split(SUMMARY_PARTS)
36
+ .filter((part) => !shown.has(part))
37
+ .flatMap((part) => toSnippet(part, SUMMARY_CHARS) ?? []);
37
38
  }
38
39
  export function latestRequestAt(chats, unfinished) {
39
40
  const times = [...chats.map((chat) => chat.at), ...unfinished.map((entry) => entry.askedAt)];
@@ -17,7 +17,7 @@ export interface Fact {
17
17
  source: string;
18
18
  tags?: string[];
19
19
  }
20
- export declare function youScope(project?: string): string;
20
+ export declare function youScope(project?: string, now?: () => number): 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;
@@ -4,6 +4,7 @@ import { homedir, hostname } from 'node:os';
4
4
  import { isAbsolute } from 'node:path';
5
5
  import { currentAuthor, observationId } from '../capture/identity.js';
6
6
  import { redact } from '../capture/redact.js';
7
+ import { SCOPE_TTL_MS } from '../store/project-resolver.js';
7
8
  import { scopeToken } from '../util/scope.js';
8
9
  export const FACT_SESSION = 'facts';
9
10
  export const FACT_TAG = 'fact';
@@ -30,22 +31,25 @@ function folderOf(project) {
30
31
  return project;
31
32
  return homedir();
32
33
  }
33
- export function youScope(project) {
34
- const folder = folderOf(project);
35
- let scope = you.get(folder);
36
- if (scope === undefined) {
37
- let email = '';
38
- try {
39
- email = execFileSync('git', ['config', '--get', 'user.email'], {
40
- cwd: folder,
41
- encoding: 'utf8',
42
- stdio: ['ignore', 'pipe', 'ignore'],
43
- }).trim();
44
- }
45
- catch { }
46
- scope = `@you:${email || currentAuthor() || 'unknown'}`;
47
- you.set(folder, scope);
34
+ function gitEmail(folder) {
35
+ try {
36
+ return execFileSync('git', ['config', '--get', 'user.email'], {
37
+ cwd: folder,
38
+ encoding: 'utf8',
39
+ stdio: ['ignore', 'pipe', 'ignore'],
40
+ }).trim();
41
+ }
42
+ catch {
43
+ return '';
48
44
  }
45
+ }
46
+ export function youScope(project, now = Date.now) {
47
+ const folder = folderOf(project);
48
+ const cached = you.get(folder);
49
+ if (cached && now() - cached.at < SCOPE_TTL_MS)
50
+ return cached.scope;
51
+ const scope = `@you:${gitEmail(folder) || currentAuthor() || 'unknown'}`;
52
+ you.set(folder, { at: now(), scope });
49
53
  return scope;
50
54
  }
51
55
  export function machineTag() {
@@ -82,7 +86,7 @@ export function factLabel(obs) {
82
86
  const type = factType(obs);
83
87
  if (type)
84
88
  return LABELS[type];
85
- return obs.sessionId === 'manual' ? LABELS.rule : null;
89
+ return obs.sessionId === 'manual' || obs.kind === 'preference' ? LABELS.rule : null;
86
90
  }
87
91
  export function factToObservation(fact, project) {
88
92
  const owner = scopeProject(fact.scope, project);
@@ -0,0 +1,13 @@
1
+ export interface StartFacts {
2
+ block: string;
3
+ ids: Set<string>;
4
+ }
5
+ export interface Entry {
6
+ texts: string[];
7
+ id: string | null;
8
+ line: (texts: string[]) => string;
9
+ }
10
+ export type Section = [string, Entry[]];
11
+ export declare function entry(line: string, id: string | null, text: string): Entry;
12
+ export declare function summaryEntry(parts: string[]): Entry;
13
+ export declare function render(sections: Section[], maxChars: number, earlier?: ReadonlySet<string>): StartFacts | null;
@@ -0,0 +1,43 @@
1
+ const SUMMARY_JOIN = '; ';
2
+ export function entry(line, id, text) {
3
+ return { texts: [text], id, line: () => line };
4
+ }
5
+ export function summaryEntry(parts) {
6
+ return { texts: parts, id: null, line: (kept) => `- Summary: ${kept.join(SUMMARY_JOIN)}` };
7
+ }
8
+ const textKey = (text) => text.toLowerCase().replace(/\s+/g, ' ').trim();
9
+ function unseen(texts, seen) {
10
+ const keys = texts.map(textKey);
11
+ return texts.filter((_, index) => {
12
+ const key = keys[index] ?? '';
13
+ return !seen.has(key) && keys.indexOf(key) === index;
14
+ });
15
+ }
16
+ export function render(sections, maxChars, earlier = new Set()) {
17
+ const seen = new Set([...earlier].map(textKey));
18
+ const ids = new Set();
19
+ const blocks = [];
20
+ let budget = maxChars;
21
+ for (const [heading, entries] of sections) {
22
+ const lines = [];
23
+ for (const item of entries) {
24
+ const kept = unseen(item.texts, seen);
25
+ if (kept.length > 0) {
26
+ const line = item.line(kept);
27
+ if (line.length > budget)
28
+ break;
29
+ budget -= line.length + 1;
30
+ for (const text of kept)
31
+ seen.add(textKey(text));
32
+ lines.push(line);
33
+ }
34
+ if (item.id)
35
+ ids.add(item.id);
36
+ }
37
+ if (lines.length > 0)
38
+ blocks.push([heading, ...lines].join('\n'));
39
+ }
40
+ if (blocks.length === 0)
41
+ return null;
42
+ return { block: ['<memory>', blocks.join('\n\n'), '</memory>'].join('\n'), ids };
43
+ }
@@ -1,7 +1,4 @@
1
1
  import type { RecallContext } from '../context.js';
2
- export interface StartFacts {
3
- block: string;
4
- ids: Set<string>;
5
- }
6
- export declare function startFacts(ctx: RecallContext, project: string, hidden?: ReadonlySet<string>, current?: string): Promise<StartFacts | null>;
7
- export declare function recentWorkBlock(ctx: RecallContext, project: string, current?: string, hidden?: ReadonlySet<string>): Promise<StartFacts | null>;
2
+ import type { StartFacts } from './start-render.js';
3
+ export type { StartFacts } from './start-render.js';
4
+ export declare function startFacts(ctx: RecallContext, project: string, hidden?: ReadonlySet<string>, current?: string, earlier?: ReadonlySet<string>): Promise<StartFacts | null>;
@@ -4,14 +4,16 @@ import { activeRequests } from '../capture/active.js';
4
4
  import { handoffBodyLines, handoffHeading, newestHandoff, passedNote } from './handoff.js';
5
5
  import { latestRequestAt, recentChats, turnLine } from './last-chat.js';
6
6
  import { MANUAL_SESSION, factLine, seenByClaude } from './render.js';
7
+ import { entry, render, summaryEntry } from './start-render.js';
7
8
  import { formatDay, formatDayTime } from '../util/day.js';
8
9
  import { toShortId } from '../util/shortid.js';
9
10
  const ABOUT_YOU = 6;
10
11
  const THIS_PROJECT = 10;
11
12
  const TODOS = 4;
12
13
  const UNCOMMITTED = 3;
14
+ const CHAT_RULES = 8;
13
15
  const ORDER = ['rule', 'decision', 'deadend', 'fact'];
14
- export async function startFacts(ctx, project, hidden = new Set(), current) {
16
+ export async function startFacts(ctx, project, hidden = new Set(), current, earlier = new Set()) {
15
17
  const visible = (obs) => obs.status !== 'replaced' && !hidden.has(obs.id) && !seenByClaude(obs, project);
16
18
  const list = async (owner, sessionId, limit) => (await ctx.store.list({ project: owner, sessionId, newest: true, limit, bodies: false })).filter(visible);
17
19
  const you = onePerKey(await list(youScope(project), FACT_SESSION, 50));
@@ -19,21 +21,21 @@ export async function startFacts(ctx, project, hidden = new Set(), current) {
19
21
  const handoff = await newestHandoff(ctx, project);
20
22
  const shownHandoff = handoff && visible(handoff) ? handoff : null;
21
23
  const manual = (await list(project, MANUAL_SESSION, 20)).filter((obs) => obs.id !== handoff?.id);
22
- if (you.length + facts.length + manual.length === 0 && !shownHandoff)
23
- return null;
24
+ const chatRules = await chatRulesFor(ctx, project, [...facts, ...manual], visible);
24
25
  const rank = (obs) => {
25
26
  const index = ORDER.indexOf(factType(obs) ?? 'rule');
26
27
  return index === -1 ? ORDER.length : index;
27
28
  };
28
- const known = [...manual, ...facts.filter((obs) => factType(obs) !== 'todo')].sort((a, b) => rank(a) - rank(b) || b.createdAt - a.createdAt);
29
+ const known = [...manual, ...chatRules, ...facts.filter((obs) => factType(obs) !== 'todo')].sort((a, b) => rank(a) - rank(b) || b.createdAt - a.createdAt);
29
30
  const todos = facts.filter((obs) => factType(obs) === 'todo').slice(0, TODOS);
30
31
  const uncommitted = (await openWork(ctx.store, project))
31
32
  .filter((obs) => !hidden.has(obs.id))
32
33
  .slice(0, UNCOMMITTED);
33
34
  const recent = await recentWork(ctx, project, current);
35
+ const factEntry = (obs) => entry(factLine(obs), obs.id, obs.title);
34
36
  const sections = [
35
- ['About you:', you.slice(0, ABOUT_YOU).map((obs) => [factLine(obs), obs.id])],
36
- ['This project:', known.slice(0, THIS_PROJECT).map((obs) => [factLine(obs), obs.id])],
37
+ ['About you:', you.slice(0, ABOUT_YOU).map(factEntry)],
38
+ ['This project:', known.slice(0, THIS_PROJECT).map(factEntry)],
37
39
  ...recentSections(recent, hidden),
38
40
  [
39
41
  shownHandoff
@@ -44,12 +46,25 @@ export async function startFacts(ctx, project, hidden = new Set(), current) {
44
46
  [
45
47
  'Where you stopped:',
46
48
  [
47
- ...todos.map((obs) => [factLine(obs), obs.id]),
48
- ...uncommitted.map((obs) => [`- Not committed: ${obs.title}`, null]),
49
+ ...todos.map(factEntry),
50
+ ...uncommitted.map((obs) => entry(`- Not committed: ${obs.title}`, obs.id, obs.title)),
49
51
  ],
50
52
  ],
51
53
  ];
52
- return render(sections, ctx.config.inject.maxChars);
54
+ return render(sections, ctx.config.inject.maxChars, earlier);
55
+ }
56
+ async function chatRulesFor(ctx, project, rulesAndFacts, visible) {
57
+ if (rulesAndFacts.some((obs) => obs.kind === 'preference'))
58
+ return [];
59
+ const rows = await ctx.store.list({
60
+ project,
61
+ kind: 'preference',
62
+ excludeSessions: [FACT_SESSION, MANUAL_SESSION],
63
+ newest: true,
64
+ limit: CHAT_RULES,
65
+ bodies: false,
66
+ });
67
+ return rows.filter(visible);
53
68
  }
54
69
  async function recentWork(ctx, project, current) {
55
70
  const unfinished = activeRequests(await ctx.store.projectScope(project), current);
@@ -62,50 +77,21 @@ function recentSections(recent, hidden) {
62
77
  ...recent.chats.map((chat, index) => chatSection(chat, index, hidden)),
63
78
  ];
64
79
  }
65
- export async function recentWorkBlock(ctx, project, current, hidden = new Set()) {
66
- const sections = recentSections(await recentWork(ctx, project, current), hidden);
67
- return sections.some(([, entries]) => entries.length > 0)
68
- ? render(sections, ctx.config.inject.maxChars)
69
- : null;
70
- }
71
- function render(sections, maxChars) {
72
- const lines = ['<memory>'];
73
- const ids = new Set();
74
- let budget = maxChars;
75
- for (const [heading, entries] of sections) {
76
- if (entries.length === 0)
77
- continue;
78
- if (lines.length > 1)
79
- lines.push('');
80
- lines.push(heading);
81
- for (const [line, id] of entries) {
82
- if (line.length > budget)
83
- break;
84
- budget -= line.length + 1;
85
- lines.push(line);
86
- if (id)
87
- ids.add(id);
88
- }
89
- }
90
- lines.push('</memory>');
91
- return { block: lines.join('\n'), ids };
92
- }
93
- function unfinishedSection(entry) {
80
+ function unfinishedSection(item) {
94
81
  return [
95
- `Not finished yet in another chat (${formatDayTime(entry.askedAt)}):`,
96
- [[`- asked "${entry.request}"`, null]],
82
+ `Not finished yet in another chat (${formatDayTime(item.askedAt)}):`,
83
+ [entry(`- asked "${item.request}"`, null, item.request)],
97
84
  ];
98
85
  }
99
86
  function chatSection(chat, index, hidden) {
100
87
  const heading = `${index === 0 ? 'Last chat' : 'Chat before'} (${formatDay(chat.at)}):`;
101
88
  const turns = chat.turns
102
89
  .filter((turn) => !hidden.has(turn.id))
103
- .map((turn) => [`- ${turnLine(turn)} (${toShortId(turn.id)})`, turn.id]);
104
- return [heading, chat.summary ? [[`- Summary: ${chat.summary}`, null], ...turns] : turns];
90
+ .map((turn) => entry(`- ${turnLine(turn)} (${toShortId(turn.id)})`, turn.id, turn.title));
91
+ return [heading, chat.summary.length > 0 ? [summaryEntry(chat.summary), ...turns] : turns];
105
92
  }
106
93
  function handoffEntries(handoff) {
107
- return handoffBodyLines(handoff).map((line, index) => [
108
- index === 0 ? `${line} (${toShortId(handoff.id)})` : line,
109
- index === 0 ? handoff.id : null,
110
- ]);
94
+ return handoffBodyLines(handoff).map((line, index) => index === 0
95
+ ? entry(`${line} (${toShortId(handoff.id)})`, handoff.id, line)
96
+ : entry(line, null, line));
111
97
  }
@@ -2,11 +2,12 @@ import { statSync } from 'node:fs';
2
2
  import { createRequire } from 'node:module';
3
3
  import { join } from 'node:path';
4
4
  import { CONFIG_DIR } from '../config/dir.js';
5
+ import { hookEnv } from '../util/env.js';
5
6
  export const GRAMMARS = ['go', 'java', 'kotlin', 'python', 'ruby', 'rust'];
6
7
  export const PLATFORM = `${process.platform}-${process.arch}`;
7
8
  export const PLATFORM_PACKAGE = `claude-db-grammars-${PLATFORM}`;
8
9
  const loaded = new Map();
9
- export const grammarHome = () => process.env.CLAUDE_DB_GRAMMARS ?? join(CONFIG_DIR, 'grammars');
10
+ export const grammarHome = () => hookEnv()['CLAUDE_DB_GRAMMARS'] ?? join(CONFIG_DIR, 'grammars');
10
11
  const places = () => [
11
12
  createRequire(import.meta.url),
12
13
  createRequire(join(grammarHome(), 'package.json')),
@@ -1,3 +1,3 @@
1
1
  export { LANGUAGES, languageFor, languageNames } from './languages/index.js';
2
2
  export { SCAN_VERSION, currentHashes, hashOf, saveScan, scanRepository } from './scan/index.js';
3
- export { NO_GRAPH, NO_GRAPH_FOR_PATH, addText, answerQuery, formatGraph, formatText, hasGraph, nearest, queryGraph, refreshGraph, suggestFor, } from './query/index.js';
3
+ export { NO_GRAPH, NO_GRAPH_FOR_PATH, addText, answerQuery, formatGraph, formatText, hasGraph, nearest, queryGraph, refreshGraph, suggestFor, usagesAnswer, } from './query/index.js';