Repository navigation
Description
Part of #1351.
Scope
A lease holder behind a gateway can use the four file routes of #1356 on its fleet lease exactly as on a worker: the gateway relays each request to the lease's worker over the worker's existing byte channel, so neither side holds a whole file. A relayed file request is cut only after 330 s in which no byte moved, not after 330 s in total, so a slow but live 2 GiB upload completes.
In short
The gateway stops answering 501 on the file routes and relays them to the worker that holds the lease, which serves them from its lease folder through a relay target named files. Uploads that keep moving are not cut by the relay's 330 s deadline; that deadline now starts once the upload body has been fully sent, and an upload silent for 330 s ends 408 UPLOAD_STALLED No bytes of <name> arrived for 330 s. The gateway refuses a Content-Length over 2 GiB itself (413 FILE_TOO_LARGE, nothing relayed), because the relay does not carry that header, and gives a download back its Content-Length from the worker's simlock-content-length header (the bytes of that body, the range's on a 206).
flowchart LR
C[HTTP caller] -->|"PUT/GET/list/DELETE"| G["gateway HTTP (changed: relays)"]
G -->|"lease.get: owner or operator"| GD[gateway dispatcher]
G -->|"GatewayRelay, target files (changed: deadline after body)"| W["worker relay target files (new)"]
W --> LF["lease-files (#1356)"]
Before and after, against a gateway at $GW:
$ curl -X PUT "$GW/v1/leases/$LEASE/files/fixture.tar" -H "Authorization: Bearer $T" --data-binary @fixture.tar
{"error":{"code":"UNSUPPORTED_IN_GATEWAY_MODE",...}} (before, after #1356: 501)
{"name":"fixture.tar","size":70000000} (after: 201)
(an upload whose bytes stop for 330 s)
{"error":{"code":"UPLOAD_STALLED","message":"No bytes of fixture.tar arrived for 330 s","name":"fixture.tar"}} (408)
Quantities, for a file PUT: opening the channel keeps today's 330 s deadline (503 WORKER_UNREACHABLE); from the open, the silence bound is 330 s with no byte read from the caller's body; after the body ends, the 330 s head deadline counts from that end; after the head, the existing 330 s idle bound on the response body. GET, list and DELETE keep today's relay timers unchanged. A user would notice if this goes wrong: an upload through a gateway cut while it is still moving, or a file stored on another worker than the lease's.
Technical spec
Facts
Commit 5aedc2d.
| # | Fact | Source |
|---|---|---|
| F1 | The relay's deadline (default 330 s) is armed in start() and its failure is WORKER_UNREACHABLE worker <id> did not finish the relayed request within <ms> ms |
src/gateway/relay.ts:40, :154-162 |
| F2 | A one-way relay uploads the request body before the worker's head can arrive, so the deadline covers the whole upload | src/gateway/relay.ts:218, :221-247 |
| F3 | The deadline is cancelled when the head arrives; the body is then bounded by silence (idleMs, default 330 s) |
src/gateway/relay.ts:44, :272-280, :437-446 |
| F4 | GatewayRelay.relay(workerId, {target, request, tenant, leaseId}); deadlineMs and idleMs are per relay instance |
src/gateway/relay.ts:17-30, :64-90 |
| F5 | The gateway builds one GatewayRelay and hands modules relay bound to their name |
src/daemon/main.ts:453-472; src/modules/host.ts:237-243 |
| F6 | The worker's relay targets are the modules' plus options.relayTargets; "the core names no relay target (ADR 0026)" |
src/daemon/worker-composition.ts:494-497; src/daemon/relay.ts:1-4 |
| F7 | An unknown target answers RELAY_TARGET_MISSING |
src/daemon/relay.ts:126-133 |
| F8 | The worker hands a relayed body to its target as a Request with a pull stream (highWaterMark: 0), so nothing is buffered |
src/daemon/relay.ts:208-229, :319-338 |
| F9 | A target that answers before the body ends errors the body: "The relay answered before its request body ended" | src/daemon/relay.ts:218, :311 |
| F10 | A gateway lease resolved for a route carries workerId and workerLeaseId |
src/http/app.ts:836-841; src/modules/types.ts:117-124 |
| F11 | A gateway's tenant for a relayed call is <requesterPrefix><principal>, prefix gw:<instance id>: |
src/stream/gateway-link.ts:60; src/daemon/main.ts (requesterPrefix: gatewayRequesterPrefix) |
| F12 | relay-gateway.e2e.test.ts already proves a 20 MiB PUT body reaches a target byte for byte, and an empty upload |
src/daemon/relay-gateway.e2e.test.ts:388, :410 |
| F13 | A worker relays status, headers and body back unchanged | src/daemon/relay-gateway.e2e.test.ts:195 |
| F14 | No live link to a lease's worker → WORKER_UNREACHABLE (503) |
src/gateway/AGENTS.md (After the grant); src/contract/errors.ts:561-565 |
| F15 | relay.request's target is capabilityNameSchema, ^[a-z][a-z0-9-]{0,31}$; files passes |
src/contract/operations.ts:1080; src/contract/status-schemas.ts:316 |
| F16 | The relay drops content-length (and other framing headers) in both directions and re-creates framing on each side |
src/contract/relay.ts:27-34, :60; src/daemon/relay.ts:327, :348 |
| F17 | The relay's body idle message today blames the worker (worker <id> sent nothing on the relayed response) |
src/gateway/relay.ts:437-446 |
Modules touched
Searches: GatewayRelay, relayTargets, deadlineMs, leaseFiles (#1356's dependency), RELAY_TARGET_MISSING.
src/gateway/relay.ts: a per-call optionupload: {name, silenceMs}: the start deadline is cancelled once the channel is open (not at the head), the request body is bounded bysilenceMswith no byte read from the caller, failingUPLOAD_STALLEDwithname, and the head deadline is armed when the body ends; the silence timer stops when the worker's head arrives first (a refusal partway).src/daemon/main.ts: the gateway'sleaseFilesdependency, of Lease files on a worker: upload, download, list and delete a lease's files #1356'shandle(request, identity, lease, name)type, sosrc/http/does not change: it refuses aContent-Lengthover 2 GiB, relays tolease.workerIdwith targetfiles, the path/files/leases/<encodeURIComponent(workerLeaseId)>[/<encodeURIComponent(name)>], tenant<requesterPrefix><identity.principal>and theuploadoption on aPUT; it copiessimlock-content-lengthintoContent-Length, and rewrites a worker's JSON refusal that names the worker lease id (UNKNOWN_LEASE,LEASE_FILES_FULL'sleaseId) to the caller's lease id, reading at most 16 KiB of it, as the stream module'swithCallersLeaseIddoes.StartDaemonOptions(test only): a lower file silence bound for the gateway and thelimitsLease files on a worker: upload, download, list and delete a lease's files #1356's store takes for the worker, so the rig reaches 408 and 413 without gigabytes or minutes.src/daemon/worker-composition.ts: registerfilesinrelayTargets, served byLeaseFiles.src/lease-files/: the relay target (path/files/leases/<worker lease id>[/<name>], the lease looked up in the worker's registry: 404UNKNOWN_LEASEwhen gone).src/daemon/relay.ts: the why-comment (ADR 0067 §7).- Docs:
remote-agents.mdx(through a gateway),src/gateway/AGENTS.md,src/daemon/AGENTS.md,src/lease-files/AGENTS.md.
flowchart LR
GH["gateway http app"] --> MF["daemon/main leaseFiles (changed)"]
MF --> GR["gateway/relay (changed)"]
GR -->|uplink channel| DR[daemon/relay]
DR --> T["files target (new, lease-files)"]
T --> LF[lease-files]
sequenceDiagram
participant C as Caller
participant G as Gateway
participant W as Worker
C->>G: PUT /v1/leases/GL1/files/fixture.tar
G->>G: lease.get GL1 → workerId W1, workerLeaseId L1 (404 UNKNOWN_LEASE)
G->>W: relay.request target files, /files/leases/L1/fixture.tar, channel
alt worker gone
G-->>C: 503 WORKER_UNREACHABLE
end
G->>W: body bytes (silence bound 330 s)
W-->>G: head 201 / 413 / 409 / 404 (deadline 330 s from the body's end)
G-->>C: same status, headers, body
Contract and event changes
This PR bumps the protocol by one only if relay.request changes; the end-of-body deadline is the gateway's own timer, so the expected answer is none after a search of relay.request's schema (src/contract/operations.ts:1075-1095). No event. The relay path /files/leases/<worker lease id>[/<name>] is gateway-to-worker only; the worker checks the lease id against LEASE_ID_PATTERN and the name against the File name rule, 400 BAD_REQUEST otherwise.
Other code on the same state
Searches: new GatewayRelay (1 hit, src/daemon/main.ts:453), relayTargets (worker composition and module host), deadlineMs in src/gateway/ (relay only).
| Component | State | When both act | Which wins |
|---|---|---|---|
| agent-device and stream relays | the shared GatewayRelay and its timers |
they never set the new option and keep today's deadline | independent |
| A module relay target | the worker's target map | files is reserved as a module name (#1356), so no module can register it |
the core |
| The 64-channel link limit | open channels per side | a 65th relay waits for a free channel, bounded by the start deadline as today | the link |
| #1356 lease end on the worker | the folder | a relayed PUT running when the lease ends answers 404 UNKNOWN_LEASE through the relay |
the lease end |
Failure modes
| Call | Outcome | What happens | Limit, counted from |
|---|---|---|---|
lease.get on the gateway |
unknown or not the caller's | 404 UNKNOWN_LEASE, nothing relayed |
— |
GatewayRelay.relay |
no live link | 503 WORKER_UNREACHABLE |
at once |
GatewayRelay.relay |
worker refusal (413, 409, 404, 400, 422) | relayed with its status and headers; a JSON body whose error.code is UNKNOWN_LEASE or LEASE_FILES_FULL gets the caller's lease id in place of the worker's (message and leaseId); any other body, one over 16 KiB, or one that is not JSON is relayed unchanged |
the first 16 KiB of the body, read once |
GatewayRelay.relay |
worker without the files target (older version) |
503 RELAY_TARGET_MISSING relayed as the worker answers it |
— |
| channel open | not within 330 s | relay.cancel, 503 WORKER_UNREACHABLE, as today |
from the request's start |
| request body | silent for 330 s | relay.cancel, 408 UPLOAD_STALLED; the worker removes its temp file (#1356's cut) |
from the channel's open or the last byte read from the caller |
Content-Length |
over 2 GiB | 413 FILE_TOO_LARGE, nothing relayed |
before any byte |
| head | none within 330 s after the body ended | relay.cancel, 503 WORKER_UNREACHABLE |
from the body's end |
| response body | silent for 330 s | the body errors | from the head or the last byte |
| uplink | drops mid-upload | the relay fails WORKER_UNREACHABLE; the worker's body errors and #1356 removes the temp file |
— |
State written: none on the gateway; the worker's is #1356's.
Rules in play
- ADR 0067 §2: "A gateway relays each to the lease's worker over the existing relay channel"; §6 the silence bound, the gateway's own
Content-Lengthcheck and the size header; §7 the core targetfiles. - ADR 0026 §1 as narrowed by 0067: the daemon names one relay target of its own.
- ADR 0005 §19b, ownership checked at the gateway:
lease.getthere, and the worker serves the relayed lease id it was given, as the stream's relay does. - Gateway rule "A worker must not be able to crash, stall or corrupt its gateway": the gateway reads at most 16 KiB of a refusal and parses only those two codes' bodies, as the stream module's
withCallersLeaseIddoes; everything else is relayed as bytes. - Documentation rule 9:
remote-agents.mdxsays the routes work through a gateway.
Tests
Seam: the gateway and worker relay rig (src/daemon/test-relay-support.ts, which runs on the system clock), modelled on src/daemon/relay-gateway.e2e.test.ts, with the real files target over a temporary folder and the test-only lower silence bound and limits; the 330 s timers themselves with the fake clock in src/gateway/relay.test.ts.
- Through a gateway, a lease holder's PUT, GET, list and DELETE reach the lease's worker and answer as on a worker. Cases: 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.
- Through a gateway, a file upload is cut only by 330 s of silence, not by 330 s in total. Cases: body still flowing past the bound in total succeeds; body silent past the bound → 408 UPLOAD_STALLED over HTTP (rig); channel open past 330 s → 503 WORKER_UNREACHABLE (fake clock); a non-file relay still has its 330 s head deadline (fake clock).
- 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. Cases: another agent; disconnected worker; a worker's UNKNOWN_LEASE or LEASE_FILES_FULL names the caller's lease id.
Done when
pnpm exec vitest run --project unit src/daemon/relay-gateway.e2e.test.tspasses with the first and third Tests lines.pnpm exec vitest run --project unit src/gateway/relay.test.tspasses with the second line's fake-clock cases, and the rig file with its 408 case.remote-agents.mdxsays the file routes work through a gateway, and its curl session uses$SIMLOCK_URLfor either.
Out of scope
- Resumable uploads (feature Non-goals).
- Any change to the agent-device, stream, input or tunnel relays.
Depends on
Written by an agent.
Activity
test: a gateway gives a download its Content-Length and answers a Ran…
feat(lease-files): a gateway gives a download its Content-Length from…
test: a gateway refuses a Content-Length over 2 GiB with 413 FILE_TOO…
feat(lease-files): a gateway refuses a Content-Length over 2 GiB befo…
test: through a gateway, a worker's 413, 409 and 404 reach the caller…
feat(daemon): a worker takes lease file limits for tests (#1358)
test: through a gateway, ten 20 MiB uploads at once are all stored (#…
test: through a gateway, a caller that does not hold the lease gets 4…
feat(lease-files): a gateway gives the caller's lease id back in a wo…
test: through a gateway, a file upload is cut only by silence, not by…
feat(gateway): a relayed file upload is cut by 330 s of silence, and …
test: a gateway cuts a file upload only after 330 s of silence and ke…
docs(lease-files): the file routes work through a gateway (#1358)
refactor(gateway): split the file relay and the upload exchange into …
test(gateway): the upload relay test fails on its named assertion whe…
chore(stack): merge task/1357 into the lease files client (#1358)
fix(gateway): an upload the worker stopped reading still ends at the …
test(daemon): the relay test's second lease waits until the fleet can…
docs(adr): ADR 0067 is Accepted (#1358)
feat(lease-files): a gateway relays the file routes to the lease's wo…