turn_iteration_prep.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. """Outer-iteration bookkeeping for the conversation turn loop, in call order:
  2. ``begin_iteration`` (pending redirect, interrupt / review-budget / iteration-budget exits),
  3. ``prepare_iteration`` (``agent:step`` callback, skill-nudge counter, pre-API ``/steer`` drain as a
  4. standalone user row after the newest tool result, run-budget wrap-up notice, tool_call
  5. argument sanitization, interrupt-placeholder ghost-row neutralisation, role-alternation repair),
  6. ``announce_api_call`` (verbose summary / quiet spinner) and, after the retry loop,
  7. ``apply_retry_restarts`` (consumes the ``TurnRetryState`` restart flags). Nothing here
  8. imports ``agent.conversation_loop`` at module level (cycle)."""
  9. from __future__ import annotations
  10. import logging
  11. import random
  12. import sys
  13. from contextlib import suppress
  14. from dataclasses import dataclass
  15. from typing import Any, Dict, List, Tuple
  16. from agent.display import KawaiiSpinner
  17. from agent.interrupt_control import interrupt_issuer, interrupted_during_api_call_reason
  18. from agent.turn_context_compaction import _reanchor
  19. from agent.turn_truncation import boosted_output_cap
  20. logger = logging.getLogger("agent.conversation_loop")
  21. def _anchors_current_turn(messages: Any, idx: Any, user_message: Any) -> bool:
  22. """True when ``messages[idx]`` is this turn's user row (verbatim, or its user-originated view)."""
  23. if not isinstance(idx, int) or not 0 <= idx < len(messages):
  24. return False
  25. msg = messages[idx]
  26. if not (isinstance(msg, dict) and msg.get("role") == "user"):
  27. return False
  28. if msg.get("content") == user_message:
  29. return True
  30. from agent.context_compressor import user_originated_turn_view
  31. view = user_originated_turn_view(msg)
  32. return view is not None and view.get("content") == user_message
  33. ITERATION_BUDGET_WARNING_TEMPLATE = (
  34. "[SYSTEM NOTICE — iteration budget checkpoint] You have used {used} of {maximum} "
  35. "iterations. Checkpoint durable progress now, then continue the task; do not stop "
  36. "solely because of this warning."
  37. )
  38. def _maybe_inject_iteration_budget_warning(agent: Any, messages: Any) -> bool:
  39. """Append the opt-in one-shot warning to the newest tool result."""
  40. import os
  41. from agent.delegation_context import is_dispatcher_owned_worker_context
  42. # Cancellation results still need persistence, but must not urge more work.
  43. if getattr(agent, "_interrupt_requested", False):
  44. return False
  45. ratio = getattr(agent, "budget_warning_ratio", None)
  46. kanban_worker = (
  47. bool(os.environ.get("HERMES_KANBAN_TASK"))
  48. and is_dispatcher_owned_worker_context()
  49. and "kanban_complete" in getattr(agent, "valid_tool_names", ())
  50. )
  51. if ratio is None and kanban_worker:
  52. ratio = 0.9
  53. budget = getattr(agent, "iteration_budget", None)
  54. if (
  55. ratio is None
  56. or budget is None
  57. or budget.max_total <= 1
  58. or budget.max_total >= sys.maxsize
  59. or getattr(agent, "_iteration_budget_warning_injected", False)
  60. or budget.used < min(ratio * budget.max_total, budget.max_total - 1)
  61. ):
  62. return False
  63. notice = ITERATION_BUDGET_WARNING_TEMPLATE.format(
  64. used=budget.used, maximum=budget.max_total
  65. )
  66. if kanban_worker:
  67. notice += (
  68. " While tools are still available, call kanban_complete only if all task "
  69. "requirements are verified, or kanban_request_review if it is ready for "
  70. "review; otherwise persist a kanban_comment handoff and "
  71. "continue. A diff or commit alone is not completion evidence."
  72. )
  73. # Only the current tool-result tail is mutable; an older turn may already be cached.
  74. from agent.context_compressor import _DB_PERSISTED_MARKER
  75. if (not messages or messages[-1].get("role") != "tool"
  76. or messages[-1].get(_DB_PERSISTED_MARKER)):
  77. return False
  78. message = messages[-1]
  79. content = message.get("content", "")
  80. if isinstance(content, str):
  81. message["content"] = content + f"\n\n{notice}"
  82. elif isinstance(content, list) or content is None:
  83. message["content"] = [*(content or []), {"type": "text", "text": notice}]
  84. else:
  85. return False
  86. agent._iteration_budget_warning_injected = True
  87. return True
  88. @dataclass
  89. class IterationPrep:
  90. """Always ``action == "fallthrough"``. ``messages`` is the (possibly filtered) transcript
  91. and ``request_logger`` the per-request logger the caller keeps using."""
  92. action: str
  93. messages: Any
  94. request_logger: Any
  95. current_turn_user_idx: Any
  96. # Retire hidden interrupt placeholders whose text the model echoes on replay
  97. # (#81841 for the scaffold, #132949 for the placeholder). A hidden row still
  98. # reaches the provider as assistant content; a natural-language phrase in that
  99. # position is reproduced verbatim. The row is never dropped: removal can form
  100. # ``tool -> user`` (#48879) or ``user -> user``, which repair then merges —
  101. # losing the second row's checkpoint ``api_content``. Neutralise a copy instead.
  102. def _neutralise_replay_echo_ghosts(seq: list[dict[str, Any]]) -> tuple[list[dict[str, Any]], int]:
  103. """(new list, neutralised); never mutates ``seq`` or its row dicts."""
  104. from agent.agent_runtime_helpers_placeholders import (
  105. _INTERRUPTED_PLACEHOLDER,
  106. _LEGACY_INTERRUPTED_PLACEHOLDER,
  107. )
  108. from agent.conversation_loop import _INTERRUPT_SCAFFOLD_MARKER
  109. # Retire only spellings that predate the #132949 wording change: the scaffold
  110. # (#81841) and the old placeholder, both reproduced verbatim. The current
  111. # placeholder is the post-fix value — it stays (this is also what
  112. # neutralisation rewrites to, so the rewrite does not retrigger the filter).
  113. hazards = {_INTERRUPT_SCAFFOLD_MARKER, _LEGACY_INTERRUPTED_PLACEHOLDER}
  114. neutralised = 0
  115. out: list[dict[str, Any]] = []
  116. for m in seq:
  117. if (
  118. m.get("display_kind") == "hidden" and m.get("role") == "assistant"
  119. and any(isinstance(m.get(k), str) and m[k].strip() in hazards for k in ("content", "api_content"))
  120. ):
  121. m = {**m, "content": "", "api_content": _INTERRUPTED_PLACEHOLDER}
  122. neutralised += 1
  123. out.append(m)
  124. return (out if neutralised else seq), neutralised
  125. def prepare_iteration(
  126. agent: Any, *, messages: Any, api_call_count: Any, user_message: Any = None, current_turn_user_idx: Any = None,
  127. ) -> IterationPrep:
  128. """Prepare ``messages`` for this iteration in the original order. Every mutation here is
  129. cache-safe by construction: steer text is appended as a new (not yet persisted) user row, the ghost-row
  130. filter neutralises copies of hidden interrupt placeholders, and repair runs BEFORE the request build."""
  131. from agent.conversation_loop import _maybe_inject_run_budget_wrapup
  132. # nous.anthropic_wire=auto: a wire switch decided from the previous response lands here,
  133. # before this iteration's request is built and with nothing in flight.
  134. if getattr(agent, "_nous_wire_pending", None):
  135. from agent.nous_wire import apply_pending_wire_switch
  136. apply_pending_wire_switch(agent)
  137. # Fire step_callback for gateway hooks (agent:step event).
  138. if agent.step_callback is not None:
  139. try:
  140. agent.step_callback(api_call_count, _previous_tool_round(messages))
  141. except Exception as _step_err:
  142. logger.debug("step_callback error (iteration %s): %s", api_call_count, _step_err)
  143. # Tool-calling iterations for the skill nudge; resets whenever skill_manage is used.
  144. if agent._skill_nudge_interval > 0 and "skill_manage" in agent.valid_tool_names:
  145. agent._iters_since_skill += 1
  146. # Nous agent keys live ~1 h and a single turn can run for hours: adopt the keepalive's fresh
  147. # key before the one in hand expires (local JWT exp read; no network unless inside the skew)
  148. # instead of letting this iteration's request 401. With many agents sharing the hour that
  149. # 401 was a storm, and the pool benched the sole credential for all of them.
  150. agent._adopt_nous_key_before_expiry()
  151. # Drain a /steer sent during the last API call so it lands THIS iteration. Delivered as a
  152. # standalone user row after the newest tool result (never smeared onto the tool row: that
  153. # row is already persisted append-only, so replay would diverge from the live request and
  154. # break the prompt cache — same contract as apply_pending_steer_to_tool_results).
  155. _pre_api_steer = agent._drain_pending_steer()
  156. if _pre_api_steer:
  157. _inject_steer_after_newest_tool_result(agent, messages, _pre_api_steer)
  158. # One-shot run-budget wrap-up notice at 80% of agent.run_budget_seconds, appended to the
  159. # newest tool result; off with no budget.
  160. if getattr(agent, "run_budget_seconds", None):
  161. _maybe_inject_run_budget_wrapup(agent, messages)
  162. # Appended to the newest tool result; never a synthetic user/system row.
  163. _maybe_inject_iteration_budget_warning(agent, messages)
  164. request_logger = getattr(agent, "logger", None) or logger # same name as the origin module
  165. # Per-agent validation cursor skips re-parsing tool_call args already validated.
  166. # Identity-keyed; a rewritten list breaks the prefix match and forces a re-scan.
  167. _sanitize_cursor = getattr(agent, "_sanitize_args_cursor", None)
  168. if _sanitize_cursor is None:
  169. _sanitize_cursor = {}
  170. with suppress(Exception):
  171. agent._sanitize_args_cursor = _sanitize_cursor
  172. repaired_tool_calls = agent._sanitize_tool_call_arguments(
  173. messages, logger=request_logger, session_id=agent.session_id, cursor=_sanitize_cursor
  174. )
  175. if repaired_tool_calls > 0:
  176. # In-place arg repair may have popped _DB_PERSISTED_MARKER off stamped live dicts;
  177. # force a full flush scan so the repaired rows are rewritten.
  178. agent._db_flush_scan_prefix = None
  179. request_logger.info(
  180. "Sanitized %s corrupted tool_call arguments before request (session=%s)",
  181. repaired_tool_calls,
  182. agent.session_id or "-",
  183. )
  184. messages, _echo_neutralised = _neutralise_replay_echo_ghosts(messages)
  185. if _echo_neutralised:
  186. request_logger.info(
  187. "Neutralised %d interrupt-placeholder row(s) before request (session=%s)",
  188. _echo_neutralised, agent.session_id or "-",
  189. )
  190. # Repair malformed role alternation (tool→user / user→user tails): providers
  191. # return empty content on them and the empty-retry loop spins. The _with_cursor
  192. # variant also recomputes the SessionDB flush cursor after compaction.
  193. from agent.agent_runtime_helpers import repair_message_sequence_with_cursor
  194. repaired_seq = repair_message_sequence_with_cursor(agent, messages)
  195. if repaired_seq > 0:
  196. request_logger.info(
  197. "Repaired %s message-alternation violations before request (session=%s)",
  198. repaired_seq,
  199. agent.session_id or "-",
  200. )
  201. # The merge shrank the list, so the index recorded at turn start can point past this
  202. # turn's user row: prefetch would inject into a historical row and index-settling hosts
  203. # (hermes-webui) would write the current turn to the FRONT of the context. Re-anchor as
  204. # the compression-restart path does (last verbatim row wins, never a historical copy);
  205. # without the text the index cannot be re-derived and is left detectably stale.
  206. if user_message is not None:
  207. _reanchored_idx = _reanchor(agent, messages, user_message)
  208. if _reanchored_idx != current_turn_user_idx:
  209. request_logger.info(
  210. "Re-anchored current_turn_user_idx %s -> %s after alternation repair (session=%s)",
  211. current_turn_user_idx, _reanchored_idx, agent.session_id or "-",
  212. )
  213. current_turn_user_idx = _reanchored_idx
  214. # Mid-turn compaction (post-tool gate, overflow restart, recovery) rebuilds ``messages`` without
  215. # handing back a new index. A stale index splits the request's replay prefix inside this turn's
  216. # tool rows: prefix canonicalization then drops the assistant tool_call whose result fell past the
  217. # split, the orphaned result is sanitized away, and the model silently loses tool output that
  218. # state.db still holds. A valid index always lands on this turn's user row; re-anchor otherwise.
  219. if user_message is not None and not _anchors_current_turn(messages, current_turn_user_idx, user_message):
  220. _reanchored_idx = _reanchor(agent, messages, user_message)
  221. request_logger.info(
  222. "Re-anchored stale current_turn_user_idx %s -> %s (session=%s)",
  223. current_turn_user_idx, _reanchored_idx, agent.session_id or "-",
  224. )
  225. current_turn_user_idx = _reanchored_idx
  226. return IterationPrep(
  227. action="fallthrough", messages=messages, request_logger=request_logger,
  228. current_turn_user_idx=current_turn_user_idx,
  229. )
  230. def _previous_tool_round(messages: Any) -> list:
  231. """The newest assistant tool_calls batch with each call's result, for ``agent:step``."""
  232. for _idx, _m in enumerate(reversed(messages)):
  233. if _m.get("role") == "assistant" and _m.get("tool_calls"):
  234. _results_by_id = {}
  235. for _tm in messages[len(messages) - _idx:]:
  236. if _tm.get("role") != "tool":
  237. break
  238. _tcid = _tm.get("tool_call_id")
  239. if _tcid:
  240. _results_by_id[_tcid] = _tm.get("content", "")
  241. return [
  242. {
  243. "name": tc["function"]["name"],
  244. "result": _results_by_id.get(tc.get("id")),
  245. "arguments": tc["function"].get("arguments"),
  246. }
  247. for tc in _m["tool_calls"]
  248. if isinstance(tc, dict)
  249. ]
  250. return []
  251. def _inject_steer_after_newest_tool_result(agent: Any, messages: Any, steer_text: str) -> None:
  252. """Append the steer marker as a standalone user row after the newest tool message; with no
  253. tool message, put the text back so the post-tool-execution drain delivers it later."""
  254. for _si in range(len(messages) - 1, -1, -1):
  255. _sm = messages[_si]
  256. if isinstance(_sm, dict) and _sm.get("role") == "tool":
  257. from agent.prompt_builder import steer_user_row
  258. messages.insert(_si + 1, steer_user_row(steer_text))
  259. logger.debug("Pre-API-call steer drain: appended user row after tool msg at index %d", _si)
  260. return
  261. from agent.agent_runtime_helpers import _requeue_pending_steer
  262. _requeue_pending_steer(agent, steer_text)
  263. @dataclass
  264. class ApiCallAnnouncement:
  265. """Always ``action == "fallthrough"``; ``thinking_spinner`` is the started raw spinner or
  266. None (TUI widget / streaming consumers / verbose mode)."""
  267. action: str
  268. thinking_spinner: Any
  269. def announce_api_call(
  270. agent: Any, *, messages: Any, api_messages: Any, api_call_count: Any, approx_tokens: Any,
  271. total_chars: Any,
  272. ) -> ApiCallAnnouncement:
  273. """Print the request summary (verbose) or start the quiet-mode thinking indicator."""
  274. thinking_spinner = None
  275. if not agent.quiet_mode:
  276. agent._vprint(f"\n{agent.log_prefix}🔄 Making API call #{api_call_count}/{agent.max_iterations}...")
  277. agent._vprint(f"{agent.log_prefix} 📊 Request size: {len(api_messages)} messages, ~{approx_tokens:,} tokens (~{total_chars:,} chars)")
  278. agent._vprint(f"{agent.log_prefix} 🔧 Available tools: {len(agent.tools) if agent.tools else 0}")
  279. else:
  280. # Animated thinking spinner in quiet mode
  281. face = random.choice(KawaiiSpinner.get_thinking_faces())
  282. verb = random.choice(KawaiiSpinner.get_thinking_verbs())
  283. if agent.thinking_callback:
  284. # CLI TUI mode: use prompt_toolkit widget instead of raw spinner
  285. # (works in both streaming and non-streaming modes)
  286. agent.thinking_callback(f"{face} {verb}...")
  287. elif not agent._has_stream_consumers() and agent._should_start_quiet_spinner():
  288. # Raw KawaiiSpinner only when no streaming consumers and the
  289. # spinner output has a safe sink.
  290. spinner_type = random.choice(['brain', 'sparkle', 'pulse', 'moon', 'star'])
  291. thinking_spinner = KawaiiSpinner(f"{face} {verb}...", spinner_type=spinner_type, print_fn=agent._print_fn)
  292. thinking_spinner.start()
  293. # Log request details if verbose
  294. if agent.verbose_logging:
  295. logging.debug(f"API Request - Model: {agent.model}, Messages: {len(messages)}, Tools: {len(agent.tools) if agent.tools else 0}")
  296. logging.debug(f"Last message role: {messages[-1]['role'] if messages else 'none'}")
  297. logging.debug(f"Total message size: ~{approx_tokens:,} tokens")
  298. return ApiCallAnnouncement(action="fallthrough", thinking_spinner=thinking_spinner)
  299. @dataclass
  300. class IterationStart:
  301. """``action``: ``"fallthrough"`` (run the iteration) or ``"break"`` (turn ends: interrupt,
  302. review input budget or iteration budget exhausted — ``_turn_exit_reason`` set)."""
  303. action: str
  304. original_user_message: Any
  305. api_call_count: Any
  306. interrupted: Any
  307. _turn_exit_reason: Any
  308. def begin_iteration(
  309. agent: Any, *, messages: Any, conversation_history: Any, original_user_message: Any,
  310. api_call_count: Any, interrupted: Any, _turn_exit_reason: Any,
  311. ) -> IterationStart:
  312. """Iteration entry in the original order: apply a pending redirect, reset the checkpoint
  313. dedup, then the interrupt / review-budget / iteration-budget exits. ``api_call_count`` is
  314. incremented here (the grace call consumes its flag instead of the budget)."""
  315. from agent.conversation_loop import (
  316. _apply_active_turn_redirect, _review_input_budget_exhausted
  317. )
  318. def _verdict(action: str) -> IterationStart:
  319. return IterationStart(
  320. action=action, original_user_message=original_user_message,
  321. api_call_count=api_call_count, interrupted=interrupted,
  322. _turn_exit_reason=_turn_exit_reason,
  323. )
  324. _redirect_text = agent._drain_pending_redirect()
  325. if _redirect_text:
  326. _apply_active_turn_redirect(agent, messages, _redirect_text)
  327. if isinstance(original_user_message, str):
  328. original_user_message = (
  329. f"{original_user_message}\n\n" f"User correction during the turn: {_redirect_text}"
  330. )
  331. agent._persist_session(messages, conversation_history)
  332. # Reset per-turn checkpoint dedup so each iteration can take one snapshot.
  333. agent._checkpoint_mgr.new_turn()
  334. if agent._interrupt_requested:
  335. interrupted = True
  336. _issuer = interrupt_issuer(agent)
  337. _turn_exit_reason = f"interrupted_by_system({_issuer})" if _issuer else "interrupted_by_user"
  338. if not agent.quiet_mode:
  339. agent._safe_print("\n⚡ Breaking out of tool loop due to interrupt...")
  340. return _verdict("break")
  341. # Aggregate input budget for detached auxiliary forks bounds the whole review, not
  342. # each request; checked between iterations so the crossing request's writes landed.
  343. if _review_input_budget_exhausted(agent):
  344. _turn_exit_reason = "review_input_budget_exhausted"
  345. if not agent.quiet_mode:
  346. agent._safe_print(
  347. f"\n⏹️ Review input budget exhausted "
  348. f"({int(agent.session_input_tokens):,} tokens) — stopping "
  349. f"the review tool loop before the next provider call.", diagnostic=True,
  350. )
  351. return _verdict("break")
  352. api_call_count += 1
  353. agent._api_call_count = api_call_count
  354. agent._touch_activity(f"starting API call #{api_call_count}")
  355. # Grace call: budget exhausted but the model gets one more call. Consume the
  356. # flag so the loop exits after this iteration regardless of outcome.
  357. if agent._budget_grace_call:
  358. # Exhaustion retains one toolless grace call regardless of whether an opt-in
  359. # checkpoint was emitted; the warning never extends the hard budget.
  360. agent._budget_grace_call = False
  361. elif not agent.iteration_budget.consume():
  362. _turn_exit_reason = "budget_exhausted"
  363. if not agent.quiet_mode:
  364. agent._safe_print(f"\n⚠️ Iteration budget exhausted ({agent.iteration_budget.used}/{agent.iteration_budget.max_total} iterations used)", diagnostic=True)
  365. return _verdict("break")
  366. return _verdict("fallthrough")
  367. @dataclass
  368. class RetryRestartVerdict:
  369. """``action``: ``"fallthrough"`` (a response is ready — process it), ``"continue"``
  370. (a restart flag re-issues the iteration: redirect / compressed / rebuilt-for-fallback /
  371. length continuation) or ``"break"`` (turn ends: interrupted, non-actionable compaction
  372. handoff, or every retry exhausted without a response)."""
  373. action: str
  374. current_turn_user_idx: Any
  375. final_response: Any
  376. retry_count: Any
  377. restart_count: Any
  378. api_call_count: Any
  379. _preflight_compression_blocked: Any
  380. _turn_exit_reason: Any
  381. def apply_retry_restarts(
  382. agent: Any, *, _retry: Any, response: Any, interrupted: Any, messages: Any,
  383. conversation_history: Any, user_message: Any, api_kwargs: Any, current_turn_user_idx: Any,
  384. final_response: Any, retry_count: Any, max_retries: Any, api_call_count: Any,
  385. restart_count: Any, length_continue_retries: Any,
  386. _preflight_compression_blocked: Any, _turn_exit_reason: Any,
  387. ) -> RetryRestartVerdict:
  388. """Consume the ``TurnRetryState`` restart flags after the retry loop, in the original
  389. priority order. Refunds the iteration budget/count for restarts that produced no valid
  390. assistant item; ``restart_with_rebuilt_messages`` is the single consumer that clears
  391. ``_preflight_compression_blocked`` so the fallback gets a fresh preflight (#84733).
  392. The two refunding restart paths (redirect and rebuilt-for-fallback) are bounded by
  393. ``max_retries`` via ``restart_count`` (restarts since the last response) so a runaway
  394. interrupt/redirect that keeps re-arming a restart flag cannot refund the budget
  395. forever and hold the turn lease indefinitely."""
  396. from agent.conversation_loop import (
  397. _HANDOFF_SKIP_FINAL_RESPONSE, _should_skip_model_call_for_reference_handoff
  398. )
  399. def _verdict(action: str) -> RetryRestartVerdict:
  400. return RetryRestartVerdict(
  401. action=action, current_turn_user_idx=current_turn_user_idx,
  402. final_response=final_response, retry_count=retry_count, restart_count=restart_count,
  403. api_call_count=api_call_count,
  404. _preflight_compression_blocked=_preflight_compression_blocked,
  405. _turn_exit_reason=_turn_exit_reason,
  406. )
  407. if _retry.restart_with_redirected_messages:
  408. restart_count += 1
  409. if restart_count > max_retries:
  410. # A redirect/interrupt keeps re-arming this flag: stop refunding the iteration
  411. # budget and re-issuing the same logical iteration, or a runaway turn holds the
  412. # turn lease indefinitely (redirect restarts previously had no bound).
  413. _turn_exit_reason = "redirect_restart_limit_exceeded"
  414. logger.warning(
  415. "Redirected-message restart limit (%s) exceeded; ending turn instead of "
  416. "refunding the iteration budget indefinitely.",
  417. max_retries,
  418. )
  419. # The correction that tripped the cap was never applied; hand it back as the
  420. # next user turn (result["pending_steer"]) instead of losing it to clear_interrupt().
  421. _unapplied = agent._drain_pending_redirect()
  422. if _unapplied:
  423. agent.steer(_unapplied)
  424. return _verdict("break")
  425. # Cancelled request produced no valid assistant item: reuse the same logical
  426. # iteration after the outer loop appends partial context + correction.
  427. api_call_count -= 1
  428. agent.iteration_budget.refund()
  429. _retry.restart_with_redirected_messages = False
  430. return _verdict("continue")
  431. if interrupted:
  432. _turn_exit_reason = interrupted_during_api_call_reason(agent)
  433. return _verdict("break")
  434. if _retry.restart_with_compressed_messages:
  435. api_call_count -= 1
  436. agent.iteration_budget.refund()
  437. # Compression restarts count toward the retry limit so a compression that
  438. # shrinks messages but not enough can't loop forever.
  439. retry_count += 1
  440. _retry.restart_with_compressed_messages = False
  441. if _should_skip_model_call_for_reference_handoff(
  442. # Compression rebuilt the list (tail messages are fresh compaction copies), so the
  443. # pre-compression index of this turn's user message is stale. Re-anchor both index trackers: the
  444. # api_content stamp below, the loop's injection site, and the flush's persist-override row
  445. # (#48677) must all target the surviving dict, not a stale position. Exact-content match first
  446. # so a todo-snapshot user message appended after the tail can't steal the anchor.
  447. messages, user_message
  448. ):
  449. logger.info(
  450. "Skipping compressed-restart model call: reference-only "
  451. "handoff would be the sole active user turn (#80622)"
  452. )
  453. if not final_response:
  454. final_response = _HANDOFF_SKIP_FINAL_RESPONSE
  455. _turn_exit_reason = "compaction_handoff_not_actionable"
  456. return _verdict("break")
  457. # In-loop compression rebuilt `messages`; re-anchor the current-turn index
  458. # like the prologue, AFTER the handoff guard (it may re-append this turn's
  459. # ask). A stale anchor injects prefetch into a historical row.
  460. current_turn_user_idx = _reanchor(agent, messages, user_message)
  461. return _verdict("continue")
  462. if _retry.restart_with_rebuilt_messages:
  463. restart_count += 1
  464. if restart_count > max_retries:
  465. # A stall/failure keeps re-escalating to the fallback chain: stop refunding the
  466. # iteration budget and re-issuing, or a runaway turn holds the turn lease
  467. # indefinitely (rebuilt restarts previously had no bound).
  468. _turn_exit_reason = "rebuilt_restart_limit_exceeded"
  469. logger.warning(
  470. "Rebuilt-message restart limit (%s) exceeded; ending turn instead of "
  471. "refunding the iteration budget indefinitely.",
  472. max_retries,
  473. )
  474. return _verdict("break")
  475. # A stall/failure escalated to the fallback chain: re-issue against the
  476. # active fallback provider, refunding budget/count for the stalled attempt.
  477. api_call_count -= 1
  478. agent.iteration_budget.refund()
  479. _retry.restart_with_rebuilt_messages = False
  480. # Failover shrank the compressor window: clear the preflight block so
  481. # preflight re-runs before the first fallback call (single consumer).
  482. _preflight_compression_blocked = False
  483. return _verdict("continue")
  484. if _retry.restart_with_length_continuation:
  485. # Boost the output budget per retry (shared ladder, see boosted_output_cap).
  486. agent._ephemeral_max_output_tokens = boosted_output_cap(
  487. agent, agent._requested_output_cap_from_api_kwargs(api_kwargs), length_continue_retries
  488. )
  489. return _verdict("continue")
  490. # All retries may exhaust with `response` still None; break out cleanly.
  491. if response is None:
  492. _turn_exit_reason = "all_retries_exhausted_no_response"
  493. agent._emit_diagnostic_status("❌ The model provider didn't answer after all retries. Send /retry, or switch models with /model.")
  494. agent._persist_session(messages, conversation_history)
  495. return _verdict("break")
  496. # A response arrived, so the turn is not stuck re-issuing a cancelled request: start
  497. # the refunding-restart bound over (restart_count = restarts since the last response).
  498. # Counting every mid-turn correction for the whole turn ended healthy interactive
  499. # turns on the (max_retries + 1)th message (#128000).
  500. restart_count = 0
  501. return _verdict("fallthrough")