@@ -9,7 +9,13 @@ import { BaseProvider } from "../../../api/providers/base-provider"
99import { ApiMessage } from "../../task-persistence/apiMessages"
1010import * as condenseModule from "../../condense"
1111
12- import { TOKEN_BUFFER_PERCENTAGE , estimateTokenCount , truncateConversation , manageContext } from "../index"
12+ import {
13+ TOKEN_BUFFER_PERCENTAGE ,
14+ estimateTokenCount ,
15+ truncateConversation ,
16+ manageContext ,
17+ willManageContext ,
18+ } from "../index"
1319
1420// Create a mock ApiHandler for testing
1521class MockApiHandler extends BaseProvider {
@@ -1280,4 +1286,125 @@ describe("Context Management", () => {
12801286 expect ( result2 . truncationId ) . toBeDefined ( )
12811287 } )
12821288 } )
1289+
1290+ /**
1291+ * Tests for the willManageContext helper function
1292+ */
1293+ describe ( "willManageContext" , ( ) => {
1294+ it ( "should return true when context percent exceeds threshold" , ( ) => {
1295+ const result = willManageContext ( {
1296+ totalTokens : 60000 ,
1297+ contextWindow : 100000 , // 60% of context window
1298+ maxTokens : 30000 ,
1299+ autoCondenseContext : true ,
1300+ autoCondenseContextPercent : 50 , // 50% threshold
1301+ profileThresholds : { } ,
1302+ currentProfileId : "default" ,
1303+ lastMessageTokens : 0 ,
1304+ } )
1305+ expect ( result ) . toBe ( true )
1306+ } )
1307+
1308+ it ( "should return false when context percent is below threshold" , ( ) => {
1309+ const result = willManageContext ( {
1310+ totalTokens : 40000 ,
1311+ contextWindow : 100000 , // 40% of context window
1312+ maxTokens : 30000 ,
1313+ autoCondenseContext : true ,
1314+ autoCondenseContextPercent : 50 , // 50% threshold
1315+ profileThresholds : { } ,
1316+ currentProfileId : "default" ,
1317+ lastMessageTokens : 0 ,
1318+ } )
1319+ expect ( result ) . toBe ( false )
1320+ } )
1321+
1322+ it ( "should return true when tokens exceed allowedTokens even if autoCondenseContext is false" , ( ) => {
1323+ // allowedTokens = contextWindow * (1 - 0.1) - reservedTokens = 100000 * 0.9 - 30000 = 60000
1324+ const result = willManageContext ( {
1325+ totalTokens : 60001 , // Exceeds allowedTokens
1326+ contextWindow : 100000 ,
1327+ maxTokens : 30000 ,
1328+ autoCondenseContext : false , // Even with auto-condense disabled
1329+ autoCondenseContextPercent : 50 ,
1330+ profileThresholds : { } ,
1331+ currentProfileId : "default" ,
1332+ lastMessageTokens : 0 ,
1333+ } )
1334+ expect ( result ) . toBe ( true )
1335+ } )
1336+
1337+ it ( "should return false when autoCondenseContext is false and tokens are below allowedTokens" , ( ) => {
1338+ // allowedTokens = contextWindow * (1 - 0.1) - reservedTokens = 100000 * 0.9 - 30000 = 60000
1339+ const result = willManageContext ( {
1340+ totalTokens : 59999 , // Below allowedTokens
1341+ contextWindow : 100000 ,
1342+ maxTokens : 30000 ,
1343+ autoCondenseContext : false ,
1344+ autoCondenseContextPercent : 50 , // This shouldn't matter since autoCondenseContext is false
1345+ profileThresholds : { } ,
1346+ currentProfileId : "default" ,
1347+ lastMessageTokens : 0 ,
1348+ } )
1349+ expect ( result ) . toBe ( false )
1350+ } )
1351+
1352+ it ( "should use profile-specific threshold when available" , ( ) => {
1353+ const result = willManageContext ( {
1354+ totalTokens : 55000 ,
1355+ contextWindow : 100000 , // 55% of context window
1356+ maxTokens : 30000 ,
1357+ autoCondenseContext : true ,
1358+ autoCondenseContextPercent : 80 , // Global threshold 80%
1359+ profileThresholds : { "test-profile" : 50 } , // Profile threshold 50%
1360+ currentProfileId : "test-profile" ,
1361+ lastMessageTokens : 0 ,
1362+ } )
1363+ // Should trigger because 55% > 50% (profile threshold)
1364+ expect ( result ) . toBe ( true )
1365+ } )
1366+
1367+ it ( "should fall back to global threshold when profile threshold is -1" , ( ) => {
1368+ const result = willManageContext ( {
1369+ totalTokens : 55000 ,
1370+ contextWindow : 100000 , // 55% of context window
1371+ maxTokens : 30000 ,
1372+ autoCondenseContext : true ,
1373+ autoCondenseContextPercent : 80 , // Global threshold 80%
1374+ profileThresholds : { "test-profile" : - 1 } , // Profile uses global
1375+ currentProfileId : "test-profile" ,
1376+ lastMessageTokens : 0 ,
1377+ } )
1378+ // Should NOT trigger because 55% < 80% (global threshold)
1379+ expect ( result ) . toBe ( false )
1380+ } )
1381+
1382+ it ( "should include lastMessageTokens in the calculation" , ( ) => {
1383+ // Without lastMessageTokens: 49000 tokens = 49%
1384+ // With lastMessageTokens: 49000 + 2000 = 51000 tokens = 51%
1385+ const resultWithoutLastMessage = willManageContext ( {
1386+ totalTokens : 49000 ,
1387+ contextWindow : 100000 ,
1388+ maxTokens : 30000 ,
1389+ autoCondenseContext : true ,
1390+ autoCondenseContextPercent : 50 , // 50% threshold
1391+ profileThresholds : { } ,
1392+ currentProfileId : "default" ,
1393+ lastMessageTokens : 0 ,
1394+ } )
1395+ expect ( resultWithoutLastMessage ) . toBe ( false )
1396+
1397+ const resultWithLastMessage = willManageContext ( {
1398+ totalTokens : 49000 ,
1399+ contextWindow : 100000 ,
1400+ maxTokens : 30000 ,
1401+ autoCondenseContext : true ,
1402+ autoCondenseContextPercent : 50 , // 50% threshold
1403+ profileThresholds : { } ,
1404+ currentProfileId : "default" ,
1405+ lastMessageTokens : 2000 , // Pushes total to 51%
1406+ } )
1407+ expect ( resultWithLastMessage ) . toBe ( true )
1408+ } )
1409+ } )
12831410} )
0 commit comments