Skip to content

feat: add compaction-counter widget#282

Open
CorticalCode wants to merge 1 commit intosirmalloc:mainfrom
CorticalCode:feature/compaction-counter
Open

feat: add compaction-counter widget#282
CorticalCode wants to merge 1 commit intosirmalloc:mainfrom
CorticalCode:feature/compaction-counter

Conversation

@CorticalCode
Copy link
Copy Markdown
Contributor

Summary

  • Adds a compaction-counter widget that tracks context compaction events per session
  • When Claude Code compacts conversation context, used_percentage drops — this widget detects the drop and displays ↻N
  • Addresses the detection gap noted in Ctx is not being updated after /compact #92 by using statusline data instead of JSONL transcripts

How It Works

Context only grows until compaction — any drop in used_percentage > 2 percentage points between consecutive renders is counted as a compaction event. The 2-point threshold filters rounding noise and cache accounting wobble (±1 point) while reliably catching real compaction events on both 200K and 1M context windows.

Per-session state is stored in ~/.cache/ccstatusline/compaction/compaction-{sessionId}.json, following the skills caching pattern.

Caveats

Changes

File Change
src/utils/compaction.ts New — detection logic + session state I/O (Zod-validated)
src/widgets/CompactionCounter.ts New — widget class (returns ↻N or null, supports preview mode)
src/utils/__tests__/compaction.test.ts New — 11 detection unit tests
src/widgets/__tests__/CompactionCounter.test.ts New — 13 widget tests
src/types/RenderContext.ts Added CompactionData interface and compactionData field
src/ccstatusline.ts Compaction detection in render pipeline (conditional, only when widget is configured)
src/utils/widget-manifest.ts Registered compaction-counter
src/widgets/index.ts Barrel export

Design Decisions

  • No configurable threshold — kept simple for first contribution. 2% handles both 200K and 1M windows.
  • Conditional gathering — compaction detection only runs when the widget is present in the configured layout (follows existing pattern for speed/skills widgets)
  • Zod validation on state file parse to handle corruption gracefully
  • JSON state files (not JSONL) since we only track 2 numbers per session

Test Plan

  • bun test — 24 new tests pass (11 detection + 13 widget), no regressions
  • bun run lint — zero errors, zero warnings
  • bun run build — succeeds, dist/ccstatusline.js produced
  • 3 pre-existing keychain test failures confirmed present on main (not introduced by this PR)

Origin

Ported from CorticalCode/cc-statusline, a TypeScript statusline project with compression detection, burn-rate velocity projection, and SQLite usage logging.

🤖 Generated with Claude Code

Tracks context compaction events per session. When Claude Code compacts
conversation context, used_percentage drops — this widget detects the
drop and displays ↻N.

Detection: any drop in used_percentage > 2 percentage points between
consecutive renders counts as compaction. The 2-point threshold filters
rounding noise and cache wobble while catching real compaction events
on both 200K and 1M context windows.

State stored per-session in ~/.cache/ccstatusline/compaction/ as JSON,
following the skills caching pattern.

Related: addresses gap noted in sirmalloc#92

Origin: ported from CorticalCode/cc-statusline
https://github.com/CorticalCode/cc-statusline

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

1 participant