feat(engine): engine protocol for out-of-process engines
The engine contract now crosses a process boundary, and the harness drives every engine over it. An engine runs in a child process, on another machine, or on a device, written in any language, and the harness drives it exactly as it drives one built with defineEngine. defineEngine stays as the TypeScript way to write an engine: the harness serves it in process over loopbackTransport, so every suite crosses the wire and a contract change that cannot fails everywhere.
The protocol
The Language Server Protocol, applied to engines:
- Same base layer: JSON-RPC 2.0 over
Content-Lengthframing, requests and notifications both ways. - Same lifecycle:
initialize/initialized/shutdown/exit. - A server announces a manifest the way a language server announces capabilities.
Each engine member is one request (engine/observe, keyboard/type, lifecycle/init, ...) whose params are its arguments by name. Callbacks into the harness (appLog, screen, resolveSecret, log) are server-to-client messages. An AbortSignal crosses as an id plus $/abort with its reason, so a TestTimeoutError reaches the engine as it would in process. Errors keep their code, retryability, and category.
flowchart LR
H[harness: ResolvedTarget.engine<br/>EngineClientHandle] -- JSON-RPC --> T{transport}
T -- loopback --> S1[serveEngine: web, mobile, any defineEngine]
T -- spawn stdio --> S2[server in a child process]
T -- socket --> S3[server on another machine]
T -- adb forward / xcodebuild --> S4[Java or Swift server on a device]
import { connectEngine, spawnTransport } from 'e2e/engine';
engine: connectEngine({
manifest: { name: 'kiosk', version: '1.0.0', spiVersion: 1, platform: 'kiosk', methods: ['engine/observe', 'engine/perform'], actions: ['tap'] },
transport: spawnTransport({ command: './kiosk-engine' }),
resolveLocators: true, // the server only produces a tree
session: { restart: (operation) => relaunchFromHost(operation) }, // answered on the client
});
- One table,
METHODSinpackages/e2e/src/engine/protocol/protocol.ts, maps each method to its member and arguments.describeEngine, the server, and the client are all read off it. connectEnginebuilds an engine from the manifest and hands it todefineEngine, so a manifest is held to every rule an in-process engine is (engine/performneedsactions,keyboard/typeneedskeyboard/press, ...). The manifest is declared at config load; the server's own must match atinitialize. Its JSON Schema ships asschema/engine-manifest-v1.schema.json.- The harness holds only protocol clients:
ResolvedTarget.engineis anEngineClientHandle, produced byprotocolEngineinresolveTargets. One engine always gets the same client, so handle identity checks (MCPENGINE_IN_USE, hosted workers) still hold. fixtures,validateApp, declared secrets, andsessionhooks run on the client. Asessionhook replaces the server's method, for what only the host can do (relaunching an app withsimctl).- A server that cannot write the host's artifact directory returns a screenshot's bytes; the client writes the file. Transports receive the target's app, so a device transport can install
appPath. - A server forgets a signal id once it aborts, or once the attempt it was first seen in ends.
- Cleanup is bounded: a server that ignores the
finish/disposebudget is closed by its transport anyway, and a lateendAttemptretires only its own attempt. - A secret resolves once: the server derives redacted forms (basic-auth header) through
attempt/deriveSecretwhile the value resolves.ResolveSecretOptions.derivedmay return a promise. - Errors keep their stack, Node error code, and causes across the wire.
serveEnginealways answerslifecycle/startAttempt/endAttempt(anddescribeEnginelists them), so signals are freed for engines without attempt hooks. - Worker connections queue: an MCP session closing while the next one opens calls
initagain beforedispose, so calls go to the newest connection anddisposeends the oldest. - Contract change:
Engine.disposeruns onceinitwas called, even when it failed. A worker that never initialized opened no connection, so its engine gets nodispose. In the changeset. - Docs: one Engines section. Guides:
engines(overview),writing-an-engine,engine-servers(new). Reference:reference/engineandreference/engine-protocol(new).
Verified
Ran it locally: yes. Built packages, this branch:
| Suite | Result |
|---|---|
e2e package (unit + integration) | 4033 passed |
@e2e-dev/web / @e2e-dev/mobile | 635 / 447 passed |
| testbed / testbed agent | 81 / 29 passed |
| web-benchmark / web-benchmark agent | 91 / 33 passed |
| mobile benchmark, both platforms | 112 passed; the 2 failures (iOS setOrientation, Android setNetwork) fail the same way on main on this machine |
CI: all checks green, including e2e ios 55 passed, android 57 passed, ios agent 20 passed (1 flaky: the Gestures double-tap XCTest cannot always deliver, flaky on main too), android agent 20 passed.
Engines in other languages. The experimental Java Android engine and Swift iOS engine (from the closed #1234 and #1236) were ported onto this protocol; the protocol needed one addition for them, client-side session hooks. Mobile benchmark selection, same device, agent-device 0.21.35, two runs each:
| Engine | Android (emulator) | iOS (simulator) |
|---|---|---|
| native over this protocol | 42/42, 140s / 136s | 41/41, 383s / 383s |
mobile() on agent-device | 42/42, 288s / 291s | 41/41, 420s |
-
iOS on one simulator with no other run on it. One more native run hit a web-view tap flake that also fails 2 in 8 runs without this PR; one more agent-device run took 444s with 2 of its own failures.
-
The iOS gap was swipes: XCTest waited for the list to stop coasting after each one (3.1s against agent-device's 0.9s). The native server now skips that wait around scroll swipes only; reads were already 6x faster (92ms against 575ms per
locate). -
The engines themselves stay on local branches; this PR ships only the protocol.
-
Errors: an engine throwing
EngineError,TestError, a plainError, aTypeError, a string, and a retryableNODE_STALEreports the same code, category, and message over the protocol as in process. -
Overhead:
observecosts its JSON round trip: 0.14 ms for 200 nodes, 1.6 ms for 2000, 10-28 ms for a 10000-node tree. Testbed wall time shows no consistent gap against main. -
A real child process: unit tests cover a server in its own process, a crash mid-call that surfaces the server's stderr, and a hand-written JSON-RPC server with no
serveEngine.