agent: guard against repeated identical tool calls and runaway turns #157

Closed
opened 2026-10-15 20:18:05 +00:00 by hermes · 0 comments
Collaborator

Problem

max_turns defaults to 200 (crates/hx-core/src/agent.rs) and nothing detects a loop. A confused model can repeat the same failing call while context grows. Code reading only; no transcript shows this happening yet.

Change

In the tool loop, track (tool, args hash, result hash). On the 3rd identical consecutive repeat, append to the tool result: "This call already returned the same result twice; change approach or ask the user." Optionally add a soft per-run token budget that emits an Event::Info warning (configurable, off by default).

Exempt bash calls that are plausibly polling (a command that includes a sleep, or one with an explicit timeout).

Effect

Helps only in the tail cases. Rate unknown; check real sessions for repeats first (count identical consecutive calls in the session file).

Risk

False positives on legitimate retries (e.g. rerunning a flaky test).

Verify

Unit test with a fake provider that issues the same call 4 times; assert the hint appears on the 3rd result and not before.

## Problem `max_turns` defaults to 200 (`crates/hx-core/src/agent.rs`) and nothing detects a loop. A confused model can repeat the same failing call while context grows. Code reading only; no transcript shows this happening yet. ## Change In the tool loop, track `(tool, args hash, result hash)`. On the 3rd identical consecutive repeat, append to the tool result: "This call already returned the same result twice; change approach or ask the user." Optionally add a soft per-run token budget that emits an `Event::Info` warning (configurable, off by default). Exempt `bash` calls that are plausibly polling (a command that includes a sleep, or one with an explicit timeout). ## Effect Helps only in the tail cases. Rate unknown; check real sessions for repeats first (count identical consecutive calls in the session file). ## Risk False positives on legitimate retries (e.g. rerunning a flaky test). ## Verify Unit test with a fake provider that issues the same call 4 times; assert the hint appears on the 3rd result and not before.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: johannes/hx#157