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

Method exploreOneSymbol

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

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