mcp-unindexed.test.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * Unindexed-workspace session policy tests.
  3. *
  4. * An MCP session attached to a workspace with no .codegraph/ must go quiet
  5. * rather than fail loudly: `initialize` returns the short "inactive"
  6. * instructions variant (not the full playbook), `tools/list` returns an
  7. * EMPTY list, and a tool call that still arrives (cross-project
  8. * `projectPath`, or a host that skips tools/list) answers with a
  9. * SUCCESS-shaped guidance message — never `isError: true`. One or two early
  10. * isError responses teach an agent to abandon codegraph for the whole
  11. * session; that observed failure mode is what this suite guards.
  12. */
  13. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  14. import { spawn, ChildProcessWithoutNullStreams } from 'child_process';
  15. import * as fs from 'fs';
  16. import * as path from 'path';
  17. import * as os from 'os';
  18. import { CodeGraph } from '../src';
  19. import { ToolHandler } from '../src/mcp/tools';
  20. const BIN = path.resolve(__dirname, '../dist/bin/codegraph.js');
  21. function spawnServer(cwd: string): ChildProcessWithoutNullStreams {
  22. return spawn(process.execPath, [BIN, 'serve', '--mcp'], {
  23. cwd,
  24. stdio: ['pipe', 'pipe', 'pipe'],
  25. // Direct (in-process) mode — the unindexed path never has a daemon
  26. // anyway (the daemon socket lives in .codegraph/), and this keeps the
  27. // suite from leaking a detached daemon in the indexed test.
  28. // CODEGRAPH_WASM_RELAUNCHED skips the --liftoff-only re-exec: without
  29. // it the server runs as a GRANDCHILD that survives child.kill() on
  30. // Windows and holds the temp cwd/SQLite handles, failing teardown with
  31. // EPERM no matter how long rmSync retries (the class documented for
  32. // the mcp-initialize/mcp-roots suites).
  33. env: { ...process.env, CODEGRAPH_NO_DAEMON: '1', CODEGRAPH_WASM_RELAUNCHED: '1' },
  34. }) as ChildProcessWithoutNullStreams;
  35. }
  36. /** Send a JSON-RPC request and resolve with the response matching its id. */
  37. function request(
  38. child: ChildProcessWithoutNullStreams,
  39. msg: { id: number; method: string; params?: unknown },
  40. timeoutMs = 15000
  41. ): Promise<Record<string, unknown>> {
  42. return new Promise((resolve, reject) => {
  43. let buf = '';
  44. const timer = setTimeout(() => {
  45. child.stdout.off('data', onData);
  46. reject(new Error(`timeout waiting for response id=${msg.id}`));
  47. }, timeoutMs);
  48. const onData = (chunk: Buffer) => {
  49. buf += chunk.toString();
  50. let idx: number;
  51. while ((idx = buf.indexOf('\n')) !== -1) {
  52. const line = buf.slice(0, idx).trim();
  53. buf = buf.slice(idx + 1);
  54. if (!line) continue;
  55. try {
  56. const parsed = JSON.parse(line) as Record<string, unknown>;
  57. if (parsed.id === msg.id) {
  58. clearTimeout(timer);
  59. child.stdout.off('data', onData);
  60. resolve(parsed);
  61. return;
  62. }
  63. } catch {
  64. // non-JSON noise on stdout — ignore
  65. }
  66. }
  67. };
  68. child.stdout.on('data', onData);
  69. child.stdin.write(JSON.stringify({ jsonrpc: '2.0', ...msg }) + '\n');
  70. });
  71. }
  72. function initializeParams(projectPath: string) {
  73. return {
  74. protocolVersion: '2025-11-25',
  75. capabilities: {},
  76. clientInfo: { name: 'test', version: '0.0.0' },
  77. rootUri: `file://${projectPath}`,
  78. };
  79. }
  80. describe('Unindexed-workspace session policy', () => {
  81. let tempDir: string;
  82. let child: ChildProcessWithoutNullStreams | null = null;
  83. beforeEach(() => {
  84. tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-unindexed-'));
  85. });
  86. afterEach(async () => {
  87. if (child) {
  88. // Wait for the child to actually exit before removing its cwd — on
  89. // Windows a just-killed process briefly holds the directory/SQLite
  90. // handles, and an immediate rmSync fails the teardown with EPERM
  91. // (the documented file-locking class that fails the sibling
  92. // mcp-initialize/mcp-roots suites). kill + await exit + retried
  93. // removal keeps this suite green on Windows.
  94. const exited = new Promise<void>((resolve) => child!.once('exit', () => resolve()));
  95. child.kill('SIGKILL');
  96. await Promise.race([exited, new Promise((r) => setTimeout(r, 3000))]);
  97. child = null;
  98. }
  99. fs.rmSync(tempDir, { recursive: true, force: true, maxRetries: 10, retryDelay: 200 });
  100. });
  101. it('initialize returns the short "inactive" instructions, not the playbook', async () => {
  102. fs.writeFileSync(path.join(tempDir, 'index.ts'), 'export const x = 1;\n');
  103. child = spawnServer(tempDir);
  104. const res = await request(child, { id: 0, method: 'initialize', params: initializeParams(tempDir) });
  105. const instructions = (res.result as { instructions: string }).instructions;
  106. expect(instructions).toMatch(/inactive/i);
  107. expect(instructions).toMatch(/codegraph init/);
  108. // The full playbook must NOT be sent into a session where every call fails
  109. expect(instructions).not.toMatch(/How to query/);
  110. expect(instructions).not.toMatch(/codegraph_explore/);
  111. });
  112. it('tools/list returns an EMPTY list when the workspace has no index', async () => {
  113. child = spawnServer(tempDir);
  114. await request(child, { id: 0, method: 'initialize', params: initializeParams(tempDir) });
  115. const res = await request(child, { id: 1, method: 'tools/list' });
  116. expect((res.result as { tools: unknown[] }).tools).toEqual([]);
  117. });
  118. it('an INDEXED workspace still gets the full playbook and the explore tool', async () => {
  119. fs.writeFileSync(path.join(tempDir, 'index.ts'), 'export function hello(): string { return "hi"; }\n');
  120. const cg = await CodeGraph.init(tempDir, { index: true });
  121. cg.close();
  122. child = spawnServer(tempDir);
  123. const init = await request(child, { id: 0, method: 'initialize', params: initializeParams(tempDir) });
  124. const instructions = (init.result as { instructions: string }).instructions;
  125. expect(instructions).toMatch(/How to query/);
  126. expect(instructions).not.toMatch(/inactive/i);
  127. const list = await request(child, { id: 1, method: 'tools/list' });
  128. const tools = (list.result as { tools: Array<{ name: string }> }).tools;
  129. // The default surface is pared to explore alone (see DEFAULT_MCP_TOOLS) — the
  130. // contract under test is "indexed → tools are PRESENT", in contrast to the
  131. // unindexed empty list above.
  132. expect(tools.length).toBeGreaterThanOrEqual(1);
  133. expect(tools.map((t) => t.name)).toContain('codegraph_explore');
  134. });
  135. });
  136. describe('No-error policy on expected conditions', () => {
  137. let tempDir: string;
  138. beforeEach(() => {
  139. tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-noerror-'));
  140. });
  141. afterEach(() => {
  142. fs.rmSync(tempDir, { recursive: true, force: true });
  143. });
  144. it('cross-project query to an unindexed path is SUCCESS-shaped guidance, not isError', async () => {
  145. const res = await new ToolHandler(null).execute('codegraph_search', {
  146. query: 'anything',
  147. projectPath: tempDir,
  148. });
  149. expect(res.isError).toBeUndefined();
  150. expect(res.content[0]!.text).toMatch(/isn't indexed/);
  151. expect(res.content[0]!.text).toMatch(/codegraph init/);
  152. expect(res.content[0]!.text).toMatch(/built-in tools/);
  153. });
  154. it('no-default-project (working-directory detection miss) is SUCCESS-shaped guidance', async () => {
  155. const res = await new ToolHandler(null).execute('codegraph_search', { query: 'anything' });
  156. expect(res.isError).toBeUndefined();
  157. expect(res.content[0]!.text).toMatch(/No CodeGraph project is loaded/);
  158. expect(res.content[0]!.text).toMatch(/projectPath/);
  159. });
  160. it.runIf(process.platform !== 'win32')(
  161. 'sensitive-path refusal stays a hard error (no retry encouragement)',
  162. async () => {
  163. const res = await new ToolHandler(null).execute('codegraph_search', {
  164. query: 'anything',
  165. projectPath: '/etc',
  166. });
  167. expect(res.isError).toBe(true);
  168. expect(res.content[0]!.text).not.toMatch(/retry the call once/);
  169. }
  170. );
  171. });
  172. describe('search kind filter', () => {
  173. let tempDir: string;
  174. let cg: CodeGraph;
  175. beforeEach(async () => {
  176. tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-kind-'));
  177. fs.writeFileSync(
  178. path.join(tempDir, 'types.ts'),
  179. 'export type PaymentMethod = { id: string };\nexport function pay(): void {}\n'
  180. );
  181. cg = await CodeGraph.init(tempDir, { index: true });
  182. });
  183. afterEach(() => {
  184. cg.close();
  185. fs.rmSync(tempDir, { recursive: true, force: true });
  186. });
  187. it("kind: 'type' (the advertised enum value) finds type aliases", async () => {
  188. const res = await new ToolHandler(cg).execute('codegraph_search', {
  189. query: 'PaymentMethod',
  190. kind: 'type',
  191. });
  192. expect(res.isError).toBeUndefined();
  193. expect(res.content[0]!.text).toMatch(/PaymentMethod/);
  194. expect(res.content[0]!.text).not.toMatch(/No results found/);
  195. });
  196. });