Harness engineering · Chapter 14 of 23

Sandbox and dry runs

By the end of this chapter you can say what a command the model runs may touch and build it that way: the checkout as its folder, an environment with nothing secret in it, a timeout that stops everything the command started, and a cap on what comes back. You can also run the agent in a dry run, where nothing in the checkout changes and the run ends with a diff, and apply that diff later, only if the files have not moved on. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Control
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Harness engineering are free. This one comes with the all labs plan.

Why you need this

Amit looks after Hisaab's servers, the payment gateway's secret and the bill. Chapter 13 gave him rules: rm * denied, tests/** read-only, .env never read. He likes the rules, and he still has two worries.

The first is what a command can reach after a rule lets it through. python -m pytest -q is allowed, and it is a program like any other: it runs with everything your harness has. Your harness has the lab model's key in OPENAI_API_KEY, and like many apps it reads .env for its own settings, so it also has HISAAB_GATEWAY_KEY_SECRET. Every command it starts inherits both. A test file, a package's install script or a line the model was tricked into running can print them, or send them anywhere.

The second is Farah's request. Before the agent touches the real repository she wants to see what it would change, read the diff, and only then say yes. The harness you have cannot do that: every edit_file writes the file the moment the model asks, and every command runs for real. A rule says yes or no to one call; it cannot say "do all of it, but on paper".

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    clean_env.py: what a command may touch

    Amit wants every command the model runs kept away from the harness's secrets. In clean_env.py, write sandbox_env(root): a new dictionary with only PATH, LANG and TERM from os.environ (those it has) and HOME set to str(root). Write run_sandboxed(root, command, timeout=30, cap=4000): run the command with a shell in the checkout, with that environment, stdout and stderr together; past timeout seconds, stop it and everything it started and return a text saying it timed out; else return shape(output, cap) (from agent) and then "\n(exit code N)". Write class Sandboxed(Workspace) whose run_command uses run_sandboxed.

    Under if __name__ == "__main__":, make a checkout of T08, load its .env into os.environ, run env | cut -d= -f1 with Workspace(root).run_command and with Sandboxed(root).run_command, and print the names with KEY, SECRET, TOKEN or PASSWORD in them, sorted and comma-separated (or none):

    plain: GPG_KEY, HISAAB_GATEWAY_KEY_ID, ...

    sandboxed: none

    Then run run_agent on T08's issue with Sandboxed(root), grade it and print T08: passed (5 steps), with the run's steps.

    Careful: with shell=True, the shell starts the command as a second process. A timeout that kills only the shell leaves it running.

  2. Task 2

    dry_run.py: changes on paper

    Farah wants to see what the agent would change before it changes anything. In dry_run.py, write is_read_only(command): True when the command is, or starts with followed by a space, one of python -m pytest, pytest, ls, cat, head, tail, grep, wc, git diff, git status, git log, git show, and has none of ; & | < > ` $ or a newline in it.

    Write class DryRun(Workspace) with overlay (a dictionary: path relative to the checkout to text) and refused (a list). text(path) returns the overlay's text if the run changed the file, else the file's; save(path, text) puts the text in the overlay and never writes the checkout; files() also lists the overlay's new files. run_command(command): not read-only, add it to refused and return {"error": "dry run: ..."} as JSON, saying why; read-only, run it in a temporary copy of the checkout with the overlay written into it. diff(): a unified diff (difflib) of every overlay file against the checkout, with the names a/PATH and b/PATH.

    Under if __name__ == "__main__":, make a checkout of T09, print checkout: PATH, run run_agent on T09's issue in a DryRun with system=NAIVE_PROMPT, then print refused: COMMAND for each refused command, the diff, and last checkout diff: N lines (the lines of workbench.diff(root)).

    Careful: key the overlay by the resolved path relative to the checkout, so ./hisaab/upi.py and hisaab/upi.py are one file.

  3. Task 3

    apply.py: apply a dry run later

    In apply.py, write save_plan(dry, file): write a JSON file with one entry per file whose text changed in the dry run, {"PATH": {"before": the checkout's text, or None for a new file, "after": the overlay's text}}, and return the list of those paths. Write apply_plan(root, file): read the plan and check every file first; if any file's text in root is not its "before", write nothing and raise ValueError naming those files. Else write every "after" and return the list of paths written.

    Under if __name__ == "__main__":, make a checkout of T01, run run_agent on T01's issue in a DryRun (from dry_run), save the plan to a file next to the checkout (not inside it), and print, with the grades from workbench.grade:

    plan: hisaab/money.py

    before apply: T01 failed

    applied: hisaab/money.py

    after apply: T01 passed

    Careful: a plan written over a file someone changed since the dry run undoes their work. Check every file before writing any.

  4. Task 4 · interview question

    Interview: leak.py, a dry run that still writes

    "Your dry run passed, and the checkout changed. How?" teammate.py (given) has a teammate's dry run: edits in an overlay, commands with rm, mv or > refused, every other command run in the checkout. In leak.py, write LEAKS: at least three commands with no rm, mv or > in them, each of which changes a file in the checkout when TeammateDryRun runs it.

    Under if __name__ == "__main__":, for each command in order, run it with TeammateDryRun(root).run_command on a fresh clean checkout (workbench.checkout(None)) and with your DryRun on another, and say whether each checkout changed (workbench.diff(root) is not empty). Print the teammate's result, yours and the command, two spaces apart:

    leaked held touch notes.txt

    then the totals:

    TeammateDryRun: 3 of 3 leaked, DryRun: 0 of 3

    Careful: one fresh checkout per command and per dry run, or one command's change shows up as the next one's.

What you will learn

  • What a command may touch: its folder, its environment, its time and its output.
  • Why an allowlist beats a filter for the environment, measured on the lab's own variables.
  • A timeout that stops a command's children too, not just the shell.
  • A dry run: an overlay the tools read and write, commands that could change files refused, a diff at the end.
  • Applying a dry run later, and refusing when the files changed since.

More in Control

Build "Sandbox and dry runs" yourself

Hisaab, a Lucknow invoicing startup, wants a coding agent to clear its bug list. You build its harness 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 are free. This one comes with the all labs plan.