feat: start services in e2e mcp and openHost (ENG-819)

Merged
SA
4d ago
+973-237

Builds on #2191 (services) and #2258 (app.command.env function), which are merged.

What

e2e mcp and openHost start the config's services, where they used to refuse a config that declares them.

  • e2e mcp starts a config file's services when the first session on it opens, and stops them when the last one closes. All the sessions on that file share them. Each session calls the env function with their values, and open_session lists them in a Services: line.
  • openHost starts the services and calls the env function when the host opens. host.close() stops the app processes, then the services.
  • Sessions in both get the services fixture, and run_code gets services with the same values: await services.email.createInbox().

Why

A coding agent driving the app through e2e mcp, or another test runner through openHost, needs the same dependencies that e2e run starts. Without this, a config with services couldn't open a session in either one.

How it works

  1. The standalone host takes services (packages/core/src/run/standalone.ts:38) and passes them to startDeclaredProcesses for env functions, beside the process pool, and to the session's fixtures (standalone.ts:165, :174).
  2. e2e mcp keeps services in a SharedProcesses<RunServices> pool keyed by config path (packages/e2e/src/mcp/session.ts:158). The pool was SharedAppProcesses and now takes a type parameter, so it's renamed (packages/core/src/run/process-pool.ts:50). open_session gets the services before the app starts (session.ts:482), and closeAttempt releases them after the attempt closes (session.ts:268). It first waits for any app start that's still stopping in the background, so the last session never stops the services under an app (session.ts:284). A second Ctrl+C ends the server process, so the signal that would force a stop never aborts (NEVER_ABORTS from core, with the reason at session.ts:476). When open_session fails, each cleanup error is logged as a warning (session.ts:590).
  3. openHost starts the services before it opens the host, and stops them if opening fails (packages/e2e/src/run/open-session.ts:97). closeHost closes the host, then stops the services. close() keeps the first closeHost() promise, so calling it twice stops each service once (open-session.ts:113, :131).
  4. run_code gets services as a built-in, and the code recorder wraps it like app (packages/e2e/src/session-tools/run-code.ts:260, :311). A session attached to a held test gets the run's services through HeldTestTools, which now takes one options object (packages/e2e/src/run/worker/held-test-tools.ts:503).
  5. The INVALID_CONFIG refusals from #2191 are gone. docs/services.mdx, the MCP and runner references, and the skill pages describe how services run in e2e mcp and openHost.
  6. SharedProcesses.drained() resolves after every stop and every abandoned start's cleanup has finished (packages/core/src/run/process-pool.ts:139). closeAll awaits it for app processes and then for services, so a client that disconnects while a service is starting doesn't end the server before the services that already started are stopped (packages/e2e/src/mcp/session.ts:220).
  7. Every session on a config file joins the shared services, even when the config doesn't have any now, because it may share an app process with a session that uses them (session.ts:475).
  8. startServices takes the config as the secret registry and holds it while start and stop code run, so credentials and secrets work in services under e2e mcp and openHost as they do in e2e run (packages/e2e/src/run/services.ts:123).
  9. When a member isn't an identifier, the run_code recorder writes it with brackets (like services["my-mail"]), in a call and in data passed as an argument, so a recorded line is valid code (memberText, packages/e2e/src/session-tools/code-recorder.ts:347). test_lines reads a fixture name followed by [ too, and counts services when a line only passes it along, so the draft's test body asks for services (packages/e2e/src/session-tools/test-lines.ts:707, :712).

Not covered yet

One limit is out of scope for this PR:

  • openHost starts one set of services for each host. If another test runner has several workers that each open a host, each worker starts one copy of every service. That differs from e2e run, where all workers share one instance. The guide and the skill say so.

Verified

Ran it locally: yes

The built CLI at this PR's head (b6f27ed) on a scratch project with the docs email example: e2e run, then an openHost script that creates an inbox, signs up, and waits for the email:

$ e2e run
 ✓ service "email" ready 2ms
 ✓ target "web" command at http://127.0.0.1:55072/ ready 92ms
   ✓ |web| tests/signup.e2e.ts > signing up sends one welcome email 382ms
      Tests  1 passed (1)

$ node host.mjs   # openHost, openSession, sign up, waitForEmail, close
notice service: service "email" started in 1 ms
email {"to":"15054ee6-2764-4a89-8777-70a43fa7280e@example.com","subject":"Welcome"}
session close errors 0
host close errors 0

e2e mcp over stdio on the same project: open_session, a service call from run_code, then close_session:

> open_session
Session 01a2189d-a912-72ab-96a1-0306acc530e7 open on target "web" (platform web, engine web 0.17.0), headless; config /private/tmp/svc-verify/e2e.config.ts, read with the e2e mcp server's environment, not your shell's.
App: http://127.0.0.1:55133/ (port 55133, assigned for the declared port 0).
Services: email. Every session open on this config file shares them.
Process: app.command "node" (ready at http://127.0.0.1:55133/; env from a function). e2e starts no other app process for this session.
> run_code: const inbox = await services.email.createInbox(); return inbox.address.endsWith("@example.com");
Returned: true
> close_session
Session 01a2189d-a912-72ab-96a1-0306acc530e7 closed (closed by the agent); 1 tool calls ran.

open_session adds a Services: line only when the config has services, so there's no main side to compare. Benchmark: pnpm bench:ab at this PR's head against main, on two web benchmark files:

bench-ab: c3109278 (merge base with origin/main) vs b6f27edd

apps/web-benchmark: e2e run --workers 1 tests/control-inventory.e2e.ts tests/cart-totals.e2e.ts. 6 pairs in random order, resolved after 6 pairs. Threshold ±3% and 10 ms. darwin arm64, Apple M5 Pro x18, Node 24.22.1, load 4.8.

Behavior

Every test ran the same steps with the same outcome on every run of both builds.

Timings

Metricbase median (IQR)head median (IQR)change (95% CI)verdict
wall (process)4.53 s (4.48 s-4.54 s)4.50 s (4.41 s-4.61 s)-0.7% (-2.6% to +3.3%)unresolved
suite (sum of attempts)3.69 s (3.67 s-3.72 s)3.71 s (3.66 s-3.75 s)-1.0% (-2.2% to +2.3%)same
action (events)2.54 s (2.54 s-2.58 s)2.55 s (2.54 s-2.57 s)-0.1% (-2.2% to +0.9%)same, under the floor
step locator.longPress1.51 s (1.51 s-1.52 s)1.51 s (1.50 s-1.51 s)-0.2% (-1.1% to +0.4%)same, under the floor
step app.open740 ms (736 ms-743 ms)746 ms (726 ms-773 ms)-1.0% (-3.5% to +5.4%)same, under the floor
step locator.tap457 ms (447 ms-468 ms)470 ms (448 ms-479 ms)+0.5% (-5.3% to +5.6%)same, under the floor
step locator.check286 ms (276 ms-298 ms)296 ms (281 ms-297 ms)-0.2% (-4.6% to +7.9%)same, under the floor
step expect.toHaveText132 ms (129 ms-135 ms)130 ms (127 ms-134 ms)+1.1% (-6.6% to +4.3%)same, under the floor
step locator.scrollIntoView71 ms (69 ms-72 ms)72 ms (68 ms-72 ms)+0.0% (-7.1% to +7.3%)same, under the floor
step locator.secondaryTap70 ms (61 ms-79 ms)64 ms (62 ms-73 ms)-8.5% (-21.0% to +25.2%)same, under the floor
step locator.setInputFiles65 ms (64 ms-67 ms)63 ms (61 ms-65 ms)-3.2% (-6.2% to -0.7%)same, under the floor
step locator.doubleTap62 ms (61 ms-62 ms)63 ms (61 ms-63 ms)+0.8% (-16.7% to +3.3%)same, under the floor
step locator.uncheck38 ms (30 ms-45 ms)33 ms (32 ms-33 ms)-14.3% (-29.7% to +31.3%)same, under the floor
step app.back15 ms (14 ms-16 ms)14 ms (14 ms-15 ms)-6.5% (-12.3% to +10.2%)same, under the floor
step expect.toBeVisible3 ms (2 ms-3 ms)2 ms (2 ms-2 ms)-33.3% (-42.3% to +41.4%)same, under the floor
step expect.toBeChecked2 ms (2 ms-2 ms)3 ms (2 ms-3 ms)0.0% (0.0% to +73.2%)same
step expect.toHaveURL1 ms (0 ms-1 ms)0 ms (0 ms-1 ms)0.0% (0.0% to 0.0%)same

Counters

Every counter matched on every run of both builds.

Tests outside the threshold: 0 of 95

With 95 tests compared at 95%, about 4.8 could land outside by chance.