Skip to content

feat(core): delegation tree with budget carve, depth cap, write lease and report (#585) - #618

Merged
ajianaz merged 5 commits into
developfrom
feat/delegation-core
Nov 11, 2026
Merged

ajianaz merged 5 commits into
developfrom
feat/delegation-core

Conversation

@ajianaz

@ajianaz ajianaz commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

What

Part 1 of #585 (epic #583): a new tole-core module, delegation. It keeps one goal as an append-only, auditable tree: the orchestrator at depth 0 and delegated profiles below it. Every delegation cap is enforced inside it:

  • budget carve;
  • depth limit;
  • parent ∩ profile allowlist;
  • write lease per path;
  • machine guard;
  • mandatory completion report.

Nothing is wired yet. agent_start keeps its depth-1 cap until the follow-up PR, which adds the delegate tool, tole audit and the profile and secret wiring.

Why

Design docs/profiles-collaboration-secrets.md A6, A8 and A10, with the owner decisions in force:

  • No count cap: the number of children is not fixed. The budget carve is the cap, and a child's budget comes out of the parent's, never on top of it.
  • Depth: 2 by default, 3 at most.
  • Writes: every child may write, but it must hand in a report, and the run must be auditable.
  • Write lease: one per path.
  • max_live_processes: only a machine guard.

Putting every cap in one module means no caller (tool, @mention routing, cron as a profile) can skip one.

Changes

  • crates/tole-core/src/delegation.rs (new):
    • Log: records goal, spawn, start, write_lease, release and settle over the existing LedgerStore (the same append, fold and torn-tail rules as the cron ledger). Unknown kinds are skipped, and a newer version of a known kind is refused. Nothing reads a clock, spawns or touches the filesystem: now, process liveness and measured usage are passed in. The host holds a lock around every mutating call, as for the cron ledger.
    • begin(&Goal, now): opens the root (the orchestrator) with the root budget, its allowlist, max_depth (1 to 3; anything else is refused) and max_live.
    • delegate(parent, &DelegateRequest, own_spend, now) -> Handle:
      • Budget carve: tokens and steps come out of the parent's remaining budget, which is its budget minus its own spend minus each child's charge. A running child is charged its whole carve; a settled one is charged its own spend plus its children's charges, capped at its carve.
      • Own spend: the parent's reported own spend is kept in the spawn record. A later report lower than the highest one so far is refused (Invalid), also after reopening the log.
      • Request: a request above what is left is clamped to it. An omitted request takes half of what is left. Zero is refused, and a spawn with nothing left is refused (BudgetExhausted).
      • Deadline: never later than the parent's.
      • Depth: a node at the goal's max_depth cannot delegate (DepthCap). may_delegate tells the host where to register the tool.
      • Allowlist: the profile patterns a parent pattern covers (approval::list_covers, the rule agent_start already uses), plus every deny of both. Uncovered patterns are dropped and recorded.
      • Mode: a consult node can only spawn consult nodes.
      • Node ids: follow the tree, e.g. g.1.2.
    • start(node, process lease, session, now): a spawn is recorded queued. It starts unless the goal already has max_live delegated processes running (Queued { live, max }, which the host can show). next_queued gives the next one to start.
    • lease_write / release:
      • Only a running execute node may take a lease.
      • The path must be absolute and lexically normal (/a/b or /: no empty, . or .. component, no //, no trailing slash). Any other spelling is refused with Invalid, never rewritten, so /w/src/../src/a.rs cannot dodge a lease on /w/src/a.rs. The host canonicalizes first (symlinks, case folding): this module does no filesystem access.
      • A lease conflicts with any live lease on the same path, an ancestor or a descendant, and is re-entrant for its holder.
      • If two writers race, the first in log order wins, in the fold too. The fold also ignores a forged lease record with a non-normal path.
      • Leases end when the holder settles.
    • settle(node, outcome, used, report, reason, now):
      • Outcome::Ok becomes done only with a complete Report and no unsettled children. Otherwise it becomes incomplete, with the reasons recorded.
      • A complete report has a summary. If it lists changed files, it also needs a diff ref and the checks run, and a consult run may not report changed files.
      • used must be the node's measured own usage, for every outcome. It is what the parent is charged, and the rest of the carve goes back to the parent. It is never taken below the node's highest reported own spend. Outcome::Budget is always charged the whole carve. A host that cannot measure passes the whole carve (Spend::all_of).
      • The first terminal record is final, also when a second one raced past the check.
    • recover(now, alive, measured):
      • A running node whose process is gone settles interrupted. Its usage is unknown, so it is charged its whole carve unless measured returns what it really spent (a lower measured value refunds the difference). A crash can never refund a budget it may have burned.
      • A queued child of a settled parent settles cancelled and refunds in full, since it never started.
    • tree / render_tree: the audit view, one line per node, indented by depth: profile, status, mode, charged/carved tokens and steps, allowlist, workspace, session, report summary and reason.
  • crates/tole-core/src/delegation_tests.rs (new): the unit tests.
  • Docs:

Testing

  • 17 unit tests in delegation_tests.rs, covering:

    • the carve (clamping, refusal, refund on settle, the parent's own spend);
    • subtree charging;
    • the half default and the deadline cap;
    • depth 2, the 1–3 bounds and a duplicate goal;
    • the allowlist intersection with denies kept;
    • consult limits;
    • leases (same path, ancestor, descendant, root, sibling worktree, re-entry, release, end on settle);
    • non-normalized lease paths refused;
    • two processes on one log, and a lease race in the fold;
    • the report rules;
    • the live-process queue and recovery;
    • replay to the same tree and the rendered audit lines;
    • forward and newer versions;
    • a raced second settle;
    • budget soundness:
      • spawn → start → recover with the process dead leaves the parent's available budget unchanged;
      • a spawn-and-crash loop ends with BudgetExhausted after exactly 10 cycles of a 1000-token goal;
      • a queued child cancelled before it started refunds in full;
      • a measured spend below the carve refunds the difference;
      • a budget stop is charged the whole carve;
      • a lower own-spend report is refused, also after reopening the log.
  • Each cap was broken on purpose; the matching tests went red, and green again once restored:

    Behavior broken Result
    children's charges not subtracted (carve adds) 2 tests failed
    settled subtree not charged 1 failed
    depth check off 1 failed
    allowlist not intersected 1 failed
    lease conflict only on the exact path 2 failed
    ok always done (report not mandatory) 1 failed
    live-process guard off 1 failed
    deadline not capped by the parent's 1 failed
    first settle not final in the fold 1 failed
    racing lease not first-wins in the fold 1 failed
    lease paths only checked for a leading / (the previous code) 1 failed
    the API accepts any non-empty path 1 failed
    the fold accepts a non-normal lease record 1 failed
    a dead node settles with zero usage (the previous code) 2 failed (crash refund, spawn+crash loop)
    measured usage ignored (always the whole carve) 1 failed
    a cancelled queued node is charged its carve 1 failed
    Budget charged only what was passed 1 failed
    a lower own spend accepted 1 failed

    The new budget tests need the new recover(now, alive, measured) signature, so they cannot compile against the previous code. The red evidence there is the break row "a dead node settles with zero usage", which puts the previous behavior back into the new code.

  • Commands (private CARGO_TARGET_DIR, CARGO_INCREMENTAL=0, debuginfo off; develop merged in):

    • cargo fmt --all: clean
    • cargo clippy --workspace --all-targets -- -D warnings: clean
    • cargo test --workspace: 1474 passed, 0 failed
    • cargo test -p tole-core --no-default-features: 505 passed, 0 failed
    • cargo build -p tole-cli --no-default-features: ok
    • cora review --no-llm --base origin/develop: no issues found

Known limits and choices to confirm:

  • Size: about 1900 lines, above the ~1000 target. About 750 are tests, and the module is one deep interface whose caps are meant to sit together.
  • Time: time is shared wall-clock, so a child's minutes cap its deadline at the parent's instead of being subtracted from a pot. Tokens and steps are subtracted.
  • Default carve: an omitted request takes half of what is left, so a parent keeps room for itself and its siblings.
  • Lease granularity: a lease is per absolute, normalized path and covers everything under it. The wiring PR will lease the canonicalized workspace (or worktree) root by default.
  • Measured usage: the module trusts the host's measured usage, read from the child's own session usage records in the wiring PR. A host that cannot measure must pass the whole carve.
  • Not here: secrets (parent grant ∩ Profile::granted, with SecretSet from feat(core): SecretSet — known-value redaction at every egress sink, child minimum env #595), the delegation.* config keys, and the mailbox result flow. They belong to the wiring PR.

… and report (#585)

New tole-core module delegation: an append-only, auditable tree of one
goal, with the orchestrator at depth 0 and delegated profiles below.

A child's tokens and steps are carved from its parent's remaining budget
and its deadline never passes the parent's: a running child keeps its
whole carve reserved and a settled one is charged what its subtree spent,
so any number of children share one pot. Depth defaults to 2 and can
never exceed 3. The child allowlist is the profile patterns a parent
pattern covers, plus every deny; a consult node cannot spawn an executor
or write. An execute node takes a write lease per path; a lease on the
same path, an ancestor or a descendant conflicts, and the first in log
order wins a race. max_live_processes only queues.

An ok run settles done only with a completion report (summary; changed
files need a diff ref and the checks run) and no unsettled children,
else incomplete with the reasons. A terminal record is never
overwritten. Recovery settles dead processes interrupted and queued
orphans cancelled; render_tree prints the audit view.

The delegate tool, tole audit and the profile wiring follow.
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

Comment thread crates/tole-core/src/delegation.rs Outdated
if path.is_empty() || !path.starts_with('/') {
return Err(DelegationError::Invalid(format!(
"lease path {path:?} must be absolute"
)));
lease_write only checked for a leading slash and compared paths by
component, so /w/src/../src/a.rs, /w/src/./a.rs or //w/src/a.rs did
not conflict with a lease on /w/src/a.rs and two children could write
one file. lease_write and release now refuse any path that is not
absolute and lexically normal (no empty, . or .. component, no trailing
slash), and the fold ignores a forged lease record with such a path. The
module does no filesystem access; the host canonicalizes first.
recover() settled a running node whose process was gone with zero
usage, so its whole carve went back to the parent and spawn+crash cycles
could spend past the goal's budget. A dead node is now charged its whole
carve unless the host passes a measured value (lower measured usage still
refunds the difference); only a queued node that never started refunds
everything. A budget stop is charged its whole carve, a settle never goes
below the node's reported own spend, and delegate() refuses an own spend
lower than one the node reported before (kept in the spawn record).
@ajianaz
ajianaz merged commit fb95ac8 into develop Nov 11, 2026
19 of 20 checks passed
@ajianaz
ajianaz deleted the feat/delegation-core branch November 20, 2026 08:36
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.

3 participants