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,5 +1,5 @@
1
1
  export function withProjectScope(inner, resolver) {
2
- const keyOf = async (project) => (await resolver.resolve(project)).key;
2
+ const keyOf = (project) => resolver.key(project);
3
3
  async function expand(filter) {
4
4
  if (filter === undefined)
5
5
  return undefined;
@@ -27,6 +27,10 @@ export function withProjectScope(inner, resolver) {
27
27
  const project = await expand(filter.project);
28
28
  return inner.list(project === undefined ? filter : { ...filter, project });
29
29
  },
30
+ async count(filter) {
31
+ const project = await expand(filter.project);
32
+ return inner.count(project === undefined ? filter : { ...filter, project });
33
+ },
30
34
  async searchKeyword(query) {
31
35
  const project = await expand(query.project);
32
36
  return inner.searchKeyword(project === undefined ? query : { ...query, project });
@@ -1,11 +1,10 @@
1
1
  import type { DatabaseSync } from 'node:sqlite';
2
2
  import type { CodeEdge, CodeSymbol, EdgeFilter, ScannedFile, SymbolFilter } from '../../types.js';
3
- export declare function upsertGraph(db: DatabaseSync, 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(db: DatabaseSync, scan: GraphRows): Promise<void>;
5
+ export declare function replaceGraph(db: DatabaseSync, project: string, files: string[] | null, scan: GraphRows): Promise<void>;
8
6
  export declare function findSymbols(db: DatabaseSync, filter: SymbolFilter): Promise<CodeSymbol[]>;
7
+ export declare function symbolNames(db: DatabaseSync, project: string, limit: number): Promise<string[]>;
9
8
  export declare function findEdges(db: DatabaseSync, filter: EdgeFilter): Promise<CodeEdge[]>;
10
- export declare function scannedFiles(db: DatabaseSync, project: string): Promise<ScannedFile[]>;
9
+ export declare function scannedFiles(db: DatabaseSync, project: string, limit?: number): Promise<ScannedFile[]>;
11
10
  export declare function removeGraph(db: DatabaseSync, project: string, files?: string[]): Promise<number>;
@@ -1,5 +1,5 @@
1
1
  import { toEdge, toSymbol } from './rows.js';
2
- export async function upsertGraph(db, scan) {
2
+ function writeGraphRows(db, scan) {
3
3
  const symbol = db.prepare(`INSERT OR REPLACE INTO symbols
4
4
  (id, project, name, kind, file, line, lang, signature)
5
5
  VALUES (?, ?, ?, ?, ?, ?, ?, ?)`);
@@ -9,24 +9,54 @@ export async function upsertGraph(db, scan) {
9
9
  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
10
10
  const file = db.prepare(`INSERT OR REPLACE INTO scanned_files (project, path, hash, scanned_at)
11
11
  VALUES (?, ?, ?, ?)`);
12
+ for (const s of scan.symbols) {
13
+ symbol.run(s.id, s.project, s.name, s.kind, s.file, s.line, s.lang, s.signature);
14
+ }
15
+ for (const e of scan.edges) {
16
+ edge.run(e.id, e.project, e.srcId, e.srcName, e.dstId, e.dstName, e.relation, e.confidence, e.score, e.file, e.line);
17
+ }
18
+ for (const f of scan.files)
19
+ file.run(f.project, f.path, f.hash, f.scannedAt);
20
+ }
21
+ const DELETE_BATCH = 500;
22
+ function deleteGraphRows(db, project, files) {
23
+ const batches = files === null ? [null] : [];
24
+ for (let start = 0; files !== null && start < files.length; start += DELETE_BATCH) {
25
+ batches.push(files.slice(start, start + DELETE_BATCH));
26
+ }
27
+ let removed = 0;
28
+ for (const batch of batches) {
29
+ const holes = batch?.map(() => '?').join(',');
30
+ const where = batch ? `project = ? AND file IN (${holes})` : 'project = ?';
31
+ const cacheWhere = batch ? `project = ? AND path IN (${holes})` : 'project = ?';
32
+ const params = (batch ? [project, ...batch] : [project]);
33
+ removed += Number(db.prepare(`DELETE FROM symbols WHERE ${where}`).run(...params).changes);
34
+ db.prepare(`DELETE FROM symbol_edges WHERE ${where}`).run(...params);
35
+ db.prepare(`DELETE FROM scanned_files WHERE ${cacheWhere}`).run(...params);
36
+ }
37
+ return removed;
38
+ }
39
+ function inTransaction(db, work) {
12
40
  db.exec('BEGIN');
13
41
  try {
14
- for (const s of scan.symbols) {
15
- symbol.run(s.id, s.project, s.name, s.kind, s.file, s.line, s.lang, s.signature);
16
- }
17
- for (const e of scan.edges) {
18
- edge.run(e.id, e.project, e.srcId, e.srcName, e.dstId, e.dstName, e.relation, e.confidence, e.score, e.file, e.line);
19
- }
20
- for (const f of scan.files) {
21
- file.run(f.project, f.path, f.hash, f.scannedAt);
22
- }
42
+ const result = work();
23
43
  db.exec('COMMIT');
44
+ return result;
24
45
  }
25
46
  catch (error) {
26
47
  db.exec('ROLLBACK');
27
48
  throw error;
28
49
  }
29
50
  }
51
+ export async function upsertGraph(db, scan) {
52
+ inTransaction(db, () => writeGraphRows(db, scan));
53
+ }
54
+ export async function replaceGraph(db, project, files, scan) {
55
+ inTransaction(db, () => {
56
+ deleteGraphRows(db, project, files);
57
+ writeGraphRows(db, scan);
58
+ });
59
+ }
30
60
  export async function findSymbols(db, filter) {
31
61
  const conditions = ['project = ?'];
32
62
  const params = [filter.project];
@@ -45,6 +75,12 @@ export async function findSymbols(db, filter) {
45
75
  .all(...params);
46
76
  return rows.map(toSymbol);
47
77
  }
78
+ export async function symbolNames(db, project, limit) {
79
+ const rows = db
80
+ .prepare('SELECT name FROM symbols WHERE project = ? ORDER BY file, line LIMIT ?')
81
+ .all(project, limit);
82
+ return rows.map((row) => row['name']);
83
+ }
48
84
  export async function findEdges(db, filter) {
49
85
  const params = [filter.project];
50
86
  const sides = [];
@@ -57,6 +93,15 @@ export async function findEdges(db, filter) {
57
93
  sides.push(`${column} IN (${ids.map(() => '?').join(',')})`);
58
94
  params.push(...ids);
59
95
  }
96
+ for (const [side, names] of [
97
+ ['src', filter.unresolvedSrcNames],
98
+ ['dst', filter.unresolvedDstNames],
99
+ ]) {
100
+ if (!names || names.length === 0)
101
+ continue;
102
+ sides.push(`(${side}_id = '' AND ${side}_name IN (${names.map(() => '?').join(',')}))`);
103
+ params.push(...names);
104
+ }
60
105
  let where = sides.length > 0 ? ` AND (${sides.join(' OR ')})` : '';
61
106
  if (filter.dstName !== undefined) {
62
107
  where += ' AND (dst_name = ? OR dst_name LIKE ?)';
@@ -68,8 +113,10 @@ export async function findEdges(db, filter) {
68
113
  .all(...params);
69
114
  return rows.map(toEdge);
70
115
  }
71
- export async function scannedFiles(db, project) {
72
- const rows = db.prepare('SELECT * FROM scanned_files WHERE project = ?').all(project);
116
+ export async function scannedFiles(db, project, limit) {
117
+ const rows = (limit === undefined
118
+ ? db.prepare('SELECT * FROM scanned_files WHERE project = ?').all(project)
119
+ : db.prepare('SELECT * FROM scanned_files WHERE project = ? LIMIT ?').all(project, limit));
73
120
  return rows.map((row) => ({
74
121
  project: row['project'],
75
122
  path: row['path'],
@@ -78,27 +125,6 @@ export async function scannedFiles(db, project) {
78
125
  }));
79
126
  }
80
127
  export async function removeGraph(db, project, files) {
81
- const scoped = files && files.length > 0;
82
- const where = scoped
83
- ? `project = ? AND file IN (${files.map(() => '?').join(',')})`
84
- : 'project = ?';
85
- const params = scoped ? [project, ...files] : [project];
86
- const count = db
87
- .prepare(`SELECT COUNT(*) AS n FROM symbols WHERE ${where}`)
88
- .get(...params);
89
- db.exec('BEGIN');
90
- try {
91
- db.prepare(`DELETE FROM symbols WHERE ${where}`).run(...params);
92
- db.prepare(`DELETE FROM symbol_edges WHERE ${where}`).run(...params);
93
- const cacheWhere = scoped
94
- ? `project = ? AND path IN (${files.map(() => '?').join(',')})`
95
- : 'project = ?';
96
- db.prepare(`DELETE FROM scanned_files WHERE ${cacheWhere}`).run(...params);
97
- db.exec('COMMIT');
98
- }
99
- catch (error) {
100
- db.exec('ROLLBACK');
101
- throw error;
102
- }
103
- return count?.n ?? 0;
128
+ const scoped = files !== undefined && files.length > 0;
129
+ return inTransaction(db, () => deleteGraphRows(db, project, scoped ? files : null));
104
130
  }
@@ -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 const HERE: string;
4
4
  export declare class SqliteStore implements MemoryStore {
5
5
  private readonly db;
@@ -19,6 +19,7 @@ export declare class SqliteStore implements MemoryStore {
19
19
  getObservations(ids: string[]): Promise<Observation[]>;
20
20
  remove(filter: RemoveFilter): Promise<number>;
21
21
  list(filter: ListFilter): Promise<Observation[]>;
22
+ count(filter: CountFilter): Promise<number>;
22
23
  listProjects(): Promise<ProjectSummary[]>;
23
24
  sessionProjects(): Promise<string[]>;
24
25
  inventory(): Promise<string[]>;
@@ -27,13 +28,11 @@ export declare class SqliteStore implements MemoryStore {
27
28
  closeObservations(ids: string[]): Promise<number>;
28
29
  markReplaced(ids: string[]): Promise<number>;
29
30
  timeline(query: TimelineQuery): Promise<ObservationIndexEntry[]>;
30
- upsertGraph(scan: {
31
- symbols: CodeSymbol[];
32
- edges: CodeEdge[];
33
- files: ScannedFile[];
34
- }): Promise<void>;
31
+ upsertGraph(scan: GraphRows): Promise<void>;
32
+ replaceGraph(project: string, files: string[] | null, scan: GraphRows): Promise<void>;
35
33
  findSymbols(filter: SymbolFilter): Promise<CodeSymbol[]>;
34
+ symbolNames(project: string, limit: number): Promise<string[]>;
36
35
  findEdges(filter: EdgeFilter): Promise<CodeEdge[]>;
37
- scannedFiles(project: string): Promise<ScannedFile[]>;
36
+ scannedFiles(project: string, limit?: number): Promise<ScannedFile[]>;
38
37
  removeGraph(project: string, files?: string[]): Promise<number>;
39
38
  }
@@ -97,6 +97,9 @@ export class SqliteStore {
97
97
  async list(filter) {
98
98
  return observationsOps.list(this.db, filter);
99
99
  }
100
+ async count(filter) {
101
+ return observationsOps.count(this.db, filter);
102
+ }
100
103
  async listProjects() {
101
104
  return observationsOps.listProjects(this.db);
102
105
  }
@@ -124,14 +127,20 @@ export class SqliteStore {
124
127
  async upsertGraph(scan) {
125
128
  return graphOps.upsertGraph(this.db, scan);
126
129
  }
130
+ async replaceGraph(project, files, scan) {
131
+ return graphOps.replaceGraph(this.db, project, files, scan);
132
+ }
127
133
  async findSymbols(filter) {
128
134
  return graphOps.findSymbols(this.db, filter);
129
135
  }
136
+ async symbolNames(project, limit) {
137
+ return graphOps.symbolNames(this.db, project, limit);
138
+ }
130
139
  async findEdges(filter) {
131
140
  return graphOps.findEdges(this.db, filter);
132
141
  }
133
- async scannedFiles(project) {
134
- return graphOps.scannedFiles(this.db, project);
142
+ async scannedFiles(project, limit) {
143
+ return graphOps.scannedFiles(this.db, project, limit);
135
144
  }
136
145
  async removeGraph(project, files) {
137
146
  return graphOps.removeGraph(this.db, project, files);
@@ -1,10 +1,11 @@
1
1
  import type { DatabaseSync } from 'node:sqlite';
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
  export declare function insertObservations(db: DatabaseSync, observations: Observation[]): Promise<void>;
5
5
  export declare function getObservations(db: DatabaseSync, ids: string[]): Promise<Observation[]>;
6
6
  export declare function remove(db: DatabaseSync, filter: RemoveFilter): Promise<number>;
7
7
  export declare function list(db: DatabaseSync, filter: ListFilter): Promise<Observation[]>;
8
+ export declare function count(db: DatabaseSync, filter: CountFilter): Promise<number>;
8
9
  export declare function listProjects(db: DatabaseSync): Promise<ProjectSummary[]>;
9
10
  export declare function inventory(db: DatabaseSync): Promise<string[]>;
10
11
  export declare function closeObservations(db: DatabaseSync, ids: string[]): Promise<number>;
@@ -1,6 +1,6 @@
1
1
  import { foreignNames, isWholeScope } from '../adapter.js';
2
2
  import { toObservation } from './rows.js';
3
- import { excludeSessionsClause, projectClause, removeWhere } from './filters.js';
3
+ import { TAG_PREDICATE, excludeSessionsClause, projectClause, removeWhere } from './filters.js';
4
4
  import { partitionIds } from '../../util/shortid.js';
5
5
  import { packVector } from '../../util/vector.js';
6
6
  import { scopeToken } from '../../util/scope.js';
@@ -11,11 +11,15 @@ export async function insertObservations(db, observations) {
11
11
  const stmt = db.prepare(`INSERT OR REPLACE INTO observations
12
12
  (id, session_id, project, scope, kind, title, body, files, tags,
13
13
  created_at, embedding, embedder, author, status)
14
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
14
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
15
+ COALESCE((SELECT status FROM observations
16
+ WHERE id = ? AND status = 'done' AND ? = 'open' AND files = ?), ?))`);
15
17
  db.exec('BEGIN');
16
18
  try {
17
19
  for (const obs of observations) {
18
- stmt.run(obs.id, obs.sessionId, obs.project, scopeToken(obs.project), obs.kind, obs.title, obs.body, JSON.stringify(obs.files), JSON.stringify(obs.tags), obs.createdAt, obs.embedding ? packVector(obs.embedding) : null, obs.embedder ?? null, obs.author ?? null, obs.status ?? 'done');
20
+ const files = JSON.stringify(obs.files);
21
+ const status = obs.status ?? 'done';
22
+ stmt.run(obs.id, obs.sessionId, obs.project, scopeToken(obs.project), obs.kind, obs.title, obs.body, files, JSON.stringify(obs.tags), obs.createdAt, obs.embedding ? packVector(obs.embedding) : null, obs.embedder ?? null, obs.author ?? null, obs.id, status, files, status);
19
23
  }
20
24
  db.exec('COMMIT');
21
25
  }
@@ -76,11 +80,8 @@ export async function remove(db, filter) {
76
80
  db.exec('VACUUM');
77
81
  return count?.n ?? 0;
78
82
  }
79
- export async function list(db, filter) {
80
- if (noProjects(filter.project))
81
- return [];
83
+ function scopeConditions(filter, params) {
82
84
  const conditions = [];
83
- const params = [];
84
85
  const project = projectClause('project', filter.project, params);
85
86
  if (project)
86
87
  conditions.push(project);
@@ -99,6 +100,17 @@ export async function list(db, filter) {
99
100
  conditions.push('status = ?');
100
101
  params.push(filter.status);
101
102
  }
103
+ if (filter.tag) {
104
+ conditions.push(TAG_PREDICATE(''));
105
+ params.push(filter.tag);
106
+ }
107
+ return conditions;
108
+ }
109
+ export async function list(db, filter) {
110
+ if (noProjects(filter.project))
111
+ return [];
112
+ const params = [];
113
+ const conditions = scopeConditions(filter, params);
102
114
  if (filter.after !== undefined) {
103
115
  if (filter.afterId === undefined) {
104
116
  conditions.push('created_at > ?');
@@ -109,6 +121,14 @@ export async function list(db, filter) {
109
121
  params.push(filter.after, filter.after, filter.afterId);
110
122
  }
111
123
  }
124
+ if (filter.vectorStale) {
125
+ const stale = ['embedding IS NULL', 'embedder IS NULL'];
126
+ if (filter.vectorStale.embedder !== undefined) {
127
+ stale.push('embedder != ?');
128
+ params.push(filter.vectorStale.embedder);
129
+ }
130
+ conditions.push(`(${stale.join(' OR ')})`);
131
+ }
112
132
  params.push(filter.limit ?? 1000);
113
133
  const direction = filter.newest ? 'DESC' : 'ASC';
114
134
  const rows = db
@@ -118,6 +138,17 @@ export async function list(db, filter) {
118
138
  .all(...params);
119
139
  return rows.map(toObservation);
120
140
  }
141
+ export async function count(db, filter) {
142
+ if (noProjects(filter.project))
143
+ return 0;
144
+ const params = [];
145
+ const conditions = scopeConditions(filter, params);
146
+ const row = db
147
+ .prepare(`SELECT COUNT(*) AS n FROM observations
148
+ ${conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : ''}`)
149
+ .get(...params);
150
+ return Number(row?.n ?? 0);
151
+ }
121
152
  export async function listProjects(db) {
122
153
  const rows = db
123
154
  .prepare(`SELECT project, COUNT(*) AS n, MAX(created_at) AS last
package/dist/types.d.ts CHANGED
@@ -58,11 +58,18 @@ export interface ListFilter {
58
58
  excludeSessions?: string[];
59
59
  kind?: ObservationKind;
60
60
  status?: ObservationStatus;
61
+ tag?: string;
61
62
  after?: number;
62
63
  afterId?: string;
63
64
  limit?: number;
64
65
  newest?: boolean;
66
+ vectors?: boolean;
67
+ bodies?: boolean;
68
+ vectorStale?: {
69
+ embedder?: string;
70
+ };
65
71
  }
72
+ export type CountFilter = Pick<ListFilter, 'project' | 'sessionId' | 'excludeSessions' | 'kind' | 'status' | 'tag'>;
66
73
  export interface TimelineQuery {
67
74
  observationId: string;
68
75
  before: number;
@@ -104,6 +111,8 @@ export interface EdgeFilter {
104
111
  project: string;
105
112
  srcIds?: string[];
106
113
  dstIds?: string[];
114
+ unresolvedSrcNames?: string[];
115
+ unresolvedDstNames?: string[];
107
116
  dstName?: string;
108
117
  limit?: number;
109
118
  }
@@ -1,4 +1,5 @@
1
1
  export declare function jobMark(job: string, key: string): number;
2
2
  export declare function claimJob(job: string, key: string, now?: number): boolean;
3
+ export declare function keepJob(job: string, key: string): () => void;
3
4
  export declare function finishJob(job: string, key: string, mark: number): void;
4
5
  export declare function releaseJob(job: string, key: string): void;
@@ -1,4 +1,4 @@
1
- import { mkdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
1
+ import { mkdirSync, readFileSync, rmSync, statSync, utimesSync, writeFileSync } from 'node:fs';
2
2
  import { join } from 'node:path';
3
3
  import { CONFIG_DIR } from '../config/index.js';
4
4
  import { scopeToken } from './scope.js';
@@ -20,22 +20,62 @@ export function jobMark(job, key) {
20
20
  return 0;
21
21
  }
22
22
  }
23
+ const BREAKER_STALE_MS = 30_000;
24
+ const KEEP_EVERY_MS = 60_000;
25
+ function ageOf(path, now) {
26
+ try {
27
+ return now - statSync(path).mtimeMs;
28
+ }
29
+ catch {
30
+ return null;
31
+ }
32
+ }
33
+ function breakStaleLock(lock, now) {
34
+ const breaker = `${lock}.break`;
35
+ if (!tryLock(breaker, now)) {
36
+ const age = ageOf(breaker, now);
37
+ if (age === null || age < BREAKER_STALE_MS)
38
+ return false;
39
+ rmSync(breaker, { force: true });
40
+ if (!tryLock(breaker, now))
41
+ return false;
42
+ }
43
+ try {
44
+ const age = ageOf(lock, now);
45
+ if (age !== null && age < STALE_LOCK_MS)
46
+ return false;
47
+ rmSync(lock, { force: true });
48
+ return tryLock(lock, now);
49
+ }
50
+ finally {
51
+ rmSync(breaker, { force: true });
52
+ }
53
+ }
23
54
  export function claimJob(job, key, now = Date.now()) {
24
55
  const lock = lockPath(job, key);
25
56
  mkdirSync(jobDir(job), { recursive: true });
26
57
  if (tryLock(lock, now))
27
58
  return true;
28
- let lockedAt;
29
- try {
30
- lockedAt = statSync(lock).mtimeMs;
31
- }
32
- catch {
59
+ const age = ageOf(lock, now);
60
+ if (age === null)
33
61
  return tryLock(lock, now);
34
- }
35
- if (now - lockedAt < STALE_LOCK_MS)
62
+ if (age < STALE_LOCK_MS)
36
63
  return false;
37
- rmSync(lock, { force: true });
38
- return tryLock(lock, now);
64
+ return breakStaleLock(lock, now);
65
+ }
66
+ export function keepJob(job, key) {
67
+ const lock = lockPath(job, key);
68
+ const timer = setInterval(() => {
69
+ try {
70
+ const at = new Date();
71
+ utimesSync(lock, at, at);
72
+ }
73
+ catch {
74
+ clearInterval(timer);
75
+ }
76
+ }, KEEP_EVERY_MS);
77
+ timer.unref();
78
+ return () => clearInterval(timer);
39
79
  }
40
80
  export function finishJob(job, key, mark) {
41
81
  mkdirSync(jobDir(job), { recursive: true });
@@ -76,6 +76,11 @@ claude-db use "postgres://user:pass@host:5432/memory"
76
76
  For MongoDB, install `mongodb` and use a `mongodb+srv://` address. Clones of one repository share
77
77
  memory by git remote, so every machine must run 0.11.0 or newer.
78
78
 
79
+ On a remote database each hook pays to start node, open a connection and load the embedding model before
80
+ it does any work, about a second in all. `claude-db daemon on` keeps one process warm that the hooks hand
81
+ their work to, so the next hooks skip that cost. It stops after 15 idle minutes (`daemon.idleMinutes`)
82
+ and starts again with the next hook. `claude-db daemon off` turns it off.
83
+
79
84
  ## Upgrading
80
85
 
81
86
  ```bash
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-db",
3
- "version": "0.12.3",
3
+ "version": "0.13.0",
4
4
  "description": "Free, open-source memory for Claude Code. Remembers why decisions were made and maps your code. Your own database (SQLite, Postgres or MongoDB), no cloud, no subscription.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -54,8 +54,6 @@
54
54
  "dependencies": {
55
55
  "@ast-grep/napi": "^0.45.0",
56
56
  "@modelcontextprotocol/sdk": "^1.30.0",
57
- "graphology": "^0.26.0",
58
- "graphology-shortest-path": "^2.1.0",
59
57
  "oxc-resolver": "^11.24.2",
60
58
  "zod": "^4.4.3"
61
59
  },
@@ -130,10 +128,10 @@
130
128
  "access": "public"
131
129
  },
132
130
  "optionalDependencies": {
133
- "claude-db-grammars-linux-x64": "0.12.3",
134
- "claude-db-grammars-linux-arm64": "0.12.3",
135
- "claude-db-grammars-darwin-x64": "0.12.3",
136
- "claude-db-grammars-darwin-arm64": "0.12.3",
137
- "claude-db-grammars-win32-x64": "0.12.3"
131
+ "claude-db-grammars-linux-x64": "0.13.0",
132
+ "claude-db-grammars-linux-arm64": "0.13.0",
133
+ "claude-db-grammars-darwin-x64": "0.13.0",
134
+ "claude-db-grammars-darwin-arm64": "0.13.0",
135
+ "claude-db-grammars-win32-x64": "0.13.0"
138
136
  }
139
137
  }