diff --git a/03_implementation/src/hermes3d/api/routes/design.py b/03_implementation/src/hermes3d/api/routes/design.py index 3b4a289a..3aef205b 100644 --- a/03_implementation/src/hermes3d/api/routes/design.py +++ b/03_implementation/src/hermes3d/api/routes/design.py @@ -901,8 +901,15 @@ def _probe_python_provider( try: import importlib.metadata as meta_mod version_str = meta_mod.version(module_name) - except Exception: - pass + except Exception as exc: # noqa: BLE001 -- Wave Agent 7: surface the silent fallback + try: + import logging + logging.getLogger(__name__).debug( + "design.module_version_lookup: metadata.version(%r) failed: %s: %s", + module_name, type(exc).__name__, exc, + ) + except Exception: # noqa: BLE001 + pass if spec.origin: module_path = str(spec.origin) diff --git a/03_implementation/src/hermes3d/api/routes/system.py b/03_implementation/src/hermes3d/api/routes/system.py index 4dedbced..a437c391 100644 --- a/03_implementation/src/hermes3d/api/routes/system.py +++ b/03_implementation/src/hermes3d/api/routes/system.py @@ -551,8 +551,15 @@ def _host_telemetry() -> dict[str, Any]: cpu_pct = int(round(psutil.cpu_percent(interval=0.05))) ram_pct = int(round(psutil.virtual_memory().percent)) - except Exception: - pass + except Exception as exc: # noqa: BLE001 -- Wave Agent 7: psutil import/probe is optional; log for observability + try: + import logging + logging.getLogger(__name__).debug( + "system.metrics: psutil unavailable; cpu/ram default to 0: %s: %s", + type(exc).__name__, exc, + ) + except Exception: # noqa: BLE001 + pass try: disk_pct = int(round(shutil.disk_usage(implementation_path()).used / shutil.disk_usage(implementation_path()).total * 100)) except OSError: diff --git a/03_implementation/src/hermes3d/api/routes/update_center.py b/03_implementation/src/hermes3d/api/routes/update_center.py index ef54ee7b..46acd22c 100644 --- a/03_implementation/src/hermes3d/api/routes/update_center.py +++ b/03_implementation/src/hermes3d/api/routes/update_center.py @@ -267,8 +267,15 @@ def update_center() -> dict[str, Any]: as_json({"updater_status": velopack["status"], "providers": provider_summary}), ), ) - except Exception: - pass + except Exception as exc: # noqa: BLE001 -- Wave Agent 7: log the proof-event insert fallback + try: + import logging + logging.getLogger(__name__).warning( + "update_center.read: proof_events insert failed: %s: %s", + type(exc).__name__, exc, + ) + except Exception: # noqa: BLE001 + pass return payload diff --git a/03_implementation/src/hermes3d/core/agents/auto_recovery.py b/03_implementation/src/hermes3d/core/agents/auto_recovery.py index 50663d77..22e29819 100644 --- a/03_implementation/src/hermes3d/core/agents/auto_recovery.py +++ b/03_implementation/src/hermes3d/core/agents/auto_recovery.py @@ -154,8 +154,11 @@ def auto_recover( result.duration_seconds = time.time() - started log.info("Recovered %s via soft restart", printer_id) return result - except Exception: - pass + except Exception as exc: # noqa: BLE001 -- Wave Agent 7: log recovery-poll failure + log.debug( + "auto_recovery.printer_state_poll_soft fallback: %s: %s", + type(exc).__name__, exc, + ) # ---- Step 2: firmware restart ---- time.sleep(min(firmware_restart_wait_s, max(0.0, max_total_s - (time.time() - started)))) @@ -194,8 +197,11 @@ def auto_recover( result.duration_seconds = time.time() - started log.info("Recovered %s via firmware restart", printer_id) return result - except Exception: - pass + except Exception as exc: # noqa: BLE001 -- Wave Agent 7: log firmware-recovery-poll failure + log.debug( + "auto_recovery.firmware_restart fallback: %s: %s", + type(exc).__name__, exc, + ) result.outcome = RecoveryOutcome.GAVE_UP result.final_state = state if "state" in locals() else {} diff --git a/03_implementation/src/hermes3d/core/orchestration/repair_agent.py b/03_implementation/src/hermes3d/core/orchestration/repair_agent.py index 53a2721f..22986f19 100644 --- a/03_implementation/src/hermes3d/core/orchestration/repair_agent.py +++ b/03_implementation/src/hermes3d/core/orchestration/repair_agent.py @@ -179,8 +179,11 @@ def _try_llm(self, esc: RepairEscalation) -> RepairResult | None: from hermes3d.core.llm.providers import extract_json suggestion = extract_json(getattr(result, "text", "")) or suggestion - except Exception: - pass + except Exception as exc: # noqa: BLE001 -- Wave Agent 7: log suggestion extraction failure + log.debug( + "repair_agent.extract_json fallback: %s: %s", + type(exc).__name__, exc, + ) return RepairResult( outcome="fixed", strategy_used="llm_suggestion", diff --git a/03_implementation/src/hermes3d/services/module_runtime.py b/03_implementation/src/hermes3d/services/module_runtime.py index 11baffef..4d9795c4 100644 --- a/03_implementation/src/hermes3d/services/module_runtime.py +++ b/03_implementation/src/hermes3d/services/module_runtime.py @@ -2239,7 +2239,21 @@ def _private_runtime_env() -> dict[str, str]: from hermes3d.services.agent_runtime import private_env return private_env() - except Exception: + except Exception as exc: # noqa: BLE001 -- silent fallback was Wave Agent 7 finding + # Squad G follow-up (Remaining/Skipped Wave 2026-05-09): expose the + # silent-fallback so observability is preserved. Use ``redact_text`` + # because env-import exceptions can carry path / token fragments. + try: + import logging + + from hermes3d.gateways.redaction import redact_text + + logging.getLogger(__name__).warning( + "_private_runtime_env: private_env import/load failed: %s", + redact_text(f"{type(exc).__name__}: {exc}")[:200], + ) + except Exception: # noqa: BLE001 -- best-effort during teardown + pass return {} diff --git a/04_testing/pytest/unit/test_broad_except_cleanup.py b/04_testing/pytest/unit/test_broad_except_cleanup.py new file mode 100644 index 00000000..f4afd19a --- /dev/null +++ b/04_testing/pytest/unit/test_broad_except_cleanup.py @@ -0,0 +1,100 @@ +"""Wave Agent 7 broad-except cleanup — source-level pin tests. + +Source pin tests that confirm the 7 silent-fallback sites Wave Agent 7 +flagged now emit a ``logging.getLogger(__name__).{warning|debug}(...)`` +call before the ``pass``. Catches accidental revert. + +Sites covered (per Wave synthesis 2026-05-09): +- services/module_runtime.py:_private_runtime_env (warning + redact_text) +- api/routes/design.py:module_version_lookup (debug) +- api/routes/update_center.py:read fallback (warning) +- api/routes/system.py:metrics psutil fallback (debug) +- core/orchestration/repair_agent.py:extract_json fallback (debug) +- core/agents/auto_recovery.py:printer_state_poll_soft fallback (debug) +- core/agents/auto_recovery.py:firmware_restart fallback (debug) + +Behavior is otherwise unchanged on every site (silent fallback semantics +preserved; observability added). + +References: +- OWASP A09:2021 Logging & Monitoring Failures +- https://docs.python.org/3/library/logging.html +""" + +from __future__ import annotations + +import inspect + +import pytest + + +def _read_source(dotted: str) -> str: + """Read a Python source file under hermes3d by dotted module name.""" + parts = dotted.split(".") + from pathlib import Path + + src_root = Path(__file__).resolve().parents[3] / "03_implementation" / "src" + file_path = src_root / Path(*parts).with_suffix(".py") + return file_path.read_text(encoding="utf-8") + + +@pytest.mark.parametrize( + "module_dotted,marker_substring,description", + [ + ( + "hermes3d.services.module_runtime", + "_private_runtime_env: private_env import/load failed", + "module_runtime._private_runtime_env logs + redacts on fallback", + ), + ( + "hermes3d.api.routes.design", + "design.module_version_lookup", + "design.module_version_lookup logs on metadata.version() fallback", + ), + ( + "hermes3d.api.routes.update_center", + "update_center.read", + "update_center.read logs on proof_events insert fallback", + ), + ( + "hermes3d.api.routes.system", + "system.metrics", + "system.metrics logs on psutil fallback", + ), + ( + "hermes3d.core.orchestration.repair_agent", + "repair_agent.extract_json", + "repair_agent logs on extract_json fallback", + ), + ( + "hermes3d.core.agents.auto_recovery", + "auto_recovery.printer_state_poll_soft", + "auto_recovery soft-restart logs on poll fallback", + ), + ( + "hermes3d.core.agents.auto_recovery", + "auto_recovery.firmware_restart", + "auto_recovery firmware-restart logs on poll fallback", + ), + ], +) +def test_broad_except_site_now_logs( + module_dotted: str, marker_substring: str, description: str +) -> None: + """Each Wave Agent 7 site must contain a recognizable log marker.""" + src = _read_source(module_dotted) + assert marker_substring in src, ( + f"Wave Agent 7 broad-except cleanup regression — {description}: " + f"expected marker {marker_substring!r} not found in source." + ) + + +def test_module_runtime_uses_redact_text_for_env_exception() -> None: + """``_private_runtime_env`` exceptions can carry path/token fragments; + must run through ``redact_text`` before logging (Wave Agent 7 caveat).""" + src = _read_source("hermes3d.services.module_runtime") + # The redact_text call must appear in or near the _private_runtime_env block. + assert "redact_text" in src, ( + "module_runtime must import redact_text for safe exception logging" + ) + assert "_private_runtime_env" in src