Nahum LitvinKubernetes, containers and sandboxes, from the production side
Agent tooling8 min read

137 steps, a model in 3 of them: turning agent work into a Go CLI

My coding agent used to run my infrastructure upgrades from a skill file, a little differently every time. Now a Go CLI runs them, and the agent's job is to run the CLI and fix it when it breaks.

My coding agent used to do my infrastructure upgrades by hand. It read a long skill file, ran the commands, judged the output and wrote the PRs. It did it a little differently every time. Two weeks ago I started moving that work into a Go CLI. Today its flows have 137 steps, and a model appears in 3 of them. The agent still runs every upgrade. It just runs the CLI now, and when the CLI is wrong, it fixes the CLI.

I rebuilt the pattern from scratch as my own open source software factory, cli-over-llm. The binary is yacol, yet another CLI over LLM. Its sample job is labeling release notes, so it runs on any laptop. I never type its commands. I ask the agent in plain words, and the agent runs yacol.

The rule

The rule came from me, mid-session. The agent had proposed one more LLM "repair round" to fix labels its last LLM call guessed wrong. I wrote:

Prompts and LLMs are manual work, and very expensive. They are the fallback for things code cannot handle.

A model call costs money every time. It starts fresh, with no memory of the last run. Code does the same thing every time, and a rerun is free. So the order is fixed. Code goes first. The model gets only what code can't answer. Each model answer is a bug report against the code.

Rules first, model last

The example repo labels release note lines as breaking, deprecation, dependency, feature or fix. That's a job people paste into a chat window. Here, regex rules in a YAML file answer first. The model only sees lines no rule matched, and only with -model. Its answer must be one of the same five labels. Anything else is an error.

func Order() []string {
	return slices.SortedFunc(maps.Keys(Registry), func(a, b string) int {
		return cmp.Or(cmp.Compare(rank(a), rank(b)), cmp.Compare(a, b))
	})
}

Every classifier registers with a class, Code or Model, and the zero value counts as Model. Code always runs first. A line nothing can answer is unknown, and the CLI exits 1. A pipeline never reads "no answer" as "nothing breaks".

The corpus file carries a cap: max_model_share, the fraction of test cases only the model can answer. A test fails when the share goes over it. yacol ratchet lowers the cap when a new rule takes cases from the model. Raising it by hand is possible, but it's a one-line diff a reviewer can't miss.

The agent runs the CLI, and fixes the CLI

This is the part that changed how I work. The agent's instructions say: you operate the CLI, you do not do its job.

flowchart TB
  subgraph run["agent runs the CLI"]
    direction LR
    a["yacol run"] -->|exit 0| b["report as printed"]
  end
  subgraph fix["the CLI failed"]
    direction LR
    c["yacol case: red"] --> d["one rule"] --> e["yacol check"]
  end
  run -->|unknown or wrong| fix
  fix -->|rerun| run

When an upgrade went wrong without hurting anything, my instruction was short: fix the CLI and rerun it. Never finish the step by hand. A step finished by hand is a step the next run will need a human for again.

The example repo caught this on its first real run. I pointed it at a sample changelog. It labeled the Markdown heading ## v2.0.0 as an unknown release note. The fix went through the loop. A red test with that exact line, a two-word change to skip headings, green, rerun.

A failed step is where the rerun starts

The loop only pays off if a rerun is cheap. So a job is a flow, a YAML list of steps:

steps:
  - {id: notes, kind: triage, args: {file: examples/patch-notes.md, block: breaking}}
  - {id: deploy, kind: exec, cmd: [echo, deploying]}
  - {id: soak, kind: soak, cmd: ["true"], args: {every: 1s, for: 3s}}
revert:
  - {id: rollback, kind: exec, cmd: [echo, rolling back]}

yacol flow writes each finished step to a state file next to the flow. Say notes fails on a line no rule knows. The agent fixes the CLI and runs the same command again. Finished steps print skip, and the run picks up at the failed step. Edit a step and it runs again: the done mark is a hash of the whole step. Every step's arguments are checked before the first one runs. A typo in step 3 can't leave steps 1 and 2 half applied.

The last step is a soak. One green check right after a deploy proves little. A soak runs its check every interval for the whole window. One red check fails the step.

A flow that changes something has to say how it can go wrong before it loads. Every step kind is read-only or mutating, and a kind with no class counts as mutating. A flow with a mutating step needs a revert list and an STPA block. For each mutating step it names the unsafe type, when it happens, and the step that guards it. STPA has four types: not provided, provided, wrong timing, wrong duration.

stpa:
  hazard: users run a release that breaks them
  unsafe:
    - {action: deploy, type: provided, when: the notes carry a breaking change, guard: notes}
    - {action: deploy, type: wrong-duration, when: it fails minutes after it looked healthy, guard: soak}

Leave one out and yacol flow refuses the file. When I put STPA questions in my agent's prompt, they tied a plain checklist except on timing. Here they're a schema the flow has to pass, and the timing and duration types sit next to every change. -dry-run runs only the read-only steps. Each outcome of the example flow is a replay golden: success, resume, breach, revert and more. The flow runs against recorded command results, and the transcript is a file in the repo.

I measured it

Writing rules is work, so I wanted a number. I had the model label all 17 test lines 3 times each with claude -p. Every answer, cost and duration went into a recording. yacol bench replays it against the rules.

yacol bench: the model was right every time it answered, cost $5.35 and 4 minutes of call time per pass, and failed 3 of 51 calls. Rules cost $0, ran in under a millisecond, and left 2 lines unknown. Seven rules cut the model's share from 100% to 12%.

The model surprised me. It was right every time it answered, and it never gave one line two different labels. On this corpus the case for code isn't accuracy. A pass cost $5.35 and 4 minutes of model call time, against nothing and under a millisecond. Most of the 31 cents per call is my agent context, which claude -p loads on every call. And 3 of 51 calls (6%) came back with no answer. Each rule takes its lines from the model for good. Seven rules took it from every line down to 12%.

Maintenance is subcommands too

The same rule applies to the repo itself. A skill file that says "run lint, then tests, then coverage" is prose. An agent reads it some of the time. So those steps are subcommands of the binary:

CommandDoes
yacol case LABEL LINEAdds a test case, exits 1 when no rule covers it, and drops the model flag once one does
yacol ratchetLowers the model-share cap to the current share
yacol checkLint, tests, 90% coverage floor, deadcode, govulncheck

CI runs go run . check. So does the pre-push hook. So does the agent. One list of gates, in Go, with tests. The skills shrank to "run this, act on the exit code". Two things still come from outside the binary. One is the right label for a new line, from me or a model answer I confirmed. The other is the regex for the new rule.

yacol check failed twice on its first day. Once on an unchecked os.RemoveAll, once on coverage at 88.6% under the floor. Both were real.

Packages that can't lean on each other

A CLI that replaces judgment has to stay boring. So the package lines are enforced:

  • depguard rules make imports one-way. The rules package can't import os. The maintenance package can't import the model package or the runner that calls it, so no maintenance command reaches a model.
  • An architecture test walks the AST and fails if any package other than its owner names claude or go as a binary, or imports os/exec.
  • A golden file lists every classifier with its class, so adding one is a one-line diff to review.
  • One exec function starts every process, so the timeout default lives in one place. In Go, a zero timeout means already expired, not no timeout.
  • deadcode must print nothing.

Where I was wrong

In the real CLI, I added a lint rule banning exec.Command outside the one exec function. Lint said 0 issues, and I almost took that as proof.

So I dropped in a throwaway file that breaks the rule, and ran the linter again. Still 0 issues. An older exclusion, meant for two other patterns, had switched off the whole linter outside two packages. golangci-lint excludes per linter, not per pattern. One text: line fixed it. Now every new rule gets broken on purpose once before I believe it.

What I kept

The agent didn't get less useful. It moved up a level. It used to be the worker. Now it's the operator and the maintainer. It runs the CLI. When the CLI can't do something, it teaches the CLI. Each fix makes the next run need it a little less.

Nahum Litvin, Lead Platform Engineer.

Comments