Datasets:
Download code/robohome/src/engine/real_test/projection_adapter.py from RoboParty/RoboHome: direct link, hf CLI and curl.
- Browser
- Download file 3.53 kB
-
https://huggingface.co/datasets/RoboParty/RoboHome/resolve/main/code/robohome/src/engine/real_test/projection_adapter.py
- Command line
-
hf download hf://datasets/RoboParty/RoboHome/code/robohome/src/engine/real_test/projection_adapter.py
-
curl -L -o projection_adapter.py https://huggingface.co/datasets/RoboParty/RoboHome/resolve/main/code/robohome/src/engine/real_test/projection_adapter.py
3.53 kB
| """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 | |