<?php
declare(strict_types=1);
$lane=getenv('NR_LANE');$port=$lane==='B'?18893:18894;$r=json_decode(file_get_contents(__DIR__.'/private/request.json'),true);$f=__DIR__.'/private/http-attempts-clientheaders.json';if(is_file($f))throw new RuntimeException('No HTTP replay');$a=[];
foreach($r['cases']as $c){$a[]=['label'=>$c['label'],'path'=>$c['path'],'headers_sent'=>$c['headers'],'sent'=>true,'complete'=>false];$i=count($a)-1;$save=static function()use(&$a,$f){file_put_contents($f,json_encode($a,JSON_PRETTY_PRINT));chmod($f,0600);};$save();$ctx=stream_context_create(['http'=>['method'=>'POST','header'=>"Content-Type: application/x-www-form-urlencoded\r\nToken: ".$r['token']."\r\nAccept: application/json, text/plain, */*\r\nCache-Control: max-age=28880\r\nX-CDX-Case: ".$c['label']."\r\n",'content'=>http_build_query($c['params']),'ignore_errors'=>true,'follow_location'=>0,'timeout'=>30]]);$body=file_get_contents('http://127.0.0.1:'.$port.$c['path'],false,$ctx);$a[$i]['complete']=$body!==false;$a[$i]['headers']=$http_response_header??[];$a[$i]['body_base64']=$body===false?null:base64_encode($body);$save();if($body===false||!is_file(__DIR__.'/private/wire-'.$c['label'].'.json'))throw new RuntimeException('HTTP incomplete; no blind retry');$w=json_decode(file_get_contents(__DIR__.'/private/wire-'.$c['label'].'.json'),true);if(!$w['source_unchanged']||!$w['old_logs_unchanged']||!$w['exact_log_count'])throw new RuntimeException('Scope/log failure; preserve raw');}
echo json_encode(['lane'=>$lane,'HTTP'=>count($a),'complete'=>count(array_filter($a,static function($x){return $x['complete'];}))]),"\n";
