diff --git a/agent/compaction/compaction_test.go b/agent/compaction/compaction_test.go index 3aa44fea..08026b43 100644 --- a/agent/compaction/compaction_test.go +++ b/agent/compaction/compaction_test.go @@ -85,6 +85,40 @@ func TestTruncationStrategy_ExcludesOldestGroups(t *testing.T) { } } +func TestTruncationStrategy_SkipsPreExcludedAndSystemGroups(t *testing.T) { + index := compaction.CreateMessageIndex([]*message.Message{ + textMessage(message.RoleSystem, "system"), + textMessage(message.RoleUser, "u1"), + textMessage(message.RoleAssistant, "a1"), + textMessage(message.RoleUser, "u2"), + }, nil) + index.Groups[1].IsExcluded = true + strategy := &compaction.TruncationStrategy{ + MinimumPreservedGroups: 1, + } + + compacted, err := strategy.Compact(t.Context(), index) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if !compacted { + t.Fatal("expected compaction") + } + + if index.Groups[0].IsExcluded { + t.Fatal("expected system group to be preserved") + } + if !index.Groups[1].IsExcluded { + t.Fatal("expected pre-excluded group to remain excluded") + } + if !index.Groups[2].IsExcluded { + t.Fatal("expected oldest removable group to be excluded") + } + if index.Groups[3].IsExcluded { + t.Fatal("expected minimum preserved group to remain included") + } +} + func TestTruncationStrategy_ZeroValueUsesDefaults(t *testing.T) { index := compaction.CreateMessageIndex(turnMessages(17), nil) strategy := &compaction.TruncationStrategy{} diff --git a/agent/compaction/truncation.go b/agent/compaction/truncation.go index 00f80211..dbed94a3 100644 --- a/agent/compaction/truncation.go +++ b/agent/compaction/truncation.go @@ -35,12 +35,7 @@ func (strategy *TruncationStrategy) Compact(_ context.Context, index *MessageInd } minimumPreservedGroups := cmp.Or(max(strategy.MinimumPreservedGroups, 0), defaultMinimumPreservedTruncationGroups) - var removableCount int - for _, group := range index.Groups { - if !group.IsExcluded && group.Kind != GroupKindSystem { - removableCount++ - } - } + removableCount := index.IncludedNonSystemGroupCount() maxRemovable := removableCount - minimumPreservedGroups if maxRemovable <= 0 { return false, nil