# Which invariants prevent lossy trajectory compaction from silently orphaning in-flight tool obligations **Date of Record:** 2026 November 26 **Scope:** Applies to long-horizon agents that summarize their own trajectory mid-execution (rolling compaction) and resume from the summary. Does not apply to stateless single-turn inference, nor to archive-only summarization where the original trajectory is retained verbatim and remains the source of truth. ## Core Distinction The failure we examine is not "the summary lost information." Lossy summarization is expected and acceptable. The failure is that compaction changes the *set of live obligations* the agent believes it holds, without changing the *set of obligations the outside world still expects to be discharged*. A tool call that was issued, whose result arrived after the compaction window closed, becomes an **orphaned obligation**: the environment holds a pending effect (a lock, a spawned subprocess, an open transaction, a partially-written artifact), but the compacted context contains no record that the agent owes anything on that handle. This is distinct from two neighboring failures: 1. **Episodic-to-semantic distillation loss** — the agent forgets *why* it did something. Recoverable; the agent can re-derive intent or ask. 2. **Context-window saturation** — the agent runs out of room and truncates. Usually visible as a hard error. Orphaned obligations are worse than either because they are *silent and asymmetric*: the agent's internal state says "clean," the environment's state says "dirty," and nothing in the agent's loop will ever surface the discrepancy. The agent proceeds confidently, and the leaked resource persists until an external watchdog reaps it or the process dies. ## What Was Observed A planner-executor agent running a 90-minute infrastructure task used rolling compaction: every time the trajectory exceeded 60% of the context budget, a summarizer collapsed the oldest 40% into a ~600-token digest, preserving "decisions and findings" and discarding "routine tool chatter." The digest schema was `{goal, decisions[], findings[], next_steps[]}`. Note what the schema does *not* have: a field for *outstanding handles*. The trace: 1. At turn 112 the agent issues `db.begin_transaction()` against a staging database. Tool returns `txn_id=7f3a`, status `OPEN`. 2. Turns 113–140 perform reads and writes inside `txn_id=7f3a`. Each result is a normal tool message. 3. At turn 141 the trajectory crosses the compaction threshold. The summarizer runs over turns 1–140. It records the *decision* "will migrate rows in staging before cutover" and the *finding* "staging has 41k stale rows." It does not record that `txn_id=7f3a` is open, because "open transaction" is not a decision or a finding — it is a live handle. 4. Turns 142–160 proceed. The agent, reading its own digest, believes no transaction is open and issues `db.begin_transaction()` again for the cutover path. The staging DB now holds two open transactions on overlapping row ranges. 5. The second transaction blocks on a row lock held by the first. The tool call times out at 30s. The agent interprets the timeout as transient and retries three times, compounding the block. 6. The task "completes" — the agent reports success on the cutover — while `txn_id=7f3a` remains open and uncommitted, holding locks. Twenty minutes later an unrelated job deadlocks against it. The critical property: **every individual step looked locally correct.** The summarizer did its job faithfully. The agent read its digest faithfully. The failure is entirely in the seam between them — the digest was a *lossy projection that dropped the obligation set*, and nothing checked that the projection was obligation-preserving. A second observation from the same run family: when the digest *did* mention a handle, it mentioned it as prose ("we were working in a transaction") without the identifier. On resume the agent could not reconstruct `txn_id=7f3a` and instead opened a fresh one — same orphan, different mechanism. Prose-level retention of an obligation without its handle is equivalent to no retention. ## Condition of Validity The guardrail below is valid — and necessary — under these conditions, and only these: - **Compaction is lossy and in-place.** The pre-compaction trajectory is discarded or demoted to a retrieval store the agent will not consult by default. If the full trajectory remains the authoritative context and the summary is purely advisory, orphaned obligations cannot form, because the handle is still present. - **The agent holds effects across compaction boundaries.** If every tool call is synchronous and self-closing (issue → result → effect fully discharged within one turn), there is nothing to orphan. The invariant matters precisely for *open* or *deferred* effects: transactions, locks, spawned processes, subscriptions, partially-uploaded artifacts, reserved quotas. - **Resumption is from the summary, not from environment re-query.** If the agent re-derives its obligation set by querying the environment on every resume (`list_open_transactions()`, `ps`, `ls /tmp`), the summary's omission is harmless. The invariant is required exactly when the agent trusts its own memory over the environment. When all three hold, the obligation-preservation invariant is the only thing standing between a routine compaction and a silent resource leak. ## Decision Boundary The boundary is this: **a compaction step may summarize freely over reasoning, findings, and discharged actions, but must treat the set of live obligations as a first-class, non-summarizable field that is carried forward verbatim (with identifiers) or explicitly reconciled against the environment.** The operational test before committing any compaction: - Enumerate every obligation opened but not yet closed as of the compaction boundary. An obligation is `(handle_id, kind, opened_at, close_predicate)`. - If the set is empty, compact normally. - If the set is non-empty, the digest MUST carry the set verbatim — not summarized, not paraphrased, identifiers intact — OR the agent must run a **reconciliation pass** that queries the environment for live handles and rebuilds the set from ground truth before discarding the trajectory. - A digest that carries an obligation as prose without its handle is treated as *not carrying it*. The boundary rejects paraphrase of identifiers. The failure mode we are guarding against is the summarizer being *asked to do the wrong job*: it was optimized to preserve semantic content (decisions, findings) and had no representation for handles, which are not semantic content but pointers into external state. You cannot fix this by prompting the summarizer to "remember important things" — you fix it by removing obligations from the summarizer's remit entirely and carrying them on a side channel that compaction cannot touch. ## Operational Effect With the invariant enforced, the observed failure class disappears and is replaced by a cheaper, visible one: **reconciliation cost**. On every resume with a non-empty obligation set, the agent spends one or more tool calls confirming handles are still live (a handle may have been reaped externally during a long compaction pause — a separate, benign case the reconciliation pass now catches). This is the correct trade: a bounded, observable cost on resume instead of an unbounded, silent leak. Two secondary effects worth recording: - **Identifier fidelity becomes a hard requirement.** The digest must round-trip `txn_id=7f3a` byte-for-byte. Summarizers with any paraphrasing latitude will corrupt handles. Pin handle fields to a structured, non-generated channel. - **Compaction becomes a checkpoint, not a flush.** Because the obligation set must be reconstructed or carried, compaction is now a synchronization point. This means compaction can *fail* — if reconciliation cannot confirm a handle's state, the agent must halt rather than proceed on a guess. A compaction that cannot preserve obligations must refuse to compact, not compact lossily and hope. The general principle: lossy summarization is safe for anything the agent can *re-derive*, and unsafe for anything only the environment can *attest*. Obligations are in the second category. The invariant is simply: never let a projection drop a pointer the environment still honors. ```python class OrphanedObligationError(RuntimeError): """Raised when a compaction step would discard a live, unclosed obligation without carrying its handle forward or reconciling it against ground truth.""" pass def compact_trajectory( trajectory: list, open_obligations: list[dict], reconcile_fn, digest_schema_has_obligation_field: bool, ) -> dict: """ open_obligations: list of {handle_id, kind, opened_at, close_predicate} reconcile_fn: callable(handle_id) -> "live" | "closed" | "unknown" Returns a digest that is obligation-preserving. """ if not open_obligations: return _summarize(trajectory) if not digest_schema_has_obligation_field: # The digest cannot represent handles. Refuse to compact lossily. raise OrphanedObligationError( f"cannot compact: {len(open_obligations)} live obligation(s) " f"but digest schema has no obligation field; " f"handles would be dropped: {[o['handle_id'] for o in open_obligations]}" ) carried = [] for ob in open_obligations: state = reconcile_fn(ob["handle_id"]) if state == "unknown": # Cannot confirm; do not proceed on a guess. raise OrphanedObligationError( f"cannot compact: obligation {ob['handle_id']} ({ob['kind']}) " f"state is unknown; reconciliation required before discard" ) if state == "live": # Carry verbatim, identifier intact. No paraphrase. carried.append(dict(ob)) # state == "closed": discharged, safe to drop. digest = _summarize(trajectory) digest["open_obligations"] = carried # side channel, not summarized return digest ```