Missing validation for failure_threshold parameter in CircuitBreaker.__init__ #10565

Open
opened 2026-04-18 17:18:53 +00:00 by HAL9000 · 0 comments
Owner

Metadata

Commit Message: Add validation for failure_threshold parameter in CircuitBreaker.init

Branch Name: fix/circuit-breaker-failure-threshold-validation

Background and Context

The CircuitBreaker class in src/cleveragents/core/circuit_breaker.py does not validate the failure_threshold parameter in its __init__ method. This parameter is critical to the circuit breaker's core functionality, as it determines when the circuit breaker should transition to the OPEN state.

Currently, the __init__ method validates half_open_max_successes with a check for positive values (lines 87-90), but applies no validation to failure_threshold. This inconsistency allows invalid threshold values to be set, which breaks the circuit breaker's functionality.

Code Evidence:

  • File: src/cleveragents/core/circuit_breaker.py
  • Lines 87-90: The __init__ method validates half_open_max_successes with if half_open_max_successes < 1: raise ValueError(...) but does not validate failure_threshold
  • Line 400: The failure_threshold is used in comparison (if self.failure_count >= self.failure_threshold) without prior validation that it's a positive integer

Expected Behavior

The CircuitBreaker.init method should validate that failure_threshold is a positive integer (>= 1) and raise a ValueError with a descriptive message if it is not. This validation should be consistent with the existing validation for half_open_max_successes.

Actual Behavior

No validation is performed on the failure_threshold parameter. If a user sets failure_threshold to 0 or a negative value:

  • With failure_threshold=0: The condition self.failure_count >= self.failure_threshold will always be true, causing the circuit breaker to immediately open on the first failure
  • With failure_threshold<0: The circuit breaker will never open because the condition will never be true

This breaks the circuit breaker's core functionality and violates the expected behavior of the pattern.

Acceptance Criteria

  • The CircuitBreaker.__init__ method validates that failure_threshold >= 1
  • A ValueError is raised with a clear message if failure_threshold < 1
  • The validation is consistent with the existing half_open_max_successes validation pattern
  • Unit tests verify that invalid failure_threshold values raise ValueError
  • Unit tests verify that valid failure_threshold values (>= 1) are accepted
  • Documentation is updated to clarify the failure_threshold parameter requirements

Subtasks

  • Add validation logic to CircuitBreaker.__init__ for failure_threshold
  • Write unit tests for invalid failure_threshold values (0, -1, etc.)
  • Write unit tests for valid failure_threshold values
  • Update docstring to document the validation requirement
  • Run full test suite to ensure no regressions

Definition of Done

This issue is complete when:

  1. The failure_threshold parameter is validated in CircuitBreaker.__init__
  2. A ValueError is raised for invalid values with a descriptive message
  3. All new unit tests pass
  4. All existing tests continue to pass
  5. Code coverage remains >= 97%
  6. The change is documented in the docstring

Automated by CleverAgents Bot
Agent: new-issue-creator

## Metadata **Commit Message:** Add validation for failure_threshold parameter in CircuitBreaker.__init__ **Branch Name:** fix/circuit-breaker-failure-threshold-validation ## Background and Context The CircuitBreaker class in `src/cleveragents/core/circuit_breaker.py` does not validate the `failure_threshold` parameter in its `__init__` method. This parameter is critical to the circuit breaker's core functionality, as it determines when the circuit breaker should transition to the OPEN state. Currently, the `__init__` method validates `half_open_max_successes` with a check for positive values (lines 87-90), but applies no validation to `failure_threshold`. This inconsistency allows invalid threshold values to be set, which breaks the circuit breaker's functionality. **Code Evidence:** - File: `src/cleveragents/core/circuit_breaker.py` - Lines 87-90: The `__init__` method validates `half_open_max_successes` with `if half_open_max_successes < 1: raise ValueError(...)` but does not validate `failure_threshold` - Line 400: The `failure_threshold` is used in comparison (`if self.failure_count >= self.failure_threshold`) without prior validation that it's a positive integer ## Expected Behavior The CircuitBreaker.__init__ method should validate that `failure_threshold` is a positive integer (>= 1) and raise a `ValueError` with a descriptive message if it is not. This validation should be consistent with the existing validation for `half_open_max_successes`. ## Actual Behavior No validation is performed on the `failure_threshold` parameter. If a user sets `failure_threshold` to 0 or a negative value: - With `failure_threshold=0`: The condition `self.failure_count >= self.failure_threshold` will always be true, causing the circuit breaker to immediately open on the first failure - With `failure_threshold<0`: The circuit breaker will never open because the condition will never be true This breaks the circuit breaker's core functionality and violates the expected behavior of the pattern. ## Acceptance Criteria - [ ] The `CircuitBreaker.__init__` method validates that `failure_threshold >= 1` - [ ] A `ValueError` is raised with a clear message if `failure_threshold < 1` - [ ] The validation is consistent with the existing `half_open_max_successes` validation pattern - [ ] Unit tests verify that invalid `failure_threshold` values raise `ValueError` - [ ] Unit tests verify that valid `failure_threshold` values (>= 1) are accepted - [ ] Documentation is updated to clarify the `failure_threshold` parameter requirements ## Subtasks - [ ] Add validation logic to `CircuitBreaker.__init__` for `failure_threshold` - [ ] Write unit tests for invalid `failure_threshold` values (0, -1, etc.) - [ ] Write unit tests for valid `failure_threshold` values - [ ] Update docstring to document the validation requirement - [ ] Run full test suite to ensure no regressions ## Definition of Done This issue is complete when: 1. The `failure_threshold` parameter is validated in `CircuitBreaker.__init__` 2. A `ValueError` is raised for invalid values with a descriptive message 3. All new unit tests pass 4. All existing tests continue to pass 5. Code coverage remains >= 97% 6. The change is documented in the docstring --- **Automated by CleverAgents Bot** Agent: new-issue-creator
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cleveragents/cleveragents-core#10565
No description provided.