Skip to content

Fix max-steps warning timing to appear after final step completes#249

Merged
itomek merged 2 commits intomainfrom
fix/max-steps-warning-timing
Jan 23, 2026
Merged

Fix max-steps warning timing to appear after final step completes#249
itomek merged 2 commits intomainfrom
fix/max-steps-warning-timing

Conversation

@itomek
Copy link
Collaborator

@itomek itomek commented Jan 23, 2026

Fixes #221

Problem

The max-steps warning appeared BEFORE the final step's work was executed, creating confusing output:

Before this fix (with max_steps=5):

Step 4: [work output]
⚠️ Reached maximum steps limit (5 steps)  ← Warning after Step 4
Step 5: [work output]                       ← Step 5 runs after warning

Root Cause

The while loop checked if steps_taken == steps_limit immediately after incrementing the counter (line 1173), but before the step's actual work was performed. This caused the warning to appear prematurely.

while steps_taken < steps_limit and final_answer is None:
    steps_taken += 1              # Increment to 5
    if steps_taken == steps_limit:  # Check passes → WARNING SHOWN
        show_warning()            # ← Warning BEFORE step 5 work
    
    print_step_header()           # Display "Step 5"
    [... Step 5 work ...]         # Do the actual work

Solution

Move the max-steps check to the end of the while loop, after:

  1. The step's work completes
  2. The final answer check (which exits early if answer is found)

This ensures Step 5 executes completely before any warning appears.

while steps_taken < steps_limit and final_answer is None:
    steps_taken += 1
    print_step_header()
    [... Step 5 work ...]         # Do the work FIRST
    
    if "answer" in parsed:        # Check for answer
        final_answer = parsed["answer"]
        break                     # Exit cleanly if found
    
    if steps_taken == steps_limit:  # Check AFTER work
        show_warning()            # Warning only if no answer

After This Fix

Users now see the correct behavior:

Step 4: [work output]
Step 5: [work output]                       ← Step 5 completes first
⚠️ Reached maximum steps limit (5 steps)  ← Warning AFTER Step 5
Continue with 50 more steps? (y/n):

If a final answer is provided during Step 5, the agent exits cleanly without showing the warning at all.

Testing

  • ✅ All unit tests pass (27 tests in tests/unit/)
  • ✅ Verified control flow with demonstration script
  • ✅ Default max_steps=5 preserved (conservative base class default)

Files Changed

  • src/gaia/agents/base/agent.py: Moved max-steps check from line 1177 to line 1975

Fixes #221 - Off-by-one bug where max-steps warning appeared BEFORE
the final step's work was executed.

## Problem
With max_steps=5, the warning appeared after Step 4 output and before
Step 5 began executing. This created confusion as users saw:
- Step 4 output
- Warning: "Reached maximum steps limit (5 steps)"
- Step 5 output

## Root Cause
The max-steps check occurred immediately after incrementing steps_taken
(line 1173) and before the step's actual work was performed (lines 1182+).

## Solution
Move the max-steps check to the END of the while loop, after:
1. The step's work completes
2. Final answer check (lines 1968-1972)

This ensures Step 5 executes completely before the warning appears.

## After Fix
Users now see:
- Step 4 output
- Step 5 output
- Warning: "Reached maximum steps limit (5 steps)"

The warning only appears if no final answer was provided during Step 5.
If a final answer is found, the agent exits cleanly without showing
the warning.

## Testing
- All unit tests pass (27 tests in tests/unit/)
- Verified control flow with demonstration script
- Default max_steps remains at 5 (conservative)
@itomek itomek self-assigned this Jan 23, 2026
@itomek itomek enabled auto-merge January 23, 2026 20:04
@github-actions github-actions bot added the agents Agent system changes label Jan 23, 2026
@itomek itomek added this pull request to the merge queue Jan 23, 2026
Merged via the queue into main with commit 4ba2bc5 Jan 23, 2026
61 checks passed
@itomek itomek deleted the fix/max-steps-warning-timing branch January 23, 2026 22:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent system changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Maximum steps limit appears early

2 participants