Two coupled changes addressing the issue's underlying ask — "how does the
agent know when the index lags" — without resorting to a static wait.
Per-file staleness banner
-------------------------
FileWatcher now tracks per-path `pendingFiles` (path, firstSeenMs,
lastSeenMs, indexing) — events since the last successful sync, cleared
only after a sync whose `syncStartedMs >= lastSeenMs` commits. Chokidar
initial-scan events are gated behind a `ready` flag (with `waitUntilReady()`
exposed so tests can deterministically wait through it) so a fresh startup
doesn't falsely flag every existing file as pending.
ToolHandler now wraps every code-returning response (search, context,
callers, callees, impact, trace, explore, node, files) with
`withStalenessNotice`: intersects "files referenced in the response" with
`getPendingFiles()` and emits a hybrid signal —
* banner at the top for files referenced AND pending (with edit age +
indexing/pending-sync state, telling the agent to Read those specific
files directly; the rest of the response stays fresh and codegraph
stays authoritative for it),
* compact footer for pending files elsewhere in the project not
referenced above (capped at 5).
Cost is one boolean check + N substring matches when pending; zero
allocation when idle. `codegraph_status` surfaces the same data as a
first-class `### Pending sync:` section so the agent can ask "is the index
caught up?" in one call.
Cross-project quirk: when an agent passes `projectPath` matching the
default session's project, the staleness wrapper switches from the cached
cross-project CodeGraph (no watcher) to the default one (with watcher) so
the signal still fires. Same fix applied to `handleStatus`.
CODEGRAPH_WATCH_DEBOUNCE_MS
---------------------------
MCP `serve --mcp` now reads `CODEGRAPH_WATCH_DEBOUNCE_MS` and forwards it
to `cg.watch({ debounceMs })`. Clamped to [100ms, 60s]; out-of-range or
non-numeric values fall back to the FileWatcher default (2000ms). Active
value is logged to stderr on watcher startup so it's discoverable. The
docs in `server-instructions.ts`, `installer/instructions-template.ts`,
and `.cursor/rules/codegraph.mdc` no longer claim "~500ms"; they now
describe the banner mechanism instead — since per-file staleness replaces
the "wait N ms" guidance entirely, the docs become accurate at any
debounce value.
Validation
----------
* 847 unit/integration tests pass (added 15 new ones — pending-file
tracking, banner/footer routing, status section, env-var parsing).
* Direct MCP probe through a real `codegraph serve --mcp` process: edit a
file, query within the debounce window, banner fires naming the
edited file with edit-age.
* Real Claude TUI session via `scripts/agent-eval/itrun.sh` with
`CODEGRAPH_WATCH_DEBOUNCE_MS=10000`: agent edits `math.ts`, calls
`codegraph_explore`, reads the banner, **and discloses it unprompted in
its final reply**: "note: symbol index is mid-sync for the new `divide`,
but the source it returned is verbatim from disk."
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
4a4a37d135
commit
b48170e69f
@@ -185,7 +185,18 @@ export class MCPEngine {
|
||||
return;
|
||||
}
|
||||
|
||||
// Optional override for the debounce window via env var (issue #403).
|
||||
// Useful for workspaces with bursty writes (formatter-on-save chains,
|
||||
// large generated outputs) where the 2s default fires too often. Clamped
|
||||
// to [100ms, 60s]; out-of-range / non-numeric values fall back to the
|
||||
// FileWatcher default. We log the active value so it's discoverable.
|
||||
const debounceMs = parseDebounceEnv(process.env.CODEGRAPH_WATCH_DEBOUNCE_MS);
|
||||
if (debounceMs !== undefined) {
|
||||
process.stderr.write(`[CodeGraph MCP] File watcher debounce: ${debounceMs}ms (CODEGRAPH_WATCH_DEBOUNCE_MS)\n`);
|
||||
}
|
||||
|
||||
const started = this.cg.watch({
|
||||
debounceMs,
|
||||
onSyncComplete: (result) => {
|
||||
if (result.filesChanged > 0) {
|
||||
process.stderr.write(
|
||||
@@ -230,3 +241,24 @@ export class MCPEngine {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse and clamp the CODEGRAPH_WATCH_DEBOUNCE_MS env override.
|
||||
*
|
||||
* Issue #403: workspaces with bursty writes (formatter-on-save, multi-file
|
||||
* refactors) sometimes want a longer quiet window before sync. Returns
|
||||
* `undefined` for unset / empty / non-numeric / out-of-range values so the
|
||||
* FileWatcher default (2000ms) takes over — never throws.
|
||||
*
|
||||
* Clamp range: 100ms (faster would mean a sync per keystroke) to 60s (longer
|
||||
* and the watcher feels broken). Out-of-range values are treated as "ignore
|
||||
* this misconfiguration" rather than capped, since silently capping a 0 or
|
||||
* a typoed value would mask a real config bug.
|
||||
*/
|
||||
export function parseDebounceEnv(raw: string | undefined): number | undefined {
|
||||
if (!raw || !raw.trim()) return undefined;
|
||||
const n = Number(raw);
|
||||
if (!Number.isFinite(n) || !Number.isInteger(n)) return undefined;
|
||||
if (n < 100 || n > 60000) return undefined;
|
||||
return n;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user