diff --git a/agent/turn_context.py b/agent/turn_context.py index 3d297d92ca..082cd0ca76 100644 --- a/agent/turn_context.py +++ b/agent/turn_context.py @@ -13,6 +13,7 @@ import sys import threading import time import uuid +from contextlib import suppress from dataclasses import dataclass from 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: - pass return 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: A def _publish_runtime_main(agent: Any) -> None: """Tell auxiliary_client the live main provider/model for this turn (after primary restoration settled the runtime). Never raises: failure loses only the scope.""" - try: + with suppress(Exception): from agent.auxiliary_client import set_runtime_main from 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: - pass def _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_reaction kind = detect_reaction(original_user_message) if kind: reaction_callback(kind) - except Exception: - pass def _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) -> s if 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: - pass ext_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: - pass return ext_prefetch_cache diff --git a/agent/turn_iteration_prep.py b/agent/turn_iteration_prep.py index 5b6953aecc..78051669db 100644 --- a/agent/turn_iteration_prep.py +++ b/agent/turn_iteration_prep.py @@ -11,6 +11,7 @@ from __future__ import annotations import logging import random +from contextlib import suppress from dataclasses import dataclass from 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: - pass repaired_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 + marker else: # 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: - pass logger.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.py index f3525bf5f1..9982cd1afd 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 into from __future__ import annotations import logging +from contextlib import suppress from typing import Any from 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 -= 1 agent._api_call_count = v.api_call_count - try: + with suppress(Exception): agent.iteration_budget.refund() - except Exception: - pass v.action = "break" return v