@@ -171,7 +171,7 @@ async def _execute_with_semaphore(idx):
171171 if trajectory .terminated :
172172 if trajectory .termination_reason == TerminationReason .ERROR :
173173 evaluation_rows [idx ].rollout_status .status = "error"
174- evaluation_rows [idx ].rollout_status .reason = trajectory .control_plane_summary .get (
174+ evaluation_rows [idx ].rollout_status .reason = trajectory .control_plane_summary .get (
175175 "error_message" , None
176176 )
177177 else :
@@ -362,6 +362,12 @@ async def _execute_rollout(
362362 if recording_mode :
363363 policy .log_conversation_state_for_playback (rollout_idx , step - 1 , conversation_history )
364364
365+ if env_end :
366+ # if the env marks the end of the rollout, break the tool call loop
367+ # but set the termination reason later after the final policy call
368+ trajectory .terminated = True
369+ break
370+
365371 if step >= steps :
366372 trajectory .terminated = True
367373 trajectory .termination_reason = TerminationReason .MAX_STEPS
@@ -395,9 +401,16 @@ async def _execute_rollout(
395401 if recording_mode :
396402 policy .log_conversation_state_for_playback (rollout_idx , step - 1 , conversation_history )
397403
398- # update control plane summary if the env marks end
404+ # if the env marks end, update control plane summary and do one last policy call, then break the agent loop
405+ # this is to ensure each turn ends with an assistant message, which will align with the actual agentic llm behavior
399406 if env_end :
400- # Add final control plane summary
407+ _ , usage_stats , finish_reason = await policy (tool_schema , rollout_idx , conversation_history )
408+ if usage_stats :
409+ trajectory .usage ["prompt_tokens" ] += usage_stats .prompt_tokens
410+ trajectory .usage ["completion_tokens" ] += usage_stats .completion_tokens
411+ trajectory .usage ["total_tokens" ] += usage_stats .total_tokens
412+ trajectory .terminated = True
413+ trajectory .termination_reason = TerminationReason .from_str (finish_reason )
401414 trajectory .control_plane_summary .update (
402415 {
403416 "total_reward" : trajectory .total_reward ,
0 commit comments