|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------------------------------------------*/ |
| 4 | + |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using GitHub.Copilot.SDK.Test.Harness; |
| 7 | +using Xunit; |
| 8 | +using Xunit.Abstractions; |
| 9 | + |
| 10 | +namespace GitHub.Copilot.SDK.Test; |
| 11 | + |
| 12 | +public class CompactionTests(E2ETestFixture fixture, ITestOutputHelper output) : E2ETestBase(fixture, "compaction", output) |
| 13 | +{ |
| 14 | + [Fact(Skip = "Compaction tests are flaky on Windows CI - proxy startup timing issues")] |
| 15 | + [Trait("Category", "SkipOnWindows")] |
| 16 | + public async Task Should_Trigger_Compaction_With_Low_Threshold_And_Emit_Events() |
| 17 | + { |
| 18 | + // Create session with very low compaction thresholds to trigger compaction quickly |
| 19 | + var session = await Client.CreateSessionAsync(new SessionConfig |
| 20 | + { |
| 21 | + InfiniteSessions = new InfiniteSessionConfig |
| 22 | + { |
| 23 | + Enabled = true, |
| 24 | + // Trigger background compaction at 0.5% context usage (~1000 tokens) |
| 25 | + BackgroundCompactionThreshold = 0.005, |
| 26 | + // Block at 1% to ensure compaction runs |
| 27 | + BufferExhaustionThreshold = 0.01 |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + var compactionStartEvents = new List<SessionCompactionStartEvent>(); |
| 32 | + var compactionCompleteEvents = new List<SessionCompactionCompleteEvent>(); |
| 33 | + |
| 34 | + session.On(evt => |
| 35 | + { |
| 36 | + if (evt is SessionCompactionStartEvent startEvt) |
| 37 | + { |
| 38 | + compactionStartEvents.Add(startEvt); |
| 39 | + } |
| 40 | + if (evt is SessionCompactionCompleteEvent completeEvt) |
| 41 | + { |
| 42 | + compactionCompleteEvents.Add(completeEvt); |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + // Send multiple messages to fill up the context window |
| 47 | + await session.SendAndWaitAsync(new MessageOptions |
| 48 | + { |
| 49 | + Prompt = "Tell me a long story about a dragon. Be very detailed." |
| 50 | + }); |
| 51 | + await session.SendAndWaitAsync(new MessageOptions |
| 52 | + { |
| 53 | + Prompt = "Continue the story with more details about the dragon's castle." |
| 54 | + }); |
| 55 | + await session.SendAndWaitAsync(new MessageOptions |
| 56 | + { |
| 57 | + Prompt = "Now describe the dragon's treasure in great detail." |
| 58 | + }); |
| 59 | + |
| 60 | + // Should have triggered compaction at least once |
| 61 | + Assert.True(compactionStartEvents.Count >= 1, "Expected at least 1 compaction_start event"); |
| 62 | + Assert.True(compactionCompleteEvents.Count >= 1, "Expected at least 1 compaction_complete event"); |
| 63 | + |
| 64 | + // Compaction should have succeeded |
| 65 | + var lastComplete = compactionCompleteEvents[^1]; |
| 66 | + Assert.True(lastComplete.Data.Success, "Expected compaction to succeed"); |
| 67 | + |
| 68 | + // Should have removed some tokens |
| 69 | + if (lastComplete.Data.TokensRemoved.HasValue) |
| 70 | + { |
| 71 | + Assert.True(lastComplete.Data.TokensRemoved > 0, "Expected tokensRemoved > 0"); |
| 72 | + } |
| 73 | + |
| 74 | + // Verify the session still works after compaction |
| 75 | + var answer = await session.SendAndWaitAsync(new MessageOptions |
| 76 | + { |
| 77 | + Prompt = "What was the story about?" |
| 78 | + }); |
| 79 | + Assert.NotNull(answer); |
| 80 | + Assert.NotNull(answer!.Data.Content); |
| 81 | + // Should remember it was about a dragon (context preserved via summary) |
| 82 | + Assert.Contains("dragon", answer.Data.Content.ToLower()); |
| 83 | + } |
| 84 | + |
| 85 | + [Fact(Skip = "Compaction tests are flaky on Windows CI - proxy startup timing issues")] |
| 86 | + [Trait("Category", "SkipOnWindows")] |
| 87 | + public async Task Should_Not_Emit_Compaction_Events_When_Infinite_Sessions_Disabled() |
| 88 | + { |
| 89 | + var session = await Client.CreateSessionAsync(new SessionConfig |
| 90 | + { |
| 91 | + InfiniteSessions = new InfiniteSessionConfig |
| 92 | + { |
| 93 | + Enabled = false |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + var compactionEvents = new List<SessionEvent>(); |
| 98 | + |
| 99 | + session.On(evt => |
| 100 | + { |
| 101 | + if (evt is SessionCompactionStartEvent or SessionCompactionCompleteEvent) |
| 102 | + { |
| 103 | + compactionEvents.Add(evt); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + await session.SendAndWaitAsync(new MessageOptions { Prompt = "What is 2+2?" }); |
| 108 | + |
| 109 | + // Should not have any compaction events when disabled |
| 110 | + Assert.Empty(compactionEvents); |
| 111 | + } |
| 112 | +} |
0 commit comments