From a13038db8788700a7efaff0f71b190ba5801ddb0 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 2 Jul 2026 19:11:10 +0800 Subject: [PATCH] fix(ops): avoid truncating stockplatform runtime readback --- ...ockplatform_public_api_runtime_readback.py | 7 ++-- ...ockplatform_public_api_runtime_readback.py | 32 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/apps/api/src/services/stockplatform_public_api_runtime_readback.py b/apps/api/src/services/stockplatform_public_api_runtime_readback.py index 4441e338..d2f95a9c 100644 --- a/apps/api/src/services/stockplatform_public_api_runtime_readback.py +++ b/apps/api/src/services/stockplatform_public_api_runtime_readback.py @@ -33,6 +33,7 @@ _DEFAULT_BASE_URL = "https://stock.wooo.work" _DEFAULT_TIMEOUT_SECONDS = 4.0 _DEFAULT_DATA_ENDPOINT_TIMEOUT_SECONDS = 10.0 _DEFAULT_PROBE_ATTEMPTS = 2 +_MAX_READBACK_BODY_BYTES = 500_000 Probe = Callable[[str, float], dict[str, Any]] @@ -650,10 +651,12 @@ def _http_probe(url: str, timeout_seconds: float) -> dict[str, Any]: ) try: with urllib.request.urlopen(request, timeout=timeout_seconds) as response: - body = response.read(8192).decode("utf-8", "replace") + body = response.read(_MAX_READBACK_BODY_BYTES).decode( + "utf-8", "replace" + ) return {"http_status": response.status, "body": body, "error": ""} except urllib.error.HTTPError as exc: - body = exc.read(2048).decode("utf-8", "replace") + body = exc.read(_MAX_READBACK_BODY_BYTES).decode("utf-8", "replace") return {"http_status": exc.code, "body": body, "error": ""} except Exception as exc: # noqa: BLE001 - readback must fail closed. return {"http_status": None, "body": "", "error": _error_text(exc)} diff --git a/apps/api/tests/test_stockplatform_public_api_runtime_readback.py b/apps/api/tests/test_stockplatform_public_api_runtime_readback.py index 00c07eca..025eaa68 100644 --- a/apps/api/tests/test_stockplatform_public_api_runtime_readback.py +++ b/apps/api/tests/test_stockplatform_public_api_runtime_readback.py @@ -7,6 +7,7 @@ from fastapi.testclient import TestClient from src.api.v1 import agents from src.api.v1.agents import router +from src.services import stockplatform_public_api_runtime_readback as runtime_readback from src.services.stockplatform_public_api_runtime_readback import ( load_latest_stockplatform_public_api_runtime_readback, ) @@ -235,6 +236,37 @@ def test_stockplatform_public_api_runtime_endpoint_returns_readback(monkeypatch) assert data["active_blocker_count"] == 5 +def test_http_probe_reads_full_large_json_body(monkeypatch): + body = json.dumps({"padding": "x" * 9000, "status": "ok"}).encode() + + class Response: + status = 200 + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc, traceback): + return False + + def read(self, size: int) -> bytes: + assert size >= len(body) + return body + + monkeypatch.setattr( + runtime_readback.urllib.request, + "urlopen", + lambda request, timeout: Response(), + ) + + result = runtime_readback._http_probe( + "https://stock.wooo.work/api/v1/system/ingestion", + 10, + ) + + assert result["http_status"] == 200 + assert json.loads(result["body"])["status"] == "ok" + + def _probe_public_web_ok_api_502(url: str, timeout_seconds: float) -> dict: del timeout_seconds if url.endswith("/healthz") and "/api/" not in url: