hermes_state_identity.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. """Durable message-identity columns for SessionDB (schema v31): the codec between the live identity keys
  2. (``agent.message_metadata``) and the ``message_uid`` / ``absorbed_message_uids`` / ``tool_call_uids`` /
  3. ``tool_call_uid`` columns, shared by the transcript writers, restore, and transcript repair."""
  4. from __future__ import annotations
  5. import json
  6. from typing import Any, Dict, List, Mapping, MutableMapping, Optional, Tuple
  7. from agent.message_metadata import ABSORBED_MESSAGE_UIDS, MESSAGE_UID, TOOL_CALL_UID, TOOL_CALL_UIDS, uid_list
  8. from agent.message_sanitization import coalesce_tool_call_id
  9. from hermes_state_common import _json_or
  10. def _live_or_column(msg: Mapping[str, Any], live_key: str, column: str) -> Any:
  11. """Flushed dicts and batch rows carry the live key; import payloads carry the column name."""
  12. return msg.get(live_key) if live_key in msg else msg.get(column)
  13. def _uid_list(value: Any) -> list[str]:
  14. """Normalize a uid list (a live list, or the JSON text an export/import carries) to unique non-empty
  15. strings in order; anything else is ``[]``."""
  16. if isinstance(value, str):
  17. value = _json_or(value, [], "Failed to deserialize a message uid list, falling back to []")
  18. return uid_list(value)
  19. def _uid_map(value: Any) -> dict[str, Any]:
  20. """Normalize a ``{tool call id: uid}`` map (a live dict, or the JSON text an export/import carries): a
  21. non-empty string uid, or a list of them for a provider id repeated inside one row (one per occurrence,
  22. see ``merge_tool_call_uids``); anything else is dropped, and a non-map is ``{}``."""
  23. if isinstance(value, str):
  24. value = _json_or(value, {}, "Failed to deserialize a tool-call uid map, falling back to {}")
  25. if not isinstance(value, dict):
  26. return {}
  27. return {k: v for k, v in value.items() if isinstance(k, str) and k and (
  28. (isinstance(v, str) and v) or (isinstance(v, list) and v and all(isinstance(u, str) and u for u in v)))}
  29. def _tool_call_uid_map(msg: Mapping[str, Any]) -> dict[str, Any]:
  30. return _uid_map(_live_or_column(msg, TOOL_CALL_UIDS, "tool_call_uids"))
  31. def _absorbed_uids_json(msg: Mapping[str, Any]) -> Optional[str]:
  32. uids = _uid_list(_live_or_column(msg, ABSORBED_MESSAGE_UIDS, "absorbed_message_uids"))
  33. return json.dumps(uids) if uids else None
  34. def _tool_call_uids_json(msg: Mapping[str, Any]) -> Optional[str]:
  35. uids = _tool_call_uid_map(msg)
  36. return json.dumps(uids, sort_keys=True) if uids else None
  37. def _tool_call_uid_or_none(msg: Mapping[str, Any]) -> Optional[str]:
  38. uid = _live_or_column(msg, TOOL_CALL_UID, "tool_call_uid")
  39. return uid if isinstance(uid, str) and uid else None
  40. def _live_extends(live_uid: Any, stored_uid: Any) -> bool:
  41. """A live per-occurrence list that starts with every stored occurrence: a fold's union not yet written."""
  42. stored = stored_uid if isinstance(stored_uid, list) else [stored_uid]
  43. return isinstance(live_uid, list) and live_uid[:len(stored)] == stored
  44. def _fill_missing_tool_call_uids(row: Any, msg: MutableMapping[str, Any]) -> None:
  45. """Before a row-addressed rewrite serializes *msg*: stored uids for calls it still names but whose uid it
  46. lost (a clone), so the rewrite does not null them. A call the live dict dropped keeps no uid."""
  47. if not row["tool_call_uids"] or not isinstance(calls := msg.get("tool_calls"), list):
  48. return
  49. live = msg.get(TOOL_CALL_UIDS)
  50. live = live if isinstance(live, dict) else {}
  51. named = {coalesce_tool_call_id(tc) for tc in calls}
  52. if missing := {k: v for k, v in _uid_map(row["tool_call_uids"]).items() if k in named and k not in live}:
  53. msg[TOOL_CALL_UIDS] = {**live, **missing}
  54. def _restore_row_identity(row: Any, msg: MutableMapping[str, Any]) -> None:
  55. """A stored row's uid and tool-call uids onto its live dict (the stored value wins). A live per-occurrence
  56. list that already holds the stored uid is kept: it is a fold's union the row has not been rewritten
  57. with yet, and replacing it with the stored single uid would strip the later occurrence."""
  58. if row[MESSAGE_UID]:
  59. msg[MESSAGE_UID] = row[MESSAGE_UID]
  60. if row["tool_call_uids"] and (stored := _uid_map(row["tool_call_uids"])):
  61. live = msg.get(TOOL_CALL_UIDS)
  62. live = live if isinstance(live, dict) else {}
  63. msg[TOOL_CALL_UIDS] = {**live, **{call_id: uid for call_id, uid in stored.items()
  64. if not _live_extends(live.get(call_id), uid)}}
  65. if row["tool_call_uid"]:
  66. msg[TOOL_CALL_UID] = row["tool_call_uid"]
  67. def _restore_identity_columns(row: Any, msg: MutableMapping[str, Any]) -> None:
  68. """The stored identity columns onto a restored dict under their live keys (NULL/empty add nothing)."""
  69. _restore_row_identity(row, msg)
  70. if row["absorbed_message_uids"] and (absorbed := _uid_list(row["absorbed_message_uids"])):
  71. msg[ABSORBED_MESSAGE_UIDS] = absorbed
  72. def _stable_tool_key(row: Any) -> Optional[tuple[Any, ...]]:
  73. """Display-dedupe key of a tool-calling assistant row built from its stable call ids instead of the arguments a
  74. prune rewrites (#117750: a pruned carried-forward copy must collapse with its durable original). ``None`` for
  75. every other row and for an incomplete id set, so the caller keeps the full content key: a tool RESULT row keeps
  76. its payload in the key, because folding the archived full output into its pruned stub would drop the original
  77. from compacted history and transcript exports, and distinct id-less calls never merge."""
  78. if row["role"] != "assistant":
  79. return None
  80. calls = _json_or(row["tool_calls"] or "[]", [], "Failed to deserialize tool_calls, falling back to []")
  81. call_ids = tuple(coalesce_tool_call_id(tc) for tc in calls or ())
  82. if call_ids and all(call_ids):
  83. return ("assistant", None, row["timestamp"], row["tool_call_id"], call_ids, row["tool_name"])
  84. return None