Agent V2 evaluation traces drop output retry attempts
Description
Agent V2 creates a new evaluation trace collector for every structured-output retry. When an earlier backend run succeeds but its declared output fails validation, the collector is discarded before the next attempt. The final evaluation trace therefore contains only the last successful correction and loses the failed attempt's messages, tool activity, output, and validation context.
Steps to reproduce
- Configure an Agent V2 declared output with type validation and one retry.
- Run an evaluation where the first backend output has the wrong type and the second output is valid.
- Inspect
evaluation_tracein the final node metadata.
Current behavior
Only the final attempt is present in the captured trajectory and tool calls.
Expected behavior
The evaluation trace should preserve each correction attempt in order, including the rejected output and failure reason, while keeping replayed event detection scoped to each backend run.
Proposed change
Reuse one trace collector across the node's output retries, append rejected outputs as failed final_output steps, and reset run-scoped event/tool indexes at each attempt boundary. The existing maximum of 10 retries naturally limits the retained execution attempts to 11.
The collector lifecycle is currently bound inside the retry iteration loop rather than the node's execution context. When schema validation fails on a backend output, the collector instance is torn down, dropping the previous attempt's tool calls and token counts from node_metadata["evaluation_trace"].
Moving collector initialization outside the retry loop addresses this directly. The main detail to handle is isolating run-level replay indices: each retry needs to append an explicit failed attempt step (capturing raw payload + validation error) while resetting per-run tool call counters so event sequencing doesn't bleed across attempt boundaries.