Skip to content

The agent that grows with you

fix(sessions): an assistant fold that discards the later text records no merge witness

kshitijk4poor5d8b17f865c9
diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.py
index 724f2b51409..4441f632788 100644
--- a/agent/agent_runtime_helpers.py
+++ b/agent/agent_runtime_helpers.py
@@ -381,8 +381,10 @@ def _is_codex_interim(m: Dict) -> bool:
)
-def _merge_assistant_into(prev: Dict, msg: Dict) -> None:
- """Fold a consecutive assistant ``msg`` into ``prev`` (union tool_calls, concat text)."""
+def _merge_assistant_into(prev: Dict, msg: Dict) -> bool:
+ """Fold a consecutive assistant ``msg`` into ``prev`` (union tool_calls, concat text). Returns whether
+ ``msg``'s text lives on in ``prev``: multimodal (list) content is never joined, so a non-empty ``msg``
+ content beside a list (or a list beside non-empty text) is discarded and must earn no merge witness."""
from agent.context_compressor import _DB_PERSISTED_MARKER
prev_calls = list(prev.get("tool_calls") or [])
@@ -414,7 +416,9 @@ def _merge_assistant_into(prev: Dict, msg: Dict) -> None:
prev_content = prev.get("content")
new_content = msg.get("content")
content_rewritten = False
+ text_kept = not new_content # nothing to lose
if isinstance(prev_content, str) and isinstance(new_content, str):
+ text_kept = True
joined = "\n".join(p for p in (prev_content.strip(), new_content.strip()) if p)
prev["content"] = joined
# A falsy new_content leaves ``joined`` == prev_content; that is not a rewrite.
@@ -424,6 +428,7 @@ def _merge_assistant_into(prev: Dict, msg: Dict) -> None:
elif not prev_content and new_content is not None:
prev["content"] = new_content
content_rewritten = new_content != prev_content
+ text_kept = True
# Carry reasoning_content from the later turn only if the earlier lacks it (strict thinking
# providers need one on the merged tool-call turn).
reasoning_carried = False
@@ -451,6 +456,7 @@ def _merge_assistant_into(prev: Dict, msg: Dict) -> None:
# keeps the pre-merge row. The caller recomputes the flush cursor for the surviving sequence.
if content_rewritten or calls_changed or reasoning_carried:
prev.pop(_DB_PERSISTED_MARKER, None)
+ return text_kept
def _remember_absorbed_row(survivor: Dict[str, Any], dropped: Dict[str, Any], *, folded: bool = True) -> None:
@@ -496,8 +502,7 @@ def _merge_consecutive_assistants(messages: List[Dict]) -> Tuple[List[Dict], int
_remember_absorbed_row(msg, prev, folded=False)
collapsed[-1] = msg
else:
- _merge_assistant_into(prev, msg)
- _remember_absorbed_row(prev, msg)
+ _remember_absorbed_row(prev, msg, folded=_merge_assistant_into(prev, msg))
repairs += 1
continue
collapsed.append(msg)
diff --git a/tests/agent/test_context_engine_message_uid.py b/tests/agent/test_context_engine_message_uid.py
index 411b898e095..78f47835901 100644
--- a/tests/agent/test_context_engine_message_uid.py
+++ b/tests/agent/test_context_engine_message_uid.py
@@ -351,6 +351,24 @@ def test_a_reused_id_merge_survives_rewrite_and_restore(db):
assert next(m for m in again if m["role"] == "tool")["_tool_call_uid"] == occurrences[1]
[email protected]("kept, dropped", [
+ ([{"type": "text", "text": "kept"}], "UNIQUE-DROPPED-TEXT"),
+ ("kept", [{"type": "text", "text": "UNIQUE-DROPPED-PART"}]),
+ ([{"type": "text", "text": "kept"}], [{"type": "text", "text": "UNIQUE-DROPPED-PART"}]),
+])
+def test_an_assistant_fold_that_discards_the_later_text_is_no_witness(kept, dropped):
+ """Multimodal content is never joined: the later turn's row is retired, but its uid must not be
+ claimed as a constituent of text it does not contain."""
+ from agent.agent_runtime_helpers import _merge_consecutive_assistants
+
+ survivor = {"role": "assistant", "content": kept, "message_uid": "a" * UID_LEN}
+ later = {"role": "assistant", "content": dropped, "message_uid": "b" * UID_LEN, "_row_id": 9}
+ merged, repairs = _merge_consecutive_assistants([survivor, later])
+ assert repairs == 1 and merged == [survivor] and survivor["content"] == kept
+ assert "_absorbed_message_uids" not in survivor
+ assert survivor["_absorbed_row_ids"] == [9]
+
+
def test_a_select_context_selection_is_stripped_before_the_provider():
"""The request copy is stripped BEFORE ``select_context``; an engine that hands back the
``conversation_messages`` clones must not put the ids back on the wire."""
diff --git a/website/docs/developer-guide/context-engine-plugin.md b/website/docs/developer-guide/context-engine-plugin.md
index 349bbec6474..bf18aa0c125 100644
--- a/website/docs/developer-guide/context-engine-plugin.md
+++ b/website/docs/developer-guide/context-engine-plugin.md
@@ -176,8 +176,9 @@ What the host guarantees:
the host's fold of `A` and `B` rather than a new message. Merges made while
restoring a history (`repair_alternation=True`) record the witness the same
way. A row the host discards rather than folds (a provisional verification
- candidate superseded by the final answer) is not recorded: the witness
- names text that lives on in the composite, nothing else.
+ candidate superseded by the final answer, or an assistant turn whose text
+ sits beside multimodal content, which is never joined) is not recorded: the
+ witness names text that lives on in the composite, nothing else.
- **Engine-authored rows keep the uid the engine sets.** If your `compress()`
output pre-stamps `message_uid` on a summary carrier (or any row it emits),
the host writes that value through the commit, both in place and on