Skip to content

The agent that grows with you

fix(sessions): branch copies that drop tool_calls don't carry tool-call uid maps

kshitijk4poor4de41452ff52
diff --git a/agent/message_metadata.py b/agent/message_metadata.py
index f250efb4e1d..4e0796abaea 100644
--- a/agent/message_metadata.py
+++ b/agent/message_metadata.py
@@ -75,16 +75,20 @@ def copy_identity_fields(src: Mapping[str, Any], dst: MutableMapping[str, Any])
for key, kind in _IDENTITY_FIELD_TYPES:
value = src.get(key)
if isinstance(value, kind) and value:
- dst[key] = kind(value) if kind is not str else value
+ dst[key] = kind(value)
-def message_identity(msg: MutableMapping[str, Any]) -> dict:
+def message_identity(msg: MutableMapping[str, Any], *, with_tool_uids: bool = False) -> dict:
"""The identity fields a new row copied from *msg* must carry, minting *msg*'s uid first when it has none:
a branch/seed copy writes fresh rows from the live dicts the new session keeps using, and a row without
- them would restore with a different uid than the live dict carries."""
+ them would restore with a different uid than the live dict carries. ``with_tool_uids`` only for a copy
+ that also carries ``tool_calls`` / ``tool_call_id``; a uid map on a row without its calls pairs nothing."""
stamp_message_uid(msg)
identity: dict = {}
copy_identity_fields(msg, identity)
+ if not with_tool_uids:
+ identity.pop(TOOL_CALL_UIDS, None)
+ identity.pop(TOOL_CALL_UID, None)
return identity
diff --git a/hermes_cli/cli_commands_mixin.py b/hermes_cli/cli_commands_mixin.py
index f884ba755f2..1bbf4c75f94 100644
--- a/hermes_cli/cli_commands_mixin.py
+++ b/hermes_cli/cli_commands_mixin.py
@@ -1421,7 +1421,7 @@ class CLICommandsMixin:
self._session_db.append_messages_batch(new_session_id, [
{"role": msg.get("role", "user"), "tool_name": msg.get("tool_name") or msg.get("name"),
"api_content": extract_api_content_sidecar(msg),
- **{k: msg.get(k) for k in _BRANCH_COPY_KEYS}, **message_identity(msg)}
+ **{k: msg.get(k) for k in _BRANCH_COPY_KEYS}, **message_identity(msg, with_tool_uids=True)}
for msg in self.conversation_history], chunk_rows=500)
with suppress(Exception):
self._session_db.set_session_title(new_session_id, branch_title)
diff --git a/tui_gateway/session_workdir.py b/tui_gateway/session_workdir.py
index 472fab9c9e9..956b7a60632 100644
--- a/tui_gateway/session_workdir.py
+++ b/tui_gateway/session_workdir.py
@@ -454,8 +454,9 @@ def _persist_branch_seed(session: dict) -> None:
if not (key := session.get("session_key")) or not session.get("seeded") or session.get("_branch_seed_persisted"):
return
from agent.message_metadata import message_identity
- with session["history_lock"]:
- seed = [{**msg, **message_identity(msg)} for msg in (session.get("history") or [])]
+ with session["history_lock"]: # message_identity stamps the live dicts
+ seed = [{"role": msg.get("role", "user"), **{f: msg.get(f) for f in _WORKDIR_SEED_FIELDS},
+ **message_identity(msg)} for msg in (session.get("history") or [])]
if not seed:
return
with _session_db(session) as db:
@@ -466,10 +467,7 @@ def _persist_branch_seed(session: dict) -> None:
# partial seed with _branch_seed_persisted unset.
# Bounded-chunk transactions (see #23254): a branch seed can be hundreds of rows; chunking keeps
# each BEGIN IMMEDIATE short so concurrent writers aren't starved.
- db.append_messages_batch(
- key, [{"role": msg.get("role", "user"), **{f: msg.get(f) for f in _WORKDIR_SEED_FIELDS},
- **message_identity(msg)} for msg in seed],
- chunk_rows=500)
+ db.append_messages_batch(key, seed, chunk_rows=500)
session["_branch_seed_persisted"] = True
except Exception as exc:
_workdir_reraise_disk_full(exc, "branch seed persist failed")