Build your own coding agent · Chapter 17 of 20
Interrupts and resume
By the end of this chapter you can stop Kaarigar in the middle of a run without leaving a server behind or a conversation the API refuses, and start it again later from its transcript, with its own state back, so that it finishes the job as if it had never stopped. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Scale and trust
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Build your own coding agent are free. This one comes with the all labs plan.
Why you need this
Sneha watches Kaarigar work on MD-104 and sees it start the fare server. She has seen enough: she wants a Stop button. Every coding agent has one (Esc in Claude Code, a stop button in Cursor), and people press it all the time: the agent went the wrong way, the meeting starts, the laptop lid closes. Jaya has a condition: Stop must stop everything Kaarigar started, now, not in ten seconds. Rukhsana has another: a stopped run must be able to go on later, and the result must be the same fix with the same tests, not a half-done mess.
Kaarigar so far cannot do either. Its loop takes a signal, but chapter 5's run_command never looks at it: stopped 1.5 s into python scripts/fare_server.py, the run ends only when the command's 10 s timeout fires, 10.4 s in. And a stopped run is just gone: the transcript of chapter 16 records every event, but nothing turns it back into a run that can go on.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
stop_command.py: a command that stops on an interrupt
Write run_command(root, command, timeout=10, signal=None) -> Ran (Ran and clean_env are in kaarigar.py). A signal that is already set starts nothing and returns at once with interrupted True. Otherwise run bash -c command in root with clean_env(), in its own session (start_new_session=True), collecting stdout and stderr; while it runs, check the signal and the clock every few hundredths of a second, and kill the whole process group (os.killpg) at the timeout (timed_out True) or when the signal is set (interrupted True). When the command ends, kill the group once more. exit_code is None when it was stopped.
The check sets the signal 0.4 s into python scripts/fare_server.py: run_command must return within 3 s with nothing left running. A command that ends by itself must return as soon as it ends.
- Task 2
close_out.py: every call gets a result
Write unanswered(messages): the ids of the tool calls in assistant messages that no tool message answers, in order. And close_out(messages, note=NOT_RUN): a new list (the one given stays as it was) with every message in place, and for each call with no result a tool message {"role": "tool", "tool_call_id": ID, "content": note}, right after that reply's own tool messages and before the next message. With nothing missing, the same messages come back.
The check sends a cut conversation to the lab model (refused, a 400) and your closed-out one (accepted).
- Task 3
resume.py: go on from the transcript
Write resume(file, root) -> Restore (Restore is in kaarigar.py). messages: the start event's system prompt and task, then each reply's message, each tool event as a tool message and each feedback event as a user message, in order, through close_out. todos: the items of the last todo_write call (items an earlier call had are kept). read: each path whose last view (its last read_file, or an edit_file whose result starts with ok) still holds: every numbered line in that view is still line N of the file in root, word for word. Only allowed calls count.
Under if __name__ == "__main__": run kaarigar on a checkout of MD-104 with your run_command, a transcript and a signal set after 1.5 s, then print stopped: STOP after N steps, S s, then processes left: N. Resume on the same root with restore=resume(file, root) and your run_command, then print resumed: STOP after N more steps and MD-104: fixed (or not fixed with the reason).
- Task 4 · interview question
Interview: anywhere.py, resume from anywhere
Prove that a run can be stopped anywhere. Run kaarigar on MD-109 once, with no interrupt: N is its number of tool calls; print MD-109: N tool calls in a run that is not interrupted. Then for each k from 1 to N, interrupt a fresh run right after its k-th tool call (on_event, a signal and a transcript) and go on in that checkout with the messages alone (restore=Restore(messages)); do the same again in another fresh checkout with the whole resume(file, root). Print one line per k, two spaces apart:
after call K messages only: fixed with state: fixed (not fixed where it was not)
Then messages only: F/N fixed and with state: F/N fixed.
What you will learn
- Where an interrupt must reach: the loop between steps, the request in flight, the command running, and the calls not yet run.
- Why every call needs a result, even one that never ran, and where that result goes.
- Resuming from a transcript: the conversation, and the state that lived only in the process that stopped (read stamps, the to-do list).
- How to prove a run can be stopped anywhere.
More in Scale and trust
Build "Interrupts and resume" yourself
MeterDown, a Pune app for auto-rickshaw fares, has eleven open issues. You build Kaarigar, an agent that fixes them without breaking tests, running risky commands or touching files outside the repository. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Build your own coding agent are free. This one comes with the all labs plan.