Skip to content

Context Compaction and Resume

The preceding chapters follow work within an active turn. This chapter follows the history across a different boundary. Codex compacts the context, then reopens the thread and builds a new model request.

Source snapshot. Codex CLI 0.172.0. The evidence in this chapter comes from inspecting the implementation and the upstream integration test. The fixture supplies scripted model responses.

State What it holds
Model-visible history The ordered input items that feed the next request. The turn loop takes a fresh snapshot before sending one. Request input
Rollout The persisted stream of thread records. The resume path reads these records to reconstruct the thread’s model-visible history. Reconstruction entry
Compaction checkpoint A rollout record that can contain the replacement history, its context-window number, and resume metadata. CompactedItem

The useful distinction is between producing the replacement history and reconstructing it later. The main fixture compares request inputs across a legacy resume. A separate paginated fixture examines what the thread store loads.

  1. Request compaction. The fixture submits Op::Compact and waits for the compact turn to complete. Its mock provider answers the summary request with a fixed string. The production dispatcher also has provider-backed and token-budget branches. This walkthrough follows the local, model-backed branch used in the fixture. Test helper · Scripted summary · Compaction dispatch
  2. Build the replacement. The local compactor snapshots history and adds the summarization request to the input it sends. The local compactor selects always-retained items and walks recent user messages from newest to oldest within an approximate token budget. Text-only messages that fit can retain their original content. Other selected messages contribute truncated text. The assembled replacement also includes the summary and refreshed context. Summarization input · Retention logic · Assemble the replacement
  3. Install and record it. replace_compacted_history installs the new item list and captures a copy in the CompactedItem. The persistence call writes that checkpoint alongside its world-state and turn-context records. Install and construct the checkpoint · Persist rollout items
  4. Reconstruct on resume. When the newest checkpoint qualifies as a replay boundary, reconstruction can begin at its replacement history and replay the later records. The boundary check requires a replacement and a window number, plus resume metadata for non-paginated histories. If that shortcut is unavailable, reconstruction considers the full supplied rollout when selecting what to replay. Boundary selection · Replay input selection · Restore replacement and replay later records

The test sends hello world, requests compaction, then sends AFTER_COMPACT. It shuts down the thread, reopens it from the recorded rollout path, and sends AFTER_RESUME. It also forks at the selected user-message boundary and sends AFTER_FORK on that branch. Fixture flow · Resume helper

Before shutdown [retained prefix, hello world, summary, AFTER_COMPACT]
After resume [same input prefix, later items, AFTER_RESUME]
After fork [same input prefix, branch-specific later items, AFTER_FORK]

This is a schematic of the input comparison. The test removes empty user messages and the compaction prompt before comparison, aligns the fork’s thread-scoped first item ID, and checks that the normalized post-compaction input is a prefix of both later requests. It separately checks the observed user-message order and the last message on each path. This is evidence about history reconstruction for the scripted fixture. Summary fidelity remains a separate evaluation question. Normalization · Prefix assertions · Message assertions

The resume-and-fork fixture opens history from a legacy rollout path. Its prefix assertion measures reconstructed input. The same prefix could come from reading the full rollout or a shorter suffix. Legacy resume helper · Legacy history loader

The separate paginated_compaction_cold_resume_from_bounded_suffix fixture checks the loaded context directly. It expects session metadata followed by a compaction checkpoint with resume metadata, then checks that the first request after cold resume shares the replacement-history prefix of the pre-shutdown request. These two assertions cover the shape of the loaded context and the model input it produces. They leave bytes read, loading latency, and summary fidelity open. Loaded context assertion · Provider request comparison

Start with the request containing AFTER_COMPACT. Its input was captured before the provider returned the answer to that prompt. After checking the common normalized prefix, inspect the remaining user text before AFTER_RESUME or AFTER_FORK. The assertion allows zero or more whole repeats of the seeded user prefix. For example, if the seed were [A, B], a suffix of [A, B] would pass this check, while [A] would fail. With an empty seed, the suffix must also be empty. The letters are an illustration of the assertion. Resume comparison · Fork comparison

Which facts need to survive a context boundary verbatim? Give an agent a versioned file and a known decision that depends on that version. Compare full-history continuation, summary-only continuation, and a summary with the version retained as a structured field. Measure whether the next action uses the correct version, keeping the model and task fixed.

  1. Why does the test inspect the request after reopening the thread?
  2. Could the prefixes match even if the summary omitted a key fact?

Answers. The request reveals what the next model call actually received after reconstruction. A fixed summary can survive reconstruction exactly while omitting task-critical information from the original history.

Continue with Session History and Memory to distinguish saved thread history from cross-session reuse.