feat(agent): enhance message consolidation and context management

- Improved the message consolidation logic to preserve the current turn's user query and all subsequent assistant/tool messages, ensuring better context retention.
- Updated the CompressContext function to reflect the new consolidation strategy, maintaining the system prompt and relevant recent messages.
- Refactored the context manager to support optional message repository for improved context rebuilding from persistent storage.
- Added comprehensive tests to validate the new consolidation behavior and ensure correct message handling across various scenarios.
This commit is contained in:
wizardchen
2026-03-24 17:32:24 +08:00
committed by lyingbug
parent b980d4619d
commit a8924e301e
11 changed files with 662 additions and 535 deletions
+53 -36
View File
@@ -70,10 +70,11 @@ func (c *Consolidator) ShouldConsolidate(currentTokens int) bool {
// Consolidate summarizes older messages and returns a compressed message array.
// It preserves:
// - The system prompt (first message)
// - The current user query (last message)
// - Recent messages that fit within the token budget
// - The current turn: user query (last user message) and all subsequent
// assistant/tool messages belonging to the same turn
// - Recent history messages that fit within the token budget
//
// Older messages are replaced with a summary system message.
// Older history messages are replaced with a summary system message.
// On LLM failure after maxConsolidationAttempts, falls back to raw text archiving.
func (c *Consolidator) Consolidate(
ctx context.Context,
@@ -84,32 +85,51 @@ func (c *Consolidator) Consolidate(
}
systemMsg := messages[0]
lastMsg := messages[len(messages)-1]
middle := messages[1 : len(messages)-1]
// Determine how many messages to keep vs. consolidate.
// We want to consolidate enough to bring tokens below the threshold.
targetTokens := int(float64(c.maxTokens) * c.threshold * 0.6) // aim for 60% of threshold
keepFromEnd := c.findKeepBoundary(middle, targetTokens, &systemMsg, &lastMsg)
if keepFromEnd >= len(middle) {
// Nothing to consolidate
// Find the current user query — the last message with role "user".
// Everything from this point onward (user query + assistant tool_calls +
// tool results) is the active turn and must be preserved intact.
lastUserIdx := 0
for i := len(messages) - 1; i >= 1; i-- {
if messages[i].Role == "user" {
lastUserIdx = i
break
}
}
if lastUserIdx <= 1 {
return messages, nil
}
toConsolidate := middle[:len(middle)-keepFromEnd]
toKeep := middle[len(middle)-keepFromEnd:]
history := messages[1:lastUserIdx]
tail := messages[lastUserIdx:]
if len(history) < 2 {
return messages, nil
}
targetTokens := int(float64(c.maxTokens) * c.threshold * 0.6) // aim for 60% of threshold
tailTokens := 0
for i := range tail {
tailTokens += c.estimator.EstimateMessage(&tail[i])
}
keepFromEnd := c.findKeepBoundary(history, targetTokens, &systemMsg, tailTokens)
if keepFromEnd >= len(history) {
return messages, nil
}
toConsolidate := history[:len(history)-keepFromEnd]
toKeep := history[len(history)-keepFromEnd:]
// Try LLM-powered summarization
summary, err := c.summarizeWithRetry(ctx, toConsolidate)
if err != nil {
// Fall back to raw archiving
logger.Warnf(ctx, "[MemoryConsolidator] LLM summarization failed after retries, "+
"falling back to raw archive: %v", err)
summary = c.rawArchive(toConsolidate)
}
// Build consolidated messages
summaryMsg := chat.Message{
Role: "system",
Content: fmt.Sprintf(
@@ -118,59 +138,56 @@ func (c *Consolidator) Consolidate(
),
}
result := make([]chat.Message, 0, 3+len(toKeep))
result := make([]chat.Message, 0, 2+len(toKeep)+len(tail))
result = append(result, systemMsg)
result = append(result, summaryMsg)
result = append(result, toKeep...)
result = append(result, lastMsg)
result = append(result, tail...)
logger.Infof(ctx, "[MemoryConsolidator] Consolidated %d messages → summary (%d chars), "+
"keeping %d recent messages",
len(toConsolidate), len(summary), len(toKeep))
"keeping %d history + %d current-turn messages",
len(toConsolidate), len(summary), len(toKeep), len(tail))
return result, nil
}
// findKeepBoundary determines how many messages from the end of middle to keep.
// findKeepBoundary determines how many messages from the end of history to keep.
// Returns the count of messages to keep (from the end), respecting tool_call/tool_result boundaries.
// tailTokens is the token cost of the current-turn tail that is always preserved.
func (c *Consolidator) findKeepBoundary(
middle []chat.Message,
history []chat.Message,
targetTokens int,
systemMsg, lastMsg *chat.Message,
systemMsg *chat.Message,
tailTokens int,
) int {
// Budget for kept messages = target - system - last
budget := targetTokens -
c.estimator.EstimateMessage(systemMsg) -
c.estimator.EstimateMessage(lastMsg) -
tailTokens -
500 // reserve for summary message
if budget <= 0 {
return 0
}
// Walk from the end, accumulating tokens, respecting tool boundaries
tokens := 0
keepCount := 0
i := len(middle) - 1
i := len(history) - 1
for i >= 0 {
msg := middle[i]
msg := history[i]
msgTokens := c.estimator.EstimateMessage(&msg)
// If this is a tool result, we must also keep its assistant message
if msg.Role == "tool" {
// Walk back to find the assistant message with tool_calls
groupTokens := msgTokens
groupSize := 1
j := i - 1
for j >= 0 && middle[j].Role == "tool" {
groupTokens += c.estimator.EstimateMessage(&middle[j])
for j >= 0 && history[j].Role == "tool" {
groupTokens += c.estimator.EstimateMessage(&history[j])
groupSize++
j--
}
// Include the assistant message
if j >= 0 && middle[j].Role == "assistant" {
groupTokens += c.estimator.EstimateMessage(&middle[j])
if j >= 0 && history[j].Role == "assistant" {
groupTokens += c.estimator.EstimateMessage(&history[j])
groupSize++
}
+279
View File
@@ -1,13 +1,37 @@
package memory
import (
"context"
"strings"
"testing"
"github.com/Tencent/WeKnora/internal/agent/token"
"github.com/Tencent/WeKnora/internal/models/chat"
"github.com/Tencent/WeKnora/internal/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// stubChat is a minimal chat.Chat implementation for testing consolidation.
type stubChat struct {
response string
err error
}
func (s *stubChat) Chat(_ context.Context, _ []chat.Message, _ *chat.ChatOptions) (*types.ChatResponse, error) {
if s.err != nil {
return nil, s.err
}
return &types.ChatResponse{Content: s.response}, nil
}
func (s *stubChat) ChatStream(context.Context, []chat.Message, *chat.ChatOptions) (<-chan types.StreamResponse, error) {
return nil, nil
}
func (s *stubChat) GetModelName() string { return "stub" }
func (s *stubChat) GetModelID() string { return "stub" }
func TestConsolidator_ShouldConsolidate(t *testing.T) {
est, err := token.NewEstimator()
assert.NoError(t, err)
@@ -93,3 +117,258 @@ func TestBuildConsolidationPrompt(t *testing.T) {
assert.Contains(t, prompt, "web_search")
assert.Contains(t, prompt, "**Tool [web_search]**: results here")
}
// ---------- Consolidate() 核心流程测试 ----------
func TestConsolidate_TooFewMessages(t *testing.T) {
est, err := token.NewEstimator()
require.NoError(t, err)
c := NewConsolidator(&stubChat{response: "summary"}, est, 100, 0)
msgs := []chat.Message{
{Role: "system", Content: "sys"},
{Role: "user", Content: "hi"},
{Role: "assistant", Content: "hello"},
}
result, err := c.Consolidate(context.Background(), msgs)
require.NoError(t, err)
assert.Equal(t, msgs, result, "<=3 messages should be returned unchanged")
}
func TestConsolidate_Round1_UserQueryAtEnd(t *testing.T) {
est, err := token.NewEstimator()
require.NoError(t, err)
// Use a stub that returns a known summary.
c := NewConsolidator(&stubChat{response: "summary of old history"}, est, 200, 0)
long := strings.Repeat("old context data ", 200)
msgs := []chat.Message{
{Role: "system", Content: "You are a helpful assistant"},
{Role: "user", Content: long},
{Role: "assistant", Content: long},
{Role: "user", Content: long},
{Role: "assistant", Content: long},
{Role: "user", Content: "current question"}, // last user = current query
}
result, err := c.Consolidate(context.Background(), msgs)
require.NoError(t, err)
// System prompt is preserved.
assert.Equal(t, "system", result[0].Role)
assert.Equal(t, "You are a helpful assistant", result[0].Content)
// The current user query (last user message) must be preserved at the tail.
last := result[len(result)-1]
assert.Equal(t, "user", last.Role)
assert.Equal(t, "current question", last.Content)
// A summary message must have been inserted.
assert.Equal(t, "system", result[1].Role)
assert.Contains(t, result[1].Content, "Memory Summary")
// Total message count should be fewer than original.
assert.Less(t, len(result), len(msgs))
}
func TestConsolidate_Round2Plus_UserQueryNotAtEnd(t *testing.T) {
est, err := token.NewEstimator()
require.NoError(t, err)
c := NewConsolidator(&stubChat{response: "consolidated history"}, est, 300, 0)
longContent := strings.Repeat("verbose content ", 200)
// Simulates Agent Round 2+:
// [system, ...old_history..., user_query, assistant+tools, tool_results]
// The user query is NOT the last message.
msgs := []chat.Message{
{Role: "system", Content: "You are a helpful assistant"},
// --- old history (from previous turns) ---
{Role: "user", Content: longContent},
{Role: "assistant", Content: longContent},
{Role: "user", Content: longContent},
{Role: "assistant", Content: longContent, ToolCalls: []chat.ToolCall{
{ID: "old_call", Function: chat.FunctionCall{Name: "search", Arguments: `{}`}},
}},
{Role: "tool", Content: longContent, ToolCallID: "old_call", Name: "search"},
// --- current turn ---
{Role: "user", Content: "what is the weather today?"}, // current user query
{Role: "assistant", Content: "let me check", ToolCalls: []chat.ToolCall{
{ID: "call_1", Function: chat.FunctionCall{Name: "weather", Arguments: `{"city":"beijing"}`}},
}},
{Role: "tool", Content: "sunny, 25°C", ToolCallID: "call_1", Name: "weather"},
}
result, err := c.Consolidate(context.Background(), msgs)
require.NoError(t, err)
// System prompt preserved.
assert.Equal(t, "system", result[0].Role)
// The entire current turn tail must be intact (user query + assistant + tool).
// Find the user query in result.
userQueryIdx := -1
for i, m := range result {
if m.Role == "user" && m.Content == "what is the weather today?" {
userQueryIdx = i
break
}
}
require.NotEqual(t, -1, userQueryIdx, "current user query must be preserved")
// After user query: assistant with tool_calls, then tool result.
require.Greater(t, len(result), userQueryIdx+2)
assert.Equal(t, "assistant", result[userQueryIdx+1].Role)
assert.Equal(t, "let me check", result[userQueryIdx+1].Content)
assert.Len(t, result[userQueryIdx+1].ToolCalls, 1)
assert.Equal(t, "tool", result[userQueryIdx+2].Role)
assert.Equal(t, "sunny, 25°C", result[userQueryIdx+2].Content)
// Summary message exists.
hasSummary := false
for _, m := range result {
if m.Role == "system" && strings.Contains(m.Content, "Memory Summary") {
hasSummary = true
break
}
}
assert.True(t, hasSummary, "should contain a memory summary message")
// Message count reduced.
assert.Less(t, len(result), len(msgs))
}
func TestConsolidate_Round2Plus_MultipleToolCallsPreserved(t *testing.T) {
est, err := token.NewEstimator()
require.NoError(t, err)
c := NewConsolidator(&stubChat{response: "summary"}, est, 300, 0)
longContent := strings.Repeat("filler ", 300)
msgs := []chat.Message{
{Role: "system", Content: "sys"},
// old history
{Role: "user", Content: longContent},
{Role: "assistant", Content: longContent},
// current turn with parallel tool calls
{Role: "user", Content: "do two things"},
{Role: "assistant", Content: "ok", ToolCalls: []chat.ToolCall{
{ID: "c1", Function: chat.FunctionCall{Name: "toolA", Arguments: `{}`}},
{ID: "c2", Function: chat.FunctionCall{Name: "toolB", Arguments: `{}`}},
}},
{Role: "tool", Content: "resultA", ToolCallID: "c1", Name: "toolA"},
{Role: "tool", Content: "resultB", ToolCallID: "c2", Name: "toolB"},
}
result, err := c.Consolidate(context.Background(), msgs)
require.NoError(t, err)
// All three current-turn messages after the user query must be present.
var toolNames []string
for _, m := range result {
if m.Role == "tool" {
toolNames = append(toolNames, m.Name)
}
}
assert.Contains(t, toolNames, "toolA")
assert.Contains(t, toolNames, "toolB")
// User query preserved.
found := false
for _, m := range result {
if m.Role == "user" && m.Content == "do two things" {
found = true
}
}
assert.True(t, found)
}
func TestConsolidate_NoUserMessage_ReturnUnchanged(t *testing.T) {
est, err := token.NewEstimator()
require.NoError(t, err)
c := NewConsolidator(&stubChat{response: "summary"}, est, 100, 0)
// Edge case: no user message at all (shouldn't happen normally but be defensive).
msgs := []chat.Message{
{Role: "system", Content: "sys"},
{Role: "assistant", Content: "hello"},
{Role: "assistant", Content: "world"},
{Role: "assistant", Content: "more"},
}
result, err := c.Consolidate(context.Background(), msgs)
require.NoError(t, err)
assert.Equal(t, msgs, result, "no user message → return unchanged")
}
func TestConsolidate_LLMFailure_FallsBackToRawArchive(t *testing.T) {
est, err := token.NewEstimator()
require.NoError(t, err)
failChat := &stubChat{err: assert.AnError}
c := NewConsolidator(failChat, est, 200, 0)
longContent := strings.Repeat("data ", 300)
msgs := []chat.Message{
{Role: "system", Content: "sys"},
{Role: "user", Content: longContent},
{Role: "assistant", Content: longContent},
{Role: "user", Content: longContent},
{Role: "assistant", Content: longContent},
{Role: "user", Content: "current"},
{Role: "assistant", Content: "thinking", ToolCalls: []chat.ToolCall{
{ID: "c1", Function: chat.FunctionCall{Name: "t1"}},
}},
{Role: "tool", Content: "res", ToolCallID: "c1", Name: "t1"},
}
result, err := c.Consolidate(context.Background(), msgs)
require.NoError(t, err)
// Should still have a summary (raw archive fallback).
hasSummary := false
for _, m := range result {
if m.Role == "system" && strings.Contains(m.Content, "Memory Summary") {
hasSummary = true
assert.Contains(t, m.Content, "Raw conversation archive",
"should be a raw archive when LLM fails")
}
}
assert.True(t, hasSummary)
// Current turn tail must still be preserved.
last := result[len(result)-1]
assert.Equal(t, "tool", last.Role)
userFound := false
for _, m := range result {
if m.Role == "user" && m.Content == "current" {
userFound = true
}
}
assert.True(t, userFound, "current user query must survive even on LLM failure")
}
func TestConsolidate_OnlyCurrentTurn_NothingToConsolidate(t *testing.T) {
est, err := token.NewEstimator()
require.NoError(t, err)
c := NewConsolidator(&stubChat{response: "summary"}, est, 100, 0)
// Only system + user + assistant + tool → history between system and user is empty.
msgs := []chat.Message{
{Role: "system", Content: "sys"},
{Role: "user", Content: "hello"},
{Role: "assistant", Content: "let me help", ToolCalls: []chat.ToolCall{
{ID: "c1", Function: chat.FunctionCall{Name: "t1"}},
}},
{Role: "tool", Content: "done", ToolCallID: "c1", Name: "t1"},
}
result, err := c.Consolidate(context.Background(), msgs)
require.NoError(t, err)
assert.Equal(t, msgs, result, "nothing to consolidate → unchanged")
}