<?php
declare(strict_types=1);
require __DIR__.'/bootstrap.php';require __DIR__.'/StrictWire.php';$p=json_decode((string)file_get_contents($runtime.'/private-retention.json'),true);$report=json_decode((string)file_get_contents($runtime.'/report.json'),true);$check($p['marker']===$marker&&$p['tenant']===$tenant&&empty($p['errors']),'Exact retained green context');$owned=$p['owned_primary_keys'];$sid=$p['staff_id'];$uid=$p['user_id'];$masterIds=$p['master_ids'];$check(count($masterIds)===2,'Existing two own masters');$auditBefore=count(file($runtime.'/write-audit.jsonl',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES)?:[]);
foreach($pks as $table=>$pk){$rows=$query('SELECT * FROM yoshop_'.$table.' WHERE store_id=? ORDER BY '.$pk,[$tenant]);$expectedIds=$owned[$table];$present=array_map('intval',array_column($rows,$pk));foreach($present as $id)$check(in_array($id,$expectedIds,true),'Fresh retained exact PK '.$table);foreach($rows as $r){if($table==='user')$check($r['nick_name']===$marker.'_USER','Own User name');elseif($table==='staff')$check((int)$r['id']===$sid&&(int)$r['user_id']===$uid&&$r['name']===$marker.'_STAFF'&&$r['create_by']===$marker,'Positive own Staff/User');elseif($table==='health')$check((int)$r['staff_id']===$sid&&(int)$r['health']===5&&$r['create_by']===$marker,'Retained health5');else $check($r['staff_id']===(string)$sid&&$r['create_by']===$marker,'Own committed CSV staff/creator');}}
$sports=$query('SELECT id,name FROM yoshop_sport ORDER BY id');$names=[];foreach($sports as $s)$names[(string)$s['id']]=$s['name'];$staffName=$query('SELECT name FROM yoshop_staff WHERE id=? AND store_id=?',[$sid,$tenant])[0]['name'];
$detail=static function(int $id)use($query,$check,$tenant,$sid,$marker,$names,$staffName):array{$r=$query('SELECT * FROM yoshop_athlete_master WHERE id=? AND store_id=?',[$id,$tenant]);$check(count($r)===1&&$r[0]['staff_id']===(string)$sid&&$r[0]['create_by']===$marker,'Original exact detail owner');$out=$r[0];foreach(['id','sport_id','start_age','end_age','gender','store_id','is_delete'] as $k)$out[$k]=(int)$out[$k];foreach(['create_time','update_time'] as $k)$out[$k]=date('Y-m-d H:i:s',strtotime((string)$out[$k]));$out['sport_event_id']=!empty($out['sport_event_id'])?explode(',',$out['sport_event_id']):[];$out['staff_id']=[(string)$sid];$pla=[];foreach($query('SELECT strength,pal FROM yoshop_athlete_pal WHERE master_id=? AND store_id=? ORDER BY id',[$id,$tenant]) as $v)$pla[]=['strength'=>(int)$v['strength'],'pal'=>bcadd((string)$v['pal'],'0',2)];$check(count($pla)===5,'Original five committed PAL rows');$out['pla']=$pla;$out['sport_name']=$names[(string)$out['sport_id']];$out['sport_event_name']=implode(',',array_map(static function($id)use($names){return $names[(string)$id]??'';},$out['sport_event_id']));$out['staff_names']=$staffName;return ['detail'=>$out];};
$baseline=$p['cases'];$corrections=[];foreach($baseline as &$c){if(!in_array($c['scenario'],['PILOT_CF_CONFIG_DETAIL_CREATE','PILOT_CF_CONFIG_DETAIL_REPEAT_CREATE'],true))continue;$id=$c['body']['data']['detail']['id'];$raw=json_decode($p['responses'][$c['scenario']]['raw']);$pass=strictWireEqual($raw->data,json_decode(json_encode($detail($id))));$c['complete_mysql_original_projection_equal']=$pass;$corrections[]=['scenario'=>$c['scenario'],'strict_object_keys_scalar_types_array_order_verified'=>true,'original_mysql_projection_equal'=>$pass];}unset($c);
define('TP_ENV','poc');define('BUSINESS','aizhct');define('ENTERPRISE_NO','10001');require $root.'/vendor/autoload.php';$app=new \think\App($root);$app->setEnvName('poc');$app->setRuntimePath($runtime.'/fixture-runtime/');$app->initialize();$cache=new \think\cache\driver\File($app,['path'=>$runtime.'/cache/','prefix'=>'','expire'=>0]);$token=bin2hex(random_bytes(24));$cache->set($token,['user'=>['store_user_id'=>$tenant,'store_id'=>$tenant,'real_name'=>$marker,'user_name'=>$marker,'is_super'=>1],'store_id'=>$tenant,'is_login'=>true],1800);
$shape=static function($v)use(&$shape){if(is_object($v)){$o=[];foreach(get_object_vars($v) as $k=>$x)$o[$k]=$shape($x);ksort($o);return ['object'=>$o];}if(is_array($v))return ['array'=>array_map($shape,$v)];return gettype($v);};$new=[];$rawNew=[];$mysqlUnchanged=[];
try{foreach(['CREATE','REPEAT_CREATE'] as $i=>$phase){$old='PILOT_CF_CONFIG_DETAIL_'.$phase;$scenario='PILOT_CF_GREEN_CONFIG_DETAIL_'.$phase;$h="Host: store.staff-config.invalid\r\nAccess-Token: ".$token."\r\nPlatform: POC\r\nX-Pilot-Scenario: ".$scenario."\r\nContent-Type: application/json\r\n";$raw=file_get_contents($base.'/store/store.Config/detail',false,stream_context_create(['http'=>['method'=>'POST','header'=>$h,'content'=>json_encode(['id'=>$masterIds[$i]]),'timeout'=>30,'ignore_errors'=>true]]));$body=json_decode($raw,true);$check($body['status']===200,'Green detail success');$check(strictWireEqual(json_decode($raw)->data,json_decode(json_encode($detail($masterIds[$i])))),'Complete green original detail value/type/container contract');$new[$old]=['scenario'=>$scenario,'method'=>'POST','path'=>'/store/store.Config/detail','http_status'=>200,'body'=>$body,'raw_json_shape'=>$shape(json_decode($raw)),'complete_mysql_original_projection_equal'=>true];$rawNew[$scenario]=$raw;
        if($lane==='B'){$before=json_decode($p['responses'][$old]['raw']);$after=json_decode($raw);$check(preg_match('/^[0-9a-f]{13}$/D',$before->trace_id)===1&&preg_match('/^[0-9a-f]{13}$/D',$after->trace_id)===1,'MySQL before/after trace format');$before->trace_id=$after->trace_id='VALID_DYNAMIC_TRACE';$check(strictWireEqual($before,$after),'Complete original MySQL response unchanged after PAL patch');$mysqlUnchanged[]=['before'=>$old,'after'=>$scenario,'complete_wire_value_type_container_equal'=>true];}
    }}finally{$cache->delete($token);$tokenGone=$cache->get($token)===null;$cache->clear();}
$cases=$baseline;foreach($cases as &$c)if(isset($new[$c['scenario']]))$c=$new[$c['scenario']];unset($c);$check($snapshot()===$p['before'],'Green original seven table exact-owned-PK exclusion SHA');$auditAfter=count(file($runtime.'/write-audit.jsonl',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES)?:[]);$check($auditAfter===$auditBefore,'Read-only green replay DML0');
$normal=static function($x,string $context='')use(&$normal,$sid,$masterIds){if(!is_array($x))return $x;if(isset($x['path']))$context=strpos($x['path'],'getStaffInfo')!==false?'staff':'master';foreach($x as $k=>&$v){if($k==='id'&&$context==='staff'&&$v===$sid)$v=1;elseif($k==='id'&&$context==='master'&&is_int($v)&&in_array($v,$masterIds,true))$v=array_search($v,$masterIds,true)+1;elseif($k==='staff_id'&&is_string($v)&&$v===(string)$sid)$v='1';elseif($k==='staff_id'&&is_array($v)&&$v===[(string)$sid])$v=['1'];elseif(in_array($k,['create_time','update_time'],true)&&is_string($v)&&preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/D',$v))$v='VERIFIED_OWN_ORIGINAL_TIME';else $v=$normal($v,$context);}unset($v);return $x;};
foreach($cases as &$c){$trace=$c['body']['trace_id']??null;$c['trace_format_valid']=is_string($trace)&&preg_match('/^[0-9a-f]{13}$/D',$trace)===1;$check($c['trace_format_valid'],'Actual trace');$c['body']['trace_id']='VALID_DYNAMIC_TRACE';}unset($c);$report['cases']=$normal($cases);$report['findings']=[];$report['baseline_oracle_reverification']=$corrections;$report['oracle_object_member_order_only_corrected']=true;$report['read_only_green']=['new_http_requests'=>2,'new_DML'=>0,'no_successful_writes_replayed'=>true,'raw_wire_independent_original_projection_equal'=>true];$report['old_loaded_boundary']=$report['loaded_boundary'];$report['loaded_boundary']=$boundary;$report['resources']=['token_absent'=>$tokenGone,'cache_files'=>0];$events=[];foreach(file($runtime.'/queries.jsonl',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES)?:[] as $l)$events[]=json_decode($l,true);$report['events']=$events;$report['mysql_before_after_unchanged']=$mysqlUnchanged;
file_put_contents($runtime.'/private-green-responses.json',json_encode($rawNew,JSON_PRESERVE_ZERO_FRACTION|JSON_UNESCAPED_UNICODE));chmod($runtime.'/private-green-responses.json',0600);file_put_contents($runtime.'/green-evidence.json',json_encode($report,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));echo json_encode(['lane'=>$lane,'green_read_only_requests'=>2,'new_DML'=>0,'baseline_oracle_reverification'=>$corrections,'all_green_full_wire_original_equal'=>true]),"\n";
