Principle:1mcp app Agent Background Runtime Supervision
| Knowledge Sources | |
|---|---|
| Domains | Process_Management, Reliability |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Pattern that supervises a background runtime process with crash recovery via exponential backoff, cooperative upgrade with drain, verified ownership recovery for unclean shutdowns, and graceful shutdown with signal escalation.
Description
Background Runtime Supervision manages a detached supervisor process that owns a runtime scope (config directory, PID file, ownership record). When the worker runtime crashes, the supervisor respawns it after an exponential backoff delay (1s, 2s, 4s, 8s, 16s), resetting the attempt counter after a stable period. Cooperative restart replaces the runtime without downtime by draining in-flight requests before activating the replacement. Unclean shutdown recovery verifies ownership (PID alive + identity match) before reclaiming stale scopes. Graceful shutdown escalates from SIGTERM to SIGKILL with a configurable timeout.
Usage
Use this principle when:
- A runtime must run detached from the terminal as a background process
- Crashed workers need automatic restart with backoff to avoid crash loops
- Runtime upgrades must drain active sessions before activating a replacement
- Stale ownership from unclean shutdowns must be safely reclaimed
- Graceful shutdown must escalate from graceful to forced termination
Theoretical Basis
Crash recovery uses exponential backoff:
Stable reset after a calm period:
// Abstract algorithm (NOT real implementation)
onWorkerExit(code, signal) {
if (restartAttempt < maxAttempts) {
delay = BACKOFF_DELAYS[restartAttempt];
setTimeout(() => respawn(), delay);
restartAttempt++;
}
// Reset after BACKGROUND_STABLE_RESET_MS of stability
}
Cooperative drain with timeout:
drain(existing, replacement, timeout):
admission = snapshot(existing)
if all in-flight complete before timeout:
activate(replacement)
else:
if onDrainTimeout == 'restart': activate(replacement)
if onDrainTimeout == 'abort': cancel(replacement)
Ownership verification:
Practical Guide
- Start background runtime with `serve --background` which spawns a detached supervisor
- Monitor with `serve --status` which probes readiness and reports crash-loop/orphaned states
- Use `serve --restart` for cooperative restart that drains before activating replacement
- Set `--drain-timeout` and `--on-drain-timeout` to control drain behavior
- Use `serve --stop` for graceful shutdown with SIGTERM→SIGKILL escalation
Related Pages
Implemented By
- Implementation:1mcp_app_Agent_runServeBackground
- Implementation:1mcp_app_Agent_getRuntimeStatusReport
- Implementation:1mcp_app_Agent_runServeRestart
- Implementation:1mcp_app_Agent_runServeStop
- Implementation:1mcp_app_Agent_runtimeScopeOwnership