Skip to content

fix(agent): detect non-consecutive tool call loops - #83

Closed
HamzaKhanBUIC wants to merge 1 commit into
getdownandcode:mainfrom
HamzaKhanBUIC:fix/duplicate-tool-call-detector
Closed

HamzaKhanBUIC wants to merge 1 commit into
getdownandcode:mainfrom
HamzaKhanBUIC:fix/duplicate-tool-call-detector

Conversation

@HamzaKhanBUIC

Copy link
Copy Markdown

Summary

Fixes #72.

Enhances the duplicate tool call detector in Agent.run() to detect non-consecutive repeating cycles (e.g., A, B, A, B, A, B or A, B, C, A, B, C) and halt execution before consuming max_steps.

Problem

The detector only tracked the single immediately preceding tool signature (last_tool_signature). If an LLM alternated between two or more calls (e.g. calc(1), calc(2), calc(1), calc(2)), the consecutive counter was reset on every call, allowing the agent to spin endlessly until hitting max_steps.

Solution

  • In �gentkit/core/agent.py:
    • Maintained full tool signature history ool_call_signatures during the run.
    • Added cycle detection inspecting suffixes of lengths 1 to max_tool_cycle_length (default 5).
    • Detected repeating cycles of length L repeated hreshold times (max_consecutive_duplicate_tool_calls for L=1, max_duplicate_tool_call_cycles with fallback to max_consecutive_duplicate_tool_calls for L>1).
    • Emitted informative failure reasons identifying the cycle structure and repetition count.
  • In �gentkit/core/errors.py:
    • Extended DuplicateToolCallLoopError with cycle_length attribute while maintaining backwards compatibility with 2-argument constructors.
  • In ests/unit/test_agent_loop_detector.py:
    • Added regression test for alternating 2-tool repeating loop.
    • Added regression test for 3-tool multi-call cycle.
    • Added regression test verifying
      aise_on_failure=True raises DuplicateToolCallLoopError with cycle_length.
    • Verified existing consecutive duplicate tests and legitimate alternation tests continue to pass.

Verification

  • uv run pytest tests/unit/test_agent_loop_detector.py -> 9/9 passed
  • uv run ruff check . -> All checks passed
  • uv run ruff format --check . -> 110 files already formatted
  • uv run mypy agentkit tests/unit/test_agent_loop_detector.py -> Success: no issues found in 43 source files
  • Structural AST lint & Semgrep security check -> Clean

Copilot AI balanced review requested due to automatic review settings October 20, 2026 22:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

AUDIT-15 — Medium — Duplicate-tool-call detector only catches consecutive repeats

2 participants