Skip to content

feat(cron): resume a parked run after an approval (#581, part 1) - #638

Merged
ajianaz merged 3 commits into
developfrom
feat/parked-approvals
Nov 12, 2026
Merged

ajianaz merged 3 commits into
developfrom
feat/parked-approvals

Conversation

@ajianaz

@ajianaz ajianaz commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

What

Part 1 of #581: a cron run parked on a Write resumes after an /approvals allow. An allow on a cron run's parked call continues that run in the same session, and only the approved call runs.

Requirement 1 (allow[] is the only pre-authorization; Destructive cannot be registered) and the parking half of requirement 2 shipped in #579. This PR adds the resume. Agent-created jobs (created_by, created paused, allow[] limited to the creator's) and config-sourced job trust follow in part 2, also based on develop.

Why

Before this, tole serve --cron parked a cron Write that the job's allow list did not cover, but an allow could only resume serve sessions. The cron run stayed awaiting_approval for good, so the only option was to re-run the whole job.

Changes

How true resume works with the append-only ledger and session

  1. Ledger (tole-core). A new resume record {fire_id, resumes, job_id, claimed_at, lease} is appended under the ledger lock.
    • The fold accepts it only when the resumes run settled awaiting_approval for the same job, was not resumed before, and the new fire_id is unused. Any other resume line, hand-written or repeated, is ignored and never runs.
    • The continuation is an ordinary pending fire with the parked run's scheduled_for. It settles with its own done, and recover settles it interrupted when its lease dies, like any run.
    • The parked done is never changed. last_done now prefers the later of two runs with the same scheduled_for, so the continuation's outcome becomes the job's latest.
    • Ledger::resume refuses a run that is unsettled, not awaiting approval, or already resumed, and re-checks after appending (lost race).
  2. Session (tole-cli). cron::tick::resume_parked handles the rest.
    • It finds the unresumed awaiting_approval run with that session and runs the claim checks before writing the record: provider, delivery targets, skills and profile (pins and approval), and the daily cap. A refused continuation writes nothing, so the run stays resumable once the cause is fixed.
    • It checks that the run's session is still parked mid-turn. The session file lives in the job's workdir, which the job can write, so this pre-claim check reads it with O_NOFOLLOW | O_NONBLOCK, requires a regular file and caps it at 64 MiB.
    • The agent then opens the file once and checks that same handle. It calls JsonlStorage::open_bounded(path, 64 MiB) and checks the parked pc on the storage it opened. The replay and every append use that one handle, so a file swapped after the pre-claim check is refused at the open.
    • I chose to harden JsonlStorage::open for all callers rather than add a resume-only entry point. It now opens the file once, on unix with O_NOFOLLOW | O_NONBLOCK, and on every platform an fstat of the handle must show a regular file. The replay reads through a duplicate of that handle, the torn-tail repair truncates through it, and appends write through it; the path is never re-opened.
      • The .lock sidecar is opened the same way. Its pid write truncates the file, so a planted .lock symlink would otherwise truncate its target.
      • So are compaction's temp file and its reopen after the rename.
      • Why for all callers: a session file reached through a symlink or a FIFO is never legitimate, and every host faces the same swap risk. The full suite passes unchanged.
      • open_bounded is the one addition: it applies the size cap to the handle, and stops reading at cap + 1 if the file grows during the replay.
      • No new dependency (libc was already used here). The flags are cfg(unix); other platforms keep their behavior apart from the regular-file check. --no-default-features builds and passes.
    • Not covered: a directory on the path (.tole or .tole/sessions) swapped for a symlink. That could only reach a file with the same random session id in another directory, but the .lock sidecar would be created there. This is recorded in the threat model.
    • On that storage (the same session) it resumes the parked turn with no new prompt. The parked turn replans from the denial note. The approval is the existing one-shot for exactly the approved call (same tool, same input), so the model can repeat that call once and nothing else.
    • An allow does not run the call by itself. The approved call runs only if the resumed model makes that same call again. If the model does something else, the approved call never runs and the run settles on whatever the model does instead. The CHANGELOG and docs say this plainly.
    • The resumed turn runs under the same TurnPlan and metered provider as a fresh run: the job's model pin, its max_steps step budget, its token budget, its max_minutes limit and the cancel token. A new tole_core::turn::resume_turn_with_observer passes the plan's observer to the replan loop (resume_turn_with_cancel is unchanged), and turn_runner::resume_turn_with resumes under a plan.
    • Each budget counts the continuation alone, not the whole run (docs/automation-design.md §5). The continuation is a run of its own with its own done, and every resume needs a human allow. Its done reports only its own tokens, so the daily cap does not count the parked run's tokens twice.
    • The continuation goes through the same run, settle and deliver path as a tick.
  3. serve. On allow, POST /approvals/{id}/decision resumes a session that serve hosts as before. Any other session is handed to cron::resume_after_approval.

Nothing is auto-approved. A resume without an allow (denied or expired) parks the call again and never runs it.

Not resumable

  • Script jobs, which never park.
  • Runs parked by a standalone tole cron tick: there is no queue, so there is no approval to give. Re-run them.
  • A denied or expired approval: the run parks again.
  • Approvals lost when tole serve restarts, because the queue is in memory. The run stays awaiting_approval; re-run it.
  • A run whose session is no longer parked mid-turn, a run of a removed job, and a parked run that was already resumed. A continuation can park and be resumed in turn.

Docs

  • docs/automation-design.md §5: how resume works and what is not resumable.
  • docs/threat-model.md: a new row and a scheduled-jobs paragraph.
  • CHANGELOG.

Testing

TDD. Each rule was broken on purpose and a test failed for each:

Deliberate break Failing test
Fold accepts a resume of a run that is not parked a_forged_resume_line_never_revives_a_run
Fold accepts a second resume of the same run a_forged_resume_line_never_revives_a_run
Ledger::resume skips the status check a_parked_run_resumes_once_as_a_new_pending_run
Ledger::resume skips the already-resumed check a_parked_run_resumes_once_as_a_new_pending_run
last_done keeps the strict > a_parked_run_resumes_once_as_a_new_pending_run
resume_parked ignores resumed_by an_allowed_parked_write_resumes_the_run_once
Session pc check removed a_parked_record_whose_turn_ended_is_not_resumed
Resume opens a new session 3 resume tests
Resume skips the parked turn 3 resume tests
Setup errors swallowed (unwrap_or_default) a_skill_changed_while_parked_refuses_the_resume
Session read with plain open (follows symlinks, blocks on a FIFO) a_planted_session_symlink_or_fifo_refuses_the_resume
Resume drops the plan's observer (no step budget) a_resumed_run_stops_at_the_jobs_max_steps
Resume ignores the job's model pin a_resumed_run_uses_the_jobs_pinned_model
Resume runs unmetered (no token budget) a_resumed_run_keeps_the_token_budget
Continuation reports the whole session's tokens a_resumed_run_keeps_the_token_budget
Session open without O_NOFOLLOW a_session_swapped_for_a_symlink_after_the_check_is_refused_at_the_open, a_symlinked_lock_sidecar_is_refused_and_its_target_left_alone, compaction_never_writes_through_a_planted_temp_symlink, a_session_swapped_after_the_resume_check_is_refused_at_the_open
Session open without O_NONBLOCK a_session_swapped_for_a_fifo_is_refused_without_blocking (the read-only open blocks)
open_bounded ignores the cap open_bounded_refuses_a_session_over_the_cap
.lock sidecar opened without the hardened open a_symlinked_lock_sidecar_is_refused_and_its_target_left_alone
Compaction temp file via File::create compaction_never_writes_through_a_planted_temp_symlink

The open hardening was red first. All 5 new tole-core tests failed against the old open, with open_bounded stubbed to ignore the cap: the symlink was followed, the FIFO open blocked, the .lock symlink truncated its target, compaction wrote through the temp-file symlink, and the cap was not applied.

The regular-file check has no test of its own. With O_NONBLOCK, a FIFO or a directory already errors on Linux (EAGAIN or EISDIR), so removing the check fails nothing. The check matters for device nodes, and a test would need mknod, which needs root.

Before the budget fix, the step-budget test failed (the continuation settled Error from the loop guard instead of Budget), and the token test failed on the double count (6 tokens instead of 4). The model-pin test already passed, because both arms used the provider built from the pinned config. Breaking the pin on the resume arm only proves that the test now guards it.

New tests:

  • crates/tole-core/tests/ledger_core.rs: one resume per parked run; forged and repeated resume lines ignored by the fold; a continuation dies with its lease.
  • crates/tole-core/tests/untrusted_session_open.rs (unix): replays the race, where the check passes, then the file is swapped for a symlink to an outside copy, then open or open_bounded refuses it and the outside copy is byte for byte unchanged. Also covered:
    • a FIFO is refused by open, open_bounded and open_read_only within 5 s;
    • a symlinked .lock sidecar is refused and its target left alone;
    • compaction never writes through a planted temp-file symlink;
    • open_bounded enforces the cap and still commits within it.
  • crates/tole-cli/tests/cron_agent.rs:
    • an allow resumes the run once: same session, the prompt appears once, the write lands, the parked done stays, a second resume is None;
    • a resume without an allow parks again and writes nothing;
    • a model that does not repeat the call writes nothing;
    • a parked record whose session already ended is refused;
    • a skill changed while parked refuses the resume and leaves the run resumable;
    • a planted symlink or FIFO session file is refused without blocking;
    • a resumed run stops at the job's max_steps, counting its own steps;
    • a resumed run sends the job's pinned model;
    • a resumed run keeps the token budget and reports only its own tokens;
    • the race at the cron level: after the parked run was allowed, the session file is swapped for a symlink (to a valid parked copy outside) or a FIFO, and run_agent is entered directly, as it would be after resume_parked's check. The run errors within 5 s, the outside copy is unchanged, and the approved write never runs.

Gates:

  • cargo fmt --all
  • cargo clippy --workspace --all-targets --all-features -- -D warnings
  • cargo test --workspace: 1608 passed
  • cargo test -p tole-core --no-default-features: 565 passed
  • cora review --no-llm --staged: no issues

Not covered automatically: the serve route hand-off to cron::resume_after_approval. An end-to-end test would wait up to 61 s for the in-process ticker. The resume itself is tested at the library level with the same queue.

An allow on a cron run's parked Write (POST /approvals/{id}/decision for
a session serve does not host) continues the run in place.

- tole-core ledger: a new `resume` record claims the continuation of a
  run that settled awaiting_approval, at most once. The fold ignores a
  resume line for any other run (not parked, other job, already
  resumed), so a hand-written line cannot revive a finished run or run
  a parked one twice. The continuation is an ordinary pending fire (same
  scheduled_for) that settles with its own done and dies with its lease;
  the parked done stays, and the continuation's outcome is the job's
  latest.
- tole-cli cron: resume_parked runs the claim checks (provider, delivery,
  skills and profile, daily cap) before writing the record, reads the
  run's session from the job's workdir without following a symlink or
  blocking (size-capped, regular file only), refuses a session whose
  turn already ended, then reopens the session and resumes the parked
  turn with no new prompt; the approver's one-shot lets exactly the
  approved call run.
- serve: the allow route resumes a serve session as before and hands
  any other session to the cron resume.
- Docs: automation-design section 5 (how resume works, what is not
  resumable), threat model row and scheduled-jobs note, CHANGELOG.
Comment thread crates/tole-cli/src/cron/agent.rs Fixed
@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

The resume arm called resume_turn_with_cancel directly, without the
plan's observer, so a continuation had no step budget (only the wall
clock and the token meter bounded it).

- tole-core: resume_turn_with_observer passes an observer to the replan
  loop; resume_turn_with_cancel keeps its behavior (no observer).
- tole-cli: turn_runner::resume_turn_with resumes under a TurnPlan, and
  the cron resume arm uses it with the same plan and metered provider as
  a fresh run: model pin, max_steps, token budget, cancel. Each budget
  counts the continuation alone.
- A run's done reports only its own tokens, so the daily cap no longer
  counts the parked run's tokens a second time on a continuation.
- Docs and CHANGELOG say plainly that an allow runs the call only if the
  resumed model makes that same call again.
Comment thread crates/tole-cli/src/cron/agent.rs Outdated
Some("cron"),
) {
let opened = match resume {
// Checked again right before the reopen: the session file sits in
…581)

The cron resume checked the session file (no symlink, regular file,
size cap) and then re-opened the path with JsonlStorage::open. The
job can write its workdir, so it could swap the checked file for a
symlink in between and have the resumed turn append through it.

- tole-core: JsonlStorage::open opens the session file once and
  replays, repairs a torn tail and appends through that handle; the
  path is never re-opened. On unix the open uses O_NOFOLLOW and
  O_NONBLOCK. On every platform an fstat of the handle must show a
  regular file. The .lock sidecar (whose pid write truncates the file)
  and compaction's temp file and reopen are opened the same way.
- New JsonlStorage::open_bounded also caps the size on the handle and
  stops reading at the cap if the file grows during the replay.
- tole-cli cron: the resume opens the session with open_bounded and
  checks that the turn is still parked on that same storage, instead
  of a separate check followed by a re-open.
- Docs (automation-design section 5, threat model) and CHANGELOG.
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