"""Existing v1 canonical Unicode and line-ending normalization only.""" import unicodedata from collections.abc import Mapping from .types import ExtractionEnvelope NORMALIZATION_POLICY = "unicode-line-endings-v1" def normalize_search_text(text: str, policy: str = NORMALIZATION_POLICY) -> str: if policy != NORMALIZATION_POLICY: raise ValueError("Unsupported normalization policy; migration/reindex design required") return unicodedata.normalize("NFC", text.replace("\r\n", "\n").replace("\r", "\n")) def normalized_inputs(envelope: ExtractionEnvelope, processing_profile: Mapping) -> tuple[str, ...]: """Check/rederive all inputs and aggregate character budget before consumption. Faithful evidence is never mutated; missing search_text is derived on demand. No partial collection is returned if any block or the total fails validation. """ policy = processing_profile["normalizer"] if envelope.normalization_policy != policy: raise ValueError("Artifact normalization identity mismatch") limit = processing_profile["limits"]["max_normalized_characters"] total, inputs = 0, [] for block in envelope.blocks: derived = normalize_search_text(block.text, policy) if block.search_text is not None and block.search_text != derived: raise ValueError("Stored search text is not a valid faithful-text derivation") total += len(derived) if total > limit: raise ValueError("Normalized input exceeds character budget") inputs.append(derived) return tuple(inputs)