@@ -23,6 +23,7 @@ import type { Provider } from "../../src/provider/provider"
2323import * as SessionProcessorModule from "../../src/session/processor"
2424import { Snapshot } from "../../src/snapshot"
2525import { ProviderTest } from "../fake/provider"
26+ import { isOverflow } from "../../src/session/overflow"
2627
2728Log . init ( { print : false } )
2829
@@ -365,24 +366,24 @@ describe("session.compaction.isOverflow", () => {
365366 } )
366367 } )
367368
368- test ( "BUG: asymmetry — limit.input model allows 30K more usage before compaction than equivalent model without it " , async ( ) => {
369+ test ( "overflow uses total conversation size (input + output + reasoning + cache) " , async ( ) => {
369370 await using tmp = await tmpdir ( )
370371 await Instance . provide ( {
371372 directory : tmp . path ,
372373 fn : async ( ) => {
373- // Two models with identical context/output limits, differing only in limit.input
374374 const withInputLimit = createModel ( { context : 200_000 , input : 200_000 , output : 32_000 } )
375375 const withoutInputLimit = createModel ( { context : 200_000 , output : 32_000 } )
376376
377- // 170K total tokens — well above context-output (168K) but below input limit (200K)
378- const tokens = { input : 166_000 , output : 10_000 , reasoning : 0 , cache : { read : 5_000 , write : 0 } }
377+ // Total size = 100K + 70K + 0 + 5K + 0 = 175K
378+ const tokens = { input : 100_000 , output : 70_000 , reasoning : 0 , cache : { read : 5_000 , write : 0 } }
379379
380380 const withLimit = await SessionCompaction . isOverflow ( { tokens, model : withInputLimit } )
381381 const withoutLimit = await SessionCompaction . isOverflow ( { tokens, model : withoutInputLimit } )
382382
383- // Both models have identical real capacity — they should agree:
384- expect ( withLimit ) . toBe ( true ) // should compact (170K leaves no room for 32K output)
385- expect ( withoutLimit ) . toBe ( true ) // correctly compacts (170K > 168K)
383+ // withInputLimit: usable = 200K - 20K = 180K. totalSize=175K < 180K. No overflow.
384+ expect ( withLimit ) . toBe ( false )
385+ // withoutInputLimit: usable = 200K - 32K = 168K. totalSize=175K > 168K. Overflow.
386+ expect ( withoutLimit ) . toBe ( true )
386387 } ,
387388 } )
388389 } )
@@ -1085,7 +1086,7 @@ describe("session.getUsage", () => {
10851086 expect ( result . tokens . input ) . toBe ( 1000 )
10861087 expect ( result . tokens . output ) . toBe ( 400 )
10871088 expect ( result . tokens . reasoning ) . toBe ( 100 )
1088- expect ( result . tokens . total ) . toBe ( 1500 )
1089+ expect ( result . total ) . toBe ( 1000 + 400 + 100 + 0 + 0 )
10891090 } )
10901091
10911092 test ( "does not double count reasoning tokens in cost" , ( ) => {
@@ -1182,8 +1183,7 @@ describe("session.getUsage", () => {
11821183 expect ( result . tokens . input ) . toBe ( 500 )
11831184 expect ( result . tokens . cache . read ) . toBe ( 200 )
11841185 expect ( result . tokens . cache . write ) . toBe ( 300 )
1185- // total = adjusted (500) + output (500) + cacheRead (200) + cacheWrite (300)
1186- expect ( result . tokens . total ) . toBe ( 1500 )
1186+ expect ( result . total ) . toBe ( 500 + 500 + 0 + 200 + 300 )
11871187 return
11881188 }
11891189
@@ -1201,8 +1201,7 @@ describe("session.getUsage", () => {
12011201 expect ( result . tokens . input ) . toBe ( 500 )
12021202 expect ( result . tokens . cache . read ) . toBe ( 200 )
12031203 expect ( result . tokens . cache . write ) . toBe ( 300 )
1204- // total = adjusted (500) + output (500) + cacheRead (200) + cacheWrite (300)
1205- expect ( result . tokens . total ) . toBe ( 1500 )
1204+ expect ( result . total ) . toBe ( 500 + 500 + 0 + 200 + 300 )
12061205 } ,
12071206 )
12081207
@@ -1227,4 +1226,195 @@ describe("session.getUsage", () => {
12271226 expect ( result . tokens . cache . read ) . toBe ( 200 )
12281227 expect ( result . tokens . cache . write ) . toBe ( 300 )
12291228 } )
1229+
1230+ test ( "total is derived from token components" , ( ) => {
1231+ const model = createModel ( { context : 100_000 , output : 32_000 } )
1232+ const result = Session . getUsage ( {
1233+ model,
1234+ usage : {
1235+ inputTokens : 1000 ,
1236+ outputTokens : 500 ,
1237+ totalTokens : 1500 ,
1238+ } ,
1239+ } )
1240+ expect ( result . total ) . toBe ( 1000 + 500 + 0 + 0 + 0 )
1241+ expect ( result . tokens . input ) . toBe ( 1000 )
1242+ expect ( result . tokens . output ) . toBe ( 500 )
1243+ } )
1244+ } )
1245+
1246+ describe ( "MessageV2.token helpers" , ( ) => {
1247+ test ( "promptSize returns input + cache read + cache write" , ( ) => {
1248+ const tokens = { input : 5000 , output : 2000 , reasoning : 500 , cache : { read : 3000 , write : 1000 } }
1249+ expect ( MessageV2 . promptSize ( tokens ) ) . toBe ( 5000 + 3000 + 1000 )
1250+ } )
1251+
1252+ test ( "promptSize works with zero cache" , ( ) => {
1253+ const tokens = { input : 5000 , output : 2000 , reasoning : 0 , cache : { read : 0 , write : 0 } }
1254+ expect ( MessageV2 . promptSize ( tokens ) ) . toBe ( 5000 )
1255+ } )
1256+
1257+ test ( "totalSize returns sum of all token fields" , ( ) => {
1258+ const tokens = { input : 5000 , output : 2000 , reasoning : 500 , cache : { read : 3000 , write : 1000 } }
1259+ expect ( MessageV2 . totalSize ( tokens ) ) . toBe ( 5000 + 2000 + 500 + 3000 + 1000 )
1260+ } )
1261+ } )
1262+
1263+ describe ( "isOverflow" , ( ) => {
1264+ function makeConfig ( opts ?: { auto ?: boolean ; reserved ?: number } ) : Config . Info {
1265+ return {
1266+ compaction : {
1267+ auto : opts ?. auto ?? true ,
1268+ reserved : opts ?. reserved ,
1269+ } ,
1270+ } as Config . Info
1271+ }
1272+
1273+ test ( "returns false when auto compaction is disabled" , ( ) => {
1274+ const model = createModel ( { context : 100_000 , output : 32_000 } )
1275+ const tokens = { input : 90_000 , output : 500 , reasoning : 0 , cache : { read : 0 , write : 0 } }
1276+ expect ( isOverflow ( { cfg : makeConfig ( { auto : false } ) , tokens, model } ) ) . toBe ( false )
1277+ } )
1278+
1279+ test ( "returns false when context limit is 0" , ( ) => {
1280+ const model = {
1281+ ...createModel ( { context : 100_000 , output : 32_000 } ) ,
1282+ limit : { ...createModel ( { context : 100_000 , output : 32_000 } ) . limit , context : 0 } ,
1283+ }
1284+ const tokens = { input : 90_000 , output : 500 , reasoning : 0 , cache : { read : 0 , write : 0 } }
1285+ expect ( isOverflow ( { cfg : makeConfig ( ) , tokens, model } ) ) . toBe ( false )
1286+ } )
1287+
1288+ test ( "uses total size (all token fields) for overflow check" , ( ) => {
1289+ const model = createModel ( { context : 100_000 , output : 32_000 } )
1290+ // Total size = 50_000 + 30_000 + 5_000 + 10_000 + 5_000 = 100_000
1291+ // With context=100k and output=32k, usable = 100k - 20k = 80k
1292+ // 100k >= 80k => overflowing
1293+ const tokens = { input : 50_000 , output : 30_000 , reasoning : 5_000 , cache : { read : 10_000 , write : 5_000 } }
1294+ expect ( isOverflow ( { cfg : makeConfig ( ) , tokens, model } ) ) . toBe ( true )
1295+ } )
1296+
1297+ test ( "does not overflow when total size fits in usable context" , ( ) => {
1298+ const model = createModel ( { context : 100_000 , output : 32_000 } )
1299+ // Total size = 40_000 + 10_000 + 5_000 + 5_000 + 0 = 60_000
1300+ // usable = 100k - 20k = 80k
1301+ // 60k < 80k => not overflowing
1302+ const tokens = { input : 40_000 , output : 10_000 , reasoning : 5_000 , cache : { read : 5_000 , write : 0 } }
1303+ expect ( isOverflow ( { cfg : makeConfig ( ) , tokens, model } ) ) . toBe ( false )
1304+ } )
1305+
1306+ test ( "overflows when total size exceeds usable context" , ( ) => {
1307+ const model = createModel ( { context : 100_000 , output : 32_000 } )
1308+ // Total size = 70_000 + 1_000 + 0 + 10_000 + 5_000 = 86_000
1309+ // usable = 100k - 20k = 80k
1310+ // 86k >= 80k => overflowing
1311+ const tokens = { input : 70_000 , output : 1_000 , reasoning : 0 , cache : { read : 10_000 , write : 5_000 } }
1312+ expect ( isOverflow ( { cfg : makeConfig ( ) , tokens, model } ) ) . toBe ( true )
1313+ } )
1314+
1315+ test ( "respects custom reserved value" , ( ) => {
1316+ const model = createModel ( { context : 100_000 , output : 32_000 } )
1317+ // Total size = 70_000 + 1_000 + 0 + 10_000 + 5_000 = 86_000
1318+ // reserved=50k, usable=100k-50k=50k
1319+ // 86k >= 50k => overflow
1320+ const tokens = { input : 70_000 , output : 1_000 , reasoning : 0 , cache : { read : 10_000 , write : 5_000 } }
1321+ expect ( isOverflow ( { cfg : makeConfig ( { reserved : 50_000 } ) , tokens, model } ) ) . toBe ( true )
1322+ } )
1323+ } )
1324+
1325+ describe ( "token accumulation across steps" , ( ) => {
1326+ test ( "accumulates output, reasoning, and cache.write across steps; replaces input and cache.read" , ( ) => {
1327+ // Step 1: initial request
1328+ const step1Tokens = Session . getUsage ( {
1329+ model : createModel ( { context : 100_000 , output : 32_000 } ) ,
1330+ usage : { inputTokens : 10000 , outputTokens : 500 , totalTokens : 10500 } ,
1331+ } ) . tokens
1332+ expect ( step1Tokens . input ) . toBe ( 10000 )
1333+ expect ( step1Tokens . output ) . toBe ( 500 )
1334+ expect ( step1Tokens . reasoning ) . toBe ( 0 )
1335+ expect ( step1Tokens . cache . read ) . toBe ( 0 )
1336+ expect ( step1Tokens . cache . write ) . toBe ( 0 )
1337+
1338+ // After step 1: assistantMessage.tokens = step1Tokens (no prev)
1339+ let accumulated = { ...step1Tokens , cache : { ...step1Tokens . cache } }
1340+
1341+ // Step 2: tool result, conversation has grown
1342+ const step2Tokens = Session . getUsage ( {
1343+ model : createModel ( { context : 100_000 , output : 32_000 } ) ,
1344+ usage : {
1345+ inputTokens : 15000 ,
1346+ outputTokens : 800 ,
1347+ reasoningTokens : 200 ,
1348+ cachedInputTokens : 5000 ,
1349+ totalTokens : 16000 ,
1350+ } ,
1351+ } ) . tokens
1352+ expect ( step2Tokens . input ) . toBe ( 10000 ) // 15000 - 5000 (cached)
1353+ expect ( step2Tokens . output ) . toBe ( 600 ) // 800 - 200 (reasoning)
1354+ expect ( step2Tokens . reasoning ) . toBe ( 200 )
1355+ expect ( step2Tokens . cache . read ) . toBe ( 5000 )
1356+ expect ( step2Tokens . cache . write ) . toBe ( 0 )
1357+
1358+ // Simulate what processor.ts does: accumulate additive fields, replace snapshot fields
1359+ accumulated = {
1360+ input : step2Tokens . input ,
1361+ output : ( accumulated . output ?? 0 ) + step2Tokens . output ,
1362+ reasoning : ( accumulated . reasoning ?? 0 ) + step2Tokens . reasoning ,
1363+ cache : {
1364+ read : step2Tokens . cache . read ,
1365+ write : ( accumulated . cache ?. write ?? 0 ) + step2Tokens . cache . write ,
1366+ } ,
1367+ }
1368+
1369+ // After two steps:
1370+ expect ( accumulated . input ) . toBe ( 10000 ) // replaced: last step's input
1371+ expect ( accumulated . output ) . toBe ( 500 + 600 ) // accumulated: 1100
1372+ expect ( accumulated . reasoning ) . toBe ( 0 + 200 ) // accumulated: 200
1373+ expect ( accumulated . cache . read ) . toBe ( 5000 ) // replaced: last step's cache read
1374+ expect ( accumulated . cache . write ) . toBe ( 0 + 0 ) // accumulated: 0
1375+
1376+ // Step 3: more tool calls, cache hits
1377+ const step3Tokens = Session . getUsage ( {
1378+ model : createModel ( { context : 100_000 , output : 32_000 } ) ,
1379+ usage : {
1380+ inputTokens : 20000 ,
1381+ outputTokens : 1200 ,
1382+ reasoningTokens : 300 ,
1383+ cachedInputTokens : 12000 ,
1384+ totalTokens : 21200 ,
1385+ } ,
1386+ metadata : { anthropic : { cacheCreationInputTokens : 500 } } ,
1387+ } ) . tokens
1388+ expect ( step3Tokens . input ) . toBe ( 7500 ) // 20000 - 12000 - 500
1389+ expect ( step3Tokens . output ) . toBe ( 900 ) // 1200 - 300
1390+ expect ( step3Tokens . reasoning ) . toBe ( 300 )
1391+ expect ( step3Tokens . cache . read ) . toBe ( 12000 )
1392+ expect ( step3Tokens . cache . write ) . toBe ( 500 )
1393+
1394+ accumulated = {
1395+ input : step3Tokens . input ,
1396+ output : accumulated . output + step3Tokens . output ,
1397+ reasoning : accumulated . reasoning + step3Tokens . reasoning ,
1398+ cache : {
1399+ read : step3Tokens . cache . read ,
1400+ write : accumulated . cache . write + step3Tokens . cache . write ,
1401+ } ,
1402+ }
1403+
1404+ // After three steps:
1405+ expect ( accumulated . input ) . toBe ( 7500 ) // replaced: latest step
1406+ expect ( accumulated . output ) . toBe ( 500 + 600 + 900 ) // accumulated: 2000
1407+ expect ( accumulated . reasoning ) . toBe ( 0 + 200 + 300 ) // accumulated: 500
1408+ expect ( accumulated . cache . read ) . toBe ( 12000 ) // replaced: latest step
1409+ expect ( accumulated . cache . write ) . toBe ( 0 + 0 + 500 ) // accumulated: 500
1410+
1411+ // promptSize = input tokens sent to the LLM (current prompt footprint)
1412+ const promptSize = MessageV2 . promptSize ( accumulated )
1413+ expect ( promptSize ) . toBe ( 7500 + 12000 + 500 ) // 20_000
1414+
1415+ // totalSize = full conversation size (used for context overflow and display %)
1416+ // includes output because the next turn's prompt will include this response
1417+ const totalSize = MessageV2 . totalSize ( accumulated )
1418+ expect ( totalSize ) . toBe ( 7500 + 2000 + 500 + 12000 + 500 ) // 22_500
1419+ } )
12301420} )
0 commit comments