"""Verify each actual read and ordered write, then summarize the two cases.""" from collections import Counter from pathlib import Path import ctypes as c import hashlib import json import struct ROOT = Path(__file__).resolve().parents[2] OUT = ROOT/'test/context-access-20260917' class Medium(c.Structure): _fields_ = [('real_helium', c.c_int)] + [(k, c.c_double) for k in ['R','cp','Tref','slope','mu','muT','S']] class State(c.Structure): _fields_ = [('medium', Medium)] + [(k, c.c_double) for k in ['p','T','h','rho','mu','isentropic_factor','isentropic_exponent']] + [('valid', c.c_uint),('temperatures',c.c_void_p),('jacobian',c.c_void_p)] class Pipe(c.Structure): _fields_ = [('medium',Medium)] + [(k,c.c_double) for k in ['p1','p2','T','diameter','length','roughness','flow']] + [('kind',c.c_int),('valid',c.c_int)] class Context(c.Structure): _fields_ = [('states',c.c_void_p),('count',c.c_size_t),('capacity',c.c_size_t),('temperatures',c.c_void_p),('jacobian',c.c_void_p)] def doubles(value): data = bytes.fromhex(value) return list(struct.unpack('<'+'d'*(len(data)//8),data)) def value(data, typ): if typ == c.c_double: return struct.unpack('74 else None if st74: summary['state74']={'p':st74.p,'T':st74.T,'valid':st74.valid} pending=None for event in events[2:]: kind=event['event'] if kind=='query': assert pending is None pending=dict(event, allMatches=first_match(memory,event), tested=[]) elif kind=='match': assert pending and pending['kind']==event['kind'] matches=pending['allMatches']; expected=matches[0] if matches else -1 assert event['slot']==expected and event['hit']==bool(matches) expected_scans=list(range(expected+1 if expected>=0 else pending['count'])) assert pending['tested']==expected_scans,(begin,pending) summary['queries'].append(dict(kind=event['kind'],key=pending['key'],keyValues=doubles(pending['key']),mediumKind=pending['mediumKind'],hit=event['hit'],slot=event['slot'],allMatches=matches,scanCount=len(expected_scans))) pending=None elif kind in ['read','write']: domain=event['domain']; offset=event['offset']; data=bytes.fromhex(event['value']) previous=memory[domain][offset:offset+len(data)] slot,field,typ=named(domain,offset,len(data)) if kind=='read': assert previous==data,(begin,event,'read mismatch') summary['accessReads']+=1 # Lookup reads are recorded in raw events; this list isolates # fields consumed after selection, including observers/keys. if domain!='context' and pending is None and event['function']!='same_medium': summary['consumed'].append(dict(domain=domain,slot=slot,field=field,value=event['value'],function=event['function'])) else: summary['accessWrites']+=1 equal=all(known[domain][offset:offset+len(data)]) and previous==data summary['sameValueWrites']+=equal memory[domain][offset:offset+len(data)]=data known[domain][offset:offset+len(data)]=b'\1'*len(data) summary['writes'].append(dict(domain=domain,slot=slot,field=field,value=event['value'],sameValue=equal,function=event['function'])) elif kind=='valid_test': st=State.from_buffer_copy(memory['states'],event['slot']*c.sizeof(State)) assert st.valid & event['mask']==event['value'] if pending is not None: pending['tested'].append(event['slot']) else: summary['validTests'].append(event) elif kind=='allocate': assert event['branch']=='append', 'Scratch requires additional object registration; fail closed.' assert event['slot']==event['countAfter']-1 assert event['countAfter']==Context.from_buffer_copy(memory['context']).count assert event['countAfter']<=event['capacity'] summary['allocations'].append(event) elif kind=='snapshot': assert event['phase']=='exit' for domain in memory: expected=bytes.fromhex(event[domain]) assert memory[domain][:len(expected)]==expected,(begin,domain,'write replay mismatch') summary['exitCount']=event['count'] # Everything not written remains the live probe entry, checked # across every byte of every existing slot and every pipe slot. summary['replayExact']=True elif kind=='end': summary['outputs']=event['outputs'] summary['outputValues']=doubles(event['outputs']) elif kind.startswith('scalar_'): summary['scalar'].append(event) else: raise AssertionError(event) assert pending is None and summary['replayExact'] # Snapshot replay alone cannot detect an omitted equal-valued write. # Check the reviewed source's mandatory store sequence independently. pipe_writes=[w for w in summary['writes'] if w['domain']=='pipes'] assert [w['field'] for w in pipe_writes]==['valid','medium','p1','p2','T','diameter','length','roughness','kind','flow','valid'] assert int.from_bytes(bytes.fromhex(pipe_writes[0]['value']),'little')==0 assert int.from_bytes(bytes.fromhex(pipe_writes[-1]['value']),'little')==1 assert pipe_writes[-2]['value']==summary['outputs'] assert all(w['slot']==(28 if begin['position']==52 else 0) for w in pipe_writes) for allocation in summary['allocations']: writes=[w for w in summary['writes'] if w['domain']=='states' and w['slot']==allocation['slot']] assert [w['field'] for w in writes[:7]]==['*','medium','p','T','valid','temperatures','jacobian'] assert not [w for w in summary['writes'] if w['domain']=='states' and w['slot']