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

Method exploreOneSymbol

src/mcp/tools.ts:8129–8211  ·  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

8127 * drifted files). `CODEGRAPH_EXPLORE_ONE_SYMBOL=0` turns it off, for A/B runs.
8128 */
8129 private exploreOneSymbol(
8130 cg: CodeGraph,
8131 matchQuery: string,
8132 query: string,
8133 projectRoot: string,
8134 budget: ExploreOutputBudget,
8135 maxFiles: number,
8136 indexedFileCount: number,
8137 ): ToolResult | null {
8138 if (process.env.CODEGRAPH_EXPLORE_ONE_SYMBOL === '0') return null;
8139 const name = oneSymbolQueryName(matchQuery);
8140 if (!name) return null;
8141 let defs: Node[] | null;
8142 try {
8143 defs = oneSymbolDefinitions(cg, name);
8144 } catch {
8145 return null;
8146 }
8147 if (!defs) return null;
8148
8149 // Each file read once, and only trusted when it still matches the index:
8150 // a changed file's indexed line numbers can point at other code (#1474).
8151 const read = new Map<string, { content: string; lines: string[] } | null>();
8152 const readFile = (filePath: string) => {
8153 const held = read.get(filePath);
8154 if (held !== undefined) return held;
8155 let entry: { content: string; lines: string[] } | null = null;
8156 try {
8157 const abs = validatePathWithinRoot(projectRoot, filePath);
8158 if (abs && existsSync(abs)) {
8159 const content = readFileSync(abs, 'utf-8');
8160 if (!this.isFileStaleOnDisk(cg, filePath, content)) entry = { content, lines: content.split(/\r?\n/) };
8161 }
8162 } catch {
8163 entry = null;
8164 }
8165 read.set(filePath, entry);
8166 return entry;
8167 };
8168
8169 const isTest = this.testCodeReading(cg);
8170 const rel = (p: string) => p.replace(/\\/g, '/');
8171 let answer: OneSymbolAnswer | null;
8172 try {
8173 answer = renderOneSymbolAnswer({
8174 cg,
8175 maxChars: budget.maxOutputChars,
8176 // A definition is the answer, so it may take half the tier's ceiling,
8177 // never less than the per-file cap the general path gives a file.
8178 bodyChars: Math.max(budget.maxCharsPerFile, Math.floor(budget.maxOutputChars / 2)),
8179 readLines: (filePath) => readFile(filePath)?.lines ?? null,
8180 isTest,
8181 synthNote: (edge) => this.synthEdgeNote(edge),
8182 elidedNote: elidedBodyNote,
8183 testsThroughCallers: (dependents) => this.indirectTestNote(cg, dependents, rel, isTest).replace(/^;\s*/, ''),
8184 nodeServed: this.isToolServed('node'),
8185 }, name, defs);
8186 } 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