orphan=spawn(process.execPath,[path.join(service.ROOT,'server.js')],{cwd:service.ROOT,env,stdio:'ignore'});letorphanReady=false;for(letattempt=0;attempt<100;attempt++){awaitnewPromise(resolve=>setTimeout(resolve,50));try{constorphanPort=Number((awaitfsp.readFile(path.join(publicDir,'.linkfield-port'),'utf8')).trim()),health=awaitfetch(`http://127.0.0.1:${orphanPort}/api/cloud/status`);if(health.ok){orphanReady=true;break}}catch(_){}}assert(orphanReady,'Legacy-lock recovery fixture did not start');awaitfsp.writeFile(worldLockFile,String(orphan.pid));constblockedStart=spawnSync(process.execPath,[path.join(service.ROOT,'scripts','service-control.js'),'start'],{cwd:service.ROOT,env,encoding:'utf8',timeout:5000});assert.notEqual(blockedStart.status,0);assert.match(blockedStart.stderr,/npm run recover/i);assert.equal(service.isProcessRunning(orphan.pid),true,'Legacy lock preflight stopped an unverified process');
constrecovery=spawnSync(process.execPath,[path.join(service.ROOT,'scripts','service-control.js'),'recover'],{cwd:service.ROOT,env,encoding:'utf8',timeout:20_000});assert.equal(recovery.status,0,recovery.stderr||recovery.stdout);assert.match(recovery.stdout,/Recovered the LinkField world lock/i);assert.equal(service.isProcessRunning(orphan.pid),false,'Explicit recovery did not stop the legacy numeric lock holder');orphan=null;
orphan=spawn(process.execPath,[path.join(service.ROOT,'server.js')],{cwd:service.ROOT,env,stdio:'ignore'});orphanReady=false;for(letattempt=0;attempt<100;attempt++){awaitnewPromise(resolve=>setTimeout(resolve,50));try{constorphanPort=Number((awaitfsp.readFile(path.join(publicDir,'.linkfield-port'),'utf8')).trim()),health=awaitfetch(`http://127.0.0.1:${orphanPort}/api/cloud/status`);if(health.ok){orphanReady=true;break}}catch(_){}}assert(orphanReady,'Orphan-server replacement fixture did not start');assert((awaitservice.legacyLinkFieldPids({worldDir})).includes(orphan.pid),'World-lock discovery did not find the orphan LinkField server');
awaitfsp.appendFile(logFile,'x'.repeat(8192));letrotated=false;for(letattempt=0;attempt<50;attempt++){awaitnewPromise(resolve=>setTimeout(resolve,100));if(fs.existsSync(`${logFile}.1`)){rotated=true;break}}assert(rotated,'Supervisor did not rotate a live oversized log');
awaitfsp.writeFile(pidFile,JSON.stringify({...pidRecord,nonce:'0'.repeat(32)}));constrefusedStop=spawnSync(process.execPath,[path.join(service.ROOT,'scripts','service-control.js'),'stop'],{cwd:service.ROOT,env,encoding:'utf8',timeout:10_000});assert.notEqual(refusedStop.status,0);assert.match(refusedStop.stderr,/not a verified LinkField supervisor/i);assert.equal(service.isProcessRunning(pid),true);awaitfsp.writeFile(pidFile,JSON.stringify(pidRecord));