Skip to content

feat(cli): wire the machine resource guard into delegation - #630

Merged
ajianaz merged 4 commits into
developfrom
feat/resource-guard-wiring
Nov 12, 2026
Merged

ajianaz merged 4 commits into
developfrom
feat/resource-guard-wiring

Conversation

@ajianaz

@ajianaz ajianaz commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

What

Part 2 of the delegation server-load guard: the wiring. The drain that delegate and delegate_poll share now runs Thresholds::admit (from #626) before every delegated launch. The census comes from a new machine-wide registry. Delegated children start at child_nice. The replies show why a spawn waits and for how long, and tole doctor prints 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):
    • Registry: <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 ledger FileStore (whole line, O_APPEND, fsync).
    • Lock order: the registry has its own lock, 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).
    • Census:
      • live_machine: entries whose lease is alive, using lease_alive and its zombie rule, so a child that died without being settled frees its slot.
      • ramping: live entries started less than RAMP_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.
    • Compaction: dead entries older than max(30 s, spawn_interval_secs) are dropped on the next admission, by writing a temp file and renaming it.
    • Reads: the file is opened with 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 runs start, or answers Wait(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:
    • drain calls Guard::gate for each queued head, after the goal's max_live_processes and the workspace-lease checks. A Wait breaks the loop, so the head holds the queue (strict FIFO), and every later delegate or poll re-evaluates it.
    • drain now also returns why it stopped: the guard's reason, the goal limit, or the write lease.
    • Disk is checked on the data dir, the workspace and, in worktree mode, .tole-worktrees.
    • Setup gains probe (None means LinuxProbe) and thresholds (None means 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.
    • Replies:
      • A queued delegate carries queued_reason, waited_secs (seconds since the node was recorded) and queue, a list of the goal's queued nodes, each with node, waited_secs and queued_reason. The head shows the stop reason; the others show "behind <head>".
      • The delegate_poll reply's delegation view carries the same queue.
      • The first delegate of a host carries guard_notes: the single unreadable-probe note plus the probe notes, such as the memory.stat fallback.
  • tole-core:
    • ChildOpts::nice: the spawn calls apply_child_nice with it. The delegate tool sets it to child_nice on every delegated launch.
    • Delegation::queued() lists all queued nodes, oldest first.
  • tole doctor: a "delegation guard:" block with ✓ or ! for each check:
    • memory free against what a spawn needs (floor + per-child reserve, ramping included);
    • load per CPU;
    • disk on the data dir and the current directory;
    • process ids;
    • live delegated children on this machine against machine_max_live;
    • the unreadable-probe notes;
    • a warning for each spawn queued longer than 10 minutes, in any delegation log.
  • Docs: the threat-model "Delegation resource guard" section (registry, lock order, liveness, FIFO, reply fields, doctor), the README config row, and a CHANGELOG entry.

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_DIR set explicitly and CARGO_INCREMENTAL=0.

  • cargo test --workspace --no-fail-fast passes: 1544 passed, 0 failed (83 suites).
  • cargo test -p tole-core --no-default-features passes: 536 passed.
  • cargo test -p tole-cli --no-default-features passes: 454 passed. With --features shell-tools added: 617 passed.
  • cargo fmt --all -- --check passes.
  • cargo clippy -D warnings passes for --workspace --all-targets, -p tole-core --no-default-features --all-targets, -p tole-cli --no-default-features and -p tole-cli --no-default-features --features shell-tools (the CI forms).
  • cargo check -p tole-core --no-default-features --tests passes.
  • 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 through Setup:

  • low_memory_queues_in_order_and_a_later_delegate_starts_them_when_it_recovers:
    • with 100 MB free, A is queued with queued_reason "memory 100 MB free < 1536 MB needed" and nothing is spawned;
    • B queues behind A: the head's reason is memory, B's is "behind", and A's waited_secs is at least 2 after a 2 s wait;
    • once memory recovers, the next delegate starts A, then B, then itself, in that order;
    • the started child's /proc niceness is at least 10.
  • two_goals_share_the_machine_count_and_a_dead_child_frees_its_slot:
    • two hosts on two roots, so two goals and two logs, share one data dir, with machine_max_live = 1;
    • B waits with "1 delegated children live on this machine (max 1)";
    • A is killed without being settled; after the spawn interval, B's next delegate starts B's head and queues the new spawn behind it;
    • B takes its thresholds from the user config through Setup.
  • doctor_shows_the_headroom_and_a_spawn_stuck_in_the_queue:
    • the memory, load, disk and live-count lines;
    • a spawn recorded 1000 s ago warns "has waited 16 min";
    • with the real probe, a data dir that does not exist yet gives no "cannot read disk" note. This was a real false note that the e2e doctor test caught on a fresh HOME.
  • Existing tests run with a roomy fake probe and open thresholds (interval 0, machine_max_live 1024), 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:

Reverted piece Caught by
guard never consulted (the pre-wiring state) memory test, machine test
other goals not counted (live_machine 0) machine test
dead lease still counted machine test
started child not registered machine test
config thresholds not wired through Setup machine test
child_nice not applied memory test
queued_reason missing memory test, machine test
waited_secs not measured memory test
doctor never warns about a stuck spawn doctor test
doctor ignores the ramping reserve doctor test
disk checked on a missing path as given doctor test

Hard rules: charging and the fold are untouched. The registry, the only new file read, is opened with O_NOFOLLOW|O_NONBLOCK and a size cap before the read. Nothing new can hang under the log lock, except that statvfs on 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

  • Branch name follows convention (feat/resource-guard-wiring)
  • Branch is from develop
  • Commit messages follow Conventional Commits
  • No secrets or credentials committed
  • One logical change per PR

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
@github-actions

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

@ajianaz
ajianaz merged commit abace1e into develop Nov 12, 2026
19 of 20 checks passed
@ajianaz
ajianaz deleted the feat/resource-guard-wiring branch November 20, 2026 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants