Repository navigation
feat(core): machine resource guard for delegated children - #626
Merged
Merged
Conversation
New tole-core module resource_guard (owner requirement: delegation must not overload or crash the server). Thresholds::admit answers Go, or Wait with a reason (a queue decision, never an error), from a Probe and a machine-wide census: live delegated children across all goals and sessions, the gap since the last start, free memory over a floor plus a reserve per child still ramping up (cgroup v2 memory.max/current when a limit is set, else MemAvailable; the smaller headroom wins), the one-minute load per CPU, free process ids (cgroup pids.max, else pid_max) and free disk for an unprivileged user on each checked path (statvfs f_bavail). An unreadable probe is skipped with one startup note. apply_child_nice starts a delegated child at niceness child_nice, never below the parent's. The eight thresholds are [delegation] keys of the user config only, bounded; the delegate tool wiring follows.
# Conflicts: # CHANGELOG.md # docs/threat-model.md
memory.current includes reclaimable page cache. After heavy file IO (builds, git, checkouts) a container sits near memory.max while most of it is inactive file cache, which the kernel frees only under pressure: limit - current then reads about zero, every spawn queues and the queue never drains. Headroom under a cgroup limit is now limit minus the working set, memory.current less inactive_file of memory.stat (the file field when inactive_file is absent), as Kubernetes counts it. When memory.stat is unreadable the guard keeps the conservative limit - current and says so in a startup note (new Probe::notes). Also: the pid_max fallback counts threads (/proc/<pid>/task), since pid_max bounds thread ids too, and the free pids share uses saturating_mul.
🔍 Cora AI Code Review✅ No issues found. Code looks good! Review powered by cora-code · BYOK · MIT |
12 tasks done
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 1 of the delegation server-load guard: a new
tole-coremodule,resource_guard, that decides whether a delegated child may start on this machine. It also adds the eight thresholds as[delegation]keys in the user config. This PR does not wire anything intodelegate.rs; that is part 2.Why
Owner requirement: delegation must never overload or crash the server, automatically. That applies to the orchestrator (depth 0) and the agent below it (depth 1), the two levels that can spawn at the default
max_depth2.Today nothing in the delegation code reads memory, load, disk or process ids.
max_live_processescounts only theRunningnodes of ONE goal, so two sessions or goals can each run 8 children. This sandbox has run out of disk from build directories several times, so the risk is real.Changes
tole-core/src/resource_guard.rs(new):Probetrait:memory,load,disk(path)andpids, each returningOption. Tests use fixed probes, so every decision is deterministic.Thresholdsand their defaults:min_free_memory_mb1024child_memory_mb512max_load_per_cpu1.5min_free_disk_mb2048min_free_pids_percent10spawn_interval_secs2machine_max_livemax(2, CPUs)child_nice10Thresholds::with(&Overrides): applies config values and bounds-checks every key. Zero, negative and absurd values are errors namingdelegation.<key>.Thresholds::admit(probe, Census, paths) -> Admit::{Go, Wait { reason }}: aWaitis a queue decision, never an error. It checks, in order:machine_max_live(all goals and sessions);spawn_interval_secs;min_free_memory_mb+child_memory_mb× (children still ramping + 1): a child that started but has not used its memory yet is reserved;The first failing check gives the reason, for example
queued: memory 612 MB free < 1536 MB needed.Unreadable probes: a probe that cannot be read is skipped, never treated as fail-closed or as plenty.
startup_notesreturns ONE note naming the skipped checks, plus one line for each probe note (such as thememory.statfallback).LinuxProbe, with pure parse functions tested on fixtures:memory.maxminus the WORKING SET, as Kubernetes counts it:memory.currentlessinactive_fileofmemory.stat(thefilefield wheninactive_fileis absent).memory.currentincludes reclaimable page cache, so after builds or checkouts it sits near the limit and limit − current would queue every spawn forever. Ifmemory.statis unreadable, the guard falls back to the conservative limit − current and says so in a startup note (newProbe::notes, default empty). With no limit (max), it usesMemAvailable. When both are known, the smaller headroom wins. The cgroup path comes from/proc/self/cgroup0::. The pureparse_memory_stat_inactive_fileandmemory_from_fileshold the logic./proc/loadavg.statvfsf_bavail, notf_bfree.pids.max/pids.current, else/proc/sys/kernel/pid_maxagainst the THREADS in/proc(/proc/<pid>/taskentries; pid_max bounds thread ids too). The free share usessaturating_mul.apply_child_nice(cmd, nice): a pre-execsetpriorityto max(the parent's niceness,child_nice). It never lowers niceness, and a failure never stops the spawn. It is unix only and a no-op elsewhere. The wiring PR will call it.tole-cli/src/config.rs:DelegationConfig. That table is already user layer only: a project config is ignored and shown as IGNORED inconfig check.DelegationConfig::resource_thresholds(); config parsing validates throughThresholds::with.config checkrenders the keys. None of the names contains a secret-like word.Testing
All run locally on ee5c855 (develop is still 3850b5e, #623, already merged in), with
CARGO_TARGET_DIRset andCARGO_INCREMENTAL=0.cargo test --workspace --no-fail-fastpasses: 1531 passed, 0 failed (83 suites).cargo test -p tole-core --no-default-featurespasses: 532 passed, 0 failed.cargo test -p tole-cli --no-default-featurespasses: 445 passed, 0 failed.cargo fmt --all -- --checkpasses.cargo clippy --workspace --all-targets -- -D warningspasses, and so docargo clippy -p tole-core --no-default-features --all-targetsandcargo clippy -p tole-cli --no-default-features.cora review --no-llm --base origin/develop(cora v0.20.0): "No issues found". The LLM review is CI Cora Review.the_linux_probe_reads_this_machine: memory, load, disk of the temp dir and pids are all readable, and a missing path isNone.a_child_runs_at_least_at_the_child_niceness: a spawnedcat /proc/self/statruns at niceness max(ours, 10), and at ours with 0.New tests:
resource_guard::tests(21):max, a hybrid v1/v2/proc/self/cgroup, and a container limit below host memory;inactive_file, gives about 3.1 GiB of headroom and a child is admitted; the same current withinactive_file0 waits; a missing or garbledmemory.stattakes the conservative path and the note reachesstartup_notes; amemory.statwithoutinactive_fileusesfile;u64::MAXpid limit does not overflow;config::tests::delegation_resource_keys_are_bounded_and_user_layer_only: every key mapped, bad values (zero, negative, out of range, non-numbers) rejected, a project config ignored, andconfig checkrendering.Red/green: each rule was broken on purpose, the named test was seen failing, the file was restored, and the test passed again. All 23 mutants were killed:
>→>=maxread as unparsablefilefallback inmemory.statstartup_notesSize: 1,251 lines, which is over the ~900 target; the working-set fix added about 200. Roughly half is tests. If CI Cora cannot complete, the config keys (119 lines) can move into the part 2 wiring PR.
Related Issues
Follow-up to #585 (epic #583) and #623; owner requirement for delegation. Part 2 will wire the guard into
Delegator::delegateand the drain, add a machine-wide live registry (ledger file store plus lease liveness in the data dir), addtole doctorlines, and addqueued_reasonandwaited_secstodelegatereplies (so a stuck queue is visible to the orchestrator and intole doctor).Checklist
feat/resource-guard)develop