Files
codegraph/__tests__/index-command.test.ts
T
Colby MchenryandClaude Opus 5.5 3f58e34383 fix(cli): index no longer blocks on a daemon lock naming a process that isn't CodeGraph (#2819)
codegraph index told you to run `codegraph daemon stop`, which the CLI
rejects ("too many arguments"), whenever it could not verify the process
named in .codegraph/daemon.pid. In that state `codegraph daemon` lists
nothing, because it lists only daemons that answer their socket. And
`codegraph unlock` kept an old-format lock whose process was alive (the
rule from issue #1834), so nothing the CLI offered got past it. The
writer-lock message gave the same wrong advice.

Root cause: a live pid in the lock was the only evidence, and a pid
proves nothing once the OS gives it to another program. Every CodeGraph
daemon since the first one (v0.9.5) runs
`<runtime> ... codegraph.js serve --mcp --path <root>`, so the holder's
command line settles the question.

Fix:
- New src/mcp/process-identity.ts reads a process's command line and
  executable name from the OS: Win32_Process through PowerShell (tasklist
  as fallback) on Windows, /proc on Linux, ps elsewhere. Each lookup is
  capped at 5 s and never throws. A holder is a CodeGraph MCP server,
  provably something else, or unknown, and unknown is never evidence.
- stopDaemonAt treats an unverified live holder that is provably not a
  CodeGraph server like a dead one. Its lock is cleared, under the same
  writer-slot claim, and the process is never signalled.
  clearStaleDaemonArtifacts (codegraph unlock) does the same for an
  old-format lock. A holder that may be a daemon keeps its lock, as
  before (issues #1553 and #1834).
- daemonStopFailureMessage builds index's error from what remains: the
  pid, what it runs, and a command that clears it (taskkill /PID N /F or
  kill N), or which lock file to delete when only that will do.
- writerLockHeldMessage names `codegraph daemon` instead of the missing
  `codegraph daemon stop`.

Validation (Windows 11):
- New daemon-lock-holder.test.ts (11 tests) and a daemon-registry test.
  With main's daemon-registry.ts, writer-lock.ts and codegraph.ts put
  back, 11 tests fail, including all 8 CLI cases and the writer-lock
  wording; with the fix all pass. Four existing tests used generic
  processes to stand in for a daemon that doesn't answer. Those stand-ins
  now carry a CodeGraph server's command line, and pass on main and here.
- Real CLI with a private HOME, 4 cases: an old-format lock naming a live
  PING.EXE; an old-format lock naming a live Node process that isn't
  CodeGraph; a current-format lock naming that kind of Node process with
  a pipe nobody listens on; an old-format lock naming a Node process whose
  command line reads `codegraph.js serve --mcp`. On main all 4 index runs
  exit 1 with the old advice, `daemon stop` is rejected, `daemon` lists
  nothing, and unlock clears only the third. With the fix the first 3
  rebuild (exit 0), each holder stays alive, and unlock clears the first
  too. The 4th exits 1, naming the pid and `taskkill /PID <pid> /F`;
  after that command, index exits 0.

Contributor PR #2373 (@danusha2345) changes the two strings only; I read
it for guidance.

Fixes #2770

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 23:32:31 +00:00

259 lines
9.8 KiB
TypeScript

/**
* Regression coverage for issue #874: `codegraph index` produced 0 nodes / 0
* edges while `codegraph init` worked, and appeared to wipe the graph.
*
* Root cause: `index` ran a full extraction against the already-populated DB
* without clearing it first. Every file's content hash still matched, so the
* orchestrator skipped re-inserting all of them, and the run reported its delta
* (after - before = 0) as "0 nodes, 0 edges". The fix makes `index` a true full
* rebuild — clear, then re-index — so it produces the same complete result as a
* fresh `init`.
*
* Exercised end-to-end against the built binary so the CLI wiring (not just the
* library) is covered.
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { execFileSync } from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import { CodeGraph } from '../src';
import { DatabaseConnection } from '../src/db';
import { encodeLockInfo, getDaemonPidPath } from '../src/mcp/daemon-paths';
const BIN = path.resolve(__dirname, '../dist/bin/codegraph.js');
/** Normalize a PRAGMA read across return shapes (array | object | scalar). */
function pragmaValue(raw: unknown, key: string): unknown {
const row = Array.isArray(raw) ? raw[0] : raw;
if (row !== null && typeof row === 'object') return (row as Record<string, unknown>)[key];
return row;
}
function runCodegraph(args: string[], cwd: string): string {
return execFileSync(process.execPath, [BIN, ...args], {
cwd,
encoding: 'utf-8',
env: { ...process.env, CODEGRAPH_NO_DAEMON: '1' },
stdio: ['ignore', 'pipe', 'pipe'],
});
}
function graphCounts(dir: string): { nodes: number; edges: number } {
const cg = CodeGraph.openSync(dir);
try {
const stats = cg.getStats();
return { nodes: stats.nodeCount, edges: stats.edgeCount };
} finally {
cg.close();
}
}
function isAlive(pid: number): boolean {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
}
/**
* A live process standing in for a daemon that does not answer its socket.
* Its command line reads like a CodeGraph MCP server's: one that is plainly
* some other program would make the lock stale, and `index` would clear it
* (#2770).
*/
function startDetachedProcess(): number {
const source = [
"const { spawn } = require('child_process');",
"const child = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)', 'codegraph.js', 'serve', '--mcp'], { detached: true, stdio: 'ignore' });",
'child.unref();',
'console.log(child.pid);',
].join(' ');
const pid = Number(execFileSync(process.execPath, ['-e', source], { encoding: 'utf8' }).trim());
if (!Number.isInteger(pid) || pid <= 0) throw new Error('could not start daemon fixture');
return pid;
}
describe('codegraph index — full re-index keeps the graph populated (#874)', () => {
let tempDir: string;
beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-index-cmd-'));
// A couple of files with a call edge so there is a non-trivial graph to
// (fail to) reproduce.
fs.writeFileSync(
path.join(tempDir, 'a.ts'),
`export function greet(name: string) { return hello(name); }\n` +
`export function hello(n: string) { return 'hi ' + n; }\n`,
);
fs.writeFileSync(
path.join(tempDir, 'b.ts'),
`import { greet } from './a';\nexport function main() { return greet('world'); }\n`,
);
});
afterEach(() => {
fs.rmSync(tempDir, { recursive: true, force: true });
});
it('reproduces init\'s node/edge counts instead of emptying the index', () => {
runCodegraph(['init'], tempDir);
const afterInit = graphCounts(tempDir);
expect(afterInit.nodes).toBeGreaterThan(0);
expect(afterInit.edges).toBeGreaterThan(0);
const out = runCodegraph(['index'], tempDir);
const afterIndex = graphCounts(tempDir);
// The graph is still fully populated — `index` rebuilt it, it did not wipe it.
expect(afterIndex.nodes).toBe(afterInit.nodes);
expect(afterIndex.edges).toBe(afterInit.edges);
// ...and the CLI reported the real counts, never the misleading "0 nodes".
expect(out).not.toMatch(/\b0 nodes, 0 edges\b/);
expect(out).toMatch(new RegExp(`\\b${afterInit.nodes} nodes\\b`));
});
it('is idempotent: a second index does not grow the graph', () => {
runCodegraph(['init'], tempDir);
runCodegraph(['index'], tempDir);
const first = graphCounts(tempDir);
runCodegraph(['index'], tempDir);
const second = graphCounts(tempDir);
// A clean rebuild each time — no duplicate (re-resolved) edges accumulating
// across runs (the C# "+18 edges" symptom in the report).
expect(second.nodes).toBe(first.nodes);
expect(second.edges).toBe(first.edges);
});
it('--quiet path also rebuilds a populated graph', () => {
runCodegraph(['init'], tempDir);
const afterInit = graphCounts(tempDir);
runCodegraph(['index', '--quiet'], tempDir);
const afterIndex = graphCounts(tempDir);
expect(afterIndex.nodes).toBe(afterInit.nodes);
expect(afterIndex.edges).toBe(afterInit.edges);
});
it('refuses to recreate while a live daemon cannot be verified (#1325)', () => {
runCodegraph(['init'], tempDir);
const daemonPid = startDetachedProcess();
const lockPath = getDaemonPidPath(fs.realpathSync(tempDir));
fs.writeFileSync(
lockPath,
encodeLockInfo({
pid: daemonPid,
version: 'test',
socketPath: path.join(tempDir, '.codegraph', 'missing.sock'),
startedAt: Date.now(),
}),
);
try {
expect(() => runCodegraph(['index', '--quiet'], tempDir)).toThrow(/did not answer as its daemon, so it was left running/);
expect(isAlive(daemonPid)).toBe(true);
expect(fs.existsSync(lockPath)).toBe(true);
} finally {
if (isAlive(daemonPid)) process.kill(daemonPid, 'SIGKILL');
}
});
});
/**
* Regression coverage for issue #1067: a full re-index must RECOVER an existing
* oversized/stale index from earlier versions, not wedge on it.
*
* Root cause: `index` opened the old database and DELETE-d every row to clear
* it. With FTS triggers firing per deleted node, a pre-fix poisoned graph (an
* ignored gitlink corpus scanned into ~1.6M nodes + a multi-GB WAL, #1065) took
* well over the 60s liveness-watchdog window to clear, so the process was
* SIGKILLed before scanning even began and the bad state could never be rebuilt
* away. The fix discards (unlinks) the database files and re-initializes a fresh
* one — O(1) regardless of size — so `index` recovers any prior state.
*/
describe('codegraph index — recovers a stale/oversized prior index (#1067)', () => {
let tempDir: string;
const dbPath = (dir: string) => path.join(dir, '.codegraph', 'codegraph.db');
beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-index-recover-'));
fs.writeFileSync(
path.join(tempDir, 'a.ts'),
`export function greet(name: string) { return hello(name); }\n` +
`export function hello(n: string) { return 'hi ' + n; }\n`,
);
});
afterEach(() => {
fs.rmSync(tempDir, { recursive: true, force: true });
});
it('rebuilds to the current disk state, discarding content for files that no longer exist', () => {
// Stand in for the "old graph indexed an ignored corpus" shape: index a tree
// that also has a junk/ directory, then delete junk/ from disk so the DB now
// carries stale nodes for paths that should no longer be indexed.
const junkDir = path.join(tempDir, 'junk');
fs.mkdirSync(junkDir);
for (let i = 0; i < 12; i++) {
fs.writeFileSync(path.join(junkDir, `j${i}.ts`), `export function j${i}() { return ${i}; }\n`);
}
runCodegraph(['init'], tempDir);
const withJunk = graphCounts(tempDir);
// Remove the corpus from disk. The DB still holds its nodes — the stale,
// oversized prior state #1067 is about.
fs.rmSync(junkDir, { recursive: true, force: true });
runCodegraph(['index'], tempDir);
const recovered = graphCounts(tempDir);
// The rebuild reflects only what's on disk now — the junk nodes are gone…
expect(recovered.nodes).toBeLessThan(withJunk.nodes);
// …and the result is identical to a fresh init of the same (now-smaller) tree.
const fresh = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-index-fresh-'));
try {
fs.copyFileSync(path.join(tempDir, 'a.ts'), path.join(fresh, 'a.ts'));
runCodegraph(['init'], fresh);
const freshCounts = graphCounts(fresh);
expect(recovered.nodes).toBe(freshCounts.nodes);
expect(recovered.edges).toBe(freshCounts.edges);
} finally {
fs.rmSync(fresh, { recursive: true, force: true });
}
});
// The fix rebuilds a fresh DB rather than DELETE-ing rows in place. Prove it
// with a header sentinel: PRAGMA user_version survives an in-place clear but
// not a from-scratch recreate. (An inode check is unreliable — ext4/overlayfs
// recycle the inode number after unlink+recreate.)
it('rebuilds a fresh database rather than clearing the old one in place', () => {
runCodegraph(['init'], tempDir);
const stamp = DatabaseConnection.open(dbPath(tempDir));
stamp.getDb().pragma('user_version = 4242');
stamp.close();
runCodegraph(['index'], tempDir);
const check = DatabaseConnection.open(dbPath(tempDir));
const userVersion = pragmaValue(check.getDb().pragma('user_version'), 'user_version');
check.close();
// Sentinel gone → `index` discarded the old DB and rebuilt it, the path that
// avoids the per-row FTS delete wedge on a poisoned graph (#1067).
expect(Number(userVersion)).not.toBe(4242);
// …and the graph is intact afterwards.
const counts = graphCounts(tempDir);
expect(counts.nodes).toBeGreaterThan(0);
expect(counts.edges).toBeGreaterThan(0);
});
});