feat!: the app is declared on its target; services are defineService handles

Closed
OK
1mo ago
+3466-681

Services are defineService handles, listed on the targets that need them. This is the second half of the config redesign, on top of #698 (the app on its target, now on main); it replaces the closed #489.

const db = defineService({ name: 'db', executable: 'docker', args: ['compose', 'up', '--wait'], waitForExit: true, teardown: { ... } });
const seed = defineService({ name: 'seed', dependsOn: [db], start: async () => { ... }, stop: async () => { ... } });
const webServer = defineService({ name: 'web-server', executable: 'pnpm', args: ['dev', '--port', '{port}'], readyUrl: 'http://127.0.0.1:0', env: { API: api.url }, dependsOn: [seed] });

targets: [
  { name: 'chromium', engine: web(), app: { url: webServer.url }, services: [webServer] },
  { name: 'firefox', engine: web({ browser: 'firefox' }), app: { url: webServer.url }, services: [webServer] },   // one shared dev server
  { name: 'ios', engine: mobile({ platform: 'ios' }), app: { bundleId: 'dev.shop.app', appPath: './build/Shop.app' }, services: [seed] },
],

One process model

  • A service is a process (executable with readyUrl or waitForExit) or a function (start, stop), never both. dependsOn orders services and pulls dependencies in; defineService copies and freezes its lists, so a cycle cannot exist.
  • app.command is shorthand for a service named app:<target>.
  • {port} is a process's own free port, and a process with more than one names them (ports: { smtp: 0 }, {port:smtp}). A service that uses {port} on a fixed port is INVALID_CONFIG, with a message spelling the new form (it used to mean the app's port).
  • Address placeholders are plain strings holding a token: svc.url, svc.port, and svc.urlOf('smtp'), svc.portOf('smtp') for a named port, usable inside a longer string. The config resolves without ports, and assignPorts binds them, in the main process and again in each worker from the port list the main process hands down.
  • Services live for the whole run. Function services get the addresses of their dependencies. Child processes inherit the runner's full environment, with env on top.
  • Refused at load: two processes probing one fixed address, app.command beside a service URL, a placeholder outside the reader's graph, a placeholder in a readyUrl, an app.readyUrl on port 0.

Layout

  • config/services/ is four modules behind an index: model (templates and resolved services), collect (the graph and its start order), template (checking a process into a template, {port} and placeholder rules), bind (substituting the run's ports, keys, port requests, digest). An app command is a process spec that serves app.url; nothing in the template code knows a role.
  • ResolvedTarget.appUrl carries app.url as the service resolution reads it; ResolvedApp holds only resolved facts. The services types live in types/services.ts, re-exported from e2e.

e2e mcp

  • Sessions share a process when its key matches: name plus dependency keys for function services, the resolved command for processes.
  • A session that opens while one that was assigned ports is still admitted reads the same ports, serialized across concurrent opens, so a fixed-port process stays shared when a service it depends on asks for a free one; once no such session is left, the next gets fresh ports. Before, the second session found the first's process and failed with APP_ALREADY_RUNNING.

Bugs found by live runs, fixed here

  • A hung function start ignored interrupts. Now the first interrupt aborts the start, and teardown runs. Function services also get a startupTimeout (default 60s).
  • teardown ran for a service that never spawned, which could run docker compose down on your own stack. Now it runs only after a spawn.
  • A throwing stop is named: service "seed" stop failed: ..., and a function service that never started is not stopped.
  • A locator's reported waitedMs is timed on its own deadline's clock (Deadline.min anchors at now), so it can no longer read one millisecond short of the timeout.

Verified

Ran it locally: yes

  • Built CLI on the testbed: two chromium targets shared one port-0 dev server through app: { url: webServer.url }, with a function seed service and an API placeholder, on two workers. Both targets landed on the same port, and the seed's start/stop wrapped the run.
  • A hung function start under the real CLI: the first SIGINT exits 130, and the log shows the dependencies stopped and their teardowns ran.
  • The MCP port fix: mcp-session-host.test.ts opens two sessions at once on a fixed-port app command beside a port-0 service. It failed with APP_ALREADY_RUNNING before the fix and passes after.