claude-db 0.8.5 → 0.9.1

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 (54) hide show
  1. package/CHANGELOG.md +103 -0
  2. package/dist/capture/flush.js +3 -1
  3. package/dist/capture/progress.js +41 -9
  4. package/dist/capture/transcript.js +8 -2
  5. package/dist/capture/turn-extractor.js +2 -2
  6. package/dist/cli/commands/doctor.js +2 -2
  7. package/dist/cli/commands/graph.js +12 -3
  8. package/dist/cli/commands/insight.js +2 -1
  9. package/dist/cli/commands/maintain.js +2 -2
  10. package/dist/cli/commands/setup.js +3 -3
  11. package/dist/cli/commands/sync.js +9 -3
  12. package/dist/cli/commands/transfer.js +60 -10
  13. package/dist/cli/commands/view.js +3 -2
  14. package/dist/graph/languages/basic.js +5 -0
  15. package/dist/graph/query/lookup.js +7 -3
  16. package/dist/graph/scan/extract.d.ts +2 -0
  17. package/dist/graph/scan/extract.js +40 -20
  18. package/dist/graph/scan/files.d.ts +2 -1
  19. package/dist/graph/scan/files.js +4 -3
  20. package/dist/graph/scan/index.d.ts +1 -0
  21. package/dist/graph/scan/index.js +11 -1
  22. package/dist/graph/scan/resolve.js +17 -6
  23. package/dist/hooks/grep-symbols.js +1 -1
  24. package/dist/hooks/relevance.d.ts +1 -1
  25. package/dist/hooks/relevance.js +3 -1
  26. package/dist/hooks/session-start.js +1 -1
  27. package/dist/hooks/user-prompt.js +3 -2
  28. package/dist/store/adapter.d.ts +1 -0
  29. package/dist/store/mongo/graph.js +7 -0
  30. package/dist/store/mongo/index.d.ts +1 -0
  31. package/dist/store/mongo/index.js +3 -0
  32. package/dist/store/mongo/observations.js +11 -3
  33. package/dist/store/mongo/search.js +7 -5
  34. package/dist/store/mongo/sessions.d.ts +1 -0
  35. package/dist/store/mongo/sessions.js +8 -5
  36. package/dist/store/mongo/vectorSearch.js +1 -1
  37. package/dist/store/postgres/graph.js +7 -2
  38. package/dist/store/postgres/index.d.ts +1 -0
  39. package/dist/store/postgres/index.js +4 -1
  40. package/dist/store/postgres/insert.d.ts +1 -1
  41. package/dist/store/postgres/insert.js +2 -1
  42. package/dist/store/postgres/meta.js +16 -4
  43. package/dist/store/postgres/observations.js +10 -3
  44. package/dist/store/postgres/sessions.d.ts +1 -0
  45. package/dist/store/postgres/sessions.js +4 -0
  46. package/dist/store/sqlite/graph.js +6 -2
  47. package/dist/store/sqlite/index.d.ts +1 -0
  48. package/dist/store/sqlite/index.js +3 -0
  49. package/dist/store/sqlite/observations.js +10 -3
  50. package/dist/store/sqlite/sessions.d.ts +1 -0
  51. package/dist/store/sqlite/sessions.js +4 -0
  52. package/dist/types.d.ts +3 -0
  53. package/dist/usages/find.js +10 -6
  54. package/package.json +1 -1
@@ -5,7 +5,7 @@ import { join } from 'node:path';
5
5
  import { BASIC_FINGERPRINT, LANGUAGES, languageFor } from '../languages/index.js';
6
6
  const MAX_BUFFER = 64 * 1024 * 1024;
7
7
  const MAX_FILE_BYTES = 1024 * 1024;
8
- export const SCAN_VERSION = 4;
8
+ export const SCAN_VERSION = 5;
9
9
  const RULES_FINGERPRINT = createHash('sha256')
10
10
  .update(JSON.stringify(LANGUAGES, (_key, value) => (value instanceof RegExp ? String(value) : value)))
11
11
  .update(BASIC_FINGERPRINT)
@@ -40,8 +40,9 @@ export function readSource(root, path) {
40
40
  catch {
41
41
  return null;
42
42
  }
43
- if (bytes.length > MAX_FILE_BYTES || bytes.includes(0))
44
- return null;
43
+ if (bytes.length > MAX_FILE_BYTES || bytes.includes(0)) {
44
+ return { path, spec, source: '', hash: hashOf(bytes), unreadable: true };
45
+ }
45
46
  return { path, spec, source: bytes.toString('utf8'), hash: hashOf(bytes) };
46
47
  }
47
48
  export function currentHashes(root) {
@@ -6,6 +6,7 @@ export interface ScanOptions {
6
6
  project: string;
7
7
  known: Map<string, string>;
8
8
  force?: boolean;
9
+ existing?: CodeSymbol[];
9
10
  }
10
11
  export interface ScanResult {
11
12
  symbols: CodeSymbol[];
@@ -26,6 +26,12 @@ export function scanRepository(options) {
26
26
  result.skipped += 1;
27
27
  continue;
28
28
  }
29
+ if (file.unreadable) {
30
+ result.unsupported += 1;
31
+ result.changed.push(path);
32
+ result.files.push({ project, path, hash: file.hash, scannedAt });
33
+ continue;
34
+ }
29
35
  let extracted;
30
36
  try {
31
37
  extracted = extractFile(file, project);
@@ -39,6 +45,10 @@ export function scanRepository(options) {
39
45
  result.changed.push(path);
40
46
  result.files.push({ project, path, hash: file.hash, scannedAt });
41
47
  }
42
- result.edges = resolveEdges(project, result.symbols, references);
48
+ const rescanned = new Set(result.changed);
49
+ const resolvable = options.existing === undefined
50
+ ? result.symbols
51
+ : [...result.symbols, ...options.existing.filter((s) => !rescanned.has(s.file))];
52
+ result.edges = resolveEdges(project, resolvable, references);
43
53
  return result;
44
54
  }
@@ -2,8 +2,8 @@ import { observationId } from '../../capture/identity.js';
2
2
  const SAME_FILE = 1;
3
3
  const SINGLE_MATCH = 0.95;
4
4
  const AMBIGUOUS_MATCH = 0.85;
5
- function edgeId(project, file, line, from, to) {
6
- return observationId('graph', 0, `${project}\0${file}\0${line}\0${from}\0${to}`);
5
+ function edgeId(project, file, line, from, to, relation, origin) {
6
+ return observationId('graph', 0, `${project}\0${file}\0${line}\0${from}\0${to}\0${relation}\0${origin}`);
7
7
  }
8
8
  export function resolveEdges(project, symbols, references) {
9
9
  const byName = new Map();
@@ -15,9 +15,13 @@ export function resolveEdges(project, symbols, references) {
15
15
  byName.set(symbol.name, [symbol]);
16
16
  }
17
17
  const edges = [];
18
+ const origins = new Map();
18
19
  for (const reference of references) {
19
20
  const candidates = byName.get(reference.name) ?? [];
20
- const local = candidates.find((candidate) => candidate.file === reference.file);
21
+ const exact = reference.to
22
+ ? candidates.find((candidate) => candidate.id === reference.to)
23
+ : undefined;
24
+ const local = exact ?? candidates.find((candidate) => candidate.file === reference.file);
21
25
  const target = local ?? candidates[0];
22
26
  if (!target && reference.weak)
23
27
  continue;
@@ -33,7 +37,7 @@ export function resolveEdges(project, symbols, references) {
33
37
  }
34
38
  const fromName = reference.from?.name ?? reference.file;
35
39
  edges.push({
36
- id: edgeId(project, reference.file, reference.line, fromName, reference.name),
40
+ id: edgeId(project, reference.file, reference.line, fromName, reference.name, reference.relation, reference.origin ?? ''),
37
41
  project,
38
42
  srcId: reference.from?.id ?? '',
39
43
  srcName: fromName,
@@ -45,10 +49,13 @@ export function resolveEdges(project, symbols, references) {
45
49
  file: reference.file,
46
50
  line: reference.line,
47
51
  });
52
+ const added = edges[edges.length - 1];
53
+ if (added && reference.origin !== undefined)
54
+ origins.set(added.id, reference.origin);
48
55
  }
49
- return mergeMemberCalls(edges);
56
+ return mergeMemberCalls(edges, origins);
50
57
  }
51
- function mergeMemberCalls(edges) {
58
+ function mergeMemberCalls(edges, origins) {
52
59
  const byLine = new Map();
53
60
  for (const edge of edges) {
54
61
  const key = `${edge.file}\0${edge.line}`;
@@ -70,6 +77,10 @@ function mergeMemberCalls(edges) {
70
77
  continue;
71
78
  if (bare.dstName !== tail || bare.relation !== qualified.relation)
72
79
  continue;
80
+ const from = origins.get(qualified.id);
81
+ const to = origins.get(bare.id);
82
+ if (from === undefined || to === undefined || from !== to)
83
+ continue;
73
84
  if (!qualified.dstId && bare.dstId) {
74
85
  qualified.dstId = bare.dstId;
75
86
  qualified.confidence = bare.confidence;
@@ -16,7 +16,7 @@ export function symbolsGreppedIn(command) {
16
16
  const rest = match[5] ?? '';
17
17
  if (!SYMBOL.test(pattern) && !WORD.test(pattern))
18
18
  continue;
19
- if (/-\w*v/.test(flags))
19
+ if (/(?:^|\s)-[A-Za-z]*v|--invert-match/.test(flags))
20
20
  continue;
21
21
  const invocation = match[0];
22
22
  const piped = /^\s*\|/.test(invocation);
@@ -3,4 +3,4 @@ export declare function overlapCount(prompt: string, entry: {
3
3
  title: string;
4
4
  snippet?: string | undefined;
5
5
  }): number;
6
- export declare function renderPromptContext(entries: ObservationIndexEntry[], maxChars: number, expanded?: Observation[], expandMaxChars?: number): string | null;
6
+ export declare function renderPromptContext(entries: ObservationIndexEntry[], maxChars: number, expanded?: Observation[], expandMaxChars?: number, shown?: string[]): string | null;
@@ -9,7 +9,7 @@ export function overlapCount(prompt, entry) {
9
9
  shared++;
10
10
  return shared;
11
11
  }
12
- export function renderPromptContext(entries, maxChars, expanded = [], expandMaxChars = 900) {
12
+ export function renderPromptContext(entries, maxChars, expanded = [], expandMaxChars = 900, shown = []) {
13
13
  if (entries.length === 0)
14
14
  return null;
15
15
  const expandedIds = new Set(expanded.map((obs) => obs.id));
@@ -17,6 +17,7 @@ export function renderPromptContext(entries, maxChars, expanded = [], expandMaxC
17
17
  for (const obs of expanded) {
18
18
  const date = new Date(obs.createdAt).toISOString().slice(0, 10);
19
19
  sections.push(`${toShortId(obs.id)} ${obs.kind} ${date} ${obs.title}\n` + clip(obs.body, expandMaxChars));
20
+ shown.push(obs.id);
20
21
  }
21
22
  const lines = [];
22
23
  let budget = maxChars;
@@ -30,6 +31,7 @@ export function renderPromptContext(entries, maxChars, expanded = [], expandMaxC
30
31
  break;
31
32
  budget -= line.length + 1;
32
33
  lines.push(line);
34
+ shown.push(entry.id);
33
35
  }
34
36
  if (sections.length === 0 && lines.length === 0)
35
37
  return null;
@@ -78,7 +78,7 @@ await runHook(async () => {
78
78
  lines.push(`- ${obs.title}`);
79
79
  }
80
80
  }
81
- const rules = (await ctx.store.list({ project, kind: 'preference', limit: 100 }))
81
+ const rules = (await ctx.store.list({ project, kind: 'preference', limit: 100, newest: true }))
82
82
  .sort((a, b) => {
83
83
  const manual = Number(b.sessionId === 'manual') - Number(a.sessionId === 'manual');
84
84
  return manual !== 0 ? manual : b.createdAt - a.createdAt;
@@ -40,10 +40,11 @@ await runHook(async () => {
40
40
  const expanded = toExpand.length > 0
41
41
  ? await ctx.search.getObservations(toExpand.map((entry) => entry.id))
42
42
  : [];
43
- const block = renderPromptContext(entries, ctx.config.inject.promptMaxChars, expanded, ctx.config.inject.expandMaxChars);
43
+ const emitted = [];
44
+ const block = renderPromptContext(entries, ctx.config.inject.promptMaxChars, expanded, ctx.config.inject.expandMaxChars, emitted);
44
45
  if (!block)
45
46
  return;
46
- markShown(sessionId, entries.map((entry) => entry.id));
47
+ markShown(sessionId, emitted);
47
48
  emitContext(`${block}\n(context ≈ ${Math.round(block.length / 4)} tokens)\n`);
48
49
  }
49
50
  finally {
@@ -12,6 +12,7 @@ export interface MemoryStore {
12
12
  upsertSession(session: Session): Promise<void>;
13
13
  getSession(id: string): Promise<Session | null>;
14
14
  recentSessions(project: string, limit: number): Promise<Session[]>;
15
+ sessionProjects(): Promise<string[]>;
15
16
  clearSummary(id: string): Promise<boolean>;
16
17
  insertObservations(observations: Observation[]): Promise<void>;
17
18
  getObservations(ids: string[]): Promise<Observation[]>;
@@ -1,4 +1,5 @@
1
1
  import { toEdge, toSymbol, upsertsOf } from './docs.js';
2
+ import { escapeRegex } from './helpers.js';
2
3
  export async function upsertGraph(symbols, edges, scanned, scan) {
3
4
  if (scan.symbols.length > 0) {
4
5
  await symbols.bulkWrite(upsertsOf(scan.symbols, (s) => s.id), { ordered: false });
@@ -34,8 +35,14 @@ export async function findEdges(symbols, edges, scanned, filter) {
34
35
  }
35
36
  if (alternatives.length > 0)
36
37
  query['$or'] = alternatives;
38
+ if (filter.dstName !== undefined) {
39
+ query['dstName'] = {
40
+ $in: [filter.dstName, new RegExp(`\\.${escapeRegex(filter.dstName)}$`)],
41
+ };
42
+ }
37
43
  const docs = await edges
38
44
  .find(query)
45
+ .sort({ file: 1, line: 1, _id: 1 })
39
46
  .limit(filter.limit ?? 2000)
40
47
  .toArray();
41
48
  return docs.map(toEdge);
@@ -18,6 +18,7 @@ export declare class MongoStore implements MemoryStore {
18
18
  upsertSession(session: Session): Promise<void>;
19
19
  getSession(id: string): Promise<Session | null>;
20
20
  recentSessions(project: string, limit: number): Promise<Session[]>;
21
+ sessionProjects(): Promise<string[]>;
21
22
  clearSummary(id: string): Promise<boolean>;
22
23
  insertObservations(observations: Observation[]): Promise<void>;
23
24
  getObservations(ids: string[]): Promise<Observation[]>;
@@ -60,6 +60,9 @@ export class MongoStore {
60
60
  async recentSessions(project, limit) {
61
61
  return sessionsOps.recentSessions(this.sessions, project, limit);
62
62
  }
63
+ async sessionProjects() {
64
+ return sessionsOps.sessionProjects(this.sessions);
65
+ }
63
66
  async clearSummary(id) {
64
67
  return sessionsOps.clearSummary(this.sessions, id);
65
68
  }
@@ -65,11 +65,19 @@ export async function list(observations, sessions, symbols, edges, scanned, filt
65
65
  query['kind'] = filter.kind;
66
66
  if (filter.status)
67
67
  query['status'] = filter.status;
68
- if (filter.after !== undefined)
69
- query['createdAt'] = { $gt: filter.after };
68
+ if (filter.after !== undefined) {
69
+ if (filter.afterId === undefined)
70
+ query['createdAt'] = { $gt: filter.after };
71
+ else
72
+ query['$or'] = [
73
+ { createdAt: { $gt: filter.after } },
74
+ { createdAt: filter.after, _id: { $gt: filter.afterId } },
75
+ ];
76
+ }
77
+ const direction = filter.newest ? -1 : 1;
70
78
  const docs = await observations
71
79
  .find(query)
72
- .sort({ createdAt: 1 })
80
+ .sort({ createdAt: direction, _id: direction })
73
81
  .limit(filter.limit ?? 1000)
74
82
  .toArray();
75
83
  return docs.map(toObservation);
@@ -32,11 +32,13 @@ export async function timeline(observations, query) {
32
32
  .sort({ createdAt: -1 })
33
33
  .limit(query.before + 1)
34
34
  .toArray(),
35
- observations
36
- .find({ project: anchor.project, createdAt: { $gt: anchor.createdAt } }, { projection })
37
- .sort({ createdAt: 1 })
38
- .limit(query.after)
39
- .toArray(),
35
+ query.after <= 0
36
+ ? Promise.resolve([])
37
+ : observations
38
+ .find({ project: anchor.project, createdAt: { $gt: anchor.createdAt } }, { projection })
39
+ .sort({ createdAt: 1 })
40
+ .limit(query.after)
41
+ .toArray(),
40
42
  ]);
41
43
  return [...before.reverse(), ...after].map((doc) => toIndexEntry(doc, 0));
42
44
  }
@@ -5,3 +5,4 @@ export declare function upsertSession(sessions: Collection<SessionDoc>, session:
5
5
  export declare function getSession(sessions: Collection<SessionDoc>, id: string): Promise<Session | null>;
6
6
  export declare function clearSummary(sessions: Collection<SessionDoc>, id: string): Promise<boolean>;
7
7
  export declare function recentSessions(sessions: Collection<SessionDoc>, project: string, limit: number): Promise<Session[]>;
8
+ export declare function sessionProjects(sessions: Collection<SessionDoc>): Promise<string[]>;
@@ -1,14 +1,11 @@
1
1
  import { toSession } from './docs.js';
2
2
  export async function upsertSession(sessions, session) {
3
- const set = {
4
- project: session.project,
5
- startedAt: session.startedAt,
6
- };
3
+ const set = { project: session.project };
7
4
  if (session.endedAt !== undefined)
8
5
  set.endedAt = session.endedAt;
9
6
  if (session.summary !== undefined)
10
7
  set.summary = session.summary;
11
- await sessions.updateOne({ _id: session.id }, { $set: set }, { upsert: true });
8
+ await sessions.updateOne({ _id: session.id }, { $set: set, $setOnInsert: { startedAt: session.startedAt } }, { upsert: true });
12
9
  }
13
10
  export async function getSession(sessions, id) {
14
11
  const doc = await sessions.findOne({ _id: id });
@@ -26,3 +23,9 @@ export async function recentSessions(sessions, project, limit) {
26
23
  .toArray();
27
24
  return docs.map(toSession);
28
25
  }
26
+ export async function sessionProjects(sessions) {
27
+ const rows = await sessions
28
+ .aggregate([{ $group: { _id: '$project' } }])
29
+ .toArray();
30
+ return rows.map((row) => String(row._id ?? '')).filter((project) => project.length > 0);
31
+ }
@@ -37,7 +37,7 @@ export async function searchVector(observations, cache, vector, query) {
37
37
  },
38
38
  ])
39
39
  .toArray();
40
- return docs.map((doc) => toIndexEntry(doc, doc.score));
40
+ return docs.map((doc) => toIndexEntry(doc, Number(doc.score) * 2 - 1));
41
41
  }
42
42
  const docs = await observations
43
43
  .find({
@@ -96,9 +96,14 @@ export async function findEdges(pool, filter) {
96
96
  values.push(ids);
97
97
  sides.push(`${column} = ANY($${values.length}::text[])`);
98
98
  }
99
- const where = sides.length > 0 ? ` AND (${sides.join(' OR ')})` : '';
99
+ let where = sides.length > 0 ? ` AND (${sides.join(' OR ')})` : '';
100
+ if (filter.dstName !== undefined) {
101
+ values.push(filter.dstName, `%.${filter.dstName}`);
102
+ where += ` AND (dst_name = $${values.length - 1} OR dst_name LIKE $${values.length})`;
103
+ }
100
104
  values.push(filter.limit ?? 2000);
101
- const res = await pool.query(`SELECT * FROM symbol_edges WHERE project = $1${where} LIMIT $${values.length}`, values);
105
+ const res = await pool.query(`SELECT * FROM symbol_edges WHERE project = $1${where}
106
+ ORDER BY file, line, id LIMIT $${values.length}`, values);
102
107
  return res.rows.map(toEdge);
103
108
  }
104
109
  export async function scannedFiles(pool, project) {
@@ -23,6 +23,7 @@ export declare class PostgresStore implements MemoryStore {
23
23
  ping(): Promise<boolean>;
24
24
  upsertSession(session: Session): Promise<void>;
25
25
  getSession(id: string): Promise<Session | null>;
26
+ sessionProjects(): Promise<string[]>;
26
27
  recentSessions(project: string, limit: number): Promise<Session[]>;
27
28
  clearSummary(id: string): Promise<boolean>;
28
29
  insertObservations(observations: Observation[]): Promise<void>;
@@ -96,6 +96,9 @@ export class PostgresStore {
96
96
  async getSession(id) {
97
97
  return sessionsOps.getSession(this.pool, id);
98
98
  }
99
+ async sessionProjects() {
100
+ return sessionsOps.sessionProjects(this.pool);
101
+ }
99
102
  async recentSessions(project, limit) {
100
103
  return sessionsOps.recentSessions(this.pool, project, limit);
101
104
  }
@@ -103,7 +106,7 @@ export class PostgresStore {
103
106
  return sessionsOps.clearSummary(this.pool, id);
104
107
  }
105
108
  async insertObservations(observations) {
106
- return insertOps.insertObservations(this.pool, (dims) => this.ensureVectorColumn(dims), observations);
109
+ return insertOps.insertObservations(this.pool, (dims) => this.ensureVectorColumn(dims), observations, this.vectorDims !== null);
107
110
  }
108
111
  async getObservations(ids) {
109
112
  return observationsOps.getObservations(this.pool, ids);
@@ -1,3 +1,3 @@
1
1
  import type { Pool } from './driver.js';
2
2
  import type { Observation } from '../../types.js';
3
- export declare function insertObservations(pool: Pool, ensureVectorColumn: (dims: number) => Promise<boolean>, observations: Observation[]): Promise<void>;
3
+ export declare function insertObservations(pool: Pool, ensureVectorColumn: (dims: number) => Promise<boolean>, observations: Observation[], hasVectorColumn?: boolean): Promise<void>;
@@ -1,4 +1,4 @@
1
- export async function insertObservations(pool, ensureVectorColumn, observations) {
1
+ export async function insertObservations(pool, ensureVectorColumn, observations, hasVectorColumn = false) {
2
2
  if (observations.length === 0)
3
3
  return;
4
4
  const width = observations.find((obs) => obs.embedding?.length)?.embedding?.length;
@@ -44,6 +44,7 @@ export async function insertObservations(pool, ensureVectorColumn, observations)
44
44
  const updates = columns
45
45
  .slice(1)
46
46
  .map((column) => `${column} = EXCLUDED.${column}`)
47
+ .concat(!vectorStored && (hasVectorColumn || vectors) ? ['embedding = NULL'] : [])
47
48
  .join(', ');
48
49
  await client.query(`INSERT INTO observations (${columns.join(', ')}) VALUES (${holes})
49
50
  ON CONFLICT (id) DO UPDATE SET ${updates}`, values);
@@ -21,12 +21,24 @@ export async function ensureTagsIndexed(pool) {
21
21
  JOIN pg_attribute a ON a.attrelid = d.adrelid AND a.attnum = d.adnum
22
22
  WHERE d.adrelid = 'observations'::regclass AND a.attname = 'tsv'`);
23
23
  const expr = String(res.rows[0]?.['expr'] ?? '');
24
- if (expr.length === 0 || expr.includes('tags'))
24
+ if (expr.includes('tags'))
25
25
  return;
26
- await pool.query('ALTER TABLE observations DROP COLUMN tsv');
27
- await pool.query(`ALTER TABLE observations
26
+ const client = await pool.connect();
27
+ try {
28
+ await client.query('BEGIN');
29
+ await client.query('ALTER TABLE observations DROP COLUMN IF EXISTS tsv');
30
+ await client.query(`ALTER TABLE observations
28
31
  ADD COLUMN tsv TSVECTOR GENERATED ALWAYS AS (${TSV_EXPRESSION}) STORED`);
29
- await pool.query('CREATE INDEX IF NOT EXISTS idx_obs_tsv ON observations USING GIN(tsv)');
32
+ await client.query('CREATE INDEX IF NOT EXISTS idx_obs_tsv ON observations USING GIN(tsv)');
33
+ await client.query('COMMIT');
34
+ }
35
+ catch (error) {
36
+ await client.query('ROLLBACK');
37
+ throw error;
38
+ }
39
+ finally {
40
+ client.release();
41
+ }
30
42
  }
31
43
  export async function readVectorDims(pool) {
32
44
  const res = await pool.query(`SELECT atttypmod AS dims FROM pg_attribute
@@ -88,13 +88,20 @@ export async function list(pool, filter) {
88
88
  conditions.push(`status = $${values.length}`);
89
89
  }
90
90
  if (filter.after !== undefined) {
91
- values.push(filter.after);
92
- conditions.push(`created_at > $${values.length}`);
91
+ if (filter.afterId === undefined) {
92
+ values.push(filter.after);
93
+ conditions.push(`created_at > $${values.length}`);
94
+ }
95
+ else {
96
+ values.push(filter.after, filter.after, filter.afterId);
97
+ conditions.push(`(created_at > $${values.length - 2} OR (created_at = $${values.length - 1} AND id > $${values.length}))`);
98
+ }
93
99
  }
94
100
  values.push(filter.limit ?? 1000);
101
+ const direction = filter.newest ? 'DESC' : 'ASC';
95
102
  const res = await pool.query(`SELECT * FROM observations
96
103
  ${conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : ''}
97
- ORDER BY created_at ASC LIMIT $${values.length}`, values);
104
+ ORDER BY created_at ${direction}, id ${direction} LIMIT $${values.length}`, values);
98
105
  return res.rows.map(toObservation);
99
106
  }
100
107
  export async function listProjects(pool) {
@@ -4,3 +4,4 @@ export declare function upsertSession(pool: Pool, session: Session): Promise<voi
4
4
  export declare function getSession(pool: Pool, id: string): Promise<Session | null>;
5
5
  export declare function clearSummary(pool: Pool, id: string): Promise<boolean>;
6
6
  export declare function recentSessions(pool: Pool, project: string, limit: number): Promise<Session[]>;
7
+ export declare function sessionProjects(pool: Pool): Promise<string[]>;
@@ -28,3 +28,7 @@ export async function recentSessions(pool, project, limit) {
28
28
  ORDER BY started_at DESC LIMIT $2`, [project, limit]);
29
29
  return res.rows.map(toSession);
30
30
  }
31
+ export async function sessionProjects(pool) {
32
+ const res = await pool.query('SELECT DISTINCT project FROM sessions');
33
+ return res.rows.map((row) => String(row['project'] ?? '')).filter((p) => p.length > 0);
34
+ }
@@ -57,10 +57,14 @@ export async function findEdges(db, filter) {
57
57
  sides.push(`${column} IN (${ids.map(() => '?').join(',')})`);
58
58
  params.push(...ids);
59
59
  }
60
- const where = sides.length > 0 ? ` AND (${sides.join(' OR ')})` : '';
60
+ let where = sides.length > 0 ? ` AND (${sides.join(' OR ')})` : '';
61
+ if (filter.dstName !== undefined) {
62
+ where += ' AND (dst_name = ? OR dst_name LIKE ?)';
63
+ params.push(filter.dstName, `%.${filter.dstName}`);
64
+ }
61
65
  params.push(filter.limit ?? 2000);
62
66
  const rows = db
63
- .prepare(`SELECT * FROM symbol_edges WHERE project = ?${where} LIMIT ?`)
67
+ .prepare(`SELECT * FROM symbol_edges WHERE project = ?${where} ORDER BY file, line, id LIMIT ?`)
64
68
  .all(...params);
65
69
  return rows.map(toEdge);
66
70
  }
@@ -18,6 +18,7 @@ export declare class SqliteStore implements MemoryStore {
18
18
  remove(filter: RemoveFilter): Promise<number>;
19
19
  list(filter: ListFilter): Promise<Observation[]>;
20
20
  listProjects(): Promise<ProjectSummary[]>;
21
+ sessionProjects(): Promise<string[]>;
21
22
  inventory(): Promise<string[]>;
22
23
  searchKeyword(query: SearchQuery): Promise<ObservationIndexEntry[]>;
23
24
  searchVector(vector: number[], query: SearchQuery): Promise<ObservationIndexEntry[]>;
@@ -74,6 +74,9 @@ export class SqliteStore {
74
74
  async listProjects() {
75
75
  return observationsOps.listProjects(this.db);
76
76
  }
77
+ async sessionProjects() {
78
+ return sessionsOps.sessionProjects(this.db);
79
+ }
77
80
  async inventory() {
78
81
  return observationsOps.inventory(this.db);
79
82
  }
@@ -87,14 +87,21 @@ export async function list(db, filter) {
87
87
  params.push(filter.status);
88
88
  }
89
89
  if (filter.after !== undefined) {
90
- conditions.push('created_at > ?');
91
- params.push(filter.after);
90
+ if (filter.afterId === undefined) {
91
+ conditions.push('created_at > ?');
92
+ params.push(filter.after);
93
+ }
94
+ else {
95
+ conditions.push('(created_at > ? OR (created_at = ? AND id > ?))');
96
+ params.push(filter.after, filter.after, filter.afterId);
97
+ }
92
98
  }
93
99
  params.push(filter.limit ?? 1000);
100
+ const direction = filter.newest ? 'DESC' : 'ASC';
94
101
  const rows = db
95
102
  .prepare(`SELECT * FROM observations
96
103
  ${conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : ''}
97
- ORDER BY created_at ASC LIMIT ?`)
104
+ ORDER BY created_at ${direction}, id ${direction} LIMIT ?`)
98
105
  .all(...params);
99
106
  return rows.map(toObservation);
100
107
  }
@@ -4,3 +4,4 @@ export declare function upsertSession(db: DatabaseSync, session: Session): Promi
4
4
  export declare function clearSummary(db: DatabaseSync, id: string): Promise<boolean>;
5
5
  export declare function getSession(db: DatabaseSync, id: string): Promise<Session | null>;
6
6
  export declare function recentSessions(db: DatabaseSync, project: string, limit: number): Promise<Session[]>;
7
+ export declare function sessionProjects(db: DatabaseSync): Promise<string[]>;
@@ -25,3 +25,7 @@ export async function recentSessions(db, project, limit) {
25
25
  .all(project, limit);
26
26
  return rows.map(toSession);
27
27
  }
28
+ export async function sessionProjects(db) {
29
+ const rows = db.prepare('SELECT DISTINCT project FROM sessions').all();
30
+ return rows.map((row) => String(row['project'] ?? '')).filter((project) => project.length > 0);
31
+ }
package/dist/types.d.ts CHANGED
@@ -53,7 +53,9 @@ export interface ListFilter {
53
53
  kind?: ObservationKind;
54
54
  status?: ObservationStatus;
55
55
  after?: number;
56
+ afterId?: string;
56
57
  limit?: number;
58
+ newest?: boolean;
57
59
  }
58
60
  export interface TimelineQuery {
59
61
  observationId: string;
@@ -96,6 +98,7 @@ export interface EdgeFilter {
96
98
  project: string;
97
99
  srcIds?: string[];
98
100
  dstIds?: string[];
101
+ dstName?: string;
99
102
  limit?: number;
100
103
  }
101
104
  export interface ScannedFile {
@@ -59,8 +59,7 @@ function buildArgs({ symbol, regex }) {
59
59
  function withContext(root, matches, context) {
60
60
  const fileLines = new Map();
61
61
  const out = [];
62
- let lastFile = '';
63
- let lastLineShown = 0;
62
+ const emitted = new Map();
64
63
  for (const m of matches) {
65
64
  let lines = fileLines.get(m.file);
66
65
  if (lines === undefined) {
@@ -78,8 +77,14 @@ function withContext(root, matches, context) {
78
77
  }
79
78
  const from = Math.max(1, m.line - context);
80
79
  const to = Math.min(lines.length, m.line + context);
81
- const start = m.file === lastFile ? Math.max(from, lastLineShown + 1) : from;
82
- for (let n = start; n <= to; n += 1) {
80
+ for (let n = from; n <= to; n += 1) {
81
+ const key = `${m.file}\0${n}`;
82
+ const at = emitted.get(key);
83
+ if (at !== undefined) {
84
+ if (n === m.line)
85
+ out[at] = m;
86
+ continue;
87
+ }
83
88
  out.push(n === m.line
84
89
  ? m
85
90
  : {
@@ -89,9 +94,8 @@ function withContext(root, matches, context) {
89
94
  isDefinition: false,
90
95
  isMatch: false,
91
96
  });
97
+ emitted.set(key, out.length - 1);
92
98
  }
93
- lastFile = m.file;
94
- lastLineShown = to;
95
99
  }
96
100
  return out;
97
101
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-db",
3
- "version": "0.8.5",
3
+ "version": "0.9.1",
4
4
  "description": "Persistent memory for Claude Code. Captures why decisions were made, not just which files changed. Bring your own database: SQLite, MongoDB or Postgres. No cloud, no subscription.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",