claude-db 0.10.3 → 0.11.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 (93) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/README.md +2 -1
  3. package/dist/catchup/gather.d.ts +17 -0
  4. package/dist/catchup/gather.js +39 -0
  5. package/dist/catchup/git-state.d.ts +7 -0
  6. package/dist/catchup/git-state.js +32 -0
  7. package/dist/catchup/render.d.ts +3 -0
  8. package/dist/catchup/render.js +41 -0
  9. package/dist/cli/backfill-output.d.ts +13 -0
  10. package/dist/cli/backfill-output.js +78 -0
  11. package/dist/cli/commands/catchup.d.ts +1 -0
  12. package/dist/cli/commands/catchup.js +14 -0
  13. package/dist/cli/commands/distill.js +13 -2
  14. package/dist/cli/commands/doctor.d.ts +2 -0
  15. package/dist/cli/commands/doctor.js +42 -3
  16. package/dist/cli/commands/insight.js +2 -1
  17. package/dist/cli/commands/setup.js +29 -9
  18. package/dist/cli/index.js +9 -0
  19. package/dist/cli/install.d.ts +6 -1
  20. package/dist/cli/install.js +8 -18
  21. package/dist/cli/paths.d.ts +1 -1
  22. package/dist/cli/paths.js +2 -2
  23. package/dist/cli/refresh.js +4 -12
  24. package/dist/cli/skills.d.ts +17 -0
  25. package/dist/cli/skills.js +65 -0
  26. package/dist/cli/usage.js +2 -0
  27. package/dist/config/schema.d.ts +3 -0
  28. package/dist/config/schema.js +8 -0
  29. package/dist/context.js +5 -2
  30. package/dist/facts/distill.d.ts +16 -1
  31. package/dist/facts/distill.js +20 -7
  32. package/dist/facts/handoff.d.ts +6 -0
  33. package/dist/facts/handoff.js +25 -0
  34. package/dist/facts/health.d.ts +3 -0
  35. package/dist/facts/health.js +10 -0
  36. package/dist/facts/model.d.ts +1 -0
  37. package/dist/facts/model.js +12 -0
  38. package/dist/facts/start.js +16 -5
  39. package/dist/store/adapter.d.ts +4 -2
  40. package/dist/store/adapter.js +1 -1
  41. package/dist/store/mongo/docs.d.ts +6 -0
  42. package/dist/store/mongo/filters.d.ts +2 -1
  43. package/dist/store/mongo/filters.js +9 -2
  44. package/dist/store/mongo/index.d.ts +5 -2
  45. package/dist/store/mongo/index.js +13 -2
  46. package/dist/store/mongo/observations.js +9 -5
  47. package/dist/store/mongo/project-links.d.ts +4 -0
  48. package/dist/store/mongo/project-links.js +13 -0
  49. package/dist/store/mongo/search.js +3 -0
  50. package/dist/store/mongo/sessions.d.ts +2 -2
  51. package/dist/store/mongo/sessions.js +5 -1
  52. package/dist/store/mongo/vectorSearch.js +3 -0
  53. package/dist/store/postgres/filters.d.ts +2 -1
  54. package/dist/store/postgres/filters.js +11 -4
  55. package/dist/store/postgres/index.d.ts +4 -2
  56. package/dist/store/postgres/index.js +7 -0
  57. package/dist/store/postgres/observations.js +15 -10
  58. package/dist/store/postgres/project-links.d.ts +3 -0
  59. package/dist/store/postgres/project-links.js +15 -0
  60. package/dist/store/postgres/schema.d.ts +2 -2
  61. package/dist/store/postgres/schema.js +9 -1
  62. package/dist/store/postgres/search.js +3 -0
  63. package/dist/store/postgres/sessions.d.ts +2 -2
  64. package/dist/store/postgres/sessions.js +9 -2
  65. package/dist/store/postgres/vectorSearch.js +3 -0
  66. package/dist/store/project-resolver.d.ts +16 -0
  67. package/dist/store/project-resolver.js +49 -0
  68. package/dist/store/project-scope.d.ts +5 -0
  69. package/dist/store/project-scope.js +16 -0
  70. package/dist/store/scoped-store.d.ts +3 -0
  71. package/dist/store/scoped-store.js +49 -0
  72. package/dist/store/sqlite/filters.d.ts +3 -2
  73. package/dist/store/sqlite/filters.js +23 -9
  74. package/dist/store/sqlite/index.d.ts +4 -2
  75. package/dist/store/sqlite/index.js +7 -0
  76. package/dist/store/sqlite/observations.js +13 -7
  77. package/dist/store/sqlite/project-links.d.ts +3 -0
  78. package/dist/store/sqlite/project-links.js +15 -0
  79. package/dist/store/sqlite/schema.sql +9 -0
  80. package/dist/store/sqlite/search.js +5 -0
  81. package/dist/store/sqlite/sessions.d.ts +2 -2
  82. package/dist/store/sqlite/sessions.js +8 -2
  83. package/dist/types.d.ts +4 -3
  84. package/dist/util/claude-binary.d.ts +15 -1
  85. package/dist/util/claude-binary.js +39 -7
  86. package/dist/util/project-key.d.ts +7 -0
  87. package/dist/util/project-key.js +23 -0
  88. package/dist/util/remote-key.d.ts +3 -0
  89. package/dist/util/remote-key.js +50 -0
  90. package/docs/setup-guide.md +21 -13
  91. package/package.json +1 -1
  92. package/skills/catchup/SKILL.md +44 -0
  93. package/skills/handoff/SKILL.md +62 -0
@@ -1,5 +1,7 @@
1
+ import { projectMatch } from './filters.js';
1
2
  import { toSession } from './docs.js';
2
3
  import { summaryTime } from '../session-time.js';
4
+ import { noProjects } from '../project-scope.js';
3
5
  export async function upsertSession(sessions, session) {
4
6
  const set = { project: session.project };
5
7
  if (session.endedAt !== undefined)
@@ -22,8 +24,10 @@ export async function clearSummary(sessions, id) {
22
24
  return (result.modifiedCount ?? 0) > 0;
23
25
  }
24
26
  export async function recentSessions(sessions, project, limit) {
27
+ if (noProjects(project))
28
+ return [];
25
29
  const docs = await sessions
26
- .find({ project, summary: { $type: 'string' } })
30
+ .find({ ...projectMatch(project), summary: { $type: 'string' } })
27
31
  .sort({ startedAt: -1 })
28
32
  .limit(limit)
29
33
  .toArray();
@@ -2,7 +2,10 @@ import { toIndexEntry } from './docs.js';
2
2
  import { scopeFilter, visibleFilter } from './filters.js';
3
3
  import * as searchOps from './search.js';
4
4
  import { cosine } from '../../util/vector.js';
5
+ import { noProjects } from '../project-scope.js';
5
6
  export async function searchVector(observations, cache, vector, query) {
7
+ if (noProjects(query.project))
8
+ return [];
6
9
  if (cache.atlasVectorIndex === null) {
7
10
  cache.atlasVectorIndex = await searchOps.hasAtlasVectorIndex(observations);
8
11
  }
@@ -1,2 +1,3 @@
1
- import type { SearchQuery } from '../../types.js';
1
+ import type { ProjectFilter, SearchQuery } from '../../types.js';
2
+ export declare function projectClause(filter: ProjectFilter | undefined, values: unknown[], column?: string): string | null;
2
3
  export declare function appendScope(query: SearchQuery, conditions: string[], values: unknown[]): void;
@@ -1,9 +1,16 @@
1
+ import { projectsOf } from '../project-scope.js';
2
+ export function projectClause(filter, values, column = 'project') {
3
+ const projects = projectsOf(filter);
4
+ if (projects.length === 0)
5
+ return null;
6
+ values.push(projects);
7
+ return `${column} = ANY($${values.length}::text[])`;
8
+ }
1
9
  export function appendScope(query, conditions, values) {
2
10
  conditions.push("status <> 'replaced'");
3
- if (query.project) {
4
- values.push(query.project);
5
- conditions.push(`project = $${values.length}`);
6
- }
11
+ const project = projectClause(query.project, values);
12
+ if (project)
13
+ conditions.push(project);
7
14
  if (query.kind) {
8
15
  values.push(query.kind);
9
16
  conditions.push(`kind = $${values.length}`);
@@ -1,4 +1,4 @@
1
- import type { CodeEdge, CodeSymbol, EdgeFilter, ListFilter, Observation, ObservationIndexEntry, RemoveFilter, ScannedFile, SearchQuery, Session, SymbolFilter, TimelineQuery } from '../../types.js';
1
+ import type { CodeEdge, CodeSymbol, EdgeFilter, ListFilter, Observation, ObservationIndexEntry, ProjectFilter, RemoveFilter, ScannedFile, SearchQuery, Session, SymbolFilter, TimelineQuery } from '../../types.js';
2
2
  import type { MemoryStore, ProjectSummary } from '../adapter.js';
3
3
  export declare class PostgresStore implements MemoryStore {
4
4
  private readonly pool;
@@ -21,10 +21,12 @@ export declare class PostgresStore implements MemoryStore {
21
21
  private ensureVectorColumn;
22
22
  close(): Promise<void>;
23
23
  ping(): Promise<boolean>;
24
+ linkProject(folder: string, key: string, now?: number): Promise<void>;
25
+ projectScope(folder: string): Promise<string[]>;
24
26
  upsertSession(session: Session): Promise<void>;
25
27
  getSession(id: string): Promise<Session | null>;
26
28
  sessionProjects(): Promise<string[]>;
27
- recentSessions(project: string, limit: number): Promise<Session[]>;
29
+ recentSessions(project: ProjectFilter, limit: number): Promise<Session[]>;
28
30
  clearSummary(id: string): Promise<boolean>;
29
31
  insertObservations(observations: Observation[]): Promise<void>;
30
32
  getObservations(ids: string[]): Promise<Observation[]>;
@@ -5,6 +5,7 @@ import * as searchOps from './search.js';
5
5
  import * as observationsOps from './observations.js';
6
6
  import * as sessionsOps from './sessions.js';
7
7
  import * as graphOps from './graph.js';
8
+ import * as projectLinksOps from './project-links.js';
8
9
  import { importPg } from './driver.js';
9
10
  import { DDL, SCHEMA_VERSION } from './schema.js';
10
11
  export class PostgresStore {
@@ -90,6 +91,12 @@ export class PostgresStore {
90
91
  const res = await this.pool.query('SELECT 1 AS ok');
91
92
  return res.rowCount === 1;
92
93
  }
94
+ async linkProject(folder, key, now = Date.now()) {
95
+ return projectLinksOps.linkProject(this.pool, folder, key, now);
96
+ }
97
+ async projectScope(folder) {
98
+ return projectLinksOps.projectScope(this.pool, folder);
99
+ }
93
100
  async upsertSession(session) {
94
101
  return sessionsOps.upsertSession(this.pool, session);
95
102
  }
@@ -1,6 +1,8 @@
1
1
  import { foreignNames, isWholeScope } from '../adapter.js';
2
2
  import { partitionIds } from '../../util/shortid.js';
3
+ import { projectClause } from './filters.js';
3
4
  import { toIndexEntry, toObservation } from './rows.js';
5
+ import { noProjects } from '../project-scope.js';
4
6
  export async function getObservations(pool, ids) {
5
7
  if (ids.length === 0)
6
8
  return [];
@@ -19,6 +21,8 @@ export async function getObservations(pool, ids) {
19
21
  return res.rows.map(toObservation);
20
22
  }
21
23
  export async function remove(pool, filter) {
24
+ if (noProjects(filter.project))
25
+ return 0;
22
26
  if (filter.ids?.length === 0)
23
27
  return 0;
24
28
  const conditions = [];
@@ -36,10 +40,9 @@ export async function remove(pool, filter) {
36
40
  }
37
41
  conditions.push(`(${alternatives.join(' OR ')})`);
38
42
  }
39
- if (filter.project) {
40
- values.push(filter.project);
41
- conditions.push(`project = $${values.length}`);
42
- }
43
+ const project = projectClause(filter.project, values);
44
+ if (project)
45
+ conditions.push(project);
43
46
  if (filter.kind) {
44
47
  values.push(filter.kind);
45
48
  conditions.push(`kind = $${values.length}`);
@@ -55,8 +58,9 @@ export async function remove(pool, filter) {
55
58
  const counted = await client.query(`SELECT COUNT(*)::int AS n FROM observations ${where}`, values);
56
59
  await client.query(`DELETE FROM observations ${where}`, values);
57
60
  if (isWholeScope(filter)) {
58
- const scope = filter.project ? 'WHERE project = $1' : '';
59
- const scopeValues = filter.project ? [filter.project] : [];
61
+ const scopeValues = [];
62
+ const wipeClause = projectClause(filter.project, scopeValues);
63
+ const scope = wipeClause ? `WHERE ${wipeClause}` : '';
60
64
  for (const table of ['sessions', 'symbols', 'symbol_edges', 'scanned_files']) {
61
65
  await client.query(`DELETE FROM ${table} ${scope}`, scopeValues);
62
66
  }
@@ -73,12 +77,13 @@ export async function remove(pool, filter) {
73
77
  }
74
78
  }
75
79
  export async function list(pool, filter) {
80
+ if (noProjects(filter.project))
81
+ return [];
76
82
  const conditions = [];
77
83
  const values = [];
78
- if (filter.project) {
79
- values.push(filter.project);
80
- conditions.push(`project = $${values.length}`);
81
- }
84
+ const project = projectClause(filter.project, values);
85
+ if (project)
86
+ conditions.push(project);
82
87
  if (filter.sessionId) {
83
88
  values.push(filter.sessionId);
84
89
  conditions.push(`session_id = $${values.length}`);
@@ -0,0 +1,3 @@
1
+ import type { Pool } from './driver.js';
2
+ export declare function linkProject(pool: Pool, folder: string, key: string, now: number): Promise<void>;
3
+ export declare function projectScope(pool: Pool, folder: string): Promise<string[]>;
@@ -0,0 +1,15 @@
1
+ import { isLinkable, orderScope } from '../project-scope.js';
2
+ export async function linkProject(pool, folder, key, now) {
3
+ if (!isLinkable(folder, key))
4
+ return;
5
+ await pool.query(`INSERT INTO project_links (folder, project_key, first_seen)
6
+ VALUES ($1, $2, $3)
7
+ ON CONFLICT (folder, project_key) DO NOTHING`, [folder, key, now]);
8
+ }
9
+ export async function projectScope(pool, folder) {
10
+ const res = await pool.query(`SELECT project_key AS value FROM project_links WHERE folder = $1
11
+ UNION
12
+ SELECT folder AS value FROM project_links
13
+ WHERE project_key IN (SELECT project_key FROM project_links WHERE folder = $1)`, [folder]);
14
+ return orderScope(folder, res.rows.map((row) => String(row['value'])));
15
+ }
@@ -1,3 +1,3 @@
1
1
  export declare const TSV_EXPRESSION = "\n setweight(to_tsvector('english', coalesce(title, '')), 'A') ||\n setweight(to_tsvector('english', coalesce(tags::text, '')), 'B') ||\n setweight(to_tsvector('english', coalesce(body, '')), 'C')";
2
- export declare const SCHEMA_VERSION = 4;
3
- export declare const DDL = "\n CREATE TABLE IF NOT EXISTS claude_db_meta (\n id INT PRIMARY KEY,\n version INT NOT NULL,\n vector_enabled BOOLEAN NOT NULL DEFAULT FALSE,\n vector_dims INT\n );\n\n CREATE TABLE IF NOT EXISTS sessions (\n id TEXT PRIMARY KEY,\n project TEXT NOT NULL,\n started_at BIGINT NOT NULL,\n ended_at BIGINT,\n summary TEXT\n );\n CREATE INDEX IF NOT EXISTS idx_sessions_project\n ON sessions(project, started_at DESC);\n\n CREATE TABLE IF NOT EXISTS observations (\n id TEXT PRIMARY KEY,\n session_id TEXT NOT NULL,\n project TEXT NOT NULL,\n kind TEXT NOT NULL,\n title TEXT NOT NULL,\n body TEXT NOT NULL,\n files JSONB NOT NULL DEFAULT '[]'::jsonb,\n tags JSONB NOT NULL DEFAULT '[]'::jsonb,\n created_at BIGINT NOT NULL,\n embedder TEXT,\n author TEXT,\n tsv TSVECTOR GENERATED ALWAYS AS (\n setweight(to_tsvector('english', coalesce(title, '')), 'A') ||\n setweight(to_tsvector('english', coalesce(tags::text, '')), 'B') ||\n setweight(to_tsvector('english', coalesce(body, '')), 'C')) STORED\n );\n CREATE INDEX IF NOT EXISTS idx_obs_tsv ON observations USING GIN(tsv);\n CREATE INDEX IF NOT EXISTS idx_obs_project_time\n ON observations(project, created_at DESC);\n CREATE INDEX IF NOT EXISTS idx_obs_tags ON observations USING GIN(tags);\n\n ALTER TABLE observations ADD COLUMN IF NOT EXISTS embedder TEXT;\n ALTER TABLE observations ADD COLUMN IF NOT EXISTS author TEXT;\n ALTER TABLE observations ADD COLUMN IF NOT EXISTS status TEXT NOT NULL DEFAULT 'done';\n\n ALTER TABLE sessions ADD COLUMN IF NOT EXISTS updated_at BIGINT;\n ALTER TABLE sessions ADD COLUMN IF NOT EXISTS distilled_at BIGINT;\n\n CREATE TABLE IF NOT EXISTS symbols (\n id TEXT PRIMARY KEY,\n project TEXT NOT NULL,\n name TEXT NOT NULL,\n kind TEXT NOT NULL,\n file TEXT NOT NULL,\n line INT NOT NULL,\n lang TEXT NOT NULL,\n signature TEXT NOT NULL DEFAULT ''\n );\n CREATE INDEX IF NOT EXISTS idx_symbols_lookup ON symbols(project, name);\n CREATE INDEX IF NOT EXISTS idx_symbols_file ON symbols(project, file);\n\n CREATE TABLE IF NOT EXISTS symbol_edges (\n id TEXT PRIMARY KEY,\n project TEXT NOT NULL,\n src_id TEXT NOT NULL,\n src_name TEXT NOT NULL,\n dst_id TEXT NOT NULL DEFAULT '',\n dst_name TEXT NOT NULL,\n relation TEXT NOT NULL,\n confidence TEXT NOT NULL,\n score REAL NOT NULL DEFAULT 1.0,\n file TEXT NOT NULL,\n line INT NOT NULL\n );\n CREATE INDEX IF NOT EXISTS idx_edges_src ON symbol_edges(project, src_id);\n CREATE INDEX IF NOT EXISTS idx_edges_dst ON symbol_edges(project, dst_id);\n CREATE INDEX IF NOT EXISTS idx_edges_file ON symbol_edges(project, file);\n\n CREATE TABLE IF NOT EXISTS scanned_files (\n project TEXT NOT NULL,\n path TEXT NOT NULL,\n hash TEXT NOT NULL,\n scanned_at BIGINT NOT NULL,\n PRIMARY KEY (project, path)\n );\n";
2
+ export declare const SCHEMA_VERSION = 5;
3
+ export declare const DDL = "\n CREATE TABLE IF NOT EXISTS claude_db_meta (\n id INT PRIMARY KEY,\n version INT NOT NULL,\n vector_enabled BOOLEAN NOT NULL DEFAULT FALSE,\n vector_dims INT\n );\n\n CREATE TABLE IF NOT EXISTS sessions (\n id TEXT PRIMARY KEY,\n project TEXT NOT NULL,\n started_at BIGINT NOT NULL,\n ended_at BIGINT,\n summary TEXT\n );\n CREATE INDEX IF NOT EXISTS idx_sessions_project\n ON sessions(project, started_at DESC);\n\n CREATE TABLE IF NOT EXISTS observations (\n id TEXT PRIMARY KEY,\n session_id TEXT NOT NULL,\n project TEXT NOT NULL,\n kind TEXT NOT NULL,\n title TEXT NOT NULL,\n body TEXT NOT NULL,\n files JSONB NOT NULL DEFAULT '[]'::jsonb,\n tags JSONB NOT NULL DEFAULT '[]'::jsonb,\n created_at BIGINT NOT NULL,\n embedder TEXT,\n author TEXT,\n tsv TSVECTOR GENERATED ALWAYS AS (\n setweight(to_tsvector('english', coalesce(title, '')), 'A') ||\n setweight(to_tsvector('english', coalesce(tags::text, '')), 'B') ||\n setweight(to_tsvector('english', coalesce(body, '')), 'C')) STORED\n );\n CREATE INDEX IF NOT EXISTS idx_obs_tsv ON observations USING GIN(tsv);\n CREATE INDEX IF NOT EXISTS idx_obs_project_time\n ON observations(project, created_at DESC);\n CREATE INDEX IF NOT EXISTS idx_obs_tags ON observations USING GIN(tags);\n\n ALTER TABLE observations ADD COLUMN IF NOT EXISTS embedder TEXT;\n ALTER TABLE observations ADD COLUMN IF NOT EXISTS author TEXT;\n ALTER TABLE observations ADD COLUMN IF NOT EXISTS status TEXT NOT NULL DEFAULT 'done';\n\n ALTER TABLE sessions ADD COLUMN IF NOT EXISTS updated_at BIGINT;\n ALTER TABLE sessions ADD COLUMN IF NOT EXISTS distilled_at BIGINT;\n\n CREATE TABLE IF NOT EXISTS project_links (\n folder TEXT NOT NULL,\n project_key TEXT NOT NULL,\n first_seen BIGINT NOT NULL,\n PRIMARY KEY (folder, project_key)\n );\n CREATE INDEX IF NOT EXISTS idx_project_links_key ON project_links(project_key);\n\n CREATE TABLE IF NOT EXISTS symbols (\n id TEXT PRIMARY KEY,\n project TEXT NOT NULL,\n name TEXT NOT NULL,\n kind TEXT NOT NULL,\n file TEXT NOT NULL,\n line INT NOT NULL,\n lang TEXT NOT NULL,\n signature TEXT NOT NULL DEFAULT ''\n );\n CREATE INDEX IF NOT EXISTS idx_symbols_lookup ON symbols(project, name);\n CREATE INDEX IF NOT EXISTS idx_symbols_file ON symbols(project, file);\n\n CREATE TABLE IF NOT EXISTS symbol_edges (\n id TEXT PRIMARY KEY,\n project TEXT NOT NULL,\n src_id TEXT NOT NULL,\n src_name TEXT NOT NULL,\n dst_id TEXT NOT NULL DEFAULT '',\n dst_name TEXT NOT NULL,\n relation TEXT NOT NULL,\n confidence TEXT NOT NULL,\n score REAL NOT NULL DEFAULT 1.0,\n file TEXT NOT NULL,\n line INT NOT NULL\n );\n CREATE INDEX IF NOT EXISTS idx_edges_src ON symbol_edges(project, src_id);\n CREATE INDEX IF NOT EXISTS idx_edges_dst ON symbol_edges(project, dst_id);\n CREATE INDEX IF NOT EXISTS idx_edges_file ON symbol_edges(project, file);\n\n CREATE TABLE IF NOT EXISTS scanned_files (\n project TEXT NOT NULL,\n path TEXT NOT NULL,\n hash TEXT NOT NULL,\n scanned_at BIGINT NOT NULL,\n PRIMARY KEY (project, path)\n );\n";
@@ -2,7 +2,7 @@ export const TSV_EXPRESSION = `
2
2
  setweight(to_tsvector('english', coalesce(title, '')), 'A') ||
3
3
  setweight(to_tsvector('english', coalesce(tags::text, '')), 'B') ||
4
4
  setweight(to_tsvector('english', coalesce(body, '')), 'C')`;
5
- export const SCHEMA_VERSION = 4;
5
+ export const SCHEMA_VERSION = 5;
6
6
  export const DDL = `
7
7
  CREATE TABLE IF NOT EXISTS claude_db_meta (
8
8
  id INT PRIMARY KEY,
@@ -47,6 +47,14 @@ export const DDL = `
47
47
  ALTER TABLE sessions ADD COLUMN IF NOT EXISTS updated_at BIGINT;
48
48
  ALTER TABLE sessions ADD COLUMN IF NOT EXISTS distilled_at BIGINT;
49
49
 
50
+ CREATE TABLE IF NOT EXISTS project_links (
51
+ folder TEXT NOT NULL,
52
+ project_key TEXT NOT NULL,
53
+ first_seen BIGINT NOT NULL,
54
+ PRIMARY KEY (folder, project_key)
55
+ );
56
+ CREATE INDEX IF NOT EXISTS idx_project_links_key ON project_links(project_key);
57
+
50
58
  CREATE TABLE IF NOT EXISTS symbols (
51
59
  id TEXT PRIMARY KEY,
52
60
  project TEXT NOT NULL,
@@ -1,6 +1,9 @@
1
1
  import { appendScope } from './filters.js';
2
2
  import { toIndexEntry } from './rows.js';
3
+ import { noProjects } from '../project-scope.js';
3
4
  export async function searchKeyword(pool, query) {
5
+ if (noProjects(query.project))
6
+ return [];
4
7
  const conditions = ['tsv @@ q.tsq'];
5
8
  const values = [query.text];
6
9
  appendScope(query, conditions, values);
@@ -1,7 +1,7 @@
1
1
  import type { Pool } from './driver.js';
2
- import type { Session } from '../../types.js';
2
+ import type { ProjectFilter, Session } from '../../types.js';
3
3
  export declare function upsertSession(pool: Pool, session: Session): Promise<void>;
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
- export declare function recentSessions(pool: Pool, project: string, limit: number): Promise<Session[]>;
6
+ export declare function recentSessions(pool: Pool, project: ProjectFilter, limit: number): Promise<Session[]>;
7
7
  export declare function sessionProjects(pool: Pool): Promise<string[]>;
@@ -1,5 +1,7 @@
1
+ import { projectClause } from './filters.js';
1
2
  import { toSession } from './rows.js';
2
3
  import { summaryTime } from '../session-time.js';
4
+ import { noProjects } from '../project-scope.js';
3
5
  export async function upsertSession(pool, session) {
4
6
  await pool.query(`INSERT INTO sessions (id, project, started_at, ended_at, summary, updated_at, distilled_at)
5
7
  VALUES ($1, $2, $3, $4, $5, $6, $7)
@@ -28,9 +30,14 @@ export async function clearSummary(pool, id) {
28
30
  return (result.rowCount ?? 0) > 0;
29
31
  }
30
32
  export async function recentSessions(pool, project, limit) {
33
+ if (noProjects(project))
34
+ return [];
35
+ const values = [];
36
+ const scope = projectClause(project, values) ?? 'TRUE';
37
+ values.push(limit);
31
38
  const res = await pool.query(`SELECT * FROM sessions
32
- WHERE project = $1 AND summary IS NOT NULL
33
- ORDER BY started_at DESC LIMIT $2`, [project, limit]);
39
+ WHERE ${scope} AND summary IS NOT NULL
40
+ ORDER BY started_at DESC LIMIT $${values.length}`, values);
34
41
  return res.rows.map(toSession);
35
42
  }
36
43
  export async function sessionProjects(pool) {
@@ -1,6 +1,9 @@
1
1
  import { appendScope } from './filters.js';
2
2
  import { toIndexEntry } from './rows.js';
3
+ import { noProjects } from '../project-scope.js';
3
4
  export async function searchVector(pool, vectorEnabled, vectorDims, vector, query) {
5
+ if (noProjects(query.project))
6
+ return [];
4
7
  if (!vectorEnabled || vectorDims !== vector.length)
5
8
  return [];
6
9
  const conditions = ['embedding IS NOT NULL'];
@@ -0,0 +1,16 @@
1
+ import type { MemoryStore } from './adapter.js';
2
+ export interface ScopeResolverOptions {
3
+ store: Pick<MemoryStore, 'linkProject' | 'projectScope'>;
4
+ remote: string;
5
+ isFolder?: (value: string) => boolean;
6
+ keyOf?: (folder: string, remote: string) => string;
7
+ }
8
+ export interface ProjectScope {
9
+ key: string;
10
+ names: string[];
11
+ }
12
+ export interface ScopeResolver {
13
+ resolve(value: string): Promise<ProjectScope>;
14
+ }
15
+ export declare function isExistingFolder(value: string): boolean;
16
+ export declare function createScopeResolver(options: ScopeResolverOptions): ScopeResolver;
@@ -0,0 +1,49 @@
1
+ import { statSync } from 'node:fs';
2
+ import { isAbsolute } from 'node:path';
3
+ import { projectKey } from '../util/project-key.js';
4
+ import { orderScope } from './project-scope.js';
5
+ export function isExistingFolder(value) {
6
+ if (!isAbsolute(value))
7
+ return false;
8
+ try {
9
+ return statSync(value, { throwIfNoEntry: false })?.isDirectory() === true;
10
+ }
11
+ catch {
12
+ return false;
13
+ }
14
+ }
15
+ export function createScopeResolver(options) {
16
+ const { store, remote, isFolder = isExistingFolder, keyOf = (folder, name) => projectKey(folder, { remote: name }), } = options;
17
+ const keys = new Map();
18
+ async function linkedKey(folder) {
19
+ const key = keyOf(folder, remote);
20
+ try {
21
+ await store.linkProject(folder, key);
22
+ }
23
+ catch {
24
+ return key;
25
+ }
26
+ return key;
27
+ }
28
+ async function namesFor(folder, key) {
29
+ try {
30
+ return orderScope(folder, [...(await store.projectScope(folder)), key]);
31
+ }
32
+ catch {
33
+ return orderScope(folder, [key]);
34
+ }
35
+ }
36
+ return {
37
+ async resolve(value) {
38
+ if (!isFolder(value))
39
+ return { key: value, names: [value] };
40
+ let pending = keys.get(value);
41
+ if (!pending) {
42
+ pending = linkedKey(value);
43
+ keys.set(value, pending);
44
+ }
45
+ const key = await pending;
46
+ return { key, names: await namesFor(value, key) };
47
+ },
48
+ };
49
+ }
@@ -0,0 +1,5 @@
1
+ import type { ProjectFilter } from '../types.js';
2
+ export declare function projectsOf(filter: ProjectFilter | undefined): string[];
3
+ export declare function noProjects(filter: ProjectFilter | undefined): boolean;
4
+ export declare function isLinkable(folder: string, key: string): boolean;
5
+ export declare function orderScope(folder: string, related: Iterable<string>): string[];
@@ -0,0 +1,16 @@
1
+ export function projectsOf(filter) {
2
+ if (filter === undefined)
3
+ return [];
4
+ const values = typeof filter === 'string' ? [filter] : filter;
5
+ return [...new Set(values.filter((value) => value.length > 0))];
6
+ }
7
+ export function noProjects(filter) {
8
+ return typeof filter !== 'string' && filter !== undefined && projectsOf(filter).length === 0;
9
+ }
10
+ export function isLinkable(folder, key) {
11
+ return folder.trim().length > 0 && key.trim().length > 0 && folder !== key;
12
+ }
13
+ export function orderScope(folder, related) {
14
+ const others = [...new Set(related)].filter((value) => value !== folder).sort();
15
+ return [folder, ...others];
16
+ }
@@ -0,0 +1,3 @@
1
+ import type { MemoryStore } from './adapter.js';
2
+ import type { ScopeResolver } from './project-resolver.js';
3
+ export declare function withProjectScope(inner: MemoryStore, resolver: ScopeResolver): MemoryStore;
@@ -0,0 +1,49 @@
1
+ export function withProjectScope(inner, resolver) {
2
+ const keyOf = async (project) => (await resolver.resolve(project)).key;
3
+ async function expand(filter) {
4
+ if (filter === undefined)
5
+ return undefined;
6
+ const values = typeof filter === 'string' ? [filter] : filter;
7
+ const scopes = await Promise.all(values.map((value) => resolver.resolve(value)));
8
+ const names = [...new Set(scopes.flatMap((scope) => scope.names))];
9
+ return typeof filter === 'string' && names.length === 1 ? names[0] : names;
10
+ }
11
+ const overrides = {
12
+ async insertObservations(observations) {
13
+ const keyed = await Promise.all(observations.map(async (obs) => ({ ...obs, project: await keyOf(obs.project) })));
14
+ return inner.insertObservations(keyed);
15
+ },
16
+ async upsertSession(session) {
17
+ return inner.upsertSession({ ...session, project: await keyOf(session.project) });
18
+ },
19
+ async recentSessions(project, limit) {
20
+ return inner.recentSessions((await expand(project)) ?? project, limit);
21
+ },
22
+ async remove(filter) {
23
+ const project = await expand(filter.project);
24
+ return inner.remove(project === undefined ? filter : { ...filter, project });
25
+ },
26
+ async list(filter) {
27
+ const project = await expand(filter.project);
28
+ return inner.list(project === undefined ? filter : { ...filter, project });
29
+ },
30
+ async searchKeyword(query) {
31
+ const project = await expand(query.project);
32
+ return inner.searchKeyword(project === undefined ? query : { ...query, project });
33
+ },
34
+ async searchVector(vector, query) {
35
+ const project = await expand(query.project);
36
+ return inner.searchVector(vector, project === undefined ? query : { ...query, project });
37
+ },
38
+ };
39
+ return new Proxy(inner, {
40
+ get(target, property, receiver) {
41
+ if (property in overrides)
42
+ return overrides[property];
43
+ const value = Reflect.get(target, property, receiver);
44
+ if (typeof value !== 'function')
45
+ return value;
46
+ return value.bind(target);
47
+ },
48
+ });
49
+ }
@@ -1,9 +1,10 @@
1
- import type { RemoveFilter, SearchQuery } from '../../types.js';
1
+ import type { ProjectFilter, RemoveFilter, SearchQuery } from '../../types.js';
2
+ export declare function projectClause(column: string, filter: ProjectFilter | undefined, params: unknown[]): string | null;
2
3
  export declare function removeWhere(filter: RemoveFilter): {
3
4
  where: string;
4
5
  params: unknown[];
5
6
  };
6
7
  export declare const TAG_PREDICATE: (prefix: string) => string;
7
8
  export declare function appendScope(query: SearchQuery, conditions: string[], params: unknown[], prefix: string): void;
8
- export declare function toMatchExpression(text: string, project?: string): string | null;
9
+ export declare function toMatchExpression(text: string, project?: ProjectFilter): string | null;
9
10
  export declare function toFilePath(uri: string): string;
@@ -1,7 +1,17 @@
1
+ import { projectsOf } from '../project-scope.js';
1
2
  import { partitionIds } from '../../util/shortid.js';
2
3
  import { resolve } from 'node:path';
3
4
  import { scopeToken } from '../../util/scope.js';
4
5
  import { meaningfulTokens } from '../../search/stopwords.js';
6
+ export function projectClause(column, filter, params) {
7
+ const projects = projectsOf(filter);
8
+ if (projects.length === 0)
9
+ return null;
10
+ params.push(...projects);
11
+ return projects.length === 1
12
+ ? `${column} = ?`
13
+ : `${column} IN (${projects.map(() => '?').join(',')})`;
14
+ }
5
15
  export function removeWhere(filter) {
6
16
  const conditions = [];
7
17
  const params = [];
@@ -18,10 +28,9 @@ export function removeWhere(filter) {
18
28
  }
19
29
  conditions.push(`(${alternatives.join(' OR ')})`);
20
30
  }
21
- if (filter.project) {
22
- conditions.push('project = ?');
23
- params.push(filter.project);
24
- }
31
+ const project = projectClause('project', filter.project, params);
32
+ if (project)
33
+ conditions.push(project);
25
34
  if (filter.kind) {
26
35
  conditions.push('kind = ?');
27
36
  params.push(filter.kind);
@@ -38,10 +47,9 @@ export function removeWhere(filter) {
38
47
  export const TAG_PREDICATE = (prefix) => `EXISTS (SELECT 1 FROM json_each(${prefix}tags) WHERE value = ?)`;
39
48
  export function appendScope(query, conditions, params, prefix) {
40
49
  conditions.push(`${prefix}status != 'replaced'`);
41
- if (query.project) {
42
- conditions.push(`${prefix}project = ?`);
43
- params.push(query.project);
44
- }
50
+ const project = projectClause(`${prefix}project`, query.project, params);
51
+ if (project)
52
+ conditions.push(project);
45
53
  if (query.kind) {
46
54
  conditions.push(`${prefix}kind = ?`);
47
55
  params.push(query.kind);
@@ -63,9 +71,15 @@ export function appendScope(query, conditions, params, prefix) {
63
71
  params.push(...query.excludeSessions);
64
72
  }
65
73
  }
74
+ function scopeExpression(filter) {
75
+ const tokens = projectsOf(filter).map(scopeToken);
76
+ if (tokens.length === 0)
77
+ return null;
78
+ return tokens.length === 1 ? `scope:${tokens[0]}` : `scope:(${tokens.join(' OR ')})`;
79
+ }
66
80
  export function toMatchExpression(text, project) {
67
81
  const tokens = meaningfulTokens(text);
68
- const scope = project ? `scope:${scopeToken(project)}` : null;
82
+ const scope = scopeExpression(project);
69
83
  if (tokens.length === 0)
70
84
  return null;
71
85
  const terms = tokens.map((token) => `"${token}"`).join(' OR ');
@@ -1,4 +1,4 @@
1
- import type { CodeEdge, CodeSymbol, EdgeFilter, ListFilter, Observation, ObservationIndexEntry, RemoveFilter, ScannedFile, SearchQuery, Session, SymbolFilter, TimelineQuery } from '../../types.js';
1
+ import type { CodeEdge, CodeSymbol, EdgeFilter, ListFilter, Observation, ObservationIndexEntry, ProjectFilter, RemoveFilter, ScannedFile, SearchQuery, Session, SymbolFilter, TimelineQuery } from '../../types.js';
2
2
  import type { MemoryStore, ProjectSummary } from '../adapter.js';
3
3
  export declare const HERE: string;
4
4
  export declare class SqliteStore implements MemoryStore {
@@ -9,9 +9,11 @@ export declare class SqliteStore implements MemoryStore {
9
9
  init(): Promise<void>;
10
10
  close(): Promise<void>;
11
11
  ping(): Promise<boolean>;
12
+ linkProject(folder: string, key: string, now?: number): Promise<void>;
13
+ projectScope(folder: string): Promise<string[]>;
12
14
  upsertSession(session: Session): Promise<void>;
13
15
  getSession(id: string): Promise<Session | null>;
14
- recentSessions(project: string, limit: number): Promise<Session[]>;
16
+ recentSessions(project: ProjectFilter, limit: number): Promise<Session[]>;
15
17
  clearSummary(id: string): Promise<boolean>;
16
18
  insertObservations(observations: Observation[]): Promise<void>;
17
19
  getObservations(ids: string[]): Promise<Observation[]>;
@@ -2,6 +2,7 @@ import * as observationsOps from './observations.js';
2
2
  import * as searchOps from './search.js';
3
3
  import * as sessionsOps from './sessions.js';
4
4
  import * as graphOps from './graph.js';
5
+ import * as projectLinksOps from './project-links.js';
5
6
  import { DatabaseSync } from 'node:sqlite';
6
7
  import { toFilePath } from './filters.js';
7
8
  import { dirname, resolve } from 'node:path';
@@ -66,6 +67,12 @@ export class SqliteStore {
66
67
  async ping() {
67
68
  return this.db.prepare('SELECT 1 AS ok').get() !== undefined;
68
69
  }
70
+ async linkProject(folder, key, now = Date.now()) {
71
+ return projectLinksOps.linkProject(this.db, folder, key, now);
72
+ }
73
+ async projectScope(folder) {
74
+ return projectLinksOps.projectScope(this.db, folder);
75
+ }
69
76
  async upsertSession(session) {
70
77
  return sessionsOps.upsertSession(this.db, session);
71
78
  }
@@ -1,9 +1,10 @@
1
1
  import { foreignNames, isWholeScope } from '../adapter.js';
2
2
  import { toObservation } from './rows.js';
3
- import { removeWhere } from './filters.js';
3
+ import { 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';
7
+ import { noProjects } from '../project-scope.js';
7
8
  export async function insertObservations(db, observations) {
8
9
  if (observations.length === 0)
9
10
  return;
@@ -43,6 +44,8 @@ export async function getObservations(db, ids) {
43
44
  return rows.map(toObservation);
44
45
  }
45
46
  export async function remove(db, filter) {
47
+ if (noProjects(filter.project))
48
+ return 0;
46
49
  if (filter.ids?.length === 0)
47
50
  return 0;
48
51
  const { where, params } = removeWhere(filter);
@@ -54,8 +57,10 @@ export async function remove(db, filter) {
54
57
  try {
55
58
  db.prepare(`DELETE FROM observations ${where}`).run(...params);
56
59
  if (wipe) {
57
- const scope = filter.project ? 'WHERE project = ?' : '';
58
- const scopeParams = (filter.project ? [filter.project] : []);
60
+ const wipeParams = [];
61
+ const wipeClause = projectClause('project', filter.project, wipeParams);
62
+ const scope = wipeClause ? `WHERE ${wipeClause}` : '';
63
+ const scopeParams = wipeParams;
59
64
  db.prepare(`DELETE FROM sessions ${scope}`).run(...scopeParams);
60
65
  for (const table of ['symbols', 'symbol_edges', 'scanned_files']) {
61
66
  db.prepare(`DELETE FROM ${table} ${scope}`).run(...scopeParams);
@@ -72,12 +77,13 @@ export async function remove(db, filter) {
72
77
  return count?.n ?? 0;
73
78
  }
74
79
  export async function list(db, filter) {
80
+ if (noProjects(filter.project))
81
+ return [];
75
82
  const conditions = [];
76
83
  const params = [];
77
- if (filter.project) {
78
- conditions.push('project = ?');
79
- params.push(filter.project);
80
- }
84
+ const project = projectClause('project', filter.project, params);
85
+ if (project)
86
+ conditions.push(project);
81
87
  if (filter.sessionId) {
82
88
  conditions.push('session_id = ?');
83
89
  params.push(filter.sessionId);
@@ -0,0 +1,3 @@
1
+ import type { DatabaseSync } from 'node:sqlite';
2
+ export declare function linkProject(db: DatabaseSync, folder: string, key: string, now: number): Promise<void>;
3
+ export declare function projectScope(db: DatabaseSync, folder: string): Promise<string[]>;
@@ -0,0 +1,15 @@
1
+ import { isLinkable, orderScope } from '../project-scope.js';
2
+ export async function linkProject(db, folder, key, now) {
3
+ if (!isLinkable(folder, key))
4
+ return;
5
+ db.prepare('INSERT OR IGNORE INTO project_links (folder, project_key, first_seen) VALUES (?, ?, ?)').run(folder, key, now);
6
+ }
7
+ export async function projectScope(db, folder) {
8
+ const rows = db
9
+ .prepare(`SELECT project_key AS value FROM project_links WHERE folder = ?
10
+ UNION
11
+ SELECT folder AS value FROM project_links
12
+ WHERE project_key IN (SELECT project_key FROM project_links WHERE folder = ?)`)
13
+ .all(folder, folder);
14
+ return orderScope(folder, rows.map((row) => String(row['value'])));
15
+ }
@@ -102,3 +102,12 @@ CREATE TABLE IF NOT EXISTS scanned_files (
102
102
  scanned_at INTEGER NOT NULL,
103
103
  PRIMARY KEY (project, path)
104
104
  );
105
+
106
+ CREATE TABLE IF NOT EXISTS project_links (
107
+ folder TEXT NOT NULL,
108
+ project_key TEXT NOT NULL,
109
+ first_seen INTEGER NOT NULL,
110
+ PRIMARY KEY (folder, project_key)
111
+ );
112
+
113
+ CREATE INDEX IF NOT EXISTS idx_project_links_key ON project_links(project_key);