From 86d59d5b3f7be6deeb529b57a3b39ba3a388d3bb Mon Sep 17 00:00:00 2001 From: Pigbibi <20649888+Pigbibi@users.noreply.github.com> Date: Fri, 19 Jun 2026 18:57:22 +0800 Subject: [PATCH] Return retry status for Firstrade execution blocks --- main.py | 9 ++++++++- tests/test_request_handling.py | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 21762d7..cc95df0 100644 --- a/main.py +++ b/main.py @@ -238,6 +238,12 @@ def _strategy_result_diagnostics(result: dict[str, Any]) -> dict[str, Any]: return diagnostics +def _strategy_result_http_status(result: dict[str, Any]) -> int: + if result.get("execution_blocked") and result.get("execution_block_retryable") and not result.get("funding_blocked"): + return 500 + return 200 + + def _persist_runtime_report(report: dict[str, Any]) -> str | None: persisted = persist_runtime_report( report, @@ -414,7 +420,8 @@ def run_strategy(): if not _runtime_target_enabled_env(): return jsonify({"ok": True, "status": "skipped", "skip_reason": "runtime_target_disabled"}), 200 try: - return jsonify(_run_strategy_cycle_with_report()) + result = _run_strategy_cycle_with_report() + return jsonify(result), _strategy_result_http_status(result) except (FirstradePlatformError, EnvironmentError, ValueError) as exc: notification_attempted = _handle_strategy_run_exception(exc) return ( diff --git a/tests/test_request_handling.py b/tests/test_request_handling.py index 90989f1..32a0afb 100644 --- a/tests/test_request_handling.py +++ b/tests/test_request_handling.py @@ -90,6 +90,29 @@ def test_run_endpoint_returns_200_for_terminal_funding_block(monkeypatch): assert payload["execution_block_retryable"] is False +def test_run_endpoint_returns_500_for_retryable_execution_block(monkeypatch): + monkeypatch.setenv("FIRSTRADE_RUN_STRATEGY_ON_HTTP", "true") + monkeypatch.setattr( + main, + "_run_strategy_cycle_with_report", + lambda **_kwargs: { + "ok": False, + "execution_blocked": True, + "execution_block_retryable": True, + "funding_blocked": False, + "error": "Strategy execution blocked; see execution_blocking_skips.", + }, + ) + client = main.app.test_client() + + response = client.post("/run") + + assert response.status_code == 500 + payload = response.get_json() + assert payload["execution_blocked"] is True + assert payload["execution_block_retryable"] is True + + def test_probe_endpoint_is_disabled_without_explicit_http_gate(monkeypatch): monkeypatch.delenv("FIRSTRADE_RUN_SESSION_CHECK_ON_HTTP", raising=False) client = main.app.test_client()