Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
3502a34
feat(contract,daemon,client,cli,http): device.exec runs a command on …
claude Sep 6, 2026
5c516cb
test: cover device.exec across the contract, both transports, and the…
claude Sep 6, 2026
2be8ee9
docs: device.exec, its SSE route, and exec.timeoutMs
claude Sep 6, 2026
65facd9
docs(known-pitfalls): a device.exec command is authorized once, at it…
claude Sep 6, 2026
567f61a
feat(contract,daemon,cli,http): settle device.exec's authorization, s…
claude Sep 6, 2026
0869f35
fix(http,android,contract,cli): close two exec defects and land the s…
claude Sep 6, 2026
09a942a
refactor(contract,cli,config): mode lives in status.get's daemon block
claude Sep 6, 2026
73f89f1
test(e2e): read health from the daemon block
claude Sep 6, 2026
173f275
fix(android): refuse adb server-scope flags by position, not by subst…
claude Sep 6, 2026
5fe0ccd
fix(ports,daemon,http): carry exec backpressure end to end
claude Sep 6, 2026
1798096
fix(cli): relabel both refusal codes, and stop over-filtering a lease…
claude Sep 6, 2026
2f3422a
test(daemon,core): cover the exec timeout race and the two new config…
claude Sep 6, 2026
3a6a2ae
docs: say what the code does about exec refusals, streaming and modes
claude Sep 6, 2026
1d1d165
fix(android): fail closed on unrecognized adb globals (round 3, F1)
claude Sep 7, 2026
ab3b5f3
fix(ports,http): stop settling device.exec while output delivery is o…
claude Sep 7, 2026
0a67a11
fix(core): reject a gateway with http disabled; document gateway's re…
claude Sep 7, 2026
98e4d16
fix(daemon,drivers): report EXEC_TIMEOUT over a stalled consumer; clo…
claude Sep 7, 2026
0fd5b8a
fix(http,contract): correct exec's operator role doc, move requesterI…
claude Sep 7, 2026
1a9fc00
fix(ports,http): per-stream resume, sync-throw safety, and an already…
claude Sep 7, 2026
6e5dae3
docs: fix the mode/gateway contradiction, spell out an agent token's …
claude Sep 7, 2026
3dcb698
test: fix remaining round-4 test-title mismatches (items 2, 3, 5, 6)
claude Sep 7, 2026
15fab9b
docs: drop the duplicated device.exec pitfalls the stack collapse left
claude Sep 7, 2026
2982358
fix(e2e): stop the fake driver mirroring the bare-shell bug it models
claude Sep 7, 2026
5cf87a5
docs: merge the duplicated device.exec section left by the stack coll…
claude Sep 8, 2026
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
61 changes: 59 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ specification those changes are written against.

### ⚠ BREAKING CHANGES

- **The daemon protocol is now 5**, `{min: 5, max: 5}`, with no compatibility
shim — the same no-shim rule that took it to 4. ADR 0005 adds `device.exec`
and its `output` push family, and moves `status.get`'s `health` into a
`daemon` block that also carries `mode`, so
advertising 4 would claim a compatibility path that does not exist. A client
built against 4 does not overlap this daemon and its `hello` fails with
`PROTOCOL_VERSION_UNSUPPORTED` naming both ranges; `daemon.stop` stays the
frozen exception, so `simlock daemon stop` then starting the new daemon is
the upgrade path.
- **Contract:** `lease.heartbeat` is removed as an operation, and `heartbeat`
is removed as a `hello` capability. The daemon never pushes to a client to
prove liveness any more; nothing replaces it, because `lease.renew` already
Expand Down Expand Up @@ -67,8 +76,9 @@ specification those changes are written against.
reading `lastHeartbeatAt` gets `undefined`.
- **Socket protocol:** moves twice in this release, once per ADR, each time
with no compatibility shim. ADR 0004 removes `lease.heartbeat` and `mode`,
taking the wire to protocol 4; ADR 0005 adds `device.exec` and its `output`
push family, taking it to 5. **What ships advertises `{min: 5, max: 5}`** —
taking the wire to protocol 4; ADR 0005 adds `device.exec`, its `output`
push family, and the `mode` field on `status.get`'s daemon block, taking it
to 5. **What ships advertises `{min: 5, max: 5}`** —
4 is a step along the way, not a range anything releases with, since a
range widens only where a compatibility path is kept (ADR 0003 §6). A
version-mismatched client fails `hello` with `PROTOCOL_VERSION_UNSUPPORTED`
Expand Down Expand Up @@ -159,6 +169,53 @@ those changes add, alongside the breaking changes above.
`StartupConverger` orphan sweep that existed to clean up after it.
- **config:** `lease.maxTtlMs` bounds what any caller may ask for, so one
client cannot pin a device for a day by naming a large `ttlMs`.
- **contract, daemon, http, cli:** `device.exec` runs `simctl`/`adb` **on the
machine that owns the device** and streams the output back, so an agent that
is not on that machine can drive the device it leased
([ADR 0005](docs/adr/0005-gateway-and-worker-modes.md) §19a-§19e). The daemon
resolves the command through the same driver passthrough `simlock simctl` /
`simlock adb` already use — the same root scoping, the same refusal list —
and pushes each chunk as it arrives (a new request-scoped `output` push
family) rather than buffering any of it; the call resolves with the
command's exit code. Over HTTP it is `POST /v1/leases/{id}/exec`, answering
Server-Sent Events: an `output` event per chunk, then a terminal `exit` or
`error`. `simlock/client` gains `exec(input, { onOutput })`.
`simlock simctl` / `simlock adb` are unchanged against a `worker` — they
still spawn with inherited stdio, so an interactive `adb shell` keeps its
terminal — and take this path against a `gateway`, which owns no devices;
the CLI reads which it is talking to from `mode` in `status.get`'s daemon
block rather than guessing from its transport, and both commands now accept
`--lease <id>` (ignored against a worker, so one command line works against
either). `stdin` is a one-shot string, read from a pipe to EOF before the
command starts: there is no pseudo-terminal, so line-oriented commands work,
full-screen ones do not, and a bare `adb shell` is refused
(`PASSTHROUGH_REFUSED`) rather than left to hang. On this one operation
**`admin` does not bypass ownership**: an admin session names the
`requesterId` it is running the command for and the daemon compares it to
the lease's own, which is what keeps a proxy holding one admin credential
from reaching every lease on the machine.
- **contract:** `status.get` gains a `daemon` block, `{ health, mode }`.
`health` moves into it from the top level, and `mode` (`worker`/`gateway`,
read straight from the new `mode` config key) joins it — the one field that
tells a client which kind of daemon answered, and what `simlock simctl` /
`simlock adb` branch on. `simlock status` renders it as
`Daemon: running (worker)`.
- **android:** `simlock adb` now refuses a caller-supplied `-P`, `-H`, `-L`,
or `--server-port` anywhere in adb's globals — the arguments before the
subcommand — including the attached forms `-P5037`/`-Hhost` and one that
follows another global's value (`-s emulator-5554 -P 5037 shell …`), the way
`simlock simctl` already refuses `--set`/`--profiles`. `adb` takes the
_last_ `-P` on the line, so one supplied by a caller silently won over the
one Simlock inserts and pointed the command at another server — including
the machine's default one, outside Simlock's containment entirely. Past the
subcommand those spellings are operands and still pass through
(`simlock adb shell echo -Please`), and `-s`/`-t`/`-d`/`-e` are not refused
at all: they select a device inside the containment rather than leaving it.
- **config:** `exec.timeoutMs` (default ten minutes) bounds one `device.exec`
command; past it the process is killed and the call fails with the new
`EXEC_TIMEOUT` error code (CLI exit `10`, the code the other "ran out of
time" outcome already uses, and HTTP `504`) rather than reporting the exit
code the kill produced.

### ADR 0005: gateway and worker modes

Expand Down
29 changes: 27 additions & 2 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ applies to HTTP automatically because there is only one code path to fix.
a range widens only when a compatibility path is actually kept (ADR 0003 §6).
Two changes have moved it since. ADR 0004 removed `lease.heartbeat` and
`mode` from the contract with no shim behind them, taking the wire to
protocol 4; ADR 0005 adds `device.exec` and its `output` push family, again
with no compatibility path kept, taking it to 5. So the range both sides
protocol 4; ADR 0005 adds `device.exec`, its `output` push family, and a
`mode` field `status.get` now always carries, again with no compatibility
path kept, taking it to 5. So the range both sides
advertise is `{min: 5, max: 5}`, an older client and a current daemon simply
do not overlap, and `hello` fails with `PROTOCOL_VERSION_UNSUPPORTED` naming
both ranges. The same negotiation runs over a worker's uplink, which is why a
Expand Down Expand Up @@ -693,6 +694,30 @@ by the migration, which `doctor` reports and `--fix` destroys through the old
unscoped path — permitted because a registry record names it, which is what
registry-only destruction asks for.

Containment cuts both ways, so the scoping has to be handed back to a lease
holder that needs to drive its device. That happens two ways, and both go
through the same driver code (`Driver.passthrough()`, which owns the scoping
flags *and* the list of verbs it will not proxy -- `simctl delete`,
`adb kill-server`, anything that would change a device's lifecycle behind the
registry's back). `driver.passthrough` *resolves* the scoped command and hands
it back for the caller to run, which is what `simlock simctl` / `simlock adb`
do locally: the daemon is the process that knows the root, the CLI is the one
with a terminal, so an interactive `adb shell` keeps its tty and its exit code.
`device.exec` (ADR 0005) *runs* the same resolved command on the daemon's own
machine through the `ProcessRunner` port and streams stdout/stderr back as
request-scoped pushes, resolving with the exit code -- for a caller who is not
on that machine and for whom a command line naming a device set would be
useless. One resolution, one refusal list, two ways to reach it; the split is
who spawns the process, never what is allowed.

The one thing the resolution is told about its caller is whether there is a
terminal behind it, because that is the one thing that genuinely differs: an
exec'd command runs on pipes, so a driver may refuse there what it allows a
local invocation with a tty (a bare `adb shell`). The fact travels to the
driver rather than being decided in the daemon, for the same reason the rest of
the list lives there -- knowing which of adb's commands needs a terminal is
Android's business, not the core's.

Ownership is proven when the driver starts, and re-proven
(`Driver.revalidateRoot()`) immediately before `doctor --purge-orphans`
destroys anything in a root: reporting can live with a proof taken days ago,
Expand Down
43 changes: 36 additions & 7 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,14 @@ Refused, all exit 2 with `USAGE` and a message naming what to run instead:
ends as `lease_lost`. `shutdown <udid>` of a single device is allowed.
- `runtime delete` — it deletes a runtime shared with Xcode, and Simlock will
not download one back. Delete it through Xcode if that is what you mean.
- `--set` and `--profiles`, in any spelling — `simlock simctl` supplies the
device set itself. A caller-supplied one would point simctl outside what
Simlock manages, and (because their value is a separate argument) would let
a refused verb read as an ordinary operand. Run `xcrun simctl` directly if
you mean to leave Simlock's set.
- `--set` and `--profiles`, wherever they appear *before* the subcommand and
however they are spelled (`-set`, `--set <path>`, `--set=<path>`) —
`simlock simctl` supplies the device set itself. A caller-supplied one would
point simctl outside what Simlock manages, and (because their value is a
separate argument) would let a refused verb read as an ordinary operand. Run
`xcrun simctl` directly if you mean to leave Simlock's set. Past the
subcommand they are that subcommand's own operands and are left alone —
`simlock simctl spawn booted foo --set x` is passing `--set x` to `foo`.

Against a **gateway**, the device is on another machine, so the command runs
there instead — see [Against a gateway](#against-a-gateway). The refusals
Expand All @@ -465,8 +468,8 @@ simlock adb logcat -d
```

Refused, all exit 2 with `USAGE` and a message naming what to run instead.
Each is matched anywhere in the arguments, so `-s <serial> emu kill` and
`-P 1 kill-server` are caught too:
The refused *verbs* are matched anywhere in the arguments, so
`-s <serial> emu kill` is caught too:

- `kill-server` — it would detach every leased emulator at once. (Simlock's
server rejects `kill-server` outright in any case.)
Expand All @@ -480,6 +483,32 @@ Use `simlock release` (which reclaims the device for you) or `simlock cleanup`
instead. As with `simlock simctl`, against a gateway the command runs on the
worker that owns the device (see [Against a gateway](#against-a-gateway)) and
the refusals above hold on both ends.
Globals — the arguments before the subcommand — are refused by *position*
and by an **allow list**, rather than anywhere on the line or by naming the
ones known to be dangerous:

- Only `-s`, `-t`, `-d`, and `-e` (in every spelling adb accepts, including
the attached forms `-t123`, and including one that follows another
global's value, e.g. `-s emulator-5554 -t 1 shell …`) are let through —
they select a device inside the containment Simlock already established
rather than escaping it (see [known-pitfalls.md](known-pitfalls.md)).
`--version` and `--help` are also let through, since adb answers those on
its own before it ever looks for a subcommand.
- Everything else positioned there is refused, whether or not it is a flag
this driver has a name for: `-P`, `-H`, `-L`, `--server-port` (including
the attached forms `-P5037` and `-Hhost`) are refused because `simlock adb`
supplies the server itself and `adb` takes the *last* one on the line, so a
caller-supplied one would silently win and point the command at a server
that cannot see Simlock's devices (or at one Simlock must not touch); any
other global — known adb globals this driver has no reason to allow
(`-a`, `--exit-on-write-error`, `--one-device`, the undocumented
`--reply-fd`) as well as any global a future adb release adds — is refused
the same way, on the principle that an argument whose effect on the command
this driver cannot vouch for is refused rather than assumed harmless.

From the subcommand onwards those spellings are operands and pass through:
`simlock adb shell echo -Please` echoes a word. Run `adb` directly if you
mean to leave Simlock's server.

## `simlock release <lease-id> | --all`

Expand Down
67 changes: 67 additions & 0 deletions docs/CLIENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,73 @@ and MCP's tool schemas now share. Nothing core-private
(`DeviceRecord`/`LeaseRecord`-the-core-type) ever appears on this surface;
see `src/simlock-client/no-core-leak.test.ts`.

## Running a command on the leased device: `exec`

Two methods reach the device a lease names, and which one you want depends on
one thing: whether this process is on the machine that owns it.

```ts
// Same machine: resolve the scoped command and run it yourself.
const command = await client.resolvePassthrough({ tool: "adb", args: ["shell", "getprop"] });
// -> { command: "/sdk/adb", args: ["-P", "5038", "shell", "getprop"], env: { ... } }

// Somewhere else: run it on the daemon's machine and stream the output back.
const { exitCode } = await client.exec(
{ leaseId: grant.lease.id, tool: "adb", args: ["shell", "getprop"] },
{ onOutput: ({ stream, chunk }) => process[stream].write(chunk) },
);
```

`resolvePassthrough` only resolves: it hands back the command line the
driver builds (the scoping flags for its own device root, `simctl --set` or
`adb -P`), and running it is yours to do — which only works if the paths and
the adb port it names exist where you are.

`exec` runs it. The daemon resolves the same command through the same
driver — the same scoping, and the same refusal list, so a verb the driver
will not proxy comes back as `PASSTHROUGH_REFUSED` from either method, and a
`tool` no driver on that machine wraps as `UNKNOWN_PASSTHROUGH_TOOL` — spawns
it on its own machine, and streams the output to `onOutput` as it arrives.
Each call gets `{ stream: "stdout" | "stderr", chunk }`; `chunk` is whatever
the command wrote, decoded as UTF-8 and forwarded unsplit, so a caller that
wants lines assembles them itself. Nothing is buffered daemon-side and there is
no size cap. The promise resolves with the command's own `exitCode`.

`leaseId` is an ownership proof and nothing more: the *device* is named by the
command's own arguments, which the daemon does not parse. An agent-role client
is authorized against the lease it owns, exactly as for `renewLease` — a lease
it does not own is `FORBIDDEN`, and an id that names no lease is
`UNKNOWN_LEASE`.

An **admin-role** client (`simlock/admin` with a credential) does *not* get the
usual admin bypass here. It passes `requesterId` — the agent it is running the
command for — and it must match the requester the lease was granted to, or the
call is `FORBIDDEN`. Omitting it is not a bypass either: the *daemon* fills it
in with the connection's own principal and then applies the same comparison,
so an admin connection that names nobody reaches only the leases granted to
itself. The field exists for a proxy holding one admin connection on behalf of
many agents; an operator reaching another agent's device names that agent
deliberately rather than getting there implicitly. An agent-role client has no
say in it at all — its `requesterId` is ignored, and it is authorized against
the lease it owns.

Three limits worth knowing:

- **No pseudo-terminal.** Line-oriented commands work; full-screen and
interactive ones do not, and a bare `adb shell` -- which is exactly the
interactive shell -- is refused with `PASSTHROUGH_REFUSED` rather than left
to hang until the timeout.
- **`stdin` is one shot.** The optional `stdin` string is written to the
command once and the pipe is then closed — not a channel you can write to
over time.
- **`exec.timeoutMs`** (ten minutes by default) kills a command that outruns
it, and the call rejects with `EXEC_TIMEOUT` rather than reporting the exit
code the kill produced. Losing the connection does not kill the command;
it only ends the output you were receiving.

Paths in `args` resolve on the daemon's filesystem, not yours. Getting an
`.app` or an `.apk` there is out of scope for now.

## One connection, no reconnect, no retry

This is the one thing to internalize before building anything on top of this
Expand Down
Loading
Loading