Skip to content

agent-slots hook: read T3's slot gate count instead of counting processes (#849) - #1292

Merged
scuttletoby merged 4 commits into
mainfrom
fork/agent-slots-server-count
Nov 29, 2026
Merged

scuttletoby merged 4 commits into
mainfrom
fork/agent-slots-server-count

Conversation

@scuttletoby

Copy link
Copy Markdown
Owner

Part of #849.

State question: what part of #849 does this cover? The hook. fork-kit/agent-guards' agent-slots hook now reads T3's own slot gate count (server.agentSlots, the machine line wait_list shows) instead of running pgrep -c claude. Not covered yet: "every tool reads it" beyond this hook, and the orchestrator's private state file (not in this repository; once a machine sets t3ServerState, the slot line in that file can go).

What

  • Server: apps/server/src/fork/agentSlotsCountFile.ts (new fork file). It writes agent-slots.json next to the home's server-runtime.json: {"version":1,"pid","writtenAt","slots":{"used","max"}}. It writes once at start, before the layer is built, then every 20 s. It reads SlotGate.usage, the same count wait_list uses (running agents, starting entries, held slots and pending wake-ups). slots is null while slots are off or the count cannot be read. max is null when there is no machine limit. A failed write is only logged. It is wired in forkLayers.ts (a fork file) next to the work panel, under the one SlotGate.layer, so no second gate is made. No upstream file changes.
  • Hook: new key agentSlots.t3ServerState (default "", which keeps today's process count). Set it to the home's userdata/server-runtime.json and the hook uses the server's count. It only uses that count when the file's pid matches the pid in server-runtime.json and the file is under 90 s old. It reads no links, does not wait on a fifo, and caps the read at 64 KiB. In every other case (no server, no file, another process, stale or future time, slots off, wrong version, bad numbers) it falls back to the process count, and the context line says why: Agent slots: 3/10 claude processes alive (T3's slot count not read: agent-slots.json is stale (600 s old)), 7 free. When T3 sets no machine limit, maxAlive is the limit.
  • Docs: the server.agentSlots section of docs/fork/README.md, the kit README's hook row and key table (the formatter re-aligned the table), the hook's own docstring, and machine.example.json.

Tests (red on main, then green)

  • fork-kit/agent-guards/tests/agent-slots.test.py: 6 new tests. Against main's hook they give 17 failures; with the change all 12 tests pass. Each answers one question:
    • Does the server's count win over pgrep, for both the context line and the stop block?
    • Does a child that finished or crashed free its slot at the server's next count (full, so the stop passes; then one free, so the stop is blocked)?
    • With no machine limit in T3, is maxAlive the limit?
    • In each of 12 states it cannot trust, does it count processes and name the reason?
    • Are a link or a fifo left unread, without waiting?
    • Do the file name and the 20 s interval read from the server's source match the hook's constants (90 s is at least 3 intervals)?
  • apps/server/src/fork/agentSlotsCountFile.test.ts (new, 6 tests): the first write at start holds the gate's count and this pid; off/unread and no machine limit are written as null; each write has the count at that moment, so a finished child frees its slot; the count is read again one interval later (TestClock); an unwritable path fails nothing; forkLayers.ts has exactly one SlotGate.layer.
  • config.test.py and common/tests/machine_file.test.py pass. vp run check:changed passes (format, lint, knip, typecheck, the 2 test files). The fork-checks scripts pass: schema, upstream files, seams.
  • Not run here: the server boot tests (holdWork.boot.test.ts) need a local socket, which this machine blocks. CI runs them.

.github/fork-ci-tests.txt is unchanged: agent-slots.test.py still runs in about 1 s.

🤖 Generated with Claude Code

scuttletoby and others added 3 commits November 29, 2026 09:15
…sses (#849)

The server writes its slot gate's machine count (SlotGate.usage, the line
wait_list shows) to agent-slots.json beside server-runtime.json, at start
and every 20 s (fork/agentSlotsCountFile.ts). The agent-slots hook reads it
when the new agentSlots.t3ServerState names that server-runtime.json: only
for the pid there, only while under 90 s old, no link, no fifo, size cap.
Anything else falls back to the process count and the context line says why.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fork-kit/agent-guards/tests/agent-slots.test.py reads the server's slot
count file name and interval from apps/server/src/fork/agentSlotsCountFile.ts,
which the sparse checkouts did not include, so its tests failed with
FileNotFoundError. Add it to the three lists kept in step.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The branch conflicted with main, so GitHub kept the pull request's
old merge commit and the head run of Fork tests failed. Both sides
are kept: the agent-slots count import beside main's sender-role
import, and main's README tables with this branch's agent-slots rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@scuttletoby

Copy link
Copy Markdown
Owner Author

lens: code · reviewer: Claude Opus 5.5 (medium) · read at: 37e5a9e

blocker: none
major: none
minor: after a crash, server-runtime.json stays (only a clean stop clears it) and the hook does not check that its pid is still alive, so for up to 90 s the dead server's last count is used. This is harmless because it fails open (it counts too many, so a stop is let through, never blocked wrongly). A cheap os.kill(pid, 0) check (ProcessLookupError means it is gone) would close that gap and name the reason.
minor: layer repeats writeCount with Schedule.forever. Only the write is wrapped in catchCause, so a defect from gate.usage would end the repeat fiber without a sound. The file then goes stale and the hook falls back with "stale", which is safe, but the reason shown would be wrong. Putting the catchCause around the whole writeCount would keep it ticking.

Checked:

  • Atomic write: writeFileStringAtomically writes to a temp dir in the same folder and then renames, so a reader never sees a partial file. A partial or garbled file is handled anyway ("cannot read").
  • Old, missing or empty state: with t3ServerState: "" the hook takes the old path unchanged, the same text plus the process count. No server-runtime.json gives "not running". No agent-slots.json (an older build) gives a fallback. A wrong version, a non-dict file, a naive or garbled writtenAt, a future time over 30 s, max: 0 or negative values all give a fallback. SlotGate.usage only ever gives a max that is null or > 0, so rejecting 0 matches the writer.
  • Runs again / restart: the first write happens during layer build, before the new server-runtime.json exists. The reader then sees the old pid, or no file after a clean stop, and falls back until both agree. A stopped server's file never matches a later server's pid.
  • Two servers on one home: acquireServerOwnership runs before the layers are built, so only one server writes. Only server.ts builds layerRuntimeServices, so no CLI process overwrites the file.
  • Time format: the ISO ...123Z from DateTime.formatIso parses on Python 3.12, and Z is replaced, so 3.7–3.10 work too.
  • Tests: agent-slots.test.py 12/12 pass here. holdWork.boot.test.ts was not run locally (the machine was at capacity). It is covered by this head's green Test Server 1–6 CI shards.

State question: running again rewrites the same file atomically every 20 s. Stopping half-way leaves the last whole file, which goes stale after 90 s or is dropped at once on a pid change. Old, empty or missing state, or a setting left empty, falls back to the process count and names the reason.

🤖 Generated with Claude Code

@scuttletoby scuttletoby added the fork:merge-ready Merge conveyor: lenses done, merge when green (PRs) label Nov 29, 2026
@scuttletoby
scuttletoby merged commit 194043d into main Nov 29, 2026
35 checks passed
@scuttletoby
scuttletoby deleted the fork/agent-slots-server-count branch November 29, 2026 22:19
scuttletoby added a commit that referenced this pull request Nov 30, 2026
… shared reader (#849) (#1437)

fork-kit/heavy's agent-slots hook counted pgrep processes while agent-guards'
read server.agentSlots (#1292). Both now read agent-slots.json through
fork-kit/common/t3_slot_count.py, which both installers copy.

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

fork:merge-ready Merge conveyor: lenses done, merge when green (PRs)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant