Skip to content

The agent that grows with you

refactor(hermes_cli): try/except-pass -> contextlib.suppress in desktop/dashboard/tui_launch

Teknium8a141af62f83
diff --git a/hermes_cli/main_dashboard.py b/hermes_cli/main_dashboard.py
index 07c2840677f..78dba581f4f 100644
--- a/hermes_cli/main_dashboard.py
+++ b/hermes_cli/main_dashboard.py
@@ -7,6 +7,7 @@ Names that stay in main are imported lazily inside the functions that use them
avoids an import cycle).
"""
+import contextlib
import os
import re
import shlex
@@ -247,10 +248,8 @@ def _respawn_dashboard_processes(commands: list[list[str]]) -> list[list[str]]:
respawned: list[list[str]] = []
failed: list[tuple[list[str], str]] = []
log_path = get_hermes_home() / "logs" / "dashboard-restart.log"
- try:
+ with contextlib.suppress(OSError):
log_path.parent.mkdir(parents=True, exist_ok=True)
- except OSError:
- pass
for command in commands:
try:
@@ -287,10 +286,8 @@ class _UpdateOutputStream:
def write(self, data):
# Mirror to the log file first — it's the most reliable destination.
if self._log is not None:
- try:
+ with contextlib.suppress(Exception):
self._log.write(data)
- except Exception:
- pass
if not self._original_broken:
try:
return self._original.write(data)
@@ -300,10 +297,8 @@ class _UpdateOutputStream:
def flush(self):
if self._log is not None:
- try:
+ with contextlib.suppress(Exception):
self._log.flush()
- except Exception:
- pass
if self._original_broken:
return
try:
@@ -340,10 +335,9 @@ def _install_hangup_protection(gateway_mode: bool = False):
import signal as _signal
if hasattr(_signal, "SIGHUP"):
- try:
+ # Non-main thread: update still runs, just without hangup protection.
+ with contextlib.suppress(ValueError, OSError):
_signal.signal(_signal.SIGHUP, _signal.SIG_IGN)
- except (ValueError, OSError):
- pass # non-main thread — update still runs, just without hangup protection
# Any failure here is non-fatal; we just skip the wrap.
try:
@@ -373,14 +367,10 @@ def _finalize_update_output(state):
if not state:
return
if state.get("installed"):
- try:
+ with contextlib.suppress(Exception):
sys.stdout = state.get("prev_stdout", sys.stdout)
- except Exception:
- pass
- try:
+ with contextlib.suppress(Exception):
sys.stderr = state.get("prev_stderr", sys.stderr)
- except Exception:
- pass
log_file = state.get("log_file")
if log_file is not None:
try:
@@ -624,10 +614,8 @@ def _read_ssh_session_token_file(path: str) -> str:
if file_fd >= 0:
os.close(file_fd)
if directory_fd >= 0:
- try:
+ with contextlib.suppress(OSError):
os.unlink(relative.parts[1], dir_fd=directory_fd)
- except OSError:
- pass
os.close(directory_fd)
if root_fd >= 0:
os.close(root_fd)
diff --git a/hermes_cli/main_desktop.py b/hermes_cli/main_desktop.py
index 301add161e5..441c8af2a7e 100644
--- a/hermes_cli/main_desktop.py
+++ b/hermes_cli/main_desktop.py
@@ -8,6 +8,7 @@ avoids an import cycle).
"""
import logging
+import contextlib
import argparse
import os
import re
@@ -424,10 +425,8 @@ def _ensure_desktop_exe_launchable(desktop_dir: Path, packaged_executable: Optio
# Only the exe's OWN output dir is purged (a staging dir), never the live
# release/ tree that still holds the last working app.
_purge_electron_build_cache(desktop_dir, release_dir=packaged_executable.parent.parent)
- try:
+ with contextlib.suppress(OSError):
_desktop_stamp_path().unlink()
- except OSError:
- pass
restored = _rollback_desktop_from_backup(packaged_executable)
if restored is not None:
@@ -562,10 +561,8 @@ def _redownload_electron_dist(project_root: Path, env: dict, *, mirror: Optional
return False
shutil.rmtree(electron_dir / "dist", ignore_errors=True)
- try:
+ with contextlib.suppress(OSError):
(electron_dir / "path.txt").unlink()
- except OSError:
- pass
dl_env = with_hermes_node_path(env)
if mirror:
diff --git a/hermes_cli/main_tui_launch.py b/hermes_cli/main_tui_launch.py
index 5f62e521524..94476eb6ddf 100644
--- a/hermes_cli/main_tui_launch.py
+++ b/hermes_cli/main_tui_launch.py
@@ -8,6 +8,7 @@ avoids an import cycle).
"""
import logging
+import contextlib
import json
import os
import shutil
@@ -767,10 +768,8 @@ def _launch_tui(
if code in {0, 130}:
_print_tui_exit_summary(resume_session_id, active_session_file)
finally:
- try:
+ with contextlib.suppress(OSError):
os.unlink(active_session_file)
- except OSError:
- pass
if wt_info:
try:
from cli import _cleanup_worktree