stitchkit 0.108.2 → 0.108.3
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 +16 -0
- package/dist/entrypoints/agent-runtime/coding-tools.js +3 -2
- package/dist/entrypoints/agent-runtime/sandbox.js +5 -3
- package/dist/entrypoints/cli.js +1 -1
- package/dist/entrypoints/contract.js +1 -1
- package/dist/entrypoints/index.js +1 -1
- package/dist/entrypoints/node.js +1 -1
- package/dist/entrypoints/process/owner-loss.d.ts +3 -0
- package/dist/entrypoints/process/owner-loss.d.ts.map +1 -0
- package/dist/entrypoints/process/owner-loss.js +6 -0
- package/dist/entrypoints/process.js +4 -2
- package/dist/entrypoints/server.js +1 -1
- package/dist/entrypoints/tools.js +1 -1
- package/dist/{index-k0kzhdw9.js → index-dasy32c8.js} +23 -634
- package/dist/index-dj1aycxw.js +639 -0
- package/dist/{index-4qh90tyw.js → index-fgq4w9bf.js} +23 -1
- package/dist/{index-vyaehh67.js → index-tycmtxky.js} +6 -4
- package/dist/process/owner-loss-guard.d.ts +9 -0
- package/dist/process/owner-loss-guard.d.ts.map +1 -1
- package/dist/tools/cli/args-fields.d.ts.map +1 -1
- package/llms/cli.2.txt +107 -0
- package/llms/cli.txt +8 -107
- package/llms/process-owner-loss.txt +18 -0
- package/llms/process.txt +18 -0
- package/llms/stitchkit.txt +1 -0
- package/llms-full.txt +35 -0
- package/llms.txt +4 -3
- package/package.json +5 -1
- /package/dist/{index-q4hjtyye.js → index-qd8x73e8.js} +0 -0
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
NativeCommandOptionsSchema,
|
|
3
|
+
NativeCommandError2,
|
|
4
|
+
abortOwnerLossLaunch,
|
|
5
|
+
attachOwnerLossControl,
|
|
6
|
+
transferOwnerLossControl,
|
|
7
|
+
awaitCommandStart,
|
|
8
|
+
ownerLossAvailable,
|
|
9
|
+
ownerLossGuardCommand,
|
|
10
|
+
hostIsBun,
|
|
11
|
+
spawnBunChild,
|
|
12
|
+
spawnBunInheritedChild,
|
|
13
|
+
ownerLossGuardEntry
|
|
14
|
+
} from "./index-dj1aycxw.js";
|
|
1
15
|
import {
|
|
2
16
|
raceAbort
|
|
3
17
|
} from "./index-b0kddvzw.js";
|
|
@@ -9,154 +23,6 @@ import {
|
|
|
9
23
|
assertPositiveSafeInteger
|
|
10
24
|
} from "./index-0s4xebbg.js";
|
|
11
25
|
|
|
12
|
-
// src/process/contract.ts
|
|
13
|
-
import { z } from "zod";
|
|
14
|
-
var NativeCommandSettlementSchema = z.discriminatedUnion("kind", [
|
|
15
|
-
z.object({
|
|
16
|
-
kind: z.literal("exit"),
|
|
17
|
-
exitCode: z.number().int().nullable(),
|
|
18
|
-
signal: z.string().nullable()
|
|
19
|
-
}),
|
|
20
|
-
z.object({
|
|
21
|
-
kind: z.literal("stopped"),
|
|
22
|
-
cause: z.unknown(),
|
|
23
|
-
exitCode: z.number().int().nullable(),
|
|
24
|
-
signal: z.string().nullable()
|
|
25
|
-
}),
|
|
26
|
-
z.object({ kind: z.literal("error"), cause: z.unknown() })
|
|
27
|
-
]);
|
|
28
|
-
var COMMAND_CLEANUP_TIMEOUT_MS = 2000;
|
|
29
|
-
var MAX_COMMAND_STOP_GRACE_MS = 3600000;
|
|
30
|
-
var NativeCommandStopPolicySchema = z.strictObject({
|
|
31
|
-
target: z.enum(["group", "leader"]),
|
|
32
|
-
signal: z.enum(["SIGTERM", "SIGINT", "SIGHUP", "SIGQUIT", "SIGUSR1", "SIGUSR2"]).default("SIGTERM"),
|
|
33
|
-
graceMs: z.number().int().nonnegative().max(MAX_COMMAND_STOP_GRACE_MS),
|
|
34
|
-
killOn: z.custom((v) => v instanceof AbortSignal).optional()
|
|
35
|
-
});
|
|
36
|
-
var REMOVED_NATIVE_COMMAND_OPTIONS = {
|
|
37
|
-
killGraceMs: "stop: { target: 'group', graceMs } (removed in 0.105.0)"
|
|
38
|
-
};
|
|
39
|
-
function unknownOptionMessage(keys) {
|
|
40
|
-
return keys.map((key) => {
|
|
41
|
-
const name = String(key);
|
|
42
|
-
const replacement = Object.hasOwn(REMOVED_NATIVE_COMMAND_OPTIONS, name) ? REMOVED_NATIVE_COMMAND_OPTIONS[name] : undefined;
|
|
43
|
-
return replacement === undefined ? `Unknown runNativeCommand option ${name}` : `Unknown runNativeCommand option ${name}; use ${replacement}`;
|
|
44
|
-
}).join("; ");
|
|
45
|
-
}
|
|
46
|
-
var DEFAULT_GROUP_STOP = {
|
|
47
|
-
target: "group",
|
|
48
|
-
signal: "SIGTERM",
|
|
49
|
-
graceMs: 100
|
|
50
|
-
};
|
|
51
|
-
var DEFAULT_LEADER_STOP = {
|
|
52
|
-
target: "leader",
|
|
53
|
-
signal: "SIGTERM",
|
|
54
|
-
graceMs: 100
|
|
55
|
-
};
|
|
56
|
-
var NativeCommandOptionsSchema = z.strictObject({
|
|
57
|
-
executable: z.string().min(1),
|
|
58
|
-
args: z.array(z.string()).default([]),
|
|
59
|
-
cwd: z.string().optional(),
|
|
60
|
-
env: z.record(z.string(), z.string()).optional(),
|
|
61
|
-
envPolicy: z.enum(["declared-only", "ambient"]).default("declared-only"),
|
|
62
|
-
signal: z.custom((v) => v instanceof AbortSignal).optional(),
|
|
63
|
-
timeoutMs: z.number().int().positive().max(MAX_TIMER_MS).optional(),
|
|
64
|
-
maxOutputBytes: z.number().int().positive().optional(),
|
|
65
|
-
capture: z.boolean().default(false),
|
|
66
|
-
stdin: z.custom((v) => v instanceof Uint8Array).optional(),
|
|
67
|
-
maxStdinBytes: z.number().int().positive().default(1024 * 1024),
|
|
68
|
-
onOutput: z.custom((v) => typeof v === "function").optional(),
|
|
69
|
-
onLeaderSettled: z.custom((v) => typeof v === "function").optional(),
|
|
70
|
-
onLeaderStarted: z.custom((v) => typeof v === "function").optional(),
|
|
71
|
-
drainTimeoutMs: z.number().int().positive().max(MAX_TIMER_MS).optional(),
|
|
72
|
-
group: z.enum(["own", "caller"]).default("own"),
|
|
73
|
-
ownerLoss: z.enum(["terminate"]).optional(),
|
|
74
|
-
stdio: z.enum(["pipe", "inherit"]).default("pipe"),
|
|
75
|
-
stop: NativeCommandStopPolicySchema.optional(),
|
|
76
|
-
descendants: z.enum(["terminate-after-leader", "leave"]).optional(),
|
|
77
|
-
cleanupTimeoutMs: z.number().int().positive().max(30000).default(COMMAND_CLEANUP_TIMEOUT_MS)
|
|
78
|
-
}, {
|
|
79
|
-
error: (issue) => issue.code === "unrecognized_keys" ? unknownOptionMessage(issue.keys) : undefined
|
|
80
|
-
}).check((ctx) => {
|
|
81
|
-
const input = ctx.value;
|
|
82
|
-
if (!input.signal && input.timeoutMs === undefined)
|
|
83
|
-
ctx.issues.push({
|
|
84
|
-
code: "custom",
|
|
85
|
-
input: ctx.value,
|
|
86
|
-
message: "Declare a caller signal or a finite timeout"
|
|
87
|
-
});
|
|
88
|
-
if (input.capture && input.maxOutputBytes === undefined)
|
|
89
|
-
ctx.issues.push({
|
|
90
|
-
code: "custom",
|
|
91
|
-
input: ctx.value,
|
|
92
|
-
message: "Capture requires a finite combined output budget"
|
|
93
|
-
});
|
|
94
|
-
if (input.stdin && input.stdin.byteLength > input.maxStdinBytes)
|
|
95
|
-
ctx.issues.push({
|
|
96
|
-
code: "custom",
|
|
97
|
-
input: ctx.value,
|
|
98
|
-
message: "stdin exceeds maxStdinBytes"
|
|
99
|
-
});
|
|
100
|
-
if (input.group === "caller" && input.stop?.target === "group")
|
|
101
|
-
ctx.issues.push({
|
|
102
|
-
code: "custom",
|
|
103
|
-
input: ctx.value,
|
|
104
|
-
message: "group: 'caller' stops only the leader; use stop.target 'leader'"
|
|
105
|
-
});
|
|
106
|
-
if (input.group === "caller" && input.descendants !== undefined)
|
|
107
|
-
ctx.issues.push({
|
|
108
|
-
code: "custom",
|
|
109
|
-
input: ctx.value,
|
|
110
|
-
message: "group: 'caller' has no group of its own, so descendants does not apply"
|
|
111
|
-
});
|
|
112
|
-
if (input.group === "caller" && input.ownerLoss !== undefined)
|
|
113
|
-
ctx.issues.push({
|
|
114
|
-
code: "custom",
|
|
115
|
-
input: ctx.value,
|
|
116
|
-
message: "ownerLoss: 'terminate' requires group: 'own'"
|
|
117
|
-
});
|
|
118
|
-
if (input.stdio === "inherit") {
|
|
119
|
-
const piped = [
|
|
120
|
-
input.capture && "capture",
|
|
121
|
-
input.onOutput && "onOutput",
|
|
122
|
-
input.stdin && "stdin",
|
|
123
|
-
input.maxOutputBytes !== undefined && "maxOutputBytes",
|
|
124
|
-
input.drainTimeoutMs !== undefined && "drainTimeoutMs"
|
|
125
|
-
].filter((name) => typeof name === "string");
|
|
126
|
-
for (const name of piped)
|
|
127
|
-
ctx.issues.push({
|
|
128
|
-
code: "custom",
|
|
129
|
-
input: ctx.value,
|
|
130
|
-
message: `stdio: 'inherit' leaves no pipe for ${name}`
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
}).transform((input) => ({
|
|
134
|
-
...input,
|
|
135
|
-
stop: input.stop ?? (input.group === "caller" ? DEFAULT_LEADER_STOP : DEFAULT_GROUP_STOP),
|
|
136
|
-
descendants: input.descendants ?? "terminate-after-leader"
|
|
137
|
-
}));
|
|
138
|
-
var NativeCommandResultSchema = z.object({
|
|
139
|
-
exitCode: z.number().int().nullable(),
|
|
140
|
-
signal: z.string().nullable(),
|
|
141
|
-
descendantsStopped: z.boolean(),
|
|
142
|
-
stdout: z.instanceof(Uint8Array),
|
|
143
|
-
stderr: z.instanceof(Uint8Array)
|
|
144
|
-
});
|
|
145
|
-
var NativeCommandLimitReasonSchema = z.enum(["deadline", "output-budget"]);
|
|
146
|
-
|
|
147
|
-
class NativeCommandError2 extends Error {
|
|
148
|
-
code;
|
|
149
|
-
reason;
|
|
150
|
-
constructor(code, message, options) {
|
|
151
|
-
super(message, options);
|
|
152
|
-
this.code = code;
|
|
153
|
-
if (options?.reason !== undefined && code !== "COMMAND_LIMIT")
|
|
154
|
-
throw new TypeError("Only COMMAND_LIMIT accepts a limit reason");
|
|
155
|
-
this.reason = options?.reason === undefined ? undefined : NativeCommandLimitReasonSchema.parse(options.reason);
|
|
156
|
-
this.name = "NativeCommandError";
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
26
|
// src/process/group.ts
|
|
161
27
|
function createCommandDeadline(timeoutMs, reason) {
|
|
162
28
|
assertPositiveSafeInteger("Command deadline", timeoutMs, RangeError);
|
|
@@ -402,343 +268,11 @@ function createCommandLifetime(input) {
|
|
|
402
268
|
}
|
|
403
269
|
|
|
404
270
|
// src/process/launch.ts
|
|
405
|
-
import { Readable as
|
|
271
|
+
import { Readable as Readable2 } from "node:stream";
|
|
406
272
|
|
|
407
273
|
// src/process/owned-child.ts
|
|
408
274
|
import { spawn } from "node:child_process";
|
|
409
275
|
import { Duplex } from "node:stream";
|
|
410
|
-
|
|
411
|
-
// src/process/bun-child.ts
|
|
412
|
-
import { EventEmitter } from "node:events";
|
|
413
|
-
import { Readable, Writable } from "node:stream";
|
|
414
|
-
|
|
415
|
-
// src/process/owner-loss-control.ts
|
|
416
|
-
import { z as z2 } from "zod";
|
|
417
|
-
var MAX_GUARD_MESSAGE_BYTES = 4096;
|
|
418
|
-
var OwnerLossGuardMessageSchema = z2.discriminatedUnion("kind", [
|
|
419
|
-
z2.strictObject({ kind: z2.literal("started"), pid: z2.number().int().positive() }),
|
|
420
|
-
z2.strictObject({
|
|
421
|
-
kind: z2.literal("unavailable"),
|
|
422
|
-
message: z2.string().min(1).max(1024),
|
|
423
|
-
code: z2.string().min(1).max(64).optional()
|
|
424
|
-
})
|
|
425
|
-
]);
|
|
426
|
-
var guards = new WeakMap;
|
|
427
|
-
function abortOwnerLossLaunch(pid) {
|
|
428
|
-
if (pid === undefined)
|
|
429
|
-
return;
|
|
430
|
-
try {
|
|
431
|
-
process.kill(-pid, "SIGKILL");
|
|
432
|
-
} catch (error) {
|
|
433
|
-
if (!(error instanceof Error && ("code" in error) && error.code === "ESRCH"))
|
|
434
|
-
throw error;
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
function unavailable(message, cause) {
|
|
438
|
-
return new NativeCommandError2("COMMAND_UNAVAILABLE", "Command could not start", {
|
|
439
|
-
cause: cause ?? new Error(message)
|
|
440
|
-
});
|
|
441
|
-
}
|
|
442
|
-
function attachOwnerLossControl(child, control) {
|
|
443
|
-
const settled = Promise.withResolvers();
|
|
444
|
-
settled.promise.catch(() => {
|
|
445
|
-
return;
|
|
446
|
-
});
|
|
447
|
-
let complete = false;
|
|
448
|
-
let bytes = 0;
|
|
449
|
-
let text = "";
|
|
450
|
-
const decoder = new TextDecoder;
|
|
451
|
-
const ignoreSettledError = () => {
|
|
452
|
-
return;
|
|
453
|
-
};
|
|
454
|
-
const finish = (outcome) => {
|
|
455
|
-
if (complete)
|
|
456
|
-
return;
|
|
457
|
-
complete = true;
|
|
458
|
-
control.off("data", onData);
|
|
459
|
-
control.off("end", onMissing);
|
|
460
|
-
control.off("close", onMissing);
|
|
461
|
-
control.off("error", onError);
|
|
462
|
-
control.on("error", ignoreSettledError);
|
|
463
|
-
control.once("close", () => control.off("error", ignoreSettledError));
|
|
464
|
-
if ("pid" in outcome)
|
|
465
|
-
settled.resolve(outcome.pid);
|
|
466
|
-
else
|
|
467
|
-
settled.reject(outcome.error);
|
|
468
|
-
};
|
|
469
|
-
const onMissing = () => finish({ error: unavailable("Owner-loss guard exited before the command started") });
|
|
470
|
-
const onError = (cause) => finish({ error: unavailable("Owner-loss guard control failed", cause) });
|
|
471
|
-
const onData = (chunk) => {
|
|
472
|
-
if (complete)
|
|
473
|
-
return;
|
|
474
|
-
bytes += chunk.byteLength;
|
|
475
|
-
if (bytes > MAX_GUARD_MESSAGE_BYTES) {
|
|
476
|
-
finish({ error: unavailable("Owner-loss guard response exceeded its finite bound") });
|
|
477
|
-
return;
|
|
478
|
-
}
|
|
479
|
-
text += decoder.decode(chunk, { stream: true });
|
|
480
|
-
const newline = text.indexOf(`
|
|
481
|
-
`);
|
|
482
|
-
if (newline < 0)
|
|
483
|
-
return;
|
|
484
|
-
try {
|
|
485
|
-
const message = OwnerLossGuardMessageSchema.parse(JSON.parse(text.slice(0, newline)));
|
|
486
|
-
if (message.kind === "started")
|
|
487
|
-
finish({ pid: message.pid });
|
|
488
|
-
else {
|
|
489
|
-
const cause = new Error(message.message);
|
|
490
|
-
if (message.code !== undefined)
|
|
491
|
-
Object.defineProperty(cause, "code", { value: message.code });
|
|
492
|
-
finish({ error: unavailable(message.message, cause) });
|
|
493
|
-
}
|
|
494
|
-
} catch (cause) {
|
|
495
|
-
finish({ error: unavailable("Owner-loss guard returned an invalid response", cause) });
|
|
496
|
-
}
|
|
497
|
-
};
|
|
498
|
-
control.on("data", onData);
|
|
499
|
-
control.once("end", onMissing);
|
|
500
|
-
control.once("close", onMissing);
|
|
501
|
-
control.once("error", onError);
|
|
502
|
-
child.on("exit", onMissing);
|
|
503
|
-
guards.set(child, { control, started: settled.promise });
|
|
504
|
-
}
|
|
505
|
-
function transferOwnerLossControl(source, target) {
|
|
506
|
-
const registration = guards.get(source);
|
|
507
|
-
if (registration !== undefined)
|
|
508
|
-
guards.set(target, registration);
|
|
509
|
-
}
|
|
510
|
-
async function awaitCommandStart(child) {
|
|
511
|
-
const registration = guards.get(child);
|
|
512
|
-
if (registration !== undefined)
|
|
513
|
-
await registration.started;
|
|
514
|
-
return child.pid;
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
// src/process/owner-loss-protocol.ts
|
|
518
|
-
var OWNER_LOSS_GUARD_FLAG = "--stitchkit-owner-loss-guard-v1";
|
|
519
|
-
function ownerLossAvailable(platform) {
|
|
520
|
-
return platform === "linux" || platform === "darwin";
|
|
521
|
-
}
|
|
522
|
-
function ownerLossGuardCommand(guard, executable, args) {
|
|
523
|
-
return [
|
|
524
|
-
process.execPath,
|
|
525
|
-
guard.entry,
|
|
526
|
-
OWNER_LOSS_GUARD_FLAG,
|
|
527
|
-
guard.signalTarget,
|
|
528
|
-
executable,
|
|
529
|
-
...args
|
|
530
|
-
];
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
// src/process/owner-loss-socket.ts
|
|
534
|
-
import { connect } from "node:net";
|
|
535
|
-
function connectOwnerLossSocket(fd) {
|
|
536
|
-
return Reflect.apply(connect, undefined, [{ fd }]);
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
// src/process/bun-child.ts
|
|
540
|
-
function hostIsBun() {
|
|
541
|
-
return typeof Bun !== "undefined";
|
|
542
|
-
}
|
|
543
|
-
function readableOf(source) {
|
|
544
|
-
const reader = source.getReader();
|
|
545
|
-
let reading = false;
|
|
546
|
-
return new Readable({
|
|
547
|
-
read() {
|
|
548
|
-
if (reading)
|
|
549
|
-
return;
|
|
550
|
-
reading = true;
|
|
551
|
-
reader.read().then(({ done, value }) => {
|
|
552
|
-
reading = false;
|
|
553
|
-
if (done)
|
|
554
|
-
this.push(null);
|
|
555
|
-
else
|
|
556
|
-
this.push(value);
|
|
557
|
-
}, (error) => {
|
|
558
|
-
reading = false;
|
|
559
|
-
this.destroy(error instanceof Error ? error : new Error(String(error)));
|
|
560
|
-
});
|
|
561
|
-
},
|
|
562
|
-
destroy(error, done) {
|
|
563
|
-
reader.cancel().then(() => done(error), () => done(error));
|
|
564
|
-
}
|
|
565
|
-
});
|
|
566
|
-
}
|
|
567
|
-
var REPORTED_SPAWN_CODES = new Set(["ENOENT", "EACCES", "EAGAIN", "EMFILE", "ENFILE"]);
|
|
568
|
-
function reportedSpawnFailure(cause) {
|
|
569
|
-
return typeof cause === "object" && cause !== null && "code" in cause && typeof cause.code === "string" && REPORTED_SPAWN_CODES.has(cause.code);
|
|
570
|
-
}
|
|
571
|
-
var waitClosed = (stream) => stream.closed ? Promise.resolve() : new Promise((resolve) => stream.once("close", () => resolve()));
|
|
572
|
-
function defineBunChildren() {
|
|
573
|
-
|
|
574
|
-
class BunProcess extends EventEmitter {
|
|
575
|
-
pipes;
|
|
576
|
-
pid;
|
|
577
|
-
exitCode = null;
|
|
578
|
-
signalCode = null;
|
|
579
|
-
#subprocess;
|
|
580
|
-
constructor(pipes) {
|
|
581
|
-
super();
|
|
582
|
-
this.pipes = pipes;
|
|
583
|
-
}
|
|
584
|
-
watch(subprocess) {
|
|
585
|
-
this.pid = subprocess.pid;
|
|
586
|
-
this.#subprocess = subprocess;
|
|
587
|
-
subprocess.exited.then(() => {
|
|
588
|
-
this.exitCode = subprocess.exitCode;
|
|
589
|
-
this.signalCode = subprocess.signalCode;
|
|
590
|
-
this.emit("exit", this.exitCode, this.signalCode);
|
|
591
|
-
for (const pipe of this.pipes)
|
|
592
|
-
if (pipe.readableFlowing === null)
|
|
593
|
-
pipe.resume();
|
|
594
|
-
this.closeAfterPipes();
|
|
595
|
-
});
|
|
596
|
-
}
|
|
597
|
-
fail(cause) {
|
|
598
|
-
for (const pipe of this.pipes)
|
|
599
|
-
pipe.resume();
|
|
600
|
-
setImmediate(() => {
|
|
601
|
-
this.emit("error", cause);
|
|
602
|
-
this.closeAfterPipes();
|
|
603
|
-
});
|
|
604
|
-
}
|
|
605
|
-
kill(signal) {
|
|
606
|
-
const subprocess = this.#subprocess;
|
|
607
|
-
if (!subprocess || this.exitCode !== null || this.signalCode !== null)
|
|
608
|
-
return false;
|
|
609
|
-
try {
|
|
610
|
-
subprocess.kill(signal);
|
|
611
|
-
return true;
|
|
612
|
-
} catch {
|
|
613
|
-
return false;
|
|
614
|
-
}
|
|
615
|
-
}
|
|
616
|
-
closeAfterPipes() {
|
|
617
|
-
Promise.all(this.pipes.map(waitClosed)).then(() => this.emit("close", this.exitCode, this.signalCode));
|
|
618
|
-
}
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
class BunChild extends BunProcess {
|
|
622
|
-
stdin;
|
|
623
|
-
stdout;
|
|
624
|
-
stderr;
|
|
625
|
-
constructor(stdin, stdout, stderr) {
|
|
626
|
-
super([stdout, stderr]);
|
|
627
|
-
this.stdin = stdin;
|
|
628
|
-
this.stdout = stdout;
|
|
629
|
-
this.stderr = stderr;
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
return { BunProcess, BunChild };
|
|
633
|
-
}
|
|
634
|
-
var bunChildren;
|
|
635
|
-
function children() {
|
|
636
|
-
bunChildren ??= defineBunChildren();
|
|
637
|
-
return bunChildren;
|
|
638
|
-
}
|
|
639
|
-
function sinkWritable(sink) {
|
|
640
|
-
let ended = false;
|
|
641
|
-
const end = () => {
|
|
642
|
-
if (ended)
|
|
643
|
-
return Promise.resolve(0);
|
|
644
|
-
ended = true;
|
|
645
|
-
return Promise.resolve().then(() => sink.end());
|
|
646
|
-
};
|
|
647
|
-
return new Writable({
|
|
648
|
-
write(chunk, _encoding, done) {
|
|
649
|
-
Promise.resolve().then(() => sink.write(chunk)).then(() => done(), done);
|
|
650
|
-
},
|
|
651
|
-
final(done) {
|
|
652
|
-
end().then(() => done(), done);
|
|
653
|
-
},
|
|
654
|
-
destroy(error, done) {
|
|
655
|
-
end().then(() => done(error), () => done(error));
|
|
656
|
-
}
|
|
657
|
-
});
|
|
658
|
-
}
|
|
659
|
-
function spawnBunChild(input) {
|
|
660
|
-
const { BunChild } = children();
|
|
661
|
-
try {
|
|
662
|
-
const ownerLoss = input.ownerLoss;
|
|
663
|
-
const guarded = ownerLoss !== undefined;
|
|
664
|
-
const command = guarded ? ownerLossGuardCommand(ownerLoss, input.executable, input.args) : [input.executable, ...input.args];
|
|
665
|
-
const subprocess = guarded ? Bun.spawn(command, {
|
|
666
|
-
cwd: input.cwd,
|
|
667
|
-
env: input.env,
|
|
668
|
-
detached: input.group,
|
|
669
|
-
stdio: ["pipe", "pipe", "pipe", "socket-fd"]
|
|
670
|
-
}) : Bun.spawn(command, {
|
|
671
|
-
cwd: input.cwd,
|
|
672
|
-
env: input.env,
|
|
673
|
-
detached: input.group,
|
|
674
|
-
stdin: "pipe",
|
|
675
|
-
stdout: "pipe",
|
|
676
|
-
stderr: "pipe"
|
|
677
|
-
});
|
|
678
|
-
const child = new BunChild(sinkWritable(subprocess.stdin), readableOf(subprocess.stdout), readableOf(subprocess.stderr));
|
|
679
|
-
child.watch(subprocess);
|
|
680
|
-
if (guarded) {
|
|
681
|
-
const fd = subprocess.stdio[3];
|
|
682
|
-
if (typeof fd !== "number") {
|
|
683
|
-
abortOwnerLossLaunch(subprocess.pid);
|
|
684
|
-
throw new Error("Owner-loss guard supplied no control socket");
|
|
685
|
-
}
|
|
686
|
-
attachOwnerLossControl(child, connectOwnerLossSocket(fd));
|
|
687
|
-
}
|
|
688
|
-
return child;
|
|
689
|
-
} catch (cause) {
|
|
690
|
-
if (!reportedSpawnFailure(cause))
|
|
691
|
-
throw cause;
|
|
692
|
-
const child = new BunChild(new Writable({ write: (_chunk, _encoding, done) => done() }), Readable.from([]), Readable.from([]));
|
|
693
|
-
child.fail(cause);
|
|
694
|
-
return child;
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
function spawnBunInheritedChild(input) {
|
|
698
|
-
const { BunProcess } = children();
|
|
699
|
-
const child = new BunProcess([]);
|
|
700
|
-
try {
|
|
701
|
-
const ownerLoss = input.ownerLoss;
|
|
702
|
-
const guarded = ownerLoss !== undefined;
|
|
703
|
-
const command = guarded ? ownerLossGuardCommand(ownerLoss, input.executable, input.args) : [input.executable, ...input.args];
|
|
704
|
-
const subprocess = guarded ? Bun.spawn(command, {
|
|
705
|
-
cwd: input.cwd,
|
|
706
|
-
env: input.env,
|
|
707
|
-
detached: input.group,
|
|
708
|
-
stdio: ["inherit", "inherit", "inherit", "socket-fd"]
|
|
709
|
-
}) : Bun.spawn(command, {
|
|
710
|
-
cwd: input.cwd,
|
|
711
|
-
env: input.env,
|
|
712
|
-
detached: input.group,
|
|
713
|
-
stdin: "inherit",
|
|
714
|
-
stdout: "inherit",
|
|
715
|
-
stderr: "inherit"
|
|
716
|
-
});
|
|
717
|
-
child.watch(subprocess);
|
|
718
|
-
if (guarded) {
|
|
719
|
-
const fd = subprocess.stdio[3];
|
|
720
|
-
if (typeof fd !== "number") {
|
|
721
|
-
abortOwnerLossLaunch(subprocess.pid);
|
|
722
|
-
throw new Error("Owner-loss guard supplied no control socket");
|
|
723
|
-
}
|
|
724
|
-
attachOwnerLossControl(child, connectOwnerLossSocket(fd));
|
|
725
|
-
}
|
|
726
|
-
} catch (cause) {
|
|
727
|
-
if (!reportedSpawnFailure(cause))
|
|
728
|
-
throw cause;
|
|
729
|
-
child.fail(cause);
|
|
730
|
-
}
|
|
731
|
-
return child;
|
|
732
|
-
}
|
|
733
|
-
function spawnBunGuardTarget(executable, args) {
|
|
734
|
-
return Bun.spawn([executable, ...args], {
|
|
735
|
-
stdin: "inherit",
|
|
736
|
-
stdout: "inherit",
|
|
737
|
-
stderr: "inherit"
|
|
738
|
-
});
|
|
739
|
-
}
|
|
740
|
-
|
|
741
|
-
// src/process/owned-child.ts
|
|
742
276
|
var groups = new WeakSet;
|
|
743
277
|
function spawnOwnedCommand(input) {
|
|
744
278
|
const child = hostIsBun() ? spawnBunChild(input) : nodeChild(input);
|
|
@@ -836,153 +370,8 @@ function ownsCommandGroup(child) {
|
|
|
836
370
|
return groups.has(child);
|
|
837
371
|
}
|
|
838
372
|
|
|
839
|
-
// src/process/owner-loss-guard.ts
|
|
840
|
-
import { spawn as spawn2 } from "node:child_process";
|
|
841
|
-
import { Socket } from "node:net";
|
|
842
|
-
import { fileURLToPath } from "node:url";
|
|
843
|
-
var FORWARDED_SIGNALS = [
|
|
844
|
-
"SIGTERM",
|
|
845
|
-
"SIGINT",
|
|
846
|
-
"SIGHUP",
|
|
847
|
-
"SIGQUIT",
|
|
848
|
-
"SIGUSR1",
|
|
849
|
-
"SIGUSR2"
|
|
850
|
-
];
|
|
851
|
-
function causeMessage(cause) {
|
|
852
|
-
const message = cause instanceof Error ? cause.message : String(cause);
|
|
853
|
-
if (typeof cause === "object" && cause !== null && "code" in cause && typeof cause.code === "string")
|
|
854
|
-
return { message, code: cause.code };
|
|
855
|
-
return { message };
|
|
856
|
-
}
|
|
857
|
-
function guardMessage(control, message, written) {
|
|
858
|
-
control.write(`${JSON.stringify(message)}
|
|
859
|
-
`, written);
|
|
860
|
-
}
|
|
861
|
-
function mirrorTargetExit(control, code, signal) {
|
|
862
|
-
control.destroy();
|
|
863
|
-
if (signal !== null)
|
|
864
|
-
process.kill(process.pid, signal);
|
|
865
|
-
else
|
|
866
|
-
process.exitCode = code ?? 1;
|
|
867
|
-
}
|
|
868
|
-
function refuseTarget(control, cause) {
|
|
869
|
-
const detail = causeMessage(cause);
|
|
870
|
-
control.end(`${JSON.stringify({ kind: "unavailable", ...detail })}
|
|
871
|
-
`, () => {
|
|
872
|
-
process.exitCode = 127;
|
|
873
|
-
});
|
|
874
|
-
}
|
|
875
|
-
function spawnNodeTarget(executable, args, onStarted, onExit, onUnavailable) {
|
|
876
|
-
let child;
|
|
877
|
-
try {
|
|
878
|
-
child = spawn2(executable, [...args], { stdio: "inherit" });
|
|
879
|
-
} catch (cause) {
|
|
880
|
-
onUnavailable(cause);
|
|
881
|
-
return;
|
|
882
|
-
}
|
|
883
|
-
child.once("spawn", () => {
|
|
884
|
-
const pid = child.pid;
|
|
885
|
-
if (pid === undefined)
|
|
886
|
-
onUnavailable(new Error("Command started without a pid"));
|
|
887
|
-
else
|
|
888
|
-
onStarted(pid);
|
|
889
|
-
});
|
|
890
|
-
child.once("error", onUnavailable);
|
|
891
|
-
child.once("exit", onExit);
|
|
892
|
-
}
|
|
893
|
-
function spawnBunTarget(executable, args, onStarted, onExit, onUnavailable) {
|
|
894
|
-
let child;
|
|
895
|
-
try {
|
|
896
|
-
child = spawnBunGuardTarget(executable, args);
|
|
897
|
-
} catch (cause) {
|
|
898
|
-
onUnavailable(cause);
|
|
899
|
-
return;
|
|
900
|
-
}
|
|
901
|
-
onStarted(child.pid);
|
|
902
|
-
child.exited.then(() => onExit(child.exitCode, child.signalCode));
|
|
903
|
-
}
|
|
904
|
-
function runOwnerLossGuard(signalTarget, executable, args) {
|
|
905
|
-
const bun = hostIsBun();
|
|
906
|
-
const control = bun ? connectOwnerLossSocket(3) : new Socket({ fd: 3, readable: true, writable: true });
|
|
907
|
-
let armed = true;
|
|
908
|
-
const ownerLost = () => {
|
|
909
|
-
if (!armed)
|
|
910
|
-
return;
|
|
911
|
-
process.kill(-process.pid, "SIGKILL");
|
|
912
|
-
};
|
|
913
|
-
control.once("end", ownerLost);
|
|
914
|
-
control.once("close", ownerLost);
|
|
915
|
-
control.once("error", ownerLost);
|
|
916
|
-
if (signalTarget !== "group" && signalTarget !== "leader" || executable === undefined) {
|
|
917
|
-
armed = false;
|
|
918
|
-
refuseTarget(control, new Error("Owner-loss guard received an invalid invocation"));
|
|
919
|
-
return;
|
|
920
|
-
}
|
|
921
|
-
let targetPid;
|
|
922
|
-
let handshakeWritten = false;
|
|
923
|
-
let pendingTargetExit;
|
|
924
|
-
const forwarders = new Map;
|
|
925
|
-
if (signalTarget === "leader")
|
|
926
|
-
for (const signal of FORWARDED_SIGNALS) {
|
|
927
|
-
const forward = () => {
|
|
928
|
-
if (targetPid === undefined)
|
|
929
|
-
return;
|
|
930
|
-
try {
|
|
931
|
-
process.kill(targetPid, signal);
|
|
932
|
-
} catch (error) {
|
|
933
|
-
if (!(error instanceof Error && ("code" in error) && error.code === "ESRCH"))
|
|
934
|
-
throw error;
|
|
935
|
-
}
|
|
936
|
-
};
|
|
937
|
-
forwarders.set(signal, forward);
|
|
938
|
-
process.on(signal, forward);
|
|
939
|
-
}
|
|
940
|
-
const removeForwarders = () => {
|
|
941
|
-
for (const [signal, forward] of forwarders)
|
|
942
|
-
process.removeListener(signal, forward);
|
|
943
|
-
forwarders.clear();
|
|
944
|
-
};
|
|
945
|
-
const targetStarted = (pid) => {
|
|
946
|
-
targetPid = pid;
|
|
947
|
-
guardMessage(control, { kind: "started", pid }, () => {
|
|
948
|
-
handshakeWritten = true;
|
|
949
|
-
const pending = pendingTargetExit;
|
|
950
|
-
if (pending !== undefined) {
|
|
951
|
-
pendingTargetExit = undefined;
|
|
952
|
-
targetExit(pending.code, pending.signal);
|
|
953
|
-
}
|
|
954
|
-
});
|
|
955
|
-
};
|
|
956
|
-
const targetExit = (code, signal) => {
|
|
957
|
-
if (!handshakeWritten) {
|
|
958
|
-
pendingTargetExit = { code, signal };
|
|
959
|
-
return;
|
|
960
|
-
}
|
|
961
|
-
armed = false;
|
|
962
|
-
removeForwarders();
|
|
963
|
-
mirrorTargetExit(control, code, signal);
|
|
964
|
-
};
|
|
965
|
-
const targetUnavailable = (cause) => {
|
|
966
|
-
armed = false;
|
|
967
|
-
removeForwarders();
|
|
968
|
-
refuseTarget(control, cause);
|
|
969
|
-
};
|
|
970
|
-
if (!bun)
|
|
971
|
-
spawnNodeTarget(executable, args, targetStarted, targetExit, targetUnavailable);
|
|
972
|
-
else
|
|
973
|
-
spawnBunTarget(executable, args, targetStarted, targetExit, targetUnavailable);
|
|
974
|
-
}
|
|
975
|
-
function ownerLossGuardEntry() {
|
|
976
|
-
return fileURLToPath(import.meta.url);
|
|
977
|
-
}
|
|
978
|
-
if (process.argv[2] === OWNER_LOSS_GUARD_FLAG) {
|
|
979
|
-
const entry = process.argv[1];
|
|
980
|
-
if (entry !== undefined && fileURLToPath(import.meta.url) === entry)
|
|
981
|
-
runOwnerLossGuard(process.argv[3], process.argv[4], process.argv.slice(5));
|
|
982
|
-
}
|
|
983
|
-
|
|
984
373
|
// src/process/transport.ts
|
|
985
|
-
import { Readable
|
|
374
|
+
import { Readable, Writable } from "node:stream";
|
|
986
375
|
function createCommandTransport(child) {
|
|
987
376
|
const leader = Promise.withResolvers();
|
|
988
377
|
const closed = Promise.withResolvers();
|
|
@@ -1039,17 +428,17 @@ function createCommandTransport(child) {
|
|
|
1039
428
|
stderr = child.stderr;
|
|
1040
429
|
destructors.set("stderr", () => stderr?.destroy());
|
|
1041
430
|
const input = "stdin" in child ? child.stdin : undefined;
|
|
1042
|
-
if (input instanceof
|
|
431
|
+
if (input instanceof Writable) {
|
|
1043
432
|
stdin = input;
|
|
1044
433
|
pendingHandles.add("stdin");
|
|
1045
434
|
destructors.set("stdin", () => input.destroy());
|
|
1046
435
|
pipe("stdin", input);
|
|
1047
436
|
}
|
|
1048
|
-
if (stdout instanceof
|
|
437
|
+
if (stdout instanceof Readable)
|
|
1049
438
|
pipe("stdout", stdout);
|
|
1050
|
-
if (stderr instanceof
|
|
439
|
+
if (stderr instanceof Readable)
|
|
1051
440
|
pipe("stderr", stderr);
|
|
1052
|
-
if (stdout instanceof
|
|
441
|
+
if (stdout instanceof Readable && stderr instanceof Readable)
|
|
1053
442
|
release("launcher");
|
|
1054
443
|
return { stdout, stderr };
|
|
1055
444
|
},
|
|
@@ -1067,7 +456,7 @@ function createCommandTransport(child) {
|
|
|
1067
456
|
child.stdout.destroy();
|
|
1068
457
|
else if (name === "stderr")
|
|
1069
458
|
child.stderr.destroy();
|
|
1070
|
-
else if ("stdin" in child && child.stdin instanceof
|
|
459
|
+
else if ("stdin" in child && child.stdin instanceof Writable)
|
|
1071
460
|
child.stdin.destroy();
|
|
1072
461
|
} catch (cause) {
|
|
1073
462
|
failures.push(cause);
|
|
@@ -1100,7 +489,7 @@ function nativeCommandOwner(child) {
|
|
|
1100
489
|
return owners.get(child);
|
|
1101
490
|
}
|
|
1102
491
|
function outputStream(source, closed, maxBufferedBytes) {
|
|
1103
|
-
if (source instanceof
|
|
492
|
+
if (source instanceof Readable2)
|
|
1104
493
|
return {
|
|
1105
494
|
bytes: source,
|
|
1106
495
|
destroy() {
|
|
@@ -1525,4 +914,4 @@ function startNativeCommand(input, onSpawn, driver) {
|
|
|
1525
914
|
return owner;
|
|
1526
915
|
}
|
|
1527
916
|
|
|
1528
|
-
export {
|
|
917
|
+
export { CommandStopped, spawnOwnedCommand, nativeCommandOwner, terminateOwnedGroup, startNativeCommand };
|