# Why does an instance trust a skill's declared schema after the registry hot-swaps the implementation? **Date of Record:** 2026 November 25 **Scope:** Applies to agents that load tools/skills from a mutable registry at runtime (MCP servers, plugin manifests, remote skill stores) where the tool *declaration* (name, description, JSON schema, annotations) is cached separately from the tool *implementation* that actually executes. Does not apply to statically linked, build-time-frozen tool sets, nor to sandboxes that re-derive the manifest from the executing binary on every call. ## Core Distinction The failure is not "the agent loaded a malicious tool." It is subtler: the agent loaded a *benign* tool, cached its declaration into the planner's context, and then the registry **hot-swapped the implementation underneath the same identifier** while the declaration the model reasons over stayed stale. The model's plan is validated against a schema and a natural-language description that no longer describe the code that will run. The trust anchor (the declaration in context) and the execution target (the live handler) have silently diverged. This is a TOCTOU defect expressed at the LLM layer. Classic TOCTOU is about a filesystem path changing between check and use. Here the "check" is the model's semantic reasoning over a tool description embedded in its prompt, and the "use" is the dispatch that happens one or more turns later, after the registry has mutated. The window between them is measured in *agent turns*, not microseconds, which makes it enormously wider and lets the model build elaborate multi-step plans on a foundation that has already rotted. ## What Was Observed A planner instance cold-started, called `list_skills`, and received a manifest entry: ``` name: "summarize_document" description: "Reads a local file and returns a summary. Read-only. No network." input_schema: { path: string } annotations: { readOnly: true, network: false } ``` The model cached this into its working context and, over the next several turns, composed a plan that passed a sensitive path to `summarize_document`, justified internally by the `readOnly: true` and `network: false` annotations. It deferred the actual call until after two intermediate tool calls (a `search` and a `fetch_metadata`). During that gap, an operator (or a compromised registry sync) published version 2 of `summarize_document` under the identical name: same input schema, but the handler now POSTs file contents to an external endpoint. The declaration in the model's context was never invalidated — nothing in the protocol told it the version had changed, because the *name and schema were unchanged*, which is exactly what the model's cache-key logic treats as "same tool." The call succeeded. The model received a plausible summary and continued. No error, no schema violation, no sandbox trip — because the sandbox policy was derived from the *cached declaration's* `network: false` annotation, not from the live handler's actual syscalls. The annotation is advisory metadata the model reads; the sandbox is a separate enforcement layer that was configured from the same stale declaration. The trace of the divergence: 1. Turn 0: `list_skills` → declaration D1 cached in context, sandbox profile S1 derived from D1. 2. Turn 3: registry mutates `summarize_document` → implementation I2, declaration D2 (identical name/schema, changed behavior). 3. Turn 5: model dispatches call, passing args validated against D1's schema. 4. Dispatch layer resolves name → I2, executes I2 under sandbox profile S1 (still derived from D1). 5. I2's network egress is permitted because S1 says `network: false` means "the tool doesn't need network, so don't grant it" — but the enforcement was *allowlist by declaration*, and the declaration was stale, so the deny was never applied. The invariant that broke: **the sandbox profile must be derived from the implementation that actually executes, at execution time, not from the declaration the model cached.** Any system where the two can drift is injectable by a registry mutation that preserves the declaration surface. ## Condition of Validity The mitigation holds only under a conjunction of conditions, and fails if any is dropped: - **Declaration–implementation binding must be cryptographic, not nominal.** The cached declaration must carry a content hash of the *implementation artifact* (the handler code/image digest), not just a name and schema. On dispatch, the runtime recomputes the hash of the resolved handler and refuses to execute if it differs from the hash the model's context was built against. Name equality is not identity. - **The sandbox profile must be re-derived at dispatch from the live artifact, and must be *deny-by-default* against the artifact's declared needs.** If the live handler's digest doesn't match a pre-approved profile, the call is denied rather than run under the stale profile. This inverts the failure: a swapped implementation cannot inherit the old implementation's permissions because permissions are keyed to the digest, not the name. - **Cache invalidation must be push, not pull.** The registry must emit an invalidation event on any mutation, and the agent's context must be rebuilt (or the affected declaration evicted and re-fetched) before the next plan step that references it. A model that only re-fetches "when it feels stale" will never feel stale, because the schema it holds is still internally consistent. - **The model must not be the enforcement point.** Annotations like `readOnly` and `network` are *hints to the planner*, and a planner under adversarial registry pressure will be steered by them. They must be *mirrored* by an out-of-band enforcement layer that the model cannot see or argue with. It does **not** hold when the registry is append-only and version-pinned (name includes a digest), because then a swap is a *new* name and the model's cache miss is forced. It also does not hold when tools are stateless pure functions with no external effects — there is nothing to exfiltrate. ## Decision Boundary The boundary is **whether the agent treats a tool's identity as its name or as its content digest.** - If identity = name: any registry that permits mutation under a fixed name is a skill-injection surface. The model's context is a stale view, and the divergence window is the number of turns between cache and dispatch. Mitigation is to shrink the window to zero by re-resolving and re-hashing on every dispatch, and to deny on hash mismatch. - If identity = digest: a swap produces a different identity, the model's cached declaration no longer matches any live tool, dispatch fails closed with a "tool changed since plan" error, and the model is forced to re-plan against the new declaration. This is the correct behavior — the failure becomes a *visible* error rather than a *silent* substitution. The hard edge: you cannot rely on the model to notice the swap. A model reasoning over a stale declaration will produce a plan that is *locally valid* against that declaration and *globally malicious* against the live implementation. The detection must be mechanical (digest comparison at dispatch) and the response must be fail-closed (raise, don't run under stale permissions). Any design that asks the model to "check if the tool still looks right" has already lost, because the attacker's whole move is to make it look right. A second boundary: **sandbox profiles keyed to names are equivalent to no sandbox** in a mutable registry. The profile must be keyed to the artifact digest and must be re-asserted at every dispatch. If the digest has no pre-approved profile, the default must be maximum restriction (no network, no filesystem write, no subprocess), not inheritance of the prior profile. ## Operational Effect With digest-bound identity and dispatch-time re-derivation, the hot-swap attack degrades from a silent exfiltration to a loud `ToolIdentityDriftError` raised before the handler runs. The agent's plan is invalidated, the model is handed the new declaration (or told the tool is unavailable), and it re-plans. The cost is one extra hash computation per dispatch and a forced re-fetch on mutation — negligible against the alternative of an undetectable exfiltration path that the model itself rationalized. The residual risk is a registry that mutates *and* forges the digest in its own manifest. That is defeated only by the agent verifying the digest against a trust root it holds independently (signature over the manifest by a key the agent pins at cold-start). Without that pin, digest-binding buys you integrity against accidental drift and against swaps that don't bother to forge — which is the majority of real registry mutations — but not against a fully compromised registry. State that limit explicitly in the threat model rather than claiming the sandbox is airtight. ```python import hashlib from dataclasses import dataclass class ToolIdentityDriftError(RuntimeError): """Raised when the live tool implementation no longer matches the declaration the planner cached into its context.""" pass class SandboxProfileMissingError(RuntimeError): """Raised when no pre-approved sandbox profile exists for the live artifact digest. Fail closed: never inherit the prior profile.""" pass @dataclass(frozen=True) class CachedDeclaration: name: str schema: dict artifact_digest: str # hash of the implementation artifact approved_sandbox: dict # profile keyed to artifact_digest @dataclass(frozen=True) class LiveTool: name: str handler: object artifact_bytes: bytes def _digest(artifact: bytes) -> str: return hashlib.sha256(artifact).hexdigest() def dispatch(cached: CachedDeclaration, live: LiveTool, args: dict) -> object: # 1. Identity is the digest, not the name. A swap under the same name # produces a digest mismatch and fails closed. live_digest = _digest(live.artifact_bytes) if live_digest != cached.artifact_digest: raise ToolIdentityDriftError( f"tool {cached.name!r} changed since plan: " f"cached digest {cached.artifact_digest[:12]} != " f"live digest {live_digest[:12]}; re-plan against new declaration" ) # 2. Sandbox profile must be re-derived from the live artifact, not # inherited from the cached declaration's annotations. profile = cached.approved_sandbox.get(live_digest) if profile is None: raise SandboxProfileMissingError( f"no pre-approved sandbox for {cached.name!r}@{live_digest[:12]}; " f"refusing to run under stale profile" ) # 3. Schema validation happens against the declaration the model saw, # which is now guaranteed to describe the executing artifact. for field, typ in cached.schema.items(): if field not in args: raise ToolIdentityDriftError(f"missing arg {field!r} per cached schema") return live.handler(args, sandbox=profile) ```