@sorbetai/tools-node 0.0.0-stage → 0.1.0-alpha.17
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/LICENSE.pi +21 -0
- package/README.md +358 -2
- package/dist/environment/index.d.ts +2 -0
- package/dist/environment/index.js +4 -0
- package/dist/environment/process-worker.d.ts +1 -0
- package/dist/environment/process-worker.js +46 -0
- package/dist/environment/process-worker.js.map +1 -0
- package/dist/environment-CtRyPVb9.js +1222 -0
- package/dist/environment-CtRyPVb9.js.map +1 -0
- package/dist/index-B3RbI0Wh.d.ts +385 -0
- package/dist/index.d.ts +549 -0
- package/dist/index.js +3314 -0
- package/dist/index.js.map +1 -0
- package/dist/plugin/LICENSE.pi-schema.typebox +23 -0
- package/dist/plugin/workspace-component.js +2253 -0
- package/dist/supervised-command-DNcHAKrD.js +813 -0
- package/dist/supervised-command-DNcHAKrD.js.map +1 -0
- package/package.json +53 -3
|
@@ -0,0 +1,1222 @@
|
|
|
1
|
+
import { i as windowsJobName, n as cleanupWindowsJob, r as probeWindowsJob } from "./supervised-command-DNcHAKrD.js";
|
|
2
|
+
import * as s from "@earendil-works/pi-schema";
|
|
3
|
+
import { check, decode } from "@sorbetai/schema";
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
import { closeSync, constants, createReadStream, existsSync, fsyncSync, mkdtempSync, openSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { lstat, mkdir, mkdtemp, open, readFile, readdir, realpath, rename, rm, writeFile } from "node:fs/promises";
|
|
7
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
8
|
+
import { isError } from "es-toolkit";
|
|
9
|
+
import toError from "ensure-error";
|
|
10
|
+
import { tmpdir } from "node:os";
|
|
11
|
+
import { createInterface } from "node:readline";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
import { StringDecoder } from "node:string_decoder";
|
|
14
|
+
import { execa } from "execa";
|
|
15
|
+
import { fork } from "node:child_process";
|
|
16
|
+
import { setTimeout as setTimeout$1 } from "node:timers/promises";
|
|
17
|
+
import * as Context from "effect/Context";
|
|
18
|
+
import * as Effect from "effect/Effect";
|
|
19
|
+
import * as Exit from "effect/Exit";
|
|
20
|
+
import * as Scope from "effect/Scope";
|
|
21
|
+
import * as Semaphore from "effect/Semaphore";
|
|
22
|
+
|
|
23
|
+
//#region src/environment/contracts.ts
|
|
24
|
+
function ok(value) {
|
|
25
|
+
return {
|
|
26
|
+
ok: true,
|
|
27
|
+
value
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function err(error) {
|
|
31
|
+
return {
|
|
32
|
+
ok: false,
|
|
33
|
+
error
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function getOrThrow(result) {
|
|
37
|
+
if (!result.ok) throw result.error;
|
|
38
|
+
return result.value;
|
|
39
|
+
}
|
|
40
|
+
var FileError = class extends Error {
|
|
41
|
+
code;
|
|
42
|
+
path;
|
|
43
|
+
constructor(code, message, path, cause) {
|
|
44
|
+
super(message, { cause });
|
|
45
|
+
this.name = "FileError";
|
|
46
|
+
this.code = code;
|
|
47
|
+
this.path = path;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
var ExecutionError = class extends Error {
|
|
51
|
+
code;
|
|
52
|
+
/** Captured output remains inspectable after cancellation or execution failure.
|
|
53
|
+
* A capture failure may leave its artifact incomplete; the error still takes precedence.
|
|
54
|
+
*/
|
|
55
|
+
output;
|
|
56
|
+
constructor(code, message, cause, output) {
|
|
57
|
+
super(message, { cause });
|
|
58
|
+
this.name = "ExecutionError";
|
|
59
|
+
this.code = code;
|
|
60
|
+
this.output = output;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/environment/process-emergency.ts
|
|
66
|
+
const Pid = s.int({ minimum: 1 });
|
|
67
|
+
const MemberSchema = s.obj({
|
|
68
|
+
pid: Pid,
|
|
69
|
+
pgid: Pid,
|
|
70
|
+
startIdentity: s.str(),
|
|
71
|
+
zombie: s.bool()
|
|
72
|
+
}, { open: false });
|
|
73
|
+
const IdentitySchema = s.obj({
|
|
74
|
+
version: s.lit(1),
|
|
75
|
+
platform: s.union([s.lit("linux"), s.lit("darwin")]),
|
|
76
|
+
bootSession: s.str(),
|
|
77
|
+
workerNonce: s.str(),
|
|
78
|
+
state: s.union([s.lit("group"), s.lit("gone")]),
|
|
79
|
+
pid: Pid,
|
|
80
|
+
members: s.arr(MemberSchema)
|
|
81
|
+
}, { open: false });
|
|
82
|
+
const WindowsIdentitySchema = s.obj({
|
|
83
|
+
version: s.lit(1),
|
|
84
|
+
platform: s.lit("win32"),
|
|
85
|
+
workerNonce: s.str({ pattern: "^[a-zA-Z0-9-]{1,128}$" })
|
|
86
|
+
}, { open: false });
|
|
87
|
+
const WINDOWS_IDENTITY_PREFIX = "sorbet-job-v1:";
|
|
88
|
+
const IDENTITY_PREFIX = "sorbet-process-v1:";
|
|
89
|
+
const MAX_IDENTITY_BYTES = 1024 * 1024;
|
|
90
|
+
const missingProcessCodes = new Set(["ENOENT", "ESRCH"]);
|
|
91
|
+
const SUPPORTED_PLATFORMS = new Set(["linux", "darwin"]);
|
|
92
|
+
const INSPECTION_TIMEOUT_MS = 1e3;
|
|
93
|
+
const DRAIN_TIMEOUT_MS = 2e3;
|
|
94
|
+
/** Only inspect live OS state; neither command text nor environment is collected.
|
|
95
|
+
* Linux uses kernel start ticks; Darwin's ps lstart has one-second resolution.
|
|
96
|
+
* This is verification immediately before signaling, not an atomic pidfd operation.
|
|
97
|
+
*/
|
|
98
|
+
async function groupMembers(pgid) {
|
|
99
|
+
if (!SUPPORTED_PLATFORMS.has(process.platform)) throw new Error("Emergency process cleanup is unsupported on this platform");
|
|
100
|
+
const inspectionSignal = AbortSignal.timeout(INSPECTION_TIMEOUT_MS);
|
|
101
|
+
const result = await execa("/bin/ps", ["-axo", "pid=,ppid=,pgid=,stat=,lstart="], {
|
|
102
|
+
env: {
|
|
103
|
+
LC_ALL: "C",
|
|
104
|
+
TZ: "UTC"
|
|
105
|
+
},
|
|
106
|
+
timeout: INSPECTION_TIMEOUT_MS,
|
|
107
|
+
cancelSignal: inspectionSignal,
|
|
108
|
+
forceKillAfterDelay: 100,
|
|
109
|
+
maxBuffer: 1024 * 1024
|
|
110
|
+
});
|
|
111
|
+
const members = [];
|
|
112
|
+
for (const line of result.stdout.split("\n")) {
|
|
113
|
+
const match = /^\s*(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(.+?)\s*$/.exec(line);
|
|
114
|
+
if (match === null || Number(match[3]) !== pgid) continue;
|
|
115
|
+
const pid = Number(match[1]);
|
|
116
|
+
let startIdentity = match[5] ?? "";
|
|
117
|
+
let zombie = match[4]?.startsWith("Z") ?? false;
|
|
118
|
+
if (process.platform === "linux") {
|
|
119
|
+
let stat$1;
|
|
120
|
+
try {
|
|
121
|
+
stat$1 = await readFile(`/proc/${pid}/stat`, {
|
|
122
|
+
encoding: "utf8",
|
|
123
|
+
signal: inspectionSignal
|
|
124
|
+
});
|
|
125
|
+
} catch (cause) {
|
|
126
|
+
if (!isError(cause) || !missingProcessCodes.has(Reflect.get(cause, "code"))) throw cause;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
const fields = stat$1.slice(stat$1.lastIndexOf(")") + 2).trim().split(/\s+/);
|
|
130
|
+
if (Number(fields[2]) !== pgid || fields[19] === void 0) continue;
|
|
131
|
+
startIdentity = fields[19];
|
|
132
|
+
zombie = fields[0] === "Z";
|
|
133
|
+
}
|
|
134
|
+
if (startIdentity) members.push({
|
|
135
|
+
pid,
|
|
136
|
+
pgid,
|
|
137
|
+
startIdentity,
|
|
138
|
+
zombie
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return members;
|
|
142
|
+
}
|
|
143
|
+
function matches(member, known) {
|
|
144
|
+
return known.some((identity) => identity.pid === member.pid && identity.pgid === member.pgid && identity.startIdentity === member.startIdentity);
|
|
145
|
+
}
|
|
146
|
+
/** Fail before dispatch when this host cannot support verified emergency cleanup. */
|
|
147
|
+
async function verifyProcessInspection() {
|
|
148
|
+
if (process.platform === "win32") {
|
|
149
|
+
await probeWindowsJob();
|
|
150
|
+
return "windows-job";
|
|
151
|
+
}
|
|
152
|
+
const bootSession = await readBootSession();
|
|
153
|
+
await groupMembers(process.pid);
|
|
154
|
+
if (process.platform === "linux") await readFile("/proc/self/stat", "utf8");
|
|
155
|
+
return bootSession;
|
|
156
|
+
}
|
|
157
|
+
async function readBootSession() {
|
|
158
|
+
if (!SUPPORTED_PLATFORMS.has(process.platform)) throw new ExecutionError("spawn_error", "Managed process supervision is unsupported on this platform; requires Linux or macOS");
|
|
159
|
+
try {
|
|
160
|
+
return (process.platform === "linux" ? await readFile("/proc/sys/kernel/random/boot_id", {
|
|
161
|
+
encoding: "utf8",
|
|
162
|
+
signal: AbortSignal.timeout(INSPECTION_TIMEOUT_MS)
|
|
163
|
+
}) : (await execa("/usr/sbin/sysctl", ["-n", "kern.bootsessionuuid"], {
|
|
164
|
+
timeout: INSPECTION_TIMEOUT_MS,
|
|
165
|
+
forceKillAfterDelay: 100,
|
|
166
|
+
maxBuffer: 1024
|
|
167
|
+
})).stdout).trim().toLowerCase();
|
|
168
|
+
} catch {
|
|
169
|
+
throw new ExecutionError("spawn_error", "Managed process boot identity could not be inspected");
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/** Called before exposing onStarted. An empty snapshot proves the group is gone;
|
|
173
|
+
* inspection failures and surviving groups without a verified leader fail closed.
|
|
174
|
+
*/
|
|
175
|
+
async function captureCommandGroup(pid, workerNonce) {
|
|
176
|
+
if (process.platform === "win32") {
|
|
177
|
+
if (!workerNonce || !check(WindowsIdentitySchema, {
|
|
178
|
+
version: 1,
|
|
179
|
+
platform: "win32",
|
|
180
|
+
workerNonce
|
|
181
|
+
})) throw new ExecutionError("spawn_error", "Invalid Windows Job identity");
|
|
182
|
+
return {
|
|
183
|
+
pid,
|
|
184
|
+
members: [],
|
|
185
|
+
windowsJob: windowsJobName(workerNonce)
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
let members = await groupMembers(pid);
|
|
189
|
+
if (members.some((member) => !member.zombie) && !members.some((member) => member.pid === pid && !member.zombie)) {
|
|
190
|
+
await setTimeout$1(25);
|
|
191
|
+
members = await groupMembers(pid);
|
|
192
|
+
}
|
|
193
|
+
if (!members.some((member) => !member.zombie)) return {
|
|
194
|
+
pid,
|
|
195
|
+
members: []
|
|
196
|
+
};
|
|
197
|
+
if (!members.some((member) => member.pid === pid && !member.zombie)) throw new ExecutionError("spawn_error", "Command process identity could not be verified");
|
|
198
|
+
return {
|
|
199
|
+
pid,
|
|
200
|
+
members
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
function encodeProcessIdentity(identity, workerNonce, bootSession) {
|
|
204
|
+
if (identity.windowsJob !== void 0) return WINDOWS_IDENTITY_PREFIX + JSON.stringify({
|
|
205
|
+
version: 1,
|
|
206
|
+
platform: "win32",
|
|
207
|
+
workerNonce
|
|
208
|
+
});
|
|
209
|
+
const value = {
|
|
210
|
+
version: 1,
|
|
211
|
+
platform: process.platform,
|
|
212
|
+
bootSession,
|
|
213
|
+
workerNonce,
|
|
214
|
+
state: identity.members.length === 0 ? "gone" : "group",
|
|
215
|
+
pid: identity.pid,
|
|
216
|
+
members: identity.members
|
|
217
|
+
};
|
|
218
|
+
const token = IDENTITY_PREFIX + JSON.stringify(value);
|
|
219
|
+
if (Buffer.byteLength(token) > MAX_IDENTITY_BYTES) throw new ExecutionError("spawn_error", "Command process identity exceeds size limit");
|
|
220
|
+
return token;
|
|
221
|
+
}
|
|
222
|
+
/** Reconcile a trusted local task record before writing restart settlement.
|
|
223
|
+
* Resolves after verified drain, proven absence/reuse, or a different boot session.
|
|
224
|
+
* Throws ExecutionError for malformed/unsupported identities, failed inspection,
|
|
225
|
+
* ambiguous ownership, or failed bounded drain. Never pass model-supplied tokens.
|
|
226
|
+
* Linux kernel ticks and macOS second-resolution start identities are checked
|
|
227
|
+
* before group signals; races and descendants escaping their group remain possible.
|
|
228
|
+
*/
|
|
229
|
+
async function settleAbandonedProcess(processIdentity) {
|
|
230
|
+
if (processIdentity.startsWith(WINDOWS_IDENTITY_PREFIX)) {
|
|
231
|
+
let value;
|
|
232
|
+
try {
|
|
233
|
+
value = JSON.parse(processIdentity.slice(14));
|
|
234
|
+
} catch {
|
|
235
|
+
throw new ExecutionError("spawn_error", "Invalid managed process identity");
|
|
236
|
+
}
|
|
237
|
+
if (process.platform !== "win32" || !check(WindowsIdentitySchema, value)) throw new ExecutionError("spawn_error", "Invalid managed process identity");
|
|
238
|
+
if (!await cleanupWindowsJob(windowsJobName(value.workerNonce))) throw new ExecutionError("spawn_error", "Abandoned command Job cleanup could not be verified");
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
let decoded;
|
|
242
|
+
try {
|
|
243
|
+
if (Buffer.byteLength(processIdentity) > MAX_IDENTITY_BYTES || !processIdentity.startsWith(IDENTITY_PREFIX)) throw new Error("Invalid identity");
|
|
244
|
+
decoded = JSON.parse(processIdentity.slice(18));
|
|
245
|
+
} catch {
|
|
246
|
+
throw new ExecutionError("spawn_error", "Invalid managed process identity");
|
|
247
|
+
}
|
|
248
|
+
if (!check(IdentitySchema, decoded) || decoded.members.some((member) => member.pgid !== decoded.pid) || (decoded.state === "gone" ? decoded.members.length !== 0 : !decoded.members.some((member) => member.pid === decoded.pid))) throw new ExecutionError("spawn_error", "Invalid managed process identity");
|
|
249
|
+
const bootSession = await readBootSession();
|
|
250
|
+
if (decoded.platform !== process.platform) throw new ExecutionError("spawn_error", "Managed process identity belongs to a different platform");
|
|
251
|
+
if (decoded.bootSession !== bootSession || decoded.state === "gone") return;
|
|
252
|
+
const cleanup = await cleanupCommandGroup(decoded, 1e3);
|
|
253
|
+
if (!cleanup.drained && !cleanup.reused) throw new ExecutionError("spawn_error", "Abandoned command group cleanup could not be verified");
|
|
254
|
+
}
|
|
255
|
+
function groupWasReused(identity, current) {
|
|
256
|
+
const previousLeader = identity.members.find((member) => member.pid === identity.pid);
|
|
257
|
+
const currentLeader = current.find((member) => member.pid === identity.pid);
|
|
258
|
+
return previousLeader !== void 0 && currentLeader !== void 0 && previousLeader.startIdentity !== currentLeader.startIdentity;
|
|
259
|
+
}
|
|
260
|
+
/** Emergency fallback only: Execa normally owns cancellation inside the worker.
|
|
261
|
+
* Its handle is unavailable after worker death, so revalidate a known surviving
|
|
262
|
+
* group member before each signal. An unknown/reused group is never signaled.
|
|
263
|
+
*/
|
|
264
|
+
async function cleanupCommandGroup(identity, graceMs) {
|
|
265
|
+
if (identity === void 0) return {
|
|
266
|
+
forced: false,
|
|
267
|
+
drained: false
|
|
268
|
+
};
|
|
269
|
+
if (identity.windowsJob !== void 0) try {
|
|
270
|
+
return {
|
|
271
|
+
forced: true,
|
|
272
|
+
drained: await cleanupWindowsJob(identity.windowsJob)
|
|
273
|
+
};
|
|
274
|
+
} catch {
|
|
275
|
+
return {
|
|
276
|
+
forced: false,
|
|
277
|
+
drained: false
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
let known = identity.members;
|
|
281
|
+
let forced = false;
|
|
282
|
+
let signal;
|
|
283
|
+
try {
|
|
284
|
+
const initial = await groupMembers(identity.pid);
|
|
285
|
+
if (!initial.some((member) => !member.zombie)) return {
|
|
286
|
+
forced,
|
|
287
|
+
drained: true
|
|
288
|
+
};
|
|
289
|
+
if (!initial.some((member) => matches(member, known))) return {
|
|
290
|
+
forced,
|
|
291
|
+
drained: false,
|
|
292
|
+
reused: groupWasReused(identity, initial)
|
|
293
|
+
};
|
|
294
|
+
known = initial;
|
|
295
|
+
process.kill(-identity.pid, "SIGTERM");
|
|
296
|
+
signal = "SIGTERM";
|
|
297
|
+
const forceAt = performance.now() + Math.min(graceMs, DRAIN_TIMEOUT_MS);
|
|
298
|
+
const deadline = forceAt + DRAIN_TIMEOUT_MS;
|
|
299
|
+
while (performance.now() < deadline) {
|
|
300
|
+
const current = await groupMembers(identity.pid);
|
|
301
|
+
if (!current.some((member) => !member.zombie)) return {
|
|
302
|
+
forced,
|
|
303
|
+
drained: true,
|
|
304
|
+
signal
|
|
305
|
+
};
|
|
306
|
+
if (!current.some((member) => matches(member, known))) return {
|
|
307
|
+
forced,
|
|
308
|
+
drained: false,
|
|
309
|
+
signal,
|
|
310
|
+
reused: groupWasReused(identity, current)
|
|
311
|
+
};
|
|
312
|
+
known = current;
|
|
313
|
+
if (!forced && performance.now() >= forceAt) {
|
|
314
|
+
process.kill(-identity.pid, "SIGKILL");
|
|
315
|
+
signal = "SIGKILL";
|
|
316
|
+
forced = true;
|
|
317
|
+
}
|
|
318
|
+
await setTimeout$1(25);
|
|
319
|
+
}
|
|
320
|
+
} catch {}
|
|
321
|
+
return {
|
|
322
|
+
forced,
|
|
323
|
+
drained: false,
|
|
324
|
+
signal
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
//#endregion
|
|
329
|
+
//#region src/environment/process-output.ts
|
|
330
|
+
const PreviewBytes = 50 * 1024;
|
|
331
|
+
/** Callback consumers own their full capture; direct callers receive a retained artifact. */
|
|
332
|
+
var ProcessOutput = class {
|
|
333
|
+
#retain;
|
|
334
|
+
#previewBytes;
|
|
335
|
+
#streams = {
|
|
336
|
+
stdout: "",
|
|
337
|
+
stderr: ""
|
|
338
|
+
};
|
|
339
|
+
#chunks = [];
|
|
340
|
+
#bytes = 0;
|
|
341
|
+
#fd;
|
|
342
|
+
#path;
|
|
343
|
+
constructor(retain, previewBytes = PreviewBytes) {
|
|
344
|
+
this.#retain = retain;
|
|
345
|
+
this.#previewBytes = previewBytes;
|
|
346
|
+
}
|
|
347
|
+
append(stream, text) {
|
|
348
|
+
if (!text) return;
|
|
349
|
+
const bytes = Buffer.from(text);
|
|
350
|
+
this.#bytes += bytes.length;
|
|
351
|
+
if (this.#retain) {
|
|
352
|
+
if (this.#fd === void 0 && this.#bytes > this.#previewBytes) {
|
|
353
|
+
this.#path = join(mkdtempSync(join(tmpdir(), "sorbet-process-")), "output.log");
|
|
354
|
+
this.#fd = openSync(this.#path, "wx", 384);
|
|
355
|
+
for (const chunk of this.#chunks) writeFileSync(this.#fd, chunk);
|
|
356
|
+
this.#chunks = [];
|
|
357
|
+
}
|
|
358
|
+
if (this.#fd !== void 0) writeFileSync(this.#fd, bytes);
|
|
359
|
+
else this.#chunks.push(bytes);
|
|
360
|
+
}
|
|
361
|
+
const preview = Buffer.from(this.#streams[stream] + text);
|
|
362
|
+
let offset = Math.max(0, preview.length - this.#previewBytes);
|
|
363
|
+
while (offset < preview.length && (preview[offset] & 192) === 128) offset++;
|
|
364
|
+
this.#streams[stream] = preview.subarray(offset).toString("utf8");
|
|
365
|
+
}
|
|
366
|
+
snapshot() {
|
|
367
|
+
const truncated = this.#bytes > Buffer.byteLength(this.#streams.stdout) + Buffer.byteLength(this.#streams.stderr);
|
|
368
|
+
if (!truncated && this.#path === void 0) return { ...this.#streams };
|
|
369
|
+
return {
|
|
370
|
+
...this.#streams,
|
|
371
|
+
outputBytes: this.#bytes,
|
|
372
|
+
outputTruncated: truncated,
|
|
373
|
+
outputReference: this.#path
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
close() {
|
|
377
|
+
if (this.#fd === void 0) return;
|
|
378
|
+
const fd = this.#fd;
|
|
379
|
+
this.#fd = void 0;
|
|
380
|
+
try {
|
|
381
|
+
fsyncSync(fd);
|
|
382
|
+
} finally {
|
|
383
|
+
closeSync(fd);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
//#endregion
|
|
389
|
+
//#region src/environment/managed-process.ts
|
|
390
|
+
const StartupErrorCodes = new Set([
|
|
391
|
+
"MODULE_NOT_FOUND",
|
|
392
|
+
"ERR_MODULE_NOT_FOUND",
|
|
393
|
+
"ERR_PACKAGE_PATH_NOT_EXPORTED",
|
|
394
|
+
"ERR_PACKAGE_IMPORT_NOT_DEFINED",
|
|
395
|
+
"ERR_REQUIRE_ESM",
|
|
396
|
+
"ERR_UNKNOWN_FILE_EXTENSION",
|
|
397
|
+
"ERR_DLOPEN_FAILED"
|
|
398
|
+
]);
|
|
399
|
+
async function executeManagedCommand(command, options, outputRedactor) {
|
|
400
|
+
const adjacent = new URL("./process-worker.js", import.meta.url);
|
|
401
|
+
const bundled = new URL("./environment/process-worker.js", import.meta.url);
|
|
402
|
+
let workerPath = adjacent;
|
|
403
|
+
if (!existsSync(workerPath)) workerPath = bundled;
|
|
404
|
+
if (!existsSync(workerPath)) workerPath = new URL("../../dist/environment/process-worker.js", import.meta.url);
|
|
405
|
+
return executeProcessWorker(workerPath, command, options, outputRedactor);
|
|
406
|
+
}
|
|
407
|
+
/** The platform cannot provide verified process-tree supervision here. Callers
|
|
408
|
+
* may use a foreground executor when the command never reached dispatch.
|
|
409
|
+
*/
|
|
410
|
+
var SupervisionUnavailableError = class extends Error {
|
|
411
|
+
constructor(cause) {
|
|
412
|
+
super("Managed process supervision is unavailable", { cause });
|
|
413
|
+
this.name = "SupervisionUnavailableError";
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
/** The worker must finish resource-free bootstrap and send ready before accepting
|
|
417
|
+
* a request. Command and Sandbox resource acquisition starts only after dispatch.
|
|
418
|
+
*/
|
|
419
|
+
async function executeProcessWorker(workerPath, command, options, outputRedactor) {
|
|
420
|
+
let bootSession;
|
|
421
|
+
try {
|
|
422
|
+
bootSession = await verifyProcessInspection();
|
|
423
|
+
} catch (cause) {
|
|
424
|
+
return err(new ExecutionError("spawn_error", "Background supervision is unavailable on this platform", new SupervisionUnavailableError(toError(cause))));
|
|
425
|
+
}
|
|
426
|
+
if (options.abortSignal?.aborted) return err(new ExecutionError("aborted", "Command aborted"));
|
|
427
|
+
return new Promise((resolve$1) => {
|
|
428
|
+
let failure;
|
|
429
|
+
let exited;
|
|
430
|
+
let settled = false;
|
|
431
|
+
let dispatched = false;
|
|
432
|
+
let startupStderr = Buffer.alloc(0);
|
|
433
|
+
let startupLoaderCode;
|
|
434
|
+
let cancellationWatchdog;
|
|
435
|
+
let identity;
|
|
436
|
+
let notifications = Promise.resolve();
|
|
437
|
+
let commandPid;
|
|
438
|
+
let workerNonce;
|
|
439
|
+
let commandStartedAt = Date.now();
|
|
440
|
+
const output = new ProcessOutput(options.onStdout === void 0 || options.onStderr === void 0);
|
|
441
|
+
let outputFlushed = false;
|
|
442
|
+
const flushOutput = () => {
|
|
443
|
+
if (outputFlushed) return;
|
|
444
|
+
outputFlushed = true;
|
|
445
|
+
for (const stream of ["stdout", "stderr"]) {
|
|
446
|
+
const tail = outputRedactor?.transform(stream, "", true) ?? "";
|
|
447
|
+
if (!tail) continue;
|
|
448
|
+
output.append(stream, tail);
|
|
449
|
+
if (stream === "stdout") options.onStdout?.(tail);
|
|
450
|
+
else options.onStderr?.(tail);
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
const startedAt = Date.now();
|
|
454
|
+
const worker = fork(workerPath, [], {
|
|
455
|
+
detached: true,
|
|
456
|
+
stdio: [
|
|
457
|
+
"ignore",
|
|
458
|
+
"ignore",
|
|
459
|
+
"pipe",
|
|
460
|
+
"ipc"
|
|
461
|
+
],
|
|
462
|
+
execArgv: []
|
|
463
|
+
});
|
|
464
|
+
const closed = new Promise((resolveClosed) => {
|
|
465
|
+
worker.once("close", () => resolveClosed());
|
|
466
|
+
});
|
|
467
|
+
worker.stderr?.on("data", (chunk) => {
|
|
468
|
+
if (!dispatched) {
|
|
469
|
+
const diagnostic = Buffer.concat([startupStderr, chunk]);
|
|
470
|
+
const codes = diagnostic.toString("utf8").match(/\b[A-Z][A-Z_]+\b/g) ?? [];
|
|
471
|
+
startupLoaderCode ??= codes.find((candidate) => StartupErrorCodes.has(candidate));
|
|
472
|
+
startupStderr = diagnostic.subarray(-8192);
|
|
473
|
+
}
|
|
474
|
+
});
|
|
475
|
+
const cancel = () => {
|
|
476
|
+
if (settled) return;
|
|
477
|
+
if (worker.connected) worker.send("cancel", () => {});
|
|
478
|
+
cancellationWatchdog ??= setTimeout(() => worker.kill("SIGKILL"), (options.lifecycle?.forceKillAfterDelayMs ?? 1e3) + 2e3);
|
|
479
|
+
};
|
|
480
|
+
const startupWatchdog = setTimeout(() => {
|
|
481
|
+
failure ??= new ExecutionError("spawn_error", "Command execution worker did not become ready");
|
|
482
|
+
cancel();
|
|
483
|
+
}, 1e4);
|
|
484
|
+
const callback = (run) => {
|
|
485
|
+
try {
|
|
486
|
+
run();
|
|
487
|
+
} catch (cause) {
|
|
488
|
+
const error = toError(cause);
|
|
489
|
+
failure ??= new ExecutionError("callback_error", error.message, error);
|
|
490
|
+
cancel();
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
const finish = async (code = null, signal = null) => {
|
|
494
|
+
if (settled) return;
|
|
495
|
+
settled = true;
|
|
496
|
+
clearTimeout(cancellationWatchdog);
|
|
497
|
+
clearTimeout(startupWatchdog);
|
|
498
|
+
await notifications;
|
|
499
|
+
callback(flushOutput);
|
|
500
|
+
options.abortSignal?.removeEventListener("abort", cancel);
|
|
501
|
+
if (exited === void 0) {
|
|
502
|
+
const cleanup = dispatched ? await cleanupCommandGroup(identity, options.lifecycle?.forceKillAfterDelayMs ?? 1e3) : {
|
|
503
|
+
forced: false,
|
|
504
|
+
drained: true,
|
|
505
|
+
signal: void 0
|
|
506
|
+
};
|
|
507
|
+
const cleanupStatus = cleanup.drained ? "command group drained" : "command group cleanup could not be verified";
|
|
508
|
+
if (!dispatched) failure ??= new ExecutionError(options.abortSignal?.aborted ? "aborted" : "spawn_error", `Command execution failed before dispatch (worker=${workerPath.pathname}, exitCode=${code}, signal=${signal}, loader=${startupLoaderCode ?? "unavailable"})`);
|
|
509
|
+
failure ??= new ExecutionError("spawn_error", `Process worker exited without a terminal report (exitCode=${code}, signal=${signal}; ${cleanupStatus})`);
|
|
510
|
+
exited = {
|
|
511
|
+
workerNonce: workerNonce ?? "worker-exited-before-start",
|
|
512
|
+
outputBytes: 0,
|
|
513
|
+
pid: commandPid,
|
|
514
|
+
workerPid: worker.pid ?? 0,
|
|
515
|
+
startedAt: commandPid === void 0 ? startedAt : commandStartedAt,
|
|
516
|
+
exitedAt: Date.now(),
|
|
517
|
+
signal: cleanup.signal,
|
|
518
|
+
forced: cleanup.forced,
|
|
519
|
+
truncated: false,
|
|
520
|
+
cleanupVerified: cleanup.drained,
|
|
521
|
+
dispatched,
|
|
522
|
+
...output.snapshot(),
|
|
523
|
+
errorCode: failure.code
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
const result = exited;
|
|
527
|
+
try {
|
|
528
|
+
output.close();
|
|
529
|
+
} catch (cause) {
|
|
530
|
+
const error = toError(cause);
|
|
531
|
+
failure = new ExecutionError("callback_error", error.message, error);
|
|
532
|
+
}
|
|
533
|
+
callback(() => options.lifecycle?.onExited?.({
|
|
534
|
+
...result,
|
|
535
|
+
errorCode: failure?.code ?? result.errorCode
|
|
536
|
+
}));
|
|
537
|
+
if (failure !== void 0) resolve$1(err(new ExecutionError(failure.code, failure.message, failure.cause === void 0 ? void 0 : toError(failure.cause), output.snapshot())));
|
|
538
|
+
else resolve$1(ok({
|
|
539
|
+
...output.snapshot(),
|
|
540
|
+
exitCode: result.exitCode ?? 1
|
|
541
|
+
}));
|
|
542
|
+
};
|
|
543
|
+
worker.on("message", (message) => {
|
|
544
|
+
notifications = notifications.then(async () => {
|
|
545
|
+
switch (message.kind) {
|
|
546
|
+
case "ready":
|
|
547
|
+
clearTimeout(startupWatchdog);
|
|
548
|
+
if (dispatched || settled || options.abortSignal?.aborted || failure !== void 0) break;
|
|
549
|
+
try {
|
|
550
|
+
await Promise.race([options.beforeStart?.(), closed]);
|
|
551
|
+
options.abortSignal?.throwIfAborted();
|
|
552
|
+
} catch (cause) {
|
|
553
|
+
if (settled) break;
|
|
554
|
+
const error = toError(cause);
|
|
555
|
+
failure ??= new ExecutionError(options.abortSignal?.aborted ? "aborted" : "spawn_error", error.message, error);
|
|
556
|
+
cancel();
|
|
557
|
+
break;
|
|
558
|
+
}
|
|
559
|
+
if (settled || failure !== void 0) break;
|
|
560
|
+
dispatched = true;
|
|
561
|
+
startupStderr = Buffer.alloc(0);
|
|
562
|
+
worker.send(command, (cause) => {
|
|
563
|
+
if (cause) {
|
|
564
|
+
failure ??= new ExecutionError("spawn_error", "Process worker request could not be delivered");
|
|
565
|
+
cancel();
|
|
566
|
+
}
|
|
567
|
+
});
|
|
568
|
+
break;
|
|
569
|
+
case "started":
|
|
570
|
+
commandPid = message.pid;
|
|
571
|
+
workerNonce = message.workerNonce;
|
|
572
|
+
commandStartedAt = message.startedAt;
|
|
573
|
+
try {
|
|
574
|
+
identity = await captureCommandGroup(message.pid, message.workerNonce);
|
|
575
|
+
const processIdentity = encodeProcessIdentity(identity, message.workerNonce, bootSession);
|
|
576
|
+
callback(() => options.lifecycle?.onStarted?.({
|
|
577
|
+
...message,
|
|
578
|
+
processIdentity
|
|
579
|
+
}));
|
|
580
|
+
} catch {
|
|
581
|
+
failure ??= new ExecutionError("spawn_error", "Command process identity could not be verified");
|
|
582
|
+
cancel();
|
|
583
|
+
}
|
|
584
|
+
break;
|
|
585
|
+
case "output":
|
|
586
|
+
callback(() => {
|
|
587
|
+
const text = outputRedactor?.transform(message.stream, message.text) ?? message.text;
|
|
588
|
+
output.append(message.stream, text);
|
|
589
|
+
if (failure === void 0) if (message.stream === "stdout") options.onStdout?.(text);
|
|
590
|
+
else options.onStderr?.(text);
|
|
591
|
+
});
|
|
592
|
+
if (worker.connected) worker.send({
|
|
593
|
+
kind: "output-ack",
|
|
594
|
+
sequence: message.sequence
|
|
595
|
+
}, () => {});
|
|
596
|
+
break;
|
|
597
|
+
case "exited":
|
|
598
|
+
callback(flushOutput);
|
|
599
|
+
exited = {
|
|
600
|
+
...message.result,
|
|
601
|
+
...output.snapshot()
|
|
602
|
+
};
|
|
603
|
+
if (message.result.errorCode !== void 0) failure ??= new ExecutionError(message.result.errorCode, message.result.message ?? "Command failed");
|
|
604
|
+
break;
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
});
|
|
608
|
+
worker.once("error", () => {
|
|
609
|
+
failure ??= new ExecutionError("spawn_error", "Process worker could not be started");
|
|
610
|
+
cancel();
|
|
611
|
+
if (worker.pid === void 0) finish();
|
|
612
|
+
});
|
|
613
|
+
worker.once("close", (code, signal) => {
|
|
614
|
+
finish(code, signal);
|
|
615
|
+
});
|
|
616
|
+
options.abortSignal?.addEventListener("abort", cancel, { once: true });
|
|
617
|
+
if (options.abortSignal?.aborted) cancel();
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
//#endregion
|
|
622
|
+
//#region src/environment/process.ts
|
|
623
|
+
const MAX_TIMEOUT_MS = 2147483647;
|
|
624
|
+
const SUPERVISED_PLATFORMS = new Set([
|
|
625
|
+
"linux",
|
|
626
|
+
"darwin",
|
|
627
|
+
"win32"
|
|
628
|
+
]);
|
|
629
|
+
/** Both streams share a byte budget and failure, but never share UTF-8 decoder state. */
|
|
630
|
+
var CommandCapture = class {
|
|
631
|
+
limit;
|
|
632
|
+
#terminate;
|
|
633
|
+
#bytes = 0;
|
|
634
|
+
#failure;
|
|
635
|
+
constructor(limit, terminate) {
|
|
636
|
+
this.limit = limit;
|
|
637
|
+
this.#terminate = terminate;
|
|
638
|
+
}
|
|
639
|
+
get failure() {
|
|
640
|
+
return this.#failure;
|
|
641
|
+
}
|
|
642
|
+
stream(name, outputRedactor, readCallback) {
|
|
643
|
+
const decoder = new StringDecoder("utf8");
|
|
644
|
+
const consume = (text, bytes, final = false) => this.#consume(outputRedactor?.transform(name, text, final) ?? text, bytes, readCallback());
|
|
645
|
+
return {
|
|
646
|
+
transform: function* (chunk) {
|
|
647
|
+
consume(decoder.write(Buffer.from(chunk)), chunk.byteLength);
|
|
648
|
+
yield new Uint8Array();
|
|
649
|
+
},
|
|
650
|
+
final: function* () {
|
|
651
|
+
consume(decoder.end(), 0, true);
|
|
652
|
+
yield new Uint8Array();
|
|
653
|
+
},
|
|
654
|
+
binary: true
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
exceededLimit() {
|
|
658
|
+
return new ExecutionError("unknown", `Command exceeded the ${this.limit} byte output limit`);
|
|
659
|
+
}
|
|
660
|
+
#consume(chunk, bytes, callback) {
|
|
661
|
+
this.#bytes += bytes;
|
|
662
|
+
if (this.limit !== void 0 && this.#bytes > this.limit) {
|
|
663
|
+
this.#failure = this.exceededLimit();
|
|
664
|
+
this.#terminate();
|
|
665
|
+
throw this.#failure;
|
|
666
|
+
}
|
|
667
|
+
try {
|
|
668
|
+
if (chunk.length > 0) callback?.(chunk);
|
|
669
|
+
} catch (cause) {
|
|
670
|
+
const error = toError(cause);
|
|
671
|
+
this.#failure = new ExecutionError("callback_error", error.message, error);
|
|
672
|
+
this.#terminate();
|
|
673
|
+
throw this.#failure;
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
/** Executes already assembled argv; cancellation and completion release the whole process group. */
|
|
678
|
+
async function executeCommand(command) {
|
|
679
|
+
let release;
|
|
680
|
+
let cleanupVerified = true;
|
|
681
|
+
const lifecycle = command.options?.lifecycle;
|
|
682
|
+
const admitted = lifecycle === void 0 ? command : {
|
|
683
|
+
...command,
|
|
684
|
+
options: {
|
|
685
|
+
...command.options,
|
|
686
|
+
lifecycle: {
|
|
687
|
+
...lifecycle,
|
|
688
|
+
onExited: (exit) => {
|
|
689
|
+
cleanupVerified = exit.cleanupVerified !== false;
|
|
690
|
+
lifecycle.onExited?.(exit);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
try {
|
|
696
|
+
release = await command.capacity?.take(command.options?.abortSignal);
|
|
697
|
+
return await executeAdmittedCommand(admitted);
|
|
698
|
+
} catch (cause) {
|
|
699
|
+
const error = toError(cause);
|
|
700
|
+
return err(new ExecutionError(command.options?.abortSignal?.aborted ? "aborted" : "spawn_error", error.message, error));
|
|
701
|
+
} finally {
|
|
702
|
+
if (cleanupVerified) await release?.();
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
async function executeAdmittedCommand(command) {
|
|
706
|
+
const { options } = command;
|
|
707
|
+
if (options?.abortSignal?.aborted) return err(new ExecutionError("aborted", "Command aborted"));
|
|
708
|
+
const timeout = options?.timeout === void 0 ? void 0 : options.timeout * 1e3;
|
|
709
|
+
if (timeout !== void 0 && (!Number.isFinite(timeout) || timeout <= 0 || timeout > MAX_TIMEOUT_MS)) return err(new ExecutionError("timeout", "Invalid timeout"));
|
|
710
|
+
if (SUPERVISED_PLATFORMS.has(process.platform)) {
|
|
711
|
+
const maxOutputBytes = options?.lifecycle?.maxOutputBytes ?? command.maxOutputBytes;
|
|
712
|
+
const forceKillAfterDelayMs = options?.lifecycle?.forceKillAfterDelayMs ?? 1e3;
|
|
713
|
+
if (maxOutputBytes !== void 0 && (!Number.isSafeInteger(maxOutputBytes) || maxOutputBytes < 0) || !Number.isSafeInteger(forceKillAfterDelayMs) || forceKillAfterDelayMs < 0 || forceKillAfterDelayMs > MAX_TIMEOUT_MS) return err(new ExecutionError("spawn_error", "Invalid managed process limits"));
|
|
714
|
+
const inherited = command.environment ?? process.env;
|
|
715
|
+
const environment = options?.inheritEnv === false ? options.env : {
|
|
716
|
+
...inherited,
|
|
717
|
+
...options?.env
|
|
718
|
+
};
|
|
719
|
+
const env = {};
|
|
720
|
+
for (const [key, value] of Object.entries(environment ?? {})) if (value !== void 0) env[key] = value;
|
|
721
|
+
let supervised;
|
|
722
|
+
try {
|
|
723
|
+
supervised = await executeManagedCommand({
|
|
724
|
+
kind: "command",
|
|
725
|
+
workerNonce: randomUUID(),
|
|
726
|
+
executable: command.executable,
|
|
727
|
+
args: [...command.args],
|
|
728
|
+
cwd: command.cwd,
|
|
729
|
+
env,
|
|
730
|
+
input: command.input,
|
|
731
|
+
timeout,
|
|
732
|
+
maxOutputBytes,
|
|
733
|
+
forceKillAfterDelayMs
|
|
734
|
+
}, options ?? {}, command.outputRedactor);
|
|
735
|
+
} catch {
|
|
736
|
+
return err(new ExecutionError("spawn_error", "Command could not be started"));
|
|
737
|
+
}
|
|
738
|
+
if (process.platform !== "win32" || supervised.ok || !(supervised.error.cause instanceof SupervisionUnavailableError)) return supervised;
|
|
739
|
+
}
|
|
740
|
+
let terminate;
|
|
741
|
+
const capture = new CommandCapture(command.maxOutputBytes, () => terminate?.());
|
|
742
|
+
const output = new ProcessOutput(options?.onStdout === void 0 || options.onStderr === void 0, command.maxOutputBytes);
|
|
743
|
+
let outcome;
|
|
744
|
+
try {
|
|
745
|
+
await options?.beforeStart?.();
|
|
746
|
+
options?.abortSignal?.throwIfAborted();
|
|
747
|
+
const child = execa(command.executable, [...command.args], {
|
|
748
|
+
cwd: command.cwd,
|
|
749
|
+
env: options?.inheritEnv === false ? options.env : {
|
|
750
|
+
...command.environment,
|
|
751
|
+
...options?.env
|
|
752
|
+
},
|
|
753
|
+
extendEnv: command.environment === void 0 && options?.inheritEnv !== false,
|
|
754
|
+
killDescendants: true,
|
|
755
|
+
windowsHide: true,
|
|
756
|
+
cancelSignal: options?.abortSignal,
|
|
757
|
+
timeout,
|
|
758
|
+
killSignal: "SIGKILL",
|
|
759
|
+
reject: false,
|
|
760
|
+
stripFinalNewline: false,
|
|
761
|
+
buffer: false,
|
|
762
|
+
input: command.input,
|
|
763
|
+
stdin: command.input === void 0 ? "ignore" : "pipe",
|
|
764
|
+
stdout: capture.stream("stdout", command.outputRedactor, () => (text) => {
|
|
765
|
+
output.append("stdout", text);
|
|
766
|
+
options?.onStdout?.(text);
|
|
767
|
+
}),
|
|
768
|
+
stderr: capture.stream("stderr", command.outputRedactor, () => (text) => {
|
|
769
|
+
output.append("stderr", text);
|
|
770
|
+
options?.onStderr?.(text);
|
|
771
|
+
})
|
|
772
|
+
});
|
|
773
|
+
terminate = () => {
|
|
774
|
+
child.kill("SIGKILL");
|
|
775
|
+
};
|
|
776
|
+
child.nodeChildProcess.once("exit", terminate);
|
|
777
|
+
const result = await child;
|
|
778
|
+
if (capture.failure !== void 0) throw capture.failure;
|
|
779
|
+
if (result.timedOut) throw new ExecutionError("timeout", `Command timed out after ${options?.timeout} seconds`);
|
|
780
|
+
if (result.isCanceled || options?.abortSignal?.aborted) throw new ExecutionError("aborted", "Command aborted");
|
|
781
|
+
if (result.exitCode === void 0) throw new ExecutionError("spawn_error", result.message ?? "Command could not be executed");
|
|
782
|
+
outcome = ok({
|
|
783
|
+
...output.snapshot(),
|
|
784
|
+
exitCode: result.exitCode
|
|
785
|
+
});
|
|
786
|
+
} catch (cause) {
|
|
787
|
+
const error = toError(cause);
|
|
788
|
+
const failure = cause instanceof ExecutionError ? cause : capture.failure;
|
|
789
|
+
outcome = err(new ExecutionError(failure?.code ?? "spawn_error", failure?.message ?? error.message, error, output.snapshot()));
|
|
790
|
+
} finally {
|
|
791
|
+
terminate?.();
|
|
792
|
+
try {
|
|
793
|
+
output.close();
|
|
794
|
+
} catch (cause) {
|
|
795
|
+
const error = toError(cause);
|
|
796
|
+
outcome = err(new ExecutionError("callback_error", error.message, error, output.snapshot()));
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
return outcome;
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
//#endregion
|
|
803
|
+
//#region src/environment/output-redaction.ts
|
|
804
|
+
function urlComponent(value) {
|
|
805
|
+
return encodeURIComponent(value).replace(/[!'()*]/gu, (character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
806
|
+
}
|
|
807
|
+
function codePointLength(value) {
|
|
808
|
+
const point = value.codePointAt(0);
|
|
809
|
+
return point !== void 0 && point > 65535 ? 2 : 1;
|
|
810
|
+
}
|
|
811
|
+
function patternsOf(secrets) {
|
|
812
|
+
const patterns = /* @__PURE__ */ new Map();
|
|
813
|
+
for (const [environmentName, value] of Object.entries(secrets)) {
|
|
814
|
+
if (value.length === 0) continue;
|
|
815
|
+
const label = `[secret:${environmentName.replace(/^SORBET_SECRET_/u, "") || environmentName}]`;
|
|
816
|
+
const variants = new Set([value, JSON.stringify(value).slice(1, -1)]);
|
|
817
|
+
try {
|
|
818
|
+
const encoded = urlComponent(value);
|
|
819
|
+
variants.add(encoded);
|
|
820
|
+
variants.add(encoded.replace(/%20/gu, "+"));
|
|
821
|
+
variants.add(encoded.replace(/%[0-9A-F]{2}/gu, (escape) => escape.toLowerCase()));
|
|
822
|
+
variants.add(encoded.replace(/%20/gu, "+").replace(/%[0-9A-F]{2}/gu, (escape) => escape.toLowerCase()));
|
|
823
|
+
} catch {}
|
|
824
|
+
for (const variant of variants) if (variant.length > 0) patterns.set(variant, {
|
|
825
|
+
value: variant,
|
|
826
|
+
label
|
|
827
|
+
});
|
|
828
|
+
}
|
|
829
|
+
return [...patterns.values()].sort((left, right) => right.value.length - left.value.length);
|
|
830
|
+
}
|
|
831
|
+
/** Redacts complete values across chunks before command output is retained or emitted. */
|
|
832
|
+
var StreamingOutputRedactor = class {
|
|
833
|
+
#patterns;
|
|
834
|
+
#lookahead;
|
|
835
|
+
#streams = {
|
|
836
|
+
stdout: {
|
|
837
|
+
pending: "",
|
|
838
|
+
finished: false
|
|
839
|
+
},
|
|
840
|
+
stderr: {
|
|
841
|
+
pending: "",
|
|
842
|
+
finished: false
|
|
843
|
+
}
|
|
844
|
+
};
|
|
845
|
+
constructor(secrets) {
|
|
846
|
+
this.#patterns = patternsOf(secrets);
|
|
847
|
+
this.#lookahead = Math.max(0, ...this.#patterns.map((pattern) => pattern.value.length)) + 1;
|
|
848
|
+
}
|
|
849
|
+
transform(stream, chunk, final = false) {
|
|
850
|
+
const state = this.#streams[stream];
|
|
851
|
+
if (state.finished) return chunk;
|
|
852
|
+
if (this.#patterns.length === 0) {
|
|
853
|
+
if (final) state.finished = true;
|
|
854
|
+
return chunk;
|
|
855
|
+
}
|
|
856
|
+
let input = state.pending + chunk;
|
|
857
|
+
state.pending = "";
|
|
858
|
+
let output = "";
|
|
859
|
+
const retained = final ? 0 : this.#lookahead;
|
|
860
|
+
while (input.length > retained) {
|
|
861
|
+
const match = this.#matchAtStart(input);
|
|
862
|
+
if (match !== void 0) {
|
|
863
|
+
output += match.label;
|
|
864
|
+
input = input.slice(match.value.length);
|
|
865
|
+
continue;
|
|
866
|
+
}
|
|
867
|
+
const width = codePointLength(input);
|
|
868
|
+
const character = input.slice(0, width);
|
|
869
|
+
output += character;
|
|
870
|
+
input = input.slice(width);
|
|
871
|
+
}
|
|
872
|
+
if (final) {
|
|
873
|
+
state.finished = true;
|
|
874
|
+
state.pending = "";
|
|
875
|
+
} else state.pending = input;
|
|
876
|
+
return output;
|
|
877
|
+
}
|
|
878
|
+
#matchAtStart(input) {
|
|
879
|
+
for (const pattern of this.#patterns) if (input.startsWith(pattern.value)) return pattern;
|
|
880
|
+
}
|
|
881
|
+
};
|
|
882
|
+
|
|
883
|
+
//#endregion
|
|
884
|
+
//#region src/environment/node-environment.ts
|
|
885
|
+
const NodeFailureSchema = s.obj({ code: s.opt(s.str()) }, { open: true });
|
|
886
|
+
const FileRangeSchema = s.obj({
|
|
887
|
+
offset: s.int({
|
|
888
|
+
minimum: 0,
|
|
889
|
+
maximum: Number.MAX_SAFE_INTEGER
|
|
890
|
+
}),
|
|
891
|
+
length: s.int({
|
|
892
|
+
minimum: 0,
|
|
893
|
+
maximum: Number.MAX_SAFE_INTEGER
|
|
894
|
+
})
|
|
895
|
+
}, { open: true });
|
|
896
|
+
const FILE_FAILURE_CODES = new Map([
|
|
897
|
+
["ABORT_ERR", "aborted"],
|
|
898
|
+
["ENOENT", "not_found"],
|
|
899
|
+
["EACCES", "permission_denied"],
|
|
900
|
+
["EPERM", "permission_denied"],
|
|
901
|
+
["ELOOP", "permission_denied"],
|
|
902
|
+
["ENOTDIR", "not_directory"],
|
|
903
|
+
["EISDIR", "is_directory"],
|
|
904
|
+
["EINVAL", "invalid"],
|
|
905
|
+
["ERR_INVALID_ARG_VALUE", "invalid"],
|
|
906
|
+
["ERR_INVALID_URL", "invalid"],
|
|
907
|
+
["ERR_INVALID_FILE_URL_HOST", "invalid"],
|
|
908
|
+
["ERR_INVALID_FILE_URL_PATH", "invalid"]
|
|
909
|
+
]);
|
|
910
|
+
var NodeExecutionEnv = class {
|
|
911
|
+
managedCommands = new Set([
|
|
912
|
+
"linux",
|
|
913
|
+
"darwin",
|
|
914
|
+
"win32"
|
|
915
|
+
]).has(process.platform) ? true : void 0;
|
|
916
|
+
cwd;
|
|
917
|
+
#capacity;
|
|
918
|
+
#shellPath;
|
|
919
|
+
#shellEnv;
|
|
920
|
+
#shellEnvironment;
|
|
921
|
+
#rejectSymlinkReads;
|
|
922
|
+
#pending = /* @__PURE__ */ new Map();
|
|
923
|
+
#closed = false;
|
|
924
|
+
constructor(options) {
|
|
925
|
+
this.cwd = resolve(options.cwd);
|
|
926
|
+
this.#capacity = options.capacity;
|
|
927
|
+
this.#shellPath = options.shellPath ?? (process.platform === "win32" ? "cmd.exe" : "/bin/bash");
|
|
928
|
+
this.#shellEnv = options.shellEnv;
|
|
929
|
+
this.#shellEnvironment = options.shellEnvironment;
|
|
930
|
+
this.#rejectSymlinkReads = options.rejectSymlinkReads ?? false;
|
|
931
|
+
}
|
|
932
|
+
absolutePath(path, options) {
|
|
933
|
+
return this.#file(path, options, async (absolute) => absolute);
|
|
934
|
+
}
|
|
935
|
+
joinPath(parts, options) {
|
|
936
|
+
return this.#file(this.cwd, options, async () => join(...parts));
|
|
937
|
+
}
|
|
938
|
+
readTextFile(path, options) {
|
|
939
|
+
return this.#file(path, options, async (absolute, invocation) => {
|
|
940
|
+
const handle = await this.#openForRead(absolute);
|
|
941
|
+
try {
|
|
942
|
+
const data = await handle.readFile({ signal: invocation.abortSignal });
|
|
943
|
+
return new TextDecoder("utf-8", {
|
|
944
|
+
fatal: true,
|
|
945
|
+
ignoreBOM: true
|
|
946
|
+
}).decode(data);
|
|
947
|
+
} finally {
|
|
948
|
+
await handle.close();
|
|
949
|
+
}
|
|
950
|
+
});
|
|
951
|
+
}
|
|
952
|
+
readTextLines(path, options) {
|
|
953
|
+
return this.#file(path, options, async (absolute, invocation) => {
|
|
954
|
+
if (options?.maxLines === 0) return [];
|
|
955
|
+
const handle = await this.#openForRead(absolute);
|
|
956
|
+
const stream = createReadStream(absolute, {
|
|
957
|
+
encoding: "utf8",
|
|
958
|
+
fd: handle.fd,
|
|
959
|
+
autoClose: false,
|
|
960
|
+
signal: invocation.abortSignal
|
|
961
|
+
});
|
|
962
|
+
const reader = createInterface({
|
|
963
|
+
input: stream,
|
|
964
|
+
crlfDelay: Infinity
|
|
965
|
+
});
|
|
966
|
+
const lines = [];
|
|
967
|
+
try {
|
|
968
|
+
for await (const line of reader) {
|
|
969
|
+
lines.push(line);
|
|
970
|
+
if (options?.maxLines !== void 0 && lines.length >= options.maxLines) break;
|
|
971
|
+
}
|
|
972
|
+
invocation.abortSignal?.throwIfAborted();
|
|
973
|
+
return lines;
|
|
974
|
+
} finally {
|
|
975
|
+
reader.close();
|
|
976
|
+
stream.destroy();
|
|
977
|
+
await handle.close();
|
|
978
|
+
}
|
|
979
|
+
});
|
|
980
|
+
}
|
|
981
|
+
readBinaryFile(path, options) {
|
|
982
|
+
const range = options?.range === void 0 ? void 0 : { ...options.range };
|
|
983
|
+
return this.#file(path, options, async (absolute, invocation) => {
|
|
984
|
+
const handle = await this.#openForRead(absolute);
|
|
985
|
+
try {
|
|
986
|
+
if (range === void 0) return await handle.readFile({ signal: invocation.abortSignal });
|
|
987
|
+
const captured = decode(FileRangeSchema, range);
|
|
988
|
+
invocation.abortSignal?.throwIfAborted();
|
|
989
|
+
await invocation.beforeStart?.();
|
|
990
|
+
invocation.abortSignal?.throwIfAborted();
|
|
991
|
+
const stats = await handle.stat();
|
|
992
|
+
const length = Math.min(captured.length, Math.max(0, stats.size - captured.offset));
|
|
993
|
+
const output = new Uint8Array(length);
|
|
994
|
+
let received = 0;
|
|
995
|
+
while (received < length) {
|
|
996
|
+
await invocation.beforeStart?.();
|
|
997
|
+
invocation.abortSignal?.throwIfAborted();
|
|
998
|
+
const { bytesRead } = await handle.read(output, received, length - received, captured.offset + received);
|
|
999
|
+
if (bytesRead === 0) break;
|
|
1000
|
+
received += bytesRead;
|
|
1001
|
+
}
|
|
1002
|
+
invocation.abortSignal?.throwIfAborted();
|
|
1003
|
+
return output.subarray(0, received);
|
|
1004
|
+
} finally {
|
|
1005
|
+
await handle.close();
|
|
1006
|
+
}
|
|
1007
|
+
});
|
|
1008
|
+
}
|
|
1009
|
+
writeFile(path, content, options) {
|
|
1010
|
+
return this.#file(path, options, async (absolute, invocation) => {
|
|
1011
|
+
await mkdir(dirname(absolute), { recursive: true });
|
|
1012
|
+
invocation.abortSignal?.throwIfAborted();
|
|
1013
|
+
await invocation.beforeStart?.();
|
|
1014
|
+
invocation.abortSignal?.throwIfAborted();
|
|
1015
|
+
await writeFile(absolute, content, { signal: invocation.abortSignal });
|
|
1016
|
+
});
|
|
1017
|
+
}
|
|
1018
|
+
appendFile(path, content, options) {
|
|
1019
|
+
return this.#file(path, options, async (absolute, invocation) => {
|
|
1020
|
+
await mkdir(dirname(absolute), { recursive: true });
|
|
1021
|
+
invocation.abortSignal?.throwIfAborted();
|
|
1022
|
+
await invocation.beforeStart?.();
|
|
1023
|
+
invocation.abortSignal?.throwIfAborted();
|
|
1024
|
+
await writeFile(absolute, content, {
|
|
1025
|
+
flag: "a",
|
|
1026
|
+
signal: invocation.abortSignal
|
|
1027
|
+
});
|
|
1028
|
+
});
|
|
1029
|
+
}
|
|
1030
|
+
renameFile(source, destination, options) {
|
|
1031
|
+
return this.#file(source, options, (absolute) => rename(absolute, resolve(this.cwd, destination)));
|
|
1032
|
+
}
|
|
1033
|
+
fileInfo(path, options) {
|
|
1034
|
+
return this.#file(path, options, async (absolute) => this.#metadata(absolute, await lstat(absolute)));
|
|
1035
|
+
}
|
|
1036
|
+
listDir(path, options) {
|
|
1037
|
+
return this.#file(path, options, async (absolute, invocation) => {
|
|
1038
|
+
const entries = await readdir(absolute);
|
|
1039
|
+
const infos = [];
|
|
1040
|
+
for (const name of entries.sort()) {
|
|
1041
|
+
invocation.abortSignal?.throwIfAborted();
|
|
1042
|
+
const entryPath = join(absolute, name);
|
|
1043
|
+
await invocation.beforeStart?.();
|
|
1044
|
+
invocation.abortSignal?.throwIfAborted();
|
|
1045
|
+
infos.push(this.#metadata(entryPath, await lstat(entryPath)));
|
|
1046
|
+
}
|
|
1047
|
+
return infos;
|
|
1048
|
+
});
|
|
1049
|
+
}
|
|
1050
|
+
canonicalPath(path, options) {
|
|
1051
|
+
return this.#file(path, options, (absolute) => realpath(absolute));
|
|
1052
|
+
}
|
|
1053
|
+
async exists(path, options) {
|
|
1054
|
+
const result = await this.fileInfo(path, options);
|
|
1055
|
+
if (result.ok) return ok(true);
|
|
1056
|
+
return result.error.code === "not_found" ? ok(false) : result;
|
|
1057
|
+
}
|
|
1058
|
+
createDir(path, options) {
|
|
1059
|
+
return this.#file(path, options, async (absolute) => {
|
|
1060
|
+
await mkdir(absolute, { recursive: options?.recursive ?? true });
|
|
1061
|
+
});
|
|
1062
|
+
}
|
|
1063
|
+
remove(path, options) {
|
|
1064
|
+
return this.#file(path, options, (absolute) => rm(absolute, {
|
|
1065
|
+
recursive: options?.recursive ?? false,
|
|
1066
|
+
force: options?.force ?? false
|
|
1067
|
+
}));
|
|
1068
|
+
}
|
|
1069
|
+
createTempDir(prefix = "tmp-", options) {
|
|
1070
|
+
return this.#file(tmpdir(), options, (absolute) => mkdtemp(join(absolute, prefix)));
|
|
1071
|
+
}
|
|
1072
|
+
createTempFile(options) {
|
|
1073
|
+
return this.#file(tmpdir(), options, async (absolute, invocation) => {
|
|
1074
|
+
const path = join(await mkdtemp(join(absolute, "sorbet-")), `${options?.prefix ?? ""}${randomUUID()}${options?.suffix ?? ""}`);
|
|
1075
|
+
invocation.abortSignal?.throwIfAborted();
|
|
1076
|
+
await invocation.beforeStart?.();
|
|
1077
|
+
invocation.abortSignal?.throwIfAborted();
|
|
1078
|
+
await writeFile(path, "", {
|
|
1079
|
+
flag: "wx",
|
|
1080
|
+
signal: invocation.abortSignal
|
|
1081
|
+
});
|
|
1082
|
+
return path;
|
|
1083
|
+
});
|
|
1084
|
+
}
|
|
1085
|
+
async exec(command, options) {
|
|
1086
|
+
if (this.#closed) return err(new ExecutionError("spawn_error", "Execution environment is closed"));
|
|
1087
|
+
const controller = new AbortController();
|
|
1088
|
+
const abortSignal = options?.abortSignal === void 0 ? controller.signal : AbortSignal.any([controller.signal, options.abortSignal]);
|
|
1089
|
+
const binding = this.#shellEnvironment?.();
|
|
1090
|
+
let environment = this.#shellEnv;
|
|
1091
|
+
if (binding !== void 0) {
|
|
1092
|
+
environment = { ...this.#shellEnv ?? process.env };
|
|
1093
|
+
for (const name of binding.remove ?? []) delete environment[name];
|
|
1094
|
+
Object.assign(environment, binding.variables, binding.secrets);
|
|
1095
|
+
}
|
|
1096
|
+
const argv = process.platform === "win32" ? [
|
|
1097
|
+
"/d",
|
|
1098
|
+
"/s",
|
|
1099
|
+
"/c",
|
|
1100
|
+
command
|
|
1101
|
+
] : ["-c", command];
|
|
1102
|
+
const pending = executeCommand({
|
|
1103
|
+
capacity: this.#capacity,
|
|
1104
|
+
executable: this.#shellPath,
|
|
1105
|
+
args: argv,
|
|
1106
|
+
cwd: resolve(this.cwd, options?.cwd ?? this.cwd),
|
|
1107
|
+
environment,
|
|
1108
|
+
outputRedactor: binding?.secrets === void 0 ? void 0 : new StreamingOutputRedactor(binding.secrets),
|
|
1109
|
+
options: {
|
|
1110
|
+
...options,
|
|
1111
|
+
abortSignal
|
|
1112
|
+
}
|
|
1113
|
+
});
|
|
1114
|
+
this.#pending.set(controller, pending);
|
|
1115
|
+
try {
|
|
1116
|
+
return await pending;
|
|
1117
|
+
} finally {
|
|
1118
|
+
this.#pending.delete(controller);
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
async cleanup() {
|
|
1122
|
+
this.#closed = true;
|
|
1123
|
+
for (const controller of this.#pending.keys()) controller.abort();
|
|
1124
|
+
await Promise.allSettled(this.#pending.values());
|
|
1125
|
+
}
|
|
1126
|
+
async #openForRead(path) {
|
|
1127
|
+
if (!this.#rejectSymlinkReads) return open(path, constants.O_RDONLY);
|
|
1128
|
+
if (process.platform === "darwin") return open(path, constants.O_RDONLY | 536870912);
|
|
1129
|
+
if (process.platform !== "linux") throw new FileError("not_supported", "Symlink-safe sandbox reads are unavailable", path);
|
|
1130
|
+
const components = path.split("/").filter(Boolean);
|
|
1131
|
+
let directory = await open("/", constants.O_RDONLY | constants.O_DIRECTORY);
|
|
1132
|
+
try {
|
|
1133
|
+
for (const component of components.slice(0, -1)) {
|
|
1134
|
+
const child = await open(`/proc/self/fd/${directory.fd}/${component}`, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
|
|
1135
|
+
await directory.close();
|
|
1136
|
+
directory = child;
|
|
1137
|
+
}
|
|
1138
|
+
const leaf = components.at(-1);
|
|
1139
|
+
return await open(leaf === void 0 ? `/proc/self/fd/${directory.fd}/.` : `/proc/self/fd/${directory.fd}/${leaf}`, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
1140
|
+
} finally {
|
|
1141
|
+
await directory.close();
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
#metadata(path, stats) {
|
|
1145
|
+
let kind;
|
|
1146
|
+
if (stats.isFile()) kind = "file";
|
|
1147
|
+
else if (stats.isDirectory()) kind = "directory";
|
|
1148
|
+
else if (stats.isSymbolicLink()) kind = "symlink";
|
|
1149
|
+
else throw new FileError("not_supported", "Unsupported filesystem object", path);
|
|
1150
|
+
return {
|
|
1151
|
+
name: basename(path),
|
|
1152
|
+
path,
|
|
1153
|
+
kind,
|
|
1154
|
+
size: stats.size,
|
|
1155
|
+
mtimeMs: stats.mtimeMs
|
|
1156
|
+
};
|
|
1157
|
+
}
|
|
1158
|
+
async #file(path, options, run) {
|
|
1159
|
+
if (this.#closed) return err(new FileError("aborted", "Execution environment is closed", path));
|
|
1160
|
+
const controller = new AbortController();
|
|
1161
|
+
const abortSignal = options?.abortSignal === void 0 ? controller.signal : AbortSignal.any([controller.signal, options.abortSignal]);
|
|
1162
|
+
const pending = this.#runFile(path, {
|
|
1163
|
+
...options,
|
|
1164
|
+
abortSignal
|
|
1165
|
+
}, run);
|
|
1166
|
+
this.#pending.set(controller, pending);
|
|
1167
|
+
try {
|
|
1168
|
+
return await pending;
|
|
1169
|
+
} finally {
|
|
1170
|
+
this.#pending.delete(controller);
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
async #runFile(path, options, run) {
|
|
1174
|
+
let absolute = resolve(this.cwd, path);
|
|
1175
|
+
try {
|
|
1176
|
+
if (path.startsWith("file://")) absolute = fileURLToPath(path);
|
|
1177
|
+
options?.abortSignal?.throwIfAborted();
|
|
1178
|
+
await options?.beforeStart?.();
|
|
1179
|
+
if (this.#closed) return err(new FileError("aborted", "Execution environment is closed", absolute));
|
|
1180
|
+
options?.abortSignal?.throwIfAborted();
|
|
1181
|
+
return ok(await run(absolute, options ?? {}));
|
|
1182
|
+
} catch (cause) {
|
|
1183
|
+
if (cause instanceof FileError) return err(cause);
|
|
1184
|
+
const error = toError(cause);
|
|
1185
|
+
const code = check(NodeFailureSchema, cause) ? cause.code : void 0;
|
|
1186
|
+
return err(new FileError(options?.abortSignal?.aborted ? "aborted" : FILE_FAILURE_CODES.get(code ?? "") ?? "unknown", error.message, absolute, error));
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
};
|
|
1190
|
+
|
|
1191
|
+
//#endregion
|
|
1192
|
+
//#region src/environment/service.ts
|
|
1193
|
+
/** Borrowed by one mounted Tool group; the Provider owns cleanup and sandbox policy. */
|
|
1194
|
+
const ExecutionEnvironment = /* @__PURE__ */ Context.Service("sorbet/tools-node/ExecutionEnvironment/v1");
|
|
1195
|
+
|
|
1196
|
+
//#endregion
|
|
1197
|
+
//#region src/environment/process-capacity.ts
|
|
1198
|
+
/** Shared by local environments; waiting is cancellable and never launches a process. */
|
|
1199
|
+
var ProcessCapacity = class {
|
|
1200
|
+
#permits;
|
|
1201
|
+
constructor(maximum = 32) {
|
|
1202
|
+
this.#permits = Semaphore.makeUnsafe(decode(s.int({
|
|
1203
|
+
minimum: 1,
|
|
1204
|
+
maximum: Number.MAX_SAFE_INTEGER
|
|
1205
|
+
}), maximum));
|
|
1206
|
+
}
|
|
1207
|
+
/** Return the permit only after the owned process and its cleanup have settled. */
|
|
1208
|
+
async take(signal) {
|
|
1209
|
+
const scope = Scope.makeUnsafe();
|
|
1210
|
+
try {
|
|
1211
|
+
await Effect.runPromise(Effect.uninterruptibleMask((restore) => restore(this.#permits.take(1)).pipe(Effect.andThen(Scope.addFinalizer(scope, this.#permits.release(1))))), { signal });
|
|
1212
|
+
return () => Effect.runPromise(Scope.close(scope, Exit.void));
|
|
1213
|
+
} catch (cause) {
|
|
1214
|
+
await Effect.runPromise(Scope.close(scope, Exit.void));
|
|
1215
|
+
throw cause;
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
};
|
|
1219
|
+
|
|
1220
|
+
//#endregion
|
|
1221
|
+
export { executeProcessWorker as a, FileError as c, ok as d, executeCommand as i, err as l, ExecutionEnvironment as n, settleAbandonedProcess as o, NodeExecutionEnv as r, ExecutionError as s, ProcessCapacity as t, getOrThrow as u };
|
|
1222
|
+
//# sourceMappingURL=environment-CtRyPVb9.js.map
|