@@ -2103,6 +2103,82 @@ describe("agentLoop tool termination", () => {
21032103 expect ( events . slice ( agentEnd + 1 ) ) . toEqual ( [ ] ) ;
21042104 } ) ;
21052105
2106+ it . each ( [ "parallel" , "sequential" ] as const ) (
2107+ "delivers loop warnings after raw outcome hooks in %s batches" ,
2108+ async ( toolExecution ) => {
2109+ const executed : string [ ] = [ ] ;
2110+ const requestMessages : Message [ ] [ ] = [ ] ;
2111+ const rawOutcomes : AgentToolResult < unknown > [ ] = [ ] ;
2112+ const streamFn = createTurnSequenceStream (
2113+ [
2114+ [
2115+ { type : "toolCall" , id : "warned" , name : "read" , arguments : { } } ,
2116+ { type : "toolCall" , id : "sibling" , name : "list" , arguments : { } } ,
2117+ ] ,
2118+ [ { type : "toolCall" , id : "next" , name : "read" , arguments : { } } ] ,
2119+ [ { type : "text" , text : "done" } ] ,
2120+ ] ,
2121+ requestMessages ,
2122+ ) ;
2123+ const events = await collectEvents (
2124+ agentLoop (
2125+ [ { role : "user" , content : "run" , timestamp : 1 } ] ,
2126+ {
2127+ systemPrompt : "" ,
2128+ messages : [ ] ,
2129+ tools : [ makeTool ( "read" , executed ) , makeTool ( "list" , executed ) ] ,
2130+ } ,
2131+ {
2132+ ...config ,
2133+ toolExecution,
2134+ beforeToolBatch : async ( { calls } ) => ( {
2135+ warnings : calls
2136+ . filter ( ( { toolCall } ) => toolCall . id === "warned" )
2137+ . map ( ( { toolCall } ) => ( {
2138+ kind : "tool-loop-warning" as const ,
2139+ toolCallId : toolCall . id ,
2140+ count : 10 ,
2141+ } ) ) ,
2142+ } ) ,
2143+ afterToolCall : async ( { result } ) => {
2144+ rawOutcomes . push ( result ) ;
2145+ } ,
2146+ afterToolOutcome : async ( { result } ) => {
2147+ rawOutcomes . push ( result ) ;
2148+ return { content : [ ...result . content , { type : "text" , text : "outcome hook" } ] } ;
2149+ } ,
2150+ } ,
2151+ undefined ,
2152+ streamFn ,
2153+ ) ,
2154+ ) ;
2155+ expect ( rawOutcomes . every ( ( result ) => result . content . length === 1 ) ) . toBe ( true ) ;
2156+ const results = requestMessages . at ( - 1 ) ?. filter ( ( message ) => message . role === "toolResult" ) ;
2157+ expect ( results ?. map ( ( message ) => message . content ) ) . toEqual ( [
2158+ [
2159+ { type : "text" , text : "read result" } ,
2160+ { type : "text" , text : "outcome hook" } ,
2161+ {
2162+ type : "text" ,
2163+ text : "[System note: Tool-loop warning after 10 repeated calls. Change your approach or stop if you are not making progress.]" ,
2164+ } ,
2165+ ] ,
2166+ [
2167+ { type : "text" , text : "list result" } ,
2168+ { type : "text" , text : "outcome hook" } ,
2169+ ] ,
2170+ [
2171+ { type : "text" , text : "read result" } ,
2172+ { type : "text" , text : "outcome hook" } ,
2173+ ] ,
2174+ ] ) ;
2175+ expect (
2176+ events . filter ( ( event ) => event . type === "tool_execution_end" ) . map ( ( event ) => event . result ) ,
2177+ ) . toEqual ( results ?. map ( ( message ) => expect . objectContaining ( { content : message . content } ) ) ) ;
2178+ expect ( executed ) . toEqual ( [ "read" , "list" , "read" ] ) ;
2179+ } ,
2180+ ) ;
2181+
21062182 it ( "gives the model one recovery turn with the normal tool catalog" , async ( ) => {
21072183 const executed : string [ ] = [ ] ;
21082184 const providerToolNames : string [ ] [ ] = [ ] ;
@@ -2622,6 +2698,9 @@ describe("agentLoop tool termination", () => {
26222698 { systemPrompt : "" , messages : [ ] , tools : [ tool ] } ,
26232699 {
26242700 ...config ,
2701+ beforeToolBatch : async ( ) => ( {
2702+ warnings : [ { kind : "tool-loop-warning" , toolCallId : "commit-probe" , count : 10 } ] ,
2703+ } ) ,
26252704 afterToolCall : async ( ) => ( { details : { phase : "after-call" } } ) ,
26262705 afterToolOutcome : async ( ) => ( { details : { phase : "after-outcome" } } ) ,
26272706 } ,
@@ -2945,7 +3024,7 @@ describe("agentLoop tool termination", () => {
29453024 expect ( events . at ( - 1 ) ) . toMatchObject ( { type : "agent_end" } ) ;
29463025 } ) ;
29473026
2948- it ( "normalizes a tool result with missing content before the next model turn " , async ( ) => {
3027+ it . each ( [ false , true ] ) ( "normalizes missing tool content with loop warning=%s " , async ( warn ) => {
29493028 const contexts : Context [ ] = [ ] ;
29503029 const streamFn = createTurnSequenceStream (
29513030 [
@@ -2969,7 +3048,14 @@ describe("agentLoop tool termination", () => {
29693048 agentLoop (
29703049 [ { role : "user" , content : "run" , timestamp : 1 } ] ,
29713050 { systemPrompt : "" , messages : [ ] , tools : [ tool ] } ,
2972- config ,
3051+ {
3052+ ...config ,
3053+ beforeToolBatch : async ( ) => ( {
3054+ warnings : warn
3055+ ? [ { kind : "tool-loop-warning" , toolCallId : "call-empty" , count : 10 } ]
3056+ : [ ] ,
3057+ } ) ,
3058+ } ,
29733059 undefined ,
29743060 streamFn ,
29753061 ) ,
@@ -2980,7 +3066,9 @@ describe("agentLoop tool termination", () => {
29803066 expect . objectContaining ( {
29813067 role : "toolResult" ,
29823068 toolName : "empty" ,
2983- content : [ ] ,
3069+ content : warn
3070+ ? [ { type : "text" , text : expect . stringContaining ( "Tool-loop warning after 10" ) } ]
3071+ : [ ] ,
29843072 } ) ,
29853073 ) ;
29863074 } ) ;
0 commit comments