forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompaction.test.ts
More file actions
89 lines (76 loc) · 3.6 KB
/
compaction.test.ts
File metadata and controls
89 lines (76 loc) · 3.6 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
import { describe, expect, it } from "vitest";
import { SessionEvent, approveAll } from "../../src/index.js";
import { createSdkTestContext } from "./harness/sdkTestContext.js";
// TODO: Compaction tests are skipped due to flakiness — re-enable once stabilized
describe.skip("Compaction", async () => {
const { copilotClient: client } = await createSdkTestContext();
it("should trigger compaction with low threshold and emit events", async () => {
// Create session with very low compaction thresholds to trigger compaction quickly
const session = await client.createSession({
onPermissionRequest: approveAll,
infiniteSessions: {
enabled: true,
// Trigger background compaction at 0.5% context usage (~1000 tokens)
backgroundCompactionThreshold: 0.005,
// Block at 1% to ensure compaction runs
bufferExhaustionThreshold: 0.01,
},
});
const events: SessionEvent[] = [];
session.on((event) => {
events.push(event);
});
// Send multiple messages to fill up the context window
// With such low thresholds, even a few messages should trigger compaction
await session.sendAndWait({
prompt: "Tell me a story about a dragon. Be detailed.",
});
await session.sendAndWait({
prompt: "Continue the story with more details about the dragon's castle.",
});
await session.sendAndWait({
prompt: "Now describe the dragon's treasure in great detail.",
});
// Check for compaction events
const compactionStartEvents = events.filter((e) => e.type === "session.compaction_start");
const compactionCompleteEvents = events.filter(
(e) => e.type === "session.compaction_complete"
);
// Should have triggered compaction at least once
expect(compactionStartEvents.length).toBeGreaterThanOrEqual(1);
expect(compactionCompleteEvents.length).toBeGreaterThanOrEqual(1);
// Compaction should have succeeded
const lastCompactionComplete =
compactionCompleteEvents[compactionCompleteEvents.length - 1];
expect(lastCompactionComplete.data.success).toBe(true);
// Should have removed some tokens
if (lastCompactionComplete.data.tokensRemoved !== undefined) {
expect(lastCompactionComplete.data.tokensRemoved).toBeGreaterThan(0);
}
// Verify the session still works after compaction
const answer = await session.sendAndWait({ prompt: "What was the story about?" });
expect(answer?.data.content).toBeDefined();
// Should remember it was about a dragon (context preserved via summary)
expect(answer?.data.content?.toLowerCase()).toContain("dragon");
}, 120000);
it("should not emit compaction events when infinite sessions disabled", async () => {
const session = await client.createSession({
onPermissionRequest: approveAll,
infiniteSessions: {
enabled: false,
},
});
const compactionEvents: SessionEvent[] = [];
session.on((event) => {
if (
event.type === "session.compaction_start" ||
event.type === "session.compaction_complete"
) {
compactionEvents.push(event);
}
});
await session.sendAndWait({ prompt: "What is 2+2?" });
// Should not have any compaction events when disabled
expect(compactionEvents.length).toBe(0);
});
});