@@ -732,36 +732,24 @@ export class Runner extends RunHooks<any, AgentOutputType<unknown>> {
732732
733733 state . _originalInput = turnResult . originalInput ;
734734 state . _generatedItems = turnResult . generatedItems ;
735- // Don't reset counter here - it's already been adjusted by resolveInterruptedTurn's rewind logic
736- // Counter will be reset when _currentTurn is incremented (starting a new turn)
735+ if ( turnResult . nextStep . type === 'next_step_run_again' ) {
736+ state . _currentTurnPersistedItemCount = 0 ;
737+ }
737738 state . _currentStep = turnResult . nextStep ;
738739
739740 if ( turnResult . nextStep . type === 'next_step_interruption' ) {
740741 // we are still in an interruption, so we need to avoid an infinite loop
741742 return new RunResult < TContext , TAgent > ( state ) ;
742743 }
743744
744- // If continuing from interruption with next_step_run_again, continue the loop
745- // but DON'T increment turn or reset counter - we're continuing the same turn
746- if ( turnResult . nextStep . type === 'next_step_run_again' ) {
747- continue ;
748- }
749-
750745 continue ;
751746 }
752747
753748 if ( state . _currentStep . type === 'next_step_run_again' ) {
754749 const artifacts = await prepareAgentArtifacts ( state ) ;
755750
756- // Only increment turn and reset counter when starting a NEW turn
757- // If counter is non-zero, it means we're continuing from an interruption (counter was rewound)
758- // In that case, don't reset the counter - it's already been adjusted by the rewind logic
759751 state . _currentTurn ++ ;
760- if ( state . _currentTurnPersistedItemCount === 0 ) {
761- // Only reset if counter is already 0 (starting a new turn)
762- // If counter is non-zero, we're continuing from interruption and it was already adjusted
763- state . _currentTurnPersistedItemCount = 0 ;
764- }
752+ state . _currentTurnPersistedItemCount = 0 ;
765753
766754 if ( state . _currentTurn > state . _maxTurns ) {
767755 state . _currentAgentSpan ?. setError ( {
@@ -879,8 +867,9 @@ export class Runner extends RunHooks<any, AgentOutputType<unknown>> {
879867
880868 state . _originalInput = turnResult . originalInput ;
881869 state . _generatedItems = turnResult . generatedItems ;
882- // Don't reset counter here - it's already been adjusted by resolveInterruptedTurn's rewind logic
883- // Counter will be reset when _currentTurn is incremented (starting a new turn)
870+ if ( turnResult . nextStep . type === 'next_step_run_again' ) {
871+ state . _currentTurnPersistedItemCount = 0 ;
872+ }
884873 state . _currentStep = turnResult . nextStep ;
885874
886875 if ( parallelGuardrailPromise ) {
@@ -1032,8 +1021,9 @@ export class Runner extends RunHooks<any, AgentOutputType<unknown>> {
10321021
10331022 result . state . _originalInput = turnResult . originalInput ;
10341023 result . state . _generatedItems = turnResult . generatedItems ;
1035- // Don't reset counter here - it's already been adjusted by resolveInterruptedTurn's rewind logic
1036- // Counter will be reset when _currentTurn is incremented (starting a new turn)
1024+ if ( turnResult . nextStep . type === 'next_step_run_again' ) {
1025+ result . state . _currentTurnPersistedItemCount = 0 ;
1026+ }
10371027 result . state . _currentStep = turnResult . nextStep ;
10381028 if ( turnResult . nextStep . type === 'next_step_interruption' ) {
10391029 // we are still in an interruption, so we need to avoid an infinite loop
@@ -1045,15 +1035,8 @@ export class Runner extends RunHooks<any, AgentOutputType<unknown>> {
10451035 if ( result . state . _currentStep . type === 'next_step_run_again' ) {
10461036 const artifacts = await prepareAgentArtifacts ( result . state ) ;
10471037
1048- // Only increment turn and reset counter when starting a NEW turn
1049- // If counter is non-zero, it means we're continuing from an interruption (counter was rewound)
1050- // In that case, don't reset the counter - it's already been adjusted by the rewind logic
10511038 result . state . _currentTurn ++ ;
1052- if ( result . state . _currentTurnPersistedItemCount === 0 ) {
1053- // Only reset if counter is already 0 (starting a new turn)
1054- // If counter is non-zero, we're continuing from interruption and it was already adjusted
1055- result . state . _currentTurnPersistedItemCount = 0 ;
1056- }
1039+ result . state . _currentTurnPersistedItemCount = 0 ;
10571040
10581041 if ( result . state . _currentTurn > result . state . _maxTurns ) {
10591042 result . state . _currentAgentSpan ?. setError ( {
@@ -1225,15 +1208,10 @@ export class Runner extends RunHooks<any, AgentOutputType<unknown>> {
12251208
12261209 result . state . _originalInput = turnResult . originalInput ;
12271210 result . state . _generatedItems = turnResult . generatedItems ;
1228- // Don't reset counter here - it's already been adjusted by resolveInterruptedTurn's rewind logic
1229- // Counter will be reset when _currentTurn is incremented (starting a new turn)
1230- result . state . _currentStep = turnResult . nextStep ;
1231-
1232- // If continuing from interruption with next_step_run_again, don't increment turn or reset counter
1233- // We're continuing the same turn, not starting a new one
12341211 if ( turnResult . nextStep . type === 'next_step_run_again' ) {
1235- continue ;
1212+ result . state . _currentTurnPersistedItemCount = 0 ;
12361213 }
1214+ result . state . _currentStep = turnResult . nextStep ;
12371215 }
12381216
12391217 if ( result . state . _currentStep . type === 'next_step_final_output' ) {
0 commit comments