Skip to content

feat(agent): add response retry hooks#2182

Merged
gold-silver-copper merged 5 commits into
mainfrom
agent/add-response-retry-hook
Jul 18, 2026
Merged

feat(agent): add response retry hooks#2182
gold-silver-copper merged 5 commits into
mainfrom
agent/add-response-retry-hook

Conversation

@gold-silver-copper

@gold-silver-copper gold-silver-copper commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Description

Add a typed response-retry action to the canonical on_model_turn_finished hook without adding retry configuration or counters to agent builders, agents, runners, or run state.

The new ModelTurnAction lets hooks accept, retry, or stop a completed model turn. Retry requests explicitly distinguish between:

  • Repeat, which discards the rejected assistant response and reuses the same prompt and preceding history with fresh request preparation. Completion-call hooks, retrieval, and dynamic tool resolution run again, so the resulting provider request may differ from the rejected attempt.
  • Feedback, which preserves a non-empty rejected assistant response and appends corrective user feedback before the next model call. Canonical empty assistant turns are omitted from history.

Both modes retain usage and completion-call accounting and consume the existing max_turns model-call budget. Policy-specific limits remain hook-owned and can use the run-scoped Scratchpad; the public documentation and runnable agent_with_retry_hook example demonstrate a bounded, instance-isolated policy.

Blocking and streaming execution share the same action resolver and stateless AgentRun transition. Streaming retries suppress the rejected provider final and emit ModelTurnRetried so consumers can reset provisional deltas. Tool-bearing response retries fail before tool execution to avoid constructing provider-visible history with unanswered calls.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

AgentHook::on_model_turn_finished now returns ModelTurnAction instead of ObservationAction.

Testing

  • cargo fmt
  • cargo clippy --all-targets --all-features -- -D warnings
  • cargo test
  • cargo doc --workspace --no-deps
  • Recorded and replayed the OpenAI response-retry cassette
  • OpenAI cassette registration and secret-safety checks

Regression coverage includes blocking and streaming Repeat/Feedback behavior, fresh request preparation on Repeat, canonical empty-turn history handling, total-budget exhaustion, usage and completion-call accounting, hook ordering and nested stacks, run/concurrency isolation, streaming rollback signaling, fail-safe tool-bearing retries, and a live-recorded OpenAI feedback-retry history.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove the feature works
  • New and existing unit tests pass locally with my changes

Notes

Response retries intentionally do not support turns containing tool calls. Those turns must be steered with tool-call hooks so Rig never persists unanswered tool calls as ordinary conversation history.

gold-silver-copper and others added 5 commits July 17, 2026 22:46
Co-authored-by: Erik Tews <erik@datenzone.de>
Co-authored-by: Erik Tews <erik@datenzone.de>
Co-authored-by: Erik Tews <erik@datenzone.de>
Co-authored-by: Erik Tews <erik@datenzone.de>
Co-authored-by: Erik Tews <erik@datenzone.de>
@gold-silver-copper
gold-silver-copper force-pushed the agent/add-response-retry-hook branch from 1e6bb42 to f5737b3 Compare July 18, 2026 05:49
@gold-silver-copper
gold-silver-copper marked this pull request as ready for review July 18, 2026 06:15
@gold-silver-copper
gold-silver-copper added this pull request to the merge queue Jul 18, 2026
Merged via the queue into main with commit 87f3f5b Jul 18, 2026
6 checks passed
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.

1 participant