1
0

repro-daemon-clients.mjs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env node
  2. // Reproduction harness B — the FAITHFUL opencode scenario.
  3. //
  4. // Spawns N real `codegraph serve --mcp --path <repo>` processes (each becomes a
  5. // proxy that attaches to ONE shared daemon — exactly what opencode does with N
  6. // subagents), drives clean MCP JSON-RPC over each child's stdio, then fires ONE
  7. // concurrent wave of codegraph_explore tools/call across all N and measures
  8. // end-to-end latency + timeouts. This captures transport-flush starvation: a
  9. // daemon event-loop blocked in synchronous explore compute can neither read the
  10. // next request nor flush a finished response.
  11. //
  12. // Usage: node repro-daemon-clients.mjs <repo> <N=10> [perCallTimeoutMs=60000] [warm=1]
  13. import { spawn } from 'node:child_process';
  14. import { performance } from 'node:perf_hooks';
  15. import { resolve } from 'node:path';
  16. const [, , repoRaw, nRaw, timeoutRaw, warmRaw] = process.argv;
  17. const repo = resolve(repoRaw || '.');
  18. const N = Number(nRaw) || 10;
  19. const TIMEOUT_MS = Number(timeoutRaw) || 60000;
  20. const WARM = warmRaw === undefined ? true : warmRaw !== '0';
  21. const CLI = resolve('dist/bin/codegraph.js');
  22. const QUERIES = [
  23. 'how does the text model handle edits and undo',
  24. 'how does the file service watch for changes on disk',
  25. 'how does the keybinding service resolve a chord to a command',
  26. 'how does the extension host activate an extension',
  27. 'how does the editor render decorations in the viewport',
  28. 'how does the search service stream results to the UI',
  29. 'how does the terminal process manager spawn a shell',
  30. 'how does the configuration service merge user and workspace settings',
  31. 'how does the debug adapter forward breakpoints to the runtime',
  32. 'how does the quick input widget filter its items',
  33. 'how does the notification service queue and show toasts',
  34. 'how does the git extension compute the diff for a file',
  35. ];
  36. function makeClient(id) {
  37. const child = spawn('node', [CLI, 'serve', '--mcp', '--path', repo], {
  38. env: { ...process.env, CODEGRAPH_TELEMETRY: '0', DO_NOT_TRACK: '1', CODEGRAPH_MCP_LOG_ATTACH: '0' },
  39. stdio: ['pipe', 'pipe', 'inherit'],
  40. });
  41. let buf = '';
  42. const waiters = new Map(); // id -> resolve
  43. child.stdout.setEncoding('utf8');
  44. child.stdout.on('data', (chunk) => {
  45. buf += chunk;
  46. let idx;
  47. while ((idx = buf.indexOf('\n')) !== -1) {
  48. const line = buf.slice(0, idx).trim();
  49. buf = buf.slice(idx + 1);
  50. if (!line) continue;
  51. let msg; try { msg = JSON.parse(line); } catch { continue; }
  52. if (msg.id !== undefined && waiters.has(msg.id)) {
  53. waiters.get(msg.id)(msg);
  54. waiters.delete(msg.id);
  55. }
  56. }
  57. });
  58. const send = (obj) => child.stdin.write(JSON.stringify(obj) + '\n');
  59. const request = (method, params, rpcId, timeoutMs) =>
  60. new Promise((res) => {
  61. let timer;
  62. if (timeoutMs) timer = setTimeout(() => { waiters.delete(rpcId); res({ __timeout: true }); }, timeoutMs);
  63. waiters.set(rpcId, (m) => { if (timer) clearTimeout(timer); res(m); });
  64. send({ jsonrpc: '2.0', id: rpcId, method, params });
  65. });
  66. return { id, child, send, request };
  67. }
  68. const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
  69. const clients = Array.from({ length: N }, (_, i) => makeClient(i));
  70. // Initialize every client (handshake is answered locally by each proxy, instant).
  71. await Promise.all(clients.map((c) =>
  72. c.request('initialize', { protocolVersion: '2024-11-05', capabilities: {}, clientInfo: { name: 'repro', version: '1' } }, `init-${c.id}`, 10000)
  73. .then(() => c.send({ jsonrpc: '2.0', method: 'initialized' }))
  74. ));
  75. // Warm the daemon: one explore through client 0 forces daemon spawn + project
  76. // open + catch-up gate to complete, so the concurrent wave measures the STEADY
  77. // state (the user's real scenario after the first call), not cold start.
  78. if (WARM) {
  79. process.stderr.write('[repro] warming daemon (first explore triggers spawn+open+catchup)...\n');
  80. const t0 = performance.now();
  81. const r = await clients[0].request('tools/call', { name: 'codegraph_explore', arguments: { query: QUERIES[0] } }, 'warm-0', 120000);
  82. process.stderr.write(`[repro] warm explore took ${Math.round(performance.now() - t0)}ms (timeout=${!!r.__timeout})\n`);
  83. await sleep(500);
  84. }
  85. // THE WAVE: fire one explore on every client as simultaneously as possible.
  86. process.stderr.write(`[repro] firing ${N} concurrent explores...\n`);
  87. const waveStart = performance.now();
  88. const results = await Promise.all(clients.map((c, i) => {
  89. const started = performance.now();
  90. return c.request('tools/call', { name: 'codegraph_explore', arguments: { query: QUERIES[i % QUERIES.length] } }, `call-${c.id}`, TIMEOUT_MS)
  91. .then((m) => ({
  92. id: c.id,
  93. ms: Math.round(performance.now() - started),
  94. timedOut: !!m.__timeout,
  95. ok: !!m.result && !m.result.isError,
  96. chars: m.result?.content?.[0]?.text?.length ?? 0,
  97. }));
  98. }));
  99. const waveMs = Math.round(performance.now() - waveStart);
  100. const lat = results.map((r) => r.ms).sort((a, b) => a - b);
  101. const timeouts = results.filter((r) => r.timedOut).length;
  102. const p = (q) => lat[Math.min(lat.length - 1, Math.floor(q * lat.length))];
  103. console.log('='.repeat(64));
  104. console.log(`HARNESS B (real daemon + ${N} proxies) repo=${repo}`);
  105. console.log(`warm=${WARM} perCallTimeout=${TIMEOUT_MS}ms`);
  106. console.log('-'.repeat(64));
  107. console.log(`wave wall-clock: ${waveMs}ms`);
  108. console.log(`per-call latency min=${lat[0]} p50=${p(0.5)} p90=${p(0.9)} max=${lat[lat.length - 1]} (ms)`);
  109. console.log(`TIMEOUTS (>${TIMEOUT_MS}ms): ${timeouts} / ${N}`);
  110. console.log(`completion order (id:ms): ${results.slice().sort((a,b)=>a.ms-b.ms).map(r=>`${r.id}:${r.ms}`).join(' ')}`);
  111. console.log('='.repeat(64));
  112. for (const c of clients) { try { c.child.stdin.end(); c.child.kill('SIGTERM'); } catch {} }
  113. await sleep(300);
  114. process.exit(0);