"""Summarize full-trajectory position379 keys and directly timed tail work.""" from collections import Counter,defaultdict import hashlib,json,statistics,struct from pathlib import Path ROOT=Path(__file__).resolve().parents[2] OUT=ROOT/'test/position379-tail-20260917' RECORD=struct.Struct('=0: if b[10:14]!=b[14:18]:rejects['baseline_changes_x87_status']+=1 elif r[10:14]!=b[10:14]:rejects['environment_differs']+=1 elif key!=b[3:8]:rejects['key_differs']+=1 else:rejects['other']+=1 if r[1]>=0 and key==b[3:8]: key_hits+=1;group_hits[r[1]]+=1 if struct.pack('=0 and r[3:8]==b[3:8]], groups=[dict(key=[f'{x:016x}' for x in key],groups=colors) for key,colors in buckets.items()], keyHits=sum(r[1]>=0 and r[3:8]==b[3:8] for r in rr), guardedHits=sum(r[2] for r in rr))) assert len(groups)==896 and output_mismatches==0 baseline=[r for r in records if r[1]<0];probes=[r for r in records if r[1]>=0];hits=[r for r in records if r[2]];executed=[r for r in records if r[21]] assert sum(r[18] for r in records)==info['tailTicks'] # Conservative gate: charge raw lookup/capture/reset (including their timer # overhead), save tail work after subtracting one empty timer per hit. saved=sum(max(0,r[18]-empty) for r in hits)*scale lookup=sum(r[19] for r in probes)*scale baseline_cost=(info['baseLookupTicks']+info['captureTicks']+info['resetTicks'])*scale guarded_hit_rate=len(hits)/len(probes) result=dict(label=folder.name,clockHz=clock,emptyClockNs=empty/clock*1e9,entries=len(records),tailExecutions=len(executed),baselineEntries=len(baseline),probeEntries=len(probes), tailRawUs=stats([r[18]*scale for r in executed]),tailCorrectedUs=stats([max(0,r[18]-empty)*scale for r in executed]), hitTailCorrectedUs=stats([max(0,r[18]-empty)*scale for r in hits if r[21]]) if hits and any(r[21] for r in hits) else None, lookupHitUs=stats([r[19]*scale for r in hits]),lookupMissUs=stats([r[19]*scale for r in probes if not r[2]]), baselineAddedUsPerJacobian=baseline_cost/896,keyHits=key_hits,keyHitRate=key_hits/len(probes), baselineCaptureWithoutResetUs=stats([(r[19]+r[20])*scale for r in baseline]), unlimitedMemoTheoreticalHits=general_hits,unlimitedMemoHitRate=general_hits/len(probes), guardedHits=len(hits),guardedHitRate=guarded_hit_rate,envChanges=env_changes,bitKeyOutputMismatches=output_mismatches, baselineEnvChanges=baseline_env_changes,rejects=dict(rejects), environmentChanges={name:[dict(before=f'0x{a:x}',after=f'0x{b:x}',count=n) for (a,b),n in c.items()] for name,c in environment_changes.items()}, uniqueKeysHistogram=dict(Counter(j['uniqueKeys'] for j in jacobians)),groupHits=dict(group_hits), rawTailSavedMs=sum(r[18] for r in hits)*scale/1000,correctedTailSavedMs=saved/1000, allProbeLookupMs=lookup/1000,baselineAddedMs=baseline_cost/1000, conservativeDiagnosticNetMs=(saved-lookup-baseline_cost)/1000, hitBudgetUsAfterBaselineAndMisses=(saved-baseline_cost-sum(r[19] for r in probes if not r[2])*scale)/len(hits) if hits else 0, inputSha256=hashlib.sha256((folder/'tail-records.bin').read_bytes()).hexdigest()) (folder/'key-groups.json').write_text(json.dumps(jacobians,indent=2)+'\n',encoding='utf-8') (folder/'analysis.json').write_text(json.dumps(result,indent=2)+'\n',encoding='utf-8') return result def main(): rows=[analyze(p) for p in sorted(OUT.glob('diagnostic-*')) if (p/'tail-records.bin').exists()] assert rows # An original-execution miss may legitimately change FP status. Only the # proposed hit subset must have zero effect (asserted above). gate=all(r['conservativeDiagnosticNetMs']>0 and r['lookupHitUs']['mean'].8 for r in rows) timing=[] for folder in sorted(OUT.glob('tail-only-*')): if not (folder/'tail-records.bin').exists():continue info=json.loads((folder/'tail.json').read_text());records=list(RECORD.iter_unpack((folder/'tail-records.bin').read_bytes())) assert len(records)==info['entries']==info['executions']==24192 empty=statistics.median(x[0] for x in struct.iter_unpack('