history_commentary.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. """Display-only Codex commentary projection shared by REST and gateway history.
  2. Provider items and the stored reasoning string remain untouched for model replay.
  3. Only the projected fields may be used for public transcript text.
  4. """
  5. import json
  6. from contextlib import contextmanager
  7. from typing import Any
  8. from agent.redact import redact_sensitive_text
  9. from hermes_constants import reset_hermes_home_override, set_hermes_home_override
  10. from utils import is_truthy_value
  11. @contextmanager
  12. def _owning_home(home):
  13. token = set_hermes_home_override(str(home)) if home is not None else None
  14. try:
  15. yield
  16. finally:
  17. if token is not None:
  18. reset_hermes_home_override(token)
  19. # display_metadata flag on a ``hold_stream`` turn's published reply: its commentary items stay for
  20. # model replay, but the transform never saw them, so no history projection shows them.
  21. COMMENTARY_WITHHELD = "commentary_withheld"
  22. def visible_commentary(text: str, *, strip_thinking=None) -> str:
  23. """Use the same think stripping and secret redaction as live delivery."""
  24. if strip_thinking is None:
  25. from agent.agent_runtime_helpers import strip_think_blocks
  26. strip_thinking = lambda value: strip_think_blocks(None, value)
  27. visible = strip_thinking(text).strip()
  28. return redact_sensitive_text(visible) if visible else visible
  29. def _phase_message_items(message: dict, phases: frozenset[str | None]) -> list[str]:
  30. items = message.get("codex_message_items")
  31. if isinstance(items, str):
  32. try:
  33. items = json.loads(items)
  34. except (TypeError, ValueError):
  35. return []
  36. if not isinstance(items, list):
  37. return []
  38. result = []
  39. for item in items:
  40. if (
  41. not isinstance(item, dict)
  42. or item.get("type") != "message"
  43. or item.get("role") != "assistant"
  44. ):
  45. continue
  46. phase, content = item.get("phase"), item.get("content")
  47. if phase is not None and not isinstance(phase, str):
  48. continue
  49. normalized_phase = phase.strip().lower() if isinstance(phase, str) else None
  50. if normalized_phase not in phases or not isinstance(content, list):
  51. continue
  52. text = "".join(
  53. part["text"]
  54. for part in content
  55. if isinstance(part, dict)
  56. and part.get("type") == "output_text"
  57. and isinstance(part.get("text"), str)
  58. and part["text"].strip()
  59. ).strip()
  60. if text:
  61. result.append(text)
  62. return result
  63. def commentary_items(message: dict) -> list[str]:
  64. return _phase_message_items(message, frozenset({"commentary"}))
  65. def _final_items(message: dict) -> list[str]:
  66. # Unphased assistant messages are normalized as final content too.
  67. return _phase_message_items(message, frozenset({"final", "final_answer", None}))
  68. def _without_flattened_commentary(reasoning: str, commentary: list[str]) -> str:
  69. """Omit exact whole-line public segments from a display copy, not source data.
  70. Current normalization separates parts with two newlines; older rows may
  71. have one. If a private segment happens to be identical, origin is ambiguous:
  72. omit both occurrences rather than leaking the public text or choosing the
  73. wrong one. Surrounding reasoning is retained in its original line style.
  74. """
  75. remaining = reasoning
  76. for text in sorted(set(commentary), key=len, reverse=True):
  77. search_from = 0
  78. while (at := remaining.find(text, search_from)) >= 0:
  79. end = at + len(text)
  80. if (at == 0 or remaining[at - 1] == "\n") and (
  81. end == len(remaining) or remaining[end] == "\n"
  82. ):
  83. before, after = remaining[:at], remaining[end:]
  84. prefix, suffix = before.rstrip("\n"), after.lstrip("\n")
  85. separator = (
  86. "\n\n"
  87. if before.endswith("\n\n") or after.startswith("\n\n")
  88. else "\n"
  89. )
  90. remaining = prefix + (separator if prefix and suffix else "") + suffix
  91. search_from = 0
  92. else:
  93. search_from = end
  94. return remaining
  95. def _project_one(message: dict, *, enabled: bool) -> dict:
  96. if message.get("role") != "assistant" or message.get("display_kind") == "hidden":
  97. return message
  98. raw = commentary_items(message)
  99. if not raw:
  100. return message
  101. projected = dict(message)
  102. # A disabled profile must not recover raw sidecar text on the frontend.
  103. projected["display_commentary"] = []
  104. if enabled and not (message.get("display_metadata") or {}).get(COMMENTARY_WITHHELD):
  105. projected["display_commentary"] = [
  106. part for text in raw if (part := visible_commentary(text))
  107. ]
  108. reasoning = (
  109. message.get("reasoning")
  110. or message.get("reasoning_content")
  111. or message.get("reasoning_details")
  112. or ""
  113. )
  114. if isinstance(reasoning, str):
  115. projected["display_reasoning"] = _without_flattened_commentary(reasoning, raw)
  116. # A nonempty canonical answer can also be filled by stream recovery after
  117. # the provider sidecar was captured. Without a matching final item there
  118. # is no way to distinguish "commentary + final" from a real final that
  119. # merely starts with the same words. Never delete canonical answer bytes.
  120. # Sanitize the display copy when it contains raw commentary and avoid
  121. # duplicating a leading commentary item in a separate Desktop bubble.
  122. content = message.get(
  123. "display_content", message.get("content", message.get("text"))
  124. )
  125. if isinstance(content, str) and any(text in content for text in raw):
  126. finals = _final_items(message)
  127. if not (finals and content.strip() == "\n".join(finals)):
  128. projected["display_content"] = visible_commentary(content)
  129. if any(content.startswith(text) for text in raw):
  130. projected["display_commentary"] = []
  131. return projected
  132. def project_history_commentary(messages: list[dict], *, home: Any = None) -> list[dict]:
  133. """Project a batch inside the owning profile's config and redaction scope."""
  134. if not any(
  135. isinstance(message, dict) and message.get("codex_message_items")
  136. for message in messages
  137. ):
  138. return messages
  139. with _owning_home(home):
  140. from hermes_cli.config import load_config
  141. try:
  142. display = load_config().get("display") or {}
  143. enabled = is_truthy_value(
  144. display.get("show_commentary"), default=True
  145. ) and is_truthy_value(
  146. display.get("interim_assistant_messages"), default=True
  147. )
  148. except Exception:
  149. enabled = False # unreadable policy must not publish raw provider items
  150. return [
  151. _project_one(message, enabled=enabled)
  152. if isinstance(message, dict)
  153. else message
  154. for message in messages
  155. ]