feat!: the app is declared on its target; services are defineService handles
Closed
OK
1mo ago+3466+3466+0123456789012345678901234567890123456789-681-681-012345678901234567890123456789
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 (
executablewithreadyUrlorwaitForExit) or a function (start,stop), never both.dependsOnorders services and pulls dependencies in;defineServicecopies and freezes its lists, so a cycle cannot exist. app.commandis shorthand for a service namedapp:<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 isINVALID_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, andsvc.urlOf('smtp'),svc.portOf('smtp')for a named port, usable inside a longer string. The config resolves without ports, andassignPortsbinds 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
envon top. - Refused at load: two processes probing one fixed address,
app.commandbeside a service URL, a placeholder outside the reader's graph, a placeholder in areadyUrl, anapp.readyUrlon 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 servesapp.url; nothing in the template code knows a role.ResolvedTarget.appUrlcarriesapp.urlas the service resolution reads it;ResolvedAppholds only resolved facts. The services types live intypes/services.ts, re-exported frome2e.
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
startignored interrupts. Now the first interrupt aborts the start, and teardown runs. Function services also get astartupTimeout(default 60s). teardownran for a service that never spawned, which could rundocker compose downon your own stack. Now it runs only after a spawn.- A throwing
stopis named:service "seed" stop failed: ..., and a function service that never started is not stopped. - A locator's reported
waitedMsis timed on its own deadline's clock (Deadline.minanchors 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'sstart/stopwrapped the run. - A hung function
startunder 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.tsopens two sessions at once on a fixed-port app command beside a port-0 service. It failed withAPP_ALREADY_RUNNINGbefore the fix and passes after.