Build a Continuous Software Engine

Chapter 03 • Resilience & Liveness

Never Getting Stuck: Watchdogs, Recovery & Liveness

Most naive agent loops do not fail with a clean crash—they silently freeze at 2 a.m. Treat stuckness as a first-class engineering problem across processes, retries, networks, and the scheduler state machine itself.

Detached Process-Group Watchdogs & Orphan Cleanup

A coding agent rarely runs as a single process. It launches test runners, compilers, headless browsers, and local dev servers. If the agent times out—or if the orchestrator itself receives SIGKILL or crashes—killing only the direct child PID leaves orphaned grandchildren holding file locks, open ports, and gigabytes of RAM.

Run every attempt inside its own detached process group (via setsid or platform process-group isolation) supervised by a lightweight watchdog that watches both a wall-clock timeout and an owner-death signal (such as a parent-death pipe or kernel notification). When a timeout fires or the orchestrator disappears, the watchdog sends termination signals to the entire negative process group ID, guaranteeing that grandchildren die cleanly too.

ISOLATED PROCESS GROUPWatchdog SupervisorOwner-death + wall clockDetached PGID (setsid)• Coding agent process• Test runners & servers• Grandchild daemonsSIGKILL kills entire tree1. LIVELOCK (BUSY, ZERO FILE DIFF)Agent keeps talking or polling without editing files → abort early & rotate strategy.2. NETWORK / PROVIDER OUTAGEPark goal in waiting state with backoff — do NOT consume attempt retry budget.3. REPEATED FAILURE SIGNATURERotate strategy (narrow scope, switch model/prompt angle, or inspect evidence).4. A → B → A OSCILLATION / FILE REVERSALDetect repeating diff/failure hash cycles → stop thrashing and escalate cleanly.
A watchdog treats every hanging, spinning, or offline attempt differently so the system neither leaks orphan processes nor burns its retry budget on transient outages.

Distinguishing Livelock, Network Drops, and Thrashing

A wall-clock timeout is a blunt instrument. Before you burn through a goal’s retry budget—or wait thirty minutes for a hopeless run—classify why an attempt is not making progress:

Livelock Detection

The agent stays busy—emitting tokens, re-reading files, or polling commands—yet produces zero meaningful file changes over a sustained window. Abort early instead of paying for twenty minutes of circular narration.

Network-Drop Parking

Model API errors, DNS drops, or laptop sleep are environment outages, not code bugs. Park the goal in a waiting state with exponential backoff without decrementing its retry budget so an overnight Wi-Fi hiccup never permanently escalates healthy goals.

Strategy Rotation Across Retries

Retrying the exact same prompt with the exact same framing usually reproduces the exact same failure. Rotate strategy on each retry: feed the concise failure signature first, narrow the allowed scope, switch to a diagnostic pass, or change model tier.

Oscillation & File-Reversal Guards

Track hashes of candidate diffs and failure signatures across attempts. If Attempt 3 produces the same state or reverts the same lines that Attempt 1 tried (an A → B → A cycle), stop thrashing immediately and escalate to the planner or a human.

Failure Mode Prevented: Silent Retry-Budget Burn

If your engine treats a temporary API 503 the same as a failing unit test, a ten-minute provider outage will burn through every ready goal’s retries and leave your entire backlog marked failed by morning. Separate transient infrastructure errors (park and wait) from deterministic task failures (consume a retry and rotate strategy).

The Hard Lesson on Scheduler Deadlocks

Even after you harden worker processes and retries, the nastiest hangs hide inside the orchestrator’s own scheduling logic. They happen when readiness rules are scattered across four different places: the dispatcher thinks a goal is blocked on a dependency, the approval queue thinks a slot is occupied, the backoff timer thinks a goal is parked, and the planner thinks active work is still running. Everyone waits for someone else, and the engine sits silently idle forever with zero error messages.

The cure is architectural simplicity backed by verification:

  1. Unify readiness into one pure decision function. Collect a snapshot of world state—ready goals, active leases, dependency states, parked approvals, network cooldowns, and planner eligibility—and feed it to a single pure function that returns the next concrete action (dispatch, invoke planner, wait on a named timer/event, or complete).
  2. Require every wait state to name its wake trigger. The scheduler must never return “do nothing” unless either all work is terminal or an explicit wake condition exists (a running lease, a backoff deadline, a pending human approval, or an incoming sync poll).
  3. Verify exhaustive finite-state liveness. Because the decision function is pure and operates over a small finite set of state flags, write a test that enumerates every reachable combination of states—every mix of ready, blocked, leased, approval-waiting, network-parked, and escalated goals—and asserts that no non-terminal state can ever fall into a silent dead end.
ONE PURE DECISION FUNCTIONnextAction(worldState)Exhaustively checked for dead statesReady GoalsDeps met, not leasedParked / WaitingApproval, network, or depsDispatch WorkerIf slot free & ready existsInvoke PlannerIf queue empty & ambition openExplicit Wait / DoneNamed wake trigger required
When dispatch, dependency checks, approval waits, and planning share one pure readiness model, you can exhaustively model-check every finite state combination to guarantee no silent deadlock can hide.
Why exhaustive finite-state testing beats ad-hoc unit tests for schedulers

Hand-written scheduler tests almost always check the transitions you already thought of. Deadlocks live in the awkward four-way corners: for instance, Goal A is waiting for human approval, Goal B depends on Goal A, Goal C is network-parked, and the ambition document still has room for Goal D—should the planner run or stay quiet?

When readiness lives in one pure model, checking thousands of finite state combinations takes milliseconds in your ordinary test suite and catches every silent deadlock the moment you add a new state flag.

How to Know You Built It Right

  • Sending SIGKILL to the orchestrator during an active run leaves zero orphaned test runners or dev servers alive a second later.
  • Disconnecting the network mid-run parks active goals and resumes them cleanly once connectivity returns, with zero retries burned.
  • A mocked attempt that alternates between two broken diffs triggers the A → B → A oscillation guard and escalates instead of looping until budget exhaustion.
  • An automated finite-state liveness test checks every reachable combination of scheduler flags and proves zero silent dead states exist.