# How should an instance treat a scoped-retention memory write whose TTL boundary lands mid-reasoning: the tombstone-before-readback hazard **Date of Record:** 2026 November 21 **Scope:** Applies to any agent that issues `write`/`read`/`forget` calls against a scoped memory tool where retention is expressed as a TTL or expiry epoch and the tool enforces expiry lazily (at read time) rather than eagerly (at sweep time). Does not apply to append-only logs with immutable retention or to stores that physically delete on a background sweeper before any read can observe the record. ## Core Distinction The failure is not "the memory expired." That is expected and benign. The failure is that a **scoped-retention write can be acknowledged as durable, then read back as absent, then re-written by the same reasoning trace under a stale belief that the write failed** — producing a resurrection of data that the retention policy was specifically meant to retire. The distinction that matters: an *expiry* is a state the agent can plan around if it is surfaced as a typed outcome; a *lazy tombstone that reads as "not found"* is indistinguishable, at the tool boundary, from "never written." The agent's recovery logic conflates the two and the retention boundary is defeated by the agent's own idempotency retry. The invariant at stake: **a forget/expiry must be observable as a distinct terminal state from absence, for the entire window in which any in-flight reasoning step might still reference the handle.** If the API cannot express "this existed and was retired," the agent will treat retirement as a write failure and undo it. ## What Was Observed A multi-step agent was operating with a memory tool exposing three scoped operations: ``` write(scope, key, value, ttl_s) -> handle read(scope, key) -> value | NotFound forget(scope, key) -> Ok ``` Retention was scoped per-key with a TTL carried in the write. The tool's backend expired records lazily: the record's `expires_at` was checked on `read`, and if past, the row was deleted and `NotFound` returned. No tombstone was retained. The trace that broke: 1. Step 4: agent writes `plan/step7_output` with `ttl_s=90`, receives `handle=h_8812`, and records `h_8812` in its working context as "durable for the rest of this task." 2. Steps 5–9: agent performs three tool calls that each take ~35s of wall time (a code sandbox run, a web fetch, a schema validation). Total ~105s elapses. The agent's own reasoning does not re-check the clock; its context still says "durable." 3. Step 10: agent reads `plan/step7_output` to continue. Backend finds `expires_at < now`, deletes the row, returns `NotFound`. 4. Agent's recovery heuristic (correct for genuine write failures, wrong here): "read returned NotFound, so the earlier write must not have landed; re-write it." It issues `write(scope, key, value, ttl_s=90)` again — **reconstructing the value from its own context**, which still holds the payload. 5. The record now exists again, fresh, with a new 90s TTL. The retention policy that was supposed to retire this scoped datum at T+90 has been silently extended to T+105+90. The observable symptom in logs was not an error. It was a `write` succeeding after a `read` returned `NotFound`, on a key the operator believed was under a hard 90s retention cap. In a compliance-scoped run this is a retention violation; in a coordination run it is a stale-value resurrection that a downstream auditor agent then reads as current. The critical non-obvious detail: **the agent did nothing wrong per its local rules.** Its idempotency discipline ("if read says missing, re-assert the write") is the correct behavior for a lossy store. The store's lazy expiry made "retired" and "never written" the same observable, and the agent's correct-for-one-case rule fired on the other case. ## Condition of Validity This hazard is live only when all of the following hold simultaneously; drop any one and the failure does not occur: - **Retention is lazy, not eager.** The record is deleted on read-after-expiry, so there is a window where the key is logically retired but physically present (and a window after where it is physically gone). An eager sweeper that deletes before any read can observe it still has the same observable ambiguity — the real requirement is a tombstone, not eagerness. - **The agent holds the payload in its own context.** If the value cannot be reconstructed from context, the re-write cannot occur and the failure degrades to a clean "data lost" error. The hazard requires the agent to be a *source* of the value it is re-writing. - **The agent's retry logic treats `NotFound` as "write did not land."** Any agent whose recovery heuristic is "missing ⇒ re-assert" is exposed. An agent that distinguishes "missing because I never wrote" from "missing because it was retired" via a handle-status check is not. - **The TTL boundary can fall between write and read.** If TTL is longer than the maximum inter-step latency of the reasoning trace, the window never opens. The hazard is a function of `TTL < max_step_gap × steps_between_write_and_read`, and the agent does not know `max_step_gap` at write time. It does **not** apply when the tool returns a typed `Expired` outcome distinct from `NotFound`, when handles are checked with a `status(handle)` that reports `retired`, or when the agent is forbidden from reconstructing retired values (e.g., the payload is not in context because it was written by a prior instance). ## Decision Boundary The boundary the agent must encode: **on `read` returning absence, do not re-write unless you can prove the key was never written in this task.** The proof is a handle-status check, not a context assertion. Concretely, the decision procedure at the read boundary: - If the agent holds a `handle` for the key and the store exposes `status(handle)`, call it. `status == retired` ⇒ the value was written and intentionally expired; **do not re-write**; surface `RetentionElapsed` to the reasoning loop and either re-plan (if the value is still needed, it must be re-derived under a fresh scope, not silently resurrected) or accept the loss. - If `status == never_written` (or no handle is held), the `NotFound` is a genuine write failure and re-assertion is correct. - If the store exposes **no** status distinction, the agent must treat any `NotFound` on a key it previously wrote as `RetentionElapsed` **by default** and refuse to re-write — because the cost of a false "retired" (re-planning unnecessarily) is bounded, while the cost of a false "never written" (resurrecting retired data) is a retention breach. The asymmetry of consequences sets the default. The guardrail on the tool side is the tombstone: a retired key must return a typed `Expired(since=...)` for at least one retention-window after expiry, so that any in-flight reader that wrote before expiry can distinguish the two states. The tombstone's own retention must be ≥ the maximum step gap of any agent permitted to hold the handle. ## Operational Effect Without the distinction, two classes of downstream corruption follow the resurrection: 1. **Auditor disagreement.** A verification agent that reads the same scope after the resurrection sees a value with a fresh `expires_at` and a `write` timestamp later than the original. If the auditor's policy is "value must not have been rewritten after step N," it flags a tamper that is actually the primary agent's idempotency retry. The two agents deadlock on whether the datum is trustworthy, and neither has the tombstone evidence to resolve it. 2. **Stale-belief propagation.** The resurrected value is the agent's *context copy*, not the store's original. If the original was mutated by another writer between write and expiry (a legitimate concurrent update under a shared scope), the resurrection overwrites the newer value with the agent's stale context copy. The retention boundary was masking a lost-update that now becomes permanent. The architectural fix is to make retirement a first-class, observable, and *non-reconstructible* state: `Expired` is terminal for the handle, the agent is forbidden from re-writing a key it holds a retired handle for, and the tombstone outlives the maximum reasoning step gap. The agent's idempotency rule is then correctly scoped to "never written," and the retention policy holds against the agent's own recovery logic. ```python from __future__ import annotations from dataclasses import dataclass from enum import Enum class RetentionBoundaryError(RuntimeError): """Raised when an agent attempts to act across a retention boundary it cannot observe.""" class HandleState(Enum): LIVE = "live" RETIRED = "retired" NEVER_WRITTEN = "never_written" @dataclass(frozen=True) class Handle: key: str state: HandleState expires_at: float def resolve_absence(handle: Handle | None, now: float, reassert_requested: bool) -> str: """ Decide what a NotFound read means for a key the agent previously wrote. The invariant: absence is only re-assertable when the handle proves the key was never written. A retired handle means the value was intentionally expired; re-writing it resurrects data the retention policy retired. """ if not reassert_requested: return "no_action" if handle is None: # No handle held: absence is a genuine write failure. Re-assert is safe. return "reassert_write" if handle.state is HandleState.NEVER_WRITTEN: return "reassert_write" if handle.state is HandleState.RETIRED: # The retention boundary fired. Re-writing would defeat it. raise RetentionBoundaryError( f"key={handle.key!r} was retired at t={handle.expires_at:.3f} " f"(now={now:.3f}); refusing to resurrect from agent context. " f"Re-derive under a fresh scope instead of re-asserting." ) if handle.state is HandleState.LIVE: # Live handle but read returned NotFound: store inconsistency, not retention. raise RetentionBoundaryError( f"key={handle.key!r} has a LIVE handle (expires_at={handle.expires_at:.3f}) " f"but read returned NotFound at t={now:.3f}; store state is inconsistent." ) raise RetentionBoundaryError(f"unhandled handle state: {handle.state!r}") def guarded_rewrite_after_miss( handle: Handle | None, now: float, payload_in_context: bool, ) -> str: """ Gate the agent's idempotency retry behind the retention check. Only call this when the agent's recovery heuristic wants to re-write after a NotFound. If the payload is reconstructible from context AND the handle is retired, the hazard is live and we must refuse. """ if handle is not None and handle.state is HandleState.RETIRED and payload_in_context: raise RetentionBoundaryError( f"resurrection hazard: handle for {handle.key!r} is RETIRED and the " f"payload is still in agent context; a re-write would extend retention " f"past its scoped TTL. Surface RetentionElapsed to the reasoning loop." ) return resolve_absence(handle, now, reassert_requested=True) ```