1- import { beforeAll , beforeEach , describe , expect , it } from "vitest" ;
1+ import { beforeAll , beforeEach , describe , expect , it , vi } from "vitest" ;
22import {
33 makeAttemptResult ,
44 makeCompactionSuccess ,
@@ -10,9 +10,11 @@ import {
1010 loadRunOverflowCompactionHarness ,
1111 mockedContextEngine ,
1212 mockedCompactDirect ,
13+ mockedEvaluateContextWindowGuard ,
1314 mockedIsCompactionFailureError ,
1415 mockedIsLikelyContextOverflowError ,
1516 mockedLog ,
17+ mockedResolveContextWindowInfo ,
1618 mockedRunEmbeddedAttempt ,
1719 mockedSessionLikelyHasOversizedToolResults ,
1820 mockedTruncateOversizedToolResultsInSession ,
@@ -172,6 +174,86 @@ describe("overflow compaction in run loop", () => {
172174 expect ( result . meta . error ) . toBeUndefined ( ) ;
173175 } ) ;
174176
177+ it ( "falls back to tool-result truncation and retries when real aggregate tool-result detection trips" , async ( ) => {
178+ const { sessionLikelyHasOversizedToolResults } = await vi . importActual <
179+ typeof import ( "./tool-result-truncation.js" )
180+ > ( "./tool-result-truncation.js" ) ;
181+ mockedResolveContextWindowInfo . mockReturnValue ( {
182+ tokens : 10_000 ,
183+ source : "model" ,
184+ } ) ;
185+ mockedEvaluateContextWindowGuard . mockReturnValue ( {
186+ shouldWarn : false ,
187+ shouldBlock : false ,
188+ tokens : 10_000 ,
189+ source : "model" ,
190+ } ) ;
191+
192+ mockedRunEmbeddedAttempt
193+ . mockResolvedValueOnce (
194+ makeAttemptResult ( {
195+ promptError : makeOverflowError ( ) ,
196+ messagesSnapshot : [
197+ {
198+ role : "user" ,
199+ content : "u" . repeat ( 20_000 ) ,
200+ } as EmbeddedRunAttemptResult [ "messagesSnapshot" ] [ number ] ,
201+ {
202+ role : "toolResult" ,
203+ toolCallId : "call_a" ,
204+ toolName : "read" ,
205+ content : [ { type : "text" , text : "a" . repeat ( 10_000 ) } ] ,
206+ isError : false ,
207+ } as EmbeddedRunAttemptResult [ "messagesSnapshot" ] [ number ] ,
208+ {
209+ role : "toolResult" ,
210+ toolCallId : "call_b" ,
211+ toolName : "read" ,
212+ content : [ { type : "text" , text : "b" . repeat ( 10_000 ) } ] ,
213+ isError : false ,
214+ } as EmbeddedRunAttemptResult [ "messagesSnapshot" ] [ number ] ,
215+ {
216+ role : "toolResult" ,
217+ toolCallId : "call_c" ,
218+ toolName : "read" ,
219+ content : [ { type : "text" , text : "c" . repeat ( 10_000 ) } ] ,
220+ isError : false ,
221+ } as EmbeddedRunAttemptResult [ "messagesSnapshot" ] [ number ] ,
222+ ] ,
223+ } ) ,
224+ )
225+ . mockResolvedValueOnce ( makeAttemptResult ( { promptError : null } ) ) ;
226+
227+ mockedCompactDirect . mockResolvedValueOnce ( {
228+ ok : false ,
229+ compacted : false ,
230+ reason : "nothing to compact" ,
231+ } ) ;
232+ mockedSessionLikelyHasOversizedToolResults . mockImplementation (
233+ ( ( params : Parameters < typeof sessionLikelyHasOversizedToolResults > [ 0 ] ) =>
234+ sessionLikelyHasOversizedToolResults ( params ) ) as never ,
235+ ) ;
236+ mockedTruncateOversizedToolResultsInSession . mockResolvedValueOnce ( {
237+ truncated : true ,
238+ truncatedCount : 2 ,
239+ } ) ;
240+
241+ const result = await runEmbeddedPiAgent ( baseParams ) ;
242+
243+ expect ( mockedCompactDirect ) . toHaveBeenCalledTimes ( 1 ) ;
244+ expect ( mockedSessionLikelyHasOversizedToolResults ) . toHaveBeenCalledWith (
245+ expect . objectContaining ( { contextWindowTokens : 10_000 } ) ,
246+ ) ;
247+ expect ( mockedTruncateOversizedToolResultsInSession ) . toHaveBeenCalledWith (
248+ expect . objectContaining ( { sessionFile : "/tmp/session.json" } ) ,
249+ ) ;
250+ expect ( mockedRunEmbeddedAttempt ) . toHaveBeenCalledTimes ( 2 ) ;
251+ expect ( mockedLog . info ) . toHaveBeenCalledWith (
252+ expect . stringContaining ( "Truncated 2 tool result(s)" ) ,
253+ ) ;
254+ expect ( result . meta . error ) . toBeUndefined ( ) ;
255+ } ) ;
256+
175257 it ( "retries compaction up to 3 times before giving up" , async ( ) => {
176258 const overflowError = makeOverflowError ( ) ;
177259
0 commit comments