* Handle codegraph_callers
(args: Record<string, unknown>)
| 3423 | * Handle codegraph_callers |
| 3424 | */ |
| 3425 | private async handleCallers(args: Record<string, unknown>): Promise<ToolResult> { |
| 3426 | const symbol = this.validateString(args.symbol, 'symbol'); |
| 3427 | if (typeof symbol !== 'string') return symbol; |
| 3428 | |
| 3429 | const cg = this.getCodeGraph(args.projectPath as string | undefined); |
| 3430 | const limit = clamp((args.limit as number) || 20, 1, 100); |
| 3431 | const fileFilter = typeof args.file === 'string' ? args.file : undefined; |
| 3432 | |
| 3433 | const allMatches = this.findAllSymbols(cg, symbol); |
| 3434 | if (allMatches.nodes.length === 0) { |
| 3435 | return this.textResult(`Symbol "${symbol}" not found in the codebase${allMatches.note}`); |
| 3436 | } |
| 3437 | |
| 3438 | const { groups, filteredOut } = this.groupDefinitions(allMatches.nodes, fileFilter); |
| 3439 | const answerPaths = new Set(groups.flat().map(node => node.filePath)); |
| 3440 | const filterNote = filteredOut |
| 3441 | ? `\n\n> **Note:** no definition of "${symbol}" matches file "${fileFilter}" — showing all definitions instead.` |
| 3442 | : ''; |
| 3443 | |
| 3444 | // Every site of each caller (#2909): one row per caller, at the lines its |
| 3445 | // calls sit on, and the symbol itself when it calls itself. |
| 3446 | const collect = (defNodes: Node[]) => { |
| 3447 | const callers: Node[] = []; |
| 3448 | const labels = new Map<string, string>(); |
| 3449 | const sites = new Map<string, Edge[]>(); |
| 3450 | const recursive = new Set<string>(); |
| 3451 | for (const node of defNodes) { |
| 3452 | for (const c of cg.getCallSites(node.id, 'callers')) { |
| 3453 | const known = sites.get(c.node.id); |
| 3454 | if (known) { |
| 3455 | known.push(...c.edges); |
| 3456 | continue; |
| 3457 | } |
| 3458 | sites.set(c.node.id, [...c.edges]); |
| 3459 | if (c.recursive) recursive.add(c.node.id); |
| 3460 | callers.push(c.node); |
| 3461 | answerPaths.add(c.node.filePath); |
| 3462 | const label = this.edgeLabel(c.edges[0]!); |
| 3463 | if (label) labels.set(c.node.id, label); |
| 3464 | } |
| 3465 | } |
| 3466 | const locations = new Map(callers.map((n) => [n.id, this.callSiteLocation(n, sites.get(n.id) ?? [])])); |
| 3467 | return { callers, labels, locations, recursive }; |
| 3468 | }; |
| 3469 | |
| 3470 | // Single definition (or same-file overloads): the familiar flat list. |
| 3471 | if (groups.length === 1) { |
| 3472 | const { callers, labels, locations, recursive } = collect(groups[0]!); |
| 3473 | if (callers.length === 0) { |
| 3474 | return this.answerResult(cg, `No callers found for "${symbol}"${allMatches.note}${filterNote}`, answerPaths); |
| 3475 | } |
| 3476 | // A successful `file` narrowing makes the multi-symbol aggregation note |
| 3477 | // stale — suppress it. |
| 3478 | const note = fileFilter && !filteredOut ? '' : allMatches.note; |
| 3479 | // Say when the cap cut the list (#1639, #1674): a truncated answer with |
| 3480 | // no marker reads as the complete set, and an agent under-counts from it. |
| 3481 | const cut = callers.length > limit |
| 3482 | ? `\n\n> Showing ${limit} of ${callers.length} callers; pass \`limit\` (up to 100) to widen.` |
no test coverage detected