Repository navigation
Description
Part of #1110.
Scope
An agent on another machine runs simlock tunnel reverse <device-port>[:<caller-port>] … against a lone worker or a gateway, and its leased Android device reaches the caller's loopback ports while the command runs. This adds the tunnel CLI command, a WebSocket client port built on ws and loaded with import() only when simlock tunnel first opens a socket, an HTTP lookup port (GET /v1/identity, GET /v1/leases), the fake driver's device-tcp use in a fast end-to-end test against a lone worker, and the slow-lane emulator test for reverse against a lone worker. It corrects the comment in src/ports/uplink-websocket.ts that says Node 22's client cannot set headers (it can; ws is chosen for the refusal body and pings). The CLI reference describes the command, SIMLOCK_URL and SIMLOCK_TOKEN, and the global --token's new wording.
In short
After this PR, on the caller's machine:
$ export SIMLOCK_URL=https://worker.example:7300 SIMLOCK_TOKEN=sk_agent_…
$ simlock tunnel reverse 5173 reverse 8081:3000
reverse device:5173 -> localhost:5173 (lease 3f9c)
reverse device:8081 -> localhost:3000 (lease 3f9c)
reverse 5173: nothing listening on localhost:5173 # stderr, at most once per 10 s per tunnel
^C # close sent (waited at most 2 s), exit 0
--url overrides SIMLOCK_URL; the global --token overrides SIMLOCK_TOKEN and is sent as Authorization: Bearer <token>. --lease <id> names the lease; without it an agent token's only lease is used (its role from GET /v1/identity, its leases from GET /v1/leases), and an operator token must name one. Every failure is one JSON line on stderr, {"error":{"code":"<code>","message":"<message>"}}, with the codes and exits of #1110's "CLI exits and bounds" row: a usage error exits 2; a refusal exits 1 with its code and Lease <id>: <message>; a lost connection (including 45 s with no message or ping, or 90 s with no READY) exits 1 with TUNNEL_DISCONNECTED; the lease ending exits 14 with TUNNEL_LEASE_ENDED and Lease <id> ended: <reason>.
flowchart LR
U[simlock tunnel - new] -->|GET /v1/leases when no --lease - new port| H[daemon HTTP]
U -->|ws, loaded on first use, Authorization: Bearer - new port| T[/v1/leases/id/tunnel]
T --> M[worker tunnel module]
U -->|per OPEN| L[caller localhost:port]
| Input | Today | After |
|---|---|---|
simlock tunnel reverse 5173 |
unknown command | tunnel open |
reverse 8081:3000 |
unknown command | device 8081 → caller 3000 |
reverse 80 |
unknown command | the worker refuses: exit 1 TUNNEL_BAD_PORT |
reverse 70000, reverse abc, reverse 5173:, reverse 5173:0, no tunnel |
unknown command | usage error, exit 2 |
reverse 5173 reverse 6000:5173 (same caller port) |
unknown command | usage error, exit 2 |
--url http://10.0.0.5:7300 |
unknown command | usage error: http:// only on loopback |
no --url, or no token |
unknown command | usage error naming the flag and the variable |
no --lease; an agent token with 0 or 2 leases; an operator token |
unknown command | usage error naming them, exit 2 |
If this goes wrong, the command keeps running after its tunnels are gone, or a large page stalls halfway.
Technical spec
Facts
Commit 31ff66e.
| # | Fact | Source |
|---|---|---|
| F1 | Top-level commands are registered in the CLI tree; #1108 adds rotate there and to the root usage |
src/cli/tree.ts:131-132, #1108 Modules touched |
| F2 | The CLI may import ports and contract, never modules, daemon, drivers, gateway, http |
src/cli/AGENTS.md:5-8 |
| F3 | Every CLI failure is one JSON line on stderr; its code and exit come from the contract's error table; a usage error exits 2 | src/cli/errors.ts:45, src/cli/errors.ts:56, src/cli/AGENTS.md:51-52 |
| F4 | Exit 14 is LEASE_LOST_EXIT_CODE, no row in the error table; simlock lease returns it when its lease is lost |
src/contract/errors.ts:216-217, src/cli/commands/lease.ts:223 |
| F5 | --token is a global flag, stripped before any command runs, handed to the command as context.token; its help says "connect as admin" |
src/cli/index.ts:45, src/cli/index.ts:60, src/cli/index.ts:223 |
| F6 | Node 22.13's bundled undici WebSocket sends custom headers, so the comment in uplink-websocket.ts is wrong; but it turns any non-101 answer into one generic error with no status or body, and answers pings without telling the caller |
v6.21.1:lib/web/websocket/connection.js:59-63, v6.21.1:lib/web/websocket/connection.js:108-111, v6.21.1:lib/web/websocket/receiver.js:385-399 (undici); src/ports/uplink-websocket.ts:6-8 |
| F7 | GET /v1/leases lists the caller's leases, role agent; an operator sees every lease |
src/http/lease-declarations.ts:285-292 |
| F8 | resolveRemoteLeaseId (moved to a shared CLI file by #1108) picks the only lease and errors on none or several, filtering by requester for an admin session; its messages name requesterId and SIMLOCK_AGENT_ID |
src/cli/commands/passthrough.ts:106-126 |
| F9 | The upgrade router answers a path it does not route with a bare 404 Not Found; lease ids match [^/]+ |
src/ports/upgrade-router.ts:24-31 |
| F10 | TcpStreamConnector connects to a loopback port; TcpStream gains end() in #1112 |
src/ports/tcp-stream.ts:6-14, #1110 Shared values "byte streams" |
| F11 | The codec, codes, limits and close codes are #1110's Shared values; the worker side is #1112, the relay #1113 | #1110 Shared values |
| F12 | ADR 0005 accepts wss://, or ws:// only on loopback, for a gateway URL; WHATWG URL.hostname gives [::1] for IPv6 loopback |
docs/internal/adr/0005-gateway-and-worker-modes.md:116-121 |
| F13 | The slow Android lane runs e2e/slow-android-*.test.ts |
e2e/slow-android-smoke.test.ts:1 |
| F14 | adb reverse reaches a host loopback listener (adb 37.0.0) |
run 2026-10-30: host listener received "reverse-ok\n" |
| F15 | Only uplink-websocket.ts and upgrade-router.ts import ws, so the CLI and MCP never load it; the CLI may import a ports file only through index, testing or definition |
src/ports/AGENTS.md Rules, .dependency-cruiser.mjs:29-44 |
| F16 | GET /v1/identity answers who the token in the request is, with its role |
src/http/route-declarations.ts:78-85 |
| F17 | src/cli/lazy-loading.test.ts checks which modules each command loads |
src/cli/lazy-loading.test.ts:1 |
Modules touched
Searches: name: "adb" in src/cli/tree.ts, resolveRemoteLeaseId, SIMLOCK_ in src/cli/environment.ts, connect as admin.
src/cli/—commands/tunnel.ts(new),tree.ts,index.ts(usage; the global--tokenhelp: "the credential a command sends"),environment.ts(SIMLOCK_URL,SIMLOCK_TOKEN); the shared resolver takes a lease list and the caller's role instead of an admin client, and words its errors for the caller (--lease, the token) when called bytunnel;lazy-loading.test.tsgainstunneland shows no other command loadsws.src/ports/—websocket-client.ts(new, in the barrel: the port, its fake, and a Node adapter that loadswswithimport()on firstconnect, exposing a refused upgrade's status and body and every ping);http-json.ts(new: one GET with a bearer token, 10 s, body at most 1 MiB, with its fake);uplink-websocket.tscomment corrected; the manual'swsrule names the lazy adapter.src/core/—FakeDriver's device-tcp opens fake device connections on request (test support).src/daemon/—tunnel-cli.e2e.test.ts(new).e2e/slow-android-tunnel.test.ts(new).website/— CLI reference.
flowchart LR
cli[cli tunnel*] --> ws[ports websocket-client*]
cli --> http[ports http-json*]
cli --> tcp[ports tcp-stream]
cli --> codec[contract tunnel-frames]
cli --> resolver[cli shared lease resolver*]
Contract and event changes
none: the codes and codec are #1112's; searched src/contract for this task's names: no change.
New inputs: --url an absolute https:// URL, or http:// with hostname 127.0.0.1, [::1] or localhost, at most 2048 characters; the token 1 to 4096 characters; --lease 1 to 256 characters, percent-encoded into the path; tunnel arguments reverse <d>[:<c>] with d and c integers 1 to 65535 (the worker checks d's 1024 floor), at most 8, no caller port twice.
Other code on the same state
Searches: SIMLOCK_TOKEN, SIMLOCK_URL (0 hits), context.token (each command's admin connection).
- Other commands' use of
--token: unchanged (the admin credential for the local daemon); only the help wording widens. simlock simctl/adb: keep using the shared resolver through their admin client's lease list; their tests stay as they are.
Failure modes
| Call | Outcome | What happens | Limit, counted from |
|---|---|---|---|
GET /v1/identity, GET /v1/leases |
401 or 403 | exit 1 with the body's code | 10 s, from the request |
GET /v1/identity, GET /v1/leases |
no answer | exit 1 TUNNEL_DISCONNECTED |
10 s |
| upgrade | 401, 403, 404, 503 | exit 1 with the body's code (from ws's unexpected-response); a bare 404 is UNKNOWN_LEASE |
10 s handshake |
HELLO |
no READY or REFUSED |
exit 1 TUNNEL_DISCONNECTED |
90 s, from HELLO |
| socket | REFUSED |
exit 1 with its code | — |
| socket | close 4001 | exit 14 TUNNEL_LEASE_ENDED |
— |
| socket | close 1011, 1001, dropped, or silent | exit 1 TUNNEL_DISCONNECTED |
45 s with no message or ping |
loopback connect on OPEN |
refused | RESET to the worker; the warning line |
5 s, from the OPEN |
| Ctrl-C, SIGTERM | — | close sent, exit 0 | 2 s for the close |
The command writes no state.
Rules in play
- ADR 0054 §8 (PR docs(adr): 0054 a lease holder tunnels device ports to its own machine #1111):
ws, loaded bysimlock tunnelalone. - ADR 0005 §4: a token travels in a header at upgrade time.
- Ports manual: a new external API is a port with a fake, built in the composition root.
- Architecture rule 10: the device-port floor lives in the worker only.
Tests
Seam: the CLI command with fake ports, modelled on src/cli/passthrough.test.ts; the join on a daemon end-to-end with real HTTP, modelled on src/daemon/input-lease.e2e.test.ts.
- A user runs
simlock tunnel reverse …on another machine and sees one line per tunnel, then the device reaches its loopback port; cases:5173;8081:3000; two reverses; flags vs env;--leasevs an agent token's only lease vs none vs several; operator token without--lease→ usage error; nothing listening → one warning per 10 s, tunnel stays; a 1 MiB response arrives whole (credit granted). - A user's
simlock tunnelthat cannot run exits with its code and one JSON error line; cases: usage errors (70000,abc,5173:, no tunnel, same caller port twice);http://to a non-loopback host; 401 on the upgrade; refusal → exit 1 with its code (reverse 80→TUNNEL_BAD_PORT); no message for 45 s →TUNNEL_DISCONNECTED; noREADYin 90 s →TUNNEL_DISCONNECTED; an idle tunnel with server pings stays open past 60 s; a refused upgrade shows its code (401, 404UNKNOWN_LEASE, 503TUNNEL_UNAVAILABLE); lease ends →TUNNEL_LEASE_ENDED, exit 14; Ctrl-C and SIGTERM → close sent, exit 0. - The CLI against a lone worker with fake devices opens a reverse tunnel, carries bytes, and its rules are gone after Ctrl-C; cases: reverse ok; refusal reaches the CLI as its code; Ctrl-C removes the rule.
- On a real emulator against a lone worker, a page served on the caller loads on the device through
reverse; cases: reverse.
Done when
- The Tests lines pass.
e2e/slow-android-tunnel.test.tspasses in the slow lane on a machine with the Android SDK: an emulator lease on a lone worker with tunnels on;simlock tunnel reverse <p>with a loopback HTTP server on the caller; on the device,(printf 'GET / HTTP/1.0\r\n\r\n'; sleep 2) | nc 127.0.0.1 <p>prints the server's body (thencform the Facts' check used).lazy-loading.test.tsshowswsloaded bysimlock tunneland by no other command or the MCP entry.- The CLI reference describes
simlock tunnel, listsSIMLOCK_URLandSIMLOCK_TOKEN, and words the global--tokenas the credential a command sends.
Out of scope
forward(simlock tunnel forward: reach a leased device's port from the caller's machine #1115); a JS client or MCP tool.
Depends on
Written by an agent.
Activity
test: the CLI against a lone worker with fake devices refuses with th…
feat(cli): a tunnel the worker refuses exits with the worker's code (c…
test: a user's simlock tunnel that cannot run exits with its code and…
feat(cli): simlock tunnel checks its arguments, url and token before …
test: simlock tunnel prints one line per tunnel and words its lease e…
feat(cli): simlock tunnel words its lease errors for the token and an…
test: nothing listening warns once per 10 s per tunnel and a 1 MiB re…
feat(cli): simlock tunnel warns when nothing listens and honours the …
test: simlock tunnel that is refused or loses its worker exits with t…
feat(cli): simlock tunnel exits with the worker's code, times out sil…
test: lazy-loading shows ws loaded by simlock tunnel alone (callstack…
test: the http-json and websocket-client adapters against a real serv…
docs(cli): the tunnel command, its lease resolver and the lazy ws ada…
test: a page on the caller's loopback port loads on a real emulator t…
chore(tunnel): Fallow findings of the cli and ports cycles (callstack…
3 remaining items
chore(tunnel): merge origin/main into task/1114 (callstackincubator#1114
fix(tunnel): terminate the socket after the 2 s Ctrl-C bound; name th…
test(tunnel): the 45 s silence and 90 s no-READY ends terminate the s…
V3RON commentedon Nov 1, 2026
Handoff
Done:
e2e/slow-android-tunnel.test.ts.Not done: the review cap is reached. The fix-check raised R4: the 90 s and 45 s timeout tests never asserted that the socket is terminated. R4 is fixed in 50c1f69, two test assertions only. Both were probed: removing
, truefrom each timer'sendcall fails the matching assertion. No lens has reviewed the fix.Findings: R1, R2 and R4 are fixed. R3 (
--urlis origin-only) is nonblocking.Blocked on: the maintainer, who either accepts 50c1f69 and merges, or asks for one more check. #1115 (#1127) waits on this PR.
Written by an agent.
feat(tunnel): simlock tunnel reverses a leased device's port to the c…