ai
I built agent-guard to decide whether an agent's change should go live
A check-apply-verify harness for agent edits. It probes the live system first, applies the change, probes again, and rolls back with a fresh recovery probe when the change breaks something. 24 of 24 replayed scenarios land on the exit code they should.
- #ai-agents
- #evaluation
- #reliability
- #typescript
I'm a full-stack engineer looking for applied AI and agent-engineering work. My last post was about cue-bench, which asks whether a browser agent can finish messy work. This one is about the question that comes after: when an agent proposes a change, should that change go live?
That project is agent-guard. It's small: a TypeScript library with one function, guardedApply, plus a CLI that runs bundled checks against local fixtures.
The incident it comes from
A site edit silently broke contact-form delivery. The form still showed its success message, but the enquiries never reached the inbox, and they were lost.
The lesson I took is that "the page still works" and "the business outcome still happens" are different tests. An agent that edits a form can't be trusted on what the page shows. You have to submit an enquiry and look in the inbox.
The lifecycle
guardedApply takes four callbacks: baseline, apply, verify and rollback. It checks the live system before touching it. If that baseline fails, it stops and leaves the target alone, because you can't judge a change against a system that's already broken. If the baseline passes, it applies the change and probes again. If the new probe fails, or apply throws partway through a write, it rolls back and runs a fresh recovery probe. A rollback only counts as recovered once that probe passes.
Scroll sideways to read it. The labels stay full size.
I drew this with a statechart tool that writes the model in TLA+ and has the TLC model checker walk every path. TLC reached 32 states and 72 steps, and these rules held in all of them:
- A failing baseline never touches the target. Apply and rollback are never called.
- A change is kept only when
applyreturned and a fresh probe passed on the changed system. - An edit that threw or failed its check is never kept.
- Every failed edit ends either rolled back and verified, or flagged as a failed recovery. There's no quiet third ending.
- "Recovered" means the rollback ran and a recovery probe passed after it.
- The guard never ends on a good verdict while a probe would fail.
- Each callback runs at most once per call.
The model makes one assumption that matters: the probes are honest. The guard can only be as good as the check you give it. A probe that loads the page and says "looks fine" would have passed the incident above. That's why the bundled checks look for evidence outside the page: the email in the inbox, the entries in the webhook ledger, the body that arrives after a redirect.
Four ways a change can look fine and not be
The CLI ships with four adapters, each backed by a JSON check file. All of them run on loopback with synthetic fixtures.
- Mail. The bundled broken edit empties the list of email fields. The form still reports success, but no matching email arrives within 1.5 seconds. The guard restores the original form and confirms delivery with a new enquiry.
- Webhook. The edit adds a second event type. The guard sends event A, retries A, then sends a separate event B for the same lead. Every request returns HTTP 204, but the ledger ends up with three entries where there should be two.
- Conversions. Two enquiries are submitted and mail is withheld for one of them. Both forms report success. The broken edit records two conversions, but only one has a matching email in the inbox.
- Redirects. The original redirect is a temporary HTTP 307. The edit makes it a permanent 301, and Node's fetch follows a 301 as a GET, so the destination gets an empty body. A permanent 308 keeps the POST and its body, so that version is accepted.
In each case the success signal everyone watches (a 200, a 204, a thank-you message) stays green while the outcome breaks.
Exit codes are the interface
An agent or a CI job reads the exit code, not the log. So I gave each outcome its own code, and none of them means "probably fine".
Scroll sideways to read it. The labels stay full size.
The proposal is requested and validated inside the baseline step. That ordering matters. The model is only asked once the live system is known to be healthy, and an unusable proposal blocks the run before anything is applied. TLC checked this model too (23 states, 36 steps), and these rules held:
- Exits 1, 4, 5 and 64 never apply anything.
- Bad arguments don't touch the target, run the baseline, or call the provider.
- The model is asked only after the baseline passed, and at most once per run.
- Bundled checks never call the provider.
- Exit 0 means a candidate probe passed. Exit 2 means a recovery probe passed. Exit 3 means recovery didn't verify.
The repo's tests agree with the model. With the provider stubbed, the broken-baseline and missing-key cases both make 0 provider requests.
What I measured
There are two commands, and I ran both on the merged main branch on 20 November 2026.
npm test typechecks, then runs the CLI checks. That's 6 runs of the demo and baseline commands, 10 cases of the live path against a local provider stub, 18 outcome scenarios across the webhook, conversion and redirect checks, and a library test where apply throws after a partial write. Every case printed the verdict and exit code it expected, and the command exited 0.
npm run eval replays every bundled scenario and rewrites docs/eval.md. It replayed 24 scenarios, and all 24 matched their expected verdict and exit code.
Most of the scenarios are deliberately bad. Only 5 of the 24 are edits that should be kept, and 1 is a clean baseline. The other 18 are broken systems, broken edits, failed recoveries and malformed proposals. The harness is there to catch those.
One live run, and what it does not prove
The repo also keeps one recorded run against a real model, gpt-6-luna. The task was to add an optional Company field to the form and include it in the email. The proposal kept the original fields and email order, and appended Company to both. The guard submitted three enquiries: one on the original form, one on the edited form with a company, and one on the edited form without one. The emails matched each time, and the run ended with EDIT ACCEPTED and exit 0.
That's one proposal. It shows the live path works end to end. It says nothing about how often a model gets this right, and the README says so.
Rough edges
- A rollback can't bring back an enquiry that was already lost. The guard shortens the window, but it doesn't close it.
- Your callbacks own the snapshot, the permissions, the timeouts and the write coordination. If the process crashes mid-recovery, the library can't help.
- The standalone
baselinecommand exits 2 on bad arguments. It's documented, but it collides with "rejected, recovery verified", and I'd move it to 64 like the rest of the CLI. - I skipped the MCP wrapper for now. The next step is a staging deployment example with a persistent snapshot and an independent health check. An MCP tool could then expose that one guarded deployment to an agent.
How it fits with cue-bench
cue-bench and agent-guard come from the same habit. Neither one trusts the agent's account of what happened. cue-bench grades a run from the stock ledger, not from the model's memo. agent-guard keeps a change only if the inbox, the ledger or the destination shows the outcome still happens. One tells you whether an agent can do the work. The other decides whether its work is allowed to stay.
How to run it
With Node 22.18 or later:
npm ci && npm run demo
The demo runs offline on loopback and needs no API key. npm test runs the checks above, and npm run eval regenerates the scenario report. To guard your own change, run npm pack, install the tarball, and pass your four callbacks to guardedApply.
I'm interested in applied AI and agent-engineering roles. agent-guard is a small, tested harness for one specific job: making sure an agent's change does what the system needs, not just what the page shows.
Related posts
Continue with articles that overlap in category or shared tags.
ai
I built Cuqe so a small local model can say no to an agent's action
A 0.6B verifier that runs on a Mac, scores whether a proposed action matches the request, and declines anything under a calibrated cutoff. In archived development runs it kept 39 of 48 correct actions, declined 44 of 48 when the right action was missing, and answered at 365 ms p95.
Read related postai
I built cue-bench to score browser agents on messy retail work
A local Playwright workbench, matched replays, and a ledger grader. 172 receiving attempts, zero recovery passes, and one run that posted 13 ink jackets and zero moss jackets.
Read related postai
I built CDM to test small models on historical BTC data, and better probabilities were not better direction calls
A research lab that tests whether small local models and simple rules extract useful signals from historical crypto data. On 501 held-out decisions across 84 dates, pooled probability scores improved. Direction stays unproven. A first Qwen3 0.6B fit predicted flat on 1,031 of 1,034 windows, and no after-cost trading result has been established.
Read related post