@psnext/lscg 0.1.4 → 0.1.6

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 (62) hide show
  1. package/README.md +117 -15
  2. package/dist/bin/lscg.js +0 -0
  3. package/dist/src/cli-progress.d.ts +7 -0
  4. package/dist/src/cli-progress.js +59 -0
  5. package/dist/src/cli.js +62 -9
  6. package/dist/src/explore/sigma-provider.d.ts +27 -0
  7. package/dist/src/explore/sigma-provider.js +87 -0
  8. package/dist/src/explore/sigma-render.d.ts +18 -0
  9. package/dist/src/explore/sigma-render.js +67 -0
  10. package/dist/src/graph/attribution.d.ts +2 -2
  11. package/dist/src/graph/attribution.js +36 -13
  12. package/dist/src/graph/explore.d.ts +20 -0
  13. package/dist/src/graph/explore.js +200 -0
  14. package/dist/src/graph/repository.d.ts +36 -4
  15. package/dist/src/graph/repository.js +443 -159
  16. package/dist/src/graph/repositoryScanWorker.d.ts +21 -0
  17. package/dist/src/graph/repositoryScanWorker.js +45 -0
  18. package/dist/src/index.d.ts +6 -0
  19. package/dist/src/index.js +5 -0
  20. package/dist/src/mcp/server.js +21 -3
  21. package/dist/src/parser/treeSitter.js +20 -1
  22. package/dist/src/scanner/artifactInventory.d.ts +35 -0
  23. package/dist/src/scanner/artifactInventory.js +139 -0
  24. package/dist/src/scanner/attributionPlugin.d.ts +5 -0
  25. package/dist/src/scanner/attributionPlugin.js +16 -0
  26. package/dist/src/scanner/discover.js +89 -16
  27. package/dist/src/scanner/fingerprint.js +5 -0
  28. package/dist/src/scanner/javaDependencyPlugin.d.ts +5 -0
  29. package/dist/src/scanner/javaDependencyPlugin.js +107 -0
  30. package/dist/src/scanner/javaPlugin.d.ts +5 -0
  31. package/dist/src/scanner/javaPlugin.js +199 -0
  32. package/dist/src/scanner/javaScanWorker.d.ts +2 -0
  33. package/dist/src/scanner/javaScanWorker.js +8 -0
  34. package/dist/src/scanner/packageParseWorker.d.ts +17 -0
  35. package/dist/src/scanner/packageParseWorker.js +30 -0
  36. package/dist/src/scanner/packagePlugin.js +83 -24
  37. package/dist/src/scanner/parallelScan.d.ts +2 -0
  38. package/dist/src/scanner/parallelScan.js +32 -0
  39. package/dist/src/scanner/plugins.d.ts +38 -4
  40. package/dist/src/scanner/plugins.js +58 -5
  41. package/dist/src/scanner/pythonPlugin.d.ts +5 -0
  42. package/dist/src/scanner/pythonPlugin.js +198 -0
  43. package/dist/src/scanner/pythonScanWorker.d.ts +2 -0
  44. package/dist/src/scanner/pythonScanWorker.js +8 -0
  45. package/dist/src/storage/connection.js +85 -0
  46. package/dist/src/storage/database.d.ts +1 -0
  47. package/dist/src/storage/database.js +1 -0
  48. package/dist/src/storage/explore-queries.d.ts +52 -0
  49. package/dist/src/storage/explore-queries.js +184 -0
  50. package/dist/src/storage/graph-writes.d.ts +22 -3
  51. package/dist/src/storage/graph-writes.js +167 -20
  52. package/dist/src/storage/manifest-inventory.d.ts +23 -0
  53. package/dist/src/storage/manifest-inventory.js +82 -0
  54. package/dist/src/storage/plugin-graph.js +3 -3
  55. package/dist/src/storage/queries.d.ts +10 -3
  56. package/dist/src/storage/queries.js +99 -24
  57. package/dist/src/storage/schema.d.ts +2 -2
  58. package/dist/src/storage/schema.js +48 -1
  59. package/dist/src/types.d.ts +110 -6
  60. package/dist/src/watch.d.ts +20 -2
  61. package/dist/src/watch.js +211 -46
  62. package/package.json +9 -3
@@ -2,6 +2,7 @@ import { createHash } from 'node:crypto';
2
2
  import { readFileSync, statSync } from 'node:fs';
3
3
  import path from 'node:path';
4
4
  import { discoverSourceFiles } from './discover.js';
5
+ import { inventoryRepositoryManifests } from './artifactInventory.js';
5
6
  export const SCANNER_PLUGIN_API_VERSION = 1;
6
7
  export const DEFAULT_PLUGIN_RESOURCE_LIMITS = {
7
8
  maxNodes: 100_000,
@@ -16,8 +17,9 @@ const GRAPH_NODE_KINDS = new Set(['file', 'symbol', 'import', 'export', 'call',
16
17
  const GRAPH_EDGE_KINDS = new Set(['contains', 'defines', 'imports', 'exports', 'calls', 'attributed_to', 'provides']);
17
18
  /** Namespace used by plugins to target nodes produced by the built-in scanner. */
18
19
  export const BUILTIN_GRAPH_PLUGIN = '__lscg_builtin__';
19
- export function createRepositoryScanContext(repoPath) {
20
+ export function createRepositoryScanContext(repoPath, suppliedManifests) {
20
21
  const discoveredPaths = discoverSourceFiles(repoPath);
22
+ const manifests = suppliedManifests ? [...suppliedManifests] : inventoryRepositoryManifests(repoPath);
21
23
  const files = discoveredPaths.flatMap((relativePath) => {
22
24
  try {
23
25
  const absolutePath = path.join(repoPath, relativePath);
@@ -38,23 +40,32 @@ export function createRepositoryScanContext(repoPath) {
38
40
  return [];
39
41
  }
40
42
  });
41
- return { repoPath, files, discoveredPaths, readFile: (relativePath) => readFileSync(path.join(repoPath, relativePath), 'utf8') };
43
+ return { repoPath, files, discoveredPaths, manifests, readFile: (relativePath) => readFileSync(path.join(repoPath, relativePath), 'utf8') };
42
44
  }
43
- export async function runScannerPlugins(context, plugins, resourceLimits = {}) {
45
+ export async function runScannerPlugins(context, plugins, resourceLimits = {}, delta, previousContributions = new Map()) {
44
46
  const limits = normalizePluginResourceLimits(resourceLimits);
45
47
  const nodes = [];
46
48
  const edges = [];
47
49
  const diagnostics = [];
48
50
  const failedPlugins = [];
51
+ const failedPaths = new Set();
49
52
  const successfulPlugins = [];
53
+ const enrichmentPlugins = new Map();
54
+ const incrementalRetractions = new Map();
50
55
  const owners = new Map();
51
56
  for (const plugin of plugins) {
52
57
  try {
53
58
  validatePlugin(plugin);
54
59
  await plugin.initialize?.(context);
55
- const result = await plugin.scan(context);
60
+ const result = plugin.scanIncremental && delta
61
+ ? await plugin.scanIncremental(context, delta, previousContributions.get(plugin.name))
62
+ : await plugin.scan(context);
56
63
  const finalized = await plugin.finalize?.(context);
64
+ if (plugin.scanIncremental && delta && 'retractions' in result && result.retractions)
65
+ incrementalRetractions.set(plugin.name, result.retractions);
57
66
  validatePluginResult(plugin.name, result, 'scan');
67
+ for (const failedPath of result.failedPaths ?? [])
68
+ failedPaths.add(failedPath);
58
69
  if (finalized !== undefined)
59
70
  validatePluginResult(plugin.name, finalized, 'finalize');
60
71
  enforceResourceLimits(plugin, result, finalized, limits);
@@ -62,6 +73,11 @@ export async function runScannerPlugins(context, plugins, resourceLimits = {}) {
62
73
  if (finalized)
63
74
  collect(plugin, finalized, nodes, edges, owners, diagnostics, limits);
64
75
  successfulPlugins.push(plugin.name);
76
+ if (plugin.capabilities.includes('enrichment')) {
77
+ if (typeof plugin.enrich !== 'function')
78
+ throw new Error(`invalid enrichment plugin: ${plugin.name} must define enrich()`);
79
+ enrichmentPlugins.set(plugin.name, plugin);
80
+ }
65
81
  }
66
82
  catch (error) {
67
83
  failedPlugins.push(plugin.name);
@@ -86,6 +102,15 @@ export async function runScannerPlugins(context, plugins, resourceLimits = {}) {
86
102
  return [canonicalNodeIdentity(node.pluginName ?? '', node.factKey), materialized];
87
103
  }));
88
104
  const materializedNodes = [...nodeByIdentity.values()];
105
+ for (const [plugin, retractions] of incrementalRetractions) {
106
+ const previous = previousContributions.get(plugin);
107
+ const retractedNodeKeys = new Set(retractions.nodes ?? []);
108
+ for (const node of previous?.nodes ?? []) {
109
+ const factKey = String(node.metadata.factKey ?? '');
110
+ if (factKey && !retractedNodeKeys.has(factKey))
111
+ nodeByIdentity.set(canonicalNodeIdentity(plugin, factKey), node);
112
+ }
113
+ }
89
114
  const materializedEdges = [];
90
115
  for (const edge of edges) {
91
116
  const sourceIdentity = canonicalNodeIdentity(edge.sourcePlugin ?? edge.pluginName ?? '', edge.sourceFactKey);
@@ -104,6 +129,7 @@ export async function runScannerPlugins(context, plugins, resourceLimits = {}) {
104
129
  sourceId: source.id,
105
130
  targetId: target.id,
106
131
  kind: edge.kind,
132
+ type: edge.type ?? edge.kind,
107
133
  confidence: edge.confidence ?? 1,
108
134
  metadata: {
109
135
  ...(edge.metadata ?? {}),
@@ -126,6 +152,33 @@ export async function runScannerPlugins(context, plugins, resourceLimits = {}) {
126
152
  if (plugin)
127
153
  contributionFor(plugin).edges.push(edge);
128
154
  }
155
+ for (const [plugin, retractions] of incrementalRetractions) {
156
+ const previous = previousContributions.get(plugin);
157
+ if (!previous)
158
+ continue;
159
+ const current = contributionFor(plugin);
160
+ const nodeKeys = new Set(retractions.nodes ?? []);
161
+ const edgeKeys = new Set(retractions.edges ?? []);
162
+ const currentNodeKeys = new Set(current.nodes.map((node) => String(node.metadata.factKey ?? '')));
163
+ const currentEdgeKeys = new Set(current.edges.map((edge) => String(edge.metadata.factKey ?? '')));
164
+ current.nodes = [
165
+ ...previous.nodes.filter((node) => !nodeKeys.has(String(node.metadata.factKey ?? '')) && !currentNodeKeys.has(String(node.metadata.factKey ?? ''))),
166
+ ...current.nodes
167
+ ];
168
+ current.edges = [
169
+ ...previous.edges.filter((edge) => !edgeKeys.has(String(edge.metadata.factKey ?? '')) && !currentEdgeKeys.has(String(edge.metadata.factKey ?? ''))),
170
+ ...current.edges
171
+ ];
172
+ }
173
+ const partiallyReconciledPlugins = new Set(incrementalRetractions.keys());
174
+ const mergedNodes = [
175
+ ...materializedNodes.filter((node) => !partiallyReconciledPlugins.has(String(node.metadata.plugin ?? ''))),
176
+ ...[...partiallyReconciledPlugins].flatMap((plugin) => pluginContributions.get(plugin)?.nodes ?? [])
177
+ ];
178
+ const mergedEdges = [
179
+ ...materializedEdges.filter((edge) => !partiallyReconciledPlugins.has(String(edge.metadata.plugin ?? ''))),
180
+ ...[...partiallyReconciledPlugins].flatMap((plugin) => pluginContributions.get(plugin)?.edges ?? [])
181
+ ];
129
182
  function contributionFor(plugin) {
130
183
  const existing = pluginContributions.get(plugin);
131
184
  if (existing)
@@ -134,7 +187,7 @@ export async function runScannerPlugins(context, plugins, resourceLimits = {}) {
134
187
  pluginContributions.set(plugin, created);
135
188
  return created;
136
189
  }
137
- return { nodes: materializedNodes, edges: materializedEdges, diagnostics, failedPlugins, successfulPlugins, pluginContributions };
190
+ return { nodes: mergedNodes, edges: mergedEdges, diagnostics, failedPaths: [...failedPaths].sort(), failedPlugins, successfulPlugins, pluginContributions, enrichmentPlugins };
138
191
  function collect(plugin, result, nodeOutput, edgeOutput, ownerMap, messages, contributionLimits) {
139
192
  for (const node of result.nodes ?? []) {
140
193
  const identity = canonicalNodeIdentity(plugin.name, node.factKey);
@@ -0,0 +1,5 @@
1
+ import type { PluginScanResult, RepositoryScanFile, ScannerPlugin } from './plugins.js';
2
+ export declare const PYTHON_SCANNER_PLUGIN_NAME = "python-scanner-plugin";
3
+ export declare function scanPythonFiles(files: readonly RepositoryScanFile[]): PluginScanResult;
4
+ export declare const PythonScannerPlugin: ScannerPlugin;
5
+ //# sourceMappingURL=pythonPlugin.d.ts.map
@@ -0,0 +1,198 @@
1
+ import Parser from 'tree-sitter';
2
+ import { parseSource } from '../parser/treeSitter.js';
3
+ import { runBatchedWorkers } from './parallelScan.js';
4
+ export const PYTHON_SCANNER_PLUGIN_NAME = 'python-scanner-plugin';
5
+ const PYTHON_EXTENSIONS = new Set(['.py']);
6
+ const SYMBOL_TYPES = new Set(['class_definition', 'function_definition', 'assignment']);
7
+ const IMPORT_TYPES = new Set(['import_statement', 'import_from_statement']);
8
+ const CALL_TYPES = new Set(['call']);
9
+ export function scanPythonFiles(files) {
10
+ const nodes = [];
11
+ const edges = [];
12
+ for (const file of files) {
13
+ if (!isPython(file.path))
14
+ continue;
15
+ const parsed = parseSource(file.absolutePath, file.source);
16
+ if (!parsed || parsed.language !== 'python')
17
+ continue;
18
+ const sourceBuffer = Buffer.from(file.source, 'utf8');
19
+ const fileKey = factKey(file.path, 'file', 0, sourceBuffer.length);
20
+ nodes.push(node(file, fileKey, parsed, 'file', 'file', file.path, parsed.tree.rootNode, sourceBuffer, { language: 'python' }));
21
+ walk(parsed.tree.rootNode, [], [], fileKey, file, parsed, sourceBuffer, nodes, edges);
22
+ }
23
+ return { nodes, edges };
24
+ }
25
+ async function scanPythonFilesInBatches(files) {
26
+ const pythonFiles = files.filter((file) => isPython(file.path));
27
+ const results = await runBatchedWorkers(pythonFiles, new URL('./pythonScanWorker.js', import.meta.url), (batch) => batch);
28
+ return {
29
+ nodes: results.flatMap((result) => result.nodes),
30
+ edges: results.flatMap((result) => result.edges),
31
+ failedPaths: results.flatMap((result) => result.failedPaths ?? []),
32
+ diagnostics: results.flatMap((result) => result.diagnostics ?? [])
33
+ };
34
+ }
35
+ export const PythonScannerPlugin = {
36
+ name: PYTHON_SCANNER_PLUGIN_NAME,
37
+ apiVersion: 1,
38
+ capabilities: ['file-scanner'],
39
+ scan: (context) => scanPythonFilesInBatches(context.files),
40
+ scanIncremental: (context, delta, previous) => scanPythonIncremental(context, delta, previous)
41
+ };
42
+ async function scanPythonIncremental(context, delta, previous) {
43
+ if (delta.full || !previous)
44
+ return { ...(await scanPythonFilesInBatches(context.files)), retractions: { nodes: [], edges: [] } };
45
+ const changed = new Set([...delta.changedPaths, ...delta.deletedPaths].filter(isPython));
46
+ if (changed.size === 0)
47
+ return { nodes: [], edges: [], retractions: { nodes: [], edges: [] } };
48
+ const retractions = { nodes: [], edges: [] };
49
+ for (const fact of previous.nodes) {
50
+ if (fact.metadata.filePath && changed.has(String(fact.metadata.filePath))) {
51
+ const key = String(fact.metadata.factKey ?? '');
52
+ if (key)
53
+ retractions.nodes.push(key);
54
+ }
55
+ }
56
+ for (const fact of previous.edges) {
57
+ if (fact.metadata.filePath && changed.has(String(fact.metadata.filePath))) {
58
+ const key = String(fact.metadata.factKey ?? '');
59
+ if (key)
60
+ retractions.edges.push(key);
61
+ }
62
+ }
63
+ return {
64
+ ...(await scanPythonFilesInBatches(context.files.filter((file) => changed.has(file.path)))),
65
+ retractions
66
+ };
67
+ }
68
+ function walk(treeNode, symbolAncestry, graphAncestry, fileKey, file, parsed, sourceBuffer, nodes, edges) {
69
+ let currentKey;
70
+ let nextSymbols = symbolAncestry;
71
+ let nextGraph = graphAncestry;
72
+ const kind = kindFor(treeNode);
73
+ if (kind) {
74
+ const name = nameFor(treeNode, sourceBuffer);
75
+ currentKey = factKey(file.path, kind, treeNode.startIndex, treeNode.endIndex);
76
+ const metadata = metadataFor(treeNode, sourceBuffer, name);
77
+ nodes.push(node(file, currentKey, parsed, kind, treeNode.type, name, treeNode, sourceBuffer, metadata));
78
+ const parentKey = graphAncestry.at(-1) ?? fileKey;
79
+ edges.push({
80
+ factKey: `contains:${file.path}:${treeNode.startIndex}:${treeNode.endIndex}`,
81
+ sourceFactKey: parentKey,
82
+ targetFactKey: currentKey,
83
+ filePath: file.path,
84
+ kind: kind === 'symbol' && symbolAncestry.length === 0 ? 'defines' : 'contains',
85
+ metadata: { discoveredFrom: treeNode.type }
86
+ });
87
+ if (kind === 'import') {
88
+ edges.push({
89
+ factKey: `imports:${file.path}:${treeNode.startIndex}:${treeNode.endIndex}`,
90
+ sourceFactKey: fileKey,
91
+ targetFactKey: currentKey,
92
+ filePath: file.path,
93
+ kind: 'imports',
94
+ confidence: 0.65,
95
+ metadata: { module: metadata.module ?? name }
96
+ });
97
+ }
98
+ else if (kind === 'call') {
99
+ edges.push({
100
+ factKey: `calls:${file.path}:${treeNode.startIndex}:${treeNode.endIndex}`,
101
+ sourceFactKey: symbolAncestry.at(-1) ?? fileKey,
102
+ targetFactKey: currentKey,
103
+ filePath: file.path,
104
+ kind: 'calls',
105
+ confidence: 0.65,
106
+ metadata: { callee: name }
107
+ });
108
+ }
109
+ if (kind === 'symbol') {
110
+ if (symbolAncestry.length === 0 && name && !name.startsWith('_')) {
111
+ const exportKey = factKey(file.path, 'export', treeNode.startIndex, treeNode.endIndex);
112
+ nodes.push(node(file, exportKey, parsed, 'export', 'implicit_export', name, treeNode, sourceBuffer, {
113
+ exportKind: 'implicit-python-export',
114
+ symbol: name
115
+ }));
116
+ edges.push({
117
+ factKey: `exports:${file.path}:${treeNode.startIndex}:${treeNode.endIndex}`,
118
+ sourceFactKey: fileKey,
119
+ targetFactKey: exportKey,
120
+ filePath: file.path,
121
+ kind: 'exports',
122
+ metadata: { symbol: name, implicit: true }
123
+ });
124
+ }
125
+ nextSymbols = [...symbolAncestry, currentKey];
126
+ }
127
+ nextGraph = [...graphAncestry, currentKey];
128
+ }
129
+ for (const child of treeNode.namedChildren) {
130
+ walk(child, nextSymbols, nextGraph, fileKey, file, parsed, sourceBuffer, nodes, edges);
131
+ }
132
+ }
133
+ function kindFor(node) {
134
+ if (SYMBOL_TYPES.has(node.type))
135
+ return 'symbol';
136
+ if (IMPORT_TYPES.has(node.type))
137
+ return 'import';
138
+ if (CALL_TYPES.has(node.type))
139
+ return 'call';
140
+ return undefined;
141
+ }
142
+ function nameFor(node, sourceBuffer) {
143
+ const name = node.childForFieldName('name');
144
+ if (name)
145
+ return textFor(name, sourceBuffer).trim();
146
+ if (node.type === 'assignment' || node.type === 'augmented_assignment') {
147
+ const left = node.childForFieldName('left') ?? node.namedChildren[0];
148
+ return left ? textFor(left, sourceBuffer).trim() : null;
149
+ }
150
+ if (node.type === 'call') {
151
+ const fn = node.childForFieldName('function') ?? node.namedChildren[0];
152
+ return fn ? textFor(fn, sourceBuffer).trim().slice(0, 120) : null;
153
+ }
154
+ if (IMPORT_TYPES.has(node.type))
155
+ return moduleForImport(node, sourceBuffer);
156
+ return null;
157
+ }
158
+ function metadataFor(node, sourceBuffer, name) {
159
+ const snippet = textFor(node, sourceBuffer).trim().slice(0, 240);
160
+ if (IMPORT_TYPES.has(node.type))
161
+ return { module: moduleForImport(node, sourceBuffer), snippet };
162
+ if (node.type === 'call')
163
+ return { callee: name, snippet };
164
+ return { snippet };
165
+ }
166
+ function moduleForImport(node, sourceBuffer) {
167
+ const source = textFor(node, sourceBuffer).trim();
168
+ if (node.type === 'import_statement')
169
+ return source.replace(/^import\s+/, '').split(',')[0]?.trim() ?? source;
170
+ return /^from\s+([^\s]+)\s+import\s+/u.exec(source)?.[1] ?? source;
171
+ }
172
+ function node(file, factKeyValue, parsed, kind, type, name, treeNode, sourceBuffer, metadata) {
173
+ return {
174
+ factKey: factKeyValue,
175
+ filePath: file.path,
176
+ kind,
177
+ type,
178
+ name,
179
+ startByte: treeNode.startIndex,
180
+ endByte: treeNode.endIndex,
181
+ startPoint: treeNode.startPosition,
182
+ endPoint: treeNode.endPosition,
183
+ sourceHash: file.hash,
184
+ parser: parsed.parser,
185
+ parserVersion: parsed.parserVersion,
186
+ metadata: { ...metadata, filePath: file.path, factKey: factKeyValue, sourceBytes: sourceBuffer.length }
187
+ };
188
+ }
189
+ function factKey(filePath, kind, startByte, endByte) {
190
+ return `${kind}:${filePath}:${startByte}:${endByte}`;
191
+ }
192
+ function textFor(node, sourceBuffer) {
193
+ return sourceBuffer.subarray(node.startIndex, node.endIndex).toString('utf8');
194
+ }
195
+ function isPython(filePath) {
196
+ return PYTHON_EXTENSIONS.has(filePath.slice(filePath.lastIndexOf('.')).toLowerCase());
197
+ }
198
+ //# sourceMappingURL=pythonPlugin.js.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=pythonScanWorker.d.ts.map
@@ -0,0 +1,8 @@
1
+ import { parentPort } from 'node:worker_threads';
2
+ import { scanPythonFiles } from './pythonPlugin.js';
3
+ if (!parentPort)
4
+ throw new Error('python scan worker requires a parent port');
5
+ parentPort.on('message', (files) => {
6
+ parentPort.postMessage(scanPythonFiles(files));
7
+ });
8
+ //# sourceMappingURL=pythonScanWorker.js.map
@@ -9,6 +9,7 @@ export function openGraphDatabase(databasePath) {
9
9
  db.exec('PRAGMA journal_mode = WAL;');
10
10
  db.exec(CREATE_SCHEMA_SQL);
11
11
  ensureSchemaVersion(db);
12
+ db.exec('CREATE INDEX IF NOT EXISTS idx_edges_repo_type ON edges(repository_id, type);');
12
13
  return db;
13
14
  }
14
15
  export function openReadOnlyGraphDatabase(databasePath) {
@@ -44,6 +45,26 @@ function ensureSchemaVersion(db) {
44
45
  }
45
46
  if (currentVersion <= 3) {
46
47
  migrateSchemaV3ToV4(db);
48
+ db.prepare('INSERT OR IGNORE INTO schema_migrations(version) VALUES (?)').run(4);
49
+ }
50
+ if (currentVersion <= 4) {
51
+ migrateSchemaV4ToV5(db);
52
+ db.prepare('INSERT OR IGNORE INTO schema_migrations(version) VALUES (?)').run(5);
53
+ }
54
+ if (currentVersion <= 5) {
55
+ migrateSchemaV5ToV6(db);
56
+ db.prepare('INSERT OR IGNORE INTO schema_migrations(version) VALUES (?)').run(6);
57
+ }
58
+ if (currentVersion <= 6) {
59
+ migrateSchemaV6ToV7(db);
60
+ db.prepare('INSERT OR IGNORE INTO schema_migrations(version) VALUES (?)').run(7);
61
+ }
62
+ if (currentVersion <= 7) {
63
+ migrateSchemaV7ToV8(db);
64
+ db.prepare('INSERT OR IGNORE INTO schema_migrations(version) VALUES (?)').run(8);
65
+ }
66
+ if (currentVersion <= 8) {
67
+ migrateSchemaV8ToV9(db);
47
68
  db.prepare('INSERT OR IGNORE INTO schema_migrations(version) VALUES (?)').run(SCHEMA_VERSION);
48
69
  return;
49
70
  }
@@ -63,6 +84,70 @@ function migrateSchemaV3ToV4(db) {
63
84
  CREATE INDEX IF NOT EXISTS idx_plugin_contributions_repo ON plugin_contributions(repository_id);
64
85
  `);
65
86
  }
87
+ function migrateSchemaV4ToV5(db) {
88
+ db.exec(`
89
+ CREATE TABLE IF NOT EXISTS file_scan_failures (
90
+ repository_id TEXT NOT NULL REFERENCES repositories(id) ON DELETE CASCADE,
91
+ path TEXT NOT NULL,
92
+ diagnostic TEXT NOT NULL,
93
+ failed_at TEXT NOT NULL DEFAULT (datetime('now')),
94
+ PRIMARY KEY(repository_id, path)
95
+ );
96
+ CREATE TABLE IF NOT EXISTS repository_scan_state (
97
+ repository_id TEXT PRIMARY KEY REFERENCES repositories(id) ON DELETE CASCADE,
98
+ state TEXT NOT NULL CHECK(state IN ('fresh', 'degraded', 'stale')),
99
+ updated_at TEXT NOT NULL DEFAULT (datetime('now'))
100
+ );
101
+ `);
102
+ }
103
+ function migrateSchemaV5ToV6(db) {
104
+ db.exec(`
105
+ CREATE TABLE IF NOT EXISTS file_enrichment_state (
106
+ repository_id TEXT NOT NULL REFERENCES repositories(id) ON DELETE CASCADE,
107
+ file_id TEXT PRIMARY KEY REFERENCES files(id) ON DELETE CASCADE,
108
+ source_hash TEXT NOT NULL,
109
+ state TEXT NOT NULL CHECK(state IN ('pending', 'complete', 'failed')),
110
+ diagnostic TEXT,
111
+ queued_at TEXT NOT NULL DEFAULT (datetime('now')),
112
+ updated_at TEXT NOT NULL DEFAULT (datetime('now')),
113
+ completed_at TEXT,
114
+ failed_at TEXT
115
+ );
116
+ CREATE INDEX IF NOT EXISTS idx_file_enrichment_state_repo_state ON file_enrichment_state(repository_id, state);
117
+ `);
118
+ }
119
+ function migrateSchemaV6ToV7(db) {
120
+ db.exec(`
121
+ CREATE TABLE IF NOT EXISTS repository_manifest_inventory (
122
+ repository_id TEXT NOT NULL REFERENCES repositories(id) ON DELETE CASCADE,
123
+ path TEXT NOT NULL,
124
+ kind TEXT NOT NULL CHECK(kind IN ('maven-pom', 'gradle-groovy', 'gradle-kotlin')),
125
+ module_identity TEXT NOT NULL,
126
+ status TEXT NOT NULL CHECK(status IN ('observed', 'read_failed', 'traversal_incomplete', 'confirmed_deleted')),
127
+ size INTEGER NOT NULL,
128
+ mtime_ms INTEGER NOT NULL,
129
+ source_hash TEXT,
130
+ generation INTEGER NOT NULL,
131
+ updated_at TEXT NOT NULL DEFAULT (datetime('now')),
132
+ PRIMARY KEY(repository_id, path)
133
+ );
134
+ CREATE INDEX IF NOT EXISTS idx_repository_manifest_inventory_repo_status
135
+ ON repository_manifest_inventory(repository_id, status);
136
+ `);
137
+ }
138
+ function migrateSchemaV7ToV8(db) {
139
+ const columns = new Set(db.prepare('PRAGMA table_info(file_enrichment_state)').all().map((column) => column.name));
140
+ if (!columns.has('plugin_name'))
141
+ db.exec("ALTER TABLE file_enrichment_state ADD COLUMN plugin_name TEXT NOT NULL DEFAULT 'git-attribution-plugin'");
142
+ if (!columns.has('history_fingerprint'))
143
+ db.exec('ALTER TABLE file_enrichment_state ADD COLUMN history_fingerprint TEXT');
144
+ if (!columns.has('outcome'))
145
+ db.exec('ALTER TABLE file_enrichment_state ADD COLUMN outcome TEXT');
146
+ }
147
+ function migrateSchemaV8ToV9(db) {
148
+ db.exec('ALTER TABLE edges ADD COLUMN type TEXT NOT NULL DEFAULT kind;');
149
+ db.exec('CREATE INDEX IF NOT EXISTS idx_edges_repo_type ON edges(repository_id, type);');
150
+ }
66
151
  function getCurrentSchemaVersion(db) {
67
152
  const row = db.prepare('SELECT COALESCE(MAX(version), 0) AS version FROM schema_migrations').get();
68
153
  return Number(row?.version ?? 0);
@@ -5,6 +5,7 @@
5
5
  */
6
6
  export * from './connection.js';
7
7
  export * from './plugin-contributions.js';
8
+ export * from './manifest-inventory.js';
8
9
  export * from './graph-writes.js';
9
10
  export * from './plugin-graph.js';
10
11
  export * from './queries.js';
@@ -5,6 +5,7 @@
5
5
  */
6
6
  export * from './connection.js';
7
7
  export * from './plugin-contributions.js';
8
+ export * from './manifest-inventory.js';
8
9
  export * from './graph-writes.js';
9
10
  export * from './plugin-graph.js';
10
11
  export * from './queries.js';
@@ -0,0 +1,52 @@
1
+ import { DatabaseSync } from 'node:sqlite';
2
+ import type { ExploreEdgeKind, ExploreDirection, GraphNodeKind, Point } from '../types.js';
3
+ export interface ExploreAggregateRow {
4
+ id: string;
5
+ path: string;
6
+ language: string;
7
+ childCount: number;
8
+ repositoryId: string;
9
+ }
10
+ export interface ExploreSourceRow {
11
+ id: string;
12
+ kind: GraphNodeKind;
13
+ type: string;
14
+ name: string | null;
15
+ fileId: string | null;
16
+ path: string | null;
17
+ startPoint: Point;
18
+ endPoint: Point;
19
+ metadata: Record<string, unknown>;
20
+ }
21
+ export interface ExploreCandidateRow extends ExploreSourceRow {
22
+ rank: number;
23
+ match: 'exact' | 'qualified' | 'path' | 'prefix' | 'substring';
24
+ qualifiedName: string | null;
25
+ aggregateId: string | null;
26
+ }
27
+ export interface ExploreEdgeRow {
28
+ id: string;
29
+ sourceId: string;
30
+ targetId: string;
31
+ kind: ExploreEdgeKind;
32
+ confidence: number;
33
+ metadata: Record<string, unknown>;
34
+ }
35
+ export declare function selectExploreAggregates(db: DatabaseSync, repositoryId: string, search?: string): ExploreAggregateRow[];
36
+ export declare function selectExploreAggregateEdges(db: DatabaseSync, repositoryId: string, kinds?: readonly ExploreEdgeKind[]): Array<ExploreEdgeRow & {
37
+ count: number;
38
+ sourceAggregateId: string;
39
+ targetAggregateId: string;
40
+ }>;
41
+ export declare function selectExploreSourcesForSearch(db: DatabaseSync, repositoryId: string, term: string, limit?: number): ExploreSourceRow[];
42
+ export declare function selectExploreCandidates(db: DatabaseSync, repositoryId: string, term: string, { kind, file, fileAnchor, limit }?: {
43
+ kind?: string;
44
+ file?: string;
45
+ fileAnchor?: boolean;
46
+ limit?: number;
47
+ }): ExploreCandidateRow[];
48
+ export declare function selectExploreNode(db: DatabaseSync, repositoryId: string, id: string): ExploreSourceRow | null;
49
+ export declare function selectExploreTraversalEdges(db: DatabaseSync, repositoryId: string, frontier: readonly string[], direction: ExploreDirection, kinds: readonly ExploreEdgeKind[], limit: number): ExploreEdgeRow[];
50
+ export declare function selectExploreNodeRows(db: DatabaseSync, repositoryId: string, ids: readonly string[]): ExploreSourceRow[];
51
+ export declare function aggregateId(repositoryId: string, fileId: string): string;
52
+ //# sourceMappingURL=explore-queries.d.ts.map