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

Method isFileStaleOnDisk

src/mcp/tools.ts:2871–2906  ·  view source on GitHub ↗

* On-disk drift check for a single indexed file (issue #1474). The code * renderers slice CURRENT bytes at INDEXED line ranges; when the file * changed after its last index sync those ranges can point at a DIFFERENT * symbol's code — served under the requested name with `isError: false`.

(cg: CodeGraph, relPath: string, content?: string)

Source from the content-addressed store, hash-verified

2869 * would needlessly push the agent back to Read.
2870 */
2871 private isFileStaleOnDisk(cg: CodeGraph, relPath: string, content?: string): boolean {
2872 let root: string;
2873 try {
2874 root = cg.getProjectRoot();
2875 } catch {
2876 return false;
2877 }
2878 const key = `${root}\0${relPath}`;
2879 const now = Date.now();
2880 const hit = this.driftCache.get(key);
2881 if (hit && now - hit.at < ToolHandler.DRIFT_TTL_MS) return hit.stale;
2882 let stale = false;
2883 try {
2884 const rec = cg.getFile(relPath);
2885 const absPath = rec ? validatePathWithinRoot(root, relPath) : null;
2886 if (rec && absPath && existsSync(absPath)) {
2887 const st = statSync(absPath);
2888 // Same freshness test as the sync fast path (extraction/index.ts):
2889 // equal size + equal floored mtime ⇒ unchanged, no read needed.
2890 if (st.size !== rec.size || Math.floor(st.mtimeMs) !== Math.floor(rec.modifiedAt)) {
2891 // A file over the index's size limit is stored as its size stamp
2892 // (#1910), so it is compared as one, without reading it.
2893 const data = indexedHashInput(st.size, () => content ?? readFileSync(absPath, 'utf-8'));
2894 // Must stay byte-identical to extraction's `hashContent` (sha256 over
2895 // the utf-8 string) — the identical-rewrite test in
2896 // mcp-stale-slice.test.ts pins the parity. Inlined (not imported)
2897 // to keep the extraction module off the MCP startup path.
2898 stale = createHash('sha256').update(data).digest('hex') !== rec.contentHash;
2899 }
2900 }
2901 } catch {
2902 stale = false;
2903 }
2904 this.driftCache.set(key, { at: now, stale });
2905 return stale;
2906 }
2907
2908 private answerResult(cg: CodeGraph, text: string, paths: Iterable<string>): ToolResult {
2909 const result = this.textResult(text);

Callers 3

handleExploreMethod · 0.95
renderNodeSectionMethod · 0.95

Calls 7

validatePathWithinRootFunction · 0.90
indexedHashInputFunction · 0.90
getFileMethod · 0.80
updateMethod · 0.80
getProjectRootMethod · 0.65
getMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected