.NET Compaction - "MessageProvider" approach (#3)#4533
Open
.NET Compaction - "MessageProvider" approach (#3)#4533
Conversation
Co-authored-by: crickman <66376200+crickman@users.noreply.github.com>
…essage-compaction-provider
Contributor
There was a problem hiding this comment.
Pull request overview
Introduces a new .NET “MessageProvider” compaction approach that groups chat history into atomic message groups (system/user/assistant/tool-call/summary) and applies configurable compaction strategies (including pipeline composition) before agent invocations, with supporting tests and a sample.
Changes:
- Added compaction core types (
MessageIndex/MessageGroup) and multiple built-in compaction strategies (tool-result collapse, summarization, sliding-window, truncation, chat-reducer bridge, and pipeline composition). - Added
MessageCompactionContextProviderfor in-run compaction via the agent context provider pipeline. - Added unit tests, a new Step 18 sample, and introduced
Microsoft.ML.Tokenizersdependency for token counting.
Reviewed changes
Copilot reviewed 32 out of 32 changed files in this pull request and generated 15 comments.
Show a summary per file
| File | Description |
|---|---|
| dotnet/tests/Microsoft.Agents.AI.UnitTests/Microsoft.Agents.AI.UnitTests.csproj | Adds Tokenizers dependency for compaction-related tests. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/Compaction/TruncationCompactionStrategyTests.cs | Tests truncation strategy behavior and edge cases. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/Compaction/ToolResultCompactionStrategyTests.cs | Tests tool-result collapsing behavior and triggers/targets. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/Compaction/SummarizationCompactionStrategyTests.cs | Tests LLM-based summarization strategy behavior and fallbacks. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/Compaction/SlidingWindowCompactionStrategyTests.cs | Tests turn-based sliding window exclusions and preservation rules. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/Compaction/PipelineCompactionStrategyTests.cs | Tests strategy pipeline composition and execution ordering. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/Compaction/MessageIndexTests.cs | Tests grouping, metrics, incremental update, and token counting behavior. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/Compaction/MessageCompactionContextProviderTests.cs | Tests session-state-backed context provider compaction behavior. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/Compaction/CompactionTriggersTests.cs | Tests trigger predicates and combinators. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/Compaction/CompactionStrategyTests.cs | Tests base strategy trigger/target semantics and short-circuiting. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/Compaction/ChatReducerCompactionStrategyTests.cs | Tests reducer-bridging strategy behavior and rebuild semantics. |
| dotnet/src/Microsoft.Agents.AI/Microsoft.Agents.AI.csproj | Adds Tokenizers dependency and fixes formatting for InternalsVisibleTo. |
| dotnet/src/Microsoft.Agents.AI/Compaction/TruncationCompactionStrategy.cs | Implements oldest-group truncation with minimum-preserved floor. |
| dotnet/src/Microsoft.Agents.AI/Compaction/ToolResultCompactionStrategy.cs | Implements collapsing older tool-call groups into concise assistant summaries. |
| dotnet/src/Microsoft.Agents.AI/Compaction/SummarizationCompactionStrategy.cs | Implements LLM summarization compaction into a summary group/message. |
| dotnet/src/Microsoft.Agents.AI/Compaction/SlidingWindowCompactionStrategy.cs | Implements turn-based sliding window compaction. |
| dotnet/src/Microsoft.Agents.AI/Compaction/PipelineCompactionStrategy.cs | Adds sequential composition of multiple compaction strategies. |
| dotnet/src/Microsoft.Agents.AI/Compaction/MessageIndex.cs | Adds grouping/indexing, metrics, and incremental update support for chat messages. |
| dotnet/src/Microsoft.Agents.AI/Compaction/MessageGroupKind.cs | Adds group-kind enum for compaction grouping. |
| dotnet/src/Microsoft.Agents.AI/Compaction/MessageGroup.cs | Adds message group model with metrics and exclusion semantics. |
| dotnet/src/Microsoft.Agents.AI/Compaction/MessageCompactionContextProvider.cs | Adds AIContextProvider that applies compaction per invocation with session state. |
| dotnet/src/Microsoft.Agents.AI/Compaction/CompactionTriggers.cs | Adds trigger helpers for token/message/turn/group thresholds and composition. |
| dotnet/src/Microsoft.Agents.AI/Compaction/CompactionTrigger.cs | Adds trigger delegate type. |
| dotnet/src/Microsoft.Agents.AI/Compaction/CompactionStrategy.cs | Adds strategy base class with trigger/target orchestration and metrics logging. |
| dotnet/src/Microsoft.Agents.AI/Compaction/ChatReducerCompactionStrategy.cs | Bridges IChatReducer into the compaction strategy model. |
| dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientExtensions.cs | Minor formatting adjustment in middleware setup. |
| dotnet/src/Microsoft.Agents.AI.Abstractions/InMemoryChatHistoryProvider.cs | Refactors reducer invocation path and introduces (currently stubbed) compaction hook. |
| dotnet/samples/02-agents/Agents/README.md | Adds new Step 18 compaction pipeline sample entry. |
| dotnet/samples/02-agents/Agents/Agent_Step18_CompactionPipeline/Program.cs | New sample demonstrating compaction pipeline usage with an agent/tool loop. |
| dotnet/samples/02-agents/Agents/Agent_Step18_CompactionPipeline/Agent_Step18_CompactionPipeline.csproj | New sample project wiring dependencies and project reference. |
| dotnet/agent-framework-dotnet.slnx | Adds new sample project to solution. |
| dotnet/Directory.Packages.props | Adds Microsoft.ML.Tokenizers package version. |
dotnet/src/Microsoft.Agents.AI/Compaction/SlidingWindowCompactionStrategy.cs
Outdated
Show resolved
Hide resolved
dotnet/src/Microsoft.Agents.AI/Compaction/ToolResultCompactionStrategy.cs
Show resolved
Hide resolved
dotnet/src/Microsoft.Agents.AI/Compaction/SummarizationCompactionStrategy.cs
Outdated
Show resolved
Hide resolved
dotnet/src/Microsoft.Agents.AI.Abstractions/InMemoryChatHistoryProvider.cs
Outdated
Show resolved
Hide resolved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
Context management (compaction) is one of the key features in the "dev-harness" effort. This change introduces structured handling of long-running AI chat conversations by compacting historical context while preserving key decisions and intent. By reducing token growth and context drift, it improves response quality, performance, and cost predictability over extended sessions. The implementation is designed to be extensible and transparent, making context lifecycle management a first‑class concern for agent development.
[Spec]
Description
The goal of this approach is inject compaction as part of the
IChatClientarchitecture. This approach maintains an index that allows for presenting a compacted version of the conversation without modifying the source "chat history".Features
IChatReducerDetails
Compaction occurs via a
CompactionStrategy. A set of strategies are incuded as part of this initial release, including a pipeline strategy that is able to sequentially apply one or more strategies:ToolResultCompactionStrategySummarizationCompactionStrategySlidingWindowCompactionStrategyTruncationCompactionStrategyPipelineCompactionStrategyChatReducerCompactionStrategyIChatReducerIChatReducerCode
or
or
Contribution Checklist