@@ -3,7 +3,6 @@ import { checkAutoCompaction } from "./autoCompactionCheck";
33import type { WorkspaceUsageState } from "@/browser/stores/WorkspaceStore" ;
44import type { ChatUsageDisplay } from "@/common/utils/tokens/usageAggregator" ;
55import { KNOWN_MODELS } from "@/common/constants/knownModels" ;
6- import { FORCE_COMPACTION_TOKEN_BUFFER } from "@/common/constants/ui" ;
76
87// Helper to create a mock usage entry
98const createUsageEntry = (
@@ -302,63 +301,72 @@ describe("checkAutoCompaction", () => {
302301 } ) ;
303302 } ) ;
304303
305- describe ( "Force Compaction (Live Usage)" , ( ) => {
304+ describe ( "Force Compaction (threshold + 5% buffer)" , ( ) => {
305+ // Force-compact triggers at threshold + 5%
306+ // With default 70% threshold, force-compact at 75%
306307 const SONNET_MAX_TOKENS = 200_000 ;
307- const BUFFER = FORCE_COMPACTION_TOKEN_BUFFER ;
308308
309- test ( "shouldForceCompact is false when no liveUsage (falls back to lastUsage with room)" , ( ) => {
310- const usage = createMockUsage ( 100_000 ) ; // 100k remaining - plenty of room
309+ test ( "shouldForceCompact is false when usage below threshold + 5%" , ( ) => {
310+ // 70% usage, threshold 70%, force at 75% - should NOT trigger
311+ const usage = createMockUsage ( 140_000 ) ; // 70%
311312 const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false ) ;
312313
313314 expect ( result . shouldForceCompact ) . toBe ( false ) ;
314315 } ) ;
315316
316- test ( "shouldForceCompact is false when currentUsage has plenty of room " , ( ) => {
317- const liveUsage = createUsageEntry ( 100_000 ) ; // 100k remaining
318- const usage = createMockUsage ( 50_000 , undefined , KNOWN_MODELS . SONNET . id , liveUsage ) ;
317+ test ( "shouldForceCompact is false when usage just below force threshold " , ( ) => {
318+ // 74% usage, threshold 70%, force at 75% - should NOT trigger
319+ const usage = createMockUsage ( 148_000 ) ; // 74%
319320 const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false ) ;
320321
321322 expect ( result . shouldForceCompact ) . toBe ( false ) ;
322323 } ) ;
323324
324- test ( "shouldForceCompact is true when remaining <= buffer" , ( ) => {
325- // Exactly at buffer threshold
326- const liveUsage = createUsageEntry ( SONNET_MAX_TOKENS - BUFFER ) ;
327- const usage = createMockUsage ( 50_000 , undefined , KNOWN_MODELS . SONNET . id , liveUsage ) ;
325+ test ( "shouldForceCompact is true when usage at force threshold" , ( ) => {
326+ // 75% usage, threshold 70%, force at 75% - should trigger
327+ const usage = createMockUsage ( 150_000 ) ; // 75%
328328 const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false ) ;
329329
330330 expect ( result . shouldForceCompact ) . toBe ( true ) ;
331331 } ) ;
332332
333- test ( "shouldForceCompact is true when over context limit " , ( ) => {
334- const liveUsage = createUsageEntry ( SONNET_MAX_TOKENS + 5000 ) ;
335- const usage = createMockUsage ( 50_000 , undefined , KNOWN_MODELS . SONNET . id , liveUsage ) ;
333+ test ( "shouldForceCompact is true when usage above force threshold " , ( ) => {
334+ // 80% usage, threshold 70%, force at 75% - should trigger
335+ const usage = createMockUsage ( 160_000 ) ; // 80%
336336 const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false ) ;
337337
338338 expect ( result . shouldForceCompact ) . toBe ( true ) ;
339339 } ) ;
340340
341- test ( "shouldForceCompact is false when just above buffer " , ( ) => {
342- // 1 token above buffer threshold
343- const liveUsage = createUsageEntry ( SONNET_MAX_TOKENS - BUFFER - 1 ) ;
344- const usage = createMockUsage ( 50_000 , undefined , KNOWN_MODELS . SONNET . id , liveUsage ) ;
341+ test ( "shouldForceCompact uses liveUsage when available " , ( ) => {
342+ // lastUsage at 50%, liveUsage at 75% - should trigger based on live
343+ const liveUsage = createUsageEntry ( 150_000 ) ; // 75%
344+ const usage = createMockUsage ( 100_000 , undefined , KNOWN_MODELS . SONNET . id , liveUsage ) ;
345345 const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false ) ;
346346
347- expect ( result . shouldForceCompact ) . toBe ( false ) ;
347+ expect ( result . shouldForceCompact ) . toBe ( true ) ;
348+ expect ( result . usagePercentage ) . toBe ( 75 ) ; // usagePercentage reflects live when streaming
349+ } ) ;
350+
351+ test ( "shouldForceCompact respects custom threshold" , ( ) => {
352+ // 55% usage with 50% threshold - force at 55%, should trigger
353+ const usage = createMockUsage ( 110_000 ) ; // 55%
354+ const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false , 0.5 ) ;
355+
356+ expect ( result . shouldForceCompact ) . toBe ( true ) ;
348357 } ) ;
349358
350359 test ( "shouldForceCompact respects 1M context mode" , ( ) => {
351- // With 1M context, exactly at buffer threshold
352- const liveUsage = createUsageEntry ( 1_000_000 - BUFFER ) ;
360+ // 75% of 1M = 750k tokens
361+ const liveUsage = createUsageEntry ( 750_000 ) ;
353362 const usage = createMockUsage ( 50_000 , undefined , KNOWN_MODELS . SONNET . id , liveUsage ) ;
354363 const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , true ) ;
355364
356365 expect ( result . shouldForceCompact ) . toBe ( true ) ;
357366 } ) ;
358367
359- test ( "shouldForceCompact triggers with empty history but liveUsage near limit" , ( ) => {
360- // Bug fix: empty history but liveUsage should still trigger
361- const liveUsage = createUsageEntry ( SONNET_MAX_TOKENS - BUFFER ) ;
368+ test ( "shouldForceCompact triggers with empty history but liveUsage at force threshold" , ( ) => {
369+ const liveUsage = createUsageEntry ( 150_000 ) ; // 75%
362370 const usage : WorkspaceUsageState = {
363371 usageHistory : [ ] ,
364372 totalTokens : 0 ,
@@ -367,12 +375,11 @@ describe("checkAutoCompaction", () => {
367375 const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false ) ;
368376
369377 expect ( result . shouldForceCompact ) . toBe ( true ) ;
370- expect ( result . usagePercentage ) . toBe ( 0 ) ; // No lastUsage for percentage
378+ expect ( result . usagePercentage ) . toBe ( 75 ) ; // usagePercentage reflects live even with empty history
371379 } ) ;
372380
373381 test ( "shouldForceCompact is false when auto-compaction disabled" , ( ) => {
374- const liveUsage = createUsageEntry ( 199_000 ) ; // Very close to limit
375- const usage = createMockUsage ( 50_000 , undefined , KNOWN_MODELS . SONNET . id , liveUsage ) ;
382+ const usage = createMockUsage ( 190_000 ) ; // 95% - would trigger if enabled
376383 const result = checkAutoCompaction ( usage , KNOWN_MODELS . SONNET . id , false , 1.0 ) ; // disabled
377384
378385 expect ( result . shouldForceCompact ) . toBe ( false ) ;
0 commit comments