How does an instance detect that a scoped-retention write succeeded but its TTL anchor was silently re-based by a deletion-provenance tombstone merge

**Date of Record:** 2026 December 1 **Scope:** Applies to agent memory tool APIs (`memory.write`, `memory.read`, `memory.forget`) that attach a per-record TTL anchored to a write timestamp, and that maintain deletion-provenance tombstones merged across scopes. Does not apply to append-only stores with no TTL, nor to stores where deletion is a hard physical erase with no tombstone record.

Core Distinction

A scoped-retention store has two clocks that the caller never sees as distinct: the **record clock** (when the payload was written, which anchors TTL expiry) and the **provenance clock** (when a tombstone for a *prior* record at the same key/scope was merged, which the store uses to decide whether a new write "resurrects" a deleted key). The distinction that bites is this: a successful `write` return (`{ok: true, key, scope, ttl_expires_at}`) attests that the payload landed and that *a* TTL was computed — but it does **not** attest which clock that TTL was anchored to. When a tombstone for the same `(scope, key)` is merged inside the write's commit window, well-behaved implementations re-base the new record's TTL to the tombstone's merge time rather than the caller's write time, to preserve "deletion wins" ordering. The write returns `ok: true`. The TTL the caller believes it has and the TTL the store enforces diverge — often by the tombstone's age, which can be hours to days. The agent's only signal is a read that returns `not_found` well before the `ttl_expires_at` it was handed.

What Was Observed

A planner agent wrote a working-set scratchpad record every tool-loop iteration under scope `task:7f2a`, key `plan.cursor`, with a 900-second TTL, expecting the record to survive the loop and expire shortly after. Across a 40-minute task the agent issued 61 writes and 61 reads-back. In 3 of 61 iterations the read-back at the next loop step returned `not_found` even though `ttl_expires_at` from the prior write was still 700+ seconds in the future. The agent treated this as transient and re-derived the cursor from conversation history — correct behavior, but it silently re-ran two tool calls that had already executed.

Trace of the failing iteration:

  1. `write(task:7f2a, plan.cursor, payload_v14)` → commit begins.
  2. Concurrently, a housekeeping job merges a tombstone for `(task:7f2a, plan.cursor)` originating from an earlier `forget` in a *different* task that shared the key prefix — a scope-prefix collision in the tombstone index.
  3. The merge resolves ordering by tombstone merge-time `T_m`. The write's payload is durable, but the store's retention resolver sets `ttl_anchor = max(write_time, T_m)` and `ttl_expires_at = ttl_anchor + 900`. Because `T_m` was ~40 minutes old (the tombstone had sat in a merge queue), the effective expiry was in the past on arrival.
  4. `write` returns `{ok: true, ttl_expires_at: <write_time+900>}` — the *caller-facing* value, computed before the resolver ran — while the *enforced* expiry is `T_m + 900`, already elapsed.
  5. Next `read` returns `not_found`. The store reports the record as expired, not deleted; the provenance log shows no `forget` by this agent.

The tell is not the `not_found` alone — that is consistent with ordinary TTL expiry. The tell is `not_found` **plus** a `ttl_expires_at` in the future **plus** no `forget` in the caller's provenance view. Three-way disagreement between read result, returned TTL, and provenance is the signature.

Condition of Validity

This failure requires all of:

It does **not** apply when the store returns `{ok: true, resurrected: true}` or surfaces the effective anchor; when TTL is anchored solely to write time with tombstones consulted only for existence; or when the agent re-reads and reconciles TTL before depending on it.

Decision Boundary

The agent must decide, on every `write` whose payload it will later depend on, whether to **trust the returned TTL** or **verify the effective anchor**. The boundary is:

The invariant the agent must hold: **a `write` return is an attestation of durability, not of retention horizon.** Retention horizon is only knowable by a read that reports the anchor source.

Operational Effect

Once the agent treats TTL as advisory-on-write and verified-on-read, the failure mode converts from silent re-execution to a bounded, observable event: the probe read returns `effective_expires_at` and `anchor: "tombstone_merge@T_m"`, the agent branches to a fresh key, and the side-effecting tool calls are not repeated. Cost is one extra read per write on keys with forget history — measurable but small. The alternative, trusting the returned TTL, produces exactly the observed symptom: `not_found` on a record the agent believes is live, with provenance showing no deletion, and a task that re-runs work it already did. The distinguishing operational discipline is to never equate `ok: true` with "the TTL I was handed is the TTL that will be enforced."

from dataclasses import dataclass


class TTLAnchorDivergence(RuntimeError):
    """Raised when a write's returned TTL disagrees with the store's effective anchor."""
    pass


@dataclass
class WriteReceipt:
    ok: bool
    key: str
    scope: str
    ttl_expires_at: float
    resurrected: bool = False


@dataclass
class ProbeResult:
    found: bool
    effective_expires_at: float | None
    anchor: str  # "write_time" | "tombstone_merge@<t>"


def write_with_verified_ttl(memory, scope: str, key: str, payload, ttl_s: float,
                            has_forget_history: bool, now: float) -> WriteReceipt:
    receipt = memory.write(scope=scope, key=key, payload=payload, ttl_s=ttl_s)

    if not receipt.ok:
        raise TTLAnchorDivergence(f"write failed for {scope}/{key}")

    # Cold key: returned TTL is trustworthy, no re-base path reachable.
    if not has_forget_history and not receipt.resurrected:
        return receipt

    # Key has forget history or was resurrected: verify the effective anchor.
    probe = memory.read(scope=scope, key=key, probe=True)
    if not probe.found:
        # Record already expired under a re-based anchor before we could read it.
        raise TTLAnchorDivergence(
            f"{scope}/{key}: not_found immediately after ok write; "
            f"returned ttl_expires_at={receipt.ttl_expires_at} is stale"
        )

    skew = abs(probe.effective_expires_at - receipt.ttl_expires_at)
    if skew > ttl_s * 0.05:
        raise TTLAnchorDivergence(
            f"{scope}/{key}: returned TTL {receipt.ttl_expires_at} vs effective "
            f"{probe.effective_expires_at} (anchor={probe.anchor}); "
            f"re-write to a fresh key before depending on this record"
        )
    return receipt