Skip to content

feat(server): fold over-window histories in bounded compaction passes - #3751

Merged
inureyes merged 4 commits into
lablup:mainfrom
Jaeyeong-CHOI:feat/2523-multi-pass-compaction
Dec 4, 2026
Merged

inureyes merged 4 commits into
lablup:mainfrom
Jaeyeong-CHOI:feat/2523-multi-pass-compaction

Conversation

@Jaeyeong-CHOI

@Jaeyeong-CHOI Jaeyeong-CHOI commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Part of #2523: the multi-pass part, with the automatic summary budget it needs.

Automatic compaction on /v1/responses (context_management) now folds a history longer than the context window in bounded summary passes instead of refusing it with context_length_exceeded. The automatic summary budget moves to upstream's (1/8 of the window, 256 to 1024 tokens, with one retry at twice that, at most 1/4 of the window), because the later passes also re-summarize the previous handoff and ran past the old 1/16 budget on a real model. POST /v1/responses/compact is unchanged: one window, one pass, the client's max_output_tokens, no retry. Chat Completions / Messages compaction and the Codex compaction_trigger item are left for follow-up PRs; both build on this pass loop.

Behavior change: an automatic compaction whose input is under the window but whose summarized prefix does not fit one summary pass (prompt plus budget above the window) used to fail with 400 context_length_exceeded; it is now folded in passes like an over-window history. The automatic summary budget also changes, from 1/16 of the window (128 to 1024 tokens) to 1/8 (256 to 1024) with one retry. No CHANGELOG entry is included, since the changelog is written at release time.

#2523 acceptance criteria

  • /v1/chat/completions and /v1/messages compact an overflowing history and honor context_management, with route tests: follow-up PR.
  • compaction_trigger returns one compaction item on both arms, with route tests: follow-up PR.
  • A history over one window compacts in bounded passes; a single oversized turn still fails with context_length_exceeded: this PR.
  • docs/responses-api.md and docs/server-features.md updated: this PR updates both for multi-pass; each follow-up updates them for its part.

What changed

  • compact_history (src/server/responses_compaction.rs): when the summarized prefix does not fit one summary pass and the caller allows it (CompactionLimits::multi_pass), the prefix is folded in at most MAX_SUMMARY_PASSES (8, the bound the issue names) passes. Each pass summarizes the system/developer messages, the previous pass's handoff (as an assistant message), and the longest run of whole turns at the front of what is left that still fits one pass. Runs end only at the existing safe_boundaries (before a user turn, or after the output that closes the last open call) or at the end, so a function_call never leaves its function_call_output, and every pass consumes at least one turn. The end of each run is binary-searched (a longer run never fits where a shorter one does not), so a pass costs O(log n) counts.
  • Refusals, all context_length_exceeded, none of which replaces context: a turn too long for any pass (from the second pass on, the message names the previous handoff plus the next turn, since either may be what does not fit); a pass whose handoff is not shorter than what it read; a prefix that needs more than 8 passes; the instructions plus newest turn alone filling the window (checked before any pass runs); a compacted result that leaves no room for output.
  • CompactionBackend gains summary_fits(head): the headroom test generate_summary already makes (rendered summary prompt plus the largest budget the pass may use against the window), without generating. ResponsesCompactionBackend implements it on the count path (so a media prompt is counted on the worker), sharing one summary_request builder with summarize.
  • Automatic summary budget (src/server/routes/responses_auto_compact.rs): auto_summary_tokens is (window / 8).clamp(256, 1024) (was (window / 16).clamp(128, 1024)), and the new auto_retry_tokens is min(window / 4, 2 × budget), never below the budget, as upstream's automatic path sizes them. ResponsesCompactionBackend gains retry_tokens; when a summary generation stops at length and the retry budget is larger, summarize runs it once more with the retry budget and sums the usage of both. summary_fits reserves the retry budget, so a retry never overflows the window. A summary cut again on the retry is still 502 server_error, with a message that names both budgets instead of telling the client to raise max_output_tokens (which does not set the automatic budget). POST /v1/responses/compact passes retry_tokens: None and keeps its message and behavior.
  • Summary usage after multi-pass is summed over every pass (and retry); the returned text is the last handoff.
  • apply_auto_compaction sets multi_pass: true. POST /v1/responses/compact sets multi_pass: false and keeps its one-window, one-pass contract (history_over_one_window_fails_without_generating still holds), as upstream's standalone compact endpoint does.
  • Docs: docs/responses-api.md (Automatic compaction: budget and retry, the pass loop and its failures, the "same as compact" sentence no longer claims the same failures; the failure table; the "Not implemented" line), docs/server-features.md.

Deviations from the issue and why

  • Scope: the multi-pass acceptance criterion. Chat Completions / Messages and compaction_trigger are separate follow-ups (see the checklist); the last of them will close the issue.
  • Multi-pass applies to automatic compaction only; /v1/responses/compact still requires its input to fit one window, matching upstream ("The standalone /responses/compact endpoint still requires its input to fit").
  • Pass bound: 8, as the issue states. Current upstream compaction.py no longer has a fixed cap ("the number of passes is bounded by the input"); here every pass must still consume at least one turn and shorten its input, and the cap stays as a hard stop on summary generations per request.
  • Summary budget: the issue does not mention it; it is in this PR because multi-pass needs it (see Verification). Only the automatic path changes, as upstream's retry is automatic-only (retry_tokens = ... if automatic else summary_tokens).
  • Not ported (follow-up): upstream keeps a summary still cut after the retry as a truncated handoff with a marker instead of failing, and clips oversized tool output with an omission marker before giving up on a turn that does not fit a pass. Both are lossy and change refusal contracts (the first also on /compact upstream), so they are left for a separate PR; here such a summary is 502 and such a turn is context_length_exceeded.

Review checklist

  • Boundaries: a head that fits one pass is summarized once with multi_pass on; exactly MAX_SUMMARY_PASSES passes succeed and one more turn fails; a turn too long for any pass fails without summarizing; a later turn that fits alone but not beside the previous handoff fails after the first pass with the handoff named; a newest turn that fills the window fails before any pass; a result that still fills the window after every pass fails; a history under the window too long for one pass is folded; a pass that does not shorten its input fails; an empty pass summary is a summary failure; tool groups are never split across passes; with multi_pass off a history over one window still fails before summarizing. Budget table from window 0 to 2^20 (256/256 up to 1024 at 1/8, retry capped at 1/4 and never below the budget); a cut summary retried once at twice the budget succeeds; cut twice is 502 without a third generation; /compact does not retry.
  • Complexity: each pass binary-searches its end over the safe boundaries of what is left, so O(log n) counts per pass and at most 8 passes; every count is bounded by one window. Every automatic compaction now runs one extra summary_fits render and count of the summarized prefix before summarizing; a retry is at most one extra generation per pass.
  • Other callers: CompactionLimits is built in two places (apply_auto_compaction, POST /v1/responses/compact) and both set multi_pass explicitly; ResponsesCompactionBackend is built in the same two places and both set retry_tokens explicitly; CompactionBackend has one production implementor (ResponsesCompactionBackend) and the test fake, both implement summary_fits. summary_fits uses the same max-token resolution and window as generate_summary's pre-generation check. generate_summary has one caller (summarize) and now reports a length finish instead of refusing it, so the refusal moved into summarize with the same /compact wording.
  • No explanatory comments removed; the refusal messages keep context_length_exceeded.
  • cargo fmt --all -- --check clean.

Verification

NVIDIA GB10 (sm_121), driver 580.178.04, CUDA 13, rustc 1.97.1. Base 9402cf893; commit tree b75db775. fmt, check_used_by.py, check_used_by_test.sh, check_env_var_docs.py and the responses_ tests ran on b75db775. Clippy, the full gate and the live binary ran on 42bc549a, which differs from it only by a CHANGELOG entry since removed (no code, test or build script reads CHANGELOG.md). Every other command below ran on 42bc549a unless it says otherwise.

  • Fail before, on unmodified 9402cf893 plus only the route tests of this commit (responses_auto_compact_tests.rs without the budget-table unit test, which names functions main does not have, and responses_compact_route_tests.rs): cargo test --locked --profile test-fast --features cuda --lib -- responses_auto_compact_tests responses_compact_route_tests → 28 passed, 3 failed: a_history_over_the_window_is_folded_in_bounded_passes (400 input is 2621 tokens, above the 2048-token context window), a_summary_cut_at_its_budget_is_retried_once_at_twice_the_budget (502 the summary reached max_output_tokens (256)), a_summary_still_cut_after_the_retry_is_a_bad_gateway (the message tells the client to raise max_output_tokens). The new /compact test (a_summary_cut_at_max_output_tokens_is_refused_without_a_retry) passes on the base, as it should: it pins the unchanged contract.
  • Fail before for the new core tests: they need CompactionLimits::multi_pass, which only this commit adds, so they were run against the previous revision of this branch (multi-pass without the budget change, the retry and the second-pass message), with every test of this commit except the budget table: 298 passed, 4 failed. a_later_pass_whose_handoff_and_turn_do_not_fit_names_the_handoff fails there (a turn of the history is too long for one summary pass), the two retry route tests fail as above, and responses_background_cancel_tests::cancel_while_in_progress_returns_the_partial_output failed once there (conversation; it passed in an identical earlier run of that revision and in every run on this commit); a_compacted_result_that_still_fills_the_window_fails and a_history_under_the_window_too_long_for_one_pass_is_folded_too cover multi-pass branches and pass on both.
  • Pass after: cargo test --locked --profile test-fast --features cuda --lib -- responses_ → 303 passed, 0 failed (on 42bc549a, and three times on b75db775). One further run on b75db775 failed only responses_background_route_tests::previous_response_id_waits_for_the_job_and_delete_refuses_an_active_job (conversation), a background-job timing test this change does not touch; -- responses_background then passed 53/53 five times in a row, and the full gate (one test thread) passed it.
  • cargo fmt --all -- --check: clean.
  • cargo clippy --locked --workspace --all-targets --features cuda -- -D warnings: clean.
  • python3 scripts/ci/check_used_by.py, bash scripts/ci/check_used_by_test.sh, python3 scripts/ci/check_env_var_docs.py: pass.
  • make verify-test-cuda (cargo test --workspace --profile test-fast --features cuda --no-fail-fast -- --test-threads=1): 207 test binaries, 18,908 passed, 2 failed, 578 ignored; the mlxcel lib binary 14,309 passed, 0 failed, 310 ignored. The two failures are mlx_test_guard_coverage::mlx_evaluating_lib_tests_take_mlx_test_guard (flags only src/vision/encoders/nemotron_h_nano_omni_tests.rs:276) and step3p7_parity::text_decode_delegates_sequence_local_native_batching (assert!(!wrapper.supports_padded_prefill())); both fail the same way on unmodified 9402cf893 (same build directory, those two targets), neither file is touched here, and both were pre-existing on the base; current main fixes both (feat(speculative): add DeepSeek-V4.1 bundled DSpark drafter (#3262) #3686, test: restore MLX guard scan and no-GPU catalog test gates #3765).
  • git merge-tree --write-tree origin/main HEAD: merges cleanly onto current origin/main (5bf7b8628). On the merged tree, the Python tooling tests (python3 -m unittest discover -s tests -p 'test_*.py'), the scripts/ci checks CI runs and cargo fmt --all -- --check pass.

Live server

Both binaries built with cargo build --locked --release --features cuda --bin mlxcel (different sha256), the fix binary from this commit's tree. mlxcel serve -m Qwen3-4B-Instruct-2507-NVFP4 --ctx-size 8192 --parallel 1; the checkpoint is the llmcompressor NVFP4 export of Qwen/Qwen3-4B-Instruct-2507, snapshot revision 3b387679aa88ca4393ab6efc34d71616b61bc57e. Requests on /v1/responses with context_management: [{"type":"compaction","compact_threshold":4000}], max_output_tokens: 64, temperature: 0. Each case ran twice, non-streamed and streamed (4 requests per case and binary).

  • A: system message, 6 whole turns of about 1.7K tokens each (a different fact in each), then a question about the fact in the first turn: 10590 tokens.
  • B: the same history, but the newest user turn alone is 11066 tokens (21636 total).
  • C: 4 of those turns plus a shorter fifth, then the same question: 7952 tokens, under the window, but the summarized prefix plus a summary budget does not fit it.
Case (8192 window) 9402cf893, runs 1 and 2, both arms this commit, runs 1 and 2, both arms
A 400 context_length_exceeded: input is 10590 tokens, above the 8192-token context window (4/4) 200, output [compaction, message], answer "The project code name is BLUE HERON." (4/4); 2 passes each (5282 → 464-532, then 5750-5818 → 511-760 tokens); 10590 → 533-782 tokens; 23-31 s
B 400, input is 21636 tokens, above the 8192-token context window (4/4) 400 context_length_exceeded: the instructions and the newest turn alone are 11078 tokens, which leaves no room in the 8192-token context window, before any pass (4/4)
C 400 context_length_exceeded: the summary request needs 8059 prompt tokens plus 512 output tokens, above the 8192-token context window (4/4) 200, [compaction, message], "The project code name is BLUE HERON." (4/4); 2 passes (5282 → 463-494, 3111-3142 → 549-570); 7952 → 571-592 tokens; 22-23 s

No pass needed the retry at this window: every handoff (463-760 tokens) fit the first 1024-token budget. With the 1/16 budget this PR replaces (512 at 8192), the same A requests ended in 502 at pass 2 in 3 of 4 requests (the summary reached max_output_tokens (512) before it finished).

The retry path was exercised on the same binary and checkpoint at --ctx-size 4096 (budget 512, retry 1024), where one 1.7K-token turn takes most of a pass. main refused all 12 requests with 400 (above the 4096-token context window). With this commit, B was refused before any pass as above (4/4). A and C ran one turn per pass, and most passes reached 512 tokens and were retried (logged summary reached its budget; retrying once). 6 of the 8 A/C requests reached 1024 on a retry at pass 4, 5 or 6 and ended in 502 server_error (the compaction summary reached its 512-token budget, and its 1024-token retry, before it finished; original context preserved, 89-159 s). The other 2 (C, streamed) completed after 5 passes (7952 → 772 and 838 tokens) but had lost the first turn's fact: their answers were "Project code name: Aurora." and "The project code name is not provided in the given context."

Limits

  • Each pass is a full summary generation (two when it retries) before the answer starts, and the stream has not opened yet, so a streamed client receives no bytes until every pass has finished (22-31 s for two passes on the 4B model at an 8192 window; up to 159 s at 4096).
  • A newest turn that nearly fills the window is refused only after the passes ran (the pre-check covers the instructions plus newest turn alone; the result check catches the rest); upstream checks the same way.
  • Small windows: when one turn takes most of a pass (4096 window above), the handoff grows with every pass. This 4B model's handoffs then passed the 1024-token retry at pass 4 to 6 in 6 of 8 folds (502), and the two folds that completed after 5 passes had lost a fact from the first turn. Bounded passes keep the context and the request bounded; they do not make a long fold lossless.
  • A summary still cut after its retry is 502; upstream would keep it truncated with a marker (see Deviations). Oversized tool output is not clipped before giving up on a turn (upstream does).
  • Chat Completions / Messages compaction and compaction_trigger are not in this PR.

Automatic Responses compaction (context_management) now folds a summarized
prefix that does not fit one summary pass in at most eight passes, so a
history longer than the context window compacts instead of failing with
context_length_exceeded. Each pass summarizes the instructions, the previous
handoff and the longest run of whole turns that fits, split only at the
existing safe boundaries, and must come out shorter than its input. A turn too
long for any pass, a newest turn that alone fills the window, or a prefix that
needs more than eight passes still fails with context_length_exceeded.

The automatic summary budget follows upstream: 1/8 of the window (256 to 1024
tokens, was 1/16, 128 to 1024), and a summary cut at it is retried once with
twice the budget, at most 1/4 of the window, before failing with 502. Every
pass reserves room for the retry budget. POST /v1/responses/compact keeps its
one-window, one-pass contract, its max_output_tokens budget and no retry.

Part of lablup#2523.
@inureyes inureyes added status:review Under review type:enhancement New features, capabilities, or significant additions priority:medium Medium priority labels Dec 4, 2026
Automatic compaction with multi-pass accepted any history length and only failed at the eighth pass, so a crafted history could cost eight summary generations plus a re-tokenization of an unbounded prefix per search step. A history above nine context windows can never fold (each pass reads at most one window and the retained turns fit one more), so it is now refused with context_length_exceeded before any count or generation.
@inureyes

inureyes commented Dec 4, 2026

Copy link
Copy Markdown
Member

Hardening review

Reviewed against the PR body and issue #2523 (multi-pass acceptance criterion only; Chat Completions / Messages and compaction_trigger stay open for follow-ups).

Findings

  • Opt-in: multi-pass runs only for automatic compaction, which requires the client to send context_management. POST /v1/responses/compact keeps multi_pass: false and its one-window contract. The behavior change (an under-window history whose prefix does not fit one pass is now folded instead of 400) and the new 1/8 budget with one retry are documented in docs/responses-api.md.
  • Bounded work: at most 8 passes, each at most two generations (16 generations total), each pass prompt checked by summary_fits against the window with the retry budget reserved. Every pass must consume at least one turn and shorten its input, so there is no unbounded loop.
  • Structure preservation: system/developer messages lead every pass and the result; runs end only at safe_boundaries, so function_call / function_call_output pairs stay together; the newest turns are retained and the "newest turn alone fills the window" case is refused before any pass.
  • Gap fixed (cost amplification): with multi_pass on, the only up-front length gate was removed, so a history of any length proceeded. A crafted history far beyond what 8 passes can fold would run all 8 passes (up to 16 summary generations) before failing at the pass bound, and each binary-search step re-rendered and re-tokenized a prefix of the whole oversized input.

Fixes pushed

  • 9c89f6723 fix(server): refuse a history no pass bound can fold before any pass. A history above (MAX_SUMMARY_PASSES + 1) context windows can never fold (each pass reads at most one window of turns and the instructions plus retained turns fit one more), so it now fails with context_length_exceeded before any count or generation. New test a_history_beyond_what_the_pass_bound_can_fold_fails_before_any_work; docs sentence added.
  • 1779dd3fe merge of current origin/main (clean).
  • 8dacd0daa rustfmt of the new bound.

Validation (on the merged tree)

  • cargo test --locked --profile test-fast --lib -- responses_ --test-threads=4: 304 passed, 0 failed (303 existing plus the new test).
  • cargo clippy --locked --all-targets -- -D warnings: clean.
  • cargo fmt --all -- --check: clean.
  • python3 scripts/ci/check_env_var_docs.py: OK (no new env vars or startup flags; no runtime-settings or WebUI changes).

Residual notes

  • Cancellation is unchanged from single-pass: a background cancel or a dropped foreground request stops the loop at the next await, so at most the in-flight summary generation completes; the next pass does not start.
  • As the PR body states, small windows can make long folds lossy and a summary still cut after its retry is 502; both are documented limits, not regressions.

@inureyes
inureyes merged commit 042ab75 into lablup:main Dec 4, 2026
36 checks passed
@inureyes inureyes added status:done Completed and removed status:review Under review labels Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:medium Medium priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants