Skip to content

Commit 0ef46ab

Browse files
committed
🤖 fix: use cumulative token usage for auto-compaction check
Previous implementation only checked last message tokens against context window, effectively disabling auto-compaction unless a single response exceeded 70%. Now uses WorkspaceUsageState.totalTokens for cumulative conversation tracking. - Remove lastUsage extraction and manual token calculation - Use pre-calculated totalTokens from WorkspaceStore - Simplifies code by ~20 lines - Auto-compaction now correctly triggers at 70% cumulative usage
1 parent 75bea13 commit 0ef46ab

File tree

2 files changed

+4
-22
lines changed

2 files changed

+4
-22
lines changed

src/browser/utils/chatCommands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type { ParsedCommand } from "@/browser/utils/slashCommands/types";
2121
import { applyCompactionOverrides } from "@/browser/utils/messages/compactionOptions";
2222
import { resolveCompactionModel } from "@/browser/utils/messages/compactionModelPreference";
2323
import { getRuntimeKey } from "@/common/constants/storage";
24-
import { ImageAttachment } from "../components/ImageAttachments";
24+
import type { ImageAttachment } from "../components/ImageAttachments";
2525

2626
// ============================================================================
2727
// Workspace Creation

src/browser/utils/compaction/autoCompactionCheck.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const WARNING_ADVANCE_PERCENT = 10;
4040
* @param use1M - Whether 1M context is enabled
4141
* @param threshold - Usage percentage threshold (0.0-1.0, default 0.7 = 70%)
4242
* @param warningAdvancePercent - Show warning this many percentage points before threshold (default 10)
43-
* @returns Check result with shouldAutoCompact flag, warning flag, and usage details
43+
* @returns Check result with warning flag and usage percentage
4444
*/
4545
export function shouldAutoCompact(
4646
usage: WorkspaceUsageState | undefined,
@@ -60,16 +60,6 @@ export function shouldAutoCompact(
6060
};
6161
}
6262

63-
// Get last usage (most recent API response)
64-
const lastUsage = usage.usageHistory[usage.usageHistory.length - 1];
65-
if (!lastUsage) {
66-
return {
67-
shouldShowWarning: false,
68-
usagePercentage: 0,
69-
thresholdPercentage,
70-
};
71-
}
72-
7363
// Determine max tokens for this model
7464
const modelStats = getModelStats(model);
7565
const maxTokens = use1M && supports1MContext(model) ? 1_000_000 : modelStats?.max_input_tokens;
@@ -83,16 +73,8 @@ export function shouldAutoCompact(
8373
};
8474
}
8575

86-
// Calculate total tokens used in last request
87-
const totalUsed =
88-
lastUsage.input.tokens +
89-
lastUsage.cached.tokens +
90-
lastUsage.cacheCreate.tokens +
91-
lastUsage.output.tokens +
92-
lastUsage.reasoning.tokens;
93-
94-
// Calculate usage percentage
95-
const usagePercentage = (totalUsed / maxTokens) * 100;
76+
// Calculate usage percentage from cumulative conversation total
77+
const usagePercentage = (usage.totalTokens / maxTokens) * 100;
9678

9779
// Show warning if within advance window (e.g., 60% for 70% threshold with 10% advance)
9880
const shouldShowWarning = usagePercentage >= thresholdPercentage - warningAdvancePercent;

0 commit comments

Comments
 (0)