tools.ts 194 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155
  1. /**
  2. * MCP Tool Definitions
  3. *
  4. * Defines the tools exposed by the CodeGraph MCP server.
  5. */
  6. import type CodeGraph from '../index';
  7. import { findNearestCodeGraphRoot } from '../directory';
  8. // Lazy-load the heavy CodeGraph chain off the MCP startup path — see the same
  9. // helper in engine.ts. ToolHandler must load to answer tools/list (static
  10. // schemas), but it must NOT drag in sqlite/query layers before the daemon binds;
  11. // CodeGraph is pulled in only when a tool actually opens a project. require() is
  12. // sync + cached (CommonJS build).
  13. const loadCodeGraph = (): typeof import('../index').default =>
  14. (require('../index') as typeof import('../index')).default;
  15. import {
  16. detectWorktreeIndexMismatch,
  17. worktreeMismatchWarning,
  18. worktreeMismatchNotice,
  19. type WorktreeIndexMismatch,
  20. } from '../sync/worktree';
  21. import type { PendingFile } from '../sync';
  22. import type { Node, Edge, SearchResult, Subgraph, NodeKind } from '../types';
  23. import { isTestFile, normalizeNameToken } from '../search/query-utils';
  24. import {
  25. existsSync,
  26. readFileSync,
  27. } from 'fs';
  28. import { clamp, validatePathWithinRoot, validateProjectPath, isConfigLeafNode, CONFIG_LEAF_LANGUAGES } from '../utils';
  29. import { isGeneratedFile } from '../extraction/generated-detection';
  30. import { scanDynamicDispatch } from './dynamic-boundaries';
  31. /**
  32. * An expected, recoverable "codegraph can't serve this" condition — most
  33. * importantly a project with no index. The dispatch catch converts these to
  34. * SUCCESS-shaped responses (guidance text, NO isError): an `isError: true`
  35. * early in a session teaches the agent the toolset is broken and it stops
  36. * calling codegraph entirely (observed repeatedly), which is exactly wrong
  37. * for conditions the agent can simply work around (use built-in tools for
  38. * that codebase / pass projectPath). isError is reserved for "stop trying"
  39. * cases: security refusals ({@link PathRefusalError}) and genuine
  40. * malfunctions.
  41. */
  42. export class NotIndexedError extends Error {}
  43. /**
  44. * A security refusal (sensitive system path). Stays `isError: true` WITHOUT
  45. * retry guidance — abandoning this path is the desired agent reaction.
  46. */
  47. export class PathRefusalError extends Error {}
  48. import { resolve as resolvePath } from 'path';
  49. /** Maximum output length to prevent context bloat (characters) */
  50. const MAX_OUTPUT_LENGTH = 15000;
  51. /**
  52. * Maximum length for free-form string inputs (query, task, symbol).
  53. * Bounds memory and CPU when a buggy or hostile MCP client sends a
  54. * huge payload — without this an attacker could ship a 100MB string
  55. * and force a full FTS5 scan / OOM the server. 10 000 characters is
  56. * far beyond any realistic legitimate query.
  57. */
  58. const MAX_INPUT_LENGTH = 10_000;
  59. /**
  60. * Maximum length for path-like string inputs (projectPath, path
  61. * filter, glob pattern). Paths beyond a few thousand chars are
  62. * never legitimate and signal abuse or a bug upstream.
  63. */
  64. const MAX_PATH_LENGTH = 4_096;
  65. /**
  66. * Rust path roots that have no file-system equivalent — `crate` is the
  67. * current crate, `super` is the parent module, `self` is the current
  68. * module. Used by `matchesSymbol` to strip these before file-path
  69. * matching so `crate::configurator::stage_apply::run` resolves the
  70. * same as `configurator::stage_apply::run`.
  71. */
  72. const RUST_PATH_PREFIXES = new Set(['crate', 'super', 'self']);
  73. /**
  74. * Node kinds that contain other symbols. For these, `codegraph_node` with
  75. * `includeCode=true` returns a structural outline (member names + signatures
  76. * + line numbers) instead of the full body, which for a large class is a
  77. * multi-thousand-character wall of source that bloats the agent's context.
  78. */
  79. const CONTAINER_NODE_KINDS = new Set<NodeKind>([
  80. 'class', 'struct', 'interface', 'trait', 'protocol', 'enum', 'namespace', 'module',
  81. ]);
  82. /** Last `::` / `.` / `/`-separated segment of a qualified symbol. */
  83. function lastQualifierPart(symbol: string): string {
  84. const parts = symbol.split(/::|[./]/).filter((p) => p.length > 0);
  85. return parts[parts.length - 1] ?? symbol;
  86. }
  87. /**
  88. * Calculate the recommended number of codegraph_explore calls based on project size.
  89. * Larger codebases need more exploration calls to cover their surface area,
  90. * but smaller ones should use fewer to avoid unnecessary overhead.
  91. */
  92. export function getExploreBudget(fileCount: number): number {
  93. if (fileCount < 500) return 1;
  94. if (fileCount < 5000) return 2;
  95. if (fileCount < 15000) return 3;
  96. if (fileCount < 25000) return 4;
  97. return 5;
  98. }
  99. /**
  100. * Adaptive output budget for `codegraph_explore`, scaled to project size.
  101. *
  102. * Smaller codebases get a tighter total cap, fewer default files, smaller
  103. * per-file cap, and tighter clustering — so a focused query on a 100-file
  104. * project doesn't dump a whole file's worth of source into the agent's
  105. * context. Larger codebases keep the generous defaults because the
  106. * agent's native discovery cost (grep + find + many Reads) genuinely
  107. * dwarfs a fat explore call at that scale.
  108. *
  109. * Meta-text (relationships map, "additional relevant files" list,
  110. * completeness signal, budget note) is gated off for tiny projects
  111. * where one rich call is the whole story and the extra prose is just
  112. * overhead.
  113. *
  114. * Tier breakpoints mirror `getExploreBudget` so a project sits in the
  115. * same tier across both knobs.
  116. */
  117. export interface ExploreOutputBudget {
  118. /** Hard cap on total output characters. */
  119. maxOutputChars: number;
  120. /** Default `maxFiles` when the caller didn't specify one. */
  121. defaultMaxFiles: number;
  122. /** Cap on contiguous source returned per file (across all its clusters). */
  123. maxCharsPerFile: number;
  124. /** Cluster gap threshold in lines — tighter clustering on small projects. */
  125. gapThreshold: number;
  126. /** Max symbols listed in the per-file header (`#### path — sym(kind), ...`). */
  127. maxSymbolsInFileHeader: number;
  128. /** Max edges shown per relationship kind in the Relationships section. */
  129. maxEdgesPerRelationshipKind: number;
  130. /** Include the "Relationships" section. */
  131. includeRelationships: boolean;
  132. /** Include the "Additional relevant files (not shown)" trailing list. */
  133. includeAdditionalFiles: boolean;
  134. /** Include the "Complete source code is included above…" reminder. */
  135. includeCompletenessSignal: boolean;
  136. /** Include the explore-budget reminder at the end. */
  137. includeBudgetNote: boolean;
  138. /**
  139. * Hard-drop test/spec/icon/i18n files from the relevant-file set unless
  140. * the query itself mentions tests. Today they're only deprioritized in
  141. * the sort, which on tiny repos still lets one slip into the top N (e.g.
  142. * cobra's `command_test.go` displaced `args.go` and contributed ~10KB of
  143. * pure noise to "How does cobra parse commands?"). Off by default; on
  144. * for the very-tiny tier where one slip dominates the budget.
  145. */
  146. excludeLowValueFiles: boolean;
  147. }
  148. export function getExploreOutputBudget(fileCount: number): ExploreOutputBudget {
  149. // Tiered budget, scaled to project size. The budget is a CEILING (relevance
  150. // still gates WHAT is included), and it MUST stay under the agent's INLINE
  151. // tool-result cap (~25K chars). Above that, the host externalizes the result
  152. // to a file the agent then Reads back — re-introducing a read AND the
  153. // cache-write cost — which is exactly what a 35K vscode explore did in the
  154. // n=4 README A/B. So even large repos cap at ~24K: the answer is the handful
  155. // of ~100-line flow windows the agent would have grep-located and read (it
  156. // natively reads ~6–9 files, median 100-line ranges), NOT a sprawl of 12
  157. // files. Concentration onto the flow emerges from this cap + the named-file-
  158. // first sort dropping peripheral files. Invariant: a larger tier must never
  159. // get a smaller `maxCharsPerFile` than a smaller tier.
  160. if (fileCount < 150) {
  161. return {
  162. // ITER3: revert iter2's aggressive body shrink (forced Read fallback —
  163. // the per-file 2.5K cap pushed the agent to Read instead of node).
  164. // Back to the iter1 shape (13K/4/3.8K) but keep the test-file
  165. // hard-exclude. The cost lever for this tier lives in steering the
  166. // agent to stop after 1-2 calls, not in this budget.
  167. maxOutputChars: 13000,
  168. defaultMaxFiles: 4,
  169. maxCharsPerFile: 3800,
  170. gapThreshold: 7,
  171. maxSymbolsInFileHeader: 5,
  172. maxEdgesPerRelationshipKind: 4,
  173. includeRelationships: false,
  174. includeAdditionalFiles: false,
  175. includeCompletenessSignal: false,
  176. includeBudgetNote: false,
  177. excludeLowValueFiles: true,
  178. };
  179. }
  180. if (fileCount < 500) {
  181. return {
  182. // ITER3: same revert/keep-filter pattern as <150.
  183. maxOutputChars: 18000,
  184. defaultMaxFiles: 5,
  185. maxCharsPerFile: 3800,
  186. gapThreshold: 8,
  187. maxSymbolsInFileHeader: 6,
  188. maxEdgesPerRelationshipKind: 6,
  189. includeRelationships: false,
  190. includeAdditionalFiles: false,
  191. includeCompletenessSignal: false,
  192. includeBudgetNote: false,
  193. excludeLowValueFiles: true,
  194. };
  195. }
  196. if (fileCount < 5000) {
  197. return {
  198. // ~150-line per-file window (the native read unit) × ~6 files, capped at
  199. // the ~24K inline ceiling so the response is never externalized. Per-file
  200. // stays ≥ the <500 tier (3800) — monotonic.
  201. maxOutputChars: 24000,
  202. defaultMaxFiles: 8,
  203. maxCharsPerFile: 6500,
  204. gapThreshold: 12,
  205. maxSymbolsInFileHeader: 10,
  206. maxEdgesPerRelationshipKind: 10,
  207. includeRelationships: true,
  208. includeAdditionalFiles: true,
  209. includeCompletenessSignal: true,
  210. includeBudgetNote: true,
  211. excludeLowValueFiles: false,
  212. };
  213. }
  214. // Large + very-large repos: SAME ~24K inline ceiling (a bigger response just
  215. // externalizes — see vscode). More files indexed → more CALLS via
  216. // getExploreBudget, not a bigger single response. Per-file 7000 (≥ smaller
  217. // tiers) gives the central file a ~180-line orientation window.
  218. if (fileCount < 15000) {
  219. return {
  220. maxOutputChars: 24000,
  221. defaultMaxFiles: 8,
  222. maxCharsPerFile: 7000,
  223. gapThreshold: 15,
  224. maxSymbolsInFileHeader: 15,
  225. maxEdgesPerRelationshipKind: 15,
  226. includeRelationships: true,
  227. includeAdditionalFiles: true,
  228. includeCompletenessSignal: true,
  229. includeBudgetNote: true,
  230. excludeLowValueFiles: false,
  231. };
  232. }
  233. return {
  234. maxOutputChars: 24000,
  235. defaultMaxFiles: 8,
  236. maxCharsPerFile: 7000,
  237. gapThreshold: 15,
  238. maxSymbolsInFileHeader: 15,
  239. maxEdgesPerRelationshipKind: 15,
  240. includeRelationships: true,
  241. includeAdditionalFiles: true,
  242. includeCompletenessSignal: true,
  243. includeBudgetNote: true,
  244. excludeLowValueFiles: false,
  245. };
  246. }
  247. /**
  248. * Whether `codegraph_explore` should prefix source lines with their line
  249. * numbers (cat -n style: `<num>\t<code>`).
  250. *
  251. * Line numbers let the agent cite `file:line` straight from the explore
  252. * payload instead of re-Reading the file just to find a line number — the
  253. * dominant residual cost on precise-tracing questions (#185 follow-up).
  254. *
  255. * Defaults ON. Set `CODEGRAPH_EXPLORE_LINENUMS=0` to disable (used by the
  256. * A/B harness to measure the payload-cost vs. read-savings tradeoff).
  257. */
  258. function exploreLineNumbersEnabled(): boolean {
  259. return process.env.CODEGRAPH_EXPLORE_LINENUMS !== '0';
  260. }
  261. /**
  262. * Adaptive explore sizing (default ON). `codegraph_explore` skeletonizes OFF-SPINE
  263. * polymorphic-sibling files — a file whose class is one of ≥3 interchangeable
  264. * implementations of a shared interface (e.g. OkHttp's `: Interceptor` classes) —
  265. * to class + member signatures (bodies elided), keeping the on-spine exemplar full.
  266. * This sizes the response to the answer instead of the budget cap on sibling-heavy
  267. * flows (OkHttp interceptor-chain explore 28.5k→16.6k, ~28% cheaper than native
  268. * search, reads flat). It is PROVABLY INERT elsewhere: distinct pipeline steps (no
  269. * ≥3-implementer supertype, e.g. Excalidraw's `renderStaticScene`) and on-spine
  270. * files keep full source — output is byte-identical to shipped on excalidraw /
  271. * tokio / django / vscode / gin. Set `CODEGRAPH_ADAPTIVE_EXPLORE=0` to disable.
  272. */
  273. function adaptiveExploreEnabled(): boolean {
  274. return process.env.CODEGRAPH_ADAPTIVE_EXPLORE !== '0' && process.env.CODEGRAPH_ADAPTIVE_EXPLORE !== 'false';
  275. }
  276. /**
  277. * Prefix each line of a source slice with its 1-based line number, matching
  278. * the Read tool's `cat -n` convention (number + tab) so the agent treats it
  279. * the same way it treats Read output.
  280. *
  281. * @param slice contiguous source text (already extracted from the file)
  282. * @param firstLineNumber the 1-based line number of the slice's first line
  283. */
  284. function numberSourceLines(slice: string, firstLineNumber: number): string {
  285. const out: string[] = [];
  286. const split = slice.split('\n');
  287. for (let i = 0; i < split.length; i++) {
  288. out.push(`${firstLineNumber + i}\t${split[i]}`);
  289. }
  290. return out.join('\n');
  291. }
  292. /**
  293. * Per-file staleness banner emitted at the top of a tool response when the
  294. * file watcher has pending events for files referenced by the response.
  295. * The agent uses this to fall back to Read for those specific files
  296. * without waiting for the debounced sync (issue #403).
  297. */
  298. export function formatStaleBanner(stale: PendingFile[]): string {
  299. const now = Date.now();
  300. const lines = stale.map((p) => {
  301. const ageMs = Math.max(0, now - p.lastSeenMs);
  302. const label = p.indexing ? 'indexing in progress' : 'pending sync';
  303. return ` - ${p.path} (edited ${ageMs}ms ago, ${label})`;
  304. });
  305. return (
  306. '⚠️ Some files referenced below were edited since the last index sync — ' +
  307. 'their codegraph entries may be stale:\n' +
  308. lines.join('\n') +
  309. '\nFor accurate content of those specific files, Read them directly. ' +
  310. 'The rest of this response is fresh.'
  311. );
  312. }
  313. /**
  314. * Compact footer listing pending files that are NOT referenced in this
  315. * response. Gives the agent a complete project-wide freshness picture
  316. * without bloating the main banner.
  317. */
  318. export function formatStaleFooter(stale: PendingFile[]): string {
  319. const MAX = 5;
  320. const now = Date.now();
  321. const shown = stale.slice(0, MAX);
  322. const lines = shown.map((p) => {
  323. const ageMs = Math.max(0, now - p.lastSeenMs);
  324. return ` - ${p.path} (edited ${ageMs}ms ago)`;
  325. });
  326. const more = stale.length > MAX ? `\n - …and ${stale.length - MAX} more` : '';
  327. return (
  328. `(Note: ${stale.length} file(s) elsewhere in this project are pending index ` +
  329. `sync but were not referenced above:\n${lines.join('\n')}${more})`
  330. );
  331. }
  332. /**
  333. * Whole-index degradation banner (issue #876). Emitted at the top of a read
  334. * tool response when live watching has permanently stopped — at which point
  335. * `getPendingFiles()` is empty, so the per-file banner above can't fire even
  336. * though the index is now FROZEN and silently drifting stale. Leads with the
  337. * agent-actionable instruction (Read directly) and carries the reason, which
  338. * already names the operator remedy (`codegraph sync` / git hooks).
  339. */
  340. export function formatDegradedBanner(reason: string | null): string {
  341. return (
  342. '⚠️ CodeGraph auto-sync is DISABLED — live file watching stopped, so the index is ' +
  343. 'frozen and any file edited since then is stale here. Read files directly to confirm ' +
  344. 'current content before relying on it.' +
  345. (reason ? `\n Reason: ${reason}` : '')
  346. );
  347. }
  348. /**
  349. * MCP Tool definition
  350. */
  351. export interface ToolDefinition {
  352. name: string;
  353. description: string;
  354. inputSchema: {
  355. type: 'object';
  356. properties: Record<string, PropertySchema>;
  357. required?: string[];
  358. };
  359. }
  360. interface PropertySchema {
  361. type: string;
  362. description: string;
  363. enum?: string[];
  364. default?: unknown;
  365. }
  366. /**
  367. * Tool execution result
  368. */
  369. export interface ToolResult {
  370. content: Array<{
  371. type: 'text';
  372. text: string;
  373. }>;
  374. isError?: boolean;
  375. }
  376. /**
  377. * Common projectPath property for cross-project queries
  378. */
  379. const projectPathProperty: PropertySchema = {
  380. type: 'string',
  381. description: 'Path to a different project with .codegraph/ initialized. If omitted, uses current project. Use this to query other codebases.',
  382. };
  383. /**
  384. * All CodeGraph MCP tools
  385. *
  386. * Designed for minimal context usage - use codegraph_explore as the primary tool
  387. * (one call usually answers the whole question), and only use other tools for
  388. * targeted follow-up queries.
  389. *
  390. * All tools support cross-project queries via the optional `projectPath` parameter.
  391. */
  392. export const tools: ToolDefinition[] = [
  393. {
  394. name: 'codegraph_search',
  395. description: 'Quick symbol search by name. Returns locations only (no code). Use codegraph_explore instead to get the actual source / understand an area in one call.',
  396. inputSchema: {
  397. type: 'object',
  398. properties: {
  399. query: {
  400. type: 'string',
  401. description: 'Symbol name or partial name (e.g., "auth", "signIn", "UserService")',
  402. },
  403. kind: {
  404. type: 'string',
  405. description: 'Filter by node kind',
  406. enum: ['function', 'method', 'class', 'interface', 'type', 'variable', 'route', 'component'],
  407. },
  408. limit: {
  409. type: 'number',
  410. description: 'Maximum results (default: 10)',
  411. default: 10,
  412. },
  413. projectPath: projectPathProperty,
  414. },
  415. required: ['query'],
  416. },
  417. },
  418. {
  419. name: 'codegraph_callers',
  420. description: 'List functions that call <symbol>. For the full flow, use codegraph_explore.',
  421. inputSchema: {
  422. type: 'object',
  423. properties: {
  424. symbol: {
  425. type: 'string',
  426. description: 'Name of the function, method, or class to find callers for',
  427. },
  428. file: {
  429. type: 'string',
  430. description: 'Narrow to the definition in this file (path or suffix) when several same-named symbols exist (e.g. one UserService per app in a monorepo)',
  431. },
  432. limit: {
  433. type: 'number',
  434. description: 'Maximum number of callers to return (default: 20)',
  435. default: 20,
  436. },
  437. projectPath: projectPathProperty,
  438. },
  439. required: ['symbol'],
  440. },
  441. },
  442. {
  443. name: 'codegraph_callees',
  444. description: 'List functions that <symbol> calls. For the full flow, use codegraph_explore.',
  445. inputSchema: {
  446. type: 'object',
  447. properties: {
  448. symbol: {
  449. type: 'string',
  450. description: 'Name of the function, method, or class to find callees for',
  451. },
  452. file: {
  453. type: 'string',
  454. description: 'Narrow to the definition in this file (path or suffix) when several same-named symbols exist',
  455. },
  456. limit: {
  457. type: 'number',
  458. description: 'Maximum number of callees to return (default: 20)',
  459. default: 20,
  460. },
  461. projectPath: projectPathProperty,
  462. },
  463. required: ['symbol'],
  464. },
  465. },
  466. {
  467. name: 'codegraph_impact',
  468. description: 'List symbols affected by changing <symbol>. Use before a refactor.',
  469. inputSchema: {
  470. type: 'object',
  471. properties: {
  472. symbol: {
  473. type: 'string',
  474. description: 'Name of the symbol to analyze impact for',
  475. },
  476. file: {
  477. type: 'string',
  478. description: 'Narrow to the definition in this file (path or suffix) when several same-named symbols exist',
  479. },
  480. depth: {
  481. type: 'number',
  482. description: 'How many levels of dependencies to traverse (default: 2)',
  483. default: 2,
  484. },
  485. projectPath: projectPathProperty,
  486. },
  487. required: ['symbol'],
  488. },
  489. },
  490. {
  491. name: 'codegraph_node',
  492. description: 'Two modes. (1) READ A FILE — use INSTEAD of the Read tool: pass `file` (a path or basename) with no `symbol` and it returns that file\'s current on-disk source with line numbers, exactly the shape Read gives you (`<n>\\t<line>`, safe to Edit from), narrowable with `offset`/`limit` just like Read — PLUS a one-line note of which files depend on it. Same bytes as Read, faster (served from the index), with the blast radius attached. Use it whenever you would Read a source file. (2) ONE SYMBOL you can name — its location, signature, verbatim source (includeCode=true) and caller/callee trail in one call, so before changing it you see what calls it and what your edit would break. For an AMBIGUOUS name it returns EVERY matching definition\'s body in one call (so you never Read a file to find the right overload); pass `file`/`line` to pin one. Use codegraph_explore for several related symbols or the full flow.',
  493. inputSchema: {
  494. type: 'object',
  495. properties: {
  496. symbol: {
  497. type: 'string',
  498. description: 'Name of the symbol to read (symbol mode). Omit it and pass `file` alone to read a whole file like Read.',
  499. },
  500. includeCode: {
  501. type: 'boolean',
  502. description: 'Symbol mode: include the symbol\'s full body (default: false). Ignored in file mode, which always returns source unless `symbolsOnly` is set.',
  503. default: false,
  504. },
  505. file: {
  506. type: 'string',
  507. description: 'A file path or basename (e.g. "harness.rs", "src/auth/session.ts"). Pass it ALONE (no symbol) to READ the file like the Read tool — its full source with line numbers + which files depend on it. Or pass it WITH a symbol to disambiguate an overloaded name to the definition in this file.',
  508. },
  509. offset: {
  510. type: 'number',
  511. description: 'File mode: 1-based line to start reading from, exactly like Read\'s offset. Defaults to the start of the file.',
  512. },
  513. limit: {
  514. type: 'number',
  515. description: 'File mode: maximum number of lines to return, exactly like Read\'s limit. Defaults to the whole file (capped at 2000 lines, like Read).',
  516. },
  517. symbolsOnly: {
  518. type: 'boolean',
  519. description: 'File mode: return just the file\'s symbol map + dependents (a cheap structural overview) instead of its source.',
  520. default: false,
  521. },
  522. line: {
  523. type: 'number',
  524. description: 'Symbol mode only: disambiguate to the definition at/around this line (use with the file:line a trail showed you).',
  525. },
  526. projectPath: projectPathProperty,
  527. },
  528. required: [],
  529. },
  530. },
  531. {
  532. name: 'codegraph_explore',
  533. description: 'PRIMARY TOOL — call FIRST for almost any question OR before an edit: how does X work, architecture, a bug, where/what is X, surveying an area, or the symbols you are about to change. Returns the verbatim source of the relevant symbols grouped by file in ONE capped call (Read-equivalent — treat the shown source as already Read; do NOT re-open those files), plus the call path among them. Query can be a natural-language question OR a bag of symbol/file names. Usually the ONLY call you need — more accurate context, in far fewer tokens and round-trips than a search/Read/Grep loop.',
  534. inputSchema: {
  535. type: 'object',
  536. properties: {
  537. query: {
  538. type: 'string',
  539. description: 'Symbol names, file names, or short code terms to explore (e.g., "AuthService loginUser session-manager", "GraphTraverser BFS impact traversal.ts"). For a flow question, name the symbols spanning the flow (e.g. "mutateElement renderScene"). A natural-language question works too — no prior codegraph_search needed.',
  540. },
  541. maxFiles: {
  542. type: 'number',
  543. description: 'Maximum number of files to include source code from (default: 12)',
  544. default: 12,
  545. },
  546. projectPath: projectPathProperty,
  547. },
  548. required: ['query'],
  549. },
  550. },
  551. {
  552. name: 'codegraph_status',
  553. description: 'Index health check (files / nodes / edges). Skip unless debugging.',
  554. inputSchema: {
  555. type: 'object',
  556. properties: {
  557. projectPath: projectPathProperty,
  558. },
  559. },
  560. },
  561. {
  562. name: 'codegraph_files',
  563. description: 'Indexed file tree with language + symbol counts. Faster than Glob for project layout.',
  564. inputSchema: {
  565. type: 'object',
  566. properties: {
  567. path: {
  568. type: 'string',
  569. description: 'Filter to files under this directory path (e.g., "src/components"). Returns all files if not specified.',
  570. },
  571. pattern: {
  572. type: 'string',
  573. description: 'Filter files matching this glob pattern (e.g., "*.tsx", "**/*.test.ts")',
  574. },
  575. format: {
  576. type: 'string',
  577. description: 'Output format: "tree" (hierarchical, default), "flat" (simple list), "grouped" (by language)',
  578. enum: ['tree', 'flat', 'grouped'],
  579. default: 'tree',
  580. },
  581. includeMetadata: {
  582. type: 'boolean',
  583. description: 'Include file metadata like language and symbol count (default: true)',
  584. default: true,
  585. },
  586. maxDepth: {
  587. type: 'number',
  588. description: 'Maximum directory depth to show (default: unlimited)',
  589. },
  590. projectPath: projectPathProperty,
  591. },
  592. },
  593. },
  594. ];
  595. /**
  596. * Allowlist-filtered tool definitions WITHOUT an engine — the static surface the
  597. * proxy answers `tools/list` with before any project is open. Mirrors
  598. * `ToolHandler.getTools()` in the no-CodeGraph case (the dynamic per-repo budget
  599. * note in a description only adds once `cg` is loaded; the schemas are static).
  600. */
  601. export function getStaticTools(): ToolDefinition[] {
  602. const raw = process.env.CODEGRAPH_MCP_TOOLS;
  603. if (!raw || !raw.trim()) {
  604. return tools.filter(t => DEFAULT_MCP_TOOLS.has(t.name.replace(/^codegraph_/, '')));
  605. }
  606. const allow = new Set(raw.split(',').map(s => s.trim().replace(/^codegraph_/, '')).filter(Boolean));
  607. return allow.size ? tools.filter(t => allow.has(t.name.replace(/^codegraph_/, ''))) : tools;
  608. }
  609. /**
  610. * The MCP tools served by DEFAULT (short names). Pared to ONLY `codegraph_explore`
  611. * — the single tool that reliably earns its place: one capped call returns the
  612. * verbatim source of the relevant symbols grouped by file. Every other tool is a
  613. * narrower slice of what explore already does, and presence itself steers
  614. * mis-picks, so they are no longer LISTED to agents.
  615. *
  616. * The other defined tools (`node`, `search`, `callers`, plus callees/impact/files/
  617. * status) remain fully functional — handlers stay, the library API and CLI are
  618. * untouched, and `CODEGRAPH_MCP_TOOLS=explore,node,...` re-enables any of them.
  619. */
  620. const DEFAULT_MCP_TOOLS = new Set(['explore']);
  621. /**
  622. * Tool handler that executes tools against a CodeGraph instance
  623. *
  624. * Supports cross-project queries via the projectPath parameter.
  625. * Other projects are opened on-demand and cached for performance.
  626. */
  627. export class ToolHandler {
  628. // Cache of opened CodeGraph instances for cross-project queries
  629. private projectCache: Map<string, CodeGraph> = new Map();
  630. // The directory the server last searched for a default project. Surfaced in
  631. // the "not initialized" error so users can see why detection missed.
  632. private defaultProjectHint: string | null = null;
  633. // Per-start-path cache of the git worktree/index mismatch (issue #155). The
  634. // mismatch is a fixed property of (where the request came from → which
  635. // .codegraph/ it resolves to), so the up-to-two `git rev-parse` spawns run
  636. // once and every later tool call reuses the result — never shelling out to
  637. // git on the hot path. `undefined` = not computed yet; `null` = no mismatch.
  638. private worktreeMismatchCache: Map<string, WorktreeIndexMismatch | null> = new Map();
  639. // Gate that the MCP engine pokes after `cg.open()` so the first tool call
  640. // blocks on the post-open filesystem reconcile (catch-up sync). Without
  641. // this, a tool call that races past `catchUpSync()` serves rows for files
  642. // that were deleted (or edited) while no MCP server was running — and the
  643. // per-file staleness banner can't help, because `getPendingFiles()` is
  644. // populated by the watcher, not by catch-up. Cleared on first await so
  645. // subsequent calls don't pay any cost.
  646. private catchUpGate: Promise<void> | null = null;
  647. constructor(private cg: CodeGraph | null) {}
  648. /**
  649. * Update the default CodeGraph instance (e.g. after lazy initialization)
  650. */
  651. setDefaultCodeGraph(cg: CodeGraph): void {
  652. this.cg = cg;
  653. }
  654. /**
  655. * Engine-only: register the catch-up sync promise so the next `execute()`
  656. * call awaits it before serving. The handler swallows rejections (the
  657. * engine logs them) so a sync failure never propagates as a tool error;
  658. * we still want to serve a best-effort result over the same potentially-
  659. * stale data, which is what would have happened without the gate.
  660. */
  661. setCatchUpGate(p: Promise<void> | null): void {
  662. this.catchUpGate = p;
  663. }
  664. /**
  665. * Record the directory the server tried to resolve the default project from.
  666. * Used only to make the "no default project" error actionable.
  667. */
  668. setDefaultProjectHint(searchedPath: string): void {
  669. this.defaultProjectHint = searchedPath;
  670. }
  671. /**
  672. * Whether a default CodeGraph instance is available
  673. */
  674. hasDefaultCodeGraph(): boolean {
  675. return this.cg !== null;
  676. }
  677. /**
  678. * Optional allowlist of exposed tools, parsed from the CODEGRAPH_MCP_TOOLS
  679. * env var (comma-separated short names, e.g. "trace,search,node,context").
  680. * Unset/empty → every tool is exposed. Lets an operator (or an A/B harness)
  681. * trim the tool surface without rebuilding the client config; the ablated
  682. * tool is then truly absent from ListTools rather than merely denied on call.
  683. * Matching is on the short form, so "node" and "codegraph_node" both work.
  684. */
  685. private toolAllowlist(): Set<string> | null {
  686. const raw = process.env.CODEGRAPH_MCP_TOOLS;
  687. if (!raw || !raw.trim()) return null;
  688. const short = (s: string) => s.trim().replace(/^codegraph_/, '');
  689. const set = new Set(raw.split(',').map(short).filter(Boolean));
  690. return set.size ? set : null;
  691. }
  692. /** Whether a tool name passes the CODEGRAPH_MCP_TOOLS allowlist (if any). */
  693. private isToolAllowed(name: string): boolean {
  694. const allow = this.toolAllowlist();
  695. return !allow || allow.has(name.replace(/^codegraph_/, ''));
  696. }
  697. /**
  698. * Get tool definitions with dynamic descriptions based on project size.
  699. * The codegraph_explore tool description includes a budget recommendation
  700. * scaled to the number of indexed files. Honors the CODEGRAPH_MCP_TOOLS
  701. * allowlist so a trimmed surface is reflected in ListTools.
  702. */
  703. getTools(): ToolDefinition[] {
  704. const allow = this.toolAllowlist();
  705. // No explicit allowlist → the default 4-tool surface (see
  706. // DEFAULT_MCP_TOOLS for the evidence). An allowlist replaces the
  707. // default entirely, so any defined tool can be re-enabled.
  708. let visible = allow
  709. ? tools.filter(t => allow.has(t.name.replace(/^codegraph_/, '')))
  710. : tools.filter(t => DEFAULT_MCP_TOOLS.has(t.name.replace(/^codegraph_/, '')));
  711. if (!this.cg) return visible;
  712. try {
  713. const stats = this.cg.getStats();
  714. const budget = getExploreBudget(stats.fileCount);
  715. // Tiny-repo tool gating: on projects under TINY_REPO_FILE_THRESHOLD
  716. // files, only expose the core trio (search, node, explore) — one
  717. // below even the 4-tool default: at this scale callers, too, reduces
  718. // to one grep. (Historical note: the audit below ran when context and
  719. // trace still existed; its "5 core tools" are today's trio.)
  720. //
  721. // n=2 audits ruled out cutting below 5 tools:
  722. // - 3-tool gate (search + context + trace): cost regressed on
  723. // cobra/ky/sinatra. The agent fell back to raw Reads to cover
  724. // what codegraph_node + codegraph_explore would have answered.
  725. // - 1-tool gate (search only): catastrophic regression — express
  726. // went from -43% WIN to +107% LOSS. With only search, the agent
  727. // can't navigate the call graph structurally and reads everything.
  728. //
  729. // 5 is the empirical lower bound. Tools beyond search/context/
  730. // node/explore/trace pay overhead that the agent doesn't recoup
  731. // on tiny-repo flow questions.
  732. // ITER4: raise threshold 150 → 500 so single-file frameworks
  733. // (sinatra at 159, slim_framework around 200) also get the
  734. // 5-tool surface. The empirical 5-tool floor was set on <150
  735. // probes; iter3 measurement showed sinatra is structurally the
  736. // SAME problem as cobra (single-file WITHOUT-arm Read wins),
  737. // so it deserves the same gating.
  738. const TINY_REPO_FILE_THRESHOLD = 500;
  739. const TINY_REPO_CORE_TOOLS = new Set([
  740. 'codegraph_explore',
  741. 'codegraph_search',
  742. 'codegraph_node',
  743. ]);
  744. if (stats.fileCount < TINY_REPO_FILE_THRESHOLD) {
  745. visible = visible.filter(t => TINY_REPO_CORE_TOOLS.has(t.name));
  746. }
  747. return visible.map(tool => {
  748. if (tool.name === 'codegraph_explore') {
  749. return {
  750. ...tool,
  751. description: `${tool.description} Budget: make at most ${budget} calls for this project (${stats.fileCount.toLocaleString()} files indexed).`,
  752. };
  753. }
  754. return tool;
  755. });
  756. } catch {
  757. return visible;
  758. }
  759. }
  760. /**
  761. * Get CodeGraph instance for a project
  762. *
  763. * If projectPath is provided, opens that project's CodeGraph (cached).
  764. * Otherwise returns the default CodeGraph instance.
  765. *
  766. * Walks up parent directories to find the nearest .codegraph/ folder,
  767. * similar to how git finds .git/ directories.
  768. */
  769. private getCodeGraph(projectPath?: string): CodeGraph {
  770. if (!projectPath) {
  771. if (!this.cg) {
  772. const searched = this.defaultProjectHint ?? process.cwd();
  773. throw new NotIndexedError(
  774. 'No CodeGraph project is loaded for this session.\n' +
  775. `Searched for a .codegraph/ directory starting from: ${searched}\n` +
  776. 'If this project IS indexed, this is a working-directory detection issue: ' +
  777. "the MCP client launched the server outside your project and didn't report the " +
  778. 'workspace root. Fix it either way:\n' +
  779. ' • Pass projectPath to the tool call, e.g. projectPath: "/absolute/path/to/your/project"\n' +
  780. ' • Or add --path to the server\'s MCP config args: ["serve", "--mcp", "--path", "/absolute/path/to/your/project"]\n' +
  781. 'If the project simply has no index, continue with your built-in tools (Read/Grep/Glob) ' +
  782. "and don't call codegraph again this session — the user can run 'codegraph init' to enable it."
  783. );
  784. }
  785. return this.cg;
  786. }
  787. // Reject sensitive system directories before opening. Only validate a
  788. // path that actually exists — a nested or not-yet-created sub-path of a
  789. // real project must still be allowed to resolve UP to its .codegraph/
  790. // root below (issue #238), so we don't run the existence-checking
  791. // validator on paths that are meant to walk up.
  792. if (existsSync(projectPath)) {
  793. const pathError = validateProjectPath(projectPath);
  794. if (pathError) {
  795. throw new PathRefusalError(pathError);
  796. }
  797. }
  798. // Always RE-RESOLVE the nearest .codegraph/ from the input path. The walk
  799. // is cheap (a few existsSync up the tree) and is the only thing that
  800. // notices a path whose index root CHANGED since it was first seen — most
  801. // importantly a git worktree that gained its own .codegraph/ after the
  802. // (long-lived) server first resolved it up to the parent checkout. We used
  803. // to short-circuit on a `projectCache[projectPath]` entry before resolving,
  804. // which pinned that first resolution for the server's whole lifetime, so a
  805. // worktree kept being served the parent checkout's index until restart
  806. // (#926). The DB connection itself is still cached (by resolved root,
  807. // below), so re-resolving costs only the stat walk, never a reopen.
  808. const resolvedRoot = findNearestCodeGraphRoot(projectPath);
  809. if (!resolvedRoot) {
  810. throw new NotIndexedError(
  811. `The project at ${projectPath} isn't indexed with codegraph (no .codegraph/ directory found ` +
  812. 'walking up from it), so codegraph cannot query it. Use your built-in tools (Read/Grep/Glob) ' +
  813. "for that codebase instead, and don't call codegraph for it again this session. " +
  814. "Indexing is the user's decision — they can run 'codegraph init' in that project to enable it."
  815. );
  816. }
  817. // If the path resolves to the default project, reuse the already-open
  818. // default instance rather than opening a SECOND connection to the same DB.
  819. // A duplicate connection serializes reads against the watcher's auto-sync
  820. // writes; on the wasm backend (no WAL) that surfaces as intermittent
  821. // "database is locked" on concurrent tool calls. See issue #238. The
  822. // default instance is owned/closed by the server, so it's never cached.
  823. if (this.cg && this.cg.getProjectRoot() === resolvedRoot) {
  824. return this.cg;
  825. }
  826. // Cache the open DB connection by RESOLVED ROOT only — never by the input
  827. // path. One key per instance means closeAll() closes each exactly once, and
  828. // a changed resolution maps to a different entry instead of a stale hit.
  829. const cached = this.projectCache.get(resolvedRoot);
  830. if (cached) return cached;
  831. const cg = loadCodeGraph().openSync(resolvedRoot);
  832. this.projectCache.set(resolvedRoot, cg);
  833. return cg;
  834. }
  835. /**
  836. * Close all cached project connections
  837. */
  838. closeAll(): void {
  839. for (const cg of this.projectCache.values()) {
  840. cg.close();
  841. }
  842. this.projectCache.clear();
  843. this.worktreeMismatchCache.clear();
  844. }
  845. /**
  846. * Validate that a value is a non-empty string within length bounds.
  847. *
  848. * The `maxLength` cap protects against MCP clients that ship huge
  849. * payloads (10MB+ query strings either by accident or maliciously).
  850. * Without this, a single oversized input can pin the FTS5 index or
  851. * exhaust memory before any real work runs.
  852. */
  853. private validateString(
  854. value: unknown,
  855. name: string,
  856. maxLength: number = MAX_INPUT_LENGTH
  857. ): string | ToolResult {
  858. if (typeof value !== 'string' || value.length === 0) {
  859. return this.errorResult(`${name} must be a non-empty string`);
  860. }
  861. if (value.length > maxLength) {
  862. return this.errorResult(
  863. `${name} exceeds maximum length of ${maxLength} characters (got ${value.length})`
  864. );
  865. }
  866. return value;
  867. }
  868. /**
  869. * Validate an optional path-like string input. Returns the value if
  870. * valid (or undefined), or a ToolResult with the error.
  871. */
  872. private validateOptionalPath(
  873. value: unknown,
  874. name: string
  875. ): string | undefined | ToolResult {
  876. if (value === undefined || value === null) return undefined;
  877. if (typeof value !== 'string') {
  878. return this.errorResult(`${name} must be a string`);
  879. }
  880. if (value.length > MAX_PATH_LENGTH) {
  881. return this.errorResult(
  882. `${name} exceeds maximum length of ${MAX_PATH_LENGTH} characters (got ${value.length})`
  883. );
  884. }
  885. return value;
  886. }
  887. /**
  888. * Cached git worktree/index mismatch for a tool call's effective project.
  889. *
  890. * The "effective project" is what the request targets: an explicit
  891. * `projectPath` arg, else the directory the server resolved its default
  892. * project from (`defaultProjectHint`), else cwd. Memoized per start path —
  893. * see `worktreeMismatchCache`. Best-effort: if the project can't be resolved
  894. * (e.g. nothing initialized yet), it reports "no mismatch" so a tool is never
  895. * broken by this check.
  896. */
  897. private worktreeMismatchFor(projectPath?: string): WorktreeIndexMismatch | null {
  898. const startPath = projectPath ?? this.defaultProjectHint ?? process.cwd();
  899. // The verdict depends on BOTH the start path AND the index root it resolves
  900. // to, so the cache must be keyed on the pair. Resolve the index root first
  901. // (cheap — getCodeGraph re-walks to the nearest .codegraph/, no git), then
  902. // key on `(startPath, indexRoot)`. The moment that root changes — most
  903. // importantly when a git worktree gains its own index and the walk-up stops
  904. // there instead of at the parent checkout — the key changes and the verdict
  905. // is recomputed, instead of serving the stale "borrowed the parent's index"
  906. // warning for the server's whole lifetime. Keying on startPath alone pinned
  907. // that first verdict until restart (#926).
  908. let indexRoot: string;
  909. try {
  910. indexRoot = this.getCodeGraph(projectPath).getProjectRoot();
  911. } catch {
  912. // No resolvable project (or any other resolution error) → nothing to warn.
  913. return null;
  914. }
  915. const cacheKey = `${startPath}\u0000${indexRoot}`;
  916. const cached = this.worktreeMismatchCache.get(cacheKey);
  917. if (cached !== undefined) return cached;
  918. const mismatch = detectWorktreeIndexMismatch(startPath, indexRoot);
  919. this.worktreeMismatchCache.set(cacheKey, mismatch);
  920. return mismatch;
  921. }
  922. /**
  923. * Prefix a successful read-tool result with a compact worktree-mismatch
  924. * notice when the resolved index belongs to a different git working tree than
  925. * the caller's (issue #155). Without this, an agent in a nested worktree
  926. * silently trusts main-branch results. No-op on error results and when there
  927. * is no mismatch. `codegraph_status` is excluded — it embeds its own verbose
  928. * warning — so it stays out of this path.
  929. */
  930. private withWorktreeNotice(result: ToolResult, projectPath?: string): ToolResult {
  931. if (result.isError) return result;
  932. const mismatch = this.worktreeMismatchFor(projectPath);
  933. if (!mismatch) return result;
  934. const notice = worktreeMismatchNotice(mismatch);
  935. const [first, ...rest] = result.content;
  936. if (first && first.type === 'text') {
  937. return { ...result, content: [{ type: 'text', text: `${notice}\n\n${first.text}` }, ...rest] };
  938. }
  939. return result;
  940. }
  941. /**
  942. * Annotate a successful read-tool result with per-file staleness — the
  943. * non-blocking answer to issue #403. The file watcher tracks every event
  944. * it sees per path; here we intersect "files referenced in this response"
  945. * against that pending set and prepend a compact banner so the agent can
  946. * fall back to Read for those *specific* files without waiting for the
  947. * debounced sync to fire. Other pending files in the project (not
  948. * referenced by this response) get a small footer so the agent has a
  949. * complete picture without bloating the banner.
  950. *
  951. * Cost when nothing is pending — the common case — is one boolean check.
  952. * No I/O, no parsing of markdown beyond a per-pending-file substring scan.
  953. */
  954. private withStalenessNotice(result: ToolResult, projectPath?: string): ToolResult {
  955. if (result.isError) return result;
  956. let cg: CodeGraph;
  957. try {
  958. cg = this.getCodeGraph(projectPath);
  959. } catch {
  960. return result; // no default project — leave as is
  961. }
  962. // Cross-project `projectPath` calls open a cached CodeGraph WITHOUT a
  963. // watcher (watchers are only attached to the default session project).
  964. // When the cross-project path happens to be the same project as the
  965. // default cg, the cached instance is the wrong one — its pendingFiles is
  966. // permanently empty. Detect the equal-path case and prefer the default
  967. // cg so the staleness signal still fires when an agent passes the
  968. // explicit projectPath form of its own project.
  969. if (this.cg && cg !== this.cg) {
  970. try {
  971. const sameProject =
  972. resolvePath(this.cg.getProjectRoot()) === resolvePath(cg.getProjectRoot());
  973. if (sameProject) cg = this.cg;
  974. } catch {
  975. /* getProjectRoot may throw on a closed instance — leave cg as is */
  976. }
  977. }
  978. // Whole-index degradation (#876): once live watching has permanently
  979. // stopped, getPendingFiles() is empty so the per-file banner below can't
  980. // fire — but the index is now FROZEN and silently drifting stale. Surface
  981. // one global notice instead, so the agent Reads for current content rather
  982. // than trusting a response off a no-longer-updating index. (Cross-project
  983. // calls open a watcher-less CodeGraph, so this is false there — correct: we
  984. // only know degraded state for the default session project.)
  985. let degraded = false;
  986. try {
  987. degraded = cg.isWatcherDegraded?.() ?? false;
  988. } catch {
  989. degraded = false;
  990. }
  991. if (degraded) {
  992. const [head, ...tail] = result.content;
  993. if (!head || head.type !== 'text') return result;
  994. let reason: string | null = null;
  995. try {
  996. reason = cg.getWatcherDegradedReason?.() ?? null;
  997. } catch {
  998. reason = null;
  999. }
  1000. const composed = `${formatDegradedBanner(reason)}\n\n${head.text}`;
  1001. return { ...result, content: [{ type: 'text', text: composed }, ...tail] };
  1002. }
  1003. // Defensive: some test fakes inject a partial CodeGraph stub without the
  1004. // newer pending-files API. Treat missing/throwing as "no pending files."
  1005. let pending: PendingFile[] = [];
  1006. try {
  1007. pending = cg.getPendingFiles?.() ?? [];
  1008. } catch {
  1009. return result;
  1010. }
  1011. if (pending.length === 0) return result;
  1012. const [first, ...rest] = result.content;
  1013. if (!first || first.type !== 'text') return result;
  1014. const text = first.text;
  1015. const inResponse: PendingFile[] = [];
  1016. const elsewhere: PendingFile[] = [];
  1017. for (const p of pending) {
  1018. // Substring match against the project-relative POSIX path — that's
  1019. // exactly the format both the watcher and every codegraph response
  1020. // emit, so a plain includes() is sufficient and avoids regex pitfalls.
  1021. if (text.includes(p.path)) inResponse.push(p);
  1022. else elsewhere.push(p);
  1023. }
  1024. let banner = '';
  1025. if (inResponse.length > 0) {
  1026. banner = formatStaleBanner(inResponse);
  1027. }
  1028. let footer = '';
  1029. if (elsewhere.length > 0) {
  1030. footer = formatStaleFooter(elsewhere);
  1031. }
  1032. if (!banner && !footer) return result;
  1033. const composed = [banner, text, footer].filter(Boolean).join('\n\n');
  1034. return { ...result, content: [{ type: 'text', text: composed }, ...rest] };
  1035. }
  1036. /**
  1037. * Execute a tool by name
  1038. */
  1039. async execute(toolName: string, args: Record<string, unknown>): Promise<ToolResult> {
  1040. try {
  1041. // Block the first tool call on the engine's post-open reconcile so we
  1042. // never serve rows for files deleted/edited while no MCP server was
  1043. // running. The gate is cleared after first await — subsequent calls
  1044. // pay nothing. Catch-up failures are logged by the engine; we
  1045. // proceed regardless so a transient sync error never breaks tools.
  1046. if (this.catchUpGate) {
  1047. const gate = this.catchUpGate;
  1048. this.catchUpGate = null;
  1049. try { await gate; } catch { /* engine already logged */ }
  1050. }
  1051. // Honor the optional tool allowlist (CODEGRAPH_MCP_TOOLS): a trimmed
  1052. // surface rejects ablated tools defensively even if a client cached them.
  1053. if (!this.isToolAllowed(toolName)) {
  1054. return this.errorResult(`Tool ${toolName} is disabled via CODEGRAPH_MCP_TOOLS`);
  1055. }
  1056. // Cross-cutting input validation. All tools accept an optional
  1057. // `projectPath` and most accept either `query`, `task`, or
  1058. // `symbol` — bound their lengths centrally so individual handlers
  1059. // can stay focused on tool-specific logic.
  1060. const pathCheck = this.validateOptionalPath(args.projectPath, 'projectPath');
  1061. if (typeof pathCheck === 'object' && pathCheck !== undefined) {
  1062. return pathCheck;
  1063. }
  1064. // The `path` and `pattern` properties used by codegraph_files are
  1065. // also path-shaped — apply the same cap.
  1066. if (args.path !== undefined) {
  1067. const check = this.validateOptionalPath(args.path, 'path');
  1068. if (typeof check === 'object' && check !== undefined) return check;
  1069. }
  1070. if (args.pattern !== undefined) {
  1071. const check = this.validateOptionalPath(args.pattern, 'pattern');
  1072. if (typeof check === 'object' && check !== undefined) return check;
  1073. }
  1074. // Read tools resolve through a single result variable so cross-cutting
  1075. // notices — worktree-index mismatch (issue #155) and per-file
  1076. // staleness (issue #403) — can be applied in one place. status embeds
  1077. // its own verbose worktree warning but still flows through the
  1078. // staleness wrapper so its pending-files section stays consistent
  1079. // with what the read tools surface.
  1080. let result: ToolResult;
  1081. switch (toolName) {
  1082. case 'codegraph_search':
  1083. result = await this.handleSearch(args); break;
  1084. case 'codegraph_callers':
  1085. result = await this.handleCallers(args); break;
  1086. case 'codegraph_callees':
  1087. result = await this.handleCallees(args); break;
  1088. case 'codegraph_impact':
  1089. result = await this.handleImpact(args); break;
  1090. case 'codegraph_explore':
  1091. result = await this.handleExplore(args); break;
  1092. case 'codegraph_node':
  1093. result = await this.handleNode(args); break;
  1094. case 'codegraph_status':
  1095. // status embeds the pending-files list as a first-class section
  1096. // (see handleStatus), so we skip the auto-banner wrapper here to
  1097. // avoid duplicating the same info at the top of the response.
  1098. return await this.handleStatus(args);
  1099. case 'codegraph_files':
  1100. result = await this.handleFiles(args); break;
  1101. default:
  1102. return this.errorResult(`Unknown tool: ${toolName}`);
  1103. }
  1104. const withWorktree = this.withWorktreeNotice(result, args.projectPath as string | undefined);
  1105. return this.withStalenessNotice(withWorktree, args.projectPath as string | undefined);
  1106. } catch (err) {
  1107. // Expected condition, not a malfunction: answer as a SUCCESS so the
  1108. // agent keeps trusting the toolset for projects that ARE indexed.
  1109. // (An isError here teaches session-long abandonment — see NotIndexedError.)
  1110. if (err instanceof NotIndexedError) {
  1111. return this.textResult(err.message);
  1112. }
  1113. // Security refusal: a clean error, no retry encouragement.
  1114. if (err instanceof PathRefusalError) {
  1115. return this.errorResult(err.message);
  1116. }
  1117. return this.errorResult(
  1118. `Tool execution failed: ${err instanceof Error ? err.message : String(err)}. ` +
  1119. 'This is an internal codegraph error — retry the call once; if it persists, ' +
  1120. 'continue without codegraph for this task.'
  1121. );
  1122. }
  1123. }
  1124. /**
  1125. * Handle codegraph_search
  1126. */
  1127. private async handleSearch(args: Record<string, unknown>): Promise<ToolResult> {
  1128. const query = this.validateString(args.query, 'query');
  1129. if (typeof query !== 'string') return query;
  1130. const cg = this.getCodeGraph(args.projectPath as string | undefined);
  1131. const rawKind = args.kind as string | undefined;
  1132. // The schema enum says 'type' (what agents naturally reach for); the
  1133. // NodeKind is 'type_alias'. Without the mapping, kind: "type" silently
  1134. // matched nothing — a filter value we advertise must work.
  1135. const kind = rawKind === 'type' ? 'type_alias' : rawKind;
  1136. const rawLimit = Number(args.limit) || 10;
  1137. const limit = clamp(rawLimit, 1, 100);
  1138. const results = cg.searchNodes(query, {
  1139. limit,
  1140. kinds: kind ? [kind as NodeKind] : undefined,
  1141. });
  1142. if (results.length === 0) {
  1143. return this.textResult(`No results found for "${query}"`);
  1144. }
  1145. // Down-rank generated files within the FTS-returned set so a search
  1146. // for "Send" surfaces the hand-written keeper before .pb.go stubs
  1147. // that share the name. Stable: only reorders generated vs. not.
  1148. const ranked = [...results].sort((a, b) => {
  1149. const aGen = isGeneratedFile(a.node.filePath) ? 1 : 0;
  1150. const bGen = isGeneratedFile(b.node.filePath) ? 1 : 0;
  1151. return aGen - bGen;
  1152. });
  1153. const formatted = this.formatSearchResults(ranked);
  1154. return this.textResult(this.truncateOutput(formatted));
  1155. }
  1156. /**
  1157. * Group symbol matches into DISTINCT DEFINITIONS — one group per
  1158. * (filePath, qualifiedName), so same-file overloads stay together while
  1159. * unrelated same-named classes across a monorepo's apps (#764: one
  1160. * `UserService` per NestJS app) are kept apart. Optionally narrowed by a
  1161. * `file` path/suffix first.
  1162. */
  1163. private groupDefinitions(
  1164. nodes: Node[],
  1165. fileFilter: string | undefined
  1166. ): { groups: Node[][]; filteredOut: boolean } {
  1167. let pool = nodes;
  1168. let filteredOut = false;
  1169. if (fileFilter) {
  1170. const wanted = fileFilter.replace(/^\.\//, '');
  1171. const narrowed = pool.filter(
  1172. (n) => n.filePath === wanted || n.filePath.endsWith(wanted) || n.filePath.endsWith(`/${wanted}`)
  1173. );
  1174. if (narrowed.length > 0) {
  1175. pool = narrowed;
  1176. } else {
  1177. filteredOut = true;
  1178. }
  1179. }
  1180. const byDef = new Map<string, Node[]>();
  1181. for (const n of pool) {
  1182. const key = `${n.filePath}|${n.qualifiedName}`;
  1183. const group = byDef.get(key);
  1184. if (group) group.push(n);
  1185. else byDef.set(key, [n]);
  1186. }
  1187. return { groups: [...byDef.values()], filteredOut };
  1188. }
  1189. /** Section heading for one distinct definition in grouped output. */
  1190. private definitionHeading(group: Node[]): string {
  1191. const head = group[0]!;
  1192. const line = head.startLine ? `:${head.startLine}` : '';
  1193. return `### ${head.qualifiedName} (${head.kind}) — ${head.filePath}${line}`;
  1194. }
  1195. /**
  1196. * Handle codegraph_callers
  1197. */
  1198. private async handleCallers(args: Record<string, unknown>): Promise<ToolResult> {
  1199. const symbol = this.validateString(args.symbol, 'symbol');
  1200. if (typeof symbol !== 'string') return symbol;
  1201. const cg = this.getCodeGraph(args.projectPath as string | undefined);
  1202. const limit = clamp((args.limit as number) || 20, 1, 100);
  1203. const fileFilter = typeof args.file === 'string' ? args.file : undefined;
  1204. const allMatches = this.findAllSymbols(cg, symbol);
  1205. if (allMatches.nodes.length === 0) {
  1206. return this.textResult(`Symbol "${symbol}" not found in the codebase`);
  1207. }
  1208. const { groups, filteredOut } = this.groupDefinitions(allMatches.nodes, fileFilter);
  1209. const filterNote = filteredOut
  1210. ? `\n\n> **Note:** no definition of "${symbol}" matches file "${fileFilter}" — showing all definitions instead.`
  1211. : '';
  1212. const collect = (defNodes: Node[]) => {
  1213. const seen = new Set<string>();
  1214. const callers: Node[] = [];
  1215. const labels = new Map<string, string>();
  1216. for (const node of defNodes) {
  1217. for (const c of cg.getCallers(node.id)) {
  1218. if (!seen.has(c.node.id)) {
  1219. seen.add(c.node.id);
  1220. callers.push(c.node);
  1221. const label = this.edgeLabel(c.edge);
  1222. if (label) labels.set(c.node.id, label);
  1223. }
  1224. }
  1225. }
  1226. return { callers, labels };
  1227. };
  1228. // Single definition (or same-file overloads): the familiar flat list.
  1229. if (groups.length === 1) {
  1230. const { callers, labels } = collect(groups[0]!);
  1231. if (callers.length === 0) {
  1232. return this.textResult(`No callers found for "${symbol}"${allMatches.note}${filterNote}`);
  1233. }
  1234. // A successful `file` narrowing makes the multi-symbol aggregation note
  1235. // stale — suppress it.
  1236. const note = fileFilter && !filteredOut ? '' : allMatches.note;
  1237. const formatted = this.formatNodeList(callers.slice(0, limit), `Callers of ${symbol}`, labels) + note + filterNote;
  1238. return this.textResult(this.truncateOutput(formatted));
  1239. }
  1240. // Multiple DISTINCT definitions (#764): one section per definition so an
  1241. // agent never mistakes one app's callers for another's. Narrow with
  1242. // `file` to focus a single definition.
  1243. const lines: string[] = [
  1244. `## Callers of ${symbol} — ${groups.length} distinct definitions (narrow with \`file\`)`,
  1245. ];
  1246. for (const group of groups) {
  1247. const { callers, labels } = collect(group);
  1248. lines.push('', this.definitionHeading(group));
  1249. if (callers.length === 0) {
  1250. lines.push('- (no callers)');
  1251. continue;
  1252. }
  1253. for (const node of callers.slice(0, limit)) {
  1254. const location = node.startLine ? `:${node.startLine}` : '';
  1255. const label = labels.get(node.id);
  1256. lines.push(`- ${node.name} (${node.kind}) - ${node.filePath}${location}${label ? ` — via ${label}` : ''}`);
  1257. }
  1258. }
  1259. return this.textResult(this.truncateOutput(lines.join('\n') + filterNote));
  1260. }
  1261. /**
  1262. * Handle codegraph_callees
  1263. */
  1264. private async handleCallees(args: Record<string, unknown>): Promise<ToolResult> {
  1265. const symbol = this.validateString(args.symbol, 'symbol');
  1266. if (typeof symbol !== 'string') return symbol;
  1267. const cg = this.getCodeGraph(args.projectPath as string | undefined);
  1268. const limit = clamp((args.limit as number) || 20, 1, 100);
  1269. const fileFilter = typeof args.file === 'string' ? args.file : undefined;
  1270. const allMatches = this.findAllSymbols(cg, symbol);
  1271. if (allMatches.nodes.length === 0) {
  1272. return this.textResult(`Symbol "${symbol}" not found in the codebase`);
  1273. }
  1274. const { groups, filteredOut } = this.groupDefinitions(allMatches.nodes, fileFilter);
  1275. const filterNote = filteredOut
  1276. ? `\n\n> **Note:** no definition of "${symbol}" matches file "${fileFilter}" — showing all definitions instead.`
  1277. : '';
  1278. const collect = (defNodes: Node[]) => {
  1279. const seen = new Set<string>();
  1280. const callees: Node[] = [];
  1281. const labels = new Map<string, string>();
  1282. for (const node of defNodes) {
  1283. for (const c of cg.getCallees(node.id)) {
  1284. if (!seen.has(c.node.id)) {
  1285. seen.add(c.node.id);
  1286. callees.push(c.node);
  1287. const label = this.edgeLabel(c.edge);
  1288. if (label) labels.set(c.node.id, label);
  1289. }
  1290. }
  1291. }
  1292. return { callees, labels };
  1293. };
  1294. if (groups.length === 1) {
  1295. const { callees, labels } = collect(groups[0]!);
  1296. if (callees.length === 0) {
  1297. return this.textResult(`No callees found for "${symbol}"${allMatches.note}${filterNote}`);
  1298. }
  1299. // A successful `file` narrowing makes the multi-symbol aggregation note
  1300. // stale — suppress it.
  1301. const note = fileFilter && !filteredOut ? '' : allMatches.note;
  1302. const formatted = this.formatNodeList(callees.slice(0, limit), `Callees of ${symbol}`, labels) + note + filterNote;
  1303. return this.textResult(this.truncateOutput(formatted));
  1304. }
  1305. // Multiple DISTINCT definitions (#764): per-definition sections.
  1306. const lines: string[] = [
  1307. `## Callees of ${symbol} — ${groups.length} distinct definitions (narrow with \`file\`)`,
  1308. ];
  1309. for (const group of groups) {
  1310. const { callees, labels } = collect(group);
  1311. lines.push('', this.definitionHeading(group));
  1312. if (callees.length === 0) {
  1313. lines.push('- (no callees)');
  1314. continue;
  1315. }
  1316. for (const node of callees.slice(0, limit)) {
  1317. const location = node.startLine ? `:${node.startLine}` : '';
  1318. const label = labels.get(node.id);
  1319. lines.push(`- ${node.name} (${node.kind}) - ${node.filePath}${location}${label ? ` — via ${label}` : ''}`);
  1320. }
  1321. }
  1322. return this.textResult(this.truncateOutput(lines.join('\n') + filterNote));
  1323. }
  1324. /**
  1325. * Handle codegraph_impact
  1326. */
  1327. private async handleImpact(args: Record<string, unknown>): Promise<ToolResult> {
  1328. const symbol = this.validateString(args.symbol, 'symbol');
  1329. if (typeof symbol !== 'string') return symbol;
  1330. const cg = this.getCodeGraph(args.projectPath as string | undefined);
  1331. const depth = clamp((args.depth as number) || 2, 1, 10);
  1332. const fileFilter = typeof args.file === 'string' ? args.file : undefined;
  1333. const allMatches = this.findAllSymbols(cg, symbol);
  1334. if (allMatches.nodes.length === 0) {
  1335. return this.textResult(`Symbol "${symbol}" not found in the codebase`);
  1336. }
  1337. const { groups, filteredOut } = this.groupDefinitions(allMatches.nodes, fileFilter);
  1338. const filterNote = filteredOut
  1339. ? `\n\n> **Note:** no definition of "${symbol}" matches file "${fileFilter}" — showing all definitions instead.`
  1340. : '';
  1341. const impactOf = (defNodes: Node[]) => {
  1342. const mergedNodes = new Map<string, Node>();
  1343. const mergedEdges: Edge[] = [];
  1344. const seenEdges = new Set<string>();
  1345. for (const node of defNodes) {
  1346. const impact = cg.getImpactRadius(node.id, depth);
  1347. for (const [id, n] of impact.nodes) {
  1348. mergedNodes.set(id, n);
  1349. }
  1350. for (const e of impact.edges) {
  1351. const key = `${e.source}->${e.target}:${e.kind}`;
  1352. if (!seenEdges.has(key)) {
  1353. seenEdges.add(key);
  1354. mergedEdges.push(e);
  1355. }
  1356. }
  1357. }
  1358. return { nodes: mergedNodes, edges: mergedEdges, roots: defNodes.map((n) => n.id) };
  1359. };
  1360. // Single definition (or same-file overloads): the familiar merged report.
  1361. if (groups.length === 1) {
  1362. const formatted = this.formatImpact(symbol, impactOf(groups[0]!)) + (fileFilter && !filteredOut ? "" : allMatches.note) + filterNote;
  1363. return this.textResult(this.truncateOutput(formatted));
  1364. }
  1365. // Multiple DISTINCT definitions (#764): a blast radius PER definition —
  1366. // merging unrelated same-named classes (one UserService per monorepo app)
  1367. // overstated impact and confused agents. Narrow with `file`.
  1368. const sections: string[] = [
  1369. `## Impact of ${symbol} — ${groups.length} distinct definitions (each with its own blast radius; narrow with \`file\`)`,
  1370. ];
  1371. for (const group of groups) {
  1372. const head = group[0]!;
  1373. const line = head.startLine ? `:${head.startLine}` : '';
  1374. sections.push(
  1375. '',
  1376. this.formatImpact(`${head.qualifiedName} (${head.filePath}${line})`, impactOf(group))
  1377. );
  1378. }
  1379. return this.textResult(this.truncateOutput(sections.join('\n') + filterNote));
  1380. }
  1381. /**
  1382. * Describe a synthesized (dynamic-dispatch) edge for human output: how the
  1383. * callback was wired up — the bridge static parsing can't see. Returns null
  1384. * for ordinary static edges. Used by trace + the node trail so a synthesized
  1385. * hop reads as "registered via onUpdate at App.tsx:3148", not a bare arrow.
  1386. */
  1387. private synthEdgeNote(edge: Edge | null): { label: string; compact: string; registeredAt?: string } | null {
  1388. if (!edge || edge.provenance !== 'heuristic') return null;
  1389. const m = edge.metadata as Record<string, unknown> | undefined;
  1390. const registeredAt = typeof m?.registeredAt === 'string' ? m.registeredAt : undefined;
  1391. const at = registeredAt ? ` @${registeredAt}` : '';
  1392. if (m?.synthesizedBy === 'callback') {
  1393. const via = m.via ? `\`${String(m.via)}\`` : 'a registrar';
  1394. const field = m.field ? ` on .${String(m.field)}` : '';
  1395. return {
  1396. label: `callback — registered via ${via}${field} (dynamic dispatch)`,
  1397. compact: `dynamic: callback via ${via}${at}`,
  1398. registeredAt,
  1399. };
  1400. }
  1401. if (m?.synthesizedBy === 'event-emitter') {
  1402. const ev = m.event ? `\`${String(m.event)}\`` : 'an event';
  1403. return {
  1404. label: `event ${ev} — emit → handler (dynamic dispatch)`,
  1405. compact: `dynamic: event ${ev}${at}`,
  1406. registeredAt,
  1407. };
  1408. }
  1409. if (m?.synthesizedBy === 'react-render') {
  1410. return {
  1411. label: `React re-render — \`setState\` re-runs render() (dynamic dispatch)`,
  1412. compact: `dynamic: React re-render via setState${at}`,
  1413. registeredAt,
  1414. };
  1415. }
  1416. if (m?.synthesizedBy === 'jsx-render') {
  1417. const child = m.via ? `<${String(m.via)}>` : 'a child component';
  1418. return {
  1419. label: `renders ${child} (JSX child — dynamic dispatch)`,
  1420. compact: `dynamic: renders ${child}`,
  1421. registeredAt,
  1422. };
  1423. }
  1424. if (m?.synthesizedBy === 'vue-handler') {
  1425. const ev = m.event ? `@${String(m.event)}` : 'a template event';
  1426. return {
  1427. label: `Vue template handler — bound to ${ev} (dynamic dispatch)`,
  1428. compact: `dynamic: Vue ${ev} handler`,
  1429. registeredAt,
  1430. };
  1431. }
  1432. if (m?.synthesizedBy === 'interface-impl') {
  1433. return {
  1434. label: `interface/abstract dispatch — runs the implementation override (dynamic dispatch)`,
  1435. compact: `dynamic: interface → impl${at}`,
  1436. registeredAt,
  1437. };
  1438. }
  1439. if (m?.synthesizedBy === 'closure-collection') {
  1440. const field = m.field ? `\`${String(m.field)}\`` : 'a collection';
  1441. return {
  1442. label: `closure collection — runs handlers appended to ${field} (dynamic dispatch)`,
  1443. compact: `dynamic: runs ${field} handlers${at}`,
  1444. registeredAt,
  1445. };
  1446. }
  1447. // Generic fallback for any other synthesizer (redux-thunk, gin-middleware-chain,
  1448. // flutter-build, …): a synthesized hop must never read as a bare static `calls`.
  1449. // It's a dynamic-dispatch bridge — label it as one and keep its wiring site.
  1450. if (typeof m?.synthesizedBy === 'string') {
  1451. const kind = m.synthesizedBy.replace(/-/g, ' ');
  1452. return { label: `${kind} (dynamic dispatch)`, compact: `dynamic: ${kind}${at}`, registeredAt };
  1453. }
  1454. return null;
  1455. }
  1456. /**
  1457. * Flow-from-named-symbols: an agent's codegraph_explore query is a bag of
  1458. * symbol names that usually spans the flow it's investigating (e.g.
  1459. * "PmsProductController getList PmsProductService list PmsProductServiceImpl").
  1460. * Surface the longest call chain AMONG those named symbols — scoped to what the
  1461. * agent explicitly named, so (unlike a fuzzy relevance set) there's no
  1462. * wrong-feature wandering. Rides synthesized edges, so controller→service-
  1463. * interface→impl shows up. Returns '' if no chain of >=3 nodes exists.
  1464. *
  1465. * Ambiguous tokens (Java `list` → dozens of nodes) are disambiguated by
  1466. * CO-NAMING: the agent names the class too, so we keep only `list` candidates
  1467. * whose qualifiedName contains another named token (`PmsProductServiceImpl::list`),
  1468. * dropping unrelated `OmsOrderService::list`.
  1469. */
  1470. private buildFlowFromNamedSymbols(cg: CodeGraph, query: string): { text: string; pathNodeIds: Set<string>; namedNodeIds: Set<string>; uniqueNamedNodeIds: Set<string>; spineCallSites: Map<string, number> } {
  1471. // spineCallSites: for each spine node, the line where it CALLS the next hop —
  1472. // lets the source assembler window an oversize spine method (e.g. n8n's 962-line
  1473. // processRunExecutionData) to the call site instead of dumping the whole body.
  1474. const EMPTY = { text: '', pathNodeIds: new Set<string>(), namedNodeIds: new Set<string>(), uniqueNamedNodeIds: new Set<string>(), spineCallSites: new Map<string, number>() };
  1475. try {
  1476. const CALLABLE = new Set(['method', 'function', 'component', 'constructor']);
  1477. // Strip only a REAL file extension (Create.cs → Create); KEEP qualified
  1478. // names (Class.method / Class::method) — the agent's most precise input,
  1479. // resolved exactly by findAllSymbols. (The old strip mangled Class.method
  1480. // into Class, throwing the method away.)
  1481. const FILE_EXT = /\.(?:java|kt|kts|ts|tsx|js|jsx|mjs|cjs|cs|py|go|rb|php|swift|rs|cpp|cc|cxx|c|h|hpp|scala|lua|dart|vue|svelte|astro)$/i;
  1482. const tokens = [...new Set(
  1483. query.split(/[\s,()[\]]+/)
  1484. .map((t) => t.replace(FILE_EXT, '').trim())
  1485. .filter((t) => t.length >= 3 && /^[A-Za-z_$][\w$]*(?:(?:::|\.)[\w$]+)*$/.test(t))
  1486. )].slice(0, 16);
  1487. if (tokens.length < 2) return EMPTY;
  1488. // Pool of name SEGMENTS (Class + method from every token) used to
  1489. // disambiguate an ambiguous SIMPLE name: keep a candidate only if its
  1490. // CONTAINER class is itself named in the query.
  1491. const segPool = new Set<string>();
  1492. for (const t of tokens) for (const s of t.toLowerCase().split(/::|\./)) if (s) segPool.add(s);
  1493. const named = new Map<string, Node>();
  1494. // Nodes whose token is SPECIFIC — a (near-)unique callable name (<=3 defs in
  1495. // the whole graph). These are safe to SPARE a file on: the agent named THIS
  1496. // method (`getResponseWithInterceptorChain`, 1 def). A hyper-polymorphic name
  1497. // (`as_sql`, 110 defs across every Expression/Compiler subclass) is NOT here,
  1498. // so naming it doesn't keep every backend variant full and flood the budget.
  1499. const uniqueNamedNodeIds = new Set<string>();
  1500. // token → resolved node ids: drives the token-coverage check that gates
  1501. // the dynamic-boundary scan (a token is covered when ANY of its nodes
  1502. // lands on the main chain — overloads off the chain don't count against).
  1503. const tokenNodes = new Map<string, string[]>();
  1504. // token → its full same-name callable family (before the container filter).
  1505. // A LARGE family that fails to connect on the chain is a polymorphic
  1506. // interface/registry dispatch — surfaced by buildPolymorphicBoundaries below.
  1507. const tokenFamily = new Map<string, Node[]>();
  1508. // Non-callable endpoints (CONSTANT/VARIABLE/FIELD) connected by a SYNTHESIZED
  1509. // edge. RTK thunks are `const X = createAsyncThunk(...)`, so a thunk→thunk hop
  1510. // is constant→constant — the CALLABLE-only `named` set can't hold it, and
  1511. // without this the hop is invisible to the Flow path at every tier (the
  1512. // Relationships section catches it only on repos ≥500 files). Kept SEPARATE
  1513. // from `named` (which drives the call-chain + source sizing, callable-only);
  1514. // fed only to the dynamic-dispatch-links scan below.
  1515. const dynNamed = new Map<string, Node>();
  1516. const DYN_KINDS = new Set(['constant', 'variable', 'field', 'property']);
  1517. const hasHeuristicEdge = (id: string): boolean =>
  1518. [...cg.getCallers(id), ...cg.getCallees(id)].some(({ edge }) => edge.provenance === 'heuristic');
  1519. for (const t of tokens) {
  1520. const hits = this.findAllSymbols(cg, t).nodes;
  1521. const cands = hits.filter((n) => CALLABLE.has(n.kind));
  1522. tokenFamily.set(t, cands);
  1523. // A qualified or otherwise-specific name (<=3 hits) keeps all; an
  1524. // ambiguous simple name keeps only candidates whose container is named.
  1525. const specific = cands.length <= 3;
  1526. const pick = specific
  1527. ? cands
  1528. : cands.filter((n) => {
  1529. const segs = (n.qualifiedName || '').toLowerCase().split(/::|\./).filter(Boolean);
  1530. const container = segs.length >= 2 ? segs[segs.length - 2] : '';
  1531. return !!container && segPool.has(container);
  1532. });
  1533. const kept = pick.slice(0, 6);
  1534. tokenNodes.set(t, kept.map((n) => n.id));
  1535. for (const n of kept) {
  1536. named.set(n.id, n);
  1537. if (specific) uniqueNamedNodeIds.add(n.id);
  1538. }
  1539. // Same token, non-callable synth endpoints (capped, precision-gated on an
  1540. // actual heuristic edge so plain config constants never qualify).
  1541. if (dynNamed.size < 12) {
  1542. for (const n of hits) {
  1543. if (CALLABLE.has(n.kind) || !DYN_KINDS.has(n.kind) || dynNamed.has(n.id)) continue;
  1544. if (hasHeuristicEdge(n.id)) dynNamed.set(n.id, n);
  1545. if (dynNamed.size >= 12) break;
  1546. }
  1547. }
  1548. if (named.size > 40) break;
  1549. }
  1550. // Surface synthesized (heuristic) edges incident to a named symbol — INCLUDING
  1551. // the non-callable CONSTANT endpoints in `dynNamed`. `skipInChain` drops a hop
  1552. // already shown in the rendered main chain (a 2-node chain renders nothing, so a
  1553. // direct named→named synth hop still surfaces — #687).
  1554. const collectSynthLinks = (skipInChain: ((e: Edge) => boolean) | null): string[] => {
  1555. const synthLines: string[] = [];
  1556. const synthSeen = new Set<string>();
  1557. for (const n of [...named.values(), ...dynNamed.values()]) {
  1558. if (synthLines.length >= 6) break;
  1559. for (const { node: other, edge } of [...cg.getCallers(n.id), ...cg.getCallees(n.id)]) {
  1560. if (synthLines.length >= 6) break;
  1561. if (edge.provenance !== 'heuristic' || other.id === n.id) continue;
  1562. if (skipInChain && skipInChain(edge)) continue;
  1563. const src = edge.source === n.id ? n : other;
  1564. const tgt = edge.source === n.id ? other : n;
  1565. const key = `${src.name}>${tgt.name}`;
  1566. if (synthSeen.has(key)) continue;
  1567. synthSeen.add(key);
  1568. const note = this.synthEdgeNote(edge);
  1569. synthLines.push(`- ${src.name} → ${tgt.name} [${note ? note.compact : edge.kind}]`);
  1570. }
  1571. }
  1572. return synthLines;
  1573. };
  1574. if (named.size < 2) {
  1575. // <2 CALLABLES resolved. Two recoveries before giving up: (1) synthesized
  1576. // edges among named CONSTANT/VARIABLE endpoints — RTK thunk→thunk is
  1577. // constant→constant, so `named` can be empty while `dynNamed` holds the
  1578. // whole chain; (2) the one resolved callable's body may hold the
  1579. // dynamic-dispatch site that EXPLAINS a half-connected flow.
  1580. const synthLines = collectSynthLinks(null);
  1581. const boundaries = named.size === 0 ? '' : (this.buildDynamicBoundaries(cg, [...named.values()], named) || '');
  1582. if (synthLines.length === 0 && !boundaries) return EMPTY;
  1583. const out: string[] = [];
  1584. if (synthLines.length) out.push(
  1585. '## Dynamic-dispatch links among your symbols',
  1586. '(synthesized — the indirect hops grep/Read would reconstruct; the `@file:line` is the wiring site)',
  1587. '', ...synthLines, '');
  1588. if (boundaries) out.push(boundaries);
  1589. out.push('> Full source for these symbols is below.\n');
  1590. return { text: out.join('\n'), pathNodeIds: new Set(), namedNodeIds: new Set<string>([...named.keys(), ...dynNamed.keys()]), uniqueNamedNodeIds, spineCallSites: new Map<string, number>() };
  1591. }
  1592. const MAX_HOPS = 7;
  1593. let best: Array<{ node: Node; edge: Edge | null }> | null = null;
  1594. // BFS the full call graph (incl. synth edges) from each named seed, but
  1595. // only ACCEPT a sink that is also named — both ends anchored to symbols the
  1596. // agent named, so the chain stays on-topic while bridging intermediates
  1597. // (e.g. the exact interface overload) that the token resolution missed.
  1598. for (const seed of [...named.values()].slice(0, 8)) {
  1599. const parent = new Map<string, { prev: string | null; edge: Edge | null; node: Node }>();
  1600. parent.set(seed.id, { prev: null, edge: null, node: seed });
  1601. const q: Array<{ id: string; depth: number; streak: number }> = [{ id: seed.id, depth: 0, streak: 0 }];
  1602. let deep: string | null = null, deepDepth = 0;
  1603. const MAX_BRIDGE = 1; // ≤1 consecutive UNNAMED hop: bridge one missing intermediate, never wander a god-function's fan-out
  1604. for (let h = 0; h < q.length && parent.size < 1500; h++) {
  1605. const { id, depth, streak } = q[h]!;
  1606. if (id !== seed.id && named.has(id) && depth > deepDepth) { deep = id; deepDepth = depth; }
  1607. if (depth >= MAX_HOPS - 1) continue;
  1608. for (const c of cg.getCallees(id)) {
  1609. if (c.edge.kind !== 'calls' || parent.has(c.node.id)) continue;
  1610. const newStreak = named.has(c.node.id) ? 0 : streak + 1;
  1611. if (newStreak > MAX_BRIDGE) continue;
  1612. parent.set(c.node.id, { prev: id, edge: c.edge, node: c.node });
  1613. q.push({ id: c.node.id, depth: depth + 1, streak: newStreak });
  1614. }
  1615. }
  1616. if (!deep) continue;
  1617. const chain: Array<{ node: Node; edge: Edge | null }> = [];
  1618. let cur: string | null = deep;
  1619. while (cur) { const p = parent.get(cur); if (!p) break; chain.push({ node: p.node, edge: p.edge }); cur = p.prev; }
  1620. chain.reverse();
  1621. if (!best || chain.length > best.length) best = chain;
  1622. }
  1623. const hasMain = !!best && best.length >= 3;
  1624. const pathIds = new Set((best ?? []).map((s) => s.node.id));
  1625. // Where each spine node calls the NEXT hop (best[i+1].edge is the edge from
  1626. // best[i] → best[i+1]; its line is the call site inside best[i]'s body). Lets
  1627. // the assembler window an oversize spine method to the call instead of dumping it.
  1628. const spineCallSites = new Map<string, number>();
  1629. if (best) for (let i = 0; i < best.length - 1; i++) {
  1630. const ln = best[i + 1]?.edge?.line;
  1631. if (ln && ln > 0 && !spineCallSites.has(best[i]!.node.id)) spineCallSites.set(best[i]!.node.id, ln);
  1632. }
  1633. // Dynamic-boundary scan (#687) — fires ONLY when the flow the agent
  1634. // asked about did not fully connect: some token resolved to nodes but
  1635. // none of them sit on the main chain (or there is no chain at all). A
  1636. // healthy flow skips this entirely. Scan order: the chain's dead end
  1637. // first (where the partial flow stops), then the disconnected symbols,
  1638. // agent-specific (unique-named) ones first.
  1639. let boundaryText = '';
  1640. {
  1641. const uncovered: Node[] = [];
  1642. if (!hasMain) {
  1643. // No rendered chain — but a 2-node chain still CONNECTS its two
  1644. // endpoints (e.g. via one synthesized hop, surfaced below as a
  1645. // dynamic-dispatch link). Only nodes off that short chain are
  1646. // unexplained breaks worth scanning.
  1647. for (const n of named.values()) if (!pathIds.has(n.id)) uncovered.push(n);
  1648. } else {
  1649. for (const ids of tokenNodes.values()) {
  1650. if (ids.length === 0 || ids.some((id) => pathIds.has(id))) continue;
  1651. for (const id of ids) { const n = named.get(id); if (n) uncovered.push(n); }
  1652. }
  1653. }
  1654. if (uncovered.length > 0) {
  1655. const scanList: Node[] = [];
  1656. if (hasMain) scanList.push(best![best!.length - 1]!.node);
  1657. scanList.push(...uncovered.sort((a, b) =>
  1658. (uniqueNamedNodeIds.has(b.id) ? 1 : 0) - (uniqueNamedNodeIds.has(a.id) ? 1 : 0)));
  1659. boundaryText = this.buildDynamicBoundaries(cg, scanList, named);
  1660. }
  1661. }
  1662. // Interface/registry-dispatch announcement (extends #687 to GRAPH-visible
  1663. // polymorphism). A method the agent NAMED that resolves to a large same-name
  1664. // family AND did not land on the main chain is almost always a runtime
  1665. // dispatch (plugin/strategy/handler interface): the concrete target is chosen
  1666. // at runtime from N implementations, so no single static edge is the answer.
  1667. // The body-scan above can't see this — `nodeType.execute()` is textually an
  1668. // ordinary call; the polymorphism lives in the graph (implements edges), so
  1669. // detect it there. Fires ONLY for an uncovered named token; a connected flow
  1670. // stays silent.
  1671. let polyText = '';
  1672. {
  1673. const POLY_MIN_FAMILY = 8; // smaller families are overload sets, not dispatch
  1674. const polyCands: Array<{ token: string; family: Node[] }> = [];
  1675. for (const [t, fam] of tokenFamily) {
  1676. if (fam.length < POLY_MIN_FAMILY) continue;
  1677. const ids = tokenNodes.get(t) || [];
  1678. if (ids.some((id) => pathIds.has(id))) continue; // covered by the flow — silent
  1679. polyCands.push({ token: t, family: fam });
  1680. }
  1681. if (polyCands.length) polyText = this.buildPolymorphicBoundaries(cg, polyCands, named);
  1682. }
  1683. // Supplementary: dynamic-dispatch (synthesized) edges incident to a named
  1684. // symbol (incl. the non-callable CONSTANT endpoints in `dynNamed`) — the
  1685. // indirect hops an agent would otherwise grep/Read to reconstruct ("where do
  1686. // the appended `validators` actually run?"). Surfaced even when the OTHER end
  1687. // wasn't named. The skip drops a hop already in the rendered main chain; a
  1688. // 2-node chain renders nothing (hasMain false) so a direct named→named synth
  1689. // hop still surfaces — too short for Flow, but #687-visible here.
  1690. const synthLines = collectSynthLinks(
  1691. hasMain ? (e: Edge) => pathIds.has(e.source) && pathIds.has(e.target) : null
  1692. );
  1693. if (!hasMain && synthLines.length === 0 && !boundaryText && !polyText) return EMPTY;
  1694. const out: string[] = [];
  1695. if (hasMain) {
  1696. out.push('## Flow (call path among the symbols you queried)', '');
  1697. for (let i = 0; i < best!.length; i++) {
  1698. const step = best![i]!;
  1699. if (step.edge) { const sy = this.synthEdgeNote(step.edge); out.push(` ↓ ${sy ? sy.compact : step.edge.kind}`); }
  1700. out.push(`${i + 1}. ${step.node.name} (${step.node.filePath}:${step.node.startLine})`);
  1701. }
  1702. out.push('');
  1703. }
  1704. if (synthLines.length) {
  1705. out.push(
  1706. '## Dynamic-dispatch links among your symbols',
  1707. '(synthesized — the indirect hops grep/Read would reconstruct; the `@file:line` is the wiring site)',
  1708. '',
  1709. ...synthLines,
  1710. ''
  1711. );
  1712. }
  1713. if (boundaryText) out.push(boundaryText);
  1714. if (polyText) out.push(polyText);
  1715. out.push('> Full source for these symbols is below — the call flow among them, followed by their bodies.', '');
  1716. // namedNodeIds = every callable the agent explicitly named (a superset of
  1717. // the spine). A file holding one is something the agent asked to SEE, so it
  1718. // must keep full source even if it's an off-spine polymorphic sibling — the
  1719. // agent named `getResponseWithInterceptorChain` / `SQLCompiler.execute_sql`
  1720. // as the mechanism, not as an interchangeable leaf. See the skeleton gate.
  1721. return { text: out.join('\n'), pathNodeIds: pathIds, namedNodeIds: new Set<string>([...named.keys(), ...dynNamed.keys()]), uniqueNamedNodeIds, spineCallSites };
  1722. } catch {
  1723. return EMPTY;
  1724. }
  1725. }
  1726. /**
  1727. * Dynamic-boundary surfacing (#687): when the flow among the agent's named
  1728. * symbols does not fully connect, scan the disconnected symbols' bodies for
  1729. * dynamic-dispatch sites (computed member calls, getattr, reflection, typed
  1730. * message buses, runtime-keyed emits) and ANNOUNCE the boundary — the exact
  1731. * site, the form, and (when a key is statically visible) candidate targets —
  1732. * instead of guessing edges. The answer to "how does A reach B" when no
  1733. * static path exists IS the dispatch site: that's where the flow continues
  1734. * at runtime. Query-time, deterministic, zero graph mutation; a fully
  1735. * connected flow never reaches this method.
  1736. */
  1737. private buildDynamicBoundaries(cg: CodeGraph, scanList: Node[], named: Map<string, Node>): string {
  1738. const MAX_NOTES = 4; // boundary bullets per explore
  1739. const MAX_SCAN = 8; // bodies scanned
  1740. const MAX_TOTAL_CHARS = 200_000;
  1741. let projectRoot: string;
  1742. try { projectRoot = cg.getProjectRoot(); } catch { return ''; }
  1743. const notes: string[] = [];
  1744. const seenNode = new Set<string>();
  1745. const seenSite = new Set<string>();
  1746. let scanned = 0, charsScanned = 0;
  1747. for (const node of scanList) {
  1748. if (notes.length >= MAX_NOTES || scanned >= MAX_SCAN || charsScanned > MAX_TOTAL_CHARS) break;
  1749. if (seenNode.has(node.id) || !node.startLine || !node.endLine) continue;
  1750. seenNode.add(node.id);
  1751. const absPath = validatePathWithinRoot(projectRoot, node.filePath);
  1752. if (!absPath || !existsSync(absPath)) continue;
  1753. let content: string;
  1754. try { content = readFileSync(absPath, 'utf-8'); } catch { continue; }
  1755. const body = content.split('\n').slice(node.startLine - 1, node.endLine).join('\n');
  1756. scanned++;
  1757. charsScanned += body.length;
  1758. for (const m of scanDynamicDispatch(body, node.language || '', node.startLine)) {
  1759. if (notes.length >= MAX_NOTES) break;
  1760. const siteKey = `${node.filePath}:${m.line}:${m.form}`;
  1761. if (seenSite.has(siteKey)) continue;
  1762. seenSite.add(siteKey);
  1763. const more = m.moreSites ? ` (+${m.moreSites} more such site${m.moreSites > 1 ? 's' : ''} in this body)` : '';
  1764. notes.push(`- \`${node.name}\` (${node.filePath}:${m.line}) — ${m.label}: \`${m.snippet}\`${more}`);
  1765. if (m.key) {
  1766. const cand = this.boundaryCandidates(cg, m.key, !!m.keyIsType, named, node.id);
  1767. if (cand) notes.push(` ${cand}`);
  1768. }
  1769. }
  1770. }
  1771. if (notes.length === 0) return '';
  1772. return [
  1773. '## Dynamic boundaries (the static path ends at runtime dispatch)',
  1774. '',
  1775. ...notes,
  1776. '',
  1777. '> These sites choose their call target at runtime (registry / bus / reflection) — the site shown IS where the flow continues. To follow it, run codegraph_explore or codegraph_node on a candidate; source for the sites above is included below.',
  1778. '',
  1779. ].join('\n');
  1780. }
  1781. /**
  1782. * Interface/registry-dispatch announcement — #687 extended to GRAPH-visible
  1783. * polymorphism (the body-scan can't see it: `nodeType.execute()` is textually
  1784. * an ordinary call; the polymorphism lives in the `implements`/`extends` edges).
  1785. *
  1786. * A method the agent named that resolves to a large same-name family whose
  1787. * definers overwhelmingly implement/extend ONE supertype is a runtime dispatch:
  1788. * the concrete target is chosen at runtime from N implementations, so no single
  1789. * static edge is "the answer" — the implementations ARE the continuations. We
  1790. * announce the supertype, its TRUE implementer count, and a few concrete targets,
  1791. * then steer to codegraph_explore. Graph-only, query-time, zero mutation; the
  1792. * caller fires it ONLY for an UNCOVERED named token, so a connected flow is silent.
  1793. *
  1794. * Robust to FTS sampling bias: the same-name family is a capped FTS sample that
  1795. * over-represents whatever FTS ranks first (n8n: DB `TableOperation.execute`
  1796. * outnumbered `INodeType.execute` in the sample 7:6 even though INodeType has
  1797. * 611 implementers vs a handful). So candidate supertypes are ranked by their
  1798. * TRUE graph-wide implementer count, NOT their frequency in the sample.
  1799. */
  1800. private buildPolymorphicBoundaries(cg: CodeGraph, candidates: Array<{ token: string; family: Node[] }>, named: Map<string, Node>): string {
  1801. const CLASSY = new Set(['class', 'struct', 'interface', 'trait', 'protocol', 'abstract']);
  1802. const MIN_IMPL = 8; // a supertype needs >= this many implementers to count as "polymorphic"
  1803. const MIN_SUPPORT = 2; // >= this many sampled definers must share the supertype (ties it to the token)
  1804. const SAMPLE = 40; // family members inspected per token
  1805. const MAX_NOTES = 3;
  1806. const rel = (p: string) => p.replace(/\\/g, '/');
  1807. const containerOf = (m: Node): Node | null => {
  1808. try { const ce = cg.getIncomingEdges(m.id).find((e) => e.kind === 'contains'); return ce ? cg.getNode(ce.source) : null; }
  1809. catch { return null; }
  1810. };
  1811. const notes: string[] = [];
  1812. const seenSuper = new Set<string>();
  1813. for (const { token, family } of candidates) {
  1814. if (notes.length >= MAX_NOTES) break;
  1815. // supertype id → how many sampled definers share it + a few example definers
  1816. const supers = new Map<string, { node: Node; count: number; targets: Node[] }>();
  1817. for (const m of family.slice(0, SAMPLE)) {
  1818. const container = containerOf(m);
  1819. if (!container || !CLASSY.has(container.kind)) continue;
  1820. let sups: Node[] = [];
  1821. try {
  1822. sups = cg.getOutgoingEdges(container.id)
  1823. .filter((e) => e.kind === 'implements' || e.kind === 'extends')
  1824. .map((e) => { try { return cg.getNode(e.target); } catch { return null; } })
  1825. .filter((n): n is Node => !!n && CLASSY.has(n.kind) && (n.name?.length || 0) >= 3);
  1826. } catch { /* no supertypes — free function or unresolved */ }
  1827. for (const s of sups) {
  1828. const e = supers.get(s.id) || { node: s, count: 0, targets: [] };
  1829. e.count++;
  1830. if (e.targets.length < 6) e.targets.push(m);
  1831. supers.set(s.id, e);
  1832. }
  1833. }
  1834. // Pick the supertype with the most TRUE implementers (graph-wide), among
  1835. // those genuinely shared by the token's definers.
  1836. let best: { node: Node; impl: number; targets: Node[] } | null = null;
  1837. for (const { node, count, targets } of supers.values()) {
  1838. if (count < MIN_SUPPORT) continue;
  1839. let impl = 0;
  1840. try { impl = cg.getIncomingEdges(node.id).filter((e) => e.kind === 'implements' || e.kind === 'extends').length; }
  1841. catch { /* leave 0 — gated out below */ }
  1842. if (impl < MIN_IMPL) continue;
  1843. if (!best || impl > best.impl) best = { node, impl, targets };
  1844. }
  1845. if (!best || seenSuper.has(best.node.id)) continue;
  1846. seenSuper.add(best.node.id);
  1847. const namedNames = new Set([...named.values()].map((n) => n.name));
  1848. const eg = best.targets.slice(0, 4).map((m) => {
  1849. const cont = containerOf(m);
  1850. const disp = cont ? `${cont.name}.${m.name}` : (m.qualifiedName || m.name);
  1851. const mark = cont && namedNames.has(cont.name) ? ' ← you named this' : '';
  1852. return `\`${disp}\` (${rel(m.filePath)}:${m.startLine})${mark}`;
  1853. });
  1854. const more = best.impl > eg.length ? ` +${best.impl - eg.length} more` : '';
  1855. notes.push(`- \`${token}\` → runtime dispatch to **${best.impl}** types implementing \`${best.node.name}\` — the static path ends here, the target is chosen at runtime. e.g. ${eg.join(', ')}${more}`);
  1856. }
  1857. if (notes.length === 0) return '';
  1858. return [
  1859. '## Interface dispatch (a named method has many implementations)',
  1860. '',
  1861. ...notes,
  1862. '',
  1863. '> The method above is dispatched at runtime to one of the listed implementations (a registry / plugin / strategy interface) — there is no single static caller→callee edge; the implementations ARE the continuations. To follow one, run codegraph_explore on a listed target.',
  1864. '',
  1865. ].join('\n');
  1866. }
  1867. /**
  1868. * Shortlist candidate runtime targets for a dispatch key surfaced by
  1869. * {@link buildDynamicBoundaries}. Exact conventional names first (`save` →
  1870. * `onSave`/`handleSave`; `CreateCmd` → `CreateCmdHandler`), then FTS, with a
  1871. * normalized-containment post-filter (FTS camel-splitting is fuzzier than a
  1872. * candidate list should be). Symbols the agent already named sort first and
  1873. * are marked — that's the "you were right, here's the wiring" case.
  1874. */
  1875. private boundaryCandidates(cg: CodeGraph, key: string, keyIsType: boolean, named: Map<string, Node>, selfId: string): string {
  1876. const CALLABLE = new Set(['method', 'function', 'component', 'constructor', 'class']);
  1877. const norm = (s: string) => s.toLowerCase().replace(/[^a-z0-9]/g, '');
  1878. const keyNorm = norm(key);
  1879. if (keyNorm.length < 3) return '';
  1880. const cands = new Map<string, Node>();
  1881. const consider = (n: Node | undefined | null) => {
  1882. if (!n || n.id === selfId || !CALLABLE.has(n.kind) || cands.has(n.id)) return;
  1883. const nameNorm = norm(n.name || '');
  1884. if (nameNorm.length < 3) return;
  1885. if (!nameNorm.includes(keyNorm) && !keyNorm.includes(nameNorm)) return;
  1886. cands.set(n.id, n);
  1887. };
  1888. const cap = key.charAt(0).toUpperCase() + key.slice(1);
  1889. const probes = keyIsType
  1890. ? [`${key}Handler`, key]
  1891. : [key, `on${cap}`, `handle${cap}`, `${key}Handler`, `handle_${key}`];
  1892. for (const p of probes) {
  1893. try { for (const n of cg.getNodesByName(p)) consider(n); } catch { /* exact probe miss is fine */ }
  1894. }
  1895. let raw = 0;
  1896. try {
  1897. const results = cg.searchNodes(key, { limit: 12 });
  1898. raw = results.length;
  1899. for (const r of results) consider(r.node);
  1900. } catch { /* FTS syntax edge — exact probes already ran */ }
  1901. if (cands.size === 0) {
  1902. return raw >= 12 && key.length < 5 ? `key \`${key}\` is too generic to shortlist (${raw}+ matches)` : '';
  1903. }
  1904. // A constructor candidate duplicates its class: extractors emit ctors as
  1905. // METHOD nodes named like the class (C#/Java `Foo::Foo`) — keep the class.
  1906. const all = [...cands.values()];
  1907. const classKey = new Set(all.filter((n) => n.kind === 'class').map((n) => `${n.name}|${n.filePath}`));
  1908. const namedNames = new Set([...named.values()].map((n) => n.name));
  1909. const isNamed = (n: Node) => named.has(n.id) || namedNames.has(n.name); // the flow's named set holds callables only — transfer the mark to the class
  1910. const list = all
  1911. .filter((n) => !(n.kind !== 'class' && classKey.has(`${n.name}|${n.filePath}`)))
  1912. .sort((a, b) => (isNamed(b) ? 1 : 0) - (isNamed(a) ? 1 : 0))
  1913. .slice(0, 4)
  1914. .map((n) => {
  1915. // Typed-bus convention: the runtime target is the candidate class's
  1916. // Handle/Execute/Consume method — name the exact node, not just the class.
  1917. let display = n.qualifiedName || n.name;
  1918. let at = `${n.filePath}:${n.startLine}`;
  1919. if (keyIsType && n.kind === 'class') {
  1920. try {
  1921. const HANDLER_METHODS = /^(handle|handleAsync|execute|executeAsync|consume|consumeAsync|run|__invoke)$/i;
  1922. const method = cg.getOutgoingEdges(n.id)
  1923. .filter((e) => e.kind === 'contains')
  1924. .map((e) => { try { return cg.getNode(e.target); } catch { return null; } })
  1925. .find((c): c is Node => !!c && c.kind === 'method' && HANDLER_METHODS.test(c.name));
  1926. if (method) { display = `${n.name}.${method.name}`; at = `${method.filePath}:${method.startLine}`; }
  1927. } catch { /* class without resolvable members — show the class itself */ }
  1928. }
  1929. return `\`${display}\` (${at})${isNamed(n) ? ' ← you named this' : ''}`;
  1930. });
  1931. return `candidates for key \`${key}\`: ${list.join(', ')}`;
  1932. }
  1933. /**
  1934. * Compact "blast radius" for the entry symbols of an explore result: who
  1935. * depends on each (callers) and which test files cover it — LOCATIONS ONLY,
  1936. * no source, so the agent knows what to update / re-verify before editing
  1937. * without reaching for a separate impact call. Always-on, but skips symbols
  1938. * that have no dependents (nothing to warn about), and returns '' when none
  1939. * qualify so a leaf-only exploration stays clean.
  1940. */
  1941. private buildBlastRadiusSection(cg: CodeGraph, subgraph: Subgraph): string {
  1942. const ROOT_CAP = 5; // only the symbols the query actually targeted
  1943. const FILE_CAP = 4; // caller files listed per symbol before "+N more"
  1944. const MEANINGFUL = new Set<string>([
  1945. 'function', 'method', 'class', 'interface', 'struct', 'trait', 'protocol',
  1946. 'enum', 'type_alias', 'component', 'constant', 'variable', 'property', 'field',
  1947. ]);
  1948. const rel = (p: string) => p.replace(/\\/g, '/');
  1949. const roots = subgraph.roots
  1950. .map((id) => subgraph.nodes.get(id))
  1951. .filter((n): n is Node => !!n && MEANINGFUL.has(n.kind))
  1952. .slice(0, ROOT_CAP);
  1953. if (roots.length === 0) return '';
  1954. const entries: string[] = [];
  1955. for (const root of roots) {
  1956. let callers: Array<{ node: Node }> = [];
  1957. try { callers = cg.getCallers(root.id) as Array<{ node: Node }>; } catch { /* skip this root */ }
  1958. const seen = new Set<string>();
  1959. const uniq: Node[] = [];
  1960. for (const c of callers) {
  1961. if (c?.node && !seen.has(c.node.id)) { seen.add(c.node.id); uniq.push(c.node); }
  1962. }
  1963. if (uniq.length === 0) continue; // no blast radius → nothing to flag
  1964. const callerFiles = [...new Set(uniq.map((n) => rel(n.filePath)))];
  1965. const testFiles = callerFiles.filter((f) => isTestFile(f));
  1966. const nonTest = callerFiles.filter((f) => !isTestFile(f));
  1967. const shown = nonTest.slice(0, FILE_CAP).map((f) => `\`${f}\``).join(', ');
  1968. const more = nonTest.length > FILE_CAP ? ` +${nonTest.length - FILE_CAP} more` : '';
  1969. const where = nonTest.length > 0 ? ` in ${shown}${more}` : '';
  1970. const tests = testFiles.length > 0
  1971. ? `; tests: ${testFiles.slice(0, FILE_CAP).map((f) => `\`${f}\``).join(', ')}${testFiles.length > FILE_CAP ? ` +${testFiles.length - FILE_CAP}` : ''}`
  1972. : '; ⚠️ no covering tests found';
  1973. entries.push(
  1974. `- \`${root.name}\` (${rel(root.filePath)}:${root.startLine}) — ${uniq.length} caller${uniq.length === 1 ? '' : 's'}${where}${tests}`,
  1975. );
  1976. }
  1977. if (entries.length === 0) return '';
  1978. return [
  1979. '### Blast radius — what depends on these (update/verify before editing)',
  1980. '',
  1981. ...entries,
  1982. '',
  1983. ].join('\n');
  1984. }
  1985. /**
  1986. * Graph-connectivity relevance via Random-Walk-with-Restart (personalized
  1987. * PageRank) from the query's matched SEED nodes over the call/reference graph.
  1988. *
  1989. * This is the ranking signal text search (FTS/bm25) CANNOT provide, and it's
  1990. * codegraph's home turf: relevance by STRUCTURE, not words. A file whose
  1991. * symbols are call-connected to the matched cluster accrues walk mass and
  1992. * ranks high; a lone TEXT match — e.g. `LensSwitcher.swift` matched the word
  1993. * "switch" from `switchOrganization`, but calls none of `setUser`/`fetchUser`
  1994. * — gets only its own restart probability and ranks ~0. Immune to the
  1995. * tokenization trap that fools term matching, deterministic, no embeddings.
  1996. *
  1997. * Undirected adjacency (reachability both ways), restart α=0.25 to the seeds,
  1998. * power iteration to convergence. Bounded to the already-relevant subgraph, so
  1999. * it's a few hundred nodes × ~25 iterations — negligible cost.
  2000. */
  2001. private computeGraphRelevance(
  2002. nodeIds: string[],
  2003. edges: Edge[],
  2004. seedIds: Set<string>,
  2005. ): Map<string, number> {
  2006. const out = new Map<string, number>();
  2007. const n = nodeIds.length;
  2008. if (n === 0) return out;
  2009. const idx = new Map<string, number>();
  2010. for (let i = 0; i < n; i++) idx.set(nodeIds[i]!, i);
  2011. const RANK_EDGES = new Set<string>([
  2012. 'calls', 'references', 'extends', 'implements', 'overrides',
  2013. 'instantiates', 'returns', 'type_of', 'imports',
  2014. ]);
  2015. const adj: number[][] = Array.from({ length: n }, () => []);
  2016. for (const e of edges) {
  2017. if (!RANK_EDGES.has(e.kind)) continue;
  2018. const i = idx.get(e.source);
  2019. const j = idx.get(e.target);
  2020. if (i === undefined || j === undefined || i === j) continue;
  2021. adj[i]!.push(j);
  2022. adj[j]!.push(i); // undirected — reachable either direction
  2023. }
  2024. // Restart vector: uniform over seeds present in the candidate set. (Falls
  2025. // back to uniform-over-all if no seed landed in the set, so we never return
  2026. // all-zero.)
  2027. const r = new Array<number>(n).fill(0);
  2028. let rsum = 0;
  2029. for (const id of seedIds) {
  2030. const i = idx.get(id);
  2031. if (i !== undefined) { r[i] = 1; rsum += 1; }
  2032. }
  2033. if (rsum === 0) { for (let i = 0; i < n; i++) r[i] = 1; rsum = n; }
  2034. for (let i = 0; i < n; i++) r[i]! /= rsum;
  2035. const alpha = 0.25;
  2036. let s = r.slice();
  2037. for (let iter = 0; iter < 25; iter++) {
  2038. const next = new Array<number>(n).fill(0);
  2039. for (let i = 0; i < n; i++) {
  2040. const si = s[i]!;
  2041. if (si === 0) continue;
  2042. const d = adj[i]!.length;
  2043. if (d === 0) { next[i]! += si; continue; } // dangling: keep its mass
  2044. const share = si / d;
  2045. for (const j of adj[i]!) next[j]! += share;
  2046. }
  2047. for (let i = 0; i < n; i++) s[i] = (1 - alpha) * next[i]! + alpha * r[i]!;
  2048. }
  2049. for (let i = 0; i < n; i++) out.set(nodeIds[i]!, s[i]!);
  2050. return out;
  2051. }
  2052. /**
  2053. * Handle codegraph_explore — deep exploration in a single call
  2054. *
  2055. * Strategy: find relevant symbols via graph traversal, group by file,
  2056. * then read contiguous file sections covering all symbols per file.
  2057. * This replaces multiple codegraph_node + Read calls.
  2058. *
  2059. * Output size is adaptive to project file count via
  2060. * `getExploreOutputBudget` — see #185 for why a fixed 35k cap was a
  2061. * tax on small projects while earning its keep on large ones.
  2062. */
  2063. private async handleExplore(args: Record<string, unknown>): Promise<ToolResult> {
  2064. const query = this.validateString(args.query, 'query');
  2065. if (typeof query !== 'string') return query;
  2066. const cg = this.getCodeGraph(args.projectPath as string | undefined);
  2067. const projectRoot = cg.getProjectRoot();
  2068. // Resolve adaptive output budget from project size. Falls back to the
  2069. // largest-tier defaults if stats aren't available, which preserves
  2070. // pre-#185 behavior for callers that hit the rare stats failure.
  2071. let budget: ExploreOutputBudget;
  2072. try {
  2073. budget = getExploreOutputBudget(cg.getStats().fileCount);
  2074. } catch {
  2075. budget = getExploreOutputBudget(Infinity);
  2076. }
  2077. const maxFiles = clamp((args.maxFiles as number) || budget.defaultMaxFiles, 1, 20);
  2078. // Step 1: Find relevant context with generous parameters.
  2079. // Use a large maxNodes budget — explore has its own 35k char output limit
  2080. // that prevents context bloat, so more nodes just means better coverage
  2081. // across entry points (especially for large files like Svelte components).
  2082. const subgraph = await cg.findRelevantContext(query, {
  2083. searchLimit: 8,
  2084. traversalDepth: 3,
  2085. maxNodes: 200,
  2086. minScore: 0.2,
  2087. });
  2088. if (subgraph.nodes.size === 0) {
  2089. return this.textResult(`No relevant code found for "${query}"`);
  2090. }
  2091. // Graph-aware glue: findRelevantContext builds the subgraph from name/text
  2092. // search, so a method that BRIDGES named symbols — e.g. App.tsx's
  2093. // triggerRender, which calls the named triggerUpdate — is never a search hit
  2094. // and gets missed, forcing the agent to Read the file to trace it. Pull in
  2095. // the callers/callees of the entry (root) nodes, but ONLY those that live in
  2096. // files the subgraph already surfaces (where the agent reads to fill gaps),
  2097. // so we add wiring without dragging in unrelated files. These get an
  2098. // importance boost below so they survive the per-file cluster budget.
  2099. const glueNodeIds = new Set<string>();
  2100. const subgraphFiles = new Set<string>();
  2101. for (const n of subgraph.nodes.values()) subgraphFiles.add(n.filePath);
  2102. const GLUE_NODE_CAP = 60;
  2103. for (const rootId of subgraph.roots) {
  2104. if (glueNodeIds.size >= GLUE_NODE_CAP) break;
  2105. let neighbors: Node[] = [];
  2106. try {
  2107. neighbors = [
  2108. ...cg.getCallers(rootId).map(c => c.node),
  2109. ...cg.getCallees(rootId).map(c => c.node),
  2110. ];
  2111. } catch {
  2112. continue;
  2113. }
  2114. for (const nb of neighbors) {
  2115. if (glueNodeIds.size >= GLUE_NODE_CAP) break;
  2116. if (subgraph.nodes.has(nb.id)) continue;
  2117. if (!subgraphFiles.has(nb.filePath)) continue;
  2118. subgraph.nodes.set(nb.id, nb);
  2119. glueNodeIds.add(nb.id);
  2120. }
  2121. }
  2122. // Named-symbol seeding: findRelevantContext is an FTS/text rank, so a query
  2123. // that's a BAG of symbol names skewed toward one phase (Alamofire: 5 build
  2124. // terms, each a high-frequency name, vs 3 validate terms) lets the
  2125. // lower-frequency names fall below the search cut — their definitions, and
  2126. // whole files (Validation.swift), never get gathered, so they can never
  2127. // render and the agent Reads them. Resolve EACH named token to its
  2128. // substantive definition (skip empty stubs + test files, same relevance the
  2129. // trace endpoint picker uses) and inject it as an entry, so every symbol the
  2130. // agent explicitly named is in the subgraph and its file is scored.
  2131. const namedSeedIds = new Set<string>();
  2132. {
  2133. const FILE_EXT = /\.(?:java|kt|kts|ts|tsx|js|jsx|mjs|cjs|cs|py|go|rb|php|swift|rs|cpp|cc|cxx|c|h|hpp|scala|lua|dart|vue|svelte|astro)$/i;
  2134. const CALLABLE = new Set(['method', 'function', 'component', 'constructor']);
  2135. const isTestPath = (p: string) => /(^|\/)(tests?|specs?|__tests__|testdata|mocks?|fixtures?)\//i.test(p) || /\.(test|spec)\.[a-z]+$/i.test(p);
  2136. const bodyLines = (n: Node) => Math.max(0, (n.endLine ?? n.startLine) - n.startLine);
  2137. const tokens = [...new Set(
  2138. query.split(/[\s,()[\]]+/)
  2139. .map((t) => t.replace(FILE_EXT, '').trim())
  2140. .filter((t) => t.length >= 3 && /^[A-Za-z_$][\w$]*(?:(?:::|\.)[\w$]+)*$/.test(t))
  2141. )].slice(0, 16);
  2142. // PascalCase tokens in the query are type/file disambiguators — when the
  2143. // agent writes "DataRequest task validate", the `task`/`validate` it wants
  2144. // are DataRequest's, NOT the same-named overloads in Validation.swift /
  2145. // Concurrency.swift / the abstract base. Used below to bias overloaded
  2146. // names toward the file/class the query also names. EXCLUDE the project
  2147. // name (a PascalCase token a user naturally includes) — it names the whole
  2148. // repo, so biasing toward it just pulls overloads to whichever stack
  2149. // embeds it, re-burying the rest (#720).
  2150. const projectNameTokens = cg.getProjectNameTokens();
  2151. const typeTokens = tokens.filter(
  2152. (o) => /^[A-Z][A-Za-z0-9]{3,}/.test(o) && !projectNameTokens.has(normalizeNameToken(o)),
  2153. );
  2154. const inNamedContext = (n: Node) =>
  2155. typeTokens.some((ct) => {
  2156. const lc = ct.toLowerCase();
  2157. return n.filePath.toLowerCase().includes(lc) || n.qualifiedName.toLowerCase().includes(lc);
  2158. });
  2159. for (const t of tokens) {
  2160. // Enumerate ALL defs of a bare token via the direct index, not FTS — a
  2161. // 50+-overload name (tokio `poll`) ranks the wanted def (`Harness::poll`)
  2162. // below the FTS cut, so findAllSymbols would never see it and the
  2163. // type-token bias below couldn't pick the harness.rs one. (Same fix as
  2164. // codegraph_node's findSymbolMatches.) Qualified tokens keep findAllSymbols.
  2165. const isQual = /[.\/]|::/.test(t);
  2166. const raw = isQual ? this.findAllSymbols(cg, t).nodes : cg.getNodesByName(t);
  2167. const cands = raw
  2168. .filter((n) => CALLABLE.has(n.kind) && !isTestPath(n.filePath))
  2169. .sort((a, b) => (bodyLines(b) > 1 ? 1 : 0) - (bodyLines(a) > 1 ? 1 : 0) || bodyLines(b) - bodyLines(a));
  2170. // A specific name (<=3 defs) injects all its defs. An overloaded name
  2171. // (`validate` = 10, `request` = 44) would flood the subgraph, so inject
  2172. // only: the overloads whose file/class the query ALSO names (the agent
  2173. // told us which one it wants — DataRequest's, not Validation.swift's),
  2174. // capped; else fall back to the single most-substantive def. This is the
  2175. // explore-side mirror of codegraph_node's overload disambiguation.
  2176. let picks: Node[];
  2177. if (cands.length <= 3) {
  2178. picks = cands;
  2179. } else {
  2180. const ctx = cands.filter(inNamedContext);
  2181. picks = ctx.length > 0 ? ctx.slice(0, 4) : cands.slice(0, 1);
  2182. }
  2183. for (const n of picks) {
  2184. if (!subgraph.nodes.has(n.id)) subgraph.nodes.set(n.id, n);
  2185. // Mark as a named seed EVEN IF the FTS gather already had it — being
  2186. // "named by the agent" is independent of whether search happened to
  2187. // surface it, and it drives the +50 score, the gate, and the
  2188. // named-file sort below. (Previously only NEW injections were marked,
  2189. // so a named symbol FTS already gathered never sorted to the top.)
  2190. namedSeedIds.add(n.id);
  2191. }
  2192. }
  2193. }
  2194. // Step 2: Group nodes by file, score by relevance
  2195. const fileGroups = new Map<string, { nodes: Node[]; score: number }>();
  2196. const entryNodeIds = new Set([...subgraph.roots, ...namedSeedIds]);
  2197. // Build a set of nodes directly connected to entry points (depth 1)
  2198. const connectedToEntry = new Set<string>();
  2199. for (const edge of subgraph.edges) {
  2200. if (entryNodeIds.has(edge.source)) connectedToEntry.add(edge.target);
  2201. if (entryNodeIds.has(edge.target)) connectedToEntry.add(edge.source);
  2202. }
  2203. for (const node of subgraph.nodes.values()) {
  2204. // Skip import/export nodes — they add noise without information
  2205. if (node.kind === 'import' || node.kind === 'export') continue;
  2206. // SECURITY (#383): never render the on-disk source of a config-leaf
  2207. // (Spring application.{yml,properties} key) — its line is `key = <secret>`,
  2208. // so whole-file/cluster rendering here would push secrets into context
  2209. // unbidden. The key still appears in the flow/symbol listing above.
  2210. if (isConfigLeafNode(node)) continue;
  2211. const group = fileGroups.get(node.filePath) || { nodes: [], score: 0 };
  2212. group.nodes.push(node);
  2213. // Score: a NAMED-SEED node (a symbol the agent named that FTS missed, now
  2214. // injected) is worth far more than a mere reference — its file is where the
  2215. // answer lives. Without this, an incidental file that name-drops the flow
  2216. // (Combine.swift references request/task → score 23 from connected nodes)
  2217. // outranks the file that DEFINES a named symbol (Validation.swift's
  2218. // `validate` → 10) and steals its render slot. Definition ≫ reference.
  2219. if (namedSeedIds.has(node.id)) {
  2220. group.score += 50;
  2221. } else if (entryNodeIds.has(node.id)) {
  2222. group.score += 10;
  2223. } else if (connectedToEntry.has(node.id)) {
  2224. group.score += 3;
  2225. } else {
  2226. group.score += 1;
  2227. }
  2228. fileGroups.set(node.filePath, group);
  2229. }
  2230. // Only include files that have entry points or nodes directly connected to entry points
  2231. let relevantFiles = [...fileGroups.entries()].filter(([, group]) => group.score >= 3);
  2232. // Extract query terms for relevance checking
  2233. const queryTerms = query.toLowerCase().split(/\s+/).filter(t => t.length >= 3);
  2234. // Test/spec/icon/i18n file detector — used both for the pre-sort hard
  2235. // filter (tiny tier) and the comparator deprioritization (all tiers).
  2236. const isLowValue = (p: string) => {
  2237. const lp = p.toLowerCase();
  2238. return (
  2239. /\/(tests?|__tests?__|spec)\//.test(lp) ||
  2240. /_test\.go$/.test(lp) ||
  2241. /(?:^|\/)test_[^/]+\.py$/.test(lp) ||
  2242. /_test\.py$/.test(lp) ||
  2243. /_spec\.rb$/.test(lp) ||
  2244. /_test\.rb$/.test(lp) ||
  2245. /\.(test|spec)\.[jt]sx?$/.test(lp) ||
  2246. /(test|spec|tests)\.(java|kt|scala)$/.test(lp) ||
  2247. /(tests?|spec)\.cs$/.test(lp) ||
  2248. /tests?\.swift$/.test(lp) ||
  2249. /_test\.dart$/.test(lp) ||
  2250. /\bicons?\b/.test(lp) ||
  2251. /\bi18n\b/.test(lp)
  2252. );
  2253. };
  2254. // Hard-exclude test/spec files (ALL tiers, not just tiny). One slipped test
  2255. // file dominates the per-file budget on small repos (cobra's `command_test.go`
  2256. // displaced `args.go`) AND wastes budget on large ones (Django's
  2257. // `custom_lookups/tests.py` ate ~2.3 KB of the 28 KB cap, crowding out the
  2258. // SQLCompiler mechanism the agent then Read). A test file almost never answers
  2259. // an architecture question. Skip when the query itself is about tests — the
  2260. // legitimate "explore the tests" case — and only cut if ≥2 non-test candidates
  2261. // remain (else tests are the only signal for this area).
  2262. {
  2263. const queryMentionsTests = /\b(test|tests|testing|spec|verify|verifies)\b/i.test(query);
  2264. if (!queryMentionsTests) {
  2265. const nonLow = relevantFiles.filter(([p]) => !isLowValue(p));
  2266. if (nonLow.length >= 2) {
  2267. relevantFiles = nonLow;
  2268. }
  2269. }
  2270. }
  2271. // Secondary signal: how many DISTINCT query terms each file matches (path +
  2272. // symbol names). Kept only as a tiebreak — the PRIMARY relevance is graph
  2273. // connectivity below. (Term counting alone tied the real central file with
  2274. // incidental same-word matches; it's a weak text signal, not the ranker.)
  2275. const uniqueQueryTerms = [...new Set(queryTerms)].filter(t => t.length >= 3);
  2276. const fileTermHits = new Map<string, number>();
  2277. for (const [fp, group] of relevantFiles) {
  2278. const hay = fp.toLowerCase() + ' ' + group.nodes.map(n => n.name.toLowerCase()).join(' ');
  2279. let hits = 0;
  2280. for (const t of uniqueQueryTerms) if (hay.includes(t)) hits++;
  2281. fileTermHits.set(fp, hits);
  2282. }
  2283. // PRIMARY relevance: graph connectivity (Random-Walk-with-Restart from the
  2284. // matched seeds — see computeGraphRelevance). Aggregate each file's nodes'
  2285. // walk mass. This is the signal text search lacks: the real cluster
  2286. // (org-user.storage.ts, call-connected to the matches) accrues mass; a lone
  2287. // text match (LensSwitcher.swift, matched "switch" but calls nothing in the
  2288. // flow) gets only its restart probability → ~0, and is dropped by the gate.
  2289. const nodeRwr = this.computeGraphRelevance(
  2290. [...subgraph.nodes.keys()], subgraph.edges, entryNodeIds,
  2291. );
  2292. const fileGraphScore = new Map<string, number>();
  2293. for (const node of subgraph.nodes.values()) {
  2294. fileGraphScore.set(
  2295. node.filePath,
  2296. (fileGraphScore.get(node.filePath) ?? 0) + (nodeRwr.get(node.id) ?? 0),
  2297. );
  2298. }
  2299. const maxGraph = Math.max(0, ...fileGraphScore.values());
  2300. // Central file(s): the 1-2 most graph-central files that also match the
  2301. // query textually (so a connected hub-utility with no term match isn't
  2302. // mistaken for the subject). The heart of the answer — they earn the larger
  2303. // WHOLE-FILE ceiling below (a god-file central file still exceeds it and
  2304. // falls to generous full-method sectioning — never a whole dump).
  2305. const centralFiles = new Set(
  2306. [...fileGraphScore.entries()]
  2307. .filter(([fp, g]) => g > 0 && (fileTermHits.get(fp) ?? 0) >= 1)
  2308. .sort((a, b) => b[1] - a[1] || (fileTermHits.get(b[0]) ?? 0) - (fileTermHits.get(a[0]) ?? 0))
  2309. .slice(0, 2)
  2310. .map(([f]) => f),
  2311. );
  2312. // Files that DEFINE a symbol the agent named (or a subgraph root). These are
  2313. // the highest-relevance files there are — the agent asked for them by name —
  2314. // so the connectivity gate below must never drop them, even when their RWR
  2315. // mass is low (a leaf family file like codec.ts is call-connected to little
  2316. // but is exactly what the agent queried). Without this protection the gate
  2317. // prunes a named file and the agent Reads it back.
  2318. const entryFiles = new Set<string>();
  2319. for (const id of entryNodeIds) {
  2320. const n = subgraph.nodes.get(id);
  2321. if (n) entryFiles.add(n.filePath);
  2322. }
  2323. // Relevance gate (so the generous budget is a CEILING, not a target): keep a
  2324. // file only if it is STRUCTURALLY relevant by ANY of:
  2325. // - graph score within a fraction of the top (it's on/near the flow), OR
  2326. // - central (a query entry-point lives here), OR
  2327. // - it DEFINES a symbol the agent named (entryFiles), OR
  2328. // - it matches >= 2 DISTINCT named query terms — a strong text signal that
  2329. // the agent is asking about this file even when nothing calls it (codec.ts:
  2330. // the agent named `encode`/`Codec`/`JsonCodec`, all leaf classes with zero
  2331. // RWR mass — graph alone wrongly drops it).
  2332. // A lone text match on one shared word (LensSwitcher: term=1, g~0) is still
  2333. // dropped, so the budget never fills with incidental files. Guarded so it
  2334. // never prunes below 2.
  2335. if (maxGraph > 0) {
  2336. const gated = relevantFiles.filter(([fp]) =>
  2337. (fileGraphScore.get(fp) ?? 0) >= maxGraph * 0.06
  2338. || centralFiles.has(fp)
  2339. || entryFiles.has(fp)
  2340. || (fileTermHits.get(fp) ?? 0) >= 2,
  2341. );
  2342. if (gated.length >= 2) relevantFiles = gated;
  2343. }
  2344. // Sort files: graph-central first, then distinct-term match, then the
  2345. // existing low-value/generated/score tiebreaks.
  2346. // Files that DEFINE a symbol the agent NAMED. These sort first — ahead of
  2347. // graph connectivity — because the agent asked for them by name. Without
  2348. // this, a named leaf override reached only by dynamic dispatch (Alamofire's
  2349. // `DataRequest.task`/`validate`, low RWR mass) sorts below the high-
  2350. // connectivity abstract base (`Request.swift`) and the same-named overloads
  2351. // in other files (`Validation.swift`), falls outside the budget, and the
  2352. // agent Reads it. The named file is the answer — rank it at the top.
  2353. const namedSeedFiles = new Set<string>();
  2354. for (const id of namedSeedIds) {
  2355. const n = subgraph.nodes.get(id);
  2356. if (n) namedSeedFiles.add(n.filePath);
  2357. }
  2358. // Multi-term corroboration tier: a file that is BOTH (a) an entry/central file
  2359. // (a search root, named seed, or graph-central hub — i.e. structurally part of
  2360. // the answer) AND (b) matched by ≥2 DISTINCT query terms must not be buried by
  2361. // graph-centrality mass that accrued to a denser-but-off-topic cluster. In a
  2362. // cross-layer monorepo (an API server alongside a much larger, internally dense
  2363. // frontend that mirrors the same domain words) the Random-Walk-with-Restart mass
  2364. // — seeded from text matches that skew to the bigger layer — floats hits=0
  2365. // frontend files above the hits=2/3 backend service that IS the answer (its many
  2366. // callers don't help: it's call-isolated from the frontend seed cluster). The
  2367. // entry/central GUARD keeps this safe: an INCIDENTAL multi-term file that is
  2368. // neither entry nor central (a type/util file that matches "element"+x but isn't
  2369. // the flow) is NOT promoted, so it can't displace the graph-central answer file
  2370. // (hits=1) the way a blunt hits-only tier would. Single-layer repos with one
  2371. // cluster are unaffected (no competing mass). Set CODEGRAPH_RANK_NO_MULTITERM=1
  2372. // to disable.
  2373. const MULTITERM_OFF = process.env.CODEGRAPH_RANK_NO_MULTITERM === '1';
  2374. const isCorroborated = (fp: string) =>
  2375. !MULTITERM_OFF &&
  2376. (fileTermHits.get(fp) ?? 0) >= 2 &&
  2377. (entryFiles.has(fp) || centralFiles.has(fp));
  2378. const sortedFiles = relevantFiles.sort((a, b) => {
  2379. const aPath = a[0].toLowerCase();
  2380. const bPath = b[0].toLowerCase();
  2381. // Agent-named files first (it asked for a symbol defined here by name).
  2382. const aNamed = namedSeedFiles.has(a[0]) ? 1 : 0;
  2383. const bNamed = namedSeedFiles.has(b[0]) ? 1 : 0;
  2384. if (aNamed !== bNamed) return bNamed - aNamed;
  2385. // Corroborated (entry/central + ≥2 terms) tier, above the graph signal.
  2386. const aCorr = isCorroborated(a[0]) ? 1 : 0;
  2387. const bCorr = isCorroborated(b[0]) ? 1 : 0;
  2388. if (aCorr !== bCorr) return bCorr - aCorr;
  2389. // Graph connectivity is the next key (small epsilon so near-ties fall
  2390. // through to the text signal rather than coin-flipping on float noise).
  2391. const aG = fileGraphScore.get(a[0]) ?? 0;
  2392. const bG = fileGraphScore.get(b[0]) ?? 0;
  2393. if (Math.abs(aG - bG) > maxGraph * 0.01) return bG - aG;
  2394. const aHits = fileTermHits.get(a[0]) ?? 0;
  2395. const bHits = fileTermHits.get(b[0]) ?? 0;
  2396. if (aHits !== bHits) return bHits - aHits;
  2397. const aLow = isLowValue(aPath);
  2398. const bLow = isLowValue(bPath);
  2399. if (aLow !== bLow) return aLow ? 1 : -1;
  2400. // Deprioritize generated source (.pb.go / .pulsar.go / _mocks.go / …) —
  2401. // the agent rarely needs to see the protobuf scaffold or gomock output
  2402. // when asking about the actual flow, and dumping their bodies inflates
  2403. // the response (the cosmos Q3 explore otherwise leads with
  2404. // `expected_keepers_mocks.go`, displacing the real `tally.go` content
  2405. // and forcing the agent to Read tally.go anyway).
  2406. const aGen = isGeneratedFile(a[0]);
  2407. const bGen = isGeneratedFile(b[0]);
  2408. if (aGen !== bGen) return aGen ? 1 : -1;
  2409. if (a[1].score !== b[1].score) return b[1].score - a[1].score;
  2410. return b[1].nodes.length - a[1].nodes.length;
  2411. });
  2412. // Step 3: Build relationship map
  2413. const lines: string[] = [
  2414. `## Exploration: ${query}`,
  2415. '',
  2416. `Found ${subgraph.nodes.size} symbols across ${fileGroups.size} files.`,
  2417. '',
  2418. ];
  2419. // Blast radius (always-on, compact): for the entry symbols, who depends on
  2420. // them + which tests cover them — locations only, no source — so the agent
  2421. // knows what to update/verify before editing without a separate call.
  2422. const blastRadius = this.buildBlastRadiusSection(cg, subgraph);
  2423. if (blastRadius) lines.push(blastRadius);
  2424. // Relationship map — show how symbols connect
  2425. const significantEdges = subgraph.edges.filter(e =>
  2426. e.kind !== 'contains' // skip contains — it's implied by file grouping
  2427. );
  2428. if (budget.includeRelationships && significantEdges.length > 0) {
  2429. lines.push('### Relationships');
  2430. lines.push('');
  2431. // Group edges by kind for readability
  2432. const byKind = new Map<string, Array<{ source: string; target: string }>>();
  2433. for (const edge of significantEdges) {
  2434. const sourceNode = subgraph.nodes.get(edge.source);
  2435. const targetNode = subgraph.nodes.get(edge.target);
  2436. if (!sourceNode || !targetNode) continue;
  2437. const group = byKind.get(edge.kind) || [];
  2438. group.push({ source: sourceNode.name, target: targetNode.name });
  2439. byKind.set(edge.kind, group);
  2440. }
  2441. for (const [kind, edges] of byKind) {
  2442. const cap = budget.maxEdgesPerRelationshipKind;
  2443. const shown = edges.slice(0, cap);
  2444. lines.push(`**${kind}:**`);
  2445. for (const e of shown) {
  2446. lines.push(`- ${e.source} → ${e.target}`);
  2447. }
  2448. if (edges.length > cap) {
  2449. lines.push(`- ... and ${edges.length - cap} more`);
  2450. }
  2451. lines.push('');
  2452. }
  2453. }
  2454. // Step 4: Read contiguous file sections
  2455. // Compute the flow spine once — used both to prepend the Flow section (below)
  2456. // and to gate adaptive source sizing: files on the spine get full source,
  2457. // off-spine peers skeletonize.
  2458. const flow = this.buildFlowFromNamedSymbols(cg, query);
  2459. // Polymorphic-sibling detector for adaptive sizing. A class that implements/
  2460. // extends a supertype shared by >= MIN_SIBLINGS classes is one of many
  2461. // INTERCHANGEABLE implementations (OkHttp's 14 `: Interceptor` classes —
  2462. // showing one + the rest as signatures is enough), as opposed to a DISTINCT
  2463. // pipeline step (Excalidraw's `renderStaticScene`, which shares no supertype and
  2464. // must stay full or the agent loses real content). Only off-spine sibling files
  2465. // skeletonize; distinct steps and on-spine files keep full source. Cache
  2466. // supertype→(has ≥N implementers) so this stays a handful of edge queries.
  2467. const MIN_SIBLINGS = 3;
  2468. const siblingSuper = new Map<string, boolean>();
  2469. const isPolymorphicSibling = (nodes: Node[]): boolean => {
  2470. for (const n of nodes) {
  2471. for (const e of cg.getOutgoingEdges(n.id)) {
  2472. if (e.kind !== 'implements' && e.kind !== 'extends') continue;
  2473. let many = siblingSuper.get(e.target);
  2474. if (many === undefined) {
  2475. many = cg.getIncomingEdges(e.target)
  2476. .filter((x) => x.kind === 'implements' || x.kind === 'extends').length >= MIN_SIBLINGS;
  2477. siblingSuper.set(e.target, many);
  2478. }
  2479. if (many) return true;
  2480. }
  2481. }
  2482. return false;
  2483. };
  2484. // A file that DEFINES a polymorphic supertype (a class/interface with ≥
  2485. // MIN_SIBLINGS implementers) AND co-locates its subclasses is a redundant
  2486. // "family" file — Django's compiler.py holds `SQLCompiler` + its 4 subclasses
  2487. // (SQLInsert/Update/Delete/AggregateCompiler) in 2,266 lines. Such files are
  2488. // huge and read-anyway, so they should STILL skeletonize even when the agent
  2489. // named a method in them: a full one eats ~6.5K of the explore budget (Django
  2490. // is pinned at the 28K cap, truncating), starving the sibling files the agent
  2491. // then Reads. This flag OVERRIDES the named-callable spare below — it does NOT
  2492. // by itself spare a file. (OkHttp's RealCall implements the `Lockable` mixin
  2493. // but defines no ≥3-impl supertype, so the named spare keeps it full.)
  2494. const superMany = new Map<string, boolean>();
  2495. const definesPolymorphicSupertype = (nodes: Node[]): boolean => {
  2496. for (const n of nodes) {
  2497. if (n.kind !== 'class' && n.kind !== 'interface' && n.kind !== 'struct'
  2498. && n.kind !== 'trait' && n.kind !== 'protocol' && n.kind !== 'type_alias') continue;
  2499. let many = superMany.get(n.id);
  2500. if (many === undefined) {
  2501. many = cg.getIncomingEdges(n.id)
  2502. .filter((x) => x.kind === 'implements' || x.kind === 'extends').length >= MIN_SIBLINGS;
  2503. superMany.set(n.id, many);
  2504. }
  2505. if (many) return true;
  2506. }
  2507. return false;
  2508. };
  2509. lines.push('### Source Code');
  2510. lines.push('');
  2511. lines.push('> The code below is the **verbatim, current on-disk source** of these files — re-read from disk on this call and line-numbered, byte-for-byte identical to what the Read tool returns. It is NOT a summary, outline, or stale cache. Treat each block as a Read you have already performed: do not Read a file shown here.');
  2512. lines.push('');
  2513. let totalChars = lines.join('\n').length;
  2514. let filesIncluded = 0;
  2515. let anyFileTrimmed = false;
  2516. for (const [filePath, group] of sortedFiles) {
  2517. if (filesIncluded >= maxFiles) break;
  2518. // A file DEFINES a named/spine symbol (the answer) vs merely references the
  2519. // flow. Past 90% budget, stop pulling INCIDENTAL files — but keep scanning
  2520. // for necessary ones, which render even past the cap (bounded by maxFiles).
  2521. // Without this `continue` (was an unconditional `break`), the loop stopped
  2522. // after the build + validators-exec files and never reached the ranked-in
  2523. // validate-logic file (Alamofire's Validation.swift).
  2524. const fileNecessary = group.nodes.some(n =>
  2525. entryNodeIds.has(n.id) || flow.pathNodeIds.has(n.id) || flow.uniqueNamedNodeIds.has(n.id));
  2526. if (!fileNecessary && totalChars > budget.maxOutputChars * 0.9) continue;
  2527. const absPath = validatePathWithinRoot(projectRoot, filePath);
  2528. if (!absPath || !existsSync(absPath)) continue;
  2529. let fileContent: string;
  2530. try {
  2531. fileContent = readFileSync(absPath, 'utf-8');
  2532. } catch {
  2533. continue;
  2534. }
  2535. const fileLines = fileContent.split('\n');
  2536. const lang = group.nodes[0]?.language || '';
  2537. // Adaptive sizing (CODEGRAPH_ADAPTIVE_EXPLORE, default on): collapse a file
  2538. // to a per-symbol view when it's a redundant member of a polymorphic family.
  2539. // Engages iff ALL hold:
  2540. // 1. a flow spine exists,
  2541. // 2. no symbol in the file is on that spine (it's not the mechanism path),
  2542. // 3. it IS a polymorphic sibling (≥ MIN_SIBLINGS impls of a shared supertype),
  2543. // 4. it is NOT SPARED, where a file is spared iff the agent named a
  2544. // (near-)UNIQUE callable in it (`getResponseWithInterceptorChain`, 1 def →
  2545. // keep RealCall.kt full) UNLESS the file DEFINES the family supertype (a
  2546. // base+subclasses "family" file like Django's compiler.py — collapse it).
  2547. // Uniqueness matters: `as_sql` has 110 defs across every Compiler/Expression
  2548. // subclass; naming it must NOT keep every backend variant + test file full
  2549. // and flood the budget. That's why the spare reads uniqueNamedNodeIds.
  2550. // Within a collapsed file the render is PER-SYMBOL (condition B): a method the
  2551. // agent NAMED or that's on the spine is shown with its FULL body (so the agent
  2552. // doesn't Read the file back for it — Django's SQLCompiler.execute_sql/as_sql);
  2553. // every other symbol is just its signature. So the base mechanism survives while
  2554. // the file's other ~80 symbols + the redundant subclasses collapse to one line each.
  2555. const spareNamed = group.nodes.some(n => flow.uniqueNamedNodeIds.has(n.id));
  2556. const fileDefinesSuper = definesPolymorphicSupertype(group.nodes);
  2557. const spared = spareNamed && !fileDefinesSuper;
  2558. const CALLABLE_BODY = new Set(['method', 'function', 'constructor', 'component']);
  2559. const hasSpineNode = group.nodes.some(n => flow.pathNodeIds.has(n.id));
  2560. // On-spine god-file: the flow path runs THROUGH this file, but it also holds
  2561. // many OTHER named methods, and rendering all of them in full blows the
  2562. // per-file budget and starves the other flow files (Alamofire: the agent
  2563. // names ~7 Session.swift methods — the build spine PLUS off-path
  2564. // task/didCompleteTask — far past the whole response budget). Engage the
  2565. // per-symbol view to keep the SPINE full and collapse the off-path named
  2566. // methods to signatures. Only when there IS off-path content to shed —
  2567. // otherwise the spine is irreducible (a sequential flow has no redundancy),
  2568. // so leave it to the normal full render.
  2569. const namedBodyChars = group.nodes
  2570. .filter(n => CALLABLE_BODY.has(n.kind) && (flow.pathNodeIds.has(n.id) || flow.uniqueNamedNodeIds.has(n.id)))
  2571. .reduce((s, n) => s + fileLines.slice(n.startLine - 1, n.endLine).join('\n').length, 0);
  2572. const onSpineGodFile = hasSpineNode
  2573. && namedBodyChars > budget.maxCharsPerFile
  2574. && group.nodes.some(n => CALLABLE_BODY.has(n.kind) && flow.uniqueNamedNodeIds.has(n.id) && !flow.pathNodeIds.has(n.id));
  2575. if (adaptiveExploreEnabled() && flow.pathNodeIds.size > 0
  2576. && (onSpineGodFile || (!hasSpineNode && isPolymorphicSibling(group.nodes) && !spared))) {
  2577. const syms = group.nodes
  2578. .filter(n => n.kind !== 'import' && n.kind !== 'export' && n.startLine > 0)
  2579. .sort((a, b) => a.startLine - b.startLine);
  2580. // Pass 1: choose which symbols get a FULL body, by priority, greedily within
  2581. // a per-file body cap — so one huge family file can't body every named method
  2582. // and crowd out the other flow files (Django's query.py). A symbol earns a
  2583. // body if it's on-spine, or UNIQUELY named (`SQLCompiler.execute_sql`), or a
  2584. // co-named method WHEN this file DEFINES the family supertype (so the base
  2585. // `SQLCompiler.as_sql` body shows, but the 110 leaf `as_sql` overrides — and
  2586. // OkHttp's 5 `intercept`s if the agent names `intercept` — stay signatures).
  2587. const prio = (n: Node) => !CALLABLE_BODY.has(n.kind) ? 99
  2588. : flow.pathNodeIds.has(n.id) ? 0
  2589. : flow.uniqueNamedNodeIds.has(n.id) ? 1
  2590. : (fileDefinesSuper && flow.namedNodeIds.has(n.id)) ? 2 : 99;
  2591. // One ~250-line WINDOW per file. syms are taken by priority (spine first,
  2592. // then uniquely-named, then family-base), and the cap applies to ALL of
  2593. // them — including the spine — so a big-spine god-file (tokio's worker.rs:
  2594. // run→run_task→next_task→steal_work) can't eat the whole response and
  2595. // starve the co-flow file (harness.rs's poll). The native agent windows
  2596. // such a file too (~190 lines at a time), so this mimics, not truncates.
  2597. // Always emit ≥1 (never an empty section).
  2598. const bodyCap = budget.maxCharsPerFile * 1.5;
  2599. const bodyIds = new Set<string>();
  2600. let bodyChars = 0;
  2601. for (const n of syms.filter(n => prio(n) < 99 && n.endLine >= n.startLine).sort((a, b) => prio(a) - prio(b))) {
  2602. const sz = fileLines.slice(n.startLine - 1, n.endLine).join('\n').length;
  2603. if (bodyChars + sz > bodyCap && bodyIds.size > 0) continue;
  2604. bodyIds.add(n.id);
  2605. bodyChars += sz;
  2606. }
  2607. // Pass 2: render in line order — full body for chosen symbols, else the
  2608. // signature line (capped, with a "+N more" tail so the structure map of a
  2609. // god-file doesn't itself bloat the budget).
  2610. const skel: string[] = [];
  2611. let coveredUntil = 0; // skip symbols already inside an emitted body
  2612. let sigCount = 0, sigDropped = 0;
  2613. const SIG_MAX = Math.max(12, budget.maxSymbolsInFileHeader * 2);
  2614. for (const n of syms) {
  2615. if (n.startLine <= coveredUntil) continue;
  2616. if (bodyIds.has(n.id)) {
  2617. const end = n.endLine;
  2618. const body = fileLines.slice(n.startLine - 1, end).join('\n');
  2619. skel.push(exploreLineNumbersEnabled() ? numberSourceLines(body, n.startLine) : body);
  2620. coveredUntil = end;
  2621. } else {
  2622. // Elide the body, emit the signature. node.startLine can point at a
  2623. // decorator/annotation, so scan forward for the line that names the symbol.
  2624. let lineNo = n.startLine;
  2625. for (let k = 0; k < 4; k++) {
  2626. if ((fileLines[n.startLine - 1 + k] || '').includes(n.name)) { lineNo = n.startLine + k; break; }
  2627. }
  2628. if (lineNo <= coveredUntil) continue;
  2629. if (sigCount >= SIG_MAX) { sigDropped++; continue; }
  2630. const sig = (fileLines[lineNo - 1] || '').trim();
  2631. if (sig) { skel.push(exploreLineNumbersEnabled() ? `${lineNo}\t${sig}` : sig); sigCount++; }
  2632. }
  2633. }
  2634. if (sigDropped > 0) skel.push(`… +${sigDropped} more (signatures elided)`);
  2635. if (skel.length > 0) {
  2636. const names = [...new Set(group.nodes.filter(n => n.kind !== 'import' && n.kind !== 'export').map(n => n.name))]
  2637. .slice(0, budget.maxSymbolsInFileHeader).join(', ');
  2638. // Steer the agent to codegraph_explore for an elided body — NEVER to
  2639. // Read. The old "Read for more" / "Read for a full body" tags invited
  2640. // a Read of the very file just skeletonized; on a central, wanted file
  2641. // (Session.swift, DataRequest.swift) that fired an over-investigation
  2642. // spiral (the agent Read the skeletonized file, then kept digging).
  2643. // CLAUDE.md: explore output must never tell the agent to Read.
  2644. const tag = bodyIds.size > 0
  2645. ? 'focused (the methods you named in full, the rest as signatures — codegraph_explore a signature by name for its body; do NOT Read)'
  2646. : 'skeleton (signatures only — codegraph_explore a name for its full body; do NOT Read)';
  2647. lines.push(`#### ${filePath} — ${names} · ${tag}`, '', '```' + lang, skel.join('\n'), '```', '');
  2648. totalChars += skel.join('\n').length + 120;
  2649. filesIncluded++;
  2650. continue;
  2651. }
  2652. }
  2653. // Whole-file rule: if a relevant file is small enough to afford, return it
  2654. // ENTIRELY instead of clustering. Clustering exists to tame god-files
  2655. // (App.tsx ~13k lines); on a ~134-line component a cluster is a lossy
  2656. // subset of a file the agent will just Read in full anyway — costing a
  2657. // round-trip and a re-read every later turn. Reserve clustering for files
  2658. // too big to ship whole. Still bounded by the total maxOutputChars check.
  2659. //
  2660. // CENTRAL files (where the query's entry points live) get a larger — but
  2661. // bounded — ceiling: they're the heart of the answer, the file(s) the agent
  2662. // would Read whole, so a genuinely small one comes back whole rather than as
  2663. // thin clusters. A LARGE central file (the 791-line org-user store) exceeds
  2664. // the ceiling and falls through to sectioning/clustering below — full method
  2665. // bodies + signatures — so we never dump (or overflow on) a whole god-file.
  2666. const isCentralFile = centralFiles.has(filePath);
  2667. // Central files get a slightly larger whole-file window than peripheral ones,
  2668. // but a TIGHT one (~1.5× the per-file cap): the native read of a central file
  2669. // is a ~150–250 line orientation window, NOT the whole file. A flat "whole
  2670. // central file" both overflowed the inline cap AND starved the co-flow files
  2671. // (worker.rs ate the budget, dropping harness.rs's poll). A larger central
  2672. // file falls through to per-method windowing/clustering below.
  2673. const WHOLE_FILE_MAX_LINES = isCentralFile ? 280 : 220;
  2674. const WHOLE_FILE_MAX_CHARS = isCentralFile
  2675. ? Math.min(Math.max(0, budget.maxOutputChars - totalChars - 200), Math.round(budget.maxCharsPerFile * 1.5))
  2676. : budget.maxCharsPerFile * 3;
  2677. if (fileLines.length <= WHOLE_FILE_MAX_LINES && fileContent.length <= WHOLE_FILE_MAX_CHARS) {
  2678. const body = fileContent.replace(/\n+$/, '');
  2679. let wholeSection = exploreLineNumbersEnabled() ? numberSourceLines(body, 1) : body;
  2680. const uniqSymbols = [...new Set(
  2681. group.nodes
  2682. .filter(n => n.kind !== 'import' && n.kind !== 'export')
  2683. .map(n => `${n.name}(${n.kind})`)
  2684. )];
  2685. const headerNames = uniqSymbols.slice(0, budget.maxSymbolsInFileHeader);
  2686. const omitted = uniqSymbols.length - headerNames.length;
  2687. const wholeHeader = `#### ${filePath} — ${omitted > 0 ? `${headerNames.join(', ')}, +${omitted} more` : headerNames.join(', ')}`;
  2688. if (!fileNecessary && totalChars + wholeSection.length + 200 > budget.maxOutputChars) {
  2689. // Don't slice a whole file mid-method: an incidental file that doesn't
  2690. // fit is skipped; a necessary one (below) renders in full. Half a file
  2691. // forces the Read this is meant to prevent.
  2692. anyFileTrimmed = true;
  2693. continue;
  2694. }
  2695. lines.push(wholeHeader, '', '```' + lang, wholeSection, '```', '');
  2696. totalChars += wholeSection.length + 200;
  2697. filesIncluded++;
  2698. continue;
  2699. }
  2700. // Cluster nearby symbols to avoid reading huge gaps between distant symbols.
  2701. // Sort by start line, then merge overlapping/adjacent ranges (within the
  2702. // adaptive gap threshold). Include both node ranges AND edge source
  2703. // locations so template sections with component usages/calls are
  2704. // covered (not just script block symbols).
  2705. //
  2706. // Each range carries an `importance` score so we can rank clusters
  2707. // when the per-file budget forces us to drop some: entry-point nodes
  2708. // are worth 10, directly-connected nodes 3, peripheral nodes 1, and
  2709. // bare edge-source lines 2 (less than a connected node but more than
  2710. // a peripheral one — they hint at a reference but aren't a definition).
  2711. // Container kinds whose body can span most/all of a file. When such a
  2712. // node covers most of the file we drop it from the ranges: keeping it
  2713. // would merge every method inside it into one giant cluster spanning
  2714. // the whole file, which then tail-trims down to just the container's
  2715. // opening lines (its header/declarations) and buries the methods the
  2716. // query actually asked about (#185 follow-up — Session.swift in
  2717. // Alamofire is the canonical case: the `Session` class spans ~1,400
  2718. // lines). We want the granular symbols inside, not the envelope.
  2719. const ENVELOPE_KINDS = new Set(['file', 'module', 'class', 'struct', 'interface', 'enum', 'namespace', 'protocol', 'trait', 'component']);
  2720. // Cluster from this file's gathered nodes PLUS any callable the agent NAMED that
  2721. // lives here. Explore's relevance gather can miss a named method def in a huge
  2722. // non-sibling file — Django's query.py is 3,040 lines and `_fetch_all` (L2237)
  2723. // was gathered only as call-reference edges, never as a def, so it formed no
  2724. // cluster and the agent Read it back. Inject named defs directly and rank them
  2725. // ABOVE connected/glue nodes (importance 9) so their cluster wins the per-file
  2726. // budget — the agent explicitly asked for these symbols.
  2727. const rangeNodes = new Map<string, Node>();
  2728. for (const n of group.nodes) if (n.startLine > 0 && n.endLine > 0) rangeNodes.set(n.id, n);
  2729. for (const id of flow.namedNodeIds) {
  2730. if (rangeNodes.has(id)) continue;
  2731. const n = cg.getNode(id);
  2732. if (n && n.filePath === filePath && n.startLine > 0 && n.endLine > 0) rangeNodes.set(id, n);
  2733. }
  2734. const ranges: Array<{ start: number; end: number; name: string; kind: string; importance: number; spine: boolean; spineCallLine?: number }> = [...rangeNodes.values()]
  2735. // Drop whole-file envelope nodes (containers covering >50% of the file).
  2736. .filter(n => !(ENVELOPE_KINDS.has(n.kind) && (n.endLine - n.startLine + 1) > fileLines.length * 0.5))
  2737. .map(n => {
  2738. let importance = 1;
  2739. if (entryNodeIds.has(n.id)) importance = 10;
  2740. else if (flow.namedNodeIds.has(n.id)) importance = 9; // agent named it → keep its cluster
  2741. else if (glueNodeIds.has(n.id)) importance = 6; // bridging caller/callee of an entry
  2742. else if (connectedToEntry.has(n.id)) importance = 3;
  2743. // On the rendered call-path spine? That IS the flow answer — its cluster
  2744. // must never be dropped by the per-file budget (n8n's huge workflow-execute.ts:
  2745. // processRunExecutionData, the named flow ENTRY at L1562, is a large
  2746. // low-density method that lost the budget to denser blocks and got cut, so
  2747. // the agent Read it back — the very thing explore exists to prevent).
  2748. return { start: n.startLine, end: n.endLine, name: n.name, kind: n.kind, importance, spine: flow.pathNodeIds.has(n.id), spineCallLine: flow.spineCallSites.get(n.id) };
  2749. });
  2750. // Add edge source locations in this file — captures template references
  2751. // (component usages, event handlers) that aren't nodes themselves.
  2752. // Query edges directly from the DB (not just the subgraph) because BFS
  2753. // traversal may have pruned template reference targets due to node budget.
  2754. const edgeLines = new Set<string>(); // dedup by "line:name"
  2755. for (const node of group.nodes) {
  2756. const outgoing = cg.getOutgoingEdges(node.id);
  2757. for (const edge of outgoing) {
  2758. if (!edge.line || edge.line <= 0 || edge.kind === 'contains') continue;
  2759. const key = `${edge.line}:${edge.target}`;
  2760. if (edgeLines.has(key)) continue;
  2761. edgeLines.add(key);
  2762. // Look up target name from subgraph first, fall back to edge kind
  2763. const targetNode = subgraph.nodes.get(edge.target);
  2764. const targetName = targetNode?.name ?? edge.kind;
  2765. ranges.push({ start: edge.line, end: edge.line, name: targetName, kind: edge.kind, importance: 2, spine: false });
  2766. }
  2767. }
  2768. ranges.sort((a, b) => a.start - b.start);
  2769. if (ranges.length === 0) continue;
  2770. const gapThreshold = budget.gapThreshold;
  2771. const clusters: Array<{ start: number; end: number; symbols: string[]; score: number; maxImportance: number; hasSpine: boolean; spineCallLine?: number }> = [];
  2772. let current = {
  2773. start: ranges[0]!.start,
  2774. end: ranges[0]!.end,
  2775. symbols: [`${ranges[0]!.name}(${ranges[0]!.kind})`],
  2776. score: ranges[0]!.importance,
  2777. maxImportance: ranges[0]!.importance,
  2778. hasSpine: ranges[0]!.spine,
  2779. spineCallLine: ranges[0]!.spineCallLine,
  2780. };
  2781. for (let i = 1; i < ranges.length; i++) {
  2782. const r = ranges[i]!;
  2783. if (r.start <= current.end + gapThreshold) {
  2784. current.end = Math.max(current.end, r.end);
  2785. current.symbols.push(`${r.name}(${r.kind})`);
  2786. current.score += r.importance;
  2787. current.maxImportance = Math.max(current.maxImportance, r.importance);
  2788. current.hasSpine = current.hasSpine || r.spine;
  2789. current.spineCallLine = current.spineCallLine ?? r.spineCallLine;
  2790. } else {
  2791. clusters.push(current);
  2792. current = {
  2793. start: r.start,
  2794. end: r.end,
  2795. symbols: [`${r.name}(${r.kind})`],
  2796. score: r.importance,
  2797. maxImportance: r.importance,
  2798. hasSpine: r.spine,
  2799. spineCallLine: r.spineCallLine,
  2800. };
  2801. }
  2802. }
  2803. clusters.push(current);
  2804. // Build file section output from clusters, capped by per-file budget.
  2805. // The pathological case (#185): a file like Session.swift where every
  2806. // method is adjacent collapses into one cluster spanning the whole
  2807. // file, and dumping that into the agent's context is most of the
  2808. // token cost on small projects. We pick clusters in priority order
  2809. // until the per-file char cap is hit. Truly enormous single clusters
  2810. // get tail-trimmed with a marker.
  2811. const contextPadding = 3;
  2812. const withLineNumbers = exploreLineNumbersEnabled();
  2813. // Language-neutral separator (no `//` — not a comment in Python, Ruby,
  2814. // etc.). With line numbers on, the line-number jump also signals the gap.
  2815. const GAP_MARKER = '\n\n... (gap) ...\n\n';
  2816. // An oversize spine method (the call path runs THROUGH a god-method — n8n's
  2817. // processRunExecutionData is 962 lines) is windowed to its next-hop CALL site
  2818. // plus the signature head, NOT dumped whole. Without this the cluster is too big
  2819. // for any per-file cap and gets dropped, so the agent Reads the method back —
  2820. // the exact gap this closes. Bounded, so a god-method can't blow the budget yet
  2821. // the spine's call still appears in context.
  2822. const OVERSIZE_SPINE_LINES = 200;
  2823. const SPINE_WINDOW = 28; // lines each side of the next-hop call site
  2824. const buildSection = (c: { start: number; end: number; hasSpine?: boolean; spineCallLine?: number }): string => {
  2825. if (c.hasSpine && c.spineCallLine && (c.end - c.start + 1) > OVERSIZE_SPINE_LINES) {
  2826. const call = c.spineCallLine;
  2827. const winStart = Math.max(c.start, call - SPINE_WINDOW);
  2828. const winEnd = Math.min(c.end, call + SPINE_WINDOW);
  2829. const parts: string[] = [];
  2830. // Signature head, only when it sits clearly above the window (else the
  2831. // window already covers the method opening).
  2832. const headEnd = Math.min(c.start + 4, winStart - 2);
  2833. if (headEnd >= c.start) {
  2834. const head = fileLines.slice(c.start - 1, headEnd).join('\n');
  2835. parts.push(withLineNumbers ? numberSourceLines(head, c.start) : head);
  2836. }
  2837. const win = fileLines.slice(winStart - 1, winEnd).join('\n');
  2838. parts.push(withLineNumbers ? numberSourceLines(win, winStart) : win);
  2839. return parts.join(GAP_MARKER);
  2840. }
  2841. const startIdx = Math.max(0, c.start - 1 - contextPadding);
  2842. const endIdx = Math.min(fileLines.length, c.end + contextPadding);
  2843. const slice = fileLines.slice(startIdx, endIdx).join('\n');
  2844. // startIdx is 0-based, so the slice's first line is line startIdx + 1.
  2845. return withLineNumbers ? numberSourceLines(slice, startIdx + 1) : slice;
  2846. };
  2847. // Rank clusters for inclusion under the per-file cap. Entry-point
  2848. // clusters come first: a cluster containing a query entry point
  2849. // (importance 10) must outrank a dense block of mere declarations,
  2850. // otherwise on a large file like Session.swift the top-of-file class
  2851. // header + property list (many adjacent low-importance nodes, high
  2852. // density) wins the budget and buries the actual methods the query
  2853. // asked about (perform/didCreateURLRequest/task live deep in the
  2854. // file). Within the same importance tier, prefer density (score per
  2855. // line) so we still favor focused clusters over sprawling ones, then
  2856. // smaller span as a cheap-to-include tiebreak.
  2857. const rankedClusters = clusters
  2858. .map((c, i) => ({ idx: i, span: c.end - c.start + 1, c }))
  2859. .sort((a, b) => {
  2860. // Spine clusters first — the rendered call path IS the flow answer, so it
  2861. // outranks any denser block of peripheral declarations (a low-density entry
  2862. // method must not lose the budget to them). Within spine / within non-spine,
  2863. // the existing importance → density → score → span order holds.
  2864. if (a.c.hasSpine !== b.c.hasSpine) return (b.c.hasSpine ? 1 : 0) - (a.c.hasSpine ? 1 : 0);
  2865. if (b.c.maxImportance !== a.c.maxImportance) return b.c.maxImportance - a.c.maxImportance;
  2866. const densityA = a.c.score / a.span;
  2867. const densityB = b.c.score / b.span;
  2868. if (densityB !== densityA) return densityB - densityA;
  2869. if (b.c.score !== a.c.score) return b.c.score - a.c.score;
  2870. return a.span - b.span;
  2871. });
  2872. // Per-file budget is the SMALLER of the per-file cap and what's left of the
  2873. // total output cap — so selection (which ranks by importance) keeps the
  2874. // high-importance clusters and drops peripheral ones, instead of the
  2875. // downstream source-order trim slicing off whatever comes last in the file.
  2876. // That source-order slice is what cut Django's `_fetch_all` (L2237, importance
  2877. // 9 — agent-named) when query.py was the last of four big files to be emitted.
  2878. const fileBudget = Math.min(budget.maxCharsPerFile, Math.max(0, budget.maxOutputChars - totalChars - 200));
  2879. // Spine ceiling: a flow-path cluster may exceed the per-file cap (the call
  2880. // path is the answer), but bounded — at most ~2.5× the per-file cap and never
  2881. // past what's left of the total output cap — so a pathological long in-file
  2882. // spine can't run away or starve co-flow files entirely.
  2883. const SPINE_CEILING = Math.min(budget.maxCharsPerFile * 2.5, Math.max(0, budget.maxOutputChars - totalChars - 200));
  2884. const chosenIndices = new Set<number>();
  2885. let projectedChars = 0;
  2886. for (const rc of rankedClusters) {
  2887. const sectionLen = buildSection(rc.c).length + (chosenIndices.size > 0 ? GAP_MARKER.length : 0);
  2888. // Always take the top-ranked cluster, even if oversize, so we don't
  2889. // return an empty file section (agent would then re-Read the file,
  2890. // negating the savings).
  2891. if (chosenIndices.size === 0) {
  2892. chosenIndices.add(rc.idx);
  2893. projectedChars += sectionLen;
  2894. continue;
  2895. }
  2896. // A spine cluster (the rendered call path) is the flow answer — include it
  2897. // past the per-file budget up to the spine ceiling; non-spine clusters obey
  2898. // the normal per-file budget.
  2899. const fits = projectedChars + sectionLen <= fileBudget;
  2900. const spineFits = rc.c.hasSpine && projectedChars + sectionLen <= SPINE_CEILING;
  2901. if (!fits && !spineFits) continue;
  2902. chosenIndices.add(rc.idx);
  2903. projectedChars += sectionLen;
  2904. }
  2905. // Emit chosen clusters in source order so the file reads top-to-bottom.
  2906. let fileSection = '';
  2907. const allSymbols: string[] = [];
  2908. for (let i = 0; i < clusters.length; i++) {
  2909. if (!chosenIndices.has(i)) continue;
  2910. const cluster = clusters[i]!;
  2911. const section = buildSection(cluster);
  2912. if (fileSection.length > 0) fileSection += GAP_MARKER;
  2913. fileSection += section;
  2914. allSymbols.push(...cluster.symbols);
  2915. }
  2916. // A chosen cluster is a COMPLETE method-range — we never cut through a body.
  2917. // An oversize single cluster (a long monolithic function) renders in FULL:
  2918. // half a method is useless (the agent just Reads the rest for the other half),
  2919. // which is the very fallback explore exists to prevent. A pathological file is
  2920. // bounded by the per-file cluster SELECTION above + the total hard ceiling.
  2921. if (chosenIndices.size < clusters.length) {
  2922. anyFileTrimmed = true;
  2923. }
  2924. // Dedupe + cap the symbols list shown in the per-file header. Some
  2925. // files (Session.swift in Alamofire) produced 3.4KB symbol lists
  2926. // from cluster scoring + edge-source lines, dwarfing the per-file
  2927. // body cap. Show top names by frequency, with a "+N more" tail.
  2928. const symbolCounts = new Map<string, number>();
  2929. for (const s of allSymbols) {
  2930. symbolCounts.set(s, (symbolCounts.get(s) ?? 0) + 1);
  2931. }
  2932. const sortedSymbols = [...symbolCounts.entries()]
  2933. .sort((a, b) => b[1] - a[1])
  2934. .map(([name]) => name);
  2935. const headerCap = budget.maxSymbolsInFileHeader;
  2936. const headerSymbols = sortedSymbols.slice(0, headerCap);
  2937. const omittedCount = sortedSymbols.length - headerSymbols.length;
  2938. const headerSuffix = omittedCount > 0
  2939. ? `${headerSymbols.join(', ')}, +${omittedCount} more`
  2940. : headerSymbols.join(', ');
  2941. const fileHeader = `#### ${filePath} — ${headerSuffix}`;
  2942. // The total cap bounds INCIDENTAL files only. A file that DEFINES a symbol
  2943. // the agent named (or that's on the flow spine) renders even when the
  2944. // nominal total is used up — it's the answer, and the set is bounded by
  2945. // maxFiles AND by true-spine/named-seeding having already trimmed each file
  2946. // to its necessary content. A file that merely REFERENCES the flow
  2947. // (Combine.swift name-drops request/task) is incidental → still capped, so
  2948. // freed budget never leaks into noise. This is the last god-file layer:
  2949. // build (Session, true-spined) + validators-exec (Request) + validate
  2950. // (DataRequest/Validation) all render, instead of the cap dropping whichever
  2951. // phase the file order happened to put last.
  2952. if (!fileNecessary && totalChars + fileSection.length + 200 > budget.maxOutputChars) {
  2953. // Incidental file that doesn't fit: SKIP it whole — never slice mid-method.
  2954. // Keep scanning for necessary files (which bypass this cap and render in
  2955. // full, bounded by the hard ceiling).
  2956. anyFileTrimmed = true;
  2957. continue;
  2958. }
  2959. lines.push(fileHeader);
  2960. lines.push('');
  2961. lines.push('```' + lang);
  2962. lines.push(fileSection);
  2963. lines.push('```');
  2964. lines.push('');
  2965. totalChars += fileSection.length + 200;
  2966. filesIncluded++;
  2967. }
  2968. // Add remaining files as references (from both relevant and peripheral files).
  2969. // Small projects (per budget) skip this — the relevant story already fits
  2970. // in the source section, and a trailing pointer list is pure overhead.
  2971. if (budget.includeAdditionalFiles) {
  2972. const remainingRelevant = sortedFiles.slice(filesIncluded);
  2973. const peripheralFiles = [...fileGroups.entries()]
  2974. .filter(([, group]) => group.score < 3)
  2975. .sort((a, b) => b[1].score - a[1].score);
  2976. const remainingFiles = [...remainingRelevant, ...peripheralFiles];
  2977. if (remainingFiles.length > 0) {
  2978. lines.push('### Not shown above — explore these names for their source');
  2979. lines.push('');
  2980. for (const [filePath, group] of remainingFiles.slice(0, 10)) {
  2981. const symbols = group.nodes.map(n => `${n.name}:${n.startLine}`).join(', ');
  2982. lines.push(`- ${filePath}: ${symbols}`);
  2983. }
  2984. if (remainingFiles.length > 10) {
  2985. lines.push(`- ... and ${remainingFiles.length - 10} more files`);
  2986. }
  2987. }
  2988. }
  2989. // Add completeness signal so agents know they don't need to re-read these files.
  2990. // On small projects the budget gates this off — but if we actually had to
  2991. // trim or drop clusters, surface a brief note so the agent knows it can
  2992. // still Read for more detail.
  2993. if (budget.includeCompletenessSignal) {
  2994. lines.push('');
  2995. lines.push('---');
  2996. lines.push(`> **Complete source for ${filesIncluded} files is included above — do NOT re-read them.** If your question also needs files/symbols listed under "Not shown above" (or any area this call didn't cover), make ANOTHER codegraph_explore targeting those names — it returns the same source with line numbers and is cheaper and more complete than reading. Reserve Read for a single specific line range explore can't surface.`);
  2997. } else if (anyFileTrimmed) {
  2998. lines.push('');
  2999. lines.push(`> Some file sections were trimmed for size. For a specific symbol you still need, run another \`codegraph_explore\` (or \`codegraph_node\`) with its exact name — line-numbered source, cheaper and more complete than Read.`);
  3000. }
  3001. // Add explore budget note based on project size
  3002. if (budget.includeBudgetNote) {
  3003. try {
  3004. const stats = cg.getStats();
  3005. const callBudget = getExploreBudget(stats.fileCount);
  3006. lines.push('');
  3007. lines.push(`> **Explore budget: ${callBudget} calls for this project (${stats.fileCount.toLocaleString()} files indexed).** Each call covers ~6 files; if your question spans more, spend your remaining calls on the uncovered area BEFORE falling back to Read — another explore is cheaper and more complete than reading those files. Synthesize once you've used ${callBudget}.`);
  3008. } catch {
  3009. // Stats unavailable — skip budget note
  3010. }
  3011. }
  3012. // Final ceiling — an ABSOLUTE inline cap, not a multiple of the budget. The
  3013. // render loop renders necessary (named/spine) files even a bit past
  3014. // maxOutputChars and caps only incidental ones, so this is the last safety.
  3015. // It MUST stay under the host's inline tool-result limit (~25K chars): above
  3016. // that the result is externalized to a file the agent Reads back (a 35K
  3017. // vscode explore did exactly this in the n=4 A/B). So allow a little
  3018. // necessary overflow above the 24K budget, but hard-stop at 25K — never into
  3019. // externalize territory.
  3020. const output = flow.text + lines.join('\n');
  3021. const hardCeiling = Math.min(Math.round(budget.maxOutputChars * 1.5), 25000);
  3022. if (output.length > hardCeiling) {
  3023. // Cut at a FILE-SECTION boundary (the last `#### ` header before the
  3024. // ceiling) so we drop whole trailing file-sections rather than slicing
  3025. // through a method body — a half-rendered method just forces the Read this
  3026. // tool exists to prevent. Fall back to a line boundary only if no section
  3027. // header sits in the back half (degenerate single-giant-section case).
  3028. const cut = output.slice(0, hardCeiling);
  3029. const lastSection = cut.lastIndexOf('\n#### ');
  3030. const boundary = lastSection > hardCeiling * 0.5 ? lastSection : cut.lastIndexOf('\n');
  3031. const safe = boundary > 0 ? cut.slice(0, boundary) : cut;
  3032. return this.textResult(safe + '\n\n... (output truncated to budget; the source above is complete and verbatim — treat it as already Read. For any area not covered, run another codegraph_explore with the specific names — do NOT Read these files.)');
  3033. }
  3034. return this.textResult(output);
  3035. }
  3036. /**
  3037. * Handle codegraph_node
  3038. */
  3039. private async handleNode(args: Record<string, unknown>): Promise<ToolResult> {
  3040. const cg = this.getCodeGraph(args.projectPath as string | undefined);
  3041. // Default to false to minimize context usage
  3042. const includeCode = args.includeCode === true;
  3043. const fileHint = typeof args.file === 'string' && args.file.trim() ? args.file.trim() : undefined;
  3044. const lineHint = typeof args.line === 'number' && args.line > 0 ? args.line : undefined;
  3045. const offset = typeof args.offset === 'number' && args.offset > 0 ? Math.floor(args.offset) : undefined;
  3046. const limit = typeof args.limit === 'number' && args.limit > 0 ? Math.floor(args.limit) : undefined;
  3047. const symbolsOnly = args.symbolsOnly === true;
  3048. const symbolRaw = typeof args.symbol === 'string' ? args.symbol.trim() : '';
  3049. // FILE READ MODE: a `file` with no `symbol` reads that file like the Read
  3050. // tool — its current on-disk source with line numbers, narrowable with
  3051. // `offset`/`limit` exactly as Read does — PLUS a one-line blast-radius
  3052. // header (which files depend on it). `symbolsOnly` returns just the
  3053. // structural map instead. Backed by the index: same bytes Read gives you.
  3054. if (!symbolRaw && fileHint) {
  3055. return this.handleFileView(cg, fileHint, { offset, limit, symbolsOnly });
  3056. }
  3057. const symbol = this.validateString(args.symbol, 'symbol');
  3058. if (typeof symbol !== 'string') return symbol;
  3059. let matches = this.findSymbolMatches(cg, symbol);
  3060. if (matches.length === 0) {
  3061. return this.textResult(`Symbol "${symbol}" not found in the codebase`);
  3062. }
  3063. // Disambiguate a heavily-overloaded name to a specific definition the caller
  3064. // pinned by file/line (the `file:line` a trail or another tool showed it) —
  3065. // so it can fetch e.g. `Harness::poll` at harness.rs:153 out of 50+ `poll`s
  3066. // instead of Reading. file matches by path suffix/substring; line prefers the
  3067. // def whose body contains it, else the nearest start. Only narrows (never
  3068. // empties — if a hint matches nothing it's ignored).
  3069. if (matches.length > 1 && (fileHint || lineHint !== undefined)) {
  3070. const norm = (p: string) => p.replace(/\\/g, '/').toLowerCase();
  3071. let narrowed = matches;
  3072. if (fileHint) {
  3073. const fh = norm(fileHint);
  3074. const byFile = narrowed.filter((n) => norm(n.filePath).endsWith(fh) || norm(n.filePath).includes(fh));
  3075. if (byFile.length > 0) narrowed = byFile;
  3076. }
  3077. if (lineHint !== undefined && narrowed.length > 1) {
  3078. const containing = narrowed.filter((n) => n.startLine <= lineHint && (n.endLine ?? n.startLine) >= lineHint);
  3079. narrowed = containing.length > 0
  3080. ? containing
  3081. : [...narrowed].sort((a, b) => Math.abs(a.startLine - lineHint) - Math.abs(b.startLine - lineHint)).slice(0, 1);
  3082. }
  3083. if (narrowed.length > 0) matches = narrowed;
  3084. }
  3085. // Single definition — the common case.
  3086. if (matches.length === 1) {
  3087. return this.textResult(this.truncateOutput(await this.renderNodeSection(cg, matches[0]!, includeCode)));
  3088. }
  3089. // Multiple definitions share this name — overloads, or same-named methods on
  3090. // different types (Alamofire `didCompleteTask`/`task`/`validate`, gin
  3091. // `reset`). Returning ONE forces the agent to guess, and when it guesses
  3092. // wrong it READS the file to find the right overload — the dominant
  3093. // codegraph_node read cause on Swift/Go. So return them ALL: pack as many
  3094. // FULL bodies as fit a char budget (the agent gets the one it needs in this
  3095. // one call, no follow-up parameter to learn), and list any remainder by
  3096. // file:line so a large overload set can't overflow the per-tool cap.
  3097. const header = `**${matches.length} definitions named "${symbol}"**`;
  3098. if (!includeCode) {
  3099. const list = matches.map((n) => `- \`${n.name}\` (${n.kind}) — ${n.filePath}:${n.startLine}`);
  3100. return this.textResult(this.truncateOutput(
  3101. [header, '', 'Re-query with `includeCode: true` to get every body in one call — no need to pick one first.', '', ...list].join('\n'),
  3102. ));
  3103. }
  3104. const BODY_BUDGET = 12000; // leaves room under MAX_OUTPUT_LENGTH for the header + list
  3105. // The CHAR budget is the real limiter — keep the count cap high so a set of
  3106. // SHORT overloads (Alamofire's 10 `validate` variants, each a few lines) all
  3107. // render in full rather than relegating the one the agent wanted to a
  3108. // bodiless list. Only a set of many LARGE bodies hits the char budget first.
  3109. const HARD_CAP = 16;
  3110. const rendered: string[] = [];
  3111. const listed: Node[] = [];
  3112. let used = 0;
  3113. for (const n of matches) {
  3114. if (rendered.length >= HARD_CAP) { listed.push(n); continue; }
  3115. const section = await this.renderNodeSection(cg, n, true);
  3116. // Always emit the first; emit the rest only while within the char budget.
  3117. if (rendered.length === 0 || used + section.length <= BODY_BUDGET) {
  3118. rendered.push(section);
  3119. used += section.length;
  3120. } else {
  3121. listed.push(n);
  3122. }
  3123. }
  3124. const out: string[] = [
  3125. header,
  3126. `Returning ${rendered.length} in full${listed.length ? `; ${listed.length} more listed below` : ''} — pick the one you need (no Read required).`,
  3127. '',
  3128. rendered.join('\n\n---\n\n'),
  3129. ];
  3130. if (listed.length) {
  3131. const LIST_CAP = 20;
  3132. const shownList = listed.slice(0, LIST_CAP);
  3133. out.push(
  3134. '',
  3135. '### Other definitions',
  3136. ...shownList.map((n) => `- \`${n.name}\` (${n.kind}) — ${n.filePath}:${n.startLine}`),
  3137. );
  3138. if (listed.length > LIST_CAP) out.push(`- … +${listed.length - LIST_CAP} more`);
  3139. out.push(
  3140. '',
  3141. `> Need one of these in full? Call codegraph_node again with \`file\` (e.g. \`"${listed[0]!.filePath.split('/').pop()}"\`) or \`line\` — do NOT Read it.`,
  3142. );
  3143. }
  3144. return this.textResult(this.truncateOutput(out.join('\n')));
  3145. }
  3146. /**
  3147. * FILE READ MODE: resolve `fileArg` (path or basename) to an indexed file and
  3148. * read it like the Read tool — its current on-disk source with line numbers,
  3149. * narrowable with `offset`/`limit` exactly as Read's are — preceded by a
  3150. * one-line blast-radius header (which files depend on it). `symbolsOnly`
  3151. * returns just the structural map (symbols + dependents) instead of source.
  3152. *
  3153. * Parity goal: the numbered source block is byte-for-byte the shape Read
  3154. * returns (`<n>\t<line>`, no padding), so the agent treats it as a Read — only
  3155. * faster (served from the index) and with the blast radius attached. Security:
  3156. * yaml/properties files are summarized by key, never dumped (#383); reads go
  3157. * through validatePathWithinRoot (#527).
  3158. */
  3159. private async handleFileView(
  3160. cg: CodeGraph,
  3161. fileArg: string,
  3162. opts: { offset?: number; limit?: number; symbolsOnly?: boolean } = {},
  3163. ): Promise<ToolResult> {
  3164. const normalize = (p: string) => p.replace(/\\/g, '/').replace(/^(?:\.?\/+)+/, '').replace(/\/+$/, '');
  3165. const wantLower = normalize(fileArg).toLowerCase();
  3166. const allFiles = cg.getFiles();
  3167. if (allFiles.length === 0) return this.textResult('No files indexed. Run `codegraph index` first.');
  3168. let resolved = allFiles.find((f) => f.path.toLowerCase() === wantLower);
  3169. let candidates: typeof allFiles = [];
  3170. if (!resolved) {
  3171. candidates = allFiles.filter((f) => f.path.toLowerCase().endsWith('/' + wantLower));
  3172. if (candidates.length === 1) resolved = candidates[0];
  3173. }
  3174. if (!resolved && candidates.length === 0) {
  3175. candidates = allFiles.filter((f) => f.path.toLowerCase().includes(wantLower));
  3176. if (candidates.length === 1) resolved = candidates[0];
  3177. }
  3178. if (!resolved && candidates.length > 1) {
  3179. return this.textResult(
  3180. [`"${fileArg}" matches ${candidates.length} indexed files — pass a longer path:`, '',
  3181. ...candidates.slice(0, 25).map((f) => `- ${f.path}`)].join('\n'),
  3182. );
  3183. }
  3184. if (!resolved) {
  3185. return this.textResult(
  3186. `No indexed file matches "${fileArg}". Codegraph indexes source files; configs/docs it doesn't parse won't appear — Read those directly.`,
  3187. );
  3188. }
  3189. const filePath = resolved.path;
  3190. const nodes = cg.getNodesInFile(filePath)
  3191. .filter((n) => n.kind !== 'file' && n.kind !== 'import' && n.kind !== 'export')
  3192. .sort((a, b) => a.startLine - b.startLine);
  3193. const dependents = cg.getFileDependents(filePath);
  3194. // Compact, one-line blast radius (codegraph's value-add over a plain Read).
  3195. const depSummary = dependents.length
  3196. ? `used by ${dependents.length} file${dependents.length === 1 ? '' : 's'}: ${dependents.slice(0, 8).join(', ')}${dependents.length > 8 ? `, +${dependents.length - 8} more` : ''}`
  3197. : 'no other indexed file depends on it';
  3198. // Symbol-map renderer — for symbolsOnly, the config fallback, and read errors.
  3199. const symbolMap = (heading: string, limit = 200): string[] => {
  3200. const lines: string[] = [heading];
  3201. for (const n of nodes.slice(0, limit)) {
  3202. const sig = n.signature ? ` ${n.signature.replace(/\s+/g, ' ').trim()}` : '';
  3203. lines.push(`- \`${n.name}\` (${n.kind})${sig} — :${n.startLine}`);
  3204. }
  3205. if (nodes.length > limit) lines.push(`- … +${nodes.length - limit} more`);
  3206. return lines;
  3207. };
  3208. // symbolsOnly → the cheap structural overview, no source.
  3209. if (opts.symbolsOnly) {
  3210. const out = [`**${filePath}** — ${nodes.length} symbol${nodes.length === 1 ? '' : 's'}, ${depSummary}`, ''];
  3211. if (nodes.length) out.push(...symbolMap('### Symbols'));
  3212. else out.push('_No indexed symbols in this file._');
  3213. out.push('', '> Drop `symbolsOnly` (or pass `offset`/`limit`) to read the source, like Read.');
  3214. return this.textResult(this.truncateOutput(out.join('\n')));
  3215. }
  3216. // SECURITY (#383): never dump a raw config/data file — a yaml/properties
  3217. // line is `key: <secret>`. Summarize by key and point to a real Read.
  3218. if (CONFIG_LEAF_LANGUAGES.has(resolved.language)) {
  3219. const out = [`**${filePath}** — configuration/data file, ${depSummary}`, ''];
  3220. if (nodes.length) out.push(...symbolMap('### Keys (values withheld for safety)'));
  3221. out.push('', '> Values may be secrets, so codegraph indexes keys only. Read the file directly if you need a value.');
  3222. return this.textResult(this.truncateOutput(out.join('\n')));
  3223. }
  3224. // Read the current bytes from disk through the security chokepoint
  3225. // (validatePathWithinRoot: blocks `../` traversal and symlink escapes, #527).
  3226. const abs = validatePathWithinRoot(cg.getProjectRoot(), filePath);
  3227. let content: string | null = null;
  3228. if (abs) {
  3229. try { content = readFileSync(abs, 'utf-8'); } catch { content = null; }
  3230. }
  3231. if (content === null) {
  3232. const out = [`**${filePath}** — could not read from disk (it may have moved since indexing). ${depSummary}`, ''];
  3233. if (nodes.length) out.push(...symbolMap('### Symbols'));
  3234. out.push('', `> Read \`${filePath}\` directly for its current content.`);
  3235. return this.textResult(this.truncateOutput(out.join('\n')));
  3236. }
  3237. // Split exactly as Read does — keep the trailing empty line a final newline
  3238. // produces (Read numbers it too), so line numbers line up byte-for-byte.
  3239. const fileLines = content.split('\n');
  3240. const total = fileLines.length;
  3241. // Read-parity windowing: `offset`/`limit` mean exactly what they do on Read
  3242. // (1-based start line; max line count). Default: the whole file, capped like
  3243. // Read at 2000 lines and bounded by a char budget that tracks explore's
  3244. // proven-safe ~38k response ceiling. Overflow is stated explicitly (Read
  3245. // paginates too) — never the silent 15k truncateOutput chop.
  3246. const CHAR_BUDGET = 38000;
  3247. const DEFAULT_LIMIT = 2000;
  3248. const offset = Math.max(1, opts.offset ?? 1);
  3249. if (offset > total) {
  3250. return this.textResult(`**${filePath}** has ${total} line${total === 1 ? '' : 's'} — offset ${offset} is past the end. ${depSummary}`);
  3251. }
  3252. const maxLines = Math.max(1, opts.limit ?? DEFAULT_LIMIT);
  3253. const start = offset - 1; // 0-based
  3254. const header = `**${filePath}** — ${total} lines, ${nodes.length} symbol${nodes.length === 1 ? '' : 's'} · ${depSummary}`;
  3255. // Numbered lines, byte-for-byte Read's shape: `<n>\t<line>`, no left-pad.
  3256. const numbered: string[] = [];
  3257. let used = header.length + 8;
  3258. let i = start;
  3259. for (; i < total && numbered.length < maxLines; i++) {
  3260. const ln = `${i + 1}\t${fileLines[i]}`;
  3261. if (used + ln.length + 1 > CHAR_BUDGET && numbered.length > 0) break;
  3262. numbered.push(ln);
  3263. used += ln.length + 1;
  3264. }
  3265. const shownEnd = start + numbered.length;
  3266. const complete = offset === 1 && shownEnd >= total;
  3267. const out: string[] = [header, '', ...numbered];
  3268. if (!complete) {
  3269. out.push(
  3270. '',
  3271. `(lines ${offset}–${shownEnd} of ${total} — pass \`offset\`/\`limit\` for another range, or \`codegraph_node <symbol>\` for one symbol in full)`,
  3272. );
  3273. }
  3274. // Self-bounded to CHAR_BUDGET — do NOT route through truncateOutput (15k).
  3275. return this.textResult(out.join('\n'));
  3276. }
  3277. /** Render one symbol: details + (optional) body/outline + its caller/callee trail. */
  3278. private async renderNodeSection(cg: CodeGraph, node: Node, includeCode: boolean): Promise<string> {
  3279. let code: string | null = null;
  3280. let outline: string | null = null;
  3281. if (includeCode) {
  3282. // For container symbols (class/interface/struct/…), the full body is the
  3283. // sum of every method body — a wall of source. Return a structural outline
  3284. // (members + signatures + line numbers) instead; leaf symbols return their
  3285. // full body.
  3286. if (CONTAINER_NODE_KINDS.has(node.kind)) {
  3287. outline = this.buildContainerOutline(cg, node);
  3288. }
  3289. if (!outline) {
  3290. code = await cg.getCode(node.id);
  3291. }
  3292. }
  3293. return this.formatNodeDetails(node, code, outline) + this.formatTrail(cg, node);
  3294. }
  3295. /**
  3296. * Build the "trail" for a symbol: its direct callees (what it calls) and
  3297. * callers (what calls it), each with file:line — so codegraph_node doubles as
  3298. * the structural Grep→Read→expand primitive: a spot PLUS where to go next.
  3299. * Capped to stay cheap. Walk the graph by calling codegraph_node on a trail
  3300. * entry; no Read needed for covered hops. Empty edges on a non-leaf often mean
  3301. * dynamic dispatch the static graph couldn't resolve — that absence is itself
  3302. * a signal (read that one hop) rather than a dead end.
  3303. */
  3304. private formatTrail(cg: CodeGraph, node: Node): string {
  3305. const TRAIL_CAP = 12;
  3306. const fmt = (e: { node: Node; edge: Edge }) => {
  3307. const base = `${e.node.name} (${e.node.filePath}:${e.node.startLine})`;
  3308. const synth = this.synthEdgeNote(e.edge);
  3309. return synth ? `${base} [${synth.compact}]` : base;
  3310. };
  3311. const collect = (edges: Array<{ node: Node; edge: Edge }>): Array<{ node: Node; edge: Edge }> => {
  3312. const seen = new Set<string>([node.id]);
  3313. const out: Array<{ node: Node; edge: Edge }> = [];
  3314. for (const e of edges) {
  3315. if (seen.has(e.node.id)) continue;
  3316. seen.add(e.node.id);
  3317. out.push(e);
  3318. }
  3319. return out;
  3320. };
  3321. const callees = collect(cg.getCallees(node.id));
  3322. const callers = collect(cg.getCallers(node.id));
  3323. if (callees.length === 0 && callers.length === 0) return '';
  3324. const lines: string[] = ['', '### Trail — codegraph_node any of these to follow it (no Read needed)'];
  3325. if (callees.length > 0) {
  3326. lines.push(`**Calls →** ${callees.slice(0, TRAIL_CAP).map(fmt).join(', ')}${callees.length > TRAIL_CAP ? `, +${callees.length - TRAIL_CAP} more` : ''}`);
  3327. }
  3328. if (callers.length > 0) {
  3329. lines.push(`**Called by ←** ${callers.slice(0, TRAIL_CAP).map(fmt).join(', ')}${callers.length > TRAIL_CAP ? `, +${callers.length - TRAIL_CAP} more` : ''}`);
  3330. }
  3331. return lines.join('\n');
  3332. }
  3333. /**
  3334. * Handle codegraph_status
  3335. */
  3336. private async handleStatus(args: Record<string, unknown>): Promise<ToolResult> {
  3337. let cg = this.getCodeGraph(args.projectPath as string | undefined);
  3338. // Same trick as withStalenessNotice — when an explicit projectPath
  3339. // resolves to the same project as the default session cg, prefer the
  3340. // default so getPendingFiles() (only populated by the default's watcher)
  3341. // is non-empty when there are pending edits.
  3342. if (this.cg && cg !== this.cg) {
  3343. try {
  3344. if (resolvePath(this.cg.getProjectRoot()) === resolvePath(cg.getProjectRoot())) {
  3345. cg = this.cg;
  3346. }
  3347. } catch { /* closed instance — leave as is */ }
  3348. }
  3349. const stats = cg.getStats();
  3350. // Warn when this index actually belongs to a different git working tree
  3351. // (e.g. the server resolved up from a nested worktree to the main checkout).
  3352. // Queries then reflect that tree's branch, not the worktree being edited.
  3353. // status shows the verbose, multi-line form; the read tools get the compact
  3354. // one-liner via withWorktreeNotice. Both share the cached detection.
  3355. const mismatch = this.worktreeMismatchFor(args.projectPath as string | undefined);
  3356. const lines: string[] = [
  3357. '## CodeGraph Status',
  3358. '',
  3359. ];
  3360. if (mismatch) {
  3361. lines.push(`> ⚠ ${worktreeMismatchWarning(mismatch).replace(/\n/g, '\n> ')}`, '');
  3362. }
  3363. lines.push(
  3364. `**Files indexed:** ${stats.fileCount}`,
  3365. `**Total nodes:** ${stats.nodeCount}`,
  3366. `**Total edges:** ${stats.edgeCount}`,
  3367. `**Database size:** ${(stats.dbSizeBytes / 1024 / 1024).toFixed(2)} MB`,
  3368. );
  3369. // Surface the active SQLite backend (node:sqlite, Node's built-in real
  3370. // SQLite — full WAL + FTS5, no native build).
  3371. lines.push(`**Backend:** node:sqlite (Node built-in) — full WAL + FTS5`);
  3372. // Effective journal mode. 'wal' ⇒ concurrent reads never block on a writer;
  3373. // anything else ⇒ they can ("database is locked"). node:sqlite supports WAL
  3374. // everywhere, so a non-wal mode means the filesystem can't (network/
  3375. // virtualized mounts, WSL2 /mnt). See issue #238.
  3376. const journalMode = cg.getJournalMode();
  3377. if (journalMode === 'wal') {
  3378. lines.push(`**Journal mode:** wal (concurrent reads safe)`);
  3379. } else {
  3380. lines.push(
  3381. `**Journal mode:** ⚠ ${journalMode || 'unknown'} — WAL not active, so reads ` +
  3382. `can block on a concurrent write (WAL appears unsupported on this filesystem)`
  3383. );
  3384. }
  3385. lines.push('', '### Nodes by Kind:');
  3386. for (const [kind, count] of Object.entries(stats.nodesByKind)) {
  3387. if ((count as number) > 0) {
  3388. lines.push(`- ${kind}: ${count}`);
  3389. }
  3390. }
  3391. lines.push('', '### Languages:');
  3392. for (const [lang, count] of Object.entries(stats.filesByLanguage)) {
  3393. if ((count as number) > 0) {
  3394. lines.push(`- ${lang}: ${count}`);
  3395. }
  3396. }
  3397. // Whole-index degradation (#876): when live watching has permanently
  3398. // stopped, getPendingFiles() is empty (so no "Pending sync" section below)
  3399. // but the index is frozen — call that out explicitly here, the one place an
  3400. // agent asks "is the index caught up?".
  3401. if (cg.isWatcherDegraded()) {
  3402. lines.push(
  3403. '',
  3404. '### Auto-sync disabled:',
  3405. `- ${cg.getWatcherDegradedReason() ?? 'live file watching stopped'}`,
  3406. '- The index is frozen; Read files directly for current content.'
  3407. );
  3408. }
  3409. // Per-file freshness — the inverse of the auto-prepended staleness banner
  3410. // (issue #403). Surfacing it inside `status` gives the agent a single
  3411. // place to ask "is the index caught up?" rather than inferring from
  3412. // banners on other tool calls.
  3413. const pending = cg.getPendingFiles();
  3414. if (pending.length > 0) {
  3415. lines.push('', '### Pending sync:');
  3416. const now = Date.now();
  3417. for (const p of pending) {
  3418. const ageMs = Math.max(0, now - p.lastSeenMs);
  3419. const label = p.indexing ? 'indexing in progress' : 'pending sync';
  3420. lines.push(`- ${p.path} (edited ${ageMs}ms ago, ${label})`);
  3421. }
  3422. }
  3423. return this.textResult(lines.join('\n'));
  3424. }
  3425. /**
  3426. * Handle codegraph_files - get project file structure from the index
  3427. */
  3428. private async handleFiles(args: Record<string, unknown>): Promise<ToolResult> {
  3429. const cg = this.getCodeGraph(args.projectPath as string | undefined);
  3430. const pathFilter = args.path as string | undefined;
  3431. const pattern = args.pattern as string | undefined;
  3432. const format = (args.format as 'tree' | 'flat' | 'grouped') || 'tree';
  3433. const includeMetadata = args.includeMetadata !== false;
  3434. const maxDepth = args.maxDepth != null ? clamp(args.maxDepth as number, 1, 20) : undefined;
  3435. // Get all files from the index
  3436. const allFiles = cg.getFiles();
  3437. if (allFiles.length === 0) {
  3438. return this.textResult('No files indexed. Run `codegraph index` first.');
  3439. }
  3440. // Filter by path prefix. Stored paths are project-relative POSIX (e.g.
  3441. // "src/foo.ts"), but agents commonly pass project-root variants like "/",
  3442. // ".", "./", "" or Windows-style "src\foo" — and prefixes with leading
  3443. // "/", "./" or "\". Normalize all of those before matching so the agent
  3444. // gets results instead of falling back to Read/Glob (see #426).
  3445. const normalizedFilter = pathFilter
  3446. ? pathFilter
  3447. .replace(/\\/g, '/')
  3448. .replace(/^(?:\.?\/+)+/, '')
  3449. .replace(/^\.$/, '')
  3450. .replace(/\/+$/, '')
  3451. : '';
  3452. let files = normalizedFilter
  3453. ? allFiles.filter(f => f.path === normalizedFilter || f.path.startsWith(normalizedFilter + '/'))
  3454. : allFiles;
  3455. // Filter by glob pattern
  3456. if (pattern) {
  3457. const regex = this.globToRegex(pattern);
  3458. files = files.filter(f => regex.test(f.path));
  3459. }
  3460. if (files.length === 0) {
  3461. return this.textResult(`No files found matching the criteria.`);
  3462. }
  3463. // Format output
  3464. let output: string;
  3465. switch (format) {
  3466. case 'flat':
  3467. output = this.formatFilesFlat(files, includeMetadata);
  3468. break;
  3469. case 'grouped':
  3470. output = this.formatFilesGrouped(files, includeMetadata);
  3471. break;
  3472. case 'tree':
  3473. default:
  3474. output = this.formatFilesTree(files, includeMetadata, maxDepth);
  3475. break;
  3476. }
  3477. return this.textResult(this.truncateOutput(output));
  3478. }
  3479. /**
  3480. * Convert glob pattern to regex
  3481. */
  3482. private globToRegex(pattern: string): RegExp {
  3483. const escaped = pattern
  3484. .replace(/[.+^${}()|[\]\\]/g, '\\$&') // Escape special regex chars except * and ?
  3485. .replace(/\*\*/g, '{{GLOBSTAR}}') // Temp placeholder for **
  3486. .replace(/\*/g, '[^/]*') // * matches anything except /
  3487. .replace(/\?/g, '[^/]') // ? matches single char except /
  3488. .replace(/\{\{GLOBSTAR\}\}/g, '.*'); // ** matches anything including /
  3489. return new RegExp(escaped);
  3490. }
  3491. /**
  3492. * Format files as a flat list
  3493. */
  3494. private formatFilesFlat(files: { path: string; language: string; nodeCount: number }[], includeMetadata: boolean): string {
  3495. const lines: string[] = [`## Files (${files.length})`, ''];
  3496. for (const file of files.sort((a, b) => a.path.localeCompare(b.path))) {
  3497. if (includeMetadata) {
  3498. lines.push(`- ${file.path} (${file.language}, ${file.nodeCount} symbols)`);
  3499. } else {
  3500. lines.push(`- ${file.path}`);
  3501. }
  3502. }
  3503. return lines.join('\n');
  3504. }
  3505. /**
  3506. * Format files grouped by language
  3507. */
  3508. private formatFilesGrouped(files: { path: string; language: string; nodeCount: number }[], includeMetadata: boolean): string {
  3509. const byLang = new Map<string, typeof files>();
  3510. for (const file of files) {
  3511. const existing = byLang.get(file.language) || [];
  3512. existing.push(file);
  3513. byLang.set(file.language, existing);
  3514. }
  3515. const lines: string[] = [`## Files by Language (${files.length} total)`, ''];
  3516. // Sort languages by file count (descending)
  3517. const sortedLangs = [...byLang.entries()].sort((a, b) => b[1].length - a[1].length);
  3518. for (const [lang, langFiles] of sortedLangs) {
  3519. lines.push(`### ${lang} (${langFiles.length})`);
  3520. for (const file of langFiles.sort((a, b) => a.path.localeCompare(b.path))) {
  3521. if (includeMetadata) {
  3522. lines.push(`- ${file.path} (${file.nodeCount} symbols)`);
  3523. } else {
  3524. lines.push(`- ${file.path}`);
  3525. }
  3526. }
  3527. lines.push('');
  3528. }
  3529. return lines.join('\n');
  3530. }
  3531. /**
  3532. * Format files as a tree structure
  3533. */
  3534. private formatFilesTree(
  3535. files: { path: string; language: string; nodeCount: number }[],
  3536. includeMetadata: boolean,
  3537. maxDepth?: number
  3538. ): string {
  3539. // Build tree structure
  3540. interface TreeNode {
  3541. name: string;
  3542. children: Map<string, TreeNode>;
  3543. file?: { language: string; nodeCount: number };
  3544. }
  3545. const root: TreeNode = { name: '', children: new Map() };
  3546. for (const file of files) {
  3547. const parts = file.path.split('/');
  3548. let current = root;
  3549. for (let i = 0; i < parts.length; i++) {
  3550. const part = parts[i];
  3551. if (!part) continue;
  3552. if (!current.children.has(part)) {
  3553. current.children.set(part, { name: part, children: new Map() });
  3554. }
  3555. current = current.children.get(part)!;
  3556. // If this is the last part, it's a file
  3557. if (i === parts.length - 1) {
  3558. current.file = { language: file.language, nodeCount: file.nodeCount };
  3559. }
  3560. }
  3561. }
  3562. // Render tree
  3563. const lines: string[] = [`## Project Structure (${files.length} files)`, ''];
  3564. const renderNode = (node: TreeNode, prefix: string, isLast: boolean, depth: number): void => {
  3565. if (maxDepth !== undefined && depth > maxDepth) return;
  3566. const connector = isLast ? '└── ' : '├── ';
  3567. const childPrefix = isLast ? ' ' : '│ ';
  3568. if (node.name) {
  3569. let line = prefix + connector + node.name;
  3570. if (node.file && includeMetadata) {
  3571. line += ` (${node.file.language}, ${node.file.nodeCount} symbols)`;
  3572. }
  3573. lines.push(line);
  3574. }
  3575. const children = [...node.children.values()];
  3576. // Sort: directories first, then files, both alphabetically
  3577. children.sort((a, b) => {
  3578. const aIsDir = a.children.size > 0 && !a.file;
  3579. const bIsDir = b.children.size > 0 && !b.file;
  3580. if (aIsDir !== bIsDir) return aIsDir ? -1 : 1;
  3581. return a.name.localeCompare(b.name);
  3582. });
  3583. for (let i = 0; i < children.length; i++) {
  3584. const child = children[i]!;
  3585. const nextPrefix = node.name ? prefix + childPrefix : prefix;
  3586. renderNode(child, nextPrefix, i === children.length - 1, depth + 1);
  3587. }
  3588. };
  3589. renderNode(root, '', true, 0);
  3590. return lines.join('\n');
  3591. }
  3592. // =========================================================================
  3593. // Symbol resolution helpers
  3594. // =========================================================================
  3595. /**
  3596. * Find a symbol by name, handling disambiguation when multiple matches exist.
  3597. * Returns the best match and a note about alternatives if any.
  3598. */
  3599. /**
  3600. * Check if a node matches a symbol query.
  3601. *
  3602. * Accepts simple names (`run`) and three flavors of qualifier:
  3603. * - dotted `Session.request` (TS/JS/Python)
  3604. * - colon-pair `stage_apply::run` (Rust, C++, Ruby)
  3605. * - slash `configurator/stage_apply` (path-ish)
  3606. *
  3607. * Multi-level qualifiers compose: `crate::configurator::stage_apply::run`
  3608. * works. Rust path prefixes (`crate`, `super`, `self`) are stripped so
  3609. * the canonical `crate::module::symbol` form resolves.
  3610. *
  3611. * Resolution order, last part must always equal `node.name`:
  3612. * 1. Suffix-match against `qualifiedName` (handles class-scoped methods
  3613. * where the extractor builds the qualified name from the AST stack)
  3614. * 2. File-path containment (handles file-derived modules in Rust/
  3615. * Python — `stage_apply::run` matches a `run` in `stage_apply.rs`)
  3616. */
  3617. private matchesSymbol(node: Node, symbol: string): boolean {
  3618. // Simple name match
  3619. if (node.name === symbol) return true;
  3620. // File basename match (e.g., "product-card" matches "product-card.liquid")
  3621. if (node.kind === 'file' && node.name.replace(/\.[^.]+$/, '') === symbol) return true;
  3622. // Qualified-name lookups: split on any supported separator. `\w` keeps
  3623. // identifier chars (incl. `_`) intact; everything else is treated as
  3624. // a separator we tolerate.
  3625. if (!/[.\/]|::/.test(symbol)) return false;
  3626. const parts = symbol.split(/::|[./]/).filter((p) => p.length > 0);
  3627. if (parts.length < 2) return false;
  3628. const lastPart = parts[parts.length - 1]!;
  3629. if (node.name !== lastPart) return false;
  3630. // Stage 1: qualified-name suffix match. The extractor joins the
  3631. // semantic hierarchy with `::`, so `Session.request` and
  3632. // `Session::request` both become `Session::request` here.
  3633. const colonSuffix = parts.join('::');
  3634. if (node.qualifiedName.includes(colonSuffix)) return true;
  3635. // Stage 2: file-path containment. Rust modules and Python packages
  3636. // are not in `qualifiedName` — they're encoded in the file path. So
  3637. // `stage_apply::run` matches a `run` in any file whose path
  3638. // contains a `stage_apply` segment (with or without an extension).
  3639. //
  3640. // Filter out Rust path prefixes that have no file-system equivalent.
  3641. const containerHints = parts.slice(0, -1).filter((p) => !RUST_PATH_PREFIXES.has(p));
  3642. if (containerHints.length === 0) return false;
  3643. const segments = node.filePath.split('/').filter((s) => s.length > 0);
  3644. return containerHints.every((hint) =>
  3645. segments.some((seg) => seg === hint || seg.replace(/\.[^.]+$/, '') === hint)
  3646. );
  3647. }
  3648. /**
  3649. * Find ALL definitions matching a name, ranked, so codegraph_node can return
  3650. * every overload instead of guessing one (the wrong guess → a Read). Keepers
  3651. * rank before generated stubs (.pb.go etc.); stable within a group preserves
  3652. * FTS order. Returns [] when nothing matches; a qualified lookup that finds no
  3653. * exact match returns [] rather than a misleading fuzzy file hit (#173); a
  3654. * bare name with no exact match falls back to the single top fuzzy result.
  3655. */
  3656. private findSymbolMatches(cg: CodeGraph, symbol: string): Node[] {
  3657. const isQualified = /[.\/]|::/.test(symbol);
  3658. // For a bare name, enumerate EVERY exact-name definition via the direct index
  3659. // (not FTS, which caps + ranks): tokio's `poll` has 50+ defs and the one the
  3660. // caller wants (`Harness::poll` at harness.rs:153) ranks below any search cut,
  3661. // so it could be neither rendered nor pinned by the file/line disambiguator —
  3662. // and the agent Read it. With the full set, the multi-overload render + the
  3663. // file/line filter can both reach it.
  3664. if (!isQualified) {
  3665. const exact = cg.getNodesByName(symbol);
  3666. if (exact.length > 0) {
  3667. return [...exact].sort((a, b) => (isGeneratedFile(a.filePath) ? 1 : 0) - (isGeneratedFile(b.filePath) ? 1 : 0));
  3668. }
  3669. // No exact match — use the single top fuzzy result (e.g. a file basename).
  3670. const fuzzy = cg.searchNodes(symbol, { limit: 10 });
  3671. return fuzzy[0] ? [fuzzy[0].node] : [];
  3672. }
  3673. // Qualified lookup (`Session.request`, `stage_apply::run`): FTS + matchesSymbol.
  3674. const limit = 50;
  3675. let results = cg.searchNodes(symbol, { limit });
  3676. // FTS strips colons, so `stage_apply::run` searches the literal
  3677. // `stage_applyrun` and finds nothing. Re-search by the bare last part and
  3678. // let `matchesSymbol` filter by qualifier.
  3679. if (isQualified && results.length === 0) {
  3680. const tail = lastQualifierPart(symbol);
  3681. if (tail && tail !== symbol) results = cg.searchNodes(tail, { limit });
  3682. }
  3683. if (results.length === 0) return [];
  3684. const exactMatches = results.filter((r) => this.matchesSymbol(r.node, symbol));
  3685. if (exactMatches.length === 0) {
  3686. // No exact match — a qualified lookup must not fall back to a fuzzy file
  3687. // hit (#173); a bare name may use the single top fuzzy result.
  3688. return isQualified ? [] : results[0] ? [results[0].node] : [];
  3689. }
  3690. // Down-rank generated files (.pb.go, .pulsar.go, _grpc.pb.go, …) so a flow
  3691. // query prefers the keeper implementation over the protobuf-generated stub.
  3692. return [...exactMatches]
  3693. .sort((a, b) => (isGeneratedFile(a.node.filePath) ? 1 : 0) - (isGeneratedFile(b.node.filePath) ? 1 : 0))
  3694. .map((r) => r.node);
  3695. }
  3696. /**
  3697. * Find ALL symbols matching a name. Used by callers/callees/impact to aggregate
  3698. * results across all matching symbols (e.g., multiple classes with an `execute` method).
  3699. */
  3700. private findAllSymbols(cg: CodeGraph, symbol: string): { nodes: Node[]; note: string } {
  3701. let results = cg.searchNodes(symbol, { limit: 50 });
  3702. // Mirror the fallback in `findSymbol` for qualified queries — FTS
  3703. // strips colons, so a module-qualified lookup needs a second pass
  3704. // by the bare last part.
  3705. if (results.length === 0 && /[.\/]|::/.test(symbol)) {
  3706. const tail = lastQualifierPart(symbol);
  3707. if (tail && tail !== symbol) results = cg.searchNodes(tail, { limit: 50 });
  3708. }
  3709. if (results.length === 0) {
  3710. return { nodes: [], note: '' };
  3711. }
  3712. const exactMatches = results.filter(r => this.matchesSymbol(r.node, symbol));
  3713. if (exactMatches.length <= 1) {
  3714. const node = exactMatches[0]?.node ?? results[0]!.node;
  3715. return { nodes: [node], note: '' };
  3716. }
  3717. // Same generated-file down-rank as findSymbol — keeps callers/callees
  3718. // /impact aggregation aligned (a query against "Send" returns the
  3719. // hand-written implementations before the protobuf scaffold).
  3720. const ranked = [...exactMatches].sort((a, b) => {
  3721. const aGen = isGeneratedFile(a.node.filePath) ? 1 : 0;
  3722. const bGen = isGeneratedFile(b.node.filePath) ? 1 : 0;
  3723. return aGen - bGen;
  3724. });
  3725. const locations = ranked.map(r =>
  3726. `${r.node.kind} at ${r.node.filePath}:${r.node.startLine}`
  3727. );
  3728. const note = `\n\n> **Note:** Aggregated results across ${ranked.length} symbols named "${symbol}": ${locations.join(', ')}`;
  3729. return { nodes: ranked.map(r => r.node), note };
  3730. }
  3731. /**
  3732. * Truncate output if it exceeds the maximum length
  3733. */
  3734. private truncateOutput(text: string): string {
  3735. if (text.length <= MAX_OUTPUT_LENGTH) return text;
  3736. const truncated = text.slice(0, MAX_OUTPUT_LENGTH);
  3737. const lastNewline = truncated.lastIndexOf('\n');
  3738. const cutPoint = lastNewline > MAX_OUTPUT_LENGTH * 0.8 ? lastNewline : MAX_OUTPUT_LENGTH;
  3739. return truncated.slice(0, cutPoint) + '\n\n... (output truncated)';
  3740. }
  3741. // =========================================================================
  3742. // Formatting helpers (compact by default to reduce context usage)
  3743. // =========================================================================
  3744. private formatSearchResults(results: SearchResult[]): string {
  3745. const lines: string[] = [`## Search Results (${results.length} found)`, ''];
  3746. for (const result of results) {
  3747. const { node } = result;
  3748. const location = node.startLine ? `:${node.startLine}` : '';
  3749. // Compact format: one line per result with key info
  3750. lines.push(`### ${node.name} (${node.kind})`);
  3751. lines.push(`${node.filePath}${location}`);
  3752. if (node.signature) lines.push(`\`${node.signature}\``);
  3753. lines.push('');
  3754. }
  3755. return lines.join('\n');
  3756. }
  3757. private formatNodeList(nodes: Node[], title: string, labels?: Map<string, string>): string {
  3758. const lines: string[] = [`## ${title} (${nodes.length} found)`, ''];
  3759. for (const node of nodes) {
  3760. const location = node.startLine ? `:${node.startLine}` : '';
  3761. // Compact: just name, kind, location — plus the relationship when it
  3762. // isn't a plain call (callback registration, instantiation, …).
  3763. const label = labels?.get(node.id);
  3764. lines.push(
  3765. `- ${node.name} (${node.kind}) - ${node.filePath}${location}${label ? ` — via ${label}` : ''}`
  3766. );
  3767. }
  3768. return lines.join('\n');
  3769. }
  3770. /**
  3771. * Relationship label for a non-`calls` edge in callers/callees lists. A
  3772. * function-as-value edge (#756) is the high-signal one: `callers(cb)`
  3773. * showing "via callback registration" tells the agent this is where the
  3774. * callback is WIRED, not where it's invoked.
  3775. */
  3776. private edgeLabel(edge: Edge): string | null {
  3777. if (edge.kind === 'calls') return null;
  3778. if (edge.metadata?.fnRef === true) return 'callback registration';
  3779. if (edge.kind === 'instantiates') return 'instantiation';
  3780. if (edge.kind === 'imports') return 'import';
  3781. if (edge.kind === 'references') return 'reference';
  3782. return edge.kind;
  3783. }
  3784. private formatImpact(symbol: string, impact: Subgraph): string {
  3785. const nodeCount = impact.nodes.size;
  3786. // Compact format: just list affected symbols grouped by file
  3787. const lines: string[] = [
  3788. `## Impact: "${symbol}" affects ${nodeCount} symbols`,
  3789. '',
  3790. ];
  3791. // Group by file
  3792. const byFile = new Map<string, Node[]>();
  3793. for (const node of impact.nodes.values()) {
  3794. const existing = byFile.get(node.filePath) || [];
  3795. existing.push(node);
  3796. byFile.set(node.filePath, existing);
  3797. }
  3798. for (const [file, nodes] of byFile) {
  3799. lines.push(`**${file}:**`);
  3800. // Compact: inline list
  3801. const nodeList = nodes.map(n => `${n.name}:${n.startLine}`).join(', ');
  3802. lines.push(nodeList);
  3803. lines.push('');
  3804. }
  3805. return lines.join('\n');
  3806. }
  3807. /**
  3808. * Build a compact structural outline of a container symbol from its
  3809. * indexed children (methods, fields, properties, …) — name, kind,
  3810. * line number, and signature — so the agent gets the shape of a class
  3811. * without the full source of every method. Returns '' when the container
  3812. * has no indexed children, so the caller can fall back to full source.
  3813. */
  3814. private buildContainerOutline(cg: CodeGraph, node: Node): string {
  3815. const children = cg.getChildren(node.id)
  3816. .filter(c => c.kind !== 'import' && c.kind !== 'export')
  3817. .sort((a, b) => (a.startLine ?? 0) - (b.startLine ?? 0));
  3818. if (children.length === 0) return '';
  3819. const lines = [`**Members (${children.length}):**`, ''];
  3820. for (const c of children) {
  3821. const loc = c.startLine ? `:${c.startLine}` : '';
  3822. const sig = c.signature ? ` — \`${c.signature}\`` : '';
  3823. lines.push(`- ${c.name} (${c.kind})${loc}${sig}`);
  3824. }
  3825. return lines.join('\n');
  3826. }
  3827. private formatNodeDetails(node: Node, code: string | null, outline?: string | null): string {
  3828. const location = node.startLine ? `:${node.startLine}` : '';
  3829. const lines: string[] = [
  3830. `## ${node.name} (${node.kind})`,
  3831. '',
  3832. `**Location:** ${node.filePath}${location}`,
  3833. ];
  3834. if (node.signature) {
  3835. lines.push(`**Signature:** \`${node.signature}\``);
  3836. }
  3837. // Only include docstring if it's short and useful
  3838. if (node.docstring && node.docstring.length < 200) {
  3839. lines.push('', node.docstring);
  3840. }
  3841. if (outline) {
  3842. lines.push('', outline, '',
  3843. `> Structural outline only. Read \`${node.filePath}\` or call codegraph_node on a specific member for its body.`);
  3844. } else if (code) {
  3845. // Line-numbered (cat -n style, like codegraph_explore and Read) so the
  3846. // agent can cite/edit exact lines without re-Reading the file for them.
  3847. const numbered = node.startLine ? numberSourceLines(code, node.startLine) : code;
  3848. lines.push('', '```' + node.language, numbered, '```');
  3849. }
  3850. return lines.join('\n');
  3851. }
  3852. private textResult(text: string): ToolResult {
  3853. return {
  3854. content: [{ type: 'text', text }],
  3855. };
  3856. }
  3857. private errorResult(message: string): ToolResult {
  3858. return {
  3859. content: [{ type: 'text', text: `Error: ${message}` }],
  3860. isError: true,
  3861. };
  3862. }
  3863. }