Skip to content

Fix context bar color to reflect autocompact proximity#14

Merged
himattm merged 2 commits intomainfrom
mmckenna/context-window-improvements
Feb 11, 2026
Merged

Fix context bar color to reflect autocompact proximity#14
himattm merged 2 commits intomainfrom
mmckenna/context-window-improvements

Conversation

@himattm
Copy link
Owner

@himattm himattm commented Feb 11, 2026

Summary

  • The context 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)
  • Added compactionProximity() helper to scale color thresholds against the autocompact trigger point, so the bar turns red when compaction is imminent
  • Displayed percentage and bar fill remain unchanged — only the color determination changes

Effect on display

Scenario Raw % Buffer Proximity Old Color New Color
The bug 81% 22.5% ~104% yellow red
Approaching 60% 22.5% ~77% white yellow
Normal 30% 22.5% ~39% white white
Buffer disabled 81% 0% 81% yellow yellow

Test plan

  • All 34 existing + new tests pass (go test ./internal/statusline/ -v)
  • go build ./... succeeds
  • Pre-commit hooks pass (gofmt, go vet, go build)
  • Manual test: 81% with default buffer shows red ANSI color
  • Manual test: 60% with default buffer shows yellow
  • Manual test: 30% with default buffer shows white
  • Manual test: buffer disabled (0%) — colors match raw percentage

🤖 Generated with Claude Code

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>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 336 to 344
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)
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines 767 to 781
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
}
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

Copilot uses AI. Check for mistakes.
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>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@himattm himattm merged commit 7fb69de into main Feb 11, 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.

1 participant