fix(models): thread incoming_provenance + protect multi-tool blocks

Rebase threading fix and a backward scan so a chronological insert
cannot split a multi-result tool block.
This commit is contained in:
nesquena-hermes committed 2026-09-22 17:23:39 +00:00
1 parent 7a30ea5f39
commit 44f8d3ed37
3 files changed
+138 -11

No files matched your search

+18 -10
View File
@@ -10382,16 +10382,22 @@ def _insert_state_message_chronologically(messages: list, msg: dict) -> bool:
# preceding assistant's tool_calls, not just the first — a multi-tool
# turn has several adjacent tool results, and inserting between any of
# them splits the block (assistant, tool, <insert>, tool).
if (
idx < len(messages)
and messages[idx].get("role") == "tool"
and idx > 0
and messages[idx - 1].get("role") == "assistant"
and messages[idx - 1].get("tool_calls")
):
while idx < len(messages) and messages[idx].get("role") == "tool":
idx += 1
advanced = True
if idx < len(messages) and messages[idx].get("role") == "tool":
# Walk back over any contiguous tool rows already emitted for
# this block, so the owning assistant is found even when idx
# lands on the SECOND result of a multi-tool turn (where
# messages[idx - 1] is another tool row, not the assistant).
owner = idx - 1
while owner >= 0 and messages[owner].get("role") == "tool":
owner -= 1
if (
owner >= 0
and messages[owner].get("role") == "assistant"
and messages[owner].get("tool_calls")
):
while idx < len(messages) and messages[idx].get("role") == "tool":
idx += 1
advanced = True
# (b) Skip past an equal-timestamp run whose left neighbour shares
# this message's role — inserting there would re-order an
# already-matched same-role turn (user, <inserted user>, assistant).
@@ -10434,6 +10440,7 @@ def merge_session_messages_append_only(
state_messages,
truncation_watermark=truncation_watermark,
truncation_boundary=truncation_boundary,
incoming_provenance=incoming_provenance,
)
finally:
_STRUCTURED_IDENTITY_MEMO.reset(token)
@@ -10445,6 +10452,7 @@ def _merge_session_messages_append_only_impl(
*,
truncation_watermark=None,
truncation_boundary=None,
incoming_provenance=None,
) -> list:
"""Merge sidecar/context and state.db messages without deleting local rows.