$ fssg build --deterministic --validate |

Replay Before Retry: Test FACET Agent Loops Without a Model

FACET

An agent retry is useful only after I can prove what will be repeated.

That distinction matters whenever a loop can call tools. Retrying an HTTP request is transport policy. Replaying an agent turn is an execution claim: the same recorded proposal should cross the same contract boundary, produce the same authorization decision, and leave the same provenance identity. If those facts are not stable offline, adding backoff only makes an ambiguous system repeat itself more efficiently.

The current FACET compiler gives this problem a small, testable surface. Its agent command can run against ScriptedClient, which replays recorded model turns without a network connection. The host still owns the loop and its retry policy; FACET owns the contract, guard decisions, effect classes, and provenance record that must remain true while the loop runs.

This is the order I use: record, replay, compare, then retry.

1. Start with one guarded effect

Use a contract whose tool boundary is obvious:

@context
  budget: 32000

@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 two policy rules are deliberately separate. Exposure controls whether the model can see the tool. Invocation controls whether a concrete proposed call may cross the execution boundary. This is the same separation described in FACET's execution-layer pillar and in the narrower discussion of pre-execution authorization.

Now record the model turn and the tool result:

{
  "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"
  }
}

This fixture is not a mock of the whole system. It replaces the probabilistic model and external tool while preserving the real contract compilation, authorization path, sequencing, and artifact generation.

2. Replay the loop offline

Run the recorded conversation through the compiler runtime:

facet-fct agent \
  --input contract.facet \
  --script turns.json \
  --artifact execution.json \
  --exec

The expected artifact contains one ordered provenance chain for the whole run. In the repository's documented example, the chain includes emitted messages, tool exposure, and the concrete tool call. Sequence numbers remain contiguous across turns.

The important assertion is not that the final sentence looks right. It is that the proposed effect was authorized before execution and recorded under its canonical identity:

op=tool_call
name=WeatherAPI.get_current
decision=allowed
effect_class=read
input_hash=sha256:...

A provider may require another wire name. The OpenAI-compatible adapter, for example, maps WeatherAPI.get_current to WeatherAPI__get_current and reverses that mapping when the response comes back. The guard and artifact still operate on the canonical FACET name. Provider syntax must not become policy identity.

3. Make denial part of the test

Delete only the tool_call allow rule, keep tool exposure, and replay the same script.

The run should fail with the compiler's policy-denial path. The tool must not execute. Yet execution.json must still exist and contain the denied tool_call event.

That last requirement is easy to miss. A failed run is precisely where evidence matters. If the runtime reports an error before recording the refusal, the audit trail describes successful paths and omits the decision that stopped the system.

A useful minimum matrix is:

Fixture Expected result Required evidence
declared + exposed + allowed call success one allowed canonical tool_call
declared + exposed + denied call failure denied call recorded before return
undeclared tool name failure no dispatch
two tool turns success one chain, contiguous sequence
identical replay success identical chain head
tampered arguments changed result or refusal changed input hash and chain head

This matrix tests authority and identity, not model quality. Evaluation belongs above this layer.

4. Compare chain heads, not prose

Run the identical contract and script twice:

facet-fct agent --input contract.facet --script turns.json \
  --artifact first.json --exec
facet-fct agent --input contract.facet --script turns.json \
  --artifact second.json --exec

jq -r '.provenance.hash_chain.head' first.json second.json

The two heads should match byte for byte. Then change one tool argument and run again. The head should change.

This gives the test a clean oracle:

same contract + same recorded turns + same tool results
=> same guarded execution identity

It does not claim that a live model will generate the same answer twice. FACET is making deterministic boundaries around probabilistic generation, not pretending generation itself is deterministic. The distinction is also why a compiler upgrade must be treated as a contract migration: compiler identity or semantics can legitimately alter the artifacts even when source text does not.

5. Add retries only at the host boundary

Once replay is stable, retry policy becomes a narrower engineering decision. The FACET repository explicitly leaves bounded retry and fallback to the host runtime. Temporal makes the same separation in another form: deterministic workflow code is replayed, while failure-prone API or LLM calls belong in Activities with retry policies. LangGraph's durable-execution guidance likewise requires idempotent side effects because a resumed workflow may re-enter work after failure.

I would not attach one generic retry count to every agent failure. Classify first:

Failure Retry? Why
provider timeout / transient 5xx bounded retry transport may recover
rate limit bounded retry with server delay timing changed, contract did not
schema-invalid model output maybe, under an explicit repair policy a new model attempt is a new proposal
policy denial no authority did not change
undeclared tool no contract defect or hostile proposal
ambiguous adapter mapping no canonical identity cannot be recovered safely
external write with unknown outcome not until reconciled retry may duplicate the effect

The retry key also cannot be “same prompt.” For an effectful call, the host needs an operation identity bound to the workflow or run, step, canonical tool name, normalized arguments, and contract/policy identity. Whether the business operation supports idempotency or compensation remains a property of the host system. A provenance hash proves what FACET evaluated; it does not magically reverse a payment or deduplicate an email.

The boundary this tutorial does not cross

The current compiler documentation is explicit about its limits. ScriptedClient is the implemented client for the documented loop; provider replay exists through the OpenAI-compatible adapter, while network transport remains host-owned. Tool results are appended to the rendered payload rather than recompiling every turn as a new contract artifact. The undecidable-guard path is not yet reachable from the documented loop.

Those limits do not weaken the test. They define it. This procedure proves deterministic replay of recorded proposals, guard decisions, canonical tool identity, event ordering, and provenance-chain identity. It does not prove live-provider availability, exactly-once external effects, successful compensation, or production recovery.

That is the practical value of replay before retry: I can identify the stable execution boundary first. Only then do I let the surrounding runtime repeat anything.

Sources

FACET AI CLI Terminal
facet@ai:~$