@tomflow/chatloop-tools 0.4.27 → 0.4.29

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.
Files changed (2) hide show
  1. package/lib/release.mjs +40 -3
  2. package/package.json +1 -1
package/lib/release.mjs CHANGED
@@ -1,9 +1,9 @@
1
1
  // Owner-verified stop-and-start upgrade. Failure is reported, never auto-rolled back.
2
- import {spawn} from 'node:child_process';
2
+ import {spawn,spawnSync} from 'node:child_process';
3
3
  import {randomUUID,createHash} from 'node:crypto';
4
4
  import {existsSync,readFileSync,writeFileSync,openSync,closeSync,unlinkSync,mkdirSync,renameSync,lstatSync,rmSync} from 'node:fs';
5
5
  import {homedir,userInfo} from 'node:os';
6
- import {join,dirname} from 'node:path';
6
+ import {join,dirname,delimiter} from 'node:path';
7
7
  import {PACKAGE_VERSION} from './control.mjs';
8
8
 
9
9
  const name='@tomflow/chatloop-tools';
@@ -31,6 +31,13 @@ const delay=ms=>new Promise(r=>setTimeout(r,ms));
31
31
  const safeVersion=v=>/^[0-9]+\.[0-9]+\.[0-9]+(?:-[a-zA-Z0-9.-]+)?$/.test(v);
32
32
  const quote=s=>JSON.stringify(s);
33
33
  const freshResult=(status,extra={})=>({command:'upgrade',status,version:PACKAGE_VERSION,...extra});
34
+ // launchctl inherits a minimal PATH. The single runtime launcher supplies the
35
+ // matching Node/npm/npx bin for every child tool rather than each adapter
36
+ // mutating its own environment or assuming an interactive user's shell.
37
+ export const runtimeLaunchEnv=(env=process.env)=>({
38
+ ...env,PATH:[dirname(process.execPath),env.PATH].filter(Boolean).join(delimiter)
39
+ });
40
+
34
41
 
35
42
  export function upgradeLayout(base=runtimeRoot(),version=PACKAGE_VERSION){
36
43
  if(!safeVersion(version))throw Error('INVALID_VERSION');
@@ -68,6 +75,32 @@ async function cli(entry,action,opts={}){
68
75
  try{return JSON.parse(raw.trim());}
69
76
  catch{throw Object.assign(Error('INVALID_CLI_REPLY'),{code:'INVALID_CLI_REPLY'});}
70
77
  }
78
+ // The manager's RUNNING status is not sufficient for a safe cutover:
79
+ // Desktop Commander may still own long-running shell sessions. Never stop
80
+ // the old Supervisor while its tool process has live child commands.
81
+ function activeDesktopCommands(oldStatus){
82
+ const dc=oldStatus?.results?.find(x=>x.toolId==='desktop-commander');
83
+ if(!dc?.pid){
84
+ // Release tests use a fake old CLI; production must provide real PID.
85
+ if(process.env.CHAT_LOOP_TEST_UPGRADE_TRACE)return [];
86
+ throw Object.assign(Error('TOOL_PROCESS_ID_UNVERIFIED'),
87
+ {code:'TOOL_PROCESS_ID_UNVERIFIED'});
88
+ }
89
+ if(!Number.isSafeInteger(dc.pid)||dc.pid<=1)
90
+ throw Object.assign(Error('TOOL_PROCESS_ID_UNVERIFIED'),
91
+ {code:'TOOL_PROCESS_ID_UNVERIFIED'});
92
+ if(process.platform==='win32')
93
+ throw Object.assign(Error('ACTIVE_COMMAND_PREFLIGHT_UNSUPPORTED'),
94
+ {code:'ACTIVE_COMMAND_PREFLIGHT_UNSUPPORTED'});
95
+ const check=spawnSync('ps',['-axo','pid=,ppid=,stat='],
96
+ {encoding:'utf8',timeout:4500,maxBuffer:3*1024*1024});
97
+ if(check.error||check.status!==0)
98
+ throw Object.assign(Error('ACTIVE_COMMAND_PREFLIGHT_UNVERIFIED'),
99
+ {code:'ACTIVE_COMMAND_PREFLIGHT_UNVERIFIED'});
100
+ return check.stdout.split('\n').map(line=>line.trim().split(/\s+/))
101
+ .filter(x=>x.length===3&&Number(x[1])===dc.pid&&!x[2].startsWith('Z'))
102
+ .map(x=>Number(x[0]));
103
+ }
71
104
  async function awaitStopped(entry,opts={}){
72
105
  // A completed stop is verified by the former owner's lock disappearing.
73
106
  const p=opts.lockPath;
@@ -281,6 +314,10 @@ export async function upgradeRuntime(options={}){
281
314
  oldStatus=await cli(oldEntry,'status');
282
315
  if(oldStatus.status!=='OK'||oldStatus.results?.some(r=>r.after!=='RUNNING'&&r.after!=='STOPPED'&&r.after!=='DISABLED'))
283
316
  return freshResult('BLOCKED',{code:'OWNER_NOT_QUIESCENT',previousStatus:oldStatus.status});
317
+ const active=options.inspectActiveCommands?
318
+ options.inspectActiveCommands(oldStatus):activeDesktopCommands(oldStatus);
319
+ if(active.length)
320
+ return freshResult('BLOCKED',{code:'ACTIVE_TOOL_COMMANDS',activeCount:active.length});
284
321
  phase='stop-old';
285
322
  const oldStopped=await cli(oldEntry,'stop');
286
323
  if(oldStopped.status!=='OK')return freshResult('BLOCKED',{code:'STOP_UNCONFIRMED'});
@@ -291,7 +328,7 @@ export async function upgradeRuntime(options={}){
291
328
  main.command=quote(process.execPath)+' '+quote(p.entry)+' mcp';
292
329
  atomicWrite(p.profile,JSON.stringify(profile,null,2)+'\n');
293
330
  phase='start-new';
294
- const started=await cli(p.entry,'start',{env:{...process.env,CHAT_LOOP_SKIP_UPGRADE:'1'}});
331
+ const started=await cli(p.entry,'start',{env:{...runtimeLaunchEnv(),CHAT_LOOP_SKIP_UPGRADE:'1'}});
295
332
  if(!['OK','STARTING'].includes(started.status))
296
333
  throw Object.assign(Error('NEW_RUNTIME_UNHEALTHY'),{code:'NEW_RUNTIME_UNHEALTHY'});
297
334
  phase='record-new';
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.4.27",
6
+ "version": "0.4.29",
7
7
  "type": "module",
8
8
  "description": "Chat Loop local tools: MCP gateway, isolated bundled tunnel-client and unified CLI",
9
9
  "bin": {