> ## Documentation Index
> Fetch the complete documentation index at: https://e2e.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Engine protocol

> Reference for the e2e engine protocol, the JSON-RPC messages the runner uses to drive an engine in another process, on another machine, or in another language.

The engine protocol is the [engine contract](https://e2e.com/docs/reference/engine) as JSON-RPC
messages. The runner drives every engine over it. An
[engine server](#engine-servers) answers it outside the runner's process: in
a child process, on another machine, or on a device, written in any
language.

```ts title="e2e.config.ts"

export default {
  targets: [
    {
      name: 'kiosk',
      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,
      }),
    },
  ],
} satisfies E2EConfig;
```

## Reference

| Function | Returns |
| --- | --- |
| [`connectEngine`](#connectengine) | `EngineHandle` |
| [`serveEngine`](#serveengine) | `Promise<void>` |
| [`describeEngine`](#describeengine) | `EngineManifest` |
| [`spawnTransport`](#spawntransport) | `EngineTransport` |
| [`socketTransport`](#sockettransport) | `EngineTransport` |
| [`loopbackTransport`](#loopbacktransport) | `EngineTransport` |

### `connectEngine`

Turns an engine server into an engine handle, the value a target's `engine`
takes.

```ts
export function connectEngine(options: ConnectEngineOptions): EngineHandle;

export interface ConnectEngineOptions {
  readonly manifest: EngineManifest;
  readonly transport: EngineTransport;
  readonly initializationOptions?: JsonValue;
  readonly secrets?: readonly Secret[];
  readonly fixtures?: Engine['fixtures'];
  readonly validateApp?: Engine['validateApp'];
  readonly resolveLocators?: boolean;
  readonly session?: EngineSession;
}
```

The manifest says what the server answers. The runner reads it at config
load, before any server runs. A server whose manifest differs fails its
first connection, and the error names each field that differs.

In the runner, one connection per target lives from `prepare` to `finish`, and
opens only when the server answers `lifecycle/prepare` or
`lifecycle/finish`. In each worker, one connection lives from `init` to
`dispose`. `e2e doctor` opens one `doctor` connection per target for
`lifecycle/doctor` alone and closes it after. A transport may start the
server for it, as `spawnTransport` does, but must not provision a machine,
lease a device, or install the app. At the doctor signal the server gets 5
seconds to answer before the connection closes. An operation call outside
`init`..`dispose` fails with `INVALID_STATE`.
A worker that never ran `init` has no connection, so the server gets no
`lifecycle/dispose` for it.
Whether two connections share state is the server's choice: a spawned
server gets a process per connection, and a loopback or socket server may
serve them all from one engine.

With `resolveLocators`, for a server without `engine/locate`, each `locate` observes and resolves the expression
with [`resolveExpression`](https://e2e.com/docs/reference/engine#resolveexpression), so a server
only has to produce a tree.
`fixtures`, `validateApp`, `secrets`, and `session` run on the client; see
[Keep host work on the client](#keep-host-work-on-the-client).

<Warning>
`lifecycle/prepare`, `lifecycle/init`, `lifecycle/finish`, and `lifecycle/doctor` carry the
run's environment, model keys and `E2E_SECRET_*` values included, and
`attempt/resolveSecret` answers with plaintext. Connect only to a server you
trust, over a channel you trust: `socketTransport` doesn't encrypt.
</Warning>

| Throws | When |
| --- | --- |
| `INVALID_CONFIG` | The manifest is malformed: an unknown method, a method listed twice, or members `defineEngine` rejects, such as `keyboard/type` without `keyboard/press`, `engine/perform` without `actions`, or `engine/perform` without `engine/observe`. |
| `ENGINE_FAILURE` | The server's manifest differs, or the connection closes mid-call. The message carries the transport's diagnostics, such as the server's last stderr lines. |

### `serveEngine`

Serves an engine built with `defineEngine` over one connection, until the
client sends `exit` or the streams close.

```ts
export function serveEngine(
  engine: EngineHandle | ((initializationOptions: unknown) => EngineHandle | Promise<EngineHandle>),
  streams?: MessageStreams, // default: process.stdin and process.stdout
): Promise<void>;
```

The factory form receives the `initializationOptions` the client sent. See
[Serve a TypeScript engine](#serve-a-typescript-engine) for a server and its
config, and for what to keep off stdout.

### `describeEngine`

Returns the manifest `serveEngine` announces for an engine handle: its
static members, the methods its declared members answer, and
`lifecycle/startAttempt` and `lifecycle/endAttempt`, which `serveEngine`
always answers. Use it to declare the manifest of a TypeScript engine
served elsewhere.

```ts
export function describeEngine(engine: EngineHandle): EngineManifest;
```

### `spawnTransport`

Starts the server as a child process per connection and speaks over its
stdin and stdout.

```ts
export function spawnTransport(options: {
  readonly command: string;
  readonly args?: readonly string[];
  readonly env?: Readonly<Record<string, string>>;
  readonly cwd?: string; // default: the project root
}): EngineTransport;
```

`env` is added to the worker's environment.
Closing a connection ends stdin, sends `SIGTERM`, and sends `SIGKILL` when
the cleanup budget runs out.
When the server's stdout ends, the connection closes. The error names the
exit code or signal and the last stderr lines.

### `socketTransport`

Connects to a server that already listens on a TCP port, on this machine
or another. Closing a connection ends the socket; the server keeps running.

```ts
export function socketTransport(options: { readonly host: string; readonly port: number }): EngineTransport;
```

### `loopbackTransport`

Serves an engine in this process over in-memory streams. Every call still
crosses the protocol, serialized as on any other transport.

```ts
export function loopbackTransport(engine: EngineHandle): EngineTransport;
```

## Engine servers

An engine server is anything that answers the [messages](#messages) below.
[`connectEngine`](#connectengine) turns one into an engine handle.

### Keep host work on the client

Some members hold functions, or need the machine the runner is on. Pass
them to `connectEngine`; they run in the runner's process:

| Option | Runs on the client |
| --- | --- |
| `fixtures` | Fixture factories, such as a `device` fixture. They reach the server through the engine's members. |
| `validateApp` | The app check at config load. |
| `secrets` | The `secrets.get()` handles the server may resolve. |
| `session` | Session hooks that replace the server's, such as relaunching an app with `simctl`. |

```ts
const engine = connectEngine({
  manifest,
  transport,
  session: { restart: (operation) => relaunch(operation) },
  fixtures: {
    device: (context) => context.fixture('device', { back: () => engine.session!.back!(context.operation()) }, { back: { kind: 'resource' } }),
  },
});
```

### Serve a TypeScript engine

[`serveEngine`](#serveengine) answers the protocol with a `defineEngine`
engine. Run it as the command of a `spawnTransport`, and declare its
manifest with [`describeEngine`](#describeengine).

```ts title="server.ts"

await serveEngine((options) => kiosk(options as { host: string }));
```

```ts title="e2e.config.ts"

export default {
  targets: [
    {
      name: 'kiosk',
      engine: connectEngine({
        manifest: describeEngine(kiosk({ host: 'localhost' })),
        transport: spawnTransport({ command: 'node', args: ['server.ts'] }),
        initializationOptions: { host: 'localhost' },
      }),
    },
  ],
} satisfies E2EConfig;
```

Keep the server's own logging on stderr, since stdout carries the protocol.
Make sure child processes the server spawns don't inherit stdout, or
requests hang waiting for them to exit. When the server exits unexpectedly
during a request, the error
[names the exit code or signal and the last stderr lines](#spawntransport).

### Write a server in another language

A server reads and writes the [base protocol](#messages) and answers:

1. [`initialize`](#lifecycle), with `{ protocolVersion: 1, manifest }`.
   Refuse any other protocol version.
2. Each [method](#engine-methods) its [manifest](#manifest) lists, such as
   `engine/observe`.
3. `shutdown`, then the `exit` notification, after which the server stops.

Ignore notifications you don't know. Report a failure as an
[error](#errors) the runner can classify as a local engine's.

### Write a transport

Write a transport when a server needs setup first, such as an app installed
on a device and a port forwarded to it. `open` receives the target's app,
because a server elsewhere can't read the project's files. A `doctor`
connection, which `e2e doctor` opens, only checks: skip the install there.
[Types](#types) has the interfaces.

```ts

export function deviceTransport(serial: string): EngineTransport {
  return {
    label: `device ${serial}`,
    async open(context) {
      if (context.role !== 'doctor' && context.app?.appPath !== undefined) {
        await install(serial, context.app.appPath);
      }
      const socket = await forward(serial, context.signal);
      return { input: socket, output: socket, close: async () => void socket.destroy() };
    },
  };
}
```

## Messages

The base protocol is the
[Language Server Protocol's](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#baseProtocol):
JSON-RPC 2.0 messages in UTF-8, each after a `Content-Length` header and a
blank line. Either side may send requests and notifications.

### Lifecycle

A connection opens and closes as an LSP connection does.

| Message | Direction | Params | Result |
| --- | --- | --- | --- |
| `initialize` | request, client to server | `{ protocolVersion: 1, clientInfo, role, initializationOptions? }` | `{ protocolVersion: 1, manifest }` |
| `initialized` | notification, client to server | `{}` | - |
| `shutdown` | request, client to server | - | `null` |
| `exit` | notification, client to server | - | - |

Each side refuses any `protocolVersion` but `1`. After `shutdown`, it leaves
its signals as they are. When a connection closes without `shutdown`, it
aborts every signal it holds.

### Manifest

Its JSON Schema ships in the `e2e` package as
`schema/engine-manifest-v1.schema.json`.

```ts
export interface EngineManifest {
  readonly name: string;
  readonly version: string;
  readonly spiVersion: 1;
  readonly platform?: string;
  readonly workers?: number;
  readonly methods: readonly EngineMethod[];
  readonly actions?: readonly LocatorActionKind[];
  readonly pointerActions?: readonly PointerActionKind[];
  readonly tapModifiers?: boolean;
  readonly selectsDevice?: boolean;
  readonly grantsAllPermissions?: boolean;
  readonly launchArgumentValues?: boolean;
  readonly fileApps?: boolean;
  readonly isolation?: 'worker';
}
```

| Field | Description |
| --- | --- |
| `methods` | The engine methods the server answers. One left out is a capability the engine lacks. |
| `actions` | Required with `engine/perform`. |
| `pointerActions` | Required with `engine/performAt`. |
| `selectsDevice` | Requires `lifecycle/prepare`. |
| `isolation` | Left out for `'attempt'`. |

The other members mean what they do on
[`Engine`](https://e2e.com/docs/reference/engine#capabilities).

### Engine methods

Each method is one engine member, a request from client to server. Its
params are the member's arguments by name, with every `OperationContext` as
`operation`. `ENGINE_METHODS` lists them.

| Method | Params | Result |
| --- | --- | --- |
| `engine/observe` | `{ operation, options? }` | `EngineSnapshot` |
| `engine/readStyle` | `{ ref, properties, operation }` | `ComputedStyleValues` |
| `engine/viewportIntersection` | `{ ref, operation }` | `ViewportIntersection`; leave `ratio` out, never `null`, when the platform cannot measure it |
| `engine/locate` | `{ expression, operation }` | `SemanticNode[]` |
| `engine/perform` | `{ ref, action, operation }` | `null` |
| `engine/performAt` | `{ point, action, operation }` | `null` |
| `keyboard/type` | `{ text, options: { replace }, operation }` | `null` |
| `keyboard/press` | `{ key, operation }` | `null` |
| `keyboard/dismiss` | `{ operation }` | `null` |
| `state/capture` | `{ operation }` | `EngineState` |
| `state/restore` | `{ state, operation }` | `null` |
| `artifacts/screenshot` | `{ label?, operation }` | `{ path }` or `{ data, mediaType }` |
| `artifacts/startVideo` | `{ operation }` | `null` |
| `artifacts/stopVideo` | `{ operation }` | `VideoSegment[]` |
| `session/open` | `{ url, operation }` | `null` |
| `session/openLink` | `{ url, operation }` | `null` |
| `session/back`, `session/restart`, `session/reset` | `{ operation }` | `null` |
| `session/resize` | `{ size: { width, height }, operation }` | `null` |
| `session/switchTab` | `{ index, operation }` | `null` |
| `session/closeTab` | `{ index, operation }` | `null` |
| `lifecycle/prepare` | `EnginePrepareInfo` without `log` | `EnginePrepareResult` or `null` |
| `lifecycle/finish` | `EngineFinishInfo` without `log` | `null` |
| `lifecycle/doctor` | `EngineDoctorInfo` without `log` | `EngineDoctorCheck[]` |
| `lifecycle/init` | `EngineInitInfo` without `log` | `null` |
| `lifecycle/startAttempt` | `{ attemptId, artifactsDir, signal, screens }` | `null` |
| `lifecycle/settleAttempt`, `lifecycle/endAttempt` | `{ signal, timeoutMs }` | `null` |
| `lifecycle/dispose` | `{ signal, timeoutMs, reuseSignal? }` | `null` |

`artifacts/screenshot` returns `{ data, mediaType: 'image/png' }`, the image
in base64, when the server cannot write the attempt's artifact directory,
such as a server on a device. The client writes it under `screenshots/`.
Bytes that arrive once `lifecycle/endAttempt` has been sent for the attempt
that asked, or after that attempt has ended, are dropped, and the call fails
with `INVALID_STATE`.
Observation pixels cross the same way: `pixels.data` is base64. File payload buffers also cross as base64 in `engine/perform`; engines receive them as `Uint8Array` bytes.

`screens` in `lifecycle/startAttempt` says whether the attempt keeps a
trace, which is when the server may send `attempt/screen`.

### Server to client

What an engine calls back into the harness crosses as server-to-client
messages. Each `attempt/*` message carries the `attemptId` it belongs to;
the client drops one for an attempt that already ended.

| Message | Kind | Params |
| --- | --- | --- |
| `engine/log` | notification | `{ line }`, from `prepare`, `init`, or `finish` |
| `attempt/appLog` | notification | `{ attemptId, entry }` |
| `attempt/navigation` | notification | `{ attemptId, line }` |
| `attempt/environment` | notification | `{ attemptId, facts }` |
| `attempt/screen` | notification | `{ attemptId, snapshot }` |
| `attempt/resolveSecret` | request | `{ attemptId, name, derivation? }`, result `{ value }` |

With `derivation`, the client asks back with `attempt/deriveSecret`
`{ derivation, plaintext }` before it answers. The server returns
`{ values }`, the forms the engine derives from the value, such as a
basic-auth header, and the client redacts them under the secret's name.

### Signals

An `AbortSignal` crosses as `{ id, aborted?, reason? }`. The client mints
one id per signal and sends `$/abort` with `{ id, reason? }` when it aborts.
A signal already aborted when it is sent carries `aborted: true`. `reason`
is the abort reason as a JSON-RPC error object, encoded like a failed
method's [error](#errors), when the harness aborted with an error of its
own, such as a test timeout. A server aborts its signal with that error, so
an engine that rethrows `signal.reason` raises what it would in process.
Without `reason`, the signal aborts with the platform's `AbortError`. A
server forgets an id once its signal aborts, and forgets every id first
seen during an attempt once `lifecycle/endAttempt` settles. An id sent again
after that is a new signal.

### Errors

A failed engine method answers with JSON-RPC error code `-32000` and the
e2e error in `data`, so the harness classifies a remote failure exactly as
a local one:

| `data` | The engine threw |
| --- | --- |
| `{ type: 'engine', code, retryable }` | An [`EngineError`](https://e2e.com/docs/reference/engine#engineerror). Only `NODE_STALE` and `FRAME_NOT_FOUND` may be retryable. |
| `{ type: 'e2e', category, code, retryable, details? }` | A runner error, such as `TestError` or `ConfigurationError`. |

Any other error code carries the message alone. The harness treats it as
an infrastructure failure, as it treats a plain `Error` from a local
engine. Every error's `data` may also carry `stack`, `errno` (a Node error
code such as `ECONNREFUSED`), and `cause`, an error object of the same
shape.

## Types

A transport opens one connection's byte streams. See
[Write a transport](#write-a-transport).

```ts
export interface EngineTransport {
  readonly label: string;
  open(context: TransportContext): Promise<TransportConnection>;
}

export interface TransportContext {
  readonly role: 'run' | 'worker' | 'doctor';
  readonly env: Readonly<Record<string, string | undefined>>;
  readonly cwd: string;
  readonly signal: AbortSignal;
  readonly app?: EngineAppInfo;
}

export interface TransportConnection {
  readonly input: Readable;
  readonly output: Writable;
  close(timeoutMs: number): Promise<void>;
  diagnostics?(): string;
}
```

| Field | Description |
| --- | --- |
| `label` | Names the transport in error messages: the command, or the address. |
| `cwd` | The project root. |
| `app` | The target's app, from `prepare`, `init`, or `doctor`. |
| `close` | Releases the process or socket, within `timeoutMs`. |
| `diagnostics` | Says what went wrong, for an error message. |

## Related

[Engines](https://e2e.com/docs/engines) compares the ways to build one.
[`e2e/engine`](https://e2e.com/docs/reference/engine) is the contract each message carries.
[Writing an engine](https://e2e.com/docs/writing-an-engine) walks through building one with
`defineEngine`.
