@@ -1062,6 +1062,28 @@ export const mainPrompt = async (
10621062 } )
10631063 }
10641064
1065+ if (
1066+ clientToolCalls . every ( ( tool ) => tool . name !== 'end_turn' ) &&
1067+ ( agentState . consecutiveAssistantMessages ?? 0 ) > 2
1068+ ) {
1069+ const isLoop = await checkForUnproductiveLoop ( messagesWithResponse , {
1070+ clientSessionId,
1071+ fingerprintId,
1072+ userInputId : promptId ,
1073+ userId,
1074+ } )
1075+ if ( isLoop ) {
1076+ logger . warn ( 'Detected unproductive loop, ending turn.' )
1077+ onResponseChunk ( '\n\nHow would you like to proceed from here?\n\n' )
1078+ fullResponse += getToolCallString ( 'end_turn' , { } )
1079+ clientToolCalls . push ( {
1080+ name : 'end_turn' ,
1081+ parameters : { } ,
1082+ id : generateCompactId ( ) ,
1083+ } )
1084+ }
1085+ }
1086+
10651087 if ( fileChanges . length === 0 && fileProcessingPromises . length > 0 ) {
10661088 onResponseChunk ( 'No changes to existing files.\n' )
10671089 }
@@ -1089,28 +1111,6 @@ export const mainPrompt = async (
10891111
10901112 const newAgentContext = await agentContextPromise
10911113
1092- if (
1093- clientToolCalls . every ( ( tool ) => tool . name !== 'end_turn' ) &&
1094- ( agentState . consecutiveAssistantMessages ?? 0 ) > 2
1095- ) {
1096- const isLoop = await checkForUnproductiveLoop ( messagesWithResponse , {
1097- clientSessionId,
1098- fingerprintId,
1099- userInputId : promptId ,
1100- userId,
1101- } )
1102- if ( isLoop ) {
1103- logger . warn ( 'Detected unproductive loop, ending turn.' )
1104- onResponseChunk ( '\n\nHow would you like to proceed from here?\n\n' )
1105- fullResponse += getToolCallString ( 'end_turn' , { } )
1106- clientToolCalls . push ( {
1107- name : 'end_turn' ,
1108- parameters : { } ,
1109- id : generateCompactId ( ) ,
1110- } )
1111- }
1112- }
1113-
11141114 let finalMessageHistory = expireMessages ( messagesWithResponse , 'agentStep' )
11151115
11161116 // Handle /compact command: replace message history with the summary
0 commit comments