claude-db 0.12.4 → 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 (158) hide show
  1. package/CHANGELOG.md +93 -0
  2. package/dist/capture/cursors.d.ts +6 -0
  3. package/dist/capture/cursors.js +80 -0
  4. package/dist/capture/flush.d.ts +1 -3
  5. package/dist/capture/flush.js +35 -96
  6. package/dist/capture/index.d.ts +3 -3
  7. package/dist/capture/index.js +3 -3
  8. package/dist/capture/progress.js +4 -2
  9. package/dist/capture/reingest.d.ts +1 -0
  10. package/dist/capture/reingest.js +4 -1
  11. package/dist/capture/scrub.d.ts +1 -0
  12. package/dist/capture/scrub.js +4 -1
  13. package/dist/capture/transcript.d.ts +1 -1
  14. package/dist/capture/transcript.js +6 -6
  15. package/dist/capture/vectors.js +2 -1
  16. package/dist/catchup/gather.js +1 -1
  17. package/dist/cli/commands/catchup.js +2 -1
  18. package/dist/cli/commands/daemon.d.ts +1 -0
  19. package/dist/cli/commands/daemon.js +29 -0
  20. package/dist/cli/commands/distill.js +2 -1
  21. package/dist/cli/commands/graph.js +11 -11
  22. package/dist/cli/commands/insight.js +10 -1
  23. package/dist/cli/commands/maintain.js +3 -1
  24. package/dist/cli/commands/memory.js +2 -1
  25. package/dist/cli/commands/setup.js +1 -2
  26. package/dist/cli/commands/sync.js +4 -1
  27. package/dist/cli/index.js +4 -0
  28. package/dist/cli/install.js +1 -2
  29. package/dist/cli/instructions.d.ts +1 -3
  30. package/dist/cli/instructions.js +5 -40
  31. package/dist/cli/refresh.js +2 -6
  32. package/dist/cli/usage.js +1 -0
  33. package/dist/config/load.js +2 -1
  34. package/dist/config/schema.d.ts +4 -2
  35. package/dist/config/schema.js +6 -2
  36. package/dist/context.d.ts +1 -0
  37. package/dist/context.js +30 -6
  38. package/dist/daemon/client.d.ts +14 -0
  39. package/dist/daemon/client.js +106 -0
  40. package/dist/daemon/paths.d.ts +18 -0
  41. package/dist/daemon/paths.js +19 -0
  42. package/dist/daemon/pid.d.ts +3 -0
  43. package/dist/daemon/pid.js +20 -0
  44. package/dist/daemon/server.d.ts +2 -0
  45. package/dist/daemon/server.js +227 -0
  46. package/dist/embed/local.js +4 -1
  47. package/dist/facts/budget.d.ts +1 -0
  48. package/dist/facts/budget.js +4 -0
  49. package/dist/facts/claude-memory.js +3 -3
  50. package/dist/facts/distill.js +2 -2
  51. package/dist/facts/known.js +1 -1
  52. package/dist/facts/last-chat.d.ts +1 -1
  53. package/dist/facts/last-chat.js +5 -4
  54. package/dist/facts/model.d.ts +1 -1
  55. package/dist/facts/model.js +31 -16
  56. package/dist/facts/start-render.d.ts +13 -0
  57. package/dist/facts/start-render.js +43 -0
  58. package/dist/facts/start.d.ts +3 -6
  59. package/dist/facts/start.js +33 -47
  60. package/dist/graph/grammars.js +2 -1
  61. package/dist/graph/index.d.ts +1 -1
  62. package/dist/graph/index.js +1 -1
  63. package/dist/graph/query/index.d.ts +1 -0
  64. package/dist/graph/query/index.js +1 -0
  65. package/dist/graph/query/refresh.js +5 -1
  66. package/dist/graph/query/stale.js +19 -2
  67. package/dist/graph/query/usages.d.ts +12 -0
  68. package/dist/graph/query/usages.js +37 -0
  69. package/dist/graph/scan/cache.d.ts +1 -0
  70. package/dist/graph/scan/cache.js +3 -2
  71. package/dist/graph/scan/files.js +2 -2
  72. package/dist/graph/scan/persist.js +12 -16
  73. package/dist/hooks/compact.d.ts +3 -1
  74. package/dist/hooks/compact.js +4 -3
  75. package/dist/hooks/detached.js +2 -0
  76. package/dist/hooks/graph-presence.d.ts +3 -0
  77. package/dist/hooks/graph-presence.js +42 -0
  78. package/dist/hooks/grep-symbols.d.ts +3 -1
  79. package/dist/hooks/grep-symbols.js +41 -2
  80. package/dist/hooks/handlers/prefer-usages.d.ts +2 -0
  81. package/dist/hooks/handlers/prefer-usages.js +118 -0
  82. package/dist/hooks/handlers/session-end.d.ts +2 -0
  83. package/dist/hooks/handlers/session-end.js +60 -0
  84. package/dist/hooks/handlers/session-start.d.ts +2 -0
  85. package/dist/hooks/handlers/session-start.js +83 -0
  86. package/dist/hooks/handlers/turn-end.d.ts +2 -0
  87. package/dist/hooks/handlers/turn-end.js +31 -0
  88. package/dist/hooks/handlers/user-prompt.d.ts +2 -0
  89. package/dist/hooks/handlers/user-prompt.js +55 -0
  90. package/dist/hooks/local.d.ts +2 -0
  91. package/dist/hooks/local.js +22 -0
  92. package/dist/hooks/payload.d.ts +9 -4
  93. package/dist/hooks/payload.js +30 -20
  94. package/dist/hooks/pick-deliver.js +2 -1
  95. package/dist/hooks/prefer-usages.js +2 -118
  96. package/dist/hooks/prompt-recall.d.ts +4 -2
  97. package/dist/hooks/prompt-recall.js +10 -10
  98. package/dist/hooks/route.d.ts +2 -0
  99. package/dist/hooks/route.js +33 -0
  100. package/dist/hooks/run.d.ts +13 -0
  101. package/dist/hooks/run.js +12 -0
  102. package/dist/hooks/save-turn.d.ts +2 -0
  103. package/dist/hooks/save-turn.js +11 -0
  104. package/dist/hooks/session-end.js +2 -63
  105. package/dist/hooks/session-start.js +2 -90
  106. package/dist/hooks/turn-end.js +2 -25
  107. package/dist/hooks/turn-save.js +10 -15
  108. package/dist/hooks/user-prompt.js +2 -58
  109. package/dist/mcp/tools/ids.d.ts +3 -0
  110. package/dist/mcp/tools/ids.js +6 -0
  111. package/dist/mcp/tools/memory.js +2 -1
  112. package/dist/mcp/tools/observations.js +3 -2
  113. package/dist/mcp/tools/search.js +4 -9
  114. package/dist/mcp/tools/usages.js +18 -44
  115. package/dist/pick/worker.js +1 -2
  116. package/dist/search/service.d.ts +6 -0
  117. package/dist/search/service.js +27 -11
  118. package/dist/store/adapter.d.ts +7 -5
  119. package/dist/store/mongo/index.d.ts +3 -6
  120. package/dist/store/mongo/index.js +5 -0
  121. package/dist/store/mongo/observations.js +13 -1
  122. package/dist/store/postgres/driver.d.ts +10 -3
  123. package/dist/store/postgres/driver.js +12 -0
  124. package/dist/store/postgres/filters.d.ts +1 -0
  125. package/dist/store/postgres/filters.js +3 -0
  126. package/dist/store/postgres/graph.d.ts +3 -5
  127. package/dist/store/postgres/graph.js +59 -52
  128. package/dist/store/postgres/index.d.ts +3 -6
  129. package/dist/store/postgres/index.js +8 -5
  130. package/dist/store/postgres/insert.js +98 -50
  131. package/dist/store/postgres/meta.d.ts +1 -0
  132. package/dist/store/postgres/meta.js +4 -0
  133. package/dist/store/postgres/observations.js +3 -3
  134. package/dist/store/project-resolver.d.ts +4 -0
  135. package/dist/store/project-resolver.js +34 -23
  136. package/dist/store/scoped-store.js +4 -2
  137. package/dist/store/sqlite/filters.d.ts +1 -0
  138. package/dist/store/sqlite/filters.js +6 -2
  139. package/dist/store/sqlite/graph.d.ts +3 -5
  140. package/dist/store/sqlite/graph.js +45 -35
  141. package/dist/store/sqlite/index.d.ts +3 -6
  142. package/dist/store/sqlite/index.js +3 -0
  143. package/dist/store/sqlite/observations.js +12 -9
  144. package/dist/util/claude-binary.js +5 -4
  145. package/dist/util/claude-cli.js +2 -1
  146. package/dist/util/claude-dir.d.ts +2 -0
  147. package/dist/util/claude-dir.js +8 -0
  148. package/dist/util/env.d.ts +2 -0
  149. package/dist/util/env.js +6 -0
  150. package/dist/util/job-lock.d.ts +1 -0
  151. package/dist/util/job-lock.js +50 -10
  152. package/dist/util/shortid.d.ts +3 -0
  153. package/dist/util/shortid.js +11 -0
  154. package/docs/how-it-works.md +5 -10
  155. package/docs/setup-guide.md +6 -1
  156. package/package.json +7 -6
  157. package/dist/hooks/start-legacy.d.ts +0 -6
  158. package/dist/hooks/start-legacy.js +0 -47
@@ -115,7 +115,7 @@ export async function cmdMerge(argv) {
115
115
  for (const session of await ctx.store.recentSessions(from, 1000)) {
116
116
  await ctx.store.upsertSession({ ...session, project: into });
117
117
  }
118
- const swept = await ctx.store.remove({ project: from });
118
+ const swept = await ctx.store.remove({ ids: await storedUnder(ctx, from) });
119
119
  console.log(`Moved ${moved} observation(s) into ${into}.`);
120
120
  if (swept > 0)
121
121
  console.log(`Cleaned up ${swept} leftover row(s) under the old path.`);
@@ -124,6 +124,15 @@ export async function cmdMerge(argv) {
124
124
  await ctx.close();
125
125
  }
126
126
  }
127
+ async function storedUnder(ctx, name) {
128
+ const ids = [];
129
+ await eachObservation(ctx, { project: name }, (batch) => {
130
+ for (const obs of batch)
131
+ if (obs.project === name)
132
+ ids.push(obs.id);
133
+ });
134
+ return ids;
135
+ }
127
136
  async function listShards(ctx, into) {
128
137
  const shards = (await ctx.store.listProjects()).filter((entry) => entry.project !== into && entry.project.startsWith(`${into}/`));
129
138
  if (shards.length === 0) {
@@ -2,7 +2,7 @@ import { CONFIG_DIR, loadConfig } from '../../config/index.js';
2
2
  import { join } from 'node:path';
3
3
  import { checkForUpdate } from '../../update.js';
4
4
  import { createContext } from '../../context.js';
5
- import { finishReingest, finishScrub, redact, reingestProject, releaseReingest, releaseScrub, rememberedTranscripts, scrubSecrets, sweepCursors, transcriptsFor, } from '../../capture/index.js';
5
+ import { finishReingest, finishScrub, keepReingest, keepScrub, redact, reingestProject, releaseReingest, releaseScrub, rememberedTranscripts, scrubSecrets, sweepCursors, transcriptsFor, } from '../../capture/index.js';
6
6
  import { resolveProject } from '../../util/project.js';
7
7
  import { rmSync } from 'node:fs';
8
8
  export async function cmdReset(argv) {
@@ -42,6 +42,7 @@ export async function cmdFlush(argv) {
42
42
  process.exit(1);
43
43
  }
44
44
  let total;
45
+ keepReingest(project);
45
46
  try {
46
47
  total = await reingest(project, transcripts, repair);
47
48
  }
@@ -93,6 +94,7 @@ export async function cmdRedact(argv) {
93
94
  const background = argv.includes('--background');
94
95
  const ctx = await createContext();
95
96
  const database = ctx.config.database;
97
+ keepScrub(database);
96
98
  let result;
97
99
  try {
98
100
  result = await scrubSecrets(ctx);
@@ -2,7 +2,7 @@ import { basename } from 'node:path';
2
2
  import { createContext } from '../../context.js';
3
3
  import { remember } from '../../capture/index.js';
4
4
  import { resolveProject } from '../../util/project.js';
5
- import { toShortId } from '../../util/shortid.js';
5
+ import { assertValidIds, toShortId } from '../../util/shortid.js';
6
6
  import { valueOf, withoutFlags } from '../args.js';
7
7
  export async function cmdSearch(argv) {
8
8
  const all = argv.includes('--all');
@@ -67,6 +67,7 @@ export async function cmdForget(argv) {
67
67
  console.error(' claude-db forget --session <session-id>');
68
68
  process.exit(1);
69
69
  }
70
+ assertValidIds(ids);
70
71
  const ctx = await createContext();
71
72
  try {
72
73
  const doomed = await ctx.store.getObservations(ids);
@@ -3,7 +3,7 @@ import { closeLandedWork, openWork, redact } from '../../capture/index.js';
3
3
  import { CONFIG_PATH } from '../../config/index.js';
4
4
  import { DIST_DIR } from '../constants.js';
5
5
  import { assertStableLocation, install, uninstall } from '../install.js';
6
- import { instructionsPathFor, mcpPathFor, settingsPathFor } from '../paths.js';
6
+ import { mcpPathFor, settingsPathFor } from '../paths.js';
7
7
  import { createContext } from '../../context.js';
8
8
  import { join, resolve } from 'node:path';
9
9
  import { readFileSync, statSync } from 'node:fs';
@@ -38,7 +38,6 @@ export async function cmdInstall(scope) {
38
38
  await ctx.close();
39
39
  console.log(`Scope : ${scope === 'project' ? `this project only (${project})` : 'all projects on this machine'}`);
40
40
  console.log(`Settings : ${settingsPath}`);
41
- console.log(`Guidance : ${instructionsPathFor(scope, project)}`);
42
41
  printSkills(skills);
43
42
  console.log(`Config : ${CONFIG_PATH}`);
44
43
  console.log(`Database : ${redact(ctx.config.database)}`);
@@ -1,6 +1,7 @@
1
1
  import { BATCH } from '../constants.js';
2
2
  import { createContext } from '../../context.js';
3
3
  import { createStore } from '../../store/index.js';
4
+ const SYNC_SCOPE = 'Sync adds rows the other side is missing and moves status forward. Edits and deletes do not travel.';
4
5
  export async function cmdSync(argv) {
5
6
  const url = argv.find((arg) => typeof arg === 'string' && !arg.startsWith('-'));
6
7
  const confirmed = argv.includes('--yes') || argv.includes('-y');
@@ -49,7 +50,8 @@ export async function cmdSync(argv) {
49
50
  if (!confirmed) {
50
51
  console.log(`This would pull ${pulled} and push ${pushed} observation(s), ` +
51
52
  `and bring ${moved} status(es) up to date.`);
52
- console.log('\nNothing was transferred. Re-run with --yes to confirm.');
53
+ console.log(`\n${SYNC_SCOPE}`);
54
+ console.log('Nothing was transferred. Re-run with --yes to confirm.');
53
55
  return;
54
56
  }
55
57
  await applyStatus(local.store, localBehind);
@@ -57,6 +59,7 @@ export async function cmdSync(argv) {
57
59
  const sessions = await syncSessions(local.store, remote);
58
60
  console.log(`Pulled ${pulled}, pushed ${pushed}, brought ${moved} status(es) up to date, ` +
59
61
  `and reconciled ${sessions} session(s).`);
62
+ console.log(SYNC_SCOPE);
60
63
  }
61
64
  finally {
62
65
  await remote.close();
package/dist/cli/index.js CHANGED
@@ -17,6 +17,7 @@ import { cmdLanguages } from './commands/languages.js';
17
17
  import { cmdSeed } from './commands/seed.js';
18
18
  import { cmdAdoption } from './commands/adoption.js';
19
19
  import { cmdView } from './commands/view.js';
20
+ import { cmdDaemon } from './commands/daemon.js';
20
21
  import { silenceSqliteWarning } from '../util/warnings.js';
21
22
  import { packageVersion } from '../update.js';
22
23
  silenceSqliteWarning();
@@ -117,6 +118,9 @@ async function run() {
117
118
  case 'sync':
118
119
  await cmdSync(args);
119
120
  break;
121
+ case 'daemon':
122
+ await cmdDaemon(args);
123
+ break;
120
124
  case '--version':
121
125
  case '-v':
122
126
  console.log(packageVersion());
@@ -3,7 +3,7 @@ import { installSkills, removeSkills } from './skills.js';
3
3
  import { resolve } from 'node:path';
4
4
  import { execFileSync } from 'node:child_process';
5
5
  import { readJson, writeJson, writeJsonOrRemove } from './files.js';
6
- import { removeInstructions, writeInstructions } from './instructions.js';
6
+ import { removeInstructions } from './instructions.js';
7
7
  import { toPosix } from '../util/paths.js';
8
8
  const HOOKS = [
9
9
  ['SessionStart', 'session-start.js'],
@@ -75,7 +75,6 @@ export function install(distDir, scope, project) {
75
75
  delete settings['mcpServers'];
76
76
  writeJson(path, settings);
77
77
  const server = resolve(distDir, 'mcp', 'server.js');
78
- writeInstructions(instructionsPathFor(scope, project));
79
78
  const skills = installSkills(distDir, scope, project);
80
79
  if (scope === 'global' && registerViaCli(server))
81
80
  return { settingsPath: path, skills };
@@ -1,5 +1,3 @@
1
1
  export declare const BLOCK_START = "<!-- claude-db:start -->";
2
2
  export declare const BLOCK_END = "<!-- claude-db:end -->";
3
- export declare const INSTRUCTIONS: string;
4
- export declare function writeInstructions(path: string): void;
5
- export declare function removeInstructions(path: string): void;
3
+ export declare function removeInstructions(path: string): boolean;
@@ -2,52 +2,17 @@ import { readText, writeAtomic } from './files.js';
2
2
  import { rmSync } from 'node:fs';
3
3
  export const BLOCK_START = '<!-- claude-db:start -->';
4
4
  export const BLOCK_END = '<!-- claude-db:end -->';
5
- export const INSTRUCTIONS = [
6
- BLOCK_START,
7
- '## Project memory',
8
- '',
9
- 'You have persistent memory of this project: your past sessions, decisions',
10
- 'and their reasoning, served by the `memory` MCP server. Session summaries',
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.',
15
- '',
16
- 'Recall the rest *before* re-deriving something you already learned. That',
17
- 'means before grepping or reading git history to reconstruct why something is',
18
- 'the way it is, before saying you lack context, and before asking the user to',
19
- 're-explain a past decision or a failed approach: call `search`, then',
20
- '`get_observations` for the ids worth reading.',
21
- '',
22
- 'Verifying against the code afterwards is right — memory records what was',
23
- 'done, the source shows what is. Starting there is what wastes the memory.',
24
- '',
25
- '`search` covers *why*: past decisions, bugs, reasoning. `find_usages` is',
26
- 'the default the moment a question is about a symbol or identifier — before',
27
- 'editing or removing a shared or exported name, or to look one up at all —',
28
- 'since it tags the likely definition and re-derives the answer from the',
29
- 'current source on every call, so it is never stale. grep still wins for',
30
- 'everything that is not a code identifier: plain text, log files, comments,',
31
- 'strings, and scoping with grep flags `find_usages` does not expose.',
32
- '',
33
- 'When the user states a standing rule or preference, record it with',
34
- '`remember` so it survives this session.',
35
- BLOCK_END,
36
- ].join('\n');
37
- export function writeInstructions(path) {
38
- const rest = withoutBlock(readText(path)).trimEnd();
39
- const body = rest.length > 0 ? `${rest}\n\n${INSTRUCTIONS}\n` : `${INSTRUCTIONS}\n`;
40
- writeAtomic(path, body);
41
- }
42
5
  export function removeInstructions(path) {
43
6
  const existing = readText(path);
44
- if (!existing.includes(BLOCK_START))
45
- return;
46
- const rest = withoutBlock(existing).trim();
7
+ const stripped = withoutBlock(existing);
8
+ if (stripped === existing)
9
+ return false;
10
+ const rest = stripped.trim();
47
11
  if (rest.length === 0)
48
12
  rmSync(path, { force: true });
49
13
  else
50
14
  writeAtomic(path, `${rest}\n`);
15
+ return true;
51
16
  }
52
17
  function withoutBlock(text) {
53
18
  const start = text.indexOf(BLOCK_START);
@@ -1,7 +1,6 @@
1
- import { BLOCK_START, INSTRUCTIONS, writeInstructions } from './instructions.js';
1
+ import { removeInstructions } from './instructions.js';
2
2
  import { refreshHooks } from './install.js';
3
3
  import { instructionsPathFor, settingsPathFor } from './paths.js';
4
- import { readText } from './files.js';
5
4
  import { refreshSkills } from './skills.js';
6
5
  export function refreshInstalled(distDir, project) {
7
6
  const refreshed = [];
@@ -11,11 +10,8 @@ export function refreshInstalled(distDir, project) {
11
10
  refreshed.push(settingsPath);
12
11
  refreshed.push(...refreshSkills(distDir, scope, project));
13
12
  const instructionsPath = instructionsPathFor(scope, project);
14
- const existing = readText(instructionsPath);
15
- if (existing.includes(BLOCK_START) && !existing.includes(INSTRUCTIONS)) {
16
- writeInstructions(instructionsPath);
13
+ if (removeInstructions(instructionsPath))
17
14
  refreshed.push(instructionsPath);
18
- }
19
15
  }
20
16
  return refreshed;
21
17
  }
package/dist/cli/usage.js CHANGED
@@ -34,6 +34,7 @@ export function usage() {
34
34
  export [--all] > out.jsonl Dump memory as JSONL, for backup or migration
35
35
  import <file.jsonl> Load a dump back in (safe to repeat)
36
36
  sync <url> [--yes] Two-way merge with another database
37
+ daemon [on|off|start|stop] Serve hooks from one warm process; no argument shows status
37
38
  update Install a newer compatible release now
38
39
  reembed [--project] Re-embed with the current model
39
40
  prune --older-than <days> Delete old memory (dry run without --yes)
@@ -2,6 +2,7 @@ import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
2
2
  import { dirname, join } from 'node:path';
3
3
  import { CONFIG_DIR } from './dir.js';
4
4
  import { ConfigSchema } from './schema.js';
5
+ import { hookEnv } from '../util/env.js';
5
6
  export { CONFIG_DIR };
6
7
  export const CONFIG_PATH = join(CONFIG_DIR, 'config.json');
7
8
  export const DEFAULT_DB_PATH = join(CONFIG_DIR, 'memory.db');
@@ -31,7 +32,7 @@ export function loadConfig() {
31
32
  }
32
33
  catch { }
33
34
  const config = ConfigSchema.parse(dropSupersededDefaults(fileConfig));
34
- const envUrl = process.env['CLAUDE_DB_URL']?.trim();
35
+ const envUrl = hookEnv()['CLAUDE_DB_URL']?.trim();
35
36
  return {
36
37
  ...config,
37
38
  database: envUrl && envUrl.length > 0 ? envUrl : config.database || DEFAULT_DB_PATH,
@@ -17,11 +17,14 @@ export declare const ConfigSchema: z.ZodObject<{
17
17
  notify: "notify";
18
18
  off: "off";
19
19
  }>>;
20
+ daemon: z.ZodPrefault<z.ZodObject<{
21
+ enabled: z.ZodDefault<z.ZodBoolean>;
22
+ idleMinutes: z.ZodDefault<z.ZodNumber>;
23
+ }, z.core.$strip>>;
20
24
  project: z.ZodPrefault<z.ZodObject<{
21
25
  remote: z.ZodDefault<z.ZodString>;
22
26
  }, z.core.$strip>>;
23
27
  capture: z.ZodPrefault<z.ZodObject<{
24
- tools: z.ZodDefault<z.ZodArray<z.ZodString>>;
25
28
  exclude: z.ZodDefault<z.ZodArray<z.ZodString>>;
26
29
  maxBodyChars: z.ZodDefault<z.ZodNumber>;
27
30
  scripted: z.ZodDefault<z.ZodBoolean>;
@@ -55,7 +58,6 @@ export declare const ConfigSchema: z.ZodObject<{
55
58
  dailyLimit: z.ZodDefault<z.ZodNumber>;
56
59
  }, z.core.$strip>>;
57
60
  inject: z.ZodPrefault<z.ZodObject<{
58
- sessions: z.ZodDefault<z.ZodNumber>;
59
61
  maxChars: z.ZodDefault<z.ZodNumber>;
60
62
  perPrompt: z.ZodDefault<z.ZodBoolean>;
61
63
  promptResults: z.ZodDefault<z.ZodNumber>;
@@ -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
@@ -20,7 +26,6 @@ export const ConfigSchema = z.object({
20
26
  .prefault({}),
21
27
  capture: z
22
28
  .object({
23
- tools: z.array(z.string()).default(['Edit', 'Write', 'Bash', 'NotebookEdit']),
24
29
  exclude: z.array(z.string()).default(['.env', 'secrets', 'node_modules', '.git/']),
25
30
  maxBodyChars: z.number().int().positive().default(4000),
26
31
  scripted: z.boolean().default(false),
@@ -45,7 +50,6 @@ export const ConfigSchema = z.object({
45
50
  .prefault({}),
46
51
  inject: z
47
52
  .object({
48
- sessions: z.number().int().nonnegative().default(5),
49
53
  maxChars: z.number().int().positive().default(6000),
50
54
  perPrompt: z.boolean().default(true),
51
55
  promptResults: z.number().int().positive().max(10).default(2),
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
@@ -4,22 +4,46 @@ 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);
10
11
  await raw.init();
11
12
  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
- };
13
+ const embedder = embedderWithDeadline(() => createEmbedder(config.embeddings.provider), config.embeddings.timeoutMs);
17
14
  const search = new SearchService(store, embedder, config.embeddings.maxScanCandidates);
18
15
  return { config, store, embedder, search, close: () => store.close() };
19
16
  }
17
+ export function embedderWithDeadline(load, timeoutMs, now = Date.now) {
18
+ let loading = null;
19
+ let ready = null;
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
+ };
33
+ return () => {
34
+ if (ready)
35
+ return Promise.resolve(ready);
36
+ if (failedAt !== null && now() - failedAt >= RETRY_LOAD_MS)
37
+ loading = null;
38
+ loading ??= startLoading();
39
+ if (timeoutMs <= 0)
40
+ return loading;
41
+ return withTimeout(loading, deadline - now());
42
+ };
43
+ }
20
44
  async function withTimeout(pending, ms) {
21
45
  if (ms <= 0)
22
- return pending;
46
+ return Promise.race([pending, Promise.resolve(new NoopEmbedder())]);
23
47
  let timer;
24
48
  const limit = new Promise((settle) => {
25
49
  timer = setTimeout(() => settle(new NoopEmbedder()), ms);
@@ -0,0 +1,14 @@
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: 'silent';
7
+ } | {
8
+ kind: 'timeout';
9
+ };
10
+ export declare function daemonWanted(env?: NodeJS.ProcessEnv): boolean;
11
+ export declare function envForDaemon(env?: NodeJS.ProcessEnv): Record<string, string>;
12
+ export declare function askDaemon(request: DaemonRequest, replyMs: number): Promise<Asked>;
13
+ export declare function startDaemon(now?: number): boolean;
14
+ export declare function replaceDaemon(asked: number | null): void;
@@ -0,0 +1,106 @@
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 { hookEnv } from '../util/env.js';
8
+ import { clearDaemonPid, readDaemonPid } from './pid.js';
9
+ import { DAEMON_SCRIPT, socketPath } from './paths.js';
10
+ const OWN_SETUP = ['CLAUDE_DB_URL', 'CLAUDE_DB_GRAPH_CACHE'];
11
+ const STARTING_FOR_MS = 15_000;
12
+ const ACK_MS = 2_000;
13
+ export const STARTING_PREFIX = 'daemon.starting.';
14
+ export function daemonWanted(env = hookEnv()) {
15
+ if (OWN_SETUP.some((name) => env[name]))
16
+ return false;
17
+ const forced = env['CLAUDE_DB_DAEMON'];
18
+ if (forced === 'on')
19
+ return true;
20
+ if (forced === 'off')
21
+ return false;
22
+ try {
23
+ const config = JSON.parse(readFileSync(join(CONFIG_DIR, 'config.json'), 'utf8'));
24
+ return config.daemon?.enabled === true;
25
+ }
26
+ catch {
27
+ return false;
28
+ }
29
+ }
30
+ export function envForDaemon(env = hookEnv()) {
31
+ const picked = {};
32
+ for (const [name, value] of Object.entries(env)) {
33
+ if (value !== undefined)
34
+ picked[name] = value;
35
+ }
36
+ return picked;
37
+ }
38
+ function parseMessage(line) {
39
+ try {
40
+ return JSON.parse(line);
41
+ }
42
+ catch {
43
+ return null;
44
+ }
45
+ }
46
+ export function askDaemon(request, replyMs) {
47
+ return new Promise((settle) => {
48
+ let sent = false;
49
+ let buffer = '';
50
+ const socket = createConnection(socketPath());
51
+ const finish = (asked) => {
52
+ clearTimeout(timer);
53
+ socket.destroy();
54
+ settle(asked);
55
+ };
56
+ const failed = () => (sent ? { kind: 'timeout' } : { kind: 'unreachable' });
57
+ let timer = setTimeout(() => finish({ kind: 'silent' }), ACK_MS);
58
+ socket.setEncoding('utf8');
59
+ socket.on('connect', () => {
60
+ sent = true;
61
+ socket.write(`${JSON.stringify(request)}\n`);
62
+ });
63
+ socket.on('data', (chunk) => {
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);
74
+ }
75
+ });
76
+ socket.on('error', () => finish(failed()));
77
+ socket.on('close', () => finish(failed()));
78
+ });
79
+ }
80
+ export function startDaemon(now = Date.now()) {
81
+ const window = Math.floor(now / STARTING_FOR_MS);
82
+ const marker = join(CONFIG_DIR, `${STARTING_PREFIX}${window}`);
83
+ try {
84
+ writeFileSync(marker, String(now), { flag: 'wx' });
85
+ }
86
+ catch {
87
+ return false;
88
+ }
89
+ runDetached(DAEMON_SCRIPT, [], homedir(), () => rmSync(marker, { force: true }));
90
+ return true;
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
+ }
@@ -0,0 +1,18 @@
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
+ }
10
+ export type DaemonReply = {
11
+ kind: 'done';
12
+ out: string;
13
+ } | {
14
+ kind: 'stale';
15
+ };
16
+ export type DaemonMessage = DaemonReply | {
17
+ kind: 'ack';
18
+ };
@@ -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,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
+ }
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};