semantic-atlas 0.2.0 → 0.4.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 (87) hide show
  1. package/.agents/skills/semantic-atlas/SKILL.md +3 -0
  2. package/.agents/skills/semantic-atlas/references/product-feedback.md +34 -0
  3. package/.agents/skills/semantic-atlas-insights/SKILL.md +55 -0
  4. package/README.md +229 -88
  5. package/README.zh-CN.md +204 -85
  6. package/dist/cli/argument-parser.js +9 -1
  7. package/dist/cli/bin.js +17 -2
  8. package/dist/cli/cli-application.d.ts +1 -0
  9. package/dist/cli/cli-application.d.ts.map +1 -1
  10. package/dist/cli/cli-application.js +64 -12
  11. package/dist/cli/insights-cli.d.ts +6 -0
  12. package/dist/cli/insights-cli.d.ts.map +1 -0
  13. package/dist/cli/insights-cli.js +236 -0
  14. package/dist/cli/main.d.ts.map +1 -1
  15. package/dist/cli/main.js +94 -2
  16. package/dist/cli/standalone-cli.d.ts.map +1 -1
  17. package/dist/cli/standalone-cli.js +44 -5
  18. package/dist/cli/types.d.ts +2 -0
  19. package/dist/cli/types.d.ts.map +1 -1
  20. package/dist/cli/web-cli.d.ts +9 -0
  21. package/dist/cli/web-cli.d.ts.map +1 -0
  22. package/dist/cli/web-cli.js +134 -0
  23. package/dist/contracts/cli.d.ts +63 -0
  24. package/dist/contracts/cli.d.ts.map +1 -1
  25. package/dist/contracts/cli.js +7 -0
  26. package/dist/contracts/insights.d.ts +2 -0
  27. package/dist/contracts/insights.d.ts.map +1 -0
  28. package/dist/contracts/insights.js +1 -0
  29. package/dist/contracts/public-contracts.d.ts +1 -0
  30. package/dist/contracts/public-contracts.d.ts.map +1 -1
  31. package/dist/contracts/public-contracts.js +1 -0
  32. package/dist/graph/graph-store.d.ts +2 -2
  33. package/dist/graph/graph-store.d.ts.map +1 -1
  34. package/dist/graph/graph-store.js +2 -2
  35. package/dist/insights/contracts.d.ts +266 -0
  36. package/dist/insights/contracts.d.ts.map +1 -0
  37. package/dist/insights/contracts.js +124 -0
  38. package/dist/insights/insights-store.d.ts +73 -0
  39. package/dist/insights/insights-store.d.ts.map +1 -0
  40. package/dist/insights/insights-store.js +246 -0
  41. package/dist/repository/repository-inspector.d.ts +1 -0
  42. package/dist/repository/repository-inspector.d.ts.map +1 -1
  43. package/dist/repository/repository-inspector.js +13 -0
  44. package/dist/setup/package-upgrader.d.ts +54 -0
  45. package/dist/setup/package-upgrader.d.ts.map +1 -0
  46. package/dist/setup/package-upgrader.js +169 -0
  47. package/dist/setup/skill-installer.d.ts +3 -2
  48. package/dist/setup/skill-installer.d.ts.map +1 -1
  49. package/dist/setup/skill-installer.js +67 -36
  50. package/dist/storage/atlas-database.d.ts +8 -1
  51. package/dist/storage/atlas-database.d.ts.map +1 -1
  52. package/dist/storage/atlas-database.js +68 -11
  53. package/dist/web/application/atlas-read-service.d.ts +27 -0
  54. package/dist/web/application/atlas-read-service.d.ts.map +1 -0
  55. package/dist/web/application/atlas-read-service.js +216 -0
  56. package/dist/web/application/primary-repository-catalog.d.ts +15 -0
  57. package/dist/web/application/primary-repository-catalog.d.ts.map +1 -0
  58. package/dist/web/application/primary-repository-catalog.js +202 -0
  59. package/dist/web/application/types.d.ts +41 -0
  60. package/dist/web/application/types.d.ts.map +1 -0
  61. package/dist/web/application/types.js +1 -0
  62. package/dist/web/server/api-router.d.ts +4 -0
  63. package/dist/web/server/api-router.d.ts.map +1 -0
  64. package/dist/web/server/api-router.js +151 -0
  65. package/dist/web/server/local-web-server.d.ts +12 -0
  66. package/dist/web/server/local-web-server.d.ts.map +1 -0
  67. package/dist/web/server/local-web-server.js +100 -0
  68. package/dist/web-client/assets/index-DneaErDj.css +1 -0
  69. package/dist/web-client/assets/index-cze_4vtt.js +2 -0
  70. package/dist/web-client/assets/index-cze_4vtt.js.map +1 -0
  71. package/dist/web-client/index.html +61 -0
  72. package/dist/world/world-graph-query.d.ts +5 -1
  73. package/dist/world/world-graph-query.d.ts.map +1 -1
  74. package/dist/world/world-graph-query.js +4 -2
  75. package/dist/world/world-snapshot-store.d.ts +2 -1
  76. package/dist/world/world-snapshot-store.d.ts.map +1 -1
  77. package/dist/world/world-snapshot-store.js +2 -2
  78. package/docs/architecture/web-viewer.md +225 -0
  79. package/docs/contracts/cli-v1.md +40 -4
  80. package/docs/contracts/http-api-v1.md +155 -0
  81. package/docs/contracts/insights-v1.md +84 -0
  82. package/docs/index.md +7 -0
  83. package/docs/log.md +103 -0
  84. package/docs/product-contract.md +45 -10
  85. package/package.json +7 -4
  86. package/schemas/cli-envelope-v1.schema.json +85 -1
  87. package/schemas/insights-envelope-v1.schema.json +428 -0
@@ -4,25 +4,36 @@ import { homedir } from "node:os";
4
4
  import { dirname, join, relative } from "node:path";
5
5
  import { fileURLToPath } from "node:url";
6
6
  const skillName = "semantic-atlas";
7
+ const insightsSkillName = "semantic-atlas-insights";
7
8
  const installationMarkerName = ".semantic-atlas-managed.json";
8
9
  export class SkillInstaller {
9
10
  #sourceDirectory;
10
11
  #targetDirectory;
12
+ #insightsSourceDirectory;
13
+ #insightsTargetDirectory;
11
14
  #legacyDirectories;
12
15
  #version;
13
16
  constructor(options) {
14
17
  const userHome = options.userHome ?? homedir();
15
18
  this.#sourceDirectory = options.sourceDirectory ?? resolveBundledSkillDirectory();
16
19
  this.#targetDirectory = join(userHome, ".agents", "skills", skillName);
20
+ this.#insightsSourceDirectory = options.insightsSourceDirectory
21
+ ?? resolveBundledSkillDirectory(insightsSkillName);
22
+ this.#insightsTargetDirectory = join(userHome, ".agents", "skills", insightsSkillName);
17
23
  this.#legacyDirectories = [join(userHome, ".codex", "skills", skillName)];
18
24
  this.#version = options.version;
19
25
  }
20
26
  async install() {
21
27
  await requireSemanticAtlasSkill(this.#sourceDirectory, "bundled Skill");
28
+ await requireManagedSkill(this.#insightsSourceDirectory, insightsSkillName, "bundled insights Skill");
22
29
  const targetExists = await exists(this.#targetDirectory);
23
30
  if (targetExists) {
24
31
  await requireSemanticAtlasSkill(this.#targetDirectory, "existing shared Skill");
25
32
  }
33
+ const insightsTargetExists = await exists(this.#insightsTargetDirectory);
34
+ if (insightsTargetExists) {
35
+ await requireManagedSkill(this.#insightsTargetDirectory, insightsSkillName, "existing shared insights Skill");
36
+ }
26
37
  const existingLegacyDirectories = await this.existingLegacyDirectories();
27
38
  for (const directory of existingLegacyDirectories) {
28
39
  await requireSemanticAtlasSkill(directory, "legacy Codex Skill");
@@ -30,20 +41,25 @@ export class SkillInstaller {
30
41
  const sourceFingerprint = await fingerprintSkill(this.#sourceDirectory);
31
42
  const targetCurrent = targetExists
32
43
  && await installedSkillIsCurrent(this.#targetDirectory, this.#version, sourceFingerprint);
33
- if (targetCurrent && existingLegacyDirectories.length === 0) {
34
- return {
35
- outcome: "current",
36
- targetDirectory: this.#targetDirectory,
37
- removedLegacyDirectories: [],
38
- };
44
+ if (!targetCurrent || existingLegacyDirectories.length > 0) {
45
+ await installSkillAtomically(this.#sourceDirectory, this.#targetDirectory, { version: this.#version, fingerprint: sourceFingerprint });
39
46
  }
40
- await this.installAtomically({ version: this.#version, fingerprint: sourceFingerprint });
47
+ const insights = await installManagedSkill({
48
+ sourceDirectory: this.#insightsSourceDirectory,
49
+ targetDirectory: this.#insightsTargetDirectory,
50
+ version: this.#version,
51
+ });
41
52
  for (const directory of existingLegacyDirectories) {
42
53
  await rm(directory, { recursive: true, force: true });
43
54
  }
44
55
  return {
45
- outcome: targetExists ? "updated" : "installed",
56
+ outcome: targetCurrent && insights === "current" && existingLegacyDirectories.length === 0
57
+ ? "current"
58
+ : targetExists || insightsTargetExists
59
+ ? "updated"
60
+ : "installed",
46
61
  targetDirectory: this.#targetDirectory,
62
+ insightsTargetDirectory: this.#insightsTargetDirectory,
47
63
  removedLegacyDirectories: existingLegacyDirectories,
48
64
  };
49
65
  }
@@ -56,35 +72,10 @@ export class SkillInstaller {
56
72
  }
57
73
  return result;
58
74
  }
59
- async installAtomically(marker) {
60
- const parent = dirname(this.#targetDirectory);
61
- const token = `${process.pid}-${randomUUID()}`;
62
- const temporary = `${this.#targetDirectory}.installing-${token}`;
63
- const backup = `${this.#targetDirectory}.backup-${token}`;
64
- const targetExists = await exists(this.#targetDirectory);
65
- await mkdir(parent, { recursive: true });
66
- await rm(temporary, { recursive: true, force: true });
67
- await rm(backup, { recursive: true, force: true });
68
- await cp(this.#sourceDirectory, temporary, { recursive: true });
69
- await writeFile(join(temporary, installationMarkerName), `${JSON.stringify(marker, null, 2)}\n`, "utf8");
70
- if (targetExists) {
71
- await rename(this.#targetDirectory, backup);
72
- }
73
- try {
74
- await rename(temporary, this.#targetDirectory);
75
- }
76
- catch (error) {
77
- if (targetExists) {
78
- await rename(backup, this.#targetDirectory);
79
- }
80
- throw error;
81
- }
82
- await rm(backup, { recursive: true, force: true });
83
- }
84
75
  }
85
- export function resolveBundledSkillDirectory() {
76
+ export function resolveBundledSkillDirectory(name = skillName) {
86
77
  const moduleDirectory = dirname(fileURLToPath(import.meta.url));
87
- return join(moduleDirectory, "..", "..", ".agents", "skills", skillName);
78
+ return join(moduleDirectory, "..", "..", ".agents", "skills", name);
88
79
  }
89
80
  async function installedSkillIsCurrent(directory, version, sourceFingerprint) {
90
81
  const marker = await readInstallationMarker(join(directory, installationMarkerName));
@@ -94,6 +85,9 @@ async function installedSkillIsCurrent(directory, version, sourceFingerprint) {
94
85
  return await fingerprintSkill(directory) === sourceFingerprint;
95
86
  }
96
87
  async function requireSemanticAtlasSkill(directory, description) {
88
+ return requireManagedSkill(directory, skillName, description);
89
+ }
90
+ async function requireManagedSkill(directory, name, description) {
97
91
  let contents;
98
92
  try {
99
93
  contents = await readFile(join(directory, "SKILL.md"), "utf8");
@@ -101,10 +95,47 @@ async function requireSemanticAtlasSkill(directory, description) {
101
95
  catch {
102
96
  throw new Error(`Refusing to replace ${description} at ${directory}: SKILL.md is missing`);
103
97
  }
104
- if (!/^---\r?\n[\s\S]*?^name:\s*["']?semantic-atlas["']?\s*$[\s\S]*?^---\s*$/mu.test(contents)) {
98
+ const escapedName = name.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
99
+ const namePattern = new RegExp(`^---\\r?\\n[\\s\\S]*?^name:\\s*["']?${escapedName}["']?\\s*$[\\s\\S]*?^---\\s*$`, "mu");
100
+ if (!namePattern.test(contents)) {
105
101
  throw new Error(`Refusing to replace unrelated Skill at ${directory}`);
106
102
  }
107
103
  }
104
+ async function installManagedSkill(options) {
105
+ const targetExists = await exists(options.targetDirectory);
106
+ const fingerprint = await fingerprintSkill(options.sourceDirectory);
107
+ const current = targetExists && await installedSkillIsCurrent(options.targetDirectory, options.version, fingerprint);
108
+ if (current)
109
+ return "current";
110
+ await installSkillAtomically(options.sourceDirectory, options.targetDirectory, {
111
+ version: options.version,
112
+ fingerprint,
113
+ });
114
+ return targetExists ? "updated" : "installed";
115
+ }
116
+ async function installSkillAtomically(sourceDirectory, targetDirectory, marker) {
117
+ const parent = dirname(targetDirectory);
118
+ const token = `${process.pid}-${randomUUID()}`;
119
+ const temporary = `${targetDirectory}.installing-${token}`;
120
+ const backup = `${targetDirectory}.backup-${token}`;
121
+ const targetExists = await exists(targetDirectory);
122
+ await mkdir(parent, { recursive: true });
123
+ await rm(temporary, { recursive: true, force: true });
124
+ await rm(backup, { recursive: true, force: true });
125
+ await cp(sourceDirectory, temporary, { recursive: true });
126
+ await writeFile(join(temporary, installationMarkerName), `${JSON.stringify(marker, null, 2)}\n`, "utf8");
127
+ if (targetExists)
128
+ await rename(targetDirectory, backup);
129
+ try {
130
+ await rename(temporary, targetDirectory);
131
+ }
132
+ catch (error) {
133
+ if (targetExists)
134
+ await rename(backup, targetDirectory);
135
+ throw error;
136
+ }
137
+ await rm(backup, { recursive: true, force: true });
138
+ }
108
139
  async function readInstallationMarker(path) {
109
140
  try {
110
141
  const parsed = JSON.parse(await readFile(path, "utf8"));
@@ -2,17 +2,24 @@ import type { DatabaseSync as NodeDatabaseSync } from "node:sqlite";
2
2
  import type { GitRepository } from "../repository/types.js";
3
3
  export declare const CURRENT_ATLAS_SCHEMA_VERSION = 1;
4
4
  export declare const SEMANTIC_ATLAS_HOME_ENVIRONMENT_VARIABLE = "SEMANTIC_ATLAS_HOME";
5
+ export type AtlasDatabaseAccess = "read-write" | "read-only";
6
+ export interface AtlasDatabaseOptions {
7
+ readonly access?: AtlasDatabaseAccess;
8
+ }
5
9
  export declare class AtlasDatabase implements Disposable {
6
10
  #private;
7
11
  readonly databasePath: string;
8
12
  readonly connection: NodeDatabaseSync;
9
- constructor(repository: GitRepository);
13
+ constructor(repository: GitRepository, options?: AtlasDatabaseOptions);
10
14
  get schemaVersion(): number;
11
15
  close(): void;
12
16
  [Symbol.dispose](): void;
13
17
  private initializeSchema;
18
+ private requireCurrentSchema;
14
19
  private registerRepository;
15
20
  }
16
21
  export declare function resolveAtlasHome(repository: GitRepository): string;
22
+ export declare function resolveExistingAtlasHome(): string | null;
23
+ export declare function resolveInsightsDatabasePath(): string;
17
24
  export declare function resolveAtlasDatabasePath(repository: GitRepository): string;
18
25
  //# sourceMappingURL=atlas-database.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"atlas-database.d.ts","sourceRoot":"","sources":["../../src/storage/atlas-database.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,YAAY,IAAI,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEpE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAE5D,eAAO,MAAM,4BAA4B,IAAI,CAAC;AAC9C,eAAO,MAAM,wCAAwC,wBAAwB,CAAC;AAM9E,qBAAa,aAAc,YAAW,UAAU;;IAC9C,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,UAAU,EAAE,gBAAgB,CAAC;gBAG1B,UAAU,EAAE,aAAa;IAcrC,IAAI,aAAa,IAAI,MAAM,CAO1B;IAED,KAAK,IAAI,IAAI;IAQb,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI;IAIxB,OAAO,CAAC,gBAAgB;IAuCxB,OAAO,CAAC,kBAAkB;CAqC3B;AAED,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,aAAa,GAAG,MAAM,CAiBlE;AAED,wBAAgB,wBAAwB,CAAC,UAAU,EAAE,aAAa,GAAG,MAAM,CAQ1E"}
1
+ {"version":3,"file":"atlas-database.d.ts","sourceRoot":"","sources":["../../src/storage/atlas-database.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,YAAY,IAAI,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEpE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAE5D,eAAO,MAAM,4BAA4B,IAAI,CAAC;AAC9C,eAAO,MAAM,wCAAwC,wBAAwB,CAAC;AAS9E,MAAM,MAAM,mBAAmB,GAAG,YAAY,GAAG,WAAW,CAAC;AAE7D,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,MAAM,CAAC,EAAE,mBAAmB,CAAC;CACvC;AAED,qBAAa,aAAc,YAAW,UAAU;;IAC9C,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,UAAU,EAAE,gBAAgB,CAAC;gBAG1B,UAAU,EAAE,aAAa,EAAE,OAAO,GAAE,oBAAyB;IAuBzE,IAAI,aAAa,IAAI,MAAM,CAO1B;IAED,KAAK,IAAI,IAAI;IAQb,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI;IAIxB,OAAO,CAAC,gBAAgB;IAuCxB,OAAO,CAAC,oBAAoB;IAW5B,OAAO,CAAC,kBAAkB;CAqC3B;AAED,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,aAAa,GAAG,MAAM,CAIlE;AAED,wBAAgB,wBAAwB,IAAI,MAAM,GAAG,IAAI,CAWxD;AAcD,wBAAgB,2BAA2B,IAAI,MAAM,CAEpD;AAmBD,wBAAgB,wBAAwB,CAAC,UAAU,EAAE,aAAa,GAAG,MAAM,CAQ1E"}
@@ -9,12 +9,21 @@ export class AtlasDatabase {
9
9
  databasePath;
10
10
  connection;
11
11
  #closed = false;
12
- constructor(repository) {
13
- this.databasePath = resolveAtlasDatabasePath(repository);
12
+ constructor(repository, options = {}) {
13
+ const access = options.access ?? "read-write";
14
+ this.databasePath = access === "read-only"
15
+ ? resolveExistingAtlasDatabasePath(repository)
16
+ : resolveAtlasDatabasePath(repository);
14
17
  const { DatabaseSync } = require("node:sqlite");
15
- this.connection = new DatabaseSync(this.databasePath);
18
+ this.connection = new DatabaseSync(this.databasePath, { readOnly: access === "read-only" });
16
19
  try {
17
- this.connection.exec("PRAGMA foreign_keys = ON; PRAGMA journal_mode = WAL; PRAGMA busy_timeout = 5000;");
20
+ this.connection.exec(access === "read-only"
21
+ ? "PRAGMA foreign_keys = ON; PRAGMA busy_timeout = 5000;"
22
+ : "PRAGMA foreign_keys = ON; PRAGMA journal_mode = WAL; PRAGMA busy_timeout = 5000;");
23
+ if (access === "read-only") {
24
+ this.requireCurrentSchema();
25
+ return;
26
+ }
18
27
  this.initializeSchema();
19
28
  this.registerRepository(repository);
20
29
  }
@@ -76,6 +85,16 @@ export class AtlasDatabase {
76
85
  throw error;
77
86
  }
78
87
  }
88
+ requireCurrentSchema() {
89
+ const atlasSchema = this.connection.prepare(`
90
+ SELECT name
91
+ FROM sqlite_schema
92
+ WHERE type = 'table' AND name = 'atlas_schema'
93
+ `).get();
94
+ if (atlasSchema === undefined || this.schemaVersion !== CURRENT_ATLAS_SCHEMA_VERSION) {
95
+ throw new Error(`The Atlas database is not a supported schema v${CURRENT_ATLAS_SCHEMA_VERSION} store`);
96
+ }
97
+ }
79
98
  registerRepository(repository) {
80
99
  const timestamp = new Date().toISOString();
81
100
  this.connection.exec("BEGIN IMMEDIATE");
@@ -111,22 +130,48 @@ export class AtlasDatabase {
111
130
  }
112
131
  }
113
132
  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`);
133
+ const realAtlasHome = resolveConfiguredAtlasHome();
134
+ requireAtlasHomeOutsideRepository(realAtlasHome, repository);
135
+ return realAtlasHome;
136
+ }
137
+ export function resolveExistingAtlasHome() {
138
+ const atlasHome = configuredAtlasHomePath();
139
+ try {
140
+ requireRealDirectory(atlasHome, "The Semantic Atlas home");
141
+ return realpathSync(atlasHome);
117
142
  }
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);
143
+ catch (error) {
144
+ if (error.code === "ENOENT") {
145
+ return null;
146
+ }
147
+ throw error;
148
+ }
149
+ }
150
+ function requireAtlasHomeOutsideRepository(realAtlasHome, repository) {
122
151
  for (const worktreeRoot of repository.worktreeRoots) {
123
152
  const realWorktreeRoot = realpathExistingPath(worktreeRoot);
124
153
  if (realWorktreeRoot !== null && isPathInside(realAtlasHome, realWorktreeRoot)) {
125
154
  throw new Error("The Semantic Atlas home must be outside every repository worktree");
126
155
  }
127
156
  }
157
+ }
158
+ export function resolveInsightsDatabasePath() {
159
+ return join(resolveConfiguredAtlasHome(), "insights.db");
160
+ }
161
+ function resolveConfiguredAtlasHome() {
162
+ const atlasHome = configuredAtlasHomePath();
163
+ mkdirSync(atlasHome, { recursive: true, mode: 0o700 });
164
+ requireRealDirectory(atlasHome, "The Semantic Atlas home");
165
+ const realAtlasHome = realpathSync(atlasHome);
128
166
  return realAtlasHome;
129
167
  }
168
+ function configuredAtlasHomePath() {
169
+ const configuredHome = process.env[SEMANTIC_ATLAS_HOME_ENVIRONMENT_VARIABLE];
170
+ if (configuredHome !== undefined && !isAbsolute(configuredHome)) {
171
+ throw new Error(`${SEMANTIC_ATLAS_HOME_ENVIRONMENT_VARIABLE} must be an absolute path`);
172
+ }
173
+ return resolve(configuredHome ?? join(homedir(), ".semantic-atlas"));
174
+ }
130
175
  export function resolveAtlasDatabasePath(repository) {
131
176
  const atlasHome = resolveAtlasHome(repository);
132
177
  const repositoriesDirectory = join(atlasHome, "repositories");
@@ -136,6 +181,18 @@ export function resolveAtlasDatabasePath(repository) {
136
181
  requireRealDirectory(repositoryDirectory, "The repository Atlas directory");
137
182
  return join(repositoryDirectory, "atlas.db");
138
183
  }
184
+ function resolveExistingAtlasDatabasePath(repository) {
185
+ const atlasHome = resolveExistingAtlasHome();
186
+ if (atlasHome === null) {
187
+ throw new Error("The Semantic Atlas home does not exist");
188
+ }
189
+ requireAtlasHomeOutsideRepository(atlasHome, repository);
190
+ const repositoriesDirectory = join(atlasHome, "repositories");
191
+ const repositoryDirectory = join(repositoriesDirectory, repository.repositoryId);
192
+ requireRealDirectory(repositoriesDirectory, "The Semantic Atlas repositories directory");
193
+ requireRealDirectory(repositoryDirectory, "The repository Atlas directory");
194
+ return join(repositoryDirectory, "atlas.db");
195
+ }
139
196
  function requireRealDirectory(path, label) {
140
197
  const metadata = lstatSync(path);
141
198
  if (!metadata.isDirectory() || metadata.isSymbolicLink()) {
@@ -0,0 +1,27 @@
1
+ import { PrimaryRepositoryCatalog } from "./primary-repository-catalog.js";
2
+ import type { WebBusinessMap, WebBusinessNode, WebBusinessSearch, WebProjectStatus, WebProjectSummary } from "./types.js";
3
+ export type AtlasReadErrorCode = "PROJECT_NOT_FOUND" | "BUSINESS_NODE_NOT_FOUND" | "ATLAS_STATE_UNAVAILABLE" | "PUBLICATION_CHANGED";
4
+ export declare class AtlasReadError extends Error {
5
+ readonly statusCode: 404 | 409;
6
+ readonly code: AtlasReadErrorCode;
7
+ constructor(statusCode: 404 | 409, code: AtlasReadErrorCode, message: string);
8
+ }
9
+ export interface AtlasReadOperations {
10
+ listProjects(): Promise<readonly WebProjectSummary[]>;
11
+ getStatus(repositoryId: string): Promise<WebProjectStatus>;
12
+ getMap(repositoryId: string, focusKey?: string): Promise<WebBusinessMap>;
13
+ searchBusiness(repositoryId: string, query: string, limit: number): Promise<WebBusinessSearch>;
14
+ getBusinessNode(repositoryId: string, businessKey: string): Promise<WebBusinessNode>;
15
+ }
16
+ export declare class AtlasReadService implements AtlasReadOperations {
17
+ #private;
18
+ constructor(catalog?: PrimaryRepositoryCatalog);
19
+ listProjects(): Promise<readonly WebProjectSummary[]>;
20
+ getStatus(repositoryId: string): Promise<WebProjectStatus>;
21
+ getMap(repositoryId: string, focusKey?: string): Promise<WebBusinessMap>;
22
+ searchBusiness(repositoryId: string, queryText: string, limit: number): Promise<WebBusinessSearch>;
23
+ getBusinessNode(repositoryId: string, businessKey: string): Promise<WebBusinessNode>;
24
+ private withPublishedWorld;
25
+ private withRepositoryRead;
26
+ }
27
+ //# sourceMappingURL=atlas-read-service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"atlas-read-service.d.ts","sourceRoot":"","sources":["../../../src/web/application/atlas-read-service.ts"],"names":[],"mappings":"AAOA,OAAO,EACL,wBAAwB,EAEzB,MAAM,iCAAiC,CAAC;AACzC,OAAO,KAAK,EACV,cAAc,EACd,eAAe,EACf,iBAAiB,EACjB,gBAAgB,EAChB,iBAAiB,EAClB,MAAM,YAAY,CAAC;AAEpB,MAAM,MAAM,kBAAkB,GAC1B,mBAAmB,GACnB,yBAAyB,GACzB,yBAAyB,GACzB,qBAAqB,CAAC;AAE1B,qBAAa,cAAe,SAAQ,KAAK;IAErC,QAAQ,CAAC,UAAU,EAAE,GAAG,GAAG,GAAG;IAC9B,QAAQ,CAAC,IAAI,EAAE,kBAAkB;gBADxB,UAAU,EAAE,GAAG,GAAG,GAAG,EACrB,IAAI,EAAE,kBAAkB,EACjC,OAAO,EAAE,MAAM;CAKlB;AAED,MAAM,WAAW,mBAAmB;IAClC,YAAY,IAAI,OAAO,CAAC,SAAS,iBAAiB,EAAE,CAAC,CAAC;IACtD,SAAS,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAC3D,MAAM,CAAC,YAAY,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;IACzE,cAAc,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;IAC/F,eAAe,CAAC,YAAY,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC;CACtF;AAED,qBAAa,gBAAiB,YAAW,mBAAmB;;gBAI9C,OAAO,GAAE,wBAAyD;IAI9E,YAAY,IAAI,OAAO,CAAC,SAAS,iBAAiB,EAAE,CAAC;IAIrD,SAAS,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC;IA2C1D,MAAM,CAAC,YAAY,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC;IAcxE,cAAc,CACZ,YAAY,EAAE,MAAM,EACpB,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,iBAAiB,CAAC;IAQ7B,eAAe,CAAC,YAAY,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC;IAcpF,OAAO,CAAC,kBAAkB;YAsCZ,kBAAkB;CA2BjC"}
@@ -0,0 +1,216 @@
1
+ var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
2
+ if (value !== null && value !== void 0) {
3
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
4
+ var dispose, inner;
5
+ if (async) {
6
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
7
+ dispose = value[Symbol.asyncDispose];
8
+ }
9
+ if (dispose === void 0) {
10
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
11
+ dispose = value[Symbol.dispose];
12
+ if (async) inner = dispose;
13
+ }
14
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
15
+ if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
16
+ env.stack.push({ value: value, dispose: dispose, async: async });
17
+ }
18
+ else if (async) {
19
+ env.stack.push({ async: true });
20
+ }
21
+ return value;
22
+ };
23
+ var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
24
+ return function (env) {
25
+ function fail(e) {
26
+ env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
27
+ env.hasError = true;
28
+ }
29
+ var r, s = 0;
30
+ function next() {
31
+ while (r = env.stack.pop()) {
32
+ try {
33
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
34
+ if (r.dispose) {
35
+ var result = r.dispose.call(r.value);
36
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
37
+ }
38
+ else s |= 1;
39
+ }
40
+ catch (e) {
41
+ fail(e);
42
+ }
43
+ }
44
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
45
+ if (env.hasError) throw env.error;
46
+ }
47
+ return next();
48
+ };
49
+ })(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
50
+ var e = new Error(message);
51
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
52
+ });
53
+ import { GraphStore } from "../../graph/graph-store.js";
54
+ import { inspectRepositoryLanguages, } from "../../repository/repository-language-support.js";
55
+ import { CodeGraphStructuralBackend } from "../../structural-backend/codegraph-backend.js";
56
+ import { StructuralBackendError } from "../../structural-backend/types.js";
57
+ import { WorldGraphQuery } from "../../world/world-graph-query.js";
58
+ import { PrimaryRepositoryCatalog, } from "./primary-repository-catalog.js";
59
+ export class AtlasReadError extends Error {
60
+ statusCode;
61
+ code;
62
+ constructor(statusCode, code, message) {
63
+ super(message);
64
+ this.statusCode = statusCode;
65
+ this.code = code;
66
+ this.name = "AtlasReadError";
67
+ }
68
+ }
69
+ export class AtlasReadService {
70
+ #catalog;
71
+ #repositoryQueues = new Map();
72
+ constructor(catalog = new PrimaryRepositoryCatalog()) {
73
+ this.#catalog = catalog;
74
+ }
75
+ listProjects() {
76
+ return this.#catalog.listProjects();
77
+ }
78
+ getStatus(repositoryId) {
79
+ return this.withRepositoryRead(repositoryId, async (project) => {
80
+ const [languages, structural] = await Promise.all([
81
+ inspectRepositoryLanguages(project.repository),
82
+ new CodeGraphStructuralBackend(project.repository).inspect(),
83
+ ]);
84
+ const warnings = [];
85
+ if (project.summary.freshness === "stale") {
86
+ warnings.push({
87
+ code: "STALE_INDEX",
88
+ message: "The repository differs from the published Atlas snapshot.",
89
+ });
90
+ }
91
+ if (structural.completeness === "incomplete") {
92
+ warnings.push({ code: "INCOMPLETE_INDEX", message: "The structural index is incomplete." });
93
+ }
94
+ for (const language of languages) {
95
+ if (language.support === "unsupported") {
96
+ warnings.push({ code: "UNSUPPORTED_LANGUAGE", message: language.reason });
97
+ }
98
+ }
99
+ return {
100
+ project: project.summary,
101
+ currentRevision: {
102
+ headCommit: project.currentSnapshot.headCommit,
103
+ changes: {
104
+ staged: project.currentSnapshot.changes.staged.length,
105
+ unstaged: project.currentSnapshot.changes.unstaged.length,
106
+ untracked: project.currentSnapshot.changes.untracked.length,
107
+ },
108
+ },
109
+ languages,
110
+ backend: {
111
+ backendVersion: structural.backendVersion,
112
+ completeness: structural.completeness,
113
+ extractionVersion: structural.extractionVersion,
114
+ indexedAt: structural.indexedAt,
115
+ },
116
+ warnings,
117
+ };
118
+ });
119
+ }
120
+ getMap(repositoryId, focusKey) {
121
+ return this.withPublishedWorld(repositoryId, async (query) => {
122
+ const map = await query.view(focusKey);
123
+ if (map === undefined) {
124
+ throw new AtlasReadError(404, "BUSINESS_NODE_NOT_FOUND", "The requested business node is not available.");
125
+ }
126
+ return map;
127
+ });
128
+ }
129
+ searchBusiness(repositoryId, queryText, limit) {
130
+ return this.withPublishedWorld(repositoryId, async (query) => ({
131
+ query: queryText,
132
+ limit,
133
+ results: await query.searchBusiness(queryText, { limit }),
134
+ }));
135
+ }
136
+ getBusinessNode(repositoryId, businessKey) {
137
+ return this.withPublishedWorld(repositoryId, async (query) => {
138
+ const node = await query.showBusiness(businessKey);
139
+ if (node === undefined) {
140
+ throw new AtlasReadError(404, "BUSINESS_NODE_NOT_FOUND", "The requested business node is not available.");
141
+ }
142
+ return node;
143
+ });
144
+ }
145
+ withPublishedWorld(repositoryId, operation) {
146
+ return this.withRepositoryRead(repositoryId, async (project) => {
147
+ const env_1 = { stack: [], error: void 0, hasError: false };
148
+ try {
149
+ if (project.summary.status !== "current" || project.summary.snapshotId === null) {
150
+ throw unavailableWorld();
151
+ }
152
+ const structural = new CodeGraphStructuralBackend(project.repository);
153
+ const state = await structural.inspect();
154
+ if (state.completeness !== "complete") {
155
+ throw unavailableWorld();
156
+ }
157
+ const graph = __addDisposableResource(env_1, new GraphStore(project.repository, { access: "read-only" }), false);
158
+ const query = __addDisposableResource(env_1, new WorldGraphQuery(project.repository, graph, structural, {
159
+ atlasAccess: "read-only",
160
+ }), false);
161
+ try {
162
+ return await operation(query);
163
+ }
164
+ catch (error) {
165
+ if (error instanceof AtlasReadError) {
166
+ throw error;
167
+ }
168
+ if (error instanceof Error && error.message.includes("publication changed")) {
169
+ throw new AtlasReadError(409, "PUBLICATION_CHANGED", "The Atlas publication changed during the request. Try again.");
170
+ }
171
+ if (isUnavailableWorldError(error)) {
172
+ throw unavailableWorld();
173
+ }
174
+ throw error;
175
+ }
176
+ }
177
+ catch (e_1) {
178
+ env_1.error = e_1;
179
+ env_1.hasError = true;
180
+ }
181
+ finally {
182
+ __disposeResources(env_1);
183
+ }
184
+ });
185
+ }
186
+ async withRepositoryRead(repositoryId, operation) {
187
+ const previous = this.#repositoryQueues.get(repositoryId) ?? Promise.resolve();
188
+ let resolveQueue;
189
+ const queue = new Promise((resolve) => { resolveQueue = resolve; });
190
+ const queueTail = previous.catch(() => undefined).then(() => queue);
191
+ this.#repositoryQueues.set(repositoryId, queueTail);
192
+ await previous.catch(() => undefined);
193
+ try {
194
+ const project = await this.#catalog.findProject(repositoryId);
195
+ if (project === undefined) {
196
+ throw new AtlasReadError(404, "PROJECT_NOT_FOUND", "The requested project is not available.");
197
+ }
198
+ return await operation(project);
199
+ }
200
+ finally {
201
+ resolveQueue();
202
+ if (this.#repositoryQueues.get(repositoryId) === queueTail) {
203
+ this.#repositoryQueues.delete(repositoryId);
204
+ }
205
+ }
206
+ }
207
+ }
208
+ function isUnavailableWorldError(error) {
209
+ return error instanceof StructuralBackendError
210
+ || (error instanceof Error && (error.message.startsWith("World snapshot is ")
211
+ || error.message.includes("current world publication")
212
+ || error.message.includes("current world snapshot")));
213
+ }
214
+ function unavailableWorld() {
215
+ return new AtlasReadError(409, "ATLAS_STATE_UNAVAILABLE", "The current Atlas publication is unavailable for this project.");
216
+ }
@@ -0,0 +1,15 @@
1
+ import type { GitRepository } from "../../repository/types.js";
2
+ import type { RepositorySnapshot } from "../../snapshots/types.js";
3
+ import type { WebProjectSummary } from "./types.js";
4
+ export interface PrimaryRepositoryProject {
5
+ readonly repository: GitRepository;
6
+ readonly currentSnapshot: RepositorySnapshot;
7
+ readonly summary: WebProjectSummary;
8
+ }
9
+ export declare class PrimaryRepositoryCatalog {
10
+ listProjects(): Promise<readonly WebProjectSummary[]>;
11
+ findProject(repositoryId: string): Promise<PrimaryRepositoryProject | undefined>;
12
+ private discoverProjects;
13
+ private inspectStoredRepository;
14
+ }
15
+ //# sourceMappingURL=primary-repository-catalog.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"primary-repository-catalog.d.ts","sourceRoot":"","sources":["../../../src/web/application/primary-repository-catalog.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAE/D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAKnE,OAAO,KAAK,EAGV,iBAAiB,EAClB,MAAM,YAAY,CAAC;AAmBpB,MAAM,WAAW,wBAAwB;IACvC,QAAQ,CAAC,UAAU,EAAE,aAAa,CAAC;IACnC,QAAQ,CAAC,eAAe,EAAE,kBAAkB,CAAC;IAC7C,QAAQ,CAAC,OAAO,EAAE,iBAAiB,CAAC;CACrC;AAED,qBAAa,wBAAwB;IAC7B,YAAY,IAAI,OAAO,CAAC,SAAS,iBAAiB,EAAE,CAAC;IAIrD,WAAW,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,wBAAwB,GAAG,SAAS,CAAC;YAOxE,gBAAgB;YAmChB,uBAAuB;CA6EtC"}