Skip to content

feat: add intra-loop stagnation detector to execution loops #415

@Aureliolo

Description

@Aureliolo

Summary

Add stagnation detection to execution loops (ReactLoop, PlanExecuteLoop) that identifies when an agent is repeating the same tool calls without making progress, and intervenes.

Motivation

From MADQA paper (arXiv:2603.12180, Snowflake/Oxford): agents persist in unproductive loops, achieving ~20% below oracle accuracy via brute-force exhaustive search rather than strategic reasoning. Current execution loops only have max_turns ceiling and budget_checker as cost gate — no mechanism to detect repetitive patterns.

Design

  • Analyze TurnRecord.tool_calls_made history across recent N turns
  • Detect repetitive patterns: same tool + same args repeated, same documents accessed, no new information gained
  • On detection, two responses:
    1. Corrective prompt injection: "You appear to be repeating the same actions without progress — try a different approach"
    2. Early termination: Terminate with STAGNATION termination reason (new enum value)
  • Configurable sensitivity: window size (N turns), similarity threshold, max repeated patterns

Affected Files

  • src/ai_company/engine/react_loop.py
  • src/ai_company/engine/plan_execute_loop.py
  • src/ai_company/engine/loop_protocol.py (add STAGNATION to termination reasons)
  • src/ai_company/engine/run_result.py

Research

Metadata

Metadata

Assignees

No one assigned

    Labels

    prio:mediumShould do, but not blockingscope:smallLess than 1 day of workspec:task-workflowDESIGN_SPEC Section 6 - Task & Workflow Enginetype:featureNew feature implementation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions