Skip to content

CLI can get stuck in repeated pre-sampling compact failure loop with large tool-output history #24732

Closed

Description

@boopy253

What version of Codex CLI is running?

codex-cli 0.133.0

What subscription do you have?

Pro

Which model were you using?

gpt-5.5, reasoning effort xhigh

What platform is your computer?

Darwin 25.5.0 arm64 arm

What terminal emulator and version are you using (if applicable)?

Ghostty initially. The replacement session was later moved to tmux after recovery. The failing session was an interactive Codex CLI session, not codex exec.

Codex doctor report

not available

What issue are you seeing?

A long-running Codex CLI goal session entered a repeated Failed to run pre-sampling compact loop. The process stayed alive and the session JSONL kept updating, but the TUI stopped accepting useful steering: sending /compact, continue, or other text did not create a new usable turn. The only practical recovery was to recover the objective from the session file and start a fresh Codex session.

The important part is that auto-compact appears to have triggered, but the compact request itself could not complete. From the outside this looked like a permanent retry loop before the next normal model turn.

Observed local log line repeated many times:

codex_core::session::turn: Failed to run pre-sampling compact

Sanitized session metrics at the time:

last_input_tokens: 224,320
last_total_tokens: 224,505
model_context_window: 258,400
session_jsonl_size: ~43.5 MB
function_call events: 4,350
function_call_output events: 4,350
token_count events: 2,782
thread_goal_updated events: 2,345
task_started events: 61
function outputs with original token count > 5,000: 651
large outputs near the failure: roughly 20k-21k tokens each

The loop continued for more than two hours in the logs. A sanitized count from the log tail found 115 occurrences, with the first around 2026-05-27T02:43:00Z and the last around 2026-05-27T04:48:25Z.

This was not a normal provider capacity message or a dead process:

  • The Codex wrapper and native process remained alive.
  • The TUI/session file kept recording token_count / goal update state.
  • No new meaningful task_started appeared from manual steering after it entered the loop.
  • The visible nominal context window still had apparent headroom, but compaction itself appeared unable to fit/complete.

My interpretation is that the pre-sampling compact request has its own effective request budget: compact prompt, retained history, tool schemas/metadata, hidden overhead, and output reserve must all fit. With enough high-volume tool output in history, the compact request itself can fail even though auto-compact exists and even though the visible nominal window suggests some headroom.

What steps can reproduce the bug?

I do not have a small deterministic repo reproduction, but the observed pattern was:

  1. Start an interactive Codex CLI goal session using gpt-5.5 xhigh.
  2. Let the session run for many hours with many tool calls and large tool outputs.
  3. Accumulate many function_call_output records, including hundreds above 5k original tokens and several around 20k tokens.
  4. Continue until Codex tries pre-sampling compaction before the next normal turn.
  5. Observe repeated Failed to run pre-sampling compact in the Codex TUI log.
  6. Try to recover from the TUI with Esc, /compact, continue, or a short steering message.
  7. Observe that the text does not become a normal new turn and the session remains in the compact retry loop.

The session contents were project-specific, so I cannot attach the raw JSONL, but the metrics above are from the local session file and logs with private details removed.

What is the expected behavior?

When auto-compact itself fails, Codex should not remain indefinitely in a pre-sampling compact retry loop. Expected recovery behavior could include one or more of:

  • Fall back to a more aggressive emergency compaction strategy that retains only a bounded tail plus last successful user/assistant summaries.
  • Drop or summarize large historical tool outputs before building the compact request.
  • Surface an actionable TUI state explaining that compaction failed and offer a safe /compact --aggressive or /recover path.
  • Allow manual /compact or user steering to land as a new recovery turn instead of being swallowed by the retry loop.
  • Emit clearer telemetry in the log: failing compaction request size, estimated tokens/bytes, retained history count, largest tool outputs, and whether the failure is size, timeout, remote error, or provider error.

Additional information

Related issues that appear similar but not identical:

This report is specifically about an interactive CLI session where pre-sampling compact did trigger, then repeatedly failed before a normal turn could proceed. The requested product behavior is a bounded fallback/recovery path so a long-running session can degrade gracefully instead of becoming unrecoverable.

Activity

  1. added
    bugSomething isn't working
    CLIIssues related to the Codex CLI
    contextIssues related to context management (including compaction)
    on May 27, 2026
  2. boopy253 commented on May 27, 2026

    @boopy253
    Author

    Additional context: this report was distilled from a sanitized RCA gist that I wrote after analyzing the failed local session/logs. The gist removes private project details and includes the broader reasoning about why auto-compact can fail even though it exists: https://gist.github.com/boopy253/55b6e02697a0508956ad20aa9ed6b001

  3. boopy253 commented on May 27, 2026

    @boopy253
    Author

    Thanks for the duplicate suggestions. I reviewed them.

    #24538 looks closest. This is likely in the same long-session compaction reliability area, but the signature I observed is slightly different:

    • interactive CLI session, not Desktop/App
    • not a resume-path report
    • pre-sampling compact did trigger
    • the local log repeatedly showed codex_core::session::turn: Failed to run pre-sampling compact
    • manual /compact / short steering text did not land as a normal new turn
    • the session history was dominated by high-volume tool outputs, with hundreds of function_call_output records above 5k original tokens

    So the distinct part of this report is the unrecoverable pre-sampling compact retry loop and the lack of an emergency/manual recovery path when the compact request itself cannot fit or complete.

    If maintainers consider this the same root cause as #24538 or one of the context_length_exceeded compact-payload issues, I’m happy for it to be deduplicated. I left the issue open for now because the failure signature and recovery behavior are a bit different.

  4. etraut-openai commented on Jun 29, 2026

    @etraut-openai
    Contributor

    Thanks for reporting this problem. Until recently, Codex used a separate endpoint and server-side logic for compaction. We recently switched to a more robust approach where the compaction logic is moved locally using the same endpoint as normal turns. This eliminates "remote compaction" errors and increases the stability and reliability of compaction operations. If you see further problems with compaction, please use /feedback and open a new bug report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    CLIIssues related to the Codex CLIbugSomething isn't workingcontextIssues related to context management (including compaction)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions