|
| 1 | +import { spawn } from 'node:child_process'; |
| 2 | +import process from 'node:process'; |
| 3 | + |
| 4 | +interface CliOptions { |
| 5 | + iterations: number; |
| 6 | + closeDelayMs: number; |
| 7 | + settleMs: number; |
| 8 | +} |
| 9 | + |
| 10 | +interface PeerProcess { |
| 11 | + pid: number; |
| 12 | + ageSeconds: number; |
| 13 | + rssKb: number; |
| 14 | + command: string; |
| 15 | +} |
| 16 | + |
| 17 | +function parseArgs(argv: string[]): CliOptions { |
| 18 | + const options: CliOptions = { |
| 19 | + iterations: 20, |
| 20 | + closeDelayMs: 0, |
| 21 | + settleMs: 2000, |
| 22 | + }; |
| 23 | + |
| 24 | + for (let index = 0; index < argv.length; index += 1) { |
| 25 | + const arg = argv[index]; |
| 26 | + const value = argv[index + 1]; |
| 27 | + |
| 28 | + if (arg === '--iterations' && value) { |
| 29 | + options.iterations = Number(value); |
| 30 | + index += 1; |
| 31 | + } else if (arg === '--close-delay-ms' && value) { |
| 32 | + options.closeDelayMs = Number(value); |
| 33 | + index += 1; |
| 34 | + } else if (arg === '--settle-ms' && value) { |
| 35 | + options.settleMs = Number(value); |
| 36 | + index += 1; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + if (!Number.isFinite(options.iterations) || options.iterations < 1) { |
| 41 | + throw new Error('--iterations must be a positive number'); |
| 42 | + } |
| 43 | + if (!Number.isFinite(options.closeDelayMs) || options.closeDelayMs < 0) { |
| 44 | + throw new Error('--close-delay-ms must be a non-negative number'); |
| 45 | + } |
| 46 | + if (!Number.isFinite(options.settleMs) || options.settleMs < 0) { |
| 47 | + throw new Error('--settle-ms must be a non-negative number'); |
| 48 | + } |
| 49 | + |
| 50 | + return options; |
| 51 | +} |
| 52 | + |
| 53 | +function delay(ms: number): Promise<void> { |
| 54 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 55 | +} |
| 56 | + |
| 57 | +function isLikelyMcpCommand(command: string): boolean { |
| 58 | + const normalized = command.toLowerCase(); |
| 59 | + return ( |
| 60 | + /(^|\s)mcp(\s|$)/.test(normalized) && |
| 61 | + !/(^|\s)daemon(\s|$)/.test(normalized) && |
| 62 | + (normalized.includes('xcodebuildmcp') || |
| 63 | + normalized.includes('build/cli.js') || |
| 64 | + normalized.includes('/cli.js')) |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +function parseElapsedSeconds(value: string): number | null { |
| 69 | + const trimmed = value.trim(); |
| 70 | + if (!trimmed) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + const daySplit = trimmed.split('-'); |
| 75 | + const timePart = daySplit.length === 2 ? daySplit[1] : daySplit[0]; |
| 76 | + const dayCount = daySplit.length === 2 ? Number(daySplit[0]) : 0; |
| 77 | + const parts = timePart.split(':').map((part) => Number(part)); |
| 78 | + |
| 79 | + if (!Number.isFinite(dayCount) || parts.some((part) => !Number.isFinite(part))) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + if (parts.length === 1) { |
| 84 | + return dayCount * 86400 + parts[0]; |
| 85 | + } |
| 86 | + if (parts.length === 2) { |
| 87 | + return dayCount * 86400 + parts[0] * 60 + parts[1]; |
| 88 | + } |
| 89 | + if (parts.length === 3) { |
| 90 | + return dayCount * 86400 + parts[0] * 3600 + parts[1] * 60 + parts[2]; |
| 91 | + } |
| 92 | + |
| 93 | + return null; |
| 94 | +} |
| 95 | + |
| 96 | +async function sampleMcpProcesses(): Promise<PeerProcess[]> { |
| 97 | + return new Promise((resolve, reject) => { |
| 98 | + const child = spawn('ps', ['-axo', 'pid=,etime=,rss=,command='], { |
| 99 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 100 | + }); |
| 101 | + let stdout = ''; |
| 102 | + let stderr = ''; |
| 103 | + |
| 104 | + child.stdout.on('data', (chunk: Buffer) => { |
| 105 | + stdout += chunk.toString(); |
| 106 | + }); |
| 107 | + child.stderr.on('data', (chunk: Buffer) => { |
| 108 | + stderr += chunk.toString(); |
| 109 | + }); |
| 110 | + child.on('error', reject); |
| 111 | + child.on('close', (code) => { |
| 112 | + if (code !== 0) { |
| 113 | + reject(new Error(stderr || `ps exited with code ${code}`)); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + const processes = stdout |
| 118 | + .split('\n') |
| 119 | + .map((line) => line.trim()) |
| 120 | + .filter(Boolean) |
| 121 | + .map((line) => { |
| 122 | + const match = line.match(/^(\d+)\s+(\S+)\s+(\d+)\s+(.+)$/); |
| 123 | + if (!match) { |
| 124 | + return null; |
| 125 | + } |
| 126 | + const ageSeconds = parseElapsedSeconds(match[2]); |
| 127 | + return { |
| 128 | + pid: Number(match[1]), |
| 129 | + ageSeconds, |
| 130 | + rssKb: Number(match[3]), |
| 131 | + command: match[4], |
| 132 | + }; |
| 133 | + }) |
| 134 | + .filter((entry): entry is PeerProcess => { |
| 135 | + return ( |
| 136 | + entry !== null && |
| 137 | + Number.isFinite(entry.pid) && |
| 138 | + Number.isFinite(entry.ageSeconds) && |
| 139 | + Number.isFinite(entry.rssKb) && |
| 140 | + isLikelyMcpCommand(entry.command) |
| 141 | + ); |
| 142 | + }); |
| 143 | + |
| 144 | + resolve(processes); |
| 145 | + }); |
| 146 | + }); |
| 147 | +} |
| 148 | + |
| 149 | +async function runIteration(closeDelayMs: number): Promise<boolean> { |
| 150 | + return new Promise((resolve) => { |
| 151 | + const child = spawn(process.execPath, ['build/cli.js', 'mcp'], { |
| 152 | + cwd: process.cwd(), |
| 153 | + stdio: ['pipe', 'ignore', 'ignore'], |
| 154 | + }); |
| 155 | + |
| 156 | + let exited = false; |
| 157 | + child.once('close', () => { |
| 158 | + exited = true; |
| 159 | + resolve(true); |
| 160 | + }); |
| 161 | + child.once('error', () => { |
| 162 | + exited = true; |
| 163 | + resolve(false); |
| 164 | + }); |
| 165 | + |
| 166 | + setTimeout(() => { |
| 167 | + child.stdin.end(); |
| 168 | + }, closeDelayMs); |
| 169 | + |
| 170 | + setTimeout( |
| 171 | + () => { |
| 172 | + if (!exited) { |
| 173 | + resolve(false); |
| 174 | + } |
| 175 | + }, |
| 176 | + Math.max(1000, closeDelayMs + 1000), |
| 177 | + ); |
| 178 | + }); |
| 179 | +} |
| 180 | + |
| 181 | +async function main(): Promise<void> { |
| 182 | + const options = parseArgs(process.argv.slice(2)); |
| 183 | + const before = await sampleMcpProcesses(); |
| 184 | + const baselinePids = new Set(before.map((entry) => entry.pid)); |
| 185 | + |
| 186 | + let exitedCount = 0; |
| 187 | + for (let index = 0; index < options.iterations; index += 1) { |
| 188 | + const exited = await runIteration(options.closeDelayMs); |
| 189 | + if (exited) { |
| 190 | + exitedCount += 1; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + await delay(options.settleMs); |
| 195 | + |
| 196 | + const after = await sampleMcpProcesses(); |
| 197 | + const lingering = after.filter((entry) => !baselinePids.has(entry.pid)); |
| 198 | + |
| 199 | + console.log( |
| 200 | + JSON.stringify( |
| 201 | + { |
| 202 | + iterations: options.iterations, |
| 203 | + exitedCount, |
| 204 | + baselineProcessCount: before.length, |
| 205 | + finalProcessCount: after.length, |
| 206 | + lingeringProcessCount: lingering.length, |
| 207 | + lingering: lingering.map(({ pid, ageSeconds, rssKb, command }) => ({ |
| 208 | + pid, |
| 209 | + ageSeconds, |
| 210 | + rssKb, |
| 211 | + command, |
| 212 | + })), |
| 213 | + }, |
| 214 | + null, |
| 215 | + 2, |
| 216 | + ), |
| 217 | + ); |
| 218 | + |
| 219 | + process.exit(lingering.length === 0 ? 0 : 1); |
| 220 | +} |
| 221 | + |
| 222 | +void main().catch((error) => { |
| 223 | + console.error(error instanceof Error ? error.message : String(error)); |
| 224 | + process.exit(1); |
| 225 | +}); |
0 commit comments