458 lines
27 KiB
Python
458 lines
27 KiB
Python
"""Dependency-free downstream contract validation; performs no business actions.
|
|
|
|
Use the exact immutable snapshot text fetched from the snapshot instance.
|
|
Persist sequence/revision watermarks with your business update before XACK.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timedelta
|
|
import hashlib
|
|
import json
|
|
import math
|
|
import re
|
|
from typing import Mapping
|
|
|
|
from .signals import DIRECTIONS, KNOWN_DIRECTIONS, aware_time, lease_until, validate_snapshot
|
|
from .timing import LEGACY_POLICY, admission_until, policy_version
|
|
|
|
|
|
ACTIVE_STATES = frozenset({"OPEN", "CONTINUE"})
|
|
TERMINAL_STATES = frozenset({"RESOLVED", "REVERSED", "EXPIRED", "CLOSED", "RETRACTED"})
|
|
OBSERVATION_NAMESPACE = "mtf:etf:observation:v1"
|
|
OBSERVATION_CONTRACT_VERSION = "etf-observation-1.0"
|
|
OBSERVATION_FIELDS = ("observation_contract_version", "purpose", "model_status",
|
|
"calibration_status", "threshold_version", "review_status", "calibration_review",
|
|
"release_id", "effective_from", "trade_instruction")
|
|
OBSERVATION_EVENT_FIELDS = ("publication_mode", "observation_contract_version", "purpose", "model_status",
|
|
"calibration_status", "threshold_version", "release_id", "trade_instruction")
|
|
|
|
|
|
class ConsumerContractError(ValueError):
|
|
"""Invalid or incomplete transport data; do not acknowledge as a valid signal."""
|
|
|
|
|
|
def _pairs(values):
|
|
result = {}
|
|
for key, value in values:
|
|
if key in result:
|
|
raise ConsumerContractError("DUPLICATE_JSON_FIELD")
|
|
result[key] = value
|
|
return result
|
|
|
|
|
|
def _json(value):
|
|
if not isinstance(value, (str, bytes)):
|
|
raise ConsumerContractError("JSON_TEXT_REQUIRED")
|
|
def invalid_constant(value):
|
|
raise ConsumerContractError("NONFINITE_JSON_NUMBER")
|
|
try:
|
|
result = json.loads(value, object_pairs_hook=_pairs, parse_constant=invalid_constant)
|
|
except (ValueError, UnicodeError) as exc:
|
|
if isinstance(exc, ConsumerContractError):
|
|
raise
|
|
raise ConsumerContractError("INVALID_JSON") from None
|
|
if not isinstance(result, dict):
|
|
raise ConsumerContractError("JSON_OBJECT_REQUIRED")
|
|
try:
|
|
json.dumps(result, allow_nan=False)
|
|
except (ValueError, TypeError):
|
|
raise ConsumerContractError("NONFINITE_JSON_NUMBER") from None
|
|
return result
|
|
|
|
|
|
def decode_stream_entry(fields: Mapping) -> dict:
|
|
"""Decode the producer's single `data` field, with str or bytes clients.
|
|
|
|
Subject identity must be inside the JSON. There is no fallback to outer
|
|
ts_code or another field, avoiding ambiguous producer/consumer contracts.
|
|
"""
|
|
if not isinstance(fields, Mapping):
|
|
raise ConsumerContractError("STREAM_FIELDS_REQUIRED")
|
|
keys = list(fields)
|
|
if keys not in (["data"], [b"data"]):
|
|
raise ConsumerContractError("SINGLE_DATA_ENVELOPE_REQUIRED")
|
|
return _json(fields[keys[0]])
|
|
|
|
|
|
def _require(condition, reason):
|
|
if not condition:
|
|
raise ConsumerContractError(reason)
|
|
|
|
|
|
def _time(value):
|
|
try:
|
|
return aware_time(value)
|
|
except (ValueError, TypeError):
|
|
raise ConsumerContractError("AWARE_TIMESTAMP_REQUIRED") from None
|
|
|
|
|
|
def validate_publication_contract(snapshot: Mapping, namespace: str, manifest: Mapping | None = None) -> dict:
|
|
"""Validate the independent observation contract without approving a model.
|
|
|
|
Legacy shadow/live publications retain their existing policy. Observation
|
|
metadata is committed in both snapshot and manifest, never inferred from a
|
|
consumer option or from the fact that a publication is current.
|
|
"""
|
|
observation = namespace == OBSERVATION_NAMESPACE
|
|
if not observation:
|
|
_require(snapshot.get("publication_mode") != "observation", "OBSERVATION_NAMESPACE_MISMATCH")
|
|
return {}
|
|
_require(snapshot.get("publication_mode") == "observation", "PUBLICATION_MODE_MISMATCH")
|
|
_require(snapshot.get("trade_instruction") is False, "OBSERVATION_IS_NOT_TRADE_INSTRUCTION")
|
|
_require(isinstance(snapshot.get("release_id"), str) and bool(snapshot["release_id"].strip()),
|
|
"INVALID_OBSERVATION_RELEASE_ID")
|
|
_require(_time(snapshot.get("window_end")) >= _time(snapshot.get("effective_from")),
|
|
"WINDOW_PREDATES_OBSERVATION_RELEASE")
|
|
for key, expected in (("observation_contract_version", OBSERVATION_CONTRACT_VERSION),
|
|
("purpose", "MARKET_OBSERVATION"), ("model_status", "uncalibrated"),
|
|
("calibration_status", "PROVISIONAL")):
|
|
_require(snapshot.get(key) == expected, "INVALID_OBSERVATION_" + key.upper())
|
|
threshold = snapshot.get("threshold_version")
|
|
_require(isinstance(threshold, str) and bool(threshold.strip()), "INVALID_OBSERVATION_THRESHOLD_VERSION")
|
|
review = snapshot.get("calibration_review")
|
|
_require(isinstance(review, dict), "INVALID_CALIBRATION_REVIEW")
|
|
_require(type(review.get("sample_count")) is int and review["sample_count"] >= 0
|
|
and type(review.get("target_days")) is int and review["target_days"] > 0
|
|
and type(review.get("eligible_cohort_size")) is int and review["eligible_cohort_size"] >= 0
|
|
and isinstance(review.get("by_subject"), dict), "INVALID_CALIBRATION_SAMPLE_COUNT")
|
|
_require(all(isinstance(k, str) and type(v) is int and v >= 0 for k, v in review["by_subject"].items())
|
|
and review["sample_count"] == min(review["by_subject"].values(), default=0),
|
|
"INVALID_CALIBRATION_SAMPLE_COUNT")
|
|
expected_review = "ACCUMULATING" if review["sample_count"] < review["target_days"] else "REVIEW_DUE"
|
|
_require(snapshot.get("review_status") == expected_review, "INVALID_OBSERVATION_REVIEW_STATUS")
|
|
for subject in snapshot.get("subjects", []):
|
|
_require(isinstance(subject, Mapping), "INVALID_OBSERVATION_SUBJECT")
|
|
observed = subject.get("observation")
|
|
if observed is not None:
|
|
_require(isinstance(observed, Mapping) and observed.get("amount_unit") == "CNY"
|
|
and observed.get("flow_basis") == "TURNOVER_NOT_NET_FLOW", "INVALID_OBSERVED_AMOUNT_BASIS")
|
|
for value_key, status_key, missing in (("amount", "window_amount_status", "MISSING"),
|
|
("cumulative_amount", "cumulative_amount_status", "INCOMPLETE_PREFIX")):
|
|
amount = observed.get(value_key)
|
|
_require(amount is None or (type(amount) in (int, float) and math.isfinite(amount) and amount >= 0),
|
|
"INVALID_OBSERVED_AMOUNT")
|
|
_require(observed.get(status_key) == ("OK" if amount is not None else missing),
|
|
"OBSERVED_AMOUNT_STATUS_MISMATCH")
|
|
if value_key + "_yuan" in observed:
|
|
_require(observed[value_key + "_yuan"] == amount, "OBSERVED_AMOUNT_ALIAS_MISMATCH")
|
|
for scope in ("WINDOW_15M", "SESSION_CUM", "EOD"):
|
|
block = subject.get(scope)
|
|
if block is None:
|
|
continue
|
|
_require(isinstance(block, Mapping) and isinstance(block.get("metrics"), dict),
|
|
"INVALID_OBSERVATION_SCOPE")
|
|
metrics = block["metrics"]
|
|
_require(metrics.get("threshold_version") == threshold, "OBSERVATION_THRESHOLD_MISMATCH")
|
|
direction = block.get("direction")
|
|
_require(isinstance(direction, str) and direction in DIRECTIONS, "UNSUPPORTED_DIRECTION")
|
|
_require(block.get("direction_status") == ("UNAVAILABLE" if direction == "UNKNOWN" else "OK"),
|
|
"INVALID_DIRECTION_STATUS")
|
|
if direction == "UNKNOWN":
|
|
balance_key = "direction_balance" if scope == "WINDOW_15M" else "cumulative_direction_balance"
|
|
_require(all(metrics.get(key) is None for key in
|
|
(balance_key, "pressure_score", "high_buy_pressure_score",
|
|
"high_sell_pressure_score", "low_buy_pressure_score", "low_sell_pressure_score")),
|
|
"UNKNOWN_DIRECTION_HAS_PRESSURE")
|
|
fields = {key: snapshot[key] for key in OBSERVATION_FIELDS}
|
|
if manifest is not None:
|
|
_require(manifest.get("publication_mode") == "observation", "PUBLICATION_MODE_MISMATCH")
|
|
_require(all(json.dumps(manifest.get(key), sort_keys=True, allow_nan=False)
|
|
== json.dumps(value, sort_keys=True, allow_nan=False) for key, value in fields.items()),
|
|
"OBSERVATION_MANIFEST_MISMATCH")
|
|
return fields
|
|
|
|
|
|
def validate_observation_snapshot(manifest: Mapping, snapshot: str | bytes, now: datetime,
|
|
*, last_revision: int = -1) -> dict:
|
|
"""Read numerical observations even when no anomaly event was emitted.
|
|
|
|
This validates a READY round and returns per-subject/per-scope permissions.
|
|
``quality_eligible`` describes the recorded facts; ``observation_eligible``
|
|
additionally requires an unexpired, timely LIVE publication. Neither grants
|
|
trading authority or claims the reference thresholds have been calibrated.
|
|
Keep revision watermarks per namespace and round, never globally per day.
|
|
"""
|
|
_require(isinstance(manifest, Mapping), "OBJECTS_REQUIRED")
|
|
_require(type(last_revision) is int and last_revision >= -1, "INVALID_CONSUMER_WATERMARK")
|
|
snap = _json(snapshot)
|
|
for item in (manifest, snap):
|
|
_require(type(item.get("schema_version")) is int and item["schema_version"] == 1,
|
|
"UNSUPPORTED_SCHEMA_VERSION")
|
|
_require(type(item.get("revision")) is int and item["revision"] >= 0, "INVALID_REVISION")
|
|
_require(manifest.get("status") == "READY", "ROUND_NOT_READY")
|
|
raw = snapshot.encode("utf-8") if isinstance(snapshot, str) else snapshot
|
|
_require(hashlib.sha256(raw).hexdigest() == manifest.get("snapshot_sha256"), "SNAPSHOT_HASH_MISMATCH")
|
|
try:
|
|
end = validate_snapshot(snap)
|
|
version = policy_version(snap)
|
|
manifest_version = policy_version(manifest)
|
|
except (ValueError, TypeError, KeyError):
|
|
raise ConsumerContractError("INVALID_SNAPSHOT") from None
|
|
_require(manifest_version == version, "ROUND_TIMING_POLICY_MISMATCH")
|
|
for key in ("round_id", "trade_date", "revision"):
|
|
_require(manifest.get(key) == snap[key], "ROUND_IDENTITY_MISMATCH")
|
|
suffix = f"{snap['trade_date']}:{snap['round_id']}:r{snap['revision']}"
|
|
_require(manifest.get("manifest_key") == f"{OBSERVATION_NAMESPACE}:manifest:{suffix}"
|
|
and manifest.get("snapshot_key") == f"{OBSERVATION_NAMESPACE}:snapshot:{suffix}",
|
|
"NAMESPACE_OR_KEY_MISMATCH")
|
|
metadata = validate_publication_contract(snap, OBSERVATION_NAMESPACE, manifest)
|
|
_require(_time(manifest.get("window_end")) == end, "WINDOW_END_MISMATCH")
|
|
published = _time(manifest.get("published_at"))
|
|
_require(_time(snap.get("published_at")) == published, "PUBLICATION_TIME_MISMATCH")
|
|
_require(published >= end, "PUBLICATION_PRECEDES_WINDOW")
|
|
expiry = lease_until(end, version)
|
|
_require(_time(manifest.get("valid_until")) == expiry, "INVALID_MANIFEST_LEASE")
|
|
_require(_time(manifest.get("admission_until")) == admission_until(end, version), "INVALID_MANIFEST_ADMISSION")
|
|
_require(manifest.get("mode") == snap.get("mode"), "EVALUATION_MODE_MISMATCH")
|
|
_require(type(manifest.get("live_eligible")) is bool, "INVALID_LIVE_ELIGIBILITY")
|
|
_require(type(manifest.get("session_closed")) is bool and type(snap.get("session_closed")) is bool
|
|
and manifest["session_closed"] == snap["session_closed"] == (end.hour == 15),
|
|
"INVALID_SESSION_CLOSED")
|
|
current = _time(now)
|
|
reason = ("OLD_ROUND_REVISION" if snap["revision"] < last_revision else
|
|
"FUTURE_EVENT" if published > current or end > current else
|
|
"REPLAY_NOT_ACTIONABLE" if snap.get("mode") != "LIVE" else
|
|
"SESSION_CLOSED" if snap["session_closed"] else
|
|
"ADMISSION_DEADLINE_PASSED" if published >= admission_until(end, version) else
|
|
"LEASE_EXPIRED" if current >= expiry else
|
|
"ROUND_NOT_LIVE_ELIGIBLE" if not manifest["live_eligible"] else
|
|
"INSUFFICIENT_DATA" if snap.get("data_status") != "OK" else None)
|
|
scopes, identities = [], set()
|
|
for subject in snap["subjects"]:
|
|
identity = (subject.get("subject_type"), subject.get("subject_id"))
|
|
_require(identity[0] in {"ETF", "INDEX", "MARKET"}
|
|
and isinstance(identity[1], str) and bool(identity[1]), "INVALID_OBSERVATION_SUBJECT")
|
|
_require(identity not in identities, "DUPLICATE_OBSERVATION_SUBJECT")
|
|
identities.add(identity)
|
|
for scope in ("WINDOW_15M", "SESSION_CUM", "EOD"):
|
|
block = subject.get(scope)
|
|
if block is None:
|
|
continue
|
|
amount_key = "amount" if scope == "WINDOW_15M" else "cumulative_amount"
|
|
facts = subject.get("observation")
|
|
value = (facts if isinstance(facts, Mapping) else block["metrics"]).get(amount_key)
|
|
amount_valid = type(value) in (int, float) and math.isfinite(value) and value >= 0
|
|
model_quality = subject.get("data_status") == "OK" and block.get("data_status") == "OK"
|
|
raw_quality = (isinstance(facts, Mapping) and facts.get("amount_unit") == "CNY"
|
|
and facts.get("flow_basis") == "TURNOVER_NOT_NET_FLOW"
|
|
and facts.get("window_amount_status" if scope == "WINDOW_15M" else "cumulative_amount_status") == "OK"
|
|
and subject.get("data_status") in {"OK", "INSUFFICIENT_HISTORY"}
|
|
and block.get("data_status") in {"OK", "INSUFFICIENT_HISTORY"})
|
|
quality = amount_valid and (model_quality or raw_quality)
|
|
direction_available = model_quality and amount_valid and block["direction_status"] == "OK"
|
|
eligible = reason is None and quality
|
|
scopes.append({"subject_type": identity[0], "subject_id": identity[1], "scope": scope,
|
|
"data_status": block.get("data_status"), "quality_eligible": quality,
|
|
"model_scoring_available": model_quality,
|
|
"observation_eligible": eligible, "amount_available": quality,
|
|
"direction_available": direction_available,
|
|
"usable_fields": (["turnover"] + (["price_volume_direction"] if direction_available else []))
|
|
if eligible else []})
|
|
available = reason is None and any(item["observation_eligible"] for item in scopes)
|
|
return dict(metadata, contract_valid=True, namespace=OBSERVATION_NAMESPACE,
|
|
round_id=snap["round_id"], revision=snap["revision"],
|
|
published_at=published.isoformat(), valid_until=expiry.isoformat(),
|
|
observation_eligible=available, trade_instruction=False,
|
|
reason_code=reason or ("CURRENT_OBSERVATION" if available else "NO_USABLE_SCOPE"),
|
|
scopes=scopes, snapshot=snap)
|
|
|
|
|
|
def validate_observation_event_metadata(event: Mapping, snapshot: Mapping):
|
|
_require(all(type(event.get(key)) is type(snapshot.get(key)) and event.get(key) == snapshot.get(key)
|
|
for key in OBSERVATION_EVENT_FIELDS), "OBSERVATION_EVENT_METADATA_MISMATCH")
|
|
|
|
|
|
def read_committed_observation(snapshot_client, event_client, manifest_key: str, *, clock=None,
|
|
last_revision: int = -1) -> dict:
|
|
"""Bounded read from the two Redis instances; no writes, API or task calls.
|
|
|
|
Fetch READY from event Redis, then the exact immutable snapshot from snapshot
|
|
Redis. The caller may supply a clock callable; it is evaluated after reads
|
|
so network delay cannot turn an expired snapshot into a current observation.
|
|
A notification or latest pointer alone is never proof of commitment.
|
|
"""
|
|
pattern = re.escape(OBSERVATION_NAMESPACE) + r":manifest:\d{4}-\d{2}-\d{2}:[A-Za-z0-9_-]+:r\d+"
|
|
_require(isinstance(manifest_key, str) and re.fullmatch(pattern, manifest_key) is not None,
|
|
"INVALID_OBSERVATION_MANIFEST_KEY")
|
|
|
|
def bounded(client, key, maximum):
|
|
size = client.strlen(key)
|
|
_require(type(size) is int and 0 < size <= maximum, "MISSING_OR_OVERSIZED_COMMITTED_DATA")
|
|
raw = client.getrange(key, 0, maximum - 1)
|
|
_require(isinstance(raw, (str, bytes)), "JSON_TEXT_REQUIRED")
|
|
_require(len(raw.encode("utf-8") if isinstance(raw, str) else raw) == size,
|
|
"COMMITTED_DATA_CHANGED_DURING_READ")
|
|
return raw
|
|
|
|
encoded = bounded(event_client, manifest_key, 262_144)
|
|
manifest = _json(encoded)
|
|
_require(manifest.get("status") == "READY", "ROUND_NOT_READY")
|
|
_require(manifest.get("manifest_key") == manifest_key, "NAMESPACE_OR_KEY_MISMATCH")
|
|
expected_key = manifest_key.replace(":manifest:", ":snapshot:", 1)
|
|
_require(manifest.get("snapshot_key") == expected_key, "NAMESPACE_OR_KEY_MISMATCH")
|
|
raw = bounded(snapshot_client, expected_key, 16_777_216)
|
|
_require(bounded(event_client, manifest_key, 262_144) == encoded, "COMMITTED_DATA_CHANGED_DURING_READ")
|
|
result = validate_observation_snapshot(manifest, raw, clock() if clock else datetime.now().astimezone(),
|
|
last_revision=last_revision)
|
|
return dict(result, manifest=manifest)
|
|
|
|
|
|
def validate_envelope(event: Mapping, manifest: Mapping, snapshot: str | bytes,
|
|
now: datetime, *, allow_shadow: bool = False,
|
|
last_event_seq: int = 0, last_revision: int = -1) -> dict:
|
|
"""Validate a committed event and return an eligibility decision.
|
|
|
|
last_event_seq is the durable watermark for this event_id; last_revision is
|
|
the durable watermark for this round_id. A sequence gap is exposed for
|
|
reconciliation; it does not make a later terminal state unsafe to apply.
|
|
Structural failures raise ConsumerContractError. Valid but stale/duplicate
|
|
or shadow events return apply=False with an explicit reason code.
|
|
"""
|
|
_require(isinstance(event, Mapping) and isinstance(manifest, Mapping), "OBJECTS_REQUIRED")
|
|
_require(type(last_event_seq) is int and last_event_seq >= 0
|
|
and type(last_revision) is int and last_revision >= -1, "INVALID_CONSUMER_WATERMARK")
|
|
_require(type(allow_shadow) is bool, "INVALID_SHADOW_POLICY")
|
|
snap = _json(snapshot)
|
|
for item in (event, manifest, snap):
|
|
_require(type(item.get("schema_version")) is int and item["schema_version"] == 1,
|
|
"UNSUPPORTED_SCHEMA_VERSION")
|
|
_require(type(item.get("revision")) is int and item["revision"] >= 0, "INVALID_REVISION")
|
|
try:
|
|
json.dumps(dict(event), allow_nan=False)
|
|
except (ValueError, TypeError):
|
|
raise ConsumerContractError("EVENT_MUST_BE_FINITE_JSON") from None
|
|
_require(manifest.get("status") == "READY", "ROUND_NOT_READY")
|
|
raw = snapshot.encode("utf-8") if isinstance(snapshot, str) else snapshot
|
|
_require(hashlib.sha256(raw).hexdigest() == manifest.get("snapshot_sha256"), "SNAPSHOT_HASH_MISMATCH")
|
|
try:
|
|
end = validate_snapshot(dict(snap))
|
|
except (ValueError, TypeError, KeyError):
|
|
raise ConsumerContractError("INVALID_SNAPSHOT") from None
|
|
try:
|
|
snapshot_policy = policy_version(snap)
|
|
manifest_policy = policy_version(manifest)
|
|
event_policy = policy_version(event)
|
|
except ValueError:
|
|
raise ConsumerContractError("UNSUPPORTED_TIMING_POLICY") from None
|
|
_require(manifest_policy == snapshot_policy, "ROUND_TIMING_POLICY_MISMATCH")
|
|
for name in ("event_id", "round_id", "subject_id", "model_version", "trade_date"):
|
|
_require(isinstance(event.get(name), str) and bool(event[name]), "MISSING_EVENT_IDENTITY")
|
|
_require(event.get("producer_id") == "mtf_etf", "UNKNOWN_PRODUCER")
|
|
_require(event.get("flow_basis") == "price_volume_proxy", "UNSUPPORTED_FLOW_BASIS")
|
|
_require(isinstance(event.get("subject_type"), str)
|
|
and event["subject_type"] in {"ETF", "INDEX", "MARKET"}, "UNSUPPORTED_SUBJECT_TYPE")
|
|
_require(isinstance(event.get("scope"), str)
|
|
and event["scope"] in {"WINDOW_15M", "SESSION_CUM"}, "UNSUPPORTED_EVENT_SCOPE")
|
|
_require(isinstance(event.get("direction"), str)
|
|
and event["direction"] in DIRECTIONS, "UNSUPPORTED_DIRECTION")
|
|
direction_status = "UNAVAILABLE" if event["direction"] == "UNKNOWN" else "OK"
|
|
# Previous known-direction v1 events did not carry this additive field.
|
|
# UNKNOWN is new and must state explicitly that direction is unavailable.
|
|
_require(event.get("direction_status", "OK") == direction_status, "INVALID_DIRECTION_STATUS")
|
|
last_known = event.get("last_known_direction", event["direction"] if event["direction"] in KNOWN_DIRECTIONS else None)
|
|
_require(last_known is None or (isinstance(last_known, str) and last_known in KNOWN_DIRECTIONS),
|
|
"INVALID_LAST_KNOWN_DIRECTION")
|
|
if event["direction"] in KNOWN_DIRECTIONS:
|
|
_require(last_known == event["direction"], "LAST_KNOWN_DIRECTION_MISMATCH")
|
|
_require(type(event.get("severity")) is int and 0 <= event["severity"] <= 3, "INVALID_SEVERITY")
|
|
_require(type(event.get("event_seq")) is int and event["event_seq"] >= 1, "INVALID_EVENT_SEQUENCE")
|
|
_require(type(event.get("revision")) is int and event["revision"] >= 0, "INVALID_REVISION")
|
|
_require(isinstance(event.get("metrics"), dict), "METRICS_OBJECT_REQUIRED")
|
|
_require(isinstance(event.get("reason_codes"), list)
|
|
and all(isinstance(value, str) for value in event["reason_codes"]), "INVALID_REASON_CODES")
|
|
state = event.get("event_state")
|
|
_require(isinstance(state, str) and state in ACTIVE_STATES | TERMINAL_STATES, "UNSUPPORTED_EVENT_STATE")
|
|
if state in ACTIVE_STATES:
|
|
_require(event_policy == snapshot_policy, "ACTIVE_TIMING_POLICY_MISMATCH")
|
|
for name in ("round_id", "revision", "trade_date"):
|
|
_require(event.get(name) == manifest.get(name) == snap.get(name), "ROUND_IDENTITY_MISMATCH")
|
|
_require(_time(event.get("window_end")) == _time(manifest.get("window_end")) == end,
|
|
"WINDOW_END_MISMATCH")
|
|
_require(_time(event.get("window_start")) == end - timedelta(minutes=15), "WINDOW_START_MISMATCH")
|
|
published = _time(event.get("published_at"))
|
|
_require(published == _time(manifest.get("published_at")), "PUBLICATION_TIME_MISMATCH")
|
|
_require(published >= end, "PUBLICATION_PRECEDES_WINDOW")
|
|
valid_until = _time(event.get("valid_until"))
|
|
opened = _time(event.get("opened_at"))
|
|
_require(opened.date() == end.date() and opened <= end and opened <= valid_until <= lease_until(end, event_policy),
|
|
"INVALID_EVENT_LEASE")
|
|
_require(_time(manifest.get("valid_until")) == lease_until(end, snapshot_policy), "INVALID_MANIFEST_LEASE")
|
|
if manifest_policy != LEGACY_POLICY or "admission_until" in manifest:
|
|
_require(_time(manifest.get("admission_until")) == admission_until(end, snapshot_policy),
|
|
"INVALID_MANIFEST_ADMISSION")
|
|
_require(type(manifest.get("live_eligible")) is bool, "INVALID_LIVE_ELIGIBILITY")
|
|
manifest_key = manifest.get("manifest_key")
|
|
_require(isinstance(manifest_key, str) and ":manifest:" in manifest_key, "INVALID_MANIFEST_KEY")
|
|
namespace = manifest_key.split(":manifest:", 1)[0]
|
|
_require(namespace in {"mtf:etf:shadow:v1", "mtf:etf:live:v1", OBSERVATION_NAMESPACE}, "UNSUPPORTED_NAMESPACE")
|
|
suffix = f"{event['trade_date']}:{event['round_id']}:r{event['revision']}"
|
|
_require(manifest_key == f"{namespace}:manifest:{suffix}"
|
|
and manifest.get("snapshot_key") == f"{namespace}:snapshot:{suffix}", "NAMESPACE_OR_KEY_MISMATCH")
|
|
deployment_mode = namespace.split(":")[2]
|
|
_require(snap.get("publication_mode", deployment_mode) == deployment_mode, "PUBLICATION_MODE_MISMATCH")
|
|
_require(manifest.get("mode") == snap.get("mode", "LIVE"), "EVALUATION_MODE_MISMATCH")
|
|
metadata = validate_publication_contract(snap, namespace, manifest)
|
|
if deployment_mode == "observation":
|
|
validate_observation_event_metadata(event, snap)
|
|
observation = (validate_observation_snapshot(manifest, snapshot, now, last_revision=last_revision)
|
|
if deployment_mode == "observation" else None)
|
|
|
|
result = {**metadata, "apply": False, "active_eligible": False, "terminal": state in TERMINAL_STATES,
|
|
"reason_code": None, "namespace": namespace, "event_id": event["event_id"],
|
|
"event_seq": event["event_seq"], "round_id": event["round_id"],
|
|
"revision": event["revision"], "event_state": state,
|
|
"direction": event["direction"], "direction_status": direction_status,
|
|
"direction_available": direction_status == "OK",
|
|
"last_known_direction": last_known,
|
|
"timing_policy_version": event_policy,
|
|
"sequence_gap": event["event_seq"] > last_event_seq + 1}
|
|
reason = None
|
|
current = _time(now)
|
|
if deployment_mode == "shadow" and not allow_shadow:
|
|
reason = "SHADOW_NAMESPACE_REJECTED"
|
|
elif event["event_seq"] <= last_event_seq:
|
|
reason = "DUPLICATE_OR_OLD_SEQUENCE"
|
|
elif event["revision"] < last_revision:
|
|
reason = "OLD_ROUND_REVISION"
|
|
elif published > current or end > current:
|
|
reason = "FUTURE_EVENT"
|
|
elif manifest.get("mode") != "LIVE":
|
|
reason = "REPLAY_NOT_ACTIONABLE"
|
|
elif state in TERMINAL_STATES:
|
|
# Closing/retracting a previously applied event remains useful after
|
|
# its lease expires and when an EOD manifest has live_eligible=False.
|
|
result["apply"] = True
|
|
reason = "TERMINAL_EVENT"
|
|
elif published >= admission_until(end, event_policy):
|
|
reason = "ADMISSION_DEADLINE_PASSED"
|
|
elif current >= valid_until:
|
|
reason = "LEASE_EXPIRED"
|
|
elif not manifest["live_eligible"]:
|
|
reason = "ROUND_NOT_LIVE_ELIGIBLE"
|
|
elif deployment_mode == "live" and snap.get("model_status") != "calibrated":
|
|
reason = "MODEL_NOT_CALIBRATED"
|
|
elif observation is not None and not observation["observation_eligible"]:
|
|
reason = observation["reason_code"]
|
|
else:
|
|
_require(event["model_version"] == snap["model_version"], "ACTIVE_MODEL_MISMATCH")
|
|
subjects = [item for item in snap["subjects"]
|
|
if item.get("subject_type") == event["subject_type"]
|
|
and item.get("subject_id") == event["subject_id"]]
|
|
_require(len(subjects) == 1, "ACTIVE_SUBJECT_MISSING_OR_DUPLICATE")
|
|
evaluation = subjects[0].get(event["scope"], {})
|
|
_require(subjects[0].get("data_status") == "OK"
|
|
and evaluation.get("data_status", "OK") == "OK"
|
|
and evaluation.get("anomaly") is True, "ACTIVE_EVIDENCE_MISSING")
|
|
_require(event["direction"] == evaluation.get("direction", "UNKNOWN")
|
|
and direction_status == evaluation.get("direction_status", direction_status)
|
|
and event["severity"] == evaluation.get("severity", 0)
|
|
and event["metrics"] == evaluation.get("metrics", {}), "ACTIVE_EVIDENCE_MISMATCH")
|
|
result.update(apply=True, active_eligible=True)
|
|
reason = "ACTIVE_EVENT"
|
|
result["reason_code"] = reason
|
|
if observation is not None:
|
|
result.update(observation_eligible=result["active_eligible"], trade_instruction=False,
|
|
usable_fields=(["turnover"] + (["price_volume_direction"] if direction_status == "OK" else []))
|
|
if result["active_eligible"] else [])
|
|
return result
|