<?php
declare(strict_types=1);
try{
 require __DIR__.'/bootstrap.php';require __DIR__.'/RequestGate.php';$scenario=$_SERVER['HTTP_X_PILOT_SCENARIO']??'';$start=microtime(true);
 $final=strpos($scenario,'PILOT_RR_FINAL_')===0;
 $cases=json_decode(file_get_contents($runtime.($final?'/cases-FINAL.json':'/cases.json')),true);$case=requestGate($_SERVER,$cases,file_get_contents('php://input'));
 $_SERVER['SCRIPT_FILENAME']=$root.'/public/index.php';$_SERVER['SCRIPT_NAME']='/index.php';$_SERVER['PHP_SELF']='/index.php';
 $app=read12App($root,$runtime,$scenario);$hosts=['store.remaining-read.invalid'=>2147001111,'terminal.remaining-read.invalid'=>2147001112];$app->env->set('store.host',$hosts);$check(getStoreId()===$hosts[$case['host']],'Actual approved host/tenant');
 $before=$small($case['context']);$source=json_decode(file_get_contents(__DIR__.($final?'/source-final-sha.json':'/source-frozen-sha.json')),true)['files'];require __DIR__.'/SqlCoverageRecorder.php';
 $rec=new SqlCoverageRecorder($runtime.'/queries.jsonl',$root,$scenario);$ops=[];$app->db->listen(static function($sql,$elapsed=null)use(&$ops,$runtime,$scenario,$rec){if(strpos((string)$sql,'CONNECT:')===0)return;$kind=preg_match('/^\s*CALL\b/i',(string)$sql)?'metadata_CALL':(preg_match('/^\s*SHOW\b/i',(string)$sql)?'metadata_SHOW':'SELECT');$ops[]=$kind;$rec->observe($sql,$elapsed);file_put_contents($runtime.'/db-listen.jsonl',json_encode(['scenario'=>$scenario,'sql'=>$sql,'operation'=>$kind,'layer'=>'framework interpolation, not native binding'],JSON_UNESCAPED_UNICODE)."\n",FILE_APPEND|LOCK_EX);chmod($runtime.'/db-listen.jsonl',0600);});register_shutdown_function([$rec,'finish']);
 $http=$app->http;$response=$http->run();$after=$small($case['context']);$check($before===$after,'Independent six original table projections unchanged');
 $actual=[];foreach(get_included_files() as $file){if(strpos($file,$root.'/')!==0)continue;$rel=substr($file,strlen($root)+1);$actual[$rel]=hash_file('sha256',$file);$check(isset($source[$rel])&&$source[$rel]===$actual[$rel],'Actual included source exact '.$rel);}
 $persist('wire-'.$scenario.'.json',['scenario'=>$scenario,'case'=>$case,'http_status'=>$response->getCode(),'elapsed_ms'=>(microtime(true)-$start)*1000,'raw'=>$response->getContent(),'before'=>$before,'original'=>$after,'source_sha256'=>$actual,'db_listen_operations'=>$ops]);
 $response->send();$http->end($response);
}catch(Throwable $e){if(isset($persist))$persist('harness-failure-'.preg_replace('/[^A-Z0-9_]/','',strtoupper($scenario??'INIT')).'.json',['class'=>get_class($e),'message'=>$e->getMessage(),'file'=>$e->getFile(),'line'=>$e->getLine()]);http_response_code(500);header('Content-Type: application/json');echo json_encode(['harness_error'=>get_class($e),'message'=>$e->getMessage()]);}
