MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / exploreOneSymbol

Method exploreOneSymbol

src/mcp/tools.ts:8400–8482  ·  view source on GitHub ↗

* codegraph_explore for a query that names one symbol (#3407; see * `explore-one-symbol.ts`). Null hands the query to the general path: no * single name, a name with no definition or more than three, or a definition * whose file changed on disk since it was indexed (the general path owns

(
    cg: CodeGraph,
    matchQuery: string,
    query: string,
    projectRoot: string,
    budget: ExploreOutputBudget,
    maxFiles: number,
    indexedFileCount: number,
  )

Source from the content-addressed store, hash-verified

8398 * drifted files). `CODEGRAPH_EXPLORE_ONE_SYMBOL=0` turns it off, for A/B runs.
8399 */
8400 private exploreOneSymbol(
8401 cg: CodeGraph,
8402 matchQuery: string,
8403 query: string,
8404 projectRoot: string,
8405 budget: ExploreOutputBudget,
8406 maxFiles: number,
8407 indexedFileCount: number,
8408 ): ToolResult | null {
8409 if (process.env.CODEGRAPH_EXPLORE_ONE_SYMBOL === '0') return null;
8410 const name = oneSymbolQueryName(matchQuery);
8411 if (!name) return null;
8412 let defs: Node[] | null;
8413 try {
8414 defs = oneSymbolDefinitions(cg, name);
8415 } catch {
8416 return null;
8417 }
8418 if (!defs) return null;
8419
8420 // Each file read once, and only trusted when it still matches the index:
8421 // a changed file's indexed line numbers can point at other code (#1474).
8422 const read = new Map<string, { content: string; lines: string[] } | null>();
8423 const readFile = (filePath: string) => {
8424 const held = read.get(filePath);
8425 if (held !== undefined) return held;
8426 let entry: { content: string; lines: string[] } | null = null;
8427 try {
8428 const abs = validatePathWithinRoot(projectRoot, filePath);
8429 if (abs && existsSync(abs)) {
8430 const content = readFileSync(abs, 'utf-8');
8431 if (!this.isFileStaleOnDisk(cg, filePath, content)) entry = { content, lines: content.split(/\r?\n/) };
8432 }
8433 } catch {
8434 entry = null;
8435 }
8436 read.set(filePath, entry);
8437 return entry;
8438 };
8439
8440 const isTest = this.testCodeReading(cg);
8441 const rel = (p: string) => p.replace(/\\/g, '/');
8442 let answer: OneSymbolAnswer | null;
8443 try {
8444 answer = renderOneSymbolAnswer({
8445 cg,
8446 maxChars: budget.maxOutputChars,
8447 // A definition is the answer, so it may take half the tier's ceiling,
8448 // never less than the per-file cap the general path gives a file.
8449 bodyChars: Math.max(budget.maxCharsPerFile, Math.floor(budget.maxOutputChars / 2)),
8450 readLines: (filePath) => readFile(filePath)?.lines ?? null,
8451 isTest,
8452 synthNote: (edge) => this.synthEdgeNote(edge),
8453 elidedNote: elidedBodyNote,
8454 testsThroughCallers: (dependents) => this.indirectTestNote(cg, dependents, rel, isTest).replace(/^;\s*/, ''),
8455 nodeServed: this.isToolServed('node'),
8456 }, name, defs);
8457 } catch {

Callers 1

handleExploreMethod · 0.95

Calls 12

testCodeReadingMethod · 0.95
synthEdgeNoteMethod · 0.95
indirectTestNoteMethod · 0.95
isToolServedMethod · 0.95
answerResultMethod · 0.95
oneSymbolQueryNameFunction · 0.90
oneSymbolDefinitionsFunction · 0.90
renderOneSymbolAnswerFunction · 0.90
fileFingerprintFunction · 0.90
finishDirectMethod · 0.80
mapMethod · 0.80
startMethod · 0.65

Tested by

no test coverage detected