$p['preview'],'before'=>$host->table('fr_calculations')->count()]));} elseif($mode==='first'){$p=json_decode(file_get_contents($file),true);$selectRule(13);$host->beginTransaction();try{$out=$rules->confirm(13,$p['cut']);file_put_contents($dir.'/locked','Company lock held');$end=microtime(true)+25;while(!is_file($dir.'/release')&µtime(true)<$end)usleep(50000);if(!is_file($dir.'/release'))throw new LogicException('Release missing');$host->commit();file_put_contents($dir.'/first.json',json_encode($out));}finally{while($host->transactionLevel()>0)$host->rollBack();}} elseif($mode==='second'){$p=json_decode(file_get_contents($file),true);$selectRule(13);request()->request->replace(['preview'=>$p['cut']]);$start=microtime(true);$r=app(App\Modules\Services\Http\FiscalRulesController::class)->confirm(request(),$rules);file_put_contents($dir.'/second.json',json_encode(['status'=>$r->getStatusCode(),'retry_after'=>$r->headers->get('Retry-After'),'elapsed_seconds'=>microtime(true)-$start]));} elseif($mode==='recover'){$p=json_decode(file_get_contents($file),true);$selectRule(13);$out=$rules->confirm(13,$p['cut']);$first=json_decode(file_get_contents($dir.'/first.json'),true);$ok($out['replayed']&&$out['id']===$first['id']&&$host->table('fr_calculations')->count()===$p['before']+1,'same key recovers one calculation after lock release');file_put_contents($dir.'/result.json',json_encode(['passed'=>true,'during_lock'=>json_decode(file_get_contents($dir.'/second.json'),true),'after_release'=>200,'replayed'=>true,'one_calculation'=>true,'regression_only'=>true],JSON_PRETTY_PRINT));} else throw new LogicException('Unknown mode');echo json_encode(['passed'=>true,'phase'=>$mode,'proof'=>$dir.'/result.json']).PHP_EOL;