DOC: phronis-ki
STATUS: ● PUBLISHED
SYSTEM PHRONIS

The Kill Switch: ~600ms to Halt, Persisted in PostgreSQL

From alert to enforced stop, with state that survives a restart.

Cover image — The Kill Switch: ~600ms to Halt, Persisted in PostgreSQL

Detection is useless without enforcement. The kill switch is the half of Phronis that turns “this agent is misbehaving” into “this agent cannot make another call.” From first bad call to enforced stop is about 600ms, and the stop survives a container restart.

// 01 — THE EXECUTOR

AlertExecutor is a background thread inside your process consuming the phronis.alerts topic. When an alert arrives, it calls registry.trip(agent_id, reason), flipping the agent to halted in an in-memory CircuitBreakerRegistry. One important config detail: enable.auto.commit = False, with manual commits after each alert, so an unclean restart can’t lose or replay offsets (that distinction caused a real bug, linked below).

// 02 — ENFORCEMENT INSIDE THE LOOP

The @tool decorator checks registry.is_halted(agent_id) at the start of every tool call. If the agent is tripped, it raises CircuitBreakerTripped immediately, before any external API call happens:

class CircuitBreakerTripped(Exception):
    # carries .agent_id and .reason

The enforcement point is inside the agent’s own call path, which is why the stop is immediate rather than advisory.

// 03 — DURABLE STATE

A circuit breaker that forgets on restart is a liability. The Kill Switch API (FastAPI) mirrors every trip and reset to PostgreSQL. On startup it runs init_db() and load_all(), restoring any previously-halted agents back into the registry, so a crashed or redeployed API comes back still enforcing the breakers that were open. If PostgreSQL is unreachable, it degrades to pure in-memory rather than failing. Mutating endpoints (/halt, /reset) are guarded by an X-API-Key; read-only ones stay open.

TAKEAWAYS

NEXT

@frogwebp brand mark
ANTHONY PENA · @FROGWEBP
I build data systems and write about everything around them, the architecture, the failures, what each one teaches me. Documenting in public since 2021: the process, not just the result.

// NEWSLETTER — THE BUILD LOG SIGNAL

When I ship something or learn something worth keeping, it lands here first — build logs, concepts, and the honest process behind them. Come along; no spam, leave anytime.