Expand description
Spawn gate (x-c5cc): global concurrency cap + free-RAM floor + queue loop.
Called at the top of the client spawn arm for the bg/headless
substrates only (pane re-execs into the Python CLI, whose mirrored gate
in fno/agents/spawn_gate.py is the sole gate on that path — exactly one
gate evaluation per spawn, LD1).
The gate is READ-ONLY: the max_live slot cap counts the fno registry
(worker provenance) and the RAM floor reads system vm_stat/meminfo. The
claude daemon roster is consulted only as a LIVENESS ORACLE for fno bg rows
that carry no local pid, and by the post-spawn QoS demotion helper — never
as a population to count (x-bdf9: the roster’s non-work sessions must not
consume worker slots; only rows that are ALSO in the fno registry count).
The gate’s only writes are its own claims (spawn-gate check→dispatch mutex,
worker:<name> headless slot claims). Every guard fails OPEN on read errors
(LD5): the gate is protective infrastructure and must never become the thing
that bricks spawning.
Structs§
- Gate
Flags - Flags the spawn arm parses for the gate.
- Gate
Guard - Held gate state. The caller keeps this alive across its dispatch call and
calls
GateGuard::release(or drops it) when the dispatch result exists, so the next waiter’s count includes the newcomer.
Constants§
- EXIT_
NO_ WAIT - EXIT_
QUEUE_ TIMEOUT - Exit codes, distinct from existing dispatch codes (2, 13, 14, 15, 18, 127).
- EXIT_
RAM_ REFUSED
Functions§
- available_
ram_ gb - Available system RAM in GB, or
Nonewhen unreadable (guard skipped, fail open — a broken vm_stat must never brick spawning). - parse_
meminfo - Parse
/proc/meminfo(Linux)MemAvailable:kB to bytes. - parse_
vm_ stat - Parse
vm_statoutput (macOS) to available bytes: (free + inactive + speculative + purgeable) pages × page size.Noneon any shape surprise so the guard fails open. - qos_
demote_ bg_ worker - After a
--substrate bgdispatch, poll the roster briefly for the new worker’s pid and demote it post-hoc (its exec is claude’s, not ours). Bounded ~10s; one warning if the pid never appears (AC3-UI). - qos_
demote_ pid - Best-effort post-hoc demotion of a claude-daemon-owned bg worker pid
(
taskpolicy -b -pon macOS,renice 10 -pon Linux; same uid, so permitted). Non-fatal: failure prints one warning, the spawn stands. - qos_
wrap - Exec-wrap a child command at background priority when
config.agents.worker_qosisutility:taskpolicy -c utility -- <cmd>on macOS,nice -n 10 <cmd>on Linux. Identity onoff/ other OSes. - run_
gate - Run the full gate for a
bg/headlessspawn. Returns a guard to keep alive across dispatch on pass, orErr(exit_code)on refusal/timeout. All human-facing output goes to stderr (LD10: the stdout receipt is byte-reserved for the pass path). - slot_
count - Count fno WORKER SLOTS in use for the
max_livecap: liveness-filtered fno registry rows + liveworker:<name>headless slot claims. - spawn_
cap_ would_ emit - Pure parity core (x-91b5, AC2-FR): would a bypass in this env emit
spawn-cap? True iffFNO_SPAWN_GATE=0AND no non-empty test-context marker. Mirrorsfno.events.gate_escape.should_emit_spawn_capexactly; a shared JSON fixture (gate_escape_spawn_cap_parity.json) asserts the two implementations agree on every row, so neither can drift (Locked Decision 5).