-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Phase 1 Quick Wins — 7 stability & UX improvements #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2c45980
feat: Phase 1 quick wins — atomic writes, smart scroll, expand descri…
OBenner 573a23b
feat: Core resilience, smart PR polling & advanced features (Phase 2-4)
OBenner 5eca594
fix: address PR review comments — atomic writes, API guards, scroll f…
OBenner 00d2a95
fix: resolve SonarCloud issues — ReDoS, redundant regex, float equality
OBenner 20fdda4
fix: address CodeRabbit review — input size guard, body truncation, c…
OBenner e7f4b7d
fix: address remaining CodeRabbit review — billing patterns, session-…
OBenner 73f58f8
fix: add isolated session pre-checks, guard empty worktree name
OBenner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| """ | ||
| Circuit Breaker | ||
| =============== | ||
|
|
||
| Implements the circuit breaker pattern to prevent repeated calls to a | ||
| failing service. After *failure_threshold* consecutive failures the | ||
| circuit opens and all subsequent calls are rejected until | ||
| *recovery_timeout* seconds have elapsed, at which point it enters a | ||
| half-open state allowing one probe call. | ||
| """ | ||
|
|
||
| import logging | ||
| import time | ||
| from enum import Enum | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class CircuitState(Enum): | ||
| """Circuit breaker states.""" | ||
|
|
||
| CLOSED = "closed" | ||
| OPEN = "open" | ||
| HALF_OPEN = "half_open" | ||
|
|
||
|
|
||
| class CircuitBreaker: | ||
| """Simple circuit breaker for API calls.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| name: str, | ||
| failure_threshold: int = 3, | ||
| recovery_timeout: float = 60.0, | ||
| ) -> None: | ||
| self.name = name | ||
| self._failure_threshold = failure_threshold | ||
| self._recovery_timeout = recovery_timeout | ||
|
|
||
| self._failure_count: int = 0 | ||
| self._last_failure_time: float = 0.0 | ||
| self._state = CircuitState.CLOSED | ||
|
|
||
| @property | ||
| def state(self) -> CircuitState: | ||
| """Return the current effective state (may transition from OPEN → HALF_OPEN).""" | ||
| if self._state == CircuitState.OPEN: | ||
| elapsed = time.monotonic() - self._last_failure_time | ||
| if elapsed >= self._recovery_timeout: | ||
| self._state = CircuitState.HALF_OPEN | ||
| logger.info( | ||
| "Circuit breaker '%s' transitioned to HALF_OPEN after %.1fs", | ||
| self.name, | ||
| elapsed, | ||
| ) | ||
| return self._state | ||
|
|
||
| def can_execute(self) -> bool: | ||
| """Return True if the circuit allows a call to proceed.""" | ||
| current = self.state | ||
| if current == CircuitState.CLOSED: | ||
| return True | ||
| if current == CircuitState.HALF_OPEN: | ||
| return True # Allow one probe call | ||
| return False # OPEN — reject | ||
|
|
||
| def record_success(self) -> None: | ||
| """Record a successful call — resets the breaker to CLOSED.""" | ||
| if self._state != CircuitState.CLOSED: | ||
| logger.info( | ||
| "Circuit breaker '%s' recovered → CLOSED", | ||
| self.name, | ||
| ) | ||
| self._failure_count = 0 | ||
| self._state = CircuitState.CLOSED | ||
|
|
||
| def record_failure(self, error: Exception | None = None) -> None: | ||
| """Record a failed call — may trip the breaker to OPEN.""" | ||
| self._failure_count += 1 | ||
| self._last_failure_time = time.monotonic() | ||
|
|
||
| if self._state == CircuitState.HALF_OPEN: | ||
| # Probe failed — reopen | ||
| self._state = CircuitState.OPEN | ||
| logger.warning( | ||
| "Circuit breaker '%s' probe failed → OPEN (error: %s)", | ||
| self.name, | ||
| error, | ||
| ) | ||
| elif self._failure_count >= self._failure_threshold: | ||
| self._state = CircuitState.OPEN | ||
| logger.warning( | ||
| "Circuit breaker '%s' tripped → OPEN after %d failures (error: %s)", | ||
| self.name, | ||
| self._failure_count, | ||
| error, | ||
| ) | ||
|
|
||
| def reset(self) -> None: | ||
| """Manually reset the breaker to CLOSED.""" | ||
| self._failure_count = 0 | ||
| self._state = CircuitState.CLOSED | ||
|
|
||
| def __repr__(self) -> str: | ||
| return ( | ||
| f"CircuitBreaker(name={self.name!r}, state={self.state.value}, " | ||
| f"failures={self._failure_count}/{self._failure_threshold})" | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.