Skip to content

feat(sandbox): sandbox the on_turnend stop gates and fold in the #845 review items (#748) - #859

Merged
ajianaz merged 2 commits into
developfrom
feat/748-sandbox-turnend-gates
Nov 15, 2026
Merged

ajianaz merged 2 commits into
developfrom
feat/748-sandbox-turnend-gates

Conversation

@ajianaz

@ajianaz ajianaz commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

What

Second slice of #748, part 2 of 2: the on_turnend stop gates run in the command profile through [sandbox.consumers] turnend = "off" | "optional" | "required" (user config only, default off). Also folds in the three low items from the independent review of #845. Refs #748 (stays open for cron scripts and deliveries). Replaces the stacked branch feat/748-sandbox-turnend, which was built on the pre-merge verify branch; this one is a fresh branch from develop (#845 merged).

Why

A stop gate usually runs code the agent wrote (cargo test) with no approval and nobody to answer (#676). Opt-in first, as decided on 2026-11-14; sandboxed by default stays a later phase (docs/sandbox-design.md §9.4, not done here).

Changes

  • tole-core hooks.rs: ProcessHook::run_raw goes through command_in_cwd(Consumer::TurnEnd, ...) and uses the launcher report. A refusal is a deny, never a pass: no working sandbox, a workspace layout the profile refuses (uninitialized or nested submodule, linked worktree inside the workspace), a launcher that cannot be spawned, or a non-zero exit for which bwrap --json-status-fd reported no exit code of the command, all return exit 126 with a reason, and the turn is denied (3-denial cap, then StopGateBlocked, tole run exit 6). A launcher failure says "the sandbox failed before or while starting the gate", not a gate verdict. A gate that ran and exited 125, 126 or 127 is its own verdict with its own output. A timeout or an unsandboxed spawn failure keeps the old non-blocking contract (feat: opt-in process hooks at the tool boundary (PreToolUse/PostToolUse, default OFF) #110); that is unchanged and not a refusal.
  • sandbox/command.rs: Consumer::always_unattended is now verify AND turnend: a stop gate runs while the model is finishing with nobody to answer, so optional counts as required for it (an attended chat gets no unsandboxed fallback for a gate; run_command and job_start keep theirs). A stop gate may be a program path (./gate.sh); it must be an executable file and its directory is bound read-only (inside the workspace or a system dir: nothing extra). Doctor: the optional-without-sandbox tail now says "a mission or a stop gate runs unattended"; the toolchain warning and the deterministic-failure note cover verify and turnend, by name.
  • config/mod.rs: turnend key (validated, config check, user layer only).
  • Review item 1 (docs): the LauncherReport doc comment and docs/sandbox-design.md §9.4 no longer say the command inherits fd 3 and "can only make the report agree". They say bwrap writes its own exit-code line last and the last one wins, that whether the command can reach the descriptor depends on the bwrap version (0.9.0 closes it inside the sandbox: echo >&3 is EBADF), and that a report that cannot be parsed or has no exit-code fails closed (Refused for verify, a deny for a gate), never a pass.
  • Review item 2: the two verify warning strings in doctor_lines_for had runs of literal spaces where a \ continuation was meant. Fixed, and the test asserts the exact text (and that no line contains a run of spaces).
  • Review item 3: real-bwrap tests that a verify command exiting 125, 126 or 127 is Failed (3 strikes, verify_failed), not Refused, and that a command writing forged exit-code lines to fd 3 cannot change the classification (fail stays fail, pass stays pass); the same for gates.
  • Docs: docs/sandbox-design.md §9.4 (call site, unattended rule, refusal and launcher failure for both consumers), threat model, CHANGELOG (new entry for the stop gates; the verify entry now points to it), epics. The README has no sandbox status line that goes stale. CI: the Sandbox (bubblewrap) job already runs stopgate_sandbox (5 targets) and verify_sandbox; the new tests live in those targets.

Callers and impact (cora is absent here; grep, each checked):

  • ProcessHook::run_raw: only ToolRegistry::turnend_denial (registry.rs), which treats any non-zero code as a deny; the tool hooks use run, which is not touched (consumer hook, never wrapped).
  • Consumer::always_unattended: launch_command (the only caller); plan_command itself still takes the flag, so unit tests pin both.
  • command_in_cwd / Launched: mission.rs (run_verify) and hooks.rs.
  • Tests rerun: command_tests.rs, command_sandbox{,_none}.rs, stopgate_sandbox.rs, sandbox_consumers.rs, verify_sandbox.rs, mission_e2e.rs, doctor and config tests.

Testing

  • Unit (command_tests.rs): the unattended rule for both consumers, doctor lines with exact text for verify, turnend and both, the launcher report parser.
  • Without bubblewrap (command_sandbox_none.rs, verify_sandbox.rs, real binary + mock provider): turnend = "optional" with no bwrap is refused as a deny even attended (reason names sandbox.consumers.turnend), the gate never ran; required exits 6 with the reason.
  • With bubblewrap (stopgate_sandbox.rs, verify_sandbox.rs; SKIPPED: line when absent, CI's Sandbox (bubblewrap) job runs them with TOLE_REQUIRE_BWRAP=1): a sandboxed gate cannot write .git/.tole, keeps its stdin payload and exit status; a gate exiting 125/126/127 is its own verdict; fake-bwrap tests: a launcher failure and an unspawnable launcher are denies that say so, and the gate never ran; verify 125/126/127 is verify_failed and fd 3 writes change nothing.
  • Fail without the change (reverted by hand, restored): the launcher-report check in run_raw removed => a_stop_gate_whose_launcher_fails_is_a_deny_that_says_so fails; always_unattended for turnend removed => the unit test, the doctor test and command_sandbox_none fail.
  • Gates on the final head: cargo fmt --all --check; cargo clippy --workspace --all-targets --all-features -- -D warnings; cargo clippy -p tole-core -p tole-cli -p tole-tui --no-default-features --all-targets -- -D warnings; cargo test of tole-core and tole-cli (default) and of tole-core, tole-cli, tole-tui with --no-default-features, with TOLE_REQUIRE_BWRAP=1: green, no SKIPPED.
  • How it was reviewed: scripts/review-local.sh could not run (cora is not installed here: exit 1, not a pass), so I self-reviewed git diff origin/develop...HEAD as an adversarial reviewer, checking: a refusal or a launcher failure cannot reach exit 0 (the report check only runs for a non-zero exit, and exit 0 from bwrap is the command's exit 0); report is Some only when the command is wrapped, so an unsandboxed gate is byte for byte the old path; the gate's stdin payload still reaches the program through bwrap; the scrub is on the bwrap process; run_raw never calls session_policy(), so --sandbox/--no-sandbox cannot reach a gate; a gate in $HOME is refused by the grant check; a forged fd 3 line cannot flip a verdict (closed on 0.9.0, last line wins otherwise). Residual, documented: a gate timeout stays non-blocking as before; toolchains outside system dirs are not visible; the security: the command sandbox profile protects .git/hooks and .git/config by leaf mounts only; renaming .git aside bypasses them #849 residuals of the command profile apply to gates too. A fresh independent reviewer will review this PR before merge.

…y that says so, pin 125/126/127 and fd 3 (#748)

Port the stop-gate sandbox onto the merged verify consumer: the gate goes
through command_in_cwd with the launcher report, a non-zero exit with no
command exit code from bwrap is a deny naming the sandbox, optional counts as
required for the gate, and the doctor toolchain warning covers both consumers
with exact text. Reword the launcher-report docs: the status descriptor is
closed inside the sandbox with bwrap 0.9.0 and an unparsable report fails
closed.
@github-actions

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

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