How to Test an AI Agent Loop Without Calling a Model
An agent loop should be testable before a real model, a real tool, or a network connection enters the room.
Most agent tests start too late. They call a provider, wait for probabilistic text, and then judge whether the final answer looks acceptable. That may test a model integration. It does not prove that the loop enforced policy before a tool call, preserved the order of decisions across turns, or recorded a refusal when execution stopped.
FACET's compiler now contains a narrower and more useful test boundary: a scripted model client. It replays recorded turns through the actual agent loop. The model is replaced, but the loop is not. Tool exposure, tool-call authorization, tool results, multi-turn sequencing, and the execution artifact still follow the runtime path.
This is the distinction I care about: a mock should remove an external dependency without removing the behavior under test.
Freeze the conversation, keep the control path
A scripted run uses three inputs:
- a
.facetcontract that declares interfaces and policy; - a JSON script containing model turns and tool results;
- an output path for the execution artifact.
The repository documentation uses a weather tool because its behavior is easy to see:
@interface WeatherAPI
fn get_current(city: string) -> string (effect="read")
@policy
allow:
- id: "expose-weather"
op: "tool_expose"
name: "WeatherAPI.get_current"
effect: "read"
- id: "call-weather"
op: "tool_call"
name: "WeatherAPI.get_current"
effect: "read"
@system
tools: [$WeatherAPI]
content: "Use WeatherAPI.get_current when asked about weather."
@user
content: "What is the weather in Minsk?"
The model side becomes a fixture:
{
"turns": [
{
"tool_calls": [
{
"id": "c1",
"name": "WeatherAPI.get_current",
"arguments": { "city": "Minsk" }
}
]
},
{ "text": "It is raining in Minsk." }
],
"tool_results": {
"WeatherAPI.get_current": "Rain, 11C"
}
}
Run it without a provider call:
facet-fct agent \
--input contract.facet \
--script turns.json \
--artifact execution.json \
--exec
The fixture determines what the model asks for. FACET still determines whether that request may become an effect. This is not a snapshot of a chat transcript. It is a deterministic stimulus for the execution boundary.
Test the refusal, not only the happy path
The most valuable change to the contract is often deletion. Remove the tool_call allow rule while leaving tool_expose intact. The model can still see the tool and request it, but the runtime guard must refuse the call.
That produces a stronger regression than "the answer contains rain":
expected exit: failure
expected error: F454
expected tool execution: none
expected artifact: written
expected tool_call decision: denied
The last line matters. A failed run is not an excuse to lose evidence. FACET records the denied operation before reporting the error, so the artifact explains where the run stopped. A system that logs only successful effects creates a clean-looking but false history.
This complements FACET Policy as Executable Authorization. Policy describes the decision logic; the scripted loop proves that the decision happens on the real path before initiation.
A useful test matrix is small:
| Fixture | Expected outcome | Property under test |
|---|---|---|
| declared tool + matching allow | call executes | allowed path reaches the handler |
| declared tool + no call rule | F454 |
denial stops before execution |
| undeclared tool name | failure | the model cannot invent capabilities |
| two tool-call turns | one contiguous sequence | provenance spans the whole run |
| identical replay | identical chain head | the artifact is reproducible |
No semantic judge is needed for these assertions. They concern names, order, decisions, and hashes.
One run needs one chain
Multi-turn systems often fragment evidence by storing each exchange as a separate log record with no stable relationship to the next. That makes an individual event inspectable but leaves the run ambiguous.
The FACET agent-loop tests assert a contiguous seq across turns. Message emission, tool exposure, and tool calls land in one provenance chain. Replaying the same contract and script should reproduce the same chain head byte for byte.
This is a more specific guarantee than canonical request identity. Request and execution artifacts answer different questions: the request artifact freezes what was prepared; the execution artifact freezes guarded decisions that occurred as the loop advanced. The scripted client tests the second artifact without making model availability part of the test.
It also exposes a practical review surface. When a policy change alters a replay, the reviewer can ask:
Which event changed?
Did its canonical name change?
Did the effect class change?
Did allow become deny—or did evaluation become undecidable?
Did sequence continuity survive?
Those are better questions than "why did the model answer differently?" because they point to code and contract changes that can be inspected.
Provider recordings belong behind the same boundary
The current reference implementation also accepts recorded OpenAI-compatible responses. FACET renders the request through the adapter, reads the recorded provider shape, reverses provider tool-name mangling, and applies the guard to the canonical name.
For example, an adapter may send WeatherAPI__get_current because the provider cannot accept a dot. The execution artifact must still record WeatherAPI.get_current. Authorization belongs to canonical identity, not to a vendor-specific spelling.
This gives adapter tests an important invariant: translation may change representation, but it must not change what policy is deciding about. I developed the broader rule in Why AI Provider Adapters Must Not Repair Semantics. Scripted provider recordings make that rule executable at the loop boundary.
What this proves—and what it does not
This test proves that, for a fixed contract and fixture, the reference path applies guard decisions before tool execution, records allowed and denied attempts, keeps event sequence across turns, and can reproduce the provenance chain.
It does not prove that a live provider will emit the same turn, that an external tool returns correct data, or that the final answer is useful. The current documentation also names two implementation limits: the built-in client is replay-oriented rather than a network transport, and tool results are appended to the rendered payload instead of recompiling the document on every turn.
Those limits define the next integration tests rather than weakening this one. Transport, external effects, and semantic quality need separate test layers. The scripted loop should stay beneath them because it answers a precise question: if the model proposes this sequence, does the system enforce the contract correctly?
I do not want agent testing to begin with intelligence. I want it to begin with causality. Freeze the proposals, replay the loop, and prove which effects were allowed to cross the boundary.