<?php
require_once __DIR__ . '/runtime/wr_bootstrap.php';
@set_time_limit(1200); @ini_set('memory_limit','1536M');
require_once __DIR__ . '/config/config.php';
if (!wr_db_guard_normalize()) { throw new RuntimeException('DB connection unavailable'); }
require_once __DIR__ . '/Chairman/memory/ChairmanPatternMemoryEngineV53.php';
require_once __DIR__ . '/Chairman/runtime/wr_chairman_os_scheduler_v60_0.php';
require_once __DIR__ . '/Chairman/audit/wr_chairman_pre_race_leakage_auditor_v61_0.php';
require_once __DIR__ . '/Chairman/audit/wr_chairman_production_readiness_gate_v62_0.php';
require_once __DIR__ . '/Chairman/audit/wr_chairman_shadow_run_validator_v63_0.php';
function v63h($v){return htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8');}
function v63date(){foreach(array('race_date','date','review_date') as $k){if(!isset($_GET[$k]))continue;$v=trim((string)$_GET[$k]);$d=DateTimeImmutable::createFromFormat('!Y-m-d',$v);$e=DateTimeImmutable::getLastErrors();if($d instanceof DateTimeImmutable&&($e===false||((int)$e['warning_count']===0&&(int)$e['error_count']===0))&&$d->format('Y-m-d')===$v)return $v;}return(new DateTimeImmutable('now',new DateTimeZone('Asia/Hong_Kong')))->format('Y-m-d');}
$date=v63date();$venue=isset($_GET['venue'])?preg_replace('/[^A-Za-z0-9_\-]/','',trim((string)$_GET['venue'])):'';$action=isset($_GET['action'])?trim((string)$_GET['action']):'latest';
$engine=new ChairmanPatternMemoryEngineV53($conn);$source=$action==='build'?$engine->build($date,$venue):$engine->latest($date,$venue);if(!is_array($source))$source=array();
$os=(new ChairmanOSSchedulerV60())->build($source,$date,$venue);$leak=(new ChairmanPreRaceLeakageAuditorV61())->audit($source,$date,$venue);$ready=(new ChairmanProductionReadinessGateV62())->certify($source,$os,$leak,$date,$venue);$data=(new ChairmanShadowRunValidatorV63())->validate($source,$ready,$date,$venue);$s=$data['shadow_summary'];
?><!doctype html><html lang="zh-Hant"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>WinRacing V63 Shadow Run</title><style>
:root{--bg:#050914;--p:#0b1424;--c:#08101d;--l:#304765;--g:#ffc107;--gr:#36f2a3;--cy:#5edbff;--r:#ff7589;--m:#9eb0c7}*{box-sizing:border-box}body{margin:0;background:var(--bg);color:#eef6ff;font:13px Arial,'Microsoft JhengHei',sans-serif}.w{max-width:1600px;margin:auto;padding:18px}.p{background:var(--p);border:1px solid #31486b;border-radius:14px;padding:15px;margin-bottom:12px}.grid{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:10px}.g3{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:10px}.c{background:var(--c);border:1px solid var(--l);border-radius:12px;padding:12px}.k{font-size:27px;font-weight:900}.gold{color:var(--g)}.green{color:var(--gr)}.cyan{color:var(--cy)}.red{color:var(--r)}.muted{color:var(--m)}.b{display:inline-block;padding:3px 8px;border-radius:999px;background:#162942;border:1px solid #36577e;font-weight:700}input{background:#07111f;color:#fff;border:1px solid #345;padding:8px}button{background:#ffc107;border:0;padding:9px 14px;font-weight:900;border-radius:6px}.critical{border-left:4px solid var(--r)}.warning{border-left:4px solid var(--g)}.fp{word-break:break-all;font-family:Consolas,monospace;font-size:11px}@media(max-width:900px){.grid,.g3{grid-template-columns:1fr}}
</style></head><body><div class="w"><div class="p"><h1>🧪 V63.0 — <span class="gold">Race-Day Shadow Run Validator</span></h1><div class="muted">在不改動任何正式決策下，重播 V62 認證結果，檢查 deterministic replay、決策覆蓋、分數範圍與只讀安全合約。</div></div>
<div class="p"><form>Race Date <input type="date" name="race_date" value="<?=v63h($date)?>"> Venue <input name="venue" value="<?=v63h($venue)?>"> <button name="action" value="build">Build + Shadow Run</button></form></div>
<div class="p"><div class="grid"><div class="c"><small>Shadow State</small><div class="k <?=$s['shadow_state']==='PASS'?'green':($s['shadow_state']==='FAIL'?'red':'gold')?>"><?=v63h($s['shadow_state'])?></div></div><div class="c"><small>Shadow Score</small><div class="k cyan"><?=number_format((float)$s['shadow_score'],1)?></div></div><div class="c"><small>Critical / Warning</small><div class="k"><span class="red"><?=v63h($s['critical_count'])?></span> / <span class="gold"><?=v63h($s['warning_count'])?></span></div></div><div class="c"><small>Shadow Gate</small><div class="k <?=$s['shadow_gate']==='BLOCKED'?'red':'green'?>" style="font-size:20px"><?=v63h($s['shadow_gate'])?></div></div></div></div>
<div class="p"><h2>Shadow Signals</h2><div class="grid"><div class="c"><small>V62 State / Score</small><div class="k cyan" style="font-size:20px"><?=v63h($s['v62_readiness_state'])?> / <?=v63h($s['v62_readiness_score'])?></div></div><div class="c"><small>Decision Count</small><div class="k"><?=v63h($s['decision_count'])?></div></div><div class="c"><small>Deterministic Replay</small><div class="k <?=$s['deterministic_replay']?'green':'red'?>"><?=$s['deterministic_replay']?'YES':'NO'?></div></div><div class="c"><small>Passed Checks</small><div class="k green"><?=v63h($s['passed_count'])?> / <?=v63h($s['check_count'])?></div></div></div></div>
<div class="p"><h2>Source Fingerprint</h2><div class="c fp cyan"><?=v63h($s['source_fingerprint'])?></div></div>
<div class="p"><h2>Validation Checks</h2><div class="g3"><?php foreach($data['checks'] as $x):?><div class="c"><h3 class="gold"><?=v63h($x['check'])?></h3><span class="b <?=$x['status']==='PASS'?'green':'red'?>"><?=v63h($x['status'])?></span> <span class="muted"><?=v63h($x['severity'])?></span></div><?php endforeach;?></div></div>
<div class="p"><h2>Findings</h2><?php if(!$data['findings']):?><div class="c green">All shadow-run validation checks passed.</div><?php else:?><div class="g3"><?php foreach($data['findings'] as $f):?><div class="c <?=strtolower(v63h($f['severity']))?>"><h3><?=v63h($f['severity'])?> · <?=v63h($f['type'])?></h3><p><?=v63h($f['message'])?></p></div><?php endforeach;?></div><?php endif;?></div>
<div class="p muted">Safety Contract: PRE-RACE ONLY ｜ V62 PASS REQUIRED ｜ DETERMINISTIC REPLAY ｜ NO DB WRITE ｜ NO SCHEMA CHANGE ｜ NO LIVE-WEIGHT CHANGE</div></div></body></html>
