mirror of
https://github.com/tiennm99/goclaw.git
synced 2026-10-20 02:17:24 +00:00
fix(agent): preserve MediaRefs when merging consecutive same-role messages
sanitizeHistory now appends MediaRefs from dropped messages so compaction summary retains knowledge of shared media files.
This commit is contained in:
1 parent
d79d738148
commit
ade6c7ba33
3 files changed
+31
-3
No files matched your search
@@ -601,6 +601,11 @@ func sanitizeHistory(msgs []providers.Message) ([]providers.Message, int) {
|
||||
slog.Debug("sanitizeHistory: merging consecutive same-role messages",
|
||||
"role", curr.Role, "index", j)
|
||||
prev.Content += "\n\n" + curr.Content
|
||||
// Preserve media refs from merged message so compaction
|
||||
// summary retains knowledge of shared media files.
|
||||
if len(curr.MediaRefs) > 0 {
|
||||
prev.MediaRefs = append(prev.MediaRefs, curr.MediaRefs...)
|
||||
}
|
||||
dropped++
|
||||
} else {
|
||||
merged = append(merged, curr)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -192,8 +193,9 @@ func TestSanitizeHistory_DedupAcrossTwoTurns(t *testing.T) {
|
||||
if len(result) != 6 {
|
||||
t.Fatalf("expected 6 messages preserved via dedup, got %d", len(result))
|
||||
}
|
||||
if dropped != 0 {
|
||||
t.Fatalf("expected 0 dropped, got %d", dropped)
|
||||
// Dedup rewrites count as changes (dropped > 0 triggers DB persistence).
|
||||
if dropped != 1 {
|
||||
t.Fatalf("expected 1 dropped (dedup counts as change), got %d", dropped)
|
||||
}
|
||||
|
||||
// Turn 1 assistant should keep original ID
|
||||
@@ -218,7 +220,7 @@ func TestSanitizeHistory_LargeHistory_Performance(t *testing.T) {
|
||||
// Build 1000-message history with proper tool pairing
|
||||
msgs := make([]providers.Message, 0, 1000)
|
||||
for i := range 250 {
|
||||
tcID := "tc_" + strings.Repeat("x", 5) + "_" + string(rune('a'+i%26)) + string(rune('0'+i%10))
|
||||
tcID := fmt.Sprintf("tc_%05d", i)
|
||||
msgs = append(msgs,
|
||||
providers.Message{Role: "user", Content: "question " + tcID},
|
||||
providers.Message{Role: "assistant", ToolCalls: []providers.ToolCall{
|
||||
|
||||
@@ -410,6 +410,27 @@ func TestSanitizeHistory_NoMergeForToolCallMessages(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeHistory_MergePreservesMediaRefs(t *testing.T) {
|
||||
msgs := []providers.Message{
|
||||
{Role: "user", Content: "here's a photo", MediaRefs: []providers.MediaRef{{Kind: "image", ID: "f1"}}},
|
||||
{Role: "user", Content: "and another", MediaRefs: []providers.MediaRef{{Kind: "image", ID: "f2"}}},
|
||||
{Role: "assistant", Content: "nice pics"},
|
||||
}
|
||||
got, dropped := sanitizeHistory(msgs)
|
||||
if len(got) != 2 {
|
||||
t.Fatalf("expected 2 messages after merge, got %d", len(got))
|
||||
}
|
||||
if len(got[0].MediaRefs) != 2 {
|
||||
t.Errorf("expected 2 media refs after merge, got %d", len(got[0].MediaRefs))
|
||||
}
|
||||
if got[0].MediaRefs[0].ID != "f1" || got[0].MediaRefs[1].ID != "f2" {
|
||||
t.Errorf("media refs not preserved correctly: %+v", got[0].MediaRefs)
|
||||
}
|
||||
if dropped != 1 {
|
||||
t.Errorf("expected 1 dropped, got %d", dropped)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUniquifyToolCallIDs(t *testing.T) {
|
||||
runID := "abcdef12-3456-7890-abcd-ef1234567890"
|
||||
|
||||
|
||||
Reference in new issue
Block a user