tools.ts 220 KB

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