"""Load only the explicitly allowed, hash-verified local preprocessing code.""" import hashlib import importlib from pathlib import Path ROOT=Path(__file__).resolve().parent ALLOWED={'command_normalizer':set(),'command_canonicalizer':{'command_normalizer.py'},'command_normalizer_v2':set(),'command_canonicalizer_v2':{'command_normalizer_v2.py'}} def load_preprocessor(metadata): name=metadata.get('module') if name not in ALLOWED:raise ValueError('Unrecognized input preprocessing module') file=ROOT/(name+'.py') if hashlib.sha256(file.read_bytes()).hexdigest()!=metadata.get('sha256'):raise ValueError('Input preprocessing source mismatch') dependencies=metadata.get('dependencies',{}) if set(dependencies)!=ALLOWED[name]:raise ValueError('Input preprocessing dependencies mismatch') for filename,expected in dependencies.items(): if hashlib.sha256((ROOT/filename).read_bytes()).hexdigest()!=expected:raise ValueError('Input preprocessing dependency source mismatch') module=importlib.import_module(name) if Path(module.__file__).resolve()!=file or module.VERSION!=metadata.get('version'):raise ValueError('Input preprocessing module identity mismatch') return module.normalize_state