fix(kis): collect all parent candidates before child evaluation; boundary case tests
Co-authored-by: genichin <4282227+genichin@users.noreply.github.com>
This commit is contained in:
co-authored by
genichin
parent
584e08eb36
commit
f3fbc179b0
@@ -418,15 +418,51 @@ class KISBroker:
|
||||
request_brno = broker_order_id.split(":")[0] if ":" in broker_order_id else ""
|
||||
request_date = inquiry_date # == order_date
|
||||
request_symbol = symbol
|
||||
row = next(
|
||||
(
|
||||
r
|
||||
for r in rows
|
||||
if (str(r.get("odno") or "").strip().lstrip("0") or "0") == normalized_odno
|
||||
),
|
||||
None,
|
||||
)
|
||||
if row is None:
|
||||
|
||||
def _row_odno_normalized(r: dict) -> str:
|
||||
return str(r.get("odno") or "").strip().lstrip("0") or "0"
|
||||
|
||||
def _row_matches_request_identity(r: dict) -> bool:
|
||||
"""True when a row's identity fields are present and agree with the
|
||||
authoritative request identity. An absent field in the row is
|
||||
treated as a mismatch (fail-closed), not as a wildcard."""
|
||||
row_brno = str(r.get("ord_gno_brno") or "").strip()
|
||||
row_dt = str(r.get("ord_dt") or "").strip()
|
||||
row_pdno = str(r.get("pdno") or "").strip()
|
||||
# brno: required only when request carries one; if request has no
|
||||
# brno (legacy odno-only format) skip the check to preserve
|
||||
# backwards compatibility.
|
||||
if request_brno and (not row_brno or row_brno != request_brno):
|
||||
return False
|
||||
# date and symbol: always required — these never legitimately vary
|
||||
# for the same order on the same inquiry date.
|
||||
if not row_dt or row_dt != request_date:
|
||||
return False
|
||||
return bool(row_pdno) and row_pdno == request_symbol
|
||||
|
||||
# Collect ALL rows whose odno matches. We must then require exactly
|
||||
# one that also passes the full identity check. Using next() here
|
||||
# would accept the first odno-match regardless of identity and stop
|
||||
# the search, missing authoritative rows that come later and ignoring
|
||||
# the ambiguous-parent edge case.
|
||||
odno_matches = [
|
||||
r for r in rows if _row_odno_normalized(r) == normalized_odno
|
||||
]
|
||||
exact_parent_matches = [r for r in odno_matches if _row_matches_request_identity(r)]
|
||||
|
||||
if len(exact_parent_matches) == 0:
|
||||
# No row with the exact request identity. If there are odno
|
||||
# matches they all failed identity validation → unknown (not grace).
|
||||
if odno_matches:
|
||||
return FillStatus(
|
||||
state="unknown",
|
||||
raw={
|
||||
"reason": "parent_identity_mismatch",
|
||||
"order_visible": True,
|
||||
"inquiry_date": inquiry_date,
|
||||
},
|
||||
)
|
||||
# Truly invisible — apply the submission visibility grace window.
|
||||
visible_grace = (
|
||||
submitted_at is None
|
||||
or (self._clock().astimezone(UTC) - submitted_at.astimezone(UTC))
|
||||
@@ -445,40 +481,20 @@ class KISBroker:
|
||||
"inquiry_date": inquiry_date,
|
||||
},
|
||||
)
|
||||
# Validate that the matched parent row's identity agrees with the
|
||||
# authoritative request identity. KIS can return stale rows for
|
||||
# reused odno values; a branch, date, or symbol mismatch means this
|
||||
# row does not belong to our order → fail-closed as unknown.
|
||||
row_brno = str(row.get("ord_gno_brno") or "").strip()
|
||||
row_dt = str(row.get("ord_dt") or "").strip()
|
||||
row_pdno = str(row.get("pdno") or "").strip()
|
||||
if request_brno and row_brno and row_brno != request_brno:
|
||||
|
||||
if len(exact_parent_matches) > 1:
|
||||
# More than one row claims the same exact identity — ambiguous.
|
||||
return FillStatus(
|
||||
state="unknown",
|
||||
raw={
|
||||
"reason": "parent_identity_mismatch_brno",
|
||||
"order_visible": True,
|
||||
"inquiry_date": inquiry_date,
|
||||
},
|
||||
)
|
||||
if row_dt and row_dt != request_date:
|
||||
return FillStatus(
|
||||
state="unknown",
|
||||
raw={
|
||||
"reason": "parent_identity_mismatch_date",
|
||||
"order_visible": True,
|
||||
"inquiry_date": inquiry_date,
|
||||
},
|
||||
)
|
||||
if row_pdno and row_pdno != request_symbol:
|
||||
return FillStatus(
|
||||
state="unknown",
|
||||
raw={
|
||||
"reason": "parent_identity_mismatch_symbol",
|
||||
"reason": "parent_identity_ambiguous",
|
||||
"order_visible": True,
|
||||
"inquiry_date": inquiry_date,
|
||||
},
|
||||
)
|
||||
|
||||
row = exact_parent_matches[0]
|
||||
|
||||
# dcy-013 ext: KIS sometimes records an external cancel as a separate
|
||||
# child row with orgn_odno=<original> and cncl_yn="Y" rather than
|
||||
# setting cncl_yn on the original row. Scan sibling rows to detect
|
||||
@@ -486,24 +502,24 @@ class KISBroker:
|
||||
#
|
||||
# Safety requirements:
|
||||
# - Child must share the AUTHORITATIVE request identity (branch, date,
|
||||
# symbol from the caller), NOT the row values. Using row values as
|
||||
# ground truth would let a mismatched row pass its own wrong values
|
||||
# back as the filter reference.
|
||||
# symbol from the caller). All identity fields must be present and
|
||||
# equal — absent fields are NOT treated as wildcards (fail-closed).
|
||||
# - Ambiguous (> 1 matching cancel) → fail-closed "unknown".
|
||||
# - Correction (정정) children (cncl_yn != "Y") that match the same
|
||||
# request identity are also fail-closed → "unknown".
|
||||
# - Unrelated rows that only share orgn_odno but differ in branch/date/
|
||||
# symbol are excluded from both candidate lists.
|
||||
# - Unrelated rows whose identity fields differ from the request are
|
||||
# excluded from both candidate lists.
|
||||
def _child_matches_request(r: dict) -> bool:
|
||||
"""True when a child row is bound to the same order identity as the request."""
|
||||
"""True when a child row's identity fields are all present and
|
||||
equal to the authoritative request identity. Missing = mismatch."""
|
||||
child_brno = str(r.get("ord_gno_brno") or "").strip()
|
||||
child_dt = str(r.get("ord_dt") or "").strip()
|
||||
child_pdno = str(r.get("pdno") or "").strip()
|
||||
if request_brno and child_brno and child_brno != request_brno:
|
||||
if request_brno and (not child_brno or child_brno != request_brno):
|
||||
return False
|
||||
if child_dt and child_dt != request_date:
|
||||
if not child_dt or child_dt != request_date:
|
||||
return False
|
||||
return not (child_pdno and child_pdno != request_symbol)
|
||||
return bool(child_pdno) and child_pdno == request_symbol
|
||||
|
||||
child_cancel_candidates = [
|
||||
r
|
||||
|
||||
Reference in New Issue
Block a user