@orkestrel/process 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,10 @@
1
1
  import { Buffer } from "node:buffer";
2
2
  import { spawn, spawnSync } from "node:child_process";
3
3
  import { statSync } from "node:fs";
4
- import { delimiter, extname, join, resolve } from "node:path";
4
+ import { join, posix, win32 } from "node:path";
5
+ import { platform } from "node:process";
5
6
  import { boundsOf, holds, isNonEmptyString, isNonNegativeInteger, isString } from "@orkestrel/contract";
6
- import { PROCESS_BACKLOG, PROCESS_CONFIRMATION, PROCESS_EVIDENCE, PROCESS_GRACE, PROCESS_OUTPUT, PROCESS_PATHEXT, PROCESS_TIMER, createDuplicateError, createInvalidError, createProtocolError, createRunError } from "../core/index.js";
7
+ import { PROCESS_BACKLOG, PROCESS_CONFIRMATION, PROCESS_EVIDENCE, PROCESS_GRACE, PROCESS_OUTPUT, PROCESS_PATHEXT, PROCESS_TIMER, createDuplicateError, createExecuteError, createInvalidError, createProtocolError } from "../core/index.js";
7
8
  import { createInterface } from "node:readline";
8
9
  import { StringDecoder } from "node:string_decoder";
9
10
  import { Emitter } from "@orkestrel/emitter";
@@ -65,10 +66,44 @@ function trimHead(bytes, limit) {
65
66
  return Buffer.from(buffer.subarray(0, end));
66
67
  }
67
68
  /**
69
+ * Takes one owned frozen snapshot of a caller's command.
70
+ *
71
+ * @remarks
72
+ * Every public entry point snapshots before it validates, so the object validated is the object
73
+ * spawned. Each property is read exactly once, because reading one runs the caller's own getter: a
74
+ * command whose `file` changes between reads would otherwise validate one executable and spawn
75
+ * another. The argument vector and the environment record are copied and frozen, so a caller
76
+ * mutating either after the call cannot reach the spawn. An absent optional stays absent rather than
77
+ * becoming an explicit `undefined`.
78
+ *
79
+ * @param command - The caller's command, whose properties may be getters
80
+ * @returns A frozen command carrying the values read at this instant
81
+ *
82
+ * @example
83
+ * ```ts
84
+ * snapshotCommand({ file: 'git', arguments: ['status'] }) // { file: 'git', arguments: ['status'] }
85
+ * ```
86
+ */
87
+ function snapshotCommand(command) {
88
+ const file = command.file;
89
+ const argumentsList = Object.freeze([...command.arguments]);
90
+ const sourceEnvironment = command.environment;
91
+ const environment = sourceEnvironment === void 0 ? void 0 : Object.freeze({ ...sourceEnvironment });
92
+ const input = command.input;
93
+ const isolated = command.isolated;
94
+ return Object.freeze({
95
+ file,
96
+ arguments: argumentsList,
97
+ ...environment === void 0 ? {} : { environment },
98
+ ...input === void 0 ? {} : { input },
99
+ ...isolated === void 0 ? {} : { isolated }
100
+ });
101
+ }
102
+ /**
68
103
  * Renders one command into its diagnostic command line.
69
104
  *
70
105
  * @param command - The executable and its argument vector
71
- * @returns The space-joined command line, for a {@link RunResult} and error messages
106
+ * @returns The space-joined command line, for an {@link ExecuteResult} and error messages
72
107
  *
73
108
  * @example
74
109
  * ```ts
@@ -79,36 +114,54 @@ function formatCommand(command) {
79
114
  return [command.file, ...command.arguments].join(" ");
80
115
  }
81
116
  /**
82
- * Reads one environment variable the way the host resolves it.
117
+ * Reads one environment variable under an explicit platform's key rules.
83
118
  *
84
119
  * @remarks
85
120
  * Windows environment keys are case-insensitive, so a merged record can hold `Path` where the
86
- * caller asks for `PATH`. The exact key is tried first and a folded scan runs only on Windows.
121
+ * caller asks for `PATH`. The exact key is tried first and a folded scan runs only for `win32`.
87
122
  *
88
123
  * @param environment - The environment record to read
89
124
  * @param name - The variable name
125
+ * @param platform - The platform whose key rules apply
90
126
  * @returns The value, or `undefined` when the environment declares no such variable
91
127
  *
92
128
  * @example
93
129
  * ```ts
94
- * readVariable({ Path: 'C:\\Windows' }, 'PATH') // 'C:\\Windows' on Windows
130
+ * readPlatformVariable({ Path: 'C:\\Windows' }, 'PATH', 'win32') // 'C:\\Windows'
95
131
  * ```
96
132
  */
97
- function readVariable(environment, name) {
133
+ function readPlatformVariable(environment, name, platform) {
98
134
  const direct = environment[name];
99
135
  if (direct !== void 0) return direct;
100
- if (process.platform !== "win32") return void 0;
136
+ if (platform !== "win32") return void 0;
101
137
  const target = name.toUpperCase();
102
138
  for (const [key, value] of Object.entries(environment)) if (key.toUpperCase() === target) return value;
103
139
  }
104
140
  /**
105
- * Merges environment overrides into the environment one child receives.
141
+ * Reads one environment variable the way the current host resolves it.
142
+ *
143
+ * @param environment - The environment record to read
144
+ * @param name - The variable name
145
+ * @returns The value, or `undefined` when the environment declares no such variable
146
+ *
147
+ * @example
148
+ * ```ts
149
+ * readVariable({ PATH: '/usr/bin' }, 'PATH') // '/usr/bin'
150
+ * ```
151
+ */
152
+ function readVariable(environment, name) {
153
+ return readPlatformVariable(environment, name, platform);
154
+ }
155
+ /**
156
+ * Merges environment layers under an explicit platform's key rules.
106
157
  *
107
158
  * @remarks
108
- * Later maps override earlier ones and an `undefined` value unsets a key. On Windows the keys fold
159
+ * Later maps override earlier ones and an `undefined` value unsets a key. For `win32` the keys fold
109
160
  * case-insensitively and the last writer wins, so `PATH` followed by `Path` yields one variable
110
161
  * rather than two the host would resolve unpredictably.
111
162
  *
163
+ * @param platform - The platform whose key rules apply
164
+ * @param parent - The parent environment layer
112
165
  * @param isolated - If `true`, the parent environment is excluded; if `false`, the overrides layer over it
113
166
  * @param base - The command's own environment overrides
114
167
  * @param override - Per-invocation overrides applied last
@@ -116,13 +169,13 @@ function readVariable(environment, name) {
116
169
  *
117
170
  * @example
118
171
  * ```ts
119
- * mergeEnvironment(false, { TOKEN: 'a' }, { TOKEN: undefined }) // TOKEN unset
172
+ * mergePlatformEnvironment('linux', {}, false, { TOKEN: 'a' }, { TOKEN: undefined }) // TOKEN unset
120
173
  * ```
121
174
  */
122
- function mergeEnvironment(isolated, base, override) {
123
- const folded = process.platform === "win32";
175
+ function mergePlatformEnvironment(platform, parent, isolated, base, override) {
176
+ const folded = platform === "win32";
124
177
  const layers = [
125
- isolated ? {} : process.env,
178
+ isolated ? {} : parent,
126
179
  base ?? {},
127
180
  override ?? {}
128
181
  ];
@@ -140,6 +193,22 @@ function mergeEnvironment(isolated, base, override) {
140
193
  return merged;
141
194
  }
142
195
  /**
196
+ * Merges environment overrides into the environment one child receives on the current host.
197
+ *
198
+ * @param isolated - If `true`, the parent environment is excluded; if `false`, the overrides layer over it
199
+ * @param base - The command's own environment overrides
200
+ * @param override - Per-invocation overrides applied last
201
+ * @returns The environment for a spawn, carrying no unset key
202
+ *
203
+ * @example
204
+ * ```ts
205
+ * mergeEnvironment(true, { TOKEN: 'a' }) // { TOKEN: 'a' }
206
+ * ```
207
+ */
208
+ function mergeEnvironment(isolated, base, override) {
209
+ return mergePlatformEnvironment(platform, process.env, isolated, base, override);
210
+ }
211
+ /**
143
212
  * Checks whether a path names a regular file.
144
213
  *
145
214
  * @remarks
@@ -158,18 +227,44 @@ function isFile(target) {
158
227
  return holds(() => statSync(target, { throwIfNoEntry: false })?.isFile() === true);
159
228
  }
160
229
  /**
161
- * Resolves a command file to the executable path the host would launch.
230
+ * Builds the executable candidates an explicit platform would search.
162
231
  *
163
232
  * @remarks
164
- * Windows alone needs this: the host searches the working directory before `PATH` and applies
233
+ * Windows alone needs this list: the host searches the working directory before `PATH` and applies
165
234
  * `PATHEXT`, and Node reproduces neither for a direct spawn. Within each searched directory the
166
235
  * literal name is tried first and each `PATHEXT` candidate after it, whether or not the name already
167
236
  * carries an extension — so `report.txt` resolves to a `report.txt` file where one exists and to
168
237
  * `report.txt.cmd` where none does. The lookup reads the child's effective environment, so an
169
238
  * overridden `PATH` selects the executable the child would have found. A POSIX host resolves the
170
- * file itself, so the answer there is always `undefined`. An appended extension is spelled the way
239
+ * file itself, so the candidate list there is empty. An appended extension is spelled the way
171
240
  * `PATHEXT` spells it rather than the way the directory entry does, which the case-insensitive host
172
- * treats as the same file.
241
+ * treats as the same file. A non-Windows platform returns an empty list because `execvp` performs
242
+ * its own lookup.
243
+ *
244
+ * @param file - The command executable name or path
245
+ * @param workspace - The directory searched first
246
+ * @param environment - The child's effective environment
247
+ * @param platform - The platform whose lookup rules apply
248
+ * @returns The ordered absolute candidate paths
249
+ *
250
+ * @example
251
+ * ```ts
252
+ * buildExecutableCandidates('git', 'C:\\work', { PATH: 'C:\\bin' }, 'win32')
253
+ * ```
254
+ */
255
+ function buildExecutableCandidates(file, workspace, environment, platform) {
256
+ if (platform !== "win32") return Object.freeze([]);
257
+ const candidates = [file, ...(readPlatformVariable(environment, "PATHEXT", platform) ?? PROCESS_PATHEXT).split(";").filter((extension) => extension.length > 0).map((extension) => `${file}${extension}`)];
258
+ const directories = file.includes("/") || file.includes("\\") ? [workspace] : [workspace, ...(readPlatformVariable(environment, "PATH", platform) ?? "").split(";")];
259
+ const targets = [];
260
+ for (const directory of directories) {
261
+ if (directory.length === 0) continue;
262
+ for (const candidate of candidates) targets.push(win32.resolve(directory, candidate));
263
+ }
264
+ return Object.freeze(targets);
265
+ }
266
+ /**
267
+ * Resolves a command file to the executable path the host would launch.
173
268
  *
174
269
  * @param file - The command executable name or path
175
270
  * @param options - The directory searched first and the child's effective environment
@@ -181,18 +276,9 @@ function isFile(target) {
181
276
  * ```
182
277
  */
183
278
  function resolveExecutable(file, options) {
184
- if (process.platform !== "win32") return void 0;
185
279
  const environment = options?.environment ?? process.env;
186
- const workspace = options?.workspace ?? process.cwd();
187
- const candidates = [file, ...(readVariable(environment, "PATHEXT") ?? PROCESS_PATHEXT).split(";").filter((extension) => extension.length > 0).map((extension) => `${file}${extension}`)];
188
- const directories = file.includes("/") || file.includes("\\") ? [workspace] : [workspace, ...(readVariable(environment, "PATH") ?? "").split(delimiter)];
189
- for (const directory of directories) {
190
- if (directory.length === 0) continue;
191
- for (const candidate of candidates) {
192
- const target = resolve(directory, candidate);
193
- if (isFile(target)) return target;
194
- }
195
- }
280
+ const candidates = buildExecutableCandidates(file, options?.workspace ?? process.cwd(), environment, platform);
281
+ for (const target of candidates) if (isFile(target)) return target;
196
282
  }
197
283
  /**
198
284
  * Quotes one command-line token for a `cmd.exe` command line.
@@ -200,8 +286,9 @@ function resolveExecutable(file, options) {
200
286
  * @remarks
201
287
  * A token carrying whitespace or a shell metacharacter is wrapped in double quotes so `cmd.exe`
202
288
  * passes it as one literal argument; an embedded double quote is doubled, which is how `cmd.exe`
203
- * reads a literal quote inside a quoted token. Every other token is left exactly as written, so a
204
- * batch script still receives `%1` without added quotes.
289
+ * reads a literal quote inside a quoted token. The quoted set includes `%`, although quoting cannot
290
+ * stop `cmd.exe` expansion; {@link buildSpawn} therefore refuses `%` in an argument to a Windows
291
+ * batch target.
205
292
  *
206
293
  * @param value - The token to quote
207
294
  * @returns The token, quoted only when `cmd.exe` would otherwise split or interpret it
@@ -216,41 +303,41 @@ function quoteArgument(value) {
216
303
  return `"${value.replaceAll("\"", "\"\"")}"`;
217
304
  }
218
305
  /**
219
- * Builds the resolved spawn form of one command.
306
+ * Builds a spawn form from a resolved file and an explicit platform.
220
307
  *
221
308
  * @remarks
222
- * The executable is resolved against the child's effective environment. A resolved `.cmd` or `.bat`
223
- * script cannot be spawned directly **on Windows**, so it runs there through an explicitly quoted
224
- * `cmd.exe /d /s /c` command line with the argument vector passed verbatim. No path uses a shell,
225
- * so a metacharacter in an argument is never interpreted. `cmd.exe` expands `%NAME%` before it
226
- * parses quotes, so no quoting can carry a percent sign through to a Windows batch target: an
227
- * argument carrying one is refused there rather than silently rewritten. The whole batch path is
228
- * Windows-only. A POSIX host has no `cmd.exe` and no restriction on spawning a file directly, so a
229
- * target named `worker.cmd` spawns directly there and receives a percent sign as literal text.
309
+ * A resolved `.cmd` or `.bat` script cannot be spawned directly **on Windows**, so it runs there
310
+ * through an explicitly quoted `cmd.exe /d /s /c` command line with the argument vector passed
311
+ * verbatim. No path uses a shell, so a metacharacter in an argument is never interpreted. `cmd.exe`
312
+ * expands `%NAME%` before it parses quotes, so no quoting can carry a percent sign through to a
313
+ * Windows batch target: an argument carrying one is refused there rather than silently rewritten.
314
+ * The whole batch path is Windows-only. A POSIX host has no `cmd.exe` and no restriction on spawning
315
+ * a file directly, so a target named `worker.cmd` spawns directly there and receives a percent sign
316
+ * as literal text.
230
317
  *
231
318
  * @param command - The executable and its argument vector
232
- * @param options - The directory searched first and the child's effective environment
319
+ * @param file - The resolved executable file
320
+ * @param environment - The child's effective environment
321
+ * @param platform - The platform whose batch rules apply
233
322
  * @returns The file, argument vector, and verbatim flag to spawn with
234
- * @throws A {@link ProcessError} coded `invalid` when the host is Windows, the resolved target is a batch script, and an argument carries a percent sign
323
+ * @throws A {@link ProcessError} coded `invalid` when `platform` is `win32`, the resolved target is a batch script, and an argument carries a percent sign
235
324
  *
236
325
  * @example
237
326
  * ```ts
238
- * buildSpawn({ file: 'node', arguments: ['--version'] }).verbatim // false
327
+ * buildPlatformSpawn({ file: 'node', arguments: ['--version'] }, 'node', {}, 'linux').verbatim // false
239
328
  * ```
240
329
  */
241
- function buildSpawn(command, options) {
242
- const file = resolveExecutable(command.file, options) ?? command.file;
243
- const extension = extname(file).toLowerCase();
244
- if (!(process.platform === "win32" && (extension === ".cmd" || extension === ".bat"))) return Object.freeze({
330
+ function buildPlatformSpawn(command, file, environment, platform) {
331
+ const extension = (platform === "win32" ? win32.extname(file) : posix.extname(file)).toLowerCase();
332
+ if (!(platform === "win32" && (extension === ".cmd" || extension === ".bat"))) return Object.freeze({
245
333
  file,
246
334
  arguments: Object.freeze([...command.arguments]),
247
335
  verbatim: false
248
336
  });
249
337
  for (const argument of command.arguments) if (argument.includes("%")) throw createInvalidError("command argument", argument);
250
338
  const line = [`"${file}"`, ...command.arguments.map(quoteArgument)].join(" ");
251
- const environment = options?.environment ?? process.env;
252
339
  return Object.freeze({
253
- file: readVariable(environment, "ComSpec") ?? "cmd.exe",
340
+ file: readPlatformVariable(environment, "ComSpec", platform) ?? "cmd.exe",
254
341
  arguments: Object.freeze([
255
342
  "/d",
256
343
  "/s",
@@ -261,6 +348,22 @@ function buildSpawn(command, options) {
261
348
  });
262
349
  }
263
350
  /**
351
+ * Builds the resolved spawn form of one command for the current host.
352
+ *
353
+ * @param command - The executable and its argument vector
354
+ * @param options - The directory searched first and the child's effective environment
355
+ * @returns The file, argument vector, and verbatim flag to spawn with
356
+ * @throws A {@link ProcessError} coded `invalid` when the host is Windows, the resolved target is a batch script, and an argument carries a percent sign
357
+ *
358
+ * @example
359
+ * ```ts
360
+ * buildSpawn({ file: 'node', arguments: ['--version'] }).verbatim // false
361
+ * ```
362
+ */
363
+ function buildSpawn(command, options) {
364
+ return buildPlatformSpawn(command, resolveExecutable(command.file, options) ?? command.file, options?.environment ?? process.env, platform);
365
+ }
366
+ /**
264
367
  * Validates one spawn-bound string.
265
368
  *
266
369
  * @remarks
@@ -378,40 +481,6 @@ function validateWorkspace(workspace) {
378
481
  validateText(workspace, "workspace", true);
379
482
  }
380
483
  /**
381
- * Retains one delivered chunk up to a byte limit and records what the stream delivered.
382
- *
383
- * @remarks
384
- * The retained head is kept as chunk slices and joined once, so a long stream never repeatedly
385
- * concatenates a growing buffer. `counts` is a two-slot tally: slot `0` counts every delivered byte
386
- * and slot `1` counts the retained bytes, so a caller compares slot `0` against `limit` to learn
387
- * whether the capture was truncated.
388
- *
389
- * @param chunk - The delivered chunk, ignored when it is not a buffer
390
- * @param chunks - The retained head, appended to in place
391
- * @param counts - The `[delivered, retained]` tally, updated in place
392
- * @param limit - The maximum retained byte count
393
- * @returns Nothing
394
- *
395
- * @example
396
- * ```ts
397
- * const chunks: Buffer[] = []
398
- * const counts = [0, 0]
399
- * retainChunk(Buffer.from('hello'), chunks, counts, 3)
400
- * counts[1] // 3
401
- * ```
402
- */
403
- function retainChunk(chunk, chunks, counts, limit) {
404
- if (!Buffer.isBuffer(chunk)) return;
405
- const delivered = counts[0] ?? 0;
406
- const retained = counts[1] ?? 0;
407
- counts[0] = delivered + chunk.byteLength;
408
- const room = limit - retained;
409
- if (room <= 0) return;
410
- const slice = chunk.byteLength <= room ? chunk : Buffer.from(chunk.subarray(0, room));
411
- chunks.push(slice);
412
- counts[1] = retained + slice.byteLength;
413
- }
414
- /**
415
484
  * Checks whether a child process has reached its native exit.
416
485
  *
417
486
  * @remarks
@@ -435,10 +504,11 @@ function isExited(child) {
435
504
  *
436
505
  * @remarks
437
506
  * On a POSIX host a child spawned `detached` leads its own process group, so signalling the
438
- * negated pid reaches the whole tree. On Windows, or when no pid is available, the signal goes to
439
- * the child directly. A throw during signalling is swallowed: the process can exit between the
440
- * caller's liveness check and this call, and the child's native exit remains the authoritative
441
- * terminal state.
507
+ * negated pid reaches the whole tree. When no group owns the pid and the host reports `ESRCH`, the
508
+ * signal falls back to the child directly. On Windows, or when no pid is available, the signal also
509
+ * goes to the child directly. Every other throw during signalling is swallowed: the process can
510
+ * exit between the caller's liveness check and this call, and the child's native exit remains the
511
+ * authoritative terminal state.
442
512
  *
443
513
  * @param child - The child boundary
444
514
  * @param signal - The signal to deliver
@@ -456,7 +526,12 @@ function killProcess(child, signal) {
456
526
  return;
457
527
  }
458
528
  process.kill(-child.pid, signal);
459
- } catch {}
529
+ } catch (error) {
530
+ if (error instanceof Error && "code" in error && error.code === "ESRCH") {
531
+ holds(() => child.kill(signal));
532
+ return;
533
+ }
534
+ }
460
535
  }
461
536
  /**
462
537
  * Kills one Windows process tree through `taskkill`.
@@ -509,24 +584,25 @@ function killTree(pid, timeout) {
509
584
  function waitForExit(child, timeout) {
510
585
  if (isExited(child)) return Promise.resolve();
511
586
  const settled = Promise.withResolvers();
512
- const timer = setTimeout(settled.resolve, timeout);
513
- child.once("exit", () => {
514
- clearTimeout(timer);
587
+ const timer = setTimeout(() => {
588
+ child.off("exit", settled.resolve);
515
589
  settled.resolve();
516
- });
517
- return settled.promise;
590
+ }, timeout);
591
+ child.once("exit", settled.resolve);
592
+ return settled.promise.finally(() => clearTimeout(timer));
518
593
  }
519
594
  /**
520
595
  * Terminates one child process tree and reports whether its native exit was observed.
521
596
  *
522
597
  * @remarks
523
- * Windows ends the tree at once through {@link killTree}, falling back to a direct kill when the
524
- * utility fails, because the host has no cooperative termination to offer. A POSIX host signals the
525
- * process group `SIGTERM`, waits `grace`, then signals `SIGKILL`. No signal is initiated once the
526
- * native exit is observed; the window between initiating a signal and the host delivering it belongs
527
- * to the operating system. `confirm` bounds each awaited step rather than the call as a whole: on
528
- * Windows it bounds the `taskkill` call and then the final wait, and on a POSIX host `grace` bounds
529
- * the cooperative wait and `confirm` the final one. Never rejects.
598
+ * Windows ends the whole tree at once through {@link killTree}, falling back to a direct kill after
599
+ * the utility fails, because the host has no cooperative termination to offer. A POSIX host signals
600
+ * the process group `SIGTERM`, falls back to the direct child when no group owns its pid, waits
601
+ * `grace`, then sends `SIGKILL` through the same route. No signal is initiated after the native exit
602
+ * is observed; the window between initiating a signal and the host delivering it belongs to the
603
+ * operating system. `confirm` bounds each awaited step rather than the call as a whole: on Windows
604
+ * it bounds the `taskkill` call and then the final wait, and on a POSIX host `grace` bounds the
605
+ * cooperative wait and `confirm` the final one. Never rejects.
530
606
  *
531
607
  * @param child - The child boundary
532
608
  * @param grace - The cooperative POSIX window in milliseconds between `SIGTERM` and `SIGKILL`
@@ -552,12 +628,12 @@ async function stopChild(child, grace, confirm) {
552
628
  return isExited(child);
553
629
  }
554
630
  /**
555
- * Builds one settled {@link RunResult} from a completed run's captured bytes and terminal facts.
631
+ * Builds one settled {@link ExecuteResult} from a completed run's captured bytes and terminal facts.
556
632
  *
557
633
  * @remarks
558
634
  * `failed` is derived: a run failed when it timed out, was aborted, ended on a host fault, was ended
559
635
  * by a signal, or exited with a code other than `0` — a `null` code from a spawn fault is therefore
560
- * a failure. Both byte fields are bounded by `limit` on a code-point boundary here, which is the one
636
+ * a failure. Each byte field is bounded by `limit` on a code-point boundary here, which is the one
561
637
  * place the captured bytes are decoded.
562
638
  *
563
639
  * @param input - The captured bytes, terminal state, and capture limit
@@ -565,7 +641,7 @@ async function stopChild(child, grace, confirm) {
565
641
  *
566
642
  * @example
567
643
  * ```ts
568
- * buildRunResult({
644
+ * buildExecuteResult({
569
645
  * command: 'git status',
570
646
  * stdout: Buffer.from('ok'),
571
647
  * stderr: Buffer.alloc(0),
@@ -578,7 +654,7 @@ async function stopChild(child, grace, confirm) {
578
654
  * }).failed // false
579
655
  * ```
580
656
  */
581
- function buildRunResult(input) {
657
+ function buildExecuteResult(input) {
582
658
  return Object.freeze({
583
659
  command: input.command,
584
660
  stdout: trimHead(input.stdout, input.limit).toString("utf8"),
@@ -591,247 +667,6 @@ function buildRunResult(input) {
591
667
  truncated: input.truncated
592
668
  });
593
669
  }
594
- /**
595
- * Runs one command to completion, buffering its output, and settles with the outcome.
596
- *
597
- * @remarks
598
- * The executable is resolved through {@link buildSpawn}, so no run uses a shell. On a POSIX host the
599
- * child is detached, which is what lets its whole process group be terminated. Standard output and
600
- * error are each byte-bounded by `limit` (default {@link PROCESS_OUTPUT}) and `truncated` reports
601
- * whether either stream exceeded it. A positive `timeout` and an aborting `signal` both terminate the
602
- * child through the same bounded stop, and only the first of them is recorded: a timeout reports
603
- * `expired` and an abort reports `aborted`, never both. After termination the outcome is awaited for
604
- * a bounded window, so a descendant holding the child's stdio cannot keep the run pending. That bound
605
- * covers a terminated run alone: a run with no `timeout` and no `signal` settles on stdio completion
606
- * rather than on process exit, so a descendant that inherited the child's stdio holds the run open
607
- * after the child itself has gone. Give such a run a `timeout`. The child's `environment` merges over
608
- * the parent unless the command is `isolated`, then `options.environment` on top, and `options.input`
609
- * overrides `command.input`. Unless `strict` is `false`, a failed run rejects with a
610
- * {@link createRunError} carrying the {@link RunResult}. An invalid option or command string rejects
611
- * before the child is spawned, because an async function cannot throw synchronously.
612
- *
613
- * @param command - The executable, arguments, and optional environment and input
614
- * @param options - Working directory, timeout, grace, signal, capture limit, and failure delivery
615
- * @returns The settled run outcome
616
- * @throws A {@link ProcessError} coded `invalid` for a malformed option, command string, or batch-bound argument, or one carrying the {@link RunResult} when the run failed and `strict` is not `false`
617
- *
618
- * @example
619
- * ```ts
620
- * const result = await run({ file: 'git', arguments: ['status'] }, { workspace: process.cwd() })
621
- * ```
622
- */
623
- async function run(command, options) {
624
- validateCommand(command);
625
- validateEnvironment(options?.environment);
626
- validateWorkspace(options?.workspace);
627
- validateTimer(options?.timeout, "option 'timeout'");
628
- validateTimer(options?.grace, "option 'grace'");
629
- validateBytes(options?.limit, "option 'limit'", 0);
630
- const limit = options?.limit ?? PROCESS_OUTPUT;
631
- const grace = options?.grace ?? PROCESS_GRACE;
632
- const timeout = options?.timeout ?? 0;
633
- const strict = options?.strict ?? true;
634
- const text = options?.input ?? command.input;
635
- const line = formatCommand(command);
636
- const workspace = options?.workspace ?? process.cwd();
637
- const environment = mergeEnvironment(command.isolated === true, command.environment, options?.environment);
638
- const plan = buildSpawn(command, {
639
- workspace,
640
- environment
641
- });
642
- const settled = Promise.withResolvers();
643
- const terminate = new AbortController();
644
- const cleanup = new AbortController();
645
- const finish = new AbortController();
646
- const outChunks = [];
647
- const errChunks = [];
648
- const outCounts = [0, 0];
649
- const errCounts = [0, 0];
650
- let spawned = false;
651
- let expired = false;
652
- let aborted = false;
653
- let cause;
654
- let timeoutTimer;
655
- let confirmTimer;
656
- const child = spawn(plan.file, [...plan.arguments], {
657
- cwd: workspace,
658
- detached: process.platform !== "win32",
659
- env: environment,
660
- stdio: [
661
- "pipe",
662
- "pipe",
663
- "pipe"
664
- ],
665
- windowsHide: true,
666
- windowsVerbatimArguments: plan.verbatim
667
- });
668
- finish.signal.addEventListener("abort", () => {
669
- clearTimeout(timeoutTimer);
670
- clearTimeout(confirmTimer);
671
- cleanup.abort();
672
- settled.resolve(buildRunResult({
673
- command: line,
674
- stdout: Buffer.concat(outChunks),
675
- stderr: Buffer.concat(errChunks),
676
- code: child.exitCode,
677
- signal: child.signalCode,
678
- expired,
679
- aborted,
680
- truncated: (outCounts[0] ?? 0) > limit || (errCounts[0] ?? 0) > limit,
681
- limit,
682
- ...cause === void 0 ? {} : { cause }
683
- }));
684
- }, { once: true });
685
- terminate.signal.addEventListener("abort", () => {
686
- clearTimeout(timeoutTimer);
687
- cleanup.abort();
688
- stopChild(child, grace, PROCESS_CONFIRMATION).then(() => {
689
- if (finish.signal.aborted) return;
690
- confirmTimer = setTimeout(() => finish.abort(), PROCESS_CONFIRMATION);
691
- });
692
- }, { once: true });
693
- child.stdin.on("error", () => void 0);
694
- child.stdout.on("data", (chunk) => retainChunk(chunk, outChunks, outCounts, limit));
695
- child.stderr.on("data", (chunk) => retainChunk(chunk, errChunks, errCounts, limit));
696
- child.once("spawn", () => {
697
- spawned = true;
698
- });
699
- child.once("error", (error) => {
700
- if (spawned) return;
701
- cause = error;
702
- finish.abort();
703
- });
704
- child.once("close", () => finish.abort());
705
- if (text !== void 0) child.stdin.write(text);
706
- child.stdin.end();
707
- if (timeout > 0) timeoutTimer = setTimeout(() => {
708
- expired = true;
709
- terminate.abort();
710
- }, timeout);
711
- if (options?.signal !== void 0) {
712
- const signal = options.signal;
713
- signal.addEventListener("abort", () => {
714
- aborted = true;
715
- terminate.abort();
716
- }, {
717
- once: true,
718
- signal: cleanup.signal
719
- });
720
- if (signal.aborted) {
721
- aborted = true;
722
- terminate.abort();
723
- }
724
- }
725
- const result = await settled.promise;
726
- if (result.failed && strict) throw createRunError(result, cause);
727
- return result;
728
- }
729
- /**
730
- * Runs one command to completion synchronously, buffering its output, and returns the outcome.
731
- *
732
- * @remarks
733
- * The synchronous counterpart of {@link run}, spawned through the same resolver and never through a
734
- * shell. The host offers no cooperative termination window and no in-flight cancellation, so this
735
- * contract carries neither. A positive `timeout` and an output overflow both end the child with
736
- * `SIGKILL`: an overflow reports `truncated` and `failed` together and trims the partial output to
737
- * `limit`, where {@link run} keeps reading and reports `truncated` without failing. The environment
738
- * and input follow the same merge as {@link run}. Unless `strict` is `false`, a failed run throws a
739
- * {@link createRunError} carrying the {@link RunResult}.
740
- *
741
- * @param command - The executable, arguments, and optional environment and input
742
- * @param options - Working directory, timeout, capture limit, and failure delivery
743
- * @returns The run outcome
744
- * @throws A {@link ProcessError} coded `invalid` for a malformed option, command string, or batch-bound argument, or one carrying the {@link RunResult} when the run failed and `strict` is not `false`
745
- *
746
- * @example
747
- * ```ts
748
- * const result = runSync({ file: 'git', arguments: ['--version'] }, { strict: false })
749
- * ```
750
- */
751
- function runSync(command, options) {
752
- validateCommand(command);
753
- validateEnvironment(options?.environment);
754
- validateWorkspace(options?.workspace);
755
- validateTimer(options?.timeout, "option 'timeout'");
756
- validateBytes(options?.limit, "option 'limit'", 0);
757
- const limit = options?.limit ?? PROCESS_OUTPUT;
758
- const timeout = options?.timeout ?? 0;
759
- const strict = options?.strict ?? true;
760
- const text = options?.input ?? command.input;
761
- const line = formatCommand(command);
762
- const workspace = options?.workspace ?? process.cwd();
763
- const environment = mergeEnvironment(command.isolated === true, command.environment, options?.environment);
764
- const plan = buildSpawn(command, {
765
- workspace,
766
- environment
767
- });
768
- const outcome = spawnSync(plan.file, [...plan.arguments], {
769
- cwd: workspace,
770
- env: environment,
771
- encoding: "buffer",
772
- maxBuffer: limit,
773
- killSignal: "SIGKILL",
774
- windowsHide: true,
775
- windowsVerbatimArguments: plan.verbatim,
776
- ...text !== void 0 ? { input: text } : {},
777
- ...timeout > 0 ? { timeout } : {}
778
- });
779
- const error = outcome.error;
780
- const fault = error !== void 0 && "code" in error && isString(error.code) ? error.code : "";
781
- const result = buildRunResult({
782
- command: line,
783
- stdout: Buffer.isBuffer(outcome.stdout) ? outcome.stdout : Buffer.alloc(0),
784
- stderr: Buffer.isBuffer(outcome.stderr) ? outcome.stderr : Buffer.alloc(0),
785
- code: outcome.status,
786
- signal: outcome.signal,
787
- expired: fault === "ETIMEDOUT",
788
- aborted: false,
789
- truncated: fault === "ENOBUFS",
790
- limit,
791
- ...error === void 0 ? {} : { cause: error }
792
- });
793
- if (result.failed && strict) throw createRunError(result, error);
794
- return result;
795
- }
796
- /**
797
- * Spawns one command as a detached process and returns without waiting for it.
798
- *
799
- * @remarks
800
- * The child owns no stdio and is unreferenced, so it outlives this process and nothing here observes
801
- * its outcome. The error listener is attached before the child is unreferenced, so a post-spawn host
802
- * fault is swallowed rather than crashing the caller; an invalid command is refused before anything
803
- * is spawned.
804
- *
805
- * @param command - The executable, arguments, and optional environment
806
- * @param options - The working directory the detached child starts in
807
- * @returns Nothing
808
- * @throws A {@link ProcessError} coded `invalid` when the working directory or a command string is malformed
809
- *
810
- * @example
811
- * ```ts
812
- * detach({ file: 'node', arguments: ['daemon.js'] }, { workspace: process.cwd() })
813
- * ```
814
- */
815
- function detach(command, options) {
816
- validateCommand(command);
817
- validateWorkspace(options?.workspace);
818
- const workspace = options?.workspace ?? process.cwd();
819
- const environment = mergeEnvironment(command.isolated === true, command.environment);
820
- const plan = buildSpawn(command, {
821
- workspace,
822
- environment
823
- });
824
- const child = spawn(plan.file, [...plan.arguments], {
825
- cwd: workspace,
826
- detached: true,
827
- env: environment,
828
- stdio: "ignore",
829
- windowsHide: true,
830
- windowsVerbatimArguments: plan.verbatim
831
- });
832
- child.once("error", () => void 0);
833
- child.unref();
834
- }
835
670
  //#endregion
836
671
  //#region src/server/Process.ts
837
672
  /**
@@ -839,20 +674,22 @@ function detach(command, options) {
839
674
  *
840
675
  * @remarks
841
676
  * Standard output is framed through `readline` into a single-consumer stream: each line goes to
842
- * exactly one waiting iterator, so two iterators split the output rather than each receiving all of
843
- * it. While no consumer has ever requested an iterator,
844
- * stdout keeps draining so `exit` still resolves, and retention stops at the `backlog` mark: a
845
- * consumer attaching later receives the retained head, a gap, then the live stream. Once an iterator
677
+ * exactly one waiting iterator, so concurrent iterators split the output rather than each receiving
678
+ * all of it. While no consumer has ever requested an iterator, stdout keeps draining so `exit` still
679
+ * resolves, and retention stops at the `backlog` mark: a
680
+ * consumer attaching later receives the retained head, a gap, then the live stream. After an iterator
846
681
  * has been requested, stdout pauses at the mark and resumes at half of it, so that consumer loses
847
- * nothing and the child feels real backpressure. The mark is soft — the host delivers whole chunks,
848
- * so the backlog overshoots it by up to one delivered chunk plus the drain that termination
849
- * releases. Standard error is decoded and forwarded live as the `stderr` event while a byte-bounded
850
- * raw tail is retained as `evidence`. The typed `emitter` also carries the child `error` cause on a
851
- * spawn fault and the terminal `exit`, alongside the `exit` promise. `stop` ends the whole tree and
852
- * reports whether the native exit was observed; `destroy` stops the child, destroys stdin so every
853
- * pending `send` settles, and destroys the emitter last. `destroy` resolving does not imply the
854
- * child's stdio has closed, so `exit` and `lines` can still be outstanding when a descendant holds
855
- * an inherited pipe.
682
+ * nothing before termination and the child feels real backpressure. The mark is soft — the ordinary backlog can
683
+ * overshoot it by the line that crossed it plus the rest of its delivered chunk. Termination never
684
+ * reapplies backpressure, and retained lines are capped at twice `backlog`; later lines are dropped
685
+ * and `truncated` reports the omission. Standard error is decoded and forwarded live as the `stderr`
686
+ * event while a byte-bounded raw tail is retained as `evidence`. The typed `emitter` also carries the
687
+ * child `error` cause on a spawn fault and the terminal `exit`, alongside the `exit` promise. `pid`,
688
+ * `code`, and `signal` read the spawned child directly, so the native exit is readable before the
689
+ * `exit` promise settles on stdio close. `stop` ends the whole tree and reports whether the native
690
+ * exit was observed; `destroy` stops the child, destroys stdin so every pending `send` settles, and
691
+ * destroys the emitter last. `destroy` resolving does not imply the child's stdio has closed, so
692
+ * `exit` and `lines` can still be outstanding when a descendant holds an inherited pipe.
856
693
  *
857
694
  * @example
858
695
  * ```ts
@@ -885,6 +722,7 @@ var Process = class {
885
722
  #pending = 0;
886
723
  #requested = false;
887
724
  #paused = false;
725
+ #truncated = false;
888
726
  #terminating = false;
889
727
  #closed = false;
890
728
  #ended = false;
@@ -897,7 +735,7 @@ var Process = class {
897
735
  * @throws A {@link ProcessError} coded `invalid` when an option or command string is malformed
898
736
  */
899
737
  constructor(options) {
900
- const command = options.command;
738
+ const source = options.command;
901
739
  const workspace = options.workspace;
902
740
  const grace = options.grace;
903
741
  const evidence = options.evidence;
@@ -906,8 +744,7 @@ var Process = class {
906
744
  const signal = options.signal;
907
745
  const on = options.on;
908
746
  const error = options.error;
909
- const isolated = command.isolated;
910
- const overrides = command.environment;
747
+ const command = snapshotCommand(source);
911
748
  const input = command.input;
912
749
  validateCommand(command);
913
750
  validateWorkspace(workspace);
@@ -923,15 +760,15 @@ var Process = class {
923
760
  this.#backlog = backlog ?? PROCESS_BACKLOG;
924
761
  this.#signal = signal;
925
762
  this.#lines = Object.freeze({ [Symbol.asyncIterator]: this.#iterate.bind(this) });
926
- const environment = mergeEnvironment(isolated === true, overrides);
763
+ const childEnvironment = mergeEnvironment(command.isolated === true, command.environment);
927
764
  const plan = buildSpawn(command, {
928
765
  workspace,
929
- environment
766
+ environment: childEnvironment
930
767
  });
931
768
  this.#child = spawn(plan.file, [...plan.arguments], {
932
769
  cwd: workspace,
933
770
  detached: process.platform !== "win32",
934
- env: environment,
771
+ env: childEnvironment,
935
772
  stdio: [
936
773
  "pipe",
937
774
  "pipe",
@@ -958,6 +795,18 @@ var Process = class {
958
795
  if (signal.aborted) this.stop();
959
796
  }
960
797
  }
798
+ /** The host process id, fixed when construction returns, or `undefined` when the spawn produced none. */
799
+ get pid() {
800
+ return this.#child.pid;
801
+ }
802
+ /** The exit code the host recorded, or `null` while the child has not exited and when a signal ended it. */
803
+ get code() {
804
+ return this.#child.exitCode;
805
+ }
806
+ /** The terminating signal name the host recorded, or `null` while the child has not exited and when it exited on its own. */
807
+ get signal() {
808
+ return this.#child.signalCode;
809
+ }
961
810
  /** The typed lifecycle observation surface. */
962
811
  get emitter() {
963
812
  return this.#emitter;
@@ -970,6 +819,10 @@ var Process = class {
970
819
  get evidence() {
971
820
  return this.#tail.toString("utf8");
972
821
  }
822
+ /** True when the `lines` stream omitted output after a retention bound was reached. */
823
+ get truncated() {
824
+ return this.#truncated;
825
+ }
973
826
  /** The terminal child state, observed once from the close event. */
974
827
  get exit() {
975
828
  return this.#exit.promise;
@@ -1068,7 +921,11 @@ var Process = class {
1068
921
  return;
1069
922
  }
1070
923
  const bytes = Buffer.byteLength(line) + 1;
1071
- if (!this.#requested && this.#pending + bytes > this.#backlog) return;
924
+ const limit = this.#terminating ? this.#backlog * 2 : this.#backlog;
925
+ if ((!this.#requested || this.#terminating) && this.#pending + bytes > limit) {
926
+ this.#truncated = true;
927
+ return;
928
+ }
1072
929
  this.#queue.push(line);
1073
930
  this.#pending += bytes;
1074
931
  this.#restrain();
@@ -1106,7 +963,7 @@ var Process = class {
1106
963
  const suffix = this.#decoder.end();
1107
964
  if (suffix.length > 0) this.#emitter.emit("stderr", suffix);
1108
965
  this.#closed = true;
1109
- if (this.#signal !== void 0 && this.#abort !== void 0) this.#signal.removeEventListener("abort", this.#abort);
966
+ this.#removeAbortListener();
1110
967
  this.#reader.close();
1111
968
  const exit = Object.freeze({
1112
969
  code,
@@ -1118,14 +975,29 @@ var Process = class {
1118
975
  #terminate() {
1119
976
  this.stop();
1120
977
  }
978
+ #removeAbortListener() {
979
+ if (this.#signal === void 0 || this.#abort === void 0) return;
980
+ this.#signal.removeEventListener("abort", this.#abort);
981
+ }
982
+ #capBacklog() {
983
+ const limit = this.#backlog * 2;
984
+ while (this.#pending > limit && this.#queue.length > this.#head) {
985
+ const line = this.#queue.pop();
986
+ if (line === void 0) break;
987
+ this.#pending -= Buffer.byteLength(line) + 1;
988
+ this.#truncated = true;
989
+ }
990
+ }
1121
991
  async #kill() {
1122
992
  this.#terminating = true;
993
+ this.#capBacklog();
1123
994
  this.#relieve();
1124
995
  const confirmed = await stopChild(this.#child, this.#grace, PROCESS_CONFIRMATION);
1125
996
  this.#child.stdin.destroy();
1126
997
  return confirmed;
1127
998
  }
1128
999
  async #teardown() {
1000
+ this.#removeAbortListener();
1129
1001
  await this.stop();
1130
1002
  this.#emitter.destroy();
1131
1003
  }
@@ -1213,8 +1085,9 @@ var ProcessManager = class {
1213
1085
  * nothing has started and a throw from one strands no process. A getter that begins `destroy`
1214
1086
  * without throwing is the one remaining race, and it leaves a bounded residual: the child is
1215
1087
  * already spawned, so the launch is refused with a {@link createProtocolError} and that child is
1216
- * torn down asynchronously, bounded by `grace` plus the confirmation window. The `destroy` barrier
1217
- * settled before the refusal, so awaiting it does not cover that teardown.
1088
+ * torn down asynchronously, bounded by `grace` plus the confirmation window. The `protocol`
1089
+ * refusal throws synchronously before the `destroy` barrier settles. The barrier does not cover
1090
+ * that child's asynchronous teardown.
1218
1091
  *
1219
1092
  * @param id - The registry key, unique among live children
1220
1093
  * @param options - The child construction options
@@ -1328,6 +1201,318 @@ function createProcessManager(options) {
1328
1201
  return new ProcessManager(options);
1329
1202
  }
1330
1203
  //#endregion
1331
- export { Process, ProcessManager, buildRunResult, buildSpawn, createProcess, createProcessManager, detach, formatCommand, isExited, isFile, killProcess, killTree, mergeEnvironment, quoteArgument, readVariable, resolveExecutable, retainChunk, run, runSync, stopChild, trimHead, trimTail, validateBytes, validateCommand, validateEnvironment, validateText, validateTimer, validateWorkspace, waitForExit };
1204
+ //#region src/server/Retention.ts
1205
+ /**
1206
+ * Accumulates byte totals while retaining a bounded stream head.
1207
+ *
1208
+ * @example
1209
+ * ```ts
1210
+ * const retention = new Retention()
1211
+ * retention.retain(Buffer.from('hello'), 3)?.toString('utf8') // 'hel'
1212
+ * retention.retained // 3
1213
+ * ```
1214
+ */
1215
+ var Retention = class {
1216
+ #delivered = 0;
1217
+ #retained = 0;
1218
+ /** The bytes delivered by the stream. */
1219
+ get delivered() {
1220
+ return this.#delivered;
1221
+ }
1222
+ /** The bytes retained from the stream head. */
1223
+ get retained() {
1224
+ return this.#retained;
1225
+ }
1226
+ /**
1227
+ * Retains a delivered chunk within a byte limit.
1228
+ *
1229
+ * @param chunk - The delivered chunk, ignored when it is not a buffer
1230
+ * @param limit - The maximum retained byte count
1231
+ * @returns The retained slice, or `undefined` when the chunk contributes no retained bytes
1232
+ */
1233
+ retain(chunk, limit) {
1234
+ if (!Buffer.isBuffer(chunk)) return void 0;
1235
+ this.#delivered += chunk.byteLength;
1236
+ const room = limit - this.#retained;
1237
+ if (room <= 0) return void 0;
1238
+ const slice = chunk.byteLength <= room ? chunk : Buffer.from(chunk.subarray(0, room));
1239
+ this.#retained += slice.byteLength;
1240
+ return slice;
1241
+ }
1242
+ };
1243
+ //#endregion
1244
+ //#region src/server/execution/execute.ts
1245
+ /**
1246
+ * Runs one command to completion, buffering its output, and settles with the outcome.
1247
+ *
1248
+ * @remarks
1249
+ * The executable is resolved through {@link buildSpawn}, so no run uses a shell. On a POSIX host the
1250
+ * child is detached, which is what lets its whole process group be terminated. Standard output and
1251
+ * error are each byte-bounded by `limit` (default {@link PROCESS_OUTPUT}) and `truncated` reports
1252
+ * whether either stream exceeded it. A positive `timeout` and an aborting `signal` both terminate the
1253
+ * child through the same bounded stop, and only the earliest is recorded: a timeout reports
1254
+ * `expired` and an abort reports `aborted`, never both. After termination the outcome is awaited for
1255
+ * a bounded window, so a descendant holding the child's stdio cannot keep the run pending. That bound
1256
+ * covers a terminated run alone: a run with no `timeout` and no `signal` settles on stdio completion
1257
+ * rather than on process exit, so a descendant that inherited the child's stdio holds the run open
1258
+ * after the child itself has gone. Give such a run a `timeout`. The child's `environment` merges over
1259
+ * the parent unless the command is `isolated`, then `options.environment` on top, and `options.input`
1260
+ * overrides `command.input`. Unless `strict` is `false`, a failed run rejects with a
1261
+ * {@link createExecuteError} carrying the {@link ExecuteResult}. An invalid option or command string rejects
1262
+ * before the child is spawned, because an async function cannot throw synchronously.
1263
+ *
1264
+ * @param command - The executable, arguments, and optional environment and input
1265
+ * @param options - Working directory, timeout, grace, signal, capture limit, and failure delivery
1266
+ * @returns The settled run outcome
1267
+ * @throws A {@link ProcessError} coded `invalid` for a malformed option, command string, or batch-bound argument, or one carrying the {@link ExecuteResult} when the run failed and `strict` is not `false`
1268
+ *
1269
+ * @example
1270
+ * ```ts
1271
+ * const result = await execute({ file: 'git', arguments: ['status'] }, { workspace: process.cwd() })
1272
+ * ```
1273
+ */
1274
+ async function execute(command, options) {
1275
+ const snapshot = snapshotCommand(command);
1276
+ const optionEnvironment = options?.environment;
1277
+ const optionWorkspace = options?.workspace;
1278
+ const optionInput = options?.input;
1279
+ const optionTimeout = options?.timeout;
1280
+ const optionGrace = options?.grace;
1281
+ const optionSignal = options?.signal;
1282
+ const optionStrict = options?.strict;
1283
+ const optionLimit = options?.limit;
1284
+ validateCommand(snapshot);
1285
+ validateEnvironment(optionEnvironment);
1286
+ validateWorkspace(optionWorkspace);
1287
+ validateTimer(optionTimeout, "option 'timeout'");
1288
+ validateTimer(optionGrace, "option 'grace'");
1289
+ validateBytes(optionLimit, "option 'limit'", 0);
1290
+ const limit = optionLimit ?? PROCESS_OUTPUT;
1291
+ const grace = optionGrace ?? PROCESS_GRACE;
1292
+ const timeout = optionTimeout ?? 0;
1293
+ const strict = optionStrict ?? true;
1294
+ const text = optionInput ?? snapshot.input;
1295
+ const line = formatCommand(snapshot);
1296
+ const workspace = optionWorkspace ?? process.cwd();
1297
+ const environment = mergeEnvironment(snapshot.isolated === true, snapshot.environment, optionEnvironment);
1298
+ const plan = buildSpawn(snapshot, {
1299
+ workspace,
1300
+ environment
1301
+ });
1302
+ const settled = Promise.withResolvers();
1303
+ const terminate = new AbortController();
1304
+ const cleanup = new AbortController();
1305
+ const finish = new AbortController();
1306
+ const outChunks = [];
1307
+ const errChunks = [];
1308
+ const outRetention = new Retention();
1309
+ const errRetention = new Retention();
1310
+ let spawned = false;
1311
+ let expired = false;
1312
+ let aborted = false;
1313
+ let cause;
1314
+ let timeoutTimer;
1315
+ let confirmTimer;
1316
+ const child = spawn(plan.file, [...plan.arguments], {
1317
+ cwd: workspace,
1318
+ detached: process.platform !== "win32",
1319
+ env: environment,
1320
+ stdio: [
1321
+ "pipe",
1322
+ "pipe",
1323
+ "pipe"
1324
+ ],
1325
+ windowsHide: true,
1326
+ windowsVerbatimArguments: plan.verbatim
1327
+ });
1328
+ finish.signal.addEventListener("abort", () => {
1329
+ clearTimeout(timeoutTimer);
1330
+ clearTimeout(confirmTimer);
1331
+ cleanup.abort();
1332
+ settled.resolve(buildExecuteResult({
1333
+ command: line,
1334
+ stdout: Buffer.concat(outChunks),
1335
+ stderr: Buffer.concat(errChunks),
1336
+ code: child.exitCode,
1337
+ signal: child.signalCode,
1338
+ expired,
1339
+ aborted,
1340
+ truncated: outRetention.delivered > limit || errRetention.delivered > limit,
1341
+ limit,
1342
+ ...cause === void 0 ? {} : { cause }
1343
+ }));
1344
+ }, { once: true });
1345
+ terminate.signal.addEventListener("abort", () => {
1346
+ clearTimeout(timeoutTimer);
1347
+ cleanup.abort();
1348
+ stopChild(child, grace, PROCESS_CONFIRMATION).then(() => {
1349
+ if (finish.signal.aborted) return;
1350
+ confirmTimer = setTimeout(() => finish.abort(), PROCESS_CONFIRMATION);
1351
+ });
1352
+ }, { once: true });
1353
+ child.stdin.on("error", () => void 0);
1354
+ child.stdout.on("data", (chunk) => {
1355
+ const retained = outRetention.retain(chunk, limit);
1356
+ if (retained !== void 0) outChunks.push(retained);
1357
+ });
1358
+ child.stderr.on("data", (chunk) => {
1359
+ const retained = errRetention.retain(chunk, limit);
1360
+ if (retained !== void 0) errChunks.push(retained);
1361
+ });
1362
+ child.once("spawn", () => {
1363
+ spawned = true;
1364
+ });
1365
+ child.once("error", (error) => {
1366
+ if (spawned) return;
1367
+ cause = error;
1368
+ finish.abort();
1369
+ });
1370
+ child.once("close", () => finish.abort());
1371
+ if (text !== void 0) child.stdin.write(text);
1372
+ child.stdin.end();
1373
+ if (timeout > 0) timeoutTimer = setTimeout(() => {
1374
+ expired = true;
1375
+ terminate.abort();
1376
+ }, timeout);
1377
+ if (optionSignal !== void 0) {
1378
+ optionSignal.addEventListener("abort", () => {
1379
+ aborted = true;
1380
+ terminate.abort();
1381
+ }, {
1382
+ once: true,
1383
+ signal: cleanup.signal
1384
+ });
1385
+ if (optionSignal.aborted) {
1386
+ aborted = true;
1387
+ terminate.abort();
1388
+ }
1389
+ }
1390
+ const result = await settled.promise;
1391
+ if (result.failed && strict) throw createExecuteError(result, cause);
1392
+ return result;
1393
+ }
1394
+ //#endregion
1395
+ //#region src/server/execution/executeSync.ts
1396
+ /**
1397
+ * Runs one command to completion synchronously, buffering its output, and returns the outcome.
1398
+ *
1399
+ * @remarks
1400
+ * The synchronous counterpart of {@link execute}, spawned through the same resolver and never through a
1401
+ * shell. The host offers no cooperative termination window and no in-flight cancellation, so this
1402
+ * contract carries neither. A positive `timeout` ends only the root process and can leave
1403
+ * descendants running; use {@link execute} or {@link Process} when timeout must terminate the tree. A
1404
+ * timeout and an output overflow both end the root with `SIGKILL`: an overflow reports `truncated`
1405
+ * and `failed` together and trims the partial output to `limit`, where {@link execute} keeps reading and
1406
+ * reports `truncated` without failing. The environment and input follow the same merge as
1407
+ * {@link execute}. Unless `strict` is `false`, a failed run throws a {@link createExecuteError}
1408
+ * carrying the {@link ExecuteResult}.
1409
+ *
1410
+ * @param command - The executable, arguments, and optional environment and input
1411
+ * @param options - Working directory, timeout, capture limit, and failure delivery
1412
+ * @returns The run outcome
1413
+ * @throws A {@link ProcessError} coded `invalid` for a malformed option, command string, or batch-bound argument, or one carrying the {@link ExecuteResult} when the run failed and `strict` is not `false`
1414
+ *
1415
+ * @example
1416
+ * ```ts
1417
+ * const result = executeSync({ file: 'git', arguments: ['--version'] }, { strict: false })
1418
+ * ```
1419
+ */
1420
+ function executeSync(command, options) {
1421
+ const snapshot = snapshotCommand(command);
1422
+ const optionEnvironment = options?.environment;
1423
+ const optionWorkspace = options?.workspace;
1424
+ const optionInput = options?.input;
1425
+ const optionTimeout = options?.timeout;
1426
+ const optionStrict = options?.strict;
1427
+ const optionLimit = options?.limit;
1428
+ validateCommand(snapshot);
1429
+ validateEnvironment(optionEnvironment);
1430
+ validateWorkspace(optionWorkspace);
1431
+ validateTimer(optionTimeout, "option 'timeout'");
1432
+ validateBytes(optionLimit, "option 'limit'", 0);
1433
+ const limit = optionLimit ?? PROCESS_OUTPUT;
1434
+ const timeout = optionTimeout ?? 0;
1435
+ const strict = optionStrict ?? true;
1436
+ const text = optionInput ?? snapshot.input;
1437
+ const line = formatCommand(snapshot);
1438
+ const workspace = optionWorkspace ?? process.cwd();
1439
+ const environment = mergeEnvironment(snapshot.isolated === true, snapshot.environment, optionEnvironment);
1440
+ const plan = buildSpawn(snapshot, {
1441
+ workspace,
1442
+ environment
1443
+ });
1444
+ const outcome = spawnSync(plan.file, [...plan.arguments], {
1445
+ cwd: workspace,
1446
+ env: environment,
1447
+ encoding: "buffer",
1448
+ maxBuffer: limit,
1449
+ killSignal: "SIGKILL",
1450
+ windowsHide: true,
1451
+ windowsVerbatimArguments: plan.verbatim,
1452
+ ...text !== void 0 ? { input: Buffer.from(text) } : {},
1453
+ ...timeout > 0 ? { timeout } : {}
1454
+ });
1455
+ const error = outcome.error;
1456
+ const fault = error !== void 0 && "code" in error && isString(error.code) ? error.code : "";
1457
+ const result = buildExecuteResult({
1458
+ command: line,
1459
+ stdout: Buffer.isBuffer(outcome.stdout) ? outcome.stdout : Buffer.alloc(0),
1460
+ stderr: Buffer.isBuffer(outcome.stderr) ? outcome.stderr : Buffer.alloc(0),
1461
+ code: outcome.status,
1462
+ signal: outcome.signal,
1463
+ expired: fault === "ETIMEDOUT",
1464
+ aborted: false,
1465
+ truncated: fault === "ENOBUFS",
1466
+ limit,
1467
+ ...error === void 0 ? {} : { cause: error }
1468
+ });
1469
+ if (result.failed && strict) throw createExecuteError(result, error);
1470
+ return result;
1471
+ }
1472
+ //#endregion
1473
+ //#region src/server/execution/detach.ts
1474
+ /**
1475
+ * Spawns one command as a detached process and returns without waiting for it.
1476
+ *
1477
+ * @remarks
1478
+ * The child owns no stdio and is unreferenced, so it outlives this process and nothing here observes
1479
+ * its outcome. The error listener is attached before the child is unreferenced, so a post-spawn host
1480
+ * fault is swallowed rather than crashing the caller; an invalid command is refused before anything
1481
+ * is spawned.
1482
+ *
1483
+ * @param command - The executable, arguments, and optional environment
1484
+ * @param options - The working directory the detached child starts in
1485
+ * @returns Nothing
1486
+ * @throws A {@link ProcessError} coded `invalid` when the working directory or a command string is malformed
1487
+ *
1488
+ * @example
1489
+ * ```ts
1490
+ * detach({ file: 'node', arguments: ['daemon.js'] }, { workspace: process.cwd() })
1491
+ * ```
1492
+ */
1493
+ function detach(command, options) {
1494
+ const snapshot = snapshotCommand(command);
1495
+ const optionWorkspace = options?.workspace;
1496
+ validateCommand(snapshot);
1497
+ validateWorkspace(optionWorkspace);
1498
+ const workspace = optionWorkspace ?? process.cwd();
1499
+ const environment = mergeEnvironment(snapshot.isolated === true, snapshot.environment);
1500
+ const plan = buildSpawn(snapshot, {
1501
+ workspace,
1502
+ environment
1503
+ });
1504
+ const child = spawn(plan.file, [...plan.arguments], {
1505
+ cwd: workspace,
1506
+ detached: true,
1507
+ env: environment,
1508
+ stdio: "ignore",
1509
+ windowsHide: true,
1510
+ windowsVerbatimArguments: plan.verbatim
1511
+ });
1512
+ child.once("error", () => void 0);
1513
+ child.unref();
1514
+ }
1515
+ //#endregion
1516
+ export { Process, ProcessManager, Retention, buildExecutableCandidates, buildExecuteResult, buildPlatformSpawn, buildSpawn, createProcess, createProcessManager, detach, execute, executeSync, formatCommand, isExited, isFile, killProcess, killTree, mergeEnvironment, mergePlatformEnvironment, quoteArgument, readPlatformVariable, readVariable, resolveExecutable, snapshotCommand, stopChild, trimHead, trimTail, validateBytes, validateCommand, validateEnvironment, validateText, validateTimer, validateWorkspace, waitForExit };
1332
1517
 
1333
1518
  //# sourceMappingURL=index.js.map