|
| 1 | +/** |
| 2 | + * Reopen a DB connection when its file is replaced under us (same path, new |
| 3 | + * inode). Regression for issue #925. |
| 4 | + * |
| 5 | + * Scenario: the project dir is removed and recreated at the same path (a |
| 6 | + * `git worktree remove` + `git worktree add`, or a fresh `codegraph init`). |
| 7 | + * A long-lived SQLite handle keeps reading the old, now-unlinked inode while |
| 8 | + * `init`/`sync` write to the new inode at the same path — so the MCP server |
| 9 | + * served a stale snapshot for the life of the daemon. Four layers are covered: |
| 10 | + * 1. `DatabaseConnection.isFileReplaced()` — the inode-identity primitive. |
| 11 | + * 2. `CodeGraph.isDbReplaced()` — a live instance reports its DB was swapped. |
| 12 | + * 3. `ToolHandler.liveCachedGraph()` — a cross-project (`projectPath`) cache |
| 13 | + * hit on a replaced DB is evicted + closed, so the caller reopens against |
| 14 | + * the new inode (the existing open path) instead of serving the stale one. |
| 15 | + * 4. `ToolHandler.getCodeGraph()` default path — a swapped default project is |
| 16 | + * reopened via the engine reload hook (the steady-state tool-call path, |
| 17 | + * which the cold init methods never re-enter — issue #925's headline case). |
| 18 | + */ |
| 19 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 20 | +import * as fs from 'fs'; |
| 21 | +import * as path from 'path'; |
| 22 | +import * as os from 'os'; |
| 23 | +import CodeGraph from '../src/index'; |
| 24 | +import { DatabaseConnection } from '../src/db'; |
| 25 | +import { ToolHandler } from '../src/mcp/tools'; |
| 26 | + |
| 27 | +const rmDb = (dbPath: string) => { |
| 28 | + for (const p of [dbPath, `${dbPath}-wal`, `${dbPath}-shm`]) fs.rmSync(p, { force: true }); |
| 29 | +}; |
| 30 | + |
| 31 | +describe('DatabaseConnection.isFileReplaced (issue #925)', () => { |
| 32 | + let dir: string; |
| 33 | + beforeEach(() => { dir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-dbid-')); }); |
| 34 | + afterEach(() => { fs.rmSync(dir, { recursive: true, force: true }); }); |
| 35 | + |
| 36 | + it('false while the same file; true after the file at the path is swapped; false while missing', () => { |
| 37 | + const dbPath = path.join(dir, 'graph.db'); |
| 38 | + const conn = DatabaseConnection.initialize(dbPath); // records inode A |
| 39 | + expect(conn.isFileReplaced()).toBe(false); |
| 40 | + |
| 41 | + // Replace the file at the same path with a brand-new inode (the recreate). |
| 42 | + rmDb(dbPath); |
| 43 | + DatabaseConnection.initialize(dbPath).close(); // creates inode B at the same path |
| 44 | + expect(conn.isFileReplaced()).toBe(true); |
| 45 | + |
| 46 | + // Mid-recreate gap (file absent) must NOT count — don't churn on a transient. |
| 47 | + rmDb(dbPath); |
| 48 | + expect(conn.isFileReplaced()).toBe(false); |
| 49 | + |
| 50 | + conn.close(); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe('Reopen on replaced project DB (issue #925)', () => { |
| 55 | + let dir: string; |
| 56 | + |
| 57 | + const buildProject = async (fnName: string) => { |
| 58 | + fs.mkdirSync(path.join(dir, 'src'), { recursive: true }); |
| 59 | + fs.writeFileSync(path.join(dir, 'src', 'probe.ts'), `export function ${fnName}() { return 1; }\n`); |
| 60 | + const cg = CodeGraph.initSync(dir, { config: { include: ['**/*.ts'], exclude: [] } }); |
| 61 | + await cg.indexAll(); |
| 62 | + cg.close(); |
| 63 | + }; |
| 64 | + const replaceProjectDb = async (fnName: string) => { |
| 65 | + fs.rmSync(path.join(dir, '.codegraph'), { recursive: true, force: true }); |
| 66 | + await buildProject(fnName); |
| 67 | + }; |
| 68 | + |
| 69 | + beforeEach(() => { dir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-db-reopen-')); }); |
| 70 | + afterEach(() => { fs.rmSync(dir, { recursive: true, force: true }); }); |
| 71 | + |
| 72 | + it('CodeGraph.isDbReplaced flips after the DB file is recreated at the same path', async () => { |
| 73 | + await buildProject('probeAlpha'); |
| 74 | + const cg = CodeGraph.openSync(dir); |
| 75 | + try { |
| 76 | + expect(cg.isDbReplaced()).toBe(false); |
| 77 | + await replaceProjectDb('probeBeta'); // new .codegraph/codegraph.db = new inode |
| 78 | + expect(cg.isDbReplaced()).toBe(true); |
| 79 | + } finally { |
| 80 | + cg.close(); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + it('ToolHandler.liveCachedGraph evicts + closes a cached project whose DB file was replaced', async () => { |
| 85 | + await buildProject('probeAlpha'); |
| 86 | + const cg = CodeGraph.openSync(dir); |
| 87 | + const closeSpy = vi.spyOn(cg, 'close'); |
| 88 | + const handler = new ToolHandler(null); |
| 89 | + // Seed the cross-project cache the way getCodeGraph would (private field). |
| 90 | + (handler as unknown as { projectCache: Map<string, CodeGraph> }).projectCache.set(dir, cg); |
| 91 | + const live = (k: string) => (handler as unknown as { liveCachedGraph(k: string): CodeGraph | null }).liveCachedGraph(k); |
| 92 | + const cached = () => (handler as unknown as { projectCache: Map<string, CodeGraph> }).projectCache; |
| 93 | + |
| 94 | + // Fresh → returned as-is, not evicted/closed. |
| 95 | + expect(live(dir)).toBe(cg); |
| 96 | + expect(closeSpy).not.toHaveBeenCalled(); |
| 97 | + |
| 98 | + // Replace the DB file at the same path → next lookup evicts + closes it, |
| 99 | + // returning null so the caller falls through to reopen against the new DB. |
| 100 | + await replaceProjectDb('probeBeta'); |
| 101 | + expect(live(dir)).toBeNull(); |
| 102 | + expect(cached().has(dir)).toBe(false); |
| 103 | + expect(closeSpy).toHaveBeenCalledTimes(1); |
| 104 | + }); |
| 105 | + |
| 106 | + it('getCodeGraph reopens the DEFAULT project via the reload hook when its DB file was replaced', async () => { |
| 107 | + await buildProject('probeAlpha'); |
| 108 | + const stale = CodeGraph.openSync(dir); |
| 109 | + const handler = new ToolHandler(stale); |
| 110 | + // Mirror the engine's reload hook: open the fresh DB and install it as default. |
| 111 | + let hookCalls = 0; |
| 112 | + handler.setDefaultReloadHook(() => { |
| 113 | + hookCalls++; |
| 114 | + handler.setDefaultCodeGraph(CodeGraph.openSync(dir)); |
| 115 | + }); |
| 116 | + const getDefault = () => |
| 117 | + (handler as unknown as { getCodeGraph(p?: string): CodeGraph }).getCodeGraph(); |
| 118 | + |
| 119 | + // Fresh default → served as-is on the steady-state (no-projectPath) path; hook not fired. |
| 120 | + expect(getDefault()).toBe(stale); |
| 121 | + expect(hookCalls).toBe(0); |
| 122 | + |
| 123 | + // Replace the DB at the same path → the default-serving path detects it and |
| 124 | + // fires the hook, which installs a fresh default; the call returns the new one. |
| 125 | + await replaceProjectDb('probeBeta'); |
| 126 | + const reopened = getDefault(); |
| 127 | + expect(hookCalls).toBe(1); |
| 128 | + expect(reopened).not.toBe(stale); |
| 129 | + expect(reopened.isDbReplaced()).toBe(false); // fresh handle on the new inode |
| 130 | + |
| 131 | + stale.close(); |
| 132 | + reopened.close(); |
| 133 | + }); |
| 134 | +}); |
0 commit comments