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
@@ -1,26 +1,3 @@
1
1
  #!/usr/bin/env node
2
- import { activeToken } from '../capture/active.js';
3
- import { loadConfig } from '../config/index.js';
4
- import { resolveProject } from '../util/project.js';
5
- import { TURN_WORKER, runDetached } from './detached.js';
6
- import { capturingDisabled, readPayload, runHook } from './payload.js';
7
- import { queueTurnSave } from './turn-job.js';
8
- await runHook(async () => {
9
- if (capturingDisabled(loadConfig().capture.scripted))
10
- return;
11
- const payload = await readPayload();
12
- const sessionId = payload.session_id;
13
- if (!sessionId)
14
- return;
15
- const project = resolveProject(payload.cwd);
16
- const tasks = Array.isArray(payload.background_tasks) ? payload.background_tasks : [];
17
- const crons = Array.isArray(payload.session_crons) ? payload.session_crons : [];
18
- const stillWorking = crons.length > 0 || tasks.some((task) => task?.status === 'running');
19
- queueTurnSave(sessionId, {
20
- project,
21
- ...(payload.transcript_path ? { transcriptPath: payload.transcript_path } : {}),
22
- finalReply: payload.last_assistant_message ?? '',
23
- activeToken: stillWorking ? null : activeToken(sessionId),
24
- });
25
- runDetached(TURN_WORKER, [sessionId], project, () => { });
26
- });
2
+ import { routeHook } from './route.js';
3
+ await routeHook('turn-end');
@@ -1,25 +1,20 @@
1
1
  #!/usr/bin/env node
2
- import { clearActive } from '../capture/active.js';
3
- import { flushSession } from '../capture/index.js';
4
2
  import { createContext } from '../context.js';
5
3
  import { silenceSqliteWarning } from '../util/warnings.js';
6
4
  import { runHook } from './payload.js';
7
- import { takeTurnSave } from './turn-job.js';
5
+ import { saveQueuedTurn } from './save-turn.js';
8
6
  silenceSqliteWarning();
9
7
  await runHook(async () => {
10
8
  const [sessionId] = process.argv.slice(2);
11
9
  if (!sessionId)
12
10
  return;
13
- const job = takeTurnSave(sessionId);
14
- if (!job)
15
- return;
16
- const ctx = await createContext();
17
- try {
18
- await flushSession(ctx, sessionId, job.project, job.transcriptPath, false, job.finalReply);
19
- }
20
- finally {
21
- await ctx.close();
22
- }
23
- if (job.activeToken)
24
- clearActive(sessionId, job.activeToken);
11
+ await saveQueuedTurn(sessionId, async (work) => {
12
+ const ctx = await createContext();
13
+ try {
14
+ return await work(ctx);
15
+ }
16
+ finally {
17
+ await ctx.close();
18
+ }
19
+ });
25
20
  });
@@ -1,59 +1,3 @@
1
1
  #!/usr/bin/env node
2
- import { recordActive } from '../capture/active.js';
3
- import { flushSession, redact } from '../capture/index.js';
4
- import { createContext } from '../context.js';
5
- import { capturingDisabled, emitContext, readPayload, runHook } from './payload.js';
6
- import { loadConfig } from '../config/index.js';
7
- import { markShown, readShown } from './shown.js';
8
- import { promptCandidates, strictRecall, withCost } from './prompt-recall.js';
9
- import { pickInBackground } from './background.js';
10
- import { clearPick, startPick } from '../pick/pending.js';
11
- import { takePick } from '../pick/run.js';
12
- import { resolveProject } from '../util/project.js';
13
- import { silenceSqliteWarning } from '../util/warnings.js';
14
- silenceSqliteWarning();
15
- await runHook(async () => {
16
- if (capturingDisabled(loadConfig().capture.scripted))
17
- return;
18
- const payload = await readPayload();
19
- const sessionId = payload.session_id;
20
- if (!sessionId)
21
- return;
22
- const project = resolveProject(payload.cwd);
23
- const ctx = await createContext();
24
- try {
25
- await ctx.store.upsertSession({ id: sessionId, project, startedAt: Date.now() });
26
- recordActive(sessionId, project, payload.prompt ?? '');
27
- const { lastReply } = await flushSession(ctx, sessionId, project, payload.transcript_path);
28
- const candidates = await promptCandidates(ctx, {
29
- prompt: payload.prompt ?? '',
30
- project,
31
- sessionId,
32
- shown: readShown(sessionId),
33
- });
34
- if (!candidates) {
35
- clearPick(sessionId);
36
- return;
37
- }
38
- const fallback = await strictRecall(ctx, candidates);
39
- if (takePick(ctx.config)) {
40
- const token = startPick(sessionId, {
41
- project,
42
- prompt: redact(candidates.prompt),
43
- previousReply: redact(lastReply),
44
- ids: candidates.entries.map((entry) => entry.id),
45
- fallback: fallback ? { text: withCost(fallback.block), ids: fallback.ids } : null,
46
- });
47
- pickInBackground(project, sessionId, token);
48
- return;
49
- }
50
- clearPick(sessionId);
51
- if (!fallback)
52
- return;
53
- markShown(sessionId, fallback.ids);
54
- emitContext(withCost(fallback.block));
55
- }
56
- finally {
57
- await ctx.close();
58
- }
59
- });
2
+ import { routeHook } from './route.js';
3
+ await routeHook('user-prompt');
@@ -0,0 +1,3 @@
1
+ import { z } from 'zod';
2
+ export declare const observationIdSchema: z.ZodString;
3
+ export declare const observationIdsSchema: z.ZodArray<z.ZodString>;
@@ -0,0 +1,6 @@
1
+ import { z } from 'zod';
2
+ import { INVALID_ID_HINT, isObservationId } from '../../util/shortid.js';
3
+ export const observationIdSchema = z.string().refine(isObservationId, {
4
+ error: (issue) => `${JSON.stringify(issue.input)} ${INVALID_ID_HINT}`,
5
+ });
6
+ export const observationIdsSchema = z.array(observationIdSchema).min(1).max(25);
@@ -1,4 +1,5 @@
1
1
  import { z } from 'zod';
2
+ import { observationIdsSchema } from './ids.js';
2
3
  import { KINDS } from './kinds.js';
3
4
  import { remember as record } from '../../capture/index.js';
4
5
  import { toShortId } from '../../util/shortid.js';
@@ -19,7 +20,7 @@ export function register(server, ctx) {
19
20
  tags: z.array(z.string()).max(5).optional().describe('Extra tags for filtering'),
20
21
  }, (args) => remember(ctx, args));
21
22
  server.tool('forget', 'Delete observations by id, for memory that is wrong or obsolete. Ids come ' +
22
- 'from search. This cannot be undone, so confirm with the user first.', { ids: z.array(z.string()).min(1).max(25) }, (args) => forget(ctx, args));
23
+ 'from search. This cannot be undone, so confirm with the user first.', { ids: observationIdsSchema }, (args) => forget(ctx, args));
23
24
  }
24
25
  async function remember(ctx, { text, kind, project, key, tags, }) {
25
26
  const observation = await record(ctx, {
@@ -1,11 +1,12 @@
1
1
  import { z } from 'zod';
2
+ import { observationIdSchema, observationIdsSchema } from './ids.js';
2
3
  import { renderFull, renderIndex } from '../render.js';
3
4
  export function register(server, ctx) {
4
5
  server.tool('timeline', 'Given one observation id from search, show what happened immediately before ' +
5
6
  'and after it. Use it when a single result is not enough to explain itself ' +
6
7
  'and you need the session around it — what led to a decision and what came ' +
7
8
  'of it. Ids are the short ids search prints.', {
8
- observation_id: z.string(),
9
+ observation_id: observationIdSchema,
9
10
  before: z.number().int().min(0).max(20).default(5),
10
11
  after: z.number().int().min(0).max(20).default(5),
11
12
  }, (args) => timeline(ctx, args));
@@ -13,7 +14,7 @@ export function register(server, ctx) {
13
14
  'ids it printed. Use it whenever a search snippet looks like it answers your ' +
14
15
  'question but is cut off — the snippet is one line, the body is the reasoning. ' +
15
16
  'Always batch every id you need into one call rather than calling repeatedly.', {
16
- ids: z.array(z.string()).min(1).max(25),
17
+ ids: observationIdsSchema,
17
18
  chars: z
18
19
  .number()
19
20
  .int()
@@ -3,15 +3,10 @@ import { KINDS } from './kinds.js';
3
3
  import { renderIndex } from '../render.js';
4
4
  import { resolveProject } from '../../util/project.js';
5
5
  export function register(server, ctx) {
6
- server.tool('search', 'USE THIS BEFORE re-deriving anything this project already decided — before ' +
7
- 'reading git history to reconstruct why code is the way it is, before saying ' +
8
- 'you lack context, and before asking the user to re-explain a past decision ' +
9
- 'or a failed approach. This answers WHY: decisions, dead ends, bugs and the ' +
10
- 'reasoning behind them, which the source code cannot tell you. Returns a ' +
11
- 'compact index (id, kind, date, title, and a line of the matching body) ' +
12
- 'without full bodies; use the snippet to decide which ids are worth passing ' +
13
- 'to get_observations rather than expanding on the title alone. For a code ' +
14
- 'symbol use find_usages instead.', {
6
+ server.tool('search', 'Answers WHY: decisions, dead ends, bugs and the reasoning behind them, which the ' +
7
+ 'source code cannot tell you. Returns a compact index (id, kind, date, title, a line ' +
8
+ 'of the matching body) without full bodies; pick the ids worth passing to ' +
9
+ 'get_observations from the snippet. For a code symbol use find_usages instead.', {
15
10
  query: z.string().describe('Natural language description of what you need'),
16
11
  project: z
17
12
  .string()
@@ -1,61 +1,43 @@
1
1
  import { z } from 'zod';
2
- import { findUsages as textUsages, formatUsages, repoRootFor } from '../../usages/index.js';
3
- import { NO_GRAPH, NO_GRAPH_FOR_PATH, answerQuery, formatGraph, hasGraph, suggestFor, } from '../../graph/index.js';
2
+ import { findUsages as textUsages, formatUsages } from '../../usages/index.js';
3
+ import { NO_GRAPH, NO_GRAPH_FOR_PATH, hasGraph, suggestFor, usagesAnswer, } from '../../graph/index.js';
4
4
  import { resolveProject } from '../../util/project.js';
5
5
  export function register(server, ctx) {
6
- server.tool('find_usages', 'USE THIS INSTEAD OF grep/rg WHENEVER THE THING YOU ARE LOOKING UP IS A CODE ' +
7
- 'SYMBOL — a function, class, method, type, interface, constant or any ' +
8
- 'identifier — and ALWAYS before editing, renaming or deleting one, because ' +
9
- 'grep cannot tell a call from an import from an inherit and will not show ' +
10
- 'you the blast radius. Reach for grep only for what is genuinely text: ' +
11
- 'plain prose, comments, log output, string literals, multi-pattern regex, ' +
12
- 'or the scoping flags this tool does not expose. Modes: "usages" (default) ' +
13
- 'lists what references the symbol, with the relation on each line, then the ' +
14
- 'grep lines the graph could not link; "explain" adds what the symbol reaches, ' +
15
- '"path" traces how two symbols connect (pass the second as `target`), and ' +
16
- '"text" is a plain live `git grep`. Graph modes re-parse anything that ' +
17
- 'changed before replying, and answer from text until `claude-db scan` has ' +
18
- 'run. Every edge is tagged EXTRACTED (read literally from the syntax) or ' +
19
- 'INFERRED (matched by name across files, with a score). A miss suggests near ' +
20
- 'names, so a half-remembered symbol is still worth asking about. Use `search` ' +
21
- 'instead for "why is this the way it is" (history).', {
22
- symbol: z
23
- .string()
24
- .min(1)
25
- .max(200)
26
- .describe('Exact name to search for, e.g. "useAuth" or "CartButton"'),
6
+ server.tool('find_usages', 'Look up a code symbol (function, class, method, type, constant): what references it ' +
7
+ 'and how. Use it before editing, renaming or deleting one; use grep for plain text, ' +
8
+ 'comments, logs and strings. Modes: "usages" (default) lists references with their ' +
9
+ 'relation, then grep lines the graph could not link; "explain" adds what the symbol ' +
10
+ 'reaches; "path" shows how `symbol` connects to `target`; "text" is a live git grep. ' +
11
+ 'Edges are EXTRACTED (from syntax) or INFERRED (matched by name, scored). A miss ' +
12
+ 'suggests near names.', {
13
+ symbol: z.string().min(1).max(200).describe('Exact name, e.g. "useAuth"'),
27
14
  mode: z
28
15
  .enum(['text', 'usages', 'explain', 'path'])
29
16
  .default('usages')
30
- .describe('How to answer. "usages" (default) uses the code graph plus grep; "text" ' +
31
- 'greps live only'),
32
- target: z.string().optional().describe('The second symbol, for mode "path" only'),
17
+ .describe('Default "usages"'),
18
+ target: z.string().optional().describe('Second symbol, for mode "path"'),
33
19
  path: z
34
20
  .string()
35
21
  .optional()
36
- .describe('A file or directory to narrow the search to; omit to search the whole ' +
37
- 'repository (never narrows on its own — a blast-radius check that quietly ' +
38
- 'skipped part of the repo would be worse than no check). Must be inside a ' +
39
- 'git working tree. Different from the memory tools’ project param, which ' +
40
- 'can point at a folder pooling several repos rather than being one itself'),
22
+ .describe('A file or folder inside a git working tree; omit for the whole repository'),
41
23
  regex: z
42
24
  .boolean()
43
25
  .default(false)
44
- .describe('Treat symbol as an extended regular expression instead of a literal name'),
26
+ .describe('Treat symbol as a regular expression (mode "text" only)'),
45
27
  context: z
46
28
  .number()
47
29
  .int()
48
30
  .min(0)
49
31
  .max(10)
50
32
  .default(0)
51
- .describe('Lines of surrounding context before/after each match'),
33
+ .describe('Lines of context around each match (mode "text" only)'),
52
34
  limit: z
53
35
  .number()
54
36
  .int()
55
37
  .min(1)
56
38
  .max(500)
57
39
  .default(100)
58
- .describe('Cap on matching lines returned; the result says how many more exist'),
40
+ .describe('Cap on matching lines; the result says how many more exist'),
59
41
  }, (args) => find_usages(ctx, args));
60
42
  }
61
43
  const reply = (text) => ({ content: [{ type: 'text', text }] });
@@ -76,14 +58,6 @@ async function find_usages(ctx, args) {
76
58
  return reply(NO_GRAPH_FOR_PATH);
77
59
  return reply(`${NO_GRAPH}\n${await textAnswer(ctx, args)}`);
78
60
  }
79
- const root = repoRootFor(args.path ?? process.cwd());
80
- const { mode, symbol, target, limit } = args;
81
- const answer = await answerQuery({
82
- store: ctx.store,
83
- root,
84
- project,
85
- query: { mode, symbol, ...(target ? { target } : {}), limit },
86
- refresh: true,
87
- });
88
- return reply(formatGraph(answer, root));
61
+ const { mode, symbol, target, limit, path } = args;
62
+ return reply(await usagesAnswer({ store: ctx.store, project, mode, symbol, target, limit, path }));
89
63
  }
@@ -1,7 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import { createContext } from '../context.js';
3
3
  import { runHook } from '../hooks/payload.js';
4
- import { withCost } from '../hooks/prompt-recall.js';
5
4
  import { silenceSqliteWarning } from '../util/warnings.js';
6
5
  import { finishPick, takeJob } from './pending.js';
7
6
  import { headlessPicker, pickMemories, recordPickFailure, recordPickSuccess } from './run.js';
@@ -24,7 +23,7 @@ await runHook(async () => {
24
23
  }
25
24
  recordPickSuccess();
26
25
  if (outcome.kind === 'picked') {
27
- finishPick(sessionId, token, { text: withCost(outcome.block), ids: outcome.ids });
26
+ finishPick(sessionId, token, { text: `${outcome.block}\n`, ids: outcome.ids });
28
27
  }
29
28
  }
30
29
  finally {
@@ -1,12 +1,18 @@
1
1
  import type { Embedder } from '../embed/index.js';
2
2
  import type { MemoryStore } from '../store/adapter.js';
3
3
  import type { Observation, ObservationIndexEntry, SearchQuery, TimelineQuery } from '../types.js';
4
+ export interface RankedCandidates {
5
+ keyword: ObservationIndexEntry[];
6
+ fused: ObservationIndexEntry[];
7
+ }
4
8
  export declare class SearchService {
5
9
  private readonly store;
6
10
  private readonly embedder;
7
11
  private readonly maxScanCandidates;
8
12
  constructor(store: MemoryStore, embedder: Embedder | (() => Promise<Embedder>), maxScanCandidates?: number);
9
13
  search(query: SearchQuery): Promise<ObservationIndexEntry[]>;
14
+ candidates(query: SearchQuery): Promise<RankedCandidates>;
15
+ private embed;
10
16
  private vectorSearch;
11
17
  timeline(query: TimelineQuery): Promise<ObservationIndexEntry[]>;
12
18
  getObservations(ids: string[]): Promise<Observation[]>;
@@ -1,5 +1,6 @@
1
1
  import { applyRecencyBoost, fuse } from './rank.js';
2
2
  import { meaningfulTokens } from './stopwords.js';
3
+ const WIDE_FACTOR = 3;
3
4
  export class SearchService {
4
5
  store;
5
6
  embedder;
@@ -10,26 +11,41 @@ export class SearchService {
10
11
  this.maxScanCandidates = maxScanCandidates;
11
12
  }
12
13
  async search(query) {
13
- if (meaningfulTokens(query.text).length === 0)
14
- return [];
14
+ const { fused } = await this.candidates(query);
15
+ return applyRecencyBoost(fused, query.until ?? Date.now());
16
+ }
17
+ async candidates(query) {
18
+ const keywordText = meaningfulTokens(query.text).join(' ');
19
+ if (keywordText === '')
20
+ return { keyword: [], fused: [] };
15
21
  const wide = {
16
22
  ...query,
17
- limit: query.limit * 3,
23
+ limit: query.limit * WIDE_FACTOR,
18
24
  maxScanCandidates: query.maxScanCandidates ?? this.maxScanCandidates,
19
25
  };
20
- const keyword = await this.store.searchKeyword(wide).catch(() => []);
21
- const vector = await this.vectorSearch(wide);
26
+ const embedded = this.embed(wide.text);
27
+ const keyword = await this.store.searchKeyword({ ...wide, text: keywordText }).catch(() => []);
28
+ const vector = await this.vectorSearch(wide, await embedded);
22
29
  const fused = fuse([keyword, vector].filter((r) => r.length > 0), query.limit);
23
- return applyRecencyBoost(fused, Date.now());
30
+ return { keyword, fused };
24
31
  }
25
- async vectorSearch(query) {
32
+ async embed(text) {
26
33
  try {
27
34
  const embedder = typeof this.embedder === 'function' ? await this.embedder() : this.embedder;
28
35
  if (embedder.dimensions === 0)
29
- return [];
30
- const [embedding] = await embedder.embed([query.text]);
31
- if (!embedding || embedding.length === 0)
32
- return [];
36
+ return null;
37
+ const [embedding] = await embedder.embed([text]);
38
+ return embedding && embedding.length > 0 ? { embedder, embedding } : null;
39
+ }
40
+ catch {
41
+ return null;
42
+ }
43
+ }
44
+ async vectorSearch(query, embedded) {
45
+ if (!embedded)
46
+ return [];
47
+ const { embedder, embedding } = embedded;
48
+ try {
33
49
  const candidates = await this.store.searchVector(embedding, {
34
50
  ...query,
35
51
  embedder: embedder.id,
@@ -4,6 +4,11 @@ export interface ProjectSummary {
4
4
  observations: number;
5
5
  lastActive: number;
6
6
  }
7
+ export interface GraphRows {
8
+ symbols: CodeSymbol[];
9
+ edges: CodeEdge[];
10
+ files: ScannedFile[];
11
+ }
7
12
  export interface MemoryStore {
8
13
  readonly kind: string;
9
14
  init(): Promise<void>;
@@ -30,11 +35,8 @@ export interface MemoryStore {
30
35
  timeline(query: TimelineQuery): Promise<ObservationIndexEntry[]>;
31
36
  closeObservations(ids: string[]): Promise<number>;
32
37
  markReplaced(ids: string[]): Promise<number>;
33
- upsertGraph(scan: {
34
- symbols: CodeSymbol[];
35
- edges: CodeEdge[];
36
- files: ScannedFile[];
37
- }): Promise<void>;
38
+ upsertGraph(scan: GraphRows): Promise<void>;
39
+ replaceGraph(project: string, files: string[] | null, scan: GraphRows): Promise<void>;
38
40
  findSymbols(filter: SymbolFilter): Promise<CodeSymbol[]>;
39
41
  symbolNames(project: string, limit: number): Promise<string[]>;
40
42
  findEdges(filter: EdgeFilter): Promise<CodeEdge[]>;
@@ -1,5 +1,5 @@
1
1
  import type { CodeEdge, CodeSymbol, CountFilter, EdgeFilter, ListFilter, Observation, ObservationIndexEntry, ProjectFilter, RemoveFilter, ScannedFile, SearchQuery, Session, SymbolFilter, TimelineQuery } from '../../types.js';
2
- import type { MemoryStore, ProjectSummary } from '../adapter.js';
2
+ import type { GraphRows, MemoryStore, ProjectSummary } from '../adapter.js';
3
3
  export declare class MongoStore implements MemoryStore {
4
4
  private readonly client;
5
5
  private readonly db;
@@ -36,11 +36,8 @@ export declare class MongoStore implements MemoryStore {
36
36
  markReplaced(ids: string[]): Promise<number>;
37
37
  timeline(query: TimelineQuery): Promise<ObservationIndexEntry[]>;
38
38
  hasAtlasVectorIndex(): Promise<boolean>;
39
- upsertGraph(scan: {
40
- symbols: CodeSymbol[];
41
- edges: CodeEdge[];
42
- files: ScannedFile[];
43
- }): Promise<void>;
39
+ upsertGraph(scan: GraphRows): Promise<void>;
40
+ replaceGraph(project: string, files: string[] | null, scan: GraphRows): Promise<void>;
44
41
  findSymbols(filter: SymbolFilter): Promise<CodeSymbol[]>;
45
42
  symbolNames(project: string, limit: number): Promise<string[]>;
46
43
  findEdges(filter: EdgeFilter): Promise<CodeEdge[]>;
@@ -121,6 +121,11 @@ export class MongoStore {
121
121
  async upsertGraph(scan) {
122
122
  return graphOps.upsertGraph(this.symbols, this.edges, this.scanned, scan);
123
123
  }
124
+ async replaceGraph(project, files, scan) {
125
+ if (files === null || files.length > 0)
126
+ await this.removeGraph(project, files ?? undefined);
127
+ await this.upsertGraph(scan);
128
+ }
124
129
  async findSymbols(filter) {
125
130
  return graphOps.findSymbols(this.symbols, this.edges, this.scanned, filter);
126
131
  }
@@ -7,7 +7,19 @@ import { noProjects } from '../project-scope.js';
7
7
  export async function insertObservations(collection, sessions, symbols, edges, scanned, rows) {
8
8
  if (rows.length === 0)
9
9
  return;
10
- await collection.bulkWrite(rows.map((obs) => ({
10
+ const reopening = rows.filter((obs) => obs.status === 'open').map((obs) => obs.id);
11
+ const closed = new Map();
12
+ if (reopening.length > 0) {
13
+ const docs = await collection
14
+ .find({ _id: { $in: reopening }, status: 'done' }, { projection: { files: 1 } })
15
+ .toArray();
16
+ for (const doc of docs)
17
+ closed.set(doc._id, doc.files);
18
+ }
19
+ const kept = (obs) => closed.has(obs.id) && JSON.stringify(closed.get(obs.id)) === JSON.stringify(obs.files)
20
+ ? { ...obs, status: 'done' }
21
+ : obs;
22
+ await collection.bulkWrite(rows.map(kept).map((obs) => ({
11
23
  replaceOne: {
12
24
  filter: { _id: obs.id },
13
25
  replacement: toDoc(obs),
@@ -10,10 +10,17 @@ export interface Pool {
10
10
  query(sql: string, values?: unknown[]): Promise<QueryResult>;
11
11
  connect(): Promise<PoolClient>;
12
12
  end(): Promise<void>;
13
+ on(event: 'error', listener: (error: Error) => void): unknown;
14
+ }
15
+ export interface PoolConfig {
16
+ connectionString: string;
17
+ max?: number;
18
+ idleTimeoutMillis?: number;
19
+ allowExitOnIdle?: boolean;
20
+ keepAlive?: boolean;
13
21
  }
14
22
  export interface PgModule {
15
- Pool: new (config: {
16
- connectionString: string;
17
- }) => Pool;
23
+ Pool: new (config: PoolConfig) => Pool;
18
24
  }
19
25
  export declare function importPg(): Promise<PgModule>;
26
+ export declare function openPool(uri: string): Promise<Pool>;
@@ -12,3 +12,15 @@ export async function importPg() {
12
12
  throw new Error('Postgres driver not installed. Run: npm install -g pg');
13
13
  }
14
14
  }
15
+ export async function openPool(uri) {
16
+ const pg = await importPg();
17
+ const pool = new pg.Pool({
18
+ connectionString: uri,
19
+ max: 4,
20
+ idleTimeoutMillis: 60_000,
21
+ allowExitOnIdle: true,
22
+ keepAlive: true,
23
+ });
24
+ pool.on('error', () => undefined);
25
+ return pool;
26
+ }
@@ -1,4 +1,5 @@
1
1
  import type { ProjectFilter, SearchQuery } from '../../types.js';
2
2
  export declare function projectClause(filter: ProjectFilter | undefined, values: unknown[], column?: string): string | null;
3
+ export declare function likePrefix(prefix: string): string;
3
4
  export declare function excludeSessionsClause(sessions: string[] | undefined, values: unknown[]): string | null;
4
5
  export declare function appendScope(query: SearchQuery, conditions: string[], values: unknown[]): void;
@@ -6,6 +6,9 @@ export function projectClause(filter, values, column = 'project') {
6
6
  values.push(projects);
7
7
  return `${column} = ANY($${values.length}::text[])`;
8
8
  }
9
+ export function likePrefix(prefix) {
10
+ return `${prefix.replace(/[\\%_]/g, '\\$&')}%`;
11
+ }
9
12
  export function excludeSessionsClause(sessions, values) {
10
13
  if (!sessions || sessions.length === 0)
11
14
  return null;
@@ -1,10 +1,8 @@
1
1
  import type { Pool } from './driver.js';
2
2
  import type { CodeEdge, CodeSymbol, EdgeFilter, ScannedFile, SymbolFilter } from '../../types.js';
3
- export declare function upsertGraph(pool: Pool, scan: {
4
- symbols: CodeSymbol[];
5
- edges: CodeEdge[];
6
- files: ScannedFile[];
7
- }): Promise<void>;
3
+ import type { GraphRows } from '../adapter.js';
4
+ export declare function upsertGraph(pool: Pool, scan: GraphRows): Promise<void>;
5
+ export declare function replaceGraph(pool: Pool, project: string, files: string[] | null, scan: GraphRows): Promise<void>;
8
6
  export declare function findSymbols(pool: Pool, filter: SymbolFilter): Promise<CodeSymbol[]>;
9
7
  export declare function symbolNames(pool: Pool, project: string, limit: number): Promise<string[]>;
10
8
  export declare function findEdges(pool: Pool, filter: EdgeFilter): Promise<CodeEdge[]>;