@mozhi0012/pi-terminal-worker 0.3.0 → 0.4.0

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.
@@ -1,312 +1,328 @@
1
- /**
2
- * pi-terminal-worker supervisor bootstrap
3
- * 随包发布的固定 ES 模块,运行在独立终端窗口中,作为子进程的 supervisor。
4
- */
5
-
6
- import * as net from "node:net";
7
- import * as crypto from "node:crypto";
8
- import { spawn } from "node:child_process";
9
- import { StringDecoder } from "node:string_decoder";
10
- import { validateDescriptor, monitorOwner, buildChildEnv } from "./bootstrap-support.mjs";
11
-
12
- function parseArgs() {
13
- const args = process.argv.slice(2);
14
- let descriptorB64 = null;
15
- let title = null;
16
- for (let i = 0; i < args.length; i++) {
17
- if (args[i] === "--descriptor" && i + 1 < args.length) {
18
- descriptorB64 = args[i + 1];
19
- } else if (args[i] === "--title" && i + 1 < args.length) {
20
- title = args[i + 1];
21
- }
22
- }
23
- if (!descriptorB64) {
24
- console.error("[bootstrap] 错误: 缺少 --descriptor 参数");
25
- process.exit(1);
26
- }
27
-
28
- try {
29
- const json = Buffer.from(descriptorB64, "base64url").toString("utf8");
30
- return { descriptor: validateDescriptor(JSON.parse(json)), title };
31
- } catch (err) {
32
- console.error("[bootstrap] 描述符解析失败:", err.message);
33
- process.exit(1);
34
- }
35
- }
36
-
37
- const parsed = parseArgs();
38
- const descriptor = parsed.descriptor;
39
- const { controllerId, workerId, pipePath, bootstrapToken } = descriptor;
40
-
41
- // PS/CMD 回退后端下由本进程设置新控制台窗口标题 (WT 后端用 --title,不受影响)
42
- if (process.platform === "win32" && parsed.title) {
43
- try {
44
- process.title = parsed.title;
45
- } catch {}
46
- }
47
-
48
- let socket = null;
49
- let seq = 0;
50
- let childProcess = null;
51
- let isExiting = false;
52
- /** 是否已完成 hello_ok 认证;未认证前拒绝 launch/terminate */
53
- let authenticated = false;
54
- /** 是否已发起过 launch;用独立布尔避免 spawn 抛错后仍可重复启动 */
55
- let launchRequested = false;
56
- /** 主控已要求终止子进程;此后子进程的退出属于预期内 */
57
- let terminateRequested = false;
58
- let stopOwnerMonitor = () => {};
59
- // owner 消失后不依赖 Pi 的 shutdown 钩子;由 supervisor 终止 L2,随后子进程 exit 路径退出。
60
- stopOwnerMonitor = monitorOwner(descriptor.ownerPid, () => {
61
- isExiting = true;
62
- if (childProcess && childProcess.exitCode === null && childProcess.signalCode === null) {
63
- terminateChild(true);
64
- } else {
65
- socket?.destroy();
66
- process.exit(0);
67
- }
68
- });
69
-
70
- /** 接收缓冲上限:超过即停止累积并销毁连接 (2 MiB) */
71
- const MAX_RECV_BUFFER_BYTES = 2 * 1024 * 1024;
72
-
73
- function isNonEmptyString(value, maxLen) {
74
- return typeof value === "string" && value.length > 0 && value.length <= maxLen;
75
- }
76
-
77
- /**
78
- * 严格校验 launch payload,返回 { ok: true } 或 { ok: false, field }。
79
- * 缺失/类型错误/超长都视为校验失败。
80
- */
81
- function validateLaunchPayload(payload) {
82
- if (typeof payload !== "object" || payload === null || Array.isArray(payload)) {
83
- return { ok: false, field: "payload" };
84
- }
85
- for (const field of ["cwd", "nodePath", "piCliPath", "extensionPath", "workerPipePath"]) {
86
- if (!isNonEmptyString(payload[field], 4096)) {
87
- return { ok: false, field };
88
- }
89
- }
90
- if (!isNonEmptyString(payload.workerToken, 512)) {
91
- return { ok: false, field: "workerToken" };
92
- }
93
- for (const field of ["provider", "model", "thinkingLevel"]) {
94
- const value = payload[field];
95
- if (value === undefined || value === null) continue;
96
- if (!isNonEmptyString(value, 512)) {
97
- return { ok: false, field };
98
- }
99
- }
100
- if (payload.toolPolicy !== undefined && payload.toolPolicy !== null && !isNonEmptyString(payload.toolPolicy, 16 * 1024)) {
101
- return { ok: false, field: "toolPolicy" };
102
- }
103
- return { ok: true };
104
- }
105
-
106
- function sendEnvelope(type, payload, replyTo) {
107
- if (!socket || socket.destroyed) return;
108
- const env = {
109
- version: 1,
110
- controllerId,
111
- workerId,
112
- id: crypto.randomUUID(),
113
- replyTo,
114
- seq: ++seq,
115
- type,
116
- payload,
117
- };
118
- socket.write(JSON.stringify(env) + "\n", "utf8");
119
- }
120
-
121
- function sendAck(replyTo, ok, error, code) {
122
- sendEnvelope("ack", { id: replyTo, ok, error, code }, replyTo);
123
- }
124
-
125
- function terminateChild(force) {
126
- if (!childProcess || childProcess.exitCode !== null || childProcess.signalCode !== null) return;
127
- if (force && process.platform === "win32") {
128
- const fallback = () => childProcess?.kill("SIGKILL");
129
- try {
130
- const killer = spawn("taskkill", ["/F", "/T", "/PID", String(childProcess.pid)], {
131
- stdio: "ignore",
132
- });
133
- killer.on("error", fallback);
134
- killer.on("exit", (code) => { if (code !== 0) fallback(); });
135
- } catch {
136
- fallback();
137
- }
138
- } else {
139
- childProcess.kill(force ? "SIGKILL" : "SIGTERM");
140
- }
141
- }
142
-
143
- function handleEnvelope(envelope) {
144
- const { id, type, payload } = envelope;
145
-
146
- if (type === "hello_ok") {
147
- // 握手完成,标记已认证,等待 controller 发送 launch 指令
148
- authenticated = true;
149
- return;
150
- }
151
-
152
- if (type === "ping") {
153
- sendEnvelope("pong", { timestamp: Date.now(), replyTimestamp: payload.timestamp }, id);
154
- return;
155
- }
156
-
157
- if (type === "launch") {
158
- if (!authenticated) {
159
- sendAck(id, false, "连接尚未完成认证", "AUTH_FAILED");
160
- return;
161
- }
162
- if (launchRequested) {
163
- sendAck(id, false, "已存在子进程,拒绝重复 launch", "ALREADY_EXISTS");
164
- return;
165
- }
166
- const validation = validateLaunchPayload(payload);
167
- if (!validation.ok) {
168
- const message = `launch payload 校验失败: ${validation.field}`;
169
- sendAck(id, false, message, "PROTOCOL_ERROR");
170
- sendEnvelope("launch_failed", { error: message });
171
- return;
172
- }
173
- // 先置位再 spawn:spawn 抛错后也不允许再次 launch,避免重复子进程歧义
174
- launchRequested = true;
175
- try {
176
- const { cwd, nodePath, piCliPath, extensionPath, workerToken, workerPipePath, provider, model, thinkingLevel, toolPolicy } = payload;
177
-
178
- // 可选模型参数:不传时保持 Pi 默认设置,不硬编码任何模型
179
- // 正常加载用户、项目与内置扩展,同时确保 Worker 通信扩展可用。
180
- const cliArgs = [piCliPath, "-e", extensionPath];
181
- if (provider) cliArgs.push("--provider", String(provider));
182
- if (model) cliArgs.push("--model", String(model));
183
- if (thinkingLevel) cliArgs.push("--thinking", String(thinkingLevel));
184
-
185
- childProcess = spawn(nodePath, cliArgs, {
186
- cwd,
187
- stdio: "inherit",
188
- shell: false,
189
- env: buildChildEnv(process.env, {
190
- role: descriptor.role,
191
- controllerId,
192
- workerId,
193
- workerPipePath,
194
- workerToken,
195
- toolPolicy,
196
- }),
197
- });
198
-
199
- sendAck(id, true);
200
-
201
- childProcess.on("error", (err) => {
202
- stopOwnerMonitor();
203
- sendEnvelope("launch_failed", { error: err.message });
204
- });
205
-
206
- childProcess.on("spawn", () => {
207
- sendEnvelope("child_spawned", { pid: childProcess.pid });
208
- });
209
-
210
- childProcess.on("exit", (code, signal) => {
211
- stopOwnerMonitor();
212
- isExiting = true;
213
- sendEnvelope("child_exit", {
214
- pid: childProcess ? childProcess.pid : 0,
215
- code,
216
- signal,
217
- });
218
-
219
- // 留出时间让退出消息刷入管道,然后退出。
220
- // 主控要求的终止按正常退出处理:taskkill 的退出码是 1,透传会让 Windows Terminal 保留窗口;
221
- // 真实崩溃保留非零退出码,窗口留着便于查看错误。真实退出码已在 child_exit 中上报。
222
- setTimeout(() => {
223
- if (socket) socket.destroy();
224
- process.exit(terminateRequested ? 0 : (code ?? 0));
225
- }, 300);
226
- });
227
- } catch (err) {
228
- stopOwnerMonitor();
229
- sendAck(id, false, err.message, "LAUNCH_FAILED");
230
- sendEnvelope("launch_failed", { error: err.message });
231
- }
232
- return;
233
- }
234
-
235
- if (type === "terminate") {
236
- if (!authenticated) {
237
- sendAck(id, false, "连接尚未完成认证", "AUTH_FAILED");
238
- return;
239
- }
240
- sendAck(id, true);
241
- terminateRequested = true;
242
- terminateChild(payload && payload.force);
243
- return;
244
- }
245
- }
246
-
247
- // 连接主控端的命名管道,带短暂重试
248
- let retryCount = 0;
249
- const maxRetries = 15;
250
-
251
- function connectToController() {
252
- socket = net.createConnection(pipePath);
253
-
254
- const decoder = new StringDecoder("utf8");
255
- let buffer = "";
256
-
257
- socket.on("connect", () => {
258
- // 发送 hello 进行 supervisor 认证
259
- sendEnvelope("hello", {
260
- role: "supervisor",
261
- token: bootstrapToken,
262
- controllerId,
263
- workerId,
264
- });
265
- });
266
-
267
- socket.on("data", (chunk) => {
268
- buffer += decoder.write(chunk);
269
- if (Buffer.byteLength(buffer, "utf8") > MAX_RECV_BUFFER_BYTES) {
270
- console.error("[bootstrap] 错误: 接收缓冲超过 2 MiB 上限,停止累积并销毁连接");
271
- try {
272
- socket.destroy();
273
- } catch {}
274
- if (!childProcess) {
275
- // 无子进程时留着一个没有通信对象的窗口没有意义
276
- process.exit(1);
277
- }
278
- return;
279
- }
280
- let idx;
281
- while ((idx = buffer.indexOf("\n")) !== -1) {
282
- const line = buffer.slice(0, idx).trim();
283
- buffer = buffer.slice(idx + 1);
284
- if (line.length === 0) continue;
285
- try {
286
- const envelope = JSON.parse(line);
287
- handleEnvelope(envelope);
288
- } catch (err) {
289
- console.error("[bootstrap] 消息解析异常:", err.message);
290
- }
291
- }
292
- });
293
-
294
- socket.on("error", (err) => {
295
- if (retryCount < maxRetries && !childProcess && !isExiting) {
296
- retryCount++;
297
- setTimeout(connectToController, 300);
298
- return;
299
- }
300
- if (!isExiting) {
301
- console.error("[bootstrap] 管道通信错误:", err.message);
302
- }
303
- });
304
-
305
- socket.on("close", () => {
306
- if (!isExiting && childProcess && !childProcess.killed) {
307
- // 主控端断开连接,保留当前子进程运行供人工查看,supervisor 结束监听
308
- }
309
- });
310
- }
311
-
312
- connectToController();
1
+ /**
2
+ * pi-terminal-worker supervisor bootstrap
3
+ * 随包发布的固定 ES 模块,运行在独立终端窗口中,作为子进程的 supervisor。
4
+ */
5
+
6
+ import * as net from "node:net";
7
+ import * as crypto from "node:crypto";
8
+ import { spawn } from "node:child_process";
9
+ import { StringDecoder } from "node:string_decoder";
10
+ import { validateDescriptor, monitorOwner, buildChildEnv } from "./bootstrap-support.mjs";
11
+ import { closeHerdrPane, killProcessTree } from "./terminal-support.mjs";
12
+
13
+ // Last action on every exit path, including invalid descriptors and lost lead owners.
14
+ process.once("exit", () => closeHerdrPane());
15
+
16
+ function parseArgs() {
17
+ const args = process.argv.slice(2);
18
+ let descriptorB64 = null;
19
+ let title = null;
20
+ for (let i = 0; i < args.length; i++) {
21
+ if (args[i] === "--descriptor" && i + 1 < args.length) {
22
+ descriptorB64 = args[i + 1];
23
+ } else if (args[i] === "--title" && i + 1 < args.length) {
24
+ title = args[i + 1];
25
+ }
26
+ }
27
+ if (!descriptorB64) {
28
+ console.error("[bootstrap] 错误: 缺少 --descriptor 参数");
29
+ process.exit(1);
30
+ }
31
+
32
+ try {
33
+ const json = Buffer.from(descriptorB64, "base64url").toString("utf8");
34
+ return { descriptor: validateDescriptor(JSON.parse(json)), title };
35
+ } catch (err) {
36
+ console.error("[bootstrap] 描述符解析失败:", err.message);
37
+ process.exit(1);
38
+ }
39
+ }
40
+
41
+ const parsed = parseArgs();
42
+ const descriptor = parsed.descriptor;
43
+ const { controllerId, workerId, pipePath, bootstrapToken } = descriptor;
44
+
45
+ // PS/CMD 回退后端下由本进程设置新控制台窗口标题 (WT 后端用 --title,不受影响)
46
+ if (process.platform === "win32" && parsed.title) {
47
+ try {
48
+ process.title = parsed.title;
49
+ } catch {}
50
+ }
51
+
52
+ let socket = null;
53
+ let seq = 0;
54
+ let childProcess = null;
55
+ let isExiting = false;
56
+ /** 是否已完成 hello_ok 认证;未认证前拒绝 launch/terminate */
57
+ let authenticated = false;
58
+ /** 是否已发起过 launch;用独立布尔避免 spawn 抛错后仍可重复启动 */
59
+ let launchRequested = false;
60
+ /** 主控已要求终止子进程;此后子进程的退出属于预期内 */
61
+ let terminateRequested = false;
62
+ let stopOwnerMonitor = () => {};
63
+ // owner 消失后不依赖 Pi 的 shutdown 钩子;由 supervisor 终止 L2,随后子进程 exit 路径退出。
64
+ stopOwnerMonitor = monitorOwner(descriptor.ownerPid, () => {
65
+ isExiting = true;
66
+ if (childProcess && childProcess.exitCode === null && childProcess.signalCode === null) {
67
+ terminateChild(true);
68
+ } else {
69
+ socket?.destroy();
70
+ process.exit(0);
71
+ }
72
+ });
73
+
74
+ /** 接收缓冲上限:超过即停止累积并销毁连接 (2 MiB) */
75
+ const MAX_RECV_BUFFER_BYTES = 2 * 1024 * 1024;
76
+
77
+ function isNonEmptyString(value, maxLen) {
78
+ return typeof value === "string" && value.length > 0 && value.length <= maxLen;
79
+ }
80
+
81
+ /**
82
+ * 严格校验 launch payload,返回 { ok: true } 或 { ok: false, field }。
83
+ * 缺失/类型错误/超长都视为校验失败。
84
+ */
85
+ function validateLaunchPayload(payload) {
86
+ if (typeof payload !== "object" || payload === null || Array.isArray(payload)) {
87
+ return { ok: false, field: "payload" };
88
+ }
89
+ for (const field of ["cwd", "nodePath", "piCliPath", "extensionPath", "workerPipePath"]) {
90
+ if (!isNonEmptyString(payload[field], 4096)) {
91
+ return { ok: false, field };
92
+ }
93
+ }
94
+ if (!isNonEmptyString(payload.workerToken, 512)) {
95
+ return { ok: false, field: "workerToken" };
96
+ }
97
+ for (const field of ["provider", "model", "thinkingLevel"]) {
98
+ const value = payload[field];
99
+ if (value === undefined || value === null) continue;
100
+ if (!isNonEmptyString(value, 512)) {
101
+ return { ok: false, field };
102
+ }
103
+ }
104
+ if (payload.toolPolicy !== undefined && payload.toolPolicy !== null && !isNonEmptyString(payload.toolPolicy, 16 * 1024)) {
105
+ return { ok: false, field: "toolPolicy" };
106
+ }
107
+ return { ok: true };
108
+ }
109
+
110
+ function sendEnvelope(type, payload, replyTo) {
111
+ if (!socket || socket.destroyed) return;
112
+ const env = {
113
+ version: 1,
114
+ controllerId,
115
+ workerId,
116
+ id: crypto.randomUUID(),
117
+ replyTo,
118
+ seq: ++seq,
119
+ type,
120
+ payload,
121
+ };
122
+ socket.write(JSON.stringify(env) + "\n", "utf8");
123
+ }
124
+
125
+ function sendAck(replyTo, ok, error, code) {
126
+ sendEnvelope("ack", { id: replyTo, ok, error, code }, replyTo);
127
+ }
128
+
129
+ function terminateChild(force) {
130
+ if (!childProcess || childProcess.exitCode !== null || childProcess.signalCode !== null) return;
131
+ if (force && process.platform === "win32") {
132
+ const fallback = () => childProcess?.kill("SIGKILL");
133
+ try {
134
+ const killer = spawn("taskkill", ["/F", "/T", "/PID", String(childProcess.pid)], {
135
+ stdio: "ignore",
136
+ });
137
+ killer.on("error", fallback);
138
+ killer.on("exit", (code) => { if (code !== 0) fallback(); });
139
+ } catch {
140
+ fallback();
141
+ }
142
+ } else if (force) {
143
+ try { killProcessTree(childProcess.pid); }
144
+ catch { childProcess.kill("SIGKILL"); }
145
+ } else {
146
+ childProcess.kill("SIGTERM");
147
+ }
148
+ }
149
+
150
+ function exitAfterFlush(code = 1) {
151
+ isExiting = true;
152
+ setTimeout(() => { socket?.destroy(); process.exit(code); }, 300);
153
+ }
154
+
155
+ function handleEnvelope(envelope) {
156
+ const { id, type, payload } = envelope;
157
+
158
+ if (type === "hello_ok") {
159
+ // 握手完成,标记已认证,等待 controller 发送 launch 指令
160
+ authenticated = true;
161
+ return;
162
+ }
163
+
164
+ if (type === "ping") {
165
+ sendEnvelope("pong", { timestamp: Date.now(), replyTimestamp: payload.timestamp }, id);
166
+ return;
167
+ }
168
+
169
+ if (type === "launch") {
170
+ if (!authenticated) {
171
+ sendAck(id, false, "连接尚未完成认证", "AUTH_FAILED");
172
+ return;
173
+ }
174
+ if (launchRequested) {
175
+ sendAck(id, false, "已存在子进程,拒绝重复 launch", "ALREADY_EXISTS");
176
+ return;
177
+ }
178
+ const validation = validateLaunchPayload(payload);
179
+ if (!validation.ok) {
180
+ const message = `launch payload 校验失败: ${validation.field}`;
181
+ sendAck(id, false, message, "PROTOCOL_ERROR");
182
+ sendEnvelope("launch_failed", { error: message });
183
+ exitAfterFlush();
184
+ return;
185
+ }
186
+ // 先置位再 spawn:spawn 抛错后也不允许再次 launch,避免重复子进程歧义
187
+ launchRequested = true;
188
+ try {
189
+ const { cwd, nodePath, piCliPath, extensionPath, workerToken, workerPipePath, provider, model, thinkingLevel, toolPolicy } = payload;
190
+
191
+ // 可选模型参数:不传时保持 Pi 默认设置,不硬编码任何模型
192
+ // 正常加载用户、项目与内置扩展,同时确保 Worker 通信扩展可用。
193
+ const cliArgs = [piCliPath, "-e", extensionPath];
194
+ if (provider) cliArgs.push("--provider", String(provider));
195
+ if (model) cliArgs.push("--model", String(model));
196
+ if (thinkingLevel) cliArgs.push("--thinking", String(thinkingLevel));
197
+
198
+ childProcess = spawn(nodePath, cliArgs, {
199
+ cwd,
200
+ stdio: "inherit",
201
+ shell: false,
202
+ env: buildChildEnv(process.env, {
203
+ role: descriptor.role,
204
+ controllerId,
205
+ workerId,
206
+ workerPipePath,
207
+ workerToken,
208
+ toolPolicy,
209
+ }),
210
+ });
211
+
212
+ sendAck(id, true);
213
+
214
+ childProcess.on("error", (err) => {
215
+ stopOwnerMonitor();
216
+ sendEnvelope("launch_failed", { error: err.message });
217
+ exitAfterFlush();
218
+ });
219
+
220
+ childProcess.on("spawn", () => {
221
+ sendEnvelope("child_spawned", { pid: childProcess.pid });
222
+ });
223
+
224
+ childProcess.on("exit", (code, signal) => {
225
+ stopOwnerMonitor();
226
+ isExiting = true;
227
+ sendEnvelope("child_exit", {
228
+ pid: childProcess ? childProcess.pid : 0,
229
+ code,
230
+ signal,
231
+ });
232
+
233
+ // 留出时间让退出消息刷入管道,然后退出。
234
+ // 主控要求的终止按正常退出处理:taskkill 的退出码是 1,透传会让 Windows Terminal 保留窗口;
235
+ // 真实崩溃保留非零退出码,窗口留着便于查看错误。真实退出码已在 child_exit 中上报。
236
+ setTimeout(() => {
237
+ if (socket) socket.destroy();
238
+ process.exit(terminateRequested ? 0 : (code ?? 0));
239
+ }, 300);
240
+ });
241
+ } catch (err) {
242
+ stopOwnerMonitor();
243
+ sendAck(id, false, err.message, "LAUNCH_FAILED");
244
+ sendEnvelope("launch_failed", { error: err.message });
245
+ exitAfterFlush();
246
+ }
247
+ return;
248
+ }
249
+
250
+ if (type === "terminate") {
251
+ if (!authenticated) {
252
+ sendAck(id, false, "连接尚未完成认证", "AUTH_FAILED");
253
+ return;
254
+ }
255
+ sendAck(id, true);
256
+ terminateRequested = true;
257
+ terminateChild(payload && payload.force);
258
+ if (!childProcess) exitAfterFlush(0);
259
+ return;
260
+ }
261
+ }
262
+
263
+ // 连接主控端的命名管道,带短暂重试
264
+ let retryCount = 0;
265
+ const maxRetries = 15;
266
+
267
+ function connectToController() {
268
+ socket = net.createConnection(pipePath);
269
+
270
+ const decoder = new StringDecoder("utf8");
271
+ let buffer = "";
272
+
273
+ socket.on("connect", () => {
274
+ // 发送 hello 进行 supervisor 认证
275
+ sendEnvelope("hello", {
276
+ role: "supervisor",
277
+ token: bootstrapToken,
278
+ controllerId,
279
+ workerId,
280
+ });
281
+ });
282
+
283
+ socket.on("data", (chunk) => {
284
+ buffer += decoder.write(chunk);
285
+ if (Buffer.byteLength(buffer, "utf8") > MAX_RECV_BUFFER_BYTES) {
286
+ console.error("[bootstrap] 错误: 接收缓冲超过 2 MiB 上限,停止累积并销毁连接");
287
+ try {
288
+ socket.destroy();
289
+ } catch {}
290
+ if (!childProcess) {
291
+ // 无子进程时留着一个没有通信对象的窗口没有意义
292
+ process.exit(1);
293
+ }
294
+ return;
295
+ }
296
+ let idx;
297
+ while ((idx = buffer.indexOf("\n")) !== -1) {
298
+ const line = buffer.slice(0, idx).trim();
299
+ buffer = buffer.slice(idx + 1);
300
+ if (line.length === 0) continue;
301
+ try {
302
+ const envelope = JSON.parse(line);
303
+ handleEnvelope(envelope);
304
+ } catch (err) {
305
+ console.error("[bootstrap] 消息解析异常:", err.message);
306
+ }
307
+ }
308
+ });
309
+
310
+ socket.on("error", (err) => {
311
+ if (retryCount < maxRetries && !childProcess && !isExiting) {
312
+ retryCount++;
313
+ setTimeout(connectToController, 300);
314
+ return;
315
+ }
316
+ if (!isExiting) {
317
+ console.error("[bootstrap] 管道通信错误:", err.message);
318
+ }
319
+ });
320
+
321
+ socket.on("close", () => {
322
+ if (!isExiting && childProcess && !childProcess.killed) {
323
+ // 主控端断开连接,保留当前子进程运行供人工查看,supervisor 结束监听
324
+ }
325
+ });
326
+ }
327
+
328
+ connectToController();