Skip to content

Graphs that teach > graphs that impress. Turn any code into an interactive knowledge graph you can explore, search, and ask questions about. Works with Claude Code, Codex, Cursor, Copilot, Gemini CLI, and more.

fix(incremental): reuse preserved scan on retries

Lum1104c66caa0f2ad7
diff --git a/tests/skill/understand/test_prepare_incremental.test.mjs b/tests/skill/understand/test_prepare_incremental.test.mjs
index 3afb25d5..b483e993 100644
--- a/tests/skill/understand/test_prepare_incremental.test.mjs
+++ b/tests/skill/understand/test_prepare_incremental.test.mjs
@@ -616,6 +616,35 @@ describe('prepare-incremental.mjs', { timeout: 30_000 }, () => {
expect(second.filesToReanalyze).toEqual(['new-package/index.ts']);
});
+ it('reuses the preserved import map when retrying a different head', () => {
+ const { root, baseCommit } = setupRepository({
+ 'src/index.js': "const value = require('./a');\nmodule.exports = value;\n",
+ 'src/a.js': 'module.exports = 1;\n',
+ 'src/b.js': 'module.exports = 2;\n',
+ 'src/c.js': 'function c() { return 3; }\nmodule.exports = c;\n',
+ });
+ writeProjectFile(
+ root,
+ 'src/index.js',
+ "const value = require('./b');\nmodule.exports = value;\n",
+ );
+ commit(root, 'abandoned head changes import');
+ const abandoned = prepare(root, baseCommit);
+ expect(abandoned.scan.importMap['src/index.js']).toEqual(['src/b.js']);
+
+ git(root, ['reset', '--hard', baseCommit]);
+ writeProjectFile(
+ root,
+ 'src/c.js',
+ 'function changedC() { return 3; }\nmodule.exports = changedC;\n',
+ );
+ commit(root, 'replacement head changes another file');
+ const replacement = prepare(root, baseCommit);
+
+ expect(replacement.scan.importMap['src/index.js']).toEqual(['src/a.js']);
+ expect(replacement.plan.filesToReanalyze).toEqual(['src/c.js']);
+ });
+
it('does not advance the baseline for generated-artifact-only commits', () => {
const { root, baseCommit } = setupRepository({
'src/a.ts': 'export const a = 1;\n',
diff --git a/understand-anything-plugin/skills/understand/prepare-incremental.mjs b/understand-anything-plugin/skills/understand/prepare-incremental.mjs
index 5eb70856..d3341df8 100644
--- a/understand-anything-plugin/skills/understand/prepare-incremental.mjs
+++ b/understand-anything-plugin/skills/understand/prepare-incremental.mjs
@@ -382,7 +382,7 @@ function runScan(projectRoot, outputPath, excludePatterns) {
return readJson(outputPath);
}
-function refreshImportMap({ projectRoot, intermediateDir, oldScan, currentScan, analysisPaths }) {
+function refreshImportMap({ projectRoot, intermediateDir, previousScan, currentScan, analysisPaths }) {
const inputPath = join(intermediateDir, 'incremental-import-input.json');
const outputPath = join(intermediateDir, 'incremental-import-output.json');
atomicWriteJson(inputPath, {
@@ -408,7 +408,7 @@ function refreshImportMap({ projectRoot, intermediateDir, oldScan, currentScan,
const path = file.path;
const candidate = Object.hasOwn(selective, path)
? selective[path]
- : oldScan?.importMap?.[path];
+ : previousScan?.importMap?.[path];
importMap[path] = Array.isArray(candidate)
? sorted(candidate.filter(target => typeof target === 'string' && currentPaths.has(target)))
: [];
@@ -608,7 +608,7 @@ async function main() {
decision.filesToReanalyze.filter(path => currentInventorySet.has(path)),
);
const missingImportEntries = currentInventory.filter(
- path => !Object.hasOwn(oldScan?.importMap ?? {}, path),
+ path => !Object.hasOwn(baselineScan?.importMap ?? {}, path),
);
// Adding/removing a resolution candidate can change an unchanged importer's
// winner (foo.ts vs foo.js vs foo/index.ts). Resolver configuration edits
@@ -624,7 +624,7 @@ async function main() {
const importMap = refreshImportMap({
projectRoot,
intermediateDir,
- oldScan,
+ previousScan: baselineScan,
currentScan,
analysisPaths: importAnalysisPaths,
});