Skip to content

feat(cli): delegate tool with carved mission budgets, leases and grants (#585) - #623

Merged
ajianaz merged 7 commits into
developfrom
feat/delegation-tool
Nov 12, 2026
Merged

ajianaz merged 7 commits into
developfrom
feat/delegation-tool

Conversation

@ajianaz

@ajianaz ajianaz commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

What

Part 3 of #585 (epic #583): the delegate / delegate_poll tools for the CLI faces (run, chat, mission), built on tole_core::delegation (#618) and the launch contract and limits from #621. Each delegated child runs as a budgeted tole mission, optionally as a profile. Its authority, secrets, budget and write access all come from the delegation tree, which also re-checks every spawn in its fold. The log lives outside every workspace. agent_start / agent_poll stay one release as deprecated aliases.

The completion report, settle-on-report and tole audit follow in part (b), which opens after this merges.

Why

Design docs/profiles-collaboration-secrets.md A6 and A8, with the owner rules and the review's hard rules in force:

  • Unknown usage: it is charged in full.
  • Own spend: it only grows.
  • Lease paths: they must be absolute, normalized and canonical.
  • Authority: a child gets allow patterns ⊆ parent ∩ profile.
  • Unattended runs: they cannot delegate wider than their own allowlist. Cron registers no agent tools, so they cannot delegate at all.
  • Secrets: they flow only as parent grant ∩ profile grants.
  • The log is the authority: the host launches what it records, so the log must be out of children's reach and the fold must refuse anything wider than the caps.

Changes

  • crates/tole-core/src/delegation.rs:
    • The fold re-checks every spawn (State::spawn_allowed): live parent, the next id, depth within the goal's limit, the consult rule, allow covered by the parent's (parent denies kept), and a non-empty budget and deadline within what the parent can still carve. Anything else is ignored.
    • Settle: never charged below the node's reported own spend, and a budget stop always its whole carve.
    • Node::seq: log order. Queued nodes start in that order (next_queued), so ties within one second keep their order.
    • Host helpers:
      • lease_conflict: any overlap, for a whole-workspace writer.
      • lease_ancestor_conflict: the path or an ancestor only, for a writer that will get a fresh directory under the path.
      • live and by_session.
  • crates/tole-cli/src/delegate.rs (new):
    • One canonical root: Delegator::for_session canonicalizes the session root once; a root that cannot be canonicalized gets no tool. That one value keys the log path, the agents dir, the agent dirs recorded as sessions, the poll tool's root and every agent_dir lookup, so a symlinked or .. workspace path still settles its children.
    • Location: the log, its lock and the queue live in <codecora home>/tole/delegation/<hash of the canonical root>/ (delegate::log_path), outside every workspace. Children get the absolute path through TOLE_DELEGATION_LOG.
    • Registration: a root session gets the tools. A delegated child gets them only while may_delegate(node) holds. A classic depth-1 agent_start child gets none.
    • delegate {task|prompt, profile?, mode?, budget?, allow?, role?, consumes?, keep_mailbox?}, under the log's exclusive lock:
      • Recovery and carve: recovery runs first, then the child is carved from the session's node with allow = parent ∩ profile (or requested, or parent).
      • Pre-check: an execute child is refused when its workspace lease is taken.
        • In place, any lease overlapping the workspace refuses it.
        • With --agents-worktree, only a lease on .tole-worktrees or above it refuses it; sibling worktree writers run in parallel.
      • Strict FIFO: the new node joins the queue, then the queue drains in log order while max_live_processes allows. A new spawn never starts ahead of an older queued one, and the reply says whether it started or is queued, plus what started from the queue.
      • Launch: the spec is built from the recorded node, with secrets recomputed: tole mission --max-steps S --max-minutes M --max-tokens T [--profile P] --allow … at the node's depth. The child keeps its provider keys plus Profile::granted(held).
    • Drain (from both delegate and poll):
      • starts queued nodes in log order;
      • stops at a queued writer whose workspace lease is taken (it holds the queue until the lease frees);
      • settles a node whose queue file or profile is gone as cancelled (zero spent), and keeps going past a failed launch.
    • delegate_poll {agent}: settles the polled child first (measured session usage, or the whole carve), then recovers the others and drains. Until part (b), an ok child settles incomplete.
    • Goal ids are unique per process (g-<secs>-<pid>-<n>).
  • ledger_file.rs: lease_alive counts an exited but unreaped child (a zombie, /proc state Z) as gone, so a finished detached child frees its slot on the next delegate without a poll.
  • tool_roster.rs / main.rs: the poll tool is rooted at the host's canonical root. tole serve keeps the classic depth-1 agent_start for now.
  • feat(cli): delegation limits and the delegated-child launch contract (#585) #621 review:
    • agents::agent_dir(root, id) returns Option and refuses empty, ., .., leading-dot and separator-bearing ids.
    • child_env::delegation() refuses node ids with a leading, trailing or doubled dot.
  • Docs: docs/threat-model.md (fold validation, file locations, what a child can still write, the in-place vs worktree pre-check, one canonical root, strict FIFO and zombies, the known limit below); CHANGELOG.md (Added, plus Deprecated agent_start / agent_poll); README.md; docs/epics.md.

Testing

  • Core (delegation_tests.rs, 20 tests): the fold ignores forged spawns, a raw settle cannot charge below the rules, and the ancestor check ignores leases below the path.
  • End to end (delegate_tests.rs, 9 tests, with a fake tole binary):
    • a_symlinked_or_dotted_root_still_settles_its_children: through a symlinked root and through a real/../real root, a finished child is polled, settled and charged its measured 50 tokens / 1 step. (A relative root is not expressed, because the test harness cannot change the cwd safely in parallel tests; it is canonicalized the same way.)
    • a_new_delegate_never_starts_ahead_of_an_older_queued_one: with max_live_processes = 1, A runs and B is queued. A exits, nobody polls, and the next delegate C starts B first and queues C.
    • with_worktrees_two_writers_run_in_parallel_each_on_its_own_lease;
    • the_log_is_outside_the_workspace_and_forged_spawns_launch_nothing;
    • the profile child (argv, depth, node, grants, lease);
    • the second writer refused before spawning (in place);
    • measured usage or the whole carve;
    • the queue with a forged queue file, and the poll starting the queued child;
    • where the tool is registered.
  • ledger_file: an_unreaped_exited_child_is_not_alive.
  • Red/green:
    • Both new end-to-end tests fail against the previous head (228a57d): the symlinked root is never settled, and C starts while B stays queued. They pass with the fix.
    • Earlier rows still hold:
      • the worktree pre-check (the reported "write-leased … or run with --agents-worktree" error);
      • the ancestor check;
      • each fold condition and the Budget settle floor;
      • the log moved back into the workspace, and the fold allow check off;
      • grants and the intersection;
      • the lease pre-check, measured or unreadable usage;
      • the queue launched from its file;
      • the depth gate and the classic child;
      • the agent_dir / delegation() id checks.
  • Commands (private CARGO_TARGET_DIR, CARGO_INCREMENTAL=0, debuginfo off; develop 8aa231c merged in):
    • cargo fmt --all: clean
    • cargo clippy --workspace --all-targets -- -D warnings: clean
    • cargo test --workspace --no-fail-fast: 1509 passed, 0 failed
    • cargo test -p tole-core --no-default-features: 511 passed, 0 failed
    • cargo build -p tole-cli --no-default-features: ok
    • cora review --no-llm --base origin/develop: no issues found

Known limits

  • The root session's own usage sits outside the goal budget. The delegate tool cannot read the root session's usage records, so the root reports zero own spend when it delegates. Only its children's usage is carved and charged; a child's own spend is measured. Follow-up issue: "Delegation: measure the root session's own usage and charge it against the goal budget, so the orchestrator's own tokens and steps count toward the cap."
  • tole serve keeps the classic depth-1 agent_start / agent_poll.
  • What a child can still write: its own agent dir under <workspace>/tole-agents/<id>/, so it could understate its usage within its carve. A child with an approved run_command can write the data dir like any path outside the jail; the fold still refuses any spawn wider than its parent.
  • Exited children nobody polled: recovery settles them as interrupted, charged their measured usage. A polled child settles as a run (incomplete until part (b) adds the report).
  • Post-spawn lease loss: this path kills the child and settles it cancelled with its measured usage or whole carve. It is reachable only by a race between the pre-check and the lease, so no test drives it.
  • Size: about 1850 lines, over the ~1100 split point. The four review rounds and their tests add about 630. I did not split again, so the tool never lands without its safety checks.

…ts (#585)

The CLI faces (run, chat, mission) get delegate and delegate_poll over
tole_core::delegation. delegate carves the child's budget from the
session's node and starts it as tole mission with the carve as its step,
minute and token caps, optionally as a profile, with --allow = parent
intersect profile; the child keeps its provider keys plus the parent
grant intersect the profile's grants. An execute child takes a write
lease on the canonicalized directory it works in, checked before it
spawns. A child gets the tool again only while above the goal's depth.

delegate_poll charges a settled child its measured session usage, or
its whole carve when the records cannot be read, and so does recovery.
Past max_live_processes spawns queue and start on a later delegate or
poll. agent_start and agent_poll stay one release as aliases; tole serve
keeps the classic depth-1 tools.

agents::agent_dir now refuses ids that are not one plain path component
and child_env::delegation refuses node ids with a leading, trailing or
doubled dot (#621 review).
Comment thread crates/tole-cli/src/delegate.rs Fixed
@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

A queued spawn kept its whole launch spec, allow list, budget and secret
names included, in a file next to the log, and drain launched from it.
A child that can write that file could widen a queued sibling. The file
now keeps only what carries no authority (role, artifacts to read,
mailbox choice); allow list, budget, depth, mode, task, workspace and
profile are read back from the node the tree recorded and the secrets
are recomputed as parent grant intersect the profile's grants.
…ut of the workspace (#585)

The host launches what the delegation log records, and the log lived in
<workspace>/tole-agents/, where an execute child's file tools could
append a spawn with allow ["*"] or a huge budget for the next drain to
start. The fold now ignores any spawn whose parent is unknown or settled,
whose id is not the parent's next, whose depth passes the goal's limit,
that runs execute under a consult parent, whose allow list is not covered
by the parent's (or drops a parent deny), or whose budget or deadline
passes what the parent can still carve at that point of the log. A settle
is never charged below the node's reported own spend, and a budget stop
always its whole carve.

The log, its lock and the queue now live in the tole data dir,
<codecora home>/tole/delegation/<hash of the canonical workspace root>/,
outside every workspace; children keep getting its absolute path in
TOLE_DELEGATION_LOG.
Comment thread crates/tole-cli/src/delegate.rs Fixed
…heck (#585)

In worktree mode the pre-check asked lease_conflict for
<workspace>/.tole-worktrees, which also matches every lease BELOW that
dir, so a second execute child was refused while the first held its own
worktree, against the refusal message and the threat model. A new core
query, lease_ancestor_conflict, reports only a lease on the path itself
or an ancestor; the worktree pre-check uses it, and the in-place check
keeps lease_conflict, where a lease anywhere inside the workspace must
block a whole-workspace writer.
"budget": {"tokens": h.budget.tokens, "steps": h.budget.steps,
"deadline": h.budget.deadline},
"allow": h.allow, "dropped": h.dropped,
});
"node": n.id, "status": n.status, "reason": n.reason,
"used": n.used.map(|u| json!({"tokens": u.tokens, "steps": u.steps})),
"started_from_queue": started,
})))
Delegator::for_session kept the session root as given while the log
recorded children under the canonical root, so with a symlinked or ..
workspace path a poll looked up the wrong agent dir and never settled
the child. The root is now canonicalized once and every key uses it.

delegate never drained the queue, so a slot freed by recovery went to
the new spawn ahead of older queued ones. A new spawn now joins the
queue and the queue drains in log order (Node::seq) on every delegate
and poll; a queued writer whose workspace lease is taken holds the
queue. An exited but unreaped child (a zombie) no longer counts as
alive, so its slot frees without a poll, and goal ids are unique per
process.
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