diff --git a/ContextCompactor.cs b/ContextCompactor.cs index 0bbda8b..a325a7f 100644 --- a/ContextCompactor.cs +++ b/ContextCompactor.cs @@ -34,7 +34,7 @@ internal sealed partial class ContextCompactor(IChatClient client) { int compacted = 0; int userTurnsSeen = 0; - var filesRead = new HashSet(StringComparer.OrdinalIgnoreCase); + Dictionary filesRead = new(StringComparer.OrdinalIgnoreCase); // Walk backwards: index 0 is system prompt, so we stop at 1. for (int i = history.Count - 1; i >= 1; i--) @@ -71,10 +71,10 @@ internal sealed partial class ContextCompactor(IChatClient client) string reason = ""; // Rule 1: Deduplication. If we have already seen this file in a newer message (since we are walking backward), redact this one. - if (filesRead.Contains(filePath)) + if (filesRead.TryGetValue(filePath, out int count) && count >= 3) { shouldRedact = true; - reason = "deduplication — you read this file again later"; + reason = "deduplication — you read this file 3 or more times later"; } // Rule 2: TTL. If this was read 2 or more user turns ago, redact it. else if (userTurnsSeen >= 2) @@ -92,7 +92,7 @@ internal sealed partial class ContextCompactor(IChatClient client) else { // Keep it, but mark that we've seen it so older reads of the same file are redacted. - filesRead.Add(filePath); + filesRead[filePath] = filesRead.GetValueOrDefault(filePath) + 1; } } }