Datasets:
File size: 3,529 Bytes
870b85a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | """Preserve source semantic hints when projecting a generation request."""
from __future__ import annotations
import copy
# These fields can change the native binding semantic key. The historical
# projector omitted procedural_kind, so source entities such as small_bowl
# were silently reclassified by role during candidate eligibility.
SEMANTIC_HINT_FIELDS = (
'procedural_kind',
'layout_slot_id',
'asset_pool_key',
'binding_semantic_key',
)
def project_with_semantic_preservation(manifest, request, *, semantic_key):
"""Restore source semantic hints and verify every projected entity.
The source manifest is authoritative for fields that influence native
binding. Existing conflicting values are rejected instead of overwritten,
because a conflict indicates a broken upstream projection contract.
"""
source_entities = manifest.get('entities', [])
projected_entities = request.get('entities', [])
source_by_id = _unique_entity_map(source_entities, 'source manifest')
projected_by_id = _unique_entity_map(projected_entities, 'projected request')
if set(source_by_id) != set(projected_by_id):
missing = sorted(set(source_by_id) - set(projected_by_id))
extra = sorted(set(projected_by_id) - set(source_by_id))
raise ValueError(f'Projected entity inventory differs: missing={missing}, extra={extra}')
changed = []
for entity_id, source in source_by_id.items():
projected = projected_by_id[entity_id]
changed_fields = []
for field in SEMANTIC_HINT_FIELDS:
if field not in source:
continue
if field in projected and projected[field] != source[field]:
raise ValueError(
f'Projected semantic hint conflict for {entity_id}: '
f'{field}={projected[field]!r}, source={source[field]!r}')
if field not in projected:
projected[field] = copy.deepcopy(source[field])
changed_fields.append(field)
if changed_fields:
changed.append({'entity_id': entity_id, 'fields': changed_fields})
source_keys = {entity_id: semantic_key(source_by_id[entity_id])
for entity_id in sorted(source_by_id)}
projected_keys = {entity_id: semantic_key(projected_by_id[entity_id])
for entity_id in sorted(projected_by_id)}
mismatches = [
{'entity_id': entity_id, 'source': source_keys[entity_id],
'projected': projected_keys[entity_id]}
for entity_id in sorted(source_keys)
if source_keys[entity_id] != projected_keys[entity_id]
]
if mismatches:
raise ValueError(f'Projected native semantic keys changed: {mismatches}')
return request, {
'schema': 'rhg.semantic-preserving-request-projection.v1',
'passed': True,
'checked_count': len(source_by_id),
'changed_count': len(changed),
'changed_entities': changed,
'source_semantic_keys': source_keys,
'projected_semantic_keys': projected_keys,
'semantic_key_mismatches': mismatches,
'preserved_fields': list(SEMANTIC_HINT_FIELDS),
}
def _unique_entity_map(entities, label):
result = {}
for entity in entities:
entity_id = entity.get('entity_id')
if not entity_id or entity_id in result:
raise ValueError(f'{label} contains duplicate or empty entity_id: {entity_id!r}')
result[entity_id] = entity
return result
|