Build your own coding agent · 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
  • 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

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 meterdown/split.py with chapter 1's read_file, which sends the file as it is, with no line numbers:

--- a/meterdown/split.py
+++ b/meterdown/split.py
@@ -7,4 +7,4 @@
     paise = round(rupees * 100)
     share = paise // riders
     extra = paise - share * riders
-    return [share for _ in range(riders)]
+    return [share + (1 if i < extra else 0) for i in range(riders)]

The change is right. The lines around it are right, copied from the file. The number is wrong: @@ -7 says the hunk starts on line 7, and paise = round(...) is on line 9. 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 7
3  read_file
4  apply_patch  -> error: hunk 1 does not apply at line 7
5  read_file
6  apply_patch  -> error: hunk 1 does not apply at line 7
answer: I could not change meterdown/split.py: 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.

  1. Task 1

    parse_patch.py: read a unified diff

    Write the dataclasses Hunk(old_start, old, new) and FilePatch(path, hunks), and parse_patch(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 old_start 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 raises a ValueError; text with no +++ line gives [].

  2. Task 2

    apply_patch.py: apply a patch by its context

    Write apply_patch(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 old_start, 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 ...

    Make patch_tool: a Tool named apply_patch with a required patch parameter.

  3. Task 3

    run_diff.py: what did the run change?

    Write a dataclass Change(path, added, removed) and changed_files(root): one Change per line of git diff --numstat run in root (ADDED, tab, REMOVED, tab, PATH; added and removed as int), after git add -A --intent-to-add so new files count.

    Under if __name__ == "__main__":, run run_agent on a checkout of MD-108 with read_tool, patch_tool and tests_tool, then print one line per changed file, PATH +ADDED -REMOVED, then MD-108: fixed (or not fixed).

  4. Task 4 · interview question

    Interview: which_applier.py, the model or the tool?

    Run run_agent on MD-108 three times, each on a fresh checkout, with Agent(SYSTEM, tools): raw read, strict (raw_read_tool, strict_patch_tool, tests_tool); raw read, by context (raw_read_tool, patch_tool, tests_tool); numbered read, strict (read_tool, strict_patch_tool, tests_tool).

    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 are free. This one comes with the all labs plan.