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(desktop): a Team-mode computer sends no Swarm files and says why plainly (#2183)

0xken15271a03c817d5
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dart
index fd19c5845..1f246db66 100644
--- a/desktop/lib/state/app_state.dart
+++ b/desktop/lib/state/app_state.dart
@@ -1703,10 +1703,12 @@ class AppNotifier extends ChangeNotifier {
final target = sharedTabsTarget;
if (target == null) {
return Future.error(
- const WsRequestFailure(
+ WsRequestFailure(
responseType: 'agent_messages_result',
code: 'MACHINE_AWAY',
- detail: 'No computer of yours is online.',
+ detail: _teamComputer
+ ? teamComputerSwarmMessage
+ : 'No computer of yours is online.',
),
);
}
@@ -1733,6 +1735,19 @@ class AppNotifier extends ChangeNotifier {
/// tab with its state and summary.
late final tabActivity = TabActivityReader(request: sharedTabs.request);
+ /// Natively, this computer switched to a Harness Team account (`harness team
+ /// on`): its daemon signs in as the team, which the server never lets act as
+ /// the person, so none of the person's Swarm requests, files or profile
+ /// pictures go out here.
+ bool get _teamComputer =>
+ viewer == null && localMachineState?.machine.isTeamAccount == true;
+
+ /// What the person is told when [_teamComputer] stops a Swarm request.
+ static const teamComputerSwarmMessage =
+ 'This computer runs Swarm agents for a Harness Team account, so it '
+ 'cannot send your own Swarm messages. Open Swarm on another of your '
+ 'machines.';
+
/// The machine [sharedTabs] asks: this computer natively; on the web one of
/// the account's own online machines, never a shared one. Never a Harness
/// Team account's, here either (a computer switched with `harness team on`):
@@ -1769,8 +1784,39 @@ class AppNotifier extends ChangeNotifier {
late final superTabs = SuperTabClient(
storage: localStore ?? HarnessFileStore.shared,
viewStorage: swarmViewStore(beside: localStore),
- upload: uploadWith(() => api),
- avatarUpload: uploadAvatarWith(() => api),
+ upload:
+ ({
+ required tabId,
+ required conversationId,
+ required name,
+ required mime,
+ required bytes,
+ required cancel,
+ required onProgress,
+ }) => _teamComputer
+ ? Future.error(
+ const SuperTabFailure(
+ 'MACHINE_UNAVAILABLE',
+ teamComputerSwarmMessage,
+ ),
+ )
+ : uploadWith(() => api)(
+ tabId: tabId,
+ conversationId: conversationId,
+ name: name,
+ mime: mime,
+ bytes: bytes,
+ cancel: cancel,
+ onProgress: onProgress,
+ ),
+ avatarUpload: ({required tabId, required png}) => _teamComputer
+ ? Future.error(
+ const SuperTabFailure(
+ 'MACHINE_UNAVAILABLE',
+ teamComputerSwarmMessage,
+ ),
+ )
+ : uploadAvatarWith(() => api)(tabId: tabId, png: png),
onMention: superTabMentionNotice(systemNotifications),
onTagCounts: notifyListeners,
onNotices: notifyListeners,
@@ -1855,13 +1901,10 @@ class AppNotifier extends ChangeNotifier {
transport: (method, path, {body}) async {
final target = sharedTabsTarget;
if (target == null) {
- if (viewer == null &&
- localMachineState?.machine.isTeamAccount == true) {
+ if (_teamComputer) {
throw const SuperTabFailure(
'MACHINE_UNAVAILABLE',
- 'This computer runs Swarm agents for a Harness Team account, so '
- 'it cannot send your own Swarm messages. Open Swarm on another '
- 'of your machines.',
+ teamComputerSwarmMessage,
);
}
throw const SuperTabFailure(
diff --git a/desktop/test/swarm_web_relay_team_test.dart b/desktop/test/swarm_web_relay_team_test.dart
index d024f97f1..b6ef9726a 100644
--- a/desktop/test/swarm_web_relay_team_test.dart
+++ b/desktop/test/swarm_web_relay_team_test.dart
@@ -1,4 +1,8 @@
+import 'dart:typed_data';
+
+import 'package:dio/dio.dart';
import 'package:flutter_test/flutter_test.dart';
+import 'package:harness/api/api_client.dart';
import 'package:harness/auth/auth_session.dart';
import 'package:harness/core/config.dart';
import 'package:harness/core/models.dart';
@@ -33,10 +37,39 @@ class _Daemon extends WsConn {
Duration timeout = const Duration(seconds: 20),
}) async {
if (type == 'super_tabs') relayed.add(machineId);
+ if (type == 'agent_messages') relayed.add('$machineId:agent_messages');
return {'value': <String, dynamic>{}};
}
}
+/// The REST client, recording the files the app sends. Nothing leaves the
+/// test: on a computer the real one posts through its local daemon.
+class _Api extends ApiClient {
+ _Api() : super(config: AppConfig.dev, session: AuthSession());
+
+ final posted = <String>[];
+
+ @override
+ Future<Response<dynamic>> postForm(
+ String path,
+ FormData form, {
+ CancelToken? cancelToken,
+ ProgressCallback? onSendProgress,
+ }) async {
+ posted.add(path);
+ return Response(
+ requestOptions: RequestOptions(path: path),
+ statusCode: 200,
+ data: {
+ 'success': true,
+ 'data': {
+ 'attachment': {'id': 'f1'},
+ },
+ },
+ );
+ }
+}
+
/// The machine list as the server sends it (MachineService toOwner): a Harness
/// Team account's machine carries `sessionKind: team` and the Mac it lives on.
Machine _row(String id, {String? sessionKind, String? hostMachineId}) =>
@@ -172,4 +205,73 @@ void main() {
);
expect(relayed, isEmpty);
});
+
+ group('on a computer switched to a Harness Team account', () {
+ late _Api api;
+ final plainly = contains('This computer runs Swarm agents');
+
+ Future<Object?> upload() => app.superTabs.upload(
+ tabId: 'tab',
+ conversationId: 'buddy-chat',
+ name: 'shot.png',
+ mime: 'image/png',
+ bytes: Uint8List.fromList([1, 2, 3]),
+ cancel: CancelToken(),
+ onProgress: (_) {},
+ );
+
+ void computer(String sessionKind) {
+ native(_row('here', sessionKind: sessionKind));
+ app.api = api = _Api();
+ }
+
+ test('a file is sent from this computer\'s own machine as before', () async {
+ computer('computer');
+ await upload();
+ expect(api.posted, ['/api/super-tabs/tab/attachments']);
+ });
+
+ test('a file is refused here, plainly, and never sent', () async {
+ computer('team');
+ await expectLater(
+ upload(),
+ throwsA(
+ isA<SuperTabFailure>()
+ .having((f) => f.code, 'code', 'MACHINE_UNAVAILABLE')
+ .having((f) => f.message, 'message', plainly),
+ ),
+ );
+ expect(api.posted, isEmpty);
+ });
+
+ test('a profile picture is refused here, plainly, and never sent', () async {
+ computer('team');
+ await expectLater(
+ app.superTabs.avatarUpload(
+ tabId: 'tab',
+ png: Uint8List.fromList([1, 2, 3]),
+ ),
+ throwsA(
+ isA<SuperTabFailure>()
+ .having((f) => f.code, 'code', 'MACHINE_UNAVAILABLE')
+ .having((f) => f.message, 'message', plainly),
+ ),
+ );
+ expect(api.posted, isEmpty);
+ });
+
+ test('a shared-tabs read is refused plainly too, and never sent', () async {
+ computer('team');
+ online(_row('elsewhere', sessionKind: 'computer'));
+ await expectLater(
+ app.sharedTabs.request({'action': 'shares'}),
+ throwsA(
+ isA<WsRequestFailure>()
+ .having((f) => f.code, 'code', 'MACHINE_AWAY')
+ .having((f) => f.detail, 'detail', plainly),
+ ),
+ );
+ expect(relayed, isEmpty);
+ });
+ });
}