Build your own coding agent (TypeScript) · Chapter 4 of 20
Patches
By the end of this chapter Kaarigar can take a change as a unified diff, the format of git diff, Codex's patches and every code review, and apply it safely: each hunk found by its own lines, the line numbers used only as a hint, and the whole patch applied or none of it. And you can show, with git, exactly what a run changed. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The tools
- 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
Chapter 3's edit_file changes one piece of text at a time. Many coding models are trained to describe a change as a patch instead: a unified diff, with the file's name, the lines that go out, the lines that come in, and a few unchanged lines around them so you can see where it goes. Codex works this way, and a patch is the natural way to change several places, or several files, in one step.
Here is the model's patch for MD-103 (split fares lose paise), after reading src/split.ts with chapter 1's read_file, which sends the file as it is, with no line numbers:
--- a/src/split.ts
+++ b/src/split.ts
@@ -6,5 +6,5 @@
const paise = Math.round(rupees * 100);
const share = Math.floor(paise / riders);
const extra = paise - share * riders;
- return Array.from({ length: riders }, () => share);
+ return Array.from({ length: riders }, (_, i) => share + (i < extra ? 1 : 0));
}
The change is right. The lines around it are right, copied from the file. The number is wrong: @@ -6 says the hunk starts on line 6, and const paise is on line 7. Applied the way patch applies a strict diff, only at the line it names, it fails, and it fails the same way every time:
2 apply_patch -> error: hunk 1 does not apply at line 6
3 read_file
4 apply_patch -> error: hunk 1 does not apply at line 6
5 read_file
6 apply_patch -> error: hunk 1 does not apply at line 6
answer: I could not change src/split.ts: the change did not apply ...
Reading the file again does not help: the model counts the lines of the same text again, and gets the same number. Kedar: "The model copied the lines and counted the number. Trust what it copied."
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
parse-patch.ts: read a unified diff
Export the types Hunk ({ oldStart, old, new }) and FilePatch ({ path, hunks }), and parsePatch(text): skip --- lines; a +++ b/PATH line starts a file (its path without b/ and without a tab and what follows); an @@ -N,n +M,m @@ line starts a hunk with oldStart N (the counts may be missing); a line starting with a space goes in old and new, - in old, + in new, each without its first character; skip anything else.
A hunk before any +++ line throws an Error; text with no +++ line gives [].
- Task 2
apply-patch.ts: apply a patch by its context
Write and export applyPatch(root, text). For each file (error: PATH does not exist), for each hunk in order, find every place its old lines are at or after where the last hunk ended, pick the one nearest line oldStart, and replace them with the new lines; note hunk K found at line X, not N when it moved. No place: error: hunk K of PATH does not apply: ... Nothing was changed.
Write no file until every hunk of every file has a place, then return ok: PATH: N hunks applied (notes), one part per file joined with ; . Not a diff: error: not a unified diff ...
Export patchTool: a Tool named apply_patch with a required patch parameter.
- Task 3
run-diff.ts: what did the run change?
Write and export changedFiles(root): one { path, added, removed } per line of git diff --numstat run in root (ADDED, tab, REMOVED, tab, PATH), after git add -A --intent-to-add so new files count.
Under if (import.meta.main), run runAgent on a checkout of MD-108 with readTool, patchTool and testsTool, then print one line per changed file, PATH +ADDED -REMOVED, then MD-108: fixed (or not fixed).
- Task 4 · interview question
Interview: which-applier.ts, the model or the tool?
Run runAgent on MD-108 three times, each on a fresh checkout, with SYSTEM: raw read, strict (rawReadTool, strictPatchTool, testsTool); raw read, by context (rawReadTool, patchTool, testsTool); numbered read, strict (readTool, strictPatchTool, testsTool).
Print one line per way: raw read, strict: not fixed, N steps, R patches refused (fixed or not from grade; R: apply_patch calls whose result starts with error:).
What you will learn
- A unified diff: files, hunks, and lines marked with a space, - or +.
- Why a model's line numbers are wrong and its context lines are right.
- Applying by context: find the hunk by its lines; the @@ number only breaks ties.
- All or nothing: a patch that fails anywhere changes nothing.
- The diff of a run: what Kaarigar changed, from git.
More in The tools
Build "Patches" 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.