-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathcompaction_test.go
More file actions
122 lines (103 loc) · 4.1 KB
/
Copy pathcompaction_test.go
File metadata and controls
122 lines (103 loc) · 4.1 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package e2e
import (
"strings"
"testing"
"time"
copilot "github.com/github/copilot-sdk/go"
"github.com/github/copilot-sdk/go/e2e/testharness"
)
func TestCompaction(t *testing.T) {
ctx := testharness.NewTestContext(t)
client := ctx.NewClient()
t.Cleanup(func() { client.ForceStop() })
t.Run("should trigger compaction with low threshold and emit events", func(t *testing.T) {
ctx.ConfigureForTest(t)
enabled := true
backgroundThreshold := 0.005 // 0.5%
bufferThreshold := 0.01 // 1%
session, err := client.CreateSession(&copilot.SessionConfig{
InfiniteSessions: &copilot.InfiniteSessionConfig{
Enabled: &enabled,
BackgroundCompactionThreshold: &backgroundThreshold,
BufferExhaustionThreshold: &bufferThreshold,
},
})
if err != nil {
t.Fatalf("Failed to create session: %v", err)
}
var compactionStartEvents []copilot.SessionEvent
var compactionCompleteEvents []copilot.SessionEvent
session.On(func(event copilot.SessionEvent) {
if event.Type == copilot.SessionCompactionStart {
compactionStartEvents = append(compactionStartEvents, event)
}
if event.Type == copilot.SessionCompactionComplete {
compactionCompleteEvents = append(compactionCompleteEvents, event)
}
})
// Send multiple messages to fill up the context window
_, err = session.SendAndWait(copilot.MessageOptions{Prompt: "Tell me a long story about a dragon. Be very detailed."}, 60*time.Second)
if err != nil {
t.Fatalf("Failed to send first message: %v", err)
}
_, err = session.SendAndWait(copilot.MessageOptions{Prompt: "Continue the story with more details about the dragon's castle."}, 60*time.Second)
if err != nil {
t.Fatalf("Failed to send second message: %v", err)
}
_, err = session.SendAndWait(copilot.MessageOptions{Prompt: "Now describe the dragon's treasure in great detail."}, 60*time.Second)
if err != nil {
t.Fatalf("Failed to send third message: %v", err)
}
// Should have triggered compaction at least once
if len(compactionStartEvents) < 1 {
t.Errorf("Expected at least 1 compaction_start event, got %d", len(compactionStartEvents))
}
if len(compactionCompleteEvents) < 1 {
t.Errorf("Expected at least 1 compaction_complete event, got %d", len(compactionCompleteEvents))
}
// Compaction should have succeeded
if len(compactionCompleteEvents) > 0 {
lastComplete := compactionCompleteEvents[len(compactionCompleteEvents)-1]
if lastComplete.Data.Success == nil || !*lastComplete.Data.Success {
t.Errorf("Expected compaction to succeed")
}
if lastComplete.Data.TokensRemoved != nil && *lastComplete.Data.TokensRemoved <= 0 {
t.Errorf("Expected tokensRemoved > 0, got %v", *lastComplete.Data.TokensRemoved)
}
}
// Verify session still works after compaction
answer, err := session.SendAndWait(copilot.MessageOptions{Prompt: "What was the story about?"}, 60*time.Second)
if err != nil {
t.Fatalf("Failed to send verification message: %v", err)
}
if answer.Data.Content == nil || !strings.Contains(strings.ToLower(*answer.Data.Content), "dragon") {
t.Errorf("Expected answer to contain 'dragon', got %v", answer.Data.Content)
}
})
t.Run("should not emit compaction events when infinite sessions disabled", func(t *testing.T) {
ctx.ConfigureForTest(t)
enabled := false
session, err := client.CreateSession(&copilot.SessionConfig{
InfiniteSessions: &copilot.InfiniteSessionConfig{
Enabled: &enabled,
},
})
if err != nil {
t.Fatalf("Failed to create session: %v", err)
}
var compactionEvents []copilot.SessionEvent
session.On(func(event copilot.SessionEvent) {
if event.Type == copilot.SessionCompactionStart || event.Type == copilot.SessionCompactionComplete {
compactionEvents = append(compactionEvents, event)
}
})
_, err = session.SendAndWait(copilot.MessageOptions{Prompt: "What is 2+2?"}, 60*time.Second)
if err != nil {
t.Fatalf("Failed to send message: %v", err)
}
// Should not have any compaction events when disabled
if len(compactionEvents) != 0 {
t.Errorf("Expected 0 compaction events when disabled, got %d", len(compactionEvents))
}
})
}