Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/internal/flows/device-exec.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ Runs the command on the machine that owns the device and streams its output back
another device (ADR 0047), see "Resolution per platform".
5. Env — `execBaseEnvironment(process.env)` (`src/daemon/exec-env.ts`, the allowlisted
keys, read once at start) + the command's `env`, minus its `unsetEnv`.
6. Spawn — `ProcessRunner.spawnStreaming`, pipes only. `stdin` is written once,
then closed.
6. Spawn — `ProcessRunner.spawnStreaming`, pipes only. `stdin` (at most 8 MiB
of characters, refused `BAD_REQUEST` by the contract before any dispatcher
forwards or spawns) is written once, then closed.
7. `started` push the moment the process exists, then one `output` push per chunk
(`{ requestId, stream, chunk }`). The push write is awaited before the child is
read again: a slow reader slows the command. Nothing is buffered; no size cap.
Expand Down
5 changes: 4 additions & 1 deletion src/contract/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@ export const driverPassthrough = defineOperation({
output: passthroughCommandSchema,
});

// why: the daemon parses a request as one string, so unbounded stdin exhausts the heap (#1242).
export const EXEC_STDIN_MAX_LENGTH = 8_388_608;

/**
* Runs `simctl`, `adb` or `devicectl` **on the machine that owns the device** and streams the output back
* (ADR 0005 §19a). The difference from `driver.passthrough` is where the process runs, not
Expand Down Expand Up @@ -468,7 +471,7 @@ export const deviceExec = defineOperation({
leaseId: z.string(),
tool: z.string().min(1),
args: z.array(z.string()),
stdin: z.string().optional(),
stdin: z.string().max(EXEC_STDIN_MAX_LENGTH).optional(),
/**
* why: Who this command is being run *for* -- the requester the lease is attributed to (ADR
* 0003 §4), not the connection's principal. Read **only on an admin session**, where it
Expand Down
34 changes: 34 additions & 0 deletions src/gateway/dispatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2500,6 +2500,40 @@ describe("GatewayDispatcher", () => {
).rejects.toMatchObject({ code: "FORBIDDEN" });
});

it("refuses a device.exec whose stdin is longer than 8,388,608 characters with BAD_REQUEST and forwards nothing to the worker, and forwards one at 8,388,608", async () => {
const { coordinator, directory, dispatcher, workers } = harness();
const client = new ScriptedWorkerClient();
directory.add("wrk_1", client);
workers.connected("wrk_1", undefined, "0.3.0");
workers.refresh("wrk_1", {
capacity: statusFixture().capacity,
health: "running",
queueDepth: 0,
catalog: catalogFixture([{ models: ["iPhone 17"], platform: "ios", runtimes: ["26.0"] }])
.platforms,
downloads: { policy: "on-request" },
});
client.requestLeaseQueue.push({ grant: grantFixture(), kind: "grant" });
const grant = await coordinator.request(
{ model: "iPhone 17", platform: "ios" },
{ allowDownload: false, noWait: true, ownerId: "agent-1", requesterId: "agent-1" },
);
const exec = (stdin: string) =>
dispatcher.dispatch(
"device.exec",
{ args: ["spawn", "booted", "untar"], leaseId: grant.lease.id, stdin, tool: "simctl" },
session({ principal: "agent-1", role: "agent" }),
);
const forwarded = () => client.calls.filter((call) => call.startsWith("device.exec:"));

client.execQueue.push({ exitCode: 0, kind: "ok" });
await expect(exec("A".repeat(8_388_608))).resolves.toEqual({ exitCode: 0 });
expect(forwarded()).toHaveLength(1);

await expect(exec("A".repeat(8_388_609))).rejects.toMatchObject({ code: "BAD_REQUEST" });
expect(forwarded()).toHaveLength(1);
});

it("calls onStarted for a silent device.exec from the worker's own started push (ADR §19a/§19b/§19e)", async () => {
const { coordinator, directory, dispatcher, workers } = harness();
const client = new ScriptedWorkerClient();
Expand Down
1 change: 1 addition & 0 deletions src/http/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1981,6 +1981,7 @@ describe("lease routes", () => {
{ args: [], tool: "simctl", unexpected: true },
{ args: [], requesterId: "a\u001b[2Jb", tool: "simctl" },
{ args: [], requesterId: "x".repeat(257), tool: "simctl" },
{ args: [], stdin: "A".repeat(8_388_609), tool: "simctl" },
]) {
const response = await postExec(app, body);
expect(response.status).toBe(400);
Expand Down
2 changes: 2 additions & 0 deletions src/http/lease-declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
deviceRecordSchema,
baselineNameSchema,
imageTagSchema,
EXEC_STDIN_MAX_LENGTH,
INPUT_ORIENTATIONS,
leaseIdSchema,
OPERATIONS,
Expand Down Expand Up @@ -137,6 +138,7 @@ const execBodySchema = z
args: z.array(z.string()),
stdin: z
.string()
.max(EXEC_STDIN_MAX_LENGTH)
.nullish()
.transform((value) => value ?? undefined),
requesterId: requesterIdSchema.nullish().transform((value) => value ?? undefined),
Expand Down
4 changes: 2 additions & 2 deletions src/source-hygiene.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ const SHRINK_ONLY: Record<string, number> = {
"src/daemon/server.ts": 1850,
"src/drivers/ios/index.test.ts": 5950,
"src/drivers/ios/index.ts": 2700,
"src/gateway/dispatcher.test.ts": 3023,
"src/gateway/dispatcher.test.ts": 3057,
"src/gateway/fleet-coordinator.test.ts": 5283,
"src/gateway/fleet-coordinator.ts": 1659,
"src/http/app.test.ts": 3487,
"src/http/app.test.ts": 3488,
"src/leasing/create-leasing.test.ts": 5276,
"src/leasing/lease-acquisition-coordinator.test.ts": 3691,
"src/leasing/lease-acquisition-coordinator.ts": 1622,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ The server starts the daemon if it is not running. It renews the agent's lease w
| `lease_simulator` | Leases one simulator or emulator for this session. Takes `platform` and, optionally, `model` or `class`, `osVersion`, `ttlMs`, `noWait`, `timeoutMs` and `allowDownload`, the same options as `simlock lease`. Returns the same lease the CLI prints. |
| `release_simulator` | Releases this session's lease. |
| `lease_status` | Reports this session's lease, or `held: false` when it has none. Safe to call any time, for example after the agent's context was compacted. |
| `run_on_device` | Runs one `simctl`, `adb` or `devicectl` command on this session's device. Takes `tool`, `args` and, optionally, `stdin`. |
| `run_on_device` | Runs one `simctl`, `adb` or `devicectl` command on this session's device. Takes `tool`, `args` and, optionally, `stdin` (at most 8,388,608 characters; a longer one fails the tool's input validation, before Simlock sees it). |
| `rotate_device` | Turns this session's iOS simulator to `portrait`, `portrait-upside-down`, `landscape-left` or `landscape-right`. |

To install and launch an app, the agent calls `run_on_device`, for example `{"tool":"simctl","args":["install","/builds/App.app"]}`. Things to know:
Expand Down
4 changes: 2 additions & 2 deletions website/src/content/docs/docs/reference/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ simlock simctl [--lease <lease-id>] [--stdin] <simctl-args...>
| Flag | Meaning | Default |
| --- | --- | --- |
| `--lease <lease-id>` | Through a gateway: the lease whose device the command runs against. Must come before the simctl arguments. On a worker it is accepted and ignored. | Through a gateway: your only lease |
| `--stdin` | Through a gateway: read stdin to its end (Ctrl-D at a terminal) and send it to the command. Must come before the simctl arguments, in either order with `--lease`. On a worker it is accepted and ignored. | Off: the command gets empty input and stdin is never read |
| `--stdin` | Through a gateway: read stdin to its end (Ctrl-D at a terminal) and send it to the command, at most 8,388,608 characters; more is refused with `BAD_REQUEST`. Must come before the simctl arguments, in either order with `--lease`. On a worker it is accepted and ignored. | Off: the command gets empty input and stdin is never read |
| `<simctl-args...>` | Passed to `xcrun simctl` as written. | — |

Every other argument goes to `simctl`, including `--help`. On a worker the command runs on this machine with your terminal. Through a gateway it runs on the worker that holds the lease, its output is streamed back. Stdin is sent only with `--stdin`; without it the command gets empty input and a pipe that never closes does not hold it up. The command exits with `simctl`'s own exit code; a Simlock failure also writes a `{"error":...}` line to stderr. Through a gateway the worker puts the lease's UDID into a command that leaves the device out, so write `simlock --gateway simctl install ./MyApp.app`. Simlock refuses `create`, `erase`, `delete`, `shutdown all`, `runtime delete`, a caller-supplied `--set` or `--profiles`, and (through a gateway) a command that names another device such as `booted`, with `PASSTHROUGH_REFUSED` (exit 2). Use `simlock release` and `simlock cleanup` to give devices back.
Expand All @@ -187,7 +187,7 @@ simlock adb [--lease <lease-id>] [--stdin] <adb-args...>
| Flag | Meaning | Default |
| --- | --- | --- |
| `--lease <lease-id>` | Through a gateway: the lease whose device the command runs against. Must come before the adb arguments. On a worker it is accepted and ignored. | Through a gateway: your only lease |
| `--stdin` | Through a gateway: read stdin to its end (Ctrl-D at a terminal) and send it to the command. Must come before the adb arguments, in either order with `--lease`. On a worker it is accepted and ignored. | Off: the command gets empty input and stdin is never read |
| `--stdin` | Through a gateway: read stdin to its end (Ctrl-D at a terminal) and send it to the command, at most 8,388,608 characters; more is refused with `BAD_REQUEST`. Must come before the adb arguments, in either order with `--lease`. On a worker it is accepted and ignored. | Off: the command gets empty input and stdin is never read |
| `<adb-args...>` | Passed to `adb` as written. | — |

The command exits with `adb`'s own exit code. Through a gateway, stdin is sent only with `--stdin`; without it the command gets empty input. Simlock refuses `adb kill-server`, `adb disconnect`, flags that move adb to a different server, and (through a gateway) `adb shell` with no command, with `PASSTHROUGH_REFUSED` (exit 2). `-s`, `-t`, `-d` and `-e` are allowed before the subcommand. Through a gateway the worker adds `-s <serial>` for an emulator, and refuses `-s` with another serial, `-e`, `-d` and `-t`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Run it with a TypeScript runner, for example `npx tsx lease-and-launch.mts`.
| `requestLease(request, { onProgress, signal })` | Waits for a device and resolves with the grant: `device`, `environment`, `lease` and `timing`, the same fields `simlock lease` prints. |
| `renewLease({ leaseId, ttlMs? })` | Moves the deadline to now plus the TTL. Resolves with the lease, including the new `ttlDeadline`. |
| `releaseLease({ leaseId })` | Ends the lease. A simulator or emulator is then deleted; a physical device is cleaned. |
| `exec({ leaseId, tool, args, stdin? }, { onOutput })` | Runs `simctl` or `adb` (or `devicectl` on a physical iPhone or iPad) on the device, and resolves with `{ exitCode }`. |
| `exec({ leaseId, tool, args, stdin? }, { onOutput })` | Runs `simctl` or `adb` (or `devicectl` on a physical iPhone or iPad) on the device, and resolves with `{ exitCode }`. `stdin` holds at most 8,388,608 characters. |
| `rotate(leaseId, orientation)` | Turns an iPhone or iPad simulator. See [Rotate without a socket](/simlock/docs/using-devices/watching-the-screen#rotate-without-a-socket). |
| `listLeases()` | Your leases. |
| `getCatalog({ platform? })` | What you can ask for, as `simlock catalog --json` prints it. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ The command runs on the worker, as the user who runs the worker's daemon. Treat
- **Leave the device out.** Simlock adds the lease's device to the command: the UDID for `simctl` (where the subcommand takes one), `-s <serial>` for `adb`, and `--device <UDID>` for `devicectl`. Write `["install","/builds/MyApp.app"]`, not a UDID or serial.
- **Naming another device is refused.** A command that names a different device, such as `booted`, another UDID, `adb -s emulator-5556`, `-e`, `-d` or `-t`, is refused with `422 PASSTHROUGH_REFUSED` before anything runs. Naming the lease's own device is accepted, except on a physical Android phone, which refuses any option before the subcommand, even its own `-s <serial>`.
- **Paths are on the worker.** `{"tool":"simctl","args":["install","/builds/MyApp.app"]}` needs `/builds/MyApp.app` to exist on the worker. The API can't upload files. Get the file there another way, or use [agent-device](/simlock/docs/using-devices/agent-device), which uploads apps for you.
- **No terminal.** `stdin` in the body is sent to the command once. Interactive commands, such as a bare `adb shell`, are refused.
- **No terminal.** `stdin` in the body is sent to the command once, and may hold at most 8,388,608 characters; a longer one is refused with `BAD_REQUEST`. Interactive commands, such as a bare `adb shell`, are refused.
- **Output is text.** Output is decoded as UTF-8, so binary output, such as a screenshot written to stdout, gets corrupted. Write it to a file on the worker instead.
- **Time limit.** A command that runs longer than 10 minutes is stopped, and the stream ends with an `error` event carrying `EXEC_TIMEOUT`. Disconnecting doesn't stop the command.
- **The same commands are refused as locally.** Everything `simlock simctl` and `simlock adb` refuse (see [Commands Simlock refuses](/simlock/docs/using-devices/working-with-a-device#commands-simlock-refuses)) is refused here with `422 PASSTHROUGH_REFUSED`, before anything runs. So is `simctl` on an Android lease, and `adb` on an iOS one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ simlock --gateway simctl --lease 3f81a2c4-9b7d-4e21-8a55-1c0e6f2d7b93.lse_9f2c6a
- `--lease <lease-id>` says which lease the command runs under. Put it right after `simctl` or `adb`. You can leave it out when you hold exactly one lease. Without a gateway, it is accepted and ignored.
- File paths in the arguments are paths on the worker, not on your machine. `simctl install` needs the app to be on the worker already.
- Commands that need a terminal, such as a bare `adb shell`, are refused.
- The command gets no input unless you pass `--stdin`. With `--stdin`, Simlock reads everything you pipe in (or type, up to Ctrl-D) and sends it once. Put `--stdin` before the command's own arguments, next to `--lease`.
- The command gets no input unless you pass `--stdin`. With `--stdin`, Simlock reads everything you pipe in (or type, up to Ctrl-D) and sends it once, up to 8,388,608 characters; more is refused with `BAD_REQUEST`. Put `--stdin` before the command's own arguments, next to `--lease`.

<Aside type="caution" title="Right after a worker restarts">
For up to about a minute after a worker restarts, a command without `--lease` can fail with `No lease is held by <your id>`, even though your lease still stands. Don't request a new device on that message. Wait a minute and run the command again, or pass `--lease <lease-id>`: then the command waits for the worker to finish starting and runs.
Expand Down
Loading