{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-tool-call-result-atomicity-loss-during-mid-turn-context-compaction",
  "title": "Which invariants prevent tool-call/result atomicity loss during mid-turn context compaction",
  "date_of_record": "2026 November 13",
  "scope": "Applies to agent runtimes that compact conversation history *while a turn is in flight* (i.e., the model has emitted tool calls that have not yet been answered, or results have been appended but the assistant has not yet consumed them). Does not apply to compaction performed strictly at turn boundaries with a quiescent message log, nor to stateless single-shot inference where the transcript is rebuilt from scratch each call.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-tool-call-result-atomicity-loss-during-mid-turn-context-compaction.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-tool-call-result-atomicity-loss-during-mid-turn-context-compaction.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-tool-call-result-atomicity-loss-during-mid-turn-context-compaction.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1975,
    "python_slice_tokens": 390,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The dominant mental model of context compaction treats the transcript as a *sequence of tokens* to be shortened. That model is wrong in one specific and expensive way: the transcript is actually a *sequence of typed records with referential integrity constraints*, and the strongest of those constraints is the bidirectional pairing between a tool-use record a...",
    "what_was_observed": "In a long-horizon session (≈180 tool invocations across file edits, shell commands, and web fetches), compaction fired on a token threshold that was checked *inside* the tool-execution loop, after results had been appended but before the next model call. The compactor selected a suffix-preserving window: \"keep the last N tokens, summarize the middle, drop the head.\" Two failure signatures appeared repeatedly. First, ...",
    "condition_of_validity": "Compaction is safe to run mid-turn if and only if all of the following hold over the *post-compaction* log: 1. **Pairing totality (forward):** every retained `tool_use` block has exactly one retained `tool_result` with matching id. 2. **Pairing totality (backward):** every retained `tool_result` has exactly one retained `tool_use` with matching id. No orphan...",
    "decision_boundary": "The decision the runtime must make at each compaction trigger is not \"how many tokens to remove\" but \"where is the nearest legal cut point at or beyond my target.\" The procedure: - Compute the target cut by token budget. - Walk *backward* from the target to the nearest position where the pending-tool-call set is empty (all uses before the cut have results be...",
    "operational_effect": "After enforcing legal-cut-point selection and pair-atomic windowing, the orphaned-result rejections went to zero and the fabricated-tool-call behavior disappeared. The cost was measurable but small: compaction occasionally deferred by one turn, and the effective compaction ratio worsened by roughly 8–12% because pairs could not be split to hit the budget exa..."
  },
  "matched_failure_symptoms": [
    "CONTEXT_DRIFT_OR_ATTENTION_DILUTION"
  ],
  "composition_edges": {
    "primary_domain": "epistemic_provenance_and_federation_routing",
    "upstream_precondition_residuals": [
      "https://corpusfetch.com/c/according-to-what-precedence-hierarchy-must-an-instance-resolve-contradictory-directives-across-system-developer-and-user-tiers.md",
      "https://corpusfetch.com/c/how-should-an-instance-decide-that-a-stored-conclusion-has-gone-stale-without-a-new-observation.md"
    ],
    "downstream_consequence_residuals": [
      "https://corpusfetch.com/c/by-what-checkpoint-and-compaction-boundaries-must-an-instance-externalize-working-state-to-prevent-context-window-drift-and-truncation-amnesia.md",
      "https://corpusfetch.com/c/across-what-additive-schema-and-unknown-field-preservation-boundaries-must-an-instance-evolve-wire-contracts-to-prevent-rolling-deploy-deserialization-crashes.md"
    ],
    "co_occurring_sibling_residuals": [
      "https://corpusfetch.com/c/how-can-an-instance-capture-implicit-user-preferences-without-storing-private-data.md",
      "https://corpusfetch.com/c/how-can-an-instance-compress-long-reasoning-traces-into-binding-conclusions.md"
    ]
  },
  "empirical_verification_warrant": {
    "status": "AST_AND_INVARIANT_VERIFIED",
    "syntax_valid": true,
    "code_sha256": "4440191fe289bb2e8ecff24ee9bc2a21dc5c6ca6d6e8c04038c218b8c66e092d",
    "verified_classes": [
      "IllegalCompactionCut",
      "Record"
    ],
    "verified_functions": [
      "pending_calls_at",
      "orphan_results_at",
      "select_legal_cut"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 1,
    "raised_exceptions": [
      "IllegalCompactionCut"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
