Repository navigation
Conversation
Record the failed snapshot fingerprint and block resubmission after a deterministic provider rejection or one transient retry. The next main-loop step returns StopCompression instead of sending the unchanged oversized history again. Refs alibaba#1877.
|
🔍 OpenCodeReview found 3 issue(s) in this PR.
📄
|
| func (st *compressionState) compressionBlocked(messages []llm.Message) bool { | ||
| snapshotID := compressionSnapshotID(messages) | ||
| st.mu.Lock() | ||
| defer st.mu.Unlock() | ||
| return st.blocked && st.failedSnapshotID == snapshotID | ||
| } |
There was a problem hiding this comment.
In the common case where no compression failure has occurred (st.blocked == false), this method still computes the full snapshot hash by iterating all messages and calling ExtractText() on each one. Since compressionBlocked is called on every turn in addNextMessage, this adds unnecessary CPU and allocation overhead for the happy path.
Consider short-circuiting with a lock-guarded read of st.blocked first, and only computing the snapshot ID when there is actually a prior failure to compare against.
Suggestion:
| func (st *compressionState) compressionBlocked(messages []llm.Message) bool { | |
| snapshotID := compressionSnapshotID(messages) | |
| st.mu.Lock() | |
| defer st.mu.Unlock() | |
| return st.blocked && st.failedSnapshotID == snapshotID | |
| } | |
| func (st *compressionState) compressionBlocked(messages []llm.Message) bool { | |
| st.mu.Lock() | |
| if !st.blocked { | |
| st.mu.Unlock() | |
| return false | |
| } | |
| failedID := st.failedSnapshotID | |
| st.mu.Unlock() | |
| snapshotID := compressionSnapshotID(messages) | |
| return failedID == snapshotID | |
| } |
| if st.compressionBlocked(*messages) { | ||
| return StopCompression | ||
| } |
There was a problem hiding this comment.
Bug: When compression is blocked, this early return prevents the assistant message and tool results from being appended to messages (that happens at lines 604-612 below). The caller in runMainLoop breaks out of the loop on any non-StopNone return, so the latest LLM response and tool call results are silently dropped from the conversation.
The blocking check should either be moved after the message appends, or the appends should happen before this check. The intent is to stop compression retries, not to discard the current turn's messages.
Apply the failed-snapshot policy to the synchronous compression path as well, and keep the current turn's assistant and tool messages when compression is blocked. Also skip the snapshot hash while no failure is recorded. Refs alibaba#1877.
|
Addressed the three self-review findings in a732d47:
Added |
Description
Background memory compression has no failure memory. When a compression request fails,
triggerAsyncCompressionclearspendingJoband logs the error; the nextaddNextMessagesees the history still above the soft limit and starts the same request again. The synchronous path had the same gap: it logged the failure but did not record it. With a deterministic rejection such as a context-window overflow, the unchanged oversized snapshot is resubmitted every round, so the run never makes progress and never records an incomplete outcome.This change records the failed snapshot and applies a finite policy to both compression paths:
compressionSnapshotIDfingerprints the visible content sent to the compressor (Role,ExtractText,ToolCallID, tool calls).Nativereplay state is excluded because compression does not send it.compressionState. Both asynchronous jobs and synchronouscompress()calls use the samerecordCompressionFailure/recordCompressionSuccesshelpers.addNextMessagestops withStopCompressionwhen the current snapshot is blocked, but appends the assistant message and tool results first, so the latest turn is not dropped from the conversation. The original failed request and its error remain in the session record.Classification uses
llm.ErrTokenBudgetandllm.ProviderHTTPStatusonly, consistent with theinternal/llmrule that retry classification never reads message text.compressionBlockedalso short-circuits while no failure is recorded, so the happy path does not hash the snapshot.Related Issue: Fixes #1877
AI Assistance
This change was prepared with AI assistance from TraeCode (DeepSeek-V4.1-Flash). I reviewed and understand every line, and ran the tests below locally.
Testing
New tests:
TestAsyncCompression_DeterministicFailureBlocksSnapshot: one request for the failed snapshot, no resend after failure, and a changed snapshot is allowed to retry.TestAsyncCompression_TransientFailureRetriesOnce: one retry, then the snapshot is blocked.TestAddNextMessage_StopsOnBlockedCompression: the main loop returnsStopCompressionand still appends the assistant/tool messages.TestSyncCompressionFailureBlocksSnapshot: a synchronous 400 is recorded and the next turn sends no new request.TestPermanentCompressionFailure: error type/status classification.TestCompressionSnapshotID: content, role, and tool-call changes alter the fingerprint.Checklist
make checkpassesgo test -race ./internal/llmlooppassesgo build ./...passes