<?php
declare(strict_types=1);require __DIR__.'/bootstrap.php';$p=json_decode(file_get_contents($runtime.'/private-report.json'),true);$j=json_decode(file_get_contents($runtime.'/private-journal.json'),true);$check($snapshot()===$p['before'],'Independent final protected original snapshot');$leaves=[];foreach(file($runtime.'/external-leaves.jsonl',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES) as $l){$v=json_decode($l,true);if($v['scenario']==='PROBE')continue;$check($v['network_sent']===false,'No external network');$leaves[]=$v;}
$events=[];foreach(file($runtime.'/queries.jsonl',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES)?:[] as $l)$events[]=json_decode($l,true);$audit=[];$original=$own();$actionIds=array_map('intval',array_column($original['action'],'id'));$addressIds=array_map('intval',array_column($original['address'],'id'));$fileIds=array_map('intval',array_column($original['file'],'file_id'));
foreach(file($runtime.'/private-sql.jsonl',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES)?:[] as $l){$v=json_decode($l,true);$sql=$v['sql'];if(!preg_match('/^\s*(INSERT|UPDATE|DELETE|MERGE|REPLACE|CREATE|DROP|ALTER)\b/i',$sql,$m))continue;$op=strtoupper($m[1]);$table=null;if(preg_match('/(?:INTO|UPDATE)\s+["`]?yoshop_(sign_action|sign_address|upload_file)["`]?\b/i',$sql,$t))$table=$t[1];$check($table!==null&&in_array($op,['INSERT','UPDATE'],true),'Only approved actual SQL write table');$ids=$table==='sign_action'?$actionIds:($table==='sign_address'?$addressIds:$fileIds);$scope=false;if($op==='INSERT'){$scope=$table==='sign_address'?preg_match('/\b(?:'.implode('|',$actionIds).')\b/',$sql)===1:preg_match('/\b214700157[12]\b/',$sql)===1;}else{$where=preg_split('/\bWHERE\b/i',$sql)[1]??'';foreach($ids as $id)if(preg_match('/["`]?id["`]?\s*=\s*[\x27]?'.$id.'\b/i',$where))$scope=true;}$check($scope,'Own primary key / tenant actual SQL');$audit[]=['scenario'=>$v['scenario'],'operation'=>$op,'table'=>$table,'own_scope'=>true,'sql_sha256'=>hash('sha256',$sql)];}
$counts=['requests'=>0,'assertions'=>0,'failures'=>0];foreach($p['cli_stats'] as $s){$counts['requests']+=$s['stats']['requests']['total'];$counts['assertions']+=$s['stats']['assertions']['total'];$counts['failures']+=$s['failures'];}
foreach($p['cases'] as &$c){$check(preg_match('/^[0-9a-f]{13}$/D',$c['body']['trace_id']??'')===1,'Raw trace verified');$c['body']['trace_id']='VALID_DYNAMIC_TRACE';}unset($c);$p['native_totals']=$counts;$p['write_audit']=$audit;$p['events']=$events;$p['external_leaves']=$leaves;$p['independent_final_original']=$original;$p['final_protected_unchanged']=true;$p['metadata_sha256']=hash_file('sha256',__DIR__.'/structure-'.$lane.'.json');file_put_contents(__DIR__.'/evidence-'.$lane.'.json',json_encode($p,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_PRESERVE_ZERO_FRACTION));
$items=[];foreach($j['collections'] as $col){$i=$col['item'][0]['item'][0];$i['request']['url']=str_replace($base,'{{base_url}}',$i['request']['url']);foreach($i['request']['header'] as &$h)if($h['key']==='Access-Token')$h['value']='{{private_token}}';unset($h);$items[]=$i;}file_put_contents(__DIR__.'/sign-executed-'.$lane.'.apifox-cli.json',json_encode(['apifoxCli'=>'2.2.8','info'=>['name'=>'Sign配置已执行档案：不能重放写入到现存租户'],'item'=>[['name'=>'Sign原生场景','item'=>$items]]],JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES));echo json_encode(['lane'=>$lane,'native_totals'=>$counts,'own_DML_audit'=>count($audit),'leaf_counts'=>array_count_values(array_column($leaves,'kind')),'protected_original_unchanged'=>true]),"\n";
