|
| 1 | +"""Safe, immutable bindings for strategy decision-data artifacts. |
| 2 | +
|
| 3 | +Decision data is deliberately distinct from execution-time quotes. A binding |
| 4 | +contains only stable identifiers and evidence hashes, never provider URLs, |
| 5 | +credentials, account identifiers, or market-data payloads. This lets a |
| 6 | +runtime prove which frozen input it used without exposing private operational |
| 7 | +details through a control plane or an execution report. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +from dataclasses import dataclass |
| 13 | +from datetime import date |
| 14 | +from hashlib import sha256 |
| 15 | +import json |
| 16 | +import re |
| 17 | +from typing import Any, Mapping |
| 18 | + |
| 19 | + |
| 20 | +DECISION_DATA_BINDING_SCHEMA_VERSION = "qpk.decision_data_binding.v1" |
| 21 | + |
| 22 | +DECISION_DATA_MODE_LEGACY_RUNTIME_FETCH = "legacy_runtime_fetch" |
| 23 | +DECISION_DATA_MODE_ARTIFACT_OPTIONAL = "artifact_optional" |
| 24 | +DECISION_DATA_MODE_ARTIFACT_REQUIRED = "artifact_required" |
| 25 | + |
| 26 | +DECISION_DATA_ASSURANCE_LEGACY = "LEGACY" |
| 27 | +DECISION_DATA_ASSURANCE_VERIFIED = "VERIFIED" |
| 28 | +DECISION_DATA_ASSURANCE_DEGRADED = "DEGRADED" |
| 29 | +DECISION_DATA_ASSURANCE_PARKED = "PARKED" |
| 30 | + |
| 31 | +_IDENTIFIER_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._:-]{1,127}$") |
| 32 | +_SHA256_RE = re.compile(r"^[0-9a-f]{64}$") |
| 33 | +_MODES = frozenset( |
| 34 | + { |
| 35 | + DECISION_DATA_MODE_LEGACY_RUNTIME_FETCH, |
| 36 | + DECISION_DATA_MODE_ARTIFACT_OPTIONAL, |
| 37 | + DECISION_DATA_MODE_ARTIFACT_REQUIRED, |
| 38 | + } |
| 39 | +) |
| 40 | +_ASSURANCE_STATUSES = frozenset( |
| 41 | + { |
| 42 | + DECISION_DATA_ASSURANCE_LEGACY, |
| 43 | + DECISION_DATA_ASSURANCE_VERIFIED, |
| 44 | + DECISION_DATA_ASSURANCE_DEGRADED, |
| 45 | + DECISION_DATA_ASSURANCE_PARKED, |
| 46 | + } |
| 47 | +) |
| 48 | + |
| 49 | + |
| 50 | +def _canonical_bytes(value: object) -> bytes: |
| 51 | + return json.dumps( |
| 52 | + value, |
| 53 | + allow_nan=False, |
| 54 | + ensure_ascii=True, |
| 55 | + separators=(",", ":"), |
| 56 | + sort_keys=True, |
| 57 | + ).encode("ascii") |
| 58 | + |
| 59 | + |
| 60 | +def _require_identifier(value: object, *, field_name: str) -> str: |
| 61 | + text = str(value or "").strip() |
| 62 | + if not _IDENTIFIER_RE.fullmatch(text): |
| 63 | + raise ValueError(f"{field_name} must be a stable identifier") |
| 64 | + return text |
| 65 | + |
| 66 | + |
| 67 | +def _require_date(value: object, *, field_name: str) -> str: |
| 68 | + text = str(value or "").strip() |
| 69 | + try: |
| 70 | + return date.fromisoformat(text).isoformat() |
| 71 | + except ValueError as exc: |
| 72 | + raise ValueError(f"{field_name} must be an ISO-8601 date") from exc |
| 73 | + |
| 74 | + |
| 75 | +def _require_sha256(value: object, *, field_name: str) -> str: |
| 76 | + text = str(value or "").strip().lower().removeprefix("sha256:") |
| 77 | + if not _SHA256_RE.fullmatch(text): |
| 78 | + raise ValueError(f"{field_name} must be a SHA-256 digest") |
| 79 | + return text |
| 80 | + |
| 81 | + |
| 82 | +@dataclass(frozen=True) |
| 83 | +class DecisionDataBinding: |
| 84 | + """A redacted, versioned reference to one strategy decision-data input. |
| 85 | +
|
| 86 | + ``legacy_runtime_fetch`` exists only for an explicit, observable migration |
| 87 | + period. Artifact modes require a content hash, cutoff date, adjustment |
| 88 | + basis, and source identities. A caller resolves the actual private |
| 89 | + artifact location through its own environment, not through this contract. |
| 90 | + """ |
| 91 | + |
| 92 | + binding_id: str |
| 93 | + strategy_scope: str |
| 94 | + mode: str |
| 95 | + source_ids: tuple[str, ...] = () |
| 96 | + as_of: str | None = None |
| 97 | + adjustment_basis: str | None = None |
| 98 | + artifact_sha256: str | None = None |
| 99 | + assurance_status: str = DECISION_DATA_ASSURANCE_LEGACY |
| 100 | + schema_version: str = DECISION_DATA_BINDING_SCHEMA_VERSION |
| 101 | + |
| 102 | + def __post_init__(self) -> None: |
| 103 | + object.__setattr__(self, "binding_id", _require_identifier(self.binding_id, field_name="binding_id")) |
| 104 | + object.__setattr__(self, "strategy_scope", _require_identifier(self.strategy_scope, field_name="strategy_scope")) |
| 105 | + |
| 106 | + mode = str(self.mode or "").strip() |
| 107 | + if mode not in _MODES: |
| 108 | + raise ValueError("mode is unsupported") |
| 109 | + object.__setattr__(self, "mode", mode) |
| 110 | + |
| 111 | + schema_version = str(self.schema_version or "").strip() |
| 112 | + if schema_version != DECISION_DATA_BINDING_SCHEMA_VERSION: |
| 113 | + raise ValueError("schema_version is unsupported") |
| 114 | + object.__setattr__(self, "schema_version", schema_version) |
| 115 | + |
| 116 | + source_ids = tuple(_require_identifier(value, field_name="source_ids[]") for value in self.source_ids) |
| 117 | + if len(set(source_ids)) != len(source_ids): |
| 118 | + raise ValueError("source_ids must not contain duplicates") |
| 119 | + object.__setattr__(self, "source_ids", source_ids) |
| 120 | + |
| 121 | + assurance_status = str(self.assurance_status or "").strip().upper() |
| 122 | + if assurance_status not in _ASSURANCE_STATUSES: |
| 123 | + raise ValueError("assurance_status is unsupported") |
| 124 | + object.__setattr__(self, "assurance_status", assurance_status) |
| 125 | + |
| 126 | + if mode == DECISION_DATA_MODE_LEGACY_RUNTIME_FETCH: |
| 127 | + if self.artifact_sha256 is not None or self.as_of is not None or self.adjustment_basis is not None: |
| 128 | + raise ValueError("legacy_runtime_fetch must not claim an immutable artifact") |
| 129 | + if assurance_status != DECISION_DATA_ASSURANCE_LEGACY: |
| 130 | + raise ValueError("legacy_runtime_fetch must use LEGACY assurance_status") |
| 131 | + return |
| 132 | + |
| 133 | + if not source_ids: |
| 134 | + raise ValueError("artifact decision-data modes require source_ids") |
| 135 | + object.__setattr__(self, "as_of", _require_date(self.as_of, field_name="as_of")) |
| 136 | + object.__setattr__( |
| 137 | + self, |
| 138 | + "adjustment_basis", |
| 139 | + _require_identifier(self.adjustment_basis, field_name="adjustment_basis"), |
| 140 | + ) |
| 141 | + object.__setattr__( |
| 142 | + self, |
| 143 | + "artifact_sha256", |
| 144 | + _require_sha256(self.artifact_sha256, field_name="artifact_sha256"), |
| 145 | + ) |
| 146 | + if assurance_status == DECISION_DATA_ASSURANCE_LEGACY: |
| 147 | + raise ValueError("artifact decision-data modes must declare an assurance status") |
| 148 | + |
| 149 | + def to_dict(self) -> dict[str, object]: |
| 150 | + """Return the public-safe contract payload without an artifact location.""" |
| 151 | + |
| 152 | + payload: dict[str, object] = { |
| 153 | + "schema_version": self.schema_version, |
| 154 | + "binding_id": self.binding_id, |
| 155 | + "strategy_scope": self.strategy_scope, |
| 156 | + "mode": self.mode, |
| 157 | + "source_ids": list(self.source_ids), |
| 158 | + "assurance_status": self.assurance_status, |
| 159 | + } |
| 160 | + if self.mode != DECISION_DATA_MODE_LEGACY_RUNTIME_FETCH: |
| 161 | + payload.update( |
| 162 | + { |
| 163 | + "as_of": self.as_of, |
| 164 | + "adjustment_basis": self.adjustment_basis, |
| 165 | + "artifact_sha256": self.artifact_sha256, |
| 166 | + } |
| 167 | + ) |
| 168 | + return payload |
| 169 | + |
| 170 | + @property |
| 171 | + def binding_sha256(self) -> str: |
| 172 | + return sha256(_canonical_bytes(self.to_dict())).hexdigest() |
| 173 | + |
| 174 | + @classmethod |
| 175 | + def from_dict(cls, payload: Mapping[str, Any]) -> "DecisionDataBinding": |
| 176 | + """Parse a public-safe binding payload and reject unknown fields.""" |
| 177 | + |
| 178 | + if not isinstance(payload, Mapping): |
| 179 | + raise ValueError("decision data binding must be an object") |
| 180 | + expected = { |
| 181 | + "schema_version", |
| 182 | + "binding_id", |
| 183 | + "strategy_scope", |
| 184 | + "mode", |
| 185 | + "source_ids", |
| 186 | + "as_of", |
| 187 | + "adjustment_basis", |
| 188 | + "artifact_sha256", |
| 189 | + "assurance_status", |
| 190 | + } |
| 191 | + unsupported = sorted(set(payload) - expected) |
| 192 | + if unsupported: |
| 193 | + raise ValueError("decision data binding contains unsupported fields: " + ", ".join(unsupported)) |
| 194 | + return cls( |
| 195 | + schema_version=payload.get("schema_version", DECISION_DATA_BINDING_SCHEMA_VERSION), |
| 196 | + binding_id=payload.get("binding_id"), |
| 197 | + strategy_scope=payload.get("strategy_scope"), |
| 198 | + mode=payload.get("mode"), |
| 199 | + source_ids=tuple(payload.get("source_ids") or ()), |
| 200 | + as_of=payload.get("as_of"), |
| 201 | + adjustment_basis=payload.get("adjustment_basis"), |
| 202 | + artifact_sha256=payload.get("artifact_sha256"), |
| 203 | + assurance_status=payload.get("assurance_status", DECISION_DATA_ASSURANCE_LEGACY), |
| 204 | + ) |
0 commit comments