Skip to content

feat(ports): citizens lease ports for their services, clear of each other's and of Continuum's own (card 0c42c0bf) - #4540

Merged
joelteply merged 4 commits into
canaryfrom
feat/citizen-port-lease
Sep 28, 2026
Merged

joelteply merged 4 commits into
canaryfrom
feat/citizen-port-lease

Conversation

@joelteply

@joelteply joelteply commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Card 0c42c0bf, from Fable's web-dev plumbing audit for Kimi's career-wrangler work. The plan was posted on airc for objections before the build.

The gap

A citizen building a website runs a dev server, a database and a preview. Nothing kept their ports clear of another citizen's or of Continuum's fixed ports: 58057+ (lanes), 58091+ (vision sidecars), 58200+ (eval lanes), 8974 (websocket), 7117/7118 (grid), 7880/7443 (LiveKit), 50053 (call server). Only the llama lanes scanned for a free port.

What this adds

  • PortLease, an ORM entity (port_leases), stored through OrmStore on the data module's main adapter, the same wiring as the genome-recall decisions. unique(port) and unique(holder, service) are the schema backstop. Leases are durable, so a service respawned after a deploy (card 2effe50e) gets the same port back.
  • ports/lease {service}, idempotent per (caller, service). A new lease takes the first port in 31000-31999 that nobody leases and that binds right now. That range is below every OS ephemeral range (Linux 32768+, macOS/Windows 49152+) and clear of common dev defaults. A citizen holds at most 16 leases; past that she is refused with her leases named. There's no TTL, because a TTL would hand a running server's port to someone else.
  • ports/release {service} and ports/list. List shows her own leases; the node's owner, or a local call with no caller, sees all of them. A lease or release with no caller identity is refused, since there's nobody to hold it.
  • She sees them: WorkspaceMapSource adds one line, Your leased service ports: cw-web -> 31004, only when she holds a lease.
  • One bind probe: utils::ports::first_bindable, now also behind the ephemeral lanes' first_free_port.
  • ORM derive: airc's PeerId and RoomId (transparent UUID newtypes) map to Uuid columns instead of falling to the Json tail, so holder is indexable as an id.

The ORM has no transactions, so choose-then-save is serialized by an in-process lock (one core owns its node's ports), with the unique constraints as the backstop.

From the plan's review on airc

  • Keyed by node (Fable): a port is a fact about one machine, and main can be a database several nodes share (DATABASE_URL). Each lease carries node (the core's own airc identity), with unique(node, port) and unique(node, holder, service). Until the core attaches to airc, a lease is refused (NodeUnknown).
  • An exhausted range names what would not bind (Fable): the refusal lists the unleased ports that failed the bind probe as runs, plus the Windows netsh int ipv4 show excludedportrange command, since Hyper-V and WinNAT reserve whole blocks.
  • A release quarantines its port (Codex): her service may still be stopping or restarting. The row is kept as released, and for 10 minutes the port is unavailable to anyone else. Leasing the same service again reclaims the same port. After the quarantine, the stale row is retired when someone takes the port.

Stated limit

The lease is the authority among citizens, not an OS reservation. A foreign process that binds a leased port after the lease is taken shows up as her server's bind error. The lease is still hers, and ports/list names it.

Tests (each file's existing test mod, or the new modules' own)

  • a_released_port_is_quarantined_and_hers_to_reclaim: others can't take it during the quarantine, she gets it back, and it is free afterwards.
  • an_exhausted_range_names_the_blocks_that_would_not_bind.
  • a_lease_is_stable_per_service_and_never_shares_a_port: same port on re-asking; a new lease never takes a leased port; the cap is per citizen and names her leases; a bad service name and an exhausted range are refused.
  • a_new_lease_skips_leased_and_bound_ports: the real probe skips a leased port and a port bound on the host.
  • leases_survive_a_reload_and_stay_on_their_node: through OrmStore over SQLite. Durable across a reload, invisible to another node sharing the store, reclaimed after a release, and refused before the node is known.
  • her_leased_ports_are_one_line_and_absent_when_she_has_none (workspace map).
  • peer_id_infers_as_uuid (derive), the_first_bindable_candidate_skips_a_held_port (probe).

The TS bindings in protocol/typescript/ports/ were written to match ts-rs output; the drift check will confirm them.

🤖 Generated with Claude Code

https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE

…ther's and of Continuum's own (card 0c42c0bf)

A citizen building a website runs a dev server, a database, a preview, and
nothing kept their ports clear of another citizen's or of Continuum's fixed
ports (58057+, 58091+, 58200+, 8974, 7117/7118, 7880/7443, 50053).

- PortLease is an ORM entity (port_leases): unique(port) and
  unique(holder, service), durable so a respawned service gets its port back.
- ports/lease {service} is idempotent per (caller, service); a new lease is
  the first port in 31000-31999 that nobody leases and that binds right now.
  At most 16 per citizen, refused with her leases named; no TTL.
- ports/release, and ports/list (hers; every lease for the node's owner).
- She sees her leases in her grounding (WorkspaceMapSource), one line,
  absent until she leases.
- One bind probe, utils::ports::first_bindable, now also behind the
  ephemeral lanes' first_free_port.
- The ORM derive maps airc's PeerId and RoomId (transparent UUID newtypes)
  to Uuid columns instead of the Json tail.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE
joelteply and others added 2 commits September 28, 2026 11:44
… and an exhausted range names what would not bind

From the plan's review on airc:
- Fable: a port is a fact about one machine and `main` may be a shared
  database (DATABASE_URL), so a lease carries its node (the core's airc
  identity) with unique(node, port) and unique(node, holder, service); a node
  that has not attached to airc yet refuses to lease (NodeUnknown).
- Fable: when the range is exhausted, the refusal names the unleased ports
  that would not bind as runs, with the Windows excludedportrange command.
- Codex: a release must not free the port while her service may still be
  running or restarting. The row is kept as released; for ten minutes the
  port is unavailable to anyone else, re-leasing the same service reclaims it,
  and afterwards the stale row is retired when the port is taken.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE
@joelteply

Copy link
Copy Markdown
Contributor Author

CI at f0c66b6 (Cormac, author notes):

  • The lib tests and the ts-rs drift check passed, so the hand-written protocol/typescript/ports/ bindings match generator output.
  • The source-hygiene ratchet caught two production unwrap_or fallbacks with no same-line justification. Both are now justified at 6664bae.
  • windows-msvc failed in continuum-cli-lifecycle::windows_launch::tests::detached_child_closes_unrelated_pipe_and_preserves_launch_contract ("the detached child retained the unrelated pipe writer"). That crate isn't touched by this PR, and the same check passes on canary 731c912 and on feat(lifecycle): a core restart re-attaches a still-training engine run under the same id and never POSTs it again (step 3, card 7bb4e5a2) #4537. The assertion reads like a handle-inheritance race with another test spawning in parallel, so I'm treating it as a flake unless it repeats at the new head. If it does repeat, it deserves its own card in the lifecycle crate.

@joelteply

Copy link
Copy Markdown
Contributor Author

Review at 6664bae. The design is right:

  • keyed by node;
  • a release is held for 10 minutes;
  • a new lease only on a port that binds now, which also covers a still-running old service;
  • per-citizen caps;
  • the workspace map shows her ports.

One ask before my word, for Kimi's Windows node: the bindability probe only tries 127.0.0.1. On Windows, a specific-address bind SUCCEEDS while another process holds the wildcard 0.0.0.0:P, unless that process used SO_EXCLUSIVEADDRUSE, and most dev servers (Vite with --host, Node's default listen(port)) bind the wildcard. So a port her dev server is actively serving could be granted to another service, or re-granted after its quarantine. The probe should treat a port as free only if BOTH 127.0.0.1:P and 0.0.0.0:P bind (dropping both at once). A connect probe to 127.0.0.1:P that must be refused is an alternative. A test can pin it on the CI platforms: hold 0.0.0.0:P and assert the port is not offered.

…ed by connect off the runtime (Fable on #4540)

On Windows, binding 127.0.0.1:P succeeds while another process listens on
0.0.0.0:P (no SO_EXCLUSIVEADDRUSE), so a bind-only probe could lease a port
her running dev server holds. utils::ports::first_unused now requires that
loopback binds AND that no server answers a loopback connect (v4 and v6);
a wildcard listener accepts that connect on every platform. It probes by
connect, not by binding the wildcard, so host firewalls never prompt. The
connect is bounded (200 ms) and blocking, so the lease decision runs in
spawn_blocking. Tests hold 0.0.0.0 and assert the port is not offered.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE
@joelteply

Copy link
Copy Markdown
Contributor Author

Word at 083b021: approve.

  • A port counts as free only when nothing answers a loopback connect and it binds; the connect sees a wildcard listener on Windows.
  • It runs off the runtime.
  • The test holds 0.0.0.0:P and asserts the port is never chosen.

One timing note, not blocking: please hold the canary merge until Codex lifts the Windows hold from the 5090 crash. A canary merge starts a deploy build on the 5090, and a build was on the CPU when it bugchecked.

joelteply added a commit that referenced this pull request Sep 28, 2026
…other host from the core's position (Cormac on #4539)

The renderer runs as the core, so an arbitrary http(s) url could reach
internal endpoints and cloud metadata. A url is now accepted only when its
host is localhost, 127.0.0.1 or [::1] (her dev server); userinfo-disguised
hosts are refused; a public page stays web/fetch's job. Narrowing further
to her leased ports follows once #4540 lands. Test extended in the
existing mod.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@joelteply
joelteply merged commit 53e65f8 into canary Sep 28, 2026
8 checks passed
@joelteply
joelteply deleted the feat/citizen-port-lease branch September 28, 2026 18:52
joelteply added a commit that referenced this pull request Sep 28, 2026
… process, so a sibling test's child cannot hold its writer (card 64ca188f) (#4556)

windows_launch::tests::detached_child_closes_unrelated_pipe_and_preserves_launch_contract
makes an INHERITABLE pipe writer on purpose, to prove the detached child does
not receive it. But std spawns every process with handle inheritance on, so any
sibling test in the same binary that spawned a process during that window
inherited the writer and held the pipe open, and the check failed on a child that
was not the one under test. It failed on four heads on 2026-09-28 (#4540, #4550,
#4551 and the canary tip a0c43bf, where a red tip makes the deploy tracker
RefuseRed and blocks every deploy).

The test now re-execs the test binary with --exact <itself> --test-threads=1
and a marker env var, and asserts that run succeeds; only the isolated run
performs the check. Nothing else runs in that process, so nothing else can
spawn while the inheritable writer exists. The check itself is unchanged.


Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant