Skip to content

The agent that grows with you

refactor(agent/E_session): contextlib.suppress for swallow-only try/except

Teknium334b72256afd
diff --git a/agent/session_activity.py b/agent/session_activity.py
index b7a161bc65a..6c756c9ad44 100644
--- a/agent/session_activity.py
+++ b/agent/session_activity.py
@@ -8,6 +8,7 @@ the default agent clock stamps ``unknown`` unless a writer passes an explicit ``
from __future__ import annotations
import time
+from contextlib import suppress
from enum import Enum
from typing import Any, Mapping, Optional
@@ -50,10 +51,8 @@ def normalize_activity_provenance(provenance: Optional[ActivityProvenance | str]
def reset_session_activity_persist_window(agent: Any) -> None:
"""Clear the durable persist rate-limit so the next stamp writes through (terminal compression
labels must not stay stuck on mid-compress text)."""
- try:
+ with suppress(Exception):
agent._session_activity_last_persist_mono = 0.0
- except Exception:
- pass
def build_activity_snapshot(
diff --git a/agent/shell_hooks.py b/agent/shell_hooks.py
index 18431165a8b..8450e2ccade 100644
--- a/agent/shell_hooks.py
+++ b/agent/shell_hooks.py
@@ -20,7 +20,7 @@ import sys
import tempfile
import threading
import time
-from contextlib import ExitStack, contextmanager
+from contextlib import ExitStack, contextmanager, suppress
from dataclasses import dataclass, field
from datetime import datetime, timezone
from pathlib import Path
@@ -361,10 +361,8 @@ def _spawn(spec: ShellHookSpec, stdin_json: str) -> Dict[str, Any]:
except Exception as exc:
# Kill the whole tree — forked helpers holding the pipes would stall the drain.
kill_process_tree(proc)
- try:
+ with suppress(Exception):
proc.communicate(timeout=1)
- except Exception:
- pass
if not isinstance(exc, subprocess.TimeoutExpired): # pragma: no cover — defensive
return failed(str(exc))
result["timed_out"] = True
@@ -540,10 +538,8 @@ def save_allowlist(data: Dict[str, Any]) -> None:
fh.write(json.dumps(data, indent=2, sort_keys=True))
atomic_replace(tmp_path, p)
except Exception:
- try:
+ with suppress(OSError):
os.unlink(tmp_path)
- except OSError:
- pass
raise
except OSError as exc:
logger.warning(
@@ -576,10 +572,8 @@ def _locked_update_approvals() -> Iterator[Dict[str, Any]]:
def _flock_unlock(lock_fh: Any) -> None:
- try:
+ with suppress(OSError):
fcntl.flock(lock_fh.fileno(), fcntl.LOCK_UN)
- except OSError:
- pass
def _prompt_and_record(event: str, command: str, *, accept_hooks: bool) -> bool:
diff --git a/agent/title_generator.py b/agent/title_generator.py
index 1235cc9c2ba..a1ed0d31e76 100644
--- a/agent/title_generator.py
+++ b/agent/title_generator.py
@@ -11,6 +11,7 @@ import json
import logging
import re
import threading
+from contextlib import suppress
from typing import Any, Callable, Optional
from agent.auxiliary_client import call_llm
@@ -407,10 +408,8 @@ def auto_title_session(
from agent.portal_tags import set_conversation_context
conversation_id = session_id
- try:
+ with suppress(Exception):
conversation_id = session_db.get_conversation_root(session_id) or session_id
- except Exception:
- pass
set_conversation_context(conversation_id)
set_accounting_context(session_db, session_id)
diff --git a/agent/trace_upload.py b/agent/trace_upload.py
index 345e1189d9c..addb159e0cf 100644
--- a/agent/trace_upload.py
+++ b/agent/trace_upload.py
@@ -14,6 +14,7 @@ import json
import logging
import os
import uuid
+from contextlib import suppress
from datetime import datetime, timezone
from typing import Any, Dict, List, Optional, Tuple
@@ -225,11 +226,9 @@ def _do_upload(
) -> str:
"""Create (idempotently) the private dataset and push the trace file.
Returns a user-facing status string. Never raises."""
- try:
+ with suppress(Exception): # lazy-install unavailable/declined — the import below surfaces the hint
from tools import lazy_deps
lazy_deps.ensure("tool.trace_upload", prompt=False)
- except Exception:
- pass # lazy-install unavailable/declined — the import below surfaces the hint
try:
from huggingface_hub import HfApi
except ImportError: