Skip to content

fix(llmloop): keep live rounds out of compression - #823

Closed
Fanzzzd wants to merge 1 commit into
alibaba:mainfrom
Fanzzzd:fix/compression-preserve-active-zone
Closed

Fanzzzd wants to merge 1 commit into
alibaba:mainfrom
Fanzzzd:fix/compression-preserve-active-zone

Conversation

@Fanzzzd

@Fanzzzd Fanzzzd commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Description

Scoped down per review feedback: this PR now fixes only the no-tool retry bound. The compression-zone defects that were previously bundled here (background compression summarizing the live tail, unreserved frozen-zone budget, template-less truncation) moved to #838 with standalone reproductions.

When the model answers without tool calls, the retry path appended the assistant text + retry nudge directly to messages, bypassing compression entirely — a model that keeps chatting without tools grows the prompt unchecked until the provider rejects it.

The fix addresses the root cause in two parts:

  1. groupIntoRounds could not see retry messages. It only recognized assistant and tool roles, so the user retry nudges belonged to no round: even when compression did run, partition boundaries and token accounting skipped them. A round now spans from its assistant message to the next assistant message (or the end of the conversation), covering tool results and interleaved user messages alike — which also simplifies the boundary math to compressEnd = lastCompressRound.end.
  2. The retry path now shares the tool rounds' bounds. addNextMessage is generalized to appendMessagesWithCompression, taking one whole logical update at a time so thresholds are evaluated once per round and compression never runs between an assistant message and its tool results. Its success predicate is the exact complement of the sync-compression trigger (<= warnLimit), so a conversation sitting exactly at the warning threshold is not misreported as a failure.

Behavioral change: a retry loop that overflows the budget now stops with StopCompression instead of growing until a provider error. No wire-format changes.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

How Has This Been Tested?

  • make test passes locally
  • TestRunPerFile_NoToolRetryBoundedByCompression discriminates: with the old retry path restored it fails (retries keep going, StopMaxRounds); with the fix it passes (1 LLM call, StopCompression)
  • TestGroupIntoRounds_RetryNudgesStayWithTheirRound pins the new round span on the retry shape; the existing TestGroupIntoRounds is updated to the span representation with identical boundaries for assistant+tool sequences

Checklist

  • My code follows the project's coding style (go fmt, go vet)
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly (if applicable) (n/a — internal behavior)
  • I have signed the CLA

Related Issues

Related to #838 (the compression-zone defects split out of this PR) and #806 (originally split out of it).

@github-actions

github-actions Bot commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

🔍 OpenCodeReview found 1 issue(s) in this PR.

  • ✅ Successfully posted inline: 0 comment(s)
  • ❌ Failed to post inline: 1 comment(s)

[bug · medium]

📄 internal/llmloop/loop.go (L555-L555)

⚠️ GitHub could not post this as an inline comment: Line 555 could not be resolved (outside PR diff hunks)

The refactored appendMessagesWithCompression now includes the tool call message and tool results in a single additions slice, which is good for atomicity. However, when compression runs and returns the messages unchanged (because everything fits), the function still returns finalCount < warnLimit. If finalCount == warnLimit, this returns false even though compression succeeded and the conversation is within limits. The check should be finalCount <= warnLimit to match the compression threshold logic (warnLimit is 80% of maxTokens, so being exactly at warnLimit is acceptable).

@Fanzzzd

Fanzzzd commented Aug 10, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the reviewer bot's finding in a1d3b78: the success return of appendMessagesWithCompression is now finalCount <= warnLimit, the exact complement of the > warnLimit sync-compression trigger, so a conversation sitting exactly at the threshold is no longer reported as a compression failure. (The strict < predates this PR, but since this PR owns the refactor of that function it's the right place to fix it.)

@stay-foolish-forever

stay-foolish-forever commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Really appreciate your contribution, but the size of the change seems too large than I expected, with multiple fixes or irrelative modifications applied in a single PR. The "no-tool retry path had no token bound" issue emerged from the bug in function groupIntoRounds and I did not see any fixes related to it. And your analysis of the issues related to background compression tasks is reasonable, but maybe there is better solution for this.

My advice is to fix the "no-tool retry path" with a neat implementation in this PR, and left the background compression issue in another PR (better with a separate issue too). Thanks again for your impressive analysis the context compression issues and your rich contribution, wish you have a good day!

When the model answers without tool calls, the retry path appended the
assistant text and retry nudge directly to the conversation, bypassing
compression entirely — a model that keeps chatting without tools grows
the prompt unchecked until the provider rejects it.

Root cause in two parts. groupIntoRounds only recognized assistant and
tool roles, so retry nudges belonged to no round: even when compression
did run, partition boundaries and token accounting never saw those
messages. Rounds now span from their assistant message to the next one,
covering tool results and interleaved user messages alike. On top of
that, the retry path now goes through the same append+compression helper
as tool rounds (addNextMessage generalized to
appendMessagesWithCompression, taking the whole logical update at once),
and its success predicate is the exact complement of the sync-compression
trigger, so a conversation sitting exactly at the warning threshold is
not misreported as a failure.

TestRunPerFile_NoToolRetryBoundedByCompression fails on the old retry
path (10 LLM calls, StopMaxRounds) and passes with the fix (1 call,
StopCompression).
@Fanzzzd

Fanzzzd commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the direction — you're right on both counts, and the PR is reworked accordingly.

Scope: this PR is now only the no-tool retry bound (single commit, internal/llmloop only). The compression-zone changes moved out to #838, which carries standalone failing-test reproductions for each defect and leaves the design space open — I noted your point that background compression may deserve a better overall solution there.

groupIntoRounds: agreed, that's where the root cause lives — it only recognized assistant/tool roles, so the retry nudges belonged to no round and stayed invisible to partition boundaries and token accounting even when compression ran. A round now spans from its assistant message to the next one, which covers tool results and interleaved user messages uniformly and simplifies the boundary computation to lastCompressRound.end (identical boundaries for ordinary assistant+tool sequences — the existing TestGroupIntoRounds cases keep the same cut points). The loop side routes the retry path through the same append+threshold helper as tool rounds, so the bound applies uniformly.

TestRunPerFile_NoToolRetryBoundedByCompression discriminates the fix (fails with the old retry path restored, passes with it), and TestGroupIntoRounds_RetryNudgesStayWithTheirRound pins the new span on the retry shape.

@lizhengfeng101

lizhengfeng101 commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

@Fanzzzd The direction here is right, but this branch has drifted far from main (it still uses the pre-refactor RunPerFile/addNextMessage and pre-messageTokens token accounting), so please rebase onto latest main and reconcile the native/reasoning accounting by hand — or, given the size of the conflict, it may be cleaner to close this and open a fresh PR off current main.

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.

3 participants