Files
codegraph/__tests__/mcp-catchup-gate.test.ts
T
ace8d8a0d0 fix(mcp): stop the first tool call hanging on a huge-repo catch-up reconcile (#905) (#950)
On a very large repo (the report is a ~93k-file / 5.7GB-DB Java monorepo) the
first MCP `tools/call` after a fresh `serve --mcp` could hang for 10+ minutes
with zero output, and with the liveness watchdog on, the daemon was SIGKILLed
mid-query instead. Root cause: the post-open catch-up reconcile that the first
tool call is gated on does ~2*N synchronous `fs.existsSync`/`fs.statSync` calls
plus a load-all-files query in two non-yielding loops. On a huge repo that wedges
the event loop for minutes, which (a) trips the 60s watchdog (it SIGKILLs a
process whose loop stops turning) and (b) blocks the first call the whole time.

Two complementary fixes:

- Make the reconcile yield. `ExtractionOrchestrator.sync()` now uses the
  yielding `scanDirectoryAsync`, and both O(files) reconcile loops
  `await setImmediate` every SYNC_RECONCILE_YIELD_INTERVAL (1000) files. The loop
  can no longer wedge the main thread, so the watchdog stays fed and the socket /
  any concurrent read stays responsive while a big reconcile runs. Results are
  unchanged — only yield points are added.

- Time-box the catch-up gate. The first `tools/call` now waits on the reconcile
  for at most CODEGRAPH_CATCHUP_GATE_TIMEOUT_MS (default 3000ms), then serves and
  lets the reconcile finish in the background (which now yields, so the served
  call runs concurrently). `=0` restores the old unbounded wait. On a normal repo
  the reconcile finishes well under the budget, so behavior is unchanged.

Tests: adds two time-box cases to mcp-catchup-gate (serves promptly when the
reconcile runs long; `=0` restores the unbounded wait). Full suite green
(1655 passed). Validated end-to-end through the real daemon: first call returns
at the ~3s time-box instead of waiting an injected 8s reconcile; no-delay control
unchanged; `=0` opt-out waits the full reconcile.

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-22 11:31:49 -05:00

174 lines
7.2 KiB
TypeScript

/**
* MCP catch-up gate — first tool call blocks on the engine's post-open
* filesystem reconcile so it never serves rows for files that were
* deleted (or edited) while no MCP server was running.
*
* Background: `MCPEngine.catchUpSync()` fires `cg.sync()` in the background.
* Before this fix it was fire-and-forget — a tool call could race past it
* and return rows for files that no longer exist on disk. The per-file
* staleness banner (`withStalenessNotice`) couldn't help, because
* `getPendingFiles()` is populated by the watcher, not by catch-up.
*
* The fix: `catchUpSync()` pushes its promise into the `ToolHandler` via
* `setCatchUpGate(p)`; the first `execute()` call awaits the gate and then
* clears it. These tests exercise the gate directly (deterministic) and
* the engine-driven path (proves the engine actually pokes the gate).
*/
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('MCP catch-up gate', () => {
let testDir: string;
let cg: CodeGraph;
let handler: ToolHandler;
beforeEach(async () => {
testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-catchup-gate-'));
fs.mkdirSync(path.join(testDir, 'src'));
fs.writeFileSync(
path.join(testDir, 'src', 'survivor.ts'),
'export function survivor() { return 1; }\n',
);
fs.writeFileSync(
path.join(testDir, 'src', 'deleted-later.ts'),
'export function deletedLater() { return 2; }\n',
);
cg = CodeGraph.initSync(testDir, { config: { include: ['**/*.ts'], exclude: [] } });
await cg.indexAll();
handler = new ToolHandler(cg);
});
afterEach(() => {
try { cg.unwatch(); } catch { /* ignore */ }
try { cg.close(); } catch { /* ignore */ }
if (fs.existsSync(testDir)) fs.rmSync(testDir, { recursive: true, force: true });
});
it('awaits the gate before serving the first tool call', async () => {
let gateResolved = false;
const gate = new Promise<void>((resolve) => {
setTimeout(() => { gateResolved = true; resolve(); }, 80);
});
handler.setCatchUpGate(gate);
const res = await handler.execute('codegraph_search', { query: 'survivor' });
expect(gateResolved).toBe(true);
expect(res.isError).toBeFalsy();
expect(res.content[0].text).toMatch(/survivor/);
});
it('drops the gate after first await — second call does not re-wait', async () => {
let awaitCount = 0;
const gate = new Promise<void>((resolve) => {
awaitCount++;
setTimeout(resolve, 20);
});
handler.setCatchUpGate(gate);
await handler.execute('codegraph_search', { query: 'survivor' });
const before = awaitCount;
await handler.execute('codegraph_search', { query: 'survivor' });
// The promise body runs once when constructed; second execute never
// resubscribes to a fresh promise because the gate field was nulled.
expect(awaitCount).toBe(before);
});
it('catch-up reconciles a deleted file before the first tool call sees it', async () => {
// Simulate the empty-project / deleted-files startup case: file is in
// the DB (we indexed it above) but vanishes from disk before the MCP
// server's first query. The catch-up sync, awaited via the gate,
// must remove the row so the first tool call returns no hit.
fs.unlinkSync(path.join(testDir, 'src', 'deleted-later.ts'));
// Push the actual catch-up sync as the gate — same flow the MCP engine
// uses (`cg.sync()` returns a Promise<SyncResult>, the wrapper voids it).
handler.setCatchUpGate(cg.sync().then(() => undefined));
const res = await handler.execute('codegraph_search', { query: 'deletedLater' });
expect(res.isError).toBeFalsy();
const text = res.content[0].text;
expect(text).not.toMatch(/src\/deleted-later\.ts/);
});
it('catch-up that converges the project to 0 files clears all rows', async () => {
// Worst case: every source file is gone between sessions. Without the
// gate, the first tool call serves whatever was in the DB. With the
// gate + the orchestrator's filesystem reconcile, the DB drains.
fs.unlinkSync(path.join(testDir, 'src', 'survivor.ts'));
fs.unlinkSync(path.join(testDir, 'src', 'deleted-later.ts'));
handler.setCatchUpGate(cg.sync().then(() => undefined));
const res = await handler.execute('codegraph_search', { query: 'survivor' });
expect(res.isError).toBeFalsy();
expect(cg.getStats().fileCount).toBe(0);
});
it('does not hang the first call when catch-up runs past the timeout (#905)', async () => {
// The issue #905 hang: on a huge repo the post-open reconcile takes minutes,
// and gating the first tool call on all of it reads as a multi-minute hang.
// With the time-box, the call is served promptly and the reconcile finishes
// in the background.
const prev = process.env.CODEGRAPH_CATCHUP_GATE_TIMEOUT_MS;
process.env.CODEGRAPH_CATCHUP_GATE_TIMEOUT_MS = '50';
let timer: NodeJS.Timeout | undefined;
try {
let gateResolved = false;
const gate = new Promise<void>((resolve) => {
timer = setTimeout(() => { gateResolved = true; resolve(); }, 5000);
});
handler.setCatchUpGate(gate);
const started = Date.now();
const res = await handler.execute('codegraph_search', { query: 'survivor' });
const elapsed = Date.now() - started;
expect(res.isError).toBeFalsy();
expect(res.content[0].text).toMatch(/survivor/);
// Served on the timeout (~50ms), NOT after the 5s reconcile.
expect(gateResolved).toBe(false);
expect(elapsed).toBeLessThan(2000);
} finally {
if (timer) clearTimeout(timer);
if (prev === undefined) delete process.env.CODEGRAPH_CATCHUP_GATE_TIMEOUT_MS;
else process.env.CODEGRAPH_CATCHUP_GATE_TIMEOUT_MS = prev;
}
});
it('CODEGRAPH_CATCHUP_GATE_TIMEOUT_MS=0 restores the unbounded wait', async () => {
const prev = process.env.CODEGRAPH_CATCHUP_GATE_TIMEOUT_MS;
process.env.CODEGRAPH_CATCHUP_GATE_TIMEOUT_MS = '0';
try {
let gateResolved = false;
const gate = new Promise<void>((resolve) => {
setTimeout(() => { gateResolved = true; resolve(); }, 80);
});
handler.setCatchUpGate(gate);
const res = await handler.execute('codegraph_search', { query: 'survivor' });
// With the time-box disabled, the call waits for the full reconcile.
expect(gateResolved).toBe(true);
expect(res.isError).toBeFalsy();
} finally {
if (prev === undefined) delete process.env.CODEGRAPH_CATCHUP_GATE_TIMEOUT_MS;
else process.env.CODEGRAPH_CATCHUP_GATE_TIMEOUT_MS = prev;
}
});
it('gate that rejects does not break the tool call', async () => {
// A catch-up sync failure (lock contention, transient FS error) must
// not poison tool dispatch — the engine logs it, the handler proceeds.
handler.setCatchUpGate(Promise.reject(new Error('simulated sync failure')));
const res = await handler.execute('codegraph_search', { query: 'survivor' });
expect(res.isError).toBeFalsy();
expect(res.content[0].text).toMatch(/survivor/);
});
});