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.

fix(web): Download app opens the build for the visitor's computer (#1233)

Jacob Pham7f12f66a0f40
diff --git a/desktop/lib/core/desktop_build.dart b/desktop/lib/core/desktop_build.dart
new file mode 100644
index 000000000..05ed2aaf6
--- /dev/null
+++ b/desktop/lib/core/desktop_build.dart
@@ -0,0 +1,139 @@
+/// Which desktop build suits the browser asking — the Dart side of the website's
+/// `website/src/lib/desktopPlatform.ts`, kept to the same rules so the app's Download app and the
+/// website's `/desktop` offer the same build.
+///
+/// macOS ships one universal app in two builds: every Mac runs the Intel one, and a Mac whose browser
+/// says its CPU is ARM gets the Apple silicon one. Only Chromium can say; Safari and Firefox report
+/// "Intel" on every Mac. On Linux the CPU decides whether the app runs at all, so an unknown CPU gets
+/// no build: the `/desktop` page offers both instead of guessing.
+library;
+
+enum DesktopBuild {
+ macos('/desktop/download-macos'),
+ macosArm64('/desktop/download/macos-arm64'),
+ linuxX64('/desktop/download/linux-x64'),
+ linuxArm64('/desktop/download/linux-arm64');
+
+ const DesktopBuild(this.path);
+
+ /// The website route that redirects to this build's latest download.
+ final String path;
+}
+
+/// The website the download routes live on.
+final kHarnessSite = Uri.parse('https://harness.autonomous.ai');
+
+/// Where Download app goes: the build's own download, or `/desktop` — which lists every build and
+/// says why none runs here — when nothing suits this browser or it could not be told.
+Uri desktopDownloadUri(DesktopBuild? build) =>
+ kHarnessSite.replace(path: build?.path ?? '/desktop');
+
+/// User-Agent Client Hints' high-entropy CPU answer.
+typedef CpuHints = ({String architecture, String bitness});
+
+/// User-Agent Client Hints (Chromium only, secure contexts only).
+class ClientHints {
+ const ClientHints({
+ required this.platform,
+ required this.mobile,
+ required this.cpu,
+ });
+
+ final String platform;
+ final bool mobile;
+
+ /// `getHighEntropyValues(['architecture', 'bitness'])`; may throw when refused.
+ final Future<CpuHints> Function() cpu;
+}
+
+/// The few `navigator` fields detection reads.
+class NavigatorSnapshot {
+ const NavigatorSnapshot({
+ this.userAgent = '',
+ this.platform = '',
+ this.maxTouchPoints = 0,
+ this.hints,
+ });
+
+ final String userAgent;
+ final String platform;
+ final int maxTouchPoints;
+ final ClientHints? hints;
+}
+
+final _arm64 = RegExp(r'\b(aarch64|arm64)\b');
+final _x64 = RegExp(r'\b(x86_64|x86-64|amd64)\b');
+
+/// `armv8l` is deliberately absent: a 32-bit userland the ARM64 AppImage cannot serve.
+DesktopBuild? _linuxBuild(String text) {
+ if (_arm64.hasMatch(text)) return DesktopBuild.linuxArm64;
+ if (_x64.hasMatch(text)) return DesktopBuild.linuxX64;
+ return null;
+}
+
+/// From the user agent and `navigator.platform` alone: the path every browser has.
+DesktopBuild? buildFromUserAgent(NavigatorSnapshot snapshot) {
+ final agent = snapshot.userAgent.toLowerCase();
+ final os = snapshot.platform.toLowerCase();
+ // Android's user agent also says "Linux", and iOS browsers' say "like Mac OS X": phones go first.
+ if (RegExp('android|iphone|ipad|ipod').hasMatch(agent) ||
+ RegExp('^(iphone|ipad|ipod)').hasMatch(os)) {
+ return null;
+ }
+ // iPadOS Safari reports itself as a Mac; only the touchscreen tells it apart.
+ if (agent.contains('macintosh') ||
+ agent.contains('mac os x') ||
+ os.startsWith('mac')) {
+ return snapshot.maxTouchPoints > 1 ? null : DesktopBuild.macos;
+ }
+ if (agent.contains('windows') || os.startsWith('win')) return null;
+ if (agent.contains('cros')) return null;
+ if (!agent.contains('linux') && !os.startsWith('linux')) return null;
+ // Chromium's user agent says x86_64 on every Linux machine; only `navigator.platform` is real there.
+ final chromium = RegExp(r'\b(chrome|chromium)/').hasMatch(agent);
+ return _linuxBuild(chromium ? os : '$os $agent');
+}
+
+enum _Cpu { x64, arm64, other }
+
+/// Null when the browser would not say; [_Cpu.other] for a CPU nothing is published for.
+Future<_Cpu?> _cpuOf(ClientHints hints) async {
+ try {
+ final (:architecture, :bitness) = await hints.cpu();
+ if (bitness == '64' && architecture == 'x86') return _Cpu.x64;
+ if (bitness == '64' && architecture == 'arm') return _Cpu.arm64;
+ if (architecture.isNotEmpty && bitness.isNotEmpty) return _Cpu.other;
+ } catch (_) {
+ // Refused by a permissions policy or privacy setting: "would not say".
+ }
+ return null;
+}
+
+/// The build for this browser, from Client Hints where offered and the user agent otherwise. Never
+/// throws; null means no build suits it or it could not be told.
+Future<DesktopBuild?> detectDesktopBuild(NavigatorSnapshot snapshot) async {
+ final hints = snapshot.hints;
+ if (hints == null || hints.platform.isEmpty) {
+ return buildFromUserAgent(snapshot);
+ }
+ if (hints.mobile) return null;
+ switch (hints.platform) {
+ case 'macOS':
+ // Anything short of a definite ARM answer keeps the build every Mac runs.
+ return await _cpuOf(hints) == _Cpu.arm64
+ ? DesktopBuild.macosArm64
+ : DesktopBuild.macos;
+ case 'Windows' || 'Android' || 'iOS':
+ return null;
+ case 'Linux':
+ return switch (await _cpuOf(hints)) {
+ _Cpu.x64 => DesktopBuild.linuxX64,
+ _Cpu.arm64 => DesktopBuild.linuxArm64,
+ _Cpu.other => null,
+ null => buildFromUserAgent(snapshot),
+ };
+ default:
+ // "Chrome OS", "Unknown" and the like: let the user agent have its say.
+ return buildFromUserAgent(snapshot);
+ }
+}
diff --git a/desktop/lib/core/desktop_download.dart b/desktop/lib/core/desktop_download.dart
new file mode 100644
index 000000000..a46c5ef64
--- /dev/null
+++ b/desktop/lib/core/desktop_download.dart
@@ -0,0 +1,33 @@
+import 'dart:async';
+
+import 'desktop_build.dart';
+import 'navigator_snapshot_native.dart'
+ if (dart.library.js_interop) 'navigator_snapshot_web.dart';
+
+/// The download link for this browser, detected once per page load.
+///
+/// Client Hints answer asynchronously, but the click has to open its tab at once: a tab opened after
+/// an await is outside the user's gesture, and browsers block it as a popup. So detection starts the
+/// first time the link is read (a Download app button's first build) and the click takes the best
+/// answer so far — the user agent's, until the hints arrive moments later.
+abstract final class DesktopDownload {
+ static Future<void>? _detecting;
+ static DesktopBuild? _detected;
+ static var _done = false;
+
+ /// Starts detection if nothing has; completes when it has answered.
+ static Future<void> warm() =>
+ _detecting ??= detectDesktopBuild(navigatorSnapshot()).then(_settle);
+
+ static void _settle(DesktopBuild? build) {
+ _detected = build;
+ _done = true;
+ }
+
+ static Uri get uri {
+ unawaited(warm());
+ return desktopDownloadUri(
+ _done ? _detected : buildFromUserAgent(navigatorSnapshot()),
+ );
+ }
+}
diff --git a/desktop/lib/core/navigator_snapshot_native.dart b/desktop/lib/core/navigator_snapshot_native.dart
new file mode 100644
index 000000000..47c191482
--- /dev/null
+++ b/desktop/lib/core/navigator_snapshot_native.dart
@@ -0,0 +1,4 @@
+import 'desktop_build.dart';
+
+/// Outside a browser there is no navigator to read: detection answers "could not tell".
+NavigatorSnapshot navigatorSnapshot() => const NavigatorSnapshot();
diff --git a/desktop/lib/core/navigator_snapshot_web.dart b/desktop/lib/core/navigator_snapshot_web.dart
new file mode 100644
index 000000000..1626c6452
--- /dev/null
+++ b/desktop/lib/core/navigator_snapshot_web.dart
@@ -0,0 +1,50 @@
+import 'dart:js_interop';
+
+import 'package:web/web.dart' as web;
+
+import 'desktop_build.dart';
+
+/// `navigator.userAgentData`: User-Agent Client Hints, which package:web does not declare yet.
+extension type _UserAgentData._(JSObject _) implements JSObject {
+ external String? get platform;
+ external bool? get mobile;
+ external JSPromise<_HighEntropyValues> getHighEntropyValues(
+ JSArray<JSString> hints,
+ );
+}
+
+extension type _HighEntropyValues._(JSObject _) implements JSObject {
+ external String? get architecture;
+ external String? get bitness;
+}
+
+extension on web.Navigator {
+ external _UserAgentData? get userAgentData;
+}
+
+NavigatorSnapshot navigatorSnapshot() {
+ final navigator = web.window.navigator;
+ final data = navigator.userAgentData;
+ return NavigatorSnapshot(
+ userAgent: navigator.userAgent,
+ platform: navigator.platform,
+ maxTouchPoints: navigator.maxTouchPoints,
+ hints: data == null
+ ? null
+ : ClientHints(
+ platform: data.platform ?? '',
+ mobile: data.mobile ?? false,
+ cpu: () async {
+ final values = await data
+ .getHighEntropyValues(
+ ['architecture'.toJS, 'bitness'.toJS].toJS,
+ )
+ .toDart;
+ return (
+ architecture: values.architecture ?? '',
+ bitness: values.bitness ?? '',
+ );
+ },
+ ),
+ );
+}
diff --git a/desktop/lib/widgets/web_download_button.dart b/desktop/lib/widgets/web_download_button.dart
index 61a62684b..441efd791 100644
--- a/desktop/lib/widgets/web_download_button.dart
+++ b/desktop/lib/widgets/web_download_button.dart
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
+import '../core/desktop_download.dart';
import '../shared/theme/app_theme.dart' as grid;
import '../shared/theme/workspace_bar_style.dart';
import 'workspace_bar_control.dart';
@@ -12,11 +13,11 @@ import 'workspace_share_button.dart';
Future<bool> openInNewTab(Uri uri) =>
launchUrl(uri, webOnlyWindowName: '_blank');
-/// The Harness product page; its download view is one query away.
+/// The Harness product page.
final harnessAppPage = Uri.parse('https://www.autonomous.ai/harness-app');
-/// The browser's consistent handoff to the existing macOS/Linux download page,
-/// in a new tab so the workspace stays open behind it.
+/// The browser's handoff to the desktop build that suits this computer
+/// ([DesktopDownload]), in a new tab so the workspace stays open behind it.
class WebDownloadButton extends StatelessWidget {
const WebDownloadButton({
super.key,
@@ -30,9 +31,8 @@ class WebDownloadButton extends StatelessWidget {
/// True fills it like Share: the bar's one filled action when Share is off.
final bool prominent;
- static final uri = harnessAppPage.replace(
- queryParameters: {'page': 'download'},
- );
+ /// This computer's build, or the `/desktop` page when none suits it.
+ static Uri get uri => DesktopDownload.uri;
static const _text = 'Download app';
/// Filled like Share when [prominent], a step brighter under the pointer;
diff --git a/desktop/test/desktop_build_test.dart b/desktop/test/desktop_build_test.dart
new file mode 100644
index 000000000..152eb9a3f
--- /dev/null
+++ b/desktop/test/desktop_build_test.dart
@@ -0,0 +1,160 @@
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness/core/desktop_build.dart';
+
+const _macSafari =
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 '
+ '(KHTML, like Gecko) Version/18.0 Safari/605.1.15';
+const _linuxChrome =
+ 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) '
+ 'Chrome/130.0.0.0 Safari/537.36';
+
+ClientHints _hints(
+ String platform, {
+ bool mobile = false,
+ CpuHints? cpu,
+ bool refuse = false,
+}) => ClientHints(
+ platform: platform,
+ mobile: mobile,
+ cpu: () async {
+ if (refuse) throw StateError('refused');
+ return cpu ?? (architecture: '', bitness: '');
+ },
+);
+
+void main() {
+ group('desktopDownloadUri', () {
+ test('a build goes to its own download route', () {
+ expect(
+ desktopDownloadUri(DesktopBuild.macosArm64).toString(),
+ 'https://harness.autonomous.ai/desktop/download/macos-arm64',
+ );
+ expect(
+ desktopDownloadUri(DesktopBuild.macos).toString(),
+ 'https://harness.autonomous.ai/desktop/download-macos',
+ );
+ });
+
+ test('no build goes to the page that lists them all', () {
+ expect(
+ desktopDownloadUri(null).toString(),
+ 'https://harness.autonomous.ai/desktop',
+ );
+ });
+ });
+
+ group('buildFromUserAgent', () {
+ DesktopBuild? of(String ua, {String platform = '', int touch = 0}) =>
+ buildFromUserAgent(
+ NavigatorSnapshot(
+ userAgent: ua,
+ platform: platform,
+ maxTouchPoints: touch,
+ ),
+ );
+
+ test('every Mac gets the build every Mac runs', () {
+ expect(of(_macSafari, platform: 'MacIntel'), DesktopBuild.macos);
+ });
+
+ test('an iPad asking for the desktop site is not a Mac', () {
+ expect(of(_macSafari, platform: 'MacIntel', touch: 5), isNull);
+ });
+
+ test('phones, Windows and ChromeOS have no build', () {
+ expect(
+ of('Mozilla/5.0 (Linux; Android 14; Pixel 8) Chrome/130.0 Mobile'),
+ isNull,
+ );
+ expect(
+ of('Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X)'),
+ isNull,
+ );
+ expect(of('Mozilla/5.0 (Windows NT 10.0; Win64; x64)'), isNull);
+ expect(of('Mozilla/5.0 (X11; CrOS x86_64 14541.0.0)'), isNull);
+ });
+
+ test('Chromium on Linux trusts navigator.platform, not its user agent', () {
+ expect(
+ of(_linuxChrome, platform: 'Linux aarch64'),
+ DesktopBuild.linuxArm64,
+ );
+ expect(of(_linuxChrome, platform: 'Linux x86_64'), DesktopBuild.linuxX64);
+ expect(of(_linuxChrome, platform: 'Linux'), isNull);
+ });
+
+ test('Firefox on Linux names its CPU in the user agent', () {
+ expect(
+ of('Mozilla/5.0 (X11; Linux aarch64; rv:131.0) Firefox/131.0'),
+ DesktopBuild.linuxArm64,
+ );
+ });
+
+ test('a 32-bit ARM userland gets no build', () {
+ expect(
+ of('Mozilla/5.0 (X11; Linux armv8l; rv:131.0) Firefox/131.0'),
+ isNull,
+ );
+ });
+ });
+
+ group('detectDesktopBuild', () {
+ Future<DesktopBuild?> detect(ClientHints hints, {String ua = ''}) =>
+ detectDesktopBuild(NavigatorSnapshot(userAgent: ua, hints: hints));
+
+ test('an ARM Mac gets the Apple silicon build', () async {
+ expect(
+ await detect(
+ _hints('macOS', cpu: (architecture: 'arm', bitness: '64')),
+ ),
+ DesktopBuild.macosArm64,
+ );
+ });
+
+ test('a Mac that will not say keeps the build every Mac runs', () async {
+ expect(await detect(_hints('macOS', refuse: true)), DesktopBuild.macos);
+ expect(
+ await detect(
+ _hints('macOS', cpu: (architecture: 'x86', bitness: '64')),
+ ),
+ DesktopBuild.macos,
+ );
+ });
+
+ test('Linux takes its CPU from the hints', () async {
+ expect(
+ await detect(
+ _hints('Linux', cpu: (architecture: 'arm', bitness: '64')),
+ ),
+ DesktopBuild.linuxArm64,
+ );
+ expect(
+ await detect(
+ _hints('Linux', cpu: (architecture: 'x86', bitness: '64')),
+ ),
+ DesktopBuild.linuxX64,
+ );
+ expect(
+ await detect(
+ _hints('Linux', cpu: (architecture: 'x86', bitness: '32')),
+ ),
+ isNull,
+ );
+ });
+
+ test('Linux whose hints refuse falls back to the user agent', () async {
+ expect(
+ await detect(
+ _hints('Linux', refuse: true),
+ ua: 'Mozilla/5.0 (X11; Linux aarch64; rv:131.0) Firefox/131.0',
+ ),
+ DesktopBuild.linuxArm64,
+ );
+ });
+
+ test('mobile and Windows hints have no build', () async {
+ expect(await detect(_hints('Android', mobile: true)), isNull);
+ expect(await detect(_hints('Windows')), isNull);
+ });
+ });
+}
diff --git a/desktop/test/shared_agent_page_test.dart b/desktop/test/shared_agent_page_test.dart
index 0b5b5b470..8361f0ddb 100644
--- a/desktop/test/shared_agent_page_test.dart
+++ b/desktop/test/shared_agent_page_test.dart
@@ -124,9 +124,10 @@ void main() {
);
expect(link.uri, Uri.parse('https://www.autonomous.ai/harness-app'));
expect(link.target, LinkTarget.blank);
+ // Outside a browser no build can be detected: the page that lists them.
expect(
WebDownloadButton.uri,
- Uri.parse('https://www.autonomous.ai/harness-app?page=download'),
+ Uri.parse('https://harness.autonomous.ai/desktop'),
);
await tester.pumpWidget(const SizedBox());
});
diff --git a/desktop/test/web/web_download_button_test.dart b/desktop/test/web/web_download_button_test.dart
index b8c7256f1..b9ea667c8 100644
--- a/desktop/test/web/web_download_button_test.dart
+++ b/desktop/test/web/web_download_button_test.dart
@@ -4,6 +4,7 @@ library;
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
+import 'package:harness/core/desktop_download.dart';
import 'package:harness/widgets/web_download_button.dart';
import 'package:harness/widgets/workspace_share_button.dart';
@@ -11,6 +12,8 @@ void main() {
testWidgets('a click opens the download page through the new-tab opener', (
tester,
) async {
+ // Settle detection first, so the link cannot change between the click and the check.
+ await tester.runAsync(DesktopDownload.warm);
final opened = <Uri>[];
await tester.pumpWidget(
MaterialApp(