Files
TheTree_mock/docs/plans/original-completion-2026-10-02/checkpoints/integer-window-codec-r290/p23-02-native-integer-window-codec-impact-whole-r293-r04.py

64 lines
7.5 KiB
Python

# coding: utf-8
from pathlib import Path
import hashlib,importlib.util,json,os,shutil,subprocess,sys,uuid
sys.dont_write_bytecode=True
R=Path(__file__).resolve().parents[2];A=R/'artifacts/original-completion-2026-10-02';D=R/'docs/plans/original-completion-2026-10-02';O=A/'p23-02-native-integer-window-codec-impact-whole-r293-r04';NAMES=['IMPLEMENTATION_PLAN.md','PROGRESS_INTEGRATION.md','work_packages.json','progress_snapshot.json']
def mod(name,p):
s=importlib.util.spec_from_file_location(name,p);m=importlib.util.module_from_spec(s);s.loader.exec_module(m);return m
def unpack(path):return subprocess.run(['/opt/homebrew/bin/zstd','-d','-q','-c',str(path)],check=True,capture_output=True,timeout=10).stdout
def main():
r=mod('r281',R/'tools/run_all_tests.py');outer=mod('owned270',r.SUPPORT/'owned_outer.py');plan=r.read(O/'input.json');fixed=plan['pins'];assert not (O/'execution.json').exists()
source=r.source_refs(R);player=r.default_player(R);docs={n:r.sha(D/n) for n in NAMES};assert source==plan['source_before'] and player==plan['default_before'] and docs==plan['doc4_before']
prepath=A/'p23-02-native-integer-window-codec-impact-whole-preexecution-independent-review-r293-r04.json';pre=r.read(prepath);assert pre['execution_allowed'] and not pre['blockers']
for group in ['evidence_sha256','canonical_doc4_context_pins']:
for key,q in pre[group].items():
f=Path(q.get('path',key));assert f.stat().st_size==q['bytes'] and r.sha(f)==q['sha256']
root=r.read(O/'preexecution-root-verification.json');assert root['passed'] and root['review_sha256']==r.sha(prepath)
assert r.read(fixed['local_review']['path'])['finite_local_equivalence_accepted']
auditpath=A/'p23-02-native-provider-fallback-static-independent-review-r280.json';audit=r.read(auditpath);assert audit['builtin_synchronous_fallback_supported'] and not audit['blockers']
auditroot=r.read(A/'p23-02-native-provider-fallback-static-root-verification-r280.json');assert auditroot['passed'] and auditroot['review_sha256']==r.sha(auditpath)
stage=Path(plan['stage']);parent=Path(plan['parent']);budget=stage/'scripts/core/performance_budget.gd';original=budget.read_bytes();modified=Path(fixed['modified_budget']['path']).read_bytes();public=(stage/'project.godot').read_bytes();patched=False
assert hashlib.sha256(original).hexdigest()==plan['original_budget_sha256']
def protect():
assert r.source_refs(R)==source and r.default_player(R)==player and {n:r.sha(D/n) for n in NAMES}==docs and r.inventory(r.SUPPORT)==plan['support_inventory']
assert r.inventory(parent,stage=True)==plan['expected_stage'] and r.inventory(stage,stage=True)==(plan['patched_stage'] if patched else plan['expected_stage'])
for key,q in fixed.items():
f=Path(q['path']);assert f.stat().st_size==q['bytes'] and r.sha(f)==q['sha256'],key
protect();oracle=unpack(fixed['oracle']['path']);assert len(oracle)==11487732 and hashlib.sha256(oracle).hexdigest()=='9df3c1b6f65dfd8be944cd5cab8c631f7533530dfef3820d9fa5d74a5b1ac30f'
phases=[];samples=[];error=None;cleanup=[];cleanup_errors=[]
try:
for mode in ['s6','baseline','candidate']:
protect();assert not outer.native.helper['live_engines']() and shutil.disk_usage(A).free>=plan['capacity_floor_bytes']
budget.write_bytes(modified);patched=True
protect();output=O/(mode+'-measurement');output.mkdir();ident=uuid.uuid4().hex;startup='TheTree-PCK-tests-'+ident+'-budget-startup';profile='TheTree-PCK-tests-'+ident+'-budget-runtime';assert not r.profile_path(startup).exists() and not r.profile_path(profile).exists()
env=r.fresh_environment(os.environ);env.update(THE_TREE_PACK_PROFILE_NAME=profile,THE_TREE_PACK_PROFILE_EXPECTED=str(r.profile_path(profile)),THE_TREE_PACK_PROFILE_REPORT=str(O/(mode+'-profile.json')),THE_TREE_RUNTIME_MONITOR_OUTPUT=str(output),THE_TREE_REPLAY_SNAPSHOT=fixed['fixture']['path'],THE_TREE_CANDIDATE_ORACLE=fixed['oracle']['path'],THE_TREE_PAIRED_CANDIDATE=mode,THE_TREE_PAIRED_SAMPLE=mode,THE_TREE_EXPECTED_BUDGET_SHA=fixed['modified_budget']['sha256'],THE_TREE_EXPECTED_PATH_SHA=fixed['original_path']['sha256'])
(stage/'project.godot').write_bytes(r.private_project(public,startup));phase=O/mode;argv=[fixed['engine']['path'],'--headless','--path',str(stage),'--script',fixed['starter_'+mode]['path'],'--verbose','--log-file',str(phase/'engine-native.log')]
result,capture=outer.run_phase(argv,phase,90,env,now_fn=r.now,dump_fn=r.write,sha_fn=r.sha,live_engines_fn=outer.native.helper['live_engines']);row=dict(mode=mode,result=result,capture=capture);phases.append(row);r.write(O/(mode+'-phase.json'),row)
rp=output/'report.json';report=r.read(rp) if rp.exists() else None
if report is not None:samples.append(report)
assert result and result['exit_code']==0 and not result['error_lines'] and not result['timed_out'] and capture['passed']
logs=''.join((phase/n).read_text() for n in ['stdout.log','stderr.log','engine-native.log']);assert 'Orphan StringName:' not in logs and 'unclaimed string names' not in logs
assert report and report['passed'] and not report['failures'] and report['pid']==result['pid']
stdout=(phase/'stdout.log').read_text();assert stdout.count('NATIVE_ADAPTER_LIBRARY_REGISTERED')==1
if mode=='s6':assert report['mode']==mode and report['checks']==24 and stdout.count('PASS: 24 checks')==1
else:
assert report['candidate']==mode and report['sample']==mode and report['checks']==15 and stdout.count('SDK whole647: 15 checks, 0 failures')==1
ref=report['after647'];raw=unpack(ref['path']);assert raw==oracle and len(raw)==ref['uncompressed_bytes'] and hashlib.sha256(raw).hexdigest()==ref['uncompressed_sha256']
r.profile_gate(r.profile_path(profile),O/(mode+'-profile.json'),profile);assert sum(f.stat().st_size for f in O.rglob('*') if f.is_file())<=16*1048576
(stage/'project.godot').write_bytes(public);budget.write_bytes(original);patched=False;protect()
except BaseException as caught:error=type(caught).__name__+': '+str(caught)
finally:
cleanup,cleanup_errors=outer.finish_captured(outer.OWNED)
if all(q['engine_ended'] is True for q in cleanup):budget.write_bytes(original);patched=False;(stage/'project.godot').write_bytes(public)
else:cleanup_errors.append('Budget/project restoration refused while owned child remains live')
guard_errors=[]
try:assert not patched and budget.read_bytes()==original;protect()
except BaseException as caught:guard_errors.append(type(caught).__name__+': '+str(caught))
pairs=[]
if len(samples)==3:
before=samples[1]['manual_tick_us'];after=samples[2]['manual_tick_us'];assert before>0 and after>=0;pairs=[dict(baseline_us=before,candidate_us=after,ratio=after/before,reduction_percent=(before-after)*100.0/before,scope='One adjacent cold paused speed1 headless observation only; no stable/P95/budget acceptance')]
passed=error is None and not guard_errors and not cleanup_errors and len(phases)==3 and len(samples)==3
r.write(O/'execution.json',dict(passed=passed,error=error,guard_errors=guard_errors,phases=phases,samples=samples,pairs=pairs,cleanup=cleanup,cleanup_errors=cleanup_errors,shared_Budget_restored_byteexact=not patched and budget.read_bytes()==original,input_source_default_doc4_preserved=not guard_errors,candidate_adopted=False,unit_accepted=False,performance_accepted=False,limitations=['Candidate original S6/GameClock impact first; only on pass baseline/candidate whole647 equality and one finite timing pair. No stable/P95/D10/50ms/final172/Main/render/OS/realtime x3/Windows/adoption/unit acceptance.']))
print(json.dumps(dict(passed=passed,error=error,guard_errors=guard_errors,pairs=pairs)),flush=True);return 0 if passed else 1
if __name__=='__main__':raise SystemExit(main())