fix: circuit breaker for c3_delegate CLI backends (v2.39.1)#22
Merged
Conversation
A broken-but-installed delegate backend (gemini/codex/claude) re-spawned a 90-120s subprocess on every call: the handlers returned the error on runtime failure (if not ok:) but never demoted the backend. Add a reusable thread-safe CircuitBreaker (services/circuit_breaker.py) and wire it into all three CLI handlers: gate before spawn, record_failure() opens after N consecutive fails (default 3 / 60s cooldown, configurable via delegate_config breaker_failure_threshold/breaker_cooldown_seconds), record_success() resets, half-open probe after cooldown. Trips emit a NotificationStore warning; the auto router skips tripped backends and falls back to ollama. Breaker state is process-global on purpose: backend health (auth, CLI version) is a host fact, not per-project. Tests: tests/test_circuit_breaker.py (5 pass). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
A broken-but-installed delegate backend (
gemini/codex/claude) re-spawned a 90–120s subprocess on everyc3_delegatecall. The handlers incli/tools/delegate.pyreturned the error on runtime failure (if not ok:) but never demoted the backend, so an installed-but-broken CLI (expired auth, model pulled, repeated timeouts) kept paying the full subprocess + timeout cost on every subsequent call.Surfaced by a multi-agent evaluation of techniques worth borrowing from Headroom: this was the one "operational hardening" idea that transfers (the cache-centric ideas don't, since C3 doesn't own the Anthropic prefix cache).
Change
services/circuit_breaker.py— a small, dependency-free, thread-safeCircuitBreaker: closed → open after N consecutive failures → half-open single probe after a cooldown → close on success.cli/tools/delegate.py— per-backend breaker wired into all three CLI handlers:allow()-gate before spawning → returns[delegate:degraded]instead of re-spawning when openrecord_failure()on theif not ok:path,record_success()on successNotificationStorewarning when a backend tripsautorouter now skips tripped backends and falls back to ollamaDesign notes
delegate_config:breaker_failure_threshold(default 3),breaker_cooldown_seconds(default 60).Tests
tests/test_circuit_breaker.py— 4 unit tests for the primitive + 1 integration test proving a broken gemini backend stops re-spawning after the breaker trips and emits exactly one notification. All 5 pass;test_cli_smoke.pyunaffected.🤖 Generated with Claude Code