From b2057c16856fc01eeb17a40aa65853a68e61b981 Mon Sep 17 00:00:00 2001 From: kshitij kapoor Date: Wed, 19 Aug 2026 23:48:08 +0530 Subject: [PATCH] refactor: extract duplicated load_config_readonly try/except into helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The identical 6-line try/except block for reading model.reasoning_echo from config appeared in both agent_init.py (init) and agent_runtime_helpers.py (switch_model). Extracted into AIAgent._read_reasoning_echo_from_config() static method — net -1 LOC. --- agent/agent_init.py | 8 +------- agent/agent_runtime_helpers.py | 8 +------- run_agent.py | 11 +++++++++++ 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/agent/agent_init.py b/agent/agent_init.py index da92e6f881..20e44607bb 100644 --- a/agent/agent_init.py +++ b/agent/agent_init.py @@ -916,13 +916,7 @@ def init_agent( # Per-provider reasoning_content echo opt-in (see _reasoning_echo_opt_in). # Read once at init; switch_model / try_activate_fallback / restore # keep it in sync with the active provider. - try: - from hermes_cli.config import load_config_readonly - agent._reasoning_echo_flag = bool( - (load_config_readonly().get("model") or {}).get("reasoning_echo") - ) - except Exception: - agent._reasoning_echo_flag = False + agent._reasoning_echo_flag = agent._read_reasoning_echo_from_config() agent.service_tier = service_tier agent.request_overrides = dict(request_overrides or {}) agent.prefill_messages = prefill_messages or [] # Prefilled conversation turns diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.py index bcb1cc3a04..dbbb0d11e9 100644 --- a/agent/agent_runtime_helpers.py +++ b/agent/agent_runtime_helpers.py @@ -2690,13 +2690,7 @@ def switch_model(agent, new_model, new_provider, api_key='', base_url='', api_mo agent.requested_provider = new_provider # Re-read reasoning_echo from config so the flag reflects the new # primary model's setting (see _reasoning_echo_opt_in). - try: - from hermes_cli.config import load_config_readonly - agent._reasoning_echo_flag = bool( - (load_config_readonly().get("model") or {}).get("reasoning_echo") - ) - except Exception: - agent._reasoning_echo_flag = False + agent._reasoning_echo_flag = agent._read_reasoning_echo_from_config() # Use the new base_url when provided. When it's empty AND the # provider is actually changing, do NOT fall back to the current # (old provider's) URL — that silently pairs the new provider label diff --git a/run_agent.py b/run_agent.py index 548c9953d8..ad8dc8d65d 100644 --- a/run_agent.py +++ b/run_agent.py @@ -7790,6 +7790,17 @@ class AIAgent: """ return bool(getattr(self, "_reasoning_echo_flag", False)) + @staticmethod + def _read_reasoning_echo_from_config() -> bool: + """Read ``model.reasoning_echo`` from config; False on any error.""" + try: + from hermes_cli.config import load_config_readonly + return bool( + (load_config_readonly().get("model") or {}).get("reasoning_echo") + ) + except Exception: + return False + def _needs_kimi_tool_reasoning(self) -> bool: """Return True when the current provider is Kimi / Moonshot thinking mode.