@@ -24,6 +24,7 @@ import type { Provider } from "../../src/provider/provider"
2424import * as SessionProcessorModule from "../../src/session/processor"
2525import { Snapshot } from "../../src/snapshot"
2626import { ProviderTest } from "../fake/provider"
27+ import { isOverflow } from "../../src/session/overflow"
2728
2829Log . init ( { print : false } )
2930
@@ -375,24 +376,24 @@ describe("session.compaction.isOverflow", () => {
375376 } )
376377 } )
377378
378- test ( "BUG: asymmetry — limit.input model allows 30K more usage before compaction than equivalent model without it " , async ( ) => {
379+ test ( "overflow uses total conversation size (input + output + reasoning + cache) " , async ( ) => {
379380 await using tmp = await tmpdir ( )
380381 await Instance . provide ( {
381382 directory : tmp . path ,
382383 fn : async ( ) => {
383- // Two models with identical context/output limits, differing only in limit.input
384384 const withInputLimit = createModel ( { context : 200_000 , input : 200_000 , output : 32_000 } )
385385 const withoutInputLimit = createModel ( { context : 200_000 , output : 32_000 } )
386386
387- // 170K total tokens — well above context-output (168K) but below input limit (200K)
388- const tokens = { input : 166_000 , output : 10_000 , reasoning : 0 , cache : { read : 5_000 , write : 0 } }
387+ // Total size = 100K + 70K + 0 + 5K + 0 = 175K
388+ const tokens = { input : 100_000 , output : 70_000 , reasoning : 0 , cache : { read : 5_000 , write : 0 } }
389389
390390 const withLimit = await SessionCompaction . isOverflow ( { tokens, model : withInputLimit } )
391391 const withoutLimit = await SessionCompaction . isOverflow ( { tokens, model : withoutInputLimit } )
392392
393- // Both models have identical real capacity — they should agree:
394- expect ( withLimit ) . toBe ( true ) // should compact (170K leaves no room for 32K output)
395- expect ( withoutLimit ) . toBe ( true ) // correctly compacts (170K > 168K)
393+ // withInputLimit: usable = 200K - 20K = 180K. totalSize=175K < 180K. No overflow.
394+ expect ( withLimit ) . toBe ( false )
395+ // withoutInputLimit: usable = 200K - 32K = 168K. totalSize=175K > 168K. Overflow.
396+ expect ( withoutLimit ) . toBe ( true )
396397 } ,
397398 } )
398399 } )
@@ -1137,7 +1138,7 @@ describe("session.getUsage", () => {
11371138 expect ( result . tokens . input ) . toBe ( 1000 )
11381139 expect ( result . tokens . output ) . toBe ( 400 )
11391140 expect ( result . tokens . reasoning ) . toBe ( 100 )
1140- expect ( result . tokens . total ) . toBe ( 1500 )
1141+ expect ( result . total ) . toBe ( 1000 + 400 + 100 + 0 + 0 )
11411142 } )
11421143
11431144 test ( "does not double count reasoning tokens in cost" , ( ) => {
@@ -1268,8 +1269,7 @@ describe("session.getUsage", () => {
12681269 expect ( result . tokens . input ) . toBe ( 500 )
12691270 expect ( result . tokens . cache . read ) . toBe ( 200 )
12701271 expect ( result . tokens . cache . write ) . toBe ( 300 )
1271- // total = adjusted (500) + output (500) + cacheRead (200) + cacheWrite (300)
1272- expect ( result . tokens . total ) . toBe ( 1500 )
1272+ expect ( result . total ) . toBe ( 500 + 500 + 0 + 200 + 300 )
12731273 return
12741274 }
12751275
@@ -1287,8 +1287,7 @@ describe("session.getUsage", () => {
12871287 expect ( result . tokens . input ) . toBe ( 500 )
12881288 expect ( result . tokens . cache . read ) . toBe ( 200 )
12891289 expect ( result . tokens . cache . write ) . toBe ( 300 )
1290- // total = adjusted (500) + output (500) + cacheRead (200) + cacheWrite (300)
1291- expect ( result . tokens . total ) . toBe ( 1500 )
1290+ expect ( result . total ) . toBe ( 500 + 500 + 0 + 200 + 300 )
12921291 } ,
12931292 )
12941293
@@ -1321,4 +1320,195 @@ describe("session.getUsage", () => {
13211320 expect ( result . tokens . cache . read ) . toBe ( 200 )
13221321 expect ( result . tokens . cache . write ) . toBe ( 300 )
13231322 } )
1323+
1324+ test ( "total is derived from token components" , ( ) => {
1325+ const model = createModel ( { context : 100_000 , output : 32_000 } )
1326+ const result = Session . getUsage ( {
1327+ model,
1328+ usage : {
1329+ inputTokens : 1000 ,
1330+ outputTokens : 500 ,
1331+ totalTokens : 1500 ,
1332+ } ,
1333+ } )
1334+ expect ( result . total ) . toBe ( 1000 + 500 + 0 + 0 + 0 )
1335+ expect ( result . tokens . input ) . toBe ( 1000 )
1336+ expect ( result . tokens . output ) . toBe ( 500 )
1337+ } )
1338+ } )
1339+
1340+ describe ( "MessageV2.token helpers" , ( ) => {
1341+ test ( "promptSize returns input + cache read + cache write" , ( ) => {
1342+ const tokens = { input : 5000 , output : 2000 , reasoning : 500 , cache : { read : 3000 , write : 1000 } }
1343+ expect ( MessageV2 . promptSize ( tokens ) ) . toBe ( 5000 + 3000 + 1000 )
1344+ } )
1345+
1346+ test ( "promptSize works with zero cache" , ( ) => {
1347+ const tokens = { input : 5000 , output : 2000 , reasoning : 0 , cache : { read : 0 , write : 0 } }
1348+ expect ( MessageV2 . promptSize ( tokens ) ) . toBe ( 5000 )
1349+ } )
1350+
1351+ test ( "totalSize returns sum of all token fields" , ( ) => {
1352+ const tokens = { input : 5000 , output : 2000 , reasoning : 500 , cache : { read : 3000 , write : 1000 } }
1353+ expect ( MessageV2 . totalSize ( tokens ) ) . toBe ( 5000 + 2000 + 500 + 3000 + 1000 )
1354+ } )
1355+ } )
1356+
1357+ describe ( "isOverflow" , ( ) => {
1358+ function makeConfig ( opts ?: { auto ?: boolean ; reserved ?: number } ) : Config . Info {
1359+ return {
1360+ compaction : {
1361+ auto : opts ?. auto ?? true ,
1362+ reserved : opts ?. reserved ,
1363+ } ,
1364+ } as Config . Info
1365+ }
1366+
1367+ test ( "returns false when auto compaction is disabled" , ( ) => {
1368+ const model = createModel ( { context : 100_000 , output : 32_000 } )
1369+ const tokens = { input : 90_000 , output : 500 , reasoning : 0 , cache : { read : 0 , write : 0 } }
1370+ expect ( isOverflow ( { cfg : makeConfig ( { auto : false } ) , tokens, model } ) ) . toBe ( false )
1371+ } )
1372+
1373+ test ( "returns false when context limit is 0" , ( ) => {
1374+ const model = {
1375+ ...createModel ( { context : 100_000 , output : 32_000 } ) ,
1376+ limit : { ...createModel ( { context : 100_000 , output : 32_000 } ) . limit , context : 0 } ,
1377+ }
1378+ const tokens = { input : 90_000 , output : 500 , reasoning : 0 , cache : { read : 0 , write : 0 } }
1379+ expect ( isOverflow ( { cfg : makeConfig ( ) , tokens, model } ) ) . toBe ( false )
1380+ } )
1381+
1382+ test ( "uses total size (all token fields) for overflow check" , ( ) => {
1383+ const model = createModel ( { context : 100_000 , output : 32_000 } )
1384+ // Total size = 50_000 + 30_000 + 5_000 + 10_000 + 5_000 = 100_000
1385+ // With context=100k and output=32k, usable = 100k - 20k = 80k
1386+ // 100k >= 80k => overflowing
1387+ const tokens = { input : 50_000 , output : 30_000 , reasoning : 5_000 , cache : { read : 10_000 , write : 5_000 } }
1388+ expect ( isOverflow ( { cfg : makeConfig ( ) , tokens, model } ) ) . toBe ( true )
1389+ } )
1390+
1391+ test ( "does not overflow when total size fits in usable context" , ( ) => {
1392+ const model = createModel ( { context : 100_000 , output : 32_000 } )
1393+ // Total size = 40_000 + 10_000 + 5_000 + 5_000 + 0 = 60_000
1394+ // usable = 100k - 20k = 80k
1395+ // 60k < 80k => not overflowing
1396+ const tokens = { input : 40_000 , output : 10_000 , reasoning : 5_000 , cache : { read : 5_000 , write : 0 } }
1397+ expect ( isOverflow ( { cfg : makeConfig ( ) , tokens, model } ) ) . toBe ( false )
1398+ } )
1399+
1400+ test ( "overflows when total size exceeds usable context" , ( ) => {
1401+ const model = createModel ( { context : 100_000 , output : 32_000 } )
1402+ // Total size = 70_000 + 1_000 + 0 + 10_000 + 5_000 = 86_000
1403+ // usable = 100k - 20k = 80k
1404+ // 86k >= 80k => overflowing
1405+ const tokens = { input : 70_000 , output : 1_000 , reasoning : 0 , cache : { read : 10_000 , write : 5_000 } }
1406+ expect ( isOverflow ( { cfg : makeConfig ( ) , tokens, model } ) ) . toBe ( true )
1407+ } )
1408+
1409+ test ( "respects custom reserved value" , ( ) => {
1410+ const model = createModel ( { context : 100_000 , output : 32_000 } )
1411+ // Total size = 70_000 + 1_000 + 0 + 10_000 + 5_000 = 86_000
1412+ // reserved=50k, usable=100k-50k=50k
1413+ // 86k >= 50k => overflow
1414+ const tokens = { input : 70_000 , output : 1_000 , reasoning : 0 , cache : { read : 10_000 , write : 5_000 } }
1415+ expect ( isOverflow ( { cfg : makeConfig ( { reserved : 50_000 } ) , tokens, model } ) ) . toBe ( true )
1416+ } )
1417+ } )
1418+
1419+ describe ( "token accumulation across steps" , ( ) => {
1420+ test ( "accumulates output, reasoning, and cache.write across steps; replaces input and cache.read" , ( ) => {
1421+ // Step 1: initial request
1422+ const step1Tokens = Session . getUsage ( {
1423+ model : createModel ( { context : 100_000 , output : 32_000 } ) ,
1424+ usage : { inputTokens : 10000 , outputTokens : 500 , totalTokens : 10500 } ,
1425+ } ) . tokens
1426+ expect ( step1Tokens . input ) . toBe ( 10000 )
1427+ expect ( step1Tokens . output ) . toBe ( 500 )
1428+ expect ( step1Tokens . reasoning ) . toBe ( 0 )
1429+ expect ( step1Tokens . cache . read ) . toBe ( 0 )
1430+ expect ( step1Tokens . cache . write ) . toBe ( 0 )
1431+
1432+ // After step 1: assistantMessage.tokens = step1Tokens (no prev)
1433+ let accumulated = { ...step1Tokens , cache : { ...step1Tokens . cache } }
1434+
1435+ // Step 2: tool result, conversation has grown
1436+ const step2Tokens = Session . getUsage ( {
1437+ model : createModel ( { context : 100_000 , output : 32_000 } ) ,
1438+ usage : {
1439+ inputTokens : 15000 ,
1440+ outputTokens : 800 ,
1441+ reasoningTokens : 200 ,
1442+ cachedInputTokens : 5000 ,
1443+ totalTokens : 16000 ,
1444+ } ,
1445+ } ) . tokens
1446+ expect ( step2Tokens . input ) . toBe ( 10000 ) // 15000 - 5000 (cached)
1447+ expect ( step2Tokens . output ) . toBe ( 600 ) // 800 - 200 (reasoning)
1448+ expect ( step2Tokens . reasoning ) . toBe ( 200 )
1449+ expect ( step2Tokens . cache . read ) . toBe ( 5000 )
1450+ expect ( step2Tokens . cache . write ) . toBe ( 0 )
1451+
1452+ // Simulate what processor.ts does: accumulate additive fields, replace snapshot fields
1453+ accumulated = {
1454+ input : step2Tokens . input ,
1455+ output : ( accumulated . output ?? 0 ) + step2Tokens . output ,
1456+ reasoning : ( accumulated . reasoning ?? 0 ) + step2Tokens . reasoning ,
1457+ cache : {
1458+ read : step2Tokens . cache . read ,
1459+ write : ( accumulated . cache ?. write ?? 0 ) + step2Tokens . cache . write ,
1460+ } ,
1461+ }
1462+
1463+ // After two steps:
1464+ expect ( accumulated . input ) . toBe ( 10000 ) // replaced: last step's input
1465+ expect ( accumulated . output ) . toBe ( 500 + 600 ) // accumulated: 1100
1466+ expect ( accumulated . reasoning ) . toBe ( 0 + 200 ) // accumulated: 200
1467+ expect ( accumulated . cache . read ) . toBe ( 5000 ) // replaced: last step's cache read
1468+ expect ( accumulated . cache . write ) . toBe ( 0 + 0 ) // accumulated: 0
1469+
1470+ // Step 3: more tool calls, cache hits
1471+ const step3Tokens = Session . getUsage ( {
1472+ model : createModel ( { context : 100_000 , output : 32_000 } ) ,
1473+ usage : {
1474+ inputTokens : 20000 ,
1475+ outputTokens : 1200 ,
1476+ reasoningTokens : 300 ,
1477+ cachedInputTokens : 12000 ,
1478+ totalTokens : 21200 ,
1479+ } ,
1480+ metadata : { anthropic : { cacheCreationInputTokens : 500 } } ,
1481+ } ) . tokens
1482+ expect ( step3Tokens . input ) . toBe ( 7500 ) // 20000 - 12000 - 500
1483+ expect ( step3Tokens . output ) . toBe ( 900 ) // 1200 - 300
1484+ expect ( step3Tokens . reasoning ) . toBe ( 300 )
1485+ expect ( step3Tokens . cache . read ) . toBe ( 12000 )
1486+ expect ( step3Tokens . cache . write ) . toBe ( 500 )
1487+
1488+ accumulated = {
1489+ input : step3Tokens . input ,
1490+ output : accumulated . output + step3Tokens . output ,
1491+ reasoning : accumulated . reasoning + step3Tokens . reasoning ,
1492+ cache : {
1493+ read : step3Tokens . cache . read ,
1494+ write : accumulated . cache . write + step3Tokens . cache . write ,
1495+ } ,
1496+ }
1497+
1498+ // After three steps:
1499+ expect ( accumulated . input ) . toBe ( 7500 ) // replaced: latest step
1500+ expect ( accumulated . output ) . toBe ( 500 + 600 + 900 ) // accumulated: 2000
1501+ expect ( accumulated . reasoning ) . toBe ( 0 + 200 + 300 ) // accumulated: 500
1502+ expect ( accumulated . cache . read ) . toBe ( 12000 ) // replaced: latest step
1503+ expect ( accumulated . cache . write ) . toBe ( 0 + 0 + 500 ) // accumulated: 500
1504+
1505+ // promptSize = input tokens sent to the LLM (current prompt footprint)
1506+ const promptSize = MessageV2 . promptSize ( accumulated )
1507+ expect ( promptSize ) . toBe ( 7500 + 12000 + 500 ) // 20_000
1508+
1509+ // totalSize = full conversation size (used for context overflow and display %)
1510+ // includes output because the next turn's prompt will include this response
1511+ const totalSize = MessageV2 . totalSize ( accumulated )
1512+ expect ( totalSize ) . toBe ( 7500 + 2000 + 500 + 12000 + 500 ) // 22_500
1513+ } )
13241514} )
0 commit comments