fix(mcp): don't warn "different git working tree" for submodules covered by the parent index (#1031, #1033) (#1039)

Indexing a super-repo now descends into its submodules and gitlinked clones, so
a query run from inside one resolves up to the parent's unified index — whose
graph DOES contain that nested repo's files. But the git-worktree-mismatch
warning still fired, telling the agent the results were from "a different working
tree" and to run `codegraph init -i` — which would split the submodule back into
its own index and undo the unified view. A false positive carrying harmful advice.

Distinguish a genuine borrowed worktree (the SAME repository on a different
branch — shares a git common dir with the index root) from a submodule/embedded
clone (a DIFFERENT repository — its own common dir), and suppress the warning
only for the latter. Add gitCommonDir() for the check. The issue-#155 linked-worktree
case is unchanged.

Verified end-to-end: the warning no longer fires for a submodule-rooted MCP
session and still fires for a real linked worktree. Edit-sync (manual sync +
the live watcher) keeps the nested repo's files current on both macOS and Linux
(active-submodule and bare-gitlink shapes), so suppressing the warning is safe.

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Colby Mchenry
2026-06-28 19:35:17 -05:00
committed by GitHub
co-authored by Claude Opus 4.8
parent a4dfc3438f
commit f73227d2f5
3 changed files with 137 additions and 0 deletions
+98
View File
@@ -20,6 +20,7 @@ import {
detectWorktreeIndexMismatch,
worktreeMismatchWarning,
gitWorktreeRoot,
gitCommonDir,
} from '../src/sync/worktree';
import CodeGraph from '../src/index';
import { ToolHandler } from '../src/mcp/tools';
@@ -262,3 +263,100 @@ describe('worktree mismatch verdict re-resolves when the index root changes (iss
expect(after.content[0].text).not.toContain('different git working tree');
});
});
/**
* A nested repo (submodule / embedded clone) whose files the PARENT index
* already covers must NOT be flagged as a borrowed worktree: indexing a
* super-repo descends into its submodules and gitlinked clones, so a query run
* from inside one resolves up to the parent index — which genuinely contains
* that nested repo's symbols. The warning's premise is false there, and its
* "run codegraph init -i" advice would fragment the unified index. (#1031, #1033)
*/
describe('detectWorktreeIndexMismatch — nested repos covered by the parent index (#1031, #1033)', () => {
let parent: string; // super-repo that owns the .codegraph index
let subSource: string; // separate repo used as the submodule source
beforeEach(() => {
parent = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-wt-parent-'));
subSource = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-wt-subsrc-'));
for (const r of [parent, subSource]) {
git(r, 'init', '-q');
git(r, 'config', 'user.email', 'test@example.com');
git(r, 'config', 'user.name', 'Test');
git(r, 'config', 'commit.gpgsign', 'false');
}
fs.writeFileSync(path.join(subSource, 'lib.ts'), 'export const x = 1;\n');
git(subSource, 'add', '.');
git(subSource, 'commit', '-q', '-m', 'sub');
fs.writeFileSync(path.join(parent, 'README.md'), '# parent\n');
git(parent, 'add', '.');
git(parent, 'commit', '-q', '-m', 'init');
});
afterEach(() => {
fs.rmSync(parent, { recursive: true, force: true });
fs.rmSync(subSource, { recursive: true, force: true });
});
function addSubmodule(name: string): string {
execFileSync(
'git',
['-c', 'protocol.file.allow=always', 'submodule', 'add', '-q', subSource, name],
{ cwd: parent, stdio: ['ignore', 'ignore', 'ignore'] },
);
git(parent, 'commit', '-q', '-m', 'add submodule');
return path.join(parent, name);
}
function addBareGitlink(name: string): string {
const dir = path.join(parent, name);
fs.mkdirSync(dir);
git(dir, 'init', '-q');
git(dir, 'config', 'user.email', 'test@example.com');
git(dir, 'config', 'user.name', 'Test');
git(dir, 'config', 'commit.gpgsign', 'false');
fs.writeFileSync(path.join(dir, 'tool.ts'), 'export const y = 2;\n');
git(dir, 'add', '.');
git(dir, 'commit', '-q', '-m', 'tool');
git(parent, 'add', name); // records a 160000 gitlink, no .gitmodules
git(parent, 'commit', '-q', '-m', 'gitlink');
return dir;
}
it('does NOT flag an active submodule covered by the parent index', () => {
const sub = addSubmodule('service-a');
// The submodule IS its own working-tree root (so the old logic flagged it)…
expect(gitWorktreeRoot(sub)).toBe(real(sub));
// …but the parent index covers it, so there must be no warning.
expect(detectWorktreeIndexMismatch(sub, parent)).toBeNull();
expect(detectWorktreeIndexMismatch(path.join(sub, 'src'), parent)).toBeNull();
});
it('does NOT flag a bare gitlink (embedded clone, no .gitmodules) covered by the parent index', () => {
const embedded = addBareGitlink('embedded');
expect(gitWorktreeRoot(embedded)).toBe(real(embedded));
expect(detectWorktreeIndexMismatch(embedded, parent)).toBeNull();
});
it('gitCommonDir differs for a nested repo vs the parent (the discriminator)', () => {
const sub = addSubmodule('service-a');
const subCommon = gitCommonDir(sub);
const parentCommon = gitCommonDir(parent);
expect(subCommon).not.toBeNull();
expect(parentCommon).not.toBeNull();
expect(subCommon).not.toBe(parentCommon); // different repository → suppress
});
it('still flags a genuine linked worktree (same repo, different branch)', () => {
// A real worktree shares the parent's git common dir, so it stays flagged —
// the suppression must not weaken the issue-#155 case.
const wt = path.join(parent, 'wt');
git(parent, 'worktree', 'add', '-q', '-b', 'feature', wt);
try {
expect(gitCommonDir(wt)).toBe(gitCommonDir(parent)); // SAME repository
expect(detectWorktreeIndexMismatch(wt, parent)).not.toBeNull();
} finally {
try { git(parent, 'worktree', 'remove', '--force', wt); } catch { /* best effort */ }
}
});
});