<?php
declare(strict_types=1);
$lane=$argv[1]??'';$mode=$argv[2]??'';
if(!in_array($lane,['B','C'],true)||!in_array($mode,['preflight','dining_preflight','dining','dining_green','boundary','refresh','green','hardware_native_prepare','hardware_native_postcheck','hardware_native_verify','cleanup','start','run','stop'],true)){exit(78);}
$pid=$lane==='B'?10001:112175;$parent=$lane==='B'?'/tmp/zhct-http-store-mysql.dQTTBP':'/tmp/zhct-dm-poc-20260928.fcpiiN/store';
$cmd=(string)file_get_contents('/proc/'.$pid.'/cmdline');if(strpos($cmd,$parent.'/public')===false){throw new RuntimeException('Reviewed parent identity drift');}
$env=[];foreach(explode("\0",(string)file_get_contents('/proc/'.$pid.'/environ')) as $v){$p=explode('=',$v,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 external boundary');}
foreach($env as $k=>$v){if(strpos($k,'POC_FAIL_')===0&&$v!==''&&$v!=='0'){throw new RuntimeException('Fault injection enabled');}}
$env['POC_REAL_QUEUE']='0';
$env['SM7_LANE']=$lane;$env['SM7_PARENT']=$parent;$env['SM7_ROOT']=__DIR__.'/source';$env['SM7_RUNTIME']=__DIR__.'/private-runtime';$port=$lane==='B'?18401:18402;$env['SM7_BASE']='http://127.0.0.1:'.$port;
$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');$state=__DIR__.'/worker_state.json';
if($mode==='stop'){
    $s=json_decode(file_get_contents($state),true);$actual=(string)file_get_contents('/proc/'.$s['pid'].'/cmdline');if(strpos($actual,__DIR__.'/index.php')===false||strpos($actual,'127.0.0.1:'.$port)===false){throw new RuntimeException('Owned worker stop guard');}posix_kill($s['pid'],SIGTERM);unlink($state);echo '{"stopped":true}',PHP_EOL;exit;
}
if($mode==='start'){
    if(is_file($state)){throw new RuntimeException('Prior state present');}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');$command='nohup '.implode(' ',array_map('escapeshellarg',$args)).' </dev/null >/dev/null 2>&1 & echo $!';$p=proc_open($command,[1=>['pipe','w'],2=>['pipe','w']],$pipes,null,$env);$out=trim(stream_get_contents($pipes[1]));$err=stream_get_contents($pipes[2]);fclose($pipes[1]);fclose($pipes[2]);if(proc_close($p)!==0||!ctype_digit($out)||$err!==''){throw new RuntimeException('Private launch failed');}$data=['pid'=>(int)$out,'port'=>$port,'lane'=>$lane];file_put_contents($state,json_encode($data));chmod($state,0600);echo json_encode($data),PHP_EOL;exit;
}
array_push($args,__DIR__.'/'.$mode.'.php');$p=proc_open(implode(' ',array_map('escapeshellarg',$args)),[0=>STDIN,1=>STDOUT,2=>STDERR],$pipes,null,$env);exit(proc_close($p));
