Skip to content

exec takes stdinFile and runs in the lease folder #1357

Closed

Description

@V3RON

Part of #1351.

Scope

A lease holder can run exec with stdinFile naming a file in the lease folder, and the command reads that file's bytes on its stdin with no size cap but the file's own 2 GiB. Every exec runs with the lease folder as its working folder, so a relative path in its arguments names a stored file and a command can write files there for download. Through a gateway, stdinFile reaches the lease's worker unchanged (the file must already be on that worker; uploading through a gateway is #1358).

In short

exec gains one optional field, stdinFile, and a fixed working folder. stdin keeps its one-shot string and its 8,388,608-character cap; giving both is refused.

flowchart LR
  C[HTTP caller] -->|"POST exec {stdinFile}"| H[HTTP app]
  H --> D["worker dispatcher (changed)"]
  D -->|"stat the file: 404 UNKNOWN_FILE before start"| F[(lease folder)]
  D -->|"spawn, cwd = lease folder (changed), stdin piped from the file (changed)"| T[simctl / adb]
  T -->|output| C
Loading

Before and after:

$ curl -N -X POST "$W/v1/leases/$LEASE/exec" -H "Authorization: Bearer $T" -H 'content-type: application/json' \
    -d '{"tool":"adb","args":["push","fixture.bin","/data/local/tmp/fixture.bin"]}'
(before: adb looks for fixture.bin in the daemon's own working folder and fails)
(after: adb finds the uploaded fixture.bin in the lease folder; exit 0)

$ curl -N -X POST "$W/v1/leases/$LEASE/exec" ... -d '{"tool":"simctl","args":["spawn","booted","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/path/helper","untar"],"stdinFile":"fixture.tar"}'
{"error":{"code":"BAD_REQUEST","message":"..."}}       (before: 400, unknown key)
event: output ... event: exit data: {"exitCode":0}      (after: the helper reads fixture.tar on stdin)

$ curl ... -d '{"tool":"simctl","args":["list"],"stdinFile":"missing.tar"}'
{"error":{"code":"UNKNOWN_FILE","message":"No file missing.tar in this lease","name":"missing.tar"}}   (after: 404, before the command starts)

The inputs table is the feature's Examples, "Exec input". If the file cannot be read to its end, the command is stopped and the stream ends with an error event STDIN_FILE_FAILED Reading <name> failed after <bytesRead> bytes; the command was stopped. simlock/client's exec type gains the optional stdinFile; MCP's run_on_device does not. A user would notice if this goes wrong: a command that sees part of the file, never sees end of input, or runs in another folder than the one its upload went to.

Technical spec

Facts

Commit 5aedc2d.

# Fact Source
F1 device.exec input is .strict() with stdin: z.string().max(EXEC_STDIN_MAX_LENGTH).optional(); an unknown key is refused src/contract/operations.ts:465-488
F2 The HTTP exec body is .strict(); stdin and requesterId take null as omitted src/http/lease-declarations.ts:134-146
F3 ProcessStreamOptions.input is a string, written to the child's stdin, which is then closed; cwd is an option src/ports/process-runner.ts:87-106
F4 The adapter passes cwd to spawn only when given src/ports/process-runner.ts:244-247
F5 #deviceExec calls spawnStreaming with env, onChunk and input from input.stdin, and no cwd src/daemon/dispatcher.ts:764-770
F6 The gateway forwards exec fields by name: leaseId, tool, args, stdin, requesterId; a new field is dropped unless added src/gateway/fleet-forwarding.ts:154-164; src/gateway/dispatcher.ts:796-807
F7 The fake driver's passthrough is a node one-liner that reports its argv and, with --fake-exec-echo-stdin, its stdin as JSON e2e/fake-driver/fake-driver.ts:180-200
F8 e2e/device-exec.test.ts already proves a stdin string reaches the command and ends with EOF e2e/device-exec.test.ts:186-198
F9 The Android driver refuses kill-server, disconnect, other globals and a bare shell; push, pull and shell <command> pass src/drivers/android/passthrough-rules.ts:28-38; src/drivers/android/AGENTS.md (Passthrough)
F10 The iOS 26.5 simulator runtime has no tar, sh or cat, so a real-device stdin check uses an Android emulator's adb ls ".../iOS 26.5.simruntime/Contents/Resources/RuntimeRoot/usr/bin" and .../bin on this machine: df, launchctl in bin, no tar or cat in usr/bin
F11 ADR 0058 fixes the CLI's --stdin and says nothing on the wire changes for stdin; this task leaves stdin alone docs/internal/adr/0058-through-a-gateway-simlock-adb-and-simctl-send-stdin-only-with-stdin.md (Decision 5)
F12 MCP's run_on_device input is OPERATIONS["device.exec"].input.omit({leaseId, requesterId}), built when the module loads; a test pins its fields to args, stdin, tool src/mcp/contracts.ts:105-108; src/mcp/server.test.ts:86-92
F13 zod 4's .omit() throws on an object schema that carries a refinement, so the "not both" rule cannot be a refinement on device.exec's input package.json (zod ^4.6.5); the checker's run of zod 4.6.5: .omit() cannot be used on object schemas containing refinements
F14 simlock/client's ExecInput is the contract's device.exec input src/simlock-client/types.ts:40
F15 The gateway's scripted worker records only device.exec:<requesterId> for an exec src/gateway/test-support.ts:498-500
F16 On a timeout the dispatcher stops a command with SIGTERM, then SIGKILL after EXEC_SIGKILL_GRACE_MS (10 s) src/daemon/dispatcher.ts (#awaitExec, EXEC_SIGKILL_GRACE_MS)

Modules touched

Searches: spawnStreaming, ProcessStreamOptions, input.stdin, EXEC_STDIN_MAX_LENGTH, execBodySchema, coordinator.exec, client.exec, OPERATIONS["device.exec"] (finds src/mcp/contracts.ts:105), ExecInput.

  • src/contract/operations.ts: stdinFile on device.exec (no refinement on the object, F13); src/contract/errors.ts: STDIN_FILE_FAILED; protocol.ts bump.
  • src/mcp/contracts.ts: run_on_device also omits stdinFile; src/mcp/server.test.ts:88-92 stays as it is and proves it.
  • src/simlock-client/types.ts: no change (F14); website/src/content/docs/docs/using-devices/programmatic-client.mdx names the field.
  • src/http/lease-declarations.ts: stdinFile in the exec body (null as omitted) and UNKNOWN_FILE among the route's 404 codes; the "not both" refusal is the dispatcher's alone (it comes before onStarted, so HTTP answers it as a plain 400).
  • src/ports/process-runner.ts (tests in src/ports/process-runner.test.ts): ProcessStreamOptions.inputStream (a ReadableStream<Uint8Array>), piped to stdin and then closed; a read error rejects the handle's inputFailed promise with the bytes written so far; in the adapter and ScriptedProcessRunner.
  • src/daemon/dispatcher.ts: the grant {id, grantedAt, platform} built from the lease and device the handler already reads (src/daemon/dispatcher.ts:739-745), then folderOf(grant) as cwd (made there, under Lease files on a worker: upload, download, list and delete a lease's files #1356's ended-grant check); stdinFile opened through openFile(grant, name) before the spawn, its read() handed as inputStream; the one "not both" refusal for every transport; on inputFailed, stop the command as a timeout does (F16) and end with STDIN_FILE_FAILED.
  • src/daemon/worker-composition.ts: hand the dispatcher LeaseFiles.
  • src/gateway/dispatcher.ts, src/gateway/fleet-forwarding.ts: forward stdinFile; src/gateway/test-support.ts: the scripted worker records the forwarded input (F15).
  • e2e/fake-driver/fake-driver.ts: the report carries cwd (process.cwd(), which the test compares after realpath, as macOS's /var is /private/var), and --fake-exec-read-file=<name> reports the size of a file read by that relative name.
  • e2e/slow-android-lease-files.test.ts (new).
  • Docs: website/src/content/docs/docs/using-devices/remote-agents.mdx (exec section), docs/internal/flows/device-exec.md, src/daemon/AGENTS.md.
flowchart LR
  HTTP["http exec body (changed)"] --> D["daemon dispatcher (changed)"]
  GD["gateway dispatcher (changed)"] --> FF["fleet-forwarding (changed)"] --> D
  D --> LF["lease-files (#1356)"]
  D --> PR["ports/process-runner (changed)"]
  C["contract device.exec (changed)"]
Loading
sequenceDiagram
  participant C as Caller
  participant G as Gateway
  participant W as Worker dispatcher
  participant P as Child
  C->>G: POST exec {stdinFile: "fixture.tar"}
  G->>W: device.exec {leaseId, tool, args, stdinFile, requesterId}
  W->>W: openFile(lease, fixture.tar), ignoring case
  alt missing
    W-->>C: 404 UNKNOWN_FILE (plain JSON, nothing started)
  else present
    W->>P: spawn, cwd = folder, stdin piped from the file then closed
    P-->>C: output pushes, then exit
  end
Loading

Contract and event changes

This PR bumps the protocol by one: a new error code STDIN_FILE_FAILED (the feature's Shared values row), and device.exec input gains stdinFile, optional string, the File name rule of the feature's Shared values (1 to 128 characters, [A-Za-z0-9][A-Za-z0-9._-]{0,127}); a bad name is 400 BAD_REQUEST; with stdin too, 400 BAD_REQUEST Give stdin or stdinFile, not both, checked once, by the worker dispatcher, never as a refinement on the shared input (F13); HTTP answers it as a plain 400 since it comes before onStarted. The HTTP body takes the same field, null read as omitted. MCP's run_on_device input leaves it out; simlock/client's type carries it. UNKNOWN_FILE is #1356's. No event.

Other code on the same state

Searches: cwd across src/ (non-test: ports/process-runner.ts, instructions/setup.ts, cli/index.ts, ports/npm-cli.ts, ports/daemon-launcher.ts; none sets it for device.exec); lease-files (only #1356).

Component State When both act Which wins
#1356 PUT to the stdinFile name while exec starts the stored file exec opens the file before the spawn; a later rename never changes the open file the file exec opened
#1356 lease-end participant the folder a lease end during exec removes the folder; the running command keeps its open file and its cwd handle, as a removed folder allows the lease end; the command runs on, as a disconnect does today
#1356 DELETE of the stdinFile name before the spawn the stored file stat then open: a delete between them fails the open → 404 UNKNOWN_FILE the delete
driver.passthrough (local CLI) none not changed: it hands the CLI a command line and runs nothing here —

Failure modes

Call Outcome What happens Limit, counted from
LeaseFiles.folderOf the grant has ended 404 UNKNOWN_LEASE, nothing spawned —
stat/open of the stdinFile ok spawn with it —
stat/open of the stdinFile absent 404 UNKNOWN_FILE, nothing spawned —
mkdirp of the lease folder throws 500 INTERNAL, logged, nothing spawned —
spawn the lease ended after folderOf, so the working folder is gone (ENOENT) 404 UNKNOWN_LEASE, a plain JSON error, nothing started —
piping the file to stdin the child exits early (EPIPE) the pipe stops; exit code as the child gives it exec.timeoutMs, from the spawn, as today
piping the file to stdin the read fails the command is stopped (SIGTERM, SIGKILL after 10 s) and the stream ends with error STDIN_FILE_FAILED, bytesRead the bytes written as above
gateway forward worker without this field (older protocol) the protocol check refuses the call as it does any shape change —

State written: the lease folder, made when missing. A restart in the middle of an exec leaves the folder; #1356's sweep owns it.

Rules in play

  • ADR 0067 §5: "Every device.exec runs with the lease's folder as its working directory"; stdinFile; stdin keeps its cap.
  • ADR 0058 Decision 5, "Nothing on the wire changes" for stdin: stdin is untouched; stdinFile is a new field.
  • ADR 0005 §19e, output streamed and never buffered: the file is piped, never read into memory.
  • Ports rule "Application code never imports fs": the file reaches the child through ProcessRunner.
  • Documentation rule 9: remote-agents.mdx describes stdinFile and the working folder.
  • Contract manual, "Every change to a contract shape bumps it by one".

Tests

Seam: the whole worker over HTTP with the fake driver (e2e/device-exec.test.ts, which already proves stdin, F8), extended; the read failure through the real adapter in src/ports/process-runner.test.ts (a real child, an input stream that errors) and its handling in src/daemon/dispatcher-exec.test.ts; the socket-side "not both" refusal in src/daemon/dispatcher-exec.test.ts; the gateway forward in src/gateway/dispatcher.test.ts; MCP in src/mcp/server.test.ts.

  • A lease holder runs exec with stdinFile and the command reads the file's bytes, then end of input. Cases: 9 MiB file; empty file; name in another case; stdinFile: null runs with no stdin; bad name .hidden 400; unknown name 404 UNKNOWN_FILE before the command starts; stdin and stdinFile both 400, over HTTP and over the daemon socket.
  • A lease holder whose stdinFile cannot be read to its end sees the command stopped and an error event STDIN_FILE_FAILED. Cases: read fails after 1 MiB; read fails at once; the real adapter tells a read error from the child closing its stdin early (EPIPE, no error event).
  • Every exec runs with the lease folder as its working folder. Cases: lease with no files yet (folder made); after an upload, a relative path names the file.
  • Through a gateway, exec with stdinFile reaches the lease's worker with the same name. Cases: stdinFile set; omitted.
  • An MCP agent's run_on_device takes no stdinFile. Cases: the tool's input lists args, stdin, tool (the existing check at src/mcp/server.test.ts:88, kept green).

Done when

  • pnpm test:e2e e2e/device-exec.test.ts passes with the first two Tests lines.
  • pnpm exec vitest run --project unit src/ports/process-runner.test.ts, … src/daemon/dispatcher-exec.test.ts, … src/gateway/dispatcher.test.ts and … src/mcp/server.test.ts pass with the other Tests lines.
  • On a Mac with the Android SDK, e2e/slow-android-lease-files.test.ts passes: it uploads a 70 MB file, runs adb push fixture.bin /data/local/tmp/fixture.bin and adb shell wc -c /data/local/tmp/fixture.bin (prints 70000000), then adb pull /data/local/tmp/fixture.bin back.bin and a GET of back.bin returns the same bytes.
  • remote-agents.mdx documents stdinFile and the working folder.

Out of scope

  • Uploading through a gateway (Lease files through a gateway #1358).
  • Any change to stdin, its cap, or the CLI's --stdin (ADR 0058).
  • stdinFile on the CLI or MCP; simlock/client only carries the type.

Depends on

Written by an agent.

Activity

  1. added
    task:draft
    on Nov 6, 2026
  2. added
    task:ready
    and removed
    task:draft
    on Nov 6, 2026
  3. self-assigned this
    on Nov 6, 2026
  4. added 19 commits that reference this issue on Nov 6, 2026
  5. added a commit that references this issue on Nov 6, 2026
    5ceaaed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

task:ready

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions