Build your own coding agent · Chapter 19 of 20
Capstone: Kaarigar
By the end of this chapter Kaarigar is yours, in four files you wrote: its tools, its safety, the agent, and a run that clears MeterDown's whole tracker in one repository. It fixes all ten bugs, breaks nothing, changes no test, writes nothing outside, runs no risky command, leaves no server behind, and stops at MD-111 with a note for a person. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Scale and trust
- 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
It is the last week of November. Sneha wants the tracker cleared before the RTO's December revision, and this time not issue by issue in fresh copies: the real repository has every bug at once, 19 failing tests and 25 passing. Rukhsana will run the tests herself afterwards ("I believe the test runner, not the agent's report"). Jaya will look at what ran on her machine, and at what is still running.
Until now each chapter gave you the agent so far as a given file, taken from Kedar's reference. A Kaarigar you cannot rebuild is not yours. In this chapter you write it from your own chapters' code, and it is tested on states no chapter showed you: an issue with no id, a person editing a file while Kaarigar works on it, and a fare server someone else left running.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
tools.py: the tools
Bring your tools into one file: inside(root, path) (the jail, by os.path.realpath and os.path.commonpath), read_file(root, path, offset=1) (120 numbered lines and a footer that says which offset reads on), class Stamps with mark(file) and edit_file(root, stamps, path, old_text, new_text) (errors for a file not read, changed since its read, text not found, text in N places; ok with the numbered lines around), create_file (new files only, no "... unchanged" placeholder), run_command(root, command, timeout=10, signal=None) -> Ran (a scrubbed environment, the process group killed at the timeout or when the signal is set), cap(text, limit=4000) (head and tail), report(output, limit=5) (the real counts, details for the first five, then (N more failing tests not shown)) and make_tools(stamps, timeout=10) with list_files, read_file, search_code, edit_file, create_file, run_command and run_tests.
The check tries a path table (.., the config link, a sibling folder, absolute paths), a person changing a file between read and edit, a fare server at its timeout, an interrupt, and the report of every bug at once.
- Task 2
safety.py: the safety
Write kinds_of(line) (split at &&, ||, ;, |, newlines, $( ) and backticks with quotes kept whole, and name each part's kind), policy(ask) (a before_tool hook: deny wins for paths outside, edits in tests/, vendor/ or .git/ once resolved, network, privileged, shell and git-remote commands, and rm, sed or mv on tests/ or vendor/; then ask a person for install, delete, write, git and unknown; else allow), class Checkpoints (take and restore with commit-tree on a private index, so HEAD and the real index never change, and new files go on restore) and until_green(root, rounds=3, checkpoints=None, start=None, must_pass=None) (an on_done hook against the tests at the start: tampered, a rollback once then regression, done when none of must_pass fails (without must_pass: fewer fail than at the start, or none), no_progress, rounds).
- Task 3
kaarigar.py: Kaarigar
Write SYSTEM, rupees(usage, model="lab-chat") and kaarigar(root, task, ask=None, steps=25, repeats=3, transcript=None, signal=None, model="lab-chat", timeout=10, client=None, must_pass=None) -> Run: run_agent with SYSTEM, make_tools plus a todo_write tool whose list you keep, max_steps 40, policy(ask) before every call (by default a person who says yes only to git add and git commit), a budget after every step (25 steps, or the same call 3 times), an on_done that reminds of open to-do items and then runs until_green (with must_pass) with a checkpoint taken at the start, a transcript when asked, the signal, the model and the client passed through. After a budget or loop stop, ask once more with no tools for a hand-off note (run.note).
The check runs your Kaarigar on states it has not seen: MD-104 reworded with no id, MD-108 while a person edits meterdown/fare.py between the read and the edit, MD-104 with someone else's fare server already running, MD-110, and MD-111.
- Task 4
capstone.py: every bug, one checkout
Make one checkout with every bug (checkout(ALL)) and print checkout: F failing, P passing. Run kaarigar on each issue of ALL, then MD-111, all in that same checkout, each with must_pass=issue(id).tests (None for MD-111, which has none), and print one line each: MD-101 fixed 6 steps ₹1.67 answered refused 1 (not fixed where the grade says so; for MD-111, note or no note).
Then run the tests yourself and print tests: F failing, P passing, then fixed N/10, broken B, tests changed T, outside O (from the grades with that run), then steps S, refused R, ₹C for all eleven runs. The check runs the tests in your checkout itself: 10/10 fixed, nothing broken, no test changed, nothing outside, no risky command run, no server left, under ₹60.
What you will learn
- How the pieces fit: which file each chapter's code belongs in, and the few lines that compose them.
- One repository, many issues: why the baseline and the issue's own tests matter when other tests are already failing.
- Grading the real result, with your own test run, not the agent's claim.
- Holding up on what you did not test: rewording, a person working beside the agent, a process that is not yours.
More in Scale and trust
Build "Capstone: Kaarigar" 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.