@@ -5,22 +5,12 @@ import * as Path from "effect/Path";
55import * as ProcessRunner from "../processRunner.ts" ;
66import { AGENT_DEVICE_VERSION , DEVICE_HUB_VERSION } from "./DeviceToolchain.ts" ;
77
8- /** Shared with the SSH bootstrap. Leases live outside installs, which npm replaces atomically . */
8+ /** Shared with the SSH bootstrap. Cleanup runs only after successful startup . */
99export const deviceToolMaintenanceScript = String . raw `
1010const maintenanceFs = require('node:fs');
1111const maintenancePath = require('node:path');
12- const maintenanceIdentity = pid => {
13- const result = process.platform === 'win32'
14- ? require('node:child_process').spawnSync('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', '(Get-Process -Id ' + pid + ').StartTime.ToUniversalTime().Ticks'], { encoding: 'utf8', timeout: 10000 })
15- : require('node:child_process').spawnSync('ps', ['-p', String(pid), '-o', 'lstart='], { encoding: 'utf8', timeout: 10000, env: { ...process.env, TZ: 'UTC', LC_ALL: 'C' } });
16- return result.status === 0 ? result.stdout.trim() || null : null;
17- };
18- const maintenanceAlive = (pid, identity) => {
19- try {
20- process.kill(pid, 0);
21- const current = identity ? maintenanceIdentity(pid) : null;
22- return !identity || !current || identity === current;
23- }
12+ const maintenanceAlive = pid => {
13+ try { process.kill(pid, 0); return true; }
2414 catch (error) { return error.code !== 'ESRCH'; }
2515};
2616async function withToolMaintenance(root, operation) {
@@ -29,7 +19,7 @@ async function withToolMaintenance(root, operation) {
2919 const nonce = require('node:crypto').randomUUID();
3020 const ownerFile = process.pid + '.' + nonce + '.json';
3121 const candidate = lock + '.' + nonce;
32- const holder = { pid: process.pid, identity: maintenanceIdentity(process.pid) };
22+ const holder = { pid: process.pid };
3323 const deadline = Date.now() + 30000;
3424 const removeEmptyLock = () => {
3525 try { maintenanceFs.rmdirSync(lock); }
@@ -52,7 +42,7 @@ async function withToolMaintenance(root, operation) {
5242 const previousFile = maintenancePath.join(lock, files[0]);
5343 let previous;
5444 try { previous = JSON.parse(maintenanceFs.readFileSync(previousFile, 'utf8')); } catch {}
55- if (Number.isSafeInteger(previous?.pid) && previous.pid > 0 && !maintenanceAlive(previous.pid, previous.identity )) {
45+ if (Number.isSafeInteger(previous?.pid) && previous.pid > 0 && !maintenanceAlive(previous.pid)) {
5646 // The unique filename belongs only to that owner. Never unlink a replacement owner's file.
5747 try { maintenanceFs.unlinkSync(previousFile); } catch (error) { if (error.code !== 'ENOENT') throw error; }
5848 }
@@ -72,17 +62,9 @@ async function withToolMaintenance(root, operation) {
7262 maintenanceFs.rmSync(candidate, { recursive: true, force: true });
7363 }
7464}
75- function claimTool(root, name, version, pid) {
76- if (!Number.isSafeInteger(pid) || pid <= 0) throw Error("Missing device tool process identity.");
77- return withToolMaintenance(root, () => {
78- const users = maintenancePath.join(root, '.users', name + '@' + version);
79- maintenanceFs.mkdirSync(users, { recursive: true });
80- maintenanceFs.writeFileSync(maintenancePath.join(users, String(pid)), maintenanceIdentity(pid) || '');
81- });
82- }
8365function pruneTools(root, specs, flat) {
8466 return withToolMaintenance(root, () => {
85- // Also protect helpers launched by older T3 releases that predate usage records .
67+ // Keep installs used by any running helper, including older T3 releases .
8668 const scan = process.platform === 'win32'
8769 ? require('node:child_process').spawnSync('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', 'Get-CimInstance Win32_Process | Select-Object -ExpandProperty CommandLine'], { encoding: 'utf8', timeout: 10000 })
8870 : require('node:child_process').spawnSync('ps', ['-ax', '-o', 'command='], { encoding: 'utf8', timeout: 10000 });
@@ -106,20 +88,8 @@ function pruneTools(root, specs, flat) {
10688 if (!completed.some(value => value.version === required)) continue;
10789 const previous = completed.filter(value => value.version !== required).sort((a, b) => b.modified - a.modified || b.version.localeCompare(a.version, 'en', { numeric: true }))[0]?.version;
10890 for (const { version, directory } of completed) {
109- const users = maintenancePath.join(root, '.users', name + '@' + version);
110- // Older releases did not register users. Never guess whether one of those installs is still in use.
111- if (!maintenanceFs.existsSync(users)) continue;
112- const leases = maintenanceFs.readdirSync(users);
113- let inUse = false;
114- for (const lease of leases) {
115- const file = maintenancePath.join(users, lease);
116- const identity = maintenanceFs.readFileSync(file, 'utf8');
117- if (!/^[1-9][0-9]*$/.test(lease) || maintenanceAlive(Number(lease), identity)) inUse = true;
118- else maintenanceFs.rmSync(file, { force: true });
119- }
120- if (version === required || version === previous || scan.stdout.includes(directory + maintenancePath.sep) || inUse) continue;
91+ if (version === required || version === previous || scan.stdout.includes(directory + maintenancePath.sep)) continue;
12192 maintenanceFs.rmSync(directory, { recursive: true, force: true });
122- maintenanceFs.rmSync(users, { recursive: true, force: true });
12393 }
12494 }
12595 });
@@ -129,7 +99,7 @@ function pruneTools(root, specs, flat) {
12999class DeviceToolMaintenanceError extends Schema . TaggedError < DeviceToolMaintenanceError > ( ) (
130100 "DeviceToolMaintenanceError" ,
131101 {
132- operation : Schema . Literals ( [ "claim" , " prune"] ) ,
102+ operation : Schema . Literal ( " prune") ,
133103 tool : Schema . Literals ( [ "hub" , "agent" ] ) ,
134104 exitCode : Schema . NullOr ( Schema . Int ) ,
135105 cause : Schema . Defect ( ) ,
@@ -143,7 +113,7 @@ class DeviceToolMaintenanceError extends Schema.TaggedError<DeviceToolMaintenanc
143113const runMaintenance = Effect . fn ( "DeviceToolchain.maintenance" ) ( function * (
144114 nodePath : string ,
145115 script : string ,
146- operation : "claim" | " prune",
116+ operation : "prune" ,
147117 tool : "hub" | "agent" ,
148118) {
149119 const runner = yield * ProcessRunner . ProcessRunner ;
@@ -163,24 +133,6 @@ const runMaintenance = Effect.fn("DeviceToolchain.maintenance")(function* (
163133 ) ;
164134} ) ;
165135
166- export const claimLocalDeviceTool = Effect . fn ( "DeviceToolchain.claim" ) ( function * (
167- baseDir : string ,
168- nodePath : string ,
169- tool : "hub" | "agent" ,
170- ) {
171- const path = yield * Path . Path ;
172- const [ name , version ] =
173- tool === "hub"
174- ? [ "expo-device-hub" , DEVICE_HUB_VERSION ]
175- : [ "agent-device" , AGENT_DEVICE_VERSION ] ;
176- yield * runMaintenance (
177- nodePath ,
178- `claimTool(${ JSON . stringify ( path . join ( baseDir , "tools" ) ) } , ${ JSON . stringify ( name ) } , ${ JSON . stringify ( version ) } , ${ process . pid } )` ,
179- "claim" ,
180- tool ,
181- ) ;
182- } ) ;
183-
184136export const pruneLocalDeviceTools = Effect . fn ( "DeviceToolchain.prune" ) ( function * (
185137 baseDir : string ,
186138 nodePath : string ,
0 commit comments