@tomflow/chatloop-tools 0.9.24 → 0.9.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/release.mjs +74 -7
- package/package.json +1 -1
package/lib/release.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Owner-verified stop-and-start upgrade
|
|
1
|
+
// Owner-verified stop-and-start upgrade with readiness-gated activation and safe rollback.
|
|
2
2
|
import {spawn} from 'node:child_process';
|
|
3
3
|
import {randomUUID,createHash} from 'node:crypto';
|
|
4
4
|
import {existsSync,readFileSync,readdirSync,writeFileSync,openSync,closeSync,unlinkSync,mkdirSync,renameSync,lstatSync,rmSync} from 'node:fs';
|
|
@@ -313,13 +313,15 @@ export async function upgradeRuntime(options={}){
|
|
|
313
313
|
try{fd=openSync(p.lock,'wx',0o600);}
|
|
314
314
|
catch{return freshResult('BLOCKED',{code:'UPGRADE_IN_PROGRESS'});}
|
|
315
315
|
const runner=options.run||run;
|
|
316
|
-
let oldEntry, oldStatus, phase='preflight'
|
|
316
|
+
let oldEntry, oldStatus, runtime, phase='preflight', oldWasStopped=false,
|
|
317
|
+
candidateStartAttempted=false, originalProfile;
|
|
317
318
|
try{
|
|
318
319
|
if(!existsSync(p.profile))return freshResult('BLOCKED',{code:'PROFILE_NOT_FOUND'});
|
|
319
320
|
const profile=readJson(p.profile);
|
|
321
|
+
originalProfile=profile;
|
|
320
322
|
if(!profile?.mcp?.commands?.some(x=>x.channel==='main'))
|
|
321
323
|
return freshResult('BLOCKED',{code:'PROFILE_MCP_NOT_FOUND'});
|
|
322
|
-
|
|
324
|
+
runtime=existsSync(p.current)?readJson(p.current):null;
|
|
323
325
|
oldEntry=runtime?.entry||p.legacy;
|
|
324
326
|
if(!existsSync(oldEntry))return freshResult('BLOCKED',{code:'PREVIOUS_RUNTIME_NOT_FOUND'});
|
|
325
327
|
const currentVersion=readJson(join(dirname(dirname(oldEntry)),'package.json')).version;
|
|
@@ -368,21 +370,86 @@ export async function upgradeRuntime(options={}){
|
|
|
368
370
|
if(oldStopped.status!=='OK')return freshResult('BLOCKED',{code:'STOP_UNCONFIRMED'});
|
|
369
371
|
phase='verify-stopped';
|
|
370
372
|
await awaitStopped(oldEntry,{lockPath:join(p.base,'local-tools','manager.lock')});
|
|
373
|
+
oldWasStopped=true;
|
|
371
374
|
phase='configure-new';
|
|
372
375
|
const main=profile.mcp.commands.find(x=>x.channel==='main');
|
|
373
376
|
main.command=quote(process.execPath)+' '+quote(p.entry)+' mcp';
|
|
374
377
|
atomicWrite(p.profile,JSON.stringify(profile,null,2)+'\n');
|
|
375
378
|
phase='start-new';
|
|
379
|
+
candidateStartAttempted=true;
|
|
376
380
|
const started=await cli(p.entry,'start',{env:{...runtimeLaunchEnv(),CHAT_LOOP_SKIP_UPGRADE:'1'}});
|
|
377
381
|
if(!['OK','STARTING'].includes(started.status))
|
|
378
382
|
throw Object.assign(Error('NEW_RUNTIME_UNHEALTHY'),{code:'NEW_RUNTIME_UNHEALTHY'});
|
|
383
|
+
// A successful start acknowledgement is not readiness. Wait for the
|
|
384
|
+
// candidate Supervisor and every enabled child to become observable.
|
|
385
|
+
phase='verify-new';
|
|
386
|
+
let verified=null;
|
|
387
|
+
for(let attempt=0;attempt<30;attempt++){
|
|
388
|
+
const status=await cli(p.entry,'status');
|
|
389
|
+
if(status.status==='OK'&&status.results?.filter(x=>x.enabled)
|
|
390
|
+
.every(x=>x.after==='RUNNING')){
|
|
391
|
+
verified=status;break;
|
|
392
|
+
}
|
|
393
|
+
await new Promise(resolve=>setTimeout(resolve,500));
|
|
394
|
+
}
|
|
395
|
+
if(!verified)
|
|
396
|
+
throw Object.assign(Error('NEW_RUNTIME_HEALTHCHECK_TIMEOUT'),{
|
|
397
|
+
code:'NEW_RUNTIME_HEALTHCHECK_TIMEOUT'
|
|
398
|
+
});
|
|
399
|
+
phase='verify-tunnel';
|
|
400
|
+
const tunnel=await cli(p.entry,['tunnel','doctor']);
|
|
401
|
+
if(tunnel.status!=='BUNDLED_READY'||tunnel.verified!==true)
|
|
402
|
+
throw Object.assign(Error('NEW_RUNTIME_TUNNEL_UNHEALTHY'),{
|
|
403
|
+
code:'NEW_RUNTIME_TUNNEL_UNHEALTHY'
|
|
404
|
+
});
|
|
379
405
|
phase='record-new';
|
|
380
406
|
atomicWrite(p.current,JSON.stringify({version:p.version,entry:p.entry,
|
|
381
|
-
status:
|
|
382
|
-
return freshResult(
|
|
407
|
+
status:'ACTIVE'},null,2)+'\n');
|
|
408
|
+
return freshResult('OK',{outcome:'ACTIVATED',managerPid:verified.managerPid,
|
|
409
|
+
healthyTools:verified.results.filter(x=>x.enabled).map(x=>x.toolId)});
|
|
383
410
|
}catch(e){
|
|
384
|
-
//
|
|
385
|
-
//
|
|
411
|
+
// If the old owner was positively stopped and the candidate failed its
|
|
412
|
+
// readiness gate, restore the exact previous MCP command and runtime
|
|
413
|
+
// pointer, then relaunch only the previously verified owner. Never guess
|
|
414
|
+
// when either process state is UNKNOWN.
|
|
415
|
+
if(oldWasStopped&&oldStatus?.status==='OK'&&oldEntry&&existsSync(oldEntry)){
|
|
416
|
+
try{
|
|
417
|
+
// Never start the old owner beside a possibly-live candidate. First
|
|
418
|
+
// positively stop and observe the candidate, otherwise preserve the
|
|
419
|
+
// blocked state for operator recovery instead of creating two owners.
|
|
420
|
+
if(candidateStartAttempted){
|
|
421
|
+
const candidateStatus=await cli(p.entry,'status');
|
|
422
|
+
if(candidateStatus.status==='OK'){
|
|
423
|
+
const stopped=await cli(p.entry,'stop');
|
|
424
|
+
if(stopped.status!=='OK')
|
|
425
|
+
throw Object.assign(Error('CANDIDATE_STOP_UNCONFIRMED'),{
|
|
426
|
+
code:'CANDIDATE_STOP_UNCONFIRMED'
|
|
427
|
+
});
|
|
428
|
+
await awaitStopped(p.entry,{
|
|
429
|
+
lockPath:join(p.base,'local-tools','manager.lock')
|
|
430
|
+
});
|
|
431
|
+
}else if(candidateStatus.status!=='STOPPED'){
|
|
432
|
+
throw Object.assign(Error('CANDIDATE_STATE_UNKNOWN'),{
|
|
433
|
+
code:'CANDIDATE_STATE_UNKNOWN'
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
if(originalProfile)atomicWrite(p.profile,JSON.stringify(originalProfile,null,2)+'\n');
|
|
438
|
+
if(runtime)atomicWrite(p.current,JSON.stringify(runtime,null,2)+'\n');
|
|
439
|
+
const recovered=await cli(oldEntry,'start',{
|
|
440
|
+
env:{...runtimeLaunchEnv(),CHAT_LOOP_SKIP_UPGRADE:'1'}
|
|
441
|
+
});
|
|
442
|
+
if(!['OK','STARTING'].includes(recovered.status))
|
|
443
|
+
throw Object.assign(Error('PREVIOUS_RUNTIME_RECOVERY_FAILED'),{
|
|
444
|
+
code:'PREVIOUS_RUNTIME_RECOVERY_FAILED'
|
|
445
|
+
});
|
|
446
|
+
return freshResult('BLOCKED',{code:e.code||'UPGRADE_FAILED',phase,
|
|
447
|
+
recovery:'PREVIOUS_RUNTIME_RESTARTED',previousVersion:runtime?.version});
|
|
448
|
+
}catch(recoveryError){
|
|
449
|
+
return freshResult('BLOCKED',{code:e.code||'UPGRADE_FAILED',phase,
|
|
450
|
+
recovery:'FAILED',recoveryCode:recoveryError.code||'UNKNOWN_RESULT'});
|
|
451
|
+
}
|
|
452
|
+
}
|
|
386
453
|
return freshResult('BLOCKED',{code:e.code||'UPGRADE_FAILED',phase});
|
|
387
454
|
}finally{
|
|
388
455
|
if(fd!==undefined)closeSync(fd);
|