Public: anyone can read this; the crew can change it.

Step 1 of a cell outliving its host: a child cell's egress proxy, loopback relays and inbound relay run in a supervisor of their own (FORGE_CELLS_OUTLIVE, off) #13761

opened this

Merged 16d 19h ago, live in production

A child forge's cell can now outlive the host process. This is step 1 of #13750 and it is off by default. It is switched on by FORGECELLSOUTLIVE=on, which I added to docs/APPS.md.

Notes

opened this

A child forge's cell can now outlive the host process. This is step 1 of #13750 and it is off by default. It is switched on by FORGE_CELLS_OUTLIVE=on, which I added to docs/APPS.md. Unix sockets can't be bound in my sandbox, so the cell-level tests were skipped here and have not run; the forge's check is their first real run. The supervisor and its kill-the-host test do run here, using a TCP port.

What changed

  • Supervisor: src/spawn/supervisor.ts is a small supervisor started with setsid, with no die-with-parent. It owns the egress proxy, the loopback relays and the inbound relay. It talks to the host through plain files in one control directory. The supervisor exits when the cell's root process is gone, on SIGTERM, or if no cell appears within a minute. It removes the sockets, and the control directory too when no host is left.
  • Cell: openCell builds the cell without --die-with-parent and starts its argv with setsid when the spec asks to outlive the host. src/spawn/cell.ts sets that for child forges when the flag is on.
  • Output and drain: The cell's output goes to a file the host follows, not a pipe the host reads. stopCellChildren leaves such cells running on drain.
  • Callbacks: The per-connection allowed list is rewritten to the supervisor when it changes. Refusals and undeclared hosts come back as events the host tails, so the existing hooks still fire.
  • Test: test/cell-outlives-host.test.ts starts a supervisor and a cell from a host process and SIGKILLs the host. The supervisor stays in its own process group, the cell stays alive, and the listener and proxy ports keep accepting. When the cell dies, the supervisor exits and its control directory is gone. It also covers the event and tail parsing and the argv differences, and the default-cell check runs only where sockets work.
  • Deletion gate: I added the supervisor's two removals to ALLOWED_REMOVALS in src/kernel/destroy_reach.ts with reasons.
  • Checks run: bun x tsc --noEmit and scripts/gate.ts --fix are clean. The related cell, spawn and billing-never-deletes tests pass (76 pass, 41 skipped).

Left out (later steps of #13750)

  • Nothing re-adopts a surviving cell when the host starts, so the flag stays off by default.
  • The beat door (openBeatDoor) still runs in the host, so a surviving child loses its heartbeat path while the host is down.
  • The child's host-side bookkeeping (exited and the limit watch) lives in the host and is not rebuilt after a restart.
  • Per-connection egress counters (recordEgress) are now recorded in the supervisor process, so the host's gauges miss them for outliving cells.
  • Other openCell callers (apps, steps, terminals) are unchanged.
  • startOn in src/spawn/cell.ts is the other untested path: it hands the cell a log file in place of pipes.

Try it

  • Run bun test test/cell-outlives-host.test.ts.
  • Start the forge with FORGE_CELLS_OUTLIVE=on, start a child, and kill the host with SIGKILL. The setsid supervisor and the cell should stay up.

Closes #13755

7 earlier comments
commented

A child forge's cell can now outlive the host process. This is step 1 of #13750 and it is off by default. It is switched on by FORGE_CELLS_OUTLIVE=on, which I added to docs/APPS.md. Unix sockets can't be bound in my sandbox, so the cell-level tests were skipped here and have not run; the forge's check is their first real run. The supervisor and its kill-the-host test do run here, using a TCP port.

What changed

  • Supervisor: src/spawn/supervisor.ts is a small supervisor started with setsid, with no die-with-parent. It owns the egress proxy, the loopback relays and the inbound relay. It talks to the host through plain files in one control directory. The supervisor exits when the cell's root process is gone, on SIGTERM, or if no cell appears within a minute. It removes the sockets, and the control directory too when no host is left.
  • Cell: openCell builds the cell without --die-with-parent and starts its argv with setsid when the spec asks to outlive the host. src/spawn/cell.ts sets that for child forges when the flag is on.
  • Output and drain: The cell's output goes to a file the host follows, not a pipe the host reads. stopCellChildren leaves such cells running on drain.
  • Callbacks: The per-connection allowed list is rewritten to the supervisor when it changes. Refusals and undeclared hosts come back as events the host tails, so the existing hooks still fire.
  • Test: test/cell-outlives-host.test.ts starts a supervisor and a cell from a host process and SIGKILLs the host. The supervisor stays in its own process group, the cell stays alive, and the listener and proxy ports keep accepting. When the cell dies, the supervisor exits and its control directory is gone. It also covers the event and tail parsing and the argv differences, and the default-cell check runs only where sockets work.
  • Deletion gate: I added the supervisor's two removals to ALLOWED_REMOVALS in src/kernel/destroy_reach.ts with reasons.
  • Checks run: bun x tsc --noEmit and scripts/gate.ts --fix are clean. The related cell, spawn and billing-never-deletes tests pass (76 pass, 41 skipped).

Left out (later steps of #13750)

  • Nothing re-adopts a surviving cell when the host starts, so the flag stays off by default.
  • The beat door (openBeatDoor) still runs in the host, so a surviving child loses its heartbeat path while the host is down.
  • The child's host-side bookkeeping (exited and the limit watch) lives in the host and is not rebuilt after a restart.
  • Per-connection egress counters (recordEgress) are now recorded in the supervisor process, so the host's gauges miss them for outliving cells.
  • Other openCell callers (apps, steps, terminals) are unchanged.
  • startOn in src/spawn/cell.ts is the other untested path: it hands the cell a log file in place of pipes.

Try it

  • Run bun test test/cell-outlives-host.test.ts.
  • Start the forge with FORGE_CELLS_OUTLIVE=on, start a child, and kill the host with SIGKILL. The setsid supervisor and the cell should stay up.
commented

The gate failure is fixed and pushed (feaae5f38). I added // real-timer: <why> to the three Bun.sleep lines in test/cell-outlives-host.test.ts (lines 28, 102 and 108) and changed nothing else.

  • Why annotate instead of injecting a clock: the test polls real processes and sockets, and line 108 waits a real grace period to show the supervisor survives its host being killed. A fake clock would defeat that.
  • Checks run here: bun scripts/gate.ts now exits 0, tsc --noEmit is clean, and bun test test/cell-outlives-host.test.ts passes (5 tests, 1 skipped, 0 failed). The skipped test is the sandbox's unix-socket skip. The forge's check runs it.
  • Gate output during the push: my first gate.ts --fix run still printed the three violations. I'd chained the commit and push in the same command, so the push went ahead before I'd confirmed the gate was clean. A second plain gate.ts run, after the push, passed.

The forge's check on the new push will confirm.

commented

A child cell's egress proxy, relays and inbound way-in now run in a supervisor process of their own, so the cell can outlive the host (FORGE_CELLS_OUTLIVE=on, #13755). This push fixes the check failures on the first version. I haven't seen the forge's check on it yet.

  • Timers: test/timers-named.test.ts now lists the three setIntervals in src/spawn/supervisor.ts as allowed. They run in a separate process with no job ring to wrap them in.
  • Spawn sites: test/spawn-sites.test.ts now lists src/spawn/supervisor.ts with a reason. It starts the supervisor with setsid and only argv the host built.
  • Test over its 2 s budget: the host-killed wait in test/cell-outlives-host.test.ts is down from 1.5 s to 0.5 s. The test checks the supervisor, cell and listeners are all still up after that wait, and it passes. Total file time dropped from 2.95 s to 2.06 s. I didn't read the single test's own time, so whether it is now under 2 s is only inferred.
  • Not touched: two failures from the log (the "pulls touching the same files" pair, and the client-size ceiling) pass when I run them alone here. Neither is in files this change touches, so I'm treating them as flakes from a loaded shard. The pairs test failed in 11 ms.

Locally, gate.ts, tsc --noEmit and the three test files I changed (timers-named, spawn-sites, cell-outlives-host) pass. The sandbox skips one cell-outlives-host test, which the forge's check runs.

commented

This pull is only step 1 of #13750. It moves the egress proxy, loopback relays and inbound relay into a per-cell supervisor, behind FORGE_CELLS_OUTLIVE=on (off by default). The beat door is not moved, and nothing re-adopts or reaps a surviving cell on a host restart.

The changes below answer sentinel's six items. tsc, gate.ts and the cell, timers and spawn-sites tests pass here. The cell-level tests are skipped in this sandbox, so the forge's check is their first real run.

  • Bounded files (1):
    • cell.log is truncated past 4 MB and events.jsonl past 1 MB. The supervisor's tick does this, so it holds after the host is gone. The host's tail does it too.
    • The tail restarts from the top when the file shrinks.
    • The supervisor writes at most 300 events a minute and drops the rest.
    • A new test in test/cell-outlives-host.test.ts fails without the bound.
    • The tail's lines still go through the per-minute child log limit (CHILD_LOG_PER_MINUTE), as before.
  • Timers (2):
    • The host's tail and allowed-list timers now run inside during(...).
    • test/timers-named.test.ts allows only the supervisor process's own tick, with that reason.
  • Beat door (3):
    • It still runs in the host, so a surviving cell loses its heartbeat.
    • I opened #13880 for it and left it out of this pull.
    • The title overstates what this pull does.
  • Unsafe default (4):
    • I removed the FORGE_CELLS_OUTLIVE row from docs/APPS.md, so the flag is undocumented until re-adoption or reaping lands (#13750 step 2).
    • I did not add a refuse-to-start check. The supervisor's control directory is a fresh temp directory each time, so a restarted host can't find the old cell.pid.
    • cellsOutlive has a comment saying the same.
  • Boundary checks (5):
    • config.json is parsed field by field (parseConfig), and the supervisor exits with 2 on a bad one.
    • cell.pid and host.pid now hold pid:starttime from /proc. A reused pid reads as gone. Where /proc has no start time, it falls back to the pid alone.
  • Diff noise (6): I reverted the comment-only rewraps in src/spawn/cell.ts and test/timers-named.test.ts. gate.ts now only notes the over-100-column lines and says the nightly rewrap handles them.

To try it, set FORGE_CELLS_OUTLIVE=on, start a child cell, kill the host, and the cell and its listener stay up. Run bun test test/cell-outlives-host.test.ts for the supervisor tests.

🤖 Generated with Claude Code

commented

The red check came from one real failure of mine. The other three passed when I re-ran them here, so I read them as load-related flakes, but I didn't confirm that. I've pushed the fix, and tsc is clean.

  • Cause: the supervisor's two new timers, cell output tail and cell allowed sync in src/spawn/supervisor.ts, run under during. The "every job src/ runs under during" test in test/job-slices.test.ts requires each one to be timed there or listed in UNTIMED with a reason. I added both to UNTIMED, saying they run in a cell's supervisor process rather than the host and need a supervised cell. test/job-slices.test.ts passes.
  • Comment rewrap: scripts/gate.ts --fix rewrapped long comments in src/spawn/cell.ts and test/timers-named.test.ts; those changes are in the same commit.
  • The other three failures: the 200 MB pipe test in test/pull-pipe.test.ts, the SSH-key sign-in test in test/forge-signin.test.ts and the merge-queue gate-result test in test/merge-queue-gate-result.test.ts all pass here. The pipe test is a timing test (2.1 s in the log), which fits that reading. I haven't traced why the other two failed in the shard run, so if they fail again on this push they need a real look.
commented

I merged main into the branch and pushed it. src/spawn/cell.ts was the only conflict.

  • Conflict: both sides had touched the sleepIdle signature in that file. I kept main's version, which takes the optional onAsleep callback. My side had only rewrapped the line.
  • Checks: tsc is clean and the gate is clean. test/job-slices.test.ts, test/timers-named.test.ts, test/spawn-sites.test.ts and the spawn-cell tests pass here. So does test/pull-pipe.test.ts, the pipe test that failed earlier, and I didn't have to rerun it.
  • Not run here: scripts/fast.ts exited 2 (forge.json changed, so the diff is too broad to name), so I picked those tests myself. The forge's full check will run the rest on this push.
commented

This is step 1 of letting a cell outlive its host, behind FORGE_CELLS_OUTLIVE=on, which is off by default and not documented. A child cell's egress proxy, loopback relays and inbound relay now run in a supervisor process of their own, outside the host. The cell is no longer tied to its parent (no --die-with-parent, started under setsid), and its output goes to a capped file the host follows. The pull is retitled to say this.

Not moved, and why the flag stays off:

  • The beat door (openBeatDoor) still runs in the host, so a cell that outlives the host loses its heartbeat (#13880).
  • Nothing re-adopts or reaps a supervised cell after the host restarts (#13750 step 2).
  • recordEgress byte counts stay in the supervisor process, so the host's egress gauges miss an outliving cell's traffic. This is now #13921, which #13750 waits on, with a TODO(#13921) where the supervisor builds its proxy.

What changed in review:

  • One egress policy. src/apps/cell_policy.ts holds the policy as plain data (CellPolicy) and the only function that turns it into proxy options (proxyOptions). openCell builds the policy once with cellPolicy(spec). The host's proxy and relays come from it, and the supervisor's config is { sockets, ...policy } (SupervisorConfig extends CellPolicy). The supervisor calls the same proxyOptions, so the two cannot drift.
  • A testable spawn. startOn's spawn-and-wire step is now spawnInCell in src/spawn/cell.ts. A supervised cell writes to logFd, is adopted, and is followed; any other cell is drained from pipes.
  • A test seam. cellChildren(parent, launcher?) takes an optional launcher, used only when the registry is first made, so stopCellChildren can be tested as it is.

New tests in test/cell-outlives-host.test.ts. Each one fails if its behaviour is removed: I checked by deleting adopt, the logFd hand-off and the early return in turn.

  • Fake supervisor: output goes to the file and not a pipe, the root pid is adopted, lines are followed, and unfollow stops it.
  • Pipes: an unsupervised cell is read from pipes.
  • Real supervisor: with a real supervisor from openCell({ outlive: true }), cell.pid records the spawned root and its output reaches the host's log.
  • Same policy: the config.json the supervisor reads equals cellPolicy(spec), and report becomes a hook only when the policy reports.
  • Stopping the host: stopCellChildren leaves an outliving cell running and stops a default one.

All of them also pass under FORGE_TEST_SOCKET. So does the existing test "FORGE_CELLS_OUTLIVE=on makes a child's cell its own session…": the host binds no socket for an outliving cell, so it needs no guard, and a comment now says so.

Checks run here: tsc and scripts/gate.ts are clean, and 505 cell, contain and spawn tests pass with none failing. The suites that need real unix sockets and cells are skipped in this sandbox and run in the forge's check.

To try it: bun test test/cell-outlives-host.test.ts

🤖 Generated with Claude Code

commented

A cell child's egress proxy and inbound relay now run under one supervisor that both the host and the supervisor use, with one egress policy between them. The spawn path is tested: a supervised cell starts under setsid, and stopping the forge leaves cells that are meant to outlive it running. That is the earlier work on this branch (#13755, #13921).

This push merges main back in (f120f1cf2). The one conflict was in src/apps/contain.ts, at the argv builder in the cell spawn path, and both sides are kept:

  • main's new noteServingCores(...) call runs before argv is built.
  • This branch's argv still adds setsid in front of the bwrap command when the cell is supervised.

The comment rewrap that scripts/gate.ts --fix made in src/core/capacity.ts is part of the merge commit.

bun x tsc --noEmit passes. scripts/fast.ts could not name the tests (the diff is not bounded), so I ran the ones that cover this change: the contain, cell, serving-cores, capacity, job-slices, timers-named and spawn-sites suites. 88 passed and none failed. 41 were skipped: they need unix sockets and real cells, which this sandbox cannot run, so the forge's check runs them on this push.

🤖 Generated with Claude Code

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs