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
Phone threads: attach files to a reply like the chat composer (#2197)
harness-for-autonomous[bot]549b0577978a
diff --git a/mobile/lib/swarm/swarm_attach.dart b/mobile/lib/swarm/swarm_attach.dartindex 38a98329f..3206f942b 100644--- a/mobile/lib/swarm/swarm_attach.dart+++ b/mobile/lib/swarm/swarm_attach.dart@@ -8,7 +8,6 @@ import 'package:harness_mobile/shared/theme/app_theme.dart';import 'package:harness_mobile/shared/theme/app_type.dart';import 'package:harness_mobile/shared/widgets/touch_target.dart';-import 'swarm_conversation.dart';import 'swarm_file_picker.dart';import 'swarm_message_row.dart'show swarmChatCard, swarmChatHair, swarmFileSize;@@ -178,13 +177,14 @@ Future<bool> showSwarmAccessOff(/// thumbnail or a file's mark, its name, its size — or, going up, how much has gone — and ✕ to/// take it off. Nothing while there are none.class SwarmOutgoingChips extends StatelessWidget {- const SwarmOutgoingChips({super.key, required this.conversation});+ const SwarmOutgoingChips({super.key, required this.box});- final SwarmConversation conversation;+ /// The chat's composer, or a thread's reply box.+ final SwarmFilesBox box;@overrideWidget build(BuildContext context) {- final files = conversation.files.files;+ final files = box.files.files;if (files.isEmpty) return const SizedBox.shrink();return SizedBox(height: _chipHeight + AppSpace.s,@@ -196,10 +196,10 @@ class SwarmOutgoingChips extends StatelessWidget {separatorBuilder: (_, _) => const SizedBox(width: AppSpace.s),itemBuilder: (context, i) => _Chip(file: files[i],- progress: conversation.isUploading(files[i])- ? conversation.uploadProgress(files[i]) ?? 0+ progress: box.isUploading(files[i])+ ? box.uploadProgress(files[i]) ?? 0: null,- onRemove: () => conversation.files.remove(files[i]),+ onRemove: () => box.files.remove(files[i]),),),);diff --git a/mobile/lib/swarm/swarm_conversation.dart b/mobile/lib/swarm/swarm_conversation.dartindex e6c7557af..088689b90 100644--- a/mobile/lib/swarm/swarm_conversation.dart+++ b/mobile/lib/swarm/swarm_conversation.dart@@ -1,6 +1,5 @@import 'dart:async';-import 'package:dio/dio.dart' show CancelToken;import 'package:flutter/foundation.dart';import 'model.dart';@@ -9,6 +8,7 @@ import 'swarm_chat_cache.dart';import 'swarm_outgoing_files.dart';import 'swarm_reader.dart';import 'swarm_request.dart';+import 'swarm_uploads.dart';/// One conversation on the phone: its messages and what the person posts to/// it — the desktop's `SuperTabController` messages and composer, for one@@ -25,7 +25,7 @@ import 'swarm_request.dart';/// and is asked for by id, as a closed one is in no full read; one it never/// had is asked for while its messages are read. One the person cannot read/// keeps nothing here: messages, draft and a post not confirmed.-class SwarmConversation extends ChangeNotifier {+class SwarmConversation extends ChangeNotifier implements SwarmFilesBox {/// [shown] and [heldAt] as they are from the start, so what it kept is/// read only as far as it is seen.SwarmConversation(@@ -58,10 +58,36 @@ class SwarmConversation extends ChangeNotifier {/// The files waiting to go with the next post. They stay in memory, and go/// when the chat closes: only what the person typed is kept, and only while/// the chat is open.+ @overridefinal files = SwarmOutgoingFiles();/// The files of a post are on their way to the backend, before the post.- bool uploading = false;+ bool get uploading => _uploads.uploading;++ late final _uploads = SwarmUploads(+ reader,+ id,+ timeout: uploadTimeout,+ onChanged: () {+ if (!_disposed) _tell();+ },+ onProgress: _composer.changed,+ );++ /// The files waiting to go with each thread's next reply, by its root:+ /// kept with its draft while the chat is open ([SwarmThread]).+ final _threadFiles = <String, SwarmOutgoingFiles>{};++ /// The files waiting to go with [root]'s thread's next reply.+ SwarmOutgoingFiles threadFiles(String root) =>+ _threadFiles.putIfAbsent(root, SwarmOutgoingFiles.new);++ /// [root]'s thread closed: its [box] goes when it holds nothing.+ void releaseThreadFiles(String root, SwarmOutgoingFiles box) {+ if (identical(_threadFiles[root], box) && box.files.isEmpty) {+ _threadFiles.remove(root);+ }+ }/// Oldest first: every message read, a reply only in its thread too.List<SuperTabMessage> messages = const [];@@ -197,19 +223,6 @@ class SwarmConversation extends ChangeNotifier {})?_sent;- /// Files already uploaded for the post being sent, with their ids, so a- /// retry sends the ids rather than uploading the files again.- final _uploaded = <SwarmOutgoingFile, String>{};-- /// How much of each file going up has gone, from 0 to 1.- final _progress = <SwarmOutgoingFile, double>{};-- /// The files of the upload under way; [_uploadRun] moves when it is given- /// up, so an answer that lands after is dropped.- List<SwarmOutgoingFile>? _uploading;- int _uploadRun = 0;- CancelToken? _cancel;-SuperTabMessage? _replyingTo;/// Where its messages are read, a thread's with `?thread=`.@@ -257,14 +270,11 @@ class SwarmConversation extends ChangeNotifier {!uploading &&(_draft.trim().isNotEmpty || files.files.isNotEmpty);- /// Whether [file] is still on its way to the backend in a send.- bool isUploading(SwarmOutgoingFile file) =>- uploading &&- (_uploading?.contains(file) ?? false) &&- !_uploaded.containsKey(file);+ @override+ bool isUploading(SwarmOutgoingFile file) => _uploads.isUploading(file);- /// How much of [file] has gone up, from 0 to 1, while it is going up.- double? uploadProgress(SwarmOutgoingFile file) => _progress[file];+ @override+ double? uploadProgress(SwarmOutgoingFile file) => _uploads.progress(file);/// The message the draft answers: the reply bar over the composer, and the/// post's `replyTo`.@@ -422,6 +432,11 @@ class SwarmConversation extends ChangeNotifier {_forget();_draft = '';_threadDrafts.clear();+ // A thread's files go too; its own screen hears it and gives up theirs.+ for (final box in _threadFiles.values) {+ box.clear();+ }+ _threadFiles.clear();_mentions.clear();_pending = null;_sent = null;@@ -648,9 +663,7 @@ class SwarmConversation extends ChangeNotifier {// A refusal of the files themselves sends them up once more; one of// the post (a mention, a recipient) leaves them uploaded.if (failure.code.contains('ATTACHMENT')) {- for (final file in _sent?.files ?? const <SwarmOutgoingFile>[]) {- _uploaded.remove(file);- }+ _uploads.forget(_sent?.files ?? const []);}_pending = null;_restore();@@ -664,9 +677,7 @@ class SwarmConversation extends ChangeNotifier {sending = false;if (!_disposed) _tell();}- for (final file in _sent?.files ?? const <SwarmOutgoingFile>[]) {- _uploaded.remove(file);- }+ _uploads.forget(_sent?.files ?? const []);unawaited(reader.refresh());// Shown as on its way until the read that brings it back.await load();@@ -682,108 +693,22 @@ class SwarmConversation extends ChangeNotifier {/// files stay where they are.Future<List<String>?> _uploadAll(List<SwarmOutgoingFile> outgoing) async {if (outgoing.isEmpty) return const [];- final run = ++_uploadRun;- uploading = true;error = null;- _uploading = outgoing;- _uploaded.removeWhere((file, _) => !outgoing.contains(file));- _tell();- try {- final ids = <String>[];- for (final file in outgoing) {- var id = _uploaded[file];- if (id == null) {- final answer = await _uploadFile(file);- if (run != _uploadRun) return null;- id = superTabMap(answer['attachment'])['id'] as String;- _uploaded[file] = id;- _progress.remove(file);- _composer.changed();- }- ids.add(id);- }- return ids;- } catch (failure) {- if (!_disposed && run == _uploadRun) {- error = failure is SuperTabFailure- ? failure.message- : failure is TimeoutException- ? 'The upload stopped moving. Try again.'- : 'Could not upload ${outgoing.length == 1 ? 'the file' : 'the files'}. Try again.';- }- return null;- } finally {- if (run == _uploadRun) {- uploading = false;- _uploading = null;- _progress.clear();- if (!_disposed) _tell();- }- }- }-- /// Sends [file] to the backend and returns its attachment. Gives up when no- /// byte has moved for [uploadTimeout], or when the upload is given up.- Future<Map<String, dynamic>> _uploadFile(SwarmOutgoingFile file) async {- final token = _cancel = CancelToken();- Timer? watchdog;- var stalled = false;- void watch() {- watchdog?.cancel();- watchdog = Timer(uploadTimeout, () {- stalled = true;- token.cancel('stalled');- });- }-- watch();- try {- // The token also ends a transport that ignores it.- return await Future.any([- reader.upload(- tabId: reader.tabId,- conversationId: id,- name: file.name,- mime: swarmMimeForName(file.name),- bytes: file.bytes,- cancel: token,- onProgress: (sent) {- if (token.isCancelled) return;- watch();- _progress[file] = sent;- _composer.changed();- },- ),- token.whenCancel.then<Map<String, dynamic>>(- (cancelled) => throw cancelled,- ),- ]);- } catch (_) {- if (stalled) throw TimeoutException('The upload stopped moving.');- rethrow;- } finally {- watchdog?.cancel();+ final up = await _uploads.upload(outgoing);+ if (up.failure case final failure? when !_disposed) {+ error = swarmUploadError(failure, outgoing.length);+ _tell();}+ return up.ids;}/// Gives up the files on their way: the composer is free again, and the/// draft and the files stay.- void cancelUpload() {- if (!uploading) return;- _cancel?.cancel('given up');- _uploadRun++;- uploading = false;- _uploading = null;- _progress.clear();- if (!_disposed) _tell();- }+ void cancelUpload() => _uploads.cancel();/// Taking a file off while the files go up gives the upload up.void _filesChanged() {- final going = _uploading;- if (uploading && going != null && !files.files.toSet().containsAll(going)) {- cancelUpload();- }+ _uploads.keepTo(files.files);_composer.changed();}@@ -969,8 +894,11 @@ class SwarmConversation extends ChangeNotifier {@overridevoid dispose() {_disposed = true;- _cancel?.cancel('closed');- _uploadRun++;+ _uploads.close();+ for (final box in _threadFiles.values) {+ box.clear();+ }+ _threadFiles.clear();files..removeListener(_filesChanged)..clear()diff --git a/mobile/lib/swarm/swarm_conversation_page.dart b/mobile/lib/swarm/swarm_conversation_page.dartindex 84dab8702..6dc81b097 100644--- a/mobile/lib/swarm/swarm_conversation_page.dart+++ b/mobile/lib/swarm/swarm_conversation_page.dart@@ -63,6 +63,33 @@ void openSwarmConversation(@visibleForTestingFuture<bool> Function() openSwarmSettings = openAppSettings;+/// A composer's + — the chat's, or a thread's reply box: the sheet, then the+/// picker it names, its files into [files], and what to say under the box to+/// [note]. A refusal of the camera or photos offers the phone's Settings.+/// Nothing is added, or said, once [stillWanted] fails.+Future<void> attachSwarmFiles(+ BuildContext context,+ SwarmOutgoingFiles files, {+ required bool Function() stillWanted,+ required ValueChanged<String?> note,+}) async {+ final source = await showSwarmAttachSheet(context);+ if (source == null || !stillWanted()) return;+ final outcome = await addSwarmPicked(files, source, stillWanted: stillWanted);+ if (!stillWanted() || !context.mounted) return;+ switch (outcome) {+ case SwarmPicked(note: final said):+ note(said);+ case SwarmPickFailed(:final message):+ note(message);+ case SwarmPickDenied(:final source):+ note(null);+ if (await showSwarmAccessOff(context, source)) {+ unawaited(openSwarmSettings());+ }+ }+}+/// One conversation: its messages, newest at the foot, older ones on demand —/// each with who wrote it, their role and when, follow-ups grouped, a reply's/// quote leading to its original; the decisions asked of the person in it;@@ -367,31 +394,17 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>_send(c);}- /// The composer's +: the sheet, then the picker it names, its files into- /// [c]'s. A refusal of the camera or photos offers the phone's Settings.+ /// The composer's + ([attachSwarmFiles]), its files into [c]'s.Future<void> _pickFiles(SwarmConversation c) async {if (_picking) return;_picking = true;try {- final source = await showSwarmAttachSheet(context);- if (source == null || !mounted || _conversation != c) return;- final outcome = await addSwarmPicked(+ await attachSwarmFiles(+ context,c.files,- source,stillWanted: () => mounted && _conversation == c,+ note: (note) => _attachNote.value = note,);- if (!mounted || _conversation != c) return;- switch (outcome) {- case SwarmPicked(:final note):- _attachNote.value = note;- case SwarmPickFailed(:final message):- _attachNote.value = message;- case SwarmPickDenied(:final source):- _attachNote.value = null;- if (await showSwarmAccessOff(context, source)) {- unawaited(openSwarmSettings());- }- }} finally {_picking = false;}@@ -579,7 +592,7 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>c.reader.snapshot?.personLook(c.reader.accountId) ??(picture: null, status: null),text: text,- files: const [],+ files: t.pendingFiles,mentions: _view.value?.mentions ?? SwarmMentions(const []),// Taken by the backend and not read back yet: still on its way.sending: t.sending || t.confirmed,@@ -1519,24 +1532,16 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>style: AppType.caption(color: AppPalette.textSecondary),),),- SwarmOutgoingChips(conversation: c),+ SwarmOutgoingChips(box: c),Row(crossAxisAlignment: CrossAxisAlignment.end,children: [if (swarmSendsFiles)- SwarmRoundButton(+ SwarmAttachButton(key: const ValueKey('swarm-attach'),- label: 'Attach files',onTap: c.uploading || c.files.room <= 0? null: () => unawaited(_pickFiles(c)),- child: Icon(- LucideIcons.plus,- size: AppIconSize.standalone,- color: c.uploading || c.files.room <= 0- ? AppPalette.textFaint- : AppPalette.textSecondary,- ),),const SizedBox(width: 6),Expanded(@@ -2575,6 +2580,25 @@ class SwarmRoundButton extends StatelessWidget {);}+/// A composer's + ([attachSwarmFiles]): off, and faint, while files go up or+/// no more fit.+class SwarmAttachButton extends StatelessWidget {+ const SwarmAttachButton({super.key, required this.onTap});++ final VoidCallback? onTap;++ @override+ Widget build(BuildContext context) => SwarmRoundButton(+ label: 'Attach files',+ onTap: onTap,+ child: Icon(+ LucideIcons.plus,+ size: AppIconSize.standalone,+ color: onTap == null ? AppPalette.textFaint : AppPalette.textSecondary,+ ),+ );+}+/// A link in the accent: Earlier messages.class _Link extends StatelessWidget {const _Link({super.key, required this.label, required this.onTap});diff --git a/mobile/lib/swarm/swarm_outgoing_files.dart b/mobile/lib/swarm/swarm_outgoing_files.dartindex 79fd96f8e..808937dac 100644--- a/mobile/lib/swarm/swarm_outgoing_files.dart+++ b/mobile/lib/swarm/swarm_outgoing_files.dart@@ -94,6 +94,18 @@ class SwarmOutgoingFiles extends ChangeNotifier {}}+/// A composer that sends [files] with its next message — the chat's, or a+/// thread's reply box — and how far each has gone up while it sends.+abstract interface class SwarmFilesBox {+ SwarmOutgoingFiles get files;++ /// Whether [file] is still on its way to the backend in a send.+ bool isUploading(SwarmOutgoingFile file);++ /// How much of [file] has gone up, from 0 to 1, while it is going up.+ double? uploadProgress(SwarmOutgoingFile file);+}+/// [name], or `name-2.ext`, `name-3.ext`… — the first not in [taken], which/// then holds it.String swarmFreeName(String name, Set<String> taken) {diff --git a/mobile/lib/swarm/swarm_thread.dart b/mobile/lib/swarm/swarm_thread.dartindex 91c5bc57f..0a1084bd6 100644--- a/mobile/lib/swarm/swarm_thread.dart+++ b/mobile/lib/swarm/swarm_thread.dart@@ -4,8 +4,10 @@ import 'package:flutter/foundation.dart';import 'model.dart';import 'swarm_conversation.dart';+import 'swarm_outgoing_files.dart';import 'swarm_reader.dart';import 'swarm_request.dart';+import 'swarm_uploads.dart';/// One thread of a chat on the phone: its root, its replies and the reply/// box — the desktop's thread panel (`SuperTabThreadPanel`) and what its@@ -16,12 +18,20 @@ import 'swarm_request.dart';/// ones on demand ([loadEarlier]), and the root by itself when no page/// brought it. Replies that arrive later come with the chat's own reads,/// which keep running under it. Nothing of it stays once the person cannot-/// read the chat: its rows, its draft and a reply not confirmed.-class SwarmThread extends ChangeNotifier {- SwarmThread(this.chat, this.root) : _draft = chat.threadDraft(root) {+/// read the chat: its rows, its draft, its files and a reply not confirmed.+///+/// Files go with a reply as they go with a post in the chat: the same+/// picker, limits and upload ([SwarmUploads]) to the chat's conversation,+/// before the reply. They wait in the chat's box for this thread+/// ([SwarmConversation.threadFiles]) while the chat is open.+class SwarmThread extends ChangeNotifier implements SwarmFilesBox {+ SwarmThread(this.chat, this.root)+ : _draft = chat.threadDraft(root),+ files = chat.threadFiles(root) {_listed = chat.conversation != null;chat.addListener(_chatChanged);reader.addListener(_chatChanged);+ files.addListener(_filesChanged);}final SwarmConversation chat;@@ -29,6 +39,31 @@ class SwarmThread extends ChangeNotifier {/// The id of the message the thread hangs from.final String root;+ /// The files waiting to go with the next reply.+ @override+ final SwarmOutgoingFiles files;++ late final _uploads = SwarmUploads(+ reader,+ chat.id,+ timeout: chat.uploadTimeout,+ onChanged: () {+ if (!_disposed) _tell();+ },+ onProgress: () {+ if (!_disposed) _composer.changed();+ },+ );++ /// The reply's files are on their way, before the reply.+ bool get uploading => _uploads.uploading;++ @override+ bool isUploading(SwarmOutgoingFile file) => _uploads.isUploading(file);++ @override+ double? uploadProgress(SwarmOutgoingFile file) => _uploads.progress(file);+SwarmReader get reader => chat.reader;/// How many pages a thread reads when it opens; past them "Show earlier@@ -155,12 +190,15 @@ class SwarmThread extends ChangeNotifier {Map<String, dynamic>? _pending;/// What the box held when [_pending] left it: a refusal puts it back.- ({String draft, bool alsoSend})? _sent;+ ({String draft, bool alsoSend, List<SwarmOutgoingFile> files})? _sent;/// The reply on its way or not confirmed: drawn under the replies,/// "Sending…" and then [unconfirmed], until it is read back.String? get pending => (_pending?['command'] as Map?)?['text'] as String?;+ /// The files that went with [pending].+ List<SwarmOutgoingFile> get pendingFiles => _sent?.files ?? const [];+/// The backend took the reply on its way, which no read has brought back/// yet: it stays on its way, and is not sent again.bool get confirmed => _confirmed;@@ -169,9 +207,20 @@ class SwarmThread extends ChangeNotifier {/// A reply was sent and not confirmed: [send] retries it.bool get unconfirmed => _pending != null && !sending && !_confirmed;- /// The box may send now: there is text, and no reply is under way.+ /// The box may send now: there is text or a file, and no reply or upload+ /// is under way.bool get canSend =>- canPost && _pending == null && !sending && _draft.trim().isNotEmpty;+ canPost &&+ _pending == null &&+ !sending &&+ !uploading &&+ (_draft.trim().isNotEmpty || files.files.isNotEmpty);++ /// Taking a file off while the files go up gives the upload up.+ void _filesChanged() {+ _uploads.keepTo(files.files);+ if (!_disposed) _composer.changed();+ }/// The chat or its Swarm changed: replies read since show, and a thread the/// person can no longer read keeps nothing.@@ -212,10 +261,15 @@ class SwarmThread extends ChangeNotifier {!_refused ||_rows.isNotEmpty ||_draft.isNotEmpty ||+ files.files.isNotEmpty ||+ uploading ||_pending != null ||error != null;_refused = true;_forget();+ // The files on their way stop, and none stays in the box.+ _uploads.cancel();+ files.clear();_draft = '';_alsoSend = false;_pending = null;@@ -400,11 +454,43 @@ class SwarmThread extends ChangeNotifier {/// leaves the box at once and shows as [pending] until it is read back./// True once the backend took it.Future<bool> send() async {- if (sending || _confirmed || _disposed || !canPost) return false;+ if (sending || uploading || _confirmed || _disposed || !canPost) {+ return false;+ }+ // An answer that comes back after the thread closed, or after what it+ // held was dropped, changes nothing: no text or files back in a box, no+ // read.+ final epoch = _epoch;+ bool current() => !_disposed && epoch == _epoch;var pending = _pending;if (pending == null) {+ final outgoing = files.files;+ if ((_draft.trim().isEmpty && outgoing.isEmpty) ||+ chat.conversation == null) {+ return false;+ }+ // The files go up first, the draft and the files staying in the box+ // meanwhile, as the chat's do.+ var ids = const <String>[];+ if (outgoing.isNotEmpty) {+ error = null;+ final up = await _uploads.upload(outgoing);+ if (!current()) return false;+ if (up.failure case final failure?) {+ if (failure is SuperTabFailure && _refusedChat(failure)) {+ return false;+ }+ error = swarmUploadError(failure, outgoing.length);+ _tell();+ return false;+ }+ ids = up.ids ?? const [];+ if (up.ids == null) return false;+ }final text = _draft.trim();- if (text.isEmpty || chat.conversation == null) return false;+ if ((text.isEmpty && ids.isEmpty) || chat.conversation == null) {+ return false;+ }pending = {'operationId': superTabOperationId(),'command': {@@ -414,21 +500,22 @@ class SwarmThread extends ChangeNotifier {'replyTo': root,'recipientIds': const <String>[],if (_alsoSend) 'alsoSend': true,+ if (ids.isNotEmpty) 'attachmentIds': ids,},};- _sent = (draft: _draft, alsoSend: _alsoSend);+ _sent = (draft: _draft, alsoSend: _alsoSend, files: outgoing);_draft = '';chat.setThreadDraft(root, '');_alsoSend = false;+ // Only what goes with this reply: more may have been added meanwhile.+ for (final file in outgoing) {+ files.remove(file);+ }}_pending = pending;sending = true;error = null;_tell();- // An answer that comes back after the thread closed, or after what it- // held was dropped, changes nothing: no text back in a box, no read.- final epoch = _epoch;- bool current() => !_disposed && epoch == _epoch;try {await reader.request('POST',@@ -443,6 +530,9 @@ class SwarmThread extends ChangeNotifier {// reply.final status = failure.statusCode;if (status >= 400 && status < 500 && status != 408 && status != 429) {+ // A refusal of the files themselves sends them up once more; one of+ // the reply leaves them uploaded.+ if (failure.code.contains('ATTACHMENT')) _uploads.forget(pendingFiles);_pending = null;_restore();}@@ -457,6 +547,7 @@ class SwarmThread extends ChangeNotifier {if (!_disposed) _tell();}if (!current()) return true;+ _uploads.forget(pendingFiles);unawaited(reader.refresh());// On its way until a read brings it back: this one, or the chat's next.await chat.load();@@ -466,13 +557,20 @@ class SwarmThread extends ChangeNotifier {return true;}- /// A refused reply's text back in the box, before anything typed since.+ /// A refused reply's text and files back in the box, before anything+ /// typed or added since.void _restore() {final sent = _sent;_sent = null;if (sent == null) return;draft = _draft.trim().isEmpty ? sent.draft : '${sent.draft}\n$_draft';_alsoSend = _alsoSend || sent.alsoSend;+ if (sent.files.isNotEmpty) {+ final since = files.files;+ files+ ..clear()+ ..add([...sent.files, ...since]);+ }}/// A reply moved between the box and the messages: both are told.@@ -485,6 +583,10 @@ class SwarmThread extends ChangeNotifier {void dispose() {_disposed = true;_epoch++;+ // An upload on its way stops; the files wait in the chat's box.+ _uploads.close();+ files.removeListener(_filesChanged);+ chat.releaseThreadFiles(root, files);chat.removeListener(_chatChanged);reader.removeListener(_chatChanged);_composer.dispose();diff --git a/mobile/lib/swarm/swarm_thread_page.dart b/mobile/lib/swarm/swarm_thread_page.dartindex 6787a195a..4076a4167 100644--- a/mobile/lib/swarm/swarm_thread_page.dart+++ b/mobile/lib/swarm/swarm_thread_page.dart@@ -8,8 +8,11 @@ import 'package:harness_mobile/phone/tty_controls.dart';import 'package:harness_mobile/shared/theme/app_ink.dart';import 'package:harness_mobile/shared/theme/app_space.dart';import 'package:harness_mobile/shared/theme/app_theme.dart';+import 'package:harness_mobile/shared/theme/app_type.dart';import 'model.dart';+import 'swarm_attach.dart';+import 'swarm_choices.dart';import 'swarm_conversation.dart';import 'swarm_conversation_page.dart';import 'swarm_message_row.dart';@@ -32,7 +35,8 @@ typedef SwarmThreadPending = Widget Function(SwarmThread thread, String text);/// One thread of a chat, full screen as Slack Mobile opens one: its root at/// the top, "N replies", its replies, and a reply box at the foot with Also-/// send (the desktop's thread panel, Tony's mockup of 10-29).+/// send and the chat's + for files (the desktop's thread panel, Tony's+/// mockup of 10-29).////// Its rows are drawn by the chat that opened it ([row], [pending]), so a/// message looks the same in both. While it is up its Swarm is watched, so@@ -72,6 +76,10 @@ class _SwarmThreadPageState extends State<SwarmThreadPage> {/// The message whose actions are open (Copy), by a tap on it.String? _acting;+ /// What the last pick left out, or why it could not pick, under the box.+ final _attachNote = ValueNotifier<String?>(null);+ bool _picking = false;+SwarmSession? _session;bool _watching = false;@@ -102,6 +110,7 @@ class _SwarmThreadPageState extends State<SwarmThreadPage> {if (_watching) _session?.unwatch(widget.swarmId);_thread.composer.removeListener(_drafted);_thread.dispose();+ _attachNote.dispose();_text.dispose();_focus.dispose();_scroll.dispose();@@ -124,9 +133,28 @@ class _SwarmThreadPageState extends State<SwarmThreadPage> {);}+ /// The box's + ([attachSwarmFiles]), its files into the thread's.+ Future<void> _pickFiles() async {+ if (_picking) return;+ _picking = true;+ try {+ await attachSwarmFiles(+ context,+ _thread.files,+ // Nothing goes into a thread the person can no longer reply in.+ stillWanted: () =>+ mounted && _thread.state != SwarmChatState.lost && _thread.canPost,+ note: (note) => _attachNote.value = note,+ );+ } finally {+ _picking = false;+ }+ }+/// Sends the reply, at the foot of the thread where it shows. Once the/// backend took it the keyboard goes down, unless the next is being typed.Future<void> _send() async {+ _attachNote.value = null;if (_scroll.hasClients) {unawaited(_scroll.animateTo(@@ -364,12 +392,14 @@ class _SwarmThreadPageState extends State<SwarmThreadPage> {);}- /// The reply box: a field that grows with what is typed, then Also send- /// and a round Send (the desktop's thread composer).+ /// The reply box: the files going with the reply, a field that grows with+ /// what is typed, then Also send, the + and a round Send (the desktop's+ /// thread composer).Widget _box(SwarmThread t, Tty tty) {final canSend = t.canSend;// A reply not confirmed is sent again from where it shows.final writable = !t.sending && t.pending == null;+ final canAttach = !t.uploading && t.files.room > 0;return Container(key: const ValueKey('swarm-thread-box'),decoration: BoxDecoration(@@ -385,6 +415,25 @@ class _SwarmThreadPageState extends State<SwarmThreadPage> {child: Column(crossAxisAlignment: CrossAxisAlignment.stretch,children: [+ ValueListenableBuilder(+ valueListenable: _attachNote,+ builder: (context, note, _) => note == null+ ? const SizedBox.shrink()+ : Padding(+ padding: const EdgeInsets.fromLTRB(+ AppSpace.xs,+ 0,+ AppSpace.xs,+ AppSpace.s,+ ),+ child: Text(+ note,+ key: const ValueKey('swarm-thread-attach-note'),+ style: AppType.caption(color: tty.yellow),+ ),+ ),+ ),+ SwarmOutgoingChips(box: t),SwarmMessageField(controller: _text,focus: _focus,@@ -400,6 +449,11 @@ class _SwarmThreadPageState extends State<SwarmThreadPage> {onChanged: writable ? (on) => t.alsoSend = on : null,),),+ if (swarmSendsFiles)+ SwarmAttachButton(+ key: const ValueKey('swarm-thread-attach'),+ onTap: canAttach ? () => unawaited(_pickFiles()) : null,+ ),SwarmRoundButton(key: const ValueKey('swarm-thread-send'),label: 'Send reply',diff --git a/mobile/lib/swarm/swarm_uploads.dart b/mobile/lib/swarm/swarm_uploads.dartnew file mode 100644index 000000000..4729b4d1e--- /dev/null+++ b/mobile/lib/swarm/swarm_uploads.dart@@ -0,0 +1,189 @@+import 'dart:async';++import 'package:dio/dio.dart' show CancelToken;+import 'package:flutter/foundation.dart';++import 'model.dart' show superTabMap;+import 'swarm_outgoing_files.dart';+import 'swarm_reader.dart';+import 'swarm_request.dart';++/// What one [SwarmUploads.upload] came to: the files' ids, or what stopped+/// it — neither when it was given up.+typedef SwarmUploaded = ({List<String>? ids, Object? failure});++/// What to tell the person about an upload that stopped on [failure], of+/// [count] files: the backend's own words, or the chat's.+String swarmUploadError(Object failure, int count) => failure is SuperTabFailure+ ? failure.message+ : failure is TimeoutException+ ? 'The upload stopped moving. Try again.'+ : 'Could not upload ${count == 1 ? 'the file' : 'the files'}. Try again.';++/// The files of one composer's message on their way to the backend before+/// the message is sent — the chat's, or a thread's reply box: one by one,+/// each given up when no byte has moved for [timeout], and the ids of those+/// already up kept, so a retry sends them rather than uploading again.+class SwarmUploads {+ SwarmUploads(+ this.reader,+ this.conversationId, {+ required this.timeout,+ required this.onChanged,+ required this.onProgress,+ });++ final SwarmReader reader;++ /// The conversation the files go up for: the only one they may go with.+ final String conversationId;++ /// How long an upload may go without a byte moving before it gives up.+ final Duration timeout;++ /// [uploading] turned on or off.+ final VoidCallback onChanged;++ /// A file went further up, or is up.+ final VoidCallback onProgress;++ /// Files are on their way.+ bool get uploading => _uploading;+ bool _uploading = false;++ /// Files already up for the message being sent, with their ids.+ final _uploaded = <SwarmOutgoingFile, String>{};++ /// How much of each file going up has gone, from 0 to 1.+ final _progress = <SwarmOutgoingFile, double>{};++ /// The files of the upload under way; [_run] moves when it is given up, so+ /// an answer that lands after is dropped.+ List<SwarmOutgoingFile>? _going;+ int _run = 0;+ CancelToken? _cancel;++ /// Whether [file] is still on its way.+ bool isUploading(SwarmOutgoingFile file) =>+ _uploading &&+ (_going?.contains(file) ?? false) &&+ !_uploaded.containsKey(file);++ /// How much of [file] has gone up, from 0 to 1, while it is going up.+ double? progress(SwarmOutgoingFile file) => _progress[file];++ /// Uploads each of [outgoing] that has not gone up yet and returns their+ /// ids, what stopped it, or neither when it was given up meanwhile.+ Future<SwarmUploaded> upload(List<SwarmOutgoingFile> outgoing) async {+ if (outgoing.isEmpty) return (ids: const <String>[], failure: null);+ final run = ++_run;+ _uploading = true;+ _going = outgoing;+ _uploaded.removeWhere((file, _) => !outgoing.contains(file));+ onChanged();+ try {+ final ids = <String>[];+ for (final file in outgoing) {+ var id = _uploaded[file];+ if (id == null) {+ final answer = await _uploadFile(file);+ if (run != _run) return (ids: null, failure: null);+ id = superTabMap(answer['attachment'])['id'] as String;+ _uploaded[file] = id;+ _progress.remove(file);+ onProgress();+ }+ ids.add(id);+ }+ return (ids: ids, failure: null);+ } catch (failure) {+ return (ids: null, failure: run == _run ? failure : null);+ } finally {+ if (run == _run) {+ _uploading = false;+ _going = null;+ _progress.clear();+ onChanged();+ }+ }+ }++ /// Sends [file] to the backend and returns its attachment. Gives up when no+ /// byte has moved for [timeout], or when the upload is given up.+ Future<Map<String, dynamic>> _uploadFile(SwarmOutgoingFile file) async {+ final token = _cancel = CancelToken();+ Timer? watchdog;+ var stalled = false;+ void watch() {+ watchdog?.cancel();+ watchdog = Timer(timeout, () {+ stalled = true;+ token.cancel('stalled');+ });+ }++ watch();+ try {+ // The token also ends a transport that ignores it.+ return await Future.any([+ reader.upload(+ tabId: reader.tabId,+ conversationId: conversationId,+ name: file.name,+ mime: swarmMimeForName(file.name),+ bytes: file.bytes,+ cancel: token,+ onProgress: (sent) {+ if (token.isCancelled) return;+ watch();+ _progress[file] = sent;+ onProgress();+ },+ ),+ token.whenCancel.then<Map<String, dynamic>>(+ (cancelled) => throw cancelled,+ ),+ ]);+ } catch (_) {+ if (stalled) throw TimeoutException('The upload stopped moving.');+ rethrow;+ } finally {+ watchdog?.cancel();+ }+ }++ /// [files] go up again at the next send: the backend refused them, or the+ /// message they went with was taken.+ void forget(Iterable<SwarmOutgoingFile> files) {+ for (final file in files) {+ _uploaded.remove(file);+ }+ }++ /// Gives up the files on their way: the composer is free again, and its+ /// draft and files stay.+ void cancel() {+ if (!_uploading) return;+ _cancel?.cancel('given up');+ _run++;+ _uploading = false;+ _going = null;+ _progress.clear();+ onChanged();+ }++ /// Gives the upload up when one of its files is no longer among [files]:+ /// taken off while it went up.+ void keepTo(List<SwarmOutgoingFile> files) {+ final going = _going;+ if (_uploading && going != null && !files.toSet().containsAll(going)) {+ cancel();+ }+ }++ /// Its composer closed: an upload on its way stops, and tells no one.+ void close() {+ _cancel?.cancel('closed');+ _run++;+ }+}diff --git a/mobile/test/render/swarm_thread_render_test.dart b/mobile/test/render/swarm_thread_render_test.dartindex d8ef01e10..82db44017 100644--- a/mobile/test/render/swarm_thread_render_test.dart+++ b/mobile/test/render/swarm_thread_render_test.dart@@ -1,3 +1,4 @@+import 'dart:async';import 'dart:io';import 'dart:ui' as ui;@@ -10,16 +11,18 @@ import 'package:harness_mobile/settings/experimental_features.dart';import 'package:harness_mobile/shared/theme/app_theme.dart' as grid;import 'package:harness_mobile/shared/theme/color_palette.dart';import 'package:harness_mobile/swarm/swarm_conversation_page.dart';+import 'package:harness_mobile/swarm/swarm_file_picker.dart';import 'package:harness_mobile/swarm/swarm_session.dart';+import 'package:image_picker/image_picker.dart' show XFile;import '../swarm/swarm_fixture.dart';import '../voice_fakes.dart';/// Renders a phone Swarm chat's threads as Tony's mockup of 10-29 draws them —/// the chat with a root's "N replies" line, a reply also sent and Reply in-/// thread, then the thread itself — in light and dark, at iPhone 14 size in-/// the system fonts, to PNGs a reviewer can look at without a phone.-/// Scripted data only.+/// thread, then the thread itself, and its reply box with files attached and+/// going up — in light and dark, at iPhone 14 size in the system fonts, to+/// PNGs a reviewer can look at without a phone. Scripted data only.////// Skipped unless PHONE_RENDER_DIR names a directory to write into:///@@ -61,10 +64,48 @@ Future<void> _loadFonts() async {}}+/// A made-up photo: sky, ground and a sun.+Future<Uint8List> _photo(Color sky, Color ground) async {+ final recorder = ui.PictureRecorder();+ final canvas = Canvas(recorder);+ canvas.drawRect(const Rect.fromLTWH(0, 0, 120, 80), Paint()..color = sky);+ canvas.drawRect(const Rect.fromLTWH(0, 50, 120, 30), Paint()..color = ground);+ canvas.drawCircle(const Offset(90, 22), 10, Paint()..color = Colors.amber);+ final image = await recorder.endRecording().toImage(120, 80);+ final png = await image.toByteData(format: ui.ImageByteFormat.png);+ return png!.buffer.asUint8List();+}+void main() {final skip = _outDir == null;const messages = '/api/super-tabs/$tab/conversations/g1/messages';+ final original = (+ photos: pickSwarmPhotos,+ documents: pickSwarmFiles,+ copies: swarmPickerCopyFolders,+ );+ setUp(() => swarmPickerCopyFolders = () async => const []);+ tearDown(() {+ swarmPickerCopyFolders = original.copies;+ pickSwarmPhotos = original.photos;+ pickSwarmFiles = original.documents;+ });++ /// An upload 60% of the way up, that never ends.+ Future<Map<String, dynamic>> stuck({+ required String tabId,+ required String conversationId,+ required String name,+ required String mime,+ required Uint8List bytes,+ required dynamic cancel,+ required void Function(double sent) onProgress,+ }) {+ onProgress(0.6);+ return Completer<Map<String, dynamic>>().future;+ }+FakeSwarm backend() {final now = DateTime.now().toUtc();Map<String, dynamic> message(@@ -211,6 +252,23 @@ void main() {Future<GlobalKey> chat(WidgetTester tester) async {await tester.runAsync(_loadFonts);+ final photos = (await tester.runAsync(+ () async => [+ await _photo(const Color(0xFF7FB2E5), const Color(0xFF5E8C4A)),+ await _photo(const Color(0xFFE9A86B), const Color(0xFF3F5B73)),+ ],+ ))!;+ pickSwarmPhotos = (limit) async => [+ XFile.fromData(photos[0], path: '/copies/IMG_2041.jpg'),+ XFile.fromData(photos[1], path: '/copies/IMG_2050.jpg'),+ ];+ pickSwarmFiles = () async => [+ XFile.fromData(+ Uint8List(640 * 1024),+ path: '/copies/report.pdf',+ length: 640 * 1024,+ ),+ ];// A palette carries its brightness: a light one is how the app goes light.final palette = HarnessPalette.values.firstWhere((p) => p.brightness == brightness,@@ -241,6 +299,7 @@ void main() {accountId: () => account.value,account: account,request: backend().request,+ upload: stuck,notices: SilentSystemNotices(),features: features,)..foreground = false;@@ -285,5 +344,46 @@ void main() {await tester.pumpAndSettle();await shoot(tester, key, 'swarm-thread-open-$look');});++ Future<void> pick(WidgetTester tester, String row) async {+ await tester.tap(find.byKey(const ValueKey('swarm-thread-attach')));+ await tester.pumpAndSettle();+ await tester.tap(find.byKey(ValueKey(row)));+ await tester.pumpAndSettle();+ // Thumbnails decode on the real clock, not the test's.+ await tester.runAsync(+ () => Future<void>.delayed(const Duration(milliseconds: 300)),+ );+ await tester.pumpAndSettle();+ }++ testWidgets('the thread, files in the reply box ($look)', skip: skip, (+ tester,+ ) async {+ final key = await chat(tester);+ await tester.tap(find.byKey(const ValueKey('swarm-thread-line-m1')));+ await tester.pumpAndSettle();+ await pick(tester, 'swarm-attach-library');+ await pick(tester, 'swarm-attach-files');+ await tester.enterText(+ find.byKey(const ValueKey('swarm-thread-field')),+ 'Here are the shots',+ );+ await tester.pumpAndSettle();+ await shoot(tester, key, 'swarm-thread-files-$look');+ });++ testWidgets('the thread, files going up ($look)', skip: skip, (+ tester,+ ) async {+ final key = await chat(tester);+ await tester.tap(find.byKey(const ValueKey('swarm-thread-line-m1')));+ await tester.pumpAndSettle();+ await pick(tester, 'swarm-attach-library');+ await tester.tap(find.byKey(const ValueKey('swarm-thread-send')));+ await tester.pump();+ await tester.pump();+ await shoot(tester, key, 'swarm-thread-uploading-$look');+ });}}diff --git a/mobile/test/swarm/swarm_chat_test.dart b/mobile/test/swarm/swarm_chat_test.dartindex 521e1c935..a6a9e9c5c 100644--- a/mobile/test/swarm/swarm_chat_test.dart+++ b/mobile/test/swarm/swarm_chat_test.dart@@ -1,5 +1,6 @@import 'dart:async';+import 'package:dio/dio.dart' show CancelToken;import 'package:flutter/material.dart';import 'package:flutter/services.dart';import 'package:flutter_test/flutter_test.dart';@@ -1748,6 +1749,201 @@ void main() {expect(key('swarm-thread-retry'), findsNothing);expect(inThread(onlyThere), findsOneWidget);});++ group('files', () {+ final picker = (+ documents: pickSwarmFiles,+ copies: swarmPickerCopyFolders,+ );+ // No plugin answers in a widget test: the pickers' copies here are in+ // memory.+ setUp(() => swarmPickerCopyFolders = () async => const []);+ tearDown(() {+ swarmPickerCopyFolders = picker.copies;+ pickSwarmFiles = picker.documents;+ });++ /// Uploads the test answers: each held until [answer] completes it.+ final uploads =+ <+ ({+ String name,+ String conversationId,+ CancelToken cancel,+ void Function(double) progress,+ Completer<Map<String, dynamic>> answer,+ })+ >[];+ setUp(uploads.clear);+ Future<Map<String, dynamic>> held({+ required String tabId,+ required String conversationId,+ required String name,+ required String mime,+ required Uint8List bytes,+ required CancelToken cancel,+ required void Function(double sent) onProgress,+ }) {+ final answer = Completer<Map<String, dynamic>>();+ uploads.add((+ name: name,+ conversationId: conversationId,+ cancel: cancel,+ progress: onProgress,+ answer: answer,+ ));+ return answer.future;+ }++ Map<String, dynamic> attachment(String id) => {+ 'attachment': {+ 'id': id,+ 'name': 'x',+ 'mime': 'text/plain',+ 'size': 1,+ 'url': 'https://cdn.harness.autonomous.ai/$id/x',+ },+ };++ /// Opens m1's thread and attaches crash.log with the box's +.+ Future<void> attachInThread(WidgetTester tester) async {+ pickSwarmFiles = () async => [+ XFile.fromData(+ Uint8List(12),+ path: '/picker-copies/crash.log',+ length: 12,+ ),+ ];+ await tester.tap(key('swarm-thread-line-m1'));+ await tester.pumpAndSettle();+ await tester.tap(key('swarm-thread-attach'));+ await tester.pumpAndSettle();+ expect(find.text('Photo library'), findsOneWidget, reason: 'the sheet');+ await tester.tap(key('swarm-attach-files'));+ await tester.pumpAndSettle();+ }++ Finder inBox(Finder finder) =>+ find.descendant(of: key('swarm-thread-box'), matching: finder);++ testWidgets('the box\'s + attaches as the chat\'s does: the files show '+ 'over the field, go up for the chat, then go with the reply', (+ tester,+ ) async {+ final fake = threaded();+ await pump(tester, fake, upload: held);+ await attachInThread(tester);++ expect(inBox(key('swarm-outgoing-crash.log')), findsOneWidget);+ expect(inBox(find.text('1 KB')), findsOneWidget);+ await tester.enterText(key('swarm-thread-field'), 'The log');+ await tester.pump();+ await tester.tap(key('swarm-thread-send'));+ await tester.pump();++ expect(uploads.single.name, 'crash.log');+ expect(uploads.single.conversationId, 'g1');+ expect(+ tester.widget<SwarmAttachButton>(key('swarm-thread-attach')).onTap,+ isNull,+ reason: 'off while the files go up',+ );+ uploads.single.progress(0.6);+ await tester.pump();+ expect(inBox(find.text('60%')), findsOneWidget);++ uploads.single.answer.complete(attachment('f1'));+ await tester.pumpAndSettle();++ expect(fake.commands.single, {+ 'type': 'message.send',+ 'conversationId': 'g1',+ 'text': 'The log',+ 'replyTo': 'm1',+ 'recipientIds': <String>[],+ 'attachmentIds': ['f1'],+ });+ expect(key('swarm-outgoing-crash.log'), findsNothing);+ await back(tester);+ expect(+ key('swarm-outgoing-crash.log'),+ findsNothing,+ reason: 'never in the chat\'s own box',+ );+ });++ testWidgets('a reply of files alone shows them while it is sent', (+ tester,+ ) async {+ final fake = threaded();+ fake.routes[commands] = (_) => Completer<Object?>().future;+ await pump(tester, fake, upload: held);+ await attachInThread(tester);++ await tester.tap(key('swarm-thread-send'));+ await tester.pump();+ uploads.single.answer.complete(attachment('f1'));+ await tester.pump();+ await tester.pump();++ expect(+ find.descendant(+ of: key('swarm-thread-pending'),+ matching: find.textContaining('crash.log'),+ ),+ findsOneWidget,+ );+ });++ testWidgets('a refused upload says what the chat says, and keeps the '+ 'draft and the files', (tester) async {+ final fake = threaded();+ await pump(tester, fake, upload: held);+ await attachInThread(tester);+ await tester.enterText(key('swarm-thread-field'), 'The log');+ await tester.pump();++ await tester.tap(key('swarm-thread-send'));+ await tester.pump();+ uploads.single.answer.completeError(StateError('socket closed'));+ await tester.pumpAndSettle();++ expect(+ tester.widget<Text>(key('swarm-thread-post-error')).data,+ 'Could not upload the file. Try again.',+ );+ expect(inBox(key('swarm-outgoing-crash.log')), findsOneWidget);+ expect(inBox(find.text('The log')), findsOneWidget);+ expect(fake.commands, isEmpty);+ });++ testWidgets('leaving the thread while its files go up sends nothing; '+ 'opened again, the draft and the files are there', (tester) async {+ final fake = threaded();+ await pump(tester, fake, upload: held);+ await attachInThread(tester);+ await tester.enterText(key('swarm-thread-field'), 'The log');+ await tester.pump();+ await tester.tap(key('swarm-thread-send'));+ await tester.pump();++ await back(tester);+ expect(uploads.single.cancel.isCancelled, isTrue);+ uploads.single.answer.complete(attachment('late'));+ await tester.pumpAndSettle();+ expect(fake.commands, isEmpty);++ await tester.tap(key('swarm-thread-line-m1'));+ await tester.pumpAndSettle();+ expect(inBox(key('swarm-outgoing-crash.log')), findsOneWidget);+ expect(inBox(find.text('The log')), findsOneWidget);+ expect(key('swarm-thread-post-error'), findsNothing);+ expect(+ tester.widget<SwarmAttachButton>(key('swarm-thread-attach')).onTap,+ isNotNull,+ );+ });+ });});group('drawn again', () {diff --git a/mobile/test/swarm/swarm_conversation_files_test.dart b/mobile/test/swarm/swarm_conversation_files_test.dartindex 29e3e902a..6ca9bb7f7 100644--- a/mobile/test/swarm/swarm_conversation_files_test.dart+++ b/mobile/test/swarm/swarm_conversation_files_test.dart@@ -1,7 +1,6 @@import 'dart:async';import 'dart:typed_data';-import 'package:dio/dio.dart';import 'package:flutter_test/flutter_test.dart';import 'package:harness_mobile/swarm/swarm_conversation.dart';import 'package:harness_mobile/swarm/swarm_outgoing_files.dart';@@ -10,68 +9,6 @@ import 'package:harness_mobile/swarm/swarm_request.dart';import 'swarm_fixture.dart';-/// One upload the chat asked for: what it sent, and how to answer it.-class _Upload {- _Upload(this.name, this.mime, this.bytes, this.conversationId, this.tabId,- this.cancel, this.onProgress);- final String name, mime, conversationId, tabId;- final Uint8List bytes;- final CancelToken cancel;- final void Function(double sent) onProgress;- final answer = Completer<Map<String, dynamic>>();-}--/// The backend's attachment upload, answered by the test.-class _Uploads {- final asked = <_Upload>[];-- /// Answers each upload at once with an id made from its name, unless off.- bool answersAtOnce = true;-- /// Refuses uploads of these names.- final refuse = <String, SuperTabFailure>{};-- List<String> get names => [for (final u in asked) u.name];-- SwarmUpload get upload =>- ({- required tabId,- required conversationId,- required name,- required mime,- required bytes,- required cancel,- required onProgress,- }) {- final u = _Upload(- name,- mime,- bytes,- conversationId,- tabId,- cancel,- onProgress,- );- asked.add(u);- if (refuse[name] case final failure?) {- u.answer.completeError(failure);- } else if (answersAtOnce) {- u.answer.complete(attachment('id-$name'));- }- return u.answer.future;- };-- static Map<String, dynamic> attachment(String id) => {- 'attachment': {- 'id': id,- 'name': 'x',- 'mime': 'image/jpeg',- 'size': 1,- 'url': 'https://cdn.harness.autonomous.ai/x',- },- };-}-void main() {const messages = '/api/super-tabs/$tab/conversations/g1/messages';const commands = '/api/super-tabs/$tab/commands';@@ -79,12 +16,12 @@ void main() {SwarmOutgoingFile file(String name, [int size = 3]) =>SwarmOutgoingFile(name, Uint8List.fromList(List.filled(size, 7)));- ({FakeSwarm swarm, SwarmReader reader, _Uploads uploads}) rig({+ ({FakeSwarm swarm, SwarmReader reader, FakeUploads uploads}) rig({String role = 'owner',bool uploads = true,}) {final swarm = FakeSwarm();- final fake = _Uploads();+ final fake = FakeUploads();swarm.routes['/api/super-tabs/$tab/parts'] = (_) => {...firstLoad(groups: [@@ -265,11 +202,11 @@ void main() {expect(c.uploadProgress(a), 0.5);expect(c.uploadProgress(b), isNull);- r.uploads.asked.single.answer.complete(_Uploads.attachment('id-a'));+ r.uploads.asked.single.answer.complete(FakeUploads.attachment('id-a'));await settle();expect(c.isUploading(a), isFalse, reason: 'it is up');expect(c.isUploading(b), isTrue);- r.uploads.asked.last.answer.complete(_Uploads.attachment('id-b'));+ r.uploads.asked.last.answer.complete(FakeUploads.attachment('id-b'));expect(await sending, isTrue);expect(c.uploading, isFalse);@@ -369,27 +306,32 @@ void main() {expect(r.swarm.commands.last['attachmentIds'], ['id-a.jpg']);});- test('a post whose answer was lost is sent again with the same files',- () async {- final r = rig();- var lost = true;- r.swarm.routes[commands] = (_) => lost- ? throw const SuperTabFailure('MACHINE_UNAVAILABLE', 'No computer.')- : {'result': <String, dynamic>{}};- final c = await open(r.reader);- c.files.add([file('a.jpg')]);-- expect(await c.send(), isFalse);- expect(c.unconfirmed, isTrue);- lost = false;- expect(await c.send(), isTrue);-- expect(r.uploads.names, ['a.jpg']);- expect([for (final s in r.swarm.commands) s['attachmentIds']], [- ['id-a.jpg'],- ['id-a.jpg'],- ]);- });+ test(+ 'a post whose answer was lost is sent again with the same files',+ () async {+ final r = rig();+ var lost = true;+ r.swarm.routes[commands] = (_) => lost+ ? throw const SuperTabFailure('MACHINE_UNAVAILABLE', 'No computer.')+ : {'result': <String, dynamic>{}};+ final c = await open(r.reader);+ c.files.add([file('a.jpg')]);++ expect(await c.send(), isFalse);+ expect(c.unconfirmed, isTrue);+ lost = false;+ expect(await c.send(), isTrue);++ expect(r.uploads.names, ['a.jpg']);+ expect(+ [for (final s in r.swarm.commands) s['attachmentIds']],+ [+ ['id-a.jpg'],+ ['id-a.jpg'],+ ],+ );+ },+ );test('files added while a post goes stay for the next one', () async {final r = rig();diff --git a/mobile/test/swarm/swarm_fixture.dart b/mobile/test/swarm/swarm_fixture.dartindex 9c6e3f046..166618bfa 100644--- a/mobile/test/swarm/swarm_fixture.dart+++ b/mobile/test/swarm/swarm_fixture.dart@@ -1,6 +1,8 @@import 'dart:async';import 'dart:io';+import 'dart:typed_data';+import 'package:dio/dio.dart' show CancelToken;import 'package:flutter_test/flutter_test.dart';import 'package:harness_mobile/swarm/swarm_home_cache.dart';@@ -268,6 +270,76 @@ class FakeSwarm {};}+/// One upload a chat or a thread asked for: what it sent, and how to answer+/// it.+class FakeUpload {+ FakeUpload(+ this.name,+ this.mime,+ this.bytes,+ this.conversationId,+ this.tabId,+ this.cancel,+ this.onProgress,+ );+ final String name, mime, conversationId, tabId;+ final Uint8List bytes;+ final CancelToken cancel;+ final void Function(double sent) onProgress;+ final answer = Completer<Map<String, dynamic>>();+}++/// The backend's attachment upload, answered by the test.+class FakeUploads {+ final asked = <FakeUpload>[];++ /// Answers each upload at once with an id made from its name, unless off.+ bool answersAtOnce = true;++ /// Refuses uploads of these names.+ final refuse = <String, SuperTabFailure>{};++ List<String> get names => [for (final u in asked) u.name];++ SwarmUpload get upload =>+ ({+ required tabId,+ required conversationId,+ required name,+ required mime,+ required bytes,+ required cancel,+ required onProgress,+ }) {+ final u = FakeUpload(+ name,+ mime,+ bytes,+ conversationId,+ tabId,+ cancel,+ onProgress,+ );+ asked.add(u);+ if (refuse[name] case final failure?) {+ u.answer.completeError(failure);+ } else if (answersAtOnce) {+ u.answer.complete(attachment('id-$name'));+ }+ return u.answer.future;+ };++ static Map<String, dynamic> attachment(String id) => {+ 'attachment': {+ 'id': id,+ 'name': 'x',+ 'mime': 'image/jpeg',+ 'size': 1,+ 'url': 'https://cdn.harness.autonomous.ai/x',+ },+ };+}+/// Lets queued microtasks and zero timers run.Future<void> settle() async {for (var i = 0; i < 20; i++) {diff --git a/mobile/test/swarm/swarm_thread_test.dart b/mobile/test/swarm/swarm_thread_test.dartindex 4aa3a4f7c..f08047762 100644--- a/mobile/test/swarm/swarm_thread_test.dart+++ b/mobile/test/swarm/swarm_thread_test.dart@@ -1,8 +1,10 @@import 'dart:async';+import 'dart:typed_data';import 'package:flutter_test/flutter_test.dart';import 'package:harness_mobile/swarm/model.dart';import 'package:harness_mobile/swarm/swarm_conversation.dart';+import 'package:harness_mobile/swarm/swarm_outgoing_files.dart';import 'package:harness_mobile/swarm/swarm_reader.dart';import 'package:harness_mobile/swarm/swarm_request.dart';import 'package:harness_mobile/swarm/swarm_thread.dart';@@ -47,12 +49,14 @@ void main() {/// A Swarm with conversation g1 at [sequence] while [listed] says so, its/// chat read with [chatRows]: root m1 and its first reply unless told.+ /// Files go up to [uploads].Future<({FakeSwarm swarm, SwarmReader reader, SwarmConversation chat})> rig({int sequence = 2,String role = 'owner',int? number,List<Map<String, dynamic>>? chatRows,bool Function()? listed,+ FakeUploads? uploads,}) async {final swarm = FakeSwarm();swarm.routes['/api/super-tabs/$tab/parts'] = (_) => {@@ -76,11 +80,14 @@ void main() {'next': null,'threads': [line('m1', 1, 2)],};- final reader = SwarmReader(- tabId: tab,- accountId: me,- request: swarm.request,- );+ final reader = uploads == null+ ? SwarmReader(tabId: tab, accountId: me, request: swarm.request)+ : SwarmReader(+ tabId: tab,+ accountId: me,+ request: swarm.request,+ upload: uploads.upload,+ );addTearDown(reader.dispose);await reader.refresh();final chat = SwarmConversation(reader, 'g1');@@ -459,6 +466,334 @@ void main() {expect(open(chat.chat).alsoSendLabel, 'Also send to the chat');});+ group('files', () {+ SwarmOutgoingFile file(String name, [int size = 3]) =>+ SwarmOutgoingFile(name, Uint8List.fromList(List.filled(size, 7)));+ List<String> names(Iterable<SwarmOutgoingFile> files) => [+ for (final f in files) f.name,+ ];++ test('attached files go up for the chat, then the reply names them, to '+ 'the chat too with Also send', () async {+ final uploads = FakeUploads();+ final r = await rig(uploads: uploads);+ readsBack(r.swarm);+ final t = open(r.chat);+ t.draft = 'Here are the shots';+ t.alsoSend = true;+ t.files.add([file('a.jpg'), file('b.pdf', 5)]);+ expect(t.canSend, isTrue);++ expect(await t.send(), isTrue);++ expect(uploads.names, ['a.jpg', 'b.pdf']);+ for (final u in uploads.asked) {+ expect((u.tabId, u.conversationId), (tab, 'g1'));+ }+ expect(uploads.asked.last.mime, 'application/pdf');+ expect(r.swarm.commands, [+ {+ 'type': 'message.send',+ 'conversationId': 'g1',+ 'text': 'Here are the shots',+ 'replyTo': 'm1',+ 'recipientIds': <String>[],+ 'alsoSend': true,+ 'attachmentIds': ['id-a.jpg', 'id-b.pdf'],+ },+ ]);+ expect(t.files.files, isEmpty);+ expect(t.pending, isNull, reason: 'read back');+ expect(t.pendingFiles, isEmpty);+ expect(r.chat.files.files, isEmpty, reason: 'the chat\'s own box');+ });++ test('a reply of files alone goes with no text', () async {+ final uploads = FakeUploads();+ final r = await rig(uploads: uploads);+ readsBack(r.swarm);+ final t = open(r.chat);+ expect(t.canSend, isFalse);+ t.files.add([file('crash.log')]);+ expect(t.canSend, isTrue);++ expect(await t.send(), isTrue);++ expect(r.swarm.commands.single['text'], '');+ expect(r.swarm.commands.single['attachmentIds'], ['id-crash.log']);+ });++ test('while the files go up the box holds them and cannot send; on their '+ 'way the reply shows them', () async {+ final uploads = FakeUploads()..answersAtOnce = false;+ final r = await rig(uploads: uploads);+ final answer = Completer<Object?>();+ r.swarm.routes[commands] = (_) => answer.future;+ final t = open(r.chat);+ final a = file('a.jpg');+ t.draft = 'look';+ t.files.add([a]);++ final sending = t.send();+ await settle();+ expect(t.uploading, isTrue);+ expect(t.isUploading(a), isTrue);+ expect(t.canSend, isFalse);+ expect(await t.send(), isFalse, reason: 'one send at a time');+ expect(t.draft, 'look');+ expect(names(t.files.files), ['a.jpg']);+ uploads.asked.single.onProgress(0.5);+ expect(t.uploadProgress(a), 0.5);++ uploads.asked.single.answer.complete(FakeUploads.attachment('f1'));+ await settle();+ expect(t.uploading, isFalse);+ expect(t.pending, 'look');+ expect(names(t.pendingFiles), ['a.jpg']);+ expect(t.files.files, isEmpty);++ answer.complete({'ok': true});+ await sending;+ });++ test('files are the thread\'s own: apart from the chat\'s and other '+ 'threads\', and kept with the thread while the chat is open', () async {+ final r = await rig();+ final first = SwarmThread(r.chat, 'm1');+ first.files.add([file('a.jpg')]);+ first.dispose();++ expect(r.chat.files.files, isEmpty);+ expect(open(r.chat, 'm9').files.files, isEmpty);+ expect(names(open(r.chat).files.files), ['a.jpg']);+ });++ test('an upload refused shows what the chat shows, keeps the draft and '+ 'the files, and sends nothing', () async {+ const refused = SuperTabFailure(+ 'ATTACHMENT_TOO_LARGE',+ 'That file is over 10 MB.',+ statusCode: 413,+ );+ final uploads = FakeUploads()..refuse['b.jpg'] = refused;+ final r = await rig(uploads: uploads);+ r.chat.files.add([file('a.jpg'), file('b.jpg')]);+ expect(await r.chat.send(), isFalse);+ final t = open(r.chat);+ t.draft = 'look';+ t.files.add([file('a.jpg'), file('b.jpg')]);++ expect(await t.send(), isFalse);++ expect(t.error, r.chat.error);+ expect(t.error, 'That file is over 10 MB.');+ expect(t.draft, 'look');+ expect(names(t.files.files), ['a.jpg', 'b.jpg']);+ expect(r.swarm.commands, isEmpty);+ expect(t.uploading, isFalse);+ expect(t.pending, isNull);+ expect(t.canSend, isTrue, reason: 'not stuck: Send tries again');+ });++ test(+ 'an upload that fails for any other reason says what the chat says',+ () async {+ final uploads = FakeUploads()..answersAtOnce = false;+ final r = await rig(uploads: uploads);+ r.chat.files.add([file('a.jpg')]);+ final chatSending = r.chat.send();+ await settle();+ uploads.asked.last.answer.completeError(StateError('socket closed'));+ expect(await chatSending, isFalse);+ final t = open(r.chat);+ t.files.add([file('a.jpg')]);++ final sending = t.send();+ await settle();+ uploads.asked.last.answer.completeError(StateError('socket closed'));++ expect(await sending, isFalse);+ expect(t.error, r.chat.error);+ expect(t.error, 'Could not upload the file. Try again.');+ },+ );++ test(+ 'Send again after a failed upload sends only what did not go up',+ () async {+ final uploads = FakeUploads()+ ..refuse['b.jpg'] = const SuperTabFailure('UPLOAD_FAILED', 'No.');+ final r = await rig(uploads: uploads);+ readsBack(r.swarm);+ final t = open(r.chat);+ t.files.add([file('a.jpg'), file('b.jpg')]);+ expect(await t.send(), isFalse);+ uploads.refuse.clear();++ expect(await t.send(), isTrue);++ expect(uploads.names, ['a.jpg', 'b.jpg', 'b.jpg']);+ expect(r.swarm.commands.single['attachmentIds'], [+ 'id-a.jpg',+ 'id-b.jpg',+ ]);+ },+ );++ test('a reply refused for its files puts them back and sends them up '+ 'again; one refused for anything else keeps them uploaded', () async {+ final uploads = FakeUploads();+ final r = await rig(uploads: uploads);+ var refusal = const SuperTabFailure(+ 'ATTACHMENT_UNAVAILABLE',+ 'That file is no longer there. Attach it again.',+ statusCode: 400,+ );+ r.swarm.routes[commands] = (_) => throw refusal;+ final t = open(r.chat);+ t.draft = 'look';+ t.files.add([file('a.jpg')]);++ expect(await t.send(), isFalse);+ expect(t.draft, 'look');+ expect(names(t.files.files), ['a.jpg']);+ expect(t.pending, isNull);+ expect(t.error, 'That file is no longer there. Attach it again.');++ refusal = const SuperTabFailure(+ 'INVALID_COMMAND',+ 'That reply could not be sent.',+ statusCode: 400,+ );+ expect(await t.send(), isFalse);+ expect(uploads.names, ['a.jpg', 'a.jpg'], reason: 'up again');+ expect(names(t.files.files), ['a.jpg']);++ readsBack(r.swarm);+ expect(await t.send(), isTrue);+ expect(uploads.names, ['a.jpg', 'a.jpg'], reason: 'kept uploaded');+ });++ test('taking a file off while it goes up gives the upload up and sends '+ 'nothing', () async {+ final uploads = FakeUploads()..answersAtOnce = false;+ final r = await rig(uploads: uploads);+ final t = open(r.chat);+ final a = file('a.jpg');+ t.draft = 'look';+ t.files.add([a, file('b.jpg')]);+ final sending = t.send();+ await settle();++ t.files.remove(a);++ expect(t.uploading, isFalse);+ expect(uploads.asked.single.cancel.isCancelled, isTrue);+ expect(await sending, isFalse);+ expect(r.swarm.commands, isEmpty);+ expect(t.draft, 'look');+ expect(names(t.files.files), ['b.jpg']);+ expect(t.canSend, isTrue);+ });++ test('leaving the thread while its files go up changes nothing: the '+ 'upload stops, nothing is sent, and the draft and the files wait for '+ 'it', () async {+ final uploads = FakeUploads()..answersAtOnce = false;+ final r = await rig(uploads: uploads);+ readsBack(r.swarm);+ final t = SwarmThread(r.chat, 'm1');+ t.draft = 'look';+ t.files.add([file('a.jpg')]);+ final sending = t.send();+ await settle();++ t.dispose();+ expect(uploads.asked.single.cancel.isCancelled, isTrue);+ uploads.asked.single.answer.complete(FakeUploads.attachment('late'));++ expect(await sending, isFalse);+ await settle();+ expect(r.swarm.commands, isEmpty);+ expect(r.chat.error, isNull);+ final again = open(r.chat);+ expect(again.draft, 'look');+ expect(names(again.files.files), ['a.jpg']);+ expect(again.uploading, isFalse);+ expect(again.error, isNull);+ expect(again.canSend, isTrue);+ });++ test('access taken away while the files go up: the upload stops, the '+ 'files and the draft go, and the late answer sends nothing', () async {+ final uploads = FakeUploads()..answersAtOnce = false;+ final r = await rig(uploads: uploads);+ final t = open(r.chat);+ t.draft = 'Secret plan';+ t.files.add([file('plan.pdf')]);+ final sending = t.send();+ await settle();++ r.reader.revoke();++ expect(t.state, SwarmChatState.lost);+ expect(t.uploading, isFalse);+ expect(t.files.files, isEmpty);+ expect(t.draft, isEmpty);+ expect(uploads.asked.single.cancel.isCancelled, isTrue);+ uploads.asked.single.answer.complete(FakeUploads.attachment('late'));+ expect(await sending, isFalse);+ expect(r.swarm.commands, isEmpty);+ expect(t.error, isNull);+ expect(r.chat.threadFiles('m1').files, isEmpty);+ });++ for (final code in ['FORBIDDEN', 'NOT_FOUND']) {+ test('an upload refused with $code drops the thread, as a refused reply '+ 'does', () async {+ final uploads = FakeUploads()+ ..refuse['plan.pdf'] = SuperTabFailure(+ code,+ 'You cannot read this chat.',+ statusCode: code == 'FORBIDDEN' ? 403 : 404,+ );+ final r = await rig(uploads: uploads);+ final t = open(r.chat);+ t.draft = 'Secret plan';+ t.files.add([file('plan.pdf')]);+ final before = r.swarm.paths.length;++ expect(await t.send(), isFalse);++ expect(t.state, SwarmChatState.lost);+ expect(t.files.files, isEmpty);+ expect(t.draft, isEmpty);+ expect(t.error, isNull, reason: 'no message from a chat not open');+ expect(r.swarm.commands, isEmpty);+ await settle();+ expect(+ r.swarm.paths.skip(before).where((p) => p.startsWith(messages)),+ isNotEmpty,+ reason: 'the chat reads again to learn it',+ );+ });+ }++ test('the chat losing access empties every thread\'s files, those a '+ 'thread screen holds too', () async {+ final r = await rig();+ final held = r.chat.threadFiles('m1')..add([file('a.jpg')]);+ final other = r.chat.threadFiles('m9')..add([file('b.jpg')]);++ r.reader.revoke();+ await settle();++ expect(held.files, isEmpty);+ expect(other.files, isEmpty);+ expect(r.chat.threadFiles('m1').files, isEmpty);+ });+ });+group('privacy', () {test('access taken away while the thread is open drops its rows, its ''draft and an answer on its way', () async {