The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: restore stale-base revert hunks in conversation_loop.py and moa_loop.py
kshitij1df33dccfe01
diff --git a/agent/conversation_loop.py b/agent/conversation_loop.pyindex b56e0649e71..acffc45d958 100644--- a/agent/conversation_loop.py+++ b/agent/conversation_loop.py@@ -76,6 +76,7 @@ from agent.model_metadata import (from agent.process_bootstrap import _install_safe_stdiofrom agent.prompt_caching import (build_prompt_cache_plan,+ effective_cache_ttl,strip_anthropic_cache_control,strip_anthropic_tool_cache_control,)@@ -86,6 +87,9 @@ from agent.retry_utils import (zai_coding_overload_retry_ceiling,)from agent.trajectory import has_incomplete_scratchpad+# Bind before the turn starts so a source-tree swap cannot load a skewed+# finalizer at turn end.+from agent.turn_finalizer import finalize_turnfrom agent.usage_pricing import estimate_usage_cost, normalize_usagefrom hermes_constants import PARTIAL_STREAM_STUB_IDfrom hermes_logging import set_session_context@@ -94,6 +98,71 @@ from utils import base_url_host_matches, env_var_enabledlogger = logging.getLogger(__name__)++# Scaffold marker used by _apply_active_turn_redirect and the ghost-row filter+# in the api_messages loop. Module-level so both sites can never drift.+_INTERRUPT_SCAFFOLD_MARKER = "[This response was interrupted by a user correction.]"+++def _restore_user_after_reference_handoff(+ messages: List[Dict[str, Any]], user_message: Any+) -> bool:+ """Re-append this turn's real user ask when compaction left only a handoff.++ Returns True when a restore append happened. The caller has already+ established that a reference-only handoff would drive the next model+ call (#80622); this helper only decides whether a restorable ask exists.+ """+ if user_message is None:+ return False+ if isinstance(user_message, str):+ if not user_message.strip():+ return False+ content: Any = user_message+ elif isinstance(user_message, list):+ if not user_message:+ return False+ content = user_message+ else:+ return False+ if (+ messages+ and isinstance(messages[-1], dict)+ and messages[-1].get("role") == "user"+ and messages[-1].get("content") == content+ ):+ return False+ messages.append({"role": "user", "content": content})+ return True+++def _should_skip_model_call_for_reference_handoff(+ messages: List[Dict[str, Any]], user_message: Any+) -> bool:+ """Guard post-compaction continues against sole-handoff active turns (#80622)."""+ from agent.context_compressor import reference_handoff_would_drive_next_model_call++ if not reference_handoff_would_drive_next_model_call(messages):+ return False+ if _restore_user_after_reference_handoff(messages, user_message):+ # The restored ask is an actionable non-synthetic user row appended+ # after the handoff — by construction the handoff no longer drives.+ return False+ return True+++# Fallback final_response for a turn ended by the sole-handoff skip (#80622).+# Deliberately NOT a replay of the last assistant text: finalize_turn's+# non-assistant-tail chokepoint (#43849) appends final_response as a fresh+# assistant row, so recovering the previous turn's prose here would duplicate+# it in the durable transcript AND re-deliver it to the user as if it were+# this turn's answer. A short status is honest and idempotent.+_HANDOFF_SKIP_FINAL_RESPONSE = (+ "Context was compacted. The previous response is complete — "+ "awaiting your next message."+)++# Stable prefix of the local interrupt status string emitted when a turn is# cancelled while waiting on the provider. Surfaces (ACP, TUI) match on this# to treat it as cancellation metadata rather than assistant prose.@@ -119,6 +188,34 @@ _API_CALL_MODULES = frozenset({})+def _join_truncated_parts(parts: List[str]) -> str:+ """Join continuation fragments, adding a newline where two would glue together (#78577)."""+ joined = ""+ for part in parts:+ if joined and not joined[-1].isspace() and part and not part[0].isspace():+ joined += "\n"+ joined += part+ return joined+++def _moa_reference_metrics_for_hook(agent: Any) -> Any:+ """Per-advisor metrics for post_api_request, or None off the MoA path.++ MoA runs N advisor models before its aggregator and returns only the+ aggregator's response, so an observability plugin sees one generation for+ the whole fan-out. The advisor spend is already computed per slot (see+ ``_RefAccounting``); this only carries it across the hook boundary.+ """+ client = getattr(agent, "client", None)+ getter = getattr(client, "last_reference_metrics", None)+ if not callable(getter):+ return None+ try:+ return getter()+ except Exception:+ return None++def _apply_active_turn_redirect(agent: Any, messages: List[Dict[str, Any]], text: str) -> None:"""Append a provider-safe checkpoint and correction to the live turn.@@ -147,77 +244,61 @@ def _apply_active_turn_redirect(agent: Any, messages: List[Dict[str, Any]], text``[This response was interrupted by a user correction.]`` and its``Visible response before the interruption:`` header exist so the MODELunderstands its own reply was cut off. They are not prose the user wrote- or the agent said. Persisting them into ``content`` painted the raw- machinery as an assistant bubble on every reload (and merged it into the- preceding tool-call bubble), which is what made a steered transcript- unreadable. Carry the scaffolded form in the ``api_content`` sidecar --- the exact bytes replayed to the provider -- and keep ``content`` clean.- When nothing was on screen there is no clean form at all, so the row is- marked ``display_kind="hidden"``: still replayed to the model, dropped by- every transcript surface (desktop, TUI, CLI resume), exactly like the+ or the agent said. Persisting them into an assistant row's ``content`` or+ ``api_content`` made the model treat the scaffold as *its own previous+ reply*, echo it, and self-replicate ghost rows across turns (#81841).+ Carry the scaffolded form only in the *user correction's* ``api_content``+ sidecar — never on the placeholder assistant row. When nothing was on+ screen the placeholder is marked ``display_kind="hidden"`` (empty+ content) so every transcript surface drops it, exactly likecompaction-reference rows."""visible = agent._strip_think_blocks(getattr(agent, "_current_streamed_assistant_text", "") or "").strip()- checkpoint_parts = ["[This response was interrupted by a user correction.]"]+ checkpoint_parts = [_INTERRUPT_SCAFFOLD_MARKER]if visible:checkpoint_parts.extend(["Visible response before the interruption:", visible])checkpoint = "\n\n".join(checkpoint_parts)+ correction = (+ "[Context from the interrupted assistant response]\n"+ f"{checkpoint}\n\n"+ f"{text}"+ )- # The normal live tail is user or tool, so an assistant checkpoint followed- # by the correction preserves strict alternation. If a transport already- # committed an assistant item, attribute the checkpoint inside the user- # correction instead of creating assistant→assistant.+ # The normal live tail is user or tool, so an assistant placeholder+ # followed by the correction preserves strict alternation. If a transport+ # already committed an assistant item, attribute the checkpoint inside the+ # user correction instead of creating assistant→assistant.if messages and messages[-1].get("role") == "assistant":- correction = (- "[Context from the interrupted assistant response]\n"- f"{checkpoint}\n\n"- f"{text}"- )# Transcript shows the user's own words; the provider replays the# scaffolded form so it still sees the interrupted context.messages.append({"role": "user", "content": text, "api_content": correction})else:- entry: Dict[str, Any] = {+ # Placeholder preserves role alternation only. Scaffold bytes must+ # never land here — the API replay path substitutes api_content back+ # into content, and a scaffold-as-assistant-reply is what the model+ # then echoes (#81841 / incomplete #73146 else branch).+ placeholder: Dict[str, Any] = {"role": "assistant",- "content": visible or checkpoint,- "api_content": checkpoint,+ "content": visible or "",}if not visible:- # Nothing reached the screen — this row carries no assistant prose- # at all, only the cut-off notice for the model.- entry["display_kind"] = "hidden"- messages.append(entry)- messages.append({"role": "user", "content": text})+ placeholder["display_kind"] = "hidden"+ messages.append(placeholder)+ messages.append(+ {"role": "user", "content": text, "api_content": correction}+ )agent._current_streamed_assistant_text = ""agent._stream_needs_break = True-def _moa_reference_metrics_for_hook(agent: Any) -> Any:- """Per-advisor metrics for post_api_request, or None off the MoA path.-- MoA runs N advisor models before its aggregator and returns only the- aggregator's response, so an observability plugin sees one generation for- the whole fan-out. The advisor spend is already computed per slot (see- ``_RefAccounting``); this only carries it across the hook boundary.- """- client = getattr(agent, "client", None)- getter = getattr(client, "last_reference_metrics", None)- if not callable(getter):- return None- try:- return getter()- except Exception:- return None--def _is_copilot_provider(agent: Any) -> bool:"""Delegate to ``AIAgent._is_copilot_provider`` (single owner of the check).@@ -561,6 +642,12 @@ def _restore_or_build_system_prompt(agent, system_message, conversation_history)# Continuing session — reuse the exact system prompt from the# previous turn so the Anthropic cache prefix matches.agent._cached_system_prompt = stored_prompt+ # Prompt-section callbacks are new-session-only. Recover their frozen+ # bytes from the persisted full prompt so a later compression rebuild+ # keeps them without evaluating plugin state in this resumed process.+ from agent.system_prompt import restore_plugin_prompt_sections++ restore_plugin_prompt_sections(agent, stored_prompt)# Reconstruct the cross-session-stable prefix for the early cache# breakpoint. The static prefix is not persisted (only the full# prompt is), so gateway surfaces that build a fresh AIAgent per@@ -722,11 +809,34 @@ def _stored_prompt_matches_runtime(agent, prompt: str) -> bool:return True+# The three _get_continuation_prompt variants below, in named-constant form+# so agent.context_compressor's _is_synthetic_compression_user_turn can+# recognize them by content after a crash/interrupt persists one mid-list —+# these rows carry no durable role beyond driving the retry, and SessionDB+# projection strips the _length_continuation_nudge metadata tag that marks+# them in live memory (see agent/context_compressor.py).+_LENGTH_CONTINUATION_NETWORK_STUB = (+ "[System: The previous response was cut off by a "+ "network error mid-stream. Continue exactly where "+ "you left off. Do not restart or repeat prior text. "+ "Finish the answer directly.]"+)+_LENGTH_CONTINUATION_OUTPUT_LIMIT = (+ "[System: Your previous response was truncated by the output "+ "length limit. Continue exactly where you left off. Do not "+ "restart or repeat prior text. Finish the answer directly.]"+)+# The dropped-tools variant interpolates the tool name list right after this+# prefix, so it can't be exact-matched — this stable prefix is what+# _is_synthetic_compression_user_turn checks with str.startswith instead.+_LENGTH_CONTINUATION_DROPPED_TOOLS_PREFIX = "[System: Your previous tool call "++def _get_continuation_prompt(is_partial_stub: bool, dropped_tools: Optional[List[str]] = None) -> str:if is_partial_stub and dropped_tools:tool_list = ", ".join(dropped_tools[:3])return (- "[System: Your previous tool call "+ f"{_LENGTH_CONTINUATION_DROPPED_TOOLS_PREFIX}"f"({tool_list}) was too large and ""the stream timed out before it ""could be delivered. Do NOT retry "@@ -739,18 +849,9 @@ def _get_continuation_prompt(is_partial_stub: bool, dropped_tools: Optional[List"tokens to avoid stream timeouts.]")elif is_partial_stub:- return (- "[System: The previous response was cut off by a "- "network error mid-stream. Continue exactly where "- "you left off. Do not restart or repeat prior text. "- "Finish the answer directly.]"- )+ return _LENGTH_CONTINUATION_NETWORK_STUBelse:- return (- "[System: Your previous response was truncated by the output "- "length limit. Continue exactly where you left off. Do not "- "restart or repeat prior text. Finish the answer directly.]"- )+ return _LENGTH_CONTINUATION_OUTPUT_LIMIT# Continuation nudge for Codex/Responses turns that came back with only@@ -768,6 +869,36 @@ _CODEX_INCOMPLETE_NUDGE = ()+# Re-prompt sent after a Codex/Responses turn ends with an acknowledgment-only+# reply (no tool calls, no final answer) — named so+# agent.context_compressor's _is_synthetic_compression_user_turn can+# recognize it by content the same way it recognizes _CODEX_INCOMPLETE_NUDGE.+_CODEX_ACK_CONTINUATION_NUDGE = (+ "[System: Continue now. Execute the required tool calls and only "+ "send your final answer after completing the task.]"+)++# Re-prompt sent when a provider returns finish_reason="tool_calls" with an+# empty tool_calls array (dropped-tool-call recovery, see the retry loop+# below). Named for the same reason as _CODEX_ACK_CONTINUATION_NUDGE — this+# pair is only stripped from the durable transcript once the turn reaches+# finalization; an interrupt/crash mid-retry can still persist it.+_DROPPED_TOOLCALL_NUDGE_CONTENT = (+ "Your previous turn indicated a tool call but none was "+ "included. Do not narrate a plan or restate intent — issue "+ "the actual tool call now to continue the task."+)++# Re-prompt sent when the model returns an empty response after executing tool+# calls (#9400). Named for the same reason as the nudges above — its+# _empty_recovery_synthetic metadata flag doesn't survive SessionDB projection.+_EMPTY_TOOL_RESPONSE_NUDGE = (+ "You just executed tool calls but returned an "+ "empty response. Please process the tool "+ "results above and continue with the task."+)++# Shared recovery hint appended to every content-policy refusal message. Both# the HTTP-200 refusal path (``finish_reason=content_filter``) and the# exception path (a provider moderation error classified as@@ -831,6 +962,50 @@ def _canonicalize_tool_call_arguments(arg_str: str) -> str:return canonical+def _clone_message_for_send(msg):+ """Structural clone of a history message for the per-call API copy.++ The send path builds ``api_messages`` from the persisted history and+ then rewrites the copies in place (canonicalization/repair of tool-call+ arguments, surrogate and non-ASCII sanitization, content strips, cache+ decoration). A shallow ``msg.copy()`` only decouples TOP-LEVEL fields:+ nested containers — ``tool_calls`` entries and their ``function`` dicts,+ multimodal ``content`` part lists, ``reasoning_details`` — remain the+ SAME objects the persisted history holds, so any in-place write there+ silently rewrites the stored transcript (#80498: an unrepairable+ ``write_file`` argument string was replaced with ``{}`` in the persisted+ turn, destroying the streamed file content).++ Cloning every container (dict/list) recursively while SHARING immutable+ leaves (strings, numbers, None) makes every downstream in-place+ transform safe by construction — current and future — at container-count+ cost, not string-byte cost: big argument strings and base64 image+ payloads are shared, never copied. Measured: ~1-5ms per 2000-message+ pathological build (20% multimodal, 30% tool calls) vs ~0.4ms for the+ shallow copy; compression keeps real request histories far smaller, and+ the build runs once per API call — noise next to the call itself.+ copy.deepcopy would be equally correct (CPython deepcopy also shares+ immutable str) but ~4x slower again and needs its memo machinery;+ history messages are JSON-shaped and acyclic (depth < 10 in practice;+ a >~1000-deep pathological nest would hit the recursion limit, exactly+ as deepcopy would), so cycle handling isn't needed here. Tuples are+ shared as leaves: JSON-derived message content never contains tuples,+ so a mutable container smuggled inside one is not a reachable shape on+ this path.+ """+ if isinstance(msg, dict):+ return {+ k: _clone_message_for_send(v) if isinstance(v, (dict, list)) else v+ for k, v in msg.items()+ }+ if isinstance(msg, list):+ return [+ _clone_message_for_send(v) if isinstance(v, (dict, list)) else v+ for v in msg+ ]+ return msg++def _canonicalize_api_tool_calls(api_messages) -> None:"""Canonicalize tool-call argument JSON on the send-path message copy.@@ -857,10 +1032,25 @@ def _canonicalize_api_tool_calls(api_messages) -> None:),}}except Exception:- tc["function"]["arguments"] = _repair_tool_call_arguments(- tc["function"]["arguments"],- tc["function"].get("name", "?"),- )+ # Copy-on-write here too. The send-path build now hands+ # this pass structurally-cloned messages (see+ # _clone_message_for_send), but this branch keeps its own+ # copy as defense in depth: some callers (tests, future+ # call sites) pass shallow copies, and assigning into a+ # shared ``tc["function"]`` would rewrite the stored+ # turn. On the unrepairable path the repair returns "{}",+ # so a write-through here replaced the model's real+ # arguments with an empty object in the transcript: a+ # stream that died mid ``write_file`` lost the file+ # content it had already streamed, with only a WARNING+ # to show for it (#80498).+ tc = {**tc, "function": {+ **tc["function"],+ "arguments": _repair_tool_call_arguments(+ tc["function"]["arguments"],+ tc["function"].get("name", "?"),+ ),+ }}new_tcs.append(tc)am["tool_calls"] = new_tcs@@ -1123,7 +1313,13 @@ def _redecorate_prompt_cache_for_provider(plan = build_prompt_cache_plan(messages,planned_tools,- cache_ttl=agent._cache_ttl,+ # Clamp per-destination: a configured 1h regresses to 5m on+ # Qwen/Alibaba routes, whose context cache is 5m-only (#84733).+ cache_ttl=effective_cache_ttl(+ agent._cache_ttl,+ provider=agent.provider,+ model=agent.model,+ ),native_anthropic=agent._use_native_cache_layout,static_system_prefix=static if isinstance(static, str) else None,direct_native_tool_cache=direct_tool_cache,@@ -1180,10 +1376,13 @@ def _apply_context_engine_selection(# and it may be replaced wholesale via the return value — never mutated in# place either. ``conversation_messages`` / ``incoming_message`` are# read-only context; copying enforces the request-only contract rather than- # merely documenting it.- _conv_copy = [dict(m) if isinstance(m, dict) else m for m in conversation_messages] \+ # merely documenting it. Structural clones, not dict(m): a shallow copy+ # would leave nested containers (tool_calls, content parts) aliased to+ # the persisted history, so an engine writing into them would rewrite+ # the transcript (#80498 aliasing class).+ _conv_copy = [_clone_message_for_send(m) for m in conversation_messages] \if conversation_messages is not None else None- _incoming_copy = dict(incoming_message) if isinstance(incoming_message, dict) else incoming_message+ _incoming_copy = _clone_message_for_send(incoming_message) if isinstance(incoming_message, dict) else incoming_messagetry:selected = engine.select_context(api_messages,@@ -1254,7 +1453,10 @@ def _notify_context_engine_turn_complete(try:hook(- [dict(m) if isinstance(m, dict) else m for m in messages],+ # Structural clones: on_turn_complete receives the PERSISTED+ # history; a shallow dict(m) would let a hook write through+ # nested containers into the transcript (#80498 aliasing class).+ [_clone_message_for_send(m) for m in messages],usage=usage,**meta,)@@ -1327,6 +1529,31 @@ def run_conversation(agent._last_compression_attempt_recorded = Falseagent._last_compression_attempt_in_place = None+ # If a background memory/skill review spawned at the end of a PRIOR turn+ # (agent/background_review.py) is still running its own run_conversation()+ # when THIS turn starts, cancel it now rather than letting both make+ # outbound API calls concurrently against the same session_id/credentials.+ # That concurrency can produce doubled prompt-token accounting on this+ # turn's own calls and, because the review fork is a fully separate+ # AIAgent with no route back to THIS agent's interrupt() by default, a+ # lockup that survives a normal /stop and needs a hard Ctrl+C.+ # ``review_agent.interrupt()`` is fire-and-forget here — it just flags+ # cancellation and aborts the review's in-flight socket; it does not+ # block waiting for the review's daemon thread to exit, so it can't add+ # latency to this turn. Only ever set on the real owning agent (the+ # review fork's own copy of this attribute stays None — reviews don't+ # spawn nested reviews), so this is a no-op on every other run_conversation+ # caller (subagents, the review fork itself, etc).+ _pending_review = getattr(agent, "_background_review_agent", None)+ if _pending_review is not None:+ try:+ _pending_review.interrupt("superseded by a new live turn")+ except Exception:+ logger.debug(+ "Failed to cancel in-flight background review for a new turn",+ exc_info=True,+ )+# Adopt any ~/.hermes/.env credential/base-url edits made since the last# turn — a Settings save updates .env but not this worker's client, which# was built at agent init (#67821). No-op when .env is unchanged.@@ -1383,6 +1610,11 @@ def run_conversation(# A configured SessionDB append failure halts only the affected turn. A# cached gateway agent must recover on the next message if storage did.agent._incremental_persistence_failed = False+ # Cause of the most recent persistence failure this turn ('locked',+ # 'disk', or 'unknown' — see hermes_state.classify_persistence_error).+ # Reset alongside the failure flag so a lock-contention diagnosis from a+ # previous turn can never leak into this turn's user-facing explanation.+ agent._last_persistence_error_cause = None# Main conversation loop counters (pure locals consumed by the loop below).api_call_count = 0@@ -1601,6 +1833,30 @@ def run_conversation(agent.session_id or "-",)+ # Drop legacy ghost rows from the incomplete #73146 else branch BEFORE+ # the alternation repair below: a hidden assistant placeholder whose+ # content/api_content is the raw interrupt scaffold. Replaying that as+ # an assistant message makes the model echo it and self-replicate+ # (#81841). Dropping before repair lets repair_message_sequence fix+ # any user→user adjacency the filter creates.+ messages = [+ msg for msg in messages+ if not (+ msg.get("display_kind") == "hidden"+ and msg.get("role") == "assistant"+ and (+ (+ isinstance(msg.get("content"), str)+ and msg["content"].strip() == _INTERRUPT_SCAFFOLD_MARKER+ )+ or (+ isinstance(msg.get("api_content"), str)+ and msg["api_content"].strip() == _INTERRUPT_SCAFFOLD_MARKER+ )+ )+ )+ ]+# Defensive: repair malformed role-alternation before API call.# Catches cases where the history got wedged into a# ``tool → user`` or ``user → user`` tail (e.g. after empty-@@ -1622,7 +1878,13 @@ def run_conversation(api_messages = []for idx, msg in enumerate(messages):- api_msg = msg.copy()++ # Structural clone, NOT msg.copy(): every in-place transform+ # below (canonicalize/repair, surrogate + non-ASCII sanitizers,+ # cache decoration) must be unable to reach the persisted+ # history through shared nested containers. See+ # _clone_message_for_send.+ api_msg = _clone_message_for_send(msg)# api_content is the persistence sidecar carrying the exact bytes# sent to the API for this message when they differ from the clean@@ -1694,8 +1956,11 @@ def run_conversation(# Remove finish_reason - not accepted by strict APIs (e.g. Mistral)if "finish_reason" in api_msg:api_msg.pop("finish_reason")- # Strip internal thinking-prefill marker- api_msg.pop("_thinking_prefill", None)+ # _thinking_prefill survives here intentionally: the drop pass below+ # needs it. The transport strips all underscore keys before the wire.+ # Strip length-continuation marks; not every transport drops underscore keys.+ api_msg.pop("_length_continuation_fragment", None)+ api_msg.pop("_length_continuation_nudge", None)# Strip Codex Responses API fields (call_id, response_item_id) for# strict providers like Mistral, Fireworks, etc. that reject unknown fields.# Uses new dicts so the internal messages list retains the fields@@ -1804,7 +2069,11 @@ def run_conversation(if agent.prefill_messages:sys_offset = 1 if (api_messages and api_messages[0].get("role") == "system") else 0for idx, pfm in enumerate(agent.prefill_messages):- api_messages.insert(sys_offset + idx, pfm.copy())+ # Structural clone: the sanitizers below run over+ # api_messages in place, and a shallow copy would let them+ # write through into agent.prefill_messages' nested+ # containers (same aliasing class as the history build).+ api_messages.insert(sys_offset + idx, _clone_message_for_send(pfm))# Per-turn context selection hook (additive, no-op by default).# Lets a context engine select/replace which context enters the@@ -1893,7 +2162,13 @@ def run_conversation(_initial_cache_plan = build_prompt_cache_plan(api_messages,tools_for_api,- cache_ttl=agent._cache_ttl,+ # Clamp per-destination: a configured 1h regresses to 5m on+ # Qwen/Alibaba routes, whose context cache is 5m-only (#84733).+ cache_ttl=effective_cache_ttl(+ agent._cache_ttl,+ provider=agent.provider,+ model=agent.model,+ ),native_anthropic=agent._use_native_cache_layout,static_system_prefix=(_static_system_prefix@@ -1938,6 +2213,15 @@ def run_conversation(_estimate_tools_tokens_rough(agent.tools) if agent.tools else 0)total_chars = approx_tokens * 4+ # Stash this request's rough estimate so update_from_response() can+ # pair it with the provider's real prompt count — the (rough, real)+ # anchor behind should_defer_preflight_to_real_usage()'s projection.+ # getattr guard: test doubles built via object.__new__ lack the method.+ _note_rough = getattr(+ agent.context_compressor, "note_request_rough_estimate", None+ )+ if callable(_note_rough):+ _note_rough(request_pressure_tokens)_runtime_context_error = _ollama_context_limit_error(agent, request_pressure_tokens@@ -2104,9 +2388,30 @@ def run_conversation(conversation_history = conversation_history_after_compression(agent, messages, conversation_history)+ # This preflight iteration never reaches the provider whether+ # we skip the turn (handoff guard below) or re-run the loop —+ # refund the consumed call/budget in BOTH cases, mirroring the+ # ollama_runtime_context_too_small early-exit above. Without+ # the refund on the break path, every skipped turn leaked one+ # iteration-budget unit for the agent's lifetime and+ # finalize_turn logged an api_call_count including a call that+ # was never made.api_call_count -= 1agent._api_call_count = api_call_countagent.iteration_budget.refund()+ if _should_skip_model_call_for_reference_handoff(+ messages, user_message+ ):+ # Reference-only handoff must not become the active turn+ # after a completed assistant response (#80622).+ logger.info(+ "Skipping post-compaction model call: reference-only "+ "handoff would be the sole active user turn (#80622)"+ )+ if not final_response:+ final_response = _HANDOFF_SKIP_FINAL_RESPONSE+ _turn_exit_reason = "compaction_handoff_not_actionable"+ breakcontinueelif (agent.compression_enabled@@ -2202,7 +2507,8 @@ def run_conversation(retry_count = 0compression_attempts = 0_retry.primary_recovery_attempted = False- continue+ _retry.restart_with_rebuilt_messages = True+ break# No fallback available — surface buffered context# so user sees the rate-limit message that led here.agent._flush_status_buffer()@@ -2255,6 +2561,16 @@ def run_conversation(api_messages,tools_for_api=tools_for_api,)+ # Outbound-request surrogate chokepoint (#50959): the messages+ # were scrubbed above, but the rest of the request body —+ # tool/function descriptions (session_search's ±-heavy text is+ # the recorded repro), extra_body, system strings routed via+ # kwargs — can still carry invalid code points that providers+ # reject with a non-retryable HTTP 400 ("invalid unicode code+ # point"). One in-place walk here guarantees the entire+ # payload json.dumps()-safe regardless of which leaf produced+ # the string. Fast no-op when the payload is clean.+ _sanitize_structure_surrogates(api_kwargs)if agent._force_ascii_payload:_sanitize_structure_non_ascii(api_kwargs)if agent.api_mode == "codex_responses":@@ -2306,13 +2622,6 @@ def run_conversation(request_messages = api_kwargs.get("input")if not isinstance(request_messages, list):request_messages = api_messages- # Anthropic (``system``) and Responses/Codex- # (``instructions``) move the system prompt out of- # messages; pass it explicitly for observability- # plugins (Langfuse).- system_prompt_for_hooks = _system_prompt_for_hooks(- api_kwargs, request_messages- )# Shallow-copy the outer list so plugins that retain the# reference for async snapshotting don't observe later# mutations of api_messages. The inner dicts are not@@ -2330,6 +2639,13 @@ def run_conversation(# provider client. New consumers should read the# sanitised view from ``request["body"]["messages"]``._request_payload = agent._api_request_payload_for_hook(api_kwargs)+ # Anthropic (``system``) and Responses/Codex+ # (``instructions``) move the system prompt out of+ # messages; pass it explicitly for observability+ # plugins (Langfuse).+ system_prompt_for_hooks = _system_prompt_for_hooks(+ api_kwargs, request_messages+ )_invoke_hook("pre_api_request",task_id=effective_task_id,@@ -2652,7 +2968,8 @@ def run_conversation(retry_count = 0compression_attempts = 0_retry.primary_recovery_attempted = False- continue+ _retry.restart_with_rebuilt_messages = True+ break# Check for error field in response (some providers include this)error_msg = "Unknown"@@ -2725,7 +3042,8 @@ def run_conversation(retry_count = 0compression_attempts = 0_retry.primary_recovery_attempted = False- continue+ _retry.restart_with_rebuilt_messages = True+ break# Terminal — flush buffered retry trace so user sees what happened.agent._flush_status_buffer()agent._emit_status(f"❌ Max retries ({max_retries}) exceeded for invalid responses. Giving up.")@@ -2902,7 +3220,8 @@ def run_conversation(retry_count = 0compression_attempts = 0_retry.primary_recovery_attempted = False- continue+ _retry.restart_with_rebuilt_messages = True+ breakagent._flush_status_buffer()_refusal_log = (@@ -2944,8 +3263,8 @@ def run_conversation(if finish_reason == "length":if getattr(response, "id", "") == PARTIAL_STREAM_STUB_ID:agent._vprint(- f"{agent.log_prefix}⚠️ Stream interrupted by network error "- f"(finish_reason='length' on partial-stream-stub)",+ f"{agent.log_prefix}⚠️ Response truncated — stream "+ f"ended before completion",force=True,)else:@@ -3072,6 +3391,11 @@ def run_conversation(# gets a coherent continuation point.if truncated_response_parts:messages = agent._get_messages_up_to_last_assistant(messages)+ # Unmark survivors: their text left the stitched partial.+ for _frag in messages:+ if isinstance(_frag, dict):+ _frag.pop("_length_continuation_fragment", None)+ _frag.pop("_length_continuation_nudge", None)agent._session_messages = messageslength_continue_retries = 0truncated_response_parts = []@@ -3108,6 +3432,8 @@ def run_conversation()if not _is_empty_partial_stub:interim_msg = agent._build_assistant_message(assistant_message, finish_reason)+ # Marked so the ceiling exit can drop the fragment trail.+ interim_msg["_length_continuation_fragment"] = Truemessages.append(interim_msg)if assistant_message.content:truncated_response_parts.append(assistant_message.content)@@ -3146,13 +3472,45 @@ def run_conversation(continue_msg = {"role": "user","content": _continue_content,+ "_length_continuation_nudge": True,}messages.append(continue_msg)agent._session_messages = messages_retry.restart_with_length_continuation = Truebreak- partial_response = agent._strip_think_blocks("".join(truncated_response_parts)).strip()+ partial_response = agent._strip_think_blocks(_join_truncated_parts(truncated_response_parts)).strip()+ if partial_response:+ agent._vprint(+ f"{agent.log_prefix}⚠️ Response still truncated "+ f"after 4 continuation attempts — keeping the "+ f"partial response received so far.",+ force=True,+ )+ # Unanswered continue nudges made every later turn re-truncate.+ _turn_start = (+ current_turn_user_idx + 1+ if isinstance(current_turn_user_idx, int)+ and current_turn_user_idx >= 0+ else 0+ )+ messages[_turn_start:] = [+ m for m in messages[_turn_start:]+ if not (+ isinstance(m, dict)+ and (+ m.get("_length_continuation_fragment")+ or m.get("_length_continuation_nudge")+ )+ )+ ]+ if partial_response:+ messages.append({+ "role": "assistant",+ "content": partial_response,+ "finish_reason": "length",+ })+ agent._session_messages = messagesagent._cleanup_task_resources(effective_task_id)agent._persist_session(messages, conversation_history)return {@@ -4188,6 +4546,39 @@ def run_conversation()continue+ # ── Native compaction rejection recovery ──────────────+ # Provider explicitly rejected the ``context_management``+ # field (structured 400 naming the param). One-shot: turn+ # native compaction off for the rest of the session and+ # retry — the next _build_api_kwargs re-resolves the gate+ # and omits the field, and Hermes' local compression takes+ # over as the sole owner. Generic 4xx/5xx/timeouts do NOT+ # match (see is_native_compaction_rejection) and take the+ # normal retry path.+ if (+ agent.api_mode == "codex_responses"+ and not _retry.native_compaction_reject_retry_attempted+ and bool(getattr(agent, "codex_responses_native_compaction", False))+ ):+ from agent.native_compaction import is_native_compaction_rejection+ if is_native_compaction_rejection(+ api_error, getattr(api_error, "status_code", None)+ ):+ _retry.native_compaction_reject_retry_attempted = True+ agent.codex_responses_native_compaction = False+ agent._vprint(+ f"{agent.log_prefix}⚠️ Provider rejected native compaction "+ f"(context_management) — disabled for this session, "+ f"local compression stays active. Retrying...",+ force=True,+ )+ logger.warning(+ "%sNative compaction rejection recovery: disabled "+ "codex_responses_native for this session and retrying",+ agent.log_prefix,+ )+ continue+# ── llama.cpp grammar-parse recovery ──────────────────# llama.cpp's ``json-schema-to-grammar`` converter rejects# regex escape classes (``\d``, ``\w``, ``\s``) and most@@ -4533,7 +4924,8 @@ def run_conversation(retry_count = 0compression_attempts = 0_retry.primary_recovery_attempted = False- continue+ _retry.restart_with_rebuilt_messages = True+ break# ── Auth-failure provider failover ───────────────────────# A 401/403 that survives the per-provider credential-refresh@@ -4566,7 +4958,8 @@ def run_conversation(retry_count = 0compression_attempts = 0_retry.primary_recovery_attempted = False- continue+ _retry.restart_with_rebuilt_messages = True+ break# ── Nous Portal: record rate limit & skip retries ─────# When Nous returns a 429 that is a genuine account-@@ -5173,7 +5566,8 @@ def run_conversation(retry_count = 0compression_attempts = 0_retry.primary_recovery_attempted = False- continue+ _retry.restart_with_rebuilt_messages = True+ breakif api_kwargs is not None:agent._dump_api_request_debug(api_kwargs, reason="non_retryable_client_error", error=api_error,@@ -5396,7 +5790,8 @@ def run_conversation(retry_count = 0compression_attempts = 0_retry.primary_recovery_attempted = False- continue+ _retry.restart_with_rebuilt_messages = True+ break# Terminal — flush buffered retry/fallback trace.agent._flush_status_buffer()_final_summary = agent._summarize_api_error(api_error)@@ -5685,12 +6080,26 @@ def run_conversation(# to fit the context window.retry_count += 1_retry.restart_with_compressed_messages = False+ if _should_skip_model_call_for_reference_handoff(+ messages, user_message+ ):+ logger.info(+ "Skipping compressed-restart model call: reference-only "+ "handoff would be the sole active user turn (#80622)"+ )+ if not final_response:+ final_response = _HANDOFF_SKIP_FINAL_RESPONSE+ _turn_exit_reason = "compaction_handoff_not_actionable"+ break# In-loop compression rebuilt `messages` with fresh compaction# copies, so the pre-compression current-turn index is stale.# Re-anchor exactly like the prologue does: a stale index that# lands on a historical user message would make the live-compose# fallback inject this turn's prefetch into that message on the# wire only, diverging the next turn's replayed prefix there.+ # Ordered AFTER the handoff guard: the guard may have re-appended+ # this turn's real user ask (restore path), and the anchor must+ # land on that restored row, not on -1 / a pre-restore index.current_turn_user_idx = reanchor_current_turn_user_idx(messages, user_message)@@ -5698,14 +6107,20 @@ def run_conversation(continueif _retry.restart_with_rebuilt_messages:- # A content-filter stream stall (#32421) was escalated to the- # fallback chain and the partial content rolled back. Re-issue- # the API call against the now-active fallback provider. Refund- # the budget/count for the stalled attempt so the fallback gets a- # fair turn.+ # A stream stall or provider failure was escalated to the+ # fallback chain (10 activation sites in the retry loop set this+ # flag and break here). Re-issue the API call against the+ # now-active fallback provider. Refund the budget/count for the+ # stalled attempt so the fallback gets a fair turn.api_call_count -= 1agent.iteration_budget.refund()_retry.restart_with_rebuilt_messages = False+ # Failover shrank the compressor's context window to the+ # fallback's; clear the preflight block so the pre-API preflight+ # re-runs against the new threshold before the first fallback+ # call (#84733). Hoisted here (the single consumer) so every+ # activation site — including ones added later — gets it.+ _preflight_compression_blocked = Falsecontinueif _retry.restart_with_length_continuation:@@ -5925,7 +6340,20 @@ def run_conversation("codex_message_items",):if _key in interim_msg:- last_msg[_key] = interim_msg[_key]+ if _key == "codex_reasoning_items":+ # Merge instead of overwrite: a native+ # compaction checkpoint captured on the+ # earlier incomplete response is the only+ # copy — the continuation won't re-emit+ # it. See merge_interim_reasoning_items.+ from agent.native_compaction import (+ merge_interim_reasoning_items,+ )+ last_msg[_key] = merge_interim_reasoning_items(+ last_msg.get(_key), interim_msg[_key]+ )+ else:+ last_msg[_key] = interim_msg[_key]else:messages.append(interim_msg)agent._emit_interim_assistant_message(interim_msg)@@ -6372,6 +6800,10 @@ def run_conversation()except Exception as exc:_tool_turn_persisted = False+ from hermes_state import classify_persistence_error+ agent._last_persistence_error_cause = (+ classify_persistence_error(exc)+ )logger.warning("Incremental tool-call persistence failed before execution ""(session=%s): %s",@@ -6384,6 +6816,10 @@ def run_conversation(# run side-effecting tools from state that exists only in# this process. Breaking also avoids retrying the same# unpersisted turn until the iteration budget is exhausted.+ # The flush may have classified the cause internally; if+ # nothing was recorded, the cause is genuinely unknown.+ if getattr(agent, "_last_persistence_error_cause", None) is None:+ agent._last_persistence_error_cause = "unknown"_turn_exit_reason = "session_persistence_failed"final_response = ""failed = True@@ -6537,6 +6973,18 @@ def run_conversation(conversation_history = conversation_history_after_compression(agent, messages, conversation_history)+ if _should_skip_model_call_for_reference_handoff(+ messages, user_message+ ):+ logger.info(+ "Skipping post-tool compaction model call: "+ "reference-only handoff would be the sole "+ "active user turn (#80622)"+ )+ if not final_response:+ final_response = _HANDOFF_SKIP_FINAL_RESPONSE+ _turn_exit_reason = "compaction_handoff_not_actionable"+ breakelif agent.compression_enabled:# Over threshold but compression is blocked (summary-LLM# cooldown or anti-thrashing). Surface a deduped warning so@@ -6584,14 +7032,12 @@ def run_conversation(# Standard no-op caller contract: only commit when the# engine returned a NEW list object with a non-zero count.if _pruned_n and _pruned_msgs is not messages:- # Do NOT rebuild conversation_history here. Unlike the- # compression branch, the prune neither rotates the session- # nor calls archive_and_compact(), so there is no new- # persistence baseline to establish. _prune_old_tool_results- # returns per-message copies that preserve the+ # Do NOT rebuild conversation_history here. The compressor+ # atomically rewrites the active transcript with the durable+ # rearm threshold, then stamps every returned row with# _DB_PERSISTED_MARKER, so the marker-based flush dedup (see- # _flush_messages_to_session_db) already prevents both- # duplicate writes and dropped rows. Calling+ # _flush_messages_to_session_db) prevents duplicate writes.+ # Calling# conversation_history_after_compression (a compaction-only# helper keyed on the _last_compaction_in_place flag) would be# a no-op at best, and on a stale in-place flag could seed@@ -6745,11 +7191,7 @@ def run_conversation(messages.append(_nudge_msg)messages.append({"role": "user",- "content": (- "You just executed tool calls but returned an "- "empty response. Please process the tool "- "results above and continue with the task."- ),+ "content": _EMPTY_TOOL_RESPONSE_NUDGE,"_empty_recovery_synthetic": True,})continue@@ -6879,6 +7321,15 @@ def run_conversation("now using %s on %s",agent.model, agent.provider,)+ # This site sits directly in the OUTER iteration+ # loop (not the retry loop), so `continue` already+ # restarts the iteration and re-runs the pre-API+ # preflight against the fallback's context window+ # (#84733). A `break` here would exit the outer+ # loop and end the turn without ever calling the+ # fallback. Clear the preflight block so the+ # re-run isn't skipped.+ _preflight_compression_blocked = Falsecontinue# Exhausted retries and fallback chain (or no@@ -6982,10 +7433,7 @@ def run_conversation(continue_msg = {"role": "user",- "content": (- "[System: Continue now. Execute the required tool calls and only "- "send your final answer after completing the task.]"- ),+ "content": _CODEX_ACK_CONTINUATION_NUDGE,}messages.append(continue_msg)agent._session_messages = messages@@ -6998,9 +7446,14 @@ def run_conversation(codex_ack_continuations = 0if truncated_response_parts:- final_response = "".join(truncated_response_parts) + final_response+ final_response = _join_truncated_parts([*truncated_response_parts, final_response])truncated_response_parts = []length_continue_retries = 0+ # The continuation recovered, so the fragments stay in the transcript.+ for _frag in messages:+ if isinstance(_frag, dict):+ _frag.pop("_length_continuation_fragment", None)+ _frag.pop("_length_continuation_nudge", None)final_response = agent._strip_think_blocks(final_response).strip()@@ -7048,11 +7501,7 @@ def run_conversation(messages.append(final_msg)messages.append({"role": "user",- "content": (- "Your previous turn indicated a tool call but none was "- "included. Do not narrate a plan or restate intent — issue "- "the actual tool call now to continue the task."- ),+ "content": _DROPPED_TOOLCALL_NUDGE_CONTENT,"_dropped_toolcall_nudge": True,})agent._session_messages = messages@@ -7253,7 +7702,24 @@ def run_conversation(continuemessages.append(final_msg)-+ # Make the completed answer durable before leaving the loop —+ # a session torn down before finalize_turn's _persist_session+ # otherwise loses a reply the user already saw (#81641). Same+ # contract as the tool-call exit (#49045) and the verify exits+ # above; _DB_PERSISTED_MARKER keeps _persist_session idempotent.+ # Unlike the tool-call exit, failure must NOT abort the turn:+ # no side effect follows and _persist_session retries the write.+ # Full incident narrative: tests/run_agent/test_81641_*.py.+ try:+ agent._flush_messages_to_session_db(messages, conversation_history)+ except Exception:+ logger.warning(+ "final text-turn flush failed (session=%s) — reply is "+ "not yet durable; relying on finalize_turn retry",+ getattr(agent, "session_id", None) or "none",+ exc_info=True,+ )+_turn_exit_reason = f"text_response(finish_reason={finish_reason})"if not agent.quiet_mode:agent._safe_print(f"🎉 Conversation completed after {api_call_count} OpenAI-compatible API call(s)")@@ -7357,7 +7823,6 @@ def run_conversation(# Post-loop turn finalization extracted to agent/turn_finalizer.finalize_turn# (god-file decomposition Phase 1 step 4). Behavior-neutral: the assembled# result dict is returned exactly as before.- from agent.turn_finalizer import finalize_turnreturn finalize_turn(agent,final_response=final_response,diff --git a/agent/moa_loop.py b/agent/moa_loop.pyindex 0f9502d5395..d783afd99b6 100644--- a/agent/moa_loop.py+++ b/agent/moa_loop.py@@ -410,6 +410,7 @@ def _maybe_apply_moa_cache_control(runtime: dict[str, Any],*,cache_disabled: bool | None = None,+ cache_ttl: str | None = None,) -> list[dict[str, Any]]:"""Decorate an advisor or aggregator request with cache_control when itsroute honors it.@@ -427,13 +428,20 @@ def _maybe_apply_moa_cache_control(``cache_disabled`` (or the live config when omitted) is stamped onto thepolicy stub so ``prompt_caching.cache_ttl: off`` is not bypassed by theblank-agent pattern (#76085).++ ``cache_ttl`` threads the agent's configured tier (default ``5m``) so+ advisor/synthesis requests stop regressing 1h → 5m; it is clamped+ per-destination by :func:`effective_cache_ttl` (Qwen → 5m, #84733)."""try:from agent.agent_runtime_helpers import (anthropic_prompt_cache_policy,blank_cache_policy_stub,)- from agent.prompt_caching import apply_anthropic_cache_control+ from agent.prompt_caching import (+ apply_anthropic_cache_control,+ effective_cache_ttl,+ )# Prefer an explicit kwarg, then a snapshot on the runtime dict# (threaded from the live agent), else config via the stub factory.@@ -454,7 +462,15 @@ def _maybe_apply_moa_cache_control(if not should_cache:return messagesreturn apply_anthropic_cache_control(- messages, native_anthropic=native_layout+ messages,+ cache_ttl=effective_cache_ttl(+ # effective_cache_ttl resolves None → "5m"; the should_cache+ # gate above already returned for cache-disabled routes.+ cache_ttl,+ provider=runtime.get("provider") or "",+ model=runtime.get("model") or "",+ ),+ native_anthropic=native_layout,)except Exception as exc: # pragma: no cover - decoration must never break a calllogger.debug("MoA cache_control decoration skipped: %s", exc)@@ -470,6 +486,7 @@ def _run_reference(reference_timeout: float | None = None,context_length_cache: Any = None,cache_disabled: bool | None = None,+ cache_ttl: str | None = None,) -> tuple[str, str, Any]:"""Call one reference model and return ``(label, text, accounting)``.@@ -536,7 +553,9 @@ def _run_reference(cache_runtime = runtimeif cache_disabled is not None:cache_runtime = {**runtime, "_cache_disabled": cache_disabled}- messages = _maybe_apply_moa_cache_control(messages, cache_runtime)+ messages = _maybe_apply_moa_cache_control(+ messages, cache_runtime, cache_ttl=cache_ttl+ )# Per-slot max_tokens takes precedence over the preset-level# reference_max_tokens passed in by the caller. This lets each# reference model have its own output cap independently.@@ -843,6 +862,10 @@ def _run_references_parallel(cache_disabled = (getattr(agent, "_cache_disabled", None) if agent is not None else None)+ # Thread the agent's configured cache TTL into every advisor request so+ # the fan-out stops regressing 1h → 5m (#84733); the destination-aware+ # clamp (Qwen → 5m) runs inside _maybe_apply_moa_cache_control.+ cache_ttl = getattr(agent, "_cache_ttl", None) if agent is not None else Nonetry:for idx, slot in enumerate(reference_models):if slot.get("provider") == "moa":@@ -862,6 +885,7 @@ def _run_references_parallel(reference_timeout=reference_timeout,context_length_cache=_ctx_len_cache,cache_disabled=cache_disabled,+ cache_ttl=cache_ttl,)] = idx@@ -1322,6 +1346,9 @@ def aggregate_moa_context(**agg_runtime,"_cache_disabled": _agg_cache_disabled,}+ # Thread the agent's configured cache TTL into the synthesis decoration+ # so the one-shot /moa path stops regressing 1h → 5m (#84733).+ _agg_cache_ttl = getattr(agent, "_cache_ttl", None) if agent is not None else Nonetry:# Same cache_control decoration as _run_reference's advisor calls# (see _maybe_apply_moa_cache_control) — this synthesis call is a@@ -1334,7 +1361,9 @@ def aggregate_moa_context(# breakpoints, even when the resolved aggregator slot is a# cache-honoring route (e.g. Claude on OpenRouter/native Anthropic).agg_messages = _maybe_apply_moa_cache_control(- [{"role": "user", "content": synth_prompt}], agg_cache_runtime+ [{"role": "user", "content": synth_prompt}],+ agg_cache_runtime,+ cache_ttl=_agg_cache_ttl,)response = call_llm(task="moa_aggregator",@@ -1764,6 +1793,13 @@ class MoAChatCompletions:api_mode=agg_runtime.get("api_mode") or "",model=agg_runtime.get("model") or "",cache_disabled=_cache_disabled,+ # Thread the agent's configured TTL and stable system prefix+ # into the aggregator plan so MoA stops regressing 1h → 5m and+ # marking the whole system prompt as one breakpoint (#84733).+ cache_ttl=getattr(_agent, "_cache_ttl", None),+ static_system_prefix=getattr(+ _agent, "_cached_system_prompt_static", None+ ),)if guidance:_attach_reference_guidance(agg_messages, str(guidance))