Repository navigation
feat(cli): wire the machine resource guard into delegation - #630
Merged
Merged
Conversation
Owner requirement: delegation must never overload or crash the server.
The drain that delegate and delegate_poll share now runs
Thresholds::admit before every launch, so the orchestrator (depth 0)
and the agent below it (depth 1) both get it. A spawn the guard holds
back stays queued, never fails, and a wait on the head holds the queue
(strict FIFO); every later delegate or poll re-evaluates it.
The census comes from a machine-wide registry,
<data dir>/delegation/machine.jsonl, under its own lock (taken only
inside a delegation log's lock): each started child registers its
{pid, start_time} lease, dead leases (zombies included) stop counting,
ramping means started less than 30 seconds ago, and the newest entry is
the last start. It is opened with O_NOFOLLOW|O_NONBLOCK and read up to
256 KiB. Disk is checked on the data dir, the workspace and, with
worktrees, their directory.
Delegated children start at child_nice (ChildOpts::nice). The delegate
and delegate_poll replies carry queued_reason and waited_secs for each
queued spawn, the first delegate carries the guard's startup notes, and
tole doctor prints the headroom against every threshold and warns about
a spawn queued longer than 10 minutes. Thresholds come from the
[delegation] keys through Setup (tests inject a probe and thresholds).
A data dir or worktree dir that is not made yet lives on its parent's filesystem: check disk there instead of reporting the disk probe as unreadable (tole doctor on a fresh home printed a false note). Also names the drain's return type for clippy.
…d-wiring # Conflicts: # CHANGELOG.md # crates/tole-cli/src/delegate.rs # crates/tole-cli/src/delegate_tests.rs
🔍 Cora AI Code Review✅ No issues found. Code looks good! Review powered by cora-code · BYOK · MIT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Part 2 of the delegation server-load guard: the wiring. The drain that
delegateanddelegate_pollshare now runsThresholds::admit(from #626) before every delegated launch. The census comes from a new machine-wide registry. Delegated children start atchild_nice. The replies show why a spawn waits and for how long, andtole doctorprints the headroom against every threshold.Why
Owner requirement: delegation must never overload or crash the server, automatically, for the orchestrator (depth 0) and the agent below it (depth 1). #626 added the decision but nothing called it. The only limit in force was
max_live_processes, which counts the running nodes of ONE goal, so two sessions or goals could each fill the machine.Changes
crates/tole-cli/src/machine_guard.rs(new):<data dir>/delegation/machine.jsonl. Every delegated child started on this machine registers its{pid, start_time}lease there when it starts, whatever its session, goal or depth. Appends go through the ledgerFileStore(whole line,O_APPEND, fsync).machine.lock. It is only ever taken while a delegation log's lock is held, never the reverse, so the two cannot deadlock. It is held for the census, the probes and the spawn (milliseconds).live_machine: entries whose lease is alive, usinglease_aliveand its zombie rule, so a child that died without being settled frees its slot.ramping: live entries started less thanRAMP_SECS(30 s) ago. A fixed window is the simplest honest rule: a child's memory use cannot be predicted, and its first provider call says nothing about its builds.last_start: the newest entry.spawn_interval_secs) are dropped on the next admission, by writing a temp file and renaming it.O_NOFOLLOW|O_NONBLOCK, only a regular file is read, at most 256 KiB, and unreadable lines are skipped. It lives outside every workspace. A child allowed to run commands could forge entries, but that can only make spawns wait.Guard::gate(paths, now, alive, start): admits one spawn and runsstart, or answersWait(reason)without running it.disk_paths: checks each path's nearest existing ancestor, so a data dir or worktree dir that is not created yet is checked on its parent's filesystem instead of being reported as unreadable.doctor_lines: described under the doctor bullet below.delegate.rs:draincallsGuard::gatefor each queued head, after the goal'smax_live_processesand the workspace-lease checks. AWaitbreaks the loop, so the head holds the queue (strict FIFO), and every later delegate or poll re-evaluates it.drainnow also returns why it stopped: the guard's reason, the goal limit, or the write lease..tole-worktrees.Setupgainsprobe(NonemeansLinuxProbe) andthresholds(Nonemeans the[delegation]keys).Setup::guard(dir)builds the guard. The registry dir is the log's grandparent, so the root session and every delegated child share it.delegatecarriesqueued_reason,waited_secs(seconds since the node was recorded) andqueue, a list of the goal's queued nodes, each withnode,waited_secsandqueued_reason. The head shows the stop reason; the others show "behind<head>".delegate_pollreply'sdelegationview carries the samequeue.delegateof a host carriesguard_notes: the single unreadable-probe note plus the probe notes, such as thememory.statfallback.tole-core:ChildOpts::nice: the spawn callsapply_child_nicewith it. The delegate tool sets it tochild_niceon every delegated launch.Delegation::queued()lists all queued nodes, oldest first.tole doctor: a "delegation guard:" block with ✓ or ! for each check:machine_max_live;Testing
All run locally on 2890e27, which includes a merge of develop 98f157d (#627 and #628; the conflicts were append-only and I kept both sides). Builds used
CARGO_TARGET_DIRset explicitly andCARGO_INCREMENTAL=0.cargo test --workspace --no-fail-fastpasses: 1544 passed, 0 failed (83 suites).cargo test -p tole-core --no-default-featurespasses: 536 passed.cargo test -p tole-cli --no-default-featurespasses: 454 passed. With--features shell-toolsadded: 617 passed.cargo fmt --all -- --checkpasses.cargo clippy -D warningspasses for--workspace --all-targets,-p tole-core --no-default-features --all-targets,-p tole-cli --no-default-featuresand-p tole-cli --no-default-features --features shell-tools(the CI forms).cargo check -p tole-core --no-default-features --testspasses.cora review --no-llm --base origin/develop: "No issues found".New tests in
delegate_tests.rs, all using a fake probe (Dial, with adjustable free memory) injected throughSetup:low_memory_queues_in_order_and_a_later_delegate_starts_them_when_it_recovers:queued_reason"memory 100 MB free < 1536 MB needed" and nothing is spawned;waited_secsis at least 2 after a 2 s wait;/procniceness is at least 10.two_goals_share_the_machine_count_and_a_dead_child_frees_its_slot:machine_max_live = 1;Setup.doctor_shows_the_headroom_and_a_spawn_stuck_in_the_queue:machine_max_live1024), because they share one registry and run in parallel.Red/green: each piece of the wiring was reverted on purpose, the named test failed, and it passed again once restored. All 11 reverts were caught:
live_machine0)Setupchild_nicenot appliedqueued_reasonmissingwaited_secsnot measuredHard rules: charging and the fold are untouched. The registry, the only new file read, is opened with
O_NOFOLLOW|O_NONBLOCKand a size cap before the read. Nothing new can hang under the log lock, except thatstatvfson a hung network filesystem would block, as any stat would.Size: 819 changed lines (786 added, 33 removed) across 10 files.
Related Issues
Part 2 of the delegation resource guard (owner requirement), after #626; follows #585 (epic #583), #623, #627 and #628.
Checklist
feat/resource-guard-wiring)develop