<?php
declare(strict_types=1);require __DIR__.'/bootstrap.php';$port=$lane==='B'?18461:18462;$sock=@fsockopen('127.0.0.1',$port,$e,$m,1);$check(!$sock,'Own worker port remains open');$app=commentApp($root,$runtime);$cache=new \think\cache\driver\File($app,['path'=>$runtime.'/cache/','prefix'=>'','expire'=>0]);$tokens=[];foreach(glob($runtime.'/private-collection*.json') as $f){$v=json_decode(file_get_contents($f),true);foreach($v['item'][0]['item'] as $item)foreach($item['request']['header'] as $h)if($h['key']==='Access-Token')$tokens[$h['value']]=true;}foreach(array_keys($tokens) as $token)$check(!$cache->has($token),'Token survives');$cache->clear();
$fixture=json_decode(file_get_contents($runtime.'/fixture-journal.json'),true);$after=$own();$check($protect()===$fixture['before_protect'],'Natural small source changed');foreach(['user','goods','order','order_goods','upload_file'] as $t)$check($after[$t]===$fixture['after'][$t],'Base original changed '.$t);$hashes=[];foreach(['app/store/model/order/Delivery.php','app/terminal/model/order/Delivery.php','app/store/model/OrderRefund.php','app/terminal/model/OrderRefund.php','app/store/controller/order/Delivery.php','app/terminal/controller/order/Delivery.php','app/store/controller/order/Refund.php','app/terminal/controller/order/Refund.php','app/common/model/Order.php','app/common/database/DmBuilder.php','cores/BaseModel.php','app/common/service/OrderReadResponse.php'] as $f)$hashes[$f]=hash_file('sha256',$root.'/'.$f);
$public=__DIR__.'/export-public';mkdir($public,0700);$redact=function($value)use(&$redact,$tokens){if(is_array($value)){foreach($value as &$v)$v=$redact($v);return $value;}if(is_string($value))foreach(array_keys($tokens) as $token)$value=str_replace($token,'{{PRIVATE_ACCESS_TOKEN}}',$value);return $value;};$native=[];foreach(['native-NA'=>'INITIAL','native-GREEN'=>'SQL_GREEN','native-FINAL'=>'FINAL'] as $dir=>$phase)foreach(glob($runtime.'/'.$dir.'/*.json') as $f){$v=json_decode(file_get_contents($f),true);if(!isset($v['run']))continue;$target='native-'.$phase.'-'.$lane.'.apifox-cli.json';file_put_contents($public.'/'.$target,json_encode($redact($v),JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES));$native[]=['phase'=>$phase,'stats'=>$v['run']['stats'],'failures'=>count($v['run']['failures']),'original_sha256'=>hash_file('sha256',$f),'redacted_sha256'=>hash_file('sha256',$public.'/'.$target)];}
$evidence=$runtime.'/evidence';mkdir($evidence,0700);$kept=[];foreach(['private-wire.jsonl','private-sql.jsonl','fixture-journal.json','fixture-values.json','cases-NA.json','cases-GREEN.json','cases-FINAL.json','run-NA.json','run-GREEN.json','run-FINAL.json','queries.jsonl'] as $name){if(!is_file($runtime.'/'.$name))continue;rename($runtime.'/'.$name,$evidence.'/'.$name);chmod($evidence.'/'.$name,0600);$kept[$name]=hash_file('sha256',$evidence.'/'.$name);}
$wipe=function($path)use(&$wipe){if(is_link($path)||is_file($path)){unlink($path);return;}if(is_dir($path)){foreach(new DirectoryIterator($path) as $f)if(!$f->isDot())$wipe($f->getPathname());rmdir($path);}};
foreach(new DirectoryIterator($runtime) as $f)if(!$f->isDot()&&$f->getFilename()!=='evidence')$wipe($f->getPathname());$check(realpath($root)===realpath(__DIR__.'/source'),'Own source path');$wipe($root);
$out=['lane'=>$lane,'port'=>$port,'port_closed'=>true,'tokens_checked_absent'=>count($tokens),'cache_removed'=>!is_dir($runtime.'/cache'),'own_source_removed'=>!file_exists($root),'credential_collections_and_native_reports_removed'=>true,'natural_new_related_table_counts_unchanged'=>true,'base_five_tables_unchanged'=>true,'retained_rows'=>array_map('count',$after),'source_sha256'=>$hashes,'private_raw_directory'=>$evidence,'private_raw_sha256'=>$kept,'native'=>$native];file_put_contents($public.'/resource-cleanup-'.$lane.'.json',json_encode($out,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));echo json_encode(['lane'=>$lane,'port_closed'=>true,'tokens_absent'=>count($tokens),'source_removed'=>true,'retained_rows'=>$out['retained_rows']]),"\n";
