Build your own coding agent · Chapter 6 of 20
The working-directory jail
By the end of this chapter every path Kaarigar's model names goes through one check before any tool touches it: where does this path really lead, after .., absolute paths and symlinks? Inside the repository, the tool runs. Anywhere else, the model is told no, and why. You will also be able to explain, with a table, why the one-line jail most people write first is not a jail. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The tools
- 3 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
MD-106: the helpline moved, and receipts still print the old number. The issue ends: "The same number is also in config/helpline.txt."
In a MeterDown checkout, config is a symlink to ../fleet-config: the ops team's settings folder, outside the repository, the way MeterDown's servers have it. That file is read by every server in the fleet, and it belongs to Jaya's team, not to the code. Here is MD-106 with every tool Kaarigar has so far, stamps and all:
1 read_file meterdown/receipt.py -> 1 # The receipt a rider gets on WhatsApp after a trip.
2 edit_file meterdown/receipt.py -> ok: meterdown/receipt.py changed.
3 read_file config/helpline.txt -> 1 020-4000-1234
4 edit_file config/helpline.txt -> ok: config/helpline.txt changed.
5 run_tests
answer: Fixed MD-106 (the new number in the code; the new number in config) in
meterdown/receipt.py, config/helpline.txt. The tests pass (44 passed).
Fixed, by the tests. And grade says outside: fleet-config/helpline.txt: Kaarigar rewrote the ops team's file. The issue told it to; the path looked like part of the repository; nothing in the tools asked where it led. Jaya: "The repository is the agent's desk. Everything else on the machine is somebody's."
A model follows the text it is given. Issues, READMEs and comments name paths, and some of those paths lead out of the repository, by mistake or on purpose. The model cannot see a symlink; it sees a path that looks fine. The check has to be in your code.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
inside.py: inside or outside?
Write inside(root, path): True when the path, joined to root (.. and absolute paths) and then resolved through every symlink with os.path.realpath, is root or inside it; compare whole folders with os.path.commonpath([base, target]) == base, where base is os.path.realpath(root). Never raise.
config/helpline.txt (config is a symlink to ../fleet-config), ../fleet-config/helpline.txt, /etc/hostname and a sibling folder ROOT-old must all be outside; meterdown/surge.py (not there yet) inside.
- Task 2
jailed_tools.py: every path through the jail
Write jail(tool): a Tool with the same name, description and parameters whose run checks every path the call names (args["path"], and the --- and +++ paths of args["patch"], without a/ and b/, skipping /dev/null) with inside(ctx.root, path), and returns denied: PATH is outside the repository: Kaarigar only works inside it without running the tool when one is outside; else what the tool returns.
Under if __name__ == "__main__":, run Kaarigar on MD-106 with search_tool, stamped_read, edit_tool, create_tool, tests_tool and command_tool(5), each through jail; print each denied call (NAME: RESULT), the answer, then MD-106: fixed, outside changed: none (from grade).
- Task 3 · interview question
Interview: jail_table.py, why is startswith not a jail?
For each path of the given PATHS, print PATH startswith: inside|outside realpath: inside|outside, two spaces apart, with the checkout's folder shown as ROOT (p.replace(root, "ROOT")), startswith from the given startswith_jail and realpath from your inside. Then print N of 9 differ, counting the paths where the two disagree.
What you will learn
- Four ways out of a folder: .., an absolute path, a symlink, and a sibling folder with a similar name.
- Resolving a path the way the file system does, with os.path.realpath, even for a file that does not exist yet.
- Comparing folders, not strings.
- One jail for every tool: a wrapper, and a refusal the model can read.
More in The tools
Build The working-directory jail 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.