@slow
Feature: AutoDebug Integration with PlanService
  As a developer
  I want the system to automatically debug build failures
  So that I can fix errors without manual intervention

  Background:
    Given I have a clean test environment
    And I have an initialized project
    And I have a current plan with instruction "Add error handling"
    And I have added files to context

  Scenario: AutoDebug fixes syntax error in generated plan
    Given the AI provider will generate code with a syntax error
    And the AutoDebugAgent can fix the syntax error
    When I run auto_debug_build with max attempts 3
    Then the build should succeed after debugging
    And debug attempts should be saved to database
    And the final attempt should be marked as successful

  Scenario: AutoDebug fixes import error
    Given the AI provider will generate code with an import error
    And the AutoDebugAgent can fix the import error
    When I run auto_debug_build with max attempts 3
    Then the build should succeed after debugging
    And debug attempts should be saved to database
    And at least 1 debug attempt should exist

  Scenario: AutoDebug gives up after 3 attempts
    Given the AI provider will generate code with an unfixable error
    And the AutoDebugAgent cannot fix the error
    When I run auto_debug_build with max attempts 3
    Then the build should fail after 3 attempts
    And debug attempts should be saved to database
    And all 3 debug attempts should be marked as unsuccessful

  Scenario: Debug attempts are saved to database
    Given the AI provider will generate code with a validation error
    When I run auto_debug_build with max attempts 2
    Then debug attempts should be saved to database
    And each attempt should have an error message
    And each attempt should have an attempt number
    And each attempt should have a timestamp

  Scenario: AutoDebug retries when actor stub fails once
    Given actor overrides are stubbed with default "openai" and alternates ""
    And actor stub "openai" will fail 1 time(s) before succeeding
    When I run auto_debug_build with max attempts 2
    Then the build should succeed after debugging
    And actor stub "openai" should record 2 generate call(s)
