fix: reduce false positive compaction detection#118
Merged
Conversation
Previously, compaction was detected solely by context size dropping >30% between turns. This caused false positives from normal cache hit rate fluctuations where inp+cr varies between tool calls. Now requires both conditions: - Context drops > 30% between consecutive turns - cache_read drops > 80% (old cache invalidated by truncation) Real compaction invalidates cached conversation prefix, causing cache_read to plummet. Normal cache fluctuations don't trigger this.
The prev_cache_read field was not propagated during incremental transcript parsing (delta merge), causing it to remain 0 on subsequent collection cycles.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Compaction detection (the
Ccount shown in context info like200k C35) was producing massive false positives. The original logic detected compaction whenever context size (inp + cr) dropped >30% between turns:In agentic loops,
cache_readfluctuates naturally between tool calls, sometimes dropping 30%+ between consecutive turns without any actual conversation truncation. This caused compaction counts of 30-40+ on sessions that never experienced a real compaction event.Fix
Compaction now requires both a context drop and a hard cache-read drop, which indicates that the old cached prefix was invalidated:
The collector also preserves the previous context/cache state while tailing appended transcript bytes. On Unix, file identity now uses
(device, inode)instead of mtime so normal appends stay on the incremental path; replacements still force a full reparse. Delta merges now carry forwardprev_cache_readeven when it drops to zero and adddelta.compaction_countto the cached total.Affected file
src/collector/claude.rs— compaction detection, transcript file identity, incremental merge state, and regression coverageTesting
cargo test— 139 passedcargo clippy -- -D warnings— passed