Skip to content

Fix remote compaction estimator/payload instruction small mismatch#10692

Merged
charley-oai merged 3 commits intomainfrom
fix/remote-compact-instruction-parity
Feb 5, 2026
Merged

Fix remote compaction estimator/payload instruction small mismatch#10692
charley-oai merged 3 commits intomainfrom
fix/remote-compact-instruction-parity

Conversation

@charley-oai
Copy link
Collaborator

@charley-oai charley-oai commented Feb 5, 2026

Summary

This PR fixes a deterministic mismatch in remote compaction where pre-trim estimation and the /v1/responses/compact payload could use different base instructions.

Before this change:

  • pre-trim estimation used model-derived instructions (model_info.get_model_instructions(...))
  • compact payload used session base instructions (sess.get_base_instructions())

After this change:

  • remote pre-trim estimation and compact payload both use the same BaseInstructions instance from session state.

Changes

  • Added a shared estimator entry point in ContextManager:
    • estimate_token_count_with_base_instructions(&self, base_instructions: &BaseInstructions) -> Option<i64>
  • Kept estimate_token_count(&TurnContext) as a thin wrapper that resolves model/personality instructions and delegates to the new helper.
  • Updated remote compaction flow to fetch base instructions once and reuse it for both:
    • trim preflight estimation
    • compact request payload construction
  • Added regression coverage for parity and behavior:
    • unit test verifying explicit-base estimator behavior
    • integration test proving remote compaction uses session override instructions and trims accordingly

Why this matters

This removes a deterministic divergence source where pre-trim could think the request fits while the actual compact request exceeded context because its instructions were longer/different.

Scope

In scope:

  • estimator/payload base-instructions parity in remote compaction

Out of scope:

  • retry-on-context_length_exceeded
  • compaction threshold/headroom policy changes
  • broader trimming policy changes

Codex author:

codex fork 019c2b24-c2df-7b31-a482-fb8cf7a28559

Add a shared ContextManager estimator that accepts explicit base instructions, keep the existing estimator as a wrapper, and use session base instructions for both remote pre-trim estimation and compact payload construction.\n\nAdd regression coverage to verify remote compaction uses session override instructions and trims consistently when those instructions increase estimated payload size.
@charley-oai
Copy link
Collaborator Author

@codex review this

@charley-oai charley-oai changed the title Fix remote compaction estimator/payload instruction mismatch Fix remote compaction estimator/payload instruction small mismatch Feb 5, 2026
@chatgpt-codex-connector
Copy link
Contributor

Codex Review: Didn't find any major issues. Nice work!

ℹ️ 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".

Copy link
Collaborator

@sayan-oai sayan-oai left a comment

Choose a reason for hiding this comment

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

looks good. one question, do we also need to update the estimate_token_count call in recompute_token_usage that runs after compaction (here)?

@charley-oai
Copy link
Collaborator Author

@codex review this

@chatgpt-codex-connector
Copy link
Contributor

Codex Review: Didn't find any major issues. Bravo.

ℹ️ 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".

@charley-oai charley-oai merged commit dc7007b into main Feb 5, 2026
32 checks passed
@charley-oai charley-oai deleted the fix/remote-compact-instruction-parity branch February 5, 2026 07:24
@github-actions github-actions bot locked and limited conversation to collaborators Feb 5, 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