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): reject relevant dirty worktree state

Lum1104da9855dd27ec
diff --git a/tests/skill/understand/test_prepare_incremental.test.mjs b/tests/skill/understand/test_prepare_incremental.test.mjs
index 1724f331..c77dff9a 100644
--- a/tests/skill/understand/test_prepare_incremental.test.mjs
+++ b/tests/skill/understand/test_prepare_incremental.test.mjs
@@ -504,6 +504,62 @@ describe('prepare-incremental.mjs', { timeout: 30_000 }, () => {
const meta = JSON.parse(readFileSync(join(root, '.ua', 'meta.json'), 'utf-8'));
expect(meta.gitCommitHash).toBe(baseCommit);
});
+
+ it('refuses to stamp HEAD when an analyzable untracked file is present', () => {
+ const { root, baseCommit } = setupRepository({
+ 'src/a.ts': 'export const a = 1;\n',
+ 'src/b.ts': 'export const b = 2;\n',
+ 'src/c.ts': 'export const c = 3;\n',
+ 'src/d.ts': 'export const d = 4;\n',
+ });
+ const metaPath = join(root, '.ua', 'meta.json');
+ const metaBefore = readFileSync(metaPath, 'utf-8');
+ writeProjectFile(root, 'src/a.ts', 'export const renamed = 1;\n');
+ commit(root, 'committed structural change');
+ writeProjectFile(root, 'src/uncommitted.ts', 'export const dirty = true;\n');
+
+ const result = spawnSync(process.execPath, [prepareScript, root, baseCommit], {
+ cwd: root,
+ encoding: 'utf-8',
+ });
+ expect(result.status).not.toBe(0);
+ expect(result.stderr).toContain('relevant uncommitted changes: src/uncommitted.ts');
+ expect(readFileSync(metaPath, 'utf-8')).toBe(metaBefore);
+ });
+
+ it('refuses a partial-commit update when another analyzed file is still unstaged', () => {
+ const { root, baseCommit } = setupRepository({
+ 'src/a.ts': 'export const a = 1;\n',
+ 'src/b.ts': 'export const b = 2;\n',
+ 'src/c.ts': 'export const c = 3;\n',
+ 'src/d.ts': 'export const d = 4;\n',
+ });
+ writeProjectFile(root, 'src/a.ts', 'export const renamed = 1;\n');
+ commit(root, 'partial commit');
+ writeProjectFile(root, 'src/b.ts', 'export const stillDirty = 2;\n');
+
+ const result = spawnSync(process.execPath, [prepareScript, root, baseCommit], {
+ cwd: root,
+ encoding: 'utf-8',
+ });
+ expect(result.status).not.toBe(0);
+ expect(result.stderr).toContain('relevant uncommitted changes: src/b.ts');
+ });
+
+ it('allows uncommitted files excluded by deterministic analysis rules', () => {
+ const { root, baseCommit } = setupRepository({
+ 'src/a.ts': 'export const a = 1;\n',
+ 'src/b.ts': 'export const b = 2;\n',
+ 'src/c.ts': 'export const c = 3;\n',
+ 'src/d.ts': 'export const d = 4;\n',
+ });
+ writeProjectFile(root, 'src/a.ts', 'export const renamed = 1;\n');
+ commit(root, 'committed structural change');
+ writeProjectFile(root, 'dist/untracked.js', 'generated();\n');
+
+ const { plan } = prepare(root, baseCommit);
+ expect(plan.filesToReanalyze).toEqual(['src/a.ts']);
+ });
});
describe('finalize-incremental.mjs', { timeout: 30_000 }, () => {
diff --git a/understand-anything-plugin/skills/understand/prepare-incremental.mjs b/understand-anything-plugin/skills/understand/prepare-incremental.mjs
index 61ee9aa8..1692f292 100644
--- a/understand-anything-plugin/skills/understand/prepare-incremental.mjs
+++ b/understand-anything-plugin/skills/understand/prepare-incremental.mjs
@@ -53,6 +53,7 @@ const {
buildFingerprintStore,
compareFingerprints,
classifyUpdate,
+ createIgnoreFilter,
resolveUaDir,
} = core;
@@ -173,6 +174,42 @@ function pathsFromChanges(changes) {
return sorted(paths);
}
+function parseNulPaths(output) {
+ return output
+ .split('\0')
+ .map(normalizeRelativePath)
+ .filter(Boolean);
+}
+
+function relevantWorktreeChanges(projectRoot, excludePatterns) {
+ const paths = sorted([
+ ...parseNulPaths(run(
+ 'git',
+ ['diff', '--cached', '--name-only', '-z', '--relative', '--', '.'],
+ { cwd: projectRoot },
+ )),
+ ...parseNulPaths(run(
+ 'git',
+ ['diff', '--name-only', '-z', '--relative', '--', '.'],
+ { cwd: projectRoot },
+ )),
+ ...parseNulPaths(run(
+ 'git',
+ ['ls-files', '--others', '--exclude-standard', '-z', '--', '.'],
+ { cwd: projectRoot },
+ )),
+ ]);
+ const ignoreFilter = createIgnoreFilter(projectRoot, excludePatterns);
+ return paths.filter(path => {
+ if (isGeneratedArtifact(path)) return false;
+ const name = path.split('/').at(-1);
+ // These control which files the live scanner can see, so a dirty version
+ // is relevant even though the control file itself is not analyzed.
+ if (name === '.gitignore' || name === '.understandignore') return true;
+ return !ignoreFilter.isIgnored(path);
+ });
+}
+
function normalizeFingerprintStore(raw, baseCommit) {
if (raw && raw.files && typeof raw.files === 'object' && !Array.isArray(raw.files)) {
return raw;
@@ -398,6 +435,15 @@ async function main() {
const baseCommit = resolveCommit(projectRoot, args.baseCommit);
const headCommit = resolveCommit(projectRoot, 'HEAD');
+ const dirtyPaths = relevantWorktreeChanges(projectRoot, args.excludePatterns);
+ if (dirtyPaths.length > 0) {
+ const preview = dirtyPaths.slice(0, 10).join(', ');
+ const suffix = dirtyPaths.length > 10 ? ` (+${dirtyPaths.length - 10} more)` : '';
+ throw new Error(
+ `Working tree has relevant uncommitted changes: ${preview}${suffix}. ` +
+ `Commit or stash them before incremental analysis so the HEAD baseline remains reproducible.`,
+ );
+ }
const changes = parseNameStatusZ(
run(
'git',
@@ -599,6 +645,7 @@ export {
isGeneratedArtifact,
isImportResolverConfig,
normalizeRelativePath,
+ relevantWorktreeChanges,
parseNameStatusZ,
pruneExistingGraph,
};