Skip to content

core: reset context for token budget compaction - #29743

Merged
bolinfest merged 3 commits into
mainfrom
pr29743
Jun 23, 2026
Merged

core: reset context for token budget compaction#29743
bolinfest merged 3 commits into
mainfrom
pr29743

Conversation

@bolinfest

@bolinfest bolinfest commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

Why

When Feature::TokenBudget is enabled, compaction should behave like new_context: start a fresh context window with the standard injected context, without asking the server to summarize old history and without carrying prior user or assistant messages into the next model request.

This is still a compaction operation from the client lifecycle perspective. Manual /compact and auto-compaction should keep the same observable side effects that clients and hooks expect, including compact hooks and TurnItem::ContextCompaction.

What changed

  • Added compact_token_budget to run token-budget manual and inline auto-compaction through a shared compaction lifecycle.
  • Split pending new_context requests from forced context-window startup: take_new_context_window_request() consumes pending requests, and start_new_context_window() installs a fresh context window.
  • Routed token-budget manual /compact and inline auto-compaction to install a fresh context window locally instead of calling server/local summarization.
  • Preserved compact lifecycle side effects for token-budget compaction by running pre/post compact hooks and emitting ContextCompaction item start/completion events.
  • Updated token-budget tests to assert fresh window IDs, absence of server-side compaction calls, dropped prior transcript messages/tool output after reset, and compact hook/item lifecycle behavior.

Testing

  • just test -p codex-core token_budget_context_uses_new_window_after_compaction
  • just test -p codex-core token_budget_compaction_runs_compact_hooks
  • just test -p codex-core token_budget_mid_turn_auto_compaction_resets_before_active_follow_up

Comment thread codex-rs/core/src/session/turn.rs Outdated
&& token_limit_reached
&& needs_follow_up
{
if turn_context.config.features.enabled(Feature::TokenBudget)

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.

we should probably drop this

Comment thread codex-rs/core/src/session/turn.rs Outdated
&& needs_follow_up
{
&& needs_follow_up;
if needs_follow_up && (new_context_window_requested || auto_compact_needed) {

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.

more complicated than needed

@bolinfest
bolinfest marked this pull request as ready for review June 23, 2026 23:34
@bolinfest
bolinfest requested a review from a team as a code owner June 23, 2026 23:34

@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: a00ab1bb6e

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

Comment on lines +926 to +930
crate::compact_token_budget::run_inline_auto_compact_task(
Arc::clone(sess),
Arc::clone(turn_context),
initial_context_injection,
)

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.

P2 Badge Reset token-budget windows with the current model context

When pre-turn compaction is triggered by a comp-hash change or model downshift, maybe_run_previous_model_inline_compact passes a StepContext built from the previous model. This token-budget shortcut then starts the new window with that previous TurnContext and persists it as the reference context, so the actual current turn sees a non-None reference and only appends diffs instead of rebuilding full initial context for the new model. In TokenBudget sessions that switch/downshift models, the first request after the reset can therefore keep stale full-context metadata from the old model (for example context/skill budgets or other initial-context-only sections).

Useful? React with 👍 / 👎.

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.

true, we should take as a followup.

Comment on lines +79 to +80
sess.start_new_context_window(turn_context.as_ref(), world_state)
.await;

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.

P2 Badge Restore resume baselines for token-budget /compact

When this path is used by standalone /compact, it writes a replacement history that already contains full initial context, but rollout reconstruction only adopts TurnContext baselines from real user-turn segments. After resuming a thread that ended with token-budget /compact (especially before any user turn), the replacement history is restored while reference_context_item remains missing or stale, so the next turn reinjects full initial context on top of the compacted window and duplicates token-budget/world-state context.

AGENTS.md reference: AGENTS.md:L103-L111

Useful? React with 👍 / 👎.

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.

true, we should take as a followup.

@bolinfest
bolinfest merged commit 32b65bb into main Jun 23, 2026
33 of 47 checks passed
@bolinfest
bolinfest deleted the pr29743 branch June 23, 2026 23:59
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 23, 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