"""Bound reranking evidence to a deterministic context plan; no model is run here.""" from dataclasses import asdict, dataclass, fields import hashlib import json import math import re import sys IDENTITY = re.compile(r"[A-Za-z0-9][A-Za-z0-9_.:/@-]{0,127}") SHA256 = re.compile(r"[a-f0-9]{64}") def identity(value): if type(value) is not str or not IDENTITY.fullmatch(value): raise ValueError("exact bounded identity required") return value def bounded_text(value): if type(value) is not str or not value.strip() or len(value) > 4096: raise ValueError("exact nonempty bounded text required") return value def sha256_text(value): if type(value) is not str or not SHA256.fullmatch(value): raise ValueError("exact lowercase SHA-256 required") return value def count(value, lower, upper): if type(value) is not int or not lower <= value <= upper: raise ValueError("bounded non-boolean integer required") return value def number(value, lower, upper): if type(value) not in (int, float) 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 rejected") if not lower <= value <= upper: raise ValueError("number outside contract range") return value def sequence(value, lower, upper): if type(value) not in (tuple, list): raise ValueError("bounded tuple or list required") count(len(value), lower, upper) return tuple(value) def digest(value): encoded = json.dumps( value, sort_keys=True, separators=(",", ":"), allow_nan=False ).encode() return hashlib.sha256(encoded).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") try: rebuilt = cls(**{field.name: getattr(record, field.name) for field in fields(cls)}) except (AttributeError, TypeError) as error: raise ValueError("malformed record") from error if record != rebuilt or record.content_id != rebuilt.content_id: raise ValueError("noncanonical or modified record") return rebuilt def scope_tuple(value): result = sequence(value, 9, 9) for item in result: identity(item) return result @dataclass(frozen=True) class AssemblyContract: scope: tuple = ( "assembly-v1", "corpus-v1", "query-policy-v1", "generator-v1", "reranker-v1", "tokenizer-v1", "language-model-v1", "evaluation-v1", "source-v1", ) max_candidates: int = 8 max_context_tokens: int = 12 max_chunks: int = 3 max_chunks_per_source: int = 1 score_order: str = "descending" packing_policy: str = "greedy-rerank-with-source-cap" content_id: str = "" def __post_init__(self): object.__setattr__(self, "scope", scope_tuple(self.scope)) count(self.max_candidates, 1, 1000) count(self.max_context_tokens, 1, 1_000_000) count(self.max_chunks, 1, self.max_candidates) count(self.max_chunks_per_source, 1, self.max_chunks) if type(self.score_order) is not str or self.score_order != "descending": raise ValueError("only exact descending score order is declared") if ( type(self.packing_policy) is not str or self.packing_policy != "greedy-rerank-with-source-cap" ): raise ValueError("unknown context packing policy") expected = seal(self) if self.content_id and self.content_id != expected: raise ValueError("assembly contract digest mismatch") object.__setattr__(self, "content_id", expected) @dataclass(frozen=True) class Candidate: scope: tuple query_id: str query_sha256: str chunk_id: str document_id: str revision: str source_id: str text: str content_sha256: str generator_rank: int token_count: int content_id: str = "" def __post_init__(self): object.__setattr__(self, "scope", scope_tuple(self.scope)) for value in ( self.query_id, self.chunk_id, self.document_id, self.revision, self.source_id, ): identity(value) sha256_text(self.query_sha256) bounded_text(self.text) sha256_text(self.content_sha256) if self.content_sha256 != digest(self.text): raise ValueError("chunk text digest mismatch") count(self.generator_rank, 1, 1000) count(self.token_count, 1, 1_000_000) expected = seal(self) if self.content_id and self.content_id != expected: raise ValueError("candidate digest mismatch") object.__setattr__(self, "content_id", expected) @dataclass(frozen=True) class CandidateSet: scope: tuple query_id: str query_sha256: str candidates: tuple content_id: str = "" def __post_init__(self): object.__setattr__(self, "scope", scope_tuple(self.scope)) identity(self.query_id) sha256_text(self.query_sha256) candidates = sequence(self.candidates, 1, 1000) copies = tuple(validate_record(candidate, Candidate) for candidate in candidates) if any( candidate.scope != self.scope or candidate.query_id != self.query_id or candidate.query_sha256 != self.query_sha256 for candidate in copies ): raise ValueError("candidate outside query and model scope") if tuple(candidate.generator_rank for candidate in copies) != tuple( range(1, len(copies) + 1) ): raise ValueError("generator ranks must be unique and contiguous") if len({candidate.chunk_id for candidate in copies}) != len(copies): raise ValueError("duplicate candidate chunk") if len({candidate.content_sha256 for candidate in copies}) != len(copies): raise ValueError("duplicate candidate content") object.__setattr__(self, "candidates", copies) expected = seal(self) if self.content_id and self.content_id != expected: raise ValueError("candidate set digest mismatch") object.__setattr__(self, "content_id", expected) @dataclass(frozen=True) class RerankEvidence: scope: tuple query_id: str query_sha256: str candidate_set_id: str candidate_ids: tuple scores: tuple content_id: str = "" def __post_init__(self): object.__setattr__(self, "scope", scope_tuple(self.scope)) identity(self.query_id) sha256_text(self.query_sha256) sha256_text(self.candidate_set_id) candidate_ids = sequence(self.candidate_ids, 1, 1000) scores = sequence(self.scores, len(candidate_ids), len(candidate_ids)) for candidate_id in candidate_ids: sha256_text(candidate_id) if len(set(candidate_ids)) != len(candidate_ids): raise ValueError("duplicate candidate evidence") for score in scores: number(score, -100, 100) object.__setattr__(self, "candidate_ids", candidate_ids) object.__setattr__(self, "scores", scores) expected = seal(self) if self.content_id and self.content_id != expected: raise ValueError("rerank evidence digest mismatch") object.__setattr__(self, "content_id", expected) @dataclass(frozen=True) class AssemblyReport: status: str selected_chunk_ids: tuple context_tokens: int source_count: int omitted: tuple evidence_id: str claim: str = "ILLUSTRATIVE_PLAN_NOT_RERANKING_BENCHMARK" def assemble_context( contract: AssemblyContract, candidates: CandidateSet, evidence: RerankEvidence, ) -> AssemblyReport: """Audit supplied rerank scores, then pack under an explicit local policy.""" contract = validate_record(contract, AssemblyContract) candidates = validate_record(candidates, CandidateSet) evidence = validate_record(evidence, RerankEvidence) if any(item.scope != contract.scope for item in (candidates, evidence)): raise ValueError("generator/reranker/tokenizer/model scope mismatch") if ( evidence.query_id != candidates.query_id or evidence.query_sha256 != candidates.query_sha256 or evidence.candidate_set_id != candidates.content_id ): raise ValueError("rerank evidence is stale or belongs to another query") candidate_ids = tuple(candidate.content_id for candidate in candidates.candidates) if evidence.candidate_ids != candidate_ids: raise ValueError("rerank scores must bind the exact candidate order") if len(candidates.candidates) > contract.max_candidates: raise ValueError("candidate generator exceeded declared rerank depth") ranked = sorted( zip(candidates.candidates, evidence.scores), key=lambda item: (-item[1], item[0].generator_rank, item[0].chunk_id), ) selected = [] omitted = [] tokens = 0 sources = {} for candidate, _score in ranked: if len(selected) >= contract.max_chunks: omitted.append((candidate.chunk_id, "max-chunks")) continue if sources.get(candidate.source_id, 0) >= contract.max_chunks_per_source: omitted.append((candidate.chunk_id, "source-cap")) continue if tokens + candidate.token_count > contract.max_context_tokens: omitted.append((candidate.chunk_id, "token-budget")) continue selected.append(candidate) tokens += candidate.token_count sources[candidate.source_id] = sources.get(candidate.source_id, 0) + 1 status = "ASSEMBLED_FOR_EVALUATION" if selected else "BLOCKED_EMPTY_PLAN" evidence_id = digest( { "contract": contract.content_id, "candidates": candidates.content_id, "rerank": evidence.content_id, "selected": [candidate.content_id for candidate in selected], "omitted": omitted, } ) return AssemblyReport( status, tuple(candidate.chunk_id for candidate in selected), tokens, len(sources), tuple(omitted), evidence_id, ) def illustrative_fixture(): contract = AssemblyContract() query_id = "query-1" query_sha = digest("why separate reranking from context packing") specifications = ( ("chunk-a", "doc-a", "source-alpha", "Candidate generators bound recall.", 6), ("chunk-b", "doc-b", "source-beta", "Cross-encoders jointly read query and passage.", 5), ("chunk-c", "doc-c", "source-alpha", "Packing is a separate policy boundary.", 4), ("chunk-d", "doc-d", "source-gamma", "Position effects need evaluation.", 3), ) candidate_records = tuple( Candidate( contract.scope, query_id, query_sha, chunk_id, document_id, "rev-1", source_id, text, digest(text), rank, token_count, ) for rank, (chunk_id, document_id, source_id, text, token_count) in enumerate( specifications, start=1 ) ) candidate_set = CandidateSet( contract.scope, query_id, query_sha, candidate_records ) evidence = RerankEvidence( contract.scope, query_id, query_sha, candidate_set.content_id, tuple(candidate.content_id for candidate in candidate_set.candidates), (0.70, 0.95, 0.90, 0.80), ) return contract, candidate_set, evidence def main(): report = assemble_context(*illustrative_fixture()) print("example=illustrative_only") print(f"status={report.status}") print("selected=" + ",".join(report.selected_chunk_ids)) print(f"tokens={report.context_tokens};sources={report.source_count}") print(f"claim={report.claim}") if __name__ == "__main__": main()