diff --git a/scripts/build-desktop-artifact.test.ts b/scripts/build-desktop-artifact.test.ts index 1c4cbd97b..ed59d1d73 100644 --- a/scripts/build-desktop-artifact.test.ts +++ b/scripts/build-desktop-artifact.test.ts @@ -121,6 +121,28 @@ it.layer(NodeServices.layer)("build-desktop-artifact", (it) => { }), ); + it.effect("retries Electron packaging when Apple's timestamp server drops a signature", () => + Effect.gen(function* () { + let attempts = 0; + const result = yield* retryTransientElectronPackaging( + Effect.suspend(() => { + attempts += 1; + return attempts < 2 + ? Effect.fail( + new TestFailure({ + message: + "Command failed: codesign --sign ... locale.pak\nlocale.pak: A timestamp was expected but was not found.", + }), + ) + : Effect.succeed("built"); + }), + ); + + assert.equal(result, "built"); + assert.equal(attempts, 2); + }), + ); + it.effect("does not retry non-network packaging failures", () => Effect.gen(function* () { let attempts = 0; diff --git a/scripts/build-desktop-artifact.ts b/scripts/build-desktop-artifact.ts index 1c12eb445..df8f8d530 100644 --- a/scripts/build-desktop-artifact.ts +++ b/scripts/build-desktop-artifact.ts @@ -564,20 +564,44 @@ const runCommand = Effect.fn("runCommand")(function* (command: ChildProcess.Comm } }); +/** + * What codesign prints when Apple's timestamp server (timestamp.apple.com) + * doesn't answer `--timestamp`. The server drops requests now and then; the + * same signature succeeds on the next try. + */ +const APPLE_TIMESTAMP_FAILURE_PATTERNS = [ + "a timestamp was expected but was not found", + "the timestamp service is not available", +] as const; + +function isAppleTimestampFailure(error: unknown): boolean { + const message = (error instanceof Error ? error.message : String(error)).toLowerCase(); + return APPLE_TIMESTAMP_FAILURE_PATTERNS.some((pattern) => message.includes(pattern)); +} + +function isTransientPackagingFailure(error: unknown): boolean { + return isTransientNetworkFailure(error) || isAppleTimestampFailure(error); +} + +/** + * Runs electron-builder again, up to twice, when it fails on a network blip or + * on Apple's timestamp server. Each run re-extracts Electron and signs from + * scratch, so a retry never sees a half-signed app. + */ export function retryTransientElectronPackaging( packaging: Effect.Effect, ): Effect.Effect { return packaging.pipe( Effect.tapError((error) => - isTransientNetworkFailure(error) + isTransientPackagingFailure(error) ? Effect.logWarning( - "[desktop-artifact] Electron packaging hit a transient network failure; bounded retry policy active.", + "[desktop-artifact] Electron packaging hit a transient network or code-signing timestamp failure; bounded retry policy active.", ) : Effect.void, ), Effect.retry({ times: 2, - while: isTransientNetworkFailure, + while: isTransientPackagingFailure, }), ); }