Skip to content

feat: add compaction-counter widget#282

Open
CorticalCode wants to merge 2 commits intosirmalloc:mainfrom
CorticalCode:feature/compaction-counter
Open

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

Conversation

@CorticalCode
Copy link
Copy Markdown
Contributor

@CorticalCode CorticalCode commented Apr 4, 2026

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)

Recent changes

  • Security: Sanitize session ID before using in cache filename to prevent path traversal
  • Reliability: Cache write failures are now best-effort — no longer crash the status line
  • Tests: Added persistence round-trip, path traversal regression, and write failure tests

🤖 Generated with Claude Code

CorticalCode and others added 2 commits April 4, 2026 03:36
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>
- Sanitize session ID to prevent path traversal via crafted StatusJSON
- Wrap cache writes in try/catch — failures no longer crash status line
- Add persistence tests: round-trip, path traversal regression, write failure
- Clarify threshold doc (drop must exceed 2 points)

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