diff --git a/src/native/NativeCommandAnalysisError.test.ts b/src/native/NativeCommandAnalysisError.test.ts new file mode 100644 index 0000000000..3c1d06b7a9 --- /dev/null +++ b/src/native/NativeCommandAnalysisError.test.ts @@ -0,0 +1,159 @@ +import { describe, expect, it } from "vitest"; + +import { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js"; +import { AnalysisTimeoutError } from "../domain/analysisErrorCore.js"; +import { projectAnalysisError } from "../domain/analysisErrorProjection.js"; +import { ProviderAdapterError } from "../domain/providerAdapterError.js"; +import { + DarwinProcessOwnershipInspectionError, + type DarwinCompilerOutcome, +} from "../process/DarwinProcessRunTokenReader.js"; +import { NativeCommandFailure } from "./CommandRunner.js"; +import { translateNativeCommandFailure } from "./NativeCommandAnalysisError.js"; + +const preparationFailure = ( + compilerOutcome: DarwinCompilerOutcome, + cancellation?: { readonly reason: unknown }, +) => + new NativeCommandFailure( + "lipo", + cancellation === undefined ? "io" : "cancelled", + null, + { + cause: new DarwinProcessOwnershipInspectionError( + "ownership-helper compilation failed", + { + compilerOutcome, + ...(cancellation === undefined ? {} : { cancellation }), + }, + ), + }, + ); + +const compilerTimeout = (timeoutMs?: number): DarwinCompilerOutcome => ({ + outcome: "timed_out", + code: "ETIMEDOUT", + signal: "SIGINT", + stderr: "", + stderrTruncated: false, + outputTruncated: false, + killed: true, + ...(timeoutMs === undefined ? {} : { timeoutMs }), +}); + +describe("translateNativeCommandFailure", () => { + it("projects an ownership-helper compiler deadline as a retryable provider timeout", () => { + const error = translateNativeCommandFailure( + "inspect_macho", + preparationFailure(compilerTimeout(1)), + new ArtifactResourceScope(), + 60_000, + { + timeoutRecovery: { + userMessage: "native command deadline", + remediationAction: "native_command_timeout_ms", + }, + }, + ); + + expect(error).toBeInstanceOf(AnalysisTimeoutError); + if (!(error instanceof AnalysisTimeoutError)) return; + expect(error.timeoutMs).toBe(1); + expect(error.userMessage).toContain("ownership helper compiler"); + + const projected = projectAnalysisError(error); + expect(projected).toMatchObject({ + code: "provider_timeout", + category: "timeout", + retryable: true, + }); + expect(projected.message).toContain("1 ms"); + expect(projected.remediation.action).toContain( + "REA_SWIFTC_COMPILE_TIMEOUT_MS", + ); + expect(projected.remediation.action).not.toContain( + "native_command_timeout_ms", + ); + }); + + it("does not infer a compiler timeout duration when it was not reported", () => { + const error = translateNativeCommandFailure( + "inspect_macho", + preparationFailure(compilerTimeout()), + new ArtifactResourceScope(), + ); + + expect(error).toBeInstanceOf(ProviderAdapterError); + expect(error).not.toBeInstanceOf(AnalysisTimeoutError); + expect(projectAnalysisError(error)).toMatchObject({ + code: "execution_failure", + category: "execution_failure", + retryable: false, + }); + }); + + it("keeps the command deadline primary when the compiler also timed out", () => { + const preparation = new DarwinProcessOwnershipInspectionError( + "ownership-helper compilation timed out", + { compilerOutcome: compilerTimeout(1) }, + ); + const error = translateNativeCommandFailure( + "inspect_macho", + new NativeCommandFailure("lipo", "timeout", null, { + cause: preparation, + }), + new ArtifactResourceScope(), + 60_000, + { + timeoutRecovery: { + userMessage: "native command deadline", + remediationAction: "native_command_timeout_ms", + }, + }, + ); + + expect(error).toBeInstanceOf(AnalysisTimeoutError); + if (!(error instanceof AnalysisTimeoutError)) return; + expect(error.timeoutMs).toBe(60_000); + const projected = projectAnalysisError(error); + expect(projected.remediation.action).toContain("native_command_timeout_ms"); + expect(projected.remediation.action).not.toContain( + "REA_SWIFTC_COMPILE_TIMEOUT_MS", + ); + }); + + it("keeps compiler exit failures distinct from deadlines", () => { + const error = translateNativeCommandFailure( + "inspect_macho", + preparationFailure({ + outcome: "exited", + code: 1, + signal: null, + stderr: "compiler failed", + stderrTruncated: false, + outputTruncated: false, + killed: false, + }), + new ArtifactResourceScope(), + ); + + expect(error).toBeInstanceOf(ProviderAdapterError); + expect(projectAnalysisError(error)).toMatchObject({ + code: "execution_failure", + category: "execution_failure", + retryable: false, + }); + }); + + it("keeps caller cancellation ahead of a compiler timeout", () => { + const cancellation = new Error("caller stopped"); + const error = translateNativeCommandFailure( + "inspect_macho", + preparationFailure(compilerTimeout(1), { reason: cancellation }), + new ArtifactResourceScope(), + ); + + expect(error).not.toBeInstanceOf(AnalysisTimeoutError); + expect(error).toMatchObject({ _tag: "AnalysisCancelledError" }); + }); +}); diff --git a/src/native/NativeCommandAnalysisError.ts b/src/native/NativeCommandAnalysisError.ts index cb8ee3a58d..e6cd374b39 100644 --- a/src/native/NativeCommandAnalysisError.ts +++ b/src/native/NativeCommandAnalysisError.ts @@ -124,7 +124,19 @@ export const translateNativeCommandFailure = ( }) : cancellationPrimary && cancelled !== undefined ? cancelled - : new ProviderAdapterError(IDENTITY.id, operation, options); + : // The ownership helper's compiler deadline is a retryable timeout + // with its own setting, not a provider failure. + darwin?.compileTimeout !== undefined + ? new AnalysisTimeoutError( + operation, + darwin.compileTimeout.timeoutMs, + { + ...options, + userMessage: darwin.compileTimeout.userMessage, + remediationAction: darwin.compileTimeout.remediationAction, + }, + ) + : new ProviderAdapterError(IDENTITY.id, operation, options); return retainProcessInspectionCleanup( preparation, bindCleanup(error), diff --git a/src/process/DarwinProcessRunTokenReader.ts b/src/process/DarwinProcessRunTokenReader.ts index 6eae004597..97373bd3ad 100644 --- a/src/process/DarwinProcessRunTokenReader.ts +++ b/src/process/DarwinProcessRunTokenReader.ts @@ -183,6 +183,29 @@ export class DarwinProcessOwnershipInspectionError extends Error { readonly cleanupFailure: AnalysisError | undefined; readonly cancellation: { readonly reason: unknown } | undefined; + /** Whether helper preparation failed because its compiler hit its deadline. */ + get compilerTimedOut(): boolean { + return this.compilerOutcome?.outcome === "timed_out"; + } + + /** The compiler deadline and its recovery, when it is known. */ + get compileTimeout(): + | { + readonly timeoutMs: number; + readonly userMessage: string; + readonly remediationAction: string; + } + | undefined { + const outcome = this.compilerOutcome; + if (outcome?.outcome !== "timed_out" || outcome.timeoutMs === undefined) + return undefined; + return { + timeoutMs: outcome.timeoutMs, + userMessage: `The macOS process ownership helper compiler reached its REA_SWIFTC_COMPILE_TIMEOUT_MS deadline of ${String(outcome.timeoutMs)} ms.`, + remediationAction: swiftcCompileTimeoutRecovery(outcome.timeoutMs), + }; + } + /** Preserve the physical preparation failure while following the exact cleanup owner. */ withCleanupFailure( cleanup: AnalysisError, diff --git a/src/process/capture/ProcessCaptureError.ts b/src/process/capture/ProcessCaptureError.ts index a907788d89..23769413f7 100644 --- a/src/process/capture/ProcessCaptureError.ts +++ b/src/process/capture/ProcessCaptureError.ts @@ -200,9 +200,7 @@ export class ProcessCaptureInspectionFailure extends ProcessCaptureError { ...(darwin?.capturedOutput === undefined ? {} : { capturedOutput: darwin.capturedOutput }), - ...(darwin?.compilerOutcome?.outcome === "timed_out" - ? { retryable: true } - : {}), + ...(darwin?.compilerTimedOut === true ? { retryable: true } : {}), ...(inspectionFailure instanceof ProcessOwnershipIdentityBatchFailure ? { diagnostics: { diff --git a/tests/boundary/providers/native/nativeProvider.test.ts b/tests/boundary/providers/native/nativeProvider.test.ts index 7ec806abf1..9bf91593bf 100644 --- a/tests/boundary/providers/native/nativeProvider.test.ts +++ b/tests/boundary/providers/native/nativeProvider.test.ts @@ -2058,3 +2058,55 @@ it("reports the selected command deadline when preparation times out", async () await client.close(); } }); + +it("projects an ownership-helper compile timeout independently from the command deadline", async () => { + const inspection = new DarwinProcessOwnershipInspectionError( + "ownership-helper compilation timed out", + { + compilerOutcome: { + outcome: "timed_out", + code: "ETIMEDOUT", + signal: "SIGINT", + stderr: "", + stderrTruncated: false, + outputTruncated: false, + killed: true, + timeoutMs: 1, + }, + }, + ); + const failure = new NativeCommandFailure("swift-demangle", "io", null, { + cause: inspection, + }); + const runner: NativeCommandRunner = { run: async () => err(failure) }; + const client = new NativeMacOSProvider({}, runner, "darwin").createClient( + machoTarget("/fixture"), + ); + try { + const result = await client.execute("demangle_swift", { + symbols: ["fixture"], + native_command_timeout_ms: 60_000, + }); + if (result.ok) throw new Error("Expected ownership-helper timeout"); + expect(result.error).toMatchObject({ _tag: "AnalysisTimeoutError" }); + expect(result.error.diagnostics).toMatchObject({ + compiler_outcome: { outcome: "timed_out", timeout_ms: 1 }, + }); + + const projected = projectAnalysisError(result.error); + expect(projected).toMatchObject({ + code: "provider_timeout", + category: "timeout", + retryable: true, + }); + expect(projected.message).toContain("of 1 ms"); + expect(projected.remediation.action).toContain( + "REA_SWIFTC_COMPILE_TIMEOUT_MS", + ); + expect(projected.remediation.action).not.toContain( + "native_command_timeout_ms", + ); + } finally { + await client.close(); + } +});