<?php
$lane=$argv[1]??'';$mode=$argv[2]??'';
if(!in_array($lane,['B','C'],true)||!in_array($mode,['launch','run','preflight','audit','repair-dry','repair-apply','repair-check','crud','wire'],true))exit(78);
$pid=$lane==='B'?10001:112175;$root=$lane==='B'?'/tmp/zhct-http-store-mysql.dQTTBP':'/tmp/zhct-dm-poc-20260928.fcpiiN/store';
if(strpos((string)file_get_contents('/proc/'.$pid.'/cmdline'),$root.'/public')===false)throw new RuntimeException('Parent drift');
$env=[];foreach(explode("\0",(string)file_get_contents('/proc/'.$pid.'/environ')) as $s){$p=explode('=',$s,2);if(count($p)===2)$env[$p[0]]=$p[1];}
if(($env['POC_EXTERNAL_STUBS']??'')!=='1'||($env['POC_REAL_QUEUE']??'0')!=='0')throw new RuntimeException('Parent unsafe');
$env['MR3_LANE']=$lane;$env['MR3_ROOT']=$root;
$env['MR3_BASELINE']=$mode==='run-red'?'1':'0';
$env['MR3_CLIENT_ONLY']=$mode==='run-client'?'1':'0';
$args=[$lane==='B'?'/usr/local/php7.3/bin/php':'/www/server/php/73/bin/php'];
if($lane==='C')array_push($args,'-c','/www/server/php/73/etc/php-cli.ini','-d','extension=/home/dmdba/dmdbms/drivers/php_pdo/libphp73_dm.so','-d','extension=/home/dmdba/dmdbms/drivers/php_pdo/php73_pdo_dm.so');
array_push($args,'-d','display_errors=0','-d','log_errors=0');
if($mode==='launch'){$port=$lane==='B'?18301:18302;if($s=@fsockopen('127.0.0.1',$port,$n,$e,1)){fclose($s);throw new RuntimeException('Port occupied');}array_push($args,'-S','127.0.0.1:'.$port,'-t',__DIR__,__DIR__.'/index.php');$cmd='nohup '.implode(' ',array_map('escapeshellarg',$args)).' </dev/null >/dev/null 2>&1 & echo $!';}
else{$files=['run'=>'run.php','preflight'=>'preflight.php','audit'=>'audit.php','repair-dry'=>'repair-consume-scene.php','repair-apply'=>'repair-consume-scene.php','repair-check'=>'repair-independent-audit.php','crud'=>'crud.php','wire'=>'frozen-http-read.php'];array_push($args,__DIR__.'/'.$files[$mode]);if($mode==='repair-dry'||$mode==='repair-apply')$args[]=$mode==='repair-dry'?'dry-run':'apply';$cmd=implode(' ',array_map('escapeshellarg',$args));}
$p=proc_open($cmd,[1=>STDOUT,2=>STDERR],$pipes,null,$env);exit(proc_close($p));
