Skip to content

feat(git): review gate before git commit via cora hook verify - #539

Closed
ajianaz wants to merge 3 commits into
developfrom
feat/review-gate-before-commit
Closed

ajianaz wants to merge 3 commits into
developfrom
feat/review-gate-before-commit

Conversation

@ajianaz

@ajianaz ajianaz commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

What

While both cora review tools are attached, a git commit is refused until the staged diff has a validated review, and the refusal tells the model how to get one. Covers the git tool's commit op and run_command / job_start running git commit.

Why

AGENTS.md makes a review of the staged diff mandatory before every commit, but an agent session could commit without ever reviewing: the review flow was guidance only (#536 made sure missions at least see it). Closes #534.

Design change from the issue (please read)

#534 proposed that tole compute a hash of git diff --cached and compare it with the diff the model passes to review_finalize. While reading cora-code 0.20.0 (the published crate) I found cora already ships exactly this: its commit gate (cora #674). mcp_cora_cora_review_finalize leaves a marker bound to a fingerprint of the exact change set under <git-dir>/cora/validations/, and cora hook verify answers "may this staged diff be committed?" with an exit code (0 validated or nothing to review, 10 reviewed but blocking, 11 not validated). So tole does not hash or record anything: it asks cora hook verify before the commit runs. Reimplementing it would have given two definitions of "this diff" that can disagree (cora's fingerprint ignores context size and index lines; a text hash of the model-passed diff would not).

Consequence: the gate needs cora >= 0.20.0 (the general supported floor stays 0.19.0). With an older cora, hook verify is a usage error and the gate fails open with one warning.

Changes

  • New review_gate.rs (lib):
    • Gate: shared per session, inactive until both mcp_cora_cora_review_prepare and _finalize are registered (a gate with no way to get a review would only lock the agent out). check_commit() runs cora hook verify --quiet in the repo (15 s timeout, child-env scrub) and maps 0 / 10 / 11; anything else (old cora, missing binary, timeout) is "unavailable": one warning, commits proceed.
    • Gated: a Tool decorator that forwards everything and asks the gate only for a commit. Kind::Git reads {"op":"commit"}; Kind::Command splits {"command": "..."} with the existing RunCommandTool::split_argv and recognizes git commit behind env, VAR=x, a path to git and git's global options (-C, -c, --git-dir, ...).
    • Policy = off | warn | block (default block). warn lets the commit through with a review_gate note in the result.
  • tool_roster.rs: when the policy is not off and MCP servers are attached, git, run_command and job_start are wrapped; the gate is activated after the MCP tools register. Server faces have no MCP servers, so no gate.
  • TOLE_REVIEW_GATE / global --review-gate (the flag sets the env var at startup, so child agents inherit it); an invalid value fails startup. Not a config key yet: a cloned project's .tole/config.toml is untrusted input and review_gate = "off" there would switch the gate off; it should join the trust-gated keys in a follow-up.
  • tole doctor prints one line about the gate (policy, whether cora has hook verify).
  • README, CHANGELOG, docs/threat-model.md (new section).

What it is not

A guardrail for a cooperating agent, not a security boundary (the approver is). sh -c "git commit", a script, a git alias and merge / cherry-pick / am are not recognized; the review is self-review by the model that wrote the change; a direct human git commit is not gated by tole. All documented in the threat model.

Testing

  • cargo test --workspace passes
  • cargo fmt --all -- --check passes
  • cargo clippy --workspace --all-targets -- -D warnings passes (also the shell-tools profile; cargo check -p tole-cli --no-default-features passes)
  • Cora review run locally: not run (cora is not installed where this was written); the CI Cora Review job is the verdict.
  • Unit tests: is_git_commit for 13 spellings that must match and 10 near-misses that must not; policy parsing; the gate against a fake hook verify (inactive or off never runs cora; block refuses 10 and 11 and passes 0; warn notes; old or missing cora fails open once); the decorator gates only commits in both input shapes; the real git tool in a real temp repo refuses an unreviewed staged diff and commits once cora validates it.
  • 8 end-to-end tests with the real binary, a scripted provider, a fake cora (MCP server for the two review tools plus hook verify from an exit-code file) and a real repo: an unreviewed diff is not committed and the model is told why; it commits after validation; a blocking verdict refuses; off and warn do not refuse and the flag beats the env; no review tools (--no-auto-mcp) means no gate; an invalid policy fails startup; run_command / job_start running git -C . commit --no-verify are refused (control: the same call commits with the gate off); doctor reports the gate. Mutation checked twice: never activating the gate, and never wrapping the tools, each fail 4 of the e2e tests.
  • Not tested: a real cora 0.20 (the fake speaks initialize, tools/list and the hook verify exit codes documented in cora's source), and cora's marker being written by a real review_finalize in the repo.

Related Issues

Closes #534. Related: #536 and #535 (merged).

Checklist

  • Branch name follows convention (feat/)
  • Branch is from develop
  • Commit messages follow Conventional Commits
  • No secrets or credentials committed
  • One logical change per PR

Generated by Claude Code

While both cora review tools are attached, a `git commit` is refused until
the staged diff has a validated review. tole records and hashes nothing
itself: it asks cora >= 0.20.0's own commit gate (`cora hook verify`:
0 validated, 10 blocking, 11 not validated), whose marker is bound to the
exact change set.

Covers the `git` tool's commit op and run_command / job_start running
`git commit` (behind env, VAR=x, a path to git, -C/-c). TOLE_REVIEW_GATE
or --review-gate = off|warn|block (default block; the flag wins; invalid
fails startup; no config key yet so a cloned project cannot weaken it).
No review tools attached means no gate; a cora without `hook verify` or
one that cannot run fails open with one warning. tole doctor reports it.

ajianaz commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor Author

CI: Test failed on serve_approvals::approval_allow_round_trip_executes_the_work (the approved write must have executed, crates/tole-cli/tests/serve_approvals.rs:271). I do not think this is this PR's. I have not re-run it yet: GitHub refused (This workflow is already running) while other jobs of the run are in progress; I will re-run once, when the run finishes.

  • The diff touches the git / run_command / job_start wrappers, review_gate.rs, the CLI flag, tole doctor and docs. tole serve attaches no MCP servers, so no gate is created for it, and the failing test (a write_file approval round trip over serve) never reaches any of that code. The test file passes locally, 25 runs in a row, and so does the whole workspace.
  • Likely cause, a race in the test itself (not reproduced): after POST /approvals/{id}/decision returns 202, it polls GET /sessions/{id}/status until busy is not true, then immediately asserts that approved.txt exists. If the resumed turn has not yet set busy when the first poll runs, the loop exits at once and the file is not written yet; on a slow runner that is enough. The whole test file finished in 0.37 s in the failing run, which fits.
  • Proposed patch (not included here, to keep this PR to one change): after the loop, poll for the file for a few seconds (or wait for busy to go true then false) instead of asserting once.

If the re-run is green I will leave it at that; if it fails again I will treat it as real and dig in.


Generated by Claude Code

@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

…commits (#534)

`cora hook verify` reads the staged diff of the repository it runs in, but
the gate always ran it in the session directory while recognizing
`git -C sub commit`, `--git-dir=...` and `GIT_DIR=...` commits: a validated
session repo (or "nothing to review") could unlock a commit into another
repository.

- `-C <dir>` (each relative to the previous) is now verified in <dir>.
- A commit that redirects git's git dir or work tree (--git-dir,
  --work-tree, GIT_DIR / GIT_WORK_TREE / GIT_INDEX_FILE / ...,
  -c core.worktree=) has no directory cora can be asked about: refused
  under block, noted under warn, without asking cora.

ajianaz commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Cora Review (blocking, review_gate.rs:156): valid, fixed.

  • Finding: Gate::verify always ran cora hook verify in the session directory, while is_git_commit recognized git -C sub commit, --git-dir=... and env GIT_DIR=... git commit. cora reads the staged diff of the repository it runs in, so a validated session repo (or "nothing to review", which is also exit 0) could unlock a commit into a nested repo. That contradicted the documented -C coverage. Verified by reading the code path, then reproduced as a failing test before the fix.
  • Fix:
    • git -C <dir> commit is verified in <dir> (each -C relative to the previous one, the first to the session dir).
    • A commit that redirects git's git dir or work tree (--git-dir, --work-tree, GIT_DIR= / GIT_WORK_TREE= / GIT_INDEX_FILE= and similar, -c core.worktree=, -c core.gitdir=) has no directory cora can be asked about, so it is refused under block (noted under warn) without asking cora, instead of being verified against the wrong index.
  • Tests: the classifier for 20 spellings (target dirs and redirects), a fake cora that records its working directory (the -C sub commit is verified in .../sub, and a refusal there is about sub), redirected commits are refused without cora being asked even when the session repo is "validated", warn notes and off passes. Mutation checked: making the target directory ignore -C fails that test.
  • Docs updated (README, CHANGELOG, threat model): "Which repository is asked".
  • Checked locally: fmt, clippy -D warnings (default and shell-tools profiles), cargo test --workspace (all green).

The earlier serve_approvals failure (unrelated test race, see my previous comment) still needs its single re-run; the new push re-runs CI on the new head anyway.


Generated by Claude Code

ajianaz commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #540: the same change squashed to one commit with no trailers. Message Standard rejected the Claude-Session: trailer on the last commit, and history on this branch cannot be rewritten (no force pushes). Closing in favour of #540.


Generated by Claude Code

@ajianaz ajianaz closed this Nov 10, 2026
ajianaz added a commit that referenced this pull request Nov 10, 2026
## What

While both cora review tools are attached, a `git commit` is refused
until the staged diff has a validated review, and the refusal tells the
model how to get one. Covers the `git` tool's `commit` op and
`run_command` / `job_start` running `git commit`. Supersedes #539 (same
change, squashed to one commit because the old branch had a commit
trailer the Message Standard check rejects and history cannot be
rewritten here). Closes #534.

## Why

AGENTS.md makes a review of the staged diff mandatory before every
commit, but an agent session could commit without ever reviewing; the
review flow was guidance only.

## Design change from the issue

#534 proposed hashing `git diff --cached` in tole. `cora-code` 0.20.0
already ships this (cora #674): `review_finalize` leaves a marker bound
to a fingerprint of the exact change set, and `cora hook verify` answers
whether the staged diff may be committed (exit 0 validated or nothing to
review, 10 reviewed but blocking, 11 not validated). tole asks `cora
hook verify` before the commit runs instead of defining "this diff" a
second time. Needs cora >= 0.20.0; with an older cora the gate fails
open with one warning.

## Changes

- New `review_gate.rs`: `Gate` (inactive until both
`mcp_cora_cora_review_prepare` and `_finalize` are registered; 15 s
timeout, child-env scrub; tooling trouble fails open with one warning),
`Gated` decorator (forwards everything, asks the gate only for a
commit), `Policy` = `off | warn | block` (default `block`).
- Commit recognition: `git commit` behind `env`, `VAR=x`, a path to git
and git global options. The commit is verified in the repo it targets
(`-C dir`, `-Cdir`); commits redirected by `--git-dir`, `--work-tree`,
`GIT_DIR`-style env, `core.worktree`, or `env -C/--chdir` are refused
under `block`.
- `tool_roster.rs` wraps `git`, `run_command`, `job_start` when the
policy is not `off` and MCP servers are attached; `TOLE_REVIEW_GATE` /
global `--review-gate`; `tole doctor` prints a gate line.
- README, CHANGELOG, `docs/threat-model.md` (new section). Not a config
key yet: an untrusted project config could set `off`; follow-up to make
it trust-gated.

## What it is not

A guardrail for a cooperating agent, not a security boundary (the
approver is). `sh -c "git commit"`, scripts, aliases and `merge` /
`cherry-pick` / `am` are not recognized; the review is self-review; a
human `git commit` is not gated. Documented in the threat model.

## Testing

- [x] `cargo test --workspace`, `cargo fmt --all -- --check`, `cargo
clippy --workspace --all-targets -- -D warnings`
- [ ] Local `cora review --staged`: not run (cora is not installed in
this environment); the CI Cora Review job is the verdict.
- [x] 14 unit tests (commit-call parsing including `-Cdir` and `env -C`
forms, policy, gate against a fake `hook verify`, decorator, real git
tool in a temp repo) and 8 e2e tests with the real binary, scripted
provider, fake cora and a real repo; mutation checked.
- Not tested: a real cora 0.20 binary.

## Related Issues

Closes #534. Supersedes #539.

---
_Generated by [Claude
Code](https://claude.ai/code/session_01GwJr9GzLbDFeXo3HZHZpyP)_

Co-authored-by: Claude <[email protected]>
ajianaz added a commit that referenced this pull request Nov 10, 2026
…pproval round trip (#543)

## What

Fixes the flaky
`serve_approvals::approval_allow_round_trip_executes_the_work` (it
failed on CI for #539 and #542, both times unrelated to the PR's diff).

## Why

After `POST /approvals/{id}/decision` returns `202 resuming`, the test
waited for the resumed turn by polling `GET /sessions/{sid}/status`
until `busy != true`. Right after the 202 the resume may not have set
`busy` yet, so the very first read says "not busy", the loop exits at
once, the queue entry already reads `approved`, and the file assertion
runs before the replayed `write_file` has landed ("the approved write
must have executed"). A "not busy" read proves nothing at that moment.

## Changes

- `crates/tole-cli/tests/serve_approvals.rs`: poll for the effect itself
(`approved.txt`, up to 10 s), then keep the existing wait for the
session to settle. No production code and no assertion weakened: the
same checks run, after the condition they depend on.

## Testing

- [x] Reproduced under CPU load (6 busy loops on 4 cores),
`approval_allow_round_trip` run 25 times in a row: **old code failed
9/25, the fix 0/25**.
- [x] `cargo fmt --check`, `cargo clippy -p tole-cli --all-targets -D
warnings`, `cargo test -p tole-cli --test serve_approvals` (3 passed).
- [ ] Local `cora review --staged`: not run (cora is not installed in
this environment); the CI Cora Review job is the verdict.

## Not included: the `tui_e2e` /quit race

An earlier CI failure suggested tests send `/quit\r` before the turn is
idle (the text would be queued as steering input instead of running as a
command), and adding `t.idle()` before `/quit` looked like the fix. I
could not reproduce it: 23 runs of the old `tui_e2e` binary under
heavier load (8 busy loops, 8 test threads) all passed, so that change
is not in this PR. It should be revisited if it fails on CI again, with
the failing test name in hand.

## Related Issues

Related to #539 and #542 (CI failures that were not theirs).

---
_Generated by [Claude
Code](https://claude.ai/code/session_01GwJr9GzLbDFeXo3HZHZpyP)_

Co-authored-by: Claude <[email protected]>
@ajianaz
ajianaz deleted the feat/review-gate-before-commit branch November 11, 2026 04:10
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.

feat(git): review gate before git commit (cora agent-as-reviewer, on by default when attached)

2 participants