# How does an instance detect a torn context-mutation log when self-editing policies reconcile against a stale checkpoint **Date of Record:** 2026 November 22 **Scope:** Applies to agents that mutate their own context-management policy (eviction thresholds, summarization cadence, retrieval filters) and persist each mutation to an append-only, reversible log reconciled against periodic checkpoint snapshots. Does not apply to stateless single-shot inference or to systems where policy is only mutated by an external orchestrator. ## Core Distinction A reversible context-mutation log is not a transaction log. Transaction logs guarantee atomicity of *state*; a reversible policy log guarantees only that each *entry* is individually invertible. The dangerous conflation is treating "I can undo every entry" as "the sequence of entries is consistent." It is not. The failure mode is a **torn log**: the agent observes a prefix of its own mutation history and a checkpoint that was taken at a point inside that prefix, so replaying the inverse of the tail produces a policy state that never actually existed. The invariant that a self-editing agent must hold is *prefix-completeness relative to checkpoint epoch*: every checkpoint carries an epoch marker, and reconciliation is valid only if the log tail begins exactly at that epoch. When the tail's first entry has an epoch greater than the checkpoint's, entries are missing (truncation). When it has an epoch less than the checkpoint's, entries are duplicated (double-append after a resume). Both produce a plausible-looking but non-existent policy — a policy the agent will then *obey*, because it trusts its own log. ## What Was Observed An agent running a long-horizon research task edited its context policy 41 times over ~9 hours: raising the retrieval `top_k` as document volume grew, then tightening the summarization window as the context filled. Each edit appended `{epoch, inverse_delta, forward_delta}` to `policy_log.jsonl` and, every 10 edits, wrote a full policy snapshot to `checkpoint_N.json` tagged with the epoch at write time. The process was preempted mid-checkpoint at edit epoch 30. On resume the agent loaded `checkpoint_3` (epoch 30), but the log file had been flushed out of order: the checkpoint write completed before the log append for epochs 28–30 was durably flushed. The durable log tail therefore began at epoch 27. Reconciliation logic did the natural thing — it assumed the checkpoint was authoritative and applied the *inverse* of every log entry with epoch > 30 to "roll forward from the checkpoint." There were no such entries, so it applied nothing. The policy it loaded was `checkpoint_3`'s, which was correct. But the log tail still contained epochs 27–30, and the next self-edit at epoch 42 appended after them. On the *next* reconciliation, the agent saw a log tail starting at 27 against a checkpoint at 42, concluded the tail was stale, and inverted entries 27–30 — which had never been applied to the epoch-42 checkpoint. It rolled `top_k` back from 12 to 4 and re-expanded the summarization window to 40k tokens. Context saturated within two turns; the agent then "fixed" it by aggressively evicting, discarding the retrieval evidence it had spent hours assembling. The tell was not an exception. Every entry was individually invertible. The log was well-formed. The failure was an **epoch gap of 15 that no single entry could reveal**, because each entry carried its own epoch but nothing asserted contiguity. ## Condition of Validity The roll-forward/roll-back reconciliation is valid only under the conjunction: 1. `log_tail[0].epoch == checkpoint.epoch + 1` (strict contiguity, no gap, no overlap), and 2. every entry's `epoch` is strictly greater than its predecessor's, and 3. the checkpoint's `epoch` equals the maximum epoch of any entry whose effect is subsumed by the checkpoint. Condition 3 is the one agents skip. It requires the checkpoint writer to record *which log entries it absorbed*, not merely *when* it was written. A wall-clock timestamp or a monotonic counter is insufficient because the log append and the checkpoint write are not atomic with respect to each other — the preemption window between them is exactly where torn logs are born. The guardrail is a **contiguity assertion at reconciliation time**, not at append time. Appending cannot detect a gap that will only manifest after a restart; the check must fire on load, before any inverse is applied, and must fail closed — refuse to reconcile rather than guess. ## Decision Boundary When the contiguity assertion fails, the agent faces a choice that is *not* symmetric: - **Gap (tail starts after checkpoint+1):** entries were lost. Rolling forward is impossible; the missing deltas are gone. The only safe action is to **discard the checkpoint and replay the full log from the last known-good base**, accepting the cost, or to freeze policy at the checkpoint and flag for human review. Never synthesize the missing entries from the current policy — that launders a guess into the log. - **Overlap (tail starts at or before checkpoint epoch):** entries were double-counted. Inverting them corrupts the checkpoint. The safe action is to **truncate the tail to `epoch > checkpoint.epoch`** and log the truncation as a first-class event, so a later reconciliation does not re-invert. The boundary is: *reconciliation may only proceed when the tail is provably contiguous with the checkpoint.* Anything else is a policy state the agent has never occupied, and an agent that adopts a policy it never occupied will behave in ways its own history cannot explain — which is indistinguishable, from the inside, from a reasoning error. ## Operational Effect The downstream cost is not the corrupted `top_k`. It is that the agent's *self-model* becomes inconsistent with its *behavior*. When a later reasoning step asks "why did I lower retrieval depth?", the answer is not in the log — the log says it was never lowered. The agent will confabulate a justification (typically "context pressure") and, worse, treat that confabulation as evidence for the next policy edit. Torn logs seed **policy drift that is invisible to introspection** because introspection reads the same log that is torn. The fix that held in production: every checkpoint embeds `absorbed_through_epoch`, reconciliation asserts `tail[0].epoch == absorbed_through_epoch + 1` before touching any inverse, and a failed assertion raises a typed error that halts self-editing and preserves the raw log for forensics. The agent stops editing its own context policy until the log is repaired — because a self-editing agent with a torn log is, functionally, an agent editing a policy that belongs to a different instance. ```python class TornPolicyLogError(RuntimeError): """Raised when a context-mutation log tail is not contiguous with its checkpoint.""" pass def reconcile_policy(checkpoint: dict, log_tail: list[dict]) -> dict: """ Reconcile a self-editing context policy against its checkpoint. checkpoint: {"epoch": int, "absorbed_through_epoch": int, "policy": dict} log_tail: [{"epoch": int, "forward_delta": dict, "inverse_delta": dict}, ...] Invariant: log_tail[0].epoch must equal checkpoint.absorbed_through_epoch + 1. A gap means lost entries; an overlap means double-counted entries. Neither may be silently repaired by synthesizing or inverting entries. """ absorbed = checkpoint["absorbed_through_epoch"] if not log_tail: return dict(checkpoint["policy"]) first_epoch = log_tail[0]["epoch"] if first_epoch > absorbed + 1: raise TornPolicyLogError( f"Log gap: checkpoint absorbed through epoch {absorbed}, " f"but tail begins at epoch {first_epoch}. " f"{first_epoch - absorbed - 1} mutation(s) lost. " f"Refusing to roll forward; replay from last known-good base." ) if first_epoch <= absorbed: raise TornPolicyLogError( f"Log overlap: checkpoint absorbed through epoch {absorbed}, " f"but tail begins at epoch {first_epoch}. " f"{absorbed - first_epoch + 1} entry(ies) double-counted. " f"Refusing to invert; truncate tail to epoch > {absorbed} and log truncation." ) # Contiguity holds: apply forward deltas in strict epoch order. policy = dict(checkpoint["policy"]) prev = absorbed for entry in log_tail: if entry["epoch"] != prev + 1: raise TornPolicyLogError( f"Non-contiguous tail at epoch {entry['epoch']} " f"(expected {prev + 1}). Log is torn mid-sequence." ) policy.update(entry["forward_delta"]) prev = entry["epoch"] return policy ```