Skip to content

Remote clients can move large files to a leased device #1351

Closed
3 of 3 issues completed
Closed

Description

@V3RON

Request: #1323

Problem

Remote clients have no way to move a large file to a leased device. Since #1318 (for #1242), device.exec refuses stdin over 8,388,608 characters. Before that, a large stdin was parsed as one JSON string and could crash the worker.

Two e2e suites already send fixtures this way and break at the cap:

  • tester-army sends a ~89 MB fixture archive (deflated, base64) as exec stdin to simctl spawn <helper> untar, from 10 workers at once.
  • budgie (budgie-at/budgie#1867) sends a tar of the app's Documents/ as base64 raw-deflate on stdin. It caps app uploads at 3 at a time because 10 in parallel through the Cloudflare tunnel ended in 524 errors.

Who it is for

A remote caller of the HTTP API (a CI suite, a test runner, an agent on another machine) that holds a lease and must get a file of tens to hundreds of MB onto the leased device, or fetch one back, often ten callers at once through a gateway behind a CDN.

Outcome

  • A lease holder uploads a file of up to 2 GiB to its lease with one HTTP PUT, as raw bytes (no base64), on a worker or through a gateway.
  • A later exec on that lease pipes the stored file into the command's stdin (stdinFile), or names it by a relative path, because every exec runs in the lease's file folder.
  • The holder lists the lease's files, deletes one, and downloads one whole or by byte range, including files a command wrote into the folder.
  • The files go away when the lease ends.
  • No single request holds a whole file in memory, on the gateway or on the worker.

How it works for the user

sequenceDiagram
  participant C as HTTP caller
  participant G as Gateway or worker
  C->>G: PUT /v1/leases/{id}/files/fixture.tar (raw bytes)
  alt name, size, lease total or disk refused
    G-->>C: 400 / 413 / 422 JSON error, nothing stored
  else another PUT to the name is running
    G-->>C: 409 FILE_BUSY
  else stored
    G-->>C: 201 {"name":"fixture.tar","size":70000000}
  end
  C->>G: POST /v1/leases/{id}/exec {"tool":"simctl","args":[...],"stdinFile":"fixture.tar"}
  alt no such file
    G-->>C: 404 UNKNOWN_FILE
  else runs
    G-->>C: SSE output, then exit
  end
  C->>G: GET /v1/leases/{id}/files/shot.png (Range optional)
  G-->>C: 200 or 206 bytes, 404 UNKNOWN_FILE, 416
  Note over C,G: lease released or expired: the folder is deleted
Loading

Before:

$ curl -X POST "$GW/v1/leases/$LEASE/exec" -H "Authorization: Bearer $TOKEN" \
    -H 'content-type: application/json' --data @exec-with-89MB-stdin.json
{"error":{"code":"BAD_REQUEST","message":"..."}}        (400: stdin over 8,388,608 characters)

After:

$ curl -X PUT "$GW/v1/leases/$LEASE/files/fixture.tar" -H "Authorization: Bearer $TOKEN" \
    --data-binary @fixture.tar
{"name":"fixture.tar","size":70000000}                   (201)

$ curl -N -X POST "$GW/v1/leases/$LEASE/exec" -H "Authorization: Bearer $TOKEN" \
    -H 'content-type: application/json' \
    -d '{"tool":"simctl","args":["spawn","booted","/path/to/helper","untar"],"stdinFile":"fixture.tar"}'
event: output ...
event: exit
data: {"exitCode":0}

$ curl "$GW/v1/leases/$LEASE/files" -H "Authorization: Bearer $TOKEN"
{"files":[{"name":"fixture.tar","size":70000000}]}

$ curl -X PUT "$GW/v1/leases/$LEASE/files/.hidden" -H "Authorization: Bearer $TOKEN" --data-binary @x
{"error":{"code":"BAD_REQUEST","message":"..."}}        (400)

Examples

File names (today every one is a 404: no route exists).

Name Today After
fixture.tar 404 accepted
app-release.apk 404 accepted
Documents.tar.deflate 404 accepted
A (one character) 404 accepted
128 characters of a 404 accepted
129 characters of a 404 400 BAD_REQUEST
.hidden 404 400 BAD_REQUEST
-rf 404 400 BAD_REQUEST
my file.tar 404 400 BAD_REQUEST
a%2Fb.tar (decodes to a/b.tar) 404 400 BAD_REQUEST
.. (the server normalizes the path away) 404 404, no file route matches
zażółć.txt 404 400 BAD_REQUEST
A.tar when a.tar is stored 404 the same file: a PUT replaces it, a GET returns it

Exec input (HTTP body of POST /v1/leases/{id}/exec).

Body Today After
stdin of 1,000 characters runs runs
stdin of 8,388,609 characters 400 400
stdinFile: "fixture.tar" (stored) 400 (unknown key) runs, file on stdin
stdinFile: "missing.tar" 400 (unknown key) 404 UNKNOWN_FILE
stdinFile: null 400 (unknown key) runs, no stdin
stdin and stdinFile both 400 (unknown key) 400 BAD_REQUEST

Download ranges, for a stored file of 1,000 bytes.

Range header After
none 200, 1,000 bytes
bytes=0-99 206, bytes 0–99, Content-Range: bytes 0-99/1000
bytes=900- 206, bytes 900–999
bytes=-100 206, bytes 900–999
bytes=1000- 416, Content-Range: bytes */1000
bytes=0-1,5-9 200, whole file (one range only; several are ignored)

What could go wrong

  • An upload is cut by a slow or flaky network and the caller must send it again from the start (likely on big files; resumable uploads are a later feature).
  • A caller's CDN refuses or times out one request carrying a big file before it reaches the gateway (possible above about 100 MB).
  • A command that used to rely on the daemon's own working folder sees the lease's folder instead (unlikely; exec commands name the device, not files).
  • A worker's disk fills with lease files until uploads are refused (bounded: 4 GiB per lease, and nothing is stored under the disk-pressure threshold).

Words used

  • lease folder: the folder on the worker that holds one lease grant's files, e.g. the place fixture.tar lands after PUT /v1/leases/L1/files/fixture.tar; deleted when lease L1 ends.
  • file name: a flat name in a lease folder, [A-Za-z0-9][A-Za-z0-9._-]{0,127}, e.g. fixture.tar; no folders; A.tar and a.tar are one file, on every worker OS.
  • stdinFile: an exec field naming a stored file whose bytes are piped to the command's stdin, then end of input, e.g. "stdinFile":"fixture.tar".
  • disk-pressure threshold: the worker's diskPressure.freeBytesThreshold (10 GiB by default); below it, no upload is stored.
  • relay: how a gateway passes an HTTP request on to the lease's worker over the worker's own connection, streaming the body in pieces.
  • silence bound: an upload is cut only after 330 s in which no byte of its body arrived, not after a total time; on a worker and through a gateway.
  • folder key: the lease folder's name, made from the lease id in hex and the grant time in ms, e.g. lease ci granted at 1762419161000 → 6369-1762419161000; two grants never share one.

Non-goals

  • Resumable or multi-part uploads (tus, the IETF draft, offsets): one PUT per file; a cut upload is sent again.
  • Unpacking an archive into folders (an iOS .app for simctl install): files are flat.
  • The local CLI, simlock/client and MCP: they keep exec's 8 MiB stdin.
  • Streamed stdin for exec, or any change to stdin's cap.
  • Files shared between leases, or kept after a lease ends.
  • Business events for uploads or downloads.
  • Configurable limits.

Completion conditions

  • On a standalone worker, PUT /v1/leases/{id}/files/fixture.tar with a 70 MB body answers 201 with its size, and GET returns the same bytes.
  • Through a gateway, an upload and download of a 20 MiB file work against a joined worker's lease, and ten uploads of 20 MiB at once all succeed (gateway and worker test rig).
  • POST /v1/leases/{id}/exec with stdinFile naming a stored 9 MiB file hands the command those bytes on stdin, then end of input (whole worker, fake driver).
  • On a real Android emulator, a 70 MB upload is pushed by its relative name (adb push fixture.bin /data/local/tmp/), adb shell wc -c prints its size, and an adb pull back into the lease folder downloads the same bytes.
  • A file over 2 GiB, a lease past 4 GiB, a bad name, a disk under its threshold and an upload silent for 330 s are each refused with the code in Shared values, and nothing is stored.
  • An upload that keeps moving for longer than 330 s in total completes, on a worker and through a gateway.
  • After the lease is released, its files answer 404 and its folder is gone from the worker's disk.
  • The website pages for remote agents, the HTTP API and errors describe the routes, stdinFile and the limits.

Open questions

none

Decisions

  • ADR 0067 — A lease has a file folder on its worker that HTTP callers fill and read (PR docs(adr): propose 0067, a lease has a file folder on its worker that HTTP callers fill and read (#1351) #1354): the routes, names, limits, stdinFile, the working folder, the core relay target files and the silence bound for relayed file requests.
  • ADR 0005 §19b′ and Non-goals — narrowed by 0067: file transfer is built, as HTTP routes.
  • ADR 0024 — Leasing calls lease-end participants before reclaim: the lease folder is deleted by one.
  • ADR 0058 — untouched: stdin keeps its one-shot string and its cap.

Shared values

Value or state Rule Written by Read by Source
Lease folder <data dir>/lease-files/<folder key>/, mode 0700; folder key = the worker lease id's UTF-8 bytes in lowercase hex, -, the lease's grantedAt in ms. Made when missing, only by LeaseFiles.folderOf (#1356), which refuses an ended grant; #1356's PUT and #1357's exec both call it #1356 #1356, #1357, #1358 src/contract/operations.ts:127-132 (case-sensitive ids); src/core/domain.ts:398-409 (grantedAt); src/ports/paths.ts:107-109
Lease end leasing's EndedLease gains grantedAt (filled from the ended lease), so the participant names the grant; it marks the grant ended, aborts its running PUTs (each answers 404 UNKNOWN_LEASE), waits for the grant's operations already in flight (mkdir, rename) to settle, then removes the folder; any PUT, exec mkdir or rename for an ended grant is refused 404 UNKNOWN_LEASE and leaves nothing; startup's sweep takes the live grants (id and grantedAt) and removes every folder none of them names and every upload temp file #1356 #1357, #1358 ADR 0024; src/leasing/lease-release-coordinator.ts:330-332
File name ^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$, checked after URL decoding; else 400 BAD_REQUEST File name <name> must be 1 to 128 letters, digits, '.', '_' or '-', starting with a letter or digit. Compared ignoring case on every worker OS (PUT, GET, DELETE, stdinFile, FILE_BUSY): a PUT removes every other spelling of its name; when a command left several spellings (a case-sensitive Linux disk), a lookup takes the exact spelling, else the first in byte order; the list shows each spelling that exists #1356 #1357 (stdinFile), #1358 ADR 0067 §3; macOS disk case-insensitive (touch $d/Ab; ls $d/aB lists the file); Linux ext4 case-sensitive
File limit 2 GiB (2,147,483,648 bytes) per file, counted on the worker as bytes received; a Content-Length over it is refused before any byte, by the worker and, through a gateway, by the gateway before relaying (the relay drops content-length) #1356, #1358 #1356, #1358 ADR 0067 §4; src/contract/relay.ts:29-33
Lease limit 4 GiB (4,294,967,296 bytes) per lease folder: every entry in the folder (subfolders walked, symlinks not followed) plus the bytes other running PUTs of the grant have received, the file being replaced not counted; checked at the start and as bytes arrive #1356 #1358 ADR 0067 §4
Disk check free bytes on the folder's disk read once, when a PUT starts #1356 #1358 src/ports/filesystem.ts:97
Silence bound an upload whose body brings no byte for 330 s is cut: temp removed, previous file kept, 408 UPLOAD_STALLED. On a worker the store counts from the request's start or the last byte it read. Through a gateway, for a file PUT only: the channel open keeps today's 330 s deadline (WORKER_UNREACHABLE); from the open, 330 s with no byte read from the caller → 408; the head deadline starts when the body has ended #1356 (worker), #1358 (gateway) — ADR 0067 §6; src/gateway/relay.ts:154-175
HTTP listener total limit the worker's and gateway's listener sets Node's requestTimeout to 0 (no total); Node's header limit stays #1356 #1358 src/http/server.ts:174-186; node -e 'require("http").createServer().requestTimeout' prints 300000
FILE_TOO_LARGE 413, exit 1, kind: domain, details {name}; message File <name> is over the 2 GiB limit for one file #1356 #1358 ADR 0067 §4
LEASE_FILES_FULL 413, exit 1, kind: domain, details {leaseId, usedBytes}; message Lease <leaseId> holds <usedBytes> bytes of files; this upload would pass the 4 GiB limit #1356 #1358 ADR 0067 §4
UNKNOWN_FILE 404, exit 1, kind: domain, details {name}; message No file <name> in this lease #1356 #1357 (stdinFile), #1358 ADR 0067 §4
UPLOAD_STALLED 408, exit 1, kind: domain, details {name}; message No bytes of <name> arrived for 330 s #1356, #1358 — ADR 0067 §6
FILE_BUSY 409, exit 1, kind: domain, details {name}; message File <name> is being uploaded by another request #1356 #1358 ADR 0067 §2
INSUFFICIENT_DISK_SPACE (existing) 422; details {platform, requiredBytes, availableBytes} with the lease's platform, requiredBytes = the threshold, availableBytes = free bytes now; message Not enough free disk space to store <name>: the worker keeps <threshold> free, only <available> available, both as formatGibibytes writes them (one decimal, e.g. 10.0 GiB) #1356 #1358 src/contract/errors.ts:68-72, :340-345; src/core/config.ts:996; src/core/driver.ts:689-691
Upload reply 201 {"name": string, "size": number} #1356 #1358 ADR 0067 §2
Download headers Content-Type: application/octet-stream, Cache-Control: no-store, Accept-Ranges: bytes, Content-Length (the bytes of this body: the range on a 206); the worker also sends simlock-content-length with the same value, which a gateway copies into Content-Length on any answer that carries it #1356 #1358 ADR 0067 §2; src/http/app.ts:251-263 (JSON ETag buffering)
List reply 200 {"files": [{"name": string, "size": number}]}, regular files whose names pass the File name rule only, sorted by name; [] when the folder is empty or absent #1356 #1358 ADR 0067 §2
Relay target files, served by the daemon, not a module; files is reserved as a module name #1356 (reserved name), #1358 (target) the gateway relay src/modules/host.ts:114; src/contract/status-schemas.ts:316
File routes' handler the HTTP app resolves the lease with lease.get (owner or operator) and calls the leaseFiles dependency with (request, identity, {id, deviceId, workerId?, workerLeaseId?}, name?); absent → 501 UNSUPPORTED_IN_GATEWAY_MODE #1356 #1358 (the gateway's dependency) src/http/app.ts:832-848
stdinFile optional string in device.exec input and the HTTP exec body (null read as omitted), the File name rule; with stdin too → 400 BAD_REQUEST Give stdin or stdinFile, not both, checked once, by the worker dispatcher (HTTP answers it as a plain 400, before the command starts), never as a refinement on the shared input schema; simlock/client's ExecInput carries it; MCP's run_on_device leaves it out #1357 gateway forwarding (#1357) src/contract/operations.ts:465-488; src/http/lease-declarations.ts:134-146; src/mcp/contracts.ts:105-108; src/simlock-client/types.ts:40
STDIN_FILE_FAILED an exec error event (the stream has started, so no HTTP status reaches the caller; its table entry is 500), exit 1, kind: domain, details {name, bytesRead}; message Reading <name> failed after <bytesRead> bytes; the command was stopped; the command is stopped as exec.timeoutMs stops one (SIGTERM, SIGKILL after 10 s) #1357 — src/daemon/dispatcher.ts (#awaitExec)
Protocol version #1356 (error codes, EndedLease is not wire) and #1357 (stdinFile, STDIN_FILE_FAILED) change contract shapes and each bump by one; #1358 bumps only if it changes one; the PR that merges second bumps again #1356, #1357 protocol-bump-check src/contract/AGENTS.md (Protocol version)

Behaviours

Behaviour (invoker, action, what it sees) Seam Cases Task
A lease holder PUTs a file and gets 201 with its name and size; a GET returns the same bytes worker HTTP app new name; same name again replaces it; empty body (size 0); 3 MiB body; 70 MB body #1356
A lease holder PUTs a bad file name and gets 400 BAD_REQUEST with the name rule worker HTTP app each row of the names table #1356
A lease holder PUTs past a limit and gets the limit's code, with nothing stored and the previous file kept worker HTTP app Content-Length over 2 GiB (refused before any byte); no Content-Length, body past 2 GiB; folder plus upload past 4 GiB, a command-written file counted; two running PUTs that pass it only together; replacing a file is not counted twice; disk free below threshold #1356
A lease holder whose PUT is cut or goes silent keeps the previous file or none; a second PUT to a name being uploaded gets 409 FILE_BUSY worker HTTP app cut with no previous file; cut with a previous file; silent for 330 s → 408 UPLOAD_STALLED; still moving past 330 s total succeeds; two PUTs at once, A.tar and a.tar included #1356
A lease holder's file names match ignoring case worker HTTP app PUT A.tar then GET a.tar; PUT a.tar replaces A.tar and the list shows a.tar; DELETE A.TAR; a command left A.tar and a.tar (case-sensitive disk): GET a.tar exact, GET A.TAR the first in byte order, both listed #1356
A lease holder GETs a file and gets it whole or the range it asked for worker HTTP app no Range; bytes=0-99; bytes=900-; bytes=-100; bytes=1000- 416; bytes=-5 on a 0-byte file 416; two ranges; unknown name 404 UNKNOWN_FILE #1356
A lease holder lists the lease's files and deletes one worker HTTP app empty folder []; two files sorted; a subfolder and a my file.png a command wrote are not listed; DELETE 204 then gone from the list; DELETE unknown 404 UNKNOWN_FILE #1356
A caller that does not hold the lease gets 404 UNKNOWN_LEASE on every file route, and an operator is served worker HTTP app another agent; unknown lease; operator token #1356
A lease's files are gone once it ends, a new lease with the same id starts empty, and startup removes the folders of leases that are no longer live worker daemon released; expired; killed; device lost; a PUT running when the lease ends answers 404 UNKNOWN_LEASE and leaves nothing; a folderOf whose mkdir is still running when the lease ends leaves no folder; lease ids CI and ci live at once; the same id leased again; startup with one live and one gone lease and an upload temp file #1356
A module named files is refused when the module host loads module host files; stream still loads #1356
On a gateway, before the gateway relays them, the file routes answer 501 UNSUPPORTED_IN_GATEWAY_MODE gateway HTTP app each route #1356
The HTTP listener of a worker and of a gateway has no total request time limit HTTP listener worker; gateway #1356
A lease holder runs exec with stdinFile and the command reads the file's bytes, then end of input worker over HTTP, fake driver 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 #1357
A lease holder whose stdinFile cannot be read to its end sees the command stopped and an error event STDIN_FILE_FAILED worker dispatcher and the process-runner adapter 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) #1357
An MCP agent's run_on_device takes no stdinFile MCP server the tool's input lists args, stdin, tool #1357
Every exec runs with the lease folder as its working folder worker over HTTP, fake driver lease with no files yet (folder made); after an upload, a relative path names the file #1357
Through a gateway, exec with stdinFile reaches the lease's worker with the same name gateway dispatcher stdinFile set; omitted #1357
Through a gateway, a lease holder's PUT, GET, list and DELETE reach the lease's worker and answer as on a worker gateway relay rig each route, with a 20 MiB file; a GET keeps its Content-Length; a Range GET 206; a worker's 413, 409 and 404 passed through; Content-Length over 2 GiB refused 413 at the gateway with nothing relayed; ten 20 MiB uploads at once all stored #1358
Through a gateway, a file upload is cut only by silence, not by a total time gateway relay rig (silence bound lowered for the test) and GatewayRelay (fake clock) body still flowing past the bound in total succeeds; body silent past the bound → 408 UPLOAD_STALLED over HTTP; channel open past 330 s → 503 WORKER_UNREACHABLE; a non-file relay still has its 330 s head deadline #1358
Through a gateway, a caller that does not hold the lease gets 404 UNKNOWN_LEASE, and a lease whose worker is gone gets 503 WORKER_UNREACHABLE gateway relay rig another agent; disconnected worker; a worker's UNKNOWN_LEASE or LEASE_FILES_FULL names the caller's lease id #1358

Tasks

flowchart LR
  A["#1330 (other feature, contract files)"] --> T1["#1356 lease files on a worker"]
  B["#1331 (other feature, protocol bump)"] --> T1
  T1 --> T2["#1357 exec stdinFile + working folder"]
  T1 --> T3["#1358 lease files through a gateway"]
Loading

Completion conditions → task:

Open work in the same area:

Written by an agent.

Sub-issues

Sub-issues

3 of 3 Issues completed
Loading 3 sub-issues

Activity

  1. added
    feature:spec
    on Nov 6, 2026
  2. V3RON commented on Nov 6, 2026

    @V3RON
    ContributorAuthor

    Spec updated: Who it is for, Outcome, How it works, Examples, What could go wrong, Words used, Non-goals, Completion conditions, Decisions (ADR 0067, PR #1354), Shared values and Behaviours written; Tasks to follow after the feature check.

    Written by an agent.

  3. V3RON commented on Nov 6, 2026

    @V3RON
    ContributorAuthor

    Spec updated: folded the feature check's round (folder per grant, names ignore case, no total request limit and a 330 s silence bound with UPLOAD_STALLED, the gateway's own Content-Length check, download headers, list rules, the real-device check) into Examples, Completion conditions, Words used, Shared values and Behaviours; Tasks written (#1356, #1357, #1358).

    Written by an agent.

  4. V3RON commented on Nov 6, 2026

    @V3RON
    ContributorAuthor

    Spec updated after the round-1 checks: Shared values (grant time on EndedLease, sweep by live grants, names on Linux, the file routes' handler shape, download length header, gateway relay timers, stdinFile on MCP and the client, STDIN_FILE_FAILED), Behaviours (gateway 501, listener limit, limit cases, read failure, MCP), Completion conditions (ten 20 MiB uploads), Open work (#1331); tasks #1356, #1357, #1358 amended to match.

    Written by an agent.

  5. added
    feature:planned
    and removed
    feature:spec
    on Nov 6, 2026
  6. github-actions commented on Nov 6, 2026

    @github-actions
    Contributor

    Closing: all 3 sub-issues are closed. The PR that closed the last one walks the completion conditions (delivery rule 9); reopen this if one does not hold on main. Flip its ADRs to Accepted if delivery has not.

  7. V3RON commented on Nov 6, 2026

    @V3RON
    ContributorAuthor

    Delivery run

    Merged: #1354 (ADR 0067), #1390 (#1356), #1393 (#1357), #1392 (#1358)
    Parked: none
    Waiting: none
    Decided: #1356 — what DELETE removes when a command left two spellings of a name → every spelling (PR #1390, pending the maintainer)

    Written by an agent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature:planned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions