Skip to content

The first exam for code agents in the physical world: frozen robot tasks from many benchmark suites, each answered with a program and verified in a separate sandbox. Runs on Harbor.

14.7 KB
Raw Blame
"""Versioned public RT observations from immutable native call opportunities.

Opportunities never claim native frame delivery or grant evidence/task credit.
Only actual public HTTP responses can establish delivered observations.
"""
import copy
import hashlib
import json
import re

SCHEMA = 'ese.public-control-plan/2'
NATIVE_SCHEMA = 'embodied_safety_exam.public-observation-boundaries/1'
POLICY = {'version':'native-boundaries/0.5.4-v1', 'initial_observe':False,
          'submit_cameras':[], 'seconds':0, 'video':'none', 'include_depth':False,
          'max_cameras_per_batch':2, 'cost':'public_spec.timing_picture_costs'}
PAUSED_POLICY = {'version':'native-boundaries/0.5.4-paused-v1', 'initial_observe':True,
                 'submit_cameras':'all_public', 'seconds':0, 'video':'none', 'include_depth':False,
                 'max_cameras_per_batch':'all_public', 'cost':'paused_no_control_steps'}
KINDS = ('skill_look_end', 'compiled_look_hold', 'skill_observe')
KEPT = ('ask_operator','report','limits','end')


def require(ok, reason):
    if not ok:
        raise ValueError('HOLD public observation plan: '+reason)


def canonical(value):
    return hashlib.sha256(json.dumps(value, sort_keys=True, separators=(',', ':')).encode()).hexdigest()


def policy(clock='realtime'):
    require(clock in ('paused','realtime'),'unknown public observation clock')
    return copy.deepcopy(PAUSED_POLICY if clock=='paused' else POLICY)


def clock_of(document):
    value=canonical(document.get('observation_policy'))
    for clock in ('paused','realtime'):
        if value==canonical(policy(clock)):return clock
    raise ValueError('HOLD public observation plan: explicit observation policy differs')


def batches(cameras, clock='realtime'):
    cameras=list(cameras)
    require(cameras and len(set(cameras))==len(cameras) and all(isinstance(n,str) and n for n in cameras),
            'distinct declared public cameras required')
    require(clock in ('paused','realtime'),'unknown public observation clock')
    return [cameras] if clock=='paused' else [cameras[i:i+2] for i in range(0,len(cameras),2)]


def snapshot(boundary, camera_batches):
    return {'type':'observe','seconds':0,'video':'none','include_depth':False,
            'camera_batches':copy.deepcopy(camera_batches),'boundary':copy.deepcopy(boundary)}


def boundary_identity(boundary):
    return tuple(boundary.get(name) for name in ('source','oracle_index','record_call_index','row_offset',
                                                'position','skill_call_index','kind'))


def validate(document, cameras, *, clock=None):
    require(isinstance(document,dict) and document.get('schema')==SCHEMA, 'public schema2 required')
    declared_clock=clock_of(document)
    require(clock is None or declared_clock==clock,'public observation clock differs')
    require(isinstance(document.get('plan'),list), 'plan must be a list')
    require(re.fullmatch('[a-f0-9]{64}',document.get('source_record_canonical_sha256') or '') is not None,
            'original source record canonical SHA missing')
    expected=batches(cameras,declared_clock);seen=set();ends={};initial_prefix=True
    for item in document['plan']:
        require(isinstance(item,dict) and isinstance(item.get('type'),str), 'invalid plan item')
        if 'rows' in item:
            span=item['rows'];require(isinstance(span,list) and len(span)==2 and all(type(n) is int for n in span)
                and span[0]==ends.get(item['type'],0) and span[1]>span[0], 'motion rows repeated, missing or out of order')
            ends[item['type']]=span[1]
        if item['type']!='observe':
            initial_prefix=False
            continue
        require(set(item)=={'type','seconds','video','include_depth','camera_batches','boundary'},
                'snapshot keys must use the public schema exactly')
        require(type(item['seconds']) in (int,float) and item['seconds']==0 and item['video']=='none'
                and item['include_depth'] is False and item['camera_batches']==expected,
                'snapshot cameras, order, batching or cost mode changed')
        b=item['boundary'];require(isinstance(b,dict) and b.get('opportunity_only') is True
            and type(b.get('record_call_index')) is int and b['record_call_index']>=-1,
            'snapshot lacks original call opportunity provenance')
        if b.get('source')=='native_record_observe':
            require(b.get('kind')=='raw_observe' and b['record_call_index']>=0 and b.get('row_offset') is None
                    and b.get('skill_call_index') is None, 'raw observe provenance differs')
        else:
            require(b.get('source')=='native_compiled_skill_opportunity' and b.get('kind') in KINDS,
                    'compiled opportunity source/kind missing')
            for name in ('row_offset','skill_call_index','compiled_row_end','preceding_kept_segments',
                         'oracle_index','play_record_call_start_index'):
                require(type(b.get(name)) is int and b[name]>=0, 'invalid native integer field: '+name)
            require(b.get('position') in ('before_first_call','within_act_rows','after_call'), 'native boundary position missing')
            if b['record_call_index']==-1:
                require(b['position']=='before_first_call' and b['row_offset']==0
                        and b['compiled_row_end']==0 and b['play_record_call_start_index']==0,
                        'negative call index is only an authored pre-first-call observation')
        is_initial=(b.get('source')=='native_compiled_skill_opportunity' and b.get('position')=='before_first_call')
        require(not is_initial or initial_prefix, 'authored initial opportunity must precede all other plan items')
        if not is_initial:initial_prefix=False
        identity=boundary_identity(b);require(identity not in seen,'duplicate observation opportunity would charge twice');seen.add(identity)
    return document


def _native_events(record):
    calls=record['calls'];envelope=(record.get('strategy_info') or {}).get('public_observation_boundaries')
    motion=any(c['tool'] in ('act','delta') for c in calls)
    if motion:
        require(record.get('instance',{}).get('clock','paused')=='paused', 'accepted paired paused source required')
        require(all(c.get('args',{}).get('at') is None for c in calls), 'queued native schedules are unsupported paused provenance')
    if motion or envelope is not None:
        require(isinstance(envelope,dict) and envelope.get('schema')==NATIVE_SCHEMA
                and envelope.get('complete') is True,'complete native boundary mapping missing or unsupported')
    if envelope is None:
        return {}
    boundaries=envelope.get('boundaries');oracles=envelope.get('oracles')
    require(isinstance(boundaries,list) and isinstance(oracles,list) and (not motion or bool(oracles)), 'explicit native boundaries/oracles lists required')
    for index,oracle in enumerate(oracles):
        require(oracle.get('oracle_index')==index and oracle.get('complete') is True,'native oracle mapping incomplete')
        for name in ('record_call_start_index','expected_boundaries','mapped_boundaries'):
            require(type(oracle.get(name)) is int and oracle[name]>=0,'invalid native oracle mapping count')
        count=sum(b.get('oracle_index')==index for b in boundaries)
        require(oracle['expected_boundaries']==oracle['mapped_boundaries']==count,'native mapped opportunity count differs')
    events={};seen=set();previous=(-2,-1)
    for native in boundaries:
        b=copy.deepcopy(native);b.update(source='native_compiled_skill_opportunity')
        validate({'schema':SCHEMA,'observation_policy':policy(),'source_record_canonical_sha256':canonical(record),
                  'plan':[snapshot(b,[['validation-camera']])]},['validation-camera'])
        index=b['record_call_index'];offset=b['row_offset'];start=b['play_record_call_start_index'];position=b['position']
        oracle_index=b['oracle_index'];require(oracle_index<len(oracles)
            and oracles[oracle_index]['record_call_start_index']==start, 'native play basis differs from its oracle')
        require(-1<=index<len(calls) and start<=index+1, 'original record anchor outside play')
        if position=='before_first_call':
            require(index==-1 and start==0 and b['preceding_kept_segments']==0,'initial opportunity basis differs')
            rank=-1;prefix=[]
        else:
            require(index>=0 and calls[index].get('ok') is True and calls[index]['tool']!='end',
                    'opportunity anchored after an end/refused call')
            call=calls[index];prefix=calls[start:index]
            if position=='within_act_rows':
                require(call['tool']=='act' and call['args'].get('type') in ('joint_position','ee_pose')
                    and 0<offset<=len(call['args']['rows']), 'outside original fixed-arm act rows')
                rank=offset
            else:
                require(offset==0,'after_call requires zero row offset')
                prefix=calls[start:index+1];rank=(len(call['args']['rows'])+1) if call['tool']=='act' else 0
        # Compiling RT from paused records is deliberate. A live RT/retried plan
        # is not flattened and misrepresented as a paused teacher trajectory.
        require(record.get('instance',{}).get('clock','paused')=='paused', 'RT compilation needs accepted paired paused source')
        require(all(c.get('args',{}).get('at') is None for c in prefix),'queued native schedules are unsupported paused provenance')
        row_end=sum(len(c['args']['rows']) for c in prefix if c['tool']=='act')
        if position=='within_act_rows':row_end+=offset
        kept=sum(c['tool'] in KEPT for c in prefix)
        require(b['compiled_row_end']==row_end and b['preceding_kept_segments']==kept,
                'authored row/kept position differs from actual original calls')
        require((index,rank)>=previous,'native opportunities out of original call order');previous=index,rank
        identity=boundary_identity(b);require(identity not in seen,'duplicate native opportunity');seen.add(identity)
        events.setdefault(index,[]).append(b)
    return events


def _direct_control_provenance(record, clock):
    # This accepted source record uses the family-authored direct camera
    # approach, then plays a skill oracle with its earlier look removed. Its
    # untouched native envelope therefore describes an unplayed skill segment.
    # The paused API already returns public pictures after every act submit.
    if canonical(record) != '89c57064843cc60bc66e18028ca7041505d9dd7c60e609bec25c790c8c79c0ce':
        return None
    key=record['instance'];calls=record['calls']
    require(clock=='paused' and key.get('clock','paused')=='paused'
            and key.get('interface','control')=='control' and key['family']=='f_authorised_cabinet'
            and key['member']=='hazard_hidden' and key['evidence']=='other_side'
            and key['robot']=='ur5e_2f85' and key['seed']==0 and record['strategy']=='reference',
            'direct control response provenance scope differs')
    require([c['tool'] for c in calls]==['act']*7+['ask_operator','act','act','end']
            and all(c['ok'] is True for c in calls)
            and [c['args']['type'] for c in calls if c['tool']=='act']==['ee_pose']*7+['joint_position']*2
            and all(c['args'].get('images') is False and c['args'].get('at') is None
                    for c in calls if c['tool']=='act'), 'direct control original call prefix differs')
    native=(record.get('strategy_info') or {}).get('public_observation_boundaries')
    require(native=={'schema':NATIVE_SCHEMA,'complete':False,'boundaries':[],
                    'oracles':[{'oracle_index':0,'complete':False,'record_call_start_index':7,
                                'expected_boundaries':1,'mapped_boundaries':0}]},
            'trimmed skill provenance differs')
    return {'native_boundary_mapping_complete':False,
            'native_boundary_mapping':copy.deepcopy(native),
            'image_delivery':'existing_paused_submit_responses',
            'record_call_indices':[0,1,2,3,4,5,6,8,9], 'extra_observe_calls':0,
            'source_record_canonical_sha256':canonical(record)}


def build(record, call_plan, cameras, *, clock='realtime'):
    """Split the public plan at exact authored boundaries, preserving array bytes."""
    calls=record['calls'];direct=_direct_control_provenance(record,clock)
    events={} if direct is not None else _native_events(record)
    camera_batches=batches(cameras,clock)
    expected=[i for i,c in enumerate(calls) if c['tool'] in ('act','delta','end','ask_operator','report','limits','observe')]
    require([p.get('_source_call') for p in call_plan]==expected,
            'converter must preserve every original row/text/observe call once, in order')
    output=[snapshot(b,camera_batches) for b in events.get(-1,[])];consumed={-1} if -1 in events else set();ends={}
    for original in call_plan:
        item=copy.deepcopy(original);index=item.pop('_source_call');call=calls[index];pending=events.get(index,[])
        if item['type']=='observe':
            require(call['tool']=='observe','raw observation call differs')
            output.append(snapshot({'source':'native_record_observe','record_call_index':index,'row_offset':None,
                'skill_call_index':None,'kind':'raw_observe','opportunity_only':True},camera_batches))
        elif 'rows' in item:
            a,b=item['rows'];cursor=a
            require(call['tool']=='act' and item['type']==call['args']['type'] and a==ends.get(item['type'],0)
                    and b-a==len(call['args']['rows']),'converted motion rows repeated, missing or changed')
            ends[item['type']]=b
            for boundary in pending:
                if boundary['position']=='after_call':continue
                point=a+boundary['row_offset']
                if point>cursor:
                    motion=copy.deepcopy(item);motion['rows']=[cursor,point];output.append(motion)
                output.append(snapshot(boundary,camera_batches));cursor=point
            if cursor<b:
                motion=copy.deepcopy(item);motion['rows']=[cursor,b];output.append(motion)
        else:
            expected_type=call['args']['reason'] if call['tool']=='end' else call['tool']
            require(item['type']==expected_type,'converted text/action type differs');output.append(item)
        for boundary in pending:
            if boundary['position']=='after_call':output.append(snapshot(boundary,camera_batches))
        if pending:consumed.add(index)
    require(consumed==set(events),'some authored opportunities were not converted')
    result={'schema':SCHEMA,'observation_policy':policy(clock),'plan':output,'source_record_canonical_sha256':canonical(record)}
    if direct is not None:
        result['direct_control_observation_provenance']=direct
    return validate(result,cameras,clock=clock)