fix(session): anchor save boundary to prompt prefix size (#4006)

build_messages merges the current message into a same-role history tail,
shrinking the prompt prefix to 1 + history_count. The save boundary
assumed a standalone current message and skipped one message too many,
cutting the first new-turn assistant message (with its tool_calls) from
persistence while keeping its tool results - producing orphaned tool
results in session history.
This commit is contained in:
tangtaizhong666 2026-06-12 09:08:48 +08:00 committed by Xubin Ren
parent df832a37e9
commit ac5e84d453

View File

@ -182,7 +182,15 @@ def _save_skip_for_turn(
"""Return the persisted-message append boundary for this turn."""
if internal_continuation_inbound(message_metadata):
return initial_message_count
return 1 + history_count + (1 if user_persisted_early else 0)
# ``build_messages`` merges the current message into the last history
# entry when both share a role, so the prompt prefix is not always
# ``2 + history_count``. Runner-appended messages always start at
# ``initial_message_count``; only step back when the current message
# exists as a standalone entry that was not persisted early.
has_standalone_current = initial_message_count > 1 + history_count
if has_standalone_current and not user_persisted_early:
return initial_message_count - 1
return initial_message_count
def _goal_continuation_available(