fix(providers): retain worker cleanup owners through shutdown

Validation: integrated changes passed formatting, typecheck, lint, resolved module-boundary verification and git diff --check. Tests and real-provider workflows were not run.
This commit is contained in:
morluto
2026-10-11 00:03:21 +08:00
parent a01148553e
commit d1235fadc9
24 changed files with 688 additions and 120 deletions
+39 -9
View File
@@ -113,6 +113,7 @@ export const runFirmwareCommand = async (context: {
launcher?: FirmwareLauncher;
outputBudget?: { root: string; bytes: number; entries: number };
acceptReportedExtractionFailure?: boolean;
retainCleanup(supervisor: ProviderProcessSupervisor): void;
}) => {
const { engine, operation, signal } = context;
signal.throwIfAborted();
@@ -141,16 +142,43 @@ export const runFirmwareCommand = async (context: {
NO_COLOR: "1",
},
});
const supervisor = new ProviderProcessSupervisor({
...spawned,
ownsProcessLifetime: true,
cleanup:
spawned.cleanup ?? (() => cleanupOwnedProcessGroup(spawned.ownership)),
});
let diagnosticTruncated = false;
const supervisor = new ProviderProcessSupervisor(
{
...spawned,
ownsProcessLifetime: true,
cleanup:
spawned.cleanup ?? (() => cleanupOwnedProcessGroup(spawned.ownership)),
},
{
maxDiagnosticBytes: FIRMWARE_LIMITS.diagnosticBytes,
onDiagnostic: (event) => {
if (event.type === "output" && event.truncated === true)
diagnosticTruncated = true;
},
},
);
const checkDiagnostics = () => {
if (diagnosticTruncated) {
const snapshot = supervisor.snapshot();
throw new AnalysisOutputError(
operation,
`Tool diagnostics exceeded the ${FIRMWARE_LIMITS.diagnosticBytes}-byte capture budget`,
{
capturedOutput: {
stdout: snapshot.stdout.text,
stderr: snapshot.stderr.text,
truncated: true,
},
},
);
}
};
let failure: unknown;
try {
const deadline = Date.now() + FIRMWARE_LIMITS.timeoutMs;
while (!(await supervisor.waitForExit(100))) {
while (!(await supervisor.waitForOutputClose(100))) {
checkDiagnostics();
if (signal.aborted) throw new AnalysisCancelledError(operation);
if (Date.now() >= deadline)
throw new AnalysisTimeoutError(operation, FIRMWARE_LIMITS.timeoutMs);
@@ -163,14 +191,15 @@ export const runFirmwareCommand = async (context: {
signal,
);
}
checkDiagnostics();
if (signal.aborted) throw new AnalysisCancelledError(operation);
} catch (cause: unknown) {
failure = cause;
}
const stopped = await supervisor.stop();
const snapshot = supervisor.snapshot();
supervisor.dispose();
if (stopped.status === "incomplete")
if (stopped.status === "incomplete") {
context.retainCleanup(supervisor);
throw new ProviderCleanupError(engine, [runId, context.cwd], {
reason: stopped.reason,
leader_pid: spawned.ownership.leaderPid,
@@ -183,6 +212,7 @@ export const runFirmwareCommand = async (context: {
stderr_bytes: snapshot.stderr.bytes,
previous_error: failure instanceof Error ? failure.message : null,
});
}
if (failure !== undefined) throw failure;
if (
snapshot.exitCode !== 0 &&