Skip to content

fix(core): stop auto-compaction looping when compaction cannot get below the limit (WI-2832) - #42

Merged
thedancingdeveloper merged 1 commit into
fork/mainfrom
wi-2832-reapply
Nov 19, 2026
Merged

thedancingdeveloper merged 1 commit into
fork/mainfrom
wi-2832-reapply

Conversation

@thedancingdeveloper

@thedancingdeveloper thedancingdeveloper commented Nov 18, 2026 •

Copy link
Copy Markdown

Replacement for #36, which is based on a 2026-10-19 merge-base and is now CONFLICTING (merging today's fork/main reports 4678 changed files of base drift). This branch carries the same change re-applied as a patch onto fork/main 5c4381bb4; #36 is left untouched for the reviewer to close.

What it does

Before: if the part of the prompt compaction cannot shrink (tool specs, instructions, retained context) was already at or over model_auto_compact_token_limit, every request after a compaction was over the limit again, so the turn looped compaction -> one tool call -> compaction (235 compactions in one observed turn).

After: immediately after a context-limit compaction, the first sampling request is checked against the limit. If that prompt alone is still at or over the limit (body after prefix for BodyAfterPrefix, or the full usable window), the turn ends with a warning naming the token count and the limit, plus ContextWindowExceeded, instead of compacting again.

The flag that marks "a context-limit compaction just ran" is set inside the Ok(true) arm before the continue that restarts the turn loop. The first re-apply set it after that continue, where it was dead code, and the loop still recurred.

Files

  • codex-rs/core/src/session/turn.rs — the guard, compaction_left_prompt_over_limit, prompt_over_limit
  • codex-rs/core/src/session/turn_tests.rs — unit tests for the scope arithmetic
  • codex-rs/core/tests/suite/compact.rs — auto_compact_stops_when_compaction_cannot_get_below_limit, plus the repeated-compaction fixture adaptation (see below)
  • codex-rs/core/tests/suite/hooks.rs — the existing mid-turn compaction hook fixture adapted to the new contract (see below)
  • FORK.md — one row in the fork-only table
  • .github/workflows/fork-ci.yml — the libcap install step on container-backed self-hosted runners (carried from fix(core): stop auto-compaction looping when compaction cannot get below the limit (WI-2832) #36)

Fixture adaptations

Two fixtures asserted the old loop: two consecutive over-limit prompts, each compacting. Under the new contract the first of those stops the turn, so each fixture now runs one under-limit continuation before the next over-limit step. The number of compactions and hook drains is unchanged; the tests still prove their original property.

  • hooks.rs::mid_turn_auto_compact_session_start_hooks_run_before_each_continuation: one under-limit continuation before the second over-limit step, so two compactions still happen and each drains exactly one session-start hook.
  • compact.rs::multiple_auto_compact_per_task_runs_after_token_limit_hit: the fixture's work steps report 270_000 total tokens, which is still above this model's model_auto_compact_token_limit of 244_800, so the guard correctly stopped the turn after the first compaction. An under-limit (100_000) continuation is inserted after the first and second compactions; the third compaction and the final response are unchanged, so the test still proves that legitimate repeated compactions run. The guard's stop path is covered by auto_compact_stops_when_compaction_cannot_get_below_limit.

Regenerating the fixture's expected_requests_inputs is what makes that diff look larger: the array is now nine entries (3 compactions + 2 continuations + the final request), each carrying the same per-request comment the original had.

Verification

Run the way CI runs it: every test job in this repo sets RUST_MIN_STACK=8388608. Without it, debug-build futures overflow the 2 MiB tokio worker stack; that is an environment fact, not a property of this change.

  • cargo fmt --all -- --check — clean
  • cargo clippy -p codex-core --all-targets -- -D warnings — clean
  • cargo test -p codex-core --lib turn — 359 passed, 0 failed
  • cargo test -p codex-core --test all suite::compact:: — 89 passed, 0 failed, 1 ignored
  • cargo test -p codex-core --test all suite::hooks:: — 87 passed, 6 failed, the identical six names fail on unmodified fork/main 5c4381bb4 on the same host
  • cargo test -p codex-core --test all (whole binary) — 2540 passed, 665 failed, 8 ignored; unmodified fork/main on the same host: 2538 passed, 666 failed, 8 ignored. The unique failing-name set on this branch (636) is a strict subset of fork/main's (637): zero branch-only failures. The residual suite failures are sandbox/harness/environment failures that also fail on the baseline.
  • The previously red required test now passes: suite::compact::multiple_auto_compact_per_task_runs_after_token_limit_hit

For the reviewer

  1. The two fixture adaptations are described above. Please confirm the shape keeps each test's intent rather than deleting a compaction; the guard's own stop path has its own test.
  2. BodyAfterPrefix arithmetic in prompt_over_limit — it subtracts auto_compact_window_prefill_tokens from the last prompt and compares against the scope limit, and separately checks the full window. Worth a second pair of eyes.
  3. Not touched: fix(core): stop auto-compaction looping when compaction cannot get below the limit (WI-2832) #36 is unchanged. Whether to close it or retarget it is a review call.

Known follow-ups (non-blocking, not addressed here)

  • The BodyAfterPrefix warning prints total prompt tokens against the body limit and names the scope limit even when the window limit tripped.
  • The pre-turn flag also fires for async-candidate compactions and has no integration test.

Nothing was deployed; no prod or dev image was rebuilt from this branch.

…low the limit (WI-2832)

Re-applied onto fork/main (5c4381b) instead of merging PR #36's 2026-10-19
base, which produced 4678 changed files of drift. The real change is 6 files.

The guard flag is set inside the `Ok(true)` arm of the compaction match, before
the `continue` that restarts the loop; setting it after that `continue` (as the
first re-apply did) meant it was never observed and the loop still recursed.

Two existing fixtures asserted the old loop (two consecutive over-limit prompts
each compacting). Under the new contract the turn stops after the first
ineffective compaction, so each fixture now runs an under-limit continuation
before the next over-limit step: the same number of compactions still happens,
and every hook still runs.

- `mid_turn_auto_compact_session_start_hooks_run_before_each_continuation`:
  one under-limit continuation before the second over-limit step.
- `multiple_auto_compact_per_task_runs_after_token_limit_hit`: the fixture's
  work steps report 270_000 tokens, which is still above this model's 244_800
  auto-compact limit, so the new guard correctly stopped the turn. An
  under-limit (100_000) continuation is inserted after the first and second
  compactions; the third compaction and the final response are unchanged, so
  the test still proves legitimate repeated compactions run. The guard's stop
  path is covered separately by
  `auto_compact_stops_when_compaction_cannot_get_below_limit`.

Verification (CI runs tests with RUST_MIN_STACK=8388608; without it the
debug-build futures overflow the 2 MiB tokio worker stack, which is why the
repo sets it in every test job):
- cargo fmt --all -- --check: clean
- cargo clippy -p codex-core --all-targets -- -D warnings: clean
- cargo test -p codex-core --lib turn: 359 passed, 0 failed
- cargo test -p codex-core --test all suite::compact::: 89 passed, 0 failed,
  1 ignored
- cargo test -p codex-core --test all suite::hooks::: 87 passed, 6 failed —
  the identical six names fail on unmodified fork/main 5c4381b
- cargo test -p codex-core --test all (whole binary): 2540 passed, 665 failed,
  8 ignored; unmodified fork/main on the same host: 2538 passed, 666 failed.
  The unique failing-name set on this branch (636) is a strict subset of
  fork/main's (637) — zero branch-only failures. The residual failures are
  sandbox/harness/environment failures that also fail on the baseline.
- the previously red test passes:
  suite::compact::multiple_auto_compact_per_task_runs_after_token_limit_hit
@thedancingdeveloper
thedancingdeveloper added this pull request to the merge queue Nov 19, 2026
Merged via the queue into fork/main with commit 4aa1c63 Nov 19, 2026
5 checks passed
@thedancingdeveloper
thedancingdeveloper deleted the wi-2832-reapply branch November 19, 2026 03:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant