Build your own coding agent (TypeScript) · 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
- TypeScript
- In the all labs plan
The first 2 chapters of Build your own coding agent (TypeScript) 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 has an AbortSignal, but chapter 5's runCommand never listens to it: stopped 1.5 s into npm run serve, the run ends only when the command's 10 s timeout fires, 10.2 s later. 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.ts: a command that stops on an interrupt
Write and export runCommand(root, command, { timeoutMs = 10_000, signal } = {}), returning { exitCode, timedOut, interrupted, output, ms } (the Ran type in kaarigar.ts). A signal that is already aborted starts nothing and returns at once with interrupted: true. Otherwise run bash -c command in root with cleanEnv(), detached, collecting stdout and stderr; kill the whole process group at the timeout (timedOut: true) or when the signal aborts (interrupted: true). When the command closes, clear the timer, remove your abort listener and kill the group once more. exitCode is null when it was stopped.
The check interrupts npm run serve after 0.4 s: it must return within 3 s with nothing left running, and three commands that finish must leave no listener on the signal.
- Task 2
close-out.ts: every call gets a result
Write and export unanswered(messages): the ids of the tool calls in assistant messages that no tool message answers, in order. And closeOut(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, 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.ts: go on from the transcript
Write and export resume(file, root), returning { messages, read, todos } (Restore in kaarigar.ts). 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 closeOut. 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 (import.meta.main): run kaarigar on a checkout of MD-104 with your runCommand, a transcript and a signal aborted 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), runCommand }, then print resumed: STOP after N more steps and MD-104: fixed (or not fixed with the reason).
- Task 4 · interview question
Interview: anywhere.ts, 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 (options.onEvent, a signal and a transcript) and go on in that checkout with the messages alone ({ 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 (TypeScript) are free. This one comes with the all labs plan.