<?php
declare(strict_types=1);try{require __DIR__.'/bootstrap.php';require __DIR__.'/natural.php';$start=microtime(true);$path=parse_url($_SERVER['REQUEST_URI']??'',PHP_URL_PATH);$scenario=$_SERVER['HTTP_X_PILOT_SCENARIO']??'';if(($_SERVER['REQUEST_METHOD']??'')!=='GET'||!preg_match('#^/(?:admin/store\.Equipment/(?:detail|lists|getStoreList)|terminal/(?:dishes\.Img/all|recommend\.Recommend/dishesCategory))$#D',(string)$path)||!preg_match('/^PILOT_AD_[A-Z0-9_]{1,60}$/D',$scenario)){http_response_code(403);echo '{"denied":true}';return;}
ini_set('log_errors','1');ini_set('error_log',$runtime.'/private-errors.log');$_SERVER['SCRIPT_FILENAME']=$root.'/public/index.php';$_SERVER['SCRIPT_NAME']='/index.php';$_SERVER['PHP_SELF']='/index.php';$app=commentApp($root,$runtime);$hosts=['terminal.admin-dishes-read.invalid'=>2147001112,'admin.admin-dishes-read.invalid'=>2147001111];$app->env->set('store.host',$hosts);$check(isset($hosts[$app->request->host()])&&getStoreId()===$hosts[$app->request->host()],'Only own two hosts; equipment1113 has no mapped host');$before=$own();$nBefore=$natural();$protected=$protect();$cfg=$app->config->get('database');foreach($cfg['connections'] as &$c){$c['trigger_sql']=true;$c['debug']=false;}unset($c);$app->config->set($cfg,'database');$app->db->setConfig($app->config);require __DIR__.'/SqlCoverageRecorder.php';$rec=new SqlCoverageRecorder($runtime.'/queries.jsonl',$root,$scenario);$ops=[];$app->db->listen(static function($sql,$elapsed=null)use($rec,$runtime,$scenario,&$ops){if(strpos((string)$sql,'CONNECT:')===0)return;$op=SqlCoverageRecorder::fingerprint($sql)['operation'];$ops[]=$op;if(!in_array($op,['select','other'],true)||preg_match('/^\s*(INSERT|UPDATE|DELETE|ALTER|DROP|CREATE|TRUNCATE|MERGE|REPLACE|GRANT|REVOKE)\b/i',$sql))throw new RuntimeException('Only real business read allowed');$rec->observe($sql,$elapsed);file_put_contents($runtime.'/private-sql.jsonl',json_encode(['scenario'=>$scenario,'sql'=>$sql,'layer'=>'ThinkPHP framework listener debug interpolation, not PDO queryString/server audit'],JSON_UNESCAPED_UNICODE)."\n",FILE_APPEND|LOCK_EX);chmod($runtime.'/private-sql.jsonl',0600);});register_shutdown_function([$rec,'finish']);
$http=$app->http;$response=$http->run();$hashes=[];$frozen=json_decode(file_get_contents(__DIR__.(strpos($scenario,'PILOT_AD_NA_')===0?'/source-http-frozen-sha.json':'/source-http-final-sha.json')),true)['files'];foreach($frozen as $f=>$sha){$class=str_replace('/','\\',substr($f,0,-4));$actual=(new ReflectionClass($class))->getFileName();$check(realpath($actual)===realpath($root.'/'.$f),'Actual isolated source identity '.$f);$hashes[$f]=hash_file('sha256',$actual);$check($hashes[$f]===$sha,'Frozen pre-HTTP source unchanged '.$f);}$after=$own();$nAfter=$natural();$fixture=json_decode(file_get_contents($runtime.'/fixture-journal.json'),true);$check($before===$after&&$after===$fixture['after'],'All32 original rows exact read-only');$check($nBefore===$nAfter&&$nAfter===$fixture['natural_after'],'Natural public projections/equipment digests exact');$check($protect()===$protected,'Prior natural scope exact');$w=['scenario'=>$scenario,'method'=>'GET','path'=>$path,'http_status'=>$response->getCode(),'elapsed_ms'=>(microtime(true)-$start)*1000,'raw'=>$response->getContent(),'before'=>$before,'original'=>$after,'natural'=>$nAfter,'natural_digest'=>naturalDigest($nAfter),'protect_before'=>$protected,'protect_after'=>$protect(),'source_sha256'=>$hashes,'operations'=>$ops];file_put_contents($runtime.'/private-wire.jsonl',json_encode($w,JSON_UNESCAPED_UNICODE|JSON_PRESERVE_ZERO_FRACTION)."\n",FILE_APPEND|LOCK_EX);chmod($runtime.'/private-wire.jsonl',0600);$response->send();$http->end($response);
}catch(Throwable $e){if(isset($persist))$persist('harness-failure.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()]);}
