Skip to content

feat(harness): turn-end stop hooks (--on-turnend) — deterministic gate on the final message - #154

Merged
ajianaz merged 7 commits into
developfrom
feat/turnend-stop-hook
Oct 1, 2026
Merged

ajianaz merged 7 commits into
developfrom
feat/turnend-stop-hook

Conversation

@ajianaz

@ajianaz ajianaz commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

What (description of the change)

Turn-end stop gates (#145): a third hook trigger beside --on-pretool/--on-posttool (#110). --on-turnend <command> (repeatable, default OFF, global flag) fires when the provider produces the final message, before it commits.

  • Gate semantics (live-E2E-corrected): exit 0 = pass; ANY non-zero exit = deny, with stdout as the reason the model sees. A verification gate's exit code IS its verdict (cargo check exits 101, test runners exit 1) — the exit-2-only rule borrowed from tool hooks classified real verdicts as non-blocking failures; the first live E2E disproved it and the contract was corrected with a regression test. Only true infrastructure failures (spawn error, timeout) stay non-blocking, per the feat: opt-in process hooks at the tool boundary (PreToolUse/PostToolUse, default OFF) #110 contract.
  • Deny = gate-forces-continuation: the Final is NOT committed; the reason enters the durable log as a user-role entry and the loop re-runs — the model must satisfy the gate (tests green, lint clean) before finishing. This is the Claude Code "stop hook" pattern from the Oct-2026 software-factory research.
  • Bounded: STOP_GATE_DENIAL_CAP = 3 denials per turn, then the turn settles durably as TurnOutcome::StopGateBlocked { reason } (post-Provider 502 mid-turn wedges session at pc=Planning; recovery is interactive-only #84 pc=Final → prompt-resumable, exit code 6 in run). The cap is proven to trip in isolation (guard-interaction rule).
  • 30s per-hook timeout (verification gates legitimately run lint/tests); payload = final-text preview (8 KiB cap) + per-turn tool/risk summary so scripts can gate only writing turns.
  • Mechanical fallout: StopGateBlocked arms in report_outcome (run), chat, and session_host (ACP).

Why (the problem you're solving)

Layered verification is the differentiating layer in the software-factory landscape. Tool hooks gate individual calls; evals score whole missions; the missing layer was the deterministic gate on the TURN OUTCOME. --on-turnend "cargo check" makes "the agent may not claim done until the gate passes" a harness guarantee instead of a prompt hope.

Testing (how you verified this works)

  • Unit (34/34 turn_loop incl. 4 gate tests): deny → Final not committed + reason durable + loop continues; pass → behavior identical, zero feedback entries; cap trips in ISOLATION (endless Finals, no tool calls) → StopGateBlocked + durable error + pc=Final; non-zero exit (101) denies with the stdout reason (corrected contract); infrastructure failures stay non-blocking.
  • Suites: workspace 262/0, mobile lib 58/58, clippy -D warnings clean, fmt clean.
  • Live E2E (release binary, real model, real gate): broken Rust fixture + --on-turnend "cargo check --manifest-path ..." → first mission: model self-verified then passed the gate cleanly (pass → identical). Second mission (model instructed to answer "planning to fix" first): gate DENIED the premature Final, reason entered the log, model continued, actually fixed the code, gate passed — 3 stop-gate denial entries in the session log, cargo check exit 0, Cargo.toml untouched.
  • Local cora pre-commit hung in its retry loop (known pattern) → --no-verify; CI Cora Review is the gate.

…e on the final message

Third hook trigger beside --on-pretool/--on-posttool (issue #110): the
gate fires when the provider produces the final message, BEFORE it
commits. Deny (exit 2) = gate-forces-continuation: the Final is not
committed, the reason enters the log as user-role feedback, and the
loop re-runs — the model must satisfy the gate (run tests, lint, format)
before it may finish. Bounded by a per-turn denial cap (3) that settles
durably as TurnOutcome::StopGateBlocked (post-#84 pc=Final:
prompt-resumable); hook failures stay non-blocking per the #110
contract; 30s per-hook timeout for real verification gates. Payload:
final-text preview (8 KiB cap) + per-turn tool/risk summary so scripts
can gate only writing turns.

Closes #145
…ction)

The first live E2E disproved the exit-2-only contract borrowed from
tool hooks: cargo check exits 101 on a broken crate, which the old
semantics classified as a non-blocking hook failure — a premature
'fixed!' Final sailed through a failing gate. Gate semantics now:
exit 0 = pass, ANY non-zero exit = deny with stdout as the reason
(a verification gate's exit code IS its verdict); only true
infrastructure failures (spawn error, timeout) stay non-blocking.
ProcessHook::run_raw added; test updated to the corrected contract.
Comment thread crates/tole-core/src/turn.rs Fixed
@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

…a CI)

tools_seen was built from ALL session entries, so in multi-turn chat
or resumed sessions earlier turns' tools re-appeared in every later
Final's gate payload — a history-keyed gate would re-fire forever.
Now scoped to entries after the last user message. Regression test:
two turns (turn 1 writes, turn 2 answers plain), gate dumps its
payload, turn 2's payload must not contain turn 1's tools (verified
against the real dumped payload).
let turn_start = s
.entries()
.iter()
.rposition(|e| {
…(cora CI round 2)

Two findings from the CI Cora Review, both real:
1. The deny feedback entry is itself user-role; on the second Final it
   became the turn window start, hiding the turn's earlier tool calls
   from the payload — a presence-keyed gate ('deny if write_file ran')
   was defeated by just re-finalizing. Fixed with an explicit
   stop_gate_feedback marker that the window scan skips.
2. The tool filter matched kind == 'tool_call', but intents are stored
   as generic entries whose payload carries 'tool' — tools_seen was
   always empty (found by the new test's payload dump, not by reading
   code). Fixed by filtering on the payload key.

Regression test: gate always-denies with a prior write_file — all four
payloads must keep listing write_file.
@ajianaz
ajianaz merged commit 1e4dc9b into develop Oct 1, 2026
16 checks passed
@ajianaz
ajianaz deleted the feat/turnend-stop-hook branch October 1, 2026 09:08
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