worktree-detection.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /**
  2. * Git worktree index-mismatch detection (issue #155).
  3. *
  4. * A CodeGraph index is resolved by walking up to the nearest `.codegraph/`.
  5. * When a worktree is nested inside the main checkout, that walk reaches the
  6. * MAIN checkout's index and a query silently returns the main branch's code
  7. * instead of the worktree's. `detectWorktreeIndexMismatch` spots exactly this
  8. * case so callers can warn.
  9. *
  10. * These tests drive real `git` against real temp worktrees — no mocking — so
  11. * they exercise the same `git rev-parse --show-toplevel` behavior production
  12. * relies on.
  13. */
  14. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  15. import { execFileSync } from 'child_process';
  16. import * as fs from 'fs';
  17. import * as os from 'os';
  18. import * as path from 'path';
  19. import {
  20. detectWorktreeIndexMismatch,
  21. worktreeMismatchWarning,
  22. gitWorktreeRoot,
  23. gitCommonDir,
  24. } from '../src/sync/worktree';
  25. import CodeGraph from '../src/index';
  26. import { ToolHandler } from '../src/mcp/tools';
  27. function git(cwd: string, ...args: string[]): void {
  28. execFileSync('git', args, { cwd, stdio: ['ignore', 'ignore', 'ignore'] });
  29. }
  30. /** realpath so macOS /var → /private/var symlinking doesn't break equality. */
  31. function real(p: string): string {
  32. return fs.realpathSync(path.resolve(p));
  33. }
  34. describe('detectWorktreeIndexMismatch (issue #155)', () => {
  35. let mainRepo: string; // main checkout — owns the .codegraph index
  36. let worktree: string; // a linked worktree nested inside the main checkout
  37. let nonGit: string; // a directory outside any git repo
  38. beforeEach(() => {
  39. mainRepo = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-wt-main-'));
  40. nonGit = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-wt-plain-'));
  41. git(mainRepo, 'init', '-q');
  42. git(mainRepo, 'config', 'user.email', 'test@example.com');
  43. git(mainRepo, 'config', 'user.name', 'Test');
  44. git(mainRepo, 'config', 'commit.gpgsign', 'false');
  45. fs.writeFileSync(path.join(mainRepo, 'README.md'), '# main\n');
  46. git(mainRepo, 'add', '.');
  47. git(mainRepo, 'commit', '-q', '-m', 'init');
  48. // Nest the worktree under the main checkout, mirroring tools that place
  49. // worktrees in (gitignored) subpaths like `.claude/worktrees/<name>/`.
  50. worktree = path.join(mainRepo, 'wt');
  51. git(mainRepo, 'worktree', 'add', '-q', '-b', 'feature', worktree);
  52. });
  53. afterEach(() => {
  54. try { git(mainRepo, 'worktree', 'remove', '--force', worktree); } catch { /* best effort */ }
  55. fs.rmSync(mainRepo, { recursive: true, force: true });
  56. fs.rmSync(nonGit, { recursive: true, force: true });
  57. });
  58. it('flags a worktree borrowing the main checkout index', () => {
  59. const m = detectWorktreeIndexMismatch(worktree, mainRepo);
  60. expect(m).not.toBeNull();
  61. expect(m!.worktreeRoot).toBe(real(worktree));
  62. expect(m!.indexRoot).toBe(real(mainRepo));
  63. });
  64. it('returns null when the index lives in the same working tree', () => {
  65. expect(detectWorktreeIndexMismatch(mainRepo, mainRepo)).toBeNull();
  66. expect(detectWorktreeIndexMismatch(worktree, worktree)).toBeNull();
  67. });
  68. it('returns null for a subdirectory of the same working tree', () => {
  69. const sub = path.join(mainRepo, 'src');
  70. fs.mkdirSync(sub);
  71. expect(detectWorktreeIndexMismatch(sub, mainRepo)).toBeNull();
  72. });
  73. it('returns null when startPath is not in a git repo', () => {
  74. expect(detectWorktreeIndexMismatch(nonGit, mainRepo)).toBeNull();
  75. });
  76. it('returns null when the index root is a plain (non-worktree) directory', () => {
  77. // startPath is a real worktree, but the index sits in an unrelated non-git
  78. // dir — that's "index in an ancestor", not "borrowed another worktree".
  79. expect(detectWorktreeIndexMismatch(worktree, nonGit)).toBeNull();
  80. });
  81. it('gitWorktreeRoot reports each tree distinctly', () => {
  82. expect(gitWorktreeRoot(worktree)).toBe(real(worktree));
  83. expect(gitWorktreeRoot(mainRepo)).toBe(real(mainRepo));
  84. expect(gitWorktreeRoot(nonGit)).toBeNull();
  85. });
  86. it('warning names both trees and the fix', () => {
  87. const msg = worktreeMismatchWarning(detectWorktreeIndexMismatch(worktree, mainRepo)!);
  88. expect(msg).toContain(real(worktree));
  89. expect(msg).toContain(real(mainRepo));
  90. expect(msg).toContain('codegraph init');
  91. });
  92. });
  93. /**
  94. * The detection above only helps if it reaches the agent. Agents call the read
  95. * tools (search/context/trace/…), almost never status — so the mismatch notice
  96. * has to ride on every read tool's result, not just status. These tests drive
  97. * the real `ToolHandler.execute` chokepoint against a real index whose default
  98. * project resolves UP from a nested worktree to the main checkout.
  99. */
  100. describe('worktree mismatch surfaces on hot read tools (issue #155)', () => {
  101. let mainRepo: string;
  102. let worktree: string;
  103. let cg: CodeGraph;
  104. let handler: ToolHandler;
  105. beforeEach(async () => {
  106. mainRepo = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-wt-tool-'));
  107. git(mainRepo, 'init', '-q');
  108. git(mainRepo, 'config', 'user.email', 'test@example.com');
  109. git(mainRepo, 'config', 'user.name', 'Test');
  110. git(mainRepo, 'config', 'commit.gpgsign', 'false');
  111. fs.mkdirSync(path.join(mainRepo, 'src'));
  112. fs.writeFileSync(path.join(mainRepo, 'src', 'a.ts'), 'export function mainOnly() { return 1; }\n');
  113. git(mainRepo, 'add', '.');
  114. git(mainRepo, 'commit', '-q', '-m', 'init');
  115. // The index lives in the MAIN checkout.
  116. cg = CodeGraph.initSync(mainRepo);
  117. await cg.indexAll();
  118. // Nested worktree, mirroring tools that place them under .claude/worktrees/<name>/.
  119. worktree = path.join(mainRepo, 'wt');
  120. git(mainRepo, 'worktree', 'add', '-q', '-b', 'feature', worktree);
  121. handler = new ToolHandler(cg);
  122. });
  123. afterEach(() => {
  124. try { cg.destroy(); } catch { /* best effort */ }
  125. try { git(mainRepo, 'worktree', 'remove', '--force', worktree); } catch { /* best effort */ }
  126. fs.rmSync(mainRepo, { recursive: true, force: true });
  127. });
  128. it('prefixes a compact notice on codegraph_search run from a nested worktree', async () => {
  129. handler.setDefaultProjectHint(worktree);
  130. const res = await handler.execute('codegraph_search', { query: 'mainOnly' });
  131. const text = res.content[0].text;
  132. expect(res.isError).toBeFalsy();
  133. expect(text).toContain('different git worktree');
  134. expect(text).toContain(real(worktree));
  135. expect(text).toContain('codegraph init');
  136. });
  137. it('does NOT prefix when the default project is the main checkout itself', async () => {
  138. handler.setDefaultProjectHint(mainRepo);
  139. const res = await handler.execute('codegraph_search', { query: 'mainOnly' });
  140. expect(res.content[0].text).not.toContain('different git worktree');
  141. });
  142. it('still shows the verbose warning on codegraph_status', async () => {
  143. handler.setDefaultProjectHint(worktree);
  144. const res = await handler.execute('codegraph_status', {});
  145. const text = res.content[0].text;
  146. expect(text).toContain('different git working tree');
  147. expect(text).toContain(real(worktree));
  148. });
  149. it('caches detection — a later tool call needs no further git spawn', async () => {
  150. handler.setDefaultProjectHint(worktree);
  151. // First call computes + caches the mismatch (this is the only git spawn).
  152. const first = await handler.execute('codegraph_search', { query: 'mainOnly' });
  153. expect(first.content[0].text).toContain('different git worktree');
  154. // Make git unreachable. A fresh detection would now return null (no notice);
  155. // the notice still appearing on a *different* tool proves it came from cache.
  156. const savedPath = process.env.PATH;
  157. process.env.PATH = '';
  158. try {
  159. const second = await handler.execute('codegraph_explore', { query: 'mainOnly' });
  160. expect(second.content[0].text).toContain('different git worktree');
  161. } finally {
  162. process.env.PATH = savedPath;
  163. }
  164. });
  165. });
  166. /**
  167. * A long-lived MCP server (the shared daemon) cached its worktree-mismatch
  168. * verdict keyed only by the start path, and that cache was cleared only on
  169. * shutdown. So once the server decided "this worktree borrows the main
  170. * checkout's index" — true while the worktree had no `.codegraph/` of its own —
  171. * the verdict was pinned for the daemon's whole life. After the worktree got
  172. * its own index (the resolved index root flipped from the main checkout to the
  173. * worktree itself), the CLI saw the worktree's index but the MCP server kept
  174. * emitting the stale false warning until a restart (issue #926).
  175. *
  176. * The verdict depends on BOTH the start path and the resolved index root, so it
  177. * must be cached under both — a changed index root has to invalidate it. This
  178. * drives the real `ToolHandler` worktree-notice path across exactly that change
  179. * (the resolved index root flips when the server's default project is re-opened
  180. * onto the worktree's own index), with no mocking.
  181. */
  182. describe('worktree mismatch verdict re-resolves when the index root changes (issue #926)', () => {
  183. let mainRepo: string;
  184. let worktree: string;
  185. let mainCg: CodeGraph;
  186. let worktreeCg: CodeGraph;
  187. let handler: ToolHandler;
  188. beforeEach(async () => {
  189. mainRepo = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-wt-926-'));
  190. git(mainRepo, 'init', '-q');
  191. git(mainRepo, 'config', 'user.email', 'test@example.com');
  192. git(mainRepo, 'config', 'user.name', 'Test');
  193. git(mainRepo, 'config', 'commit.gpgsign', 'false');
  194. fs.mkdirSync(path.join(mainRepo, 'src'));
  195. fs.writeFileSync(path.join(mainRepo, 'src', 'a.ts'), 'export function mainOnly() { return 1; }\n');
  196. git(mainRepo, 'add', '.');
  197. git(mainRepo, 'commit', '-q', '-m', 'init');
  198. // The long-lived server's default project starts as the MAIN checkout.
  199. mainCg = CodeGraph.initSync(mainRepo);
  200. await mainCg.indexAll();
  201. // Nested worktree that later gains its own index.
  202. worktree = path.join(mainRepo, 'wt');
  203. git(mainRepo, 'worktree', 'add', '-q', '-b', 'feature', worktree);
  204. worktreeCg = CodeGraph.initSync(worktree);
  205. await worktreeCg.indexAll();
  206. handler = new ToolHandler(mainCg);
  207. });
  208. afterEach(() => {
  209. try { mainCg.destroy(); } catch { /* best effort */ }
  210. try { worktreeCg.destroy(); } catch { /* best effort */ }
  211. try { git(mainRepo, 'worktree', 'remove', '--force', worktree); } catch { /* best effort */ }
  212. fs.rmSync(mainRepo, { recursive: true, force: true });
  213. });
  214. it('drops the stale "borrowed the main index" warning once the index root flips to the worktree', async () => {
  215. // The server runs from inside the worktree, default project = main checkout.
  216. handler.setDefaultProjectHint(worktree);
  217. // Phase 1: the index genuinely belongs to a different working tree (the main
  218. // checkout) → warn, and cache that verdict.
  219. const before = await handler.execute('codegraph_status', {});
  220. expect(before.content[0].text).toContain('different git working tree');
  221. expect(before.content[0].text).toContain(real(mainRepo));
  222. // Phase 2: the worktree's own index is now the server's default project
  223. // (engine re-open → setDefaultCodeGraph). The resolved index root for the
  224. // SAME start path flipped to the worktree itself, so the verdict must be
  225. // recomputed to "no mismatch" — not served stale from before.
  226. handler.setDefaultCodeGraph(worktreeCg);
  227. const after = await handler.execute('codegraph_status', {});
  228. expect(after.content[0].text).not.toContain('different git working tree');
  229. });
  230. });
  231. /**
  232. * A nested repo (submodule / embedded clone) whose files the PARENT index
  233. * already covers must NOT be flagged as a borrowed worktree: indexing a
  234. * super-repo descends into its submodules and gitlinked clones, so a query run
  235. * from inside one resolves up to the parent index — which genuinely contains
  236. * that nested repo's symbols. The warning's premise is false there, and its
  237. * "run codegraph init -i" advice would fragment the unified index. (#1031, #1033)
  238. */
  239. describe('detectWorktreeIndexMismatch — nested repos covered by the parent index (#1031, #1033)', () => {
  240. let parent: string; // super-repo that owns the .codegraph index
  241. let subSource: string; // separate repo used as the submodule source
  242. beforeEach(() => {
  243. parent = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-wt-parent-'));
  244. subSource = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-wt-subsrc-'));
  245. for (const r of [parent, subSource]) {
  246. git(r, 'init', '-q');
  247. git(r, 'config', 'user.email', 'test@example.com');
  248. git(r, 'config', 'user.name', 'Test');
  249. git(r, 'config', 'commit.gpgsign', 'false');
  250. }
  251. fs.writeFileSync(path.join(subSource, 'lib.ts'), 'export const x = 1;\n');
  252. git(subSource, 'add', '.');
  253. git(subSource, 'commit', '-q', '-m', 'sub');
  254. fs.writeFileSync(path.join(parent, 'README.md'), '# parent\n');
  255. git(parent, 'add', '.');
  256. git(parent, 'commit', '-q', '-m', 'init');
  257. });
  258. afterEach(() => {
  259. fs.rmSync(parent, { recursive: true, force: true });
  260. fs.rmSync(subSource, { recursive: true, force: true });
  261. });
  262. function addSubmodule(name: string): string {
  263. execFileSync(
  264. 'git',
  265. ['-c', 'protocol.file.allow=always', 'submodule', 'add', '-q', subSource, name],
  266. { cwd: parent, stdio: ['ignore', 'ignore', 'ignore'] },
  267. );
  268. git(parent, 'commit', '-q', '-m', 'add submodule');
  269. return path.join(parent, name);
  270. }
  271. function addBareGitlink(name: string): string {
  272. const dir = path.join(parent, name);
  273. fs.mkdirSync(dir);
  274. git(dir, 'init', '-q');
  275. git(dir, 'config', 'user.email', 'test@example.com');
  276. git(dir, 'config', 'user.name', 'Test');
  277. git(dir, 'config', 'commit.gpgsign', 'false');
  278. fs.writeFileSync(path.join(dir, 'tool.ts'), 'export const y = 2;\n');
  279. git(dir, 'add', '.');
  280. git(dir, 'commit', '-q', '-m', 'tool');
  281. git(parent, 'add', name); // records a 160000 gitlink, no .gitmodules
  282. git(parent, 'commit', '-q', '-m', 'gitlink');
  283. return dir;
  284. }
  285. it('does NOT flag an active submodule covered by the parent index', () => {
  286. const sub = addSubmodule('service-a');
  287. // The submodule IS its own working-tree root (so the old logic flagged it)…
  288. expect(gitWorktreeRoot(sub)).toBe(real(sub));
  289. // …but the parent index covers it, so there must be no warning.
  290. expect(detectWorktreeIndexMismatch(sub, parent)).toBeNull();
  291. expect(detectWorktreeIndexMismatch(path.join(sub, 'src'), parent)).toBeNull();
  292. });
  293. it('does NOT flag a bare gitlink (embedded clone, no .gitmodules) covered by the parent index', () => {
  294. const embedded = addBareGitlink('embedded');
  295. expect(gitWorktreeRoot(embedded)).toBe(real(embedded));
  296. expect(detectWorktreeIndexMismatch(embedded, parent)).toBeNull();
  297. });
  298. it('gitCommonDir differs for a nested repo vs the parent (the discriminator)', () => {
  299. const sub = addSubmodule('service-a');
  300. const subCommon = gitCommonDir(sub);
  301. const parentCommon = gitCommonDir(parent);
  302. expect(subCommon).not.toBeNull();
  303. expect(parentCommon).not.toBeNull();
  304. expect(subCommon).not.toBe(parentCommon); // different repository → suppress
  305. });
  306. it('still flags a genuine linked worktree (same repo, different branch)', () => {
  307. // A real worktree shares the parent's git common dir, so it stays flagged —
  308. // the suppression must not weaken the issue-#155 case.
  309. const wt = path.join(parent, 'wt');
  310. git(parent, 'worktree', 'add', '-q', '-b', 'feature', wt);
  311. try {
  312. expect(gitCommonDir(wt)).toBe(gitCommonDir(parent)); // SAME repository
  313. expect(detectWorktreeIndexMismatch(wt, parent)).not.toBeNull();
  314. } finally {
  315. try { git(parent, 'worktree', 'remove', '--force', wt); } catch { /* best effort */ }
  316. }
  317. });
  318. });