index.ts 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  1. /**
  2. * Context Builder
  3. *
  4. * Builds rich context for tasks by combining FTS search with graph traversal.
  5. * Outputs structured context ready to inject into Claude.
  6. */
  7. import * as fs from 'fs';
  8. import * as path from 'path';
  9. import {
  10. Node,
  11. Edge,
  12. NodeKind,
  13. EdgeKind,
  14. Subgraph,
  15. CodeBlock,
  16. TaskContext,
  17. TaskInput,
  18. BuildContextOptions,
  19. FindRelevantContextOptions,
  20. SearchResult,
  21. } from '../types';
  22. import { QueryBuilder } from '../db/queries';
  23. import { GraphTraverser } from '../graph';
  24. import { formatContextAsMarkdown, formatContextAsJson } from './formatter';
  25. import { logDebug } from '../errors';
  26. import { validatePathWithinRoot, isConfigLeafNode } from '../utils';
  27. import { isTestFile, extractSearchTerms, scorePathRelevance, getStemVariants, isDistinctiveIdentifier } from '../search/query-utils';
  28. import { LOW_CONFIDENCE_MARKER } from './markers';
  29. /**
  30. * Extract likely symbol names from a natural language query
  31. *
  32. * Identifies potential code symbols using patterns:
  33. * - CamelCase: UserService, signInWithGoogle
  34. * - snake_case: user_service, sign_in
  35. * - SCREAMING_SNAKE: MAX_RETRIES
  36. * - dot.notation: app.isPackaged (extracts both sides)
  37. * - Single words that look like identifiers (no spaces, not common English words)
  38. *
  39. * @param query - Natural language query
  40. * @returns Array of potential symbol names
  41. */
  42. function extractSymbolsFromQuery(query: string): string[] {
  43. const symbols = new Set<string>();
  44. // Extract CamelCase identifiers (2+ chars, starts with letter)
  45. const camelCasePattern = /\b([A-Z][a-z]+(?:[A-Z][a-z]*)*|[a-z]+(?:[A-Z][a-z]*)+)\b/g;
  46. let match;
  47. while ((match = camelCasePattern.exec(query)) !== null) {
  48. if (match[1] && match[1].length >= 2) {
  49. symbols.add(match[1]);
  50. }
  51. }
  52. // Extract snake_case identifiers
  53. const snakeCasePattern = /\b([a-z][a-z0-9]*(?:_[a-z0-9]+)+)\b/gi;
  54. while ((match = snakeCasePattern.exec(query)) !== null) {
  55. if (match[1] && match[1].length >= 3) {
  56. symbols.add(match[1]);
  57. }
  58. }
  59. // Extract SCREAMING_SNAKE_CASE
  60. const screamingPattern = /\b([A-Z][A-Z0-9]*(?:_[A-Z0-9]+)+)\b/g;
  61. while ((match = screamingPattern.exec(query)) !== null) {
  62. if (match[1]) {
  63. symbols.add(match[1]);
  64. }
  65. }
  66. // Extract ALL_CAPS acronyms (2+ chars, e.g., REST, HTTP, LRU, API)
  67. const acronymPattern = /\b([A-Z]{2,})\b/g;
  68. while ((match = acronymPattern.exec(query)) !== null) {
  69. if (match[1]) {
  70. symbols.add(match[1]);
  71. }
  72. }
  73. // Extract dot.notation and split into parts (e.g., "app.isPackaged" -> ["app", "isPackaged"])
  74. const dotPattern = /\b([a-zA-Z][a-zA-Z0-9]*(?:\.[a-zA-Z][a-zA-Z0-9]*)+)\b/g;
  75. while ((match = dotPattern.exec(query)) !== null) {
  76. if (match[1]) {
  77. // Add both the full path and individual parts
  78. symbols.add(match[1]);
  79. const parts = match[1].split('.');
  80. for (const part of parts) {
  81. if (part.length >= 2) {
  82. symbols.add(part);
  83. }
  84. }
  85. }
  86. }
  87. // Extract plain lowercase identifiers (3+ chars, not already matched)
  88. // Catches symbol names like "undo", "redo", "history", "render", "parse"
  89. const lowercasePattern = /\b([a-z][a-z0-9]{2,})\b/g;
  90. while ((match = lowercasePattern.exec(query)) !== null) {
  91. if (match[1]) {
  92. symbols.add(match[1]);
  93. }
  94. }
  95. // Filter out common English words that aren't likely symbol names
  96. const commonWords = new Set([
  97. 'the', 'and', 'for', 'with', 'from', 'this', 'that', 'have', 'been',
  98. 'will', 'would', 'could', 'should', 'does', 'done', 'make', 'made',
  99. 'use', 'used', 'using', 'work', 'works', 'find', 'found', 'show',
  100. 'call', 'called', 'calling', 'get', 'set', 'add', 'all', 'any',
  101. 'how', 'what', 'when', 'where', 'which', 'who', 'why',
  102. 'not', 'but', 'are', 'was', 'were', 'has', 'had', 'its',
  103. 'can', 'did', 'may', 'also', 'into', 'than', 'then', 'them',
  104. 'each', 'other', 'some', 'such', 'only', 'same', 'about',
  105. 'after', 'before', 'between', 'through', 'during', 'without',
  106. 'again', 'further', 'once', 'here', 'there', 'both', 'just',
  107. 'more', 'most', 'very', 'being', 'having', 'doing',
  108. 'system', 'need', 'needs', 'want', 'wants', 'like', 'look',
  109. 'change', 'changes', 'changed', 'changing',
  110. // Common English nouns/verbs that match thousands of unrelated code symbols
  111. 'layer', 'handle', 'handles', 'handling', 'incoming', 'outgoing',
  112. 'data', 'flow', 'flows', 'level', 'levels', 'request', 'requests',
  113. 'response', 'responses', 'implement', 'implements', 'implementation',
  114. 'interface', 'interfaces', 'class', 'classes', 'method', 'methods',
  115. 'trigger', 'triggers', 'affected', 'affect', 'affects',
  116. 'else', 'code', 'failing', 'failed', 'silently', 'decide', 'decides',
  117. 'return', 'returns', 'returned', 'take', 'takes', 'taken',
  118. 'check', 'checks', 'checked', 'create', 'creates', 'created',
  119. 'read', 'reads', 'write', 'writes', 'written',
  120. 'start', 'starts', 'stop', 'stops', 'run', 'runs', 'running',
  121. ]);
  122. return Array.from(symbols).filter(s => !commonWords.has(s.toLowerCase()));
  123. }
  124. /**
  125. * Default options for context building
  126. *
  127. * Tuned for minimal context usage while still providing useful results:
  128. * - Fewer nodes and code blocks by default
  129. * - Smaller code block size limit
  130. * - Shallower traversal
  131. */
  132. const DEFAULT_BUILD_OPTIONS: Required<BuildContextOptions> = {
  133. maxNodes: 20, // Reduced from 50 - most tasks don't need 50 symbols
  134. maxCodeBlocks: 5, // Reduced from 10 - only show most relevant code
  135. maxCodeBlockSize: 1500, // Reduced from 2000
  136. includeCode: true,
  137. format: 'markdown',
  138. searchLimit: 3, // Reduced from 5 - fewer entry points
  139. traversalDepth: 1, // Reduced from 2 - shallower graph expansion
  140. minScore: 0.3,
  141. };
  142. /**
  143. * Node kinds that provide high information value in context results.
  144. * Imports/exports are excluded because they have near-zero information density -
  145. * they tell you something exists, not how it works.
  146. */
  147. const HIGH_VALUE_NODE_KINDS: NodeKind[] = [
  148. 'function', 'method', 'class', 'interface', 'type_alias', 'struct', 'union', 'trait',
  149. 'component', 'route', 'variable', 'constant', 'enum', 'module', 'namespace',
  150. ];
  151. /**
  152. * Default options for finding relevant context
  153. */
  154. const DEFAULT_FIND_OPTIONS: Required<FindRelevantContextOptions> = {
  155. searchLimit: 3, // Reduced from 5
  156. traversalDepth: 1, // Reduced from 2
  157. maxNodes: 20, // Reduced from 50
  158. minScore: 0.3,
  159. edgeKinds: [],
  160. nodeKinds: HIGH_VALUE_NODE_KINDS, // Filter out imports/exports by default
  161. seedNames: [], // Segment-vocab supplement — filled by the facade
  162. };
  163. // Re-export the low-confidence sentinel (defined in a dependency-free leaf so
  164. // the MCP layer can import it without pulling this module's deps onto the
  165. // cold-start path). Builder code below uses the imported binding directly.
  166. export { LOW_CONFIDENCE_MARKER } from './markers';
  167. /**
  168. * Context Builder
  169. *
  170. * Coordinates semantic search and graph traversal to build
  171. * comprehensive context for tasks.
  172. */
  173. export class ContextBuilder {
  174. private projectRoot: string;
  175. private queries: QueryBuilder;
  176. private traverser: GraphTraverser;
  177. constructor(
  178. projectRoot: string,
  179. queries: QueryBuilder,
  180. traverser: GraphTraverser
  181. ) {
  182. this.projectRoot = projectRoot;
  183. this.queries = queries;
  184. this.traverser = traverser;
  185. }
  186. /**
  187. * Whether the project's `codegraph.json` `deprioritize` patterns cover this
  188. * path (#982). Explore ranks through its own path scorer as well as through
  189. * `searchNodes`, so the lever has to be applied here too or the setting would
  190. * only half-work — and explore is the surface #982 actually reports on.
  191. *
  192. * Only the -15 relevance penalty is shared. Explore's hard `continue` filters
  193. * and its non-production budget cap are deliberately NOT joined: those REMOVE
  194. * content, and `deprioritize` is a ranking lever by definition — `exclude` is
  195. * the lever for taking things out of reach.
  196. */
  197. private isDeprioritized(filePath: string): boolean {
  198. return this.queries.getDeprioritizedPathMatcher()?.(filePath) ?? false;
  199. }
  200. /**
  201. * Build context for a task
  202. *
  203. * Pipeline:
  204. * 1. Parse task input (string or {title, description})
  205. * 2. Run semantic search to find entry points
  206. * 3. Expand graph around entry points
  207. * 4. Extract code blocks for key nodes
  208. * 5. Format output for Claude
  209. *
  210. * @param input - Task description or object with title/description
  211. * @param options - Build options
  212. * @returns TaskContext (structured) or formatted string
  213. */
  214. async buildContext(
  215. input: TaskInput,
  216. options: BuildContextOptions = {}
  217. ): Promise<TaskContext | string> {
  218. const opts = { ...DEFAULT_BUILD_OPTIONS, ...options };
  219. // Parse input
  220. const query = typeof input === 'string' ? input : `${input.title}${input.description ? `: ${input.description}` : ''}`;
  221. // Find relevant context (semantic search + graph expansion)
  222. const subgraph = await this.findRelevantContext(query, {
  223. searchLimit: opts.searchLimit,
  224. traversalDepth: opts.traversalDepth,
  225. maxNodes: opts.maxNodes,
  226. minScore: opts.minScore,
  227. });
  228. // Get entry points (nodes from semantic search)
  229. const entryPoints = this.getEntryPoints(subgraph);
  230. // Extract code blocks for key nodes
  231. const codeBlocks = opts.includeCode
  232. ? await this.extractCodeBlocks(subgraph, opts.maxCodeBlocks, opts.maxCodeBlockSize)
  233. : [];
  234. // Get related files
  235. const relatedFiles = this.getRelatedFiles(subgraph);
  236. // Generate summary
  237. const summary = this.generateSummary(query, subgraph, entryPoints);
  238. // Calculate stats
  239. const stats = {
  240. nodeCount: subgraph.nodes.size,
  241. edgeCount: subgraph.edges.length,
  242. fileCount: relatedFiles.length,
  243. codeBlockCount: codeBlocks.length,
  244. totalCodeSize: codeBlocks.reduce((sum, block) => sum + block.content.length, 0),
  245. };
  246. const context: TaskContext = {
  247. query,
  248. subgraph,
  249. entryPoints,
  250. codeBlocks,
  251. relatedFiles,
  252. summary,
  253. stats,
  254. };
  255. // Return formatted output or raw context
  256. if (opts.format === 'markdown') {
  257. // Bounded candidate set (entry points + subgraph + code blocks), so the
  258. // DB-backed generated check is one probe, not a per-comparison query.
  259. const isGenerated = this.queries.generatedPredicateFor([
  260. ...entryPoints.map((n) => n.filePath),
  261. ...Array.from(subgraph.nodes.values(), (n) => n.filePath),
  262. ...codeBlocks.map((b) => b.filePath),
  263. ]);
  264. return formatContextAsMarkdown(context, isGenerated)
  265. + this.buildCallPathsSection(subgraph)
  266. + (subgraph.confidence === 'low' ? this.buildLowConfidenceNote(entryPoints) : '');
  267. } else if (opts.format === 'json') {
  268. return formatContextAsJson(context);
  269. }
  270. return context;
  271. }
  272. /**
  273. * Honest handoff appended when retrieval confidence is low (the query matched
  274. * mostly common words). Instead of the usual "this covers the surface" framing
  275. * — which, when wrong, sends the agent off to Read/Grep — it admits the
  276. * uncertainty and routes the agent to the precise tools (explore with real
  277. * symbol names, search, or files to browse the closest areas we *did* surface).
  278. */
  279. private buildLowConfidenceNote(entryPoints: Node[]): string {
  280. const dirs: string[] = [];
  281. const seen = new Set<string>();
  282. for (const n of entryPoints) {
  283. const slash = n.filePath.lastIndexOf('/');
  284. const dir = slash > 0 ? n.filePath.slice(0, slash) : n.filePath;
  285. if (!seen.has(dir)) { seen.add(dir); dirs.push(dir); }
  286. if (dirs.length >= 4) break;
  287. }
  288. const dirLine = dirs.length
  289. ? `\n- \`codegraph_files\` a likely area: ${dirs.map(d => `\`${d}\``).join(', ')}`
  290. : '';
  291. return `\n\n${LOW_CONFIDENCE_MARKER}\n\n`
  292. + 'This query matched mostly on common words, so the entry points above may '
  293. + 'be off-target — treat them as a starting point, not a complete answer. '
  294. + 'For a reliable result:\n'
  295. + '- `codegraph_explore` with the **exact symbol names** you are after '
  296. + '(class / function / method names), or\n'
  297. + '- `codegraph_search <name>` for one specific symbol'
  298. + dirLine
  299. + '\n\nDo not assume the list above is comprehensive.';
  300. }
  301. /**
  302. * Surface short call-paths among the symbols this context already found,
  303. * derived in-memory from the subgraph's `calls` edges (no extra queries).
  304. *
  305. * This bakes the value of path-finding INTO the always-loaded `context` tool.
  306. * Agents reliably read context's output but do NOT discover/adopt a standalone
  307. * trace tool (in deferred-MCP harnesses they only ToolSearch-select tools they
  308. * already know). Delivering the flow here means "how does X reach Y" is
  309. * answered without the agent needing to find, load, or choose a new tool.
  310. * Chains stop where the static call graph ends (e.g. dynamic dispatch) — that
  311. * truncation is honest, and the agent can codegraph_node the last hop to bridge.
  312. */
  313. private buildCallPathsSection(subgraph: Subgraph): string {
  314. const adj = new Map<string, string[]>();
  315. for (const e of subgraph.edges) {
  316. if (e.kind !== 'calls') continue;
  317. if (!subgraph.nodes.has(e.source) || !subgraph.nodes.has(e.target)) continue;
  318. const list = adj.get(e.source);
  319. if (list) list.push(e.target);
  320. else adj.set(e.source, [e.target]);
  321. }
  322. if (adj.size === 0) return '';
  323. const MAX_HOPS = 6;
  324. const chains: string[][] = [];
  325. let budget = 2000; // bound DFS work on dense subgraphs
  326. const dfs = (id: string, path: string[], seen: Set<string>): void => {
  327. if (budget-- <= 0) return;
  328. const next = (adj.get(id) ?? []).filter((t) => !seen.has(t));
  329. if (next.length === 0 || path.length >= MAX_HOPS) {
  330. if (path.length >= 3) chains.push([...path]); // >=3 nodes = a real flow, not a single call
  331. return;
  332. }
  333. for (const t of next) {
  334. seen.add(t);
  335. dfs(t, [...path, t], seen);
  336. seen.delete(t);
  337. }
  338. };
  339. const starts = (subgraph.roots.length > 0
  340. ? subgraph.roots.filter((id) => adj.has(id))
  341. : [...adj.keys()]
  342. ).slice(0, 5);
  343. for (const s of starts) dfs(s, [s], new Set([s]));
  344. if (chains.length === 0) return '';
  345. // Keep only chains that connect TWO OR MORE query-relevant symbols (roots).
  346. // A chain from a root into an arbitrary callee (render → onMagicFrameGenerate)
  347. // is structurally valid but tangential to the question; requiring ≥2 roots
  348. // keeps the chain anchored to what the user actually asked about. Rank by
  349. // #roots then length, and drop any that are a sub-path of a longer kept chain.
  350. const rootSet = new Set(subgraph.roots);
  351. const rootCount = (c: string[]): number => c.reduce((n, id) => n + (rootSet.has(id) ? 1 : 0), 0);
  352. const relevant = chains.filter((c) => rootCount(c) >= 2);
  353. relevant.sort((a, b) => rootCount(b) - rootCount(a) || b.length - a.length);
  354. const kept: string[][] = [];
  355. for (const c of relevant) {
  356. const key = c.join('>');
  357. if (kept.some((k) => k.join('>').includes(key))) continue;
  358. kept.push(c);
  359. if (kept.length >= 3) break;
  360. }
  361. if (kept.length === 0) return '';
  362. const name = (id: string): string => subgraph.nodes.get(id)?.name ?? id;
  363. // Synthesized (dynamic-dispatch) hops are real `calls` edges but invisible to
  364. // static parsing — mark them inline so the agent sees WHERE the callback was
  365. // wired up (`registered @file:line`) instead of grepping for it. Keyed by
  366. // "source>target".
  367. const synthByPair = new Map<string, string>();
  368. for (const e of subgraph.edges) {
  369. if (e.kind !== 'calls' || e.provenance !== 'heuristic') continue;
  370. const m = e.metadata as Record<string, unknown> | undefined;
  371. if (!m?.synthesizedBy) continue;
  372. const at = typeof m.registeredAt === 'string' ? ` @${m.registeredAt}` : '';
  373. const label = m.synthesizedBy === 'callback'
  374. ? `callback via ${m.via ? `\`${String(m.via)}\`` : 'registrar'}${at}`
  375. : m.synthesizedBy === 'react-render'
  376. ? `React re-render via setState${at}`
  377. : m.synthesizedBy === 'jsx-render'
  378. ? `renders <${String(m.via || 'child')}>`
  379. : m.synthesizedBy === 'vue-handler'
  380. ? `Vue @${String(m.event || 'event')} handler`
  381. : `event ${m.event ? `\`${String(m.event)}\`` : ''}${at}`;
  382. synthByPair.set(`${e.source}>${e.target}`, label);
  383. }
  384. const renderChain = (c: string[]): string => {
  385. let s = name(c[0]!);
  386. for (let i = 1; i < c.length; i++) {
  387. const synth = synthByPair.get(`${c[i - 1]}>${c[i]}`);
  388. s += synth ? ` →[${synth}] ${name(c[i]!)}` : ` → ${name(c[i]!)}`;
  389. }
  390. return s;
  391. };
  392. const hasSynth = kept.some((c) => c.some((_, i) => i > 0 && synthByPair.has(`${c[i - 1]}>${c[i]}`)));
  393. const lines = [
  394. '',
  395. '## Call paths',
  396. '',
  397. 'Execution flow among the key symbols (traced through the call graph):',
  398. '',
  399. ...kept.map((c) => `- ${renderChain(c)}`),
  400. '',
  401. hasSynth
  402. ? '_Hops marked `[callback/event …]` are dynamic dispatch bridged by codegraph (with the registration site); the rest are direct calls. codegraph_node any symbol for its body._'
  403. : '_codegraph_node any symbol above for its source + its own callers/callees._',
  404. ];
  405. return '\n' + lines.join('\n') + '\n';
  406. }
  407. /**
  408. * Find relevant subgraph for a query
  409. *
  410. * Uses hybrid search combining exact symbol lookup with semantic search:
  411. * 1. Extract potential symbol names from query
  412. * 2. Look up exact matches for those symbols (high confidence)
  413. * 3. Use semantic search for concept matching
  414. * 4. Merge results, prioritizing exact matches
  415. * 5. Traverse graph from entry points
  416. *
  417. * @param query - Natural language query
  418. * @param options - Search and traversal options
  419. * @returns Subgraph of relevant nodes and edges
  420. */
  421. async findRelevantContext(
  422. query: string,
  423. options: FindRelevantContextOptions = {}
  424. ): Promise<Subgraph> {
  425. const opts = { ...DEFAULT_FIND_OPTIONS, ...options };
  426. // Start with empty subgraph
  427. const nodes = new Map<string, Node>();
  428. const edges: Edge[] = [];
  429. const roots: string[] = [];
  430. // Handle empty query - return empty subgraph
  431. if (!query || query.trim().length === 0) {
  432. return { nodes, edges, roots };
  433. }
  434. // === HYBRID SEARCH ===
  435. // Step 1: Extract potential symbol names from query
  436. const symbolsFromQuery = extractSymbolsFromQuery(query);
  437. logDebug('Extracted symbols from query', { query, symbols: symbolsFromQuery });
  438. // Step 2: Look up exact matches for extracted symbols
  439. let exactMatches: SearchResult[] = [];
  440. if (symbolsFromQuery.length > 0 || opts.seedNames.length > 0) {
  441. try {
  442. if (symbolsFromQuery.length > 0) {
  443. // Get more results so we can apply co-location boosting before trimming
  444. exactMatches = this.queries.findNodesByExactName(symbolsFromQuery, {
  445. limit: Math.ceil(opts.searchLimit * 5),
  446. kinds: opts.nodeKinds && opts.nodeKinds.length > 0 ? opts.nodeKinds : undefined,
  447. });
  448. }
  449. // Step 2a: segment-vocabulary seeds. Word-level query terms cannot
  450. // reach camelCase names through FTS (one token per name), so the
  451. // caller resolves query words → names via the segment vocab and hands
  452. // them in as seedNames. Merged at a dampened score — a symbol the
  453. // query names outright must outrank a segment-derived one — but
  454. // BEFORE the co-location boost below, because several seeds landing
  455. // in one file (pinFeedIfNearBottom + feedAtBottom + handleFeedScroll)
  456. // is exactly the evidence that file is the answer.
  457. if (opts.seedNames.length > 0) {
  458. const seedResults = this.queries.findNodesByExactName(opts.seedNames, {
  459. limit: Math.ceil(opts.searchLimit * 3),
  460. kinds: opts.nodeKinds && opts.nodeKinds.length > 0 ? opts.nodeKinds : undefined,
  461. });
  462. const known = new Set(exactMatches.map((r) => r.node.id));
  463. for (const r of seedResults) {
  464. if (known.has(r.node.id)) continue;
  465. known.add(r.node.id);
  466. exactMatches.push({ ...r, score: r.score * 0.6 });
  467. }
  468. logDebug('Segment seed matches', { seedNames: opts.seedNames, added: known.size });
  469. }
  470. // Co-location boost: when multiple extracted symbols appear in the same file,
  471. // those results are much more likely to be what the user is looking for.
  472. // E.g., "scrapeLoop" + "run" both in scrape/scrape.go → boost both.
  473. if (exactMatches.length > 1) {
  474. // Build a map of files → how many distinct symbol names matched in that file
  475. const fileSymbolCounts = new Map<string, Set<string>>();
  476. for (const r of exactMatches) {
  477. const names = fileSymbolCounts.get(r.node.filePath) || new Set();
  478. names.add(r.node.name.toLowerCase());
  479. fileSymbolCounts.set(r.node.filePath, names);
  480. }
  481. // Boost results in files where multiple query symbols co-occur
  482. exactMatches = exactMatches.map(r => {
  483. const symbolCount = fileSymbolCounts.get(r.node.filePath)?.size || 1;
  484. return {
  485. ...r,
  486. score: symbolCount > 1 ? r.score + (symbolCount - 1) * 20 : r.score,
  487. };
  488. });
  489. exactMatches.sort((a, b) => b.score - a.score);
  490. }
  491. // Trim back to reasonable size
  492. exactMatches = exactMatches.slice(0, Math.ceil(opts.searchLimit * 2));
  493. logDebug('Exact symbol matches', { count: exactMatches.length });
  494. } catch (error) {
  495. logDebug('Exact symbol lookup failed', { error: String(error) });
  496. }
  497. }
  498. // Step 2b: Search for extracted symbols as definition (class/interface) prefixes.
  499. // When the user writes "REST", "bulk", or "allocation", they usually mean classes
  500. // like RestController, BulkRequest, AllocationService — not nodes named exactly that.
  501. // Also tries stem variants: "caching" → "cache" finds Cache, CacheBuilder.
  502. if (symbolsFromQuery.length > 0) {
  503. const definitionKinds: NodeKind[] = ['class', 'interface', 'struct', 'union', 'trait',
  504. 'protocol', 'enum', 'type_alias'];
  505. // Expand symbols with stem variants for broader definition matching
  506. const expandedSymbols = new Set(symbolsFromQuery);
  507. for (const sym of symbolsFromQuery) {
  508. for (const variant of getStemVariants(sym)) {
  509. expandedSymbols.add(variant);
  510. }
  511. }
  512. for (const sym of expandedSymbols) {
  513. // Title-case the symbol: "REST" → "Rest", "bulk" → "Bulk", "allocation" → "Allocation"
  514. const titleCased = sym.charAt(0).toUpperCase() + sym.slice(1).toLowerCase();
  515. if (titleCased === sym) continue; // already title-case (e.g., "Engine") — handled by exact match
  516. // Fetch more results since popular prefixes have many matches
  517. const prefixResults = this.queries.searchNodes(titleCased, {
  518. limit: 30,
  519. kinds: definitionKinds,
  520. });
  521. const matched: SearchResult[] = [];
  522. for (const r of prefixResults) {
  523. if (r.node.name.toLowerCase().startsWith(titleCased.toLowerCase())) {
  524. // Favor shorter names: "AllocationService" (18 chars) over
  525. // "AllocationBalancingRoundMetrics" (31 chars). Core classes tend
  526. // to have concise names; test/helper classes are verbose.
  527. const brevityBonus = Math.max(0, 10 - (r.node.name.length - titleCased.length) / 3);
  528. matched.push({ ...r, score: r.score + 15 + brevityBonus });
  529. }
  530. }
  531. matched.sort((a, b) => b.score - a.score);
  532. for (const r of matched.slice(0, Math.ceil(opts.searchLimit))) {
  533. const existing = exactMatches.find(e => e.node.id === r.node.id);
  534. if (!existing) {
  535. exactMatches.push(r);
  536. }
  537. }
  538. }
  539. exactMatches.sort((a, b) => b.score - a.score);
  540. exactMatches = exactMatches.slice(0, Math.ceil(opts.searchLimit * 3));
  541. }
  542. // Step 3: Run text search for natural language term matching
  543. // This catches file-name and node-name matches that semantic search may miss,
  544. // which is critical for template-heavy codebases (e.g., Liquid/Shopify themes)
  545. // where file names are the primary identifiers.
  546. let textResults: SearchResult[] = [];
  547. try {
  548. const searchTerms = extractSearchTerms(query);
  549. if (searchTerms.length > 0) {
  550. // Search each term individually to get broader coverage,
  551. // then boost results that match multiple terms
  552. const termResultsMap = new Map<string, { result: SearchResult; termHits: number }>();
  553. // When no explicit kind filter is set, exclude imports — they flood FTS
  554. // results with qualified name matches (e.g., "REST" matches 445K import paths)
  555. // but are almost never what exploration queries want.
  556. const searchKinds = opts.nodeKinds && opts.nodeKinds.length > 0
  557. ? opts.nodeKinds
  558. : ['file', 'module', 'class', 'struct', 'union', 'interface', 'trait', 'protocol',
  559. 'function', 'method', 'property', 'field', 'variable', 'constant',
  560. 'enum', 'enum_member', 'type_alias', 'namespace', 'export',
  561. 'route', 'component'] as NodeKind[];
  562. for (const term of searchTerms) {
  563. const termResults = this.queries.searchNodes(term, {
  564. limit: opts.searchLimit * 2,
  565. kinds: searchKinds,
  566. });
  567. for (const r of termResults) {
  568. const existing = termResultsMap.get(r.node.id);
  569. if (existing) {
  570. existing.termHits++;
  571. existing.result.score = Math.max(existing.result.score, r.score);
  572. } else {
  573. termResultsMap.set(r.node.id, { result: r, termHits: 1 });
  574. }
  575. }
  576. }
  577. // Boost results matching multiple terms and sort
  578. textResults = Array.from(termResultsMap.values())
  579. .map(({ result, termHits }) => ({
  580. ...result,
  581. score: result.score + (termHits - 1) * 5,
  582. }))
  583. .sort((a, b) => b.score - a.score)
  584. .slice(0, opts.searchLimit * 2);
  585. }
  586. logDebug('Text search results', { count: textResults.length });
  587. } catch (error) {
  588. logDebug('Text search failed', { query, error: String(error) });
  589. }
  590. // Step 4: Merge results, taking the max score when duplicates appear
  591. // across search channels. Exact matches may have lower scores than FTS
  592. // results for the same node — use the best score from any channel.
  593. const resultById = new Map<string, SearchResult>();
  594. let searchResults: SearchResult[] = [];
  595. // Add exact matches first
  596. for (const result of exactMatches) {
  597. const existing = resultById.get(result.node.id);
  598. if (existing) {
  599. existing.score = Math.max(existing.score, result.score);
  600. } else {
  601. resultById.set(result.node.id, result);
  602. searchResults.push(result);
  603. }
  604. }
  605. // Add text search results, upgrading scores for duplicates
  606. for (const result of textResults) {
  607. const existing = resultById.get(result.node.id);
  608. if (existing) {
  609. existing.score = Math.max(existing.score, result.score);
  610. } else {
  611. resultById.set(result.node.id, result);
  612. searchResults.push(result);
  613. }
  614. }
  615. const queryLower = query.toLowerCase();
  616. const isTestQuery = queryLower.includes('test') || queryLower.includes('spec');
  617. // Deprioritize test files early so they don't take multi-term boost slots
  618. if (!isTestQuery) {
  619. for (const result of searchResults) {
  620. if (isTestFile(result.node.filePath)) {
  621. result.score *= 0.3;
  622. }
  623. }
  624. }
  625. // Iter7 — Core-directory boost. On projects with one file that holds
  626. // the dense majority of internal call edges (e.g. sinatra's
  627. // `lib/sinatra/base.rb` at 85% of all in-file edges), the agent's
  628. // task usually asks about the framework's core. Without this boost,
  629. // ranking favors small focused extension files (e.g. text search
  630. // picks `sinatra-contrib/lib/sinatra/multi_route.rb`'s 10-line
  631. // `route` method over `base.rb`'s `route!` because the extension
  632. // file's `route` matches the query verbatim AND the file is small,
  633. // dwarfing the longer name `route!` in a 1500-line file). Boost
  634. // results that share a directory prefix with the dominant file's
  635. // directory so the core file's siblings outrank sibling-package
  636. // extensions.
  637. try {
  638. const dominant = this.queries.getDominantFile?.();
  639. if (dominant && dominant.edgeCount >= 3 * dominant.nextEdgeCount) {
  640. // Take the directory of the dominant file (everything up to the
  641. // last slash). For `lib/sinatra/base.rb` → `lib/sinatra/`.
  642. const slash = dominant.filePath.lastIndexOf('/');
  643. if (slash > 0) {
  644. const coreDir = dominant.filePath.slice(0, slash + 1);
  645. for (const result of searchResults) {
  646. if (result.node.filePath.startsWith(coreDir)) {
  647. result.score += 25;
  648. }
  649. }
  650. }
  651. }
  652. } catch {
  653. // SQL failure — fall through, scoring works without the boost
  654. }
  655. // Step 5a: Multi-term co-occurrence re-ranking (applied BEFORE truncation).
  656. // For multi-word queries like "search execution from request to shard",
  657. // nodes matching 2+ query terms in their name or path are far more relevant
  658. // than nodes matching just one generic term. Without this, "ExecutionUtils"
  659. // (matches only "execution") fills budget slots meant for "ShardSearchRequest"
  660. // (matches "shard" + "search" + "request").
  661. const queryTermsForBoost = extractSearchTerms(query);
  662. if (queryTermsForBoost.length >= 2) {
  663. // Group terms that are substrings of each other (stem variants of the same
  664. // root word). "indexed", "indexe", "index" should count as ONE concept match,
  665. // not three. Without this, stem variants inflate matchCount and give false
  666. // multi-term boosts to symbols matching one root word multiple times.
  667. const termGroups: string[][] = [];
  668. const sorted = [...queryTermsForBoost].sort((a, b) => b.length - a.length);
  669. const assigned = new Set<string>();
  670. for (const term of sorted) {
  671. if (assigned.has(term)) continue;
  672. const group = [term];
  673. assigned.add(term);
  674. for (const other of sorted) {
  675. if (assigned.has(other)) continue;
  676. if (term.includes(other) || other.includes(term)) {
  677. group.push(other);
  678. assigned.add(other);
  679. }
  680. }
  681. termGroups.push(group);
  682. }
  683. // Build a set of exact-match node IDs so we can exempt them from dampening.
  684. // When the query is "LiveEditMode DevServerPreview", these are specific
  685. // symbols the user asked for — dampening them because they only match 1
  686. // term group is counter-productive.
  687. const exactMatchIds = new Set(exactMatches.map(r => r.node.id));
  688. // ...but only exempt exact matches the user *named as an identifier*
  689. // (camelCase/snake_case/acronym). A plain dictionary word that happens to
  690. // exact-match an unrelated symbol — query "flat object" → a constant named
  691. // FLAT — must NOT be exempt, or the +exact-name bonus floats it to the top
  692. // of a prose query with zero corroboration from any other term. Classify by
  693. // the QUERY token (what the user typed), not the matched symbol's name.
  694. const distinctiveTokens = new Set(
  695. symbolsFromQuery.filter(isDistinctiveIdentifier).map(s => s.toLowerCase())
  696. );
  697. const distinctiveExactMatchIds = new Set(
  698. exactMatches
  699. .filter(r => distinctiveTokens.has(r.node.name.toLowerCase()))
  700. .map(r => r.node.id)
  701. );
  702. for (const result of searchResults) {
  703. // Check term matches in name (substring) and path DIRECTORIES (exact).
  704. // Directory segments must match exactly — "search" matches directory
  705. // "search/" but NOT "elasticsearch/". The class name is checked
  706. // separately via substring match on the node name.
  707. const nameLower = result.node.name.toLowerCase();
  708. const dirSegments = path.dirname(result.node.filePath).toLowerCase().split('/');
  709. let matchCount = 0;
  710. for (const group of termGroups) {
  711. const groupMatches = group.some(term => {
  712. const inName = nameLower.includes(term);
  713. const inDir = dirSegments.some(seg => seg === term);
  714. return inName || inDir;
  715. });
  716. if (groupMatches) matchCount++;
  717. }
  718. if (matchCount >= 2) {
  719. // Multiplicative boost — 2 terms → 2x, 3 terms → 2.5x
  720. result.score *= 1 + matchCount * 0.5;
  721. } else if (distinctiveExactMatchIds.has(result.node.id)) {
  722. // Exact match on a distinctive identifier the user explicitly named —
  723. // keep full score (e.g. "LiveEditMode DevServerPreview").
  724. } else if (exactMatchIds.has(result.node.id)) {
  725. // Exact match on a COMMON word (e.g. "flat" → FLAT): high-scoring noise
  726. // inflated by the +exact-name bonus, corroborated by no other query
  727. // term. Demote hard so corroborated matches win.
  728. result.score *= 0.3;
  729. } else {
  730. // Mild dampen for generic single-term matches — they might be generic
  731. // but could also be the right result (e.g., "Protocol" class for an IPC query).
  732. result.score *= 0.6;
  733. }
  734. }
  735. searchResults.sort((a, b) => b.score - a.score);
  736. }
  737. // Step 5b: CamelCase-boundary matching via LIKE query.
  738. // FTS can't find "Search" inside "TransportSearchAction" (one FTS token).
  739. // LIKE reliably finds these substring matches. Results are appended with
  740. // guaranteed slots so they don't compete with higher-scoring prefix matches.
  741. if (symbolsFromQuery.length > 0) {
  742. const camelDefinitionKinds: NodeKind[] = ['class', 'interface', 'struct', 'union', 'trait',
  743. 'protocol', 'enum', 'type_alias'];
  744. // Callable kinds participate too: in service-layer codebases the
  745. // camel-infix definers of a queried FIELD are methods/functions
  746. // (`profileInfo` → `getProfileInfoV2`), not classes — the type-only
  747. // whitelist made this whole step dead code there (#1196). Fetched as a
  748. // SEPARATE LIKE batch so one hot single-word term can't crowd classes
  749. // out of the length-ordered 200-row batch.
  750. const camelCallableKinds: NodeKind[] = ['function', 'method', 'component'];
  751. const camelSearchedTerms = new Set<string>();
  752. const searchIdSet = new Set(searchResults.map(r => r.node.id));
  753. // Track per-node term hits for multi-term boosting
  754. const camelNodeTerms = new Map<string, { result: SearchResult; termCount: number }>();
  755. const maxCamelPerTerm = Math.ceil(opts.searchLimit / 2);
  756. for (const sym of symbolsFromQuery) {
  757. const titleCased = sym.charAt(0).toUpperCase() + sym.slice(1).toLowerCase();
  758. if (titleCased.length < 3) continue;
  759. const termKey = titleCased.toLowerCase();
  760. if (camelSearchedTerms.has(termKey)) continue;
  761. camelSearchedTerms.add(termKey);
  762. // Fetch a large batch — popular terms like "Search" in Elasticsearch
  763. // have hundreds of substring matches. The LIKE scan cost is the same
  764. // regardless of LIMIT (SQLite scans all matches to sort), so we fetch
  765. // generously and let path-relevance scoring pick the best ones.
  766. const likeResults = [
  767. ...this.queries.findNodesByNameSubstring(titleCased, {
  768. limit: 200,
  769. kinds: camelDefinitionKinds,
  770. excludePrefix: true,
  771. }),
  772. ...this.queries.findNodesByNameSubstring(titleCased, {
  773. limit: 200,
  774. kinds: camelCallableKinds,
  775. excludePrefix: true,
  776. }),
  777. ];
  778. // Filter to CamelCase boundaries, score by path relevance, and take top N
  779. const termCandidates: SearchResult[] = [];
  780. for (const r of likeResults) {
  781. const name = r.node.name;
  782. // Case-INSENSITIVE hump lookup: title-casing lowercases interior
  783. // humps (`profileInfo` → `Profileinfo`), which SQLite's LIKE still
  784. // matched but a case-sensitive indexOf here silently dropped —
  785. // making every multi-hump query term unfindable by this step
  786. // (#1196). The match must still LAND on an uppercase char, so a
  787. // plain lowercase infix can't slip through.
  788. const idx = name.toLowerCase().indexOf(termKey);
  789. if (idx <= 0) continue;
  790. if (!/[A-Z]/.test(name.charAt(idx))) continue;
  791. // Accept CamelCase boundary (lowercase before match) OR
  792. // acronym boundary (uppercase before match, e.g., RPCProtocol)
  793. if (!/[a-zA-Z]/.test(name.charAt(idx - 1))) continue;
  794. if (searchIdSet.has(r.node.id)) continue;
  795. if (isTestFile(r.node.filePath) && !isTestQuery) continue;
  796. const pathScore = scorePathRelevance(
  797. r.node.filePath,
  798. query,
  799. undefined,
  800. this.isDeprioritized(r.node.filePath),
  801. );
  802. const brevityBonus = Math.max(0, 6 - (name.length - titleCased.length) / 4);
  803. termCandidates.push({ node: r.node, score: 8 + brevityBonus + pathScore });
  804. }
  805. termCandidates.sort((a, b) => b.score - a.score);
  806. // Widen the per-term pool for accumulation so multi-term co-occurrences
  807. // can be discovered. A class matching 3 query terms at CamelCase boundaries
  808. // is far more relevant than one matching just 1, but it needs to survive
  809. // the per-term cut for EACH term to accumulate its count.
  810. const accumPerTerm = maxCamelPerTerm * 4;
  811. for (const r of termCandidates.slice(0, accumPerTerm)) {
  812. const existing = camelNodeTerms.get(r.node.id);
  813. if (existing) {
  814. existing.termCount++;
  815. } else {
  816. camelNodeTerms.set(r.node.id, {
  817. result: r,
  818. termCount: 1,
  819. });
  820. }
  821. }
  822. }
  823. // Append CamelCase matches with multi-term boost.
  824. // These are structurally important (class names containing query terms at
  825. // CamelCase boundaries) but score much lower than FTS results. Scale their
  826. // scores up so multi-term CamelCase matches can compete with FTS results.
  827. const camelResults: SearchResult[] = [];
  828. for (const [, info] of camelNodeTerms) {
  829. // Multi-term CamelCase matches are extremely relevant — a class matching
  830. // 3+ query terms in its name (e.g., ExtensionHostProcess) is almost
  831. // certainly what the user wants. Scale aggressively.
  832. info.result.score = info.result.score * (1 + info.termCount) + (info.termCount - 1) * 30;
  833. camelResults.push(info.result);
  834. }
  835. camelResults.sort((a, b) => b.score - a.score);
  836. const maxCamelTotal = opts.searchLimit;
  837. for (const r of camelResults.slice(0, maxCamelTotal)) {
  838. searchResults.push(r);
  839. searchIdSet.add(r.node.id);
  840. }
  841. // Step 5c: Compound term matching — find classes whose name contains 2+
  842. // query terms at ANY position (not just CamelCase boundaries).
  843. // The CamelCase step above requires idx > 0, which misses classes that
  844. // START with a query term (e.g., "SearchShardsRequest" starts with "Search").
  845. // For multi-word queries, a class matching multiple query terms in its name
  846. // is almost certainly relevant regardless of position.
  847. if (symbolsFromQuery.length >= 2) {
  848. // Collect ALL LIKE results per term (reusing findNodesByNameSubstring)
  849. // but without the CamelCase boundary or prefix exclusion filters.
  850. const compoundTermMap = new Map<string, { node: Node; terms: Set<string> }>();
  851. for (const sym of symbolsFromQuery) {
  852. const titleCased = sym.charAt(0).toUpperCase() + sym.slice(1).toLowerCase();
  853. if (titleCased.length < 3) continue;
  854. const likeResults = [
  855. ...this.queries.findNodesByNameSubstring(titleCased, {
  856. limit: 200,
  857. kinds: camelDefinitionKinds,
  858. excludePrefix: false,
  859. }),
  860. // Same separate callable batch as Step 5b (#1196).
  861. ...this.queries.findNodesByNameSubstring(titleCased, {
  862. limit: 200,
  863. kinds: camelCallableKinds,
  864. excludePrefix: false,
  865. }),
  866. ];
  867. for (const r of likeResults) {
  868. if (searchIdSet.has(r.node.id)) continue;
  869. if (isTestFile(r.node.filePath) && !isTestQuery) continue;
  870. const entry = compoundTermMap.get(r.node.id);
  871. if (entry) {
  872. entry.terms.add(titleCased);
  873. } else {
  874. compoundTermMap.set(r.node.id, { node: r.node, terms: new Set([titleCased]) });
  875. }
  876. }
  877. }
  878. // Keep only nodes matching 2+ distinct terms
  879. const compoundResults: SearchResult[] = [];
  880. for (const [, entry] of compoundTermMap) {
  881. if (entry.terms.size >= 2) {
  882. const pathScore = scorePathRelevance(
  883. entry.node.filePath,
  884. query,
  885. undefined,
  886. this.isDeprioritized(entry.node.filePath),
  887. );
  888. const brevityBonus = Math.max(0, 6 - entry.node.name.length / 8);
  889. compoundResults.push({
  890. node: entry.node,
  891. score: 10 + (entry.terms.size - 1) * 20 + pathScore + brevityBonus,
  892. });
  893. }
  894. }
  895. compoundResults.sort((a, b) => b.score - a.score);
  896. const maxCompound = Math.ceil(opts.searchLimit / 2);
  897. for (const r of compoundResults.slice(0, maxCompound)) {
  898. searchResults.push(r);
  899. searchIdSet.add(r.node.id);
  900. }
  901. }
  902. }
  903. // Final sort and truncation — all search channels (exact, text, CamelCase,
  904. // compound) have now contributed. Sort by score so multi-term matches from
  905. // later steps can outrank dampened single-term matches from earlier steps.
  906. searchResults.sort((a, b) => b.score - a.score);
  907. searchResults = searchResults.slice(0, opts.searchLimit * 3);
  908. // Filter by minimum score
  909. let filteredResults = searchResults.filter((r) => r.score >= opts.minScore);
  910. // Resolve imports/exports to their actual definitions
  911. // If someone searches "terminal" and finds `import { TerminalPanel }`,
  912. // they want the TerminalPanel class, not the import statement
  913. filteredResults = this.resolveImportsToDefinitions(filteredResults);
  914. // Cap entry points so traversal budget isn't spread too thin.
  915. // With 36 entry points and maxNodes=120, each gets only 3 nodes — useless.
  916. // Cap to searchLimit so each entry point gets a meaningful traversal budget.
  917. if (filteredResults.length > opts.searchLimit) {
  918. filteredResults = filteredResults.slice(0, opts.searchLimit);
  919. }
  920. // Confidence signal for the honest-handoff footer (consumed in buildContext).
  921. // A multi-term prose query that resolves only to isolated common-word matches
  922. // — no entry point corroborated by 2+ distinct query terms, and none a
  923. // distinctive identifier the user explicitly named — is LOW confidence: the
  924. // results are best-effort, not a located answer, so the agent should be told
  925. // to drill in with explore/trace rather than trust the list as comprehensive.
  926. // Single-keyword and symbol-name queries are exempt (their single match IS the
  927. // answer), so the handoff never fires on them.
  928. let confidence: 'high' | 'low' = 'high';
  929. const confTerms = extractSearchTerms(query, { stems: false }).filter(t => t.length >= 3);
  930. if (confTerms.length >= 2 && filteredResults.length > 0) {
  931. const distinctive = new Set(
  932. symbolsFromQuery.filter(isDistinctiveIdentifier).map(s => s.toLowerCase())
  933. );
  934. const anyStrong = filteredResults.some(r => {
  935. if (distinctive.has(r.node.name.toLowerCase())) return true;
  936. const nameLower = r.node.name.toLowerCase();
  937. const dirSegs = path.dirname(r.node.filePath).toLowerCase().split('/');
  938. let hits = 0;
  939. for (const t of confTerms) {
  940. if (nameLower.includes(t) || dirSegs.includes(t)) {
  941. if (++hits >= 2) return true;
  942. }
  943. }
  944. return false;
  945. });
  946. if (!anyStrong) confidence = 'low';
  947. }
  948. // Add entry points to subgraph
  949. for (const result of filteredResults) {
  950. nodes.set(result.node.id, result.node);
  951. roots.push(result.node.id);
  952. }
  953. // Expand type hierarchy for class/interface entry points.
  954. // BFS often exhausts its per-entry-point budget on contained methods
  955. // before reaching extends/implements neighbors. This dedicated step
  956. // ensures subclasses and superclasses always appear in results.
  957. // Budget: up to maxNodes/4 hierarchy nodes to avoid flooding.
  958. const typeHierarchyKinds = new Set<string>(['class', 'interface', 'struct', 'union', 'trait', 'protocol']);
  959. const maxHierarchyNodes = Math.ceil(opts.maxNodes / 4);
  960. let hierarchyNodesAdded = 0;
  961. for (const result of filteredResults) {
  962. if (hierarchyNodesAdded >= maxHierarchyNodes) break;
  963. if (typeHierarchyKinds.has(result.node.kind)) {
  964. const hierarchy = this.traverser.getTypeHierarchy(result.node.id);
  965. for (const [id, node] of hierarchy.nodes) {
  966. if (!nodes.has(id)) {
  967. nodes.set(id, node);
  968. hierarchyNodesAdded++;
  969. }
  970. }
  971. for (const edge of hierarchy.edges) {
  972. const exists = edges.some(
  973. (e) => e.source === edge.source && e.target === edge.target && e.kind === edge.kind
  974. );
  975. if (!exists) {
  976. edges.push(edge);
  977. }
  978. }
  979. }
  980. }
  981. // Pass 2: expand hierarchy of newly-discovered parent types to find siblings.
  982. // E.g., InternalEngine → Engine (parent, from pass 1) → ReadOnlyEngine (sibling).
  983. if (hierarchyNodesAdded > 0) {
  984. const pass2Candidates = [...nodes.values()].filter(
  985. n => typeHierarchyKinds.has(n.kind) && !roots.includes(n.id)
  986. );
  987. for (const candidate of pass2Candidates) {
  988. if (hierarchyNodesAdded >= maxHierarchyNodes) break;
  989. const siblingHierarchy = this.traverser.getTypeHierarchy(candidate.id);
  990. for (const [id, node] of siblingHierarchy.nodes) {
  991. if (!nodes.has(id) && hierarchyNodesAdded < maxHierarchyNodes) {
  992. nodes.set(id, node);
  993. hierarchyNodesAdded++;
  994. }
  995. }
  996. for (const edge of siblingHierarchy.edges) {
  997. if (nodes.has(edge.source) && nodes.has(edge.target)) {
  998. const exists = edges.some(
  999. (e) => e.source === edge.source && e.target === edge.target && e.kind === edge.kind
  1000. );
  1001. if (!exists) {
  1002. edges.push(edge);
  1003. }
  1004. }
  1005. }
  1006. }
  1007. }
  1008. // Traverse from each entry point
  1009. for (const result of filteredResults) {
  1010. const traversalResult = this.traverser.traverseBFS(result.node.id, {
  1011. maxDepth: opts.traversalDepth,
  1012. edgeKinds: opts.edgeKinds && opts.edgeKinds.length > 0 ? opts.edgeKinds : undefined,
  1013. nodeKinds: opts.nodeKinds && opts.nodeKinds.length > 0 ? opts.nodeKinds : undefined,
  1014. direction: 'both',
  1015. limit: Math.ceil(opts.maxNodes / Math.max(1, filteredResults.length)),
  1016. });
  1017. // Merge nodes
  1018. for (const [id, node] of traversalResult.nodes) {
  1019. if (!nodes.has(id)) {
  1020. nodes.set(id, node);
  1021. }
  1022. }
  1023. // Merge edges (avoid duplicates)
  1024. for (const edge of traversalResult.edges) {
  1025. const exists = edges.some(
  1026. (e) => e.source === edge.source && e.target === edge.target && e.kind === edge.kind
  1027. );
  1028. if (!exists) {
  1029. edges.push(edge);
  1030. }
  1031. }
  1032. }
  1033. // Trim to max nodes if needed
  1034. let finalNodes = nodes;
  1035. let finalEdges = edges;
  1036. if (nodes.size > opts.maxNodes) {
  1037. // Prioritize entry points and their direct neighbors
  1038. const priorityIds = new Set(roots);
  1039. for (const edge of edges) {
  1040. if (priorityIds.has(edge.source)) {
  1041. priorityIds.add(edge.target);
  1042. }
  1043. if (priorityIds.has(edge.target)) {
  1044. priorityIds.add(edge.source);
  1045. }
  1046. }
  1047. // Keep priority nodes, then fill remaining slots
  1048. finalNodes = new Map<string, Node>();
  1049. for (const id of priorityIds) {
  1050. const node = nodes.get(id);
  1051. if (node && finalNodes.size < opts.maxNodes) {
  1052. finalNodes.set(id, node);
  1053. }
  1054. }
  1055. // Fill remaining from other nodes
  1056. for (const [id, node] of nodes) {
  1057. if (finalNodes.size >= opts.maxNodes) break;
  1058. if (!finalNodes.has(id)) {
  1059. finalNodes.set(id, node);
  1060. }
  1061. }
  1062. // Filter edges to only include kept nodes
  1063. finalEdges = edges.filter(
  1064. (e) => finalNodes.has(e.source) && finalNodes.has(e.target)
  1065. );
  1066. }
  1067. // Per-file diversity cap: prevent any single file from monopolizing the
  1068. // node budget. When BFS traverses from a method, it follows `contains`
  1069. // to the parent class, then back down to all sibling methods. With
  1070. // multiple entry points in the same class, one file can consume 30-40%
  1071. // of maxNodes. Cap each file to ~20% to ensure cross-file diversity.
  1072. const maxPerFile = Math.max(5, Math.ceil(opts.maxNodes * 0.2));
  1073. const fileCounts = new Map<string, string[]>();
  1074. for (const [id, node] of finalNodes) {
  1075. const ids = fileCounts.get(node.filePath) || [];
  1076. ids.push(id);
  1077. fileCounts.set(node.filePath, ids);
  1078. }
  1079. const rootSet = new Set(roots);
  1080. for (const [, nodeIds] of fileCounts) {
  1081. if (nodeIds.length <= maxPerFile) continue;
  1082. // Sort: entry points first, then classes/interfaces, then others
  1083. const kindPriority: Record<string, number> = {
  1084. class: 3, interface: 3, struct: 3, trait: 3, protocol: 3, enum: 3,
  1085. method: 1, function: 1, property: 0, field: 0, variable: 0,
  1086. };
  1087. nodeIds.sort((a, b) => {
  1088. const aRoot = rootSet.has(a) ? 10 : 0;
  1089. const bRoot = rootSet.has(b) ? 10 : 0;
  1090. const aKind = kindPriority[finalNodes.get(a)!.kind] ?? 0;
  1091. const bKind = kindPriority[finalNodes.get(b)!.kind] ?? 0;
  1092. return (bRoot + bKind) - (aRoot + aKind);
  1093. });
  1094. // Remove excess nodes (keep the highest-priority ones)
  1095. for (const id of nodeIds.slice(maxPerFile)) {
  1096. finalNodes.delete(id);
  1097. }
  1098. }
  1099. // Non-production node cap: limit test/sample/integration/example files to
  1100. // at most 15% of the budget. Many codebases have dozens of near-identical
  1101. // test implementations (e.g., 6 Guard classes in integration tests) that
  1102. // individually survive score dampening but collectively flood the result.
  1103. // Test entry points are NOT exempt — they should be evicted too.
  1104. if (!isTestQuery) {
  1105. const maxNonProd = Math.max(3, Math.ceil(opts.maxNodes * 0.15));
  1106. const nonProdIds: string[] = [];
  1107. for (const [id, node] of finalNodes) {
  1108. if (isTestFile(node.filePath)) {
  1109. nonProdIds.push(id);
  1110. }
  1111. }
  1112. if (nonProdIds.length > maxNonProd) {
  1113. for (const id of nonProdIds.slice(maxNonProd)) {
  1114. finalNodes.delete(id);
  1115. // Also remove from roots — test file entry points shouldn't anchor results
  1116. const rootIdx = roots.indexOf(id);
  1117. if (rootIdx !== -1) roots.splice(rootIdx, 1);
  1118. }
  1119. }
  1120. }
  1121. // Re-filter edges after per-file and non-production caps
  1122. finalEdges = finalEdges.filter(
  1123. (e) => finalNodes.has(e.source) && finalNodes.has(e.target)
  1124. );
  1125. // Edge recovery: BFS with many entry points leaves most nodes disconnected.
  1126. // Discover edges between already-selected nodes to recover connectivity.
  1127. const recoveryKinds: EdgeKind[] = ['calls', 'extends', 'implements', 'references', 'overrides', 'navigates'];
  1128. const recoveredEdges = this.queries.findEdgesBetweenNodes(
  1129. [...finalNodes.keys()],
  1130. recoveryKinds,
  1131. );
  1132. const existingEdgeKeys = new Set(
  1133. finalEdges.map((e) => `${e.source}:${e.target}:${e.kind}`)
  1134. );
  1135. for (const edge of recoveredEdges) {
  1136. const key = `${edge.source}:${edge.target}:${edge.kind}`;
  1137. if (!existingEdgeKeys.has(key)) {
  1138. finalEdges.push(edge);
  1139. existingEdgeKeys.add(key);
  1140. }
  1141. }
  1142. return { nodes: finalNodes, edges: finalEdges, roots, confidence };
  1143. }
  1144. /**
  1145. * Get the source code for a node
  1146. *
  1147. * Reads the file and extracts the code between startLine and endLine.
  1148. *
  1149. * @param nodeId - ID of the node
  1150. * @returns Code string or null if not found
  1151. */
  1152. async getCode(nodeId: string): Promise<string | null> {
  1153. const node = this.queries.getNodeById(nodeId);
  1154. if (!node) {
  1155. return null;
  1156. }
  1157. return this.extractNodeCode(node);
  1158. }
  1159. /**
  1160. * Extract code from a node's source file
  1161. */
  1162. private async extractNodeCode(node: Node): Promise<string | null> {
  1163. // SECURITY (#383): a config-leaf node's on-disk line is `key = <secret>`.
  1164. // Return the KEY only — never read the value off disk. This closes the
  1165. // includeCode / buildContext code-block path, mirroring the explore source
  1166. // renderer; an agent that genuinely needs a value can read the file itself.
  1167. if (isConfigLeafNode(node)) {
  1168. return node.signature || node.qualifiedName || node.name;
  1169. }
  1170. const filePath = validatePathWithinRoot(this.projectRoot, node.filePath);
  1171. if (!filePath || !fs.existsSync(filePath)) {
  1172. return null;
  1173. }
  1174. try {
  1175. const content = fs.readFileSync(filePath, 'utf-8');
  1176. const lines = content.split('\n');
  1177. // Extract lines (1-indexed to 0-indexed)
  1178. const startIdx = Math.max(0, node.startLine - 1);
  1179. const endIdx = Math.min(lines.length, node.endLine);
  1180. return lines.slice(startIdx, endIdx).join('\n');
  1181. } catch (error) {
  1182. logDebug('Failed to extract code from node', { nodeId: node.id, filePath: node.filePath, error: String(error) });
  1183. return null;
  1184. }
  1185. }
  1186. /**
  1187. * Get entry points from a subgraph (the root nodes)
  1188. */
  1189. private getEntryPoints(subgraph: Subgraph): Node[] {
  1190. return subgraph.roots
  1191. .map((id) => subgraph.nodes.get(id))
  1192. .filter((n): n is Node => n !== undefined);
  1193. }
  1194. /**
  1195. * Extract code blocks for key nodes in the subgraph
  1196. */
  1197. private async extractCodeBlocks(
  1198. subgraph: Subgraph,
  1199. maxBlocks: number,
  1200. maxBlockSize: number
  1201. ): Promise<CodeBlock[]> {
  1202. const blocks: CodeBlock[] = [];
  1203. // Prioritize entry points, then functions/methods
  1204. const priorityNodes: Node[] = [];
  1205. // First: entry points
  1206. for (const id of subgraph.roots) {
  1207. const node = subgraph.nodes.get(id);
  1208. if (node) {
  1209. priorityNodes.push(node);
  1210. }
  1211. }
  1212. // Then: functions and methods
  1213. for (const node of subgraph.nodes.values()) {
  1214. if (!subgraph.roots.includes(node.id)) {
  1215. if (node.kind === 'function' || node.kind === 'method') {
  1216. priorityNodes.push(node);
  1217. }
  1218. }
  1219. }
  1220. // Then: classes
  1221. for (const node of subgraph.nodes.values()) {
  1222. if (!subgraph.roots.includes(node.id)) {
  1223. if (node.kind === 'class') {
  1224. priorityNodes.push(node);
  1225. }
  1226. }
  1227. }
  1228. // Extract code for priority nodes
  1229. for (const node of priorityNodes) {
  1230. if (blocks.length >= maxBlocks) break;
  1231. const code = await this.extractNodeCode(node);
  1232. if (code) {
  1233. // Truncate if too long. Language-neutral marker (no `//` — not a
  1234. // comment in Python, Ruby, etc.); this renders inside a fenced
  1235. // source block whose language varies.
  1236. const truncated = code.length > maxBlockSize
  1237. ? code.slice(0, maxBlockSize) + '\n... (truncated) ...'
  1238. : code;
  1239. blocks.push({
  1240. content: truncated,
  1241. filePath: node.filePath,
  1242. startLine: node.startLine,
  1243. endLine: node.endLine,
  1244. language: node.language,
  1245. node,
  1246. });
  1247. }
  1248. }
  1249. return blocks;
  1250. }
  1251. /**
  1252. * Get unique files from a subgraph
  1253. */
  1254. private getRelatedFiles(subgraph: Subgraph): string[] {
  1255. const files = new Set<string>();
  1256. for (const node of subgraph.nodes.values()) {
  1257. files.add(node.filePath);
  1258. }
  1259. return Array.from(files).sort();
  1260. }
  1261. /**
  1262. * Generate a summary of the context
  1263. */
  1264. private generateSummary(_query: string, subgraph: Subgraph, entryPoints: Node[]): string {
  1265. const nodeCount = subgraph.nodes.size;
  1266. const edgeCount = subgraph.edges.length;
  1267. const files = this.getRelatedFiles(subgraph);
  1268. const entryPointNames = entryPoints
  1269. .slice(0, 3)
  1270. .map((n) => n.name)
  1271. .join(', ');
  1272. const remaining = entryPoints.length > 3 ? ` and ${entryPoints.length - 3} more` : '';
  1273. return `Found ${nodeCount} relevant code symbols across ${files.length} files. ` +
  1274. `Key entry points: ${entryPointNames}${remaining}. ` +
  1275. `${edgeCount} relationships identified.`;
  1276. }
  1277. /**
  1278. * Resolve import/export nodes to their actual definitions
  1279. *
  1280. * When search returns `import { TerminalPanel }`, users want the TerminalPanel
  1281. * class definition, not the import statement. This follows the `imports` edge
  1282. * to find and return the actual definition instead.
  1283. *
  1284. * @param results - Search results that may include import/export nodes
  1285. * @returns Results with imports resolved to definitions where possible
  1286. */
  1287. private resolveImportsToDefinitions(results: SearchResult[]): SearchResult[] {
  1288. const resolved: SearchResult[] = [];
  1289. const seenIds = new Set<string>();
  1290. for (const result of results) {
  1291. const { node, score } = result;
  1292. // If it's not an import/export, keep it as-is
  1293. if (node.kind !== 'import' && node.kind !== 'export') {
  1294. if (!seenIds.has(node.id)) {
  1295. seenIds.add(node.id);
  1296. resolved.push(result);
  1297. }
  1298. continue;
  1299. }
  1300. // For imports/exports, try to find what they reference
  1301. // Imports have outgoing 'imports' edges to the definition
  1302. // Exports have outgoing 'exports' edges to the definition
  1303. const edgeKind = node.kind === 'import' ? 'imports' : 'exports';
  1304. const outgoingEdges = this.queries.getOutgoingEdges(node.id, [edgeKind as EdgeKind]);
  1305. let foundDefinition = false;
  1306. for (const edge of outgoingEdges) {
  1307. const targetNode = this.queries.getNodeById(edge.target);
  1308. if (targetNode && !seenIds.has(targetNode.id)) {
  1309. // Found the definition - use it instead of the import
  1310. seenIds.add(targetNode.id);
  1311. resolved.push({
  1312. node: targetNode,
  1313. score: score, // Preserve the original score
  1314. });
  1315. foundDefinition = true;
  1316. logDebug('Resolved import to definition', {
  1317. import: node.name,
  1318. definition: targetNode.name,
  1319. kind: targetNode.kind,
  1320. });
  1321. }
  1322. }
  1323. // If we couldn't resolve the import, skip it (it's low-value on its own)
  1324. if (!foundDefinition) {
  1325. logDebug('Skipping unresolved import', { name: node.name, file: node.filePath });
  1326. }
  1327. }
  1328. return resolved;
  1329. }
  1330. }
  1331. /**
  1332. * Create a context builder
  1333. */
  1334. export function createContextBuilder(
  1335. projectRoot: string,
  1336. queries: QueryBuilder,
  1337. traverser: GraphTraverser
  1338. ): ContextBuilder {
  1339. return new ContextBuilder(projectRoot, queries, traverser);
  1340. }
  1341. // Re-export formatter
  1342. export { formatContextAsMarkdown, formatContextAsJson } from './formatter';