Skip to content

.NET Compaction - Introduce preconfigured CompactionStrategy pattern#4665

Open
Copilot wants to merge 15 commits intomainfrom
copilot/add-preconfigured-compaction-strategy
Open

.NET Compaction - Introduce preconfigured CompactionStrategy pattern#4665
Copilot wants to merge 15 commits intomainfrom
copilot/add-preconfigured-compaction-strategy

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 12, 2026

Developers shouldn't need to understand internal strategy parameters to get a well-tuned compaction pipeline. This adds a CompactionStrategy.Create(approach, size, chatClient?) factory that maps two intuitive axes to a pre-configured PipelineCompactionStrategy.

New types

  • CompactionApproach — controls pipeline composition:

    • Gentle: tool-result collapsing + LLM summarization
    • Balanced: tool-result collapsing + LLM summarization + truncation backstop
    • Aggressive: tool-result collapsing + LLM summarization + sliding window + truncation
  • CompactionSize — scales all trigger thresholds to match model context capacity:

    • Compact ≈ 8 K tokens, Moderate ≈ 32 K tokens, Generous ≈ 64 K tokens

Factory logic

Within each pipeline, early stages trigger at a fraction of the size limit (⅔ for Balanced, ½ for Aggressive) so incremental compaction fires before reaching the emergency truncation backstop.

Usage

IChatClient summarizerClient = ...;

// Balanced pipeline for a mid-range model
CompactionStrategy strategy = CompactionStrategy.Create(
    CompactionApproach.Balanced,
    CompactionSize.Moderate,
    summarizerClient);

// Gentle pipeline — no LLM required
CompactionStrategy strategy = CompactionStrategy.Create(
    CompactionApproach.Gentle,
    CompactionSize.Compact);

chatClient is required (and validated) only when approach is Balanced or Aggressive.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? No.
Original prompt

This section details on the original issue you should resolve

<issue_title>.NET Compaction - Introduce preconfigured CompactionStrategy pattern</issue_title>
<issue_description>I expect many developers will ask: How do I configured my CompactionStrategy? ...which one do I use? ...what thresholds work best?

We can expose a factory that creates a pre-configured CompactionStrategy according to a set of options:

// Setup a chat client for summarization
IChatClient summarizingChatClient = ...;

// Create a compaction strategy based on a menu selection of characteristics
CompactionStrategy tunedStrategy =
   CompactionStrategy.Create(
      Approach.Balanced,
      Size.Compact,
      summarizingChatClient);

In this example, "size" could be:

  • Compact
  • Moderate
  • Generous

And "approach" could be:

  • Aggressive
  • Balanced
  • Gentle

Perhaps the "size" parameter is informed by the maximum input tokens allowed by a given model, with a default based on a conservative estimate of the most common usage.

Behind the scenes the combination of these two axes map to a certain CompactionStrategy configuration. As we improve our configuration, developers using this pattern will automatically realize those improvements.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…eate() factory

Co-authored-by: crickman <66376200+crickman@users.noreply.github.com>
Copilot AI changed the title [WIP] Add preconfigured CompactionStrategy pattern .NET Compaction - Introduce preconfigured CompactionStrategy pattern Mar 12, 2026
Copilot AI requested a review from crickman March 12, 2026 23:46
@crickman crickman moved this to In Progress in Agent Framework Mar 13, 2026
@crickman crickman marked this pull request as ready for review March 17, 2026 20:56
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a preconfigured compaction “menu” for the .NET Agent Framework, adding a CompactionStrategy.Create(approach, size, chatClient?) factory that maps high-level options to a tuned PipelineCompactionStrategy.

Changes:

  • Added CompactionApproach and CompactionSize enums to represent the two configuration axes.
  • Implemented CompactionStrategy.Create(...) factory to generate Gentle/Balanced/Aggressive pipelines with staged triggers.
  • Added unit tests validating pipeline composition, argument validation, and (partially) size behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
dotnet/src/Microsoft.Agents.AI/Compaction/CompactionStrategy.cs Marks CompactionStrategy as partial to allow adding the factory in a separate file.
dotnet/src/Microsoft.Agents.AI/Compaction/CompactionStrategy.Factory.cs Adds CompactionStrategy.Create(...) and internal size→threshold mappings for pipeline construction.
dotnet/src/Microsoft.Agents.AI/Compaction/CompactionApproach.cs Adds the CompactionApproach enum describing pipeline composition choices.
dotnet/src/Microsoft.Agents.AI/Compaction/CompactionSize.cs Adds the CompactionSize enum describing context-size profiles.
dotnet/tests/Microsoft.Agents.AI.UnitTests/Compaction/CompactionStrategyCreateTests.cs Adds unit tests for the new factory method and size differentiation behavior.

Comment thread dotnet/src/Microsoft.Agents.AI/Compaction/CompactionStrategy.Factory.cs Outdated
Comment thread dotnet/src/Microsoft.Agents.AI/Compaction/CompactionSize.cs Outdated
Comment thread dotnet/src/Microsoft.Agents.AI/Compaction/CompactionSize.cs Outdated
Comment thread dotnet/src/Microsoft.Agents.AI/Compaction/CompactionSize.cs Outdated
Comment thread dotnet/src/Microsoft.Agents.AI/Compaction/CompactionApproach.cs
crickman and others added 2 commits March 17, 2026 14:09
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Comment thread dotnet/src/Microsoft.Agents.AI/Compaction/CompactionStrategy.Factory.cs Outdated
@crickman crickman added this pull request to the merge queue Mar 27, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Mar 27, 2026
@crickman crickman added this pull request to the merge queue Mar 27, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Mar 28, 2026
@crickman crickman added this pull request to the merge queue Mar 28, 2026
github-merge-queue Bot pushed a commit that referenced this pull request Mar 28, 2026
#4665)

* Initial plan

* Add CompactionApproach/CompactionSize enums and CompactionStrategy.Create() factory

Co-authored-by: crickman <66376200+crickman@users.noreply.github.com>

* Cleanup

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Comments

* Updated

* Add "count" term

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: crickman <66376200+crickman@users.noreply.github.com>
Co-authored-by: Chris Rickman <crickman@microsoft.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Mar 28, 2026
@crickman crickman added this pull request to the merge queue Mar 28, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Mar 28, 2026
@crickman crickman added this pull request to the merge queue Mar 28, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Mar 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

.NET Compaction - Introduce preconfigured CompactionStrategy pattern

6 participants