turn_response_intake.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. """Response intake for the conversation turn loop: normalize the raw provider response into
  2. the assistant message, splice agent-as-provider projections, fire ``post_api_request``, relay
  3. reasoning to the progress callback, and apply the incomplete-scratchpad / Codex-incomplete
  4. continuation guards. Nothing here imports ``agent.conversation_loop`` at module level (cycle).
  5. """
  6. from __future__ import annotations
  7. from dataclasses import dataclass
  8. import json
  9. import logging
  10. import re
  11. from typing import Any, Dict, Optional
  12. from agent.provider_projection import splice_provider_projection
  13. from agent.think_scrubber import THINK_TAG_NAMES
  14. from agent.trajectory import has_incomplete_scratchpad
  15. from agent.turn_truncation import (
  16. CODEX_FALLBACK_ACTIVATED, continue_codex_incomplete, normalize_response_for_agent, partial_result,
  17. )
  18. logger = logging.getLogger("agent.conversation_loop")
  19. # One alternation over the shared tag list so this path tracks every tag the scrubber hides
  20. # (the old 3-of-9 list leaked <thinking>/<思考>/... previews), case-insensitive like the scrubber.
  21. _THINK_TAG_ALT = "|".join(re.escape(name) for name in THINK_TAG_NAMES)
  22. _REASONING_TAG_RE = re.compile(rf'</?(?:{_THINK_TAG_ALT})>', re.IGNORECASE)
  23. _INLINE_REASONING_RE = re.compile(
  24. rf'<(?:{_THINK_TAG_ALT})>(.*?)</(?:{_THINK_TAG_ALT})>',
  25. re.DOTALL | re.IGNORECASE,
  26. )
  27. @dataclass
  28. class ResponseIntakeVerdict:
  29. """``action``: ``"fallthrough"`` (process ``assistant_message``), ``"continue"`` (retry the
  30. iteration: incomplete scratchpad / Codex continuation) or ``"return"`` (``result`` is the
  31. turn's result dict). ``assistant_message``/``finish_reason`` are the normalized outputs;
  32. ``active_system_prompt`` is rebound after a Codex reasoning-only fallover (#67321)."""
  33. action: str
  34. assistant_message: Any
  35. finish_reason: Any
  36. result: Optional[dict[str, Any]] = None
  37. active_system_prompt: Any = None
  38. def _coerce_content_text(raw: Any) -> str:
  39. """Some OpenAI-compatible servers (llama-server) return content as dict/list, which
  40. crashes downstream ``.strip()``; normalize to str (multimodal lists → text parts)."""
  41. if isinstance(raw, dict):
  42. return raw.get("text", "") or raw.get("content", "") or json.dumps(raw)
  43. if isinstance(raw, list):
  44. parts = []
  45. for part in raw:
  46. if isinstance(part, str):
  47. parts.append(part)
  48. elif isinstance(part, dict) and part.get("type") == "text":
  49. parts.append(part.get("text", ""))
  50. elif isinstance(part, dict) and "text" in part:
  51. parts.append(str(part["text"]))
  52. return "\n".join(parts)
  53. return str(raw)
  54. def _fire_post_api_request_hook(
  55. agent: Any, response: Any, assistant_message: Any, finish_reason: Any, *, api_messages: Any,
  56. api_call_count: Any, api_duration: Any, api_start_time: Any, api_request_id: Any,
  57. effective_task_id: Any, turn_id: Any,
  58. ) -> None:
  59. from agent.conversation_loop import _moa_reference_metrics_for_hook
  60. try:
  61. from hermes_cli.lifecycle import has_hook, invoke_hook as _invoke_hook
  62. if has_hook("post_api_request"):
  63. _invoke_hook(
  64. "post_api_request",
  65. task_id=effective_task_id,
  66. turn_id=turn_id,
  67. api_request_id=api_request_id,
  68. session_id=agent.session_id or "",
  69. platform=agent.platform or "",
  70. model=agent.model,
  71. provider=agent.provider,
  72. base_url=agent.base_url,
  73. api_mode=agent.api_mode,
  74. api_call_count=api_call_count,
  75. api_duration=api_duration,
  76. started_at=api_start_time,
  77. ended_at=api_start_time + api_duration,
  78. # First stream chunk time (epoch s); None if not streamed / no chunk.
  79. # TTFB = first_chunk_at - started_at.
  80. first_chunk_at=getattr(agent, "_last_api_first_chunk_at", None),
  81. finish_reason=finish_reason,
  82. message_count=len(api_messages),
  83. response_model=getattr(response, "model", None),
  84. response=agent._api_response_payload_for_hook(
  85. response, assistant_message, finish_reason=finish_reason
  86. ),
  87. usage=agent._usage_summary_for_api_request_hook(response),
  88. context_length=getattr(getattr(agent, "context_compressor", None), "context_length", None),
  89. assistant_message=assistant_message,
  90. assistant_content_chars=len(assistant_message.content or ""),
  91. assistant_tool_call_count=len(getattr(assistant_message, "tool_calls", None) or []),
  92. moa_references=_moa_reference_metrics_for_hook(agent),
  93. )
  94. except Exception:
  95. pass
  96. def _reasoning_preview(agent: Any, content: str, assistant_message: Any = None) -> str:
  97. """Reasoning text for the ``reasoning.available`` progress event, or ``""`` when the turn
  98. carries no reasoning at all.
  99. The structured extractor (``agent._extract_reasoning``) is authoritative: native
  100. ``reasoning`` / ``reasoning_content`` / ``reasoning_details`` first, else inline think blocks
  101. over the shared ``THINK_TAG_NAMES`` set. Only when no extractor is available do we match the
  102. inline blocks ourselves. Plain reply text is never reasoning — clients render this event as
  103. the turn's thinking block (Desktop ``message-stream.ts`` → ``appendReasoningDelta``), so
  104. forwarding the answer here prints it twice and dresses visible output up as internal
  105. reasoning (#132477, #118934, #13007)."""
  106. extractor = getattr(agent, "_extract_reasoning", None)
  107. if callable(extractor) and assistant_message is not None:
  108. extracted = extractor(assistant_message)
  109. if isinstance(extracted, str) and extracted.strip():
  110. return extracted.strip()
  111. if not content:
  112. return ""
  113. return "\n\n".join(
  114. block.strip() for block in _INLINE_REASONING_RE.findall(content) if block and block.strip()
  115. )
  116. def _relay_thinking(agent: Any, content: str, assistant_message: Any = None) -> None:
  117. """Relay the model's output to the progress callback: a subagent sends its first line to the
  118. parent display; a top-level agent with a structured callback gets ``reasoning.available``
  119. carrying extracted reasoning only — never the visible reply (#132477)."""
  120. _think_text = _REASONING_TAG_RE.sub('', (content or "").strip()).strip()
  121. first_line = _think_text.split('\n')[0][:80] if _think_text else ""
  122. if getattr(agent, '_delegate_depth', 0) > 0:
  123. # Subagent lane: an activity preview for the parent, never a reasoning block.
  124. if first_line:
  125. try:
  126. agent.tool_progress_callback("_thinking", first_line)
  127. except Exception:
  128. pass
  129. return
  130. # Native reasoning already went out as ``reasoning.delta`` for this model response (the flag
  131. # resets per response in _reset_stream_delivery_tracking); re-emitting it as
  132. # ``reasoning.available`` renders the same thinking twice (#132477).
  133. if getattr(agent, "_native_reasoning_streamed", False):
  134. return
  135. reasoning_text = _reasoning_preview(agent, content, assistant_message)
  136. if not reasoning_text:
  137. return
  138. try:
  139. agent.tool_progress_callback("reasoning.available", "_thinking", reasoning_text[:500], None)
  140. except Exception:
  141. pass
  142. def normalize_model_response(
  143. agent: Any, *, response: Any, messages: Any, api_messages: Any, conversation_history: Any,
  144. api_call_count: Any, api_duration: Any, api_start_time: Any, api_request_id: Any,
  145. effective_task_id: Any, turn_id: Any, active_system_prompt: Any = None,
  146. ) -> ResponseIntakeVerdict:
  147. """Normalize ``response`` into ``assistant_message`` (str content, never dict/list) and run
  148. the post-response hooks and continuation guards, in the original order."""
  149. assistant_message = normalize_response_for_agent(agent, response)
  150. finish_reason = assistant_message.finish_reason
  151. def _verdict(action: str, result: Optional[dict[str, Any]] = None) -> ResponseIntakeVerdict:
  152. return ResponseIntakeVerdict(
  153. action=action, assistant_message=assistant_message, finish_reason=finish_reason,
  154. result=result, active_system_prompt=active_system_prompt,
  155. )
  156. if assistant_message.content is not None and not isinstance(assistant_message.content, str):
  157. assistant_message.content = _coerce_content_text(assistant_message.content)
  158. # Agent-as-provider projection: splice the provider-agent's own tool work in as
  159. # call/result rows before this turn's assistant message; no-op for ordinary providers.
  160. splice_provider_projection(agent, response, messages)
  161. _fire_post_api_request_hook(
  162. agent, response, assistant_message, finish_reason, api_messages=api_messages,
  163. api_call_count=api_call_count, api_duration=api_duration, api_start_time=api_start_time,
  164. api_request_id=api_request_id, effective_task_id=effective_task_id, turn_id=turn_id,
  165. )
  166. from hermes_cli.observability.shared_metrics_harness import record_reply_content
  167. record_reply_content(agent, response, assistant_message)
  168. content = assistant_message.content
  169. if content and not agent.quiet_mode:
  170. if agent.verbose_logging:
  171. agent._vprint(f"{agent.log_prefix}🤖 Assistant: {content}")
  172. else:
  173. agent._vprint(f"{agent.log_prefix}🤖 Assistant: {content[:100]}{'...' if len(content) > 100 else ''}")
  174. # Gate on the callback alone, not on non-empty content: a tool-call-only turn (empty content)
  175. # still carries reasoning the preview can extract (#132477 / nekwo's note on #124031).
  176. if agent.tool_progress_callback:
  177. _relay_thinking(agent, content, assistant_message)
  178. # Incomplete <REASONING_SCRATCHPAD> (opened, never closed): the model ran out of
  179. # output tokens mid-reasoning — retry up to 2 times, then save as partial.
  180. if has_incomplete_scratchpad(content or ""):
  181. agent._incomplete_scratchpad_retries += 1
  182. agent._buffer_vprint("⚠️ Incomplete <REASONING_SCRATCHPAD> detected (opened but never closed)")
  183. if agent._incomplete_scratchpad_retries <= 2:
  184. agent._buffer_vprint(f"🔄 Retrying API call ({agent._incomplete_scratchpad_retries}/2)...")
  185. return _verdict("continue") # don't add the broken message
  186. agent._flush_status_buffer()
  187. agent._vprint(f"{agent.log_prefix}❌ Max retries (2) for incomplete scratchpad. Saving as partial.", force=True, diagnostic=True)
  188. agent._incomplete_scratchpad_retries = 0
  189. rolled_back_messages = agent._get_messages_up_to_last_assistant(messages)
  190. agent._cleanup_task_resources(effective_task_id)
  191. agent._persist_session(messages, conversation_history)
  192. return _verdict("return", partial_result(
  193. rolled_back_messages, api_call_count, "Incomplete REASONING_SCRATCHPAD after 2 retries"
  194. ))
  195. agent._incomplete_scratchpad_retries = 0
  196. if agent.api_mode == "codex_responses" and finish_reason == "incomplete":
  197. _codex_result = continue_codex_incomplete(
  198. agent, assistant_message, finish_reason, messages=messages,
  199. conversation_history=conversation_history, api_call_count=api_call_count,
  200. response=response,
  201. )
  202. if _codex_result is CODEX_FALLBACK_ACTIVATED:
  203. # The failover rewrote the Model:/Provider: identity on the cached system prompt;
  204. # rebind it so the next iteration's request is rebuilt with the new identity.
  205. from agent.conversation_loop import _sync_failover_system_message
  206. active_system_prompt = _sync_failover_system_message(agent, api_messages, active_system_prompt)
  207. return _verdict("continue")
  208. if _codex_result is not None:
  209. return _verdict("return", _codex_result)
  210. return _verdict("continue")
  211. if hasattr(agent, "_codex_incomplete_retries"):
  212. agent._codex_incomplete_retries = 0
  213. agent._codex_reasoning_only_streak = 0
  214. return _verdict("fallthrough")