forked from github/copilot-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompactionTest.java
More file actions
177 lines (145 loc) · 7.71 KB
/
CompactionTest.java
File metadata and controls
177 lines (145 loc) · 7.71 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import com.github.copilot.sdk.generated.SessionEvent;
import com.github.copilot.sdk.generated.AssistantMessageEvent;
import com.github.copilot.sdk.generated.SessionCompactionCompleteEvent;
import com.github.copilot.sdk.generated.SessionCompactionStartEvent;
import com.github.copilot.sdk.json.InfiniteSessionConfig;
import com.github.copilot.sdk.json.MessageOptions;
import com.github.copilot.sdk.json.PermissionHandler;
import com.github.copilot.sdk.json.SessionConfig;
/**
* Tests for compaction and infinite sessions functionality.
*
* <p>
* These tests verify that sessions can trigger compaction with low thresholds
* and emit appropriate events. Snapshots are stored in
* test/snapshots/compaction/.
* </p>
*/
public class CompactionTest {
private static E2ETestContext ctx;
@BeforeAll
static void setup() throws Exception {
ctx = E2ETestContext.create();
}
@AfterAll
static void teardown() throws Exception {
if (ctx != null) {
ctx.close();
}
}
/**
* Verifies that compaction is triggered with low threshold and emits events.
*
* <p>
* Disabled due to flakiness — compaction timing is non-deterministic and the
* snapshot cannot reliably match across platforms. The reference implementation
* (nodejs) also skips this test. See <a href=
* "https://github.com/github/copilot-sdk/issues/1227">copilot-sdk#1227</a>.
*
* @see Snapshot:
* compaction/should_trigger_compaction_with_low_threshold_and_emit_events
*/
@Test
@Disabled("Flaky: compaction timing varies by platform — see https://github.com/github/copilot-sdk/issues/1227")
@Timeout(value = 300, unit = TimeUnit.SECONDS)
void testShouldTriggerCompactionWithLowThresholdAndEmitEvents() throws Exception {
ctx.configureForTest("compaction", "should_trigger_compaction_with_low_threshold_and_emit_events");
// Create session with very low compaction thresholds to trigger compaction
// quickly
var infiniteConfig = new InfiniteSessionConfig().setEnabled(true)
// Trigger background compaction at 0.5% context usage (~1000 tokens)
.setBackgroundCompactionThreshold(0.005)
// Block at 1% to ensure compaction runs
.setBufferExhaustionThreshold(0.01);
var config = new SessionConfig().setInfiniteSessions(infiniteConfig)
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL);
var events = new ArrayList<SessionEvent>();
var compactionCompleteLatch = new CountDownLatch(1);
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(config).get();
session.on(event -> {
events.add(event);
if (event instanceof SessionCompactionCompleteEvent) {
compactionCompleteLatch.countDown();
}
});
// Send multiple messages to fill up the context window
// With such low thresholds, even a few messages should trigger compaction
session.sendAndWait(new MessageOptions().setPrompt("Tell me a story about a dragon. Be detailed.")).get(60,
TimeUnit.SECONDS);
session.sendAndWait(
new MessageOptions().setPrompt("Continue the story with more details about the dragon's castle."))
.get(60, TimeUnit.SECONDS);
session.sendAndWait(new MessageOptions().setPrompt("Now describe the dragon's treasure in great detail."))
.get(60, TimeUnit.SECONDS);
// Wait for compaction to complete - it may arrive slightly after sendAndWait
// returns due to async event delivery from the CLI
assertTrue(compactionCompleteLatch.await(30, TimeUnit.SECONDS),
"Should have received a compaction complete event within 30 seconds");
long compactionStartCount = events.stream().filter(e -> e instanceof SessionCompactionStartEvent).count();
long compactionCompleteCount = events.stream().filter(e -> e instanceof SessionCompactionCompleteEvent)
.count();
// Should have triggered compaction at least once
assertTrue(compactionStartCount >= 1,
"Should have triggered compaction start at least once, got: " + compactionStartCount);
assertTrue(compactionCompleteCount >= 1,
"Should have triggered compaction complete at least once, got: " + compactionCompleteCount);
// Compaction should have succeeded
SessionCompactionCompleteEvent lastCompactionComplete = events.stream()
.filter(e -> e instanceof SessionCompactionCompleteEvent)
.map(e -> (SessionCompactionCompleteEvent) e).reduce((first, second) -> second).orElse(null);
assertNotNull(lastCompactionComplete);
assertTrue(lastCompactionComplete.getData().success(), "Compaction should have succeeded");
// Verify the session still works after compaction
AssistantMessageEvent answer = session
.sendAndWait(new MessageOptions().setPrompt("What was the story about?")).get(60, TimeUnit.SECONDS);
assertNotNull(answer);
assertNotNull(answer.getData().content());
// Should remember it was about a dragon (context preserved via summary)
assertTrue(answer.getData().content().toLowerCase().contains("dragon"),
"Should remember the story was about a dragon: " + answer.getData().content());
session.close();
}
}
/**
* Verifies that compaction events are not emitted when infinite sessions is
* disabled.
*
* @see Snapshot:
* compaction/should_not_emit_compaction_events_when_infinite_sessions_disabled
*/
@Test
void testShouldNotEmitCompactionEventsWhenInfiniteSessionsDisabled() throws Exception {
ctx.configureForTest("compaction", "should_not_emit_compaction_events_when_infinite_sessions_disabled");
var infiniteConfig = new InfiniteSessionConfig().setEnabled(false);
var config = new SessionConfig().setInfiniteSessions(infiniteConfig)
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL);
var compactionEvents = new ArrayList<SessionEvent>();
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(config).get();
session.on(event -> {
if (event instanceof SessionCompactionStartEvent || event instanceof SessionCompactionCompleteEvent) {
compactionEvents.add(event);
}
});
session.sendAndWait(new MessageOptions().setPrompt("What is 2+2?")).get(60, TimeUnit.SECONDS);
// Should not have any compaction events when disabled
assertEquals(0, compactionEvents.size(),
"Should not have any compaction events when infinite sessions is disabled");
session.close();
}
}
}