Skip to content

fix(llmloop): bound memory-compression request to a safe token ceiling - #1170

Open
nvp3399 wants to merge 1 commit into
alibaba:mainfrom
nvp3399:fix/compression-request-context-overflow
Open

nvp3399 wants to merge 1 commit into
alibaba:mainfrom
nvp3399:fix/compression-request-context-overflow

Conversation

@nvp3399

@nvp3399 nvp3399 commented Sep 4, 2026 •

Copy link
Copy Markdown

Summary

runCompression builds its summarization prompt from the entire compress zone (msgs[frozenEnd:compressEnd]) with no upper bound on size. That zone is otherwise unconstrained — it holds whatever accumulated between the frozen and active zones, which can include many rounds of tool-call output (e.g. broad code_search/file_read results across a large repository). When that content grows large enough, the resulting compression request can itself exceed the model's real context window, so the LLM call fails outright and the whole review aborts instead of compressing.

Reproduced against a real Azure-hosted DeepSeek deployment: a single compression request grew to ~1.26M tokens against a 1,048,576-token model context limit. Azure rejected it with a deterministic "input longer than context length" 400, which open-code-review then surfaces as an opaque "Context compression exceeded threshold, stopping" and fails every file in the review.

This is a real-world production failure I hit and traced end-to-end (proxied/captured the actual outgoing request and Azure's response to confirm the exact byte size and error).

Fix

Adds boundCompressZoneStart, which trims the compress zone from its oldest end until it fits a fixed maxCompressionContextTokens ceiling (200K — independent of the per-file MaxTokens template setting, and sized well under real-world model context windows), always keeping at least the single newest message so the summarization call never ends up with an empty range. A <truncation_notice> is prepended when trimming occurs, so the summary's consumer knows some history was dropped rather than summarized.

Relation to #838

This addresses the unbounded-compress-zone-size symptom described in #838 — an oversized compression request can be produced when the compress zone's own accounting lets it grow past any sane threshold. I have not independently verified the "everything fits but still gets marked for compression" or "template-less truncation" cases also described in that issue, so I'm not claiming this closes #838 outright — it's a defensively-scoped fix for the specific failure mode above.

Testing

  • Existing internal/llmloop test suite passes unchanged.
  • Added TestBoundCompressZoneStart_EverythingFits, TestBoundCompressZoneStart_TrimsOldestFirst, TestBoundCompressZoneStart_SingleMessageExceedsLimit.
  • Full go build ./... is clean.
  • Re-ran my original failing reproduction (previously a guaranteed 400 on the first or second compression attempt) against the patched binary: every compression call across multiple runs succeeded, with the new truncation path visibly engaging on requests that would otherwise have overflowed the model's context window.

runCompression built its summarization prompt from the entire compress
zone (msgs[frozenEnd:compressEnd]) with no upper bound on its size. That
zone's size is otherwise unconstrained -- it holds whatever accumulated
between the frozen and active zones, which can include many rounds of
tool-call output (e.g. broad code_search/file_read results across a
large repository). When that content grows large enough, the resulting
compression request can itself exceed the model's real context window,
so the LLM call fails outright and the whole review aborts instead of
compressing.

Reproduced against a real Azure-hosted DeepSeek deployment: a single
compression request grew to ~1.26M tokens against a 1,048,576-token
model context limit, and Azure rejected it with a deterministic
"input longer than context length" 400 -- which open-code-review then
surfaces as an opaque "Context compression exceeded threshold, stopping"
and fails every file in the review.

Add boundCompressZoneStart, which trims the compress zone from its
oldest end until it fits a fixed maxCompressionContextTokens ceiling
(200K, independent of the per-file MaxTokens template setting and sized
well under real-world model context windows), always keeping at least
the single newest message so the summarization call is never left with
an empty range. A <truncation_notice> is prepended when trimming occurs.

Verified: existing internal/llmloop tests + new
TestBoundCompressZoneStart_* pass; a full `go build ./...` is clean; and
the original reproduction (previously a guaranteed 400 on the first or
second compression attempt) now completes every compression call
successfully, with the new truncation path visibly engaging on requests
that would otherwise have overflowed.

This addresses the unbounded-compress-zone-size symptom described in
alibaba#838 (an oversized compression request can be produced when the
compress zone's own accounting lets it grow past any sane threshold).
It does not claim to resolve every defect reported in alibaba#838 -- I did not
independently verify the "everything fits but still gets marked for
compression" or "template-less truncation" cases described there, so
this is a partial, defensively-scoped fix for the specific failure mode
above rather than a full fix for that issue.

Relates to alibaba#838

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01ANbui63NSJjCE9AZVsBk12
@CLAassistant

CLAassistant commented Sep 4, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s).

This branch has not been deployed

No deployments
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.

Three-zone compression summarizes or drops live rounds it is meant to preserve

2 participants