"""Compile whole-house policy into supported native inputs before freezing. material_policy.style maps to request.style candidate filtering. Structural wall/door pins belong to the runtime configuration, so callers must also use compile_search_runtime before freezing a runtime template. Floor/revision and furniture identity pins remain audit-only (see material plan search_policy). functional_policy accepts functional_layout_policy, require_verified_usage_space and explicit reservations. Reservation XY is world-local unless a support is named, in which case it follows that support's native local XY. Z stays world Z. No candidate domains, assets, original furniture shaders or evidence are edited. Native search.functional_children_first defaults to True; explicit False keeps baseline ordering. This changes search order only, retaining native checks. """ import copy import math if __package__: from .material_plan import compile_material_search_policy else: from material_plan import compile_material_search_policy def _object(value, name): if not isinstance(value, dict): raise ValueError(name + ' must be an object') return value def _settings(config): return _object(config.get('wholehouse', {}), 'wholehouse settings') def _explicit(target, key, value): if key in target and target[key] is not None and target[key] != value: raise ValueError('Conflicting explicit policy: ' + key) target[key] = copy.deepcopy(value) def compile_search_runtime(template, config): """Copy a native runtime template and apply only native wall/door selectors. This does no file I/O. The caller owns binding, saving and hashing the resulting runtime; merely returning it is not native enforcement evidence. """ result = copy.deepcopy(_object(template, 'runtime template')) # These paths configure selection/preparation, which has already finished. # Keep the original template for provenance; the native loader rejects # unknown settings, so remove only the declared orchestration settings. for key in ('retrieval_index', 'appearance_index', 'material_artifact_index'): result.pop(key, None) policy = compile_material_search_policy(_settings(config).get('material_policy')) for key, value in policy['runtime_overrides'].items(): _explicit(result, key, value) return result def _reservations(value): if not isinstance(value, list): raise ValueError('Search reservations must be a list') if any(not isinstance(row, dict) for row in value): raise ValueError('Reservation must be an object') return copy.deepcopy(value) def _vector(value, length, name): if (not isinstance(value, list) or len(value) != length or any(type(v) not in (int, float) or not math.isfinite(v) for v in value)): raise ValueError('Search reservation requires explicit finite ' + name) return value def compile_search_request(request, config, manifest): result = copy.deepcopy(request) search = _object(result.setdefault('search', {}), 'search') if type(search.setdefault('functional_children_first', True)) is not bool: raise ValueError('search.functional_children_first must be boolean') wholehouse = _settings(config) material = compile_material_search_policy(wholehouse.get('material_policy'), result.get('style'), require_style_name=True) if material['request_style'] is not None: result['style'] = material['request_style'] functional = _object(wholehouse.get('functional_policy', {}), 'functional_policy') if set(functional) - {'functional_layout_policy', 'require_verified_usage_space', 'reservations'}: raise ValueError('Unsupported functional policy') if 'functional_layout_policy' in functional: if functional['functional_layout_policy'] not in ('source_attributes_v1', 'legacy'): raise ValueError('Unknown functional_layout_policy') _explicit(result, 'functional_layout_policy', functional['functional_layout_policy']) if 'require_verified_usage_space' in functional: if type(functional['require_verified_usage_space']) is not bool: raise ValueError('require_verified_usage_space must be boolean') _explicit(result, 'require_verified_usage_space', functional['require_verified_usage_space']) constraints = result.get('search_constraints') room_policy = _object(wholehouse.get('room_policy', {}), 'room_policy') reservations = _reservations(room_policy.get('search_reservations', [])) reservations.extend(_reservations(functional.get('reservations', []))) for row in _reservations((config.get('priors') or {}).get('reservations', [])): row['occupancy_scope'] = 'supported_children' reservations.append(row) if constraints is None and not reservations: return result if constraints is None: constraints = dict(schema='rhg.search-constraints.v1', fixed_entities=[], asset_locks=[], nogoods=[], reservations=[]) result['search_constraints'] = constraints if not isinstance(constraints, dict) or constraints.get('schema') != 'rhg.search-constraints.v1': raise ValueError('Unsupported search constraints') reservations = _reservations(constraints.get('reservations', [])) + reservations target = [] constraints['reservations'] = target entities = {row['entity_id']: row for row in manifest['entities']} rooms = {row['room_id'] for row in manifest['floor_plan']['rooms']} requested_ids = {row['entity_id'] for row in request.get('furniture', manifest['entities'])} for row in reservations: bounds = _vector(row.get('bounds_xy_m'), 4, 'bounds_xy_m') if bounds[0] >= bounds[2] or bounds[1] >= bounds[3]: raise ValueError('Search reservation bounds_xy_m requires positive area') if not isinstance(row.get('purpose'), str) or not row['purpose'].strip(): raise ValueError('Search reservation requires purpose') if 'support_entity_id' in row: parent = row['support_entity_id'] if not isinstance(parent, str) or parent not in entities or parent not in requested_ids: raise ValueError('Unknown reservation support') row.setdefault('room_id', entities[parent]['room_id']) if row['room_id'] != entities[parent]['room_id']: raise ValueError('Reservation support belongs to another room') if not isinstance(row.get('room_id'), str) or row['room_id'] not in rooms: raise ValueError('Unknown reservation room') scope = row.get('occupancy_scope', 'volume') if scope not in ('volume', 'supported_children') or scope == 'supported_children' and 'support_entity_id' not in row: raise ValueError('Invalid reservation occupancy_scope') exempt = row.get('exempt_entity_ids', []) if (not isinstance(exempt, list) or any(not isinstance(e, str) or e not in requested_ids for e in exempt) or len(set(exempt)) != len(exempt)): raise ValueError('Invalid or duplicate exempt_entity_ids') if 'z_range_m' in row: low, high = _vector(row['z_range_m'], 2, 'z_range_m') if low >= high: raise ValueError('z_range_m requires positive height') if row not in target: target.append(row) return result