feat(engine): engine protocol for out-of-process engines

Merged
OK
1mo ago
+3586-31

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-Length framing, 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, METHODS in packages/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.
  • connectEngine builds an engine from the manifest and hands it to defineEngine, so a manifest is held to every rule an in-process engine is (engine/perform needs actions, keyboard/type needs keyboard/press, ...). The manifest is declared at config load; the server's own must match at initialize. Its JSON Schema ships as schema/engine-manifest-v1.schema.json.
  • The harness holds only protocol clients: ResolvedTarget.engine is an EngineClientHandle, produced by protocolEngine in resolveTargets. One engine always gets the same client, so handle identity checks (MCP ENGINE_IN_USE, hosted workers) still hold.
  • fixtures, validateApp, declared secrets, and session hooks run on the client. A session hook replaces the server's method, for what only the host can do (relaunching an app with simctl).
  • 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/dispose budget is closed by its transport anyway, and a late endAttempt retires only its own attempt.
  • A secret resolves once: the server derives redacted forms (basic-auth header) through attempt/deriveSecret while the value resolves. ResolveSecretOptions.derived may return a promise.
  • Errors keep their stack, Node error code, and causes across the wire. serveEngine always answers lifecycle/startAttempt/endAttempt (and describeEngine lists them), so signals are freed for engines without attempt hooks.
  • Worker connections queue: an MCP session closing while the next one opens calls init again before dispose, so calls go to the newest connection and dispose ends the oldest.
  • Contract change: Engine.dispose runs once init was called, even when it failed. A worker that never initialized opened no connection, so its engine gets no dispose. In the changeset.
  • Docs: one Engines section. Guides: engines (overview), writing-an-engine, engine-servers (new). Reference: reference/engine and reference/engine-protocol (new).

Verified

Ran it locally: yes. Built packages, this branch:

SuiteResult
e2e package (unit + integration)4033 passed
@e2e-dev/web / @e2e-dev/mobile635 / 447 passed
testbed / testbed agent81 / 29 passed
web-benchmark / web-benchmark agent91 / 33 passed
mobile benchmark, both platforms112 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:

EngineAndroid (emulator)iOS (simulator)
native over this protocol42/42, 140s / 136s41/41, 383s / 383s
mobile() on agent-device42/42, 288s / 291s41/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 plain Error, a TypeError, a string, and a retryable NODE_STALE reports the same code, category, and message over the protocol as in process.

  • Overhead: observe costs 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.