fix: prevent duplicate function_call items in session history after r… #702
+66
−13
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix: Duplicate function_call items in session history after resuming from interruption
Fixes #701
Problem
When using
OpenAIConversationsSessionwith human-in-the-loop (HITL) tool approval,function_callitems were being duplicated in conversation history. This occurred because_currentTurnPersistedItemCountwas incorrectly reset to 0 afterresolveInterruptedTurn, undoing the rewind logic that had already adjusted the counter.Root Cause
After
resolveInterruptedTurnreturnednext_step_run_again, the code was resetting the counter to 0 in multiple places (non-streaming and streaming paths), which causedsaveToSessionto save all items again, including the already-persistedfunction_callitem.Flow:
tool_call_itemsaved to sessionresolveInterruptedTurnrewinds counter (2 → 1) ✓saveToSessionwith counter=0 saves all items again → duplicate createdSolution
Removed counter resets after
resolveInterruptedTurn: Removed all instances where_currentTurnPersistedItemCountwas reset to 0 immediately afterresolveInterruptedTurnreturnsnext_step_run_again(lines 735, 870, 1025, 1210 inrun.ts).Preserve rewound counter value: When continuing from interruption with
next_step_run_again, only reset the counter if it's already 0 (indicating a new turn). If the counter is non-zero (was rewound), preserve the rewound value to prevent duplicates.Added handoff filtering: Added logic in
resolveInterruptedTurnto filter out already-executed handoffs bycallIdto prevent re-execution when resuming from interruption.Changes
packages/agents-core/src/run.ts:resolveInterruptedTurnin non-streaming path (lines 735, 870)resolveInterruptedTurnin streaming path (lines 1025, 1210)packages/agents-core/src/runImplementation.ts:resolveInterruptedTurnto prevent re-execution of already-executed handoffsTesting
Impact
function_callitems in session historyOpenAIConversationsSessionwith HITL