"""Illustrative image-prefix contract, not an image decoder or learned projector.""" from dataclasses import asdict, dataclass, fields import hashlib import json import math import re import sys def identity(value): if type(value) is not str or not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9_.:/@-]{0,127}", value): raise ValueError("invalid identity") def number(value, low, high): if type(value) not in (int, float) or not low <= value <= high or not math.isfinite(value): raise ValueError("finite non-boolean number required") if value != 0 and abs(value) < sys.float_info.min: raise ValueError("subnormal number") if not low <= value <= high: raise ValueError("numeric range") def count(value, low, high): if type(value) is not int or not low <= value <= high: raise ValueError("integer count range") def digest(value): return hashlib.sha256(json.dumps(value, sort_keys=True, separators=(",", ":"), allow_nan=False).encode()).hexdigest() def seal(record): if type(record.content_id) is not str: raise ValueError("content digest requires exact string") data = asdict(record) data.pop("content_id") return digest(data) def validate_record(record, cls): if type(record) is not cls: raise ValueError("concrete frozen record required") rebuilt = cls(**{f.name: getattr(record, f.name, None) for f in fields(cls)}) if record != rebuilt or record.content_id != rebuilt.content_id: raise ValueError("missing content digest or noncanonical record") def sequence(value, low, high): if type(value) not in (list, tuple): raise ValueError("bounded sequence required") count(len(value), low, high) return tuple(value) @dataclass(frozen=True) class BridgeContract: scope: tuple = ("bridge-v1", "model-v1", "tokenizer-v1", "encoder-v1", "projector-v1", "patch-v1", "rgb-minus-one-one-v1", "data-v1", "policy-v1", "source-v1", "eval-v1") patch_size: int = 2 hidden_width: int = 2 max_pixels: int = 64 max_tokens: int = 32 min_alignment_support: int = 4 alignment_margin: float = 0.1 content_id: str = "" def __post_init__(self): object.__setattr__(self, "scope", sequence(self.scope, 11, 11)) for value in self.scope: identity(value) if self.scope[6] != "rgb-minus-one-one-v1": raise ValueError("unsupported normalization") count(self.patch_size, 1, 16) count(self.hidden_width, 1, 32) count(self.max_pixels, 1, 4096) count(self.max_tokens, 2, 1024) count(self.min_alignment_support, 2, 10000) number(self.alignment_margin, 0.001, 1) expected = seal(self) if self.content_id and self.content_id != expected: raise ValueError("contract digest mismatch") object.__setattr__(self, "content_id", expected) @dataclass(frozen=True) class BridgeInput: scope: tuple media_id: str media_revision: str text_id: str text: str width: int height: int pixels: tuple patch_order: tuple projected_rows: tuple token_ids: tuple attention_mask: tuple modalities: tuple = ("image", "text") content_id: str = "" def __post_init__(self): object.__setattr__(self, "scope", sequence(self.scope, 11, 11)) for value in (*self.scope, self.media_id, self.media_revision, self.text_id): identity(value) if type(self.text) is not str or not self.text.strip() or len(self.text.encode()) > 8192: raise ValueError("bounded text required") count(self.width, 1, 64) count(self.height, 1, 64) object.__setattr__(self, "pixels", sequence(self.pixels, 3, 12288)) if len(self.pixels) != self.width * self.height * 3: raise ValueError("corrupt RGB pixel payload") for value in self.pixels: number(value, -1, 1) object.__setattr__(self, "patch_order", sequence(self.patch_order, 1, 1024)) for value in self.patch_order: count(value, 0, 1023) rows = sequence(self.projected_rows, 1, 1024) rows = tuple(sequence(row, 1, 32) for row in rows) for row in rows: for value in row: number(value, -100, 100) object.__setattr__(self, "projected_rows", rows) object.__setattr__(self, "token_ids", sequence(self.token_ids, 1, 1024)) for value in self.token_ids: count(value, 0, 1000000) object.__setattr__(self, "attention_mask", sequence(self.attention_mask, 2, 2048)) if any(type(value) is not int or value != 1 for value in self.attention_mask): raise ValueError("unpadded image-prefix visibility mask must contain integer ones") object.__setattr__(self, "modalities", sequence(self.modalities, 2, 2)) if any(type(value) is not str for value in self.modalities) or self.modalities != ("image", "text"): raise ValueError("missing, duplicate, or unordered modality") expected = seal(self) if self.content_id and self.content_id != expected: raise ValueError("input digest mismatch") object.__setattr__(self, "content_id", expected) @dataclass(frozen=True) class AlignmentEvidence: scope: tuple evaluation_id: str input_content_id: str cohort_id: str support: int matched_score: float shuffled_score: float content_id: str = "" def __post_init__(self): object.__setattr__(self, "scope", sequence(self.scope, 11, 11)) for value in (*self.scope, self.evaluation_id, self.cohort_id): identity(value) if type(self.input_content_id) is not str or not re.fullmatch("[a-f0-9]{64}", self.input_content_id): raise ValueError("exact input digest required") if self.evaluation_id != self.scope[10]: raise ValueError("evaluation identity mismatch") count(self.support, 0, 10000) number(self.matched_score, 0, 1) number(self.shuffled_score, 0, 1) expected = seal(self) if self.content_id and self.content_id != expected: raise ValueError("alignment digest mismatch") object.__setattr__(self, "content_id", expected) @dataclass(frozen=True) class BridgeReport: status: str evidence_id: str patches: int fused_tokens: int claim: str = "DECLARED_ALIGNMENT_NOT_SEMANTIC_UNDERSTANDING" def audit_bridge(contract: BridgeContract, media: BridgeInput, alignment: AlignmentEvidence) -> BridgeReport: """Bind structural validity to exact cross-modal evaluation evidence.""" validate_record(contract, BridgeContract) validate_record(media, BridgeInput) validate_record(alignment, AlignmentEvidence) if media.scope != contract.scope or alignment.scope != contract.scope: raise ValueError("encoder/projector/tokenizer/model/data scope mismatch") if alignment.input_content_id != media.content_id: raise ValueError("alignment is not bound to exact image/text content") if media.width * media.height > contract.max_pixels: raise ValueError("resolution budget") if media.width % contract.patch_size or media.height % contract.patch_size: raise ValueError("patch grid divisibility") patches = (media.width // contract.patch_size) * (media.height // contract.patch_size) if media.patch_order != tuple(range(patches)) or len(media.projected_rows) != patches: raise ValueError("raster patch order or projected shape") if any(len(row) != contract.hidden_width for row in media.projected_rows): raise ValueError("projector hidden width") fused = patches + len(media.token_ids) if len(media.attention_mask) != fused or fused > contract.max_tokens: raise ValueError("fused mask or token budget") gap = math.fsum((alignment.matched_score, -alignment.shuffled_score)) status = "BOUND" if alignment.support >= contract.min_alignment_support and gap >= contract.alignment_margin else "BLOCK_ALIGNMENT" return BridgeReport(status, digest((contract.content_id, media.content_id, alignment.content_id)), patches, fused) def illustrative_fixture(): contract = BridgeContract() media = BridgeInput(contract.scope, "image-1", "image-rev-1", "text-1", "Illustrative image question", 4, 4, (0.0,) * 48, (0, 1, 2, 3), ((0.2, 0.3),) * 4, (11, 12), (1,) * 6) alignment = AlignmentEvidence(contract.scope, "eval-v1", media.content_id, "heldout-image-text-v1", 8, 0.8, 0.4) return contract, media, alignment def main(): report = audit_bridge(*illustrative_fixture()) print("example=illustrative_only") print(f"status={report.status}") print(f"patches={report.patches};fused_tokens={report.fused_tokens}") print(f"claim={report.claim}") if __name__ == "__main__": main()