Fix context bar color to reflect autocompact proximity#14
Conversation
The bar color was based on raw usage percentage, so it showed yellow at 81% even when autocompact was about to trigger (at ~77.5% with a 22.5% buffer). Add compactionProximity() to scale the color thresholds against the autocompact trigger point, making the bar turn red when compaction is imminent while keeping the displayed percentage and bar fill unchanged. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Adjusts the context bar color to reflect how close the session is to the autocompact trigger point, instead of using the raw context usage percentage, so the bar turns red when compaction is imminent (even if raw usage is still < 90%).
Changes:
- Add
compactionProximity()to scale raw context usage against the autocompact trigger threshold. - Update
renderContext()/renderContextBar()to use scaled percentage for color selection while keeping raw percentage for fill + displayed percent. - Add unit tests covering the bug scenario (81% raw with default buffer) and buffer-disabled behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
internal/statusline/statusline.go |
Introduces proximity-based color scaling and threads a separate colorPct into the context bar renderer. |
internal/statusline/statusline_test.go |
Updates existing context bar tests for the new signature and adds tests for proximity + color behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| func compactionProximity(rawPct int, bufferPct float64) int { | ||
| if bufferPct <= 0 { | ||
| return rawPct | ||
| } | ||
| proximity := float64(rawPct) * 100.0 / (100.0 - bufferPct) | ||
| if proximity > 100 { | ||
| return 100 | ||
| } | ||
| return int(proximity) |
There was a problem hiding this comment.
compactionProximity() can produce +Inf/NaN or negative values when bufferPct >= 100 (or very close to 100) because the denominator (100-bufferPct) becomes 0 or negative. Converting an infinite float to int is implementation-dependent in Go and can yield unpredictable results, which could break coloring at runtime for misconfigured autocompactBuffer values. Add validation/clamping for bufferPct (e.g., treat bufferPct >= 100 as 100% proximity / return 100, and consider clamping to [0,99.9]) and ensure showBuffer logic doesn’t enable the buffer zone for invalid values.
| func TestCompactionProximity(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| rawPct int | ||
| bufferPct float64 | ||
| expected int | ||
| }{ | ||
| {"81% raw with 22.5% buffer", 81, 22.5, 100}, // 81 * 100 / 77.5 = 104.5 → capped at 100 | ||
| {"60% raw with 22.5% buffer", 60, 22.5, 77}, // 60 * 100 / 77.5 = 77.4 | ||
| {"30% raw with 22.5% buffer", 30, 22.5, 38}, // 30 * 100 / 77.5 = 38.7 | ||
| {"81% raw with 0% buffer", 81, 0, 81}, // disabled, returns raw | ||
| {"95% raw with 0% buffer", 95, 0, 95}, // disabled, returns raw | ||
| {"0% raw with 22.5% buffer", 0, 22.5, 0}, // 0 stays 0 | ||
| {"100% raw with 22.5% buffer", 100, 22.5, 100}, // capped at 100 | ||
| } |
There was a problem hiding this comment.
TestCompactionProximity currently doesn’t exercise the new helper’s edge cases around invalid/degenerate bufferPct values (e.g., 100% or >100%), which are the cases that can lead to Inf/NaN from the division. Add test cases for bufferPct >= 100 (and possibly a very high value like 150) to lock in the intended behavior after adding validation/clamping in compactionProximity().
Add a guard clause to return 100 when bufferPct is at or above 100%, preventing a division-by-zero in the proximity calculation. Add edge case tests for bufferPct = 100, bufferPct > 100, and negative bufferPct. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
compactionProximity()helper to scale color thresholds against the autocompact trigger point, so the bar turns red when compaction is imminentEffect on display
Test plan
go test ./internal/statusline/ -v)go build ./...succeeds🤖 Generated with Claude Code