The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(agent/turn): try/except-pass -> contextlib.suppress across turn prologue/prep/gate
Teknium08d95abdd31b
diff --git a/agent/turn_context.py b/agent/turn_context.pyindex 3d297d92ca0..082cd0ca765 100644--- a/agent/turn_context.py+++ b/agent/turn_context.py@@ -13,6 +13,7 @@ import sysimport threadingimport timeimport uuid+from contextlib import suppressfrom dataclasses import dataclassfrom typing import Any, Dict, List, Mapping, Optional, Tuple@@ -117,10 +118,8 @@ def consume_gateway_turn_context_notes(agent: Any) -> str:system prompt stays byte-stable and a cached agent never replays a stale note)."""notes = getattr(agent, "_gateway_turn_context_notes", "") or ""if hasattr(agent, "_gateway_turn_context_notes"):- try:+ with suppress(Exception):agent._gateway_turn_context_notes = ""- except Exception:- passreturn notes if isinstance(notes, str) else ""@@ -129,11 +128,10 @@ def append_notes_to_multimodal_content(content: Any, notes: str) -> bool:message (the sidecar path returns ``None`` for non-string content)."""if not notes or not isinstance(content, list):return False- try:+ with suppress(Exception):content.append({"type": "text", "text": notes})return True- except Exception:- return False+ return False# Surfaces whose sessions must not be auto-titled: cron names its own session and@@ -339,7 +337,7 @@ def _persist_under_lock(agent: Any, fn, failure_msg: str, pending_cli_message: Adef _publish_runtime_main(agent: Any) -> None:"""Tell auxiliary_client the live main provider/model for this turn (after primaryrestoration settled the runtime). Never raises: failure loses only the scope."""- try:+ with suppress(Exception):from agent.auxiliary_client import set_runtime_mainfrom agent.prompt_cache_scope import resolve_prompt_cache_scope_safe# Rotation-stable prompt-cache scope (lineage root), memoized per segment; a new@@ -352,8 +350,6 @@ def _publish_runtime_main(agent: Any) -> None:)},cache_scope=_cache_scope,)- except Exception:- passdef _refresh_mcp_tools_between_turns(agent: Any) -> None:@@ -425,15 +421,13 @@ def _reset_per_turn_agent_state(agent: Any) -> None:# Pre-turn connection health check: clean up dead TCP connections.if agent.api_mode != "anthropic_messages":- try:+ with suppress(Exception):if agent._cleanup_dead_connections():agent._emit_status("π Detected stale connections from a previous provider ""issue β cleaned up automatically. Proceeding with fresh ""connection.")- except Exception:- pass# Replay compression warning through status_callback for gateway platforms.if agent._compression_warning:agent._replay_compression_warning()@@ -526,14 +520,12 @@ def _emit_reaction(agent: Any, original_user_message: Any) -> None:reaction_callback = getattr(agent, "reaction_callback", None)if reaction_callback is None:return- try:+ with suppress(Exception):from agent.reactions import detect_reactionkind = detect_reaction(original_user_message)if kind:reaction_callback(kind)- except Exception:- passdef _ensure_session_row(agent: Any, pending_cli_message: Any) -> None:@@ -644,25 +636,19 @@ def _memory_turn_start_and_prefetch(agent: Any, original_user_message: Any) -> sif not agent._memory_manager:return ""_query = original_user_message if isinstance(original_user_message, str) else ""- try:+ with suppress(Exception):agent._memory_manager.on_turn_start(agent._user_turn_count, _query)- except Exception:- passext_prefetch_cache = ""- try:+ with suppress(Exception):if not is_trivial_prompt(_query):ext_prefetch_cache = agent._memory_manager.prefetch_all(_query) or ""- except Exception:- pass# Deterministic recall indicator via _emit_status so the model can't silently# drop injected memory.if ext_prefetch_cache:- try:+ with suppress(Exception):_recall_indicator = agent._memory_manager.describe_recall()if _recall_indicator:agent._emit_status(_recall_indicator)- except Exception:- passreturn ext_prefetch_cachediff --git a/agent/turn_iteration_prep.py b/agent/turn_iteration_prep.pyindex 5b6953aecc3..78051669db2 100644--- a/agent/turn_iteration_prep.py+++ b/agent/turn_iteration_prep.py@@ -11,6 +11,7 @@ from __future__ import annotationsimport loggingimport random+from contextlib import suppressfrom dataclasses import dataclassfrom typing import Any, Dict@@ -66,10 +67,8 @@ def prepare_iteration(agent: Any,*, messages: Any, api_call_count: Any) -> Itera_sanitize_cursor = getattr(agent, "_sanitize_args_cursor", None)if _sanitize_cursor is None:_sanitize_cursor = {}- try:+ with suppress(Exception):agent._sanitize_args_cursor = _sanitize_cursor- except Exception:- passrepaired_tool_calls = agent._sanitize_tool_call_arguments(messages, logger=request_logger, session_id=agent.session_id, cursor=_sanitize_cursor)@@ -144,12 +143,10 @@ def _inject_steer_into_newest_tool_result(agent: Any, messages: Any, steer_text:_sm["content"] = existing + markerelse:# Multimodal content blocks β append a text block.- try:+ with suppress(Exception):blocks = list(existing) if existing else []blocks.append({"type": "text", "text": marker})_sm["content"] = blocks- except Exception:- passlogger.debug("Pre-API-call steer drain: injected into tool msg at index %d", _si)diff --git a/agent/turn_preflight_gate.py b/agent/turn_preflight_gate.pyindex f3525bf5f19..9982cd1afd3 100644--- a/agent/turn_preflight_gate.py+++ b/agent/turn_preflight_gate.py@@ -7,6 +7,7 @@ assembled requests, not raw ``messages``) and the call intofrom __future__ import annotationsimport logging+from contextlib import suppressfrom typing import Anyfrom agent.message_metadata import append_message@@ -51,10 +52,8 @@ def run_preflight_gate(agent._emit_status("β Ollama runtime context is too small for Hermes tool use")v.api_call_count -= 1agent._api_call_count = v.api_call_count- try:+ with suppress(Exception):agent.iteration_budget.refund()- except Exception:- passv.action = "break"return v