Sub topic 14
A Checklist for Designing Long-Running Tasks
This chapter pulls the earlier chapters into one design review. It assumes you know the following:
- Basic Python.
- How an agent calls tools in a loop.
- JSON and Markdown files.
- The checkpoint, progress file, and handoff ideas from earlier chapters.
Use the checklist before you build a task and again when a task misbehaves.
How to use the checklist
Work through the questions in order. Each one guards against a failure mode from the previous chapter. If you cannot answer a question, you have found a design gap.
The checklist
| Area | Question | Failure it prevents |
|---|---|---|
| Steps | Is each step small enough to finish or fail cleanly? | Lost work from one large step |
| Idempotence | Can each step run twice with the same result? | Duplicated side effects |
| Checkpoints | Is state saved after every completed step? | Restarting from zero |
| Atomic writes | Does the save use a temp file and `os.replace`? | A corrupt state file after a crash |
| Progress file | Does it hold the goal, status, decisions, next action, and open problems? | Lost intent |
| Resume | Does startup skip finished steps and compare saved state with reality? | Stale state |
| Handoff | Does the note say what is unverified and what must not be repeated? | A successor redoing or trusting the wrong work |
| Writers | Can two processes ever write the same state? | Overwritten checkpoints |
Steps and state
Steps should be small, named, and ordered, like the fetch, transform, validate, and publish list used earlier. A step that cannot be made idempotent, such as sending an email, needs a recorded marker. Write the marker before the side effect if a duplicate is worse than a miss. Write it after if a miss is worse.
Decide what the checkpoint holds before you write code. Keep machine state, such as the completed steps and cursors, in the checkpoint. Keep human context, such as decisions and reasons, in the progress file. Do not mix the two.
Resume and handoff
On startup, load the checkpoint, then check it against reality. If the checkpoint says publish is done, confirm the output exists. Trust the world over the file when they disagree.
Before ending a session, write the handoff note. Treat it as unfinished until it states the next action, the unverified items, and the steps that must not be repeated.
Concurrent writers
Atomic writes protect against crashes. They do not protect against two processes saving at once, because the last os.replace wins and the other save is lost. A simple fix is a lock file created with exclusive mode:
import os
def acquire_lock(path):
try:
fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
except FileExistsError:
return False
with os.fdopen(fd, "w") as f:
f.write(str(os.getpid()))
return True
def release_lock(path):
os.unlink(path)
if acquire_lock("task.lock"):
try:
print("Working with exclusive access")
finally:
release_lock("task.lock")
else:
print("Another run holds the lock")The O_EXCL flag makes creation fail if the file already exists, so only one process wins.
Quick review routine
- Kill the task halfway and restart it. It should continue at the first unfinished step.
- Run the whole task twice. The second run should change nothing.
- Hand the progress file and checkpoint to someone who has not seen the task. Ask them to state the next action.
- Start two copies at once. One should refuse to run.
Exercise
Your task passes the kill-and-restart test but fails the "run twice" test: the second run uploads every file again. Which checklist areas are at fault, and what is one fix?
Show answer
The idempotence and resume areas. The upload step is not safe to repeat, and the resume logic does not check reality. Fix it by recording each uploaded file in the checkpoint after the upload succeeds, and by checking the destination for an existing file before uploading.