@@ -5041,6 +5041,86 @@ command = "vim"
50415041 expect ( stripSgr ( renderTranscript ( driver ) ) ) . toContain ( 'LLM not set' ) ;
50425042 } ) ;
50435043
5044+ it ( 'recomputes context usage when a status update carries context tokens without it' , async ( ) => {
5045+ const { driver } = await makeDriver ( ) ;
5046+ driver . state . appState . contextTokens = 0 ;
5047+ driver . state . appState . maxContextTokens = 1_000_000 ;
5048+ driver . state . appState . contextUsage = 0.74 ;
5049+
5050+ driver . sessionEventHandler . handleEvent (
5051+ {
5052+ type : 'agent.status.updated' ,
5053+ agentId : 'main' ,
5054+ sessionId : 'ses-1' ,
5055+ contextTokens : 180_000 ,
5056+ } as Event ,
5057+ vi . fn ( ) ,
5058+ ) ;
5059+
5060+ expect ( driver . state . appState . contextTokens ) . toBe ( 180_000 ) ;
5061+ expect ( driver . state . appState . contextUsage ) . toBeCloseTo ( 0.18 ) ;
5062+ } ) ;
5063+
5064+ it ( 'recomputes context usage when a status update carries max context tokens without it' , async ( ) => {
5065+ const { driver } = await makeDriver ( ) ;
5066+ driver . state . appState . contextTokens = 180_000 ;
5067+ driver . state . appState . maxContextTokens = 256_000 ;
5068+ driver . state . appState . contextUsage = 180_000 / 256_000 ;
5069+
5070+ driver . sessionEventHandler . handleEvent (
5071+ {
5072+ type : 'agent.status.updated' ,
5073+ agentId : 'main' ,
5074+ sessionId : 'ses-1' ,
5075+ maxContextTokens : 1_000_000 ,
5076+ } as Event ,
5077+ vi . fn ( ) ,
5078+ ) ;
5079+
5080+ expect ( driver . state . appState . maxContextTokens ) . toBe ( 1_000_000 ) ;
5081+ expect ( driver . state . appState . contextUsage ) . toBeCloseTo ( 0.18 ) ;
5082+ } ) ;
5083+
5084+ it ( 'keeps an explicit context usage from status updates' , async ( ) => {
5085+ const { driver } = await makeDriver ( ) ;
5086+ driver . state . appState . contextTokens = 100 ;
5087+ driver . state . appState . maxContextTokens = 1_000_000 ;
5088+ driver . state . appState . contextUsage = 0 ;
5089+
5090+ driver . sessionEventHandler . handleEvent (
5091+ {
5092+ type : 'agent.status.updated' ,
5093+ agentId : 'main' ,
5094+ sessionId : 'ses-1' ,
5095+ contextTokens : 180_000 ,
5096+ maxContextTokens : 1_000_000 ,
5097+ contextUsage : 0.42 ,
5098+ } as Event ,
5099+ vi . fn ( ) ,
5100+ ) ;
5101+
5102+ expect ( driver . state . appState . contextUsage ) . toBe ( 0.42 ) ;
5103+ } ) ;
5104+
5105+ it ( 'zeroes context usage when no context window is known' , async ( ) => {
5106+ const { driver } = await makeDriver ( ) ;
5107+ driver . state . appState . contextTokens = 180_000 ;
5108+ driver . state . appState . maxContextTokens = 0 ;
5109+ driver . state . appState . contextUsage = 0.74 ;
5110+
5111+ driver . sessionEventHandler . handleEvent (
5112+ {
5113+ type : 'agent.status.updated' ,
5114+ agentId : 'main' ,
5115+ sessionId : 'ses-1' ,
5116+ contextTokens : 190_000 ,
5117+ } as Event ,
5118+ vi . fn ( ) ,
5119+ ) ;
5120+
5121+ expect ( driver . state . appState . contextUsage ) . toBe ( 0 ) ;
5122+ } ) ;
5123+
50445124 it ( 'applies the effective thinking effort from status updates' , async ( ) => {
50455125 const { driver } = await makeDriver ( ) ;
50465126
0 commit comments