<?php
require __DIR__.'/common.php';$pid=$lane==='B'?6987:2282385;$port=$lane==='B'?18921:18922;$expected=__DIR__.'/index.php';$cmd=(string)@file_get_contents('/proc/'.$pid.'/cmdline');
if($cmd!==''&&(strpos($cmd,$expected)===false||strpos($cmd,'127.0.0.1:'.$port)===false))throw new RuntimeException('Worker ownership mismatch');
$files=[];$walk=static function($dir)use(&$walk,&$files){if(!is_dir($dir))return;foreach(scandir($dir)as $name){if($name==='.'||$name==='..')continue;$f=$dir.'/'.$name;if(is_link($f))throw new RuntimeException('Cleanup symlink');if(is_dir($f)){$walk($f);if(!rmdir($f))throw new RuntimeException('Own cache directory remove');}elseif(is_file($f)){$files[]=['path'=>substr($f,strlen(__DIR__)+1),'sha256'=>hash_file('sha256',$f)];if(!unlink($f))throw new RuntimeException('Own cache file remove');}else throw new RuntimeException('Unexpected cache object');}};
$walk(__DIR__.'/cache');$removed=[];foreach(['auth.json','current-case.json']as $name){$f=__DIR__.'/private/'.$name;if(is_file($f)){if(is_link($f))throw new RuntimeException('Credential symlink');$removed[]=['file'=>$name,'sha256'=>hash_file('sha256',$f)];if(!unlink($f))throw new RuntimeException('Own temporary credential remove');}}
$stopped=false;if($cmd!==''){if(!function_exists('posix_kill')||!posix_kill($pid,SIGTERM))throw new RuntimeException('Own worker stop failed');$stopped=true;}
$r=['lane'=>$lane,'own_worker_pid'=>$pid,'stop_sent'=>$stopped,'cache_removed'=>$files,'temporary_credential_removed'=>$removed,'stateless_token_globally_revoked'=>false,'original_parent_untouched'=>true,'source_after'=>$source(),'business_DML'=>0,'DDL'=>0,'HTTP'=>0];$save('cleanup.json',$r);echo json_encode(['lane'=>$lane,'worker_stop_sent'=>$stopped,'cache_removed'=>count($files),'temporary_credentials_removed'=>count($removed),'marked_rows_retained'=>true,'SQL_writes'=>0]),"\n";
