TruncationCompactionStrategy Class

Definition

A compaction strategy that removes the oldest non-system message groups, keeping at least MinimumPreservedGroups most-recent groups intact.

public sealed class TruncationCompactionStrategy : Microsoft.Agents.AI.Compaction.CompactionStrategy
type TruncationCompactionStrategy = class
    inherit CompactionStrategy
Public NotInheritable Class TruncationCompactionStrategy
Inherits CompactionStrategy
Inheritance
TruncationCompactionStrategy

Remarks

This strategy preserves system messages and removes the oldest non-system message groups first. It respects atomic group boundaries — an assistant message with tool calls and its corresponding tool result messages are always removed together.

MinimumPreservedGroups is a hard floor: even if the Target has not been reached, compaction will not touch the last MinimumPreservedGroups non-system groups.

The CompactionTrigger controls when compaction proceeds. Use CompactionTriggers for common trigger conditions such as token or group thresholds.

Constructors

Name Description
TruncationCompactionStrategy(CompactionTrigger, Int32, CompactionTrigger)

Initializes a new instance of the TruncationCompactionStrategy class.

Fields

Name Description
DefaultMinimumPreserved

The default minimum number of most-recent non-system groups to preserve.

Properties

Name Description
MinimumPreservedGroups

Gets the minimum number of most-recent non-system message groups that are always preserved. This is a hard floor that compaction cannot exceed, regardless of the target condition.

Target

Gets the target predicate that controls when compaction stops. Strategies re-evaluate this after each incremental exclusion and stop when it returns true.

(Inherited from CompactionStrategy)
Trigger

Gets the trigger predicate that controls when compaction proceeds.

(Inherited from CompactionStrategy)

Methods

Name Description
CompactAsync(CompactionMessageIndex, ILogger, CancellationToken)

Evaluates the Trigger and, when it fires, delegates to CompactCoreAsync(CompactionMessageIndex, ILogger, CancellationToken) and reports compaction metrics.

(Inherited from CompactionStrategy)
CompactCoreAsync(CompactionMessageIndex, ILogger, CancellationToken)

Applies the strategy-specific compaction logic to the specified message index.

(Inherited from CompactionStrategy)

Extension Methods

Name Description
AsChatReducer(CompactionStrategy)

Returns an IChatReducer that applies this CompactionStrategy to reduce a list of messages.

Applies to