<?php
declare(strict_types=1);function actualRepeatLock($app,$start,$end){global $runtime;$key='meal_direct_pay_'.CoreGate::$ids['AI_U1'].'_'.CoreGate::$ids['STORE_R1'];$driver=$app->cache->store('file');$file=$driver->getCacheKey($key);ck(strpos($file,$runtime.'/cache/')===0&&is_file($file)&&!is_link($file),'Exact original repeat file');clearstatcache(true,$file);$raw=file_get_contents($file);$ttl=(int)substr($raw,8,12);ck($ttl===5&&substr($raw,32)===serialize(1),'Original TTL5 and value1');return ['earliest_possible_lock_microtime'=>$start,'business_end_microtime'=>$end,'actual_filemtime_epoch'=>filemtime($file),'actual_TTL'=>$ttl,'exact_own_file'=>$file,'original_cache_driver_class'=>get_class($driver),'original_lock_file_sha256'=>hash('sha256',$raw),'observed_server_epoch'=>time()];}$caseName=$_SERVER['HTTP_X_CORE_CASE']??'INIT';try{require __DIR__.'/app.php';$cases=[];foreach(['cases.json','cases-resume.json','cases-direct-retry.json','cases-C-resume.json','cases-direct-last.json'] as $cf)if(is_file($runtime.'/'.$cf))$cases=array_merge($cases,json_decode(file_get_contents($runtime.'/'.$cf),true));ck(isset($cases[$caseName]),'Exact authorized case label');$case=$cases[$caseName];$tokens=json_decode(file_get_contents($runtime.'/tokens.json'),true);ck($_SERVER['REQUEST_METHOD']==='POST'&&parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH)===$case['path']&&($_SERVER['QUERY_STRING']??'')===''&&($_SERVER['HTTP_HOST']??'')==='core.scan-pay.invalid'&&($_SERVER['HTTP_PLATFORM']??'')==='POC'&&($_SERVER['CONTENT_TYPE']??'')==='application/json'&&file_get_contents('php://input')===$case['body_raw']&&($_SERVER['HTTP_ACCESS_TOKEN']??'')===$tokens[$case['actor']],'Original method/path/body/tenant/Auth bytes');ck(!file_exists($runtime.'/wire-attempt-'.$caseName.'.json'),'Never replay case');saveOnce('wire-attempt-'.$caseName.'.json',['case'=>$caseName,'started_microtime'=>microtime(true)]);$_SERVER['SCRIPT_FILENAME']=$root.'/public/index.php';$_SERVER['SCRIPT_NAME']='/index.php';$_SERVER['PHP_SELF']='/index.php';$app=coreApp($caseName);CoreGate::$lease=ownSnapshot();$before=CoreGate::$lease;$oldBefore=oldProtection();$guard=json_decode(file_get_contents($runtime.'/guard.json'),true);ck($oldBefore===$guard['old_protection'],'Original protection lease before HTTP');$identity=json_decode(file_get_contents($runtime.'/prepared.json'),true);foreach(['AI_U2','STORE_S1','STORE_U1','STORE_U2','STORE_R1'] as $name)ck($before[$name]===[$identity['rows'][$name]],'Immutable fresh identity fullrow '.$name);ck($before['staff_department']===[]&&$before['one_card_account_map']===[]&&$before['user_subsidy']===[]&&$before['user_gift_log']===[],'Before each HTTP excluded money/identity scope0');
 if($caseName==='DIRECT_REPEAT'){ck(file_exists($runtime.'/direct-success-lock-upper.json'),'Observed successful direct payment before repeat');$lock=json_decode(file_get_contents($runtime.'/direct-success-lock-upper.json'),true);if(!isset($lock['actual_filemtime_epoch'],$lock['actual_TTL'])||$lock['actual_TTL']!==5||time()>=$lock['actual_filemtime_epoch']+4){saveOnce('repeat-window-missed.json',['case'=>$caseName,'at'=>microtime(true),'earliest_possible_lock'=>$lock['earliest_possible_lock_microtime'],'business_HTTP_run'=>false]);http_response_code(200);header('Content-Type: application/json');echo json_encode(['harness_repeat_window_missed'=>true]);exit;}}
 $businessStart=microtime(true);$http=$app->http;$response=$http->run();$businessEnd=microtime(true);$raw=$response->getContent();$json=json_decode($raw,true);if(in_array($caseName,['DIRECT_SUCCESS','DIRECT_SUCCESS_RETRY1','DIRECT_SUCCESS_RETRY2'],true)&&is_array($json)&&($json['status']??null)===200)saveOnce('direct-success-lock-upper.json',actualRepeatLock($app,$businessStart,$businessEnd));$after=ownSnapshot();$oldAfter=oldProtection();ck($oldBefore===$oldAfter,'Old fullrow protection after HTTP');if(!in_array($caseName,['PREPAY_FIRST','PREPAY_FIRST_RETRY','PREPAY_FIRST_RETRY1','DIRECT_SUCCESS','DIRECT_SUCCESS_RETRY1','DIRECT_SUCCESS_RETRY2'],true))ck($before===$after,'Boundary/repeat full business data unchanged');$pins=json_decode(file_get_contents(__DIR__.'/source-frozen-sha.json'),true)['files'];$included=[];foreach(get_included_files() as $f)if(strpos($f,$root.'/')===0){$n=substr($f,strlen($root)+1);$included[$n]=hash_file('sha256',$f);ck(isset($pins[$n])&&$pins[$n]===$included[$n],'Actual included immutable source '.$n);}saveOnce('wire-'.$caseName.'.json',['lane'=>$lane,'case'=>$case,'raw_http_status'=>$response->getCode(),'raw_response_body'=>$raw,'business_started_microtime'=>$businessStart,'business_finished_microtime'=>$businessEnd,'own_before'=>$before,'own_after'=>$after,'original_before'=>$oldBefore,'original_after'=>$oldAfter,'native_DML_counts'=>CoreGate::$counts,'actual_native_generated_IDs'=>CoreGate::$generated,'original_orderNo'=>CoreGate::$orderNo,'included_source_sha256'=>$included]);$response->send();$http->end($response);
}catch(Throwable $e){if(function_exists('saveOnce')&&!file_exists($runtime.'/harness-failed-'.$caseName.'.json'))saveOnce('harness-failed-'.$caseName.'.json',['class'=>get_class($e),'message'=>$e->getMessage(),'file'=>$e->getFile(),'line'=>$e->getLine(),'at'=>date('c')]);http_response_code(500);header('Content-Type: application/json');echo json_encode(['harness_error'=>get_class($e),'message'=>$e->getMessage()]);}
