Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ADR 0028 — File transfers in parts (file store links)

Status: accepted · 2026-10-31 (owner request: files of 1 GB and more through the brokers and the file store links of ADR 0027).

Context

ADR 0027 moves a file as one action: upload takes it as one argument, download returns it as one reply. Every hop holds it whole (the args, the Zenoh query, the HTTP body to the store), so the links cap uploads at 64 MiB (the gateway’s POST /api/invoke body) and downloads at 16 MiB. A file of 1 GB would take several times its size in memory on a board with 2 GB, and one query of that size through a chain of brokers.

Decision

  • Uploads in parts, spooled on the link’s disk (wyrm_filestore::parts). A folder’s upload_begin {name, size, part_size?, content_type?, overwrite?, sha256?} (level write) answers {upload, part_size, parts}. The connection’s upload_part {upload, index, data} writes one part at its offset into a sparse spool file (<data_dir>/uploads/c-<conn>/<id>.part, 0700; the prefix keeps a connection named . or .. below uploads). Parts may come in any order, and again after a cut; upload_status {upload} lists the missing ones (at most 1000). upload_commit {upload} answers committing at once and sends the spool file to the store in the background (a large file takes longer than a request may wait); upload_status then says done with the Upload File answer, or failed with why. upload_abort drops it. Parts are 64 KiB … 16 MiB (default 4 MiB); a file is at most max_file_mib (default 4 GiB, at most 1 TiB).

  • To the store as it is read: Backend::put_file(parent, name, src, size, …):

    • S3: CreateMultipartUpload, parts of multipart_mib (default 16 MiB, at least 5 MiB, larger for files over 10 000 parts), CompleteMultipartUpload with If-None-Match: * unless it overwrites, AbortMultipartUpload on a failure.
    • Drive: a resumable session (uploadType=resumable, PATCH to replace), chunks of upload_chunk_kib (a multiple of 256 KiB) with Content-Range, resumed from Drive’s Range on 308. The session URL must be on the API’s origin.
    • WebDAV: one PUT whose body is streamed from the spool file (http::Request::file_body).
    • localfs: a copy from the spool file, through the same temporary file and rename as an overwrite.

    One part (S3, Drive) or one 256 KiB read buffer (WebDAV, the HTTP client) is in memory at a time.

  • Downloads in parts: a file’s download_part {offset, length?} (level read, length ≤ 16 MiB, default 4 MiB) answers {data, offset, size, eof}, through Backend::get_range: an HTTP Range request (S3, Drive, WebDAV; a store that ignores Range for an offset past 0 is refused rather than read whole), a seek and a read (localfs).

  • Ownership and bounds. An upload belongs to the ptok subject that began it: another subject’s request answers not_found. At most 4 uploads hold a spool file per connection (receiving, committing, or failed and kept for another commit; counted and taken under one lock), so the spool is at most 4 × max_file_mib; parts, downloads and commits share the connection’s two transfer slots. An upload idle for an hour is dropped with its spool file; so are all of a connection’s when it stops or is edited, and all spool files on the link’s start. A sha256 given at begin is checked before the file goes to the store.

  • Same path through the brokers: each part is an ordinary action, so a chain of brokers carries it as any other (one query per part, at most 16 MiB). A client sends parts through the gateway’s POST /api/invoke (a WebSocket frame holds 64 KiB).

Consequences

  • Memory no longer grows with the file: a 512 MiB transfer in parts of 8 MiB (debug build, the link, the client and Zenoh in one process) peaked at 133 MiB of resident memory.
  • The link’s disk holds a file in progress whole (sparse at first); max_file_mib bounds it, and upload_begin refuses a file larger than the free space of the spool’s disk minus 1 GiB (resource_exhausted), checked again at each part for the bytes still to come (ADR 0029, decision 11).
  • A connection’s uploads in progress are lost when it stops or the link restarts (the spool is not kept; the client begins again).
  • Not done: presigned URLs (bytes past the brokers when a client can reach the store); a server-side copy between connections; a hash of a download (the client checks it against the store’s etag where that is a digest).

Alternatives

  • Parts straight to the store at each upload_part (S3 parts, Drive chunks): no spool, but parts must come in order for Drive and WebDAV, S3 parts must be at least 5 MiB, and a retry from the client has to match the store’s rules. The spool takes parts in any order for every store.
  • A stream action for downloads: one request, but a stream reply cannot be resumed at an offset after a cut, and the gateway’s HTTP invoke returns the last item only.

Amendment 2026-11-15 — parts through the user gateway

Found live: a restore of a 5.8 MB backup from Studio failed with SocketException: Write failed (Broken pipe), nothing in the broker log. A part (4 MiB by default, never under 64 KiB for a file store) does not fit a gateway WebSocket frame (64 KiB), so the Dart client sends it by POST /api/invoke. The gateway takes that route only for an action with an editor: "file" parameter (ADR 0011), and restore_part (ADR 0048 §3) and upload_part declared none: every part was refused with 400 takes no file. The client never saw the refusal: the gateway read at most 64 KiB of the body and closed, and Dart’s HttpClient writes the whole body before it reads the answer.

Decided (ADR 0029):

  • The marker stays editor: "file" (spec 02 unchanged: the value is the contents of a local file, and a part is a piece of one). Every binary parameter that can be larger than a frame carries it: restore_part.data, a connection’s upload_part.data, the flow engine’s put_artifact.bytes, sys/profiles ▸ put.profile, as install_zip, install_wasm, install_backup, a folder’s upload and storage import already did. A generic action form shows a file picker for it. The gateway rule is unchanged: a large body only for an action that declares such a parameter. A string or map argument (a branch import, import_dsa, a flow def) cannot take the route and stays within a frame; rule for link authors in docs/links.md §1.
  • A refusal reaches the client. A declared Content-Length over the limit (64 MiB + 64 KiB) is answered 413 before anything is read. Any other refusal before the body is read (key, file parameter, level, step-up, the user’s upload in progress, both upload permits in use) is answered after the gateway reads and drops the body: at most the limit, within 120 s, one chunk in memory, at most 4 at once; the refused request holds neither the user’s upload slot nor a permit. With all 4 in use, or when the client sent Expect: 100-continue (it reads the answer first), the body is not read. An unauthenticated request is not read either: a client whose session ended can still see a reset, and the Dart client then throws a WyrmException (disconnected, “the gateway closed the connection while <action> sent <n> bytes …”) instead of a SocketException.
  • Tests: wyrm-gateway http_invoke_takes_large_arguments (a 4 MiB refused body gives 400 and the text, 403 before 4 MiB are taken, a 4 MiB upload after them, 413 by the declared length), wyrmd e2e uploads_in_parts_through_the_gateway (a backup read through the gateway, restore_begin + restore_part by POST /api/invoke + restore_check; upload_begin + upload_part + upload_commit to a localfs store), Dart bulk_invoke_test.dart and the gateway e2e case of a 4 MiB refusal.

Amendment 2026-11-15 (2) — the Rust client and large text arguments

After #99 the Rust client (wyrm-gateway-client: wyrm-agent, the MCP connector wyrm-mcp --gateway, wyr probe) still sent every invoke in a frame: an argument over 64 KiB closed its socket. Decided (ADR 0029):

  • The Rust client takes the route as the Dart client does. Conn::invoke sends encoded args over 60 KiB (MAX_FRAME_ARGS) by POST /api/invoke on the endpoint and session of its login, with the same TLS roots, and smaller ones in a frame. It sends Expect: 100-continue and the body only after 100 Continue (or after 10 s without an interim answer, RFC 9110): the gateway checks the session, the key, the file parameter, the level and the step-up before it reads the body, so every refusal (also 401 for a session that ended, which the gateway never drains) comes back before the body goes, as Error::Refused {status, message, http} with the gateway’s text. Timeouts: 15 s for the head, 125 s for the body, 185 s for the answer. Over 64 MiB + 64 KiB (MAX_INVOKE_BODY, the gateway’s value; a gateway test asserts both limits are the same) it refuses with Error::TooLarge before anything is sent.
  • No frame over 64 KiB leaves the client. A set, check, history or any other request over MAX_FRAME is Error::TooLarge at once: the gateway would close the socket and every stream on it. The MCP dry run does not ask the link’s check for such arguments (“not asked …; checked against ~meta only”); the call itself goes by the route. A Conn::from_raw connection (the gateway’s own /mcp, in process) has no HTTP route: an invoke over 60 KiB is TooLarge there.
  • A large string or map argument has no route; the limit stays. A branch import {json}, import_dsa {json}, a flow def (deploy, validate, put_block) and a storage publish value must fit 60 KiB. They are not marked editor: file: spec 02 makes that a binary parameter with a file’s bytes, and none of them is one. They get no file form now: a large tree goes as several branches (exported and imported one connection or one device at a time), and storage values stop at 32 KiB. The gateway’s refusal says what to do: “<action> takes no file: invoke it over the WebSocket, with arguments under 60 KiB; only a parameter with editor: file takes more”. When a real case needs more, that action gets a binary editor: file parameter beside the text one, as storage import {data | file} has; the gateway and the clients need no change.
  • wyrm-agent invoke takes --args-file <path> and --file <param>=<path> (the bytes of a file as a binary parameter, at most 64 MiB + 64 KiB).
  • Tests: wyrm-gateway rust_client_sends_large_arguments_by_http (frame and route give the same result up to 4 MiB; a non-file action’s 4 MiB refused with the text; 403 for a viewer; TooLarge over the limit and for a set/check over a frame, the socket still open; 401 with “log in again” after the user is disabled), wyrm-mcp a_large_file_argument_goes_by_http (300 KiB $bytes over TLS with --gateway-ca, the dry run, a text argument refused), wyrmd e2e uploads_in_parts_through_the_gateway (now restore_part and upload_part by the Rust client, a 100 KiB storage value refused with the text), wyrm-agent invoke_arguments_from_files.