@@ -5,61 +5,67 @@ import { createSdkTestContext } from "./harness/sdkTestContext.js";
55describe ( "Compaction" , async ( ) => {
66 const { copilotClient : client } = await createSdkTestContext ( ) ;
77
8- it ( "should trigger compaction with low threshold and emit events" , async ( ) => {
9- // Create session with very low compaction thresholds to trigger compaction quickly
10- const session = await client . createSession ( {
11- infiniteSessions : {
12- enabled : true ,
13- // Trigger background compaction at 10% context usage
14- backgroundCompactionThreshold : 0.1 ,
15- // Block at 20% to ensure compaction runs
16- bufferExhaustionThreshold : 0.2 ,
17- } ,
18- } ) ;
19-
20- const events : SessionEvent [ ] = [ ] ;
21- session . on ( ( event ) => {
22- events . push ( event ) ;
23- } ) ;
24-
25- // Send multiple messages to fill up the context window
26- // With such low thresholds, even a few messages should trigger compaction
27- await session . sendAndWait ( {
28- prompt : "Tell me a long story about a dragon. Be very detailed." ,
29- } ) ;
30- await session . sendAndWait ( {
31- prompt : "Continue the story with more details about the dragon's castle." ,
32- } ) ;
33- await session . sendAndWait ( {
34- prompt : "Now describe the dragon's treasure in great detail." ,
35- } ) ;
8+ it (
9+ "should trigger compaction with low threshold and emit events" ,
10+ async ( ) => {
11+ // Create session with very low compaction thresholds to trigger compaction quickly
12+ const session = await client . createSession ( {
13+ infiniteSessions : {
14+ enabled : true ,
15+ // Trigger background compaction at 0.5% context usage (~1000 tokens)
16+ backgroundCompactionThreshold : 0.005 ,
17+ // Block at 1% to ensure compaction runs
18+ bufferExhaustionThreshold : 0.01 ,
19+ } ,
20+ } ) ;
21+
22+ const events : SessionEvent [ ] = [ ] ;
23+ session . on ( ( event ) => {
24+ events . push ( event ) ;
25+ } ) ;
26+
27+ // Send multiple messages to fill up the context window
28+ // With such low thresholds, even a few messages should trigger compaction
29+ await session . sendAndWait ( {
30+ prompt : "Tell me a long story about a dragon. Be very detailed." ,
31+ } ) ;
32+ await session . sendAndWait ( {
33+ prompt : "Continue the story with more details about the dragon's castle." ,
34+ } ) ;
35+ await session . sendAndWait ( {
36+ prompt : "Now describe the dragon's treasure in great detail." ,
37+ } ) ;
38+
39+ // Check for compaction events
40+ const compactionStartEvents = events . filter (
41+ ( e ) => e . type === "session.compaction_start"
42+ ) ;
43+ const compactionCompleteEvents = events . filter (
44+ ( e ) => e . type === "session.compaction_complete"
45+ ) ;
46+
47+ // Should have triggered compaction at least once
48+ expect ( compactionStartEvents . length ) . toBeGreaterThanOrEqual ( 1 ) ;
49+ expect ( compactionCompleteEvents . length ) . toBeGreaterThanOrEqual ( 1 ) ;
50+
51+ // Compaction should have succeeded
52+ const lastCompactionComplete =
53+ compactionCompleteEvents [ compactionCompleteEvents . length - 1 ] ;
54+ expect ( lastCompactionComplete . data . success ) . toBe ( true ) ;
55+
56+ // Should have removed some tokens
57+ if ( lastCompactionComplete . data . tokensRemoved !== undefined ) {
58+ expect ( lastCompactionComplete . data . tokensRemoved ) . toBeGreaterThan ( 0 ) ;
59+ }
3660
37- // Check for compaction events
38- const compactionStartEvents = events . filter ( ( e ) => e . type === "session.compaction_start" ) ;
39- const compactionCompleteEvents = events . filter (
40- ( e ) => e . type === "session.compaction_complete"
41- ) ;
42-
43- // Should have triggered compaction at least once
44- expect ( compactionStartEvents . length ) . toBeGreaterThanOrEqual ( 1 ) ;
45- expect ( compactionCompleteEvents . length ) . toBeGreaterThanOrEqual ( 1 ) ;
46-
47- // Compaction should have succeeded
48- const lastCompactionComplete =
49- compactionCompleteEvents [ compactionCompleteEvents . length - 1 ] ;
50- expect ( lastCompactionComplete . data . success ) . toBe ( true ) ;
51-
52- // Should have removed some tokens
53- if ( lastCompactionComplete . data . tokensRemoved !== undefined ) {
54- expect ( lastCompactionComplete . data . tokensRemoved ) . toBeGreaterThan ( 0 ) ;
55- }
56-
57- // Verify the session still works after compaction
58- const answer = await session . sendAndWait ( { prompt : "What was the story about?" } ) ;
59- expect ( answer ?. data . content ) . toBeDefined ( ) ;
60- // Should remember it was about a dragon (context preserved via summary)
61- expect ( answer ?. data . content ?. toLowerCase ( ) ) . toContain ( "dragon" ) ;
62- } ) ;
61+ // Verify the session still works after compaction
62+ const answer = await session . sendAndWait ( { prompt : "What was the story about?" } ) ;
63+ expect ( answer ?. data . content ) . toBeDefined ( ) ;
64+ // Should remember it was about a dragon (context preserved via summary)
65+ expect ( answer ?. data . content ?. toLowerCase ( ) ) . toContain ( "dragon" ) ;
66+ } ,
67+ 120000
68+ ) ;
6369
6470 it ( "should emit usage info events showing context window state" , async ( ) => {
6571 const session = await client . createSession ( {
@@ -102,8 +108,30 @@ describe("Compaction", async () => {
102108 expect ( usageEvents . length ) . toBeGreaterThanOrEqual ( 1 ) ;
103109
104110 const lastUsageEvent = usageEvents [ usageEvents . length - 1 ] ;
105- expect ( lastUsageEvent . data . inputTokens ) . toBeGreaterThan ( 0 ) ;
106- expect ( lastUsageEvent . data . outputTokens ) . toBeGreaterThan ( 0 ) ;
111+ // Token counts may be 0 when replaying from snapshot, but model should be defined
107112 expect ( lastUsageEvent . data . model ) . toBeDefined ( ) ;
108113 } ) ;
114+
115+ it ( "should not emit compaction events when infinite sessions disabled" , async ( ) => {
116+ const session = await client . createSession ( {
117+ infiniteSessions : {
118+ enabled : false ,
119+ } ,
120+ } ) ;
121+
122+ const compactionEvents : SessionEvent [ ] = [ ] ;
123+ session . on ( ( event ) => {
124+ if (
125+ event . type === "session.compaction_start" ||
126+ event . type === "session.compaction_complete"
127+ ) {
128+ compactionEvents . push ( event ) ;
129+ }
130+ } ) ;
131+
132+ await session . sendAndWait ( { prompt : "What is 2+2?" } ) ;
133+
134+ // Should not have any compaction events when disabled
135+ expect ( compactionEvents . length ) . toBe ( 0 ) ;
136+ } ) ;
109137} ) ;
0 commit comments