GitNexus/gitnexus/test/helpers/file-result-worker.ts
Gergő Magyar ea97d6db89
fix(cache): reuse unchanged files across chunk boundaries (#3856)
* fix(cache): reuse unchanged files across chunks

Key parser facts and scopes per file while retaining batched workers
and compressed pack storage. Preserve staged repairs and recovery.

Fixes #3849

* ci: gate parse cache speed and memory

Compare fresh baseline and candidate processes on both corpora.
Require paired speed and RSS improvements, graph equality, and
bounded phase regressions; retain measure.json and failed trials.

* perf(cache): reduce result restore overhead

Batch adjacent pack reads within bounded windows. Avoid duplicate
validation and oversized decompression buffers while preserving
binding, checksum, output-limit, and corruption checks.

* ci: keep checks active for stacked cache PR

Allow PRs targeting fix/3852-root-cause to run the test and performance gates while the cache fix is stacked.

* fix(cache): address review feedback (#3856)

Keep optional cache failures from aborting analysis and retain immutable
packs to avoid copying every unchanged result during publication. Bind
benchmark corpus hashes unambiguously and cover worker fallback paths.

Validation: build, typechecks, focused suites, lint and smoke tests pass.
The full run passed 32,878 tests; two failures observed an overlapping
edit/build. Both complete failing files pass fresh reruns (64 tests).

* fix(cache): bound index reads and reuse checks

Check and read the same open index to prevent pathname replacement
from bypassing the size bound. Reject growth and truncation.

Require exactly one reparse in the Java edit benchmark and update
the expected cache counts while retaining semantic and scaling checks.

* perf(cache): share decoded fact strings

Deduplicate paths, IDs, and labels within each decode batch to reduce retained heap and peak RSS. Preserve valid facts and cache hits if optional interning fails.

Refs #3856

---------

Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
2026-11-10 10:52:15 +00:00

50 lines
2.9 KiB
TypeScript

/** Small worker fixture using the production file-result codec and flush protocol. */
import fs from 'node:fs';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
export function writeFileResultWorker(
workerPath: string,
markerPath: string,
options: { uncacheablePaths?: string[]; scopeFailures?: string[]; crashPaths?: string[] } = {},
): void {
const codec = pathToFileURL(path.resolve('dist/storage/file-result-codec.js')).href;
fs.writeFileSync(
workerPath,
`
const fs = require('node:fs');
const { parentPort } = require('node:worker_threads');
const options = ${JSON.stringify(options)};
const empty = () => ({ nodes: [], relationships: [], symbols: [], calls: [], assignments: [], routes: [], fetchCalls: [], fetchWrapperDefs: [], decoratorRoutes: [], routerIncludes: [], routerImports: [], toolDefs: [], ormQueries: [], constructorBindings: [], fileScopeBindings: [], parsedFiles: [], skippedLanguages: {}, scopeExtractionFailures: [], fileCount: 0 });
import(${JSON.stringify(codec)}).then(({ encodeFileResultSection }) => {
fs.writeFileSync(${JSON.stringify(markerPath)}, 'spawned');
let accumulated = empty();
parentPort.on('message', msg => {
if (msg.type === 'sub-batch') {
for (const file of msg.files) {
parentPort.postMessage({ type: 'starting-file', path: file.path });
if (options.crashPaths?.includes(file.path)) process.exit(134);
const result = empty();
const name = file.path.split('/').pop().replace(/\\.\\w+$/, '');
result.nodes.push({ id: 'Function:' + file.path + ':' + name, label: 'Function', properties: { name, filePath: file.path, startLine: 1, endLine: 1, language: 'typescript' } });
result.parsedFiles.push({ filePath: file.path, moduleScope: '', scopes: [], parsedImports: [], localDefs: [], referenceSites: [] });
if (options.scopeFailures?.includes(file.path)) result.scopeExtractionFailures.push(file.path);
result.fileCount = 1;
const facts = encodeFileResultSection(file.cacheKey, file.path, 'facts', { ...result, parsedFiles: [] });
const scopes = encodeFileResultSection(file.cacheKey, file.path, 'scopes', result.parsedFiles);
const entry = { key: file.cacheKey, path: file.path };
(accumulated.fileResults ??= []).push(facts && scopes
? { ...entry, encoded: { ...entry, facts, scopes, cacheable: !options.uncacheablePaths?.includes(file.path) && !result.scopeExtractionFailures.length } }
: { ...entry, raw: result });
accumulated.scopeExtractionFailures.push(...result.scopeExtractionFailures);
accumulated.fileCount++;
}
parentPort.postMessage({ type: 'progress', filesProcessed: accumulated.fileCount });
parentPort.postMessage({ type: 'sub-batch-done' });
}
if (msg.type === 'flush') { parentPort.postMessage({ type: 'result', data: accumulated }); accumulated = empty(); }
});
parentPort.postMessage({ type: 'ready' });
});
`,
);
}