Skip to content

Commit 8d4d0dd

Browse files
committed
fix: show warning when live usage crosses threshold (first response case)
Previously shouldShowWarning only used lastUsagePercentage, so the warning never appeared during the first streaming response. Now uses max of last and live usage, ensuring the countdown renders when it should.
1 parent 0f6c5ae commit 8d4d0dd

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/browser/utils/compaction/autoCompactionCheck.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,30 @@ describe("checkAutoCompaction", () => {
369369
expect(result.usagePercentage).toBe(75); // usagePercentage reflects live even with empty history
370370
});
371371

372+
test("shouldShowWarning uses live usage when no history exists", () => {
373+
// No lastUsage, liveUsage at 65% - should show warning (65% >= 60%)
374+
const liveUsage = createUsageEntry(130_000); // 65%
375+
const usage: WorkspaceUsageState = {
376+
usageHistory: [],
377+
totalTokens: 0,
378+
liveUsage,
379+
};
380+
const result = checkAutoCompaction(usage, KNOWN_MODELS.SONNET.id, false);
381+
382+
expect(result.shouldShowWarning).toBe(true);
383+
expect(result.shouldForceCompact).toBe(false); // 65% < 75%
384+
});
385+
386+
test("shouldShowWarning uses max of last and live usage", () => {
387+
// lastUsage at 50% (below warning), liveUsage at 72% (above warning)
388+
const liveUsage = createUsageEntry(144_000); // 72%
389+
const usage = createMockUsage(100_000, undefined, KNOWN_MODELS.SONNET.id, liveUsage);
390+
const result = checkAutoCompaction(usage, KNOWN_MODELS.SONNET.id, false);
391+
392+
expect(result.shouldShowWarning).toBe(true); // 72% >= 60%
393+
expect(result.shouldForceCompact).toBe(false); // 72% < 75%
394+
});
395+
372396
test("shouldForceCompact is false when auto-compaction disabled", () => {
373397
const usage = createMockUsage(190_000); // 95% - would trigger if enabled
374398
const result = checkAutoCompaction(usage, KNOWN_MODELS.SONNET.id, false, 1.0); // disabled

src/browser/utils/compaction/autoCompactionCheck.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,11 @@ export function checkAutoCompaction(
106106
const forceCompactThreshold = thresholdPercentage + FORCE_COMPACTION_BUFFER_PERCENT;
107107
const shouldForceCompact = usagePercentage >= forceCompactThreshold;
108108

109-
// Warning based on last completed usage (not live) to avoid flickering
109+
// Warning uses max of last completed and current (live when streaming)
110+
// This ensures warning shows when live usage spikes above threshold mid-stream
110111
const lastUsagePercentage = lastUsage ? (getTotalTokens(lastUsage) / maxTokens) * 100 : 0;
111-
const shouldShowWarning = lastUsagePercentage >= thresholdPercentage - warningAdvancePercent;
112+
const shouldShowWarning =
113+
Math.max(lastUsagePercentage, usagePercentage) >= thresholdPercentage - warningAdvancePercent;
112114

113115
return {
114116
shouldShowWarning,

0 commit comments

Comments
 (0)