fix: align warning-case blocking semantics across reconcile paths
Co-authored-by: genichin <4282227+genichin@users.noreply.github.com>
This commit is contained in:
co-authored by
genichin
parent
45a5e4157d
commit
310b6f6dec
@@ -29,6 +29,7 @@ from typing import Any, Callable, Optional
|
||||
from uuid import UUID
|
||||
|
||||
from trex_core.application.alarm_emitter import AlarmEmitter
|
||||
from trex_core.application.portfolio import BLOCKING_RECONCILE_CASES
|
||||
from trex_core.application.ports import IntentStore
|
||||
from trex_core.domain.slot import (
|
||||
SlotAdmissionRejectedError,
|
||||
@@ -270,6 +271,24 @@ class IntentService:
|
||||
except Exception: # noqa: BLE001 - alarm path must not affect intent decision
|
||||
pass
|
||||
|
||||
def _is_reconcile_trade_blocked(self, slot_id: str) -> bool:
|
||||
if self._warn_store is None:
|
||||
return False
|
||||
if not self._warn_store.is_warned(slot_id):
|
||||
return False
|
||||
info = self._warn_store.get_warning_info(slot_id)
|
||||
if info is None:
|
||||
return True
|
||||
raw_codes = info.get("case_codes")
|
||||
if isinstance(raw_codes, list):
|
||||
case_codes = {str(code) for code in raw_codes if code}
|
||||
else:
|
||||
case_code = str(info.get("case_code", ""))
|
||||
case_codes = {case_code} if case_code else set()
|
||||
if not case_codes:
|
||||
return True
|
||||
return bool(case_codes & BLOCKING_RECONCILE_CASES)
|
||||
|
||||
# ------------------------------------------------------------
|
||||
def _dedup(self, intent_id: str, intent_type: str, payload: dict) -> Optional[IntentDecision]:
|
||||
"""Return prior decision if intent_id already seen; else None (and reserve it)."""
|
||||
@@ -339,7 +358,7 @@ class IntentService:
|
||||
# those paths call SlotService.sell() directly.
|
||||
if self._warn_store is not None:
|
||||
try:
|
||||
if self._warn_store.is_warned(str(slot.slot_id)):
|
||||
if self._is_reconcile_trade_blocked(str(slot.slot_id)):
|
||||
return IntentDecision(
|
||||
intent.intent_id, "rejected", None, "reconcile_warned"
|
||||
)
|
||||
@@ -432,7 +451,7 @@ class IntentService:
|
||||
# SlotService.sell() / transit_slot() directly without IntentService (OQ-B).
|
||||
if self._warn_store is not None:
|
||||
try:
|
||||
if self._warn_store.is_warned(str(slot.slot_id)):
|
||||
if self._is_reconcile_trade_blocked(str(slot.slot_id)):
|
||||
return IntentDecision(
|
||||
intent.intent_id, "rejected", None, "reconcile_warned"
|
||||
)
|
||||
|
||||
@@ -17,6 +17,7 @@ from trex_core.application.account_snapshot import AccountSnapshotUnavailable
|
||||
from trex_core.application.broker_port import BalanceItem
|
||||
from trex_core.application.messaging import RpcRequest, RpcResponse
|
||||
from trex_core.application.portfolio import (
|
||||
BLOCKING_RECONCILE_CASES,
|
||||
PauseStore,
|
||||
ReconcileWarningStore,
|
||||
SnapshotStore,
|
||||
@@ -270,15 +271,16 @@ def make_slots_rpc_handler(
|
||||
for sid in warned_slot_ids:
|
||||
info = warn_store.get_warning_info(sid)
|
||||
if info is None:
|
||||
continue
|
||||
raise RuntimeError(f"missing warning info for slot_id={sid}")
|
||||
raw_codes = info.get("case_codes")
|
||||
if isinstance(raw_codes, list):
|
||||
codes = {str(code) for code in raw_codes if code}
|
||||
else:
|
||||
case_code = str(info.get("case_code", ""))
|
||||
codes = {case_code} if case_code else set()
|
||||
if codes:
|
||||
warning_cases_by_slot[str(sid)] = codes
|
||||
if not codes:
|
||||
raise RuntimeError(f"empty warning cases for slot_id={sid}")
|
||||
warning_cases_by_slot[str(sid)] = codes
|
||||
except Exception: # noqa: BLE001
|
||||
_log.exception("warn_store warning query failed — fail-closed")
|
||||
return RpcResponse(payload={
|
||||
@@ -290,7 +292,10 @@ def make_slots_rpc_handler(
|
||||
"reconcile warning store unavailable; "
|
||||
"returning no slots (fail-closed)"
|
||||
),
|
||||
"excluded_slots": {"reconcile_quantity_mismatch": 0},
|
||||
"excluded_slots": {
|
||||
"reconcile_quantity_mismatch": 0,
|
||||
"active_reconcile_warning_cases": {},
|
||||
},
|
||||
})
|
||||
|
||||
# Build merged slot rows (REQ-074)
|
||||
@@ -314,7 +319,8 @@ def make_slots_rpc_handler(
|
||||
included: list[tuple[object, dict]] = []
|
||||
for slot, row in zip(slots, slot_rows, strict=True):
|
||||
blocked_holding = (
|
||||
slot.status.value == "holding" and bool(row.get("reconcile_warned"))
|
||||
slot.status.value == "holding"
|
||||
and bool(set(row.get("reconcile_warning_cases", [])) & BLOCKING_RECONCILE_CASES)
|
||||
)
|
||||
if blocked_holding:
|
||||
excluded_count += 1
|
||||
@@ -432,7 +438,7 @@ def make_slots_rpc_handler(
|
||||
if warn_store is not None:
|
||||
base_payload["excluded_slots"] = {
|
||||
"reconcile_quantity_mismatch": excluded_count,
|
||||
"reconcile_warning_cases": excluded_case_counts,
|
||||
"active_reconcile_warning_cases": excluded_case_counts,
|
||||
}
|
||||
return RpcResponse(payload=base_payload)
|
||||
|
||||
|
||||
@@ -21,6 +21,8 @@ from datetime import UTC, datetime
|
||||
from enum import Enum
|
||||
from typing import Protocol
|
||||
|
||||
BLOCKING_RECONCILE_CASES = frozenset({"B", "C"})
|
||||
|
||||
# ---------------------------------------------------------------- portfolio-hub
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -658,7 +660,7 @@ def slot_to_dict(
|
||||
reconcile_warned = bool(warning_cases)
|
||||
d["reconcile_warned"] = reconcile_warned
|
||||
d["reconcile_warning_cases"] = sorted(warning_cases)
|
||||
d["trade_eligible"] = not reconcile_warned
|
||||
d["trade_eligible"] = not bool(warning_cases & BLOCKING_RECONCILE_CASES)
|
||||
|
||||
return d
|
||||
|
||||
|
||||
Reference in New Issue
Block a user