Skip to content

feat(serve): answer a prompt that stops for an approval with approval_required - #913

Merged
ajianaz merged 4 commits into
developfrom
feat/571-approval-required-stop-reason
Nov 17, 2026
Merged

ajianaz merged 4 commits into
developfrom
feat/571-approval-required-stop-reason

Conversation

@ajianaz

@ajianaz ajianaz commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What

POST /sessions/{id}/prompt on tole serve answers 200 {"stopReason":"approval_required","approval":{"id","tool","status":"pending",...}} when the turn stops because a Write needs an approval that its own turn queued. Every other non-final stop stays refusal.

Why

A graphical client or chat gateway could not tell "waiting for approval" from "failed" using the prompt response, and had to correlate an ephemeral SSE frame or poll GET /approvals. Closes #571 (G0a of the gateway epic #910; serve protocol only, no UI).

Changes

  • approvals.rs: the queue gets a monotonic sequence (PendingApproval.seq, mark(), queued_since()), assigned under the entries lock so sequence order equals insertion order. approval_required_body names only an entry of this session and tool queued after the snapshot. The row has the GET /approvals shape and appears only while the entry is pending; if it expired or was decided meanwhile, the answer is approval_required without approval.
  • session/host.rs: run_session_turn_detailed (optional queue parameter) takes the snapshot right after the turn claims the session and returns it as TurnResult.queue_mark; TurnResult is #[non_exhaustive]. The ACP and MCP entry points and stop_reason() are unchanged, so ACP stopReason values and the MCP stop_reason do not change. tole_core::turn is untouched.
  • serve.rs: the prompt route uses the snapshot (one turn per session at a time, 409 otherwise) and answers refusal when the turn queued nothing, such as a pre-tool hook denial, so no client waits for an approval that does not exist. A blocked stop gate, an unknown tool, a provider failure and a loop stay refusal.
  • Docs: docs/mobile-control.md (including that a turn resumed after an allow has no approval_required: watch approval_requested or poll GET /approvals?session= until feat(cron): outbound signed webhook delivery target for cron results and approval requests #911), CHANGELOG (compatibility note and the new public items, including the new seq field on PendingApproval), docs/epics.md (feat(serve): answer a prompt that stops for an approval with stopReason approval_required and the approval id #571 moved to the gateway track epic: gateway — Telegram (then Discord) over tole serve, in a separate repository #910).
  • Code intelligence (noisy on Rust, verified by grep): stop_reason is called only by run_session_turn_detailed and resume_session_turn; ACP and MCP tole_session_prompt go through the unchanged wrappers; queue_entry is called only by QueueApprover::decide. Tests to rerun: serve_approvals, serve_events, serve_approval_required, acp_approval, session_tools.

Testing

  • Real-binary (serve_approval_required): an approval stop answers approval_required with the row and the id POST /approvals/{id}/decision accepts; overlapping sessions get their own rows; unknown tool, provider failure, loop and a blocked stop gate (--on-turnend, cfg(unix)) are refusal with an empty queue.
  • Unit tests: row queued during the turn; entry decided before the lookup (answer without approval); a stop that queued nothing is not approval_required even with an older pending entry of the same session and tool; no leak across sessions or tools; sequence order equals insertion order under concurrent pushes.
  • Mutation-checked: forcing the old refusal path fails the real-binary tests; dropping the session filter or the sequence filter, or assigning the sequence outside the lock, fails the unit tests. Not covered by a test: the post-claim position of the snapshot (the window needs a resumed turn queueing an entry between two instructions and cannot be driven deterministically).
  • Independent review: two rounds by fresh read-only reviewers. Round 1 found that a pre-tool hook denial was reported as approval_required with nothing queued, and that a stale pending entry of the same session and tool could be returned (fixed by the snapshot). Round 2 found no blocker; its two should-fix items (snapshot taken before the claim, sequence assigned outside the lock) are fixed in the last commit.
  • cargo fmt, cargo clippy --workspace --all-targets -- -D warnings, cargo test --workspace (fresh TOLE_TEST_TMP, empty afterwards), cargo check -p tole-cli --no-default-features: clean. cora review --staged --no-llm: no issues. scripts/review-local.sh: not run in this container.

claude and others added 4 commits November 17, 2026 22:13
…_required

POST /sessions/{id}/prompt now answers stopReason approval_required plus
the pending approval row (GET /approvals shape, looked up by session id
and tool) instead of refusal. The row is omitted when the queue entry is
gone. Other non-final outcomes keep refusal; ACP and MCP are unchanged.

Closes #571
A pre-tool hook denial settles the same turn outcome as an approval stop
but queues nothing, and a lookup keyed on session and tool alone could
return an older undecided entry. Snapshot the approval queue sequence
before the turn and answer approval_required only for an entry of this
session and tool queued after it; otherwise keep refusal.

Also: stop-gate refusal test, concurrent cross-session test, stale and
no-queue unit tests, #[non_exhaustive] TurnResult, docs for resumed
turns.
…e session

The snapshot was taken before the busy claim, so an approvals-resume
thread could queue an entry in between and a hook denial of the same tool
would return it. run_session_turn_detailed now takes the mark right after
claim_turn and returns it in TurnResult. The queue sequence number is
also assigned under the entries lock so sequence order is insertion order.
@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

@ajianaz
ajianaz merged commit d5f6b65 into develop Nov 17, 2026
29 of 30 checks passed
@ajianaz
ajianaz deleted the feat/571-approval-required-stop-reason branch November 20, 2026 08:35
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(serve): answer a prompt that stops for an approval with stopReason approval_required and the approval id

2 participants