Skip to content

[Phase 4] Replace Fixed Self-Correction Cap with Adaptive Failure Handling #71

Description

@frankbria

Summary

The current 3-attempt self-correction cap for failing tests is arbitrary and doesn't distinguish between fixable bugs and architectural issues requiring plan revision.

Problem Analysis

Current behavior: When tests fail, agents retry up to 3 times before... (what? failing the task? escalating?).

This is problematic because:

  1. Not all failures are equal: A typo causing a test failure needs 1 retry. An architectural mismatch needs 0 retries and plan revision instead.

  2. Retrying the same approach rarely works: If the approach is fundamentally wrong, 3 attempts of the same strategy waste tokens and time.

  3. No escalation path: After 3 failures, what happens? If the task just fails, we've learned nothing. If it continues anyway, we ship broken code.

  4. Missing: "stop and think" mode: The Deep Agents pattern emphasizes that agents should recognize when they're stuck and revise their plan, not just retry.

State of the Art Comparison

From Philipp Schmid's Agents 2.0:

"No Recovery mechanism: If it goes down a rabbit hole, it rarely has the foresight to stop, backtrack, and try a new approach."

The fix isn't more retries—it's plan revision when retries aren't working.

12-Factor Agents:

"Embrace Errors: Keep error messages in the context."

Errors should inform strategy changes, not just trigger identical retries.

Proposed Improvement

Replace fixed retry count with adaptive failure handling:

Failure Type Detection → Strategy Selection → Escalation Path

Failure Classification

Failure Type Signal Action
Trivial Single assertion, clear error message Retry (max 2)
Moderate Multiple failures, related to recent change Analyze, then retry with adjustment
Architectural Systemic failures, interface mismatch Create blocker, request plan revision
External Network, API, dependency issues Wait and retry with backoff
Unknown Unclear failure mode Create blocker for human review

Implementation Approach

  1. Failure analyzer: Before retrying, classify the failure type

    • Parse test output for patterns
    • Compare to previous attempt (same error = not fixable by retry)
    • Check if failure is in code agent just modified vs. elsewhere
  2. Strategy selector:

    if failure_type == "trivial" and attempt < 2:
        return RetryStrategy(same_approach=True)
    elif failure_type == "moderate":
        return RetryStrategy(analyze_first=True, max_attempts=2)
    elif failure_type == "architectural":
        return EscalateStrategy(create_blocker=True, reason="architectural_mismatch")
    elif consecutive_same_errors >= 2:
        return EscalateStrategy(create_blocker=True, reason="stuck_in_loop")
  3. Escalation to plan revision:

    • Create async blocker: "Test failures suggest approach X won't work. Options: A, B, C. Human guidance requested."
    • Or: Escalate to Lead Agent for task re-decomposition
  4. Learning from failures:

    • When human resolves blocker, record the pattern
    • Build procedural memory: "When you see error type X in this codebase, approach Y works"

Success Criteria

  • Failure classification implemented
  • "Same error twice = don't retry" logic
  • Architectural failures create blockers instead of retrying
  • Reduced wasted tokens on futile retries
  • Higher task completion rate (failures become blockers that get resolved, not terminal states)

Metrics to Track

  • Retry efficiency: % of retries that succeed vs. fail again with same error
  • Escalation rate: % of failures that become blockers
  • Blocker resolution time: How long until human input resolves architectural issues
  • Token waste: Tokens spent on retries that didn't help

References

Metadata

Metadata

Assignees

Labels

FutureDeferred - beyond v1/v2 scope, consider for future versionsarchitectureSystem architecture and design patternsenhancementNew feature or requestphase-4Phase 4: Multi-Agent Coordinationpriority:highquality

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions