hermes_state_compaction_display.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """Display reads paint a compacted tool result once, as its archived original (#132939, #135655).
  2. Compaction can carry a tool result forward as a shortened stub: same ``message_uid`` and timestamp, short
  3. body. A tool result keeps its payload in its display key on purpose (the archived output must never fold
  4. INTO its stub), so the stub lands in a display slot (``display_order``) of its own and both copies painted.
  5. The reads therefore skip a tool row when an archived (``compacted``) display-visible tool row of the same
  6. session, ``message_uid`` and timestamp holds an EARLIER slot: that slot already paints the full original.
  7. The rule uses identity only (session, uid, timestamp, slot), never bodies, and changes no stored row. An
  8. in-place prune keeps its row's slot, so its stub shares the slot with the archived copies and the per-slot
  9. rule (the live row wins) keeps showing the stub. A row without a slot (legacy, never backfilled) is never
  10. skipped. The timestamp lets the probe seek ``(session_id, timestamp)``, so a page read stays per-row cheap.
  11. """
  12. from __future__ import annotations
  13. from typing import Any, Iterable
  14. from agent.context_compressor import MODEL_ONLY_DISPLAY_METADATA_KEY
  15. from hermes_state_common import _sql_json_extract
  16. _ORIGINAL_VISIBLE_SQL = (
  17. f"COALESCE({_sql_json_extract('original.display_metadata', '$.' + MODEL_ONLY_DISPLAY_METADATA_KEY)}, 0) = 0")
  18. def not_a_later_tool_copy_sql(alias: str) -> str:
  19. """Filter that drops *alias* rows that are a later copy of an archived tool result."""
  20. return (f" AND NOT ({alias}.role = 'tool' AND {alias}.message_uid IS NOT NULL AND EXISTS ("
  21. f"SELECT 1 FROM messages AS original WHERE original.session_id = {alias}.session_id "
  22. f"AND original.timestamp = {alias}.timestamp AND original.message_uid = {alias}.message_uid "
  23. "AND original.role = 'tool' AND original.active = 0 AND original.compacted = 1 "
  24. f"AND +original.display_order < {alias}.display_order AND {_ORIGINAL_VISIBLE_SQL}))")
  25. def later_tool_copy_ids(db: Any, rows: Iterable[Any]) -> set[int]:
  26. """Python twin of :func:`not_a_later_tool_copy_sql` over display-set *rows*: the ids it skips.
  27. Empty when the rows carry no slot (``display_order``) or archive flag (``compacted``)."""
  28. tools = [row for row in rows if row["role"] == "tool"]
  29. if not tools or "compacted" not in tools[0].keys():
  30. return set()
  31. candidates = [row for row in tools if row["active"] or row["compacted"]]
  32. if not candidates or "display_order" not in candidates[0].keys():
  33. return set()
  34. def owner(row):
  35. return row["session_id"], row["message_uid"], row["timestamp"]
  36. first_slot: dict[Any, int] = {}
  37. for row in candidates:
  38. slot = row["display_order"]
  39. if row["message_uid"] and slot is not None and not row["active"] and not db._is_model_only_row(row):
  40. first_slot[owner(row)] = min(first_slot.get(owner(row), slot), slot)
  41. return {row["id"] for row in candidates if row["message_uid"] and row["display_order"] is not None
  42. and first_slot.get(owner(row), row["display_order"]) < row["display_order"]}