Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(agent): compact the unsanitized history on overflow recovery (#4672)

ydflow1d328a9600bc
diff --git a/internal/agent/think.go b/internal/agent/think.go
index 2183b889e..d1523c4a8 100644
--- a/internal/agent/think.go
+++ b/internal/agent/think.go
@@ -617,7 +617,7 @@ func (e *AgentEngine) callLLMWithRetry(
// into the summary envelope, where the next compaction cannot read it
// back out. The provider gets the sanitized view; the engine keeps the
// structured one.
- compacted := e.forceCompaction(ctx, messages, round)
+ compacted := e.forceCompaction(ctx, *messagesPtr, round)
*messagesPtr = compacted
messages = agenttools.SanitizeMessages(compacted)
e.lastSentMsgCount = len(compacted)
diff --git a/internal/agent/think_overflow_compaction_test.go b/internal/agent/think_overflow_compaction_test.go
new file mode 100644
index 000000000..a53d7219d
--- /dev/null
+++ b/internal/agent/think_overflow_compaction_test.go
@@ -0,0 +1,61 @@
+package agent
+
+import (
+ "context"
+ "testing"
+
+ "github.com/Tencent/WeKnora/internal/models/chat"
+ "github.com/Tencent/WeKnora/internal/types"
+ "github.com/stretchr/testify/require"
+)
+
+// Regression for the overflow-recovery path in callLLMWithRetry: the retry
+// after a rejected-for-size request must compact the engine's unsanitized
+// history. Sanitizing merges adjacent user messages and the merge keeps the
+// first message's Kind, so a compaction-summary message merged with the live
+// user turn reads as a summary whose content extends past </summary>. Every
+// later compaction unwraps only the text between <summary> and </summary> and
+// treats that merged message as the boundary, so the live turn folded into it
+// becomes permanently invisible to summarization.
+func TestOverflowRecoveryCompactsUnsanitizedHistory(t *testing.T) {
+ messages := []chat.Message{
+ {Role: "system", Content: "You are a test agent."},
+ {
+ Role: "user",
+ Kind: chat.MessageKindCompactionSummary,
+ Content: "Earlier context.\n<summary>\nUser is analyzing Q3 sales.\n</summary>",
+ },
+ {Role: "user", Content: "Now compare with Q4"},
+ }
+ model := &mockChat{responses: []mockResponse{
+ {chunks: []types.StreamResponse{
+ {
+ ResponseType: types.ResponseTypeError,
+ Content: "prompt is too long: 200000 tokens > 131072 maximum",
+ Done: true,
+ },
+ }},
+ {chunks: []types.StreamResponse{
+ {ResponseType: types.ResponseTypeAnswer, Content: "ok", Done: true, FinishReason: "stop"},
+ }},
+ }}
+ engine := newTestEngine(t, model)
+ _, err := engine.callLLMWithRetry(
+ context.Background(), &messages, nil, &types.AgentState{}, "q", 0, "test-session",
+ )
+ require.NoError(t, err)
+
+ // The engine's copy keeps the structured history the caller stored.
+ require.Len(t, messages, 3,
+ "overflow recovery must not collapse the engine's history into the sanitized view")
+ require.Equal(t, chat.MessageKindCompactionSummary, messages[1].Kind)
+ require.Equal(t, "Now compare with Q4", messages[2].Content)
+ require.NotContains(t, messages[1].Content, "Now compare with Q4")
+
+ // The provider still sees the sanitized view of the recovered history.
+ require.Len(t, model.calls, 2)
+ require.Len(t, model.calls[1], 2,
+ "the retried request still merges the adjacent user messages for the provider")
+ require.Contains(t, model.calls[1][1].Content, "Now compare with Q4",
+ "the live turn must reach the provider on the retried request")
+}