hermes_state_todo.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. """The newest Todo result a resume restores: an indexed, bounded search of the resume display scope.
  2. Split out of ``hermes_state_messages`` and bound onto SessionDB through ``SessionMessagesMixin``; built
  3. on its _resume_lineage_ids/_read_ctx primitives."""
  4. from __future__ import annotations
  5. import json
  6. from typing import Any, Optional
  7. _TODO_TOOLS = "tool_name IN ('todo_list', 'todo')"
  8. class SessionTodoResultMixin:
  9. """Latest visible, paired and valid Todo tool result of a session's lineage."""
  10. def get_latest_todo_result(self, session_id: str, *, max_chars: Optional[int] = None) -> Optional[str]:
  11. """Newest visible, paired and valid Todo result in the resume display scope.
  12. Search indexed, bounded pages rather than loading a potentially long transcript. A branch owns its
  13. copied history; compression continuations also show their ancestors' archived rows. Invalid results
  14. are skipped just as the model's Todo hydration does, and explicit empty results are retained. A result
  15. whose newest copy was removed (an undo in a compression child) is skipped too, as the display read
  16. hides it: its ancestor's copy must not bring the undone plan back.
  17. """
  18. if not session_id:
  19. return None
  20. if max_chars is None:
  21. from tools.todo_tool import MAX_TODO_RESULT_CHARS
  22. max_chars = MAX_TODO_RESULT_CHARS
  23. session_ids = self._resume_lineage_ids(session_id)
  24. with self._read_ctx() as conn:
  25. removed = self._removed_todo_copies(conn, session_ids)
  26. for sid in reversed(session_ids):
  27. before_id = 2**63 - 1
  28. while True:
  29. rows = conn.execute(
  30. "SELECT id, role, content, timestamp, tool_call_id, tool_calls, tool_name, message_uid, "
  31. "absorbed_message_uids FROM messages INDEXED BY idx_messages_session_id "
  32. f"WHERE session_id = ? AND id < ? AND role = 'tool' AND {_TODO_TOOLS} AND (active = 1 OR compacted = 1) "
  33. "AND length(content) <= ? ORDER BY id DESC LIMIT 100",
  34. (sid, before_id, max_chars),
  35. ).fetchall()
  36. if not rows:
  37. break
  38. for row in rows:
  39. call_id = row["tool_call_id"]
  40. if not call_id or self._todo_copy_removed(row, removed):
  41. continue
  42. prior = conn.execute(
  43. "SELECT role, tool_calls FROM messages INDEXED BY idx_messages_session_id "
  44. "WHERE session_id = ? AND id < ? AND role IN ('assistant', 'user', 'system') "
  45. "AND (active = 1 OR compacted = 1) ORDER BY id DESC LIMIT 1",
  46. (sid, row["id"]),
  47. ).fetchone()
  48. if prior is None or prior["role"] != "assistant":
  49. continue
  50. try:
  51. calls = json.loads(prior["tool_calls"]) if prior["tool_calls"] else []
  52. def is_todo_call(call):
  53. if not isinstance(call, dict) or call.get("id") != call_id:
  54. return False
  55. function = call.get("function")
  56. if not isinstance(function, dict):
  57. return False
  58. if function.get("name") in ("todo_list", "todo"):
  59. return True
  60. if function.get("name") != "tool_call":
  61. return False
  62. arguments = function.get("arguments")
  63. if isinstance(arguments, str):
  64. arguments = json.loads(arguments)
  65. if not isinstance(arguments, dict):
  66. return False
  67. inner_calls = arguments.get("calls")
  68. return isinstance(inner_calls, list) and any(
  69. isinstance(inner, dict) and inner.get("name") in ("todo_list", "todo")
  70. for inner in inner_calls)
  71. paired = isinstance(calls, list) and any(is_todo_call(call) for call in calls)
  72. if not paired:
  73. continue
  74. data = json.loads(row["content"])
  75. if not isinstance(data, dict) or not isinstance(data.get("todos"), list):
  76. continue
  77. int(data.get("revision") or 0)
  78. except (ValueError, TypeError):
  79. continue
  80. return row["content"]
  81. before_id = rows[-1]["id"]
  82. return None
  83. def _todo_copies(self, row, identity: Any = None) -> list[Any]:
  84. """The copy identities ``_dedupe_display_generations(drop_removed=True)`` gives a Todo result row."""
  85. from hermes_state_identity import _uid_list
  86. hashed = identity or self._display_identity(self._display_dedupe_key(row))
  87. return [hashed, *(("uid", uid) for uid in (row["message_uid"], *_uid_list(row["absorbed_message_uids"])) if uid)]
  88. def _removed_todo_copies(self, conn, session_ids: list[str]) -> dict[Any, int]:
  89. """Copy identity -> newest removed (``active=0, compacted=0``) Todo result row id in the lineage."""
  90. removed: dict[Any, int] = {}
  91. for row in conn.execute(
  92. "SELECT id, role, timestamp, tool_call_id, tool_calls, tool_name, message_uid, absorbed_message_uids, "
  93. "display_identity, CASE WHEN display_identity IS NULL THEN content END AS content "
  94. f"FROM messages WHERE session_id IN ({','.join('?' * len(session_ids))}) AND active = 0 "
  95. f"AND compacted = 0 AND role = 'tool' AND {_TODO_TOOLS}", tuple(session_ids)):
  96. for copy in self._todo_copies(row, row["display_identity"]):
  97. removed[copy] = max(removed.get(copy, 0), row["id"])
  98. return removed
  99. def _todo_copy_removed(self, row, removed: dict[Any, int]) -> bool:
  100. """A newer copy of *row* was removed: the display read hides it, so the resume must not restore it."""
  101. return bool(removed) and any(removed.get(copy, 0) > row["id"] for copy in self._todo_copies(row))