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
Swarm chat: drop a folder to paste its path (#1776)
0xken156e54438bd3c2
diff --git a/desktop/lib/super_tabs/folder_read.dart b/desktop/lib/super_tabs/folder_read.dartnew file mode 100644index 000000000..e23b89f19--- /dev/null+++ b/desktop/lib/super_tabs/folder_read.dart@@ -0,0 +1,15 @@+import 'dart:typed_data';++export 'folder_read_io.dart' if (dart.library.ffi) 'folder_read_native.dart';++/// A dropped folder, held open while its files are read, so that nothing+/// outside it is read even when the folder changes underneath.+abstract interface class FolderFiles {+ /// The file reached by the names in [inside], each under the last, read+ /// without following a link. Null when one of them is a link or not a plain+ /// file or folder by now. At most [maxBytes] + 1 bytes are read, so a file+ /// that grew past what is left is never read in full.+ Uint8List? read(List<String> inside, int maxBytes);++ void close();+}diff --git a/desktop/lib/super_tabs/folder_read_io.dart b/desktop/lib/super_tabs/folder_read_io.dartnew file mode 100644index 000000000..2f63ec65b--- /dev/null+++ b/desktop/lib/super_tabs/folder_read_io.dart@@ -0,0 +1,55 @@+import 'dart:io';+import 'dart:typed_data';++import 'package:path/path.dart' as p;++import 'folder_read.dart' show FolderFiles;++FolderFiles openFolder(String path) => IoFolderFiles(path);++/// [FolderFiles] by path, where the platform gives no way to open a file+/// relative to a folder without following links (Windows). Each name on the+/// way is checked not to be a link, and the file once open must still lie in+/// the folder; a link swapped in between that check and the open is not+/// caught.+final class IoFolderFiles implements FolderFiles {+ /// Refuses a dropped folder that is a link by now, as a link inside it is+ /// left out.+ IoFolderFiles(String path) : _root = _unlinked(path);++ static String _unlinked(String path) {+ if (FileSystemEntity.isLinkSync(path)) {+ throw FileSystemException('The folder is a link now', path);+ }+ return Directory(path).resolveSymbolicLinksSync();+ }++ final String _root;++ @override+ Uint8List? read(List<String> inside, int maxBytes) {+ var path = _root;+ for (final name in inside) {+ path = p.join(path, name);+ if (FileSystemEntity.isLinkSync(path)) return null;+ }+ final file = File(path).openSync();+ try {+ if (!p.isWithin(_root, File(path).resolveSymbolicLinksSync())) {+ return null;+ }+ final bytes = BytesBuilder(copy: false);+ while (bytes.length <= maxBytes) {+ final chunk = file.readSync(maxBytes + 1 - bytes.length);+ if (chunk.isEmpty) break;+ bytes.add(chunk);+ }+ return bytes.takeBytes();+ } finally {+ file.closeSync();+ }+ }++ @override+ void close() {}+}diff --git a/desktop/lib/super_tabs/folder_read_native.dart b/desktop/lib/super_tabs/folder_read_native.dartnew file mode 100644index 000000000..9ca94a3f1--- /dev/null+++ b/desktop/lib/super_tabs/folder_read_native.dart@@ -0,0 +1,145 @@+import 'dart:ffi';+import 'dart:io';+import 'dart:math';+import 'dart:typed_data';++import 'package:ffi/ffi.dart';++import 'folder_read.dart' show FolderFiles;+import 'folder_read_io.dart';++/// [path] held open as a folder: on macOS and Linux by its descriptor, each+/// file reached from it by `openat` with `O_NOFOLLOW` at every step, so a+/// link put in place of a file or folder after the listing is refused by the+/// kernel rather than followed. Windows reads by path ([IoFolderFiles]).+FolderFiles openFolder(String path) => Platform.isMacOS || Platform.isLinux+ ? _PosixFolderFiles(path)+ : IoFolderFiles(path);++final class _PosixFolderFiles implements FolderFiles {+ /// The dropped folder itself is not followed either: one swapped for a+ /// link before the zip opens it is refused (ELOOP), not read through.+ _PosixFolderFiles(String path)+ : _fd = _libc.open(path, _flags.directory | _flags.noFollow, path);++ final int _fd;++ @override+ Uint8List? read(List<String> inside, int maxBytes) {+ final opened = <int>[];+ try {+ var at = _fd;+ for (var i = 0; i < inside.length; i++) {+ final last = i == inside.length - 1;+ final fd = _libc.openAt(+ at,+ inside[i],+ last ? _flags.file : _flags.directory | _flags.noFollow,+ inside.take(i + 1).join('/'),+ );+ if (fd == null) return null;+ opened.add(at = fd);+ }+ return _libc.readAll(at, maxBytes + 1, inside.join('/'));+ } finally {+ opened.reversed.forEach(_libc.close);+ }+ }++ @override+ void close() => _libc.close(_fd);+}++/// `open(2)` flags; `O_NOFOLLOW` and `O_DIRECTORY` differ between Linux's+/// x86 and Arm ABIs.+final _flags = () {+ final arm = Abi.current() == Abi.linuxArm64 || Abi.current() == Abi.linuxArm;+ final (nonBlock, directory, noFollow, cloexec) = Platform.isMacOS+ ? (0x4, 0x100000, 0x100, 0x1000000)+ : arm+ ? (0x800, 0x4000, 0x8000, 0x80000)+ : (0x800, 0x10000, 0x20000, 0x80000);+ return (+ noFollow: noFollow,+ directory: directory | cloexec,+ // Non-blocking, so a pipe put in place of a file cannot hang the read.+ file: noFollow | nonBlock | cloexec,+ );+}();++/// What an `openat`/`read` refusal means here: the name is now a link+/// (ELOOP), a file where a folder was (ENOTDIR), a folder, pipe or socket+/// where a file was (EISDIR, EAGAIN, ENXIO). Anything else is a failure.+final _notPlain = Platform.isMacOS ? {62, 20, 21, 35, 6} : {40, 20, 21, 11, 6};+const _eintr = 4;++final _libc = _Libc();++final class _Libc {+ final _library = DynamicLibrary.process();+ late final _open = _library+ .lookupFunction<+ Int Function(Pointer<Utf8>, Int, VarArgs<(Int,)>),+ int Function(Pointer<Utf8>, int, int)+ >('open');+ late final _openat = _library+ .lookupFunction<+ Int Function(Int, Pointer<Utf8>, Int, VarArgs<(Int,)>),+ int Function(int, Pointer<Utf8>, int, int)+ >('openat');+ late final _read = _library+ .lookupFunction<+ IntPtr Function(Int, Pointer<Uint8>, Size),+ int Function(int, Pointer<Uint8>, int)+ >('read');+ late final _close = _library+ .lookupFunction<Int Function(Int), int Function(int)>('close');+ late final _errno = _library+ .lookupFunction<Pointer<Int> Function(), Pointer<Int> Function()>(+ Platform.isMacOS ? '__error' : '__errno_location',+ );++ int open(String path, int flags, String shown) => using((arena) {+ while (true) {+ final fd = _open(path.toNativeUtf8(allocator: arena), flags, 0);+ if (fd >= 0) return fd;+ final errno = _errno().value;+ if (errno != _eintr) throw _failure('open', shown, errno);+ }+ });++ /// [name] opened in folder [at], or null when it is not what [flags] ask.+ int? openAt(int at, String name, int flags, String shown) => using((arena) {+ while (true) {+ final fd = _openat(at, name.toNativeUtf8(allocator: arena), flags, 0);+ if (fd >= 0) return fd;+ final errno = _errno().value;+ if (_notPlain.contains(errno)) return null;+ if (errno != _eintr) throw _failure('open', shown, errno);+ }+ });++ /// Up to [limit] bytes of [fd], or null when it is not a plain file.+ Uint8List? readAll(int fd, int limit, String shown) => using((arena) {+ const chunk = 1 << 16;+ final buffer = arena<Uint8>(chunk);+ final bytes = BytesBuilder();+ while (bytes.length < limit) {+ final count = _read(fd, buffer, min(chunk, limit - bytes.length));+ if (count == 0) break;+ if (count < 0) {+ final errno = _errno().value;+ if (errno == _eintr) continue;+ if (_notPlain.contains(errno)) return null;+ throw _failure('read', shown, errno);+ }+ bytes.add(buffer.asTypedList(count));+ }+ return bytes.takeBytes();+ });++ void close(int fd) => _close(fd);++ FileSystemException _failure(String call, String path, int errno) =>+ FileSystemException('$call failed', path, OSError('', errno));+}diff --git a/desktop/lib/super_tabs/folder_zip.dart b/desktop/lib/super_tabs/folder_zip.dartnew file mode 100644index 000000000..d43068485--- /dev/null+++ b/desktop/lib/super_tabs/folder_zip.dart@@ -0,0 +1,65 @@+import 'dart:io';+import 'dart:isolate';+import 'dart:typed_data';++import 'package:archive/archive.dart';+import 'package:flutter/foundation.dart' show visibleForTesting;+import 'package:path/path.dart' as p;++import 'folder_read.dart';++/// What a folder dropped into a chat is called: its own name, as its zip and+/// the hint name it.+String folderName(String path) {+ final name = p.basename(path);+ return name.isEmpty ? 'folder' : name;+}++/// [folder] as a zip whose entries sit under the folder's own name, worked on+/// a background isolate. Null when its files weigh more than [limitBytes]+/// together: they are counted before any is read, so a large folder is never+/// held in memory, and counted again as they are read, so one that grew since+/// is not either. Links are left out rather than followed, also when one takes+/// a file's or folder's place while the zip runs, so nothing from outside the+/// folder goes into it.+Future<Uint8List?> zipFolder(String folder, {required int limitBytes}) =>+ Isolate.run(() => zipFolderNow(folder, limitBytes: limitBytes));++/// [zipFolder] on this isolate. [afterScan] runs between listing the folder+/// and reading its files, where a test changes the folder underneath.+@visibleForTesting+Uint8List? zipFolderNow(+ String folder, {+ required int limitBytes,+ void Function()? afterScan,+}) {+ final name = folderName(folder);+ final opened = openFolder(folder);+ try {+ final files = <List<String>>[];+ var total = 0;+ for (final entity in Directory(+ folder,+ ).listSync(recursive: true, followLinks: false)) {+ if (entity is! File) continue;+ total += entity.lengthSync();+ if (total > limitBytes) return null;+ files.add(p.split(p.relative(entity.path, from: folder)));+ }+ afterScan?.call();+ final archive = Archive();+ var left = limitBytes;+ for (final inside in files) {+ final bytes = opened.read(inside, left);+ if (bytes == null) continue;+ left -= bytes.length;+ if (left < 0) return null;+ archive.addFile(+ ArchiveFile.bytes(p.posix.joinAll([name, ...inside]), bytes),+ );+ }+ return ZipEncoder().encodeBytes(archive);+ } finally {+ opened.close();+ }+}diff --git a/desktop/lib/super_tabs/view.dart b/desktop/lib/super_tabs/view.dartindex 5976bbdd8..68715c15c 100644--- a/desktop/lib/super_tabs/view.dart+++ b/desktop/lib/super_tabs/view.dart@@ -1,5 +1,6 @@import 'dart:async';+import 'package:flutter/foundation.dart' show kIsWeb;import 'package:flutter/material.dart';import 'package:flutter/services.dart';@@ -19,6 +20,7 @@ import 'recording_notice.dart';import 'pull_requests.dart';import 'controller.dart';import 'dialog_scope.dart';+import 'folder_zip.dart';import 'model.dart';import 'pm_chat.dart';import 'workgroup_name.dart';@@ -47,6 +49,7 @@ class SuperTabView extends StatefulWidget {required this.onAddAgent,required this.onOpenTerminal,required this.machineName,+ this.isThisComputer,this.terminal,this.onCloseTerminal,this.onOpenGroupTerminals,@@ -60,6 +63,10 @@ class SuperTabView extends StatefulWidget {final ValueChanged<SuperTabConversation> onAddAgent;final ValueChanged<SuperTabAgent> onOpenTerminal;final String Function(String machineId) machineName;++ /// Whether [machineId] is the computer this app runs on, where a folder+ /// dropped on the composer is. Unknown (null) counts as another computer.+ final bool Function(String machineId)? isThisComputer;final Widget? terminal;final VoidCallback? onCloseTerminal, onOpenGroupTerminals;final bool terminalsVisible;@@ -114,6 +121,13 @@ class _SuperTabViewState extends State<SuperTabView>int _mentionIndex = 0;String? _dismissedMention;String? _completionPrefix;++ /// The folders whose paths were dropped into each chat's message: what+ /// Attach as zip offers while those paths are still in it.+ final _droppedFolders = <String, List<String>>{};++ /// The chats whose dropped folders are being zipped.+ final _zipping = <String>{};final _completionKeys = List.generate(6, (_) => GlobalKey());SuperTabController get c => widget.controller;@@ -386,8 +400,134 @@ class _SuperTabViewState extends State<SuperTabView>return;}final id = c.selectedId;+ // From here a zip still running belongs to neither this message, which+ // took its files already, nor the next.+ final dropped = _droppedFolders.remove(id);final result = await c.send(replyTo: _replies[id]?.id);- if (mounted && result != null) setState(() => _replies.remove(id));+ if (!mounted) return;+ setState(() {+ if (result != null) {+ _replies.remove(id);+ } else if (dropped != null) {+ // Not sent: its paths are still offered as zips, in a new record+ // so the zip that was running is still let go.+ final now = _droppedFolders.putIfAbsent(id, () => []);+ now.insertAll(0, [+ for (final path in dropped)+ if (!now.contains(path)) path,+ ]);+ }+ });+ }++ /// [path] as it goes into a message: as it is, or in double quotes when it+ /// holds a space or anything else a shell reads specially, with `"`, `\`,+ /// `$` and `` ` `` escaped, so it reads back as the same path.+ static String _pathText(String path) =>+ path.contains(RegExp(r'''[\s"'$`\\!*?\[\]{}()<>|&;]'''))+ ? '"${path.replaceAllMapped(RegExp(r'["\\$`]'), (m) => '\\${m[0]}')}"'+ : path;++ /// Puts dropped folders' paths into chat [id]'s message at the cursor,+ /// space-separated, as a terminal takes a dropped folder.+ void _pasteFolders(String id, List<String> paths) {+ if (!mounted) return;+ final shown = c.selectedId == id;+ final text = shown ? _composer.text : c.draft(id);+ final selection = shown && _composer.selection.isValid+ ? _composer.selection+ : TextSelection.collapsed(offset: text.length);+ final before = text.substring(0, selection.start);+ final after = text.substring(selection.end);+ final pasted =+ '${before.isEmpty || RegExp(r'\s$').hasMatch(before) ? '' : ' '}'+ '${paths.map(_pathText).join(' ')}'+ '${after.startsWith(RegExp(r'\s')) ? '' : ' '}';+ final next = before + pasted + after;+ c.setDraft(id, next);+ final dropped = _droppedFolders.putIfAbsent(id, () => []);+ setState(() => dropped.addAll(paths.where((p) => !dropped.contains(p))));+ if (!shown) return;+ _composer.value = TextEditingValue(+ text: next,+ selection: TextSelection.collapsed(offset: before.length + pasted.length),+ );+ _focus.requestFocus();+ }++ /// The dropped folders whose paths are still in chat [id]'s message.+ List<String> _foldersInMessage(String id) => [+ for (final path in _droppedFolders[id] ?? const <String>[])+ if (_composer.text.contains(_pathText(path))) path,+ ];++ /// Whether an agent of [group] runs on another computer than this one,+ /// where a path dropped here means nothing.+ bool _agentsElsewhere(SuperTabConversation group) =>+ group.agentIds.any((agentId) {+ final agent = c.snapshot?.agent(agentId);+ return agent != null &&+ !(widget.isThisComputer?.call(agent.machineId) ?? false);+ });++ /// Attaches each of [folders] to chat [id]'s message as a zip in place of+ /// its path. A message sent meanwhile keeps its paths, and the zips are not+ /// attached to the next one.+ Future<void> _attachFoldersAsZip(String id, List<String> folders) async {+ final dropped = _droppedFolders[id];+ if (dropped == null || !_zipping.add(id)) return;+ setState(() {});+ final files = c.attachments(id);+ final zipped = <String, Uint8List>{};+ final oversized = <String>[], failed = <String>[];+ for (final folder in folders) {+ try {+ final bytes = await zipFolder(folder, limitBytes: files.limitBytes);+ bytes == null+ ? oversized.add(folderName(folder))+ : zipped[folder] = bytes;+ } on Exception {+ // Unreadable, or gone since the drop.+ failed.add(folderName(folder));+ }+ }+ if (!mounted) return;+ setState(() => _zipping.remove(id));+ if (!identical(_droppedFolders[id], dropped)) return;+ // Numbered, so two folders of one name are both attached.+ final problem = files.addPasted([+ for (final MapEntry(key: folder, value: bytes) in zipped.entries)+ HarnessAttachment('${folderName(folder)}.zip', bytes),+ ], oversized: oversized);+ // Only a folder whose zip was taken gives up its path.+ final attached = [+ for (final MapEntry(key: folder, value: bytes) in zipped.entries)+ if (files.files.any((file) => identical(file.bytes, bytes))) folder,+ ];+ final shown = c.selectedId == id;+ var text = shown ? _composer.text : c.draft(id);+ for (final folder in attached) {+ final path = _pathText(folder);+ text = text.contains('$path ')+ ? text.replaceFirst('$path ', '')+ : text.contains(' $path')+ ? text.replaceFirst(' $path', '')+ : text.replaceFirst(path, '');+ }+ dropped.removeWhere(attached.contains);+ c.setDraft(id, text);+ if (shown && _composer.text != text) {+ _composer.value = TextEditingValue(+ text: text,+ selection: TextSelection.collapsed(offset: text.length),+ );+ }+ setState(() {});+ reportAttachProblem(+ context,+ problem ??+ (failed.isEmpty ? null : '${failed.join(', ')} could not be zipped.'),+ );}void _composerMoved() {@@ -1748,6 +1888,10 @@ class _SuperTabViewState extends State<SuperTabView>final finding = _finding?.conversationId == c.selectedId ? _finding : null;final back = finding == null && _back?.conversationId == c.selectedId;final choices = _completionChoices;+ final chatId = c.selectedId;+ final pastedFolders = group != null && _agentsElsewhere(group)+ ? _foldersInMessage(chatId)+ : const <String>[];// No conversation read yet: its error is the page itself, not a line over "Loading".final unread = _readsFirst && c.snapshot == null;return Column(@@ -2159,7 +2303,12 @@ class _SuperTabViewState extends State<SuperTabView>)elsePadding(- padding: const EdgeInsets.fromLTRB(24, 12, 24, 16),+ padding: EdgeInsets.fromLTRB(+ 24,+ 12,+ 24,+ pastedFolders.isEmpty ? 16 : 6,+ ),child: Focus(onKeyEvent: (node, event) {final keys = HardwareKeyboard.instance;@@ -2223,6 +2372,11 @@ class _SuperTabViewState extends State<SuperTabView>child: NewHarnessDropZone(attachments: _files,enabled: writable && !busy,+ // A browser has no path to give; there a folder is not+ // taken, as before.+ onFolders: kIsWeb+ ? null+ : (paths) => _pasteFolders(chatId, paths),child: GestureDetector(behavior: HitTestBehavior.translucent,onTap: writable ? _focus.requestFocus : null,@@ -2357,6 +2511,35 @@ class _SuperTabViewState extends State<SuperTabView>),),),+ if (pastedFolders.isNotEmpty && !(group?.archived ?? true))+ Padding(+ key: const ValueKey('super-folder-path-hint'),+ padding: const EdgeInsets.fromLTRB(28, 0, 24, 10),+ child: Row(+ children: [+ Expanded(+ child: Text(+ pastedFolders.length == 1+ ? "This path is on this computer. Agents on other "+ "computers can't open it."+ : "These paths are on this computer. Agents on "+ "other computers can't open them.",+ style: DesktopChrome.metadata(),+ ),+ ),+ TextButton(+ onPressed: _zipping.contains(chatId) || !writable || busy+ ? null+ : () => unawaited(+ _attachFoldersAsZip(chatId, pastedFolders),+ ),+ child: Text(+ _zipping.contains(chatId) ? 'Zipping…' : 'Attach as zip',+ ),+ ),+ ],+ ),+ ),],);}diff --git a/desktop/lib/super_tabs/workspace.dart b/desktop/lib/super_tabs/workspace.dartindex 333d18f5f..f9308b5b1 100644--- a/desktop/lib/super_tabs/workspace.dart+++ b/desktop/lib/super_tabs/workspace.dart@@ -541,6 +541,8 @@ class _SuperTabWorkspaceState extends State<SuperTabWorkspace> {machineName: (machine) =>app.stateOf(machine)?.machine.displayName ??'Offline machine',+ isThisComputer: (machine) =>+ app.stateOf(machine)?.isLocalMachine == true,terminal: showTerminal? terminal: devices || agents.isNotEmptydiff --git a/desktop/lib/widgets/new_harness_attachments.dart b/desktop/lib/widgets/new_harness_attachments.dartindex 828a4d4d0..aabb060dc 100644--- a/desktop/lib/widgets/new_harness_attachments.dart+++ b/desktop/lib/widgets/new_harness_attachments.dart@@ -1,4 +1,5 @@import 'dart:async';+import 'dart:io';import 'package:desktop_drop/desktop_drop.dart';import 'package:file_selector/file_selector.dart';@@ -23,6 +24,28 @@ Future<void> _attach(if (context.mounted) reportAttachProblem(context, attachments.add(read));}+/// Attaches the dropped files and hands [onFolders] the dropped folders'+/// paths, which hold nothing to read.+Future<void> _attachOrPaste(+ BuildContext context,+ HarnessAttachments attachments,+ Iterable<DropItem> items,+ ValueChanged<List<String>> onFolders,+) async {+ final files = <XFile>[], folders = <String>[];+ for (final item in items) {+ // macOS says which items are folders; Linux and Windows give paths alone.+ final folder =+ item is DropItemDirectory ||+ (item.path.isNotEmpty && await FileSystemEntity.isDirectory(item.path));+ folder ? folders.add(item.path) : files.add(item);+ }+ if (folders.isNotEmpty) onFolders(folders);+ if (files.isNotEmpty && context.mounted) {+ await _attach(context, attachments, files);+ }+}+/// Says what an attach left out, when it left anything.void reportAttachProblem(BuildContext context, String? problem) {if (problem == null) return;@@ -103,12 +126,17 @@ class NewHarnessDropZone extends StatefulWidget {required this.attachments,required this.enabled,required this.child,+ this.onFolders,});final HarnessAttachments attachments;final bool enabled;final Widget child;+ /// Told the paths of dropped folders, which are then not attached. Without+ /// it every dropped item is read as a file.+ final ValueChanged<List<String>>? onFolders;+@overrideState<NewHarnessDropZone> createState() => _NewHarnessDropZoneState();}@@ -128,7 +156,17 @@ class _NewHarnessDropZoneState extends State<NewHarnessDropZone> {onDragExited: (_) => _hover(false),onDragDone: (details) {_hover(false);- unawaited(_attach(context, widget.attachments, details.files));+ final onFolders = widget.onFolders;+ unawaited(+ onFolders == null+ ? _attach(context, widget.attachments, details.files)+ : _attachOrPaste(+ context,+ widget.attachments,+ details.files,+ onFolders,+ ),+ );},child: Stack(children: [diff --git a/desktop/test/super_tab_folder_drop_test.dart b/desktop/test/super_tab_folder_drop_test.dartnew file mode 100644index 000000000..094063c35--- /dev/null+++ b/desktop/test/super_tab_folder_drop_test.dart@@ -0,0 +1,506 @@+// Folders dropped on a Swarm chat's composer: their paths go into the message+// at the cursor, dropped files still attach, and when the chat's agents run on+// another computer the box says so and offers the folder as a zip instead.+import 'dart:async';+import 'dart:io';+import 'dart:math';++import 'package:archive/archive.dart';+import 'package:flutter/material.dart';+import 'package:flutter/services.dart';+import 'package:flutter_test/flutter_test.dart';+import 'package:harness/super_tabs/attachment_upload.dart';+import 'package:harness/super_tabs/controller.dart';+import 'package:harness/super_tabs/view.dart';+import 'package:harness/state/harness_attachments.dart';++import 'support/super_tab_fixture.dart';++void main() {+ final composer = find.byKey(const ValueKey('super-message-composer'));+ final hint = find.byKey(const ValueKey('super-folder-path-hint'));+ final surface = find.byKey(const ValueKey('super-message-composer-surface'));++ late Directory root;+ late ConversationServer server;+ setUp(() => root = Directory.systemTemp.createTempSync('swarm-folder-drop'));+ tearDown(() => root.deleteSync(recursive: true));++ /// A real folder under [root], so the drop can tell it from a file.+ Directory folder(String name) =>+ Directory('${root.path}/$name')..createSync();++ /// The chat's one agent runs on [machineId]; this computer is `here`.+ Future<SuperTabController> open(+ WidgetTester tester, {+ String machineId = 'here',+ }) async {+ // One logical pixel per device pixel, so the platform's drop position+ // needs no scaling on any host.+ tester.view.devicePixelRatio = 1;+ addTearDown(tester.view.resetDevicePixelRatio);+ server = ConversationServer();+ (server.state['agents'] as List).add({+ 'id': 'lead-agent',+ 'name': 'Lead',+ 'displayName': 'Atlas',+ 'machineId': machineId,+ 'harnessId': 'native-lead',+ 'contributorId': 'owner',+ 'configuration': {'engine': 'claude'},+ });+ (server.state['conversations'] as List)[0]+ ..['leadId'] = 'lead-agent'+ ..['agentIds'] = ['lead-agent'];+ final controller = server.controller();+ await controller.refresh();+ await tester.binding.setSurfaceSize(const Size(1100, 800));+ addTearDown(() => tester.binding.setSurfaceSize(null));+ await tester.pumpWidget(+ MaterialApp(+ home: Scaffold(+ body: SuperTabView(+ controller: controller,+ onInvite: () {},+ onAddAgent: (_) {},+ onOpenTerminal: (_) {},+ machineName: (_) => 'Test Mac',+ isThisComputer: (machine) => machine == 'here',+ ),+ ),+ ),+ );+ await tester.pumpAndSettle();+ return controller;+ }++ Future<void> close(WidgetTester tester, SuperTabController c) async {+ await tester.pumpWidget(const SizedBox());+ await c.flush();+ c.dispose();+ }++ Future<void> platform(String method, Object arguments) =>+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger+ .handlePlatformMessage(+ 'desktop_drop',+ const StandardMethodCodec().encodeMethodCall(+ MethodCall(method, arguments),+ ),+ (_) {},+ );++ /// Lets files be read and the zip's isolate answer, a frame at a time,+ /// until [done].+ Future<void> settle(WidgetTester tester, bool Function() done) async {+ for (var i = 0; i < 200 && !done(); i++) {+ await tester.runAsync(+ () => Future<void>.delayed(const Duration(milliseconds: 20)),+ );+ await tester.pump();+ }+ await tester.pumpAndSettle();+ }++ /// Drops on the composer: [method] and [arguments] as the platform sends+ /// them, then waits [until] the drop has landed. The drop is delivered on+ /// the test's clock, as the composer's other work is, never inside+ /// `runAsync`: a drop handled there leaves frames on the real clock, and a+ /// later isolate then never lets the test finish.+ Future<void> dropAs(+ WidgetTester tester,+ String method,+ Object arguments, {+ required bool Function() until,+ }) async {+ final inside = tester.getCenter(surface);+ await platform('entered', [inside.dx, inside.dy]);+ await tester.pump();+ await platform(method, arguments);+ await settle(tester, until);+ }++ /// Drops [paths] the way Linux and Windows report them: paths alone, a+ /// folder not told apart from a file.+ Future<void> drop(+ WidgetTester tester,+ List<String> paths, {+ required bool Function() until,+ }) => dropAs(tester, 'performOperation', paths, until: until);++ TextEditingController field(WidgetTester tester) =>+ tester.widget<TextField>(composer).controller!;++ List<String> names(SuperTabController c) => [+ for (final file in c.attachments('lead').files) file.name,+ ];++ testWidgets('a dropped folder puts its path at the cursor', (tester) async {+ final controller = await open(tester);+ final docs = folder('docs');+ await tester.enterText(composer, 'see please');+ field(tester).selection = const TextSelection.collapsed(offset: 4);++ await drop(tester, [+ docs.path,+ ], until: () => field(tester).text.contains(docs.path));++ expect(field(tester).text, 'see ${docs.path} please');+ expect(+ field(tester).selection,+ TextSelection.collapsed(offset: 'see ${docs.path} '.length),+ );+ expect(controller.draft('lead'), 'see ${docs.path} please');+ expect(names(controller), isEmpty);+ await close(tester, controller);+ });++ testWidgets('several folders go in space-separated, quoted when a path has '+ 'a space', (tester) async {+ final controller = await open(tester);+ final plain = folder('plain'), spaced = folder('my notes');+ await tester.enterText(composer, 'Read');++ await drop(tester, [+ plain.path,+ spaced.path,+ ], until: () => field(tester).text.contains(spaced.path));++ expect(field(tester).text, 'Read ${plain.path} "${spaced.path}" ');+ await close(tester, controller);+ });++ testWidgets('a folder macOS marks as one pastes its path too', (+ tester,+ ) async {+ final controller = await open(tester);+ // A folder that is gone by now: macOS's own word that it is a folder is+ // enough, nothing is read from it.+ await dropAs(tester, 'performOperation_macos', [+ {'path': '/Volumes/Work/project', 'isDirectory': true},+ ], until: () => field(tester).text.isNotEmpty);++ expect(field(tester).text, '/Volumes/Work/project ');+ expect(names(controller), isEmpty);+ await close(tester, controller);+ });++ testWidgets('a dropped file still attaches and leaves the text alone', (+ tester,+ ) async {+ final controller = await open(tester);+ final log = File('${root.path}/trace.log')..writeAsStringSync('boom');+ await tester.enterText(composer, 'Here');++ await drop(tester, [log.path], until: () => names(controller).isNotEmpty);++ expect(names(controller), ['trace.log']);+ expect(field(tester).text, 'Here');+ await close(tester, controller);+ });++ testWidgets('a mixed drop attaches the files and pastes the folder paths', (+ tester,+ ) async {+ final controller = await open(tester);+ final log = File('${root.path}/trace.log')..writeAsStringSync('boom');+ final src = folder('src');++ await drop(+ tester,+ [log.path, src.path],+ until: () =>+ names(controller).isNotEmpty && field(tester).text.isNotEmpty,+ );++ expect(names(controller), ['trace.log']);+ expect(field(tester).text, '${src.path} ');+ await close(tester, controller);+ });++ testWidgets('no hint when the chat\'s agents are on this computer', (+ tester,+ ) async {+ final controller = await open(tester);++ await drop(tester, [+ folder('src').path,+ ], until: () => field(tester).text.isNotEmpty);++ expect(hint, findsNothing);+ await close(tester, controller);+ });++ testWidgets('the hint says the path is here when the chat\'s agents are '+ 'elsewhere, and goes with the path', (tester) async {+ final controller = await open(tester, machineId: 'studio');++ await drop(tester, [+ folder('src').path,+ ], until: () => field(tester).text.isNotEmpty);++ expect(hint, findsOneWidget);+ expect(+ find.descendant(+ of: hint,+ matching: find.text(+ "This path is on this computer. Agents on other computers can't "+ 'open it.',+ ),+ ),+ findsOneWidget,+ );+ expect(+ find.descendant(of: hint, matching: find.text('Attach as zip')),+ findsOneWidget,+ );++ await tester.enterText(composer, 'never mind');+ await tester.pump();+ expect(hint, findsNothing);+ await close(tester, controller);+ });++ testWidgets('a folder dropped twice is still one path to zip', (+ tester,+ ) async {+ final controller = await open(tester, machineId: 'studio');+ final src = folder('src');+ File('${src.path}/main.dart').writeAsStringSync('void main() {}');+ await drop(tester, [+ src.path,+ ], until: () => field(tester).text.contains(src.path));+ await drop(+ tester,+ [src.path],+ until: () =>+ field(tester).text.indexOf(src.path) !=+ field(tester).text.lastIndexOf(src.path),+ );++ expect(+ find.descendant(+ of: hint,+ matching: find.text(+ "This path is on this computer. Agents on other computers can't "+ 'open it.',+ ),+ ),+ findsOneWidget,+ );+ await close(tester, controller);+ });++ testWidgets('Attach as zip takes the path out and attaches the folder', (+ tester,+ ) async {+ final controller = await open(tester, machineId: 'studio');+ final src = folder('src');+ File('${src.path}/main.dart').writeAsStringSync('void main() {}');+ Directory('${src.path}/lib').createSync();+ File('${src.path}/lib/a.dart').writeAsStringSync('// a');+ await tester.enterText(composer, 'Look at');+ await drop(tester, [+ src.path,+ ], until: () => field(tester).text.contains(src.path));+ expect(field(tester).text, 'Look at ${src.path} ');++ await tester.tap(find.text('Attach as zip'));+ await settle(tester, () => names(controller).isNotEmpty);++ expect(names(controller), ['src.zip']);+ expect(field(tester).text, 'Look at ');+ expect(controller.draft('lead'), 'Look at ');+ expect(hint, findsNothing);+ final zip = ZipDecoder().decodeBytes(+ controller.attachments('lead').files.single.bytes,+ );+ final entries = {+ for (final entry in zip.files)+ if (entry.isFile) entry.name: String.fromCharCodes(entry.content),+ };+ expect(entries, {+ 'src/main.dart': 'void main() {}',+ 'src/lib/a.dart': '// a',+ });+ await close(tester, controller);+ });++ testWidgets('a folder over the attachment limit is not zipped, and its '+ 'path stays', (tester) async {+ final controller = await open(tester, machineId: 'studio');+ final big = folder('big');+ File('${big.path}/blob.bin')+ .writeAsBytesSync(List.filled(kSuperTabAttachmentMaxBytes + 1, 0));+ await drop(tester, [+ big.path,+ ], until: () => field(tester).text.contains(big.path));++ await tester.tap(find.text('Attach as zip'));+ await settle(tester, () => find.byType(SnackBar).evaluate().isNotEmpty);++ expect(names(controller), isEmpty);+ expect(find.text('big is over 10 MB.'), findsOneWidget);+ expect(field(tester).text, '${big.path} ');+ await close(tester, controller);+ });++ testWidgets('a zip that lands while its message is sending is not kept '+ 'for the next one', (tester) async {+ final controller = await open(tester, machineId: 'studio');+ final src = folder('src');+ File('${src.path}/main.dart').writeAsStringSync('void main() {}');+ await tester.enterText(composer, 'Look at');+ await drop(tester, [+ src.path,+ ], until: () => field(tester).text.contains(src.path));++ await tester.tap(find.text('Attach as zip'));+ server.holdReads = Completer<void>();+ await tester.tap(find.byTooltip('Send message'));+ await tester.pump();+ // The zip has its answer while the send still waits on the server.+ await settle(tester, () => names(controller).isNotEmpty);+ server.holdReads!.complete();+ server.holdReads = null;+ await tester.pumpAndSettle();++ expect(+ (server.posts.single['command'] as Map)['text'],+ 'Look at ${src.path}',+ );+ expect(names(controller), isEmpty);+ await close(tester, controller);+ });++ testWidgets('a message that fails to send keeps offering its folder as a '+ 'zip', (tester) async {+ final controller = await open(tester, machineId: 'studio');+ await drop(tester, [+ folder('src').path,+ ], until: () => field(tester).text.isNotEmpty);++ server.refuse = {'code': 'CONFLICT', 'message': 'Try again.'};+ await tester.tap(find.byTooltip('Send message'));+ await tester.pumpAndSettle();++ expect(hint, findsOneWidget);+ expect(+ find.descendant(of: hint, matching: find.text('Attach as zip')),+ findsOneWidget,+ );+ await close(tester, controller);+ });++ testWidgets('two folders of one name are both attached, the second '+ 'numbered', (tester) async {+ final controller = await open(tester, machineId: 'studio');+ final first = Directory('${root.path}/a/src')..createSync(recursive: true);+ final second = Directory('${root.path}/b/src')..createSync(recursive: true);+ File('${first.path}/one.txt').writeAsStringSync('1');+ File('${second.path}/two.txt').writeAsStringSync('2');+ await drop(tester, [+ first.path,+ second.path,+ ], until: () => field(tester).text.contains(second.path));++ await tester.tap(find.text('Attach as zip'));+ await settle(tester, () => names(controller).length == 2);++ expect(names(controller), ['src.zip', 'src-2.zip']);+ expect(field(tester).text, isEmpty);+ await close(tester, controller);+ });++ testWidgets('a zip the message has no room for keeps its path', (+ tester,+ ) async {+ final controller = await open(tester, machineId: 'studio');+ controller.attachments('lead').add([+ for (var i = 0; i < kSuperTabAttachmentMaxFiles; i++)+ HarnessAttachment('file-$i.txt', Uint8List.fromList([i + 1])),+ ]);+ final src = folder('src');+ File('${src.path}/main.dart').writeAsStringSync('void main() {}');+ await drop(tester, [+ src.path,+ ], until: () => field(tester).text.contains(src.path));++ await tester.tap(find.text('Attach as zip'));+ await settle(tester, () => find.byType(SnackBar).evaluate().isNotEmpty);++ expect(find.text('Attach at most 10 files.'), findsOneWidget);+ expect(names(controller), isNot(contains('src.zip')));+ expect(field(tester).text, '${src.path} ');+ expect(hint, findsOneWidget);+ await close(tester, controller);+ });++ testWidgets('a folder whose zip comes out over the limit keeps its path', (+ tester,+ ) async {+ final controller = await open(tester, machineId: 'studio');+ final src = folder('src');+ // Under the limit as files, over it once zipped: random bytes do not+ // compress, and the archive adds its headers.+ final random = Random(1);+ File('${src.path}/noise.bin').writeAsBytesSync([+ for (var i = 0; i < kSuperTabAttachmentMaxBytes - 16; i++)+ random.nextInt(256),+ ]);+ await drop(tester, [+ src.path,+ ], until: () => field(tester).text.contains(src.path));++ await tester.tap(find.text('Attach as zip'));+ await settle(tester, () => find.byType(SnackBar).evaluate().isNotEmpty);++ expect(find.text('src.zip is over 10 MB.'), findsOneWidget);+ expect(names(controller), isEmpty);+ expect(field(tester).text, '${src.path} ');+ await close(tester, controller);+ });++ testWidgets('a path a shell would read differently goes in quoted, its '+ 'quotes, dollars, backticks and backslashes escaped', (tester) async {+ final controller = await open(tester);+ final quoted = folder('my "notes"'),+ dollar = folder(r'cost$HOME'),+ tick = folder('run`date`'),+ slash = folder(r'back\slash'),+ apostrophe = folder("it's");+ final r = root.path;++ await drop(tester, [+ quoted.path,+ dollar.path,+ tick.path,+ slash.path,+ apostrophe.path,+ ], until: () => field(tester).text.contains('it'));++ expect(+ field(tester).text,+ '"$r/my \\"notes\\"" "$r/cost\\\$HOME" "$r/run\\`date\\`" '+ '"$r/back\\\\slash" "$r/it\'s" ',+ );+ await close(tester, controller);+ });++ testWidgets('Attach as zip takes out a path it escaped', (tester) async {+ final controller = await open(tester, machineId: 'studio');+ final quoted = folder('my "notes"');+ File('${quoted.path}/a.txt').writeAsStringSync('a');+ await tester.enterText(composer, 'See');+ await drop(tester, [+ quoted.path,+ ], until: () => field(tester).text.contains('notes'));++ await tester.tap(find.text('Attach as zip'));+ await settle(tester, () => names(controller).isNotEmpty);++ expect(names(controller), ['my "notes".zip']);+ expect(field(tester).text, 'See ');+ await close(tester, controller);+ });+}diff --git a/desktop/test/super_tab_folder_zip_test.dart b/desktop/test/super_tab_folder_zip_test.dartnew file mode 100644index 000000000..c398eeac6--- /dev/null+++ b/desktop/test/super_tab_folder_zip_test.dart@@ -0,0 +1,131 @@+// A folder zipped for a Swarm chat holds only what is inside it, even when+// the folder changes between being listed and being read.+import 'dart:io';+import 'dart:typed_data';++import 'package:archive/archive.dart';+import 'package:flutter_test/flutter_test.dart';+import 'package:harness/super_tabs/folder_read_io.dart';+import 'package:harness/super_tabs/folder_zip.dart';++void main() {+ late Directory root, src, outside;+ setUp(() {+ root = Directory.systemTemp.createTempSync('swarm-folder-zip');+ src = Directory('${root.path}/src')..createSync();+ outside = Directory('${root.path}/outside')..createSync();+ });+ tearDown(() => root.deleteSync(recursive: true));++ Map<String, String> entries(Uint8List zip) => {+ for (final entry in ZipDecoder().decodeBytes(zip).files)+ if (entry.isFile) entry.name: String.fromCharCodes(entry.content),+ };++ test('a file swapped for a link after the listing is left out', () {+ File('${src.path}/notes.txt').writeAsStringSync('mine');+ File('${src.path}/keep.txt').writeAsStringSync('kept');+ File('${outside.path}/secret.txt').writeAsStringSync('secret');++ final zip = zipFolderNow(+ src.path,+ limitBytes: 1 << 20,+ afterScan: () {+ File('${src.path}/notes.txt').deleteSync();+ Link('${src.path}/notes.txt').createSync('${outside.path}/secret.txt');+ },+ );++ expect(entries(zip!), {'src/keep.txt': 'kept'});+ });++ test('a folder swapped for a link after the listing is not read through', () {+ Directory('${src.path}/lib').createSync();+ File('${src.path}/lib/a.dart').writeAsStringSync('// a');+ File('${src.path}/keep.txt').writeAsStringSync('kept');+ File('${outside.path}/a.dart').writeAsStringSync('secret');++ final zip = zipFolderNow(+ src.path,+ limitBytes: 1 << 20,+ afterScan: () {+ Directory('${src.path}/lib').deleteSync(recursive: true);+ Link('${src.path}/lib').createSync(outside.path);+ },+ );++ expect(entries(zip!), {'src/keep.txt': 'kept'});+ });++ test('a file that grows past the limit after the listing is refused', () {+ File('${src.path}/log.txt').writeAsStringSync('small');++ final zip = zipFolderNow(+ src.path,+ limitBytes: 1024,+ afterScan: () =>+ File('${src.path}/log.txt')+ .writeAsBytesSync(List.filled(4096, 1), mode: FileMode.append),+ );++ expect(zip, isNull);+ });++ test('links already in the folder are left out', () {+ File('${src.path}/keep.txt').writeAsStringSync('kept');+ File('${outside.path}/secret.txt').writeAsStringSync('secret');+ Link('${src.path}/secret.txt').createSync('${outside.path}/secret.txt');+ Link('${src.path}/elsewhere').createSync(outside.path);++ expect(entries(zipFolderNow(src.path, limitBytes: 1 << 20)!), {+ 'src/keep.txt': 'kept',+ });+ });++ test('a dropped folder swapped for a link before the zip opens it is '+ 'refused', () {+ File('${outside.path}/secret.txt').writeAsStringSync('secret');+ src.deleteSync();+ Link(src.path).createSync(outside.path);++ expect(+ () => zipFolderNow(src.path, limitBytes: 1 << 20),+ throwsA(isA<FileSystemException>()),+ );+ });++ group('read by path, as on Windows', () {+ test('a link in place of the file is not read', () {+ File('${outside.path}/secret.txt').writeAsStringSync('secret');+ Link('${src.path}/notes.txt').createSync('${outside.path}/secret.txt');++ expect(IoFolderFiles(src.path).read(['notes.txt'], 100), isNull);+ });++ test('a link in place of a folder on the way is not read through', () {+ File('${outside.path}/a.dart').writeAsStringSync('secret');+ Link('${src.path}/lib').createSync(outside.path);++ expect(IoFolderFiles(src.path).read(['lib', 'a.dart'], 100), isNull);+ });++ test('a dropped folder that is now a link is refused', () {+ src.deleteSync();+ Link(src.path).createSync(outside.path);++ expect(+ () => IoFolderFiles(src.path),+ throwsA(isA<FileSystemException>()),+ );+ });++ test('reads one byte past what is left, no more', () {+ File('${src.path}/log.txt').writeAsStringSync('0123456789');++ expect(+ String.fromCharCodes(IoFolderFiles(src.path).read(['log.txt'], 3)!),+ '0123',+ );+ });+ });+}