Sub topic 13
Common Failure Modes and How to Spot Them
This chapter assumes you can read Python, know how an agent calls tools in a loop, and have read the earlier chapters on checkpoints, progress files, resuming, and handoffs. Saved state keeps a long task alive, but it can also fail in quiet ways. Each failure below comes with a symptom you can look for and a fix.
Stale state
Stale state is a checkpoint or progress file that no longer matches the real world. A person may have edited the output by hand, or another process may have deleted a file. The checkpoint still says a step is done, so the resume routine skips it.
Symptoms: a later step fails on missing input, or the final result is missing pieces that the checkpoint says exist.
Fix: before trusting a "completed" entry, check its evidence. The check can be cheap, such as confirming the output file exists and the row count matches.
import os
def verify_completed(state, expected_outputs):
"""Return the completed steps whose output files are missing."""
stale = []
for name in state["completed"]:
path = expected_outputs.get(name)
if path is not None and not os.path.exists(path):
stale.append(name)
return stale
state = {"completed": ["fetch", "transform"], "notes": []}
outputs = {"fetch": "raw.json", "transform": "clean.json"}
print(verify_completed(state, outputs))If the list is not empty, remove those steps from completed and rerun them.
Duplicated side effects
A crash can happen after a step does its work but before the checkpoint records it. On resume, the step runs again. For a pure computation this is harmless. For an upload, an email, or a payment, it creates duplicates.
Symptoms: two copies of an uploaded file, a customer who got the same message twice, or a counter that is too high.
Fix: make side effects idempotent, so repeating them has no extra effect. Use a stable key, such as a file name built from the month rather than a timestamp. Check whether the effect already happened before repeating it. Write a "Do not repeat" line in the handoff for steps that cannot be made safe.
Half-written state
If a process dies while writing a checkpoint in place, the file can be left truncated. The next resume then fails to parse it, or worse, reads partial data.
Symptoms: a JSONDecodeError on startup, or a checkpoint that is empty.
Fix: write to a temporary file and swap it in with os.replace, as in the atomic write from the checkpoints chapter. Do not catch the parse error and start from scratch silently, because that discards real progress. Stop and report it.
Concurrent writers
Two agents or processes writing the same checkpoint will overwrite each other. Each one loads the state, adds its step, and saves, so the second save erases the first one's step.
Symptoms: steps that were finished are missing from completed, or the file's contents flip between runs.
Fix: allow only one writer per task file. Use a lock file or give each worker its own checkpoint.
Drifting progress files
A progress file is only useful if it is current. Agents often update it at the start of a session and then forget it.
Symptoms: "Next action" names a step that is already done, or "Open problems" lists issues that were solved days ago.
Fix: update the progress file in the same code path that saves the checkpoint, and review it when resuming.
Lost intent
A handoff that lists what was done but not why leads the next agent to undo decisions. For example, it may rename pages that were deliberately kept at their old URLs.
Fix: record decisions with their reasons in the progress file.
Quick reference
| Failure | Typical symptom | First fix |
|---|---|---|
| Stale state | Missing input for a "done" step | Verify outputs on resume |
| Duplicated side effects | Duplicate uploads or messages | Idempotent keys, existence checks |
| Half-written state | Parse error on load | Atomic write |
| Concurrent writers | Steps vanish from the checkpoint | Lock or one file per worker |
| Drifting progress file | Wrong "Next action" | Update with each checkpoint |
| Lost intent | Decisions reversed | Record reasons |
Exercise
A job uploads a report, then saves its checkpoint. The process crashes between the two. On resume, the upload runs again and the storage bucket now holds two reports. Which failure mode is this, and what is one fix?
Show answer
This is a duplicated side effect. One fix is to make the upload idempotent by using a stable file name, such as one built from the report's month, so the second upload replaces the first. Another is to check whether the file already exists before uploading.