Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

feat(mobile): attach files in the New form, handed to the harness before its task

tony0469f3aa801a
diff --git a/mobile/lib/phone/new_agent_attachments.dart b/mobile/lib/phone/new_agent_attachments.dart
new file mode 100644
index 000000000..1d988698c
--- /dev/null
+++ b/mobile/lib/phone/new_agent_attachments.dart
@@ -0,0 +1,152 @@
+import 'package:file_selector/file_selector.dart';
+import 'package:flutter/material.dart';
+import 'package:flutter/services.dart';
+import 'package:lucide_icons_flutter/lucide_icons.dart';
+
+import 'package:harness_mobile/state/harness_attachments.dart';
+
+import 'tty.dart';
+import 'tty_controls.dart' show TtySize;
+
+/// Opens the system's document picker for any number of files — Files on iOS, the Storage Access
+/// Framework on Android. Empty is the person backing out. Replaced in tests, where there is no
+/// picker to open.
+@visibleForTesting
+Future<List<XFile>> Function() pickAttachmentFiles = () => openFiles();
+
+/// The New form's 📎: what the person picks, read into [attachments]. Null when everything picked
+/// was taken (or nothing was), else the one line to show — the desktop's words for a file over the
+/// limit, which is refused before it is read.
+Future<String?> addPickedAttachments(HarnessAttachments attachments) async {
+ final List<XFile> picked;
+ try {
+ picked = await pickAttachmentFiles();
+ } on PlatformException {
+ return 'Could not open the picker.';
+ }
+ if (picked.isEmpty) return null;
+ final files = <HarnessAttachment>[];
+ final oversized = <String>[];
+ String? unreadable;
+ for (final file in picked) {
+ final name = file.name.isEmpty ? 'file' : file.name;
+ try {
+ if (await file.length() > HarnessAttachments.maxBytes) {
+ oversized.add(name);
+ continue;
+ }
+ } catch (_) {
+ // No size to read is no reason to refuse yet: the read below says whether it is there.
+ }
+ try {
+ files.add(HarnessAttachment(name, await file.readAsBytes()));
+ } catch (_) {
+ unreadable ??= name;
+ }
+ }
+ final tooLarge = attachments.add(files, oversized: oversized);
+ return tooLarge ?? (unreadable == null ? null : 'Could not read $unreadable');
+}
+
+/// The 📎 inside the task field, beside its mic.
+class AttachmentButton extends StatelessWidget {
+ const AttachmentButton({super.key, required this.onTap});
+
+ final VoidCallback? onTap;
+
+ @override
+ Widget build(BuildContext context) {
+ final tty = Tty.of(context);
+ return Semantics(
+ button: true,
+ label: 'Attach files',
+ excludeSemantics: true,
+ child: GestureDetector(
+ key: const ValueKey('new-agent-attach'),
+ behavior: HitTestBehavior.opaque,
+ onTap: onTap == null
+ ? null
+ : () {
+ HapticFeedback.selectionClick();
+ onTap!();
+ },
+ child: SizedBox(
+ width: 44,
+ height: 44,
+ child: Center(
+ child: Icon(
+ LucideIcons.paperclip300,
+ size: 18,
+ color: onTap == null ? tty.dim : tty.text,
+ ),
+ ),
+ ),
+ ),
+ );
+ }
+}
+
+/// The files the harness will be handed, one line each under the task: the name, and × to take
+/// it back off. Nothing while there are none.
+class AttachmentList extends StatelessWidget {
+ const AttachmentList({
+ super.key,
+ required this.attachments,
+ required this.enabled,
+ });
+
+ final HarnessAttachments attachments;
+
+ /// False while the form is starting: the files are the ones on their way.
+ final bool enabled;
+
+ @override
+ Widget build(BuildContext context) => ListenableBuilder(
+ listenable: attachments,
+ builder: (context, _) {
+ final tty = Tty.of(context);
+ return Column(
+ crossAxisAlignment: CrossAxisAlignment.stretch,
+ children: [
+ for (final file in attachments.files)
+ Row(
+ key: ValueKey('attachment-${file.name}'),
+ children: [
+ Icon(LucideIcons.paperclip300, size: 14, color: tty.faint),
+ const SizedBox(width: 8),
+ Expanded(
+ child: Text(
+ file.name,
+ maxLines: 1,
+ overflow: TextOverflow.ellipsis,
+ style: tty.style(color: tty.dim, size: TtySize.meta),
+ ),
+ ),
+ Semantics(
+ button: true,
+ label: 'Remove ${file.name}',
+ excludeSemantics: true,
+ child: GestureDetector(
+ key: ValueKey('attachment-remove-${file.name}'),
+ behavior: HitTestBehavior.opaque,
+ onTap: enabled ? () => attachments.remove(file) : null,
+ child: SizedBox(
+ width: 44,
+ height: 36,
+ child: Center(
+ child: Icon(
+ LucideIcons.x300,
+ size: 14,
+ color: enabled ? tty.faint : tty.dim,
+ ),
+ ),
+ ),
+ ),
+ ),
+ ],
+ ),
+ ],
+ );
+ },
+ );
+}
diff --git a/mobile/lib/phone/new_agent_page.dart b/mobile/lib/phone/new_agent_page.dart
index 248986517..246a82e0c 100644
--- a/mobile/lib/phone/new_agent_page.dart
+++ b/mobile/lib/phone/new_agent_page.dart
@@ -9,6 +9,8 @@ import 'package:harness_mobile/core/models.dart';
import 'package:harness_mobile/core/permission_modes.dart';
import 'package:harness_mobile/shared/theme/app_theme.dart';
import 'package:harness_mobile/state/app_state.dart';
+import 'package:harness_mobile/state/attached_task_delivery.dart';
+import 'package:harness_mobile/state/harness_attachments.dart';
import 'package:harness_mobile/widgets/engine_identity.dart';
import 'package:harness_mobile/core/git_project.dart';
import 'package:harness_mobile/core/project_folder.dart';
@@ -18,6 +20,7 @@ import '../demo/sample_mode.dart' show SampleMode;
import 'agent_index.dart';
import 'branch_picker_sheet.dart';
import 'phone_navigation.dart';
+import 'new_agent_attachments.dart';
import 'new_agent_draft.dart';
import 'new_agent_model_sheet.dart';
import 'phone_status.dart';
@@ -210,10 +213,31 @@ class _NewAgentPageState extends State<NewAgentPage> {
void dispose() {
_keepDraft();
_task.dispose();
+ _attachments.dispose();
_ownVoice?.dispose();
super.dispose();
}
+ /// The files the harness will be handed — the desktop's `HarnessAttachments` behind its 📎. Only
+ /// for this open form: the draft a closed form leaves keeps the words, not the bytes.
+ final _attachments = HarnessAttachments();
+
+ /// Files can go to this machine's harnesses, and this agent takes the task they lead: the 📎 is
+ /// drawn only then, never as a button that is refused at Start.
+ bool get _canAttach {
+ final engine = _engine;
+ return engine != null &&
+ takesFirstTask(engine) &&
+ canAttachFiles(widget.notifier, _machineId);
+ }
+
+ /// The 📎: the system's picker, then the files under the task.
+ Future<void> _attach() async {
+ final problem = await addPickedAttachments(_attachments);
+ if (!mounted) return;
+ setState(() => _error = problem);
+ }
+
/// The first task — `prompt` on `agent_create`. Optional: empty starts the harness idle.
///
/// ⚠️ **No length limit on the field.** One cut whatever was typed or pasted past
@@ -848,10 +872,33 @@ class _NewAgentPageState extends State<NewAgentPage> {
});
return;
}
+ // Files ride into the live terminal after creation, and the task follows them there (see
+ // [deliverAttachedTask]), so neither is a launch argument then — the desktop's order.
+ final files = _attachments.files;
+ // Files picked before the agent or the machine changed under them: said, never dropped.
+ final cannotTake = files.isEmpty
+ ? null
+ : !takesFirstTask(engine)
+ ? 'This agent cannot be handed files. Remove them to start it.'
+ : !canAttachFiles(widget.notifier, _machineId)
+ ? 'Update Harness CLI on '
+ '${_machine?.machine.displayName ?? 'this computer'} to attach '
+ 'files, or remove them.'
+ : null;
+ if (cannotTake != null) {
+ setState(() {
+ _creating = false;
+ _error = cannotTake;
+ });
+ return;
+ }
// Held so the id of what it starts comes back here — see
// [AgentCreationAttempt.agentId] — and kept as [_creation] for as long as
// its answer is outstanding.
final creation = _creation = AgentCreationAttempt();
+ _delivery = files.isEmpty
+ ? null
+ : (creation: creation, files: files, task: task);
// A folder in a repository is not sent as a bare `cwd`: the branch and the
// worktree travel with it, and [gitFolderRequest] is the desktop's own rule
// for turning the rows above into what the machine is asked to do. A folder
@@ -893,7 +940,7 @@ class _NewAgentPageState extends State<NewAgentPage> {
model: _model,
deskTabId: widget.deskTabId,
attempt: creation,
- prompt: task.isEmpty ? null : task,
+ prompt: task.isEmpty || files.isNotEmpty ? null : task,
// Whenever a first task is sent, the harness is called by its words until the engine titles
// the session — in any project, the desktop's rule. Named only for a New folder, a second
// harness in an existing one read "Untitled Pane" until then. Nothing to say leaves the name
@@ -934,6 +981,7 @@ class _NewAgentPageState extends State<NewAgentPage> {
// by accident. See [NewAgentDraft].
newAgentDraft = null;
unawaited(widget.notifier.agentPreference.select(engine));
+ _deliverFiles(creation);
_open(creation.agentId);
return;
}
@@ -944,6 +992,40 @@ class _NewAgentPageState extends State<NewAgentPage> {
});
}
+ /// The files [creation] is to be handed once it exists, and the task that follows them — kept
+ /// beside [_creation] so a Check status that finds it started still hands them over.
+ ({AgentCreationAttempt creation, List<HarnessAttachment> files, String task})?
+ _delivery;
+
+ /// Hands a started harness its files and then its task, detached from this form, which is
+ /// replaced by the harness's page at once — the desktop's `_deliverFiles`. A problem is said on
+ /// the app's own messenger, which outlives the form.
+ void _deliverFiles(AgentCreationAttempt creation) {
+ final delivery = _delivery;
+ if (delivery == null || !identical(delivery.creation, creation)) return;
+ _delivery = null;
+ _attachments.clear();
+ final messenger = ScaffoldMessenger.maybeOf(context);
+ void report(String message) =>
+ messenger?.showSnackBar(SnackBar(content: Text(message)));
+ final agentId = creation.agentId;
+ if (agentId == null) {
+ report('The harness started without its files. Attach them in its pane.');
+ return;
+ }
+ unawaited(
+ deliverAttachedTask(
+ widget.notifier,
+ machineId: _machineId,
+ agentId: agentId,
+ files: delivery.files,
+ task: delivery.task,
+ ).then((problem) {
+ if (problem != null) report(problem);
+ }),
+ );
+ }
+
/// The last request this form made, kept while the machine has not said whether it started —
/// the desktop's `_attempt` (`state/new_harness.dart`).
///
@@ -1342,6 +1424,15 @@ class _NewAgentPageState extends State<NewAgentPage> {
action: TextInputAction.newline,
onChanged: _taskEdited,
trailing: [
+ if (_canAttach)
+ Align(
+ alignment: Alignment.bottomRight,
+ child: AttachmentButton(
+ onTap: _creating
+ ? null
+ : () => unawaited(_attach()),
+ ),
+ ),
Align(
alignment: Alignment.bottomRight,
child: ListenableBuilder(
@@ -1365,6 +1456,17 @@ class _NewAgentPageState extends State<NewAgentPage> {
),
],
),
+ // Shown while there are files even where none can go now — the
+ // machine or the agent changed under them — so they can be taken
+ // back off.
+ if (_canAttach || !_attachments.isEmpty)
+ Padding(
+ padding: const EdgeInsets.only(top: 4),
+ child: AttachmentList(
+ attachments: _attachments,
+ enabled: !_creating,
+ ),
+ ),
],
if (_error != null)
Padding(
diff --git a/mobile/lib/state/attached_task_delivery.dart b/mobile/lib/state/attached_task_delivery.dart
new file mode 100644
index 000000000..ee02a0071
--- /dev/null
+++ b/mobile/lib/state/attached_task_delivery.dart
@@ -0,0 +1,193 @@
+import 'dart:async';
+
+import 'package:xterm/xterm.dart';
+
+import '../logging/app_log.dart';
+import '../terminal/terminal_session.dart';
+import 'app_state.dart';
+import 'harness_attachments.dart';
+
+// The desktop's `state/attached_task_delivery.dart`, carried to the phone: the same waits, the same
+// readiness read off the screen, the same words. Keep the two in step.
+
+/// How long a new harness has to open its terminal and start its engine. An observed Codex update
+/// waits for completion or pane closure instead: the person may leave its confirmation open for
+/// longer than this deadline.
+const kAttachedTaskOpenTimeout = Duration(minutes: 2);
+
+/// A beat after the engine reports ready, for its input to be drawn.
+const kAttachedTaskSettle = Duration(milliseconds: 800);
+
+/// Codex can be alive in its updater before it opens a conversation. An empty chat may not have a
+/// session id yet either. Read only the visible terminal, looking for its composer and footer
+/// together, not a menu's selected row or an old prompt in scrollback.
+enum _CodexStartup { waiting, updating, ready }
+
+_CodexStartup _codexStartup(Terminal terminal) {
+ final buffer = terminal.buffer;
+ final lines = [
+ for (var row = buffer.scrollBack; row < buffer.height; row++)
+ buffer.lines[row].getText(),
+ ];
+ final prompt = lines.lastIndexWhere(
+ (line) => RegExp(r'^\s*›').hasMatch(line),
+ );
+ if (prompt >= 0 &&
+ lines
+ .skip(prompt + 1)
+ .any(
+ (line) =>
+ RegExp(r'\? for shortcuts|\d+% context left').hasMatch(line),
+ )) {
+ return _CodexStartup.ready;
+ }
+ if (RegExp(
+ r'Update available!|Updating Codex via|Update ran successfully! Please restart Codex\.',
+ ).hasMatch(lines.join('\n'))) {
+ return _CodexStartup.updating;
+ }
+ return _CodexStartup.waiting;
+}
+
+/// Can [machineId] take files into a harness's prompt? Both halves are the machine's: a file
+/// written there and its path pasted, then a space pasted.
+bool canAttachFiles(AppNotifier app, String machineId) {
+ final machine = app.stateOf(machineId);
+ return machine != null &&
+ machine.terminalPasteFileAvailable &&
+ machine.terminalPasteRawAvailable;
+}
+
+/// Hands a new harness its files, then its first message.
+///
+/// A first message given at creation reaches the engine as a launch argument, before any file
+/// could: files only travel into a live terminal, which writes each on the harness's machine and
+/// pastes its path into the prompt. So a task with files is created without one, and sent here
+/// once the prompt holds the paths — as a composer message, which the machine submits and retries
+/// Enter for. Returns what went wrong, or null.
+Future<String?> deliverAttachedTask(
+ AppNotifier app, {
+ required String machineId,
+ required String agentId,
+ required List<HarnessAttachment> files,
+ required String task,
+}) async {
+ appLog.info('attach', 'waiting on $agentId for ${files.length} file(s)');
+ final session = await _readySession(app, machineId, agentId);
+ if (session == null) {
+ appLog.warn('attach', '$agentId never took input');
+ return 'The harness did not open in time. Attach the files in its pane.';
+ }
+ for (final file in files) {
+ if (!await session.pasteFile(file.name, file.bytes) ||
+ !await session.pasteText(' ')) {
+ appLog.warn('attach', '${file.name} did not reach $agentId');
+ return 'Could not attach ${file.name}. Attach it in the pane.';
+ }
+ }
+ appLog.info('attach', '${files.length} file(s) in $agentId; sending task');
+ if (task.trim().isEmpty) return null;
+ return await session.sendComposerText(task)
+ ? null
+ : 'The files are attached, but the task was not sent.';
+}
+
+/// The harness's terminal once its engine is up and the pane takes input.
+Future<TerminalSession?> _readySession(
+ AppNotifier app,
+ String machineId,
+ String agentId,
+) async {
+ var claimed = false;
+ TerminalSession? watchedSession;
+ Terminal? watchedTerminal;
+ Timer? timeout;
+ late void Function() check;
+ TerminalSession? ready() {
+ final agent = app
+ .stateOf(machineId)
+ ?.agents
+ .where((agent) => agent.id == agentId)
+ .firstOrNull;
+ if (agent?.launchState != 'ready') return null;
+ final session = app
+ .panesFor(machineId)
+ .where((pane) => pane.agentId == agentId)
+ .map((pane) => pane.session)
+ .nonNulls
+ .firstOrNull;
+ if (session == null) return null;
+ // Output and replacement keyframes need not change the agent inventory. Watch both so the
+ // queued task wakes up when the new composer is drawn.
+ if (!identical(watchedSession, session)) {
+ watchedSession?.removeListener(check);
+ watchedSession = session..addListener(check);
+ }
+ if (!identical(watchedTerminal, session.terminal)) {
+ watchedTerminal?.removeListener(check);
+ watchedTerminal = session.terminal..addListener(check);
+ }
+ if (session.acceptsInput) {
+ if (agent?.engine == 'codex') {
+ final startup = _codexStartup(session.terminal);
+ if (startup == _CodexStartup.updating) timeout?.cancel();
+ if (startup != _CodexStartup.ready) return null;
+ }
+ return session;
+ }
+ // A pane can open as a watcher, and another window on the account (a desktop) may hold the
+ // terminal. Pressing Start with files is this person asking to type here: take it, once.
+ if (!claimed &&
+ session.streamId != null &&
+ (session.watching ||
+ session.status == TerminalSessionStatus.takenOver)) {
+ claimed = true;
+ appLog.info('attach', 'taking control of $agentId to hand it its files');
+ // A takeover can notify synchronously. Check again after reopen returns so that
+ // notification cannot be cancelled by this call's null.
+ unawaited(session.reopen(force: true).then((_) => check()));
+ }
+ return null;
+ }
+
+ final found = Completer<TerminalSession?>();
+ TerminalSession? candidate;
+ Timer? settle;
+ check = () {
+ if (found.isCompleted) return;
+ if (watchedSession != null &&
+ !app.panesFor(machineId).any((pane) => pane.agentId == agentId)) {
+ found.complete(null);
+ return;
+ }
+ final session = ready();
+ if (identical(candidate, session)) return;
+ settle?.cancel();
+ candidate = session;
+ if (session == null) return;
+ settle = Timer(kAttachedTaskSettle, () {
+ if (found.isCompleted) return;
+ if (identical(ready(), session)) {
+ found.complete(session);
+ } else {
+ candidate = null;
+ check();
+ }
+ });
+ };
+
+ timeout = Timer(kAttachedTaskOpenTimeout, () {
+ if (!found.isCompleted) found.complete(null);
+ });
+ app.addListener(check);
+ check();
+ try {
+ return await found.future;
+ } finally {
+ timeout.cancel();
+ settle?.cancel();
+ watchedSession?.removeListener(check);
+ watchedTerminal?.removeListener(check);
+ app.removeListener(check);
+ }
+}
diff --git a/mobile/lib/state/harness_attachments.dart b/mobile/lib/state/harness_attachments.dart
new file mode 100644
index 000000000..9d6ff06f1
--- /dev/null
+++ b/mobile/lib/state/harness_attachments.dart
@@ -0,0 +1,62 @@
+import 'package:flutter/foundation.dart';
+
+import '../terminal/terminal_binary.dart'
+ show terminalLocalPasteFileMaxPayloadBytes;
+
+/// One file attached to a harness that does not exist yet — the desktop's `HarnessAttachment`.
+@immutable
+class HarnessAttachment {
+ const HarnessAttachment(this.name, this.bytes);
+
+ final String name;
+ final Uint8List bytes;
+}
+
+/// The files a New form carries into its harness, held in memory until the harness's terminal is
+/// open to take them (`deliverAttachedTask`). The desktop's `HarnessAttachments`, without its
+/// paste half: the phone's files come from the system's picker only.
+class HarnessAttachments extends ChangeNotifier {
+ /// What one file may weigh: the machine's own ceiling for a pasted file.
+ static const maxBytes = terminalLocalPasteFileMaxPayloadBytes;
+
+ final _files = <HarnessAttachment>[];
+
+ List<HarnessAttachment> get files => List.unmodifiable(_files);
+ bool get isEmpty => _files.isEmpty;
+
+ /// Adds the files that fit, replacing one of the same name. Returns what to tell the person
+ /// about any left out, or null when all were taken. [oversized] names files already known to be
+ /// over [maxBytes], which were never read into memory to be refused here. The desktop's words.
+ String? add(
+ Iterable<HarnessAttachment> files, {
+ Iterable<String> oversized = const [],
+ }) {
+ final tooLarge = [...oversized];
+ for (final file in files) {
+ if (file.bytes.isEmpty) continue;
+ if (file.bytes.length > maxBytes) {
+ tooLarge.add(file.name);
+ continue;
+ }
+ _files
+ ..removeWhere((kept) => kept.name == file.name)
+ ..add(file);
+ }
+ notifyListeners();
+ if (tooLarge.isEmpty) return null;
+ final limit = '${maxBytes ~/ (1024 * 1024)} MB';
+ return tooLarge.length == 1
+ ? '${tooLarge.single} is over $limit.'
+ : '${tooLarge.length} files are over $limit.';
+ }
+
+ void remove(HarnessAttachment file) {
+ if (_files.remove(file)) notifyListeners();
+ }
+
+ void clear() {
+ if (_files.isEmpty) return;
+ _files.clear();
+ notifyListeners();
+ }
+}
diff --git a/mobile/test/attached_task_delivery_test.dart b/mobile/test/attached_task_delivery_test.dart
new file mode 100644
index 000000000..26fc1b340
--- /dev/null
+++ b/mobile/test/attached_task_delivery_test.dart
@@ -0,0 +1,334 @@
+import 'dart:async';
+import 'dart:typed_data';
+
+import 'package:flutter_test/flutter_test.dart';
+import 'package:xterm/xterm.dart';
+import 'package:harness_mobile/auth/auth_session.dart';
+import 'package:harness_mobile/core/config.dart';
+import 'package:harness_mobile/core/models.dart';
+import 'package:harness_mobile/state/app_state.dart';
+import 'package:harness_mobile/state/attached_task_delivery.dart';
+import 'package:harness_mobile/state/harness_attachments.dart';
+import 'package:harness_mobile/terminal/terminal_session.dart';
+
+// The desktop's `test/harness_attachments_test.dart`, carried with the code it tests
+// (`state/attached_task_delivery.dart`): the same waits and the same order on the phone.
+
+HarnessAttachment _file(String name, [int size = 4]) =>
+ HarnessAttachment(name, Uint8List(size));
+
+const _codexComposer =
+ '\u001b[1m›\u001b[0m \u001b[2mAsk Codex to do anything\u001b[0m\r\n'
+ '\r\n GPT-6-Astra · /tmp/work\r\n ? for shortcuts';
+
+/// Records what a harness's terminal was handed, in order.
+class _RecordingSession extends TerminalSession {
+ _RecordingSession()
+ : super(
+ machineId: 'box',
+ agentId: 'a1',
+ agentName: 'Weather',
+ engineId: 'claude',
+ send: (_, _) async => true,
+ sendBinary: (_) async => true,
+ ) {
+ status = TerminalSessionStatus.controlling;
+ streamId = 's1';
+ }
+
+ final calls = <String>[];
+
+ @override
+ Future<bool> pasteFile(String filename, Uint8List content) async {
+ calls.add('file:$filename');
+ return true;
+ }
+
+ @override
+ Future<bool> pasteText(String text) async {
+ calls.add('paste:$text');
+ return true;
+ }
+
+ @override
+ Future<bool> sendComposerText(String text) async {
+ calls.add('send:$text');
+ return true;
+ }
+
+ /// "Take control": the daemon answers by handing this window the lease.
+ @override
+ Future<void> reopen({bool force = false}) async {
+ calls.add('reopen:$force');
+ watching = false;
+ notifyListeners();
+ }
+}
+
+AppNotifier _app({
+ String launch = 'ready',
+ bool canPasteFiles = true,
+ String? engine,
+}) {
+ final app = AppNotifier(
+ config: AppConfig.dev,
+ authSession: AuthSession(),
+ configStore: null,
+ );
+ app.machineStates['box'] =
+ MachineState(
+ const Machine(
+ machineId: 'box',
+ name: 'Box',
+ authMode: MachineAuthMode.remote,
+ ),
+ )
+ ..terminalPasteFileAvailable = canPasteFiles
+ ..terminalPasteRawAvailable = true
+ ..agents = [
+ Agent(id: 'a1', name: 'Weather', launchState: launch, engine: engine),
+ ];
+ return app;
+}
+
+void main() {
+ group('HarnessAttachments', () {
+ test('keeps one file per name and refuses what is over the limit', () {
+ final files = HarnessAttachments();
+ expect(files.add([_file('a.png'), _file('b.txt')]), isNull);
+ expect(files.add([_file('a.png', 8)]), isNull);
+ expect(files.files.map((f) => f.name), ['b.txt', 'a.png']);
+ expect(files.files.last.bytes.length, 8);
+ final problem = files.add([
+ _file('huge.mov', HarnessAttachments.maxBytes + 1),
+ ]);
+ expect(problem, contains('huge.mov'));
+ expect(files.files, hasLength(2));
+ files.remove(files.files.first);
+ expect(files.files.map((f) => f.name), ['a.png']);
+ });
+ });
+
+ group('deliverAttachedTask', () {
+ testWidgets('rechecks Codex readiness and follows a replacement keyframe', (
+ tester,
+ ) async {
+ final app = _app(engine: 'codex');
+ addTearDown(app.dispose);
+ final session = _RecordingSession();
+ session.terminal.write(_codexComposer);
+ app.adoptSessionForTest(session);
+ String? problem = 'pending';
+ unawaited(
+ deliverAttachedTask(
+ app,
+ machineId: 'box',
+ agentId: 'a1',
+ files: [_file('shot.png')],
+ task: 'Look',
+ ).then((value) => problem = value),
+ );
+ await tester.pump(const Duration(milliseconds: 400));
+ // The old composer has scrolled away. It cannot make installer output
+ // ready, including during the interval originally reserved for settling.
+ session.terminal.write('${'\r\n' * 40}Updating Codex via installer…');
+ await tester.pump(kAttachedTaskSettle);
+ expect(session.calls, isEmpty);
+ expect(problem, 'pending');
+ final oldTerminal = session.terminal;
+ session.terminal = Terminal();
+ session.notifyListeners();
+ oldTerminal.write('\u001b[2J\u001b[H$_codexComposer');
+ await tester.pump(kAttachedTaskSettle);
+ expect(session.calls, isEmpty);
+ session.terminal.write(_codexComposer);
+ await tester.pump(kAttachedTaskSettle + const Duration(milliseconds: 10));
+ await tester.pump();
+ expect(problem, isNull);
+ expect(session.calls, ['file:shot.png', 'paste: ', 'send:Look']);
+ });
+
+ testWidgets('stops waiting if the updating Codex pane is closed', (
+ tester,
+ ) async {
+ final app = _app(engine: 'codex');
+ addTearDown(app.dispose);
+ final session = _RecordingSession();
+ session.terminal.write('Update available!\r\n› 1. Update now');
+ final pane = app.adoptSessionForTest(session);
+ String? problem = 'pending';
+ unawaited(
+ deliverAttachedTask(
+ app,
+ machineId: 'box',
+ agentId: 'a1',
+ files: [_file('shot.png')],
+ task: 'Look',
+ ).then((value) => problem = value),
+ );
+ await tester.pump(kAttachedTaskOpenTimeout + const Duration(seconds: 1));
+ expect(problem, 'pending');
+ app.panes.remove(pane);
+ app.notifyListeners();
+ await tester.pump();
+ expect(problem, contains('did not open in time'));
+ expect(session.calls, isEmpty);
+ session.dispose();
+ });
+
+ testWidgets(
+ 'keeps the task and image queued through a Codex startup update',
+ (tester) async {
+ final app = _app(engine: 'codex');
+ addTearDown(app.dispose);
+ final session = _RecordingSession();
+ session.terminal.write(
+ 'Update available!\r\n› 1. Update now\r\n 2. Skip\r\n'
+ 'Press enter to continue',
+ );
+ app.adoptSessionForTest(session);
+ String? problem = 'pending';
+ unawaited(
+ deliverAttachedTask(
+ app,
+ machineId: 'box',
+ agentId: 'a1',
+ files: [_file('shot.png')],
+ task: 'Fix the problem in this image',
+ ).then((value) => problem = value),
+ );
+ // The updater is a running Codex process with an input-capable terminal,
+ // but no conversation yet. Nothing may be pasted into its update menu.
+ await tester.pump(
+ kAttachedTaskOpenTimeout + const Duration(seconds: 30),
+ );
+ expect(session.calls, isEmpty);
+ expect(problem, 'pending');
+ app.notifyListeners(); // Rediscovery during installation is not readiness.
+ await tester.pump(const Duration(seconds: 30));
+ expect(session.calls, isEmpty);
+ session.terminal.write(
+ '\u001b[2J\u001b[HUpdating Codex via installer…',
+ );
+ await tester.pump(const Duration(seconds: 10));
+ expect(session.calls, isEmpty);
+ // A new empty chat is usable before its first session ID arrives. Only
+ // terminal output changes here; no agent inventory notification is sent.
+ session.terminal.write('\u001b[2J\u001b[H$_codexComposer');
+ await tester.pump(
+ kAttachedTaskSettle + const Duration(milliseconds: 10),
+ );
+ await tester.pump();
+ expect(problem, isNull);
+ expect(session.calls, [
+ 'file:shot.png',
+ 'paste: ',
+ 'send:Fix the problem in this image',
+ ]);
+ app.notifyListeners();
+ await tester.pump(const Duration(seconds: 1));
+ expect(session.calls, hasLength(3));
+ },
+ );
+
+ test('files first, each followed by a space, then the task', () async {
+ final app = _app();
+ addTearDown(app.dispose);
+ final session = _RecordingSession();
+ app.adoptSessionForTest(session);
+ final problem = await deliverAttachedTask(
+ app,
+ machineId: 'box',
+ agentId: 'a1',
+ files: [_file('shot.png'), _file('notes.md')],
+ task: 'Summarise these',
+ );
+ expect(problem, isNull);
+ expect(session.calls, [
+ 'file:shot.png',
+ 'paste: ',
+ 'file:notes.md',
+ 'paste: ',
+ 'send:Summarise these',
+ ]);
+ });
+
+ testWidgets('waits for the engine to be ready before handing anything', (
+ tester,
+ ) async {
+ final app = _app(launch: 'starting');
+ addTearDown(app.dispose);
+ final session = _RecordingSession();
+ app.adoptSessionForTest(session);
+ String? problem = 'pending';
+ unawaited(
+ deliverAttachedTask(
+ app,
+ machineId: 'box',
+ agentId: 'a1',
+ files: [_file('shot.png')],
+ task: 'Look',
+ ).then((value) => problem = value),
+ );
+ await tester.pump(const Duration(seconds: 5));
+ expect(session.calls, isEmpty);
+ app.machineStates['box']!.agents = [
+ const Agent(id: 'a1', name: 'Weather'),
+ ];
+ app.notifyListeners();
+ await tester.pump(kAttachedTaskSettle + const Duration(milliseconds: 10));
+ await tester.pump();
+ expect(problem, isNull);
+ expect(session.calls.first, 'file:shot.png');
+ });
+
+ test('takes control of a pane that opened as a watcher', () async {
+ final app = _app();
+ addTearDown(app.dispose);
+ // Another window on the account (the Mac's own app) holds the terminal.
+ final session = _RecordingSession()..watching = true;
+ app.adoptSessionForTest(session);
+ final problem = await deliverAttachedTask(
+ app,
+ machineId: 'box',
+ agentId: 'a1',
+ files: [_file('shot.png')],
+ task: 'Look',
+ );
+ expect(problem, isNull);
+ expect(session.calls, [
+ 'reopen:true',
+ 'file:shot.png',
+ 'paste: ',
+ 'send:Look',
+ ]);
+ });
+
+ testWidgets('gives up with a message when the harness never opens', (
+ tester,
+ ) async {
+ final app = _app(launch: 'starting');
+ addTearDown(app.dispose);
+ String? problem;
+ unawaited(
+ deliverAttachedTask(
+ app,
+ machineId: 'box',
+ agentId: 'a1',
+ files: [_file('shot.png')],
+ task: 'Look',
+ ).then((value) => problem = value),
+ );
+ await tester.pump(kAttachedTaskOpenTimeout + const Duration(seconds: 1));
+ await tester.pump();
+ expect(problem, contains('did not open in time'));
+ });
+ });
+
+ test('a machine whose CLI cannot paste files cannot take attachments', () {
+ final app = _app(canPasteFiles: false);
+ addTearDown(app.dispose);
+ expect(canAttachFiles(app, 'box'), isFalse);
+ expect(canAttachFiles(app, 'nowhere'), isFalse);
+ });
+}
diff --git a/mobile/test/phone_edge/new_attachments_test.dart b/mobile/test/phone_edge/new_attachments_test.dart
new file mode 100644
index 000000000..e6c4d75ee
--- /dev/null
+++ b/mobile/test/phone_edge/new_attachments_test.dart
@@ -0,0 +1,320 @@
+import 'dart:convert';
+
+import 'package:file_selector/file_selector.dart';
+import 'package:flutter/material.dart';
+import 'package:flutter/services.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/phone/agent_swipe.dart';
+import 'package:harness_mobile/phone/new_agent_attachments.dart';
+import 'package:harness_mobile/phone/new_agent_draft.dart';
+import 'package:harness_mobile/phone/new_agent_page.dart';
+import 'package:harness_mobile/state/app_state.dart';
+import 'package:harness_mobile/state/attached_task_delivery.dart'
+ show kAttachedTaskOpenTimeout;
+import 'package:harness_mobile/state/harness_attachments.dart';
+
+import 'edge_fixture.dart';
+
+/// A picked file whose size and bytes are said separately — what the picker hands over for a file
+/// it can measure without reading, or one it cannot read at all.
+class _Picked extends XFile {
+ _Picked(super.path, {this.size, this.content, this.unreadable = false});
+
+ final int? size;
+ final Uint8List? content;
+ final bool unreadable;
+
+ @override
+ String get name => path;
+
+ @override
+ Future<int> length() async => size ?? content?.length ?? 0;
+
+ @override
+ Future<Uint8List> readAsBytes() async {
+ if (unreadable) throw const _Unreadable();
+ return content ?? Uint8List(0);
+ }
+}
+
+class _Unreadable implements Exception {
+ const _Unreadable();
+}
+
+_Picked _file(String name, [String text = 'contents']) =>
+ _Picked(name, content: Uint8List.fromList(utf8.encode(text)));
+
+/// Attaching files to a New harness from the phone — the desktop's 📎. What the picker hands over
+/// is held under the task, and Start creates the harness without the task, which follows the files
+/// into its terminal (`deliverAttachedTask`).
+void main() {
+ late Future<List<XFile>> Function() restore;
+ setUp(() => restore = pickAttachmentFiles);
+ tearDown(() {
+ pickAttachmentFiles = restore;
+ newAgentDraft = null;
+ });
+
+ group('what the picker hands over', () {
+ test('is read in, one per name', () async {
+ final attachments = HarnessAttachments();
+ pickAttachmentFiles = () async => [_file('a.pdf'), _file('b.log')];
+
+ expect(await addPickedAttachments(attachments), isNull);
+ pickAttachmentFiles = () async => [_file('a.pdf', 'newer')];
+ expect(await addPickedAttachments(attachments), isNull);
+
+ expect(attachments.files.map((f) => f.name), ['b.log', 'a.pdf']);
+ expect(utf8.decode(attachments.files.last.bytes), 'newer');
+ });
+
+ test('a file over the limit is named and never read', () async {
+ final attachments = HarnessAttachments();
+ pickAttachmentFiles = () async => [
+ _Picked(
+ 'huge.zip',
+ size: HarnessAttachments.maxBytes + 1,
+ unreadable: true,
+ ),
+ _file('notes.md'),
+ ];
+
+ expect(
+ await addPickedAttachments(attachments),
+ 'huge.zip is over 20 MB.',
+ );
+ expect(attachments.files.map((f) => f.name), ['notes.md']);
+ });
+
+ test('a file that cannot be read says which', () async {
+ final attachments = HarnessAttachments();
+ pickAttachmentFiles = () async => [
+ _Picked('locked.key', size: 3, unreadable: true),
+ ];
+
+ expect(
+ await addPickedAttachments(attachments),
+ 'Could not read locked.key',
+ );
+ expect(attachments.isEmpty, isTrue);
+ });
+
+ test(
+ 'backing out says nothing; a picker that cannot open says so',
+ () async {
+ final attachments = HarnessAttachments();
+ pickAttachmentFiles = () async => [];
+ expect(await addPickedAttachments(attachments), isNull);
+
+ pickAttachmentFiles = () async => throw PlatformException(code: 'x');
+ expect(
+ await addPickedAttachments(attachments),
+ 'Could not open the picker.',
+ );
+ },
+ );
+ });
+
+ group('the form', () {
+ Map<String, dynamic>? created(String type, Map<String, dynamic> payload) =>
+ type != 'agent_create'
+ ? null
+ : {
+ 'creationId': payload['creationId'],
+ 'state': 'created',
+ 'agent': {
+ 'id': 'new-agent',
+ 'name': 'new-agent',
+ 'engine': 'claude',
+ 'terminal': {'available': true},
+ },
+ };
+
+ /// New on `m`, whose agent's folder is the project, on a machine that does or does not take
+ /// files into a harness's prompt.
+ Future<({AppNotifier app, EdgeConn conn})> openNew(
+ WidgetTester tester, {
+ bool takesFiles = true,
+ }) async {
+ setPhone(tester, largePhone);
+ final conn = EdgeConn(const {}, created);
+ conn.capabilities = {
+ ...conn.capabilities,
+ 'features': {
+ 'projectFolder': true,
+ 'pasteRaw': true,
+ 'pasteFile': takesFiles,
+ },
+ };
+ final app = edgeApp(conn: conn);
+ await tester.pumpWidget(
+ phoneApp(
+ Builder(
+ builder: (context) => TextButton(
+ onPressed: () => Navigator.of(context).push(
+ MaterialPageRoute<void>(
+ builder: (_) => NewAgentPage(notifier: app, machineId: 'm'),
+ ),
+ ),
+ child: const Text('focus'),
+ ),
+ ),
+ ),
+ );
+ await tester.tap(find.text('focus'));
+ await frames(tester);
+ // The machine's terminal features, read the way a connection reads them.
+ app.stateOf('m')!
+ ..terminalPasteFileAvailable = takesFiles
+ ..terminalPasteRawAvailable = true;
+ app.notifyListeners();
+ await frames(tester);
+ return (app: app, conn: conn);
+ }
+
+ Future<void> close(WidgetTester tester, AppNotifier app) async {
+ await tester.pumpWidget(const SizedBox());
+ await tester.pump(const Duration(seconds: 3));
+ app.dispose();
+ await tester.pump(const Duration(seconds: 30));
+ }
+
+ final attach = find.byKey(const ValueKey('new-agent-attach'));
+
+ testWidgets('the 📎 picks files into a list under the task, and × takes '
+ 'one back off', (tester) async {
+ pickAttachmentFiles = () async => [_file('report.pdf'), _file('a.log')];
+ final (:app, conn: _) = await openNew(tester);
+
+ await tapInView(tester, attach);
+ await frames(tester);
+ expect(find.text('report.pdf'), findsOneWidget);
+ expect(find.text('a.log'), findsOneWidget);
+
+ await tapInView(
+ tester,
+ find.byKey(const ValueKey('attachment-remove-a.log')),
+ );
+ await frames(tester);
+ expect(find.text('a.log'), findsNothing);
+ expect(find.text('report.pdf'), findsOneWidget);
+ await close(tester, app);
+ });
+
+ testWidgets('no 📎 on a machine that cannot take files', (tester) async {
+ final (:app, conn: _) = await openNew(tester, takesFiles: false);
+
+ expect(attach, findsNothing);
+ await close(tester, app);
+ });
+
+ testWidgets('Start with files creates the harness without its task, which '
+ 'follows the files, and opens it', (tester) async {
+ pickAttachmentFiles = () async => [_file('report.pdf')];
+ final (:app, :conn) = await openNew(tester);
+ await tapInView(tester, attach);
+ await frames(tester);
+ await tester.enterText(
+ find.widgetWithText(TextField, 'What should it do?'),
+ 'Summarise the report',
+ );
+ await frames(tester);
+
+ await tapInView(tester, find.text('Start'));
+ for (
+ var i = 0;
+ i < 120 && find.byType(AgentSwipeHost).evaluate().isEmpty;
+ i++
+ ) {
+ await tester.pump(const Duration(milliseconds: 250));
+ }
+
+ final [sent] = conn.payloads['agent_create']!;
+ expect(sent.containsKey('prompt'), isFalse, reason: 'the files go first');
+ expect(sent['name'], isNotNull, reason: 'still named by its task');
+ expect(find.byType(AgentSwipeHost), findsOneWidget);
+ // This machine never hands the terminal over, so the files wait out their time and say so.
+ await tester.pump(kAttachedTaskOpenTimeout);
+ await frames(tester);
+ expect(
+ find.text(
+ 'The harness did not open in time. Attach the files in its pane.',
+ ),
+ findsOneWidget,
+ );
+ await close(tester, app);
+ });
+
+ testWidgets('Start without files sends the task at launch, as before', (
+ tester,
+ ) async {
+ final (:app, :conn) = await openNew(tester);
+ await tester.enterText(
+ find.widgetWithText(TextField, 'What should it do?'),
+ 'Summarise the report',
+ );
+ await frames(tester);
+
+ await tapInView(tester, find.text('Start'));
+ await frames(tester, count: 20);
+
+ final [sent] = conn.payloads['agent_create']!;
+ expect(sent['prompt'], 'Summarise the report');
+ await close(tester, app);
+ });
+
+ testWidgets('files left on a machine that can no longer take them are '
+ 'said, and nothing starts', (tester) async {
+ pickAttachmentFiles = () async => [_file('report.pdf')];
+ final (:app, :conn) = await openNew(tester);
+ await tapInView(tester, attach);
+ await frames(tester);
+ app.stateOf('m')!.terminalPasteFileAvailable = false;
+ app.notifyListeners();
+ await frames(tester);
+ expect(
+ find.text('report.pdf'),
+ findsOneWidget,
+ reason: 'still removable',
+ );
+
+ await tapInView(tester, find.text('Start'));
+ await frames(tester);
+
+ expect(
+ find.textContaining('to attach files, or remove them.'),
+ findsOneWidget,
+ );
+ expect(conn.payloads['agent_create'], isNull);
+ await close(tester, app);
+ });
+
+ // The task field raises the keyboard, and the list sits under it: on a small phone the form
+ // still scrolls to Start rather than overflowing.
+ testWidgets('with files and the keyboard up on a small phone, Start is '
+ 'still reached', (tester) async {
+ pickAttachmentFiles = () async => [
+ for (var i = 0; i < 6; i++) _file('file-$i.log'),
+ ];
+ final (:app, conn: _) = await openNew(tester);
+ setPhone(tester, smallPhone);
+ await frames(tester);
+ await tapInView(tester, attach);
+ await frames(tester);
+ raiseKeyboard(tester);
+ await frames(tester);
+
+ expect(tester.takeException(), isNull, reason: 'no overflow');
+ final start = find.text('Start');
+ await tester.ensureVisible(start);
+ await tester.pump();
+ expect(
+ tester.getRect(start).bottom,
+ lessThanOrEqualTo(smallPhone.size.height - smallPhone.keyboard),
+ );
+ lowerKeyboard(tester);
+ await frames(tester);
+ await close(tester, app);
+ });
+ });
+}