Harness engineering (TypeScript) · 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
- TypeScript
- In the all labs plan
The first 2 chapters of Harness engineering (TypeScript) 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 227 to 234 of gst.ts, wrote back what it saw, and the file went from 234 lines to 8. T01's money.ts came back at 28 of its 54 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,743 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.ts: an edit by exact match
In edit-tool.ts, export editFile(root, args), args being { path, old_text, new_text } (the names the model sends). 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
new_text is written exactly as it came, $ signs included. Then export EDIT, a Tool (the type from the given agent.ts) named edit_file with the string parameters path, old_text and new_text, all required, and editFile as its fn.
- Task 2
t03-both.ts: T03 with a whole-file write and with an edit
In t03-both.ts, run runAgent (from agent.ts) on a fresh checkout of T03 twice, with new Registry([LIST, READ, SEARCH, tool]): first with agent.ts's WRITE, then with your EDIT. For each, count src/gst.ts's lines before and after the run, grade the checkout, and print:
TOOL: gst.ts 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.ts: the diff of a run
A run is reviewed by its diff. In run-diff.ts, export diffStat(root): from diff(root), an array 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 (import.meta.main), run runAgent on a fresh checkout of T10 with new 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.ts, an edit that changes every match
"Why not just text.replaceAll(old, new)?" In replace-all.ts, export editEvery(root, args), args being { path, old_text, new_text }: it replaces every match of old_text in the file and returns ok. Export EVERY, a Tool with EDIT's name, description and parameters and editEvery as its fn.
Under if (import.meta.main), run runAgent on a fresh checkout of T05 with new 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 diffStat; 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 in TypeScript 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 (TypeScript) are free. This one comes with the all labs plan.