@cydm/pie 4.12.0 → 4.12.1
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/dist/builtin/extensions/im-forward/index.js +40 -15
- package/dist/builtin/extensions/pie-proc/index.js +2 -2
- package/dist/chunks/chunk-Q4NJEPIZ.js +210 -0
- package/dist/chunks/{chunk-322WLBDY.js → chunk-Q5PSWX3B.js} +3 -3
- package/dist/chunks/{chunk-6U6UGM4C.js → chunk-Y6AVNVOD.js} +31 -1
- package/dist/chunks/chunk-ZB5FVHI7.js +618 -0
- package/dist/chunks/{manager-cli-B6GIYTI7.js → manager-cli-OTIMAWO6.js} +246 -287
- package/dist/chunks/{manager-upgrade-WHER3WTJ.js → manager-upgrade-DYRQ7YZ5.js} +70 -213
- package/dist/cli.js +3 -3
- package/package.json +1 -1
- package/dist/chunks/chunk-BSITHT43.js +0 -127
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
acquireUpgradeLock,
|
|
4
|
+
parseLaunchctlServiceState,
|
|
5
|
+
parseSystemctlServiceState,
|
|
6
|
+
queryPlatformServiceState,
|
|
7
|
+
scanLinuxServiceBlockingWorkers,
|
|
8
|
+
stopAndRefreshWindowsServiceForUpgrade
|
|
9
|
+
} from "./chunk-ZB5FVHI7.js";
|
|
2
10
|
import {
|
|
3
11
|
normalizePreInstallStopOutcome,
|
|
4
12
|
parseWindowsTaskNumericState,
|
|
@@ -6,205 +14,40 @@ import {
|
|
|
6
14
|
resolveDefaultTuiUsePidFile,
|
|
7
15
|
runWindowsPreInstallStop,
|
|
8
16
|
scanBlockingWindowsPeers
|
|
9
|
-
} from "./chunk-
|
|
17
|
+
} from "./chunk-Y6AVNVOD.js";
|
|
10
18
|
import {
|
|
11
19
|
readProcessCommandLine
|
|
12
20
|
} from "./chunk-7RJXKN7N.js";
|
|
13
|
-
import
|
|
14
|
-
pidDefinitelyDead,
|
|
15
|
-
withOwnershipMutex
|
|
16
|
-
} from "./chunk-BSITHT43.js";
|
|
21
|
+
import "./chunk-Q4NJEPIZ.js";
|
|
17
22
|
import {
|
|
18
23
|
VERSION
|
|
19
24
|
} from "./chunk-CBDQJMUI.js";
|
|
20
25
|
import "./chunk-LZQXQOPJ.js";
|
|
21
26
|
|
|
22
27
|
// src/commands/manager-upgrade.ts
|
|
23
|
-
import { spawnSync as spawnSync2 } from "node:child_process";
|
|
24
|
-
import * as fs2 from "node:fs";
|
|
25
|
-
import * as os from "node:os";
|
|
26
|
-
import * as path2 from "node:path";
|
|
27
|
-
|
|
28
|
-
// src/commands/manager-upgrade-service.ts
|
|
29
28
|
import { spawnSync } from "node:child_process";
|
|
30
|
-
import { randomUUID } from "node:crypto";
|
|
31
29
|
import * as fs from "node:fs";
|
|
30
|
+
import * as os from "node:os";
|
|
32
31
|
import * as path from "node:path";
|
|
33
|
-
function acquireUpgradeLock(lockFile) {
|
|
34
|
-
const mutexDir = `${lockFile}.mutex`;
|
|
35
|
-
const token = randomUUID();
|
|
36
|
-
let acquired = false;
|
|
37
|
-
try {
|
|
38
|
-
acquired = withOwnershipMutex(mutexDir, () => {
|
|
39
|
-
fs.mkdirSync(path.dirname(lockFile), { recursive: true });
|
|
40
|
-
if (fs.existsSync(lockFile)) {
|
|
41
|
-
let holderPid;
|
|
42
|
-
try {
|
|
43
|
-
const holder = JSON.parse(fs.readFileSync(lockFile, "utf8"));
|
|
44
|
-
if (typeof holder.pid === "number" && Number.isInteger(holder.pid) && holder.pid > 0) {
|
|
45
|
-
holderPid = holder.pid;
|
|
46
|
-
}
|
|
47
|
-
} catch {
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
if (holderPid === void 0 || !pidDefinitelyDead(holderPid)) {
|
|
51
|
-
return false;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
const tmp = `${lockFile}.${process.pid}.${token}.tmp`;
|
|
55
|
-
try {
|
|
56
|
-
fs.writeFileSync(tmp, JSON.stringify({ pid: process.pid, token, at: Date.now() }), { flag: "wx", mode: 384 });
|
|
57
|
-
fs.renameSync(tmp, lockFile);
|
|
58
|
-
return true;
|
|
59
|
-
} finally {
|
|
60
|
-
try {
|
|
61
|
-
fs.rmSync(tmp, { force: true });
|
|
62
|
-
} catch {
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
} catch {
|
|
67
|
-
return null;
|
|
68
|
-
}
|
|
69
|
-
if (!acquired) return null;
|
|
70
|
-
let released = false;
|
|
71
|
-
return {
|
|
72
|
-
release: () => {
|
|
73
|
-
if (released) return;
|
|
74
|
-
released = true;
|
|
75
|
-
try {
|
|
76
|
-
withOwnershipMutex(mutexDir, () => {
|
|
77
|
-
try {
|
|
78
|
-
const holder = JSON.parse(fs.readFileSync(lockFile, "utf8"));
|
|
79
|
-
if (holder.pid !== process.pid || holder.token !== token) return;
|
|
80
|
-
} catch {
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
try {
|
|
84
|
-
fs.rmSync(lockFile, { force: true });
|
|
85
|
-
} catch {
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
} catch {
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
function parseLaunchctlServiceState(status, stdout, stderr) {
|
|
94
|
-
const combined = `${stdout}
|
|
95
|
-
${stderr}`;
|
|
96
|
-
if (status !== 0) {
|
|
97
|
-
return /could not find service|service not found/i.test(combined) ? "stopped" : "unknown";
|
|
98
|
-
}
|
|
99
|
-
const pidMatch = /^\s*pid\s*=\s*(\d+)\s*$/im.exec(combined);
|
|
100
|
-
if (pidMatch && Number.parseInt(pidMatch[1], 10) > 0) return "running";
|
|
101
|
-
const stateMatch = /^\s*state\s*=\s*(.+)$/im.exec(combined)?.[1]?.trim().toLowerCase();
|
|
102
|
-
if (!stateMatch) return "unknown";
|
|
103
|
-
if (stateMatch === "running") return "running";
|
|
104
|
-
if (stateMatch === "not running" || stateMatch === "waiting" || stateMatch === "spawn scheduled") return "stopped";
|
|
105
|
-
return "unknown";
|
|
106
|
-
}
|
|
107
|
-
function parseSystemctlServiceState(status, stdout, stderr) {
|
|
108
|
-
const combined = `${stdout}
|
|
109
|
-
${stderr}`;
|
|
110
|
-
if (status !== 0) {
|
|
111
|
-
return /not found|could not be found|no such unit/i.test(combined) ? "stopped" : "unknown";
|
|
112
|
-
}
|
|
113
|
-
const mainPid = Number.parseInt(/^\s*MainPID=(\d+)\s*$/im.exec(combined)?.[1] ?? "0", 10);
|
|
114
|
-
if (Number.isInteger(mainPid) && mainPid > 0) return "running";
|
|
115
|
-
const active = /^\s*ActiveState=([^\r\n]+)$/im.exec(combined)?.[1]?.trim().toLowerCase();
|
|
116
|
-
if (!active) return "unknown";
|
|
117
|
-
if (active === "active" || active === "activating" || active === "reloading") return "running";
|
|
118
|
-
if (active === "inactive" || active === "failed" || active === "deactivating") return "stopped";
|
|
119
|
-
return "unknown";
|
|
120
|
-
}
|
|
121
|
-
function queryPlatformServiceState() {
|
|
122
|
-
try {
|
|
123
|
-
if (process.platform === "win32") return queryWindowsServiceTaskState();
|
|
124
|
-
if (process.platform === "darwin") {
|
|
125
|
-
const uid = process.getuid?.() ?? 501;
|
|
126
|
-
const r = spawnSync("launchctl", ["print", `gui/${uid}/dev.pie.manager`], { encoding: "utf8", timeout: 1e4 });
|
|
127
|
-
if (r.error) return "unknown";
|
|
128
|
-
return parseLaunchctlServiceState(r.status, r.stdout ?? "", r.stderr ?? "");
|
|
129
|
-
}
|
|
130
|
-
if (process.platform === "linux") {
|
|
131
|
-
const r = spawnSync("systemctl", ["--user", "show", "-p", "ActiveState", "-p", "MainPID", "dev.pie.manager"], { encoding: "utf8", timeout: 1e4 });
|
|
132
|
-
if (r.error) return "unknown";
|
|
133
|
-
return parseSystemctlServiceState(r.status, r.stdout ?? "", r.stderr ?? "");
|
|
134
|
-
}
|
|
135
|
-
return "unknown";
|
|
136
|
-
} catch {
|
|
137
|
-
return "unknown";
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
function scanLinuxServiceBlockingWorkers(discoveryDir) {
|
|
141
|
-
let files;
|
|
142
|
-
try {
|
|
143
|
-
files = fs.readdirSync(discoveryDir).filter((f) => f.endsWith(".json"));
|
|
144
|
-
} catch (error) {
|
|
145
|
-
if (error.code === "ENOENT") return { busy: false };
|
|
146
|
-
return { busy: true, detail: "discovery directory unreadable" };
|
|
147
|
-
}
|
|
148
|
-
if (files.length > 256) {
|
|
149
|
-
return { busy: true, detail: `too many discovery entries (${files.length}) to verify worker safety` };
|
|
150
|
-
}
|
|
151
|
-
const blocking = [];
|
|
152
|
-
for (const file of files) {
|
|
153
|
-
let raw;
|
|
154
|
-
try {
|
|
155
|
-
raw = fs.readFileSync(path.join(discoveryDir, file), "utf8");
|
|
156
|
-
} catch (error) {
|
|
157
|
-
if (error.code === "ENOENT") continue;
|
|
158
|
-
return { busy: true, detail: `unreadable discovery entry (${file})` };
|
|
159
|
-
}
|
|
160
|
-
let parsed;
|
|
161
|
-
try {
|
|
162
|
-
const decoded = JSON.parse(raw);
|
|
163
|
-
if (decoded === null || typeof decoded !== "object" || Array.isArray(decoded)) {
|
|
164
|
-
return { busy: true, detail: `malformed discovery entry (${file})` };
|
|
165
|
-
}
|
|
166
|
-
parsed = decoded;
|
|
167
|
-
} catch {
|
|
168
|
-
return { busy: true, detail: `malformed discovery entry (${file})` };
|
|
169
|
-
}
|
|
170
|
-
if (parsed.isManager === true) continue;
|
|
171
|
-
if (parsed.unattended !== true) continue;
|
|
172
|
-
const pid = parsed.pid;
|
|
173
|
-
if (typeof pid !== "number" || !Number.isInteger(pid) || pid <= 0) {
|
|
174
|
-
blocking.push(`${file}(invalid-pid)`);
|
|
175
|
-
continue;
|
|
176
|
-
}
|
|
177
|
-
if (pid === process.pid) continue;
|
|
178
|
-
if (pidDefinitelyDead(pid)) continue;
|
|
179
|
-
const state = typeof parsed.state === "string" && parsed.state ? parsed.state : "unknown";
|
|
180
|
-
if (state !== "idle") {
|
|
181
|
-
const name = typeof parsed.displayName === "string" && parsed.displayName ? parsed.displayName : String(pid);
|
|
182
|
-
blocking.push(`${name}(${state})`);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
return blocking.length > 0 ? { busy: true, detail: blocking.join(", ") } : { busy: false };
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// src/commands/manager-upgrade.ts
|
|
189
32
|
function upgradePaths(entryPath) {
|
|
190
33
|
const home = os.homedir() || process.env.HOME || process.env.USERPROFILE || "/tmp";
|
|
191
34
|
const override = process.env.PIE_IPC_CONFIG_DIR;
|
|
192
35
|
const agentDir = process.env.PIE_CODING_AGENT_DIR?.trim();
|
|
193
|
-
const raw = override !== void 0 ? override : agentDir ||
|
|
194
|
-
const configDir = process.platform === "win32" ? raw :
|
|
36
|
+
const raw = override !== void 0 ? override : agentDir || path.join(home, ".pie");
|
|
37
|
+
const configDir = process.platform === "win32" ? raw : path.resolve(raw === "~" ? home : raw.startsWith("~/") ? path.join(home, raw.slice(2)) : raw);
|
|
195
38
|
return {
|
|
196
|
-
workspaceDir:
|
|
197
|
-
discoveryDir:
|
|
198
|
-
stateFile:
|
|
199
|
-
resultFile:
|
|
200
|
-
lockFile:
|
|
39
|
+
workspaceDir: path.join(configDir, "manager"),
|
|
40
|
+
discoveryDir: path.join(configDir, "run", "ipc"),
|
|
41
|
+
stateFile: path.join(configDir, "manager", "manager.json"),
|
|
42
|
+
resultFile: path.join(configDir, "manager", "upgrade.json"),
|
|
43
|
+
lockFile: path.join(configDir, "manager", "upgrade.lock"),
|
|
201
44
|
entryPath
|
|
202
45
|
};
|
|
203
46
|
}
|
|
204
47
|
function isDistInstall(entryPath) {
|
|
205
|
-
let resolved =
|
|
48
|
+
let resolved = path.resolve(entryPath);
|
|
206
49
|
try {
|
|
207
|
-
resolved =
|
|
50
|
+
resolved = fs.realpathSync(resolved);
|
|
208
51
|
} catch {
|
|
209
52
|
}
|
|
210
53
|
return /node_modules[\\/]@cydm[\\/]pie[\\/]dist[\\/]cli\.(js|mjs)$/.test(resolved);
|
|
@@ -214,23 +57,23 @@ function versionDiffers(a, b) {
|
|
|
214
57
|
return canon(a) !== canon(b);
|
|
215
58
|
}
|
|
216
59
|
function resolveNpm(nodePath, platform = process.platform, env = process.env) {
|
|
217
|
-
const nodeDir =
|
|
218
|
-
const prefix =
|
|
60
|
+
const nodeDir = path.dirname(nodePath);
|
|
61
|
+
const prefix = path.dirname(nodeDir);
|
|
219
62
|
const windows = platform === "win32";
|
|
220
63
|
const candidates = [
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
64
|
+
path.join(nodeDir, "node_modules", "npm", "bin", "npm-cli.js"),
|
|
65
|
+
path.join(prefix, "lib", "node_modules", "npm", "bin", "npm-cli.js"),
|
|
66
|
+
path.join(prefix, "node_modules", "npm", "bin", "npm-cli.js")
|
|
224
67
|
];
|
|
225
68
|
if (windows) {
|
|
226
69
|
const searchPath = Object.entries(env).find(([key]) => key.toUpperCase() === "PATH")?.[1] ?? "";
|
|
227
70
|
for (const dir of searchPath.split(";").filter(Boolean)) {
|
|
228
|
-
candidates.push(
|
|
71
|
+
candidates.push(path.resolve(dir.replace(/^"(.*)"$/, "$1"), "node_modules", "npm", "bin", "npm-cli.js"));
|
|
229
72
|
}
|
|
230
73
|
}
|
|
231
74
|
const isFile = (file) => {
|
|
232
75
|
try {
|
|
233
|
-
return
|
|
76
|
+
return fs.statSync(file).isFile();
|
|
234
77
|
} catch {
|
|
235
78
|
return false;
|
|
236
79
|
}
|
|
@@ -239,12 +82,12 @@ function resolveNpm(nodePath, platform = process.platform, env = process.env) {
|
|
|
239
82
|
if (isFile(npmCli)) return { command: nodePath, prefixArgs: [npmCli], reason: `node + ${npmCli}` };
|
|
240
83
|
}
|
|
241
84
|
const binary = windows ? "npm.exe" : "npm";
|
|
242
|
-
const sibling =
|
|
85
|
+
const sibling = path.join(nodeDir, binary);
|
|
243
86
|
if (isFile(sibling)) return { command: sibling, prefixArgs: [], reason: `sibling ${sibling}` };
|
|
244
87
|
return { command: binary, prefixArgs: [], reason: `PATH ${binary}` };
|
|
245
88
|
}
|
|
246
89
|
function runNpm(plan, args, timeoutMs) {
|
|
247
|
-
const child =
|
|
90
|
+
const child = spawnSync(plan.command, [...plan.prefixArgs, ...args], {
|
|
248
91
|
encoding: "utf8",
|
|
249
92
|
timeout: timeoutMs,
|
|
250
93
|
windowsHide: true,
|
|
@@ -277,7 +120,7 @@ function parseViewVersion(stdout) {
|
|
|
277
120
|
}
|
|
278
121
|
function readDiscoveryEntry(discoveryDir, file) {
|
|
279
122
|
try {
|
|
280
|
-
const parsed = JSON.parse(
|
|
123
|
+
const parsed = JSON.parse(fs.readFileSync(path.join(discoveryDir, file), "utf8"));
|
|
281
124
|
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return void 0;
|
|
282
125
|
return parsed;
|
|
283
126
|
} catch {
|
|
@@ -287,7 +130,7 @@ function readDiscoveryEntry(discoveryDir, file) {
|
|
|
287
130
|
function scanWorkers(discoveryDir) {
|
|
288
131
|
const out = [];
|
|
289
132
|
try {
|
|
290
|
-
const files =
|
|
133
|
+
const files = fs.readdirSync(discoveryDir).filter((f) => f.endsWith(".json"));
|
|
291
134
|
for (const file of files.slice(0, 256)) {
|
|
292
135
|
const parsed = readDiscoveryEntry(discoveryDir, file);
|
|
293
136
|
if (!parsed) continue;
|
|
@@ -322,7 +165,7 @@ function partitionWorkers(peers) {
|
|
|
322
165
|
}
|
|
323
166
|
function findManagerResident(discoveryDir, sessionId) {
|
|
324
167
|
try {
|
|
325
|
-
const files =
|
|
168
|
+
const files = fs.readdirSync(discoveryDir).filter((f) => f.endsWith(".json"));
|
|
326
169
|
for (const file of files.slice(0, 256)) {
|
|
327
170
|
const parsed = readDiscoveryEntry(discoveryDir, file);
|
|
328
171
|
if (!parsed || parsed.isManager !== true || typeof parsed.pid !== "number" || !Number.isInteger(parsed.pid) || parsed.pid <= 0) continue;
|
|
@@ -353,7 +196,7 @@ function residentCommandLineLooksLikePie(pid, sessionToken) {
|
|
|
353
196
|
function processStartedAt(pid) {
|
|
354
197
|
if (process.platform !== "darwin" && process.platform !== "linux") return void 0;
|
|
355
198
|
try {
|
|
356
|
-
const out =
|
|
199
|
+
const out = spawnSync("ps", ["-p", String(pid), "-o", "lstart="], { encoding: "utf8", timeout: 5e3 });
|
|
357
200
|
const parsed = out.status === 0 ? Date.parse((out.stdout ?? "").trim()) : NaN;
|
|
358
201
|
return Number.isFinite(parsed) ? parsed : void 0;
|
|
359
202
|
} catch {
|
|
@@ -533,18 +376,18 @@ async function restartAndRecycle(effects, paths, state, fromVersion, toVersion,
|
|
|
533
376
|
}
|
|
534
377
|
function readUpgradeResult(resultFile) {
|
|
535
378
|
try {
|
|
536
|
-
return JSON.parse(
|
|
379
|
+
return JSON.parse(fs.readFileSync(resultFile, "utf8"));
|
|
537
380
|
} catch {
|
|
538
381
|
return void 0;
|
|
539
382
|
}
|
|
540
383
|
}
|
|
541
384
|
function writeResultAtomic(resultFile, result, log) {
|
|
542
385
|
try {
|
|
543
|
-
|
|
386
|
+
fs.mkdirSync(path.dirname(resultFile), { recursive: true });
|
|
544
387
|
const tmp = `${resultFile}.${process.pid}.tmp`;
|
|
545
|
-
|
|
388
|
+
fs.writeFileSync(tmp, `${JSON.stringify(result, null, " ")}
|
|
546
389
|
`);
|
|
547
|
-
|
|
390
|
+
fs.renameSync(tmp, resultFile);
|
|
548
391
|
} catch (error) {
|
|
549
392
|
log(`upgrade: could not write the result file: ${error instanceof Error ? error.message : String(error)}`);
|
|
550
393
|
}
|
|
@@ -552,7 +395,7 @@ function writeResultAtomic(resultFile, result, log) {
|
|
|
552
395
|
function insideServiceCgroup() {
|
|
553
396
|
if (process.platform !== "linux") return false;
|
|
554
397
|
try {
|
|
555
|
-
return
|
|
398
|
+
return fs.readFileSync("/proc/self/cgroup", "utf8").includes("dev.pie.manager");
|
|
556
399
|
} catch {
|
|
557
400
|
return false;
|
|
558
401
|
}
|
|
@@ -564,34 +407,34 @@ function cliEffects(paths, log) {
|
|
|
564
407
|
npmInstall: (target) => runNpm(npm, ["install", "-g", `@cydm/pie@${target}`], 5 * 6e4),
|
|
565
408
|
installTargetCheck: () => {
|
|
566
409
|
try {
|
|
567
|
-
const rootRaw =
|
|
410
|
+
const rootRaw = spawnSync(npm.command, [...npm.prefixArgs, "root", "-g"], { encoding: "utf8", timeout: 3e4, windowsHide: true });
|
|
568
411
|
const npmRoot = String(rootRaw.stdout ?? "").trim().split(/\r?\n/).filter(Boolean).pop() ?? "";
|
|
569
412
|
if (rootRaw.status !== 0 || !npmRoot) return { ok: false, detail: `npm root -g failed (exit ${rootRaw.status}): ${String(rootRaw.stderr ?? "").slice(-200)}` };
|
|
570
413
|
let entry;
|
|
571
414
|
try {
|
|
572
|
-
entry =
|
|
415
|
+
entry = fs.realpathSync(paths.entryPath);
|
|
573
416
|
} catch {
|
|
574
417
|
return { ok: false, detail: `entry ${paths.entryPath} is not resolvable` };
|
|
575
418
|
}
|
|
576
|
-
let dir =
|
|
419
|
+
let dir = path.dirname(entry);
|
|
577
420
|
let pkgRoot;
|
|
578
421
|
for (let i = 0; i < 6 && dir; i += 1) {
|
|
579
|
-
if (
|
|
422
|
+
if (fs.existsSync(path.join(dir, "package.json"))) {
|
|
580
423
|
pkgRoot = dir;
|
|
581
424
|
break;
|
|
582
425
|
}
|
|
583
|
-
const parent =
|
|
426
|
+
const parent = path.dirname(dir);
|
|
584
427
|
if (parent === dir) break;
|
|
585
428
|
dir = parent;
|
|
586
429
|
}
|
|
587
430
|
let installedPkg;
|
|
588
431
|
try {
|
|
589
|
-
installedPkg =
|
|
432
|
+
installedPkg = fs.realpathSync(path.join(npmRoot, "@cydm", "pie"));
|
|
590
433
|
} catch {
|
|
591
434
|
return { ok: false, detail: `npm root ${npmRoot} has no @cydm/pie installed (realpath failed) \u2014 the install did not land in this npm's global tree.` };
|
|
592
435
|
}
|
|
593
436
|
if (pkgRoot === void 0 || installedPkg !== pkgRoot) {
|
|
594
|
-
return { ok: false, detail: `npm installed ${installedPkg} but the running entry resolves to ${pkgRoot ??
|
|
437
|
+
return { ok: false, detail: `npm installed ${installedPkg} but the running entry resolves to ${pkgRoot ?? path.dirname(entry)} \u2014 a different prefix owns this Pie (multi-Node or custom npm prefix); the restart would keep the OLD code. Install with the npm that owns ${pkgRoot ?? entry} or fix the entry resolution.` };
|
|
595
438
|
}
|
|
596
439
|
return { ok: true, detail: `install target verified: ${pkgRoot}` };
|
|
597
440
|
} catch (error) {
|
|
@@ -605,7 +448,7 @@ function cliEffects(paths, log) {
|
|
|
605
448
|
},
|
|
606
449
|
readState: () => {
|
|
607
450
|
try {
|
|
608
|
-
return JSON.parse(
|
|
451
|
+
return JSON.parse(fs.readFileSync(paths.stateFile, "utf8"));
|
|
609
452
|
} catch {
|
|
610
453
|
return void 0;
|
|
611
454
|
}
|
|
@@ -686,7 +529,7 @@ function cliEffects(paths, log) {
|
|
|
686
529
|
}
|
|
687
530
|
},
|
|
688
531
|
startSuccessor: () => {
|
|
689
|
-
const child =
|
|
532
|
+
const child = spawnSync(
|
|
690
533
|
process.execPath,
|
|
691
534
|
[paths.entryPath, "manager", "start"],
|
|
692
535
|
{ encoding: "utf8", timeout: 9e4, windowsHide: true, cwd: paths.workspaceDir, env: process.env }
|
|
@@ -704,7 +547,7 @@ function cliEffects(paths, log) {
|
|
|
704
547
|
return { pid: void 0, detail: `pie manager start reported pid ${pid} but no readiness marker: ${out.slice(-300)}` };
|
|
705
548
|
}
|
|
706
549
|
try {
|
|
707
|
-
const recorded = JSON.parse(
|
|
550
|
+
const recorded = JSON.parse(fs.readFileSync(paths.stateFile, "utf8"));
|
|
708
551
|
if (recorded.pid !== pid) return { pid: void 0, detail: `recorded state pid ${String(recorded.pid)} \u2260 started pid ${pid} (replaced or stopped concurrently)` };
|
|
709
552
|
process.kill(pid, 0);
|
|
710
553
|
} catch (error) {
|
|
@@ -716,7 +559,7 @@ function cliEffects(paths, log) {
|
|
|
716
559
|
if (insideServiceCgroup()) {
|
|
717
560
|
try {
|
|
718
561
|
const unit = `dev.pie.manager-upgrade-${process.pid}`;
|
|
719
|
-
const delegated =
|
|
562
|
+
const delegated = spawnSync("systemd-run", ["--user", "--unit", unit, "--collect", "systemctl", "--user", "restart", "dev.pie.manager"], { encoding: "utf8", timeout: 2e4 });
|
|
720
563
|
return { ok: false, detail: `restart delegated to transient unit ${unit} (${delegated.status === 0 ? "accepted" : `systemd-run exit ${delegated.status}`}); the actor runs inside the service cgroup and cannot verify the outcome \u2014 check pie manager status afterwards` };
|
|
721
564
|
} catch (error) {
|
|
722
565
|
return { ok: false, detail: `could not delegate the service restart: ${error instanceof Error ? error.message : String(error)}` };
|
|
@@ -728,18 +571,32 @@ function cliEffects(paths, log) {
|
|
|
728
571
|
try {
|
|
729
572
|
if (process.platform === "darwin") {
|
|
730
573
|
const uid = process.getuid?.() ?? 501;
|
|
731
|
-
const r =
|
|
574
|
+
const r = spawnSync("launchctl", ["kickstart", "-k", `gui/${uid}/dev.pie.manager`], { encoding: "utf8", timeout: 2e4 });
|
|
732
575
|
restartDetail = `${r.stdout ?? ""}${r.stderr ?? ""}`.trim();
|
|
733
576
|
restartFailed = r.status !== 0;
|
|
577
|
+
if (restartFailed && /Could not find service/i.test(restartDetail)) {
|
|
578
|
+
const plist = path.join(os.homedir(), "Library", "LaunchAgents", "dev.pie.manager.plist");
|
|
579
|
+
if (fs.existsSync(plist)) {
|
|
580
|
+
const loaded = spawnSync("launchctl", ["load", plist], { encoding: "utf8", timeout: 2e4 });
|
|
581
|
+
restartDetail = `${loaded.stdout ?? ""}${loaded.stderr ?? ""}`.trim();
|
|
582
|
+
restartFailed = loaded.status !== 0;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
734
585
|
} else if (process.platform === "linux") {
|
|
735
|
-
const r =
|
|
586
|
+
const r = spawnSync("systemctl", ["--user", "restart", "dev.pie.manager"], { encoding: "utf8", timeout: 2e4 });
|
|
736
587
|
restartDetail = `${r.stdout ?? ""}${r.stderr ?? ""}`.trim();
|
|
737
588
|
restartFailed = r.status !== 0;
|
|
738
589
|
} else if (process.platform === "win32") {
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
590
|
+
const prep = stopAndRefreshWindowsServiceForUpgrade({ taskName: "dev.pie.manager", workingDir: paths.workspaceDir, nodePath: process.execPath, entryPath: paths.entryPath, sessionId: state.sessionId, env: process.env });
|
|
591
|
+
if (!prep.ok) {
|
|
592
|
+
restartDetail = prep.detail ?? "Windows service stop before refresh failed";
|
|
593
|
+
restartFailed = true;
|
|
594
|
+
} else {
|
|
595
|
+
if (!prep.refreshed && prep.refreshDetail) log(`[manager-upgrade] service launcher refresh not applied: ${prep.refreshDetail}`);
|
|
596
|
+
const r = spawnSync("schtasks", ["/Run", "/TN", "dev.pie.manager"], { encoding: "utf8", timeout: 2e4, windowsHide: true });
|
|
597
|
+
restartDetail = `${r.stdout ?? ""}${r.stderr ?? ""}`.trim();
|
|
598
|
+
restartFailed = r.status !== 0;
|
|
599
|
+
}
|
|
743
600
|
} else {
|
|
744
601
|
return { ok: false, detail: `unsupported platform ${process.platform}` };
|
|
745
602
|
}
|
|
@@ -807,7 +664,7 @@ function cliEffects(paths, log) {
|
|
|
807
664
|
residentStatus: (s) => effects.residentStatus(s),
|
|
808
665
|
terminateResident: (s) => effects.terminateResident(s),
|
|
809
666
|
endServiceTask: () => {
|
|
810
|
-
const r =
|
|
667
|
+
const r = spawnSync("schtasks", ["/End", "/TN", "dev.pie.manager"], { encoding: "utf8", timeout: 2e4, windowsHide: true });
|
|
811
668
|
return {
|
|
812
669
|
status: r.status,
|
|
813
670
|
detail: `${r.stdout ?? ""} ${r.stderr ?? ""}`.trim().slice(0, 200),
|
package/dist/cli.js
CHANGED
|
@@ -138,8 +138,8 @@ import {
|
|
|
138
138
|
claimSessionOwnership,
|
|
139
139
|
liveSessionHolder,
|
|
140
140
|
sessionOwnerAlive
|
|
141
|
-
} from "./chunks/chunk-
|
|
142
|
-
import "./chunks/chunk-
|
|
141
|
+
} from "./chunks/chunk-Q5PSWX3B.js";
|
|
142
|
+
import "./chunks/chunk-Q4NJEPIZ.js";
|
|
143
143
|
import {
|
|
144
144
|
ModelRegistry
|
|
145
145
|
} from "./chunks/chunk-IZ4DYCJ7.js";
|
|
@@ -86659,7 +86659,7 @@ async function main() {
|
|
|
86659
86659
|
if (sub === STABLE_CLI_COMMANDS.models || sub === STABLE_CLI_COMMANDS.manager) {
|
|
86660
86660
|
try {
|
|
86661
86661
|
if (sub === STABLE_CLI_COMMANDS.models) await runModelsCommand(subArgs);
|
|
86662
|
-
else await (await import("./chunks/manager-cli-
|
|
86662
|
+
else await (await import("./chunks/manager-cli-OTIMAWO6.js")).runManagerCommand(subArgs);
|
|
86663
86663
|
} catch (error) {
|
|
86664
86664
|
console.error(error instanceof Error ? error.message : String(error));
|
|
86665
86665
|
process.exitCode = 1;
|
package/package.json
CHANGED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url);
|
|
2
|
-
|
|
3
|
-
// src/core/node-ownership-lock.ts
|
|
4
|
-
import * as fs from "node:fs";
|
|
5
|
-
import * as path from "node:path";
|
|
6
|
-
import { randomUUID } from "node:crypto";
|
|
7
|
-
function pidDefinitelyDead(pid) {
|
|
8
|
-
if (!Number.isInteger(pid) || pid <= 0) return false;
|
|
9
|
-
try {
|
|
10
|
-
process.kill(pid, 0);
|
|
11
|
-
return false;
|
|
12
|
-
} catch (error) {
|
|
13
|
-
return error.code === "ESRCH";
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
function withOwnershipMutex(lock, fn) {
|
|
17
|
-
fs.mkdirSync(path.dirname(lock), { recursive: true, mode: 448 });
|
|
18
|
-
const token = randomUUID();
|
|
19
|
-
const candidate = `${lock}.acquire.${token}`;
|
|
20
|
-
fs.mkdirSync(candidate, { mode: 448 });
|
|
21
|
-
try {
|
|
22
|
-
fs.writeFileSync(path.join(candidate, `held.${token}`), JSON.stringify({ version: 1, pid: process.pid }), { flag: "wx", mode: 384 });
|
|
23
|
-
const deadline = process.hrtime.bigint() + 2000000000n;
|
|
24
|
-
for (; ; ) {
|
|
25
|
-
try {
|
|
26
|
-
fs.renameSync(candidate, lock);
|
|
27
|
-
break;
|
|
28
|
-
} catch (error) {
|
|
29
|
-
if (!["EEXIST", "ENOTEMPTY", "EPERM", "ENOTDIR"].includes(error.code ?? "")) throw error;
|
|
30
|
-
if (process.hrtime.bigint() >= deadline) throw new Error(`Ownership mutex contention: ${lock}`);
|
|
31
|
-
try {
|
|
32
|
-
const entries = fs.readdirSync(lock);
|
|
33
|
-
if (entries.every((name) => {
|
|
34
|
-
if (!/^held\.[a-f0-9-]+$/.test(name)) return false;
|
|
35
|
-
const value = JSON.parse(fs.readFileSync(path.join(lock, name), "utf8"));
|
|
36
|
-
return value?.version === 1 && pidDefinitelyDead(value.pid);
|
|
37
|
-
})) {
|
|
38
|
-
for (const name of entries) {
|
|
39
|
-
try {
|
|
40
|
-
fs.unlinkSync(path.join(lock, name));
|
|
41
|
-
} catch (e) {
|
|
42
|
-
if (e.code !== "ENOENT") throw e;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
fs.rmdirSync(lock);
|
|
46
|
-
}
|
|
47
|
-
} catch {
|
|
48
|
-
}
|
|
49
|
-
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 3);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
try {
|
|
53
|
-
return fn();
|
|
54
|
-
} finally {
|
|
55
|
-
try {
|
|
56
|
-
fs.unlinkSync(path.join(lock, `held.${token}`));
|
|
57
|
-
} catch {
|
|
58
|
-
}
|
|
59
|
-
try {
|
|
60
|
-
fs.rmdirSync(lock);
|
|
61
|
-
} catch {
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
} finally {
|
|
65
|
-
fs.rmSync(candidate, { recursive: true, force: true });
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
function readOwnership(target, valid) {
|
|
69
|
-
let raw;
|
|
70
|
-
try {
|
|
71
|
-
raw = fs.readFileSync(target, "utf8");
|
|
72
|
-
} catch (error) {
|
|
73
|
-
if (error.code === "ENOENT") return void 0;
|
|
74
|
-
throw error;
|
|
75
|
-
}
|
|
76
|
-
const value = JSON.parse(raw);
|
|
77
|
-
if (!value || !Number.isInteger(value.pid) || value.pid <= 0 || typeof value.holder !== "string" || !Number.isFinite(value.heartbeatAt) || value.generation !== void 0 && typeof value.generation !== "string" || !valid(value)) {
|
|
78
|
-
throw new Error(`Invalid ownership record: ${target}`);
|
|
79
|
-
}
|
|
80
|
-
return value;
|
|
81
|
-
}
|
|
82
|
-
function claimOwnership(target, mine, valid) {
|
|
83
|
-
const mutex = `${target}.mutex`;
|
|
84
|
-
const generation = randomUUID();
|
|
85
|
-
const publish = () => {
|
|
86
|
-
const tmp = `${target}.${randomUUID()}.tmp`;
|
|
87
|
-
try {
|
|
88
|
-
fs.writeFileSync(tmp, `${JSON.stringify({ ...mine, generation, heartbeatAt: Date.now() })}
|
|
89
|
-
`, { flag: "wx", mode: 384 });
|
|
90
|
-
fs.renameSync(tmp, target);
|
|
91
|
-
} finally {
|
|
92
|
-
fs.rmSync(tmp, { force: true });
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
const holder = withOwnershipMutex(mutex, () => {
|
|
96
|
-
const current = readOwnership(target, valid);
|
|
97
|
-
if (current && !pidDefinitelyDead(current.pid)) return current;
|
|
98
|
-
publish();
|
|
99
|
-
return void 0;
|
|
100
|
-
});
|
|
101
|
-
if (holder) return { holder };
|
|
102
|
-
let released = false;
|
|
103
|
-
return { handle: {
|
|
104
|
-
heartbeat: () => {
|
|
105
|
-
if (released) return false;
|
|
106
|
-
return withOwnershipMutex(mutex, () => {
|
|
107
|
-
if (readOwnership(target, valid)?.generation !== generation) return false;
|
|
108
|
-
publish();
|
|
109
|
-
return true;
|
|
110
|
-
});
|
|
111
|
-
},
|
|
112
|
-
release: () => {
|
|
113
|
-
if (released) return;
|
|
114
|
-
released = true;
|
|
115
|
-
withOwnershipMutex(mutex, () => {
|
|
116
|
-
if (readOwnership(target, valid)?.generation === generation) fs.unlinkSync(target);
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
} };
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export {
|
|
123
|
-
pidDefinitelyDead,
|
|
124
|
-
withOwnershipMutex,
|
|
125
|
-
readOwnership,
|
|
126
|
-
claimOwnership
|
|
127
|
-
};
|