<?php
require __DIR__.'/common.php';require __DIR__.'/observe.php';$own=json_decode(file_get_contents(__DIR__.'/private/worker-pid.json'),true);$pid=$own['pid'];$cmd=(string)@file_get_contents('/proc/'.$pid.'/cmdline');if($cmd!==''&&(strpos($cmd,__DIR__.'/index.php')===false||strpos($cmd,'127.0.0.1:'.$own['port'])===false))throw new RuntimeException('Own worker identity');
$files=[];$walk=static function($dir)use(&$walk,&$files){if(!is_dir($dir))return;foreach(scandir($dir)as $n){if($n==='.'||$n==='..')continue;$f=$dir.'/'.$n;if(is_link($f))throw new RuntimeException('Own cache symlink');if(is_dir($f)){$walk($f);if(!rmdir($f))throw new RuntimeException('Own cache directory');}else{$files[]=['path'=>substr($f,strlen(__DIR__)+1),'sha256'=>hash_file('sha256',$f)];if(!unlink($f))throw new RuntimeException('Own cache file');}}};$walk(__DIR__.'/cache');$removed=[];foreach(['auth.json','current-case.json','current-lease.json']as $n){$f=__DIR__.'/private/'.$n;if(is_file($f)){if(is_link($f))throw new RuntimeException('Auth symlink');$removed[]=['file'=>$n,'sha256'=>hash_file('sha256',$f)];if(!unlink($f))throw new RuntimeException('Own auth unlink');}}
$stopped=false;if($cmd!==''){if(!function_exists('posix_kill')||!posix_kill($pid,SIGTERM))throw new RuntimeException('Own worker stop');$stopped=true;}$after=$source();$final=json_decode(file_get_contents(__DIR__.'/private/baseline-final.json'),true);if($after!==$final['source'])throw new RuntimeException('Cleanup lease drift');$save('cleanup.json',['lane'=>$lane,'own_worker'=>$own,'stop_sent'=>$stopped,'cache_removed'=>$files,'temporary_credentials_removed'=>$removed,'source_after'=>$after,'marked_rows_retained'=>true,'stateless_token_globally_revoked'=>false,'included_files'=>$included(),'HTTP'=>0,'DML'=>0,'DDL'=>0]);echo json_encode(['lane'=>$lane,'own_worker_stopped'=>$stopped,'cache_removed'=>count($files),'credentials_removed'=>count($removed),'business_DML'=>0]),"\n";

