Skip to content

.NET: Add context window size compaction strategy for harness - #5304

Merged
westey (westey-m) merged 3 commits into
microsoft:feature-harnessfrom
westey-m:harness-compaction
Apr 16, 2026
Merged

.NET: Add context window size compaction strategy for harness#5304
westey (westey-m) merged 3 commits into
microsoft:feature-harnessfrom
westey-m:harness-compaction

Conversation

@westey-m

Copy link
Copy Markdown
Contributor

Motivation and Context

Adding an opinionated compaction strategy that is easy to use based on context window size.

Description

  • Add context window size compaction strategy for harness

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? If yes, add "[BREAKING]" prefix to the title of the PR.

CopilotAI review requested due to automatic review settings April 16, 2026 11:13
@moonbox3Evan Mattson (moonbox3) added the .NET Usage: [Issues, PRs], Target: .Net label Apr 16, 2026
@github-actionsgithub-actionsBot changed the title Add context window size compaction strategy for harness.NET: Add context window size compaction strategy for harnessApr 16, 2026

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds an opinionated “context window size”-driven compaction strategy to the .NET Agent Framework and wires it into the Harness research sample to keep long-running sessions within token budgets.

Changes:

  • Introduces ContextWindowCompactionStrategy, which derives tool-eviction and truncation triggers from (maxContextWindowTokens - maxOutputTokens).
  • Adds unit tests covering constructor validation and basic compaction behavior.
  • Updates the Harness Step01 Research sample to use the new compaction strategy and display token usage breakdown during streaming.

Reviewed changes

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

Show a summary per file
FileDescription
dotnet/src/Microsoft.Agents.AI/Compaction/ContextWindowCompactionStrategy.csNew strategy wrapping a compaction pipeline with thresholds computed from context window and output limits.
dotnet/tests/Microsoft.Agents.AI.UnitTests/Compaction/ContextWindowCompactionStrategyTests.csNew unit tests for constructor validation and compacting behavior.
dotnet/samples/02-agents/Harness/Harness_Step01_Research/Program.csUses the new strategy in the Harness sample; switches to Foundry endpoint env var and configures local history reduction.
dotnet/samples/02-agents/Harness/Harness_Step01_Research/Harness_Step01_Research.csprojAdds a project reference needed for OpenAI Responses extensions used by the sample.
dotnet/samples/02-agents/Harness/Harness_Shared_Console/HarnessConsole.csEnhances streaming console output to show token usage/reasoning and adds optional context-window parameters.

Comment threaddotnet/samples/02-agents/Harness/Harness_Shared_Console/HarnessConsole.cs Outdated
Comment threaddotnet/samples/02-agents/Harness/Harness_Step01_Research/Program.cs Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

@westey-mwestey (westey-m) left a comment

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Replying to open review comments with fixes applied — see individual thread replies below.

@moonbox3Evan Mattson (moonbox3) added the documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs label Apr 16, 2026
@westey-m
westey (westey-m) merged commit d0ac1d8 into microsoft:feature-harnessApr 16, 2026
13 checks passed
pullBot pushed a commit to nagyist/ms-agent-framework that referenced this pull request May 1, 2026
* .NET: Add a TODO AIContextProvider (microsoft#5233)
* Add a TODO AIContextProvider
* Add unit tests
* Address PR comments
* Address PR comments
* Fix test after removing one tool
* .NET: Add a ModeProvider for managing agent modes (microsoft#5247)
* Add a ModeProvider for managing agent modes
* Fix typo
* Fix typo
* Fix typo
* Address PR comments
* .NET: Add sample to show how to build a harness (microsoft#5268)
* Add sample to show how to build a harness
* Improve sample
* Sample max output tokens and model
* Fix encoding
* Fix model name in readme
* Address PR comments
* .NET: Add context window size compaction strategy for harness (microsoft#5304)
* Add context window size compaction strategy for harness
* Apply suggestions from code review
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Address PR comments
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* .NET: Add a file memory provider (microsoft#5315)
* Add a file memory provider
* Address PR comments
* Fix review comments.
* Add additional unit tests
* Addressing PR comments.
* .NET: Harness: Improve prompts and add FileSystem store (microsoft#5365)
* Harness: Improve prompts and add FileSystem store
* Address PR comments
* .NET: Harness: Improve path validation (microsoft#5404)
* Harness: Improve path validation
* Address PR comments
* .NET: Add always approve helpers, improve sample and fix bug (microsoft#5451)
* Add always approve helpers, improve sample and fix bug
* Address PR comments
* .NET: Make Todo, Mode and FileMemory providers more configurable (microsoft#5477)
* Make Todo, Mode and FileMemory providers more configurable
* Address PR comments.
* .NET: Add subagents provider and sample (microsoft#5518)
* Add subagents provider and sample
* Addressing PR comments.
* .NET: Harness filememory index plus instructions consistency (microsoft#5540)
* Add FileMemoryProvider index and improve instruction consistency
* Address PR comments.
* Address PR comments
* Address PR comments.
* Apply suggestion from @rogerbarreto
Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>
---------
Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>
* .NET: Refactor harness console to be more extensible and easy to understand with better UX (microsoft#5573)
* Refactor harness console to be more extensible and easy to understand with better UX.
* Fix formatting issues.
* Allow multiple clarifications in one response
* Address PR comments
* .NET: Add FileAccessProvdider and concurrency fix for FileMemoryProvider (microsoft#5583)
* Add FileAccessProvdider and concurrency fix for FileMemoryProvider
* Address PR comments
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentationUsage: [Issues, PRs], Target: documentation in the code base and learn docs.NETUsage: [Issues, PRs], Target: .Net

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@westey-m@lokitoth@rogerbarreto@peibekwe@moonbox3