Skip to content

fix: preserve live context during memory compression - #1048

Open
vaibhavmashal wants to merge 1 commit into
alibaba:mainfrom
vaibhavmashal:fix-compression-zone-issues
Open

fix: preserve live context during memory compression#1048
vaibhavmashal wants to merge 1 commit into
alibaba:mainfrom
vaibhavmashal:fix-compression-zone-issues

Conversation

@vaibhavmashal

Copy link
Copy Markdown

Problem

Three-zone compression (internal/llmloop/compression.go) discards live context it is designed to preserve, in three related ways:

  1. When every round fits the token budget, the "everything fits" branch marks the whole tail as the compress zone, causing every round to be summarized.
  2. The frozen zone's tokens are never reserved from the budget, allowing the active zone to overfill past the warning threshold.
  3. A missing compression template truncates the conversation to 2 messages instead of leaving it alone.

This leads to unintended truncation and summarization of recent context.

Solution

This PR applies the following fixes:

  • Represents the empty compress zone as compressEnd == frozenEnd, rather than compressEnd == len(messages).
  • Reserves the token count of messages[:frozenEnd] in addition to prevSummaryTokenEstimate when computing the active zone size.
  • Makes runCompression return the conversation unchanged when no template is configured instead of slicing it down to the first 2 messages.

Changes Made

  • internal/llmloop/compression.go:
    • Modified partitionMessages to compute and pass the frozen zone token count to computeActiveZoneSize.
    • Modified partitionMessages to set compressEnd = result.frozenEnd instead of len(messages) when everything fits in the active zone.
    • Modified runCompression to return msgs instead of msgs[:min(len(msgs), 2)] when the compression template is empty.
  • internal/llmloop/compression_zone_repro_test.go: Added reproduction tests to ensure these edge cases are covered.

Testing

  • Added automated unit tests to verify each edge case: TestRepro_EverythingFitsSummarizesLiveTail, TestRepro_FrozenZoneNotReservedFromBudget, and TestRepro_MissingTemplateTruncatesConversation.

Fixes #838

Copilot AI lite review requested due to automatic review settings August 23, 2026 20:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@github-actions

Copy link
Copy Markdown
Contributor

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

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

3 participants