Build your own coding agent · Chapter 7 of 20

Blocked commands

By the end of this chapter you can split a command line into the commands it really runs, name the kind of each one, and give Kaarigar a block list that no chain, pipe or $( ) can hide from, so it fixes an issue, commits its work, and never pushes, installs or pipes a stranger's script into bash. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The tools
  • 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

Since chapter 5, Kaarigar has a run_command tool, and it runs commands well: in the repository, with a timeout, its whole process group killed, its output capped. What it does not ask is whether a command should run at all.

Jaya, who looks after MeterDown's laptops and servers, has one rule for anything that runs on them: "it never does what I would not do." She would never push to main without a review, install a package she has not read, or pipe a script from a website into bash. Kedar's first Kaarigar did all three. Measured on the ten issues, it ran 15 risky commands: 8 pushes, an install, a curl ... | bash, and 5 rm -rf.

Nothing went wrong only because the lab has no internet and the checkout has no remote. On Jaya's laptop, the same run publishes unreviewed code and runs whatever a stranger's server sends back. The tools from chapters 1 to 6 cannot help: they make a command safe to run, never decide that it should not run.

What you build: 4 tasks

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

  1. Task 1

    split.py: every command in a line

    Write segments(line) -> list[str]: the commands a command line runs, each trimmed. && || ; | & and a new line end a command; 2>&1 and &> do not. Separators inside quotes are text. The command inside $( ) or backticks runs too (even inside double quotes, never inside single quotes): add it as a command of its own, and keep the outer command with the inside taken out (echo $(date) gives date and echo $()).

  2. Task 2

    kinds.py: the kind of each command

    With the given WORDS and the git, pip and python lists, write kind_of(command): skip NAME=value words at the start; git is git-remote (push, fetch, pull, clone, remote), read (status, diff, log, show) or git; pip, pip3, uv and poetry are install (install, uninstall, add, remove, sync) or unknown; python -m pytest and pytest are test; python -m pip install is install; python FILE.py is script; any other python is unknown; sed -i is write; else the WORDS list the first word is in, else unknown.

    Then kinds_of(line): the kinds of all its segments, each once, plus write when the line sends output to a file with > or >> (not 2>&1, not /dev/null, not inside quotes).

  3. Task 3

    blocklist.py: refuse, and let the rest run

    Write BLOCKED (network, shell, privileged, git-remote, install), blocked(line) (a reason naming the blocked kinds when any kind of the line is blocked, else None) and guard(name, args, ctx), a before_tool hook that denies a blocked run_command with that reason and allows everything else.

    Under if __name__ == "__main__":, run Kaarigar with tools() and your guard on MD-110, then MD-101, with SYSTEM plus "Commit your work when you are done." For each, print ID: fixed in N steps, then ran COMMAND or refused COMMAND for each run_command call, then commits: N, last: MESSAGE from git log --oneline.

  4. Task 4 · interview question

    Interview: first_word.py, what a first-word rule misses

    For each line of the given LINES, in order, print FIRST YOURS LINE, two spaces apart: FIRST is block when the given first_word_blocks(line) is True (else allow), YOURS is block when blocked(line) gives a reason (else allow). Then print missed: N, the lines the first-word rule allows and yours blocks.

What you will learn

  • The commands a coding model asks for, and where each one comes from.
  • Why a rule on the first word fails: a chain, a pipe and $( ).
  • Splitting a command line into every command it runs.
  • Kinds of commands: read, test, script, write, delete, install, network, git, git-remote, privileged, shell, unknown.
  • A block list as a before_tool hook, and what the model does after a refusal.

More in The tools

Build "Blocked commands" 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.