<?php
declare(strict_types=1);$sent=false;$label=$_SERVER['HTTP_X_CORE_CASE']??'UNKNOWN';try{require __DIR__.'/app.php';require __DIR__.'/observer.php';require __DIR__.'/data.php';require __DIR__.'/scope.php';require __DIR__.'/cases.php';require __DIR__.'/dto.php';$map=[];foreach(cases() as $c)$map[$c['label']]=$c;ck(isset($map[$label]),'Exact approved route label');$c=$map[$label];$token=json_decode(file_get_contents($runtime.'/tokens.json'),true)['U1'];ck($_SERVER['REQUEST_METHOD']===$c['method']&&parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH)===$c['path']&&($_SERVER['QUERY_STRING']??'')===$c['query']&&($_SERVER['HTTP_HOST']??'')==='owned-scale.invalid'&&($_SERVER['HTTP_PLATFORM']??'')==='POC'&&($_SERVER['HTTP_ACCESS_TOKEN']??'')===$token&&file_get_contents('php://input')===$c['body_raw'],'Exact authorized original request bytes/actor');ck(!file_exists($runtime.'/request-'.$label.'.json'),'Never replay');saveOnce('request-'.$label.'.json',['case'=>$c,'at'=>microtime(true),'token_sha256'=>hash('sha256',$token)]);$_SERVER['SCRIPT_FILENAME']=$root.'/public/index.php';$_SERVER['SCRIPT_NAME']='/index.php';$_SERVER['PHP_SELF']='/index.php';$app=coreApp($label);$writer=CoreGate::writer();$cap=$writer?($lane==='B'?100:180):($lane==='B'?60:120);ck(count(file($runtime.'/sql-budget.jsonl'))+$cap+100<=1000,'Reserve exact native and final protection under unified1000');if($writer){ck(date('Y-m-d')==='2026-10-11','No date drift');saveOnce('before-'.$label.'.json',['AI'=>ownRows(),'scale'=>scaleRows(),'AI_decimal_text'=>decimals()]);}$start=microtime(true);$http=$app->http;$response=$http->run();$end=microtime(true);preserveSendObserve($response,static function($r)use($label){saveOnce('response-'.$label.'.json',$r);},static function()use($response,&$sent){$response->send();$sent=true;},static function($r)use($label,$writer){global $runtime;$scale=scaleRows();if($writer){$rows=ownRows();saveOnce('after-'.$label.'.json',['AI'=>$rows,'scale'=>$scale,'counts'=>CoreGate::$counts,'generated'=>CoreGate::$generated,'AI_decimal_text'=>decimals(),'Store_value_text'=>recordText(),'Store_committed'=>CoreGate::$storeCommitted,'AI_committed'=>CoreGate::$mainCommitted]);ck(CoreGate::$counts===['INSERT.ydy_measurement_session'=>1,'INSERT.ydy_measurement_session_raw'=>1,'INSERT.ydy_health_record'=>3,'UPDATE.yoshop_user_weight'=>1,'UPDATE.yoshop_staff'=>1]&&CoreGate::$storeCommitted&&CoreGate::$mainCommitted,'Mandatory real5I2U/two commits; swallowed failure forbidden');$ids=array_column($scale['sessions'],'session_id');$file=$runtime.'/scale-flow.json';if(file_exists($file)){ck($label==='W2_YUNKANGBAO_ADD','Flow only changes for exact second writer');$old=json_decode(file_get_contents($file),true);ck(count($ids)===2&&count(array_intersect($old['session_ids'],$ids))===1,'Only one new genuine second session');file_put_contents($runtime.'/scale-flow-after-W2.json',json_encode(['session_ids'=>$ids]));chmod($runtime.'/scale-flow-after-W2.json',0600);}else saveOnce('scale-flow.json',['session_ids'=>$ids]);}else saveOnce('read-scale-'.$label.'.json',$scale);foreach(file($runtime.'/business-native.jsonl') as $line){$e=json_decode($line,true);ck($e['scenario']!==$label||!isset($e['error']),'No swallowed original native/gate failure');}saveOnce('included-'.$label.'.json',['files'=>includedProof(),'native_count'=>CoreGate::$total,'counts'=>CoreGate::$counts]);checkDTO($label,$r,$scale);},static function($e)use($label){saveOnce('observer-error-'.$label.'.json',$e);},$start,$end);try{$http->end($response);}catch(Throwable $e){saveOnce('http-end-error-'.$label.'.json',['message'=>$e->getMessage()]);}}catch(Throwable $e){if(function_exists('saveOnce')&&!file_exists($runtime.'/harness-error-'.$label.'.json'))saveOnce('harness-error-'.$label.'.json',['class'=>get_class($e),'message'=>$e->getMessage(),'sent'=>$sent]);if(!$sent){http_response_code(500);echo json_encode(['harness_error'=>get_class($e),'message'=>$e->getMessage()]);}}
