Repository navigation
Description
Is your feature request related to a problem? Please describe.
A ReAct agent can repeatedly call the same tool with the same arguments, receiving the same result or error without making progress. For example, it may keep retrying an unchanged failing command or re-reading the same file. This consumes tokens and tool execution time until the iteration budget is exhausted.
The current maxIters limit bounds total iterations, but does not distinguish productive work from repeated actions. The CodingAgent example's model-call budgets have the same limitation. Jev supervision can assess whether a worker is stuck, but is an observational, model-driven mechanism rather than a lightweight deterministic guard.
Describe the solution you'd like
Add an opt-in RepetitionGuardMiddleware using the existing MiddlewareBase extension points, without changing default agent behavior.
Proposed implementation:
- Place the reusable middleware in
agentscope-core, available to standalone ReActAgent and Harness applications through.middleware(...). - Initialize bounded, invocation-local tracking in
onAgent, carried through the per-call runtime context rather than shared mutable middleware fields. Clean up on completion, error, and cancellation; concurrent calls and subsequent turns must remain isolated. - In
onActing, fingerprint each tool call using its name and canonicalized arguments. Object key ordering should not affect equality; array ordering and argument values should remain significant. Ignore tool-call IDs. - Initially detect consecutive identical calls, with configurable warning/stop thresholds and tool exclusions or per-tool overrides for legitimate polling/retry workflows. Repetition alone does not prove lack of progress, so use conservative configuration and explicit opt-in.
- At the warning threshold, provide a bounded model-visible reminder asking the agent to change strategy. If repetition continues to the stop threshold, prevent the offending execution and terminate with a clear, distinguishable reason. The exact reminder and termination API can follow maintainers' preferred lifecycle conventions.
- Preserve existing permissions, tool-result bookkeeping, reactive cancellation, and consistent terminal behavior between
call()andstreamEvents(). Externally executed tools are outside the initial scope becauseonActingonly covers runtime-managed execution.
Keep the first PR small: multi-tool cycles (A → B → A → B), semantic progress assessment, persistence across turns, and automatic retries would be out of scope.
Verification
Use local fake tools/models to verify:
- Warning and stop thresholds; a blocked tool call is never executed.
- Equivalent argument objects match despite key ordering; changed arguments or an intervening different call reset the consecutive count.
- Tool exclusions and deterministic counting for batched calls.
- Isolation across concurrent invocations and subsequent turns.
- Cleanup on completion, errors, and cancellation.
- Consistent termination results for
call()andstreamEvents().
Add matching English/Chinese middleware documentation and a minimal opt-in example.
Describe alternatives you've considered
- Lower
maxIters: also truncates productive work and cannot explain repetition. - Prompt-only instructions: helpful guidance, but cannot reliably prevent repeated execution.
- Application-specific guards: duplicate fingerprinting, isolation, and lifecycle handling.
- Model-driven supervision: useful for broader progress assessment, but adds inference cost and serves a different purpose.
Additional context
Related to the repeated-action/non-progress protection discussed in #2142. This proposal is intentionally narrower: an invocation-scoped middleware independent of durable goal execution.
If the maintainers think this approach makes sense, please feel free to assign this issue to me. I would be happy to submit a PR.
Activity