Skip to content

fix: reduce false positive compaction detection#118

Merged
graykode merged 4 commits into
graykode:mainfrom
KorenKrita:fix/compaction-detection
May 13, 2026
Merged

fix: reduce false positive compaction detection#118
graykode merged 4 commits into
graykode:mainfrom
KorenKrita:fix/compaction-detection

Conversation

@KorenKrita
Copy link
Copy Markdown
Contributor

@KorenKrita KorenKrita commented May 11, 2026

Problem

Compaction detection (the C count shown in context info like 200k C35) was producing massive false positives. The original logic detected compaction whenever context size (inp + cr) dropped >30% between turns:

// Before: any context drop > 30% counts as compaction
if prev_context > 0 && result.last_context_tokens < prev_context * 7 / 10 {
    compaction_count += 1;
}

In agentic loops, cache_read fluctuates 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:

if prev_context > 0
    && result.last_context_tokens < prev_context * 7 / 10
    && prev_cache_read > 1000
    && cr < prev_cache_read / 5
{
    compaction_count += 1;
}

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 forward prev_cache_read even when it drops to zero and add delta.compaction_count to the cached total.

Affected file

  • src/collector/claude.rs — compaction detection, transcript file identity, incremental merge state, and regression coverage

Testing

  • cargo test — 139 passed
  • cargo clippy -- -D warnings — passed

KorenKrita and others added 4 commits May 11, 2026 18:56
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.
@graykode graykode merged commit 18cfdb1 into graykode:main May 13, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants