hermes_state_timeline.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. """Read-only prompt index and bounded transcript jumps; no transcript-wide payload hydration."""
  2. from __future__ import annotations
  3. import re
  4. from contextlib import contextmanager
  5. from agent.compaction_display import project_compaction_message_for_display
  6. from agent.context_compressor import user_originated_turn_view
  7. from hermes_state_compaction_display import not_a_later_tool_copy_sql
  8. from hermes_state_messages import DISPLAY_VISIBLE_SQL
  9. _SYNTHETIC_PROMPT = re.compile(
  10. r"^\s*(?:\[IMPORTANT: Background process |\[ASYNC (?:DELEGATION )?(?:BATCH )?COMPLETE\b|"
  11. r"A background fan-out of \d+ subagent\(s\) you dispatched earlier has finished\.|"
  12. r"A background subagent you dispatched earlier has finished\.)",
  13. re.IGNORECASE,
  14. )
  15. def _prompt_preview(db, content, display_kind, summary):
  16. message = project_compaction_message_for_display({
  17. "role": "user", "content": db._decode_content(content),
  18. "display_kind": display_kind, "_compressed_summary": bool(summary),
  19. })
  20. if message is None or user_originated_turn_view(message) is None:
  21. return ""
  22. content = message.get("content")
  23. if isinstance(content, list):
  24. content = " ".join(
  25. part if isinstance(part, str) else part.get("text", "")
  26. for part in content if isinstance(part, (str, dict)))
  27. if not isinstance(content, str):
  28. return ""
  29. text = " ".join(content.split())
  30. if not text or _SYNTHETIC_PROMPT.match(text):
  31. return ""
  32. return text if len(text) <= 120 else text[:119].rstrip() + "…"
  33. @contextmanager
  34. def _snapshot(db):
  35. # The count and page must see the same compaction/rewind generation.
  36. with db._read_ctx() as conn:
  37. conn.execute("BEGIN")
  38. try:
  39. yield conn
  40. finally:
  41. if conn.in_transaction:
  42. conn.execute("ROLLBACK")
  43. def _display_rows_sql(conn, session_id, *, users_only=False):
  44. """Return only representative ids and their stable first-row order, never bodies.
  45. Legacy stores cannot backfill on a GET. SQL groups their payload identities in
  46. SQLite; only user content crosses the Python boundary for carrier normalization.
  47. Current stores use the durable display index, including protected-tail copies.
  48. """
  49. filters = (" AND role = 'user'" if users_only else "") + DISPLAY_VISIBLE_SQL
  50. indexed = conn.execute(
  51. "SELECT 1 FROM messages WHERE session_id = ? AND (active = 1 OR compacted = 1) "
  52. f"{filters} AND (display_order IS NULL OR display_identity IS NULL) LIMIT 1",
  53. (session_id,),
  54. ).fetchone() is None
  55. if indexed:
  56. return f"""WITH display_rows AS (
  57. SELECT (SELECT candidate.id FROM messages candidate
  58. WHERE candidate.session_id = :sid
  59. AND candidate.display_order = m.display_order
  60. AND (candidate.active = 1 OR candidate.compacted = 1){DISPLAY_VISIBLE_SQL}{not_a_later_tool_copy_sql('candidate')}
  61. ORDER BY candidate.active DESC, candidate.id DESC LIMIT 1) AS row_id,
  62. m.display_order AS sort_id
  63. FROM messages m WHERE session_id = :sid AND (active = 1 OR compacted = 1){filters}{not_a_later_tool_copy_sql('m')}
  64. GROUP BY m.display_order
  65. )"""
  66. return f"""WITH ranked AS (
  67. SELECT id, MIN(id) OVER identity AS sort_id,
  68. ROW_NUMBER() OVER (identity ORDER BY active DESC, id DESC) AS preference
  69. FROM messages WHERE session_id = :sid AND (active = 1 OR compacted = 1){filters}
  70. WINDOW identity AS (PARTITION BY role,
  71. CASE WHEN role = 'user' THEN timeline_identity_content(content, display_kind) ELSE content END,
  72. timestamp, tool_call_id, tool_calls, tool_name)
  73. ), display_rows AS (SELECT id AS row_id, sort_id FROM ranked WHERE preference = 1)"""
  74. def _register_functions(db, conn):
  75. from agent.context_compressor import split_user_originated_turn
  76. def identity_content(content, display_kind):
  77. handoff, live = split_user_originated_turn({
  78. "role": "user", "content": db._decode_content(content), "display_kind": display_kind})
  79. return db._encode_content(live.get("content")) if handoff is not None and live is not None else content
  80. conn.create_function("timeline_identity_content", 2, identity_content, deterministic=True)
  81. conn.create_function("timeline_preview", 3,
  82. lambda content, kind, summary: _prompt_preview(db, content, kind, summary),
  83. deterministic=True)
  84. def get_session_messages_around(db, session_id, row_id, *, limit=120):
  85. """Read at most *limit* display rows starting at an exact human prompt.
  86. Existence probes/counts contain ids only. Full payloads are fetched only for
  87. the selected bounded page, even when the anchor is deep in a transcript.
  88. """
  89. with _snapshot(db) as conn:
  90. _register_functions(db, conn)
  91. anchor = conn.execute(
  92. "SELECT content, display_kind, _compressed_summary FROM messages "
  93. "WHERE session_id = ? AND id = ? AND role = 'user' AND (active = 1 OR compacted = 1)",
  94. (session_id, row_id),
  95. ).fetchone()
  96. if anchor is None or not _prompt_preview(db, *anchor):
  97. return None
  98. sql = _display_rows_sql(conn, session_id)
  99. params = {"sid": session_id, "row_id": row_id, "limit": limit}
  100. selected = conn.execute(sql + """
  101. SELECT sort_id FROM display_rows WHERE row_id = :row_id
  102. """, params).fetchone()
  103. if selected is None:
  104. return None
  105. params["start"] = selected["sort_id"]
  106. counts = conn.execute(sql + """
  107. SELECT COUNT(*) AS total, COALESCE(SUM(sort_id < :start), 0) AS offset FROM display_rows
  108. """, params).fetchone()
  109. rows = conn.execute(sql + """
  110. SELECT m.* FROM (SELECT row_id, sort_id FROM display_rows
  111. WHERE sort_id >= :start ORDER BY sort_id LIMIT :limit) AS page
  112. JOIN messages m ON m.id = page.row_id ORDER BY page.sort_id
  113. """, params).fetchall()
  114. messages = [db._row_to_message_dict(row, warn_context="timeline jump", summary_flag=True) for row in rows]
  115. return {"messages": messages, "pagination": {
  116. "row_id": row_id, "limit": limit, "returned": len(messages), "order": "oldest",
  117. "offset": counts["offset"], "total": counts["total"],
  118. "has_older": counts["offset"] > 0,
  119. "has_newer": counts["offset"] + len(messages) < counts["total"],
  120. }}
  121. def get_session_timeline(db, session_id, *, limit=500, after_row_id=0):
  122. """Chronological prompts. Cursor is the first physical row id of a logical turn."""
  123. with _snapshot(db) as conn:
  124. _register_functions(db, conn)
  125. sql = _display_rows_sql(conn, session_id, users_only=True) + """,
  126. prompts AS MATERIALIZED (
  127. SELECT row_id, sort_id, m.timestamp,
  128. timeline_preview(m.content, m.display_kind, m._compressed_summary) AS preview
  129. FROM display_rows JOIN messages m ON m.id = row_id
  130. ), eligible AS MATERIALIZED (SELECT * FROM prompts WHERE preview <> '')
  131. """
  132. params = {"sid": session_id, "after": after_row_id, "limit": limit + 1}
  133. rows = conn.execute(sql + """
  134. SELECT row_id, sort_id, timestamp, preview, (SELECT COUNT(*) FROM eligible) AS total
  135. FROM eligible WHERE sort_id > :after ORDER BY sort_id LIMIT :limit
  136. """, params).fetchall()
  137. total = rows[0]["total"] if rows else conn.execute(
  138. sql + "SELECT COUNT(*) FROM eligible", params).fetchone()[0]
  139. has_more = len(rows) > limit
  140. page = rows[:limit]
  141. return {
  142. "entries": [{"row_id": row["row_id"], "preview": row["preview"], "timestamp": row["timestamp"]}
  143. for row in page],
  144. "pagination": {"limit": limit, "after_row_id": after_row_id, "returned": len(page),
  145. "total": total, "has_more": has_more,
  146. "next_cursor": page[-1]["sort_id"] if has_more else None},
  147. }