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

Function validateAnswerFiles

src/mcp/answer-freshness.ts:18–66  ·  view source on GitHub ↗
(root: string, files: AnswerFile[])

Source from the content-addressed store, hash-verified

16const MAX_MS = 250;
17
18export async function validateAnswerFiles(root: string, files: AnswerFile[]): Promise<{
19 stale: string[];
20 unchecked: string[];
21}> {
22 const stale: string[] = [];
23 const unchecked: string[] = [];
24 const signal = AbortSignal.timeout(MAX_MS);
25 let bytes = 0;
26 for (let i = 0; i < files.length; i++) {
27 const file = files[i]!;
28 if (i >= MAX_FILES || bytes >= MAX_BYTES || signal.aborted) {
29 unchecked.push(...files.slice(i).map(f => f.path));
30 break;
31 }
32 if (!file.contentHash) {
33 unchecked.push(file.path);
34 continue;
35 }
36 try {
37 const hash = createHash('sha256');
38 const absolute = validatePathWithinRoot(root, file.path);
39 if (!absolute) { stale.push(file.path); continue; }
40 // A file over the index's size limit is stored as its size stamp
41 // (#1910): when the stamp still matches, the file is current without
42 // being read. Anything else falls through to the bounded read below.
43 const { size } = await stat(absolute);
44 if (size > MAX_SOURCE_FILE_SIZE_BYTES &&
45 createHash('sha256').update(oversizeStamp(size)).digest('hex') === file.contentHash) {
46 continue;
47 }
48 const stream = createReadStream(absolute, {
49 encoding: 'utf8', highWaterMark: 64 * 1024, signal,
50 });
51 let complete = true;
52 for await (const chunk of stream) {
53 bytes += Buffer.byteLength(chunk);
54 if (bytes > MAX_BYTES) { complete = false; break; }
55 hash.update(chunk);
56 }
57 if (!complete) unchecked.push(file.path);
58 else if (hash.digest('hex') !== file.contentHash) stale.push(file.path);
59 } catch {
60 // Timeout is unknown, never a clean bill of health. A missing or
61 // unreadable contributing file cannot support the indexed answer.
62 (signal.aborted ? unchecked : stale).push(file.path);
63 }
64 }
65 return { stale, unchecked };
66}

Callers 3

executeMethod · 0.90

Calls 5

validatePathWithinRootFunction · 0.90
oversizeStampFunction · 0.90
pushMethod · 0.80
mapMethod · 0.80
updateMethod · 0.80

Tested by

no test coverage detected