From c2ee4753bfbfd2a333cfef8381f2e4b5bf97307f Mon Sep 17 00:00:00 2001 From: Pigbibi <20649888+Pigbibi@users.noreply.github.com> Date: Sat, 4 Jul 2026 04:31:45 +0800 Subject: [PATCH] fix: harden cloud run runtime guard Co-Authored-By: Codex --- scripts/cloud_run_runtime_guard.py | 19 ++++++++++++ tests/test_cloud_run_runtime_guard.py | 42 +++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/scripts/cloud_run_runtime_guard.py b/scripts/cloud_run_runtime_guard.py index b17bb21..12e14d5 100644 --- a/scripts/cloud_run_runtime_guard.py +++ b/scripts/cloud_run_runtime_guard.py @@ -358,7 +358,26 @@ def _entry_text(entry: dict[str, Any]) -> str: return " ".join(chunks) +def _request_path(entry: dict[str, Any]) -> str: + request_url = str((entry.get("httpRequest") or {}).get("requestUrl") or "").strip() + if not request_url: + return "" + return urllib.parse.urlparse(request_url).path + + +def _is_ignorable_monitor_dispatch_capacity_warning(entry: dict[str, Any]) -> bool: + if not _env_bool("RUNTIME_GUARD_IGNORE_MONITOR_DISPATCH_CAPACITY_WARNINGS", True): + return False + return ( + _status(entry) == 429 + and _request_path(entry) == "/monitor-dispatch" + and "NO AVAILABLE INSTANCE" in _entry_text(entry).upper() + ) + + def _is_failure(entry: dict[str, Any]) -> bool: + if _is_ignorable_monitor_dispatch_capacity_warning(entry): + return False severity = str(entry.get("severity") or "").upper() status = _status(entry) text = _entry_text(entry).upper() diff --git a/tests/test_cloud_run_runtime_guard.py b/tests/test_cloud_run_runtime_guard.py index 06209d6..fd174f8 100644 --- a/tests/test_cloud_run_runtime_guard.py +++ b/tests/test_cloud_run_runtime_guard.py @@ -186,3 +186,45 @@ def test_scheduler_entry_since_uses_matching_service_revision_window(): guard._scheduler_entry_since(entry, {"other-service": service_since}, fallback) == fallback ) + + +def test_monitor_dispatch_capacity_warning_is_not_failure_by_default(monkeypatch): + monkeypatch.delenv("RUNTIME_GUARD_IGNORE_MONITOR_DISPATCH_CAPACITY_WARNINGS", raising=False) + entry = { + "severity": "WARNING", + "httpRequest": { + "status": 429, + "requestUrl": "https://example.run.app/monitor-dispatch", + }, + "textPayload": "The request was aborted because there was no available instance.", + } + + assert guard._is_failure(entry) is False + + +def test_monitor_dispatch_capacity_warning_can_be_counted(monkeypatch): + monkeypatch.setenv("RUNTIME_GUARD_IGNORE_MONITOR_DISPATCH_CAPACITY_WARNINGS", "false") + entry = { + "severity": "WARNING", + "httpRequest": { + "status": 429, + "requestUrl": "https://example.run.app/monitor-dispatch", + }, + "textPayload": "The request was aborted because there was no available instance.", + } + + assert guard._is_failure(entry) is True + + +def test_strategy_request_capacity_warning_still_fails(monkeypatch): + monkeypatch.delenv("RUNTIME_GUARD_IGNORE_MONITOR_DISPATCH_CAPACITY_WARNINGS", raising=False) + entry = { + "severity": "WARNING", + "httpRequest": { + "status": 429, + "requestUrl": "https://example.run.app/dry-run", + }, + "textPayload": "The request was aborted because there was no available instance.", + } + + assert guard._is_failure(entry) is True