|
22 | 22 | * |
23 | 23 | * Why one Node script for both languages: the smoke is a client, and the |
24 | 24 | * question it asks is the same whatever the server is written in. The Python |
25 | | - * servers are launched through `uv run --frozen`, never pip, like every other |
26 | | - * Python step here. |
| 25 | + * servers are launched through `uv run --no-sync`, never pip. |
27 | 26 | * |
28 | | - * Nothing touches the network or the user's files. `fetch` is pointed at an |
29 | | - * HTTP server this script starts on the loopback interface; `git` and |
30 | | - * `filesystem` get a throwaway directory; `memory` gets a throwaway graph file. |
31 | | - * The HTTP transports listen on a port the OS hands out, not the server's |
32 | | - * default 3001, so the smoke cannot collide with a server the developer has |
33 | | - * running, nor two smokes with each other. |
| 27 | + * `--no-sync` because the smoke tests an environment, it does not build one. A |
| 28 | + * plain `uv run` (even `--frozen`, which only stops the lockfile being |
| 29 | + * rewritten) creates the server's `.venv` and downloads into it when it is |
| 30 | + * missing or stale, which would make this stage a second, unannounced install |
| 31 | + * step. So each Python server must already be synced: `validate:py` does that |
| 32 | + * in the gate just before the smoke, and CI's smoke job has a sync step. A |
| 33 | + * server with no `.venv` is reported as that. |
| 34 | + * |
| 35 | + * The servers themselves are given nothing outside the machine to talk to, and |
| 36 | + * none of the user's files. `fetch` is pointed at an HTTP server this script |
| 37 | + * starts on the loopback interface; `git` and `filesystem` get a throwaway |
| 38 | + * directory; `memory` gets a throwaway graph file. The HTTP transports listen |
| 39 | + * on a port the OS reported free a moment earlier, not the server's default |
| 40 | + * 3001, so the smoke does not collide with a server the developer has running. |
| 41 | + * That port is not reserved between the probe and the server's own `listen`, |
| 42 | + * so a launch that loses it to another process is relaunched on a fresh one. |
34 | 43 | * |
35 | 44 | * One server failing does not stop the rest: the run reports every target's |
36 | 45 | * verdict and exits non-zero if any failed, so one run shows the whole picture. |
@@ -72,6 +81,14 @@ export const FETCH_MARKER = "boot-smoke-marker"; |
72 | 81 | */ |
73 | 82 | export const REQUEST_TIMEOUT_MS = 120_000; |
74 | 83 |
|
| 84 | +/** How many ports an HTTP launch may try before a taken port is the result. */ |
| 85 | +export const PORT_ATTEMPTS = 3; |
| 86 | + |
| 87 | +/** Whether a server's output says it lost its port to another process. */ |
| 88 | +export function isPortTaken(output) { |
| 89 | + return /EADDRINUSE|already in use/i.test(output); |
| 90 | +} |
| 91 | + |
75 | 92 | /** How long an HTTP server gets to start listening. */ |
76 | 93 | export const LISTEN_TIMEOUT_MS = 30_000; |
77 | 94 |
|
@@ -244,7 +261,7 @@ export function launchSpec(server, transport, ctx, root = repoRoot) { |
244 | 261 | } |
245 | 262 | return { |
246 | 263 | command: "uv", |
247 | | - args: ["run", "--frozen", `mcp-server-${server.name}`, ...serverArgs], |
| 264 | + args: ["run", "--no-sync", `mcp-server-${server.name}`, ...serverArgs], |
248 | 265 | cwd, |
249 | 266 | }; |
250 | 267 | } |
@@ -342,6 +359,10 @@ async function smokeOne(server, transport, ctx) { |
342 | 359 | throw new Error( |
343 | 360 | `${path.relative(repoRoot, spec.args[0])} does not exist — build first (\`npm run build -w src/${server.name}\`).`, |
344 | 361 | ); |
| 362 | + if (server.language === "py" && !existsSync(path.join(spec.cwd, ".venv"))) |
| 363 | + throw new Error( |
| 364 | + `src/${server.name}/.venv does not exist — sync first (\`npm run validate:py -- ${server.name}\`, or \`uv sync --locked --all-extras --dev\` in src/${server.name}). The smoke does not create environments.`, |
| 365 | + ); |
345 | 366 | server.prepare?.(ctx); |
346 | 367 |
|
347 | 368 | const client = new Client({ name: "boot-smoke", version: "0.0.0" }); |
@@ -372,42 +393,53 @@ async function smokeOne(server, transport, ctx) { |
372 | 393 | return; |
373 | 394 | } |
374 | 395 |
|
375 | | - const port = await freePort(); |
376 | | - let output = ""; |
377 | | - const child = spawn(spec.command, spec.args, { |
378 | | - cwd: spec.cwd, |
379 | | - env: envFor(server, ctx, { PORT: String(port) }), |
380 | | - stdio: ["ignore", "pipe", "pipe"], |
381 | | - }); |
382 | | - child.stdout.on("data", (chunk) => (output += chunk)); |
383 | | - child.stderr.on("data", (chunk) => (output += chunk)); |
384 | | - const tail = () => output.trim().split("\n").slice(-15).join("\n"); |
385 | | - try { |
386 | | - await new Promise((resolve, reject) => { |
387 | | - child.once("error", reject); |
388 | | - child.once("spawn", resolve); |
| 396 | + // The probe port is free when probed, not reserved: another process can take |
| 397 | + // it before the server binds. Only that one failure is relaunched, on a fresh |
| 398 | + // port; anything else the server says on its way down is the result. |
| 399 | + for (let attempt = 1; ; attempt += 1) { |
| 400 | + const port = await freePort(); |
| 401 | + let output = ""; |
| 402 | + const child = spawn(spec.command, spec.args, { |
| 403 | + cwd: spec.cwd, |
| 404 | + env: envFor(server, ctx, { PORT: String(port) }), |
| 405 | + stdio: ["ignore", "pipe", "pipe"], |
389 | 406 | }); |
390 | | - await waitForListen(port, child, tail); |
391 | | - const url = new URL( |
392 | | - `http://127.0.0.1:${port}${TRANSPORT_ENDPOINT[transport]}`, |
393 | | - ); |
394 | | - const clientTransport = |
395 | | - transport === "sse" |
396 | | - ? new SSEClientTransport(url) |
397 | | - : new StreamableHTTPClientTransport(url); |
| 407 | + child.stdout.on("data", (chunk) => (output += chunk)); |
| 408 | + child.stderr.on("data", (chunk) => (output += chunk)); |
| 409 | + const tail = () => output.trim().split("\n").slice(-15).join("\n"); |
398 | 410 | try { |
399 | | - await client.connect(clientTransport, options); |
400 | | - await exercise(client, server, ctx); |
| 411 | + await new Promise((resolve, reject) => { |
| 412 | + child.once("error", reject); |
| 413 | + child.once("spawn", resolve); |
| 414 | + }); |
| 415 | + try { |
| 416 | + await waitForListen(port, child, tail); |
| 417 | + } catch (err) { |
| 418 | + if (attempt < PORT_ATTEMPTS && isPortTaken(output)) continue; |
| 419 | + throw err; |
| 420 | + } |
| 421 | + const url = new URL( |
| 422 | + `http://127.0.0.1:${port}${TRANSPORT_ENDPOINT[transport]}`, |
| 423 | + ); |
| 424 | + const clientTransport = |
| 425 | + transport === "sse" |
| 426 | + ? new SSEClientTransport(url) |
| 427 | + : new StreamableHTTPClientTransport(url); |
| 428 | + try { |
| 429 | + await client.connect(clientTransport, options); |
| 430 | + await exercise(client, server, ctx); |
| 431 | + } finally { |
| 432 | + await client.close().catch(() => {}); |
| 433 | + } |
| 434 | + return; |
| 435 | + } catch (err) { |
| 436 | + throw new Error( |
| 437 | + `${err?.message ?? err}${tail() ? `\n--- server output ---\n${tail()}` : ""}`, |
| 438 | + { cause: err }, |
| 439 | + ); |
401 | 440 | } finally { |
402 | | - await client.close().catch(() => {}); |
| 441 | + await stop(child); |
403 | 442 | } |
404 | | - } catch (err) { |
405 | | - throw new Error( |
406 | | - `${err?.message ?? err}${tail() ? `\n--- server output ---\n${tail()}` : ""}`, |
407 | | - { cause: err }, |
408 | | - ); |
409 | | - } finally { |
410 | | - await stop(child); |
411 | 443 | } |
412 | 444 | } |
413 | 445 |
|
|
0 commit comments