Repository navigation
Description
Part of #1110.
Scope
A holder opens GET /v1/leases/{id}/tunnel on a gateway, and the gateway relays it to the lease's worker over one two-way relay, as it relays the input socket. This adds both sides of that relay: the worker's relay door into #1112's tunnel sessions (channel framing, a heartbeat every 5 s, close frames) and the gateway's bridge, refusals and drain. The route's published statuses and the remote-agents page describe tunnels through a gateway.
In short
After this PR, the same tunnel socket works through a gateway: the gateway resolves the lease, opens a two-way relay to the worker's tunnel module with the worker's own lease id as the relay's lease argument, and carries frames both ways, up to 65541 bytes each. An idle tunnel stays open, because the worker's heartbeat keeps the relay alive. A worker with tunnels off answers 503 TUNNEL_UNAVAILABLE with Tunnels are off on worker <workerId>; turn on capabilities.tunnel.enabled there before the upgrade. A caller frame over 65541 bytes gets REFUSED BAD_REQUEST and close 4003, as on a lone worker. A worker that stops answering or stops closes the caller's socket 1011; the lease ending closes it 4001; the gateway stopping closes it 1001.
flowchart LR
C[holder's socket] --> G[gateway tunnel route - new]
G -->|relayTwoWay, lease argument = workerLeaseId - new| W[worker relay door - new]
W --> S[#1112 tunnel sessions]
| Gateway case | Today | After |
|---|---|---|
| tunnel socket, worker has tunnels on | 404 | upgraded and relayed |
| worker has tunnels off | 404 | 503 TUNNEL_UNAVAILABLE |
| no answer from the worker in 10 s | — | 503 WORKER_UNREACHABLE |
| tunnel idle for a minute | — | stays open |
| worker stops answering, or is stopping | — | close 1011 |
| lease ends | — | close 4001 with the reason (lease ended if the worker's close does not come within 2 s) |
| gateway stops | — | close 1001 |
If this goes wrong, a user sees a quiet tunnel through a gateway drop after 15 s, or stay open after the worker is gone.
Technical spec
Facts
Commit 31ff66e.
| # | Fact | Source |
|---|---|---|
| F1 | The input gateway side opens relayTwoWay(workerId, path, …, lease.workerLeaseId); the worker target reads only the lease argument |
src/input/gateway-link.ts:125-133, src/input/worker-link.ts:58-59, src/modules/host.ts:244-250 |
| F2 | The input relay channel frames [len u32][tag u8][payload], heartbeat 0xF0 every 5 s, close 0xF1 {code, reason} |
src/input/framing.ts:7-31, src/input/channel.ts:5-32 |
| F3 | The input bridge reports a worker's 1001 as 1011 Worker <id> stopped answering |
src/input/gateway-link.ts:93-94, src/input/AGENTS.md:44 |
| F4 | The gateway ends a two-way relay after 15 s with no byte from the worker | src/gateway/relay.ts:46, src/gateway/relay.ts:427 |
| F5 | Refusals before the upgrade: RELAY_TARGET_MISSING → 503 with the module's code; no head in 10 s → 503 WORKER_UNREACHABLE |
src/input/AGENTS.md:47-49 |
| F6 | On leases.onRemoved the input bridge waits 2 s for the worker's 0xF1, then closes 4001 |
src/input/AGENTS.md:45 |
| F7 | The input gateway side closes its bridges at beforeDrain with 1001 |
src/input/gateway.ts:14, src/input/gateway-link.ts:110-113 |
| F8 | The input bridge drops client frames over its bound silently | src/input/gateway-link.ts:78-79 |
| F9 | A route's statuses in its definition feed the OpenAPI document; the input route's 503 is pinned by a test | src/input/definition.ts:29-33, src/http/openapi.test.ts:402-412 |
| F10 | The input end-to-end test drives a gateway and a worker with real relays (fleet({gatewayInput, workerInput})) |
src/daemon/input-lease.e2e.test.ts:377-381 |
| F11 | The uplink opens at most 64 channels per side; a tunnel run holds one | src/link/index.ts:32 |
Modules touched
Searches: relayTwoWay( and beforeDrain in src/input, statuses in src/input/definition.ts.
src/tunnel/—worker-link.tsandchannel.ts(worker relay door),gateway.ts,gateway-link.ts(bridge),refusal.ts,definition.ts(503 statuses, description); tests.src/daemon/—tunnel-lease.e2e.test.ts(new) through a gateway.src/http/—openapi.test.tspins the 503.website/— remote-agents page: tunnels through a gateway, the capability on both sides.
sequenceDiagram
participant C as Holder
participant G as Gateway tunnel
participant W as Worker tunnel
C->>G: upgrade /v1/leases/w1.3f9c/tunnel
G->>W: relayTwoWay, lease 3f9c
alt worker off
W-->>G: RELAY_TARGET_MISSING
G-->>C: 503 TUNNEL_UNAVAILABLE
else
G-->>C: 101, then frames both ways
W-->>G: 0xF0 every 5 s
W-->>G: 0xF1 {4001, expired}
G-->>C: close 4001 expired
end
Contract and event changes
none: TUNNEL_UNAVAILABLE is added by #1112; searched src/contract for this task's names: no other change.
Other code on the same state
Searches: relayTwoWay (input, stream), onRemoved (input gateway link), beforeDrain (input, stream gateways).
- Input and stream sockets on the same lease: separate relays, one uplink channel each, from the shared 64 (F11).
- Lease removal and drain: as input (F6, F7).
Failure modes
| Call | Outcome | What happens | Limit, counted from |
|---|---|---|---|
relayTwoWay open |
RELAY_TARGET_MISSING |
503 TUNNEL_UNAVAILABLE |
— |
relayTwoWay open |
no head | 503 WORKER_UNREACHABLE |
10 s, from the open |
| relay | no byte from the worker | close 1011 | 15 s, from the last byte (F4) |
| relay | 0xF1 {1001, …} |
close 1011 Worker <id> stopped answering (F3) |
— |
| relay | 0xF1 {code, reason}, other codes |
close with that code and reason | — |
| caller frame | over 65541 bytes | REFUSED BAD_REQUEST, close 4003 |
— |
| lease removed | no 0xF1 |
close 4001 lease ended |
2 s, from the removal |
| gateway drain | — | close 1001 Simlock is stopping |
— |
No state is written.
Rules in play
- ADR 0054 §1 (PR docs(adr): 0054 a lease holder tunnels device ports to its own machine #1111): the gateway relays the socket as the input socket is relayed.
- ADR 0005 Decision 1: the gateway's port stays the only inbound port.
Tests
Seams: the daemon end-to-end with a real gateway relay and a fake worker device, modelled on src/daemon/input-lease.e2e.test.ts (fleet, real clock, no timer cases); the gateway side with a fake relay and a fake clock, modelled on src/input/gateway.test.ts:75-140; the worker relay door with a fake clock, modelled on src/input/worker.test.ts. The 15 s silence bound itself is proven in src/gateway/relay.test.ts; this task proves the heartbeat that keeps an idle tunnel under it.
- A holder opens the tunnel socket through a gateway and it is relayed to the lease's worker; cases: relayed
HELLO/READYand a full 65541-byteDATA;REFUSED→ 4003; a caller frame over 65541 bytes → 4003; worker capability off → 503TUNNEL_UNAVAILABLE; worker stops → 1011; lease ends → 4001 with the worker's reason; gateway drains → 1001. - The gateway's tunnel bridge bounds every wait on the worker; cases: no head in 10 s → 503
WORKER_UNREACHABLE; the relay ends for silence → 1011; lease removed and no worker close within 2 s → 4001lease ended. - The worker's relay door keeps an idle tunnel's relay alive; cases: an idle socket sends a heartbeat every 5 s; a close sends
0xF1 {code, reason}.
Done when
- The Tests lines pass;
openapi.test.tsshows the route's 503TUNNEL_UNAVAILABLEandWORKER_UNREACHABLE. - The remote-agents page describes tunnels through a gateway, with the capability on both sides.
Out of scope
simlock tunnel(simlock tunnel: reverse a leased device's port to the caller's machine #1114);forward(simlock tunnel forward: reach a leased device's port from the caller's machine #1115).
Depends on
Written by an agent.
Activity
test: a worker with tunnels off answers 503 TUNNEL_UNAVAILABLE throug…
feat(tunnel): a worker with tunnels off answers 503 TUNNEL_UNAVAILABL…
test: a full-size DATA is relayed and a worker's REFUSED closes the h…
feat(tunnel): the worker's close frame closes the holder's socket wit…
test: a caller frame over 65541 bytes is refused BAD_REQUEST and clos…
feat(tunnel): a caller frame over 65541 bytes is refused BAD_REQUEST …
test: through a gateway the lease's end closes 4001, a stopping worke…
feat(tunnel): a stopping worker closes the holder 1011 and a draining…
test: the gateway's tunnel bridge bounds every wait on the worker (ca…
feat(tunnel): the gateway bounds its open to 10 s and a lease's remov…
feat(tunnel): the worker's relay door sends a heartbeat every 5 s and…
test: the tunnel route's 503 TUNNEL_UNAVAILABLE and WORKER_UNREACHABL…
feat(tunnel): the route's 503 statuses, the remote-agents page and th…
chore(tunnel): merge origin/task/1112 into task/1113 (callstackincuba…
fix(tunnel): a channel frame is at most one message, the heartbeat st…
chore(tunnel): merge origin/task/1112 and share one relay-channel imp…
fix(modules): drop the pipe type from the index now that only the rel…
refactor(modules): one gateway channel bridge for input and tunnel (c…
refactor(modules): keep the channel helpers private to relay-channel (c…
feat(tunnel): the gateway relays the tunnel socket to the lease's wor…