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:
@@ -293,7 +293,11 @@ func (e *AgentEngine) executeLoop(
|
||||
// the API-reported usage from the previous round plus a BPE delta
|
||||
// for newly appended messages (assistant reply + tool results).
|
||||
currentTokens := e.estimateCurrentTokens(messages)
|
||||
beforeLen := len(messages)
|
||||
messages = e.manageContextWindow(ctx, messages, state.CurrentRound+1, currentTokens)
|
||||
if len(messages) < beforeLen {
|
||||
currentTokens = e.tokenEstimator.EstimateMessages(messages)
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "[Agent][Round-%d/%d] Starting: %d messages, %d tools, est_tokens=%d",
|
||||
state.CurrentRound+1, e.config.MaxIterations, len(messages), len(tools), currentTokens)
|
||||
|
||||
@@ -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++
|
||||
}
|
||||
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -8,15 +8,14 @@ import (
|
||||
// When message tokens exceed MaxContextTokens * threshold, old messages are trimmed.
|
||||
const DefaultContextThresholdRatio = 0.8
|
||||
|
||||
// CompressContext trims older messages to bring total token count below the threshold.
|
||||
// currentTokens is the caller's best estimate of the current context size (from API
|
||||
// Usage when available, falling back to BPE estimation). The estimator is still used
|
||||
// internally for per-message-group cost calculation during trimming.
|
||||
//
|
||||
// It preserves:
|
||||
// - The first message (system prompt)
|
||||
// - The last message (current user query)
|
||||
// CompressContext trims older history messages to bring total token count below
|
||||
// the threshold. It preserves:
|
||||
// - The system prompt (first message)
|
||||
// - The current turn: user query (last user message) and all subsequent
|
||||
// assistant/tool messages
|
||||
// - tool_call / tool_result message pairs (never splits them)
|
||||
//
|
||||
// currentTokens is the caller's best estimate of the current context size.
|
||||
func CompressContext(
|
||||
messages []chat.Message,
|
||||
estimator *Estimator,
|
||||
@@ -33,10 +32,24 @@ func CompressContext(
|
||||
}
|
||||
|
||||
systemMsg := messages[0]
|
||||
lastMsg := messages[len(messages)-1]
|
||||
middle := messages[1 : len(messages)-1]
|
||||
|
||||
groups := groupToolMessages(middle)
|
||||
// Find the current user query — the last message with role "user".
|
||||
lastUserIdx := len(messages) - 1
|
||||
for i := len(messages) - 1; i >= 1; i-- {
|
||||
if messages[i].Role == "user" {
|
||||
lastUserIdx = i
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
history := messages[1:lastUserIdx]
|
||||
tail := messages[lastUserIdx:]
|
||||
|
||||
if len(history) == 0 {
|
||||
return messages
|
||||
}
|
||||
|
||||
groups := groupToolMessages(history)
|
||||
|
||||
tokensToFree := currentTokens - threshold
|
||||
freed := 0
|
||||
@@ -59,7 +72,7 @@ func CompressContext(
|
||||
for i := removeUpTo; i < len(groups); i++ {
|
||||
remaining = append(remaining, groups[i]...)
|
||||
}
|
||||
remaining = append(remaining, lastMsg)
|
||||
remaining = append(remaining, tail...)
|
||||
|
||||
return remaining
|
||||
}
|
||||
|
||||
@@ -154,6 +154,153 @@ func TestCompressContext(t *testing.T) {
|
||||
result := CompressContext(messages, e, 1, 999)
|
||||
assert.Equal(t, messages, result)
|
||||
})
|
||||
|
||||
t.Run("round2+: user query not at end, preserves current turn tail", func(t *testing.T) {
|
||||
longText := strings.Repeat("filler content ", 200)
|
||||
messages := []chat.Message{
|
||||
{Role: "system", Content: "system prompt"},
|
||||
// old history
|
||||
{Role: "user", Content: longText},
|
||||
{Role: "assistant", Content: longText},
|
||||
{Role: "user", Content: longText},
|
||||
{Role: "assistant", Content: longText},
|
||||
// current turn
|
||||
{Role: "user", Content: "current question"},
|
||||
{Role: "assistant", Content: "let me search", ToolCalls: []chat.ToolCall{
|
||||
{ID: "c1", Function: chat.FunctionCall{Name: "search", Arguments: `{"q":"test"}`}},
|
||||
}},
|
||||
{Role: "tool", Content: "search results", ToolCallID: "c1", Name: "search"},
|
||||
}
|
||||
|
||||
tokens := e.EstimateMessages(messages)
|
||||
result := CompressContext(messages, e, 300, tokens)
|
||||
|
||||
// System prompt preserved.
|
||||
assert.Equal(t, "system", result[0].Role)
|
||||
assert.Equal(t, "system prompt", result[0].Content)
|
||||
|
||||
// Current turn tail must be fully intact.
|
||||
userIdx := -1
|
||||
for i, m := range result {
|
||||
if m.Role == "user" && m.Content == "current question" {
|
||||
userIdx = i
|
||||
break
|
||||
}
|
||||
}
|
||||
require.NotEqual(t, -1, userIdx, "user query must be preserved")
|
||||
require.Greater(t, len(result), userIdx+2, "assistant + tool must follow")
|
||||
assert.Equal(t, "assistant", result[userIdx+1].Role)
|
||||
assert.Equal(t, "let me search", result[userIdx+1].Content)
|
||||
assert.Equal(t, "tool", result[userIdx+2].Role)
|
||||
assert.Equal(t, "search results", result[userIdx+2].Content)
|
||||
|
||||
// Should be shorter than original (old history trimmed).
|
||||
assert.Less(t, len(result), len(messages))
|
||||
})
|
||||
|
||||
t.Run("round2+: multiple tool results after user query all preserved", func(t *testing.T) {
|
||||
longText := strings.Repeat("data ", 300)
|
||||
messages := []chat.Message{
|
||||
{Role: "system", Content: "sys"},
|
||||
{Role: "user", Content: longText},
|
||||
{Role: "assistant", Content: longText},
|
||||
// current turn with parallel tool calls
|
||||
{Role: "user", Content: "do things"},
|
||||
{Role: "assistant", Content: "ok", ToolCalls: []chat.ToolCall{
|
||||
{ID: "c1", Function: chat.FunctionCall{Name: "t1"}},
|
||||
{ID: "c2", Function: chat.FunctionCall{Name: "t2"}},
|
||||
}},
|
||||
{Role: "tool", Content: "res1", ToolCallID: "c1", Name: "t1"},
|
||||
{Role: "tool", Content: "res2", ToolCallID: "c2", Name: "t2"},
|
||||
}
|
||||
|
||||
tokens := e.EstimateMessages(messages)
|
||||
result := CompressContext(messages, e, 200, tokens)
|
||||
|
||||
// Both tool results must be present.
|
||||
var toolNames []string
|
||||
for _, m := range result {
|
||||
if m.Role == "tool" {
|
||||
toolNames = append(toolNames, m.Name)
|
||||
}
|
||||
}
|
||||
assert.Contains(t, toolNames, "t1")
|
||||
assert.Contains(t, toolNames, "t2")
|
||||
|
||||
// User query preserved.
|
||||
found := false
|
||||
for _, m := range result {
|
||||
if m.Content == "do things" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
assert.True(t, found)
|
||||
})
|
||||
|
||||
t.Run("round2+: no history between system and user query returns unchanged", func(t *testing.T) {
|
||||
messages := []chat.Message{
|
||||
{Role: "system", Content: "sys"},
|
||||
{Role: "user", Content: "hello"},
|
||||
{Role: "assistant", Content: "thinking", ToolCalls: []chat.ToolCall{
|
||||
{ID: "c1", Function: chat.FunctionCall{Name: "t1"}},
|
||||
}},
|
||||
{Role: "tool", Content: "done", ToolCallID: "c1", Name: "t1"},
|
||||
}
|
||||
tokens := e.EstimateMessages(messages)
|
||||
result := CompressContext(messages, e, 10, tokens)
|
||||
assert.Equal(t, messages, result, "no history to trim → unchanged")
|
||||
})
|
||||
|
||||
t.Run("no user message at all returns unchanged", func(t *testing.T) {
|
||||
messages := []chat.Message{
|
||||
{Role: "system", Content: "sys"},
|
||||
{Role: "assistant", Content: strings.Repeat("x", 1000)},
|
||||
{Role: "assistant", Content: strings.Repeat("y", 1000)},
|
||||
}
|
||||
tokens := e.EstimateMessages(messages)
|
||||
result := CompressContext(messages, e, 10, tokens)
|
||||
// lastUserIdx defaults to len-1 (last msg), history = messages[1:last] = [assistant]
|
||||
// This still does its best—the important thing is it doesn't panic.
|
||||
assert.NotNil(t, result)
|
||||
})
|
||||
|
||||
t.Run("round2+: tool pair in history never split", func(t *testing.T) {
|
||||
longText := strings.Repeat("verbose ", 200)
|
||||
messages := []chat.Message{
|
||||
{Role: "system", Content: "sys"},
|
||||
// old turn 1
|
||||
{Role: "user", Content: "old query 1"},
|
||||
{Role: "assistant", Content: longText, ToolCalls: []chat.ToolCall{
|
||||
{ID: "old1", Function: chat.FunctionCall{Name: "old_tool"}},
|
||||
}},
|
||||
{Role: "tool", Content: longText, ToolCallID: "old1", Name: "old_tool"},
|
||||
// old turn 2
|
||||
{Role: "user", Content: "old query 2"},
|
||||
{Role: "assistant", Content: "short reply"},
|
||||
// current turn
|
||||
{Role: "user", Content: "current"},
|
||||
{Role: "assistant", Content: "working", ToolCalls: []chat.ToolCall{
|
||||
{ID: "new1", Function: chat.FunctionCall{Name: "new_tool"}},
|
||||
}},
|
||||
{Role: "tool", Content: "result", ToolCallID: "new1", Name: "new_tool"},
|
||||
}
|
||||
|
||||
tokens := e.EstimateMessages(messages)
|
||||
result := CompressContext(messages, e, 300, tokens)
|
||||
|
||||
// Verify no orphaned tool message: every "tool" msg must be preceded by
|
||||
// an assistant with tool_calls.
|
||||
for i, m := range result {
|
||||
if m.Role == "tool" {
|
||||
require.Greater(t, i, 0, "tool message at index 0 is impossible")
|
||||
prev := result[i-1]
|
||||
isPairedTool := prev.Role == "tool"
|
||||
isPairedAssistant := prev.Role == "assistant" && len(prev.ToolCalls) > 0
|
||||
assert.True(t, isPairedTool || isPairedAssistant,
|
||||
"tool message at %d must be preceded by assistant+tool_calls or another tool, got %s", i, prev.Role)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestGroupToolMessages(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user