Repository navigation
Expand file tree
/
Copy pathcompact_relevance_test.go
More file actions
72 lines (66 loc) · 2.14 KB
/
Copy pathcompact_relevance_test.go
File metadata and controls
72 lines (66 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package engine
import (
"context"
"testing"
"time"
"github.com/GrayCodeAI/reflect/internal/types"
)
func TestRelevancePruneStrategy_ShouldTrigger(t *testing.T) {
s := &RelevancePruneStrategy{}
if s.ShouldTrigger(makeMessages(5), 200000, 100000) {
t.Error("should not trigger with few messages")
}
if !s.ShouldTrigger(makeMessages(30), 200000, 100000) {
t.Error("should trigger with many messages over threshold")
}
}
func TestRelevancePruneStrategy_Compact(t *testing.T) {
sess := NewSessionWithClient(NewMockClientForTest(), "test", "test-model", "", nil, false)
// A long transcript; last user message is the task context.
var msgs []types.RelayMessage
for i := 0; i < 60; i++ {
msgs = append(msgs, types.RelayMessage{Role: "user", Content: "unrelated filler content that has no shared keywords"})
}
msgs = append(msgs, types.RelayMessage{Role: "user", Content: "refactor the payment module and update all tests"})
sess.Persistence().SetRawMessages(msgs)
s := &RelevancePruneStrategy{TargetTokens: 200}
result, err := s.Compact(context.Background(), sess)
if err != nil {
t.Fatal(err)
}
if result.Strategy != "relevance" {
t.Fatalf("strategy = %q, want relevance", result.Strategy)
}
// It must not crash and must return a sane result; the final user message
// should survive.
after := sess.Persistence().RawMessages()
if len(after) == 0 {
t.Fatal("no messages after pruning")
}
found := false
for _, m := range after {
if m.Content == "refactor the payment module and update all tests" {
found = true
}
}
if !found {
t.Fatal("task-context user message was dropped")
}
}
func TestToPruneMessagesAdapter(t *testing.T) {
msgs := []types.RelayMessage{
{Role: "user", Content: "hi"},
{Role: "assistant", Content: "ok", ToolUse: []types.ToolCall{{Name: "Bash"}}},
{Role: "user", Content: "err", ToolResults: []types.ToolResult{{IsError: true}}},
}
pruned := toPruneMessages(msgs, time.Now())
if len(pruned) != 3 {
t.Fatalf("len = %d", len(pruned))
}
if !pruned[1].HasToolCall {
t.Fatal("tool-use message should be flagged as tool call")
}
if !pruned[2].IsError {
t.Fatal("error result should be flagged as error")
}
}