semantic-atlas 0.1.2 → 0.1.3

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 (32) hide show
  1. package/.agents/skills/semantic-atlas/SKILL.md +2 -2
  2. package/.agents/skills/semantic-atlas/references/snapshot-bootstrap.md +5 -1
  3. package/README.md +15 -14
  4. package/README.zh-CN.md +15 -14
  5. package/dist/cli/cli-application.d.ts.map +1 -1
  6. package/dist/cli/cli-application.js +5 -5
  7. package/dist/graph/graph-store.d.ts +6 -0
  8. package/dist/graph/graph-store.d.ts.map +1 -1
  9. package/dist/graph/graph-store.js +261 -114
  10. package/dist/storage/atlas-database.d.ts +6 -2
  11. package/dist/storage/atlas-database.d.ts.map +1 -1
  12. package/dist/storage/atlas-database.js +388 -514
  13. package/dist/storage/snapshot-store.d.ts.map +1 -1
  14. package/dist/storage/snapshot-store.js +7 -10
  15. package/dist/structural-backend/codegraph-backend.d.ts +4 -1
  16. package/dist/structural-backend/codegraph-backend.d.ts.map +1 -1
  17. package/dist/structural-backend/codegraph-backend.js +10 -4
  18. package/dist/structural-backend/structural-projection-bootstrapper.d.ts +10 -0
  19. package/dist/structural-backend/structural-projection-bootstrapper.d.ts.map +1 -0
  20. package/dist/structural-backend/structural-projection-bootstrapper.js +295 -0
  21. package/dist/structural-backend/structural-publication.d.ts +1 -0
  22. package/dist/structural-backend/structural-publication.d.ts.map +1 -1
  23. package/dist/structural-backend/structural-publication.js +5 -2
  24. package/dist/world/world-model-service.d.ts.map +1 -1
  25. package/dist/world/world-model-service.js +14 -6
  26. package/dist/world/world-snapshot-store.d.ts.map +1 -1
  27. package/dist/world/world-snapshot-store.js +92 -66
  28. package/docs/architecture/codegraph-backend.md +26 -29
  29. package/docs/contracts/cli-v1.md +2 -1
  30. package/docs/contracts/graph-model.md +2 -2
  31. package/docs/product-contract.md +20 -17
  32. package/package.json +1 -1
@@ -1,484 +1,22 @@
1
- import { lstatSync, realpathSync } from "node:fs";
1
+ import { lstatSync, mkdirSync, realpathSync } from "node:fs";
2
2
  import { createRequire } from "node:module";
3
- import { join, resolve } from "node:path";
4
- export const CURRENT_ATLAS_SCHEMA_VERSION = 5;
3
+ import { homedir } from "node:os";
4
+ import { isAbsolute, join, relative, resolve } from "node:path";
5
+ export const CURRENT_ATLAS_SCHEMA_VERSION = 1;
6
+ export const SEMANTIC_ATLAS_HOME_ENVIRONMENT_VARIABLE = "SEMANTIC_ATLAS_HOME";
5
7
  const require = createRequire(import.meta.url);
6
- const migrations = [
7
- {
8
- version: 1,
9
- sql: `
10
- CREATE TABLE atlas_repositories (
11
- repository_id TEXT PRIMARY KEY,
12
- common_git_directory TEXT NOT NULL,
13
- worktree_root TEXT NOT NULL,
14
- latest_snapshot_id TEXT,
15
- updated_at TEXT NOT NULL
16
- ) STRICT;
17
-
18
- CREATE TABLE atlas_repository_snapshots (
19
- repository_id TEXT NOT NULL,
20
- snapshot_id TEXT NOT NULL,
21
- payload TEXT NOT NULL,
22
- created_at TEXT NOT NULL,
23
- PRIMARY KEY (repository_id, snapshot_id),
24
- FOREIGN KEY (repository_id)
25
- REFERENCES atlas_repositories(repository_id) ON DELETE CASCADE
26
- ) STRICT;
27
-
28
- CREATE TABLE atlas_business_nodes (
29
- node_id INTEGER PRIMARY KEY,
30
- repository_id TEXT NOT NULL,
31
- node_key TEXT NOT NULL,
32
- base_snapshot_id TEXT NOT NULL,
33
- kind TEXT NOT NULL CHECK (kind IN (
34
- 'Capability', 'Scenario', 'Operation', 'Invariant', 'Interface', 'Data'
35
- )),
36
- label TEXT NOT NULL,
37
- summary TEXT NOT NULL,
38
- certainty TEXT NOT NULL CHECK (certainty IN ('exact', 'inferred', 'hypothesis')),
39
- UNIQUE (repository_id, node_key),
40
- FOREIGN KEY (repository_id, base_snapshot_id)
41
- REFERENCES atlas_repository_snapshots(repository_id, snapshot_id)
42
- ) STRICT;
43
-
44
- CREATE TABLE atlas_business_node_aliases (
45
- node_id INTEGER NOT NULL,
46
- position INTEGER NOT NULL,
47
- alias TEXT NOT NULL,
48
- PRIMARY KEY (node_id, position),
49
- FOREIGN KEY (node_id) REFERENCES atlas_business_nodes(node_id) ON DELETE CASCADE
50
- ) STRICT;
51
-
52
- CREATE TABLE atlas_business_node_evidence (
53
- node_id INTEGER NOT NULL,
54
- position INTEGER NOT NULL,
55
- structural_reference TEXT NOT NULL,
56
- file TEXT NOT NULL,
57
- start_line INTEGER NOT NULL,
58
- start_column INTEGER NOT NULL,
59
- end_line INTEGER NOT NULL,
60
- end_column INTEGER NOT NULL,
61
- content_hash TEXT NOT NULL,
62
- PRIMARY KEY (node_id, position),
63
- FOREIGN KEY (node_id) REFERENCES atlas_business_nodes(node_id) ON DELETE CASCADE
64
- ) STRICT;
65
-
66
- CREATE TABLE atlas_business_relations (
67
- relation_id INTEGER PRIMARY KEY,
68
- repository_id TEXT NOT NULL,
69
- base_snapshot_id TEXT NOT NULL,
70
- from_key TEXT NOT NULL,
71
- relation_type TEXT NOT NULL CHECK (relation_type IN (
72
- 'part_of', 'realized_by', 'reads', 'writes', 'publishes', 'consumes',
73
- 'constrained_by', 'verified_by'
74
- )),
75
- to_domain TEXT NOT NULL CHECK (to_domain IN ('structural', 'business')),
76
- to_key TEXT NOT NULL,
77
- certainty TEXT NOT NULL CHECK (certainty IN ('exact', 'inferred', 'hypothesis')),
78
- UNIQUE (repository_id, from_key, relation_type, to_domain, to_key),
79
- FOREIGN KEY (repository_id, base_snapshot_id)
80
- REFERENCES atlas_repository_snapshots(repository_id, snapshot_id),
81
- FOREIGN KEY (repository_id, from_key)
82
- REFERENCES atlas_business_nodes(repository_id, node_key)
83
- ) STRICT;
84
-
85
- CREATE INDEX atlas_business_relations_from_index
86
- ON atlas_business_relations (repository_id, from_key);
87
- CREATE INDEX atlas_business_relations_to_index
88
- ON atlas_business_relations (repository_id, to_domain, to_key);
89
-
90
- CREATE TABLE atlas_business_relation_evidence (
91
- relation_id INTEGER NOT NULL,
92
- position INTEGER NOT NULL,
93
- structural_reference TEXT NOT NULL,
94
- file TEXT NOT NULL,
95
- start_line INTEGER NOT NULL,
96
- start_column INTEGER NOT NULL,
97
- end_line INTEGER NOT NULL,
98
- end_column INTEGER NOT NULL,
99
- content_hash TEXT NOT NULL,
100
- PRIMARY KEY (relation_id, position),
101
- FOREIGN KEY (relation_id)
102
- REFERENCES atlas_business_relations(relation_id) ON DELETE CASCADE
103
- ) STRICT;
104
-
105
- CREATE TABLE atlas_business_node_validity (
106
- node_id INTEGER NOT NULL,
107
- repository_id TEXT NOT NULL,
108
- snapshot_id TEXT NOT NULL,
109
- validity TEXT NOT NULL CHECK (validity IN ('valid', 'stale')),
110
- PRIMARY KEY (node_id, snapshot_id),
111
- FOREIGN KEY (node_id) REFERENCES atlas_business_nodes(node_id) ON DELETE CASCADE,
112
- FOREIGN KEY (repository_id, snapshot_id)
113
- REFERENCES atlas_repository_snapshots(repository_id, snapshot_id) ON DELETE CASCADE
114
- ) STRICT;
115
-
116
- CREATE TABLE atlas_business_relation_validity (
117
- relation_id INTEGER NOT NULL,
118
- repository_id TEXT NOT NULL,
119
- snapshot_id TEXT NOT NULL,
120
- validity TEXT NOT NULL CHECK (validity IN ('valid', 'stale')),
121
- PRIMARY KEY (relation_id, snapshot_id),
122
- FOREIGN KEY (relation_id)
123
- REFERENCES atlas_business_relations(relation_id) ON DELETE CASCADE,
124
- FOREIGN KEY (repository_id, snapshot_id)
125
- REFERENCES atlas_repository_snapshots(repository_id, snapshot_id) ON DELETE CASCADE
126
- ) STRICT;
127
-
128
- CREATE TABLE atlas_graph_search (
129
- repository_id TEXT NOT NULL,
130
- node_key TEXT NOT NULL,
131
- label TEXT NOT NULL,
132
- aliases TEXT NOT NULL,
133
- summary TEXT NOT NULL,
134
- symbols TEXT NOT NULL,
135
- paths TEXT NOT NULL,
136
- PRIMARY KEY (repository_id, node_key),
137
- FOREIGN KEY (repository_id, node_key)
138
- REFERENCES atlas_business_nodes(repository_id, node_key) ON DELETE CASCADE
139
- ) STRICT;
140
- `,
141
- },
142
- {
143
- version: 2,
144
- sql: `
145
- CREATE TABLE atlas_world_state (
146
- repository_id TEXT PRIMARY KEY,
147
- status TEXT NOT NULL CHECK (status IN ('missing', 'building', 'current', 'failed')),
148
- current_snapshot_id TEXT,
149
- target_snapshot_id TEXT,
150
- backend_version TEXT,
151
- extraction_version INTEGER,
152
- failure_message TEXT,
153
- started_at TEXT,
154
- published_at TEXT,
155
- updated_at TEXT NOT NULL,
156
- FOREIGN KEY (repository_id) REFERENCES atlas_repositories(repository_id) ON DELETE CASCADE,
157
- FOREIGN KEY (repository_id, current_snapshot_id)
158
- REFERENCES atlas_repository_snapshots(repository_id, snapshot_id)
159
- ) STRICT;
160
-
161
- CREATE TABLE atlas_semantic_changes (
162
- repository_id TEXT NOT NULL,
163
- from_snapshot_id TEXT,
164
- to_snapshot_id TEXT NOT NULL,
165
- added_paths TEXT NOT NULL,
166
- modified_paths TEXT NOT NULL,
167
- removed_paths TEXT NOT NULL,
168
- stale_assertions TEXT NOT NULL,
169
- created_at TEXT NOT NULL,
170
- PRIMARY KEY (repository_id, to_snapshot_id),
171
- FOREIGN KEY (repository_id, to_snapshot_id)
172
- REFERENCES atlas_repository_snapshots(repository_id, snapshot_id) ON DELETE CASCADE
173
- ) STRICT;
174
-
175
- ALTER TABLE atlas_business_node_evidence ADD COLUMN qualified_symbol TEXT;
176
- ALTER TABLE atlas_business_node_evidence ADD COLUMN structural_kind TEXT;
177
- ALTER TABLE atlas_business_node_evidence ADD COLUMN atlas_snapshot_id TEXT;
178
- ALTER TABLE atlas_business_node_evidence ADD COLUMN backend_version TEXT;
179
- ALTER TABLE atlas_business_node_evidence ADD COLUMN backend_locator TEXT;
180
- ALTER TABLE atlas_business_node_evidence ADD COLUMN binding_status TEXT NOT NULL DEFAULT 'unresolved'
181
- CHECK (binding_status IN ('bound', 'missing', 'ambiguous', 'unresolved'));
182
-
183
- ALTER TABLE atlas_business_relation_evidence ADD COLUMN qualified_symbol TEXT;
184
- ALTER TABLE atlas_business_relation_evidence ADD COLUMN structural_kind TEXT;
185
- ALTER TABLE atlas_business_relation_evidence ADD COLUMN atlas_snapshot_id TEXT;
186
- ALTER TABLE atlas_business_relation_evidence ADD COLUMN backend_version TEXT;
187
- ALTER TABLE atlas_business_relation_evidence ADD COLUMN backend_locator TEXT;
188
- ALTER TABLE atlas_business_relation_evidence ADD COLUMN binding_status TEXT NOT NULL DEFAULT 'unresolved'
189
- CHECK (binding_status IN ('bound', 'missing', 'ambiguous', 'unresolved'));
190
-
191
- INSERT INTO atlas_world_state (repository_id, status, updated_at)
192
- SELECT repository_id, 'missing', updated_at
193
- FROM atlas_repositories;
194
- `,
195
- },
196
- {
197
- version: 3,
198
- sql: `
199
- ALTER TABLE atlas_business_relations ADD COLUMN target_file TEXT;
200
- ALTER TABLE atlas_business_relations ADD COLUMN target_qualified_symbol TEXT;
201
- ALTER TABLE atlas_business_relations ADD COLUMN target_structural_kind TEXT;
202
- ALTER TABLE atlas_business_relations ADD COLUMN target_start_line INTEGER;
203
- ALTER TABLE atlas_business_relations ADD COLUMN target_start_column INTEGER;
204
- ALTER TABLE atlas_business_relations ADD COLUMN target_end_line INTEGER;
205
- ALTER TABLE atlas_business_relations ADD COLUMN target_end_column INTEGER;
206
- ALTER TABLE atlas_business_relations ADD COLUMN target_atlas_snapshot_id TEXT;
207
- ALTER TABLE atlas_business_relations ADD COLUMN target_backend_version TEXT;
208
- ALTER TABLE atlas_business_relations ADD COLUMN target_backend_locator TEXT;
209
- ALTER TABLE atlas_business_relations ADD COLUMN target_binding_status TEXT NOT NULL DEFAULT 'unresolved'
210
- CHECK (target_binding_status IN ('bound', 'missing', 'ambiguous', 'unresolved'));
211
-
212
- UPDATE atlas_business_relations
213
- SET
214
- target_file = evidence.file,
215
- target_qualified_symbol = evidence.qualified_symbol,
216
- target_structural_kind = evidence.structural_kind,
217
- target_start_line = evidence.start_line,
218
- target_start_column = evidence.start_column,
219
- target_end_line = evidence.end_line,
220
- target_end_column = evidence.end_column,
221
- target_atlas_snapshot_id = evidence.atlas_snapshot_id,
222
- target_backend_version = evidence.backend_version,
223
- target_backend_locator = evidence.backend_locator,
224
- target_binding_status = evidence.binding_status
225
- FROM atlas_business_relation_evidence AS evidence
226
- WHERE atlas_business_relations.to_domain = 'structural'
227
- AND evidence.relation_id = atlas_business_relations.relation_id
228
- AND evidence.structural_reference = atlas_business_relations.to_key
229
- AND evidence.position = (
230
- SELECT MIN(candidate.position)
231
- FROM atlas_business_relation_evidence AS candidate
232
- WHERE candidate.relation_id = atlas_business_relations.relation_id
233
- AND candidate.structural_reference = atlas_business_relations.to_key
234
- );
235
- `,
236
- },
237
- {
238
- version: 4,
239
- sql: `
240
- CREATE TABLE atlas_world_publications (
241
- publication_id INTEGER PRIMARY KEY,
242
- repository_id TEXT NOT NULL,
243
- previous_publication_id INTEGER CHECK (
244
- previous_publication_id IS NULL OR previous_publication_id < publication_id
245
- ),
246
- snapshot_id TEXT NOT NULL,
247
- added_paths TEXT NOT NULL,
248
- modified_paths TEXT NOT NULL,
249
- removed_paths TEXT NOT NULL,
250
- stale_assertions TEXT NOT NULL,
251
- published_at TEXT NOT NULL,
252
- UNIQUE (repository_id, publication_id),
253
- FOREIGN KEY (repository_id)
254
- REFERENCES atlas_repositories(repository_id) ON DELETE CASCADE,
255
- FOREIGN KEY (repository_id, snapshot_id)
256
- REFERENCES atlas_repository_snapshots(repository_id, snapshot_id) ON DELETE CASCADE,
257
- FOREIGN KEY (repository_id, previous_publication_id)
258
- REFERENCES atlas_world_publications(repository_id, publication_id)
259
- ) STRICT;
260
-
261
- INSERT INTO atlas_world_publications (
262
- publication_id,
263
- repository_id,
264
- previous_publication_id,
265
- snapshot_id,
266
- added_paths,
267
- modified_paths,
268
- removed_paths,
269
- stale_assertions,
270
- published_at
271
- )
272
- SELECT
273
- rowid,
274
- repository_id,
275
- NULL,
276
- to_snapshot_id,
277
- added_paths,
278
- modified_paths,
279
- removed_paths,
280
- stale_assertions,
281
- created_at
282
- FROM atlas_semantic_changes
283
- ORDER BY rowid;
284
-
285
- UPDATE atlas_world_publications AS publication
286
- SET previous_publication_id = (
287
- SELECT previous.publication_id
288
- FROM atlas_semantic_changes AS legacy
289
- JOIN atlas_world_publications AS previous
290
- ON previous.repository_id = legacy.repository_id
291
- AND previous.snapshot_id = legacy.from_snapshot_id
292
- AND previous.publication_id < publication.publication_id
293
- WHERE legacy.rowid = publication.publication_id
294
- ORDER BY previous.publication_id DESC
295
- LIMIT 1
296
- );
297
-
298
- CREATE INDEX atlas_world_publications_repository_order_index
299
- ON atlas_world_publications (repository_id, publication_id DESC);
300
- CREATE INDEX atlas_world_publications_snapshot_index
301
- ON atlas_world_publications (repository_id, snapshot_id, publication_id DESC);
302
-
303
- ALTER TABLE atlas_world_state
304
- ADD COLUMN current_publication_id INTEGER REFERENCES atlas_world_publications(publication_id);
305
- UPDATE atlas_world_state
306
- SET current_publication_id = (
307
- SELECT publication.publication_id
308
- FROM atlas_world_publications AS publication
309
- WHERE publication.repository_id = atlas_world_state.repository_id
310
- AND publication.snapshot_id = atlas_world_state.current_snapshot_id
311
- ORDER BY publication.publication_id DESC
312
- LIMIT 1
313
- );
314
-
315
- DROP TABLE atlas_semantic_changes;
316
- `,
317
- },
318
- {
319
- version: 5,
320
- sql: `
321
- CREATE TABLE atlas_business_relations_v5 (
322
- relation_id INTEGER PRIMARY KEY,
323
- repository_id TEXT NOT NULL,
324
- base_snapshot_id TEXT NOT NULL,
325
- from_key TEXT NOT NULL,
326
- relation_type TEXT NOT NULL CHECK (relation_type IN (
327
- 'part_of', 'invokes', 'realized_by', 'reads', 'writes', 'publishes',
328
- 'consumes', 'constrained_by', 'verified_by'
329
- )),
330
- to_domain TEXT NOT NULL CHECK (to_domain IN ('structural', 'business')),
331
- to_key TEXT NOT NULL,
332
- certainty TEXT NOT NULL CHECK (certainty IN ('exact', 'inferred', 'hypothesis')),
333
- target_file TEXT,
334
- target_qualified_symbol TEXT,
335
- target_structural_kind TEXT,
336
- target_start_line INTEGER,
337
- target_start_column INTEGER,
338
- target_end_line INTEGER,
339
- target_end_column INTEGER,
340
- target_atlas_snapshot_id TEXT,
341
- target_backend_version TEXT,
342
- target_backend_locator TEXT,
343
- target_binding_status TEXT NOT NULL DEFAULT 'unresolved'
344
- CHECK (target_binding_status IN ('bound', 'missing', 'ambiguous', 'unresolved')),
345
- UNIQUE (repository_id, from_key, relation_type, to_domain, to_key),
346
- FOREIGN KEY (repository_id, base_snapshot_id)
347
- REFERENCES atlas_repository_snapshots(repository_id, snapshot_id),
348
- FOREIGN KEY (repository_id, from_key)
349
- REFERENCES atlas_business_nodes(repository_id, node_key)
350
- ) STRICT;
351
-
352
- INSERT INTO atlas_business_relations_v5 (
353
- relation_id,
354
- repository_id,
355
- base_snapshot_id,
356
- from_key,
357
- relation_type,
358
- to_domain,
359
- to_key,
360
- certainty,
361
- target_file,
362
- target_qualified_symbol,
363
- target_structural_kind,
364
- target_start_line,
365
- target_start_column,
366
- target_end_line,
367
- target_end_column,
368
- target_atlas_snapshot_id,
369
- target_backend_version,
370
- target_backend_locator,
371
- target_binding_status
372
- )
373
- SELECT
374
- relation_id,
375
- repository_id,
376
- base_snapshot_id,
377
- from_key,
378
- relation_type,
379
- to_domain,
380
- to_key,
381
- certainty,
382
- target_file,
383
- target_qualified_symbol,
384
- target_structural_kind,
385
- target_start_line,
386
- target_start_column,
387
- target_end_line,
388
- target_end_column,
389
- target_atlas_snapshot_id,
390
- target_backend_version,
391
- target_backend_locator,
392
- target_binding_status
393
- FROM atlas_business_relations;
394
-
395
- CREATE TABLE atlas_business_relation_evidence_v5 (
396
- relation_id INTEGER NOT NULL,
397
- position INTEGER NOT NULL,
398
- structural_reference TEXT NOT NULL,
399
- file TEXT NOT NULL,
400
- start_line INTEGER NOT NULL,
401
- start_column INTEGER NOT NULL,
402
- end_line INTEGER NOT NULL,
403
- end_column INTEGER NOT NULL,
404
- content_hash TEXT NOT NULL,
405
- qualified_symbol TEXT,
406
- structural_kind TEXT,
407
- atlas_snapshot_id TEXT,
408
- backend_version TEXT,
409
- backend_locator TEXT,
410
- binding_status TEXT NOT NULL DEFAULT 'unresolved'
411
- CHECK (binding_status IN ('bound', 'missing', 'ambiguous', 'unresolved')),
412
- PRIMARY KEY (relation_id, position),
413
- FOREIGN KEY (relation_id)
414
- REFERENCES atlas_business_relations_v5(relation_id) ON DELETE CASCADE
415
- ) STRICT;
416
-
417
- INSERT INTO atlas_business_relation_evidence_v5
418
- SELECT * FROM atlas_business_relation_evidence;
419
-
420
- CREATE TABLE atlas_business_relation_validity_v5 (
421
- relation_id INTEGER NOT NULL,
422
- repository_id TEXT NOT NULL,
423
- snapshot_id TEXT NOT NULL,
424
- validity TEXT NOT NULL CHECK (validity IN ('valid', 'stale')),
425
- PRIMARY KEY (relation_id, snapshot_id),
426
- FOREIGN KEY (relation_id)
427
- REFERENCES atlas_business_relations_v5(relation_id) ON DELETE CASCADE,
428
- FOREIGN KEY (repository_id, snapshot_id)
429
- REFERENCES atlas_repository_snapshots(repository_id, snapshot_id) ON DELETE CASCADE
430
- ) STRICT;
431
-
432
- INSERT INTO atlas_business_relation_validity_v5
433
- SELECT * FROM atlas_business_relation_validity;
434
-
435
- DROP TABLE atlas_business_relation_evidence;
436
- DROP TABLE atlas_business_relation_validity;
437
- DROP TABLE atlas_business_relations;
438
- ALTER TABLE atlas_business_relations_v5 RENAME TO atlas_business_relations;
439
- ALTER TABLE atlas_business_relation_evidence_v5
440
- RENAME TO atlas_business_relation_evidence;
441
- ALTER TABLE atlas_business_relation_validity_v5
442
- RENAME TO atlas_business_relation_validity;
443
-
444
- CREATE INDEX atlas_business_relations_from_index
445
- ON atlas_business_relations (repository_id, from_key);
446
- CREATE INDEX atlas_business_relations_to_index
447
- ON atlas_business_relations (repository_id, to_domain, to_key);
448
- `,
449
- },
450
- ];
451
8
  export class AtlasDatabase {
452
9
  databasePath;
453
10
  connection;
454
11
  #closed = false;
455
12
  constructor(repository) {
456
- const atlasDirectory = join(repository.worktreeRoot, ".atlas");
457
- const databasePath = join(atlasDirectory, "codegraph.db");
458
- requireExistingSharedDatabase(atlasDirectory, databasePath);
459
- this.databasePath = databasePath;
13
+ this.databasePath = resolveAtlasDatabasePath(repository);
460
14
  const { DatabaseSync } = require("node:sqlite");
461
- this.connection = new DatabaseSync(databasePath);
15
+ this.connection = new DatabaseSync(this.databasePath);
462
16
  try {
463
- this.connection.exec("PRAGMA foreign_keys = ON; PRAGMA journal_mode = WAL;");
464
- this.applyMigrations();
465
- this.connection.prepare(`
466
- INSERT INTO atlas_repositories (
467
- repository_id,
468
- common_git_directory,
469
- worktree_root,
470
- updated_at
471
- ) VALUES (?, ?, ?, ?)
472
- ON CONFLICT (repository_id) DO UPDATE SET
473
- common_git_directory = excluded.common_git_directory,
474
- worktree_root = excluded.worktree_root,
475
- updated_at = excluded.updated_at
476
- `).run(repository.repositoryId, repository.commonGitDirectory, repository.worktreeRoot, new Date().toISOString());
477
- this.connection.prepare(`
478
- INSERT INTO atlas_world_state (repository_id, status, updated_at)
479
- VALUES (?, 'missing', ?)
480
- ON CONFLICT (repository_id) DO NOTHING
481
- `).run(repository.repositoryId, new Date().toISOString());
17
+ this.connection.exec("PRAGMA foreign_keys = ON; PRAGMA journal_mode = WAL; PRAGMA busy_timeout = 5000;");
18
+ this.initializeSchema();
19
+ this.registerRepository(repository);
482
20
  }
483
21
  catch (error) {
484
22
  this.connection.close();
@@ -487,8 +25,9 @@ export class AtlasDatabase {
487
25
  }
488
26
  get schemaVersion() {
489
27
  const row = this.connection.prepare(`
490
- SELECT COALESCE(MAX(version), 0) AS version
491
- FROM atlas_schema_migrations
28
+ SELECT version
29
+ FROM atlas_schema
30
+ WHERE singleton = 1
492
31
  `).get();
493
32
  return row.version;
494
33
  }
@@ -502,51 +41,386 @@ export class AtlasDatabase {
502
41
  [Symbol.dispose]() {
503
42
  this.close();
504
43
  }
505
- applyMigrations() {
506
- this.connection.exec(`
507
- CREATE TABLE IF NOT EXISTS atlas_schema_migrations (
508
- version INTEGER PRIMARY KEY,
509
- applied_at TEXT NOT NULL
510
- ) STRICT;
511
- `);
512
- const currentVersion = this.schemaVersion;
513
- if (currentVersion > CURRENT_ATLAS_SCHEMA_VERSION) {
514
- throw new Error(`Atlas database schema ${currentVersion} is newer than supported schema ${CURRENT_ATLAS_SCHEMA_VERSION}`);
515
- }
516
- for (const migration of migrations) {
517
- this.connection.exec("BEGIN IMMEDIATE");
518
- try {
519
- const applied = this.connection.prepare(`
520
- SELECT 1 AS applied
521
- FROM atlas_schema_migrations
522
- WHERE version = ?
523
- `).get(migration.version);
524
- if (applied === undefined) {
525
- this.connection.exec(migration.sql);
526
- this.connection.prepare(`
527
- INSERT INTO atlas_schema_migrations (version, applied_at)
528
- VALUES (?, ?)
529
- `).run(migration.version, new Date().toISOString());
530
- }
531
- this.connection.exec("COMMIT");
532
- }
533
- catch (error) {
534
- this.connection.exec("ROLLBACK");
535
- throw error;
44
+ initializeSchema() {
45
+ const atlasSchema = this.connection.prepare(`
46
+ SELECT name
47
+ FROM sqlite_schema
48
+ WHERE type = 'table' AND name = 'atlas_schema'
49
+ `).get();
50
+ if (atlasSchema !== undefined) {
51
+ const version = this.schemaVersion;
52
+ if (version !== CURRENT_ATLAS_SCHEMA_VERSION) {
53
+ throw new Error(`Atlas database schema ${version} is not supported; expected schema ${CURRENT_ATLAS_SCHEMA_VERSION}`);
536
54
  }
55
+ return;
56
+ }
57
+ const existingTables = this.connection.prepare(`
58
+ SELECT COUNT(*) AS count
59
+ FROM sqlite_schema
60
+ WHERE type = 'table' AND name NOT LIKE 'sqlite_%'
61
+ `).get();
62
+ if (existingTables.count > 0) {
63
+ throw new Error("The Atlas database is not a fresh schema v1 store");
64
+ }
65
+ this.connection.exec("BEGIN IMMEDIATE");
66
+ try {
67
+ this.connection.exec(ATLAS_SCHEMA_V1);
68
+ this.connection.prepare(`
69
+ INSERT INTO atlas_schema (singleton, version, created_at)
70
+ VALUES (1, ?, ?)
71
+ `).run(CURRENT_ATLAS_SCHEMA_VERSION, new Date().toISOString());
72
+ this.connection.exec("COMMIT");
73
+ }
74
+ catch (error) {
75
+ this.connection.exec("ROLLBACK");
76
+ throw error;
77
+ }
78
+ }
79
+ registerRepository(repository) {
80
+ const timestamp = new Date().toISOString();
81
+ this.connection.exec("BEGIN IMMEDIATE");
82
+ try {
83
+ this.connection.prepare(`
84
+ INSERT INTO atlas_repositories (
85
+ repository_id,
86
+ common_git_directory,
87
+ updated_at
88
+ ) VALUES (?, ?, ?)
89
+ ON CONFLICT (repository_id) DO UPDATE SET
90
+ common_git_directory = excluded.common_git_directory,
91
+ updated_at = excluded.updated_at
92
+ `).run(repository.repositoryId, repository.commonGitDirectory, timestamp);
93
+ this.connection.prepare(`
94
+ INSERT INTO atlas_worktree_states (
95
+ repository_id,
96
+ git_directory,
97
+ worktree_root,
98
+ status,
99
+ updated_at
100
+ ) VALUES (?, ?, ?, 'missing', ?)
101
+ ON CONFLICT (repository_id, git_directory) DO UPDATE SET
102
+ worktree_root = excluded.worktree_root,
103
+ updated_at = excluded.updated_at
104
+ `).run(repository.repositoryId, repository.gitDirectory, repository.worktreeRoot, timestamp);
105
+ this.connection.exec("COMMIT");
106
+ }
107
+ catch (error) {
108
+ this.connection.exec("ROLLBACK");
109
+ throw error;
537
110
  }
538
111
  }
539
112
  }
540
- function requireExistingSharedDatabase(atlasDirectory, databasePath) {
541
- const directory = lstatSync(atlasDirectory);
542
- if (!directory.isDirectory() || directory.isSymbolicLink()) {
543
- throw new Error("The Atlas store must be a real directory inside the target worktree");
113
+ export function resolveAtlasHome(repository) {
114
+ const configuredHome = process.env[SEMANTIC_ATLAS_HOME_ENVIRONMENT_VARIABLE];
115
+ if (configuredHome !== undefined && !isAbsolute(configuredHome)) {
116
+ throw new Error(`${SEMANTIC_ATLAS_HOME_ENVIRONMENT_VARIABLE} must be an absolute path`);
544
117
  }
545
- if (resolve(realpathSync(atlasDirectory)) !== resolve(atlasDirectory)) {
546
- throw new Error("The Atlas store resolves outside the target worktree");
118
+ const atlasHome = resolve(configuredHome ?? join(homedir(), ".semantic-atlas"));
119
+ mkdirSync(atlasHome, { recursive: true, mode: 0o700 });
120
+ requireRealDirectory(atlasHome, "The Semantic Atlas home");
121
+ const realAtlasHome = realpathSync(atlasHome);
122
+ for (const worktreeRoot of repository.worktreeRoots) {
123
+ const realWorktreeRoot = realpathExistingPath(worktreeRoot);
124
+ if (realWorktreeRoot !== null && isPathInside(realAtlasHome, realWorktreeRoot)) {
125
+ throw new Error("The Semantic Atlas home must be outside every repository worktree");
126
+ }
547
127
  }
548
- const database = lstatSync(databasePath);
549
- if (!database.isFile() || database.isSymbolicLink()) {
550
- throw new Error("The shared Atlas database must be initialized by the structural backend");
128
+ return realAtlasHome;
129
+ }
130
+ export function resolveAtlasDatabasePath(repository) {
131
+ const atlasHome = resolveAtlasHome(repository);
132
+ const repositoriesDirectory = join(atlasHome, "repositories");
133
+ const repositoryDirectory = join(repositoriesDirectory, repository.repositoryId);
134
+ mkdirSync(repositoryDirectory, { recursive: true, mode: 0o700 });
135
+ requireRealDirectory(repositoriesDirectory, "The Semantic Atlas repositories directory");
136
+ requireRealDirectory(repositoryDirectory, "The repository Atlas directory");
137
+ return join(repositoryDirectory, "atlas.db");
138
+ }
139
+ function requireRealDirectory(path, label) {
140
+ const metadata = lstatSync(path);
141
+ if (!metadata.isDirectory() || metadata.isSymbolicLink()) {
142
+ throw new Error(`${label} must be a real directory`);
143
+ }
144
+ }
145
+ function realpathExistingPath(path) {
146
+ try {
147
+ return realpathSync(path);
551
148
  }
149
+ catch (error) {
150
+ if (error.code === "ENOENT") {
151
+ return null;
152
+ }
153
+ throw error;
154
+ }
155
+ }
156
+ function isPathInside(candidate, parent) {
157
+ const pathFromParent = relative(parent, candidate);
158
+ return pathFromParent === "" || (pathFromParent !== ".."
159
+ && !pathFromParent.startsWith(`..${process.platform === "win32" ? "\\" : "/"}`)
160
+ && !isAbsolute(pathFromParent));
552
161
  }
162
+ const ATLAS_SCHEMA_V1 = `
163
+ CREATE TABLE atlas_schema (
164
+ singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
165
+ version INTEGER NOT NULL CHECK (version = 1),
166
+ created_at TEXT NOT NULL
167
+ ) STRICT;
168
+
169
+ CREATE TABLE atlas_repositories (
170
+ repository_id TEXT PRIMARY KEY,
171
+ common_git_directory TEXT NOT NULL,
172
+ updated_at TEXT NOT NULL
173
+ ) STRICT;
174
+
175
+ CREATE TABLE atlas_repository_snapshots (
176
+ repository_id TEXT NOT NULL,
177
+ snapshot_id TEXT NOT NULL,
178
+ payload TEXT NOT NULL,
179
+ created_at TEXT NOT NULL,
180
+ PRIMARY KEY (repository_id, snapshot_id),
181
+ FOREIGN KEY (repository_id)
182
+ REFERENCES atlas_repositories(repository_id) ON DELETE CASCADE
183
+ ) STRICT;
184
+
185
+ CREATE TABLE atlas_world_publications (
186
+ publication_id INTEGER PRIMARY KEY,
187
+ repository_id TEXT NOT NULL,
188
+ git_directory TEXT NOT NULL,
189
+ previous_publication_id INTEGER CHECK (
190
+ previous_publication_id IS NULL OR previous_publication_id < publication_id
191
+ ),
192
+ snapshot_id TEXT NOT NULL,
193
+ added_paths TEXT NOT NULL,
194
+ modified_paths TEXT NOT NULL,
195
+ removed_paths TEXT NOT NULL,
196
+ stale_assertions TEXT NOT NULL,
197
+ published_at TEXT NOT NULL,
198
+ UNIQUE (repository_id, git_directory, publication_id),
199
+ FOREIGN KEY (repository_id)
200
+ REFERENCES atlas_repositories(repository_id) ON DELETE CASCADE,
201
+ FOREIGN KEY (repository_id, snapshot_id)
202
+ REFERENCES atlas_repository_snapshots(repository_id, snapshot_id) ON DELETE CASCADE,
203
+ FOREIGN KEY (repository_id, git_directory, previous_publication_id)
204
+ REFERENCES atlas_world_publications(repository_id, git_directory, publication_id)
205
+ ) STRICT;
206
+
207
+ CREATE TABLE atlas_worktree_states (
208
+ repository_id TEXT NOT NULL,
209
+ git_directory TEXT NOT NULL,
210
+ worktree_root TEXT NOT NULL,
211
+ status TEXT NOT NULL CHECK (status IN ('missing', 'building', 'current', 'failed')),
212
+ current_snapshot_id TEXT,
213
+ target_snapshot_id TEXT,
214
+ backend_version TEXT,
215
+ extraction_version INTEGER,
216
+ current_publication_id INTEGER,
217
+ failure_message TEXT,
218
+ started_at TEXT,
219
+ published_at TEXT,
220
+ updated_at TEXT NOT NULL,
221
+ PRIMARY KEY (repository_id, git_directory),
222
+ FOREIGN KEY (repository_id)
223
+ REFERENCES atlas_repositories(repository_id) ON DELETE CASCADE,
224
+ FOREIGN KEY (repository_id, current_snapshot_id)
225
+ REFERENCES atlas_repository_snapshots(repository_id, snapshot_id),
226
+ FOREIGN KEY (repository_id, git_directory, current_publication_id)
227
+ REFERENCES atlas_world_publications(repository_id, git_directory, publication_id)
228
+ ) STRICT;
229
+
230
+ CREATE INDEX atlas_worktree_states_current_index
231
+ ON atlas_worktree_states (repository_id, status, published_at DESC, worktree_root);
232
+ CREATE INDEX atlas_world_publications_worktree_order_index
233
+ ON atlas_world_publications (repository_id, git_directory, publication_id DESC);
234
+ CREATE INDEX atlas_world_publications_snapshot_index
235
+ ON atlas_world_publications (repository_id, git_directory, snapshot_id, publication_id DESC);
236
+
237
+ CREATE TABLE atlas_business_nodes (
238
+ node_id INTEGER PRIMARY KEY,
239
+ repository_id TEXT NOT NULL,
240
+ node_key TEXT NOT NULL,
241
+ base_snapshot_id TEXT NOT NULL,
242
+ kind TEXT NOT NULL CHECK (kind IN (
243
+ 'Capability', 'Scenario', 'Operation', 'Invariant', 'Interface', 'Data'
244
+ )),
245
+ label TEXT NOT NULL,
246
+ summary TEXT NOT NULL,
247
+ certainty TEXT NOT NULL CHECK (certainty IN ('exact', 'inferred', 'hypothesis')),
248
+ UNIQUE (repository_id, node_key),
249
+ FOREIGN KEY (repository_id, base_snapshot_id)
250
+ REFERENCES atlas_repository_snapshots(repository_id, snapshot_id)
251
+ ) STRICT;
252
+
253
+ CREATE TABLE atlas_business_node_aliases (
254
+ node_id INTEGER NOT NULL,
255
+ position INTEGER NOT NULL,
256
+ alias TEXT NOT NULL,
257
+ PRIMARY KEY (node_id, position),
258
+ FOREIGN KEY (node_id) REFERENCES atlas_business_nodes(node_id) ON DELETE CASCADE
259
+ ) STRICT;
260
+
261
+ CREATE TABLE atlas_business_node_evidence (
262
+ node_id INTEGER NOT NULL,
263
+ position INTEGER NOT NULL,
264
+ structural_reference TEXT NOT NULL,
265
+ file TEXT NOT NULL,
266
+ qualified_symbol TEXT,
267
+ structural_kind TEXT,
268
+ start_line INTEGER NOT NULL,
269
+ start_column INTEGER NOT NULL,
270
+ end_line INTEGER NOT NULL,
271
+ end_column INTEGER NOT NULL,
272
+ content_hash TEXT NOT NULL,
273
+ backend_locator TEXT,
274
+ PRIMARY KEY (node_id, position),
275
+ FOREIGN KEY (node_id) REFERENCES atlas_business_nodes(node_id) ON DELETE CASCADE
276
+ ) STRICT;
277
+
278
+ CREATE TABLE atlas_business_node_evidence_bindings (
279
+ node_id INTEGER NOT NULL,
280
+ position INTEGER NOT NULL,
281
+ repository_id TEXT NOT NULL,
282
+ snapshot_id TEXT NOT NULL,
283
+ resolved_structural_reference TEXT,
284
+ resolved_qualified_symbol TEXT,
285
+ resolved_structural_kind TEXT,
286
+ backend_version TEXT,
287
+ backend_locator TEXT,
288
+ binding_status TEXT NOT NULL CHECK (
289
+ binding_status IN ('bound', 'missing', 'ambiguous', 'unresolved')
290
+ ),
291
+ PRIMARY KEY (node_id, position, snapshot_id),
292
+ FOREIGN KEY (node_id, position)
293
+ REFERENCES atlas_business_node_evidence(node_id, position) ON DELETE CASCADE,
294
+ FOREIGN KEY (repository_id, snapshot_id)
295
+ REFERENCES atlas_repository_snapshots(repository_id, snapshot_id) ON DELETE CASCADE
296
+ ) STRICT;
297
+
298
+ CREATE TABLE atlas_business_relations (
299
+ relation_id INTEGER PRIMARY KEY,
300
+ repository_id TEXT NOT NULL,
301
+ base_snapshot_id TEXT NOT NULL,
302
+ from_key TEXT NOT NULL,
303
+ relation_type TEXT NOT NULL CHECK (relation_type IN (
304
+ 'part_of', 'invokes', 'realized_by', 'reads', 'writes', 'publishes',
305
+ 'consumes', 'constrained_by', 'verified_by'
306
+ )),
307
+ to_domain TEXT NOT NULL CHECK (to_domain IN ('structural', 'business')),
308
+ to_key TEXT NOT NULL,
309
+ certainty TEXT NOT NULL CHECK (certainty IN ('exact', 'inferred', 'hypothesis')),
310
+ target_file TEXT,
311
+ target_qualified_symbol TEXT,
312
+ target_structural_kind TEXT,
313
+ target_start_line INTEGER,
314
+ target_start_column INTEGER,
315
+ target_end_line INTEGER,
316
+ target_end_column INTEGER,
317
+ target_backend_locator TEXT,
318
+ UNIQUE (repository_id, from_key, relation_type, to_domain, to_key),
319
+ FOREIGN KEY (repository_id, base_snapshot_id)
320
+ REFERENCES atlas_repository_snapshots(repository_id, snapshot_id),
321
+ FOREIGN KEY (repository_id, from_key)
322
+ REFERENCES atlas_business_nodes(repository_id, node_key)
323
+ ) STRICT;
324
+
325
+ CREATE INDEX atlas_business_relations_from_index
326
+ ON atlas_business_relations (repository_id, from_key);
327
+ CREATE INDEX atlas_business_relations_to_index
328
+ ON atlas_business_relations (repository_id, to_domain, to_key);
329
+
330
+ CREATE TABLE atlas_business_relation_evidence (
331
+ relation_id INTEGER NOT NULL,
332
+ position INTEGER NOT NULL,
333
+ structural_reference TEXT NOT NULL,
334
+ file TEXT NOT NULL,
335
+ qualified_symbol TEXT,
336
+ structural_kind TEXT,
337
+ start_line INTEGER NOT NULL,
338
+ start_column INTEGER NOT NULL,
339
+ end_line INTEGER NOT NULL,
340
+ end_column INTEGER NOT NULL,
341
+ content_hash TEXT NOT NULL,
342
+ backend_locator TEXT,
343
+ PRIMARY KEY (relation_id, position),
344
+ FOREIGN KEY (relation_id)
345
+ REFERENCES atlas_business_relations(relation_id) ON DELETE CASCADE
346
+ ) STRICT;
347
+
348
+ CREATE TABLE atlas_business_relation_evidence_bindings (
349
+ relation_id INTEGER NOT NULL,
350
+ position INTEGER NOT NULL,
351
+ repository_id TEXT NOT NULL,
352
+ snapshot_id TEXT NOT NULL,
353
+ resolved_structural_reference TEXT,
354
+ resolved_qualified_symbol TEXT,
355
+ resolved_structural_kind TEXT,
356
+ backend_version TEXT,
357
+ backend_locator TEXT,
358
+ binding_status TEXT NOT NULL CHECK (
359
+ binding_status IN ('bound', 'missing', 'ambiguous', 'unresolved')
360
+ ),
361
+ PRIMARY KEY (relation_id, position, snapshot_id),
362
+ FOREIGN KEY (relation_id, position)
363
+ REFERENCES atlas_business_relation_evidence(relation_id, position) ON DELETE CASCADE,
364
+ FOREIGN KEY (repository_id, snapshot_id)
365
+ REFERENCES atlas_repository_snapshots(repository_id, snapshot_id) ON DELETE CASCADE
366
+ ) STRICT;
367
+
368
+ CREATE TABLE atlas_structural_relation_target_bindings (
369
+ relation_id INTEGER NOT NULL,
370
+ repository_id TEXT NOT NULL,
371
+ snapshot_id TEXT NOT NULL,
372
+ resolved_structural_reference TEXT,
373
+ resolved_file TEXT,
374
+ resolved_qualified_symbol TEXT,
375
+ resolved_structural_kind TEXT,
376
+ resolved_start_line INTEGER,
377
+ resolved_start_column INTEGER,
378
+ resolved_end_line INTEGER,
379
+ resolved_end_column INTEGER,
380
+ backend_version TEXT,
381
+ backend_locator TEXT,
382
+ binding_status TEXT NOT NULL CHECK (
383
+ binding_status IN ('bound', 'missing', 'ambiguous', 'unresolved')
384
+ ),
385
+ PRIMARY KEY (relation_id, snapshot_id),
386
+ FOREIGN KEY (relation_id)
387
+ REFERENCES atlas_business_relations(relation_id) ON DELETE CASCADE,
388
+ FOREIGN KEY (repository_id, snapshot_id)
389
+ REFERENCES atlas_repository_snapshots(repository_id, snapshot_id) ON DELETE CASCADE
390
+ ) STRICT;
391
+
392
+ CREATE TABLE atlas_business_node_validity (
393
+ node_id INTEGER NOT NULL,
394
+ repository_id TEXT NOT NULL,
395
+ snapshot_id TEXT NOT NULL,
396
+ validity TEXT NOT NULL CHECK (validity IN ('valid', 'stale')),
397
+ PRIMARY KEY (node_id, snapshot_id),
398
+ FOREIGN KEY (node_id) REFERENCES atlas_business_nodes(node_id) ON DELETE CASCADE,
399
+ FOREIGN KEY (repository_id, snapshot_id)
400
+ REFERENCES atlas_repository_snapshots(repository_id, snapshot_id) ON DELETE CASCADE
401
+ ) STRICT;
402
+
403
+ CREATE TABLE atlas_business_relation_validity (
404
+ relation_id INTEGER NOT NULL,
405
+ repository_id TEXT NOT NULL,
406
+ snapshot_id TEXT NOT NULL,
407
+ validity TEXT NOT NULL CHECK (validity IN ('valid', 'stale')),
408
+ PRIMARY KEY (relation_id, snapshot_id),
409
+ FOREIGN KEY (relation_id) REFERENCES atlas_business_relations(relation_id) ON DELETE CASCADE,
410
+ FOREIGN KEY (repository_id, snapshot_id)
411
+ REFERENCES atlas_repository_snapshots(repository_id, snapshot_id) ON DELETE CASCADE
412
+ ) STRICT;
413
+
414
+ CREATE TABLE atlas_graph_search (
415
+ repository_id TEXT NOT NULL,
416
+ node_key TEXT NOT NULL,
417
+ label TEXT NOT NULL,
418
+ aliases TEXT NOT NULL,
419
+ summary TEXT NOT NULL,
420
+ symbols TEXT NOT NULL,
421
+ paths TEXT NOT NULL,
422
+ PRIMARY KEY (repository_id, node_key),
423
+ FOREIGN KEY (repository_id, node_key)
424
+ REFERENCES atlas_business_nodes(repository_id, node_key) ON DELETE CASCADE
425
+ ) STRICT;
426
+ `;