@@ -24,7 +24,6 @@ export interface AutoCompactionCheckResult {
2424 shouldShowWarning : boolean ;
2525 usagePercentage : number ;
2626 thresholdPercentage : number ;
27- enabled : boolean ;
2827}
2928
3029// Show warning this many percentage points before threshold
@@ -45,53 +44,33 @@ const WARNING_ADVANCE_PERCENT = 10;
4544 * @param warningAdvancePercent - Show warning this many percentage points before threshold (default 10)
4645 * @returns Check result with warning flag and usage percentage
4746 */
48- export function shouldAutoCompact (
47+ export function checkAutoCompaction (
4948 usage : WorkspaceUsageState | undefined ,
50- model : string | null | undefined ,
49+ model : string | null ,
5150 use1M : boolean ,
52- enabled = true ,
51+ enabled : boolean ,
5352 threshold : number = DEFAULT_AUTO_COMPACTION_THRESHOLD ,
5453 warningAdvancePercent : number = WARNING_ADVANCE_PERCENT
5554) : AutoCompactionCheckResult {
5655 const thresholdPercentage = threshold * 100 ;
5756
5857 // Short-circuit if auto-compaction is disabled
59- if ( ! enabled || ! model ) {
58+ // Or if no usage data yet
59+ if ( ! enabled || ! model || ! usage || usage . usageHistory . length === 0 ) {
6060 return {
6161 shouldShowWarning : false ,
6262 usagePercentage : 0 ,
6363 thresholdPercentage,
64- enabled : false ,
65- } ;
66- }
67-
68- // No usage data yet - safe default (don't trigger on first message)
69- if ( ! usage || usage . usageHistory . length === 0 ) {
70- return {
71- shouldShowWarning : false ,
72- usagePercentage : 0 ,
73- thresholdPercentage,
74- enabled : true ,
7564 } ;
7665 }
7766
7867 // Determine max tokens for this model
7968 const modelStats = getModelStats ( model ) ;
8069 const maxTokens = use1M && supports1MContext ( model ) ? 1_000_000 : modelStats ?. max_input_tokens ;
70+ const lastUsage = usage . usageHistory [ usage . usageHistory . length - 1 ] ;
8171
8272 // No max tokens known - safe default (can't calculate percentage)
8373 if ( ! maxTokens ) {
84- return {
85- shouldShowWarning : false ,
86- usagePercentage : 0 ,
87- thresholdPercentage,
88- enabled : true ,
89- } ;
90- }
91-
92- // Use last usage entry to calculate current context size (matches UI display)
93- const lastUsage = usage . usageHistory [ usage . usageHistory . length - 1 ] ;
94- if ( ! lastUsage ) {
9574 return {
9675 shouldShowWarning : false ,
9776 usagePercentage : 0 ,
@@ -115,6 +94,5 @@ export function shouldAutoCompact(
11594 shouldShowWarning,
11695 usagePercentage,
11796 thresholdPercentage,
118- enabled : true ,
11997 } ;
12098}
0 commit comments