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.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
Enhance WebSocket connection handling for viewer builds and add Apple font detection utility
Dev jacobb73b3f74fac4
diff --git a/desktop/lib/core/apple_fonts.dart b/desktop/lib/core/apple_fonts.dartnew file mode 100644index 000000000..3a58f5b49--- /dev/null+++ b/desktop/lib/core/apple_fonts.dart@@ -0,0 +1,11 @@+import 'dart:io' show Platform;++/// Whether this OS ships Apple's system fonts — SF Pro, SF Mono (CoreText's+/// `.AppleSystemUIFont*` aliases) and Menlo.+///+/// The font chains used to ask `Platform.isMacOS`, which sent iOS down the+/// Linux chain: every family there (DejaVu, Ubuntu, Noto) is missing on an+/// iPhone, so the terminal fell through to a proportional face and its grid+/// came apart. iOS resolves the same CoreText faces as a Mac, so it takes the+/// same chain.+bool get hasAppleFonts => Platform.isMacOS || Platform.isIOS;diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dartindex eea53ecd2..7b47b517f 100644--- a/desktop/lib/state/app_state.dart+++ b/desktop/lib/state/app_state.dart@@ -2238,16 +2238,21 @@ class AppNotifier extends ChangeNotifier {_pool = WsPool(wsBaseUrl: config.wsBaseUrl,autonomousEnv: _autonomousEnv,- // Every real WsConn now dials the local CLI's loopback WS (transportKind.localPlaintext, see+ // Every real desktop WsConn dials the local CLI's loopback WS (transportKind.localPlaintext, see// _conn()), which never calls this — only the compile-time-only local-manual dev fixture (see// LocalManualFixture) still dials a backend directly with a token.- accessTokenProvider: (_, _) async {+ accessTokenProvider: (force, failedToken) async {+ final directAuth = viewer?.auth;+ if (directAuth != null) {+ return directAuth.accessToken(force: force, failedToken: failedToken);+ }final fixture = localManualFixture;if (fixture != null) return fixture.apiKey;throw StateError(- 'unreachable: only the local-manual dev fixture uses a token-bearing WS transport',+ 'unreachable: only a viewer build and the local-manual dev fixture use a token-bearing WS transport',);},+ relayCodecs: viewer?.relayCodecs,onAuthFailure: _signedOutAtRuntime,onLocalFailure: _onLocalFailure,onEvent: _handleEvent,@@ -2345,12 +2350,16 @@ class AppNotifier extends ChangeNotifier {}Future<void> _refreshMachines(int revision) async {- final discovery = _discovery;+ // No machine is "this computer" to a viewer, which has no local CLI: every one — even the one+ // it runs on — is reached through the relay.+ final discovery = viewer == null ? _discovery : null;// The CLI computer id is the local identity source of truth. The loopback// status endpoint is trusted only when it advertises that same identity.- final localComputerId = await discovery.computerId();+ final localComputerId = await discovery?.computerId();if (!_authWorkCurrent(revision)) return;- final localFuture = discovery.discover(expectedComputerId: localComputerId);+ final localFuture =+ discovery?.discover(expectedComputerId: localComputerId) ??+ Future<LocalCliEndpoint?>.value();final list = await _fetchMachines();final localEndpoint = await localFuture;if (!_authWorkCurrent(revision)) return;@@ -2823,7 +2832,7 @@ class AppNotifier extends ChangeNotifier {// goes through the local CLI daemon regardless of whether it's this computer's own machine or a// relayed one: the CLI proxies foreign machines to backend transparently (see `remoteRelay.ts` in// the harness CLI repo), so this app never dials backend's WS directly anymore.- final connection = localManualFixture != null+ final connection = localManualFixture != null || viewer != null? _pool!.connFor(machineId, transportKind: WsTransportKind.cloudE2ee): _pool!.connFor(machineId,diff --git a/desktop/lib/ws/ws_conn.dart b/desktop/lib/ws/ws_conn.dartindex 255dc4c20..bdaefb557 100644--- a/desktop/lib/ws/ws_conn.dart+++ b/desktop/lib/ws/ws_conn.dart@@ -8,6 +8,7 @@ import 'package:web_socket_channel/web_socket_channel.dart';import '../logging/app_log.dart';import '../logging/redact.dart';import '../core/models.dart';+import 'relay_codec.dart';typedef AccessTokenProvider = Future<String> Function(bool forceRefresh,@@ -79,6 +80,11 @@ class WsConn {final String? localApiKey;final int localProtocolVersion;+ /// A viewer build's end-to-end session with the machine, minted fresh on every connect — the+ /// role the harness CLI plays everywhere else (see [RelayCodec]). Null leaves the relay's frames+ /// as they are, which is right for the local transport and the dev fixture.+ final RelayCodecFactory? relayCodecs;+Future<Map<String, dynamic>> Function(String type,Map<String, dynamic> payload,@@ -92,6 +98,7 @@ class WsConn {final void Function(ConnectionStatus status) onStatus;WebSocketChannel? _channel;+ RelayCodec? _codec;StreamSubscription? _sub;bool _closing = false;bool _connecting = false;@@ -156,6 +163,7 @@ class WsConn {this.localWsUri,this.localApiKey,this.localProtocolVersion = 1,+ this.relayCodecs,});Future<void> connect() async {@@ -174,6 +182,16 @@ class WsConn {}if (_closing) return;_tokenUsed = token;+ final codecs = isLocal ? null : relayCodecs;+ if (codecs != null) {+ final codec = await codecs(machineId);+ if (_closing) return;+ if (codec == null) {+ _refusePeer('NO_PEER_LINK');+ return;+ }+ _codec = codec;+ }final Uri uri;if (isLocal) {final local = localWsUri;@@ -223,10 +241,14 @@ class WsConn {}void _onRaw(dynamic raw) {+ final codec = _codec;if (raw is List<int>) {final bytes = Uint8List.fromList(raw);_inboundTail = _inboundTail- .then((_) async => onBinaryFrame?.call(bytes))+ .then((_) async {+ final local = codec == null ? bytes : codec.decodeBinary(bytes);+ if (local != null) await onBinaryFrame?.call(local);+ }).catchError((_) {// Binary E2EE/session code owns recovery for bad frames.});@@ -238,17 +260,19 @@ class WsConn {} catch (_) {return;}+ if (codec != null) {+ _inboundTail = _inboundTail+ .then((_) => _onRelayFrame(codec, message))+ .catchError((_) {+ // Keep the FIFO alive: a frame that will not open is dropped, never dispatched.+ });+ return;+ }final type = message['type'] as String? ?? '';final payload = (message['payload'] as Map<String, dynamic>?) ?? {};if (type == 'connected') {- if (payload['machineId'] == machineId) {- _ready = true;- _attempt = 0;- onStatus(ConnectionStatus.connected);- _flushQueue();- _settleReadiness();- }+ if (payload['machineId'] == machineId) _markReady();return;}final normalized = <String, dynamic>{...message, 'payload': payload};@@ -268,6 +292,67 @@ class WsConn {});}+ void _markReady() {+ _ready = true;+ _attempt = 0;+ onStatus(ConnectionStatus.connected);+ _flushQueue();+ _settleReadiness();+ }++ /// A frame on a relay connection whose E2EE session this app holds: relayClient.ts's dial+ /// handshake, then open-and-dispatch. Nothing is ready — and nothing queued goes out — until the+ /// machine's welcome proves it holds the identity this device pinned for it.+ Future<void> _onRelayFrame(+ RelayCodec codec,+ Map<String, dynamic> message,+ ) async {+ final payload = (message['payload'] as Map<String, dynamic>?) ?? {};+ switch (message['type']) {+ case 'connected':+ // The socket's first `connected` answers the socket itself (it names the user, not a+ // machine); only the select's own ack starts the handshake.+ if (payload['machineId'] == machineId) {+ _channel?.sink.add(jsonEncode(codec.helloFrame()));+ }+ return;+ case 'e2e_welcome':+ if (await codec.handleWelcome(payload)) {+ _markReady();+ } else {+ _refusePeer('E2EE_WELCOME_INVALID');+ }+ return;+ case 'e2e_denied':+ _refusePeer('E2E_DENIED');+ return;+ case 'e2e_rekey':+ codec.handleRekey(payload);+ return;+ }+ final clear = codec.decodeFrame(message);+ if (clear == null) return;+ await _dispatch({+ ...clear,+ 'payload': (clear['payload'] as Map<String, dynamic>?) ?? {},+ });+ }++ /// The machine is reachable but this device may not talk to it: never linked, the link revoked+ /// on its side, or a welcome that did not prove the pinned identity. No retry can fix any of the+ /// three — only a link can — so this stops and says so the way the CLI's own relay does, with+ /// 4404.+ void _refusePeer(String reason) {+ _closing = true;+ _ready = false;+ // needsLink first: AppNotifier's onStatus handler reads machine.needsLink to decide whether a+ // disconnect should be treated as the node going offline — it has to see it flipped before+ // onStatus runs, or the very first 4404 for this machine reads as offline for one retry cycle.+ onLocalFailure?.call(4404, reason);+ onStatus(ConnectionStatus.disconnected);+ unawaited(_channel?.sink.close());+ }+Future<void> _dispatch(Map<String, dynamic> message) async {final payload = (message['payload'] as Map<String, dynamic>?) ?? {};final requestId = payload['requestId'];@@ -389,7 +474,14 @@ class WsConn {return;}try {- channel.sink.add(bytes);+ // Sealed here, inside the outbound FIFO, so frames take their counters in send order.+ final codec = _codec;+ final wire = codec == null ? bytes : codec.encodeBinary(bytes);+ if (wire == null) {+ completer.complete(false);+ return;+ }+ channel.sink.add(wire);completer.complete(true);} catch (_) {completer.complete(false);@@ -459,7 +551,11 @@ class WsConn {if (onOutgoing != null) payload = await onOutgoing!(type, payload);final channel = _channel;if (channel == null) throw StateError('WS is not connected');- channel.sink.add(jsonEncode({'type': type, 'payload': payload}));+ final out = <String, dynamic>{'type': type, 'payload': payload};+ final codec = _codec;+ final wire = codec == null ? out : codec.encodeFrame(out);+ if (wire == null) throw StateError('E2EE session is not ready for $type');+ channel.sink.add(jsonEncode(wire));}void _flushQueue() {@@ -486,6 +582,7 @@ class WsConn {if (!identical(_channel, channel)) return;_channel = null;_sub = null;+ _codec = null;_ready = false;_rejectPending('WS disconnected');if (_closing) {@@ -557,6 +654,7 @@ class WsConn {Future<void> close() async {_closing = true;_ready = false;+ _codec = null;_reconnectTimer?.cancel();_reconnectTimer = null;_rejectPending('WS closed');@@ -577,10 +675,13 @@ class WsConn {/// immediately reconnects with a `forceReconnect` hint so the CLI daemon drops its cached relay entry/// instead of reusing it.Future<void> forceReconnect() async {- if (_closing || isLocal == false) return;+ // A viewer's relay connection holds that session itself, so for it this is simply a fresh dial+ // — and with it a fresh session.+ if (_closing || (!isLocal && relayCodecs == null)) return;_forceRelayReconnect = true;_reconnectTimer?.cancel();_ready = false;+ _codec = null;_rejectPending('forcing relay reconnect');final sub = _sub;_sub = null;diff --git a/desktop/lib/ws/ws_pool.dart b/desktop/lib/ws/ws_pool.dartindex a813c5b01..d98c4f210 100644--- a/desktop/lib/ws/ws_pool.dart+++ b/desktop/lib/ws/ws_pool.dart@@ -1,4 +1,5 @@import 'dart:async';+import 'relay_codec.dart';import '../core/models.dart';import 'ws_conn.dart';@@ -7,6 +8,9 @@ import 'ws_conn.dart';class WsPool {final String wsBaseUrl;final String autonomousEnv;++ /// Handed to every relay connection — a viewer build's E2EE sessions (see [RelayCodec]).+ final RelayCodecFactory? relayCodecs;final AccessTokenProvider accessTokenProvider;final void Function(String message) onAuthFailure;final void Function(String machineId, int code, String reason)?@@ -20,6 +24,7 @@ class WsPool {WsPool({required this.wsBaseUrl,required this.autonomousEnv,+ this.relayCodecs,required this.accessTokenProvider,required this.onAuthFailure,this.onLocalFailure,@@ -47,6 +52,9 @@ class WsPool {final conn = WsConn(wsBaseUrl: wsBaseUrl,autonomousEnv: autonomousEnv,+ relayCodecs: transportKind == WsTransportKind.cloudE2ee+ ? relayCodecs+ : null,machineId: machineId,accessTokenProvider: accessTokenProvider,onAuthFailure: onAuthFailure,