@monoes/monograph 1.4.0 → 1.4.1

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 (228) hide show
  1. package/dist/src/analysis/benchmark.js.map +1 -1
  2. package/dist/src/analysis/changed-files.d.ts.map +1 -1
  3. package/dist/src/analysis/changed-files.js +11 -2
  4. package/dist/src/analysis/changed-files.js.map +1 -1
  5. package/dist/src/analysis/changed-workspaces.d.ts.map +1 -1
  6. package/dist/src/analysis/changed-workspaces.js +11 -3
  7. package/dist/src/analysis/changed-workspaces.js.map +1 -1
  8. package/dist/src/analysis/churn.d.ts +1 -1
  9. package/dist/src/analysis/churn.d.ts.map +1 -1
  10. package/dist/src/analysis/churn.js +56 -10
  11. package/dist/src/analysis/churn.js.map +1 -1
  12. package/dist/src/analysis/feature-flags.d.ts.map +1 -1
  13. package/dist/src/analysis/feature-flags.js +11 -5
  14. package/dist/src/analysis/feature-flags.js.map +1 -1
  15. package/dist/src/analysis/progress.d.ts.map +1 -1
  16. package/dist/src/analysis/project-state.d.ts.map +1 -1
  17. package/dist/src/analysis/shape-extractor.js.map +1 -1
  18. package/dist/src/analysis/suppression-context.d.ts.map +1 -1
  19. package/dist/src/analysis/surprise-scorer.js.map +1 -1
  20. package/dist/src/analysis/targets.d.ts.map +1 -1
  21. package/dist/src/analysis/worker-pool.d.ts.map +1 -1
  22. package/dist/src/analysis/worker-pool.js.map +1 -1
  23. package/dist/src/analyze/unused-deps.js.map +1 -1
  24. package/dist/src/analyze/unused-exports.js.map +1 -1
  25. package/dist/src/analyze/unused-files.js.map +1 -1
  26. package/dist/src/cache/augment-cache.js.map +1 -1
  27. package/dist/src/cache/extraction-cache.d.ts +21 -0
  28. package/dist/src/cache/extraction-cache.d.ts.map +1 -1
  29. package/dist/src/cache/extraction-cache.js +62 -6
  30. package/dist/src/cache/extraction-cache.js.map +1 -1
  31. package/dist/src/check/index.js.map +1 -1
  32. package/dist/src/cli/check-update.js.map +1 -1
  33. package/dist/src/cli/doctor.d.ts.map +1 -1
  34. package/dist/src/cli/doctor.js +6 -10
  35. package/dist/src/cli/doctor.js.map +1 -1
  36. package/dist/src/config/boundary-config.js.map +1 -1
  37. package/dist/src/config/config-parsing.d.ts +1 -1
  38. package/dist/src/config/config-parsing.d.ts.map +1 -1
  39. package/dist/src/config/migration-types.d.ts +2 -2
  40. package/dist/src/config/migration-types.d.ts.map +1 -1
  41. package/dist/src/config/migration-types.js.map +1 -1
  42. package/dist/src/config/used-class-members.js.map +1 -1
  43. package/dist/src/config/workspace.js.map +1 -1
  44. package/dist/src/coverage/upload-source-maps.js.map +1 -1
  45. package/dist/src/duplicates/detect/filtering.d.ts.map +1 -1
  46. package/dist/src/duplicates/shingle-filter.js.map +1 -1
  47. package/dist/src/error/fallow-error.d.ts.map +1 -1
  48. package/dist/src/error/fallow-error.js.map +1 -1
  49. package/dist/src/error.d.ts.map +1 -1
  50. package/dist/src/error.js.map +1 -1
  51. package/dist/src/export/analysis-json.js.map +1 -1
  52. package/dist/src/export/badge.js.map +1 -1
  53. package/dist/src/export/dot.js.map +1 -1
  54. package/dist/src/export/sarif.js.map +1 -1
  55. package/dist/src/fix/orchestrator.js.map +1 -1
  56. package/dist/src/graph/cycle-detection.js.map +1 -1
  57. package/dist/src/graph/dead-code.d.ts +7 -2
  58. package/dist/src/graph/dead-code.d.ts.map +1 -1
  59. package/dist/src/graph/dead-code.js +19 -16
  60. package/dist/src/graph/dead-code.js.map +1 -1
  61. package/dist/src/graph/dfs.js.map +1 -1
  62. package/dist/src/graph/edge-filter.js.map +1 -1
  63. package/dist/src/graph/hotspots.js.map +1 -1
  64. package/dist/src/graph/import-chain.js.map +1 -1
  65. package/dist/src/graph/merge.js.map +1 -1
  66. package/dist/src/graph/node-search.js.map +1 -1
  67. package/dist/src/graph/node-types.d.ts.map +1 -1
  68. package/dist/src/graph/pagerank.d.ts.map +1 -1
  69. package/dist/src/graph/pagerank.js +10 -1
  70. package/dist/src/graph/pagerank.js.map +1 -1
  71. package/dist/src/graph/regex-search.js.map +1 -1
  72. package/dist/src/graph/regression.d.ts.map +1 -1
  73. package/dist/src/graph/resolve/discover-types.js.map +1 -1
  74. package/dist/src/graph/resolve/types.d.ts +3 -3
  75. package/dist/src/graph/resolve/types.d.ts.map +1 -1
  76. package/dist/src/groups/group-search.d.ts.map +1 -1
  77. package/dist/src/groups/group-search.js +9 -18
  78. package/dist/src/groups/group-search.js.map +1 -1
  79. package/dist/src/health/complexity-findings.js.map +1 -1
  80. package/dist/src/health/grouping.js.map +1 -1
  81. package/dist/src/health/health-report-types.js.map +1 -1
  82. package/dist/src/health/health-report.js.map +1 -1
  83. package/dist/src/health/targets.js.map +1 -1
  84. package/dist/src/ingest/transcribe.js.map +1 -1
  85. package/dist/src/ingest/url-ingest.js.map +1 -1
  86. package/dist/src/license/manager.d.ts.map +1 -1
  87. package/dist/src/license/manager.js.map +1 -1
  88. package/dist/src/lsp/diagnostics-push.js.map +1 -1
  89. package/dist/src/mcp-tools/impact.d.ts.map +1 -1
  90. package/dist/src/mcp-tools/impact.js +7 -1
  91. package/dist/src/mcp-tools/impact.js.map +1 -1
  92. package/dist/src/mcp-tools/list-repos.d.ts +1 -1
  93. package/dist/src/mcp-tools/list-repos.d.ts.map +1 -1
  94. package/dist/src/mcp-tools/query.d.ts +1 -1
  95. package/dist/src/mcp-tools/query.d.ts.map +1 -1
  96. package/dist/src/parsers/c.js.map +1 -1
  97. package/dist/src/parsers/cpp.js.map +1 -1
  98. package/dist/src/parsers/csharp.js.map +1 -1
  99. package/dist/src/parsers/dart.js.map +1 -1
  100. package/dist/src/parsers/go.js.map +1 -1
  101. package/dist/src/parsers/java.js.map +1 -1
  102. package/dist/src/parsers/kotlin.js.map +1 -1
  103. package/dist/src/parsers/loader.js.map +1 -1
  104. package/dist/src/parsers/php.js.map +1 -1
  105. package/dist/src/parsers/python.js.map +1 -1
  106. package/dist/src/parsers/ruby.js.map +1 -1
  107. package/dist/src/parsers/rust.js.map +1 -1
  108. package/dist/src/parsers/swift.js.map +1 -1
  109. package/dist/src/parsers/typescript.js.map +1 -1
  110. package/dist/src/parsers/vue.js.map +1 -1
  111. package/dist/src/pipeline/effort.js.map +1 -1
  112. package/dist/src/pipeline/orchestrator.d.ts.map +1 -1
  113. package/dist/src/pipeline/orchestrator.js +48 -24
  114. package/dist/src/pipeline/orchestrator.js.map +1 -1
  115. package/dist/src/pipeline/phases/bridge-adapters/electron-ipc.d.ts +11 -0
  116. package/dist/src/pipeline/phases/bridge-adapters/electron-ipc.d.ts.map +1 -0
  117. package/dist/src/pipeline/phases/bridge-adapters/electron-ipc.js +96 -0
  118. package/dist/src/pipeline/phases/bridge-adapters/electron-ipc.js.map +1 -0
  119. package/dist/src/pipeline/phases/bridge-adapters/registry.d.ts +3 -0
  120. package/dist/src/pipeline/phases/bridge-adapters/registry.d.ts.map +1 -0
  121. package/dist/src/pipeline/phases/bridge-adapters/registry.js +13 -0
  122. package/dist/src/pipeline/phases/bridge-adapters/registry.js.map +1 -0
  123. package/dist/src/pipeline/phases/bridge-adapters/tauri.d.ts +10 -0
  124. package/dist/src/pipeline/phases/bridge-adapters/tauri.d.ts.map +1 -0
  125. package/dist/src/pipeline/phases/bridge-adapters/tauri.js +104 -0
  126. package/dist/src/pipeline/phases/bridge-adapters/tauri.js.map +1 -0
  127. package/dist/src/pipeline/phases/bridge-adapters/types.d.ts +30 -0
  128. package/dist/src/pipeline/phases/bridge-adapters/types.d.ts.map +1 -0
  129. package/dist/src/pipeline/phases/bridge-adapters/types.js +2 -0
  130. package/dist/src/pipeline/phases/bridge-adapters/types.js.map +1 -0
  131. package/dist/src/pipeline/phases/bridge-adapters/wails.d.ts +10 -0
  132. package/dist/src/pipeline/phases/bridge-adapters/wails.d.ts.map +1 -0
  133. package/dist/src/pipeline/phases/bridge-adapters/wails.js +27 -0
  134. package/dist/src/pipeline/phases/bridge-adapters/wails.js.map +1 -0
  135. package/dist/src/pipeline/phases/bridge-resolver.d.ts +23 -0
  136. package/dist/src/pipeline/phases/bridge-resolver.d.ts.map +1 -0
  137. package/dist/src/pipeline/phases/bridge-resolver.js +70 -0
  138. package/dist/src/pipeline/phases/bridge-resolver.js.map +1 -0
  139. package/dist/src/pipeline/phases/cross-file.d.ts.map +1 -1
  140. package/dist/src/pipeline/phases/cross-file.js +70 -27
  141. package/dist/src/pipeline/phases/cross-file.js.map +1 -1
  142. package/dist/src/pipeline/phases/leiden.js.map +1 -1
  143. package/dist/src/pipeline/phases/parse.d.ts.map +1 -1
  144. package/dist/src/pipeline/phases/parse.js +58 -17
  145. package/dist/src/pipeline/phases/parse.js.map +1 -1
  146. package/dist/src/pipeline/phases/scan.d.ts.map +1 -1
  147. package/dist/src/pipeline/phases/scan.js +12 -5
  148. package/dist/src/pipeline/phases/scan.js.map +1 -1
  149. package/dist/src/pipeline/phases/scope-resolution.d.ts +13 -0
  150. package/dist/src/pipeline/phases/scope-resolution.d.ts.map +1 -1
  151. package/dist/src/pipeline/phases/scope-resolution.js +18 -4
  152. package/dist/src/pipeline/phases/scope-resolution.js.map +1 -1
  153. package/dist/src/pipeline/progress.d.ts.map +1 -1
  154. package/dist/src/pipeline/runner.d.ts.map +1 -1
  155. package/dist/src/pipeline/runner.js.map +1 -1
  156. package/dist/src/pipeline/types.d.ts +2 -0
  157. package/dist/src/pipeline/types.d.ts.map +1 -1
  158. package/dist/src/pipeline/types.js +1 -0
  159. package/dist/src/pipeline/types.js.map +1 -1
  160. package/dist/src/regression/baseline.js.map +1 -1
  161. package/dist/src/regression/outcome.js.map +1 -1
  162. package/dist/src/report/badge-svg.js.map +1 -1
  163. package/dist/src/report/duplication-grouping.js.map +1 -1
  164. package/dist/src/report/human-check.js.map +1 -1
  165. package/dist/src/report/human-dupes.js.map +1 -1
  166. package/dist/src/report/human-health.js.map +1 -1
  167. package/dist/src/report/json-builders.js.map +1 -1
  168. package/dist/src/report/number-format.js.map +1 -1
  169. package/dist/src/reporting/graph-report.js.map +1 -1
  170. package/dist/src/scope-resolution/evidence-weights.d.ts.map +1 -1
  171. package/dist/src/search/hybrid-query.js.map +1 -1
  172. package/dist/src/security/safe-fetch.js.map +1 -1
  173. package/dist/src/storage/db.d.ts +13 -1
  174. package/dist/src/storage/db.d.ts.map +1 -1
  175. package/dist/src/storage/db.js +17 -1
  176. package/dist/src/storage/db.js.map +1 -1
  177. package/dist/src/storage/edge-store.d.ts +6 -0
  178. package/dist/src/storage/edge-store.d.ts.map +1 -1
  179. package/dist/src/storage/edge-store.js +8 -1
  180. package/dist/src/storage/edge-store.js.map +1 -1
  181. package/dist/src/storage/fts-store.d.ts.map +1 -1
  182. package/dist/src/storage/fts-store.js +31 -9
  183. package/dist/src/storage/fts-store.js.map +1 -1
  184. package/dist/src/storage/node-store.js.map +1 -1
  185. package/dist/src/storage/unlinked-refs.js.map +1 -1
  186. package/dist/src/types.d.ts +2 -2
  187. package/dist/src/types.d.ts.map +1 -1
  188. package/dist/src/types.js.map +1 -1
  189. package/dist/src/watch/watcher.d.ts.map +1 -1
  190. package/dist/src/watch/watcher.js +12 -2
  191. package/dist/src/watch/watcher.js.map +1 -1
  192. package/dist/src/web/analyze-api.js.map +1 -1
  193. package/dist/src/web/api.js.map +1 -1
  194. package/dist/src/wiki/providers.js.map +1 -1
  195. package/dist/tsconfig.tsbuildinfo +1 -1
  196. package/package.json +5 -2
  197. package/src/__tests__/graph/dead-code.test.ts +27 -7
  198. package/src/__tests__/pipeline/phases/bridge-adapters/electron-ipc.test.ts +87 -0
  199. package/src/__tests__/pipeline/phases/bridge-adapters/tauri.test.ts +116 -0
  200. package/src/__tests__/pipeline/phases/bridge-adapters/wails.test.ts +81 -0
  201. package/src/__tests__/pipeline/phases/bridge-resolver.test.ts +110 -0
  202. package/src/analysis/changed-files.ts +14 -2
  203. package/src/analysis/changed-workspaces.ts +13 -4
  204. package/src/analysis/churn.ts +57 -10
  205. package/src/analysis/feature-flags.ts +10 -5
  206. package/src/cache/extraction-cache.ts +59 -6
  207. package/src/cli/doctor.ts +6 -10
  208. package/src/graph/dead-code.ts +19 -16
  209. package/src/graph/pagerank.ts +10 -1
  210. package/src/groups/group-search.ts +9 -27
  211. package/src/mcp-tools/impact.ts +7 -1
  212. package/src/pipeline/orchestrator.ts +50 -26
  213. package/src/pipeline/phases/bridge-adapters/electron-ipc.ts +91 -0
  214. package/src/pipeline/phases/bridge-adapters/registry.ts +14 -0
  215. package/src/pipeline/phases/bridge-adapters/tauri.ts +103 -0
  216. package/src/pipeline/phases/bridge-adapters/types.ts +31 -0
  217. package/src/pipeline/phases/bridge-adapters/wails.ts +33 -0
  218. package/src/pipeline/phases/bridge-resolver.ts +77 -0
  219. package/src/pipeline/phases/cross-file.ts +75 -27
  220. package/src/pipeline/phases/parse.ts +60 -17
  221. package/src/pipeline/phases/scan.ts +11 -5
  222. package/src/pipeline/phases/scope-resolution.ts +19 -4
  223. package/src/pipeline/types.ts +3 -0
  224. package/src/storage/db.ts +27 -1
  225. package/src/storage/edge-store.ts +8 -1
  226. package/src/storage/fts-store.ts +31 -9
  227. package/src/watch/watcher.ts +11 -2
  228. package/tsconfig.json +2 -1
@@ -0,0 +1,103 @@
1
+ import { statSync, readFileSync } from 'fs';
2
+ import { join, extname } from 'path';
3
+ import type { PipelineContext } from '../../types.js';
4
+ import type { BridgeAdapter, BridgeEndpoint } from './types.js';
5
+
6
+ const RUST_EXT = new Set(['.rs']);
7
+ const JS_TS_EXT = new Set(['.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs']);
8
+
9
+ /** `#[tauri::command]` (with or without args) directly above an fn declaration. */
10
+ const TAURI_COMMAND_RE = /#\[tauri::command(?:\([^)]*\))?\]\s*(?:pub(?:\([^)]*\))?\s+)?(?:async\s+)?fn\s+(\w+)/g;
11
+
12
+ /**
13
+ * Tauri's `invoke(...)` call from the JS/TS side, e.g.
14
+ * `import { invoke } from '@tauri-apps/api/core'; invoke('my_command', {...})`.
15
+ * We don't verify the import — matching the call shape is enough signal here,
16
+ * same tradeoff extractor.ts already makes for plain call-site regexes.
17
+ */
18
+ const TAURI_INVOKE_RE = /\binvoke\(\s*['"]([^'"]+)['"]/g;
19
+
20
+ function safeReadSource(absPath: string, maxBytes: number): string | undefined {
21
+ try {
22
+ const stat = statSync(absPath);
23
+ if (stat.size > maxBytes) return undefined;
24
+ return readFileSync(absPath, 'utf-8');
25
+ } catch {
26
+ return undefined;
27
+ }
28
+ }
29
+
30
+ /**
31
+ * Tauri (Rust <-> JS/TS): a `#[tauri::command]`-annotated Rust fn is invoked
32
+ * from the frontend by name via `invoke("name", ...)` — a string-literal
33
+ * match, not a generated file. The Rust side already has a real Function
34
+ * node (from the Rust extractor); the JS/TS call site does not, so it's
35
+ * attached to its containing File node.
36
+ */
37
+ export const tauriAdapter: BridgeAdapter = {
38
+ name: 'tauri',
39
+
40
+ // A bare .rs file isn't a Tauri signal by itself (a repo can have unrelated
41
+ // Rust code alongside a JS/TS frontend) — require an actual
42
+ // #[tauri::command] annotation before this adapter runs at all.
43
+ detect(ctx, filePaths) {
44
+ for (const relPath of filePaths) {
45
+ if (!RUST_EXT.has(extname(relPath))) continue;
46
+ const source = safeReadSource(join(ctx.repoPath, relPath), ctx.options.maxFileSizeBytes);
47
+ if (!source) continue;
48
+ TAURI_COMMAND_RE.lastIndex = 0;
49
+ if (TAURI_COMMAND_RE.test(source)) return true;
50
+ }
51
+ return false;
52
+ },
53
+
54
+ findDefinitions(ctx, filePaths) {
55
+ const endpoints: BridgeEndpoint[] = [];
56
+ for (const relPath of filePaths) {
57
+ if (!RUST_EXT.has(extname(relPath))) continue;
58
+ const source = safeReadSource(join(ctx.repoPath, relPath), ctx.options.maxFileSizeBytes);
59
+ if (!source) continue;
60
+
61
+ TAURI_COMMAND_RE.lastIndex = 0;
62
+ let m: RegExpExecArray | null;
63
+ const names: string[] = [];
64
+ while ((m = TAURI_COMMAND_RE.exec(source)) !== null) names.push(m[1]!);
65
+ if (names.length === 0) continue;
66
+
67
+ const rows = ctx.db
68
+ .prepare(`SELECT id, name FROM nodes WHERE label = 'Function' AND language = 'rust' AND file_path = ?`)
69
+ .all(relPath) as { id: string; name: string }[];
70
+ const byName = new Map(rows.map((r) => [r.name, r.id]));
71
+ for (const name of names) {
72
+ const nodeId = byName.get(name);
73
+ if (nodeId) endpoints.push({ key: name, nodeId, language: 'rust' });
74
+ }
75
+ }
76
+ return endpoints;
77
+ },
78
+
79
+ findCallSites(ctx, filePaths) {
80
+ const endpoints: BridgeEndpoint[] = [];
81
+ for (const relPath of filePaths) {
82
+ if (!JS_TS_EXT.has(extname(relPath))) continue;
83
+ const source = safeReadSource(join(ctx.repoPath, relPath), ctx.options.maxFileSizeBytes);
84
+ if (!source) continue;
85
+
86
+ TAURI_INVOKE_RE.lastIndex = 0;
87
+ let m: RegExpExecArray | null;
88
+ const commandNames: string[] = [];
89
+ while ((m = TAURI_INVOKE_RE.exec(source)) !== null) commandNames.push(m[1]!);
90
+ if (commandNames.length === 0) continue;
91
+
92
+ const fileRow = ctx.db
93
+ .prepare(`SELECT id FROM nodes WHERE label = 'File' AND file_path = ?`)
94
+ .get(relPath) as { id: string } | undefined;
95
+ if (!fileRow) continue;
96
+
97
+ for (const name of commandNames) {
98
+ endpoints.push({ key: name, nodeId: fileRow.id, language: 'javascript' });
99
+ }
100
+ }
101
+ return endpoints;
102
+ },
103
+ };
@@ -0,0 +1,31 @@
1
+ import type { PipelineContext } from '../../types.js';
2
+
3
+ /**
4
+ * One side of a cross-language bridge boundary — a definition (the thing being
5
+ * exposed across the boundary) or a call site (the thing invoking across it).
6
+ * `key` is the string both sides are matched on: an exact symbol name for
7
+ * codegen-based bridges (Wails, wasm-bindgen), or a string-literal channel/
8
+ * command name for registration-based bridges (Tauri, Electron IPC).
9
+ */
10
+ export interface BridgeEndpoint {
11
+ key: string;
12
+ nodeId: string;
13
+ language: string;
14
+ }
15
+
16
+ /**
17
+ * A cross-language bridge adapter. Each adapter independently extracts
18
+ * (key, nodeId) pairs from both sides of one specific FFI/IPC/RPC boundary;
19
+ * the bridge-resolver phase matches them by key and emits CALLS edges.
20
+ *
21
+ * `detect` must be cheap — it runs for every adapter on every build, so an
22
+ * adapter whose framework isn't present in the repo should bail out fast
23
+ * (e.g. checking file paths already collected by the scan phase) rather than
24
+ * re-reading the filesystem.
25
+ */
26
+ export interface BridgeAdapter {
27
+ name: string;
28
+ detect(ctx: PipelineContext, filePaths: string[]): boolean;
29
+ findDefinitions(ctx: PipelineContext, filePaths: string[]): BridgeEndpoint[];
30
+ findCallSites(ctx: PipelineContext, filePaths: string[]): BridgeEndpoint[];
31
+ }
@@ -0,0 +1,33 @@
1
+ import type { PipelineContext } from '../../types.js';
2
+ import type { BridgeAdapter, BridgeEndpoint } from './types.js';
3
+
4
+ const WAILS_BINDING_RE = /wailsjs\/go\//i;
5
+
6
+ /**
7
+ * Wails (Go <-> JS/TS): `wails` generates a JS/TS binding file per bound Go
8
+ * struct at `frontend/**\/wailsjs/go/<package>/<Struct>.js`, exporting one
9
+ * function per Go method with the exact same name. Both sides are already
10
+ * parsed into real Function/Method nodes by the Go and TS/JS extractors —
11
+ * this adapter just links them by name across the language boundary.
12
+ */
13
+ export const wailsAdapter: BridgeAdapter = {
14
+ name: 'wails',
15
+
16
+ detect(_ctx, filePaths) {
17
+ return filePaths.some((p) => WAILS_BINDING_RE.test(p));
18
+ },
19
+
20
+ findDefinitions(ctx) {
21
+ const rows = ctx.db
22
+ .prepare(`SELECT id, name, language FROM nodes WHERE label = 'Method' AND language = 'go' AND file_path IS NOT NULL`)
23
+ .all() as { id: string; name: string; language: string }[];
24
+ return rows.map((r): BridgeEndpoint => ({ key: r.name, nodeId: r.id, language: r.language }));
25
+ },
26
+
27
+ findCallSites(ctx) {
28
+ const rows = ctx.db
29
+ .prepare(`SELECT id, name, language, file_path FROM nodes WHERE label = 'Function' AND file_path LIKE '%wailsjs/go/%'`)
30
+ .all() as { id: string; name: string; language: string; file_path: string }[];
31
+ return rows.map((r): BridgeEndpoint => ({ key: r.name, nodeId: r.id, language: r.language ?? 'javascript' }));
32
+ },
33
+ };
@@ -0,0 +1,77 @@
1
+ import type { PipelinePhase } from '../types.js';
2
+ import type { MonographEdge } from '../../types.js';
3
+ import { makeId, CONFIDENCE_SCORE } from '../../types.js';
4
+ import { insertEdges } from '../../storage/edge-store.js';
5
+ import type { ScanOutput } from './scan.js';
6
+ import { BUILTIN_BRIDGE_ADAPTERS } from './bridge-adapters/registry.js';
7
+ import type { BridgeEndpoint } from './bridge-adapters/types.js';
8
+
9
+ export interface BridgeResolverOutput {
10
+ edgesCreated: number;
11
+ }
12
+
13
+ /**
14
+ * Cross-language bridge resolution — links calls across an FFI/IPC boundary
15
+ * that no single language's own import/call resolution can see (Wails'
16
+ * generated Go<->JS bindings, Tauri's invoke() commands, Electron's IPC
17
+ * channels, ...). Each adapter in bridge-adapters/ extracts (key, nodeId)
18
+ * pairs from both sides; here they're matched by key and turned into CALLS
19
+ * edges — reusing the existing relation so every existing consumer
20
+ * (monograph_impact, monograph_neighbors, community detection) picks them up
21
+ * with no changes on their end.
22
+ *
23
+ * Confidence is always INFERRED: a bridge edge is a name/key match, not an
24
+ * AST-verified call. A key with more than one matching definition is
25
+ * dropped rather than guessed at — the call site can't know which one was
26
+ * meant, and a wrong edge is worse than a missing one. Multiple call sites
27
+ * sharing one definition (the common case: several UI files calling the
28
+ * same bound method) is normal and always kept.
29
+ */
30
+ export const bridgeResolverPhase: PipelinePhase<BridgeResolverOutput> = {
31
+ name: 'bridge-resolver',
32
+ deps: ['scan', 'structure', 'parse'],
33
+ async execute(ctx, deps) {
34
+ if (ctx.allFilesCached) return { edgesCreated: 0 };
35
+
36
+ const { filePaths } = deps.get('scan') as ScanOutput;
37
+ const edges: MonographEdge[] = [];
38
+
39
+ for (const adapter of BUILTIN_BRIDGE_ADAPTERS) {
40
+ if (!adapter.detect(ctx, filePaths)) continue;
41
+
42
+ const definitions = adapter.findDefinitions(ctx, filePaths);
43
+ const callSites = adapter.findCallSites(ctx, filePaths);
44
+ if (definitions.length === 0 || callSites.length === 0) continue;
45
+
46
+ const definitionsByKey = new Map<string, BridgeEndpoint[]>();
47
+ for (const def of definitions) {
48
+ const arr = definitionsByKey.get(def.key);
49
+ if (arr) arr.push(def);
50
+ else definitionsByKey.set(def.key, [def]);
51
+ }
52
+
53
+ for (const callSite of callSites) {
54
+ const matches = definitionsByKey.get(callSite.key);
55
+ if (!matches || matches.length !== 1) continue; // 0 or ambiguous — drop, don't guess
56
+ const target = matches[0]!;
57
+ if (target.nodeId === callSite.nodeId) continue; // same node on both sides — nothing to link
58
+
59
+ const id = makeId('bridge', adapter.name, callSite.nodeId, target.nodeId);
60
+ const languagePair = `${callSite.language} -> ${target.language}`;
61
+ edges.push({
62
+ id,
63
+ sourceId: callSite.nodeId,
64
+ targetId: target.nodeId,
65
+ relation: 'CALLS',
66
+ confidence: 'INFERRED',
67
+ confidenceScore: CONFIDENCE_SCORE.INFERRED,
68
+ reason: `${adapter.name} bridge (${languagePair}): "${callSite.key}"`,
69
+ evidence: [{ kind: 'bridge', weight: CONFIDENCE_SCORE.INFERRED, note: `${adapter.name}: ${languagePair}` }],
70
+ });
71
+ }
72
+ }
73
+
74
+ if (edges.length > 0) insertEdges(ctx.db, edges);
75
+ return { edgesCreated: edges.length };
76
+ },
77
+ };
@@ -1,53 +1,101 @@
1
+ import { readFileSync } from 'fs';
2
+ import { join } from 'path';
1
3
  import type { PipelinePhase } from '../types.js';
2
4
  import type { MonographEdge } from '../../types.js';
3
5
  import { makeId, CONFIDENCE_SCORE } from '../../types.js';
4
6
  import { insertEdges } from '../../storage/edge-store.js';
5
7
  import type { ParseOutput } from './parse.js';
6
8
  import type { StructureOutput } from './structure.js';
9
+ import { resolveModuleSpecifier, buildWorkspacePackageMap } from './scope-resolution.js';
7
10
 
8
11
  export interface CrossFileOutput {
9
12
  resolvedEdges: MonographEdge[];
10
13
  }
11
14
 
15
+ // Mirrors parsers/extractor.ts's re-export detection regex — used here to recover
16
+ // the ORIGINAL specifier string behind a RE_EXPORTS edge's mangled placeholder id
17
+ // (makeId('import', specifier) is lossy: '/' and '.' are collapsed to '_', so the
18
+ // placeholder can't be reversed directly — we re-derive it from source instead).
19
+ const RE_EXPORT_RE = /export\s+(?:\*|\{[^}]*\})\s+from\s+['"]([^'"]+)['"]/g;
20
+
12
21
  // monolean: IMPORTS name-matching removed — it matched import names against a
13
22
  // global symbol index (e.g. `import fs` → random Variable named `fs` in wrong file).
14
23
  // scope-resolution.ts now handles cross-file CALLS via source-parsed import maps.
15
24
  export const crossFilePhase: PipelinePhase<CrossFileOutput> = {
16
25
  name: 'cross-file',
17
26
  deps: ['parse', 'structure'],
18
- async execute(_ctx, deps) {
27
+ async execute(ctx, deps) {
19
28
  const { allEdges } = deps.get('parse') as ParseOutput;
20
29
  const { fileNodes } = deps.get('structure') as StructureOutput;
21
30
 
22
- function lastSegment(s: string): string {
23
- const idx = s.lastIndexOf('/');
24
- return idx === -1 ? s : s.slice(idx + 1);
25
- }
31
+ const reExportEdges = allEdges.filter((e) => e.relation === 'RE_EXPORTS');
32
+ const resolvedEdges: MonographEdge[] = [];
26
33
 
27
- function targetBasename(rawId: string): string {
28
- const stripped = rawId.startsWith('import_') ? rawId.slice(7) : rawId;
29
- return lastSegment(stripped);
30
- }
34
+ if (reExportEdges.length > 0) {
35
+ // fileId <-> filePath maps for resolving edge endpoints to real file nodes.
36
+ const fileIdToPath = new Map<string, string>();
37
+ const knownFiles = new Set<string>();
38
+ for (const fileNode of fileNodes) {
39
+ if (fileNode.filePath) {
40
+ fileIdToPath.set(fileNode.id, fileNode.filePath);
41
+ knownFiles.add(fileNode.filePath);
42
+ }
43
+ }
44
+ const filePathToId = new Map<string, string>();
45
+ for (const [id, path] of fileIdToPath) filePathToId.set(path, id);
31
46
 
32
- const fileBasenameIndex = new Map<string, string>();
33
- for (const fileNode of fileNodes) {
34
- const basename = lastSegment(fileNode.filePath ?? '').toLowerCase();
35
- const dotIdx = basename.lastIndexOf('.');
36
- const noExt = dotIdx === -1 ? basename : basename.slice(0, dotIdx);
37
- fileBasenameIndex.set(basename, fileNode.id);
38
- if (noExt !== basename) fileBasenameIndex.set(noExt, fileNode.id);
39
- }
47
+ const workspaceMap = buildWorkspacePackageMap(ctx.repoPath);
40
48
 
41
- const resolvedEdges: MonographEdge[] = [];
49
+ // Group by importing file so each source file is read at most once.
50
+ const edgesBySource = new Map<string, MonographEdge[]>();
51
+ for (const edge of reExportEdges) {
52
+ const arr = edgesBySource.get(edge.sourceId);
53
+ if (arr) arr.push(edge);
54
+ else edgesBySource.set(edge.sourceId, [edge]);
55
+ }
56
+
57
+ for (const [sourceId, edges] of edgesBySource) {
58
+ const filePath = fileIdToPath.get(sourceId);
59
+ if (!filePath) continue;
60
+
61
+ let source: string;
62
+ try {
63
+ source = readFileSync(join(ctx.repoPath, filePath), 'utf-8');
64
+ } catch {
65
+ continue;
66
+ }
67
+
68
+ // Recover each `export * / { ... } from '<specifier>'` string, keyed by the
69
+ // same mangled placeholder id the extractor used (makeId('import', specifier))
70
+ // so it can be matched back to the corresponding edge.
71
+ const specifierByPlaceholder = new Map<string, string>();
72
+ RE_EXPORT_RE.lastIndex = 0;
73
+ let m: RegExpExecArray | null;
74
+ while ((m = RE_EXPORT_RE.exec(source)) !== null) {
75
+ const specifier = m[1]!;
76
+ specifierByPlaceholder.set(makeId('import', specifier), specifier);
77
+ }
78
+
79
+ for (const edge of edges) {
80
+ const specifier = specifierByPlaceholder.get(edge.targetId);
81
+ if (!specifier) continue;
82
+
83
+ // Resolve relative to THIS importing file's own directory (reusing the
84
+ // same resolver scope-resolution.ts uses for IMPORTS) instead of an
85
+ // ambiguous global basename lookup that collides on repeated basenames
86
+ // like index.ts / utils.ts across the repo.
87
+ const resolvedPath = resolveModuleSpecifier(
88
+ filePath,
89
+ specifier,
90
+ ctx.repoPath,
91
+ knownFiles,
92
+ workspaceMap,
93
+ );
94
+ if (!resolvedPath) continue;
42
95
 
43
- for (const edge of allEdges) {
44
- if (edge.relation === 'RE_EXPORTS') {
45
- const basename = targetBasename(edge.targetId).toLowerCase();
46
- const dotIdx = basename.lastIndexOf('.');
47
- const noExt = dotIdx === -1 ? basename : basename.slice(0, dotIdx);
48
- const resolvedId = fileBasenameIndex.get(basename) ?? fileBasenameIndex.get(noExt);
96
+ const resolvedId = filePathToId.get(resolvedPath);
97
+ if (!resolvedId || resolvedId === edge.targetId) continue;
49
98
 
50
- if (resolvedId && resolvedId !== edge.targetId) {
51
99
  resolvedEdges.push({
52
100
  ...edge,
53
101
  id: makeId(edge.sourceId, resolvedId, 'reexports_resolved'),
@@ -59,8 +107,8 @@ export const crossFilePhase: PipelinePhase<CrossFileOutput> = {
59
107
  }
60
108
  }
61
109
 
62
- if (_ctx.db && resolvedEdges.length > 0) {
63
- insertEdges(_ctx.db, resolvedEdges);
110
+ if (ctx.db && resolvedEdges.length > 0) {
111
+ insertEdges(ctx.db, resolvedEdges);
64
112
  }
65
113
 
66
114
  return { resolvedEdges };
@@ -3,8 +3,8 @@ import { extname, join } from 'path';
3
3
  import type { PipelinePhase, PipelineContext } from '../types.js';
4
4
  import type { MonographNode, MonographEdge } from '../../types.js';
5
5
  import { parseFile } from '../../parsers/loader.js';
6
- import { insertNodes } from '../../storage/node-store.js';
7
- import { insertEdges } from '../../storage/edge-store.js';
6
+ import { insertNodes, deleteNodesForFile } from '../../storage/node-store.js';
7
+ import { insertEdges, deleteEdgesForFile } from '../../storage/edge-store.js';
8
8
  import type { StructureOutput } from './structure.js';
9
9
  import { extractVariables, variableToNode } from './variables.js';
10
10
  import { ExtractionCache } from '../../cache/extraction-cache.js';
@@ -24,8 +24,18 @@ export const parsePhase: PipelinePhase<ParseOutput> = {
24
24
  const { fileNodes } = deps.get('structure') as StructureOutput;
25
25
  const symbolNodes: MonographNode[] = [];
26
26
  const allEdges: MonographEdge[] = [];
27
- const freshNodes: MonographNode[] = [];
28
- const freshEdges: MonographEdge[] = [];
27
+ // freshNodes/freshEdges (the cache-miss-only subset) are NOT accumulated in a
28
+ // parallel array during the loop — that duplicated every fresh node/edge object
29
+ // in memory for the entire pipeline run. Instead we record the [start, end)
30
+ // slice of symbolNodes/allEdges that each cache-miss file contributed, and
31
+ // materialize the fresh subset via a single slice pass at the end, only when
32
+ // it's actually needed (cacheHits > 0). On a cold build (cacheHits === 0) the
33
+ // fresh subset is never materialized at all, since nodesToInsert falls back to
34
+ // symbolNodes/allEdges directly in that case — eliminating the redundant
35
+ // allocation entirely for the memory-heaviest scenario.
36
+ const freshNodeRanges: Array<[number, number]> = [];
37
+ const freshEdgeRanges: Array<[number, number]> = [];
38
+ const staleFilePaths: string[] = [];
29
39
  const parseErrors: string[] = [];
30
40
  const fileContents = new Map<string, string>();
31
41
  let processed = 0;
@@ -39,8 +49,10 @@ export const parsePhase: PipelinePhase<ParseOutput> = {
39
49
  const ext = extname(absPath).toLowerCase();
40
50
  if (ext === '.md' || ext === '.markdown') { processed++; continue; }
41
51
 
42
- // Fast path: mtime+size check avoids reading file content entirely
43
- const cached = cache.getWithStat(absPath);
52
+ // Fast path: mtime+size check avoids reading file content entirely.
53
+ // Skipped entirely under --force so a force rebuild is a genuine from-scratch
54
+ // parse, not a replay of whatever extraction the cache happens to hold.
55
+ const cached = ctx.options.force ? null : cache.getWithStat(absPath);
44
56
  if (cached) {
45
57
  symbolNodes.push(...cached.nodes);
46
58
  allEdges.push(...cached.edges);
@@ -54,7 +66,12 @@ export const parsePhase: PipelinePhase<ParseOutput> = {
54
66
  continue;
55
67
  }
56
68
  source = readFileSync(absPath, 'utf-8');
57
- } catch { continue; }
69
+ } catch (err) {
70
+ const code = (err as NodeJS.ErrnoException)?.code;
71
+ const detail = code || (err instanceof Error ? err.message : String(err));
72
+ parseErrors.push(`${fileNode.filePath}: unreadable (${detail})`);
73
+ continue;
74
+ }
58
75
  fileContents.set(fileNode.filePath ?? absPath, source);
59
76
  const result = await parseFile(absPath, source, fileNode.filePath ?? '');
60
77
  const fileSymbols: MonographNode[] = [...result.nodes];
@@ -98,10 +115,16 @@ export const parsePhase: PipelinePhase<ParseOutput> = {
98
115
  const fileHash = cache.hashContent(source);
99
116
  cache.setDeferred(absPath, fileHash, fileSymbols, fileEdges);
100
117
  } catch { /* non-fatal */ }
118
+ const nodeStart = symbolNodes.length;
119
+ const edgeStart = allEdges.length;
101
120
  symbolNodes.push(...fileSymbols);
102
121
  allEdges.push(...fileEdges);
103
- freshNodes.push(...fileSymbols);
104
- freshEdges.push(...fileEdges);
122
+ freshNodeRanges.push([nodeStart, symbolNodes.length]);
123
+ freshEdgeRanges.push([edgeStart, allEdges.length]);
124
+ // This file was re-parsed (cache miss) — its OLD node/edge set (from the
125
+ // previous build) must be deleted before the fresh rows are inserted below,
126
+ // otherwise a renamed/removed symbol's old row survives forever (ghost rows).
127
+ if (fileNode.filePath) staleFilePaths.push(fileNode.filePath);
105
128
  cacheMisses++;
106
129
  }
107
130
  processed++;
@@ -114,16 +137,35 @@ export const parsePhase: PipelinePhase<ParseOutput> = {
114
137
  cache.flush();
115
138
 
116
139
  if (ctx.db) {
140
+ const db = ctx.db;
117
141
  // Only insert freshly-parsed nodes — cached nodes are already in the DB
118
142
  // from the previous build (node IDs are deterministic from file_path + symbol).
119
- // On first build (no cache hits), freshNodes === symbolNodes.
120
- const nodesToInsert = cacheHits > 0 ? freshNodes : symbolNodes;
121
- insertNodes(ctx.db, nodesToInsert);
143
+ // On first build (no cache hits), freshNodes === symbolNodes, so the fresh
144
+ // subset is never materialized symbolNodes/allEdges are used directly.
145
+ const freshNodes = cacheHits > 0
146
+ ? freshNodeRanges.flatMap(([s, e]) => symbolNodes.slice(s, e))
147
+ : symbolNodes;
148
+ const freshEdges = cacheHits > 0
149
+ ? freshEdgeRanges.flatMap(([s, e]) => allEdges.slice(s, e))
150
+ : allEdges;
151
+ const nodesToInsert = freshNodes;
122
152
  const knownIds = new Set(symbolNodes.map(n => n.id));
123
- const edgesToInsert = cacheHits > 0
124
- ? freshEdges.filter(e => knownIds.has(e.targetId))
125
- : allEdges.filter(e => knownIds.has(e.targetId));
126
- insertEdges(ctx.db, edgesToInsert);
153
+ const edgesToInsert = freshEdges.filter(e => knownIds.has(e.targetId));
154
+
155
+ // For every cache-miss file, purge its OLD node/edge set BEFORE inserting the
156
+ // fresh parse results, inside the same transaction — otherwise a renamed or
157
+ // removed symbol's old row would survive forever (INSERT OR REPLACE only
158
+ // overwrites rows whose id still matches; it never deletes rows whose id
159
+ // disappeared because the symbol was renamed).
160
+ const writeAll = db.transaction(() => {
161
+ for (const filePath of staleFilePaths) {
162
+ deleteEdgesForFile(db, filePath);
163
+ deleteNodesForFile(db, filePath);
164
+ }
165
+ insertNodes(db, nodesToInsert);
166
+ insertEdges(db, edgesToInsert);
167
+ });
168
+ writeAll();
127
169
  }
128
170
 
129
171
  if (cacheHits > 0 && cacheMisses === 0) {
@@ -131,8 +173,9 @@ export const parsePhase: PipelinePhase<ParseOutput> = {
131
173
  }
132
174
 
133
175
  if (cacheHits > 0) {
176
+ const freshNodeCount = freshNodeRanges.reduce((sum, [s, e]) => sum + (e - s), 0);
134
177
  ctx.onProgress?.({ phase: 'parse', filesProcessed: processed, totalFiles: fileNodes.length,
135
- message: `cache: ${cacheHits} hits, ${cacheMisses} misses (${freshNodes.length} nodes inserted)` } as any);
178
+ message: `cache: ${cacheHits} hits, ${cacheMisses} misses (${freshNodeCount} nodes inserted)` } as any);
136
179
  }
137
180
 
138
181
  return { symbolNodes, allEdges, parseErrors, fileContents, cacheStats: { hits: cacheHits, misses: cacheMisses } };
@@ -1,4 +1,4 @@
1
- import { readdirSync, statSync, existsSync, readFileSync } from 'fs';
1
+ import { readdirSync, statSync, lstatSync, existsSync, readFileSync } from 'fs';
2
2
  import { join, extname } from 'path';
3
3
  import micromatch from 'micromatch';
4
4
  import type { PipelinePhase, PipelineContext } from '../types.js';
@@ -50,16 +50,22 @@ export const scanPhase: PipelinePhase<ScanOutput> = {
50
50
  }
51
51
 
52
52
  function walk(dir: string) {
53
- let entries: string[];
54
- try { entries = readdirSync(dir); } catch { return; }
53
+ let dirents: import('fs').Dirent[];
54
+ try { dirents = readdirSync(dir, { withFileTypes: true }); } catch { return; }
55
55
 
56
- for (const entry of entries) {
56
+ for (const dirent of dirents) {
57
+ const entry = dirent.name;
57
58
  if (ignoreDirs.has(entry)) continue;
58
59
  // Skip macOS AppleDouble resource fork files (._*) — common on ExFAT/network volumes
59
60
  if (entry.startsWith('._')) continue;
61
+ // Never follow symlinks — a symlink to an ancestor directory causes infinite
62
+ // recursion (stack overflow), and a symlink to a large external tree would
63
+ // get fully indexed as if it were part of the repo. Matches how most
64
+ // code-indexing tools behave by default.
65
+ if (dirent.isSymbolicLink()) continue;
60
66
  const fullPath = join(dir, entry);
61
67
  let stat: ReturnType<typeof statSync>;
62
- try { stat = statSync(fullPath); } catch { continue; }
68
+ try { stat = lstatSync(fullPath); } catch { continue; }
63
69
 
64
70
  if (stat.isDirectory()) {
65
71
  // Always traverse directories — negation patterns may rescue files inside ignored dirs
@@ -286,11 +286,25 @@ const GO_RESOLVE_EXTS = ['.go'];
286
286
  const JAVA_RESOLVE_EXTS = ['.java'];
287
287
  const RUST_RESOLVE_EXTS = ['.rs'];
288
288
 
289
+ const workspacePackageMapCache = new Map<string, Map<string, string>>();
290
+
291
+ /** Invalidate the cached workspace package map for a repo — call at the start
292
+ * of each build so long-lived processes (watch mode) pick up package.json
293
+ * additions/removals instead of serving a stale map from a prior build. */
294
+ export function clearWorkspacePackageMapCache(repoPath: string): void {
295
+ workspacePackageMapCache.delete(repoPath);
296
+ }
297
+
289
298
  /**
290
299
  * Build package-name → directory map from workspace package.json files.
291
300
  * Scans packages/ for package.json and maps npm name to its relative src path.
301
+ * Cached per repoPath — multiple pipeline phases (cross-file, scope-resolution,
302
+ * the latter's own re-export loop) call this per-file/per-edge within the same
303
+ * build, and the workspace's package.json set doesn't change mid-build.
292
304
  */
293
- function buildWorkspacePackageMap(repoPath: string): Map<string, string> {
305
+ export function buildWorkspacePackageMap(repoPath: string): Map<string, string> {
306
+ const cached = workspacePackageMapCache.get(repoPath);
307
+ if (cached) return cached;
294
308
  const result = new Map<string, string>();
295
309
  const packagesDir = join(repoPath, 'packages');
296
310
  try {
@@ -315,10 +329,11 @@ function buildWorkspacePackageMap(repoPath: string): Map<string, string> {
315
329
  };
316
330
  scanDirs(packagesDir, 0);
317
331
  } catch { /* no packages dir */ }
332
+ workspacePackageMapCache.set(repoPath, result);
318
333
  return result;
319
334
  }
320
335
 
321
- function resolveModuleSpecifier(
336
+ export function resolveModuleSpecifier(
322
337
  importerPath: string,
323
338
  specifier: string,
324
339
  repoPath: string,
@@ -346,8 +361,8 @@ function resolveModuleSpecifier(
346
361
  return null;
347
362
  }
348
363
 
349
- // Workspace package specifier: @monomind/hooks → packages/@monomind/hooks/src/index.ts
350
- // Also handles subpath: @monomind/hooks/src/foo → packages/@monomind/hooks/src/foo.ts
364
+ // Workspace package specifier: @monoes/hooks → packages/@monomind/hooks/src/index.ts
365
+ // Also handles subpath: @monoes/hooks/src/foo → packages/@monomind/hooks/src/foo.ts
351
366
  for (const [pkgName, pkgDir] of workspaceMap) {
352
367
  if (specifier === pkgName) {
353
368
  // Bare import: resolve to package entry point (src/index.ts)
@@ -20,6 +20,8 @@ export interface PipelineOptions {
20
20
  ignore: string[];
21
21
  /** Max Section nodes to submit to LLM extraction (0 = disabled). Default 0. */
22
22
  llmMaxSections: number;
23
+ /** When true, bypass the on-disk extraction cache entirely and re-parse every file from scratch. Default false. */
24
+ force?: boolean;
23
25
  }
24
26
 
25
27
  export const DEFAULT_OPTIONS: PipelineOptions = {
@@ -29,6 +31,7 @@ export const DEFAULT_OPTIONS: PipelineOptions = {
29
31
  workerChunkBudgetBytes: 20971520,
30
32
  ignore: [],
31
33
  llmMaxSections: 0,
34
+ force: false,
32
35
  };
33
36
 
34
37
  export interface PipelinePhase<TOutput> {
package/src/storage/db.ts CHANGED
@@ -13,7 +13,23 @@ import { MonographError } from '../types.js';
13
13
 
14
14
  export type MonographDb = Database.Database;
15
15
 
16
- export function openDb(dbPath: string): MonographDb {
16
+ export interface OpenDbOptions {
17
+ /**
18
+ * When true, throw a clear error instead of silently creating a fresh,
19
+ * empty-but-migrated database if `dbPath` doesn't exist yet. Defaults to
20
+ * false to preserve legacy build-time behavior (the `monograph build`
21
+ * codepath legitimately creates new DBs). Read-only analysis tools should
22
+ * pass `true` so pointing them at a repo whose real index lives elsewhere
23
+ * (e.g. running from a subdirectory) fails loudly instead of reporting
24
+ * fake-successful-looking empty results.
25
+ */
26
+ fileMustExist?: boolean;
27
+ }
28
+
29
+ export function openDb(dbPath: string, options: OpenDbOptions = {}): MonographDb {
30
+ if (options.fileMustExist && !existsSync(dbPath)) {
31
+ throw new MonographError(`Monograph database does not exist at ${dbPath}. Run monograph build first.`);
32
+ }
17
33
  try {
18
34
  mkdirSync(dirname(dbPath), { recursive: true });
19
35
  const db = new Database(dbPath);
@@ -74,6 +90,16 @@ export function atomicRebuild(dbPath: string, buildFn: (db: MonographDb) => void
74
90
  buildFn(db);
75
91
  db.close();
76
92
  renameSync(tmpPath, dbPath);
93
+ // The old db's WAL-mode sidecar files belong to the file we just replaced —
94
+ // if left behind, the next process to open dbPath pairs the NEW db file with
95
+ // a STALE -wal/-shm whose salt/checkpoint state doesn't match, which can
96
+ // surface as "file is not a database" or subtle corruption.
97
+ for (const suffix of ['-wal', '-shm']) {
98
+ const sidecar = dbPath + suffix;
99
+ if (existsSync(sidecar)) {
100
+ try { unlinkSync(sidecar); } catch { /* best-effort cleanup */ }
101
+ }
102
+ }
77
103
  } catch (err) {
78
104
  db.close();
79
105
  if (existsSync(tmpPath)) {