Skip to content

fix: retry rejected previous-model compaction with selected model - #30319

Merged
celia-oai merged 8 commits into
mainfrom
codex/repro-retired-model-compaction
Jul 7, 2026
Merged

fix: retry rejected previous-model compaction with selected model#30319
celia-oai merged 8 commits into
mainfrom
codex/repro-retired-model-compaction

Conversation

@celia-oai

@celia-oai celia-oai commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Why

Pre-sampling compaction intentionally uses the previous turn's model when the compaction compatibility hash changes or when switching to a model with a smaller context window. This keeps compaction aligned with the settings that produced the history, but it can block the next turn when a resumed ChatGPT thread still references a model slug that has since been retired. The Codex backend rejects that compaction request before the user's currently selected model gets a chance to sample.

This PR lets those threads recover without changing previous-model compaction behavior for API-key authentication or custom providers. It is stacked on #31316, which is a behavior-preserving extraction of the individual remote compaction attempts; this PR contains the fallback behavior.

What changed

  • For automatic previous-model compaction, capture the selected model's request context when using ChatGPT authentication with the OpenAI provider and the selected model differs from the previous model.
  • If the previous-model attempt returns an InvalidRequest, retry compaction once with the selected model for both /responses/compact and Responses Compaction V2.
  • Complete history processing, lifecycle events, and token accounting with the context of the model that successfully compacted the thread.
  • If the fallback also fails, return the original previous-model error so the retry does not change the user-visible failure.
  • Record fallback attempts with reason, implementation, and outcome telemetry.
  • Leave API-key authentication, custom providers, same-model turns, and non-InvalidRequest failures on their existing paths.

Testing

  • just test -p codex-core -E 'test(pre_sampling_compact) | test(model_unavailable_error)' (10 tests)
  • Added integration coverage for a resumed thread whose model was renamed, a model downshift using Responses Compaction V2, and API-key authentication with a custom provider.

@celia-oai
celia-oai changed the base branch from release/0.142 to main June 27, 2026 01:09
@celia-oai
celia-oai force-pushed the codex/repro-retired-model-compaction branch from aff99a5 to ccffcff Compare June 27, 2026 01:14
@celia-oai
celia-oai force-pushed the codex/repro-retired-model-compaction branch from 60090a7 to e8b557b Compare July 6, 2026 23:25
@celia-oai celia-oai changed the title [codex] Add retired model compaction repro [codex] Retry retired-model compaction with selected model Jul 6, 2026
@celia-oai
celia-oai changed the base branch from main to codex/refactor-remote-compaction-attempts July 6, 2026 23:26
@celia-oai
celia-oai force-pushed the codex/refactor-remote-compaction-attempts branch from 4809aa8 to 594fed1 Compare July 6, 2026 23:37
@celia-oai
celia-oai force-pushed the codex/repro-retired-model-compaction branch from e8b557b to 6d33a24 Compare July 6, 2026 23:37
@celia-oai
celia-oai force-pushed the codex/refactor-remote-compaction-attempts branch from 594fed1 to cf62e27 Compare July 7, 2026 00:24
@celia-oai
celia-oai force-pushed the codex/repro-retired-model-compaction branch 3 times, most recently from e7ee322 to 0db34c3 Compare July 7, 2026 05:53
@celia-oai
celia-oai force-pushed the codex/refactor-remote-compaction-attempts branch from ec16a25 to 1f3200a Compare July 7, 2026 06:05
@celia-oai
celia-oai force-pushed the codex/repro-retired-model-compaction branch from 0db34c3 to 262b29f Compare July 7, 2026 06:06
@celia-oai celia-oai changed the title [codex] Retry retired-model compaction with selected model core: retry rejected previous-model compaction with selected model Jul 7, 2026
@celia-oai
celia-oai marked this pull request as ready for review July 7, 2026 06:37
@celia-oai
celia-oai requested a review from a team as a code owner July 7, 2026 06:37
@celia-oai celia-oai changed the title core: retry rejected previous-model compaction with selected model fix: retry rejected previous-model compaction with selected model Jul 7, 2026
@celia-oai
celia-oai requested a review from aibrahim-oai July 7, 2026 06:38

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a38022e872

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

turn_state,
&compaction_trace,
compaction_metadata,
analytics_details,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3 Badge Reset compaction analytics before retrying fallback

When the previous-model attempt fails after local prompt trimming, run_remote_compact_attempt has already mutated analytics_details.active_context_tokens_before based on a cloned history that never becomes the successful compaction input. Passing that same mutable details object into the selected-model retry means fallback scenarios with oversized function-call outputs can subtract the failed attempt's estimated deletions from the single tracked compaction event, and then subtract again (or keep a subtraction the fallback did not need), underreporting the pre-compaction token count; snapshot the details before the first attempt or only apply trimming deltas from the attempt that succeeds.

Useful? React with 👍 / 👎.

Comment thread codex-rs/core/src/compact_remote_v2.rs Outdated
fallback_result.as_ref().err(),
);
match fallback_result {
Ok(attempt) => (attempt, &fallback_step_context.turn),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3 Badge Run PostCompact hooks with the fallback model context

When this fallback branch succeeds, the inner implementation uses fallback_step_context.turn for history replacement and item completion, but the outer run_remote_compact_task_inner still invokes run_post_compact_hooks with the original previous-model turn_context. In ChatGPT/OpenAI fallback recoveries with configured PostCompact hooks, the hook payload's model remains the retired/previous model rather than the selected model that actually compacted the thread, so hooks that audit or gate by model run on stale data; propagate the successful compaction context back to the hook layer or run the hook with compaction_turn_context.

Useful? React with 👍 / 👎.

@celia-oai
celia-oai requested a review from sayan-oai July 7, 2026 18:42
Comment thread codex-rs/core/src/compact_remote_v2.rs Outdated
Comment thread codex-rs/core/tests/suite/compact.rs

@sayan-oai sayan-oai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense to me, might still be worth getting @aibrahim-oai's thoughts

celia-oai added a commit that referenced this pull request Jul 7, 2026
## Why
This PR is a behavior-preserving refactor only. It does not add a
fallback, change which model is used for compaction, or otherwise change
compaction behavior. The behavioral change is implemented in the stacked
follow-up, #30319.

Pre-sampling compaction deliberately uses the previous turn's context
when the compaction compatibility hash changes or when switching to a
model with a smaller context window. That preserves the model settings
that produced the history being compacted, but the previous context is
not always usable. For example, a resumed thread can still reference a
model slug that has since been retired, causing compaction to fail
before the currently selected model can sample.

#30319 addresses that failure mode by retrying compaction with the
current turn's selected model when the backend rejects the
previous-model attempt. This PR performs only that preparatory refactor.

## What changed

- Extracted one legacy `/responses/compact` request attempt into
`compact_remote_request.rs`.
- Extracted one Responses-based remote compaction request attempt into
`compact_remote_v2_attempt.rs`.
- Kept hooks, lifecycle events, analytics, window advancement, history
processing and installation, and error behavior unchanged in the
existing orchestration paths.
- Preserved standalone Responses-based compaction's owned client-session
lifetime through lifecycle completion.

## Testing

- `just test -p codex-core -E 'test(remote_compact)'` (22 tests)
Base automatically changed from codex/refactor-remote-compaction-attempts to main July 7, 2026 19:57
@celia-oai
celia-oai force-pushed the codex/repro-retired-model-compaction branch from a38022e to 5e2acb5 Compare July 7, 2026 20:01
@celia-oai
celia-oai merged commit 172ab26 into main Jul 7, 2026
35 checks passed
@celia-oai
celia-oai deleted the codex/repro-retired-model-compaction branch July 7, 2026 21:26
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 7, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants