1
0

feat: Relax deduplication in ContextCompactor to retain up to 3 file reads

This commit is contained in:
2026-03-05 11:27:21 +01:00
parent ed897aeb01
commit de6a21fb5a

View File

@@ -34,7 +34,7 @@ internal sealed partial class ContextCompactor(IChatClient client)
{ {
int compacted = 0; int compacted = 0;
int userTurnsSeen = 0; int userTurnsSeen = 0;
var filesRead = new HashSet<string>(StringComparer.OrdinalIgnoreCase); Dictionary<string, int> filesRead = new(StringComparer.OrdinalIgnoreCase);
// Walk backwards: index 0 is system prompt, so we stop at 1. // Walk backwards: index 0 is system prompt, so we stop at 1.
for (int i = history.Count - 1; i >= 1; i--) for (int i = history.Count - 1; i >= 1; i--)
@@ -71,10 +71,10 @@ internal sealed partial class ContextCompactor(IChatClient client)
string reason = ""; string reason = "";
// Rule 1: Deduplication. If we have already seen this file in a newer message (since we are walking backward), redact this one. // 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; 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. // Rule 2: TTL. If this was read 2 or more user turns ago, redact it.
else if (userTurnsSeen >= 2) else if (userTurnsSeen >= 2)
@@ -92,7 +92,7 @@ internal sealed partial class ContextCompactor(IChatClient client)
else else
{ {
// Keep it, but mark that we've seen it so older reads of the same file are redacted. // 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;
} }
} }
} }