Harness engineering · Chapter 4 of 23
Editing files
By the end of this chapter you can give a coding agent the way coding assistants change code: an edit that replaces one exact piece of text and refuses anything else, instead of writing whole files back. You will measure it against the whole-file write on T03, read the diff a run leaves behind, and see on T05 why "replace every match" is a bug. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The core
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Harness engineering are free. This one comes with the all labs plan.
Why you need this
Chapter 3 made the reading good: a page, a footer, a search. And it made the writing worse. With search, T03's model saw lines 221 to 230 of gst.py, wrote back what it saw, and the file went from 230 lines to 10. T01's money.py came back at 27 of its 53 lines. The model had fixed the right line both times; the tool threw the rest of the file away.
Kabir says this is not a model to be trained out of it, it is a tool to be replaced. A whole-file write asks the model to reproduce every line it did not mean to change, from what it saw. A model that saw a page cannot do that, and a model that saw everything still shortens long files (chapter 1's # ... rest of the table unchanged ...). It also costs: T10's three whole files were 1,762 completion tokens, to change six lines.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
edit_tool.py: an edit by exact match
In edit_tool.py, write edit_file(root, path, old_text, new_text). It changes the file only when old_text appears in it exactly once, and returns:
ok: PATH changed
when it replaced that one place with new_text;
error: old_text was not found in PATH
when it is not there;
error: old_text matches N places in PATH: add lines around it so it matches one
when it is there N times (the file is left as it was in both cases). An empty old_text creates a new file with new_text as its text (and the folders it needs), returning ok: created PATH; when that file already exists, it changes nothing and returns:
error: PATH already exists: to change it, give the old_text to replace
Then EDIT, a Tool (from the given agent.py) named edit_file with the string parameters path, old_text and new_text, all required, and edit_file as its function.
- Task 2
t03_both.py: T03 with a whole-file write and with an edit
In t03_both.py, run run_agent (from agent.py) on a fresh checkout of T03 twice, with Registry([LIST, READ, SEARCH, tool]): first with agent.py's WRITE, then with your EDIT. For each, count hisaab/gst.py's lines before and after the run, grade the checkout, and print:
TOOL: gst.py B lines before, A after, T03 passed
(TOOL the tool's name, write_file or edit_file; passed or failed from the grade).
- Task 3
run_diff.py: the diff of a run
A run is reviewed by its diff. In run_diff.py, write diff_stat(root): from workbench.diff(root), a list of (path, added, removed), one per file in the diff's order, counting the lines that start with + and with - (not the +++ and --- lines that name the files). A new file counts too.
Under if __name__ == "__main__":, run run_agent on a fresh checkout of T10 with Registry([LIST, READ, SEARCH, EDIT]), then print one line per file:
PATH +A -R
and a last line:
N files changed, +A -R
(the totals).
- Task 4 · interview question
Interview: replace_all.py, an edit that changes every match
"Why not just text.replace(old, new)?" In replace_all.py, write edit_every(root, path, old_text, new_text): it replaces every match of old_text in the file and returns ok. Make EVERY, a Tool with EDIT's name, description and parameters and edit_every as its function.
Under if __name__ == "__main__":, run run_agent on a fresh checkout of T05 with Registry([LIST, READ, SEARCH, tool]), first with EVERY, then with EDIT, and print for each:
LABEL: edit_file calls E, lines changed C, T05 passed
(LABEL every match, then exactly one; E the edit_file calls the model made; C the lines added in the diff, from diff_stat; passed or failed from the grade).
What you will learn
- An edit by exact match: old_text found exactly once is replaced; found nowhere or many times, nothing changes and the error says which.
- New files with the same tool, and why an edit never overwrites one.
- The numbers: T03 and three more tasks with a whole-file write and with an edit.
- The diff of a run: what a run changed, file by file.
- Why one match: an edit that replaces every match, on T05.
More in The core
Build "Editing files" yourself
Hisaab, a Lucknow invoicing startup, wants a coding agent to clear its bug list. You build its harness and score it on twelve tasks. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Harness engineering are free. This one comes with the all labs plan.