Build your own coding agent (TypeScript) · 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
- TypeScript
- In the all labs plan
The first 2 chapters of Build your own coding agent (TypeScript) 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 src/receipt.ts
2 edit_file src/receipt.ts -> ok: src/receipt.ts 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 src/receipt.ts, 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.ts: inside or outside?
Write and export inside(root, path): true when the path, resolved against root (.. and absolute paths) and then through every symlink, is root or inside it. A path that does not exist yet resolves through the realpath of its nearest existing folder. Compare whole folders with relative(): inside when it is '', or not .., not starting with .. and a separator, and not absolute. Never throw.
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; src/surge.ts (not there yet) inside.
- Task 2
jailed-tools.ts: every path through the jail
Write and export jail(tool): the same tool (name, description, 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 (import.meta.main), run Kaarigar on MD-106 with searchTool, stampedRead, editTool, createTool, testsTool and commandTool(5000), 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.ts, 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 startsWithJail 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 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 (TypeScript) are free. This one comes with the all labs plan.