@@ -17,11 +17,13 @@ vi.mock('../utils/errorReporting.js', () => ({
1717interface MockClient {
1818 generateContent : ReturnType < typeof vi . fn > ;
1919 getHistory : ReturnType < typeof vi . fn > ;
20+ getHistoryTail ?: ReturnType < typeof vi . fn > ;
2021 isInitialized : ReturnType < typeof vi . fn > ;
2122}
2223
2324function makeMockClient ( opts : {
2425 history ?: Content [ ] ;
26+ historyTail ?: Content [ ] ;
2527 initialized ?: boolean ;
2628 reply ?: string ;
2729 throws ?: Error ;
@@ -30,6 +32,9 @@ function makeMockClient(opts: {
3032 return {
3133 isInitialized : vi . fn ( ) . mockReturnValue ( opts . initialized ?? true ) ,
3234 getHistory : vi . fn ( ) . mockReturnValue ( opts . history ?? [ ] ) ,
35+ getHistoryTail : vi
36+ . fn ( )
37+ . mockReturnValue ( opts . historyTail ?? opts . history ?? [ ] ) ,
3338 generateContent : opts . throws
3439 ? vi . fn ( ) . mockRejectedValue ( opts . throws )
3540 : vi . fn ( ) . mockResolvedValue ( {
@@ -227,6 +232,26 @@ describe('judgeGoal', () => {
227232 expect ( generationConfig . temperature ) . toBe ( 0 ) ;
228233 } ) ;
229234
235+ it ( 'uses a bounded history tail without cloning the full session when available' , async ( ) => {
236+ const tail : Content [ ] = [
237+ { role : 'user' , parts : [ { text : 'recent prompt' } ] } ,
238+ { role : 'model' , parts : [ { text : 'recent answer' } ] } ,
239+ ] ;
240+ const client = makeMockClient ( { history : [ ] , historyTail : tail } ) ;
241+ const config = makeConfig ( { client } ) ;
242+
243+ await judgeGoal ( config , {
244+ condition : 'finish' ,
245+ lastAssistantText : 'recent answer' ,
246+ signal : new AbortController ( ) . signal ,
247+ } ) ;
248+
249+ expect ( client . getHistoryTail ) . toHaveBeenCalledWith ( 24 ) ;
250+ expect ( client . getHistory ) . not . toHaveBeenCalled ( ) ;
251+ const [ contents ] = client . generateContent . mock . calls [ 0 ] ;
252+ expect ( contents . slice ( 0 , tail . length ) ) . toEqual ( tail ) ;
253+ } ) ;
254+
230255 it ( 'appends lastAssistantText as a model turn when history does not contain it' , async ( ) => {
231256 const history : Content [ ] = [
232257 { role : 'user' , parts : [ { text : 'go' } ] } ,
0 commit comments