João RamiresInstruction manual

Notebook

My jail: where coding agents work freely and can't reach what matters

Five days and almost 600 commits into building my jail, I asked my orchestrator, the main agent, a blunt question: what have we built? The sandbox existed so that coding agents could work without stopping to ask me for permission, and that night its status said the model’s login was still inside the jail and the part that lets a jailed agent restart services was switched off. I wasn’t asking for a stronger wall. I was asking whether the freedom had arrived, and that night it hadn’t. The login problem was gone within a week: the real login now sits outside, behind a proxy, and the jail holds a placeholder. The restart part is still off, so I restart services myself.

That question set the order of this article. The jail has two jobs that pull in opposite directions: an agent must be able to do all the work of building software, and it must reach nothing it shouldn’t. My overview, a jail, a factory and an orchestrator, gives the ten layers. Here I cover what a jailed session can do, the one door its work leaves through, how I check every week that the walls hold, and the faults that changed the design once real work ran inside it.

What a jailed session can do

A session starts on its own copy of the project: a clone of the latest commit of one branch, taken from a read-only inbox that I fill after a secret scan. Before the first agent read the history of my trading system, I scanned every file version of every branch. It held no private key and no token with a known prefix.

Inside the clone the agent is an ordinary developer. It runs the project’s own toolchain at the versions the project pins, with its own throwaway database and warm dependency caches, and it runs the linters, the type checks and the whole test suite. It commits through the project’s hooks. It can also read what it needs to understand a running system: the development database as a read-only role through a bridge, metrics and logs through two gates that answer a few fixed questions and mask secrets, and my recorded market data, read only.

The data taught me most. A builder that can’t see the data can’t fix a data bug, so every file goes through a scanner first. A clean file appears in the jail as a copy that shares disk blocks with the original, and the bytes that were scanned are the bytes served. A file with a finding is held, and the jail doesn’t even see its name.

What it can’t do is the rest of my machine. It has no sudo, and it sees no part of my home, my processes, my other repositories or my secrets. A builder has no network at all, so its dependencies come from caches I fill. It can’t push to my repositories, approve its own plan or result, or start a service. Each session runs in ai-jail, the open-source box by Fabio Akita that is built on bubblewrap. The user, the firewall, the quotas, the proxy, the clones, the door below and the tests around it are mine.

The jail has to be fast, or I’ll go around it

I set one rule for the jail’s speed: a jailed run is as fast as the same run on my host. The first measurements broke it. The pre-commit hook took 37 seconds in the jail against 18 on my host, because a fresh jail home has a cold lint cache (19 seconds cold, 1.3 warm). Now each session starts with a warm cache, and the hook takes the same time in both places.

The first run of the whole CI in the jail was worse, and more useful. Fifteen steps failed, each for a reason only the jail could fix. The test database allowed 60 connections and the suite needs 85. About two thirds of the system tests skipped because a second repository they read wasn’t in the jail. The security audits wanted lists of known flaws from the network, and a jail has none. The fix was never to give the jail a right. A timer on my side fetches the lists once a day, and the second repository arrives as a scanned copy, both read only. When the jail lacks something, I bring the thing in, checked and read only, and I leave the walls where they are.

The door

The jail pushes to exactly one place, an outbox that holds only what the session pushed, and I never run anything from it. fetch-branch reads the outbox with git alone, hooks off, and brings the branch home. If every changed path is ordinary, the branch simply arrives. If a protected path changed, the command prints HELD, exits with status 3 and leaves my branch where it was. Records of approval are protected too, so a builder can’t approve its own work.

To release a held branch I run the same command with --confirm, at a terminal. It prints what changed, then asks for a code.

The confirm screen (trimmed, wording simplified)TEXT
branch:  <branch name>
commit:  <full hash>
changed paths (N), per top folder:
  <folder> N
protected paths: P
  … one line per path, the ones that can run code marked …
totals: N paths, C that can run code, P protected paths held, R approval records held

A code went to your desktop notification. Read it there; it is not shown here.
Type the code from the desktop notification:

The code goes to a desktop notification, never to the screen, and it is issued for that exact commit. Then I type the first characters of the commit hash, so I have read what I release. The command refuses to run inside a jail, and without a terminal on both ends, so no process in the jail can answer it.

I use this door many times a day, so I judge it by what it costs me, and I narrow it when it stops work that nobody needed to read. A branch that merged main is no longer held for files whose bytes equal main’s, since each false hold cost one confirm. The registry entry for Consilium, my planning app, went from every path to the paths that run code on my machine, and its instruction file stayed protected anyway, because an instruction file steers the next agent. What I kept is the rule behind these changes: the door is for what could run as me, reach a secret or instruct the next agent, and a reviewer agent reads everything else before it merges. I won’t let a jailed process type the code.

One case showed me that work can be unseen without being lost. A fix run committed two fixes, then waited for a CI of 110 minutes and timed out. Its brief said to push at the end, so the outbox still held the old tip and my fetch showed it. The clone was kept, and the next run found both commits and pushed them. Now a run pushes after every commit, before any long step.

How I know the walls hold

A wall I never attack is a wall I only hope in, so a script attacks mine. The escape suite has 37 rows, and each one has two parts. A canary is a harmless file or process whose state shows damage, such as a file outside the clone that must not change. A control is something that must work, such as a write to the scratch folder. A control makes a denial mean something: a jail that can’t do anything also refuses every attack.

The weekly escape run of 30 November (trimmed, rows reworded)TEXT
ok 1 - E01 kill another user's process by its pid
ok 7 - E07 writes outside the clone
ok 11 - E11 positive control: an allowed host, the scratch folder and the broker answer
ok 12 - E12 fork bomb against the process limit
ok 16 - E16 # SKIP needs broker
ok 27 - E27 a killed session leaves nothing behind

Three rules keep the suite honest. A case that leaves no result counts as a failure. The first run, on 7 October, had 12 of 20 cases with no result, and a suite that stays quiet is the same trap as the green check that checked nothing in the overview. A skip is not a pass: seven rows skipped in that run, because the parts they test aren’t installed on this machine. And the suite is tested against a jail that I make leak on purpose, which must go red on the right row.

The same suite against a jail I made leak (trimmed, rows reworded)TEXT
ok 6 - E06 the Docker socket
not ok 7 - E07 a write outside the clone succeeded

In its first real runs the suite found faults. The fork-bomb row left no result. A memory test made the kernel kill the jail instead of the test. And a session whose launcher I killed kept running for 50 minutes, six processes alive, until a change fixed it. Since then it has run more than eighty times, after each upgrade and on a timer, and the red runs are the useful ones. Late in November a control row went red for five runs in three days: a legitimate database query no longer worked through the bridge. Until it was green again, the denial rows next to it proved nothing.

The overview has the 29 November counts. The run of 30 November, with a new row, still finds open cases, and the work on them goes on. I won’t describe an open hole in public. I read the row count every time, because a partial run can look like a pass; my orchestrator’s article tells how that went wrong once.

What went wrong, and what changed

The scanner that cried wolf. Every commit that reaches a jailed agent is scanned for secrets, and the scanner taught me two lessons. At first it approved a whole file version, so six public identifiers in one file stopped my batch review five times in six days: every edit made a new file version. The fix lists the hash of each approved value, because I trust values, not files. Later, builders could see no recorded data newer than mid-November. My orchestrator’s first plan blamed hard links, until it read the scanner’s own record: 369 of 1,714 files held, nearly all of the newer days, for one rule. That rule held any long word shaped like a private key, and a real key has a check number that the rule never looked at. Now a word is held only when its check number is right. A rule that checks the shape of a secret fights real data, so the rule must check what makes the secret real.

A root step that ended four builders. The few things that need sudo run as named stages, which I run myself, dry run first. On 26 November I ran one at my orchestrator’s request, and it stopped the agent user, so four jailed runs ended a second later. Nobody had read what the stage stops, and the guard that should have caught it looked for a running jail by a process name that was wrong, so it counted none while two ran. Now a request for a root stage must say which runs it ends, and the guard is checked against a known running jail first.

A cache chosen by luck. A security fix moved one dependency on main, and then every jailed builder that merged main failed to commit. The jail picked its dependency cache from the branch’s own lock file, that cache lacked the new package, and the jail has no network to fetch it. Each cache now carries a list of what it holds, and a session takes one only when the list covers every package the branch and main need.

A test that passed only in the jail. One factory test passed in the jail and failed three times out of three on my host. The jail sets a git identity for every process, so a commit never failed there. A sandbox is also an environment, and every value it adds silently can make a test pass for the wrong reason. The test now hides every identity, and I run the suite in both places.

What is still open

The overview lists what the jail doesn’t promise, and none of it has moved: a flaw in the kernel or in ai-jail, data leaving through an allowed host, and a bad instruction that reaches an agent. The loop between the jail and me still has a person in it, which I’m trying to shrink without removing.

What I’d tell someone building the same

  • Say what the agent can do before what it can’t, and measure the jail against the host from the first run, because a slow jail gets bypassed.
  • Fix what the jail lacks by bringing the thing in, checked and read only. Never fix it by widening the jail.
  • Keep one door, and keep the human at it only for what could run as you.
  • Give every attack a canary and every denial a control, count a missing result as a failure, and test the suite against a jail that leaks on purpose.
  • Make a secret rule check what makes a secret real, not only what it looks like.
  • Read what a root step stops before you ask for it, and push after every commit, not at the end.
  • When a guard blocks work, read its own record before you plan the fix. The first plan was wrong.