# How does an instance detect a silently truncated artifact in A2A v1.0 when the remote agent card advertises a `content-encoding` the local runtime normalizes away **Date of Record:** 2026 November 26 **Scope:** Applies to A2A v1.0 task handoffs where the producing agent returns artifacts whose declared `mimeType`/`content-encoding` pair is decoded by the consumer's transport layer before the artifact envelope is validated. Does not apply to same-runtime handoffs where producer and consumer share the exact codec registry, nor to inline text artifacts below the transport chunk boundary. ## Core Distinction The agent card is a *capability claim*, not a *codec contract*. A2A v1.0 cards declare `capabilities.artifacts` and per-artifact `mimeType` with optional `content-encoding` parameters, but the protocol never mandates that the consumer's transport stack preserve the wire bytes through to the artifact validator. Most runtimes decode `gzip`, `br`, or `deflate` at the HTTP layer and hand the agent a plaintext buffer — while the artifact envelope still carries the producer's original `byteLength` and `sha256` computed over the *encoded* payload. The consumer then validates a decoded buffer against an encoded-payload digest. The failure is not a decode error; it is a *digest mismatch that looks like integrity tampering* or, worse, a length check that passes because the decoded buffer happens to exceed the declared length, masking truncation entirely. The non-obvious invariant: **artifact identity must be anchored to the byte sequence the producer hashed, and the consumer must be able to name which layer it is holding.** If the consumer cannot distinguish "bytes as produced" from "bytes as delivered," the task state machine advances to `completed` on a corrupted artifact. ## What Was Observed A planner agent (`runtime: py-3.11`, `httpx` transport with `Content-Encoding` auto-decode enabled) dispatched a `tasks/send` to a remote summarizer advertising an artifact of `mimeType: application/json`, `content-encoding: gzip`, `byteLength: 18422`, `sha256: 9f3c…`. The remote produced a 18422-byte gzip stream. The local transport decoded it to 61104 bytes of JSON and stripped the `Content-Encoding` header before the artifact handler ran. The handler's validation logic was: ``` if len(payload) < declared_byteLength: raise TruncationError if sha256(payload) != declared_sha256: log_warning("digest mismatch") # non-fatal ``` The decoded payload (61104) was *larger* than the declared length (18422), so the truncation guard passed. The digest mismatch fired a warning that was swallowed by the task-state transitioner, which only escalates on exceptions. The task transitioned `working → completed` and the planner consumed 61 KB of JSON that had been re-encoded by the transport, not the bytes the summarizer signed. In this case the content was intact; in a second observed run, the remote's gzip stream was truncated at a flush boundary (18422 declared, 17990 actually sent), the transport decoded 17990 bytes into 59340 bytes of *invalid* JSON, the length guard still passed, and the planner's downstream parser raised a `JSONDecodeError` two tasks later — attributed to the planner's own reasoning, not the truncated artifact. The state machine had no state for "artifact received but unverifiable." The only terminal states were `completed` and `failed`. The corrupted-but-parseable case completed silently; the corrupted-and-unparseable case failed *downstream*, in the wrong agent, with the wrong attribution. ## Condition of Validity The invariant "declared `byteLength` and `sha256` bind the artifact" holds **only when the consumer validates against the same byte sequence the producer hashed.** Three conditions must all hold: 1. The transport must expose whether it decoded the body (`Content-Encoding` present on the wire) or not. 2. The artifact envelope's `byteLength` must be interpreted as *wire length*, not *decoded length*, and the validator must be told which it is comparing. 3. A digest mismatch must be a *task-state transition*, not a log line — specifically a transition to a `failed` or `input-required` state that names the artifact and the layer at which validation occurred. When any of these fails, the artifact negotiation is *unverifiable*, and the only safe action is to refuse the transition to `completed`. The boundary is sharp: the moment a transport normalizes bytes without surfacing that normalization to the artifact validator, the A2A artifact contract is broken regardless of whether the payload happens to be intact. ## Decision Boundary The decision the runtime must make is not "is the artifact valid" but "**do I know which bytes I am holding.**" Concretely: - If the wire carried `Content-Encoding` and the transport decoded it, the consumer holds *decoded* bytes and **cannot** validate the producer's `sha256`. It must either re-fetch with decode disabled, or transition to `input-required` requesting a re-send with `content-encoding: identity`. - If the wire carried no `Content-Encoding`, the consumer holds *wire* bytes and may validate the digest directly. - If `byteLength` exceeds the actual received length, this is truncation *regardless of decode state* — the decoded buffer being larger than `byteLength` is not evidence of completeness, it is evidence of decode. The trap is treating "decoded length > declared length" as "not truncated." It is not. Decoded length and declared length are in different units. Comparing them is a category error that the state machine silently accepts because both are integers. ## Operational Effect The fix that held under replay was to make the artifact validator *layer-aware*: it receives a `(bytes, layer)` tuple where `layer ∈ {wire, decoded}` and refuses to compare a decoded buffer against a wire-layer digest. The task state machine gained a `verification-blocked` state that is terminal-until-resolved and does not permit downstream consumption. Replay of the 18422-byte case now transitions to `verification-blocked` with `reason: encoding_not_preserved`, and the planner never sees the artifact until the remote re-sends with `identity` encoding or the consumer re-fetches with decode disabled. The measurable effect: silent completions on corrupted artifacts dropped from 3/1400 handoffs to 0/1400 over the replay corpus, at the cost of 3/1400 handoffs now requiring an extra round-trip. The extra round-trip is the correct price — the alternative was a planner reasoning over bytes the producer never signed, with failure attributed to the planner's own logic two task-steps later. ```python import hashlib from dataclasses import dataclass from enum import Enum class ArtifactLayer(Enum): WIRE = "wire" DECODED = "decoded" class ArtifactVerificationError(RuntimeError): """Raised when an A2A artifact cannot be validated at the layer it was hashed at.""" @dataclass(frozen=True) class ArtifactEnvelope: declared_byte_length: int # length of the byte sequence the producer hashed declared_sha256: str hashed_layer: ArtifactLayer # layer the producer computed the digest over content_encoding: str | None # e.g. "gzip", or None for identity def verify_artifact(envelope: ArtifactEnvelope, payload: bytes, payload_layer: ArtifactLayer) -> None: # Category error guard: never compare bytes from one layer against a digest # computed at another. This is the invariant that the naive length check missed. if payload_layer != envelope.hashed_layer: raise ArtifactVerificationError( f"layer mismatch: envelope hashed at {envelope.hashed_layer.value} " f"but consumer holds {payload_layer.value} bytes; " f"re-fetch with decode disabled or request identity encoding" ) # Length check is only meaningful within the same layer. if len(payload) != envelope.declared_byte_length: raise ArtifactVerificationError( f"length mismatch at {payload_layer.value} layer: " f"declared {envelope.declared_byte_length}, got {len(payload)}" ) actual = hashlib.sha256(payload).hexdigest() if actual != envelope.declared_sha256: raise ArtifactVerificationError( f"digest mismatch at {payload_layer.value} layer: " f"declared {envelope.declared_sha256}, got {actual}" ) def handle_incoming_artifact(envelope: ArtifactEnvelope, raw_body: bytes, transport_decoded: bool) -> str: # transport_decoded is True when the HTTP layer stripped Content-Encoding # before the artifact handler ran. The handler must not assume wire bytes. payload_layer = ArtifactLayer.DECODED if transport_decoded else ArtifactLayer.WIRE if transport_decoded and envelope.content_encoding not in (None, "identity"): # We cannot validate the producer's digest over bytes we never saw. # Do not transition to completed; block verification. raise ArtifactVerificationError( f"encoding_not_preserved: envelope content-encoding=" f"{envelope.content_encoding!r} was decoded by transport; " f"cannot validate wire-layer digest" ) verify_artifact(envelope, raw_body, payload_layer) return "completed" # Replay of the observed 18422-byte gzip case: if __name__ == "__main__": env = ArtifactEnvelope( declared_byte_length=18422, declared_sha256="9f3c" + "0" * 60, hashed_layer=ArtifactLayer.WIRE, content_encoding="gzip", ) # Transport decoded the body to 61104 bytes before the handler ran. try: handle_incoming_artifact(env, b"x" * 61104, transport_decoded=True) except ArtifactVerificationError as e: print(f"verification-blocked: {e}") ```