db-reopen-on-replace.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * Deleted-but-open DB inode self-heal (issue #925).
  3. *
  4. * A long-lived process (the MCP daemon) opens `.codegraph/codegraph.db` and
  5. * holds the file descriptor for its whole life. If `.codegraph/` is removed and
  6. * recreated AT THE SAME PATH while it's running — `git worktree remove <p>` then
  7. * `git worktree add <p>` + `codegraph init`, or `rm -rf .codegraph` + re-init —
  8. * the held fd points at the now-unlinked inode and can never see the new index.
  9. * Queries then return the pre-removal snapshot until the process restarts; the
  10. * CLI (a fresh process) reads the new inode and diverges.
  11. *
  12. * The deleted-but-open-inode hazard is POSIX file semantics (an open file can't
  13. * be unlinked on Windows, and st_ino is unreliable there), so the recreate
  14. * repros are gated to non-Windows; `isReplacedOnDisk` is verified to stay false
  15. * on Windows.
  16. */
  17. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  18. import * as fs from 'fs';
  19. import * as os from 'os';
  20. import * as path from 'path';
  21. import { DatabaseConnection } from '../src/db';
  22. import { getCodeGraphDir } from '../src/directory';
  23. import CodeGraph from '../src/index';
  24. const posixOnly = it.runIf(process.platform !== 'win32');
  25. const windowsOnly = it.runIf(process.platform === 'win32');
  26. describe('DatabaseConnection.isReplacedOnDisk (issue #925)', () => {
  27. let dir: string;
  28. let dbPath: string;
  29. let conn: DatabaseConnection;
  30. beforeEach(() => {
  31. dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-925-db-'));
  32. dbPath = path.join(dir, 'codegraph.db');
  33. conn = DatabaseConnection.initialize(dbPath);
  34. });
  35. afterEach(() => {
  36. try { conn.close(); } catch { /* may already be closed */ }
  37. fs.rmSync(dir, { recursive: true, force: true });
  38. });
  39. it('is false for the file it opened (any platform)', () => {
  40. expect(conn.isReplacedOnDisk()).toBe(false);
  41. });
  42. posixOnly('becomes true once a DIFFERENT inode lives at the same path', () => {
  43. // Unlink the file we hold open, then create a fresh file at the same path —
  44. // a new inode. The held connection should now report itself replaced.
  45. fs.rmSync(dbPath);
  46. fs.writeFileSync(dbPath, 'not really a db, but a different inode');
  47. expect(conn.isReplacedOnDisk()).toBe(true);
  48. });
  49. posixOnly('is false while the file is momentarily absent (mid-recreate)', () => {
  50. // Nothing to reopen onto yet — don't claim "replaced" until a new file lands.
  51. fs.rmSync(dbPath);
  52. expect(conn.isReplacedOnDisk()).toBe(false);
  53. });
  54. windowsOnly('never fires on Windows (no usable inode / open files cannot be unlinked)', () => {
  55. expect(conn.isReplacedOnDisk()).toBe(false);
  56. });
  57. });
  58. describe('CodeGraph.reopenIfReplaced (issue #925)', () => {
  59. let root: string;
  60. beforeEach(() => {
  61. root = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-925-cg-'));
  62. fs.mkdirSync(path.join(root, 'src'));
  63. fs.writeFileSync(path.join(root, 'src', 'a.ts'), 'export function fooOld() { return 1; }\n');
  64. });
  65. afterEach(() => {
  66. fs.rmSync(root, { recursive: true, force: true });
  67. });
  68. posixOnly('heals a held connection after the index is removed and recreated at the same path', async () => {
  69. // The "server" opens and holds the DB for its lifetime.
  70. const server = CodeGraph.initSync(root);
  71. await server.indexAll();
  72. expect(server.searchNodes('fooOld').length).toBeGreaterThan(0);
  73. expect(server.searchNodes('fooNew').length).toBe(0);
  74. // Simulate `git worktree remove` + re-add (or rm -rf .codegraph + init):
  75. // a NEW index inode at the same path, carrying a renamed symbol, written by
  76. // a separate instance (mirrors a fresh `codegraph init` process).
  77. fs.rmSync(getCodeGraphDir(root), { recursive: true, force: true });
  78. fs.writeFileSync(path.join(root, 'src', 'a.ts'), 'export function fooNew() { return 2; }\n');
  79. const fresh = CodeGraph.initSync(root);
  80. await fresh.indexAll();
  81. fresh.destroy();
  82. // Pre-heal: the held fd still serves the pre-removal snapshot.
  83. expect(server.searchNodes('fooNew').length).toBe(0);
  84. expect(server.searchNodes('fooOld').length).toBeGreaterThan(0);
  85. // Heal in place — the SAME instance now reads the live inode.
  86. expect(server.reopenIfReplaced()).toBe(true);
  87. expect(server.searchNodes('fooNew').length).toBeGreaterThan(0);
  88. expect(server.searchNodes('fooOld').length).toBe(0);
  89. // Idempotent: nothing changed since, so a second call is a no-op.
  90. expect(server.reopenIfReplaced()).toBe(false);
  91. server.destroy();
  92. });
  93. posixOnly('is a no-op (returns false) when the index has not been replaced', async () => {
  94. const server = CodeGraph.initSync(root);
  95. await server.indexAll();
  96. expect(server.reopenIfReplaced()).toBe(false);
  97. server.destroy();
  98. });
  99. });