Repository navigation
Description
Summary
The circuit breaker in services.agent_client opens when an agent's health probe times out under heavy CPU load (e.g. during a long-running Claude execution). Once open, the circuit never transitions to a half-open state to test recovery — the failure counter only climbs. The only reset is a backend process restart. As a secondary effect, the open circuit blocks the cleanup service's re-verify step (#378), causing completed executions to remain stuck as "running" in the DB (zombie executions) until the next restart.
Component
Backend / services.agent_client (circuit breaker) + services.cleanup_service (re-verify step)
Priority
P1 — scheduled execution reliability is impaired; workaround is docker restart trinity-backend
Observed Behaviour
- Agent starts a long, CPU-heavy execution (>15 min, agent CPU >120%)
- Health probe to the agent times out repeatedly (5s timeout) → circuit opens
- Failure counter climbs at ~1.5–2 per minute for the duration of the execution
- Execution completes normally (claude exits, drain finishes cleanly) — agent CPU drops back to ~30%
- Circuit does not recover: counter keeps climbing even after execution ends, because there is no half-open test
- Cleanup service runs its re-verify pass; circuit-open causes it to skip the stale execution:
[Cleanup] Skipping {execution_id} for '{agent_name}' — agent unreachable during re-verify (#378); Phase 1 stale cleanup is fallback
- Execution remains stuck as
runningin DB indefinitely (zombie) - Only
docker restart trinity-backendresets the in-memory circuit counter and allows startup recovery to clear the zombie
Reproduction Steps
- Deploy current
devbranch (f7d7593+) - Trigger a long-running, computationally heavy task on an agent (expect agent container CPU >120% for 15+ minutes)
- Monitor
docker logs trinity-backendforCircuit OPENED for agent {name} after N failures - Note that failure count climbs monotonically for the duration of the execution
- Wait for execution to complete (claude process exits, drain finishes)
- Observe circuit failure count continues climbing after completion — no recovery
- Observe cleanup service skipping the now-completed execution with the re-verify warning above
- Query DB: execution status remains
runningdespite no active process
Root Cause
The circuit breaker in services.agent_client appears to implement only two states: closed and open. There is no half-open state. A standard circuit breaker pattern periodically allows a test probe through when open, and closes the circuit on success. Without this:
- Once opened by load-induced probe timeouts, the circuit never self-heals
- Every subsequent probe (from scheduler dispatches, cleanup re-verify, etc.) is short-circuited and logged as another failure, inflating the counter further
- The circuit becomes permanently open until the backend process restarts
Secondary issue: the cleanup service's re-verify step (#378) checks agent reachability via the same circuit-breaker-wrapped path. When the circuit is open, re-verify always fails, so any execution that completed while the circuit was open will remain a zombie.
Suggested Fix
Add a half-open recovery mechanism to the circuit breaker:
# Pseudocode — adapt to actual implementation in services/agent_client.py
class CircuitBreaker:
# After circuit has been open for N seconds, allow one probe through (half-open)
HALF_OPEN_AFTER_SECONDS = 30 # or configurable
def should_allow_request(self, agent_name):
state = self.get_state(agent_name)
if state == CLOSED:
return True
if state == OPEN:
time_open = now() - self.opened_at[agent_name]
if time_open >= self.HALF_OPEN_AFTER_SECONDS:
self.set_state(agent_name, HALF_OPEN)
return True # let one probe through
return False
if state == HALF_OPEN:
return False # already testing, block others
def record_success(self, agent_name):
self.failure_counts[agent_name] = 0
self.set_state(agent_name, CLOSED) # probe succeeded — close circuit
def record_failure(self, agent_name):
# ... existing logic, but also handle HALF_OPEN → OPEN transitionAlternatively, consider a time-based auto-reset: if the circuit has been open for >60s and no new execution is active on that agent, reset to closed.
Also consider: the cleanup service's re-verify step should bypass the circuit breaker (or use a separate direct probe), since re-verify is specifically for cases where normal operation has already failed.
Observed Pattern
Across multiple restarts today, clean window durations before next runaway:
- Restart 1 → runaway onset: ~55 min
- Restart 2 → runaway onset: ~131 min
- Restart 3 → runaway onset: ~94 min
Each runaway was triggered by a heavy execution on the same agent. Each drain completed cleanly (no deadlock — #657 fix confirmed working). The circuit was the sole cause of the zombie in each case.
Environment
- Trinity version:
f7d7593(dev branch) - OS: Ubuntu 22.04
- Docker: 24.x
Related
src/backend/services/agent_client.py— circuit breaker implementationsrc/backend/services/cleanup_service.py— re-verify step (bug: Execution shows Failed then Success after cleanup (race condition) #378)- bug: drain_reader_threads t.join(30) deadlocks asyncio event loop permanently when reader thread is stuck #657 — drain fix (asyncio deadlock) — confirmed resolved separately
- SEC: Redis has no requirepass — agent containers have unauthenticated full read/write access to platform task queue (AISEC-C1) #589 — Redis auth / network isolation (same deploy that introduced the recurring pattern)
Activity
vybe commentedon May 6, 2026
/claim
github-actions commentedon May 6, 2026
Claimed by @vybe. Label
status-in-progressadded.Next steps:
feature/687-<short-slug>Fixes #687fix(agent-client): circuit breaker cooldown clock no longer resets on…
vybe commentedon Jun 1, 2026
Shipped in v0.6.0 (released via #998).
Release: v0.6.1 (#1171)