Build your own coding agent (TypeScript) · Chapter 10 of 20
Checkpoints and undo
By the end of this chapter Kaarigar saves the checkout before it changes anything, can put every file back (new files removed, deleted ones returned) without moving a branch or touching the index, rolls back a change that broke tests and lets the model try again, and gives a person an undo button, one change at a time. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The loop that fixes
- 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
MD-107: night fares are short on long trips. The driver who reported it guessed the cause: "roundFare in src/money.ts cuts the paise off." The model believes him. Its first try rounds every fare up, and four tests that passed before now fail: every fare that ended below 50 paise is a rupee too high.
Chapter 9 made that honest: the run stops with regression instead of ending as if all were well. But the broken roundFare is still in the checkout, and the model never gets a second chance. Kedar's first Kaarigar left MD-107 the same way: not fixed, 4 tests broken.
Rukhsana does not want a label on the mess; she wants the mess gone. And Sneha, watching Kaarigar work on her laptop, asks for something every editor has: an undo.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
checkpoints.ts: save, restore, undo
Write and export class Checkpoints(root) with list, take(label), restore(cp) and undo(). Run git with GIT_INDEX_FILE set to join(root, '.git', 'kaarigar-index'). take: git add -A, git write-tree, git commit-tree TREE -p HEAD -m "checkpoint: LABEL"; push { id, label } on list and return it. restore: git add -A, the changed files from git diff --cached --name-only ID, then git read-tree -u --reset ID; return those files. undo: pop the last checkpoint and restore it ([] when there is none). The branch, git log and the real index must never change.
- Task 2
rollback.ts: roll back, try again
Write and export withRollback(root, cps), an onDone hook: when made, take a checkpoint ('start'), run the tests, and make untilGreen(root). The first time the verdict is regression, restore the start and return { message } saying how many tests broke and which (the first three), that the changes were rolled back and which files were restored, and to find a fix that does not break them. Otherwise, return untilGreen's answer.
Under if (import.meta.main), run MD-107 with SYSTEM, tools() and this hook; print each message sent back, then stop: STOP, steps: N, then MD-107: fixed, B broken (or not fixed).
- Task 3
undo.ts: one change at a time
Write and export checkpointed(tools, cps): the same tools in the same order, where edit_file and create_file first take a checkpoint labelled before TOOL PATH, then run, and drop that checkpoint when the result starts with error. Under if (import.meta.main), run MD-105 with SYSTEM and checkpointed(tools(), cps), then print checkpoints: LABELS (joined with "; "), then now:, undo once: and undo twice: with the diff's added Mumbai line each time, or (no change).
- Task 4 · interview question
Interview: what-undo-misses.ts
On a checkout of MD-107, note HEAD, take a checkpoint, run the given badRun(root), restore. Then print WHAT: undone or WHAT: still there for each of: src/money.ts changed, src/notes.ts created, test/fare.test.ts deleted, fleet-config/helpline.txt changed, a commit on main.
What you will learn
- Checkpoints with git: a commit no branch points at, an index of your own.
- Restore: every file as it was, files made since removed.
- Why not git stash, git checkout -- . or a commit on the branch.
- Rollback on regression: put the files back, tell the model, go on.
- Undo, one change at a time, and what no checkpoint can undo.
More in The loop that fixes
Build "Checkpoints and undo" 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.