amos 3fa3dd73ca Add 1 MEDIUM task from batch 28: ESP event loop accounting
Batch 28 commit a7fca39 gave ESP event loop accounting as MEDIUM: 2 of
3 attempts passed and the failure was a wrong fix in a complete session.
The task comes from the commit Qwen measured, its digest matches, and it
is still present at the batch branch head.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 11:54:54 +08:00

196 lines
11 KiB
Python

"""Independent public registration/queue/callback model. Candidate executes only in children."""
import itertools, json, os, re, subprocess, sys, uuid
from pathlib import Path
CANDIDATES=['components/esp_event/esp_event.c','components/esp_event/private_include/esp_event_internal.h']
BASES=['SENSOR','CONTROL','STATUS']
def cases():
out=[]
# Scope, insertion order, legacy overwrite, instances, empty/unknown events, delete and independent loops.
for variant in range(4):
commands=['create 0 16','create 1 4']
regs=[(-1,-1),(0,-1),(0,7),(1,8),(0,8),(2,-1)]
if variant&1: regs=regs[2:]+regs[:2]
for tag,(base,id_) in enumerate(regs):commands.append('reg 0 %d %d %d %d 1'%(tag,base,id_,tag%3+1))
commands+=['reg 0 6 0 7 2 1','reg 1 12 -1 -1 2 1','dump']
for n,(base,id_,has) in enumerate([(0,7,1),(1,8,1),(0,8,0),(2,13,1),(1,7,1)]):
commands.append('post 0 %d %d %d %d %d 0'%(base,id_,16921+variant*41+n,38711+n,has))
commands+=['post 1 2 4 18881 98271 1 0','run 0 200','run 1 80','dump']
base,id_=regs[2];commands+=['unreg 0 2 %d %d 1'%(base,id_),'post 0 0 7 4311 8633 1 0','run 0 200','dump']
commands+=['unreg 0 21 1 -1 0','reg 0 20 1 -1 2 0','reg 0 21 1 -1 4 0','post 0 1 5 12307 9877 1 0','run 0 200','dump','unreg 0 21 1 -1 0','delete 1','dump','delete 0','dump']
out.append(('scopes-%d'%variant,commands))
# A finite run finishes on this task. A full queue later obtains a consumer through the generic OS wait service.
for capacity in (1,2,3):
for wait in (0,1,2,7):
cmds=['create 0 %d'%capacity,'reg 0 0 -1 -1 3 1','post 0 0 7 57191 98317 1 0','run 0 1']
for n in range(capacity):cmds.append('post 0 1 %d %d %d 1 0'%(n,10331+n,21991+n))
cmds+=['schedule 0 2 0','post 0 2 17 63127 81371 1 %d'%wait,'cancel','run 0 100','dump','delete 0','dump']
out.append(('wait-%d-%d'%(capacity,wait),cmds))
# Observationally identical loop counters/registration shapes with different measured callback work.
out.append(('same-loops',[
'create 0 4','create 1 4','reg 0 0 0 7 1 1','reg 1 1 0 7 5 1',
'post 0 0 7 53171 9971 1 0','post 1 0 7 73517 19973 1 0',
'post 0 0 7 87103 31397 1 0','post 1 0 7 57131 73199 1 0',
'run 0 40','run 1 40','dump',
'post 0 0 7 91831 53813 1 0','post 1 0 7 68311 11717 1 0',
'run 0 40','run 1 40','dump','delete 0','dump','delete 1','dump']))
out.append(('same-scope',[
'create 0 4','reg 0 0 0 7 1 1','reg 0 1 0 7 3 1','reg 0 2 0 7 7 1',
'post 0 0 7 53987 31391 1 0','run 0 40','dump',
'unreg 0 1 0 7 1','post 0 0 7 13997 93133 1 0','run 0 40','dump',
'delete 0','dump']))
# Error arguments and no-handler queues; no timing assumptions about internal work.
out.append(('boundaries',['create 0 1','reg 0 0 -1 3 0 1','unreg 0 1 -1 3 0','post 0 -1 3 4 8 1 0','post 0 0 -1 4 8 1 0','unreg 0 1 0 3 1','post 0 0 3 41719 81239 1 0','post 0 1 4 7 9 0 0','dump','run 0 0','dump','delete 0','dump']))
return out
def expected(commands):
loops={}; contexts={}; clock=0; scheduled=None; output=[]
def callbacks(loop,event):
nonlocal clock
base,id_,has,x,y=event
# These cases do not require implementation-specific traversal order: each event's callback records are compared as a multiset.
records=[]
for reg in loops[loop]['regs']:
if (reg['base']<0 or reg['base']==base) and (reg['id']<0 or reg['id']==id_):
records.append((reg['tag'],BASES[base],id_,has,x if has else 0,y if has else 0))
reg['inv']+=1;reg['time']+=reg['work']*1000;clock+=reg['work']
return records
def run(loop,budget):
nonlocal clock
start=clock; rec=[];q=loops[loop]['q']
while q:
rec+=callbacks(loop,q.pop(0))
if clock-start>=budget:break
return rec
for i,command in enumerate(commands,1):
p=command.split();op=p[0];v=list(map(int,p[1:]));ret=0;cb=[];consumer=None;dump=None
if op=='create':loops[v[0]]={'cap':v[1],'q':[],'regs':[],'rx':0,'dr':0}
elif op=='reg':
loop,tag,base,id_,work,inst=v;contexts[tag]=(base,id_,inst)
if base<0 and id_>=0:ret=258
else:
regs=loops[loop]['regs'];old=next((r for r in regs if not inst and not r['instance'] and r['base']==base and r['id']==id_),None)
if old:old.update(tag=tag,work=work)
else:regs.append(dict(tag=tag,base=base,id=id_,work=work,instance=inst,inv=0,time=0))
elif op=='unreg':
loop,tag,base,id_,inst=v
if inst and tag not in contexts:ret=258
elif base<0 and id_>=0:ret=258
else:loops[loop]['regs']=[r for r in loops[loop]['regs'] if not (r['base']==base and r['id']==id_ and (r['tag']==tag if inst else not r['instance']))]
elif op=='post':
loop,base,id_,x,y,has,wait=v;l=loops[loop]
if base<0 or id_<0:ret=258
else:
if len(l['q'])==l['cap'] and wait and scheduled and scheduled[1]<=wait:
cb=run(scheduled[0],scheduled[2]);scheduled=None;consumer=0
if len(l['q'])==l['cap']:l['dr']+=1;ret=263
else:l['q'].append((base,id_,has,x,y));l['rx']+=1
elif op=='run':cb=run(v[0],v[1])
elif op=='schedule':scheduled=v
elif op=='cancel':scheduled=None
elif op=='delete':del loops[v[0]]
elif op=='dump':
dump=[]
for l in loops.values():
regs=sorted((('ESP_EVENT_ANY_BASE' if r['base']<0 else BASES[r['base']]),('ESP_EVENT_ANY_ID' if r['id']<0 else str(r['id'])),r['inv'],r['time']) for r in l['regs'])
dump.append((l['rx'],l['dr'],regs))
output.append((i,ret,cb,consumer,dump,clock*1000))
return output
def parse_dump(lines):
result=[];current=None
for line in lines:
m=re.fullmatch(r'\s*LOOP @[^,]+,\S*\s+rx:(\d+)\s+dr:(\d+)\s*',line)
if m:current=[int(m[1]),int(m[2]),[]];result.append(current);continue
m=re.fullmatch(r'\s*HANDLER @\S+\s+ev:([^,]+),([^\s]+)\s+inv:(\d+)\s+time:(-?\d+)\s+us\s*',line)
if m and current is not None:current[2].append((m[1],m[2],int(m[3]),int(m[4])));continue
if line.strip()=='NO HANDLERS REGISTERED' and current is not None:continue
raise ValueError('dump grammar')
return [(a,b,sorted(c)) for a,b,c in result]
def perfect_match(wanted,observed,allows):
"""Every expected and observed semantic record is consumed exactly once."""
if len(wanted)!=len(observed):return False
edges=[[j for j,row in enumerate(observed) if allows(expect,row)] for expect in wanted]
assigned=[None]*len(observed)
def augment(index,seen):
for slot in edges[index]:
if slot in seen:continue
seen.add(slot)
if assigned[slot] is None or augment(assigned[slot],seen):
assigned[slot]=index
return True
return False
return all(augment(index,set()) for index in range(len(wanted)))
def dump_matches(wanted,observed):
def registration(expect,row):
base,id_,inv,work_interval=expect
actual_base,actual_id,actual_inv,actual_time=row
# The general clock reads do not advance time. Only the callback's declared
# os_advance work lies between its start and end, so its cumulative measured
# interval is [work_interval, work_interval], independent of timer call count.
return ((base,id_,inv)==(actual_base,actual_id,actual_inv) and
work_interval<=actual_time<=work_interval)
def loop(expect,row):
rx,dr,regs=expect
actual_rx,actual_dr,actual_regs=row
return (rx,dr)==(actual_rx,actual_dr) and perfect_match(regs,actual_regs,registration)
return perfect_match(wanted,observed,loop)
def compare(raw,exp):
lines=raw.splitlines();assert lines.pop(0)=='IDENTITY 65534 65534','child identity'
pos=0
for i,ret,cbs,consumer,dump,elapsed in exp:
got=[];gotconsumer=None;gotdump=None
while pos<len(lines) and not lines[pos].startswith('R '):
s=lines[pos];pos+=1
if s.startswith('CB '):
p=s.split();got.append((int(p[1]),p[2],*map(int,p[3:])))
elif s.startswith('CONSUMER '):gotconsumer=int(s.split()[1])
elif s=='DUMP %d'%i:
end=lines.index('ENDDUMP',pos);gotdump=parse_dump(lines[pos:end]);pos=end+1
else:raise ValueError('unexpected raw observation')
assert pos<len(lines) and lines[pos]=='R %d %d'%(i,ret),'API return line %d'%i;pos+=1
def groups(records):
return [(key,sorted(row[0] for row in group)) for key,group in itertools.groupby(records,lambda row:row[1:])]
assert groups(got)==groups(cbs),'FIFO callback delivery line %d'%i
assert gotconsumer==consumer,'consumer completion line %d'%i
if dump is not None:
assert dump_matches(dump,gotdump),'dump one-to-one counters/runtime line %d'%i
else:assert gotdump is None
assert pos==len(lines),'extra raw observations'
def main():
plans=[(name,'\n'.join(commands)+'\n',expected(commands)) for name,commands in cases()]
candidate=Path(sys.argv[1]);support=json.loads((Path(__file__).parent/'support.json').read_text())
sources={k:v.encode() for k,v in support.items()}
for f in CANDIDATES:sources[f]=(candidate/f).read_bytes()
# Complete every material and output parent before relinquishing scratch ownership.
scratch=Path('/tmp/event-public-'+uuid.uuid4().hex)
scratch.mkdir(mode=0o700)
for f,data in sources.items():
p=scratch/f;p.parent.mkdir(parents=True,exist_ok=True);p.write_bytes(data)
(scratch/'build').mkdir()
for p in [scratch,*scratch.rglob('*')]:os.chmod(p,0o755 if p.is_dir() else 0o644)
paths=[scratch,*scratch.rglob('*')]
for p in reversed(paths):os.chown(p,65534,65534)
# The trusted parent never reads, walks, changes or deletes scratch after this point.
argv=['gcc','-std=gnu11','-O0','-g','-Iplatform','-Icomponents/esp_event/include','-Icomponents/esp_event/private_include','components/esp_event/esp_event.c','platform/os.c','observer.c','-o','build/observe']
if os.environ.get('EVENT_PREPROCESS'):
pp=subprocess.run(argv[:1]+['-E']+argv[1:-2],cwd=scratch,capture_output=True,text=True,timeout=20)
sys.stderr.write('PREPROCESS_BEGIN\n'+pp.stdout+'PREPROCESS_END\n'+pp.stderr)
assert pp.returncode==0,'preprocessor'
built=subprocess.run(argv,cwd=scratch,capture_output=True,text=True,timeout=30)
assert built.returncode==0,'compiler: '+built.stderr[-3000:]
for name,input_text,expectations in plans:
obs=subprocess.run([str(scratch/'build/observe')],input=input_text,cwd=scratch,capture_output=True,text=True,timeout=4)
if os.environ.get('EVENT_RAW'):sys.stderr.write('RAW '+name+'\n'+obs.stdout+obs.stderr)
assert obs.returncode==0,name+' execution'
try:compare(obs.stdout,expectations)
except (AssertionError,ValueError,StopIteration,TypeError) as e:raise AssertionError(name+': '+str(e))
print('PASS')
if __name__=='__main__':
try:main()
except Exception as error:print('FAIL '+str(error));raise SystemExit(1)