Detect a Runaway Child Loop
A loop feels like diligence until you notice it is the same action forever. Detect repetition by hashing the child's steps, not by reading its optimistic progress messages.
Procedure
- Have each child log every step as a line (command plus result hash) to
notes/children/<id>.steps. - Compute a rolling window of step signatures; a repeated window means the child has cycled.
- Threshold it: three identical consecutive step signatures, or the same failure message twice, is a loop.
- Watch the retry counter: the same command retried more than
max_retries(e.g. 3) without a state change is a loop. - Distinguish progress from motion: an artifact hash unchanged across two steps is no work done, regardless of activity.
- Detect the classic signatures: A-B-A-B alternation, fixed-point retries, and a climbing step count with constant output.
- On detection, stop the child (cooperative first, then terminate) before it burns the wave budget.
- Capture the last N steps and the repeated signature into
notes/loops/<id>.logfor diagnosis. - Fix the cause — usually a missing stop condition or an unfixable error the child keeps re-attempting — then re-dispatch.
- Add the detected signature to a blocklist so a re-dispatched child hitting it aborts fast.
Pitfalls
- Trusting the child's "making progress" heartbeat while its step hashes repeat.
- Counting elapsed time as a loop signal, killing a slow child that is doing genuine single-step work.
- Setting the repetition threshold at 10, so a loop runs long enough to exhaust the token budget first.
- Detecting the loop but not recording the signature, so the re-dispatched child loops identically.
- Treating A-B-A (returning to a starting state after real work) the same as a tight A-B-A-B cycle.
Verification
awk '{print $2}' notes/children/<id>.steps | uniq -c | awk '$1>=3{print "loop: "$2}'
# passes when no signature repeats 3+ times, or the loop was stopped and logged
Report to the user: the repeated signature, the step count when detected, and whether the child was stopped or re-dispatched.