@event4u/agent-config 14.20.0 → 14.22.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 (242) hide show
  1. package/.augment-plugin/marketplace.json +3 -3
  2. package/.augment-plugin/plugin.json +1 -1
  3. package/.claude-plugin/marketplace.json +1 -1
  4. package/CHANGELOG.md +441 -0
  5. package/CREDITS.md +37 -0
  6. package/README.md +1 -1
  7. package/agents/templates/.ai-council.yml.example +10 -0
  8. package/dist/agent-src/commands/analyze/inbox.md +381 -6
  9. package/dist/agent-src/commands/analyze/{reference-repo.md → repo.md} +251 -34
  10. package/dist/agent-src/commands/analyze/roadmap-repos.md +129 -0
  11. package/dist/agent-src/commands/analyze.md +12 -7
  12. package/dist/agent-src/commands/feature/plan.md +55 -16
  13. package/dist/agent-src/commands/feature/refactor.md +8 -3
  14. package/dist/agent-src/commands/humanize.md +48 -4
  15. package/dist/agent-src/contexts/communication/rules-auto/token-efficiency-mechanics.md +69 -0
  16. package/dist/agent-src/contexts/execution/auto-dispatch-classification.md +5 -5
  17. package/dist/agent-src/contexts/execution/contract-decision-sheet.md +22 -1
  18. package/dist/agent-src/contexts/execution/host-capability-manifest.md +4 -1
  19. package/dist/agent-src/contexts/execution/roadmap-process-loop.md +35 -2
  20. package/dist/agent-src/contexts/execution/terminal-states.md +4 -1
  21. package/dist/agent-src/rules/evaluator-independence.md +4 -4
  22. package/dist/agent-src/rules/external-code-graph-interop.md +19 -14
  23. package/dist/agent-src/rules/external-reference-deep-dive.md +2 -2
  24. package/dist/agent-src/rules/token-efficiency.md +26 -3
  25. package/dist/agent-src/scripts/_cli_entry.ts +51 -0
  26. package/dist/agent-src/scripts/archive_completed_roadmaps.ts +14 -23
  27. package/dist/agent-src/scripts/roadmap_gates.ts +4 -20
  28. package/dist/agent-src/scripts/roadmap_set_step.ts +5 -2
  29. package/dist/agent-src/scripts/stubs_due.ts +4 -18
  30. package/dist/agent-src/scripts/update_roadmap_progress.ts +7 -24
  31. package/dist/agent-src/skills/code-intelligence/SKILL.md +25 -0
  32. package/dist/agent-src/skills/competitive-positioning/SKILL.md +2 -2
  33. package/dist/agent-src/skills/humanizer/references/anti-aiisms.md +27 -11
  34. package/dist/agent-src/skills/learning-to-rule-or-skill/SKILL.md +1 -1
  35. package/dist/agent-src/skills/markitdown/SKILL.md +1 -1
  36. package/dist/agent-src/skills/repomix-packer/SKILL.md +1 -1
  37. package/dist/agent-src/skills/roadmap-writing/SKILL.md +17 -0
  38. package/dist/agent-src/skills/technical-specification/SKILL.md +4 -0
  39. package/dist/agent-src/templates/agent-settings.md +0 -1
  40. package/dist/agent-src/templates/agents/agent-project-settings.example.yml +1 -1
  41. package/dist/agent-src/templates/features.md +5 -0
  42. package/dist/agent-src/templates/roadmaps.md +15 -0
  43. package/dist/cli-delegate/_cli_entry.js +8 -0
  44. package/dist/cli-delegate/_lib/pin_resolver.js +170 -0
  45. package/dist/cli-delegate/archival_sweep.js +8 -0
  46. package/dist/cli-delegate/archive_completed_roadmaps.js +494 -0
  47. package/dist/cli-delegate/check_update_banner.js +923 -0
  48. package/dist/cli-delegate/{chunk-KPBWFOPZ.js → chunk-2FTMENJL.js} +194 -37
  49. package/dist/cli-delegate/{chunk-PI2GU3DP.js → chunk-2TAQ2K4V.js} +4 -1
  50. package/dist/cli-delegate/chunk-5EK5WQN6.js +28 -0
  51. package/dist/cli-delegate/chunk-7XGD2OEU.js +96 -0
  52. package/dist/cli-delegate/{chunk-C6IPZRDA.js → chunk-BIXEDQPZ.js} +7 -1
  53. package/dist/cli-delegate/chunk-DBH4AHDK.js +155 -0
  54. package/dist/cli-delegate/{chunk-NUMQHLS5.js → chunk-J3RFNKG4.js} +21 -2
  55. package/dist/cli-delegate/chunk-JO25G5H7.js +20 -0
  56. package/dist/cli-delegate/chunk-LTYVIY76.js +7961 -0
  57. package/dist/cli-delegate/chunk-QAJS5YWY.js +999 -0
  58. package/dist/cli-delegate/{chunk-ZVBOHSVF.js → chunk-TGIPZBCW.js} +1 -1
  59. package/dist/cli-delegate/{chunk-YS6XTLEK.js → chunk-TYSY33MD.js} +56 -6
  60. package/dist/cli-delegate/chunk-ULET4BYS.js +328 -0
  61. package/dist/cli-delegate/chunk-YDQSHTD7.js +187 -0
  62. package/dist/cli-delegate/chunk-YWVXLR44.js +48 -0
  63. package/dist/cli-delegate/chunk-ZZQBUHFK.js +273 -0
  64. package/dist/cli-delegate/cmd_conformance.js +19 -5
  65. package/dist/cli-delegate/cmd_doctor.js +3 -3
  66. package/dist/cli-delegate/cmd_export.js +2 -2
  67. package/dist/cli-delegate/cmd_handoff.js +27 -3
  68. package/dist/cli-delegate/cmd_prune.js +2 -2
  69. package/dist/cli-delegate/cmd_session_recycle.js +2 -2
  70. package/dist/cli-delegate/cmd_settings_migrate.js +2 -2
  71. package/dist/cli-delegate/cmd_sync.js +2 -2
  72. package/dist/cli-delegate/cmd_uninstall.js +2 -2
  73. package/dist/cli-delegate/cmd_validate.js +2 -2
  74. package/dist/cli-delegate/dashboard_mode.js +16 -0
  75. package/dist/cli-delegate/gate_budget.js +24 -0
  76. package/dist/cli-delegate/gate_execute.js +25 -0
  77. package/dist/cli-delegate/guarded_baseline.js +18 -0
  78. package/dist/cli-delegate/resume_probe.js +22 -0
  79. package/dist/cli-delegate/roadmap_gates.js +638 -0
  80. package/dist/cli-delegate/roadmap_set_step.js +173 -0
  81. package/dist/cli-delegate/stub_queue.js +47 -0
  82. package/dist/cli-delegate/stubs_due.js +38 -0
  83. package/dist/cli-delegate/ticket_bundles.js +59 -0
  84. package/dist/cli-delegate/update_roadmap_progress.js +60 -0
  85. package/dist/discovery/deprecation-report.md +1 -1
  86. package/dist/discovery/discovery-manifest.json +60 -29
  87. package/dist/discovery/discovery-manifest.json.sha256 +1 -1
  88. package/dist/discovery/discovery-manifest.summary.md +3 -3
  89. package/dist/discovery/dormancy-report.md +1 -1
  90. package/dist/discovery/orphan-report.md +1 -1
  91. package/dist/discovery/packs.json +7 -6
  92. package/dist/discovery/trust-report.md +2 -2
  93. package/dist/discovery/workspaces.json +6 -5
  94. package/dist/hooks/dispatch.js +1522 -1008
  95. package/dist/install/claudeLayerCarriage.js +237 -0
  96. package/dist/install/claudePathsPlan.js +79 -0
  97. package/dist/install/hostLayerFingerprint.js +1 -1
  98. package/dist/install/install.mjs +10 -1
  99. package/dist/install/partitionEligibility.js +231 -117
  100. package/dist/install/ruleLayerPartition.js +10 -7
  101. package/dist/mcp/registry-manifest.json +3 -3
  102. package/dist/mcp/server.json +2 -2
  103. package/dist/mcp/server.mjs +39376 -38902
  104. package/dist/scripts/ai_council/content_ceiling.js +63 -0
  105. package/dist/scripts/ai_council/seat_policy.js +106 -0
  106. package/dist/scripts/ai_council/transport_resolver.js +21 -0
  107. package/dist/scripts/condense.js +32 -25
  108. package/docs/MIGRATION.md +49 -1
  109. package/docs/contracts/settings-classes.md +1 -1
  110. package/docs/guidelines/agent-infra/minimal-safe-diff-mechanics.md +73 -0
  111. package/llms.txt +14 -14
  112. package/package.json +7 -4
  113. package/src/agent-src/scripts/_cli_entry.ts +51 -0
  114. package/src/agent-src/scripts/archive_completed_roadmaps.ts +14 -23
  115. package/src/agent-src/scripts/roadmap_gates.ts +4 -20
  116. package/src/agent-src/scripts/roadmap_set_step.ts +5 -2
  117. package/src/agent-src/scripts/stubs_due.ts +4 -18
  118. package/src/agent-src/scripts/update_roadmap_progress.ts +7 -24
  119. package/src/config/continuity-surface.json +509 -0
  120. package/src/config/estate-count-budget.json +1 -1
  121. package/src/config/gate-coverage.yml +29 -8
  122. package/src/config/gate-violation-baselines.json +8 -8
  123. package/src/config/gitignore-block.txt +19 -0
  124. package/src/config/host-injection-effect.json +73 -0
  125. package/src/config/packed-binary-manifest.json +33 -0
  126. package/src/config/preamble-payload-budget.json +112 -105
  127. package/src/config/publish-surface.json +4 -2
  128. package/src/config/release-gate-locality.yml +4 -3
  129. package/src/install/claudeLayerCarriage.ts +273 -0
  130. package/src/install/claudePathsPlan.ts +76 -0
  131. package/src/install/hostLayerFingerprint.ts +1 -1
  132. package/src/install/partitionEligibility.ts +261 -127
  133. package/src/install/ruleLayerPartition.ts +19 -23
  134. package/src/scripts/_dispatch.bash +77 -27
  135. package/src/scripts/_lib/auto_dispatch.ts +2 -2
  136. package/src/scripts/_lib/blocked_by_marker.ts +189 -0
  137. package/src/scripts/_lib/catalogue_layer_parity.ts +146 -0
  138. package/src/scripts/_lib/continuity_surface.ts +296 -0
  139. package/src/scripts/_lib/corpus_manifest.ts +26 -9
  140. package/src/scripts/_lib/hook_settings.ts +1 -1
  141. package/src/scripts/_lib/host_capability.ts +38 -1
  142. package/src/scripts/_lib/injection_effect.ts +151 -0
  143. package/src/scripts/_lib/layer_overlap_notice.ts +50 -6
  144. package/src/scripts/_lib/packed_binary_predicate.ts +225 -0
  145. package/src/scripts/_lib/pin_resolver.ts +13 -0
  146. package/src/scripts/_lib/regression_neighbourhood.ts +1 -1
  147. package/src/scripts/_lib/release_highlights.ts +19 -211
  148. package/src/scripts/_lib/release_material.ts +27 -77
  149. package/src/scripts/_lib/retired_status.ts +74 -0
  150. package/src/scripts/_lib/skill_catalogue.ts +156 -21
  151. package/src/scripts/_lib/structured_ask.ts +117 -0
  152. package/src/scripts/_lib/subagent_capsule.ts +117 -8
  153. package/src/scripts/_lib/tty_prompt.ts +76 -0
  154. package/src/scripts/ai_council/api_transport.ts +168 -0
  155. package/src/scripts/ai_council/clients.ts +58 -63
  156. package/src/scripts/ai_council/config.ts +13 -0
  157. package/src/scripts/ai_council/content_ceiling.ts +79 -0
  158. package/src/scripts/ai_council/deliberation_metrics.ts +353 -0
  159. package/src/scripts/ai_council/jury_aggregate.ts +178 -0
  160. package/src/scripts/ai_council/prompts.ts +9 -0
  161. package/src/scripts/ai_council/seat_policy.ts +137 -0
  162. package/src/scripts/ai_council/status_surface.ts +79 -0
  163. package/src/scripts/ai_council/synthesis_disagreement.ts +206 -0
  164. package/src/scripts/ai_council/transport_resolver.ts +59 -1
  165. package/src/scripts/ai_tells_rules.ts +446 -5
  166. package/src/scripts/ask_block_census.ts +499 -0
  167. package/src/scripts/bench_humanizer_eval.ts +264 -40
  168. package/src/scripts/bench_prose_tells_fp.ts +198 -0
  169. package/src/scripts/capture_skill_catalogue.ts +24 -11
  170. package/src/scripts/check_branch_work_committed.ts +234 -0
  171. package/src/scripts/check_candidate_lines.ts +499 -0
  172. package/src/scripts/check_continuity_surface.ts +475 -0
  173. package/src/scripts/check_dependency_floors.ts +13 -6
  174. package/src/scripts/check_estate_count.ts +40 -79
  175. package/src/scripts/check_pack_size.ts +263 -24
  176. package/src/scripts/check_release_highlights.ts +0 -157
  177. package/src/scripts/check_roadmap_trackable.ts +19 -16
  178. package/src/scripts/check_rule_layer_partition.ts +54 -34
  179. package/src/scripts/check_single_delivery.ts +97 -31
  180. package/src/scripts/check_update_banner.ts +13 -0
  181. package/src/scripts/code_graph/build.ts +138 -15
  182. package/src/scripts/code_graph/cli.ts +76 -8
  183. package/src/scripts/code_graph/extract.ts +11 -0
  184. package/src/scripts/code_graph/grammar_probe.ts +43 -0
  185. package/src/scripts/code_graph/loader.ts +88 -14
  186. package/src/scripts/code_graph/query.ts +121 -11
  187. package/src/scripts/code_graph/resolution_tiers.ts +238 -0
  188. package/src/scripts/code_graph/sqlite_store.ts +247 -6
  189. package/src/scripts/code_graph/types.ts +66 -1
  190. package/src/scripts/code_graph/validate.ts +23 -0
  191. package/src/scripts/condense.ts +34 -22
  192. package/src/scripts/corpus_manifest.ts +5 -5
  193. package/src/scripts/council_cli.ts +29 -39
  194. package/src/scripts/count_intent_disagreement.ts +426 -0
  195. package/src/scripts/detect_ai_tells.ts +142 -20
  196. package/src/scripts/handoff_context_hook.ts +79 -0
  197. package/src/scripts/harvest_reference_tokens.ts +698 -0
  198. package/src/scripts/hook_effect_doctor.ts +82 -1
  199. package/src/scripts/hook_manifest.json +1 -1
  200. package/src/scripts/hook_manifest.yaml +94 -12
  201. package/src/scripts/hooks/chain_nudge_hook.ts +282 -0
  202. package/src/scripts/hooks/code_graph_context_hook.ts +212 -0
  203. package/src/scripts/hooks/concern_registry.ts +6 -2
  204. package/src/scripts/hooks/delegation_nudge_hook.ts +45 -1
  205. package/src/scripts/hooks/host_semantics.ts +1 -1
  206. package/src/scripts/hooks/one_question_per_ask_hook.ts +191 -0
  207. package/src/scripts/hooks/orchestration_record_hook.ts +2 -2
  208. package/src/scripts/hooks/rule_inject_hook.ts +1 -1
  209. package/src/scripts/hooks/skill_route_hook.ts +11 -4
  210. package/src/scripts/inbox_source_census.ts +322 -0
  211. package/src/scripts/install-hooks.sh +134 -0
  212. package/src/scripts/{lint_carrier_integrity.ts → lint_deferral_integrity.ts} +60 -214
  213. package/src/scripts/lint_plan_risk_register.ts +13 -17
  214. package/src/scripts/lint_roadmap_blockers.ts +51 -1
  215. package/src/scripts/mcp_server/suggest_skill.ts +72 -0
  216. package/src/scripts/mcp_server/tools.ts +2 -56
  217. package/src/scripts/media/lib/adapter-contract.md +7 -2
  218. package/src/scripts/media/lib/fixtures/flux/asset-0001.png +0 -0
  219. package/src/scripts/media/lib/fixtures/gemini-image/asset-0001.png +0 -0
  220. package/src/scripts/media/lib/fixtures/ideogram/asset-0001.png +0 -0
  221. package/src/scripts/migrate_command_suggestions.ts +1 -1
  222. package/src/scripts/probe_unblocked_ask.ts +135 -2
  223. package/src/scripts/rdp_candidates_delta.ts +276 -0
  224. package/src/scripts/rdp_quality_eval.ts +217 -19
  225. package/src/scripts/release.ts +26 -71
  226. package/src/scripts/release_drill.ts +6 -10
  227. package/src/scripts/release_env.ts +1 -0
  228. package/src/scripts/release_mix_taxonomy.json +3 -2
  229. package/src/scripts/release_publication.ts +34 -29
  230. package/src/scripts/report_host_injection_effect.ts +189 -0
  231. package/src/scripts/report_layer_overlap.ts +41 -0
  232. package/src/scripts/report_skill_menu_census.ts +341 -0
  233. package/src/scripts/self_review_gate.ts +409 -15
  234. package/src/scripts/skill_tools/score_skill_relevance.ts +70 -6
  235. package/src/scripts/skill_tools/suggest_skill_for_task.ts +37 -3
  236. package/src/vendor/grammars/README.md +61 -0
  237. package/src/vendor/grammars/tree-sitter-javascript.wasm +0 -0
  238. package/src/vendor/grammars/tree-sitter-php.wasm +0 -0
  239. package/src/vendor/grammars/tree-sitter-typescript.wasm +0 -0
  240. package/src/scripts/_lib/carrier_status.ts +0 -118
  241. package/src/scripts/_lib/estate_carrier_cases.ts +0 -72
  242. package/src/scripts/hooks/code_graph_nudge_hook.ts +0 -191
@@ -9,22 +9,114 @@ import * as fs from 'node:fs';
9
9
 
10
10
  import { LexicalIndex, tokenize } from '../_lib/lexical_index.js';
11
11
  import { sanitizeLabel } from './sanitize.js';
12
- import { emitSqliteTwin, loadSerializedFromTwin } from './sqlite_store.js';
13
- import type { CodeEdge, CodeGraph } from './types.js';
12
+ import {
13
+ emitSqliteTwin,
14
+ type GraphIndex,
15
+ INDEXED_READ_MIN_EDGES,
16
+ loadSerializedFromTwin,
17
+ openGraphIndex,
18
+ } from './sqlite_store.js';
19
+ import type { CodeEdge, CodeGraph, CodeNode } from './types.js';
14
20
  import { validateGraph } from './validate.js';
15
21
 
16
22
  const APPROX_CHARS_PER_TOKEN = 4;
17
23
 
24
+ /**
25
+ * The node/adjacency reads every verb performs.
26
+ *
27
+ * Declared as interfaces rather than `Map` so one set of verbs runs over two
28
+ * stores. `Map` satisfies both structurally, so the in-memory path is unchanged
29
+ * and needed no edits; the SQLite path supplies objects with the same two
30
+ * methods that hit an index instead of a heap.
31
+ */
32
+ export interface NodeLookup {
33
+ get(id: string): CodeNode | undefined;
34
+ has(id: string): boolean;
35
+ }
36
+ export interface AdjacencyLookup {
37
+ get(id: string): CodeEdge[] | undefined;
38
+ }
39
+
18
40
  export interface LoadedGraph {
19
- graph: CodeGraph;
41
+ /**
42
+ * The whole deserialized graph — `null` on the indexed path, where not
43
+ * materializing it is the entire point. Nothing in this module reads it;
44
+ * the two callers that used to (`resolveSeedsTiered`, the CLI's
45
+ * changed-file seeds) go through `idsByLabel` / `idsInFiles` instead, which
46
+ * answer the same questions from an index.
47
+ */
48
+ graph: CodeGraph | null;
20
49
  source: string; // attribution path
21
- byId: Map<string, CodeGraph['nodes'][number]>;
22
- out: Map<string, CodeEdge[]>;
23
- in: Map<string, CodeEdge[]>;
24
- lex: LexicalIndex;
50
+ byId: NodeLookup;
51
+ out: AdjacencyLookup;
52
+ in: AdjacencyLookup;
53
+ /** Exact-label resolution. */
54
+ idsByLabel(label: string, limit: number): string[];
55
+ /** Non-file nodes declared in any of `files`. */
56
+ idsInFiles(files: readonly string[]): string[];
57
+ /**
58
+ * BM25 fallback corpus, LAZY.
59
+ *
60
+ * A function rather than a field because on the indexed path building it
61
+ * reads every node — the one whole-table scan left — and a seed that
62
+ * resolves by exact id or exact label must never pay for it. On the
63
+ * in-memory path the corpus is already in hand and this just returns it.
64
+ */
65
+ lex(): LexicalIndex;
66
+ /** Release any handle the store holds. No-op for the in-memory path. */
67
+ close(): void;
68
+ }
69
+
70
+ /**
71
+ * A `LoadedGraph` backed by the SQLite index — no `JSON.parse`, no whole-graph
72
+ * `Map`.
73
+ *
74
+ * The adjacency lookups are per-call SQL against `edges_source` / `edges_target`
75
+ * rather than a materialized map, which is the difference the threshold buys: a
76
+ * 2-hop `affected` touches the rows for its frontier, not every edge in the
77
+ * repository. Node reads are memoized per handle because a BFS asks for the
78
+ * same label repeatedly while rendering edge lines, and that repetition is
79
+ * cheap to remove and expensive to leave.
80
+ */
81
+ function indexBackedGraph(index: GraphIndex, source: string): LoadedGraph {
82
+ const nodeCache = new Map<string, CodeNode | undefined>();
83
+ const readNode = (id: string): CodeNode | undefined => {
84
+ if (nodeCache.has(id)) return nodeCache.get(id);
85
+ const n = index.getNode(id);
86
+ nodeCache.set(id, n);
87
+ return n;
88
+ };
89
+ let lexical: LexicalIndex | null = null;
90
+ return {
91
+ graph: null,
92
+ source,
93
+ byId: { get: readNode, has: (id) => readNode(id) !== undefined },
94
+ out: { get: (id) => index.edgesFrom(id) },
95
+ in: { get: (id) => index.edgesTo(id) },
96
+ idsByLabel: (labelText, limit) => index.idsByLabel(labelText, limit),
97
+ idsInFiles: (files) => index.idsInFiles(files),
98
+ lex: () => {
99
+ lexical ??= new LexicalIndex(index.lexicalCorpus());
100
+ return lexical;
101
+ },
102
+ close: () => index.close(),
103
+ };
25
104
  }
26
105
 
27
106
  export function loadGraph(graphPath: string, source = graphPath): LoadedGraph {
107
+ // Above the threshold, read through the INDEX: the twin's rows answer
108
+ // per-node and per-edge questions directly, so neither `JSON.parse` nor the
109
+ // two whole-graph `Map`s below ever run. This is the D6 repair — the twin
110
+ // used to be preferred and still returned the entire serialization, which
111
+ // made it a cheaper blob transport rather than an index.
112
+ const index = openGraphIndex(graphPath);
113
+ if (index) {
114
+ if (index.edgeCount >= INDEXED_READ_MIN_EDGES) return indexBackedGraph(index, source);
115
+ // Small graph: the in-memory path is cheaper. Release the handle rather
116
+ // than leaking a database for the life of the process.
117
+ index.close();
118
+ }
119
+
28
120
  // Prefer the derived SQLite twin (ADR-129): checksum/stat-verified against
29
121
  // the canonical JSON, byte-identical content, ~1/90 the load cost at
30
122
  // consumer scale. Fallback = parse the JSON — and best-effort re-emit the
@@ -51,7 +143,23 @@ export function loadGraph(graphPath: string, source = graphPath): LoadedGraph {
51
143
  add(inM, e.target, e);
52
144
  }
53
145
  const lex = new LexicalIndex(graph.nodes.map((n) => ({ id: n.id, text: `${n.label} ${n.id}` })));
54
- return { graph, source, byId, out: outM, in: inM, lex };
146
+ return {
147
+ graph,
148
+ source,
149
+ byId,
150
+ out: outM,
151
+ in: inM,
152
+ idsByLabel: (labelText, limit) =>
153
+ graph.nodes.filter((n) => n.label === labelText).slice(0, limit).map((n) => n.id),
154
+ idsInFiles: (files) => {
155
+ const set = new Set(files);
156
+ return graph.nodes.filter((n) => set.has(n.source_file) && n.kind !== 'file').map((n) => n.id);
157
+ },
158
+ lex: () => lex,
159
+ close: () => {
160
+ /* nothing held */
161
+ },
162
+ };
55
163
  }
56
164
 
57
165
  interface SeedResolution {
@@ -64,9 +172,11 @@ interface SeedResolution {
64
172
  /** Resolve a free-text seed to node ids: exact id → exact label → BM25. */
65
173
  function resolveSeedsTiered(g: LoadedGraph, seed: string, limit = 5): SeedResolution {
66
174
  if (g.byId.has(seed)) return { ids: [seed], weak: false };
67
- const exactLabel = g.graph.nodes.filter((n) => n.label === seed).map((n) => n.id);
68
- if (exactLabel.length) return { ids: exactLabel.slice(0, limit), weak: false };
69
- const ranked = g.lex.rank(tokenize(seed)).filter((r) => r.score > 0);
175
+ const exactLabel = g.idsByLabel(seed, limit);
176
+ if (exactLabel.length) return { ids: exactLabel, weak: false };
177
+ // Only here does the BM25 corpus get built — the one whole-table read on
178
+ // the indexed path, and unreachable when the seed resolved exactly.
179
+ const ranked = g.lex().rank(tokenize(seed)).filter((r) => r.score > 0);
70
180
  return { ids: ranked.slice(0, limit).map((r) => r.id), weak: true };
71
181
  }
72
182
 
@@ -0,0 +1,238 @@
1
+ /**
2
+ * Resolution tiers that run BEFORE the repo-wide name lookup
3
+ * (road-to-a-graph-that-is-shipped 2.3).
4
+ *
5
+ * The extractor's ladder ends in a same-name table across the whole
6
+ * repository. That rung is a guess and is labelled one — `INFERRED`, via
7
+ * `name-lookup`. Two configuration files in a normal project state the answer
8
+ * outright, and reading them turns a guess into a fact:
9
+ *
10
+ * · `tsconfig.json` `compilerOptions.paths` — `@shared/x` IS `src/shared/x`,
11
+ * because the project says so. Without this tier the specifier is non-relative,
12
+ * so `resolveSpecifier` returns null and the import binds to `external:@shared/x`
13
+ * — a real module, correctly named, and the wrong one.
14
+ * · `composer.json` `autoload.psr-4` — `App\Services\Mailer` IS
15
+ * `app/Services/Mailer.php`. Without this tier a PHP `use` is matched by BASE
16
+ * NAME with the namespace discarded, so two classes called `Mailer` in
17
+ * different namespaces are indistinguishable.
18
+ *
19
+ * Both are DECLARED mappings, so an edge resolved through one is EXTRACTED, not
20
+ * INFERRED: the project's own config is as much a syntactic fact as an import
21
+ * specifier is.
22
+ *
23
+ * Deterministic and IO-free at resolution time — the configs are read once by
24
+ * the caller and passed in, so `buildGraph` stays pure and identical source
25
+ * plus identical config yields identical bytes.
26
+ */
27
+ import type { ResolvedVia } from './types.js';
28
+
29
+ /** Extensions a TS/JS alias target may carry, in resolution order. */
30
+ const ALIAS_EXTS = ['.ts', '.tsx', '.mts', '.cts', '.js', '.jsx', '.mjs', '.cjs'] as const;
31
+
32
+ export interface AliasRule {
33
+ /** The pattern's literal prefix, e.g. `@shared/` for `@shared/*`. */
34
+ prefix: string;
35
+ /** Whether the pattern ended in `*` (a prefix rule) or was exact. */
36
+ wildcard: boolean;
37
+ /** Repo-relative substitution targets, already joined with `baseUrl`. */
38
+ targets: string[];
39
+ }
40
+
41
+ export interface Psr4Rule {
42
+ /** Namespace prefix WITH its trailing separator, e.g. `App\\`. */
43
+ prefix: string;
44
+ /** Repo-relative directories, with a trailing slash. */
45
+ dirs: string[];
46
+ }
47
+
48
+ export interface ResolutionConfig {
49
+ aliases: AliasRule[];
50
+ psr4: Psr4Rule[];
51
+ }
52
+
53
+ export const EMPTY_RESOLUTION_CONFIG: ResolutionConfig = { aliases: [], psr4: [] };
54
+
55
+ /**
56
+ * Parse JSON that may carry comments and trailing commas.
57
+ *
58
+ * `tsconfig.json` is JSONC by convention and this repository's own uses `//`
59
+ * comments, so `JSON.parse` on it throws. Returning null rather than throwing
60
+ * is deliberate: an unparseable config must degrade to "no tiers", never break
61
+ * a build. Strings are walked rather than regex-stripped so a `//` inside a
62
+ * path value survives.
63
+ */
64
+ export function parseJsonc(text: string): unknown | null {
65
+ let out = '';
66
+ let inString = false;
67
+ let escaped = false;
68
+ let i = 0;
69
+ while (i < text.length) {
70
+ const ch = text[i] as string;
71
+ if (inString) {
72
+ out += ch;
73
+ if (escaped) escaped = false;
74
+ else if (ch === '\\') escaped = true;
75
+ else if (ch === '"') inString = false;
76
+ i += 1;
77
+ continue;
78
+ }
79
+ if (ch === '"') {
80
+ inString = true;
81
+ out += ch;
82
+ i += 1;
83
+ continue;
84
+ }
85
+ if (ch === '/' && text[i + 1] === '/') {
86
+ while (i < text.length && text[i] !== '\n') i += 1;
87
+ continue;
88
+ }
89
+ if (ch === '/' && text[i + 1] === '*') {
90
+ i += 2;
91
+ while (i < text.length && !(text[i] === '*' && text[i + 1] === '/')) i += 1;
92
+ i += 2;
93
+ continue;
94
+ }
95
+ out += ch;
96
+ i += 1;
97
+ }
98
+ // Trailing commas before a closing brace/bracket.
99
+ out = out.replace(/,(\s*[}\]])/g, '$1');
100
+ try {
101
+ return JSON.parse(out) as unknown;
102
+ } catch {
103
+ return null;
104
+ }
105
+ }
106
+
107
+ function isRecord(v: unknown): v is Record<string, unknown> {
108
+ return typeof v === 'object' && v !== null && !Array.isArray(v);
109
+ }
110
+
111
+ /** Normalise a repo-relative path fragment: no leading `./`, no trailing `/`. */
112
+ function normalizeFragment(p: string): string {
113
+ return p.replace(/^\.\//, '').replace(/\/+$/, '');
114
+ }
115
+
116
+ /**
117
+ * Build the alias tier from a parsed `tsconfig.json`.
118
+ *
119
+ * `baseUrl` is resolved relative to the tsconfig's own directory, which for a
120
+ * root tsconfig is the repo root — so `baseUrl: "./src"` plus `"@shared/*":
121
+ * ["shared/*"]` yields the repo-relative prefix `src/shared/`.
122
+ */
123
+ export function aliasRulesFrom(tsconfig: unknown): AliasRule[] {
124
+ if (!isRecord(tsconfig)) return [];
125
+ const co = tsconfig['compilerOptions'];
126
+ if (!isRecord(co)) return [];
127
+ const paths = co['paths'];
128
+ if (!isRecord(paths)) return [];
129
+ const baseUrl = typeof co['baseUrl'] === 'string' ? normalizeFragment(co['baseUrl']) : '';
130
+ const rules: AliasRule[] = [];
131
+ for (const [pattern, valueRaw] of Object.entries(paths)) {
132
+ if (!Array.isArray(valueRaw)) continue;
133
+ const wildcard = pattern.endsWith('*');
134
+ const prefix = wildcard ? pattern.slice(0, -1) : pattern;
135
+ const targets: string[] = [];
136
+ for (const v of valueRaw) {
137
+ if (typeof v !== 'string') continue;
138
+ const tgt = wildcard ? (v.endsWith('*') ? v.slice(0, -1) : v) : v;
139
+ const joined = baseUrl ? `${baseUrl}/${normalizeFragment(tgt)}` : normalizeFragment(tgt);
140
+ targets.push(joined.replace(/\/+$/, ''));
141
+ }
142
+ if (targets.length) rules.push({ prefix, wildcard, targets });
143
+ }
144
+ // Longest prefix first: `@shared/deep/` must beat `@shared/` when both are
145
+ // declared, which is TypeScript's own rule and not a preference.
146
+ rules.sort((a, b) => b.prefix.length - a.prefix.length);
147
+ return rules;
148
+ }
149
+
150
+ /** Build the PSR-4 tier from a parsed `composer.json`. */
151
+ export function psr4RulesFrom(composer: unknown): Psr4Rule[] {
152
+ if (!isRecord(composer)) return [];
153
+ const rules: Psr4Rule[] = [];
154
+ for (const key of ['autoload', 'autoload-dev']) {
155
+ const block = composer[key];
156
+ if (!isRecord(block)) continue;
157
+ const psr4 = block['psr-4'];
158
+ if (!isRecord(psr4)) continue;
159
+ for (const [ns, valueRaw] of Object.entries(psr4)) {
160
+ const dirsRaw = Array.isArray(valueRaw) ? valueRaw : [valueRaw];
161
+ const dirs = dirsRaw
162
+ .filter((d): d is string => typeof d === 'string')
163
+ .map((d) => {
164
+ const n = normalizeFragment(d);
165
+ return n === '' ? '' : `${n}/`;
166
+ });
167
+ if (dirs.length) rules.push({ prefix: ns, dirs });
168
+ }
169
+ }
170
+ // Longest namespace first — PSR-4's own most-specific-wins rule.
171
+ rules.sort((a, b) => b.prefix.length - a.prefix.length);
172
+ return rules;
173
+ }
174
+
175
+ function firstExisting(candidate: string, fileIds: ReadonlySet<string>): string | null {
176
+ if (fileIds.has(candidate)) return candidate;
177
+ // TS source imports `@shared/mailer.js` and the file on disk is
178
+ // `mailer.ts`, so the RUNTIME extension is stripped before the candidate
179
+ // list is tried — the same normalisation `resolveSpecifier` does for a
180
+ // relative specifier. Without it every alias in an ESM-style codebase
181
+ // misses, which is most of them.
182
+ const stem = candidate.replace(/\.(js|jsx|mjs|cjs)$/, '');
183
+ for (const e of ALIAS_EXTS) if (fileIds.has(stem + e)) return stem + e;
184
+ for (const e of ALIAS_EXTS) if (fileIds.has(`${candidate}/index${e}`)) return `${candidate}/index${e}`;
185
+ return null;
186
+ }
187
+
188
+ /**
189
+ * Resolve a non-relative TS/JS specifier through the alias tier.
190
+ *
191
+ * Returns null when no rule matches or the mapped file is not in the graph —
192
+ * an alias pointing outside the built root is not a hit, and claiming it as one
193
+ * would be worse than the `external:` binding it replaces.
194
+ */
195
+ export function resolveAlias(spec: string, config: ResolutionConfig, fileIds: ReadonlySet<string>): string | null {
196
+ for (const rule of config.aliases) {
197
+ if (rule.wildcard) {
198
+ if (!spec.startsWith(rule.prefix)) continue;
199
+ const rest = spec.slice(rule.prefix.length);
200
+ for (const t of rule.targets) {
201
+ const hit = firstExisting(rest ? `${t}/${rest}` : t, fileIds);
202
+ if (hit) return hit;
203
+ }
204
+ } else {
205
+ if (spec !== rule.prefix) continue;
206
+ for (const t of rule.targets) {
207
+ const hit = firstExisting(t, fileIds);
208
+ if (hit) return hit;
209
+ }
210
+ }
211
+ }
212
+ return null;
213
+ }
214
+
215
+ /**
216
+ * Resolve a fully-qualified PHP class name through the PSR-4 tier.
217
+ *
218
+ * `App\Services\Mailer` under `{"App\\": "app/"}` becomes
219
+ * `app/Services/Mailer.php`. A leading backslash is tolerated because `use
220
+ * \App\X` is legal and means the same thing.
221
+ */
222
+ export function resolvePsr4(fqcn: string, config: ResolutionConfig, fileIds: ReadonlySet<string>): string | null {
223
+ const name = fqcn.replace(/^\\+/, '');
224
+ for (const rule of config.psr4) {
225
+ if (!name.startsWith(rule.prefix)) continue;
226
+ const rest = name.slice(rule.prefix.length).replace(/\\/g, '/');
227
+ if (!rest) continue;
228
+ for (const d of rule.dirs) {
229
+ const candidate = `${d}${rest}.php`;
230
+ if (fileIds.has(candidate)) return candidate;
231
+ }
232
+ }
233
+ return null;
234
+ }
235
+
236
+ /** The `resolved_via` value each tier stamps on the edges it produces. */
237
+ export const ALIAS_VIA: ResolvedVia = 'path-alias';
238
+ export const PSR4_VIA: ResolvedVia = 'psr4';
@@ -32,8 +32,32 @@ import {
32
32
  } from '../_lib/sqlite_guard.js';
33
33
  import type { CodeEdge, CodeGraph, CodeNode } from './types.js';
34
34
 
35
- /** Bump on ANY table/column change — a mismatch sends readers to the JSON. */
36
- export const GRAPH_STORE_VERSION = 1;
35
+ /**
36
+ * Bump on ANY table/column change — a mismatch sends readers to the JSON.
37
+ *
38
+ * 1 → 2 (2.1): `edges` gained `resolved_via` and `provider`; `nodes` gained
39
+ * indexes on `label` and `source_file`; `meta` gained `node_count` /
40
+ * `edge_count`. A v1 twin has none of them, and the indexed read path below
41
+ * would silently answer from missing columns.
42
+ *
43
+ * 2 → 3 (2.2): those two columns are now REQUIRED on every edge. A v2 twin has
44
+ * the columns but may have written them NULL, from a graph built before the
45
+ * fields existed — which reads back as a valid edge carrying a fabricated
46
+ * mechanism. Refusing it costs nothing, because the twin is derived and the
47
+ * next build re-emits it.
48
+ */
49
+ export const GRAPH_STORE_VERSION = 3;
50
+
51
+ /**
52
+ * Edge count at or above which the indexed read path replaces the blob path.
53
+ *
54
+ * A DECLARED threshold, not a measured crossover — nothing establishes that
55
+ * 50k is where the two paths actually cross. Below it, building the in-memory
56
+ * Maps is cheap enough that the extra SQL round-trips per hop are not worth
57
+ * paying; above it, `JSON.parse` plus two `Map`s over every edge is the
58
+ * dominant cost and it is paid before the first question is answered.
59
+ */
60
+ export const INDEXED_READ_MIN_EDGES = 50_000;
37
61
 
38
62
  /** Derived twin path for a JSON cache path.
39
63
  * cache-invalidation: versioned via PRAGMA user_version (GRAPH_STORE_VERSION), not the filename. */
@@ -70,15 +94,25 @@ export function emitSqliteTwin(graph: CodeGraph, serialized: string, jsonPath: s
70
94
  db.exec(
71
95
  'CREATE TABLE meta (key TEXT PRIMARY KEY, value TEXT NOT NULL);' +
72
96
  'CREATE TABLE nodes (id TEXT PRIMARY KEY, label TEXT NOT NULL, kind TEXT, source_file TEXT, l0 INTEGER, l1 INTEGER, l2 INTEGER, l3 INTEGER);' +
73
- 'CREATE TABLE edges (source TEXT NOT NULL, target TEXT NOT NULL, relation TEXT NOT NULL, confidence TEXT NOT NULL, candidates TEXT);' +
97
+ 'CREATE TABLE edges (source TEXT NOT NULL, target TEXT NOT NULL, relation TEXT NOT NULL, confidence TEXT NOT NULL, candidates TEXT, ambiguity_reason TEXT, resolved_via TEXT, provider TEXT);' +
74
98
  'CREATE INDEX edges_source ON edges(source);' +
75
- 'CREATE INDEX edges_target ON edges(target);',
99
+ 'CREATE INDEX edges_target ON edges(target);' +
100
+ // Seed resolution and the changed-file seed set are the two
101
+ // reads that would otherwise scan every node. `id` needs no
102
+ // index — it is the PRIMARY KEY.
103
+ 'CREATE INDEX nodes_label ON nodes(label);' +
104
+ 'CREATE INDEX nodes_source_file ON nodes(source_file);',
76
105
  );
77
106
  const meta = db.prepare('INSERT INTO meta (key, value) VALUES (?, ?)');
78
107
  meta.run('source_checksum', graph.source_checksum);
79
108
  meta.run('json_size', String(stat.size));
80
109
  meta.run('json_mtime_ms', String(stat.mtimeMs));
81
110
  meta.run('graph_json', serialized);
111
+ // Counts live in meta so a reader can pick its path from one row
112
+ // instead of a COUNT(*) over the table it is deciding whether to
113
+ // read at all.
114
+ meta.run('node_count', String(graph.nodes.length));
115
+ meta.run('edge_count', String(graph.edges.length));
82
116
  const insNode = db.prepare(
83
117
  'INSERT INTO nodes (id, label, kind, source_file, l0, l1, l2, l3) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
84
118
  );
@@ -96,10 +130,20 @@ export function emitSqliteTwin(graph: CodeGraph, serialized: string, jsonPath: s
96
130
  );
97
131
  }
98
132
  const insEdge = db.prepare(
99
- 'INSERT INTO edges (source, target, relation, confidence, candidates) VALUES (?, ?, ?, ?, ?)',
133
+ 'INSERT INTO edges (source, target, relation, confidence, candidates, ambiguity_reason, resolved_via, provider) ' +
134
+ 'VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
100
135
  );
101
136
  for (const e of graph.edges) {
102
- insEdge.run(e.source, e.target, e.relation, e.confidence, e.candidates ? JSON.stringify(e.candidates) : null);
137
+ insEdge.run(
138
+ e.source,
139
+ e.target,
140
+ e.relation,
141
+ e.confidence,
142
+ e.candidates ? JSON.stringify(e.candidates) : null,
143
+ e.ambiguity_reason ?? null,
144
+ e.resolved_via,
145
+ e.provider,
146
+ );
103
147
  }
104
148
  stampUserVersion(db, GRAPH_STORE_VERSION);
105
149
  } finally {
@@ -150,6 +194,203 @@ export function loadSerializedFromTwin(jsonPath: string): string | null {
150
194
  }
151
195
  }
152
196
 
197
+ /**
198
+ * A per-node / per-edge read API over the twin — the thing this module did not
199
+ * export before (road-to-a-graph-that-is-shipped D7).
200
+ *
201
+ * Every export it had took a PATH and returned the whole graph: the serialized
202
+ * blob, or a pair of counts. So "the query path prefers SQLite" was true and
203
+ * bought nothing — `loadSerializedFromTwin` hands back the entire
204
+ * serialization, and the caller then parses it and builds a `Map` over every
205
+ * node and every edge. The twin was a cheaper blob transport, never an index.
206
+ *
207
+ * These reads take an id, a label, or a file, and touch only the rows that
208
+ * answer. Statements are prepared once per handle because a BFS calls
209
+ * `edgesTo` once per frontier node, and re-preparing per hop would hand back
210
+ * the constant factor the index just removed.
211
+ *
212
+ * The handle owns an open database and MUST be closed. Every method is safe
213
+ * after `close()` only in the sense that it will throw — callers hold it for
214
+ * the duration of one query and release it.
215
+ */
216
+ export interface GraphIndex {
217
+ readonly nodeCount: number;
218
+ readonly edgeCount: number;
219
+ readonly sourceChecksum: string;
220
+ getNode(id: string): CodeNode | undefined;
221
+ hasNode(id: string): boolean;
222
+ /** Out-edges: everything `id` points at. */
223
+ edgesFrom(id: string): CodeEdge[];
224
+ /** In-edges: everything pointing at `id` — the reverse-BFS direction. */
225
+ edgesTo(id: string): CodeEdge[];
226
+ /** Exact-label resolution, served by `nodes_label`. */
227
+ idsByLabel(label: string, limit: number): string[];
228
+ /** Non-file nodes declared in any of `files` — the changed-seed lookup. */
229
+ idsInFiles(files: readonly string[]): string[];
230
+ /** Every node's id + searchable text. The ONLY whole-table read, and it
231
+ * exists because a BM25 fallback genuinely needs the corpus. Callers reach
232
+ * it only after an exact id AND an exact label both missed. */
233
+ lexicalCorpus(): { id: string; text: string }[];
234
+ close(): void;
235
+ }
236
+
237
+ interface NodeRow {
238
+ id: string;
239
+ label: string;
240
+ kind: string | null;
241
+ source_file: string | null;
242
+ l0: number | null;
243
+ l1: number | null;
244
+ l2: number | null;
245
+ l3: number | null;
246
+ }
247
+
248
+ interface EdgeRow {
249
+ source: string;
250
+ target: string;
251
+ relation: string;
252
+ confidence: string;
253
+ candidates: string | null;
254
+ ambiguity_reason: string | null;
255
+ resolved_via: string | null;
256
+ provider: string | null;
257
+ }
258
+
259
+ function toNode(r: NodeRow): CodeNode {
260
+ const loc = [r.l0, r.l1, r.l2, r.l3].filter((v): v is number => typeof v === 'number');
261
+ return {
262
+ id: r.id,
263
+ label: r.label,
264
+ kind: (r.kind ?? 'file') as CodeNode['kind'],
265
+ source_file: r.source_file ?? '',
266
+ source_location: loc,
267
+ };
268
+ }
269
+
270
+ function toEdge(r: EdgeRow): CodeEdge {
271
+ const e: CodeEdge = {
272
+ source: r.source,
273
+ target: r.target,
274
+ relation: r.relation as CodeEdge['relation'],
275
+ confidence: r.confidence as CodeEdge['confidence'],
276
+ // The SQL columns are nullable, the TypeScript fields are not. A null
277
+ // can only come from a twin emitted before these fields existed, which
278
+ // GRAPH_STORE_VERSION already refuses — so this coalesce is unreachable
279
+ // in a healthy tree and is written to fail READABLE rather than to
280
+ // fabricate: `name-lookup` is the weakest mechanism, so a mislabelled
281
+ // edge is under-trusted rather than over-trusted by Phase 3's filter.
282
+ resolved_via: (r.resolved_via ?? 'name-lookup') as CodeEdge['resolved_via'],
283
+ provider: (r.provider ?? 'native') as CodeEdge['provider'],
284
+ };
285
+ if (r.candidates) e.candidates = JSON.parse(r.candidates) as string[];
286
+ if (r.ambiguity_reason === 'receiver-unknown' || r.ambiguity_reason === 'hierarchy-unresolved') {
287
+ e.ambiguity_reason = r.ambiguity_reason;
288
+ }
289
+ return e;
290
+ }
291
+
292
+ /**
293
+ * Open the twin for INDEXED reads, or null when it cannot serve them.
294
+ *
295
+ * Null on exactly the conditions `loadSerializedFromTwin` already refuses on —
296
+ * absent, stale against the JSON's size/mtime, corrupt, version-mismatched, no
297
+ * `node:sqlite` — so the two paths agree about when the twin is trustworthy
298
+ * and a caller never has to ask twice.
299
+ */
300
+ export function openGraphIndex(jsonPath: string): GraphIndex | null {
301
+ if (!isSqliteAvailableSync()) return null;
302
+ const twin = sqliteTwinPath(jsonPath);
303
+ if (!fs.existsSync(twin)) return null;
304
+ let stat: fs.Stats;
305
+ try {
306
+ stat = fs.statSync(jsonPath);
307
+ } catch {
308
+ return null;
309
+ }
310
+ try {
311
+ const { DatabaseSync } = loadSqliteSync('code_graph.sqlite_store');
312
+ const db = new DatabaseSync(twin, { readOnly: true });
313
+ try {
314
+ if (readUserVersion(db) !== GRAPH_STORE_VERSION) {
315
+ db.close();
316
+ return null;
317
+ }
318
+ const rows = db.prepare('SELECT key, value FROM meta').all() as { key: string; value: string }[];
319
+ const meta = new Map(rows.map((r) => [r.key, r.value]));
320
+ if (meta.get('json_size') !== String(stat.size) || meta.get('json_mtime_ms') !== String(stat.mtimeMs)) {
321
+ db.close();
322
+ return null;
323
+ }
324
+ const nodeCount = Number(meta.get('node_count') ?? NaN);
325
+ const edgeCount = Number(meta.get('edge_count') ?? NaN);
326
+ if (!Number.isFinite(nodeCount) || !Number.isFinite(edgeCount)) {
327
+ // A v2 twin always carries both. Missing means something wrote
328
+ // this file that is not this module, and guessing the counts
329
+ // would be worse than declining.
330
+ db.close();
331
+ return null;
332
+ }
333
+
334
+ const qNode = db.prepare('SELECT * FROM nodes WHERE id = ?');
335
+ const qFrom = db.prepare('SELECT * FROM edges WHERE source = ?');
336
+ const qTo = db.prepare('SELECT * FROM edges WHERE target = ?');
337
+ const qLabel = db.prepare('SELECT id FROM nodes WHERE label = ? LIMIT ?');
338
+
339
+ return {
340
+ nodeCount,
341
+ edgeCount,
342
+ sourceChecksum: meta.get('source_checksum') ?? '',
343
+ getNode(id) {
344
+ const r = qNode.get(id) as unknown as NodeRow | undefined;
345
+ return r ? toNode(r) : undefined;
346
+ },
347
+ hasNode(id) {
348
+ return (qNode.get(id) as unknown as NodeRow | undefined) !== undefined;
349
+ },
350
+ edgesFrom(id) {
351
+ return (qFrom.all(id) as unknown as EdgeRow[]).map(toEdge);
352
+ },
353
+ edgesTo(id) {
354
+ return (qTo.all(id) as unknown as EdgeRow[]).map(toEdge);
355
+ },
356
+ idsByLabel(labelText, limit) {
357
+ return (qLabel.all(labelText, limit) as { id: string }[]).map((r) => r.id);
358
+ },
359
+ idsInFiles(files) {
360
+ if (files.length === 0) return [];
361
+ // Chunked because SQLite caps bound parameters (999 on the
362
+ // conservative default build) and a changed-file set from a
363
+ // wide diff can exceed it.
364
+ const out: string[] = [];
365
+ const CHUNK = 400;
366
+ for (let i = 0; i < files.length; i += CHUNK) {
367
+ const slice = files.slice(i, i + CHUNK);
368
+ const marks = slice.map(() => '?').join(',');
369
+ const stmt = db.prepare(
370
+ `SELECT id FROM nodes WHERE kind != 'file' AND source_file IN (${marks})`,
371
+ );
372
+ for (const r of stmt.all(...slice) as { id: string }[]) out.push(r.id);
373
+ }
374
+ return out;
375
+ },
376
+ lexicalCorpus() {
377
+ return (db.prepare('SELECT id, label FROM nodes').all() as { id: string; label: string }[]).map(
378
+ (r) => ({ id: r.id, text: `${r.label} ${r.id}` }),
379
+ );
380
+ },
381
+ close() {
382
+ db.close();
383
+ },
384
+ };
385
+ } catch (err) {
386
+ db.close();
387
+ throw err;
388
+ }
389
+ } catch {
390
+ return null;
391
+ }
392
+ }
393
+
153
394
  /** Structured node/edge access for future in-process consumers (kept minimal:
154
395
  * the loader above guarantees byte-identical answers; these are for tooling). */
155
396
  export function twinCounts(jsonPath: string): { nodes: number; edges: number } | null {