fix(execution): fail closed on unresolved orders
This commit is contained in:
@@ -143,7 +143,9 @@ def test_poll_fill_uses_current_all_market_contract(monkeypatch):
|
||||
|
||||
monkeypatch.setattr("trex_core.adapters.broker.kis.httpx.get", fake_get)
|
||||
|
||||
status = broker.poll_fill("91258:1234", side="buy", symbol="005930")
|
||||
status = broker.poll_fill(
|
||||
"91258:1234", side="buy", symbol="005930", order_date="20260608"
|
||||
)
|
||||
|
||||
assert status.state == "accepted"
|
||||
assert seen["tr_id"] == "VTTC0081R"
|
||||
@@ -578,8 +580,12 @@ def test_fill_poll_transport_failure_uses_backoff(monkeypatch):
|
||||
|
||||
monkeypatch.setattr("trex_core.adapters.broker.kis.httpx.get", fail)
|
||||
|
||||
first = b.poll_fill("91258:1234", side="buy", symbol="005930")
|
||||
immediate = b.poll_fill("91258:1234", side="buy", symbol="005930")
|
||||
first = b.poll_fill(
|
||||
"91258:1234", side="buy", symbol="005930", order_date="20260608"
|
||||
)
|
||||
immediate = b.poll_fill(
|
||||
"91258:1234", side="buy", symbol="005930", order_date="20260608"
|
||||
)
|
||||
|
||||
assert first.state == "unknown"
|
||||
assert immediate.state == "accepted"
|
||||
@@ -587,6 +593,8 @@ def test_fill_poll_transport_failure_uses_backoff(monkeypatch):
|
||||
assert len(calls) == 1
|
||||
|
||||
clock[0] += 31
|
||||
again = b.poll_fill("91258:1234", side="buy", symbol="005930")
|
||||
again = b.poll_fill(
|
||||
"91258:1234", side="buy", symbol="005930", order_date="20260608"
|
||||
)
|
||||
assert again.state == "unknown"
|
||||
assert len(calls) == 2
|
||||
|
||||
@@ -59,13 +59,17 @@ def _row(**kw):
|
||||
|
||||
def test_full_external_cancel_maps_to_cancelled(monkeypatch):
|
||||
monkeypatch.setattr(kis_mod.httpx, "get", _fake_get(_row(tot_ccld_qty="0", cncl_yn="Y")))
|
||||
fs = _broker().poll_fill(f"06010:{ODNO}", side="sell", symbol="005930")
|
||||
fs = _broker().poll_fill(
|
||||
f"06010:{ODNO}", side="sell", symbol="005930", order_date="20260608"
|
||||
)
|
||||
assert fs.state == "cancelled" and fs.is_terminal_reject and not fs.is_partial_cancel
|
||||
|
||||
|
||||
def test_zero_fill_not_cancelled_stays_accepted(monkeypatch):
|
||||
monkeypatch.setattr(kis_mod.httpx, "get", _fake_get(_row(tot_ccld_qty="0", cncl_yn="N")))
|
||||
fs = _broker().poll_fill(f"06010:{ODNO}", side="sell", symbol="005930")
|
||||
fs = _broker().poll_fill(
|
||||
f"06010:{ODNO}", side="sell", symbol="005930", order_date="20260608"
|
||||
)
|
||||
assert fs.state == "accepted" and not fs.is_terminal_reject
|
||||
|
||||
|
||||
@@ -83,7 +87,9 @@ def test_zero_fill_full_rejection_maps_to_rejected(monkeypatch):
|
||||
)
|
||||
),
|
||||
)
|
||||
fs = _broker().poll_fill(f"06010:{ODNO}", side="buy", symbol="005930")
|
||||
fs = _broker().poll_fill(
|
||||
f"06010:{ODNO}", side="buy", symbol="005930", order_date="20260608"
|
||||
)
|
||||
assert fs.state == "rejected" and fs.is_terminal_reject
|
||||
|
||||
|
||||
@@ -93,7 +99,9 @@ def test_zero_fill_partial_rejection_does_not_infer_terminal(monkeypatch):
|
||||
"get",
|
||||
_fake_get(_row(ord_qty="10", tot_ccld_qty="0", rjct_qty="4", cncl_yn="")),
|
||||
)
|
||||
fs = _broker().poll_fill(f"06010:{ODNO}", side="buy", symbol="005930")
|
||||
fs = _broker().poll_fill(
|
||||
f"06010:{ODNO}", side="buy", symbol="005930", order_date="20260608"
|
||||
)
|
||||
assert fs.state == "accepted" and not fs.is_terminal_reject
|
||||
|
||||
|
||||
@@ -103,7 +111,9 @@ def test_partial_fill_then_cancel_flags_partial_cancel(monkeypatch):
|
||||
"get",
|
||||
_fake_get(_row(ord_qty="10", tot_ccld_qty="4", avg_prvs="70000", cncl_yn="Y")),
|
||||
)
|
||||
fs = _broker().poll_fill(f"06010:{ODNO}", side="sell", symbol="005930")
|
||||
fs = _broker().poll_fill(
|
||||
f"06010:{ODNO}", side="sell", symbol="005930", order_date="20260608"
|
||||
)
|
||||
assert fs.state == "partially_filled"
|
||||
assert fs.cancelled and fs.is_partial_cancel
|
||||
assert fs.filled_qty == 4 and fs.avg_price == 70000.0
|
||||
@@ -116,5 +126,7 @@ def test_full_fill_unaffected_by_cancel_flag(monkeypatch):
|
||||
"get",
|
||||
_fake_get(_row(ord_qty="10", tot_ccld_qty="10", avg_prvs="70000", cncl_yn="N")),
|
||||
)
|
||||
fs = _broker().poll_fill(f"06010:{ODNO}", side="sell", symbol="005930")
|
||||
fs = _broker().poll_fill(
|
||||
f"06010:{ODNO}", side="sell", symbol="005930", order_date="20260608"
|
||||
)
|
||||
assert fs.is_filled and fs.filled_qty == 10 and not fs.is_partial_cancel
|
||||
|
||||
@@ -218,6 +218,12 @@ def test_legacy_pending_without_authoritative_metadata_is_fail_closed_and_alarm_
|
||||
order_date=None,
|
||||
updated_at=datetime.now(tz=timezone.utc) - timedelta(days=2),
|
||||
)
|
||||
broker.responder = lambda _side: FillStatus(
|
||||
state="filled",
|
||||
filled_qty=1,
|
||||
avg_price=1000,
|
||||
execution_id="wrong-current-date-order",
|
||||
)
|
||||
|
||||
execution.run_once()
|
||||
execution.run_once()
|
||||
@@ -225,6 +231,7 @@ def test_legacy_pending_without_authoritative_metadata_is_fail_closed_and_alarm_
|
||||
persisted = repo.fetch_slot(slot.slot_id)
|
||||
assert persisted.status is SlotState.PENDING_BUY
|
||||
assert broker.cancel_calls == []
|
||||
|
||||
metadata_alarms = [
|
||||
call for call in alarms.calls if call[0] == "order_reconciliation_metadata_unknown"
|
||||
]
|
||||
@@ -232,6 +239,49 @@ def test_legacy_pending_without_authoritative_metadata_is_fail_closed_and_alarm_
|
||||
assert set(metadata_alarms[0][2]) == {"slot_id", "symbol"}
|
||||
|
||||
|
||||
def test_requested_cancel_terminal_unresolved_alarm_is_deduped_per_process_and_reemitted_after_restart():
|
||||
broker = _RecordingBroker()
|
||||
repo, service, intents = _wire()
|
||||
alarms = _AlarmRecorder()
|
||||
execution = ExecutionService(service, repo, broker, alarm_emitter=alarms)
|
||||
decision = intents.submit_buy(
|
||||
BuyIntent(intent_id="requested", portfolio_id="PF-1", qty=1, timeout_sec=1)
|
||||
)
|
||||
broker.responder = lambda _side: FillStatus(state="accepted")
|
||||
execution.run_once()
|
||||
slot = repo.fetch_slot(decision.slot_id)
|
||||
repo._slots[slot.slot_id] = replace(
|
||||
slot,
|
||||
order_submitted_at=datetime.now(tz=timezone.utc) - timedelta(minutes=1),
|
||||
)
|
||||
execution.run_once()
|
||||
assert repo.fetch_slot(slot.slot_id).cancel_state == "requested"
|
||||
assert len(broker.cancel_calls) == 1
|
||||
|
||||
broker.responder = lambda _side: FillStatus(
|
||||
state="unknown",
|
||||
raw={
|
||||
"reason": "order_not_visible_unresolved",
|
||||
"raw_payload": {"authorization": "secret"},
|
||||
},
|
||||
)
|
||||
execution.run_once()
|
||||
execution.run_once()
|
||||
|
||||
persisted = repo.fetch_slot(slot.slot_id)
|
||||
assert persisted.status is SlotState.PENDING_BUY
|
||||
assert len(broker.cancel_calls) == 1
|
||||
unresolved = [call for call in alarms.calls if call[0] == "order_cancel_unresolved"]
|
||||
assert len(unresolved) == 1
|
||||
assert unresolved[0][1] == "critical"
|
||||
assert set(unresolved[0][2]) == {"slot_id", "symbol", "cancel_state", "order_date"}
|
||||
assert "secret" not in repr(unresolved[0])
|
||||
|
||||
ExecutionService(service, repo, broker, alarm_emitter=alarms).run_once()
|
||||
assert len(broker.cancel_calls) == 1
|
||||
assert len([call for call in alarms.calls if call[0] == "order_cancel_unresolved"]) == 2
|
||||
|
||||
|
||||
def test_cancel_rejected_is_fail_closed_alarm_and_authoritative_bounded_retry():
|
||||
broker = _RecordingBroker()
|
||||
broker.cancel_outcome = CancelOutcome(CancelState.REJECTED, "closed_session")
|
||||
@@ -340,3 +390,24 @@ def test_kis_recent_missing_row_stays_in_bounded_visibility_grace(monkeypatch):
|
||||
assert status.state == "accepted"
|
||||
assert status.raw["reason"] == "order_visibility_grace"
|
||||
assert status.raw["order_visible"] is False
|
||||
|
||||
|
||||
def test_kis_missing_authoritative_order_date_fails_closed_without_network(monkeypatch):
|
||||
now = datetime(2026, 8, 15, 9, 0, tzinfo=_KST)
|
||||
|
||||
def forbidden_get(*_args, **_kwargs):
|
||||
raise AssertionError("legacy order without original date must not query KIS")
|
||||
|
||||
monkeypatch.setattr(kis_mod.httpx, "get", forbidden_get)
|
||||
status = _kis(lambda: now).poll_fill(
|
||||
"06010:0000123456",
|
||||
side="buy",
|
||||
symbol="005930",
|
||||
order_date=None,
|
||||
submitted_at=None,
|
||||
)
|
||||
|
||||
assert status.state == "unknown"
|
||||
assert not status.is_filled
|
||||
assert not status.is_terminal_reject
|
||||
assert status.raw == {"reason": "order_reconciliation_metadata_unknown"}
|
||||
|
||||
Reference in New Issue
Block a user