-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathCompactionTests.cs
More file actions
110 lines (95 loc) · 4 KB
/
CompactionTests.cs
File metadata and controls
110 lines (95 loc) · 4 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
using System.Runtime.InteropServices;
using GitHub.Copilot.SDK.Test.Harness;
using Xunit;
using Xunit.Abstractions;
namespace GitHub.Copilot.SDK.Test;
public class CompactionTests(E2ETestFixture fixture, ITestOutputHelper output) : E2ETestBase(fixture, "compaction", output)
{
[Fact]
public async Task Should_Trigger_Compaction_With_Low_Threshold_And_Emit_Events()
{
// Create session with very low compaction thresholds to trigger compaction quickly
var session = await CreateSessionAsync(new SessionConfig
{
InfiniteSessions = new InfiniteSessionConfig
{
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
}
});
var compactionStartEvents = new List<SessionCompactionStartEvent>();
var compactionCompleteEvents = new List<SessionCompactionCompleteEvent>();
session.On(evt =>
{
if (evt is SessionCompactionStartEvent startEvt)
{
compactionStartEvents.Add(startEvt);
}
if (evt is SessionCompactionCompleteEvent completeEvt)
{
compactionCompleteEvents.Add(completeEvt);
}
});
// Send multiple messages to fill up the context window
await session.SendAndWaitAsync(new MessageOptions
{
Prompt = "Tell me a story about a dragon. Be detailed."
});
await session.SendAndWaitAsync(new MessageOptions
{
Prompt = "Continue the story with more details about the dragon's castle."
});
await session.SendAndWaitAsync(new MessageOptions
{
Prompt = "Now describe the dragon's treasure in great detail."
});
// Should have triggered compaction at least once
Assert.True(compactionStartEvents.Count >= 1, "Expected at least 1 compaction_start event");
Assert.True(compactionCompleteEvents.Count >= 1, "Expected at least 1 compaction_complete event");
// Compaction should have succeeded
var lastComplete = compactionCompleteEvents[^1];
Assert.True(lastComplete.Data.Success, "Expected compaction to succeed");
// Should have removed some tokens
if (lastComplete.Data.TokensRemoved.HasValue)
{
Assert.True(lastComplete.Data.TokensRemoved > 0, "Expected tokensRemoved > 0");
}
// Verify the session still works after compaction
var answer = await session.SendAndWaitAsync(new MessageOptions
{
Prompt = "What was the story about?"
});
Assert.NotNull(answer);
Assert.NotNull(answer!.Data.Content);
// Should remember it was about a dragon (context preserved via summary)
Assert.Contains("dragon", answer.Data.Content.ToLower());
}
[Fact]
public async Task Should_Not_Emit_Compaction_Events_When_Infinite_Sessions_Disabled()
{
var session = await CreateSessionAsync(new SessionConfig
{
InfiniteSessions = new InfiniteSessionConfig
{
Enabled = false
}
});
var compactionEvents = new List<SessionEvent>();
session.On(evt =>
{
if (evt is SessionCompactionStartEvent or SessionCompactionCompleteEvent)
{
compactionEvents.Add(evt);
}
});
await session.SendAndWaitAsync(new MessageOptions { Prompt = "What is 2+2?" });
// Should not have any compaction events when disabled
Assert.Empty(compactionEvents);
}
}