Your AI Agent's Kill Switch Returns a Boolean. That's the Bug (Python Circuit Breaker)
说明与用途
AI 代理循环因布尔锁定检测器被误用而持续运行数天。引入 CircuitBreaker 在达到限制时退出,防止了资源耗尽。
问题
A loop in an AI agent kept retrying a locked account, causing the bot to run for days and consume resources.
结果
The circuit breaker prevented the agent from running indefinitely, saving resources and avoiding account lockout.
工作流
- Define a check_for_lockout function that returns True when account is locked.
- Wrap the main loop to call check_for_lockout and continue if True.
- Observe that the loop keeps running after lockout detection.
- Implement a CircuitBreaker class with max_per_minute, max_total, max_runtime_s limits and a sentinel file.
- Modify the loop to use CircuitBreaker.acquire before each action.
- When limits are exceeded, CircuitBreaker.trips, writes a sentinel file, logs, and exits the process.
- Verify that the agent stops immediately after lockout is detected and limits are hit.
证据
My bot kept poking a locked platform for another three and a half days.
The detector worked. On 2026-07-21 the log line appeared, Telegram buzzed, and the selector matched exactly what it was supposed to match.
A detector that returns True hands the decision to whoever called it.
The same fix covers the bigger failure class.
- 记录日期: 2026-10-10T19:40:56.117Z