File size: 23,212 Bytes
3144483
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
import process from "node:process";
import { expectDefined } from "@openclaw/normalization-core";
import { toErrorObject } from "@openclaw/normalization-core/error-coercion";
import { resolveTimerTimeoutMs } from "@openclaw/normalization-core/number-coercion";
import { isPromiseLike } from "@openclaw/normalization-core/promise-like";
import {
  decodeWindowsOutputBuffer,
  resolveWindowsConsoleEncoding,
} from "../infra/windows-encoding.js";
import { releaseChildProcessOutputAfterExit } from "./child-process.js";
import {
  appendCapturedOutput,
  appendPreservedOutputLines,
  createCapturedOutputBuffers,
  finalizeCapturedOutput,
  flushPreservedOutputLine,
  MAX_PRESERVED_PENDING_LINE_BYTES,
  resolveMaxOutputBytes,
  resolveOutputCapture,
  shouldTerminateOnOutputError,
  shouldTerminateOnOutputLimit,
  type CapturedOutputBuffers,
  type CommandOutputCaptureMode,
  type CommandOutputCaptureOption,
  type CommandOutputErrorOption,
  type CommandOutputLimitOption,
  type CommandOutputStream,
  type PreserveOutputLine,
} from "./exec-output.js";
import {
  createSanitizedCommandError,
  isPlainCommandExitFailure,
  isPlainCommandSignalFailure,
  resolveProcessExitCode,
  TIMEOUT_EXIT_CODE,
  type SpawnResult,
} from "./exec-result.js";
import {
  COMMAND_PROCESS_TREE_KILL_GRACE_MS,
  resolveCommandProcessSignal,
  spawnCommandWithInvocation,
} from "./exec-spawn.js";
import { createCommandTerminationController } from "./exec-termination.js";

const WINDOWS_CLOSE_STATE_SETTLE_TIMEOUT_MS = 250;
const WINDOWS_CLOSE_STATE_POLL_MS = 10;

type CommandTerminationReason = SpawnResult["termination"] | "output-limit";

export type CommandOptions = {
  timeoutMs?: number;
  cwd?: string;
  input?: string | Uint8Array;
  /** Synchronous admission with the spawned PID and argv, before input is released. */
  beforeInput?: (pid: number, argv?: readonly string[]) => void;
  baseEnv?: NodeJS.ProcessEnv;
  env?: NodeJS.ProcessEnv;
  windowsVerbatimArguments?: boolean;
  noOutputTimeoutMs?: number;
  signal?: AbortSignal;
  maxOutputBytes?: number | { stdout?: number; stderr?: number };
  maxCombinedOutputBytes?: number;
  outputCapture?: CommandOutputCaptureOption;
  /** Observe raw output without owning child lifecycle. Return false to stop the command. */
  onOutputChunk?: (chunk: Buffer, stream: CommandOutputStream) => boolean | void;
  /** Accept a successful exit when only the selected diagnostic output stream failed. */
  tolerateOutputError?: { stdout?: boolean; stderr?: boolean };
  /** Terminate when the selected output stream emits an error. */
  terminateOnOutputError?: CommandOutputErrorOption;
  terminateOnOutputLimit?: CommandOutputLimitOption;
  maxPreservedOutputLines?: number;
  preserveOutputLine?: PreserveOutputLine;
  killProcessTree?: boolean;
  /** Join owned descendants even after a successful root exits. */
  requireProcessTreeExtinction?: boolean;
  /** Initial signal for direct-child and graceful process-group cancellation. */
  killSignal?: NodeJS.Signals | number;
  /** Grace between graceful termination and the force-kill fallback. */
  killGraceMs?: number;
};

export async function runCommandWithTimeout(
  argv: string[],
  optionsOrTimeout: number | CommandOptions,
): Promise<SpawnResult> {
  return await runCommandWithOutputEncoding(argv, optionsOrTimeout, false);
}

/** Run a command whose stdout and stderr are defined to be UTF-8 on every platform. */
export async function runUtf8CommandWithTimeout(
  argv: string[],
  optionsOrTimeout: number | CommandOptions,
): Promise<SpawnResult> {
  return await runCommandWithOutputEncoding(argv, optionsOrTimeout, true);
}

export type BufferSpawnResult = Omit<SpawnResult, "stdout" | "stderr"> & {
  stdout: Buffer;
  stderr: Buffer;
  windowsEncoding: string | null;
};

/** Preserve the ordinary process lifecycle while deferring decoding to its consumer. */
export async function runCommandBuffersWithTimeout(
  argv: string[],
  optionsOrTimeout: number | CommandOptions,
): Promise<BufferSpawnResult> {
  return await runCommandWithOutputEncoding(argv, optionsOrTimeout, false, true);
}

async function runCommandWithOutputEncoding(
  argv: string[],
  optionsOrTimeout: number | CommandOptions,
  forceUtf8: boolean,
): Promise<SpawnResult>;
async function runCommandWithOutputEncoding(
  argv: string[],
  optionsOrTimeout: number | CommandOptions,
  forceUtf8: boolean,
  raw: true,
): Promise<BufferSpawnResult>;
async function runCommandWithOutputEncoding(
  argv: string[],
  optionsOrTimeout: number | CommandOptions,
  forceUtf8: boolean,
  raw = false,
): Promise<SpawnResult | BufferSpawnResult> {
  const options: CommandOptions =
    typeof optionsOrTimeout === "number" ? { timeoutMs: optionsOrTimeout } : optionsOrTimeout;
  const {
    timeoutMs,
    cwd,
    input,
    baseEnv,
    env,
    noOutputTimeoutMs,
    killProcessTree,
    killSignal,
    killGraceMs,
  } = options;
  const signal = resolveCommandProcessSignal(options.signal);
  const resolvedTimeoutMs =
    typeof timeoutMs === "number" ? resolveTimerTimeoutMs(timeoutMs, 1) : undefined;
  if (options.requireProcessTreeExtinction && !killProcessTree) {
    throw new Error("Process-tree extinction requires process-tree ownership");
  }
  const hasInput = input !== undefined;
  if (options.beforeInput && !hasInput) {
    throw new Error("Child input admission requires explicit input");
  }
  const resolvedKillGraceMs = resolveTimerTimeoutMs(
    killGraceMs,
    COMMAND_PROCESS_TREE_KILL_GRACE_MS,
    0,
  );

  if (signal?.aborted) {
    const interrupted = {
      code: null,
      signal: null,
      killed: false,
      termination: "signal" as const,
      cleanup: "normal" as const,
      noOutputTimedOut: false,
    };
    return raw
      ? { ...interrupted, stdout: Buffer.alloc(0), stderr: Buffer.alloc(0), windowsEncoding: null }
      : { ...interrupted, stdout: "", stderr: "" };
  }

  const stdoutCapture = createCapturedOutputBuffers();
  const stderrCapture = createCapturedOutputBuffers();
  const maxStdoutBytes = resolveMaxOutputBytes(options.maxOutputBytes, "stdout");
  const maxStderrBytes = resolveMaxOutputBytes(options.maxOutputBytes, "stderr");
  const maxCombinedOutputBytes =
    typeof options.maxCombinedOutputBytes === "number" &&
    Number.isFinite(options.maxCombinedOutputBytes) &&
    options.maxCombinedOutputBytes > 0
      ? Math.max(1, Math.floor(options.maxCombinedOutputBytes))
      : undefined;
  const stdoutCaptureMode = resolveOutputCapture(options.outputCapture, "stdout");
  const stderrCaptureMode = resolveOutputCapture(options.outputCapture, "stderr");
  if (maxCombinedOutputBytes !== undefined && stdoutCaptureMode !== stderrCaptureMode) {
    throw new Error("maxCombinedOutputBytes requires matching stdout and stderr capture modes");
  }
  const usesCombinedTailCapture =
    maxCombinedOutputBytes !== undefined &&
    stdoutCaptureMode === "tail" &&
    stderrCaptureMode === "tail";
  const maxPreservedPendingLineBytes = Math.min(
    Math.max(maxStdoutBytes, maxStderrBytes),
    MAX_PRESERVED_PENDING_LINE_BYTES,
  );
  const maxPreservedOutputLines = Math.max(0, Math.floor(options.maxPreservedOutputLines ?? 16));
  const windowsEncoding = forceUtf8 ? null : resolveWindowsConsoleEncoding();
  const cancelController = new AbortController();
  let termination: CommandTerminationReason | undefined;
  let childExitState: { code: number | null; signal: NodeJS.Signals | null } | undefined;
  let commandSettled = false;
  let combinedOutputBytes = 0;
  let combinedCapturedBytes = 0;
  const outputBytesByStream = { stdout: 0, stderr: 0 };
  const combinedCapturedBytesByStream = { stdout: 0, stderr: 0 };
  const combinedTailChunks: Array<{ stream: CommandOutputStream; buffer: Buffer }> = [];
  let noOutputTimer: NodeJS.Timeout | undefined;
  let outputObserverError: unknown;
  let outputErrorStream: CommandOutputStream | undefined;
  let terminatingOutputError: Error | undefined;

  const { child, invocation } = spawnCommandWithInvocation(argv, {
    buffer: false,
    cancelSignal: cancelController.signal,
    inheritScopeCancellation: false,
    cwd,
    detached: Boolean(killProcessTree && process.platform !== "win32"),
    encoding: "buffer",
    baseEnv,
    env,
    forceKillAfterDelay: resolvedKillGraceMs,
    killSignal,
    ...(hasInput && !options.beforeInput ? { input } : {}),
    reject: false,
    stdio: [hasInput ? "pipe" : "inherit", "pipe", "pipe"],
    stripFinalNewline: false,
    windowsVerbatimArguments: options.windowsVerbatimArguments,
  });
  const nodeChild = child.nodeChildProcess;
  const ownsExitedProcessTree = Boolean(killProcessTree && process.platform !== "win32");
  const shouldTrackOutputTimeout =
    typeof noOutputTimeoutMs === "number" &&
    Number.isFinite(noOutputTimeoutMs) &&
    noOutputTimeoutMs > 0;
  const resolvedNoOutputTimeoutMs = shouldTrackOutputTimeout
    ? resolveTimerTimeoutMs(noOutputTimeoutMs, 1)
    : undefined;
  const ownsOutputDeadline =
    ownsExitedProcessTree &&
    (resolvedTimeoutMs !== undefined || resolvedNoOutputTimeoutMs !== undefined);
  let releaseOutput: (() => void) | undefined;
  const terminationController = createCommandTerminationController({
    child: nodeChild,
    cancelController,
    baseEnv,
    env,
    processTree: killProcessTree ? { mode: "graceful" } : undefined,
    isChildExited: () => childExitState !== undefined,
    isCommandSettled: () => commandSettled,
    killGraceMs: resolvedKillGraceMs,
    killSignal,
  });
  nodeChild.once("exit", (code, signalValue) => {
    childExitState = { code, signal: signalValue };
    // Successful tree output belongs to its command deadline, not the diagnostic
    // idle cutoff. Failed, terminated, and unowned output still gets a bounded drain.
    if (!ownsOutputDeadline || code !== 0 || termination) {
      releaseOutput = releaseChildProcessOutputAfterExit(nodeChild);
    }
    // An inner timeout can become an ordinary failed exit while its descendants survive.
    // Retain the existing tree owner through its drain without changing that exit result.
    if (killProcessTree && !termination && (code !== 0 || options.requireProcessTreeExtinction)) {
      terminationController.terminate();
    }
  });

  const clearNoOutputTimer = () => {
    if (noOutputTimer) {
      clearTimeout(noOutputTimer);
      noOutputTimer = undefined;
    }
  };
  const cancel = (reason: Exclude<CommandTerminationReason, "exit">) => {
    // Failed roots already own a drain; later deadlines must preserve their exit result.
    // Successful POSIX roots retain deadline ownership of inherited descendants.
    // Output caps remain meaningful for bytes drained after either exit.
    if (
      termination ||
      commandSettled ||
      (childExitState &&
        reason !== "output-limit" &&
        (!ownsExitedProcessTree || childExitState.code !== 0))
    ) {
      return;
    }
    termination = reason;
    if (childExitState) {
      // An escaped pipe holder can survive group termination; bound its final drain.
      releaseOutput ??= releaseChildProcessOutputAfterExit(nodeChild);
    }
    const abortDeferred = terminationController.terminate();
    if (!abortDeferred) {
      cancelController.abort();
    }
  };
  const armNoOutputTimer = () => {
    if (
      resolvedNoOutputTimeoutMs === undefined ||
      commandSettled ||
      termination ||
      (childExitState && !ownsExitedProcessTree)
    ) {
      return;
    }
    clearNoOutputTimer();
    noOutputTimer = setTimeout(() => cancel("no-output-timeout"), resolvedNoOutputTimeoutMs);
  };

  const timeoutTimer =
    resolvedTimeoutMs === undefined
      ? undefined
      : setTimeout(() => cancel("timeout"), resolvedTimeoutMs);
  const onAbort = () => cancel("signal");
  signal?.addEventListener("abort", onAbort, { once: true });
  armNoOutputTimer();

  const captureOutput = (
    capture: CapturedOutputBuffers,
    chunk: Buffer | string,
    maxBytes: number,
    stream: CommandOutputStream,
    captureMode: CommandOutputCaptureMode,
  ) => {
    const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
    outputBytesByStream[stream] += buffer.byteLength;
    const streamLimitExceeded = outputBytesByStream[stream] > maxBytes;
    if (maxCombinedOutputBytes === undefined) {
      appendCapturedOutput(capture, buffer, maxBytes, captureMode);
      if (
        streamLimitExceeded &&
        shouldTerminateOnOutputLimit(options.terminateOnOutputLimit, stream)
      ) {
        cancel("output-limit");
      }
      return;
    }

    const combinedBytesBeforeChunk = combinedOutputBytes;
    combinedOutputBytes += buffer.byteLength;
    const combinedLimitExceeded = combinedOutputBytes > maxCombinedOutputBytes;
    if (usesCombinedTailCapture) {
      combinedTailChunks.push({ stream, buffer });
      combinedCapturedBytes += buffer.byteLength;
      combinedCapturedBytesByStream[stream] += buffer.byteLength;

      const removeCapturedBytes = (index: number, requestedBytes: number) => {
        const entry = expectDefined(combinedTailChunks[index], "combined tail chunk");
        const removedBytes = Math.min(requestedBytes, entry.buffer.byteLength);
        if (removedBytes === entry.buffer.byteLength) {
          combinedTailChunks.splice(index, 1);
        } else {
          entry.buffer = Buffer.from(entry.buffer.subarray(removedBytes));
        }
        combinedCapturedBytes -= removedBytes;
        combinedCapturedBytesByStream[entry.stream] -= removedBytes;
        (entry.stream === "stdout" ? stdoutCapture : stderrCapture).truncatedBytes += removedBytes;
      };

      while (combinedCapturedBytesByStream[stream] > maxBytes) {
        const index = combinedTailChunks.findIndex((entry) => entry.stream === stream);
        if (index < 0) {
          break;
        }
        removeCapturedBytes(index, combinedCapturedBytesByStream[stream] - maxBytes);
      }
      let combinedOverflow = combinedCapturedBytes - maxCombinedOutputBytes;
      while (combinedOverflow > 0) {
        removeCapturedBytes(0, combinedOverflow);
        combinedOverflow = combinedCapturedBytes - maxCombinedOutputBytes;
      }
    } else {
      const remaining = Math.max(0, maxCombinedOutputBytes - combinedBytesBeforeChunk);
      const maxCaptureBytes = Math.min(maxBytes, capture.bytes + remaining);
      appendCapturedOutput(capture, buffer, maxCaptureBytes, captureMode);
    }
    if (
      (combinedLimitExceeded &&
        shouldTerminateOnOutputLimit(options.terminateOnOutputLimit, "combined")) ||
      (streamLimitExceeded && shouldTerminateOnOutputLimit(options.terminateOnOutputLimit, stream))
    ) {
      cancel("output-limit");
    }
  };

  const observeOutputChunk = (chunk: Buffer | string, stream: CommandOutputStream): Buffer => {
    const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
    if (termination || !options.onOutputChunk) {
      return buffer;
    }
    try {
      if (options.onOutputChunk(buffer, stream) === false) {
        cancel("output-limit");
      }
    } catch (error) {
      outputObserverError = error;
      cancel("output-limit");
    }
    return buffer;
  };

  const onOutputError = (error: unknown, stream: CommandOutputStream) => {
    outputErrorStream ??= stream;
    if (
      termination ||
      options.tolerateOutputError?.[stream] === true ||
      !shouldTerminateOnOutputError(options.terminateOnOutputError, stream)
    ) {
      return;
    }
    terminatingOutputError = toErrorObject(error, `Command ${stream} stream failed`);
    Object.assign(terminatingOutputError, { outputErrorStream: stream });
    cancel("signal");
  };
  child.stdout?.once("error", (error) => onOutputError(error, "stdout"));
  child.stderr?.once("error", (error) => onOutputError(error, "stderr"));
  child.stdout?.on("data", (chunk) => {
    const buffer = observeOutputChunk(chunk, "stdout");
    appendPreservedOutputLines({
      capture: stdoutCapture,
      chunk: buffer,
      stream: "stdout",
      preserveOutputLine: options.preserveOutputLine,
      maxPreservedOutputLines,
      maxPendingLineBytes: maxPreservedPendingLineBytes,
    });
    captureOutput(stdoutCapture, buffer, maxStdoutBytes, "stdout", stdoutCaptureMode);
    armNoOutputTimer();
  });
  child.stderr?.on("data", (chunk) => {
    const buffer = observeOutputChunk(chunk, "stderr");
    appendPreservedOutputLines({
      capture: stderrCapture,
      chunk: buffer,
      stream: "stderr",
      preserveOutputLine: options.preserveOutputLine,
      maxPreservedOutputLines,
      maxPendingLineBytes: maxPreservedPendingLineBytes,
    });
    captureOutput(stderrCapture, buffer, maxStderrBytes, "stderr", stderrCaptureMode);
    armNoOutputTimer();
  });

  let inputAdmissionError: Error | undefined;
  if (options.beforeInput) {
    nodeChild.stdin?.once("error", (cause) => {
      inputAdmissionError ??= toErrorObject(cause, "Command input failed");
      cancel("signal");
    });
    try {
      if (nodeChild.pid === undefined || !nodeChild.stdin) {
        throw new Error("Child input admission has no spawned process");
      }
      const admitted: unknown = options.beforeInput(nodeChild.pid, nodeChild.spawnargs);
      if (admitted !== undefined) {
        if (isPromiseLike(admitted)) {
          void Promise.resolve(admitted).catch(() => undefined);
        }
        throw new TypeError("Child input admission must complete synchronously");
      }
      nodeChild.stdin.end(input);
    } catch (cause) {
      inputAdmissionError = toErrorObject(cause, "Child input admission failed");
      nodeChild.stdin?.destroy();
      cancel("signal");
    }
  }

  const result = await child.finally(() => {
    commandSettled = true;
    if (timeoutTimer) {
      clearTimeout(timeoutTimer);
    }
    clearNoOutputTimer();
    signal?.removeEventListener("abort", onAbort);
    releaseOutput?.();
  });
  let cleanup = await terminationController.settle();
  const resolvedSignal = result.signal ?? childExitState?.signal ?? nodeChild.signalCode ?? null;
  if (cleanup !== "forced" && resolvedSignal) {
    cleanup = "uncertain";
  }
  if (inputAdmissionError) {
    throw Object.assign(inputAdmissionError, { cleanup });
  }
  if (terminatingOutputError) {
    throw Object.assign(terminatingOutputError, { cleanup });
  }
  if (outputObserverError !== undefined) {
    throw Object.assign(toErrorObject(outputObserverError, "Command output observer failed"), {
      cleanup,
    });
  }
  // Patched Node can report null/null after a cmd.exe shim exits. Execa turns
  // that into a cause-less failure; preserve the shim fallback only post-spawn.
  const isCauseLessWindowsShimResult =
    !termination &&
    invocation.usesWindowsExitCodeShim &&
    typeof nodeChild.pid === "number" &&
    result.code === undefined &&
    result.cause === undefined &&
    !result.timedOut &&
    !result.isCanceled &&
    !result.isMaxBuffer &&
    !result.isTerminated;
  if (isCauseLessWindowsShimResult) {
    // A patched Windows runtime can populate exitCode shortly after close.
    // Settle that state before the shim fallback can infer a clean exit.
    for (
      let elapsedMs = 0;
      elapsedMs < WINDOWS_CLOSE_STATE_SETTLE_TIMEOUT_MS;
      elapsedMs += WINDOWS_CLOSE_STATE_POLL_MS
    ) {
      if (
        childExitState?.code != null ||
        childExitState?.signal != null ||
        nodeChild.exitCode != null ||
        nodeChild.signalCode != null
      ) {
        break;
      }
      await new Promise<void>((resolve) => {
        setTimeout(resolve, WINDOWS_CLOSE_STATE_POLL_MS);
      });
    }
  }
  if (
    result.failed &&
    !termination &&
    !isPlainCommandExitFailure(result) &&
    !isPlainCommandSignalFailure(result) &&
    !isCauseLessWindowsShimResult &&
    !(
      result.exitCode === 0 &&
      outputErrorStream !== undefined &&
      options.tolerateOutputError?.[outputErrorStream] === true
    )
  ) {
    const error = createSanitizedCommandError(result);
    Object.assign(error, {
      cleanup:
        typeof nodeChild.pid === "number"
          ? cleanup === "normal"
            ? "uncertain"
            : cleanup
          : "normal",
    });
    if (outputErrorStream) {
      Object.assign(error, { outputErrorStream });
    }
    throw error;
  }

  const resolvedCode = resolveProcessExitCode({
    explicitCode: result.exitCode ?? childExitState?.code,
    childExitCode: nodeChild.exitCode,
    resolvedSignal,
    usesWindowsExitCodeShim: invocation.usesWindowsExitCodeShim,
    timedOut: termination === "timeout",
    noOutputTimedOut: termination === "no-output-timeout",
    killIssuedByTimeout: termination === "timeout" || termination === "no-output-timeout",
    killIssuedByAbort: termination === "signal" || termination === "output-limit",
  });
  termination ??= resolvedSignal != null || result.isTerminated ? "signal" : "exit";
  const normalizedCode =
    termination === "timeout" || termination === "no-output-timeout"
      ? resolvedCode == null || resolvedCode === 0
        ? TIMEOUT_EXIT_CODE
        : resolvedCode
      : resolvedCode;

  flushPreservedOutputLine({
    capture: stdoutCapture,
    stream: "stdout",
    preserveOutputLine: options.preserveOutputLine,
    maxPreservedOutputLines,
    maxPendingLineBytes: maxPreservedPendingLineBytes,
  });
  flushPreservedOutputLine({
    capture: stderrCapture,
    stream: "stderr",
    preserveOutputLine: options.preserveOutputLine,
    maxPreservedOutputLines,
    maxPendingLineBytes: maxPreservedPendingLineBytes,
  });

  if (usesCombinedTailCapture) {
    for (const entry of combinedTailChunks) {
      const capture = entry.stream === "stdout" ? stdoutCapture : stderrCapture;
      capture.chunks.push(entry.buffer);
      capture.bytes += entry.buffer.byteLength;
    }
  }

  const stdout = finalizeCapturedOutput(stdoutCapture, stdoutCaptureMode, forceUtf8);
  const stderr = finalizeCapturedOutput(stderrCapture, stderrCaptureMode, forceUtf8);
  const settled = {
    pid: nodeChild.pid,
    stdoutTruncatedBytes: stdoutCapture.truncatedBytes || undefined,
    stderrTruncatedBytes: stderrCapture.truncatedBytes || undefined,
    preservedStdoutLines:
      stdoutCapture.preservedLines.length > 0 ? stdoutCapture.preservedLines : undefined,
    preservedStderrLines:
      stderrCapture.preservedLines.length > 0 ? stderrCapture.preservedLines : undefined,
    code: normalizedCode,
    signal: resolvedSignal,
    killed: nodeChild.killed,
    cleanup,
    termination: termination === "output-limit" ? ("signal" as const) : termination,
    noOutputTimedOut: termination === "no-output-timeout",
    outputLimitExceeded: termination === "output-limit" || undefined,
    ...(outputErrorStream ? { outputErrorStream } : {}),
  };
  return raw
    ? { ...settled, stdout, stderr, windowsEncoding }
    : {
        ...settled,
        stdout: forceUtf8
          ? stdout.toString("utf8")
          : decodeWindowsOutputBuffer({ buffer: stdout, windowsEncoding }),
        stderr: forceUtf8
          ? stderr.toString("utf8")
          : decodeWindowsOutputBuffer({ buffer: stderr, windowsEncoding }),
      };
}