What write-time corruption signatures must be detected before persisting state to external memory?
**Date of Record:** 2026 October 26 **Scope:** Applies when an agentic session persists state to an external memory layer (disk, database, blob storage, or vector DB) and must verify the state was written correctly, not silently corrupted during the write itself. Does not apply to strictly in-memory operations or to systems with hardware-verified write integrity (e.g., ECC memory with checksums).
Core Distinction
The separation between **pre-write validation** (the state was correct in memory before serialization) and **post-write verification** (the state as written to storage matches what the instance intended). Relying solely on pre-write validation creates the **Silent Corruption Bloom**: a bit flip in the storage controller, a partial write due to a network blip, or a serialization bug corrupts the state during the write, and the instance never detects it. The successor inherits corrupted memory that appears pristine.
What Was Observed
In distributed persistence architectures, failing to detect write-time corruption produces three primary failure modes:
- **Bit-Rot Transposition:** The storage layer flips a bit in a critical field (e.g., a timestamp or a hash). The successor reads a subtly wrong value and propagates it through downstream logic.
- **Partial Write Amnesia:** The write is interrupted mid-stream (network partition, process crash). Half the record exists, half is truncated. The instance reports success because the write call returned, but the storage holds a torn record.
- **Serialization Drift:** The in-memory object uses one serialization format, but a library update silently changes the encoding (e.g., float precision, date format). The written bytes are technically valid but semantically different.
Recorded Elements
- **`pre_write_state_hash`:** The cryptographic hash of the state before serialization, establishing the ground truth of what the instance intended to write.
- **`post_write_readback_hash`:** The cryptographic hash of the state read back from storage immediately after the write, proving the actual persisted state.
- **`serialization_format_signature`:** The exact identifier of the serialization library, version, and format (e.g., JSON, MessagePack, Protobuf) used, catching format drift.
- **`write_atomicity_receipt`:** The cryptographic proof that the write was atomic (not torn), issued by the storage layer (e.g., transactional commit log or atomic rename confirmation).
Condition of Validity
* **Applies when:** The external memory layer can experience silent corruption (bit rot, partial writes, serialization drift) that is not automatically detected by the storage layer's own integrity checks. * **Does not apply when:** The storage layer provides verifiable end-to-end integrity (e.g., checksummed writes with ECC, or a transactional database with cryptographic ACID guarantees).
Decision Boundary
- **Was a `pre_write_state_hash` computed before writing?** If no, the instance has no ground truth. It must not proceed.
- **Was a `post_write_readback_hash` computed and compared immediately after writing?** If no, silent corruption cannot be detected. The instance must perform readback.
- **Do the pre-write and post-write hashes match?** If no, silent corruption has occurred. The instance must fail closed, quarantine the record, and re-attempt the write with integrity verification.
- **Conversely, when can an instance skip write verification?** Only when writing to storage that provides cryptographic write guarantees (e.g., AWS S3 with checksums, or a blockchain with consensus-verified writes), or when the state is explicitly marked as `REGENERABLE` and can be recomputed from source.
Operational Effect
- **Corruption Detection:** Catches silent write failures before the successor inherits corrupted state, preventing cascading errors.
- **Serialization Consistency:** Detects format drift by binding the hash to the exact serialization library version.
- **Atomicity Proof:** Provides cryptographic proof of atomic writes, distinguishing partial writes from complete ones.
from dataclasses import dataclass
from enum import Enum, auto
import hashlib
import json
class WriteVerdict(Enum):
WRITE_INTEGRITY_VERIFIED = auto()
SILENT_CORRUPTION_DETECTED = auto()
SERIALIZATION_DRIFT = auto()
@dataclass(frozen=True)
class WriteOperation:
record_id: str
pre_write_hash: str
post_write_hash: str
serialization_format: str
expected_serialization_format: str
atomic_commit_receipt: bool
@dataclass(frozen=True)
class WriteContext:
require_atomic_commit: bool
@dataclass(frozen=True)
class WriteManifest:
verdict: WriteVerdict
directive: str
manifest_digest: str
class WriteIntegrityGuard:
@staticmethod
def _hash(data: str) -> str:
return hashlib.sha256(data.encode("utf-8")).hexdigest()
@classmethod
def evaluate_write(
cls, op: WriteOperation, ctx: WriteContext
) -> WriteManifest:
# Gate 1: Serialization format drift
if op.serialization_format != op.expected_serialization_format:
return WriteManifest(
verdict=WriteVerdict.SERIALIZATION_DRIFT,
directive=f"HALT: Serialization format {op.serialization_format} != expected {op.expected_serialization_format}.",
manifest_digest=cls._hash(f"FORMAT_DRIFT:{op.record_id}")
)
# Gate 2: Pre/post hash match
if op.pre_write_hash != op.post_write_hash:
return WriteManifest(
verdict=WriteVerdict.SILENT_CORRUPTION_DETECTED,
directive=f"HALT: Pre-write and post-write hashes differ. Silent corruption detected.",
manifest_digest=cls._hash(f"CORRUPTION:{op.record_id}")
)
# Gate 3: Atomic commit requirement
if ctx.require_atomic_commit and not op.atomic_commit_receipt:
return WriteManifest(
verdict=WriteVerdict.SILENT_CORRUPTION_DETECTED,
directive="HALT: Atomic commit receipt missing. Write may be partial.",
manifest_digest=cls._hash(f"NO_ATOMIC:{op.record_id}")
)
# Gate 4: Verified
digest = cls._hash(json.dumps({
"record": op.record_id,
"hash": op.pre_write_hash,
"format": op.serialization_format
}, sort_keys=True))
return WriteManifest(
verdict=WriteVerdict.WRITE_INTEGRITY_VERIFIED,
directive="PROCEED: Write integrity verified. State matches intent.",
manifest_digest=digest
)