Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Respect retryable funding blocks

When a cycle submits at least one order and then hits an insufficient-cash skip on a later buy, run_strategy_cycle can return both funding_blocked=True and execution_block_retryable=True: terminal funding is only funding_blocked and not action_done, while funding_blocked is still copied into the result. This extra not result.get("funding_blocked") therefore makes those partial-submitted retryable blocks return HTTP 200, so the /run Cloud Scheduler job will not retry even though the result explicitly marks the execution block as retryable.

Useful? React with 👍 / 👎.

return 500
return 200


def _persist_runtime_report(report: dict[str, Any]) -> str | None:
persisted = persist_runtime_report(
report,
Expand Down Expand Up @@ -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 (
Expand Down
23 changes: 23 additions & 0 deletions tests/test_request_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down