Expand description
Per-agent and per-model cooldown tracking.
Shared by engine::runner::response (recording failures) and
engine::router::config (avoiding cooled models during pool selection).
§Persistence
All cooldowns are persisted to the KV store in SQLite so they survive
service restarts. Call init_cooldown_store once at startup (after
the TaskStore is open) to register the store and pre-load unexpired
cooldowns.
When a rate limit includes a “try again at {date}” message, the cooldown is set to that specific timestamp instead of the default duration. Relative usage windows (e.g. “5-hour usage limit”) are also detected and used to set a model-specific cooldown matching the advertised window.
Structs§
Enums§
- Credit
Exhaustion Reason - Credit exhaustion reason detected from error message.
Constants§
- AGENT_
COOLDOWN_ SECS - Default fallback cooldown for agents when no store is available: 30 minutes.
- BACKOFF_
BASE_ SECS - Base backoff for generic agent/model failures: 5 minutes.
- BACKOFF_
MAX_ SECS - Maximum backoff for generic agent/model failures: 4 hours.
- BILLING_
CYCLE_ COOLDOWN_ SECS - Base cooldown for billing cycle exhaustion: 24 hours.
- BILLING_
CYCLE_ MAX_ SECS - Maximum cooldown for billing cycle exhaustion: 7 days.
- CREDIT_
BACKOFF_ BASE_ SECS - Base backoff for credit exhaustion (out_of_credits): 1 hour.
- CREDIT_
BACKOFF_ MAX_ SECS - Standard cap for credit exhaustion and org-level disabling: 8 hours. Repeated failures at the cap move to the extended tiers (48h, then 96h)
- DEFAULT_
DEGRADED_ RATE_ LIMIT_ THRESHOLD - Default threshold: minimum rate-limit events within the window to consider an agent degraded. A single transient 429 shouldn’t mark an agent; we need a pattern of repeated failures.
- DEFAULT_
HEALTH_ CHECK_ WINDOW_ HOURS - Default health check window for recent rate-limit events (hours).
- ORG_
BACKOFF_ BASE_ SECS - Base backoff for org-level disabling: 2 hours.
- PERSISTENT_
MODEL_ BACKOFF_ BASE_ SECS - Base backoff for persistent model failures (silent exit/provider errors): 4 hours.
- PERSISTENT_
MODEL_ BACKOFF_ MAX_ SECS - Maximum backoff for persistent model failures: 7 days.
- SILENCE_
AGENT_ COOLDOWN_ SECS - Short agent cooldown applied on silence detection (120 seconds).
- SILENCE_
COUNT_ THRESHOLD - Silence detections before applying an extended cooldown (rolling window).
- SILENCE_
COUNT_ WINDOW_ SECS - Rolling window for silence detections (24 hours).
- SILENCE_
EXTENDED_ COOLDOWN_ SECS - Extended cooldown applied after repeated silence detections (4 hours).
Functions§
- clear_
agent_ degraded - Clear the degraded flag for an agent.
- clear_
cooldown - Clear a specific cooldown by key, or all cooldowns when
key == "*". - clear_
expired_ cooldowns - Clear expired cooldowns from the in-memory map.
- compute_
backoff - Compute exponential backoff duration using base-3 growth.
- cooldown_
reason - Return the reason string recorded when a cooldown was set, if the cooldown is still active.
- cooldown_
until - Return the cooldown expiry timestamp for a key (agent or agent:model), if active.
- detect_
credit_ exhaustion - Detect credit exhaustion reasons from error message.
- detect_
credit_ exhaustion_ outcome - Detect credit exhaustion from an error message and return the stable
task_runs.outcomestring for it (seeCreditExhaustionReason::outcome_str). - github_
circuit_ remaining_ secs - Returns the remaining seconds of the GitHub 5xx circuit breaker cooldown, or 0 if the circuit is closed.
- init_
cooldown_ store - is_
agent_ degraded - Check if an agent is currently flagged as degraded by the health check.
- is_
agent_ in_ cooldown - Check if an agent is currently in cooldown period.
- is_
github_ circuit_ open - Check if the GitHub 5xx circuit breaker is currently open.
- is_
model_ in_ cooldown - Check if a specific agent+model combo is in cooldown.
- list_
all_ cooldowns - Return all currently active cooldowns as
(key, cooldown_until_unix, reason). - mark_
agent_ degraded - Mark an agent as degraded (called by
refresh_degraded_agents). - record_
agent_ failure_ with_ message - Record that an agent has failed and should be temporarily avoided.
- record_
agent_ success - Reset failure counts for an agent and agent:model combo after a successful run.
- record_
credit_ exhaustion - Record an agent-level cooldown for credit exhaustion.
- record_
github_ 5xx - Record a GitHub 5xx server error and trip the circuit breaker if the sliding window threshold is exceeded.
- record_
model_ failure - Record that a specific agent+model combo has failed.
- record_
persistent_ model_ failure - Record a persistent model-specific failure and apply longer exponential backoff.
- record_
rate_ limit - Record a rate-limit event for an agent+model.
- record_
silence_ detection - Record a silence detection for an agent+model and apply extended cooldowns when repeated silences exceed the threshold within the rolling window.
- refresh_
degraded_ agents - Refresh the degraded-agent set from the rate_limits table and cooldown state.
- set_
agent_ cooldown - Set a short agent-level cooldown (in seconds).
- set_
model_ cooldown - Set a model cooldown with a custom duration (in seconds) and reason.
- set_
model_ cooldown_ until - Set a model cooldown that ends at a vendor-provided Unix timestamp.
- sync_
from_ kv - Reconcile the in-memory cooldown map with the persisted KV state.