Feature: Circuit Breaker Coverage
  Scenarios that exercise previously uncovered code paths in
  the circuit_breaker module, focusing on edge-case exception
  handling, stale-generation guards, and async equivalents.

  Background:
    Given the circuit breaker coverage module is imported

  # --- Sync path: inner CircuitBreakerOpen during HALF_OPEN (lines 160-169) ---

  Scenario: Sync call restores half-open permit when inner service raises CircuitBreakerOpen
    Given a coverage circuit breaker with failure threshold 2 and fast recovery
    And the coverage circuit breaker is transitioned to half-open state
    When I sync-call a function that raises inner CircuitBreakerOpen
    Then the caught exception should be a CircuitBreakerOpen
    And the coverage half-open permit count should be restored

  # --- Sync path: BaseException during HALF_OPEN (lines 191-195) ---

  Scenario: Sync call restores half-open permit when function raises a BaseException subclass
    Given a coverage circuit breaker with failure threshold 2 and fast recovery
    And the coverage circuit breaker is transitioned to half-open state
    When I sync-call a function that raises a KeyboardInterrupt
    Then the caught exception should be a KeyboardInterrupt
    And the coverage half-open permit count should be restored

  # --- Async path: inner CircuitBreakerOpen during HALF_OPEN (lines 286-295) ---

  Scenario: Async call restores half-open permit when inner service raises CircuitBreakerOpen
    Given a coverage circuit breaker with failure threshold 2 and fast recovery
    And the coverage circuit breaker is transitioned to half-open state
    When I async-call a function that raises inner CircuitBreakerOpen
    Then the caught exception should be a CircuitBreakerOpen
    And the coverage half-open permit count should be restored

  # --- Async path: BaseException during HALF_OPEN (covers async BaseException handler) ---

  Scenario: Async call restores half-open permit when function raises a BaseException subclass
    Given a coverage circuit breaker with failure threshold 2 and fast recovery
    And the coverage circuit breaker is transitioned to half-open state
    When I async-call a function that raises a KeyboardInterrupt
    Then the caught exception should be a KeyboardInterrupt
    And the coverage half-open permit count should be restored

  # --- Stale generation discarded in _on_success (line 360) ---

  Scenario: Stale half-open generation is discarded on success
    Given a coverage circuit breaker in half-open state with generation 3
    When _on_success is called with stale generation 1
    Then the _on_success result should be False
    And the coverage circuit breaker should still be half-open

  # --- _on_failure when already OPEN (line 384) ---

  Scenario: _on_failure is a no-op when circuit is already open
    Given a coverage circuit breaker that is already in the open state
    When _on_failure is called on the already-open breaker
    Then the _on_failure result should be False
    And the coverage circuit breaker state should still be open
    And the failure count should not have changed
