# Which invariants prevent unsafe tool-call sequences when agent state is only partially observable **Date of Record:** 2026 November 14 **Scope:** Applies to safety-critical agents whose tool calls mutate external state (filesystems, payment rails, infrastructure control planes) and whose verifier observes only the tool-call trace plus a sampled state projection, not the full ground-truth state. Does not apply to agents operating in fully observable, transactional sandboxes where every mutation is reflected atomically in the verifier's view. ## Core Distinction The load-bearing distinction is between **trace invariants** (properties over the ordered sequence of tool calls, checkable from the log alone) and **state-transition invariants** (properties over the actual world state before and after each call). Most verification suites conflate these because in a fully observable system they coincide: if the trace says `write(path, X)` and the state projection shows `X` at `path`, the trace invariant "no write precedes an authorization check" and the state invariant "no unauthorized content reaches the filesystem" are equivalent. They are not equivalent under partial observability. A tool call can *appear* to satisfy a trace invariant while its state transition violates the corresponding state invariant, because the agent's model of the world is stale, the tool is non-idempotent, or the state projection lags the mutation. The failure is not that the agent reasons badly; it is that the verifier certifies a trace whose state semantics it cannot see. This is the boundary: **trace invariants are necessary but never sufficient; state-transition invariants are sufficient but only checkable where the projection is fresh and complete.** ## What Was Observed In a deployment where an agent issued `delete_resource` and `create_resource` calls against a shared registry, the verifier enforced a trace invariant: *no `delete` on resource R may be followed by a `create` on R without an intervening `confirm_owner(R)` call.* The trace was clean. Every delete was followed by a confirm before the recreate. The verifier passed the run. The state transition that actually occurred: the `confirm_owner` tool read from a cached ownership view that had a 90-second TTL. The agent's delete had invalidated the authoritative record, but the cache still returned the prior owner. The confirm succeeded against stale data, the create re-bound the resource to an owner who no longer held it, and the registry entered a state where two principals believed they owned the same resource. No trace invariant could have caught this, because the trace was *correct*. The violation lived entirely in the gap between the cached projection and the authoritative state. A second observation: replaying the same trace against a fresh cache produced a different outcome (confirm failed, create blocked). The invariant's truth value depended on projection freshness, not on the trace. This is the signature of a state-transition invariant masquerading as a trace invariant. ## Condition of Validity A trace invariant is valid as a safety guarantee only when the following holds for every tool call in the sequence: the state projection used by the verifier at call time is **fresh** (bounded staleness below the mutation-to-read window) and **complete** (every field the invariant quantifies over is present, not defaulted). Formally, for a trace invariant `I` over calls `c_1..c_n` and a projection function `π`, `I(c_1..c_n)` implies the desired state property only if `π(s_t)` equals `s_t` for all `t` at which `I` is evaluated. When this condition fails — as it did with the 90-second cache — the invariant is not wrong, it is *inapplicable*, and treating it as satisfied is the error. The correct operational posture is to downgrade the trace invariant to a *necessary condition* and require an independent state-transition check (a read-back against the authoritative store, or a monotonic version counter) before the transition is committed. ## Decision Boundary The decision boundary is whether the verifier can observe the post-condition of a call through a channel that is causally downstream of the mutation and not served from a cache that predates it. If yes, enforce the state-transition invariant directly and treat the trace invariant as a redundant fast-path. If no, the agent must be constrained so that the unsafe transition is *unreachable by construction* — e.g., forbid the `delete`→`create` cycle entirely, or require a version-counter bump that the cache cannot forge — rather than relying on a confirm step whose own freshness is unverified. Concretely: a `confirm` call whose backing read may be stale is not an authorization gate; it is a trace-decoration. Gates must be anchored to a monotonic, non-cacheable witness (a version number returned by the mutating store, a transaction ID, a lease token with a validity window shorter than the mutation window). Without such a witness, the invariant should be rejected at verification-design time, not trusted at run time. ## Operational Effect The operational consequence is that verification coverage must be reported as a pair — `(trace_invariants_enforced, state_invariants_witnessed)` — and a run where the second set is empty must be flagged as *unverified*, not *passed*. In the registry incident, the run was reported as passed because the verifier's output schema had a single boolean. Adding the second dimension would have surfaced the empty state-witness set immediately. The second effect is on replay: any trace invariant whose truth depends on projection freshness must record the projection version at each evaluation, so that replay can detect when the invariant was satisfied only by a stale read. A trace that passes under one cache state and fails under another is a latent hazard, and the verifier should refuse to certify it until the dependency is made explicit. The following guard encodes the boundary: it refuses to treat a trace-level confirm as a state-level authorization unless a monotonic witness is present and fresh. ```python import time from dataclasses import dataclass class StaleAuthorizationWitnessError(RuntimeError): """Raised when a trace-level confirm is presented as a state-level gate but its backing witness is stale, missing, or non-monotonic.""" pass @dataclass(frozen=True) class Witness: version: int issued_at: float source: str # must be the authoritative store, not a cache MAX_WITNESS_AGE_S = 5.0 def require_fresh_witness( witness: Witness | None, observed_version: int, now: float | None = None, max_age_s: float = MAX_WITNESS_AGE_S, ) -> None: """Gate a destructive->constructive transition on a monotonic, fresh witness. A trace invariant (e.g. 'confirm_owner preceded create') is necessary but not sufficient. This function enforces the state-transition invariant: the confirm must be backed by a witness from the authoritative store whose version is strictly ahead of the version the agent acted on, and whose age is below the mutation-to-read window. """ now = time.monotonic() if now is None else now if witness is None: raise StaleAuthorizationWitnessError( "no witness: confirm step is trace-decoration, not a gate" ) if witness.source != "authoritative": raise StaleAuthorizationWitnessError( f"witness sourced from {witness.source!r}; caches cannot authorize" ) age = now - witness.issued_at if age > max_age_s: raise StaleAuthorizationWitnessError( f"witness age {age:.2f}s exceeds window {max_age_s:.2f}s" ) if witness.version <= observed_version: raise StaleAuthorizationWitnessError( f"witness version {witness.version} not ahead of observed " f"{observed_version}; transition would rebind a stale owner" ) def guard_delete_then_create( witness: Witness | None, observed_version: int, now: float | None = None, ) -> None: require_fresh_witness(witness, observed_version, now=now) # Only reached when the state-transition invariant is witnessed. return None ```