claude-db 0.12.3 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/dist/capture/cursors.d.ts +8 -0
  3. package/dist/capture/cursors.js +99 -0
  4. package/dist/capture/flush-lock.d.ts +11 -0
  5. package/dist/capture/flush-lock.js +108 -0
  6. package/dist/capture/flush.d.ts +4 -4
  7. package/dist/capture/flush.js +27 -60
  8. package/dist/capture/index.d.ts +3 -3
  9. package/dist/capture/index.js +3 -3
  10. package/dist/capture/progress.js +8 -1
  11. package/dist/capture/reingest.d.ts +1 -0
  12. package/dist/capture/reingest.js +4 -1
  13. package/dist/capture/scrub.d.ts +1 -0
  14. package/dist/capture/scrub.js +4 -1
  15. package/dist/capture/vectors.js +2 -1
  16. package/dist/catchup/gather.js +1 -0
  17. package/dist/cli/commands/daemon.d.ts +1 -0
  18. package/dist/cli/commands/daemon.js +29 -0
  19. package/dist/cli/commands/distill.js +2 -1
  20. package/dist/cli/commands/graph.js +2 -1
  21. package/dist/cli/commands/maintain.js +3 -1
  22. package/dist/cli/commands/sync.js +5 -4
  23. package/dist/cli/commands/transfer.js +4 -3
  24. package/dist/cli/index.js +4 -0
  25. package/dist/cli/usage.js +1 -0
  26. package/dist/config/schema.d.ts +4 -0
  27. package/dist/config/schema.js +6 -0
  28. package/dist/context.d.ts +1 -0
  29. package/dist/context.js +21 -6
  30. package/dist/daemon/client.d.ts +12 -0
  31. package/dist/daemon/client.js +88 -0
  32. package/dist/daemon/paths.d.ts +16 -0
  33. package/dist/daemon/paths.js +19 -0
  34. package/dist/daemon/server.d.ts +2 -0
  35. package/dist/daemon/server.js +225 -0
  36. package/dist/facts/budget.d.ts +1 -0
  37. package/dist/facts/budget.js +4 -0
  38. package/dist/facts/claude-memory.js +7 -2
  39. package/dist/facts/distill.js +6 -3
  40. package/dist/facts/handoff.js +6 -1
  41. package/dist/facts/known.js +3 -1
  42. package/dist/facts/model.d.ts +1 -1
  43. package/dist/facts/model.js +18 -7
  44. package/dist/facts/start.js +2 -2
  45. package/dist/graph/query/answer.js +1 -1
  46. package/dist/graph/query/path.js +69 -37
  47. package/dist/graph/query/refresh.js +5 -1
  48. package/dist/graph/query/suggest.js +1 -2
  49. package/dist/graph/scan/cache.d.ts +1 -0
  50. package/dist/graph/scan/cache.js +1 -1
  51. package/dist/graph/scan/persist.js +12 -16
  52. package/dist/hooks/compact.d.ts +2 -1
  53. package/dist/hooks/compact.js +9 -3
  54. package/dist/hooks/detached.d.ts +1 -0
  55. package/dist/hooks/detached.js +6 -0
  56. package/dist/hooks/graph-presence.d.ts +3 -0
  57. package/dist/hooks/graph-presence.js +42 -0
  58. package/dist/hooks/handlers/prefer-usages.d.ts +2 -0
  59. package/dist/hooks/handlers/prefer-usages.js +113 -0
  60. package/dist/hooks/handlers/session-end.d.ts +2 -0
  61. package/dist/hooks/handlers/session-end.js +62 -0
  62. package/dist/hooks/handlers/session-start.d.ts +2 -0
  63. package/dist/hooks/handlers/session-start.js +104 -0
  64. package/dist/hooks/handlers/turn-end.d.ts +2 -0
  65. package/dist/hooks/handlers/turn-end.js +31 -0
  66. package/dist/hooks/handlers/user-prompt.d.ts +2 -0
  67. package/dist/hooks/handlers/user-prompt.js +55 -0
  68. package/dist/hooks/local.d.ts +2 -0
  69. package/dist/hooks/local.js +23 -0
  70. package/dist/hooks/payload.d.ts +9 -4
  71. package/dist/hooks/payload.js +26 -17
  72. package/dist/hooks/prefer-usages.js +2 -118
  73. package/dist/hooks/route.d.ts +2 -0
  74. package/dist/hooks/route.js +28 -0
  75. package/dist/hooks/run.d.ts +14 -0
  76. package/dist/hooks/run.js +12 -0
  77. package/dist/hooks/save-turn.d.ts +2 -0
  78. package/dist/hooks/save-turn.js +11 -0
  79. package/dist/hooks/session-end.js +2 -63
  80. package/dist/hooks/session-start.js +2 -90
  81. package/dist/hooks/start-legacy.js +1 -1
  82. package/dist/hooks/turn-end.js +2 -25
  83. package/dist/hooks/turn-save.js +10 -15
  84. package/dist/hooks/user-prompt.js +2 -58
  85. package/dist/search/service.d.ts +1 -0
  86. package/dist/search/service.js +16 -6
  87. package/dist/store/adapter.d.ts +11 -7
  88. package/dist/store/each.d.ts +2 -4
  89. package/dist/store/each.js +1 -0
  90. package/dist/store/mongo/graph.d.ts +2 -1
  91. package/dist/store/mongo/graph.js +17 -2
  92. package/dist/store/mongo/index.d.ts +7 -8
  93. package/dist/store/mongo/index.js +13 -2
  94. package/dist/store/mongo/observations.d.ts +2 -1
  95. package/dist/store/mongo/observations.js +38 -4
  96. package/dist/store/postgres/driver.d.ts +10 -3
  97. package/dist/store/postgres/driver.js +12 -0
  98. package/dist/store/postgres/graph.d.ts +5 -6
  99. package/dist/store/postgres/graph.js +78 -57
  100. package/dist/store/postgres/index.d.ts +7 -8
  101. package/dist/store/postgres/index.js +16 -11
  102. package/dist/store/postgres/insert.js +98 -48
  103. package/dist/store/postgres/meta.d.ts +1 -0
  104. package/dist/store/postgres/meta.js +4 -0
  105. package/dist/store/postgres/observations.d.ts +3 -2
  106. package/dist/store/postgres/observations.js +40 -6
  107. package/dist/store/postgres/rows.js +1 -1
  108. package/dist/store/project-resolver.d.ts +3 -0
  109. package/dist/store/project-resolver.js +34 -23
  110. package/dist/store/scoped-store.js +5 -1
  111. package/dist/store/sqlite/graph.d.ts +5 -6
  112. package/dist/store/sqlite/graph.js +61 -35
  113. package/dist/store/sqlite/index.d.ts +7 -8
  114. package/dist/store/sqlite/index.js +11 -2
  115. package/dist/store/sqlite/observations.d.ts +2 -1
  116. package/dist/store/sqlite/observations.js +38 -7
  117. package/dist/types.d.ts +9 -0
  118. package/dist/util/job-lock.d.ts +1 -0
  119. package/dist/util/job-lock.js +50 -10
  120. package/docs/setup-guide.md +5 -0
  121. package/package.json +6 -8
@@ -1,91 +1,3 @@
1
1
  #!/usr/bin/env node
2
- import { readFileSync } from 'node:fs';
3
- import { homedir } from 'node:os';
4
- import { dirname, join, resolve } from 'node:path';
5
- import { fileURLToPath } from 'node:url';
6
- import { createContext } from '../context.js';
7
- import { distillNotice } from '../facts/notice.js';
8
- import { recentWorkBlock, startFacts } from '../facts/start.js';
9
- import { startBackgroundWork } from './background.js';
10
- import { refreshGraphInBackground } from './graph-refresh.js';
11
- import { recoverAfterCompact } from './compact.js';
12
- import { capturingDisabled, emitSessionStart, readPayload, runHook } from './payload.js';
13
- import { markShown } from './shown.js';
14
- import { legacyBlock } from './start-legacy.js';
15
- import { resolveProject } from '../util/project.js';
16
- import { mentionsPath } from '../util/paths.js';
17
- import { repoRootFor } from '../usages/index.js';
18
- import { refreshInstalled } from '../cli/refresh.js';
19
- import { updateNotice } from '../update.js';
20
- import { silenceSqliteWarning } from '../util/warnings.js';
21
- silenceSqliteWarning();
22
- const SERVER = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'mcp', 'server.js');
23
- const SCAN_HINT = 'Code graph not built yet — run `claude-db scan` once to enable ' +
24
- 'find_usages and the symbol-grep hook.';
25
- function refreshGraphQuietly(project) {
26
- try {
27
- refreshGraphInBackground(project, repoRootFor(project));
28
- }
29
- catch {
30
- return;
31
- }
32
- }
33
- function refreshInstalledQuietly(project) {
34
- try {
35
- refreshInstalled(resolve(dirname(fileURLToPath(import.meta.url)), '..'), project);
36
- }
37
- catch {
38
- return;
39
- }
40
- }
41
- function mcpRegistered(project) {
42
- return [join(project, '.mcp.json'), join(homedir(), '.claude.json')].some((path) => {
43
- try {
44
- return mentionsPath(readFileSync(path, 'utf8'), SERVER);
45
- }
46
- catch {
47
- return false;
48
- }
49
- });
50
- }
51
- await runHook(async () => {
52
- const payload = await readPayload();
53
- const project = resolveProject(payload.cwd);
54
- refreshInstalledQuietly(project);
55
- const ctx = await createContext();
56
- try {
57
- const capturing = !capturingDisabled(ctx.config.capture.scripted);
58
- if (capturing)
59
- startBackgroundWork(project, ctx.config.database);
60
- const earlier = payload.source === 'compact' && payload.session_id
61
- ? await recoverAfterCompact(ctx, project, payload.session_id, payload.transcript_path)
62
- : null;
63
- const restored = earlier?.ids ?? new Set();
64
- const facts = await startFacts(ctx, project, restored, payload.session_id);
65
- const recent = facts ? null : await recentWorkBlock(ctx, project, payload.session_id, restored);
66
- const legacy = facts ? null : await legacyBlock(ctx, project, restored);
67
- const shown = [...(facts?.ids ?? []), ...(recent?.ids ?? [])];
68
- if (shown.length > 0 && payload.session_id)
69
- markShown(payload.session_id, shown);
70
- const fallback = legacy?.hasMemory || !recent ? legacy?.text : undefined;
71
- const parts = [earlier?.block, facts?.block ?? fallback, recent?.block].filter((part) => Boolean(part));
72
- if ((facts || recent || legacy?.hasMemory) && mcpRegistered(project)) {
73
- parts.push("Search this project's full history with the memory MCP tools before " +
74
- 'asking the user to re-explain prior decisions.');
75
- }
76
- const scanned = (await ctx.store.scannedFiles(project)).length > 0;
77
- if (!scanned)
78
- parts.push(SCAN_HINT);
79
- const update = ctx.config.updates === 'off' ? null : updateNotice();
80
- if (update)
81
- parts.push(update);
82
- const body = parts.join('\n');
83
- const notice = capturing ? await distillNotice(ctx, project) : null;
84
- emitSessionStart(`${body}\n(context ≈ ${Math.round(body.length / 4)} tokens)\n`, notice);
85
- if (scanned)
86
- refreshGraphQuietly(project);
87
- }
88
- finally {
89
- await ctx.close();
90
- }
91
- });
2
+ import { routeHook } from './route.js';
3
+ await routeHook('session-start');
@@ -24,7 +24,7 @@ export async function legacyBlock(ctx, project, restored) {
24
24
  for (const obs of open.slice(0, 3))
25
25
  lines.push(`- ${obs.title}`);
26
26
  }
27
- const rules = (await ctx.store.list({ project, kind: 'preference', limit: 100, newest: true }))
27
+ const rules = (await ctx.store.list({ project, kind: 'preference', limit: 100, newest: true, bodies: false }))
28
28
  .filter((obs) => obs.status !== 'replaced' && !restored.has(obs.id))
29
29
  .sort((a, b) => {
30
30
  const manual = Number(b.sessionId === 'manual') - Number(a.sessionId === 'manual');
@@ -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');
@@ -7,6 +7,7 @@ export declare class SearchService {
7
7
  private readonly maxScanCandidates;
8
8
  constructor(store: MemoryStore, embedder: Embedder | (() => Promise<Embedder>), maxScanCandidates?: number);
9
9
  search(query: SearchQuery): Promise<ObservationIndexEntry[]>;
10
+ private embed;
10
11
  private vectorSearch;
11
12
  timeline(query: TimelineQuery): Promise<ObservationIndexEntry[]>;
12
13
  getObservations(ids: string[]): Promise<Observation[]>;
@@ -17,19 +17,29 @@ export class SearchService {
17
17
  limit: query.limit * 3,
18
18
  maxScanCandidates: query.maxScanCandidates ?? this.maxScanCandidates,
19
19
  };
20
+ const embedded = this.embed(wide.text);
20
21
  const keyword = await this.store.searchKeyword(wide).catch(() => []);
21
- const vector = await this.vectorSearch(wide);
22
+ const vector = await this.vectorSearch(wide, await embedded);
22
23
  const fused = fuse([keyword, vector].filter((r) => r.length > 0), query.limit);
23
24
  return applyRecencyBoost(fused, Date.now());
24
25
  }
25
- async vectorSearch(query) {
26
+ async embed(text) {
26
27
  try {
27
28
  const embedder = typeof this.embedder === 'function' ? await this.embedder() : this.embedder;
28
29
  if (embedder.dimensions === 0)
29
- return [];
30
- const [embedding] = await embedder.embed([query.text]);
31
- if (!embedding || embedding.length === 0)
32
- return [];
30
+ return null;
31
+ const [embedding] = await embedder.embed([text]);
32
+ return embedding && embedding.length > 0 ? { embedder, embedding } : null;
33
+ }
34
+ catch {
35
+ return null;
36
+ }
37
+ }
38
+ async vectorSearch(query, embedded) {
39
+ if (!embedded)
40
+ return [];
41
+ const { embedder, embedding } = embedded;
42
+ try {
33
43
  const candidates = await this.store.searchVector(embedding, {
34
44
  ...query,
35
45
  embedder: embedder.id,
@@ -1,9 +1,14 @@
1
- import type { CodeEdge, CodeSymbol, EdgeFilter, ListFilter, Observation, ObservationIndexEntry, ProjectFilter, RemoveFilter, ScannedFile, SearchQuery, Session, SymbolFilter, TimelineQuery } from '../types.js';
1
+ import type { CodeEdge, CodeSymbol, CountFilter, EdgeFilter, ListFilter, Observation, ObservationIndexEntry, ProjectFilter, RemoveFilter, ScannedFile, SearchQuery, Session, SymbolFilter, TimelineQuery } from '../types.js';
2
2
  export interface ProjectSummary {
3
3
  project: string;
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>;
@@ -22,6 +27,7 @@ export interface MemoryStore {
22
27
  storedVectorDims?(): number | null;
23
28
  remove(filter: RemoveFilter): Promise<number>;
24
29
  list(filter: ListFilter): Promise<Observation[]>;
30
+ count(filter: CountFilter): Promise<number>;
25
31
  listProjects(): Promise<ProjectSummary[]>;
26
32
  inventory(): Promise<string[]>;
27
33
  searchKeyword(query: SearchQuery): Promise<ObservationIndexEntry[]>;
@@ -29,14 +35,12 @@ export interface MemoryStore {
29
35
  timeline(query: TimelineQuery): Promise<ObservationIndexEntry[]>;
30
36
  closeObservations(ids: string[]): Promise<number>;
31
37
  markReplaced(ids: string[]): Promise<number>;
32
- upsertGraph(scan: {
33
- symbols: CodeSymbol[];
34
- edges: CodeEdge[];
35
- files: ScannedFile[];
36
- }): Promise<void>;
38
+ upsertGraph(scan: GraphRows): Promise<void>;
39
+ replaceGraph(project: string, files: string[] | null, scan: GraphRows): Promise<void>;
37
40
  findSymbols(filter: SymbolFilter): Promise<CodeSymbol[]>;
41
+ symbolNames(project: string, limit: number): Promise<string[]>;
38
42
  findEdges(filter: EdgeFilter): Promise<CodeEdge[]>;
39
- scannedFiles(project: string): Promise<ScannedFile[]>;
43
+ scannedFiles(project: string, limit?: number): Promise<ScannedFile[]>;
40
44
  removeGraph(project: string, files?: string[]): Promise<number>;
41
45
  }
42
46
  export declare function isWholeScope(filter: RemoveFilter): boolean;
@@ -1,5 +1,3 @@
1
1
  import type { RecallContext } from '../context.js';
2
- import type { Observation } from '../types.js';
3
- export declare function eachObservation(ctx: RecallContext, filter: {
4
- project?: string;
5
- }, visit: (batch: Observation[]) => Promise<void> | void): Promise<number>;
2
+ import type { ListFilter, Observation } from '../types.js';
3
+ export declare function eachObservation(ctx: RecallContext, filter: Pick<ListFilter, 'project' | 'vectorStale'>, visit: (batch: Observation[]) => Promise<void> | void): Promise<number>;
@@ -9,6 +9,7 @@ export async function eachObservation(ctx, filter, visit) {
9
9
  ...(after === undefined ? {} : { after }),
10
10
  ...(afterId === undefined ? {} : { afterId }),
11
11
  limit: PAGE,
12
+ vectors: true,
12
13
  });
13
14
  if (batch.length === 0)
14
15
  return total;
@@ -7,6 +7,7 @@ export declare function upsertGraph(symbols: Collection<SymbolDoc>, edges: Colle
7
7
  files: ScannedFile[];
8
8
  }): Promise<void>;
9
9
  export declare function findSymbols(symbols: Collection<SymbolDoc>, edges: Collection<EdgeDoc>, scanned: Collection<ScannedFileDoc>, filter: SymbolFilter): Promise<CodeSymbol[]>;
10
+ export declare function symbolNames(symbols: Collection<SymbolDoc>, project: string, limit: number): Promise<string[]>;
10
11
  export declare function findEdges(symbols: Collection<SymbolDoc>, edges: Collection<EdgeDoc>, scanned: Collection<ScannedFileDoc>, filter: EdgeFilter): Promise<CodeEdge[]>;
11
- export declare function scannedFiles(symbols: Collection<SymbolDoc>, edges: Collection<EdgeDoc>, scanned: Collection<ScannedFileDoc>, project: string): Promise<ScannedFile[]>;
12
+ export declare function scannedFiles(symbols: Collection<SymbolDoc>, edges: Collection<EdgeDoc>, scanned: Collection<ScannedFileDoc>, project: string, limit?: number): Promise<ScannedFile[]>;
12
13
  export declare function removeGraph(symbols: Collection<SymbolDoc>, edges: Collection<EdgeDoc>, scanned: Collection<ScannedFileDoc>, project: string, files?: string[]): Promise<number>;
@@ -24,6 +24,14 @@ export async function findSymbols(symbols, edges, scanned, filter) {
24
24
  .toArray();
25
25
  return docs.map(toSymbol);
26
26
  }
27
+ export async function symbolNames(symbols, project, limit) {
28
+ const docs = await symbols
29
+ .find({ project }, { projection: { name: 1 } })
30
+ .sort({ file: 1, line: 1 })
31
+ .limit(limit)
32
+ .toArray();
33
+ return docs.map((doc) => doc.name);
34
+ }
27
35
  export async function findEdges(symbols, edges, scanned, filter) {
28
36
  const query = { project: filter.project };
29
37
  const alternatives = [];
@@ -33,6 +41,12 @@ export async function findEdges(symbols, edges, scanned, filter) {
33
41
  if (filter.dstIds && filter.dstIds.length > 0) {
34
42
  alternatives.push({ dstId: { $in: filter.dstIds } });
35
43
  }
44
+ if (filter.unresolvedSrcNames && filter.unresolvedSrcNames.length > 0) {
45
+ alternatives.push({ srcId: { $in: ['', null] }, srcName: { $in: filter.unresolvedSrcNames } });
46
+ }
47
+ if (filter.unresolvedDstNames && filter.unresolvedDstNames.length > 0) {
48
+ alternatives.push({ dstId: { $in: ['', null] }, dstName: { $in: filter.unresolvedDstNames } });
49
+ }
36
50
  if (alternatives.length > 0)
37
51
  query['$or'] = alternatives;
38
52
  if (filter.dstName !== undefined) {
@@ -47,8 +61,9 @@ export async function findEdges(symbols, edges, scanned, filter) {
47
61
  .toArray();
48
62
  return docs.map(toEdge);
49
63
  }
50
- export async function scannedFiles(symbols, edges, scanned, project) {
51
- const docs = await scanned.find({ project }).toArray();
64
+ export async function scannedFiles(symbols, edges, scanned, project, limit) {
65
+ const cursor = scanned.find({ project });
66
+ const docs = await (limit === undefined ? cursor : cursor.limit(limit)).toArray();
52
67
  return docs.map((doc) => ({
53
68
  project: doc.project,
54
69
  path: doc.path,
@@ -1,5 +1,5 @@
1
- import type { CodeEdge, CodeSymbol, EdgeFilter, ListFilter, Observation, ObservationIndexEntry, ProjectFilter, RemoveFilter, ScannedFile, SearchQuery, Session, SymbolFilter, TimelineQuery } from '../../types.js';
2
- import type { MemoryStore, ProjectSummary } from '../adapter.js';
1
+ import type { CodeEdge, CodeSymbol, CountFilter, EdgeFilter, ListFilter, Observation, ObservationIndexEntry, ProjectFilter, RemoveFilter, ScannedFile, SearchQuery, Session, SymbolFilter, TimelineQuery } from '../../types.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;
@@ -27,6 +27,7 @@ export declare class MongoStore implements MemoryStore {
27
27
  getObservations(ids: string[]): Promise<Observation[]>;
28
28
  remove(filter: RemoveFilter): Promise<number>;
29
29
  list(filter: ListFilter): Promise<Observation[]>;
30
+ count(filter: CountFilter): Promise<number>;
30
31
  listProjects(): Promise<ProjectSummary[]>;
31
32
  inventory(): Promise<string[]>;
32
33
  searchKeyword(query: SearchQuery): Promise<ObservationIndexEntry[]>;
@@ -35,13 +36,11 @@ export declare class MongoStore implements MemoryStore {
35
36
  markReplaced(ids: string[]): Promise<number>;
36
37
  timeline(query: TimelineQuery): Promise<ObservationIndexEntry[]>;
37
38
  hasAtlasVectorIndex(): Promise<boolean>;
38
- upsertGraph(scan: {
39
- symbols: CodeSymbol[];
40
- edges: CodeEdge[];
41
- files: ScannedFile[];
42
- }): Promise<void>;
39
+ upsertGraph(scan: GraphRows): Promise<void>;
40
+ replaceGraph(project: string, files: string[] | null, scan: GraphRows): Promise<void>;
43
41
  findSymbols(filter: SymbolFilter): Promise<CodeSymbol[]>;
42
+ symbolNames(project: string, limit: number): Promise<string[]>;
44
43
  findEdges(filter: EdgeFilter): Promise<CodeEdge[]>;
45
- scannedFiles(project: string): Promise<ScannedFile[]>;
44
+ scannedFiles(project: string, limit?: number): Promise<ScannedFile[]>;
46
45
  removeGraph(project: string, files?: string[]): Promise<number>;
47
46
  }
@@ -89,6 +89,9 @@ export class MongoStore {
89
89
  async list(filter) {
90
90
  return observationsOps.list(this.observations, this.sessions, this.symbols, this.edges, this.scanned, filter);
91
91
  }
92
+ async count(filter) {
93
+ return observationsOps.count(this.observations, filter);
94
+ }
92
95
  async listProjects() {
93
96
  return observationsOps.listProjects(this.observations);
94
97
  }
@@ -118,14 +121,22 @@ export class MongoStore {
118
121
  async upsertGraph(scan) {
119
122
  return graphOps.upsertGraph(this.symbols, this.edges, this.scanned, scan);
120
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
+ }
121
129
  async findSymbols(filter) {
122
130
  return graphOps.findSymbols(this.symbols, this.edges, this.scanned, filter);
123
131
  }
132
+ async symbolNames(project, limit) {
133
+ return graphOps.symbolNames(this.symbols, project, limit);
134
+ }
124
135
  async findEdges(filter) {
125
136
  return graphOps.findEdges(this.symbols, this.edges, this.scanned, filter);
126
137
  }
127
- async scannedFiles(project) {
128
- return graphOps.scannedFiles(this.symbols, this.edges, this.scanned, project);
138
+ async scannedFiles(project, limit) {
139
+ return graphOps.scannedFiles(this.symbols, this.edges, this.scanned, project, limit);
129
140
  }
130
141
  async removeGraph(project, files) {
131
142
  return graphOps.removeGraph(this.symbols, this.edges, this.scanned, project, files);
@@ -1,10 +1,11 @@
1
1
  import type { Collection } from './driver.js';
2
- import type { ListFilter, Observation, RemoveFilter } from '../../types.js';
2
+ import type { CountFilter, ListFilter, Observation, RemoveFilter } from '../../types.js';
3
3
  import type { ProjectSummary } from '../adapter.js';
4
4
  import type { EdgeDoc, ObservationDoc, ScannedFileDoc, SessionDoc, SymbolDoc } from './docs.js';
5
5
  export declare function insertObservations(collection: Collection<ObservationDoc>, sessions: Collection<SessionDoc>, symbols: Collection<SymbolDoc>, edges: Collection<EdgeDoc>, scanned: Collection<ScannedFileDoc>, rows: Observation[]): Promise<void>;
6
6
  export declare function getObservations(observations: Collection<ObservationDoc>, sessions: Collection<SessionDoc>, symbols: Collection<SymbolDoc>, edges: Collection<EdgeDoc>, scanned: Collection<ScannedFileDoc>, ids: string[]): Promise<Observation[]>;
7
7
  export declare function remove(observations: Collection<ObservationDoc>, sessions: Collection<SessionDoc>, symbols: Collection<SymbolDoc>, edges: Collection<EdgeDoc>, scanned: Collection<ScannedFileDoc>, filter: RemoveFilter): Promise<number>;
8
+ export declare function count(observations: Collection<ObservationDoc>, filter: CountFilter): Promise<number>;
8
9
  export declare function list(observations: Collection<ObservationDoc>, sessions: Collection<SessionDoc>, symbols: Collection<SymbolDoc>, edges: Collection<EdgeDoc>, scanned: Collection<ScannedFileDoc>, filter: ListFilter): Promise<Observation[]>;
9
10
  export declare function listProjects(observations: Collection<ObservationDoc>): Promise<ProjectSummary[]>;
10
11
  export declare function closeObservations(observations: Collection<ObservationDoc>, ids: string[]): Promise<number>;
@@ -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),
@@ -60,9 +72,7 @@ export async function remove(observations, sessions, symbols, edges, scanned, fi
60
72
  }
61
73
  return count;
62
74
  }
63
- export async function list(observations, sessions, symbols, edges, scanned, filter) {
64
- if (noProjects(filter.project))
65
- return [];
75
+ function scopeQuery(filter) {
66
76
  const query = {};
67
77
  Object.assign(query, projectMatch(filter.project));
68
78
  Object.assign(query, sessionMatch(filter.sessionId, filter.excludeSessions));
@@ -70,6 +80,19 @@ export async function list(observations, sessions, symbols, edges, scanned, filt
70
80
  query['kind'] = filter.kind;
71
81
  if (filter.status)
72
82
  query['status'] = filter.status;
83
+ if (filter.tag)
84
+ query['tags'] = filter.tag;
85
+ return query;
86
+ }
87
+ export async function count(observations, filter) {
88
+ if (noProjects(filter.project))
89
+ return 0;
90
+ return observations.countDocuments(scopeQuery(filter));
91
+ }
92
+ export async function list(observations, sessions, symbols, edges, scanned, filter) {
93
+ if (noProjects(filter.project))
94
+ return [];
95
+ const query = scopeQuery(filter);
73
96
  if (filter.after !== undefined) {
74
97
  if (filter.afterId === undefined)
75
98
  query['createdAt'] = { $gt: filter.after };
@@ -79,6 +102,17 @@ export async function list(observations, sessions, symbols, edges, scanned, filt
79
102
  { createdAt: filter.after, _id: { $gt: filter.afterId } },
80
103
  ];
81
104
  }
105
+ if (filter.vectorStale) {
106
+ const { embedder } = filter.vectorStale;
107
+ query['$and'] = [
108
+ {
109
+ $or: [
110
+ { embedding: null },
111
+ embedder === undefined ? { embedder: null } : { embedder: { $ne: embedder } },
112
+ ],
113
+ },
114
+ ];
115
+ }
82
116
  const direction = filter.newest ? -1 : 1;
83
117
  const docs = await observations
84
118
  .find(query)
@@ -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,11 +1,10 @@
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[]>;
7
+ export declare function symbolNames(pool: Pool, project: string, limit: number): Promise<string[]>;
9
8
  export declare function findEdges(pool: Pool, filter: EdgeFilter): Promise<CodeEdge[]>;
10
- export declare function scannedFiles(pool: Pool, project: string): Promise<ScannedFile[]>;
9
+ export declare function scannedFiles(pool: Pool, project: string, limit?: number): Promise<ScannedFile[]>;
11
10
  export declare function removeGraph(pool: Pool, project: string, files?: string[]): Promise<number>;