@linxiraos/pi-messenger 1.1.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.
- package/CHANGELOG.md +676 -0
- package/README.md +424 -0
- package/config-overlay.ts +171 -0
- package/config.ts +209 -0
- package/crew/agents/crew-plan-sync.md +64 -0
- package/crew/agents/crew-planner.md +172 -0
- package/crew/agents/crew-reviewer.md +58 -0
- package/crew/agents/crew-worker.md +129 -0
- package/crew/agents.ts +434 -0
- package/crew/handlers/coordination.ts +332 -0
- package/crew/handlers/plan.ts +906 -0
- package/crew/handlers/review.ts +333 -0
- package/crew/handlers/revise.ts +376 -0
- package/crew/handlers/status.ts +295 -0
- package/crew/handlers/sync.ts +233 -0
- package/crew/handlers/task.ts +792 -0
- package/crew/handlers/team.ts +235 -0
- package/crew/handlers/work.ts +425 -0
- package/crew/id-allocator.ts +30 -0
- package/crew/index.ts +257 -0
- package/crew/live-progress.ts +58 -0
- package/crew/lobby.ts +473 -0
- package/crew/prompt.ts +252 -0
- package/crew/registry.ts +114 -0
- package/crew/spawn.ts +111 -0
- package/crew/state-autonomous.ts +164 -0
- package/crew/state-planning.ts +263 -0
- package/crew/state.ts +20 -0
- package/crew/store.ts +642 -0
- package/crew/task-actions.ts +131 -0
- package/crew/team/store.ts +479 -0
- package/crew/team/subagent-roles.ts +87 -0
- package/crew/team/types.ts +50 -0
- package/crew/types.ts +194 -0
- package/crew/utils/artifacts.ts +62 -0
- package/crew/utils/config.ts +156 -0
- package/crew/utils/discover.ts +219 -0
- package/crew/utils/install.ts +85 -0
- package/crew/utils/progress.ts +187 -0
- package/crew/utils/result.ts +16 -0
- package/crew/utils/risk-labels.ts +7 -0
- package/crew/utils/task-format.ts +14 -0
- package/crew/utils/team-roles.ts +28 -0
- package/crew/utils/truncate.ts +79 -0
- package/crew/utils/verdict.ts +55 -0
- package/feed.ts +268 -0
- package/handlers.ts +1107 -0
- package/index.ts +1206 -0
- package/install.mjs +189 -0
- package/lib.ts +467 -0
- package/overlay-actions.ts +511 -0
- package/overlay-coordinator.ts +150 -0
- package/overlay-render.ts +676 -0
- package/overlay.ts +830 -0
- package/package.json +62 -0
- package/skills/pi-messenger-crew/SKILL.md +347 -0
- package/store.ts +1139 -0
- package/vitest.config.ts +10 -0
package/crew/agents.ts
ADDED
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Crew - Agent Spawning
|
|
3
|
+
*
|
|
4
|
+
* Spawns pi processes with progress tracking, truncation, and artifacts.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { spawn } from "node:child_process";
|
|
8
|
+
import { randomUUID } from "node:crypto";
|
|
9
|
+
import * as fs from "node:fs";
|
|
10
|
+
import * as os from "node:os";
|
|
11
|
+
import * as path from "node:path";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
import { discoverCrewAgents, type CrewAgentConfig } from "./utils/discover.ts";
|
|
14
|
+
import { truncateOutput } from "./utils/truncate.ts";
|
|
15
|
+
import {
|
|
16
|
+
createProgress,
|
|
17
|
+
parseJsonlLine,
|
|
18
|
+
updateProgress,
|
|
19
|
+
getAssistantText,
|
|
20
|
+
compactEventForArtifact,
|
|
21
|
+
getTerminalProviderError,
|
|
22
|
+
type PiEvent,
|
|
23
|
+
} from "./utils/progress.ts";
|
|
24
|
+
import {
|
|
25
|
+
getArtifactPaths,
|
|
26
|
+
ensureArtifactsDir,
|
|
27
|
+
writeArtifact,
|
|
28
|
+
writeMetadata,
|
|
29
|
+
appendJsonl
|
|
30
|
+
} from "./utils/artifacts.ts";
|
|
31
|
+
import { loadCrewConfig, getTruncationForRole, type CrewConfig } from "./utils/config.ts";
|
|
32
|
+
import { removeLiveWorker, updateLiveWorker } from "./live-progress.ts";
|
|
33
|
+
import { autonomousState, waitForConcurrencyChange } from "./state.ts";
|
|
34
|
+
import { registerWorker, unregisterWorker, killAll } from "./registry.ts";
|
|
35
|
+
import type { AgentTask, AgentResult } from "./types.ts";
|
|
36
|
+
import { generateMemorableName } from "../lib.ts";
|
|
37
|
+
|
|
38
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
39
|
+
const __dirname = path.dirname(__filename);
|
|
40
|
+
const EXTENSION_DIR = path.resolve(__dirname, "..");
|
|
41
|
+
const BUILTIN_TOOLS = new Set(["read", "bash", "edit", "write", "grep", "find", "ls"]);
|
|
42
|
+
|
|
43
|
+
export interface SpawnOptions {
|
|
44
|
+
onProgress?: (results: AgentResult[]) => void;
|
|
45
|
+
crewDir?: string;
|
|
46
|
+
signal?: AbortSignal;
|
|
47
|
+
messengerDirs?: { registry: string; inbox: string };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function shutdownAllWorkers(): void {
|
|
51
|
+
killAll();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function resolveModel(
|
|
55
|
+
taskModel?: string,
|
|
56
|
+
paramModel?: string,
|
|
57
|
+
roleModel?: string,
|
|
58
|
+
configModel?: string,
|
|
59
|
+
sessionModel?: string,
|
|
60
|
+
agentModel?: string,
|
|
61
|
+
): string | undefined {
|
|
62
|
+
return taskModel ?? paramModel ?? roleModel ?? configModel ?? sessionModel ?? agentModel;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function getPiCommand(): string {
|
|
66
|
+
return process.platform === "win32" ? "pi.cmd" : "pi";
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function pushModelArgs(args: string[], model: string): void {
|
|
70
|
+
const slashIdx = model.indexOf("/");
|
|
71
|
+
if (slashIdx !== -1) {
|
|
72
|
+
args.push("--provider", model.substring(0, slashIdx), "--model", model.substring(slashIdx + 1));
|
|
73
|
+
} else {
|
|
74
|
+
args.push("--model", model);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const THINKING_LEVELS = new Set(["off", "minimal", "low", "medium", "high", "xhigh"]);
|
|
79
|
+
|
|
80
|
+
export function resolveThinking(
|
|
81
|
+
configThinking?: string,
|
|
82
|
+
agentThinking?: string,
|
|
83
|
+
): string | undefined {
|
|
84
|
+
const resolved = configThinking ?? agentThinking;
|
|
85
|
+
if (!resolved || resolved === "off") return undefined;
|
|
86
|
+
return resolved;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function modelHasThinkingSuffix(model: string | undefined): boolean {
|
|
90
|
+
if (!model) return false;
|
|
91
|
+
const colonIdx = model.lastIndexOf(":");
|
|
92
|
+
if (colonIdx === -1) return false;
|
|
93
|
+
return THINKING_LEVELS.has(model.substring(colonIdx + 1));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function raceTimeout(promise: Promise<void>, ms: number): Promise<boolean> {
|
|
97
|
+
return new Promise<boolean>(resolve => {
|
|
98
|
+
const timer = setTimeout(() => resolve(false), ms);
|
|
99
|
+
promise.then(
|
|
100
|
+
() => {
|
|
101
|
+
clearTimeout(timer);
|
|
102
|
+
resolve(true);
|
|
103
|
+
},
|
|
104
|
+
() => {
|
|
105
|
+
clearTimeout(timer);
|
|
106
|
+
resolve(false);
|
|
107
|
+
},
|
|
108
|
+
);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function discoverWorkerName(
|
|
113
|
+
pid: number | undefined,
|
|
114
|
+
registryDir: string | undefined
|
|
115
|
+
): string | null {
|
|
116
|
+
if (!pid || !registryDir || !fs.existsSync(registryDir)) return null;
|
|
117
|
+
try {
|
|
118
|
+
for (const file of fs.readdirSync(registryDir)) {
|
|
119
|
+
if (!file.endsWith(".json")) continue;
|
|
120
|
+
const reg = JSON.parse(fs.readFileSync(path.join(registryDir, file), "utf-8"));
|
|
121
|
+
if (reg.pid === pid) return reg.name;
|
|
122
|
+
}
|
|
123
|
+
} catch {}
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const SHUTDOWN_MESSAGE = `⚠️ SHUTDOWN REQUESTED: Please wrap up your current work.
|
|
128
|
+
1. Release any file reservations
|
|
129
|
+
2. If the task is not complete, leave it as in_progress (do NOT mark done)
|
|
130
|
+
3. Do NOT commit anything
|
|
131
|
+
4. Exit`;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Spawn multiple agents in parallel with concurrency limit.
|
|
135
|
+
*/
|
|
136
|
+
export async function spawnAgents(
|
|
137
|
+
tasks: AgentTask[],
|
|
138
|
+
cwd: string,
|
|
139
|
+
options: SpawnOptions = {}
|
|
140
|
+
): Promise<AgentResult[]> {
|
|
141
|
+
const crewDir = options.crewDir ?? path.join(cwd, ".pi", "messenger", "crew");
|
|
142
|
+
const config = loadCrewConfig(crewDir);
|
|
143
|
+
const agents = discoverCrewAgents(cwd);
|
|
144
|
+
const runId = randomUUID().slice(0, 8);
|
|
145
|
+
|
|
146
|
+
// Setup artifacts directory if enabled
|
|
147
|
+
const artifactsDir = path.join(crewDir, "artifacts");
|
|
148
|
+
if (config.artifacts.enabled) {
|
|
149
|
+
ensureArtifactsDir(artifactsDir, config.artifacts.cleanupDays);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const results: AgentResult[] = [];
|
|
153
|
+
const queue = tasks.map((task, index) => ({ task, index }));
|
|
154
|
+
const running: Promise<void>[] = [];
|
|
155
|
+
|
|
156
|
+
while (queue.length > 0 || running.length > 0) {
|
|
157
|
+
if (options.signal?.aborted && running.length === 0) break;
|
|
158
|
+
|
|
159
|
+
while (running.length < autonomousState.concurrency && queue.length > 0) {
|
|
160
|
+
if (options.signal?.aborted) break;
|
|
161
|
+
const { task, index } = queue.shift()!;
|
|
162
|
+
const promise = runAgent(task, index, cwd, agents, config, runId, artifactsDir, options)
|
|
163
|
+
.then(result => {
|
|
164
|
+
results.push(result);
|
|
165
|
+
running.splice(running.indexOf(promise), 1);
|
|
166
|
+
options.onProgress?.(results);
|
|
167
|
+
});
|
|
168
|
+
running.push(promise);
|
|
169
|
+
}
|
|
170
|
+
if (running.length > 0) {
|
|
171
|
+
await Promise.race([...running, waitForConcurrencyChange()]);
|
|
172
|
+
if (options.signal?.aborted) continue;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return results;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async function runAgent(
|
|
180
|
+
task: AgentTask,
|
|
181
|
+
index: number,
|
|
182
|
+
cwd: string,
|
|
183
|
+
agents: CrewAgentConfig[],
|
|
184
|
+
config: CrewConfig,
|
|
185
|
+
runId: string,
|
|
186
|
+
artifactsDir: string,
|
|
187
|
+
options: SpawnOptions
|
|
188
|
+
): Promise<AgentResult> {
|
|
189
|
+
const agentConfig = agents.find(a => a.name === task.agent);
|
|
190
|
+
const progress = createProgress(task.agent);
|
|
191
|
+
const startTime = Date.now();
|
|
192
|
+
const workerName = generateMemorableName();
|
|
193
|
+
|
|
194
|
+
const role = agentConfig?.crewRole ?? "worker";
|
|
195
|
+
const maxOutput = task.maxOutput
|
|
196
|
+
?? agentConfig?.maxOutput
|
|
197
|
+
?? getTruncationForRole(config, role);
|
|
198
|
+
|
|
199
|
+
let artifactPaths = config.artifacts.enabled
|
|
200
|
+
? getArtifactPaths(artifactsDir, runId, task.agent, index)
|
|
201
|
+
: undefined;
|
|
202
|
+
|
|
203
|
+
if (artifactPaths) {
|
|
204
|
+
try {
|
|
205
|
+
writeArtifact(artifactPaths.inputPath, `# Task for ${task.agent}\n\n${task.task}`);
|
|
206
|
+
} catch {
|
|
207
|
+
artifactPaths = undefined;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return new Promise((resolve) => {
|
|
212
|
+
// Build args for pi command
|
|
213
|
+
const args = ["--mode", "json", "--no-session", "-p"];
|
|
214
|
+
const model = task.modelOverride ?? config.models?.[role] ?? agentConfig?.model;
|
|
215
|
+
if (model) pushModelArgs(args, model);
|
|
216
|
+
|
|
217
|
+
const thinking = resolveThinking(
|
|
218
|
+
config.thinking?.[role],
|
|
219
|
+
agentConfig?.thinking,
|
|
220
|
+
);
|
|
221
|
+
if (thinking && !modelHasThinkingSuffix(model)) {
|
|
222
|
+
args.push("--thinking", thinking);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (agentConfig?.tools?.length) {
|
|
226
|
+
const builtinTools: string[] = [];
|
|
227
|
+
const extensionPaths: string[] = [];
|
|
228
|
+
for (const tool of agentConfig.tools) {
|
|
229
|
+
if (tool.includes("/") || tool.endsWith(".ts") || tool.endsWith(".js")) {
|
|
230
|
+
extensionPaths.push(tool);
|
|
231
|
+
} else if (BUILTIN_TOOLS.has(tool)) {
|
|
232
|
+
builtinTools.push(tool);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (builtinTools.length > 0) {
|
|
237
|
+
args.push("--tools", builtinTools.join(","));
|
|
238
|
+
}
|
|
239
|
+
for (const extensionPath of extensionPaths) {
|
|
240
|
+
args.push("--extension", extensionPath);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Pass extension so workers can use pi_messenger
|
|
245
|
+
args.push("--extension", EXTENSION_DIR);
|
|
246
|
+
|
|
247
|
+
let promptTmpDir: string | null = null;
|
|
248
|
+
if (agentConfig?.systemPrompt) {
|
|
249
|
+
promptTmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-messenger-agent-"));
|
|
250
|
+
const promptPath = path.join(promptTmpDir, `${task.agent.replace(/[^\w.-]/g, "_")}.md`);
|
|
251
|
+
fs.writeFileSync(promptPath, agentConfig.systemPrompt, { mode: 0o600 });
|
|
252
|
+
args.push("--append-system-prompt", promptPath);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
args.push(task.task);
|
|
256
|
+
|
|
257
|
+
const envOverrides = config.work.env ?? {};
|
|
258
|
+
const workerFlag = role === "worker"
|
|
259
|
+
? { PI_CREW_WORKER: "1", PI_AGENT_NAME: workerName }
|
|
260
|
+
: {};
|
|
261
|
+
const env = Object.keys(envOverrides).length > 0 || role === "worker"
|
|
262
|
+
? { ...process.env, ...envOverrides, ...workerFlag }
|
|
263
|
+
: undefined;
|
|
264
|
+
|
|
265
|
+
const proc = spawn(getPiCommand(), args, {
|
|
266
|
+
cwd,
|
|
267
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
268
|
+
...(env ? { env } : {}),
|
|
269
|
+
});
|
|
270
|
+
if (task.taskId) {
|
|
271
|
+
registerWorker({ type: "worker", proc, name: workerName, cwd, taskId: task.taskId });
|
|
272
|
+
}
|
|
273
|
+
let gracefulShutdownRequested = false;
|
|
274
|
+
let discoveredWorkerName: string | null = null;
|
|
275
|
+
|
|
276
|
+
let jsonlBuffer = "";
|
|
277
|
+
let fullOutput = "";
|
|
278
|
+
let terminalProviderError: string | null = null;
|
|
279
|
+
|
|
280
|
+
proc.stdout?.on("data", (data) => {
|
|
281
|
+
try {
|
|
282
|
+
jsonlBuffer += data.toString();
|
|
283
|
+
const lines = jsonlBuffer.split("\n");
|
|
284
|
+
jsonlBuffer = lines.pop() ?? "";
|
|
285
|
+
|
|
286
|
+
for (const line of lines) {
|
|
287
|
+
const event = parseJsonlLine(line);
|
|
288
|
+
if (event) {
|
|
289
|
+
updateProgress(progress, event, startTime);
|
|
290
|
+
const assistantText = getAssistantText(event);
|
|
291
|
+
if (assistantText) fullOutput = assistantText;
|
|
292
|
+
if (artifactPaths) {
|
|
293
|
+
try { appendJsonl(artifactPaths.jsonlPath, JSON.stringify(compactEventForArtifact(event))); }
|
|
294
|
+
catch { artifactPaths = undefined; }
|
|
295
|
+
}
|
|
296
|
+
const providerError = getTerminalProviderError(event);
|
|
297
|
+
if (providerError && !terminalProviderError) {
|
|
298
|
+
terminalProviderError = providerError;
|
|
299
|
+
progress.error = providerError;
|
|
300
|
+
proc.kill("SIGTERM");
|
|
301
|
+
}
|
|
302
|
+
if (task.taskId) {
|
|
303
|
+
updateLiveWorker(cwd, task.taskId, {
|
|
304
|
+
taskId: task.taskId,
|
|
305
|
+
agent: task.agent,
|
|
306
|
+
name: workerName,
|
|
307
|
+
progress: {
|
|
308
|
+
...progress,
|
|
309
|
+
recentTools: progress.recentTools.map(tool => ({ ...tool })),
|
|
310
|
+
},
|
|
311
|
+
startedAt: startTime,
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
} catch {}
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
let stderr = "";
|
|
320
|
+
proc.stderr?.on("data", (data) => { stderr += data.toString(); });
|
|
321
|
+
|
|
322
|
+
proc.on("close", (code) => {
|
|
323
|
+
if (task.taskId) {
|
|
324
|
+
removeLiveWorker(cwd, task.taskId);
|
|
325
|
+
unregisterWorker(cwd, task.taskId);
|
|
326
|
+
}
|
|
327
|
+
const exitCode = terminalProviderError ? 1 : code ?? 1;
|
|
328
|
+
progress.status = exitCode === 0 ? "completed" : "failed";
|
|
329
|
+
progress.durationMs = Date.now() - startTime;
|
|
330
|
+
if (terminalProviderError) progress.error = terminalProviderError;
|
|
331
|
+
else if (stderr && exitCode !== 0) progress.error = stderr;
|
|
332
|
+
|
|
333
|
+
const truncation = truncateOutput(fullOutput, maxOutput, artifactPaths?.outputPath);
|
|
334
|
+
|
|
335
|
+
if (artifactPaths) {
|
|
336
|
+
try {
|
|
337
|
+
writeArtifact(artifactPaths.outputPath, fullOutput);
|
|
338
|
+
writeMetadata(artifactPaths.metadataPath, {
|
|
339
|
+
runId,
|
|
340
|
+
agent: task.agent,
|
|
341
|
+
index,
|
|
342
|
+
exitCode,
|
|
343
|
+
durationMs: progress.durationMs,
|
|
344
|
+
tokens: progress.tokens,
|
|
345
|
+
truncated: truncation.truncated,
|
|
346
|
+
error: progress.error,
|
|
347
|
+
});
|
|
348
|
+
} catch {}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
if (promptTmpDir) {
|
|
352
|
+
try { fs.rmSync(promptTmpDir, { recursive: true, force: true }); } catch {}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
resolve({
|
|
356
|
+
agent: task.agent,
|
|
357
|
+
exitCode,
|
|
358
|
+
output: truncation.text,
|
|
359
|
+
truncated: truncation.truncated,
|
|
360
|
+
progress,
|
|
361
|
+
config: agentConfig,
|
|
362
|
+
taskId: task.taskId,
|
|
363
|
+
wasGracefullyShutdown: gracefulShutdownRequested,
|
|
364
|
+
error: progress.error,
|
|
365
|
+
artifactPaths: artifactPaths ? {
|
|
366
|
+
input: artifactPaths.inputPath,
|
|
367
|
+
output: artifactPaths.outputPath,
|
|
368
|
+
jsonl: artifactPaths.jsonlPath,
|
|
369
|
+
metadata: artifactPaths.metadataPath,
|
|
370
|
+
} : undefined,
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
if (gracefulShutdownRequested && discoveredWorkerName && options.messengerDirs?.registry) {
|
|
374
|
+
try {
|
|
375
|
+
fs.unlinkSync(path.join(options.messengerDirs.registry, `${discoveredWorkerName}.json`));
|
|
376
|
+
} catch {}
|
|
377
|
+
}
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
// Handle abort signal
|
|
381
|
+
if (options.signal) {
|
|
382
|
+
const gracefulShutdown = async () => {
|
|
383
|
+
gracefulShutdownRequested = true;
|
|
384
|
+
|
|
385
|
+
let messageSent = false;
|
|
386
|
+
discoveredWorkerName = discoverWorkerName(proc.pid, options.messengerDirs?.registry);
|
|
387
|
+
if (discoveredWorkerName && options.messengerDirs) {
|
|
388
|
+
try {
|
|
389
|
+
const inboxDir = path.join(options.messengerDirs.inbox, discoveredWorkerName);
|
|
390
|
+
if (fs.existsSync(inboxDir)) {
|
|
391
|
+
const msgFile = path.join(inboxDir, `${Date.now()}-shutdown.json`);
|
|
392
|
+
fs.writeFileSync(msgFile, JSON.stringify({
|
|
393
|
+
id: randomUUID(),
|
|
394
|
+
from: "crew-orchestrator",
|
|
395
|
+
to: discoveredWorkerName,
|
|
396
|
+
text: SHUTDOWN_MESSAGE,
|
|
397
|
+
timestamp: new Date().toISOString(),
|
|
398
|
+
replyTo: null,
|
|
399
|
+
}));
|
|
400
|
+
messageSent = true;
|
|
401
|
+
}
|
|
402
|
+
} catch {}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
if (messageSent) {
|
|
406
|
+
const graceMs = config.work.shutdownGracePeriodMs ?? 30000;
|
|
407
|
+
const exitPromise = new Promise<void>(r => proc.once("exit", () => r()));
|
|
408
|
+
const exited = await raceTimeout(exitPromise, graceMs);
|
|
409
|
+
if (exited) return;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (!proc.killed && proc.exitCode === null) {
|
|
413
|
+
proc.kill("SIGTERM");
|
|
414
|
+
const termPromise = new Promise<void>(r => proc.once("exit", () => r()));
|
|
415
|
+
const killed = await raceTimeout(termPromise, 5000);
|
|
416
|
+
if (killed) return;
|
|
417
|
+
} else {
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if (proc.exitCode === null) {
|
|
422
|
+
proc.kill("SIGKILL");
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
if (options.signal.aborted) {
|
|
426
|
+
gracefulShutdown().catch(() => {});
|
|
427
|
+
} else {
|
|
428
|
+
options.signal.addEventListener("abort", () => {
|
|
429
|
+
gracefulShutdown().catch(() => {});
|
|
430
|
+
}, { once: true });
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
}
|