Heuristic:1ay1 Agentty Bounded Never Fatal
| Knowledge Sources | |
|---|---|
| Domains | Reliability, Resource_Management |
| Last Updated | 2026-10-28 14:00 GMT |
Overview
Background and cleanup work must be bounded in resource usage and must never be fatal to the main process — if a background task fails, it logs and degrades, it does not crash.
Description
The Bounded_Never_Fatal heuristic requires that fire-and-forget background work, async caches, and process-exit teardown registries are designed with hard resource bounds and a non-fatal failure mode. A background task that allocates unbounded memory, or that crashes the process on failure, is a reliability hazard. Instead, background work uses bounded queues, generation-guarded caches that evict on generation mismatch, and teardown callbacks that catch and log exceptions rather than propagating them.
In agentty this appears in: the background work owner (`background.hpp`) with bounded queues and swallowed errors; the generation-guarded cache (`gen_cache.hpp`) with eviction on generation change; the teardown registry (`teardown.hpp`) with self-registering callbacks that log on failure; the models.dev snapshot (`modelsdev.hpp`) with bounded background fetch; and the dual-lock shared file (`shared_file.hpp`) with bounded retry.
Usage
Apply this heuristic when designing any background task, async cache, or cleanup callback. Ask: what happens if this fails? If the answer is "the process crashes," redesign it to log and degrade instead. Ask: what happens if this runs 1000 times? If the answer is "unbounded memory," add a bound.
The Insight (Rule of Thumb)
- Action: Background/cleanup code must be resource-bounded and must catch+log errors, never propagate them.
- Value: Bounded queues, generation-guarded eviction, try/catch in teardown callbacks.
- Trade-off: Failures are silent (logged, not surfaced) — but this is correct for background work where the caller has already moved on.
- Compatibility: Pairs with structured logging (`logx.hpp`) so swallowed errors are at least visible in logs.
Reasoning
A background task that crashes the main process is worse than no background task — it converts an async convenience into a reliability liability. A teardown callback that throws during process exit can prevent clean shutdown or leave resources leaked. By bounding resources and swallowing errors (with logging), background work degrades gracefully: the main process survives, the feature degrades, and the error is visible in logs for diagnosis. The principle is that background work is best-effort by definition — the caller has already moved on, so there is no one to propagate the error to.