Files
colbymchenry--codegraph/__tests__/explore-blast-radius.test.ts
Colby Mchenry 68eaf0dbd8 feat(mcp): codegraph_explore as the sole primary tool + store coverage + overload disambiguation (#647)
## Summary

Completes the explore-overhaul arc: `codegraph_explore` becomes the single primary tool an agent reaches for, and its coverage + output shape are tuned so flow/architecture questions resolve with near-zero Read/Grep.

### What changed
- **explore is the sole primary tool** — removed `codegraph_context` (the fuzzy-input Read-trigger) and `codegraph_trace` (under-picked by agents); explore already surfaces the call flow among the symbols you name. A plain natural-language question now works as the query.
- **Store/handler coverage** — functions defined inside object literals (Zustand `create((set, get) => ({ … }))`, Redux/Pinia/MobX, exported handler/route maps) are indexed as real symbols, including calls through `useStore.getState().fn()` and destructured `const { fn } = useStore.getState()`. A general AST rule, not a per-lib hack.
- **Overload disambiguation** — explore leads with the *right* definition when a method name is overloaded across types (a PascalCase type token in the query biases to that type's own def); `codegraph_node` returns *every* overload's body in one call, with an optional `file`/`line` selector to pin one.
- **Method-atomic render** — explore never returns half a method; at the size budget it drops whole methods/files (and lists what it dropped) instead of truncating a body mid-method.
- **Native-read-shaped output** — per-call output is capped to ~24K with a 25K hard ceiling and concentrated into ~150–250-line flow windows, mirroring how the agent natively reads; repo size scales the *call* budget, not the per-call size (a larger response just gets externalized to a file the host Reads back).
- **Blast radius** folded into explore (dependents + covering tests, locations only).

### Benchmark (refreshed on this build)
Re-validated the 7-repo A/B on 2026-06-02 (Opus 4.8, effort=high, median of 4). WITH arm re-measured on this build, WITHOUT reused:

**~16% cheaper · 47% fewer tokens · 22% faster · 58% fewer tool calls** — 0 file reads on 6 of 7 repos (Gin ~1).

The arc trades larger, cache-heavy explore responses for guaranteed near-zero reads, so cost/token margins soften vs the prior build (Excalidraw and Tokio land at cost break-even) while time and tool-calls stay clear wins everywhere — consistent with the project's stated optimization target (latency + tool-calls, not token cost).

### Validation
- Full suite green: **1112 passed, 2 skipped**.
- 28/28 plain WITH runs across the 7 README repos completed clean; reads median 0 on 6/7.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-06-02 10:15:27 -05:00

74 lines
2.8 KiB
TypeScript

/**
* codegraph_explore blast-radius section.
*
* explore now appends a compact, always-on "Blast radius" for the entry
* symbols: who depends on each (locations only — no source) and which test
* files cover it, so the agent knows what to update/verify before editing
* without a separate impact call. Symbols with no dependents are skipped, and
* the section is omitted entirely when nothing qualifies.
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import CodeGraph from '../src/index';
import { ToolHandler } from '../src/mcp/tools';
describe('codegraph_explore — blast radius', () => {
let testDir: string;
let cg: CodeGraph;
let handler: ToolHandler;
beforeEach(async () => {
testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-blast-'));
const src = path.join(testDir, 'src');
fs.mkdirSync(src, { recursive: true });
// `target` is depended on by a sibling (caller) and a test file.
fs.writeFileSync(
path.join(src, 'feature.ts'),
`export function target() { return 1; }\n` +
`export function caller() { return target(); }\n`,
);
fs.writeFileSync(
path.join(src, 'feature.test.ts'),
`import { target } from './feature';\n` +
`export function checkTarget() { return target(); }\n`,
);
// A leaf with no dependents — must NOT show up in the blast radius.
fs.writeFileSync(
path.join(src, 'leaf.ts'),
`export function lonelyLeaf() { return 42; }\n`,
);
cg = CodeGraph.initSync(testDir, { config: { include: ['**/*.ts'], exclude: [] } });
await cg.indexAll();
handler = new ToolHandler(cg);
});
afterEach(() => {
if (cg) cg.destroy();
if (fs.existsSync(testDir)) fs.rmSync(testDir, { recursive: true, force: true });
});
it('lists dependents (locations only) and covering tests for an entry symbol', async () => {
const res = await handler.execute('codegraph_explore', { query: 'target' });
const text = res.content[0].text;
expect(text).toContain('### Blast radius');
expect(text).toContain('`target`');
expect(text).toMatch(/caller/); // a caller count is reported
// It names WHERE (the caller file) — not the caller's source body.
expect(text).toContain('feature.ts');
// Test coverage is surfaced (either the covering test file, or the warning).
expect(text).toMatch(/tests:.*feature\.test\.ts|no covering tests/);
});
it('omits symbols that have no dependents from the blast radius', async () => {
const res = await handler.execute('codegraph_explore', { query: 'lonelyLeaf' });
const text = res.content[0].text;
// lonelyLeaf has zero callers — it must never appear under a blast-radius bullet.
expect(text).not.toMatch(/Blast radius[\s\S]*`lonelyLeaf`/);
});
});