|
| 1 | +"""Bounded execution-receipt facts derived from LongBridge cycle results.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +from quant_platform_kit.common.execution_receipts import ( |
| 8 | + attach_runtime_execution_receipt, |
| 9 | + resolve_execution_receipt_fact, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +def attach_cycle_execution_receipt( |
| 14 | + report: dict[str, Any], |
| 15 | + cycle_result: object, |
| 16 | +) -> dict[str, Any]: |
| 17 | + """Attach only facts that the LongBridge cycle result explicitly carries. |
| 18 | +
|
| 19 | + ``action_done`` confirms a local order-submission step, not a broker fill. |
| 20 | + A populated ``pending_orders`` list wins over it and produces a |
| 21 | + reconciliation requirement instead. |
| 22 | + """ |
| 23 | + |
| 24 | + pending_orders = tuple(getattr(cycle_result, "pending_orders", ()) or ()) |
| 25 | + outcome, confirmation = resolve_execution_receipt_fact( |
| 26 | + dry_run=bool(report.get("dry_run")), |
| 27 | + submission_attempted=bool(getattr(cycle_result, "action_done", False)), |
| 28 | + reconciliation_required=bool(pending_orders), |
| 29 | + ) |
| 30 | + return attach_runtime_execution_receipt( |
| 31 | + report, |
| 32 | + outcome=outcome, |
| 33 | + broker_confirmation=confirmation, |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +def attach_terminal_fallback_execution_receipt(report: dict[str, Any]) -> dict[str, Any]: |
| 38 | + """Attach a conservative fact when the cycle exits before a result exists.""" |
| 39 | + |
| 40 | + failed = str(report.get("status") or "").strip().lower() == "error" |
| 41 | + outcome, confirmation = resolve_execution_receipt_fact( |
| 42 | + dry_run=bool(report.get("dry_run")), |
| 43 | + submission_attempted=failed, |
| 44 | + failed=failed, |
| 45 | + ) |
| 46 | + return attach_runtime_execution_receipt( |
| 47 | + report, |
| 48 | + outcome=outcome, |
| 49 | + broker_confirmation=confirmation, |
| 50 | + ) |
0 commit comments