Skip to content

Commit 36c2e3e

Browse files
authored
Return retry status for Firstrade execution blocks
1 parent 12affb3 commit 36c2e3e

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

main.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ def _strategy_result_diagnostics(result: dict[str, Any]) -> dict[str, Any]:
238238
return diagnostics
239239

240240

241+
def _strategy_result_http_status(result: dict[str, Any]) -> int:
242+
if result.get("execution_blocked") and result.get("execution_block_retryable") and not result.get("funding_blocked"):
243+
return 500
244+
return 200
245+
246+
241247
def _persist_runtime_report(report: dict[str, Any]) -> str | None:
242248
persisted = persist_runtime_report(
243249
report,
@@ -414,7 +420,8 @@ def run_strategy():
414420
if not _runtime_target_enabled_env():
415421
return jsonify({"ok": True, "status": "skipped", "skip_reason": "runtime_target_disabled"}), 200
416422
try:
417-
return jsonify(_run_strategy_cycle_with_report())
423+
result = _run_strategy_cycle_with_report()
424+
return jsonify(result), _strategy_result_http_status(result)
418425
except (FirstradePlatformError, EnvironmentError, ValueError) as exc:
419426
notification_attempted = _handle_strategy_run_exception(exc)
420427
return (

tests/test_request_handling.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,29 @@ def test_run_endpoint_returns_200_for_terminal_funding_block(monkeypatch):
9090
assert payload["execution_block_retryable"] is False
9191

9292

93+
def test_run_endpoint_returns_500_for_retryable_execution_block(monkeypatch):
94+
monkeypatch.setenv("FIRSTRADE_RUN_STRATEGY_ON_HTTP", "true")
95+
monkeypatch.setattr(
96+
main,
97+
"_run_strategy_cycle_with_report",
98+
lambda **_kwargs: {
99+
"ok": False,
100+
"execution_blocked": True,
101+
"execution_block_retryable": True,
102+
"funding_blocked": False,
103+
"error": "Strategy execution blocked; see execution_blocking_skips.",
104+
},
105+
)
106+
client = main.app.test_client()
107+
108+
response = client.post("/run")
109+
110+
assert response.status_code == 500
111+
payload = response.get_json()
112+
assert payload["execution_blocked"] is True
113+
assert payload["execution_block_retryable"] is True
114+
115+
93116
def test_probe_endpoint_is_disabled_without_explicit_http_gate(monkeypatch):
94117
monkeypatch.delenv("FIRSTRADE_RUN_SESSION_CHECK_ON_HTTP", raising=False)
95118
client = main.app.test_client()

0 commit comments

Comments
 (0)