Skip to content

fix: circuit breaker for c3_delegate CLI backends (v2.39.1)#22

Merged
drknowhow merged 2 commits into
mainfrom
fix/delegate-circuit-breaker
Jun 24, 2026
Merged

fix: circuit breaker for c3_delegate CLI backends (v2.39.1)#22
drknowhow merged 2 commits into
mainfrom
fix/delegate-circuit-breaker

Conversation

@drknowhow

Copy link
Copy Markdown
Owner

Problem

A broken-but-installed delegate backend (gemini/codex/claude) re-spawned a 90–120s subprocess on every c3_delegate call. The handlers in cli/tools/delegate.py returned 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

  • New services/circuit_breaker.py — a small, dependency-free, thread-safe CircuitBreaker: 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 open
    • record_failure() on the if not ok: path, record_success() on success
    • one-shot NotificationStore warning when a backend trips
    • the auto router now skips tripped backends and falls back to ollama

Design notes

  • Thresholds configurable via delegate_config: breaker_failure_threshold (default 3), breaker_cooldown_seconds (default 60).
  • Breaker state is process-global on purpose — backend health (auth, CLI version) is a host fact, not per-project, so a tripped backend should gate all projects on the machine.
  • The Ollama embed/generate breaker is intentionally not wired up yet — there's no telemetry signal of flapping; the primitive is ready if/when that appears.

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.py unaffected.

🤖 Generated with Claude Code

drknowhow and others added 2 commits June 24, 2026 17:07
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>
@drknowhow drknowhow changed the title fix: circuit breaker for c3_delegate CLI backends fix: circuit breaker for c3_delegate CLI backends (v2.39.1) Jun 24, 2026
@drknowhow drknowhow merged commit d19bae7 into main Jun 24, 2026
2 of 11 checks passed
@drknowhow drknowhow deleted the fix/delegate-circuit-breaker branch June 24, 2026 21:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant