"""Exact-byte cross-split collision checks; not a proof of sample independence."""
import hashlib,re
from pathlib import Path

def sha256_file(path):
    """Stream a file digest without loading large images into memory."""
    digest=hashlib.sha256()
    with Path(path).open('rb') as source:
        for block in iter(lambda:source.read(1024*1024),b''):
            digest.update(block)
    return digest.hexdigest()

def exact_split_collisions(development,evaluation):
    """Report cross-split IDs with equal SHA-256; do not silently drop cases.

    Each entry requires a unique string ``id`` within its split and a lowercase
    64-character hexadecimal ``sha256``. Only exact-byte identity is tested;
    near duplicates, overlapping fields, biological linkage and training-set
    overlap remain unresolved even if this report has no collisions.
    """
    def validate(rows):
        seen=set();groups={}
        for row in rows:
            ident=row.get('id');digest=row.get('sha256')
            if not isinstance(ident,str) or not ident or ident in seen or not isinstance(digest,str) or re.fullmatch(r'[0-9a-f]{64}',digest) is None:
                raise ValueError('Invalid or duplicate ID or SHA-256')
            seen.add(ident);groups.setdefault(digest,[]).append(ident)
        return groups
    a=validate(development);b=validate(evaluation);collisions=[{'sha256':h,'development_ids':a[h],'evaluation_ids':b[h]} for h in sorted(a.keys()&b.keys())]
    return {'contaminated':bool(collisions),'collisions':collisions,'scope':'exact_bytes_only'}
