Skip to content

.NET: Remove required token params from HarnessAgent, make compaction opt-in#6409

Merged
westey-m merged 4 commits into
microsoft:mainfrom
westey-m:harness-optional-compaction
Jun 9, 2026
Merged

.NET: Remove required token params from HarnessAgent, make compaction opt-in#6409
westey-m merged 4 commits into
microsoft:mainfrom
westey-m:harness-optional-compaction

Conversation

@westey-m

@westey-m westey-m commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Removes the required maxContextWindowTokens and maxOutputTokens constructor parameters from HarnessAgent and AsHarnessAgent, replacing them with optional properties on HarnessAgentOptions.

When both MaxContextWindowTokens and MaxOutputTokens are provided in options, compaction is enabled as before. When either is null (the default), compaction is disabled entirely — making it opt-in.

New API

// Minimal — no compaction
var agent = new HarnessAgent(chatClient);

// With compaction enabled
var agent = new HarnessAgent(chatClient, new HarnessAgentOptions
{
    MaxContextWindowTokens = 1_050_000,
    MaxOutputTokens = 128_000,
});

Changes

  • HarnessAgentOptions.cs — Added MaxContextWindowTokens and MaxOutputTokens nullable int properties
  • HarnessAgent.cs — Simplified constructor, made compaction conditional
  • ChatClientHarnessExtensions.cs — Simplified AsHarnessAgent signature
  • 4 sample programs updated to use new API
  • Tests updated + new tests for compaction-disabled path

Closes #6333

Remove the required maxContextWindowTokens and maxOutputTokens
constructor parameters from HarnessAgent and AsHarnessAgent, replacing
them with optional MaxContextWindowTokens and MaxOutputTokens properties
on HarnessAgentOptions.

When both values are provided, compaction is enabled as before (in-loop
CompactionProvider and chat reducer on the default InMemoryChatHistory
Provider). When either is null, compaction is disabled entirely, making
it opt-in.

New constructor: HarnessAgent(IChatClient, HarnessAgentOptions?,
ILoggerFactory?, IServiceProvider?)

Closes microsoft#6333

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings June 9, 2026 10:23
@moonbox3 moonbox3 added the .NET label Jun 9, 2026
@github-actions github-actions Bot changed the title Remove required token params from HarnessAgent, make compaction opt-in .NET: Remove required token params from HarnessAgent, make compaction opt-in Jun 9, 2026

@github-actions github-actions Bot 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.

Automated Code Review

Reviewers: 5 | Confidence: 87%

✓ Correctness

The PR correctly makes compaction opt-in by moving token parameters to HarnessAgentOptions as nullable properties. The conditional logic properly enables compaction only when both values are provided, validation still fires via ContextWindowCompactionStrategy's constructor, the chat client pipeline is correctly assembled in both paths, and the InMemoryChatHistoryProvider is properly configured with or without a chat reducer. No correctness issues found.

✓ Security Reliability

The PR is clean from a security and reliability perspective. Input validation for token values is properly delegated to the ContextWindowCompactionStrategy constructor (which throws ArgumentOutOfRangeException for invalid values), null handling is correct throughout, and the conditional compaction logic is well-structured. There is one minor documentation/behavior inconsistency: when only MaxOutputTokens is set (without MaxContextWindowTokens), the code still applies it to ChatOptions.MaxOutputTokens despite the documentation claiming it will be 'left unchanged' in that scenario.

✓ Test Coverage

The PR correctly implements opt-in compaction and updates existing tests. However, the three new 'Compaction Opt-in' tests (lines 1743-1808) only assert NotNull — they verify construction succeds but don't confirm whether the CompactionProvider is actually present or absent in the pipeline. Given that the pipeline-inspection pattern (innerAgent.ChatClient.GetService<T>()) is already used elsewhere in this file, the new tests could meaningfully assert that the AIContextProviderChatClient is absent when compaction is disabled and present when enabled.

✓ Failure Modes

The PR cleanly makes compaction opt-in. Validation is delegated to ContextWindowCompactionStrategy (which throws on invalid values when both tokens are provided), and graceful degradation when compaction is disabled is sound. No silent failures, lost errors, or operational failure paths introduced. One minor doc/code inconsistency exists around MaxOutputTokens being applied to ChatOptions even when compaction is disabled, but this is safer behavior than documented.

✓ Design Approach

The overall opt-in compaction change is coherent, but the new implementation drops validation for partially supplied token settings. That leaves malformed HarnessAgentOptions values silently accepted instead of rejected, which is a design regression against the constructor contract and the existing compaction strategy validation behavior.

Suggestions

  • Doc/code mismatch: HarnessAgentOptions.MaxOutputTokens docs (line 60-61) state 'When either value is null, compaction is disabled and ChatOptions.MaxOutputTokens is left unchanged.' However, BuildChatOptions (HarnessAgent.cs:225-228) applies MaxOutputTokens to ChatOptions regardless of whether MaxContextWindowTokens is set. Consider either guarding the assignment with compactionEnabled or updating the documentation to reflect that MaxOutputTokens is always applied as a default cap on model output.

Automated review by westey-m's agents

Copilot AI 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

This PR updates the .NET Harness agent API to make context-window compaction opt-in by moving required token parameters into nullable HarnessAgentOptions properties, and simplifying both HarnessAgent and AsHarnessAgent construction accordingly.

Changes:

  • Removed required maxContextWindowTokens / maxOutputTokens constructor parameters and updated AsHarnessAgent to match.
  • Added nullable MaxContextWindowTokens / MaxOutputTokens to HarnessAgentOptions; compaction is enabled only when both are provided.
  • Updated unit tests and samples to use the new options-based API.

Reviewed changes

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

Show a summary per file
File Description
dotnet/tests/Microsoft.Agents.AI.Harness.UnitTests/HarnessAgentTests.cs Updates tests for new constructor/extension signatures and adds coverage for compaction opt-in scenarios.
dotnet/src/Microsoft.Agents.AI.Harness/HarnessAgentOptions.cs Introduces nullable token settings and documents compaction being opt-in.
dotnet/src/Microsoft.Agents.AI.Harness/HarnessAgent.cs Makes compaction conditional, updates default chat history behavior, and simplifies construction.
dotnet/src/Microsoft.Agents.AI.Harness/ChatClientHarnessExtensions.cs Simplifies AsHarnessAgent signature to accept only options/logger/services.
dotnet/samples/02-agents/Harness/Harness_Step04_CodeExecution/Program.cs Updates sample to pass compaction token settings via HarnessAgentOptions.
dotnet/samples/02-agents/Harness/Harness_Step03_DataProcessing/Program.cs Updates sample to pass compaction token settings via HarnessAgentOptions.
dotnet/samples/02-agents/Harness/Harness_Step02_Research_WithBackgroundAgents/Program.cs Updates sample agents to pass compaction token settings via HarnessAgentOptions.
dotnet/samples/02-agents/Harness/Harness_Step01_Research/Program.cs Updates sample to pass compaction token settings via HarnessAgentOptions.

Comment thread dotnet/src/Microsoft.Agents.AI.Harness/HarnessAgent.cs Outdated
Comment thread dotnet/src/Microsoft.Agents.AI.Harness/HarnessAgent.cs Outdated
…gentOptions

Allow users to provide their own CompactionStrategy via options, with
a clear priority system:
1. DisableCompaction=true: no compaction regardless of other settings
2. Custom CompactionStrategy provided: use it (token params ignored)
3. Both MaxContextWindowTokens and MaxOutputTokens set: default strategy
4. Otherwise: no compaction

Co-authored-by: Copilot <[email protected]>
- Update chatClient param XML doc to reflect compaction is opt-in
- Strengthen compaction tests to assert ChatReducer is null/not-null
  rather than just asserting construction succeeds

Co-authored-by: Copilot <[email protected]>
@westey-m westey-m added this pull request to the merge queue Jun 9, 2026
Merged via the queue into microsoft:main with commit 96d242f Jun 9, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove required tokens params from HarnessAgent

5 participants