<?php
declare(strict_types=1);
$lane=$argv[1]??'';$mode=$argv[2]??'';$cardGreen=$mode==='start_card_green';$restaurantGreen=$mode==='start_restaurant_green';if($cardGreen||$restaurantGreen){$mode='start';}if(!in_array($lane,['B','C'],true)||!in_array($mode,['metadata','metadata_after_column','schema_plan','finish_runtime','prepare_restaurant_green','postcheck_restaurant_green','prepare_card_green','postcheck_card_green','restaurant_credential_precheck','restaurant_enable','prepare_restaurant','postcheck_restaurant','card_projection_probe','prepare_dining','postcheck_dining','init_actor','candidates','prepare','postcheck','start','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';
if(strpos((string)file_get_contents('/proc/'.$pid.'/cmdline'),$parent.'/public')===false){throw new RuntimeException('Reviewed parent identity drift');}
$env=[];foreach(explode("\0",(string)file_get_contents('/proc/'.$pid.'/environ')) as $value){$pair=explode('=',$value,2);if(count($pair)===2){$env[$pair[0]]=$pair[1];}}
if(($env['POC_EXTERNAL_STUBS']??'')!=='1'||($env['POC_REAL_QUEUE']??'0')!=='0'){throw new RuntimeException('Parent external boundary');}
foreach($env as $key=>$value){if(strpos($key,'POC_FAIL_')===0&&$value!==''&&$value!=='0'){throw new RuntimeException('Fault injection enabled');}}
$env['SM7_LANE']=$lane;$env['SM7_PARENT']=$parent;$env['SM7_ROOT']=__DIR__.'/source';$env['SM7_RUNTIME']=__DIR__.'/private-runtime';$env['SM7_PORT']=$lane==='B'?'18421':'18422';
$port=(int)$env['SM7_PORT'];$env['SM7_BASE']='http://127.0.0.1:'.$port;$env['POC_REAL_QUEUE']='0';$state=__DIR__.'/worker_state.json';
if($cardGreen){$env['N7_CARD_GREEN']='1';}
if($restaurantGreen){$env['N7_RESTAURANT_GREEN']='1';}
$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==='stop'){$owned=json_decode(file_get_contents($state),true);$cmd=(string)file_get_contents('/proc/'.$owned['pid'].'/cmdline');if(strpos($cmd,__DIR__.'/index.php')===false||strpos($cmd,'127.0.0.1:'.$port)===false){throw new RuntimeException('Owned worker stop guard');}posix_kill($owned['pid'],SIGTERM);unlink($state);echo '{"stopped":true}',PHP_EOL;exit;}
if($mode==='start'){if(is_file($state)){throw new RuntimeException('Own state already exists');}if($s=@fsockopen('127.0.0.1',$port,$n,$e,1)){fclose($s);throw new RuntimeException('Private 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);$pid=trim(stream_get_contents($pipes[1]));$error=stream_get_contents($pipes[2]);fclose($pipes[1]);fclose($pipes[2]);if(proc_close($p)!==0||!ctype_digit($pid)||$error!==''){throw new RuntimeException('Private launch failed');}$owned=['pid'=>(int)$pid,'port'=>$port,'lane'=>$lane];file_put_contents($state,json_encode($owned));chmod($state,0600);echo json_encode($owned),PHP_EOL;exit;}
array_push($args,__DIR__.'/'.$mode.'.php');
$process=proc_open(implode(' ',array_map('escapeshellarg',$args)),[0=>STDIN,1=>STDOUT,2=>STDERR],$pipes,null,$env);exit(proc_close($process));
