Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Retry macOS staple network failures with bounded backoff (#3459)

harness-for-autonomous[bot]0413586e0a15
diff --git a/desktop/scripts/test_staple.py b/desktop/scripts/test_staple.py
new file mode 100644
index 000000000..1fc300e57
--- /dev/null
+++ b/desktop/scripts/test_staple.py
@@ -0,0 +1,71 @@
+"""Dry-run the uploader's real staple function; never invoke Apple or signing tools."""
+from pathlib import Path
+import subprocess
+import tempfile
+import unittest
+
+UPLOADER = Path(__file__).with_name('upload-desktop.sh')
+
+
+class StapleTests(unittest.TestCase):
+ def run_staple(self, failure, failures):
+ source = UPLOADER.read_text()
+ function = source[source.index('staple() {'):source.index('\nif [ "$DO_NOTARIZE"', source.index('staple() {'))]
+ fixture = r'''
+xcrun() {
+ [ "$1 $2" = 'stapler staple' ] || return 99
+ [ "$3" = 'fixture with spaces.app' ] || return 98
+ attempts=$(($(wc -l < "$calls") + 1))
+ echo call >> "$calls"
+ if [ "$attempts" -le "$failures" ]; then
+ echo "$failure"
+ return 68
+ fi
+ echo 'The staple and validate action worked!'
+}
+sleep() { echo "backoff $1"; }
+'''
+ # A file holds counts because production captures tool output in a subshell.
+ with tempfile.TemporaryDirectory() as folder:
+ calls = Path(folder) / 'calls'
+ calls.touch()
+ result = subprocess.run(
+ ['bash', '-c', 'set -euo pipefail\ncalls=$1; failure=$2; failures=$3\n' + fixture + function +
+ '\nstaple "fixture with spaces.app"', 'fixture', str(calls), failure, str(failures)],
+ text=True, capture_output=True, timeout=5)
+ return result, len(calls.read_text().splitlines())
+
+ def test_network_loss_then_success(self):
+ result, calls = self.run_staple('NSURLErrorDomain Code=-1005 "The network connection was lost."', 1)
+ self.assertEqual(result.returncode, 0, result.stderr)
+ self.assertEqual(calls, 2)
+ self.assertIn('backoff 15', result.stdout)
+
+ def test_longer_network_outage_recovers_on_sixth_attempt(self):
+ result, calls = self.run_staple('NSURLErrorDomain Code=-1005 "The network connection was lost."', 5)
+ self.assertEqual(result.returncode, 0, result.stderr)
+ self.assertEqual(calls, 6)
+ self.assertIn('backoff 240', result.stdout)
+
+ def test_persistent_network_failure_is_bounded(self):
+ result, calls = self.run_staple('NSURLErrorDomain Code=-1001 "The request timed out."', 99)
+ self.assertNotEqual(result.returncode, 0)
+ self.assertEqual(calls, 6)
+ self.assertIn('after 6 attempts', result.stderr)
+
+ def test_ticket_not_yet_published_then_success(self):
+ result, calls = self.run_staple('The staple and validate action failed! Error 65.', 1)
+ self.assertEqual(result.returncode, 0, result.stderr)
+ self.assertEqual(calls, 2)
+ self.assertIn('backoff 15', result.stdout)
+
+ def test_real_stapling_error_still_fails_after_five_attempts(self):
+ result, calls = self.run_staple('The staple and validate action failed! Error 65.', 99)
+ self.assertNotEqual(result.returncode, 0)
+ self.assertEqual(calls, 5)
+ self.assertIn('backoff 60', result.stdout)
+ self.assertIn('after 5 attempts', result.stderr)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/desktop/scripts/upload-desktop.sh b/desktop/scripts/upload-desktop.sh
index 59f363c05..a6de5822c 100755
--- a/desktop/scripts/upload-desktop.sh
+++ b/desktop/scripts/upload-desktop.sh
@@ -241,21 +241,37 @@ packaging_time() {
echo ">> packaging timing: $name start=$started end=$SECONDS status=$status" >&2
return "$status"
}
-# Stapling fetches the accepted ticket from Apple's CloudKit, and that lookup drops: v1.2.67 failed
-# twice, both variants, on `NSURLErrorDomain -1005 "The network connection was lost"` (Error 68)
-# with notarization already Accepted (2026-10-12). Retried a few times, spaced out; the Gatekeeper
-# assessment after it still fails closed on a bundle that did not take its ticket.
+# `stapler staple` fetches the ticket and validates it. Apple's connection can drop even after
+# notarization is Accepted: candidate 1.3.87 lost all five calls in the old 150s retry window.
+# Extend transport failures to a bounded 465s backoff window. Keep the old five attempts for
+# other failures: an Accepted ticket may not have reached CloudKit yet. Bad tickets still fail closed.
staple() {
- local attempt
- for attempt in 1 2 3 4 5; do
- xcrun stapler staple "$1" && return 0
- [ "$attempt" -lt 5 ] || break
- echo ">> stapling $1 failed (attempt $attempt of 5) — retrying in $((attempt * 15))s" >&2
- sleep $((attempt * 15))
+ local attempt output status delay limit
+ for attempt in 1 2 3 4 5 6; do
+ if output="$(xcrun stapler staple "$1" 2>&1)"; then
+ printf '%s\n' "$output"
+ return 0
+ else
+ status=$?
+ fi
+ printf '%s\n' "$output" >&2
+ # Error 68 alone is ambiguous. Require an actual transport diagnostic, including Apple's
+ # NSURLErrorDomain codes for timeout, DNS, connect, connection lost and offline failures.
+ if grep -Eiq 'NSURLErrorDomain Code=-(1001|1003|1004|1005|1006|1009)([^0-9]|$)|network connection was lost|connection (reset|refused)|network is (unreachable|down)|request timed out' <<< "$output"; then
+ limit=6
+ delay=$((15 << (attempt - 1)))
+ else
+ limit=5
+ delay=$((attempt * 15))
+ fi
+ [ "$attempt" -lt "$limit" ] || break
+ echo ">> stapling failed (attempt $attempt of $limit) — retrying in ${delay}s" >&2
+ sleep "$delay" || return
done
- echo "error: could not staple the notarization ticket to $1" >&2
- return 1
+ echo "error: could not staple the notarization ticket after $attempt attempts" >&2
+ return "$status"
}
+
if [ "$DO_NOTARIZE" -eq 1 ]; then
# This upload is discarded after stapling. Spend less CPU compressing it;
# the final downloadable ZIP below still uses ditto's normal compression.