@@ -112,4 +112,51 @@ describe("tool history normalization", () => {
112112
113113 expect ( normalized [ 1 ] ?. content [ 0 ] ) . toMatchObject ( { name : "lookup" , namespace : undefined } )
114114 } )
115+
116+ test ( "moves system updates after the results of pending calls" , ( ) => {
117+ const calls = Message . assistant ( [ toolCall ( "first" ) , toolCall ( "second" ) ] )
118+ const first = toolResult ( "first" , "one" , "first" , "text" )
119+ const second = toolResult ( "second" , "two" , "second" , "text" )
120+ const update = Message . system ( "First update." )
121+ const later = Message . system ( "Second update." )
122+ const user = Message . user ( "Continue." )
123+ const missing = ( id : string ) =>
124+ Message . tool ( ToolResultPart . make ( { id, name : id , result : "Tool result missing" , resultType : "error" } ) )
125+
126+ expect ( normalizeToolHistory ( [ calls , first , update , later , second , user ] ) ) . toEqual ( [
127+ calls ,
128+ first ,
129+ second ,
130+ update ,
131+ later ,
132+ user ,
133+ ] )
134+ expect ( normalizeToolHistory ( [ Message . assistant ( toolCall ( "first" ) ) , update , user ] ) ) . toEqual ( [
135+ Message . assistant ( toolCall ( "first" ) ) ,
136+ missing ( "first" ) ,
137+ update ,
138+ user ,
139+ ] )
140+ expect ( normalizeToolHistory ( [ Message . assistant ( toolCall ( "first" ) ) , update ] ) ) . toEqual ( [
141+ Message . assistant ( toolCall ( "first" ) ) ,
142+ missing ( "first" ) ,
143+ update ,
144+ ] )
145+ } )
146+
147+ test ( "moves effort updates after the results of pending calls" , ( ) => {
148+ const call = Message . assistant ( toolCall ( "first" ) )
149+ const result = toolResult ( "first" , "one" , "first" , "text" )
150+ const effort = Message . effort ( { effort : "low" , previous : "high" } )
151+
152+ expect ( normalizeToolHistory ( [ call , effort , result ] ) ) . toEqual ( [ call , result , effort ] )
153+ } )
154+
155+ test ( "keeps system updates in place when no call is pending" , ( ) => {
156+ const history = [ Message . assistant ( toolCall ( "first" ) ) , toolResult ( "first" , "one" , "first" , "text" ) ]
157+ const update = Message . system ( "Update." )
158+ const input = [ Message . user ( "Start." ) , update , ...history , update , Message . user ( "Continue." ) ]
159+
160+ expect ( normalizeToolHistory ( input ) ) . toBe ( input )
161+ } )
115162} )
0 commit comments