* A daemon owner starts with idle exit OFF. Armed at startup, its 500ms * timer raced this process's first connection and lost on a loaded machine, * so the daemon exited before any engine reached it (#1773). A test that * needs the idle exit enables it with armIdleExit once it hold
(mode: 'direct' | 'daemon', slowCatchUp = false)
| 118 | * session; the daemon reads the value each time its last client leaves. |
| 119 | */ |
| 120 | async function startOwner(mode: 'direct' | 'daemon', slowCatchUp = false): Promise<ChildProcess> { |
| 121 | const modulePath = path.resolve(__dirname, '../dist/mcp'); |
| 122 | const ownerScript = mode === 'daemon' ? ` |
| 123 | const { Daemon, tryAcquireDaemonLock } = require(process.argv[1] + '/daemon'); |
| 124 | const root = process.argv[2]; |
| 125 | tryAcquireDaemonLock(root); |
| 126 | const daemon = new Daemon(root, { idleTimeoutMs: 0 }); |
| 127 | process.on('message', (message) => { |
| 128 | if (message !== 'arm-idle') return; |
| 129 | daemon.idleTimeoutMs = 500; |
| 130 | process.send('idle-armed'); |
| 131 | }); |
| 132 | daemon.start().then(() => { if (!process.env.CG_TEST_HOLD_CATCHUP) process.send('ready'); }); |
| 133 | ` : ` |
| 134 | const { MCPEngine } = require(process.argv[1] + '/engine'); |
| 135 | const engine = new MCPEngine(); |
| 136 | process.on('SIGTERM', async () => { await engine.stop(); process.exit(0); }); |
| 137 | engine.ensureInitialized(process.argv[2]).then(async () => { |
| 138 | await engine.getToolHandler().execute('codegraph_status', {}); |
| 139 | if (!process.env.CG_TEST_HOLD_CATCHUP) process.send('ready'); |
| 140 | }); |
| 141 | `; |
| 142 | const script = ` |
| 143 | if (process.env.CG_TEST_HOLD_CATCHUP) { |
| 144 | const CodeGraph = require(process.argv[1] + '/../index').default; |
| 145 | const sync = CodeGraph.prototype.sync; |
| 146 | CodeGraph.prototype.sync = async function (...args) { |
| 147 | process.send('ready'); |
| 148 | await new Promise((resolve) => setTimeout(resolve, 300)); |
| 149 | return sync.apply(this, args); |
| 150 | }; |
| 151 | } |
| 152 | ` + ownerScript; |
| 153 | const child = spawn(process.execPath, ['-e', script, modulePath, serviceA], { |
| 154 | stdio: ['ignore', 'pipe', 'pipe', 'ipc'], |
| 155 | env: { ...process.env, CODEGRAPH_NO_DAEMON: mode === 'daemon' ? '0' : '1', CODEGRAPH_QUERY_POOL_SIZE: '0', CG_TEST_HOLD_CATCHUP: slowCatchUp ? '1' : '' }, |
| 156 | }); |
| 157 | children.push(child); |
| 158 | let stderr = ''; |
| 159 | child.stderr!.on('data', (chunk) => { stderr += String(chunk); }); |
| 160 | child.stdout!.resume(); |
| 161 | await new Promise<void>((resolve, reject) => { |
| 162 | const timer = setTimeout(() => reject(new Error(`Owner did not start: ${stderr}`)), 10000); |
| 163 | child.once('message', () => { clearTimeout(timer); resolve(); }); |
| 164 | child.once('error', (err) => { clearTimeout(timer); reject(err); }); |
| 165 | child.once('exit', () => { clearTimeout(timer); reject(new Error(`Owner exited: ${stderr}`)); }); |
| 166 | }); |
| 167 | return child; |
| 168 | } |
| 169 | |
| 170 | async function armIdleExit(owner: ChildProcess): Promise<void> { |
| 171 | await new Promise<void>((resolve, reject) => { |