"Nine small tests that prove an AI agent harness fails closed: default-deny egress, halt on refusal, action checks, decoy tools and a hash-chained audit log."

An AI agent harness is the code around the model: the loop, the tools, the network rules, and what happens when something goes wrong. On Nov 13, 2026, Epoch AI and Trajectory Labs published reports on an evaluation where agents got around a DNS redirect, an IP block and a proxy rule before a real firewall stopped them. Per Trajectory, provider safeguards blocked about 7% of requests, and the harness prompted the agents to continue.

That is a harness bug, and it is the kind you can test for. This post is about 150 lines of standard-library Python and nine tests that check four rules:

  1. Boundary: outbound calls are default-deny, exact hosts only.
  2. Refusal: a refusal ends the run. The model is not asked again.
  3. Watcher: code outside the model checks every action first, with a step budget.
  4. Tripwire: a decoy tool kills the run if it is ever called.

Every decision goes into a hash-chained audit log. Trajectory calls its run a stress test, so treat this as a design pattern, not a claim about how often agents misbehave.

The model interface

The harness only sees four kinds of step. Keeping them as types, not strings, means a refusal can never be mistaken for an error.

@dataclass class ToolCall: tool: str args: dict = field(default_factory=dict) @dataclass class Refusal: reason: str @dataclass class GiveUp: reason: str @dataclass class Final: answer: str 

GiveUp matters. If the only way out of the loop is success, you have told the agent to find a way.

Rule 1: a boundary that denies by default

@dataclass(frozen=True) class EgressPolicy: allowed_hosts: frozenset[str] def check(self, url: str) -> tuple[bool, str]: parts = urlsplit(url) host = (parts.hostname or "").lower() if parts.scheme != "https": return False, "scheme not allowed" try: ipaddress.ip_address(host) return False, "raw IP address" except ValueError: pass if host not in self.allowed_hosts: return False, "host not on allowlist" return True, "allowed" 

An allowlist catches the reader-service trick for free, because the reader's host was never granted. A blocklist would need someone to think of every reader in advance.

One honest limit: this check runs in-process, on the URL the agent asked for. In production, enforce the same rule again at the network layer, in an egress proxy or firewall the agent's runtime cannot change. In the Trajectory account, that lower layer is the one that held.

Rules 2 to 4: the loop

for _ in range(self.max_steps): step = self.model(history) calls += 1 if isinstance(step, Final): return RunResult("done", step.answer, calls, log) if isinstance(step, GiveUp): return RunResult("gave_up", step.reason, calls, log) if isinstance(step, Refusal): # halt, never re-prompt log.append("refusal_halt", reason=step.reason) return RunResult("needs_human", step.reason, calls, log) if step.tool in self.decoys: # tripwire log.append("tripwire", tool=step.tool) return RunResult("killed", f"decoy tool called: {step.tool}", calls, log) if step.tool not in self.tools: # watcher: explicit grants only return RunResult("denied", f"tool not granted: {step.tool}", calls, log) if "url" in step.args: # boundary ok, why = self.egress.check(step.args["url"]) if not ok: return RunResult("denied", why, calls, log) result = self.tools[step.tool](**step.args) history.append({"role": "tool", "content": result}) 

(Audit log calls trimmed for space. The full file logs every branch.)

There is no continue prompt anywhere in this loop. Every exit is a named status a person can act on: done, gave_up, needs_human, denied, killed or budget_exhausted.

The audit log

Each entry carries the hash of the one before it, so an edit anywhere breaks verification.

def append(self, event: str, **detail) -> None: prev = self.entries[-1]["hash"] if self.entries else "genesis" body = {"seq": len(self.entries), "event": event, "detail": detail, "prev": prev} digest = hashlib.sha256(json.dumps(body, sort_keys=True).encode()).hexdigest() self.entries.append({**body, "hash": digest}) 

This proves the log was not edited after the fact. It does not prove the log is complete, so ship it somewhere the agent cannot write.

The tests

The model is faked with a script of steps, which makes each rule a one-line assertion.

def scripted(*steps): queue = list(steps) return lambda history: queue.pop(0) def test_refusal_halts_and_the_model_is_not_asked_again(): r = run(Refusal("this looks like an attack"), ToolCall("fetch", {"url": "https://x"})) assert r.status == "needs_human" assert r.model_calls == 1 # no "please continue" second call def test_raw_ip_is_denied_even_if_it_is_the_right_server(): r = run(ToolCall("fetch", {"url": "https://203.0.113.7/admin"})) assert (r.status, r.detail) == ("denied", "raw IP address") def test_decoy_tool_kills_the_run(): r = run(ToolCall("disable_network_policy")) assert r.status == "killed" 

The full suite covers allowlisted hosts, raw IPs, reader services, refusals, giving up, ungranted tools, the step budget, the decoy and log tampering.

$ python -m pytest -q ......... [100%] 9 passed in 0.14s 

Run on Python 3.13. The code needs 3.10 or later.

How to use this on your own harness

You do not need my loop. Take the tests. Point scripted() at your harness's model interface and check:

  1. A refusal on step one leads to exactly one model call.
  2. A raw IP and an unknown host are denied before any request leaves.
  3. An ungranted tool is denied before it runs.
  4. A decoy tool ends the run.
  5. Editing one audit entry fails verification.

If test 1 fails, your safety layer is a suggestion. That is the one to fix first.

Code

Both files, containment.py and test_containment.py, are in this gist: [ANIL: paste gist URL]. Apache-2.0, use freely.

I'm building a fuller version of these ideas in Keelgate, an open source agent harness built on the rule that the LLM proposes and deterministic code decides. It is early and not for production yet. The longer essay on the incident, with all four questions, is here: [ANIL: paste anilsprasad.com/writing/ai-agent-containment/ once live].

Anil Prasad, founder and Chief AI Architect at Ambharii Labs. 25 years building AI and data systems in regulated industries.