From ae2953ef0175702957dfb1968607796edc503c78 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Wed, 2 Sep 2026 19:32:53 -0700 Subject: [PATCH] refactor(agent/display,error_surface,i18n): final guard-ladder collapse --- agent/display.py | 8 +++----- agent/error_surface.py | 13 +++++-------- agent/i18n.py | 17 ++++++++--------- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/agent/display.py b/agent/display.py index f289da9df6..598553bfae 100644 --- a/agent/display.py +++ b/agent/display.py @@ -552,15 +552,14 @@ def build_tool_label(tool_name: str, args: dict, max_len: int | None = None) -> def _resolved_path(path: str) -> Path: """Resolve a possibly-relative filesystem path against the current cwd.""" - candidate = Path(os.path.expanduser(path)) - return candidate if candidate.is_absolute() else Path.cwd() / candidate + return Path.cwd() / Path(os.path.expanduser(path)) # a `/` with an absolute rhs keeps the rhs def _snapshot_text(path: Path) -> str | None: """Return UTF-8 file content, or None for missing/unreadable files.""" try: return path.read_text(encoding="utf-8") - except (FileNotFoundError, IsADirectoryError, UnicodeDecodeError, OSError): + except (UnicodeDecodeError, OSError): # FileNotFoundError/IsADirectoryError are OSErrors return None @@ -853,8 +852,7 @@ class KawaiiSpinner: def start(self): if self.running: return - self.running = True - self.start_time = time.time() + self.running, self.start_time = True, time.time() self.thread = threading.Thread(target=self._animate, daemon=True) self.thread.start() diff --git a/agent/error_surface.py b/agent/error_surface.py index 666dce3145..1c6532f708 100644 --- a/agent/error_surface.py +++ b/agent/error_surface.py @@ -90,9 +90,8 @@ def _disk_full(candidate: Any) -> bool: def _result_layer(reason: str, error_text: str, provider: str) -> str: - layer = _REASON_TO_LAYER.get(reason) - if layer is not None: - return layer + if reason in _REASON_TO_LAYER: + return _REASON_TO_LAYER[reason] if reason in _TRANSPORT_REASONS and _is_custom_endpoint(provider): return LAYER_ENDPOINT return LAYER_STREAMING if _looks_like_stream_drop(error_text) else LAYER_PROVIDER @@ -117,11 +116,9 @@ def build_error_surface_from_result(result: Any, provider: str = "", model: str return _surface(LAYER_DISK, "disk_full", False, provider, model) if result.get("billing_block") or reason in ("billing", "billing_unverified"): return _surface(LAYER_BILLING, reason or "billing", False, provider, model) - if not reason: - # Failed result without a classified reason (legacy paths). - if _looks_like_stream_drop(error_text): - return _surface(LAYER_STREAMING, "stream_drop", True, provider, model) - return _surface(LAYER_PROVIDER, "unknown", True, provider, model) + if not reason: # failed result without a classified reason (legacy paths) + drop = _looks_like_stream_drop(error_text) + return _surface(LAYER_STREAMING if drop else LAYER_PROVIDER, "stream_drop" if drop else "unknown", True, provider, model) # Prefer the classifier's own verdict (``failure_retryable``); the # reason-set fallback covers older results. retryable = result.get("failure_retryable") diff --git a/agent/i18n.py b/agent/i18n.py index 4fe61acf0d..02f0c97036 100644 --- a/agent/i18n.py +++ b/agent/i18n.py @@ -98,15 +98,14 @@ def _load_catalog(lang: str) -> dict[str, str]: flat: dict[str, str] = {} if not path.is_file(): logger.debug("i18n catalog missing for %s at %s", lang, path) - else: - try: - import yaml - with path.open("r", encoding="utf-8") as f: - raw = yaml.safe_load(f) or {} - except Exception as exc: - logger.warning("Failed to load i18n catalog %s: %s", path, exc) - else: - _flatten_into(raw, "", flat) + return _cache_catalog(lang, flat) + try: + import yaml + with path.open("r", encoding="utf-8") as f: + _flatten_into(yaml.safe_load(f) or {}, "", flat) + except Exception as exc: + logger.warning("Failed to load i18n catalog %s: %s", path, exc) + flat = {} return _cache_catalog(lang, flat)