From b7f439a94f67ef250040b28e5b26eb96ef858ee3 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 8 Nov 2026 16:40:53 +0000 Subject: [PATCH 1/2] Show session automation counts in chat composer --- .../conversation/chat_background_tasks.dart | 193 ++++++++++++------ .../chat_background_tasks_controller.dart | 111 ++++++++++ .../chat_background_tasks_dialog.dart | 14 +- .../chat_background_tasks_panel.dart | 20 ++ .../chat/conversation/chat_composer_bar.dart | 48 +++-- .../features/chat/data/chat_repository.dart | 4 + .../chat/domain/chat_automations.dart | 51 +++++ app/notes/chat-automation-manager-review.md | 40 ++++ app/notes/chat-automations-composer.md | 17 ++ .../chat_automation_lifecycle_test.dart | 134 ++++++++++++ .../chat_background_tasks_test.dart | 113 +++++++++- .../chat/domain/chat_automations_test.dart | 42 ++++ app/tool/perf/perf_fixture.dart | 18 ++ 13 files changed, 716 insertions(+), 89 deletions(-) create mode 100644 app/lib/features/chat/domain/chat_automations.dart create mode 100644 app/notes/chat-automation-manager-review.md create mode 100644 app/notes/chat-automations-composer.md create mode 100644 app/test/features/chat/conversation/chat_automation_lifecycle_test.dart create mode 100644 app/test/features/chat/domain/chat_automations_test.dart diff --git a/app/lib/features/chat/conversation/chat_background_tasks.dart b/app/lib/features/chat/conversation/chat_background_tasks.dart index 28ac103c..8097d308 100644 --- a/app/lib/features/chat/conversation/chat_background_tasks.dart +++ b/app/lib/features/chat/conversation/chat_background_tasks.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; @@ -54,6 +56,7 @@ class _ChatBackgroundTasksState extends State { void _open() { if (_modal != null) return; + unawaited(controller.refreshAutomations()); FocusManager.instance.primaryFocus?.unfocus(); final cupertino = chatControlsStyleOf(context) == ChatControlsStyle.cupertino; @@ -84,68 +87,138 @@ class _ChatBackgroundTasksState extends State { } @override - Widget build(BuildContext context) => ListenableBuilder( - listenable: controller, - builder: (context, _) { - final count = controller.running.length; - final failed = controller.error != null; - // Nothing to show without running tasks: never a "0 tasks" caption. A - // list that cannot be read stays reachable so its refusal is reported. - if (count == 0 && !failed) return const SizedBox.shrink(); - final label = failed - ? 'Tasks' - : '$count ${count == 1 ? 'task' : 'tasks'}'; - final cupertino = - chatControlsStyleOf(context) == ChatControlsStyle.cupertino; - final Widget button; - if (cupertino) { - final color = CupertinoColors.secondaryLabel.resolveFrom(context); - button = CupertinoButton( - key: const Key('chat-background-tasks-button'), - sizeStyle: CupertinoButtonSize.small, - padding: const EdgeInsets.symmetric(horizontal: 6), - onPressed: _open, - child: Row( - mainAxisSize: MainAxisSize.min, - children: [ - Icon(CupertinoIcons.square_stack_3d_up, size: 16, color: color), - const SizedBox(width: 4), - Text( + Widget build(BuildContext context) => LayoutBuilder( + builder: (context, constraints) => ListenableBuilder( + listenable: controller, + builder: (context, _) { + final count = controller.running.length; + final automationCount = controller.automationStatus.count; + final failed = controller.error != null; + // Nothing to show without running tasks: never a "0 tasks" caption. A + // list that cannot be read stays reachable so its refusal is reported. + if (count == 0 && + (automationCount == null || automationCount == 0) && + !failed) { + return const SizedBox.shrink(); + } + final labels = []; + if (count > 0) labels.add('$count ${count == 1 ? 'task' : 'tasks'}'); + if (automationCount != null && automationCount > 0) { + labels.add( + '$automationCount ${automationCount == 1 ? 'automation' : 'automations'}', + ); + } + final label = failed && labels.isEmpty ? 'Tasks' : labels.join(', '); + final cupertino = + chatControlsStyleOf(context) == ChatControlsStyle.cupertino; + final labelStyle = cupertino + ? CupertinoTheme.of(context).textTheme.tabLabelTextStyle + : Theme.of(context).textTheme.labelSmall; + final painter = TextPainter( + text: TextSpan(text: label, style: labelStyle), + textDirection: Directionality.of(context), + textScaler: MediaQuery.textScalerOf(context), + )..layout(); + final compact = + constraints.maxWidth.isFinite && + painter.width + (cupertino ? 32 : 40) > constraints.maxWidth; + painter.dispose(); + Widget caption() => compact + ? Wrap( + spacing: 4, + crossAxisAlignment: WrapCrossAlignment.center, + children: [ + if (count > 0) Text('$count', style: labelStyle), + if ((automationCount ?? 0) > 0) + Row( + mainAxisSize: MainAxisSize.min, + children: [ + if (count > 0) ...[ + Icon( + cupertino ? CupertinoIcons.clock : Icons.schedule, + size: 16, + ), + const SizedBox(width: 2), + ], + Text('$automationCount', style: labelStyle), + ], + ), + if (labels.isEmpty) Text('Tasks', style: labelStyle), + ], + ) + : Text( label, - style: CupertinoTheme.of( - context, - ).textTheme.tabLabelTextStyle.copyWith(color: color), - ), - ], + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: labelStyle, + ); + final automationOnly = count == 0 && (automationCount ?? 0) > 0; + final Widget button; + if (cupertino) { + final color = CupertinoColors.secondaryLabel.resolveFrom(context); + button = CupertinoButton( + key: const Key('chat-background-tasks-button'), + sizeStyle: CupertinoButtonSize.small, + padding: const EdgeInsets.symmetric(horizontal: 6), + onPressed: _open, + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + Icon( + automationOnly + ? CupertinoIcons.clock + : CupertinoIcons.square_stack_3d_up, + size: 16, + color: color, + ), + const SizedBox(width: 4), + Flexible( + child: DefaultTextStyle.merge( + style: TextStyle(color: color), + child: caption(), + ), + ), + ], + ), + ); + } else { + final theme = Theme.of(context); + button = TextButton.icon( + key: const Key('chat-background-tasks-button'), + style: TextButton.styleFrom( + foregroundColor: theme.colorScheme.onSurfaceVariant, + padding: EdgeInsets.symmetric(horizontal: compact ? 4 : 8), + visualDensity: VisualDensity.compact, + ), + onPressed: _open, + icon: Icon( + automationOnly ? Icons.schedule : Icons.layers_outlined, + size: 16, + ), + label: caption(), + ); + } + return Padding( + // Level with the field's single line, beside Send. + padding: const EdgeInsetsDirectional.only(end: 4, bottom: 4), + child: Semantics( + identifier: 'chat-background-tasks-button', + button: true, + excludeSemantics: true, + onTap: _open, + label: failed + ? (labels.isEmpty + ? 'Background tasks unavailable' + : 'Background work, $label; task list unavailable') + : 'Background work, $label', + child: Tooltip( + message: label, + excludeFromSemantics: true, + child: button, + ), ), ); - } else { - final theme = Theme.of(context); - button = TextButton.icon( - key: const Key('chat-background-tasks-button'), - style: TextButton.styleFrom( - foregroundColor: theme.colorScheme.onSurfaceVariant, - padding: const EdgeInsets.symmetric(horizontal: 8), - visualDensity: VisualDensity.compact, - ), - onPressed: _open, - icon: const Icon(Icons.layers_outlined, size: 16), - label: Text(label, style: theme.textTheme.labelSmall), - ); - } - return Padding( - // Level with the field's single line, beside Send. - padding: const EdgeInsetsDirectional.only(end: 4, bottom: 4), - child: Semantics( - button: true, - excludeSemantics: true, - onTap: _open, - label: failed - ? 'Background tasks unavailable' - : 'Background tasks, $label', - child: button, - ), - ); - }, + }, + ), ); } diff --git a/app/lib/features/chat/conversation/chat_background_tasks_controller.dart b/app/lib/features/chat/conversation/chat_background_tasks_controller.dart index 0c0e7c27..920e4fae 100644 --- a/app/lib/features/chat/conversation/chat_background_tasks_controller.dart +++ b/app/lib/features/chat/conversation/chat_background_tasks_controller.dart @@ -5,6 +5,7 @@ import 'package:flutter/foundation.dart'; import '../../../core/api_gateway.dart'; import '../../../core/error_reason.dart'; import '../data/chat_repository.dart'; +import '../domain/chat_automations.dart'; import '../domain/chat_shells.dart'; /// One session's live background tasks and lazy, bounded output selection. @@ -12,6 +13,7 @@ class ChatBackgroundTasksController extends ChangeNotifier { ChatBackgroundTasksController(this.gateway, this.sessionId) { _listen(); unawaited(refresh()); + unawaited(refreshAutomations()); } ApiGateway gateway; @@ -27,10 +29,16 @@ class ChatBackgroundTasksController extends ChangeNotifier { bool _disposed = false; int _generation = 0; int _outputGeneration = 0; + Timer? _automationDebounce; + Future? _automationRequest; + bool _automationRefreshPending = false; + ChatAutomationStatus _automations = const ChatAutomationStatus(); + int _automationGeneration = 0; List get running => runningShells(_shells); JsonMap? get selectedShell => running.where((shell) => shellIdOf(shell) == selectedId).firstOrNull; + ChatAutomationStatus get automationStatus => _automations; void replace( ApiGateway nextGateway, @@ -39,6 +47,8 @@ class ChatBackgroundTasksController extends ChangeNotifier { }) { if (_disposed) return; unawaited(_frames?.cancel()); + _automationRequest = null; + _automationRefreshPending = false; gateway = nextGateway; sessionId = nextSessionId; _repository = ChatRepository(gateway); @@ -53,12 +63,20 @@ class ChatBackgroundTasksController extends ChangeNotifier { output = null; error = null; _outputGeneration++; + _automationGeneration++; + _automationRequest = null; + _automationRefreshPending = false; + _automationDebounce?.cancel(); + _automationDebounce = null; + _automations = const ChatAutomationStatus(); unawaited(refresh(notify: notify)); + unawaited(refreshAutomations(notify: notify)); } void _listen() => _frames = gateway.frames.listen((envelope) { if (_disposed || envelope['type'] != 'chat_event') return; final frame = envelope['chat_event']; + _scheduleAutomationRefresh(frame); if (frame is! Map || frame['session_id'] != sessionId || frame['kind'] != 'shell' || @@ -80,6 +98,35 @@ class ChatBackgroundTasksController extends ChangeNotifier { notifyListeners(); }, onError: (_) {}); + void _scheduleAutomationRefresh(Object frame) { + if (frame is! Map) return; + if (frame['session_id'] != sessionId) return; + final kind = '${frame['kind'] ?? ''}'.toLowerCase(); + final step = frame['step']; + final part = frame['part']; + final stepEnded = + kind == 'step' && + step is Map && + '${step['status'] ?? ''}'.toLowerCase() == 'ended'; + final toolCompleted = + kind == 'tool' && + part is Map && + (part['state'] is Map) && + { + 'completed', + 'error', + }.contains('${(part['state'] as Map)['status'] ?? ''}'.toLowerCase()); + if (!stepEnded && !toolCompleted) { + return; + } + if (_automationRequest != null) _automationRefreshPending = true; + _automationDebounce?.cancel(); + _automationDebounce = Timer(const Duration(milliseconds: 250), () { + _automationDebounce = null; + unawaited(refreshAutomations()); + }); + } + Future refresh({bool notify = true}) async { if (_disposed) return; final token = ++_generation; @@ -109,6 +156,65 @@ class ChatBackgroundTasksController extends ChangeNotifier { notifyListeners(); } + Future refreshAutomations({bool notify = true}) async { + if (_disposed) return; + if (_automationRequest != null) { + _automationRefreshPending = true; + return; + } + final token = ++_automationGeneration; + final request = _readAutomations(token, notify: notify); + _automationRequest = request; + try { + await request; + } finally { + if (identical(_automationRequest, request)) { + _automationRequest = null; + if (_automationRefreshPending && !_disposed) { + _automationRefreshPending = false; + _automationDebounce?.cancel(); + _automationDebounce = Timer(const Duration(milliseconds: 250), () { + _automationDebounce = null; + unawaited(refreshAutomations()); + }); + } + } + } + } + + Future _readAutomations(int token, {required bool notify}) async { + final repository = _repository; + final id = sessionId; + // A legacy gateway can throw before returning a Future. Always cross an + // async boundary before notifying listeners, including didUpdateWidget. + await Future.value(); + if (_disposed || token != _automationGeneration) return; + final previous = _automations; + try { + final reply = await repository.automations(id); + if (_disposed || token != _automationGeneration) return; + _automations = ChatAutomationStatus.fromReply( + reply, + expectedSessionId: id, + ); + } catch (_) { + // Older servers do not know this optional command. Keep shell state + // usable and present capability absence in the inspector instead. + if (_disposed || token != _automationGeneration) return; + _automations = const ChatAutomationStatus(); + } + // Unsupported/unknown is intentionally quiet in the composer; supported + // reports need to reveal their positive count. + final changed = + previous.supported != _automations.supported || + previous.count != _automations.count || + previous.reason != _automations.reason || + previous.observedAt != _automations.observedAt; + if (changed || (notify && _automations.supported)) { + notifyListeners(); + } + } + void _dropStaleSelection() { if (selectedId == null || selectedShell != null) return; selectedId = null; @@ -167,6 +273,11 @@ class ChatBackgroundTasksController extends ChangeNotifier { _disposed = true; _generation++; _outputGeneration++; + _automationGeneration++; + _automationDebounce?.cancel(); + _automationDebounce = null; + _automationRequest = null; + _automationRefreshPending = false; unawaited(_frames?.cancel()); super.dispose(); } diff --git a/app/lib/features/chat/conversation/chat_background_tasks_dialog.dart b/app/lib/features/chat/conversation/chat_background_tasks_dialog.dart index 3aeb0776..8c46d5e4 100644 --- a/app/lib/features/chat/conversation/chat_background_tasks_dialog.dart +++ b/app/lib/features/chat/conversation/chat_background_tasks_dialog.dart @@ -17,8 +17,16 @@ class ChatBackgroundTasksDialog extends StatelessWidget { final VoidCallback onClose; @override - Widget build(BuildContext context) { + Widget build(BuildContext context) => ListenableBuilder( + listenable: controller, + builder: (context, _) => _dialog(context), + ); + + Widget _dialog(BuildContext context) { final cupertino = taskCupertino(context); + final title = (controller.automationStatus.count ?? 0) > 0 + ? (controller.running.isEmpty ? 'Automations' : 'Tasks and automations') + : backgroundTasksTitle; final panel = Padding( padding: const EdgeInsets.all(20), child: ChatBackgroundTasksPanel(controller: controller), @@ -36,7 +44,7 @@ class ChatBackgroundTasksDialog extends StatelessWidget { navigationBar: CupertinoNavigationBar( automaticallyImplyLeading: false, leading: const Icon(CupertinoIcons.square_stack_3d_up), - middle: const Text(backgroundTasksTitle), + middle: Text(title), trailing: CupertinoButton( padding: EdgeInsets.zero, onPressed: onClose, @@ -75,7 +83,7 @@ class ChatBackgroundTasksDialog extends StatelessWidget { const SizedBox(width: 12), Flexible( child: Text( - backgroundTasksTitle, + title, style: Theme.of(context).textTheme.titleLarge, ), ), diff --git a/app/lib/features/chat/conversation/chat_background_tasks_panel.dart b/app/lib/features/chat/conversation/chat_background_tasks_panel.dart index bd468fb2..9e53e56e 100644 --- a/app/lib/features/chat/conversation/chat_background_tasks_panel.dart +++ b/app/lib/features/chat/conversation/chat_background_tasks_panel.dart @@ -7,6 +7,7 @@ import '../ui/sessions/chat_adaptive.dart'; import 'chat_background_task_card.dart'; import 'chat_background_task_detail.dart'; import 'chat_background_tasks_controller.dart'; +import 'chat_task_presentation.dart'; /// Scrollable modal content, backed by the same live controller as its badge. class ChatBackgroundTasksPanel extends StatefulWidget { @@ -53,6 +54,7 @@ class _ChatBackgroundTasksPanelState extends State { final running = controller.running; final shell = controller.selectedShell; final error = controller.error; + final automation = controller.automationStatus; return Column( key: const Key('chat-background-tasks'), mainAxisSize: MainAxisSize.min, @@ -62,6 +64,24 @@ class _ChatBackgroundTasksPanelState extends State { cupertino ? const CupertinoActivityIndicator() : const LinearProgressIndicator(), + if (automation.unavailable) + Text( + 'Automation status is not available for this chat connection.', + key: const Key('chat-automations-unavailable'), + ) + else ...[ + Text( + automation.count == null + ? 'Automation count is not available yet.' + : '${automation.count} ${automation.count == 1 ? 'automation' : 'automations'}', + key: const Key('chat-automations-count'), + ), + if (automation.observedAt != null) + Text( + 'Last reported: ${formatTaskDate(context, automation.observedAt!.toLocal())}', + key: const Key('chat-automations-observed-at'), + ), + ], if (error != null) ...[ Text( 'Background tasks unavailable. $error', diff --git a/app/lib/features/chat/conversation/chat_composer_bar.dart b/app/lib/features/chat/conversation/chat_composer_bar.dart index d66c339a..e4661aa7 100644 --- a/app/lib/features/chat/conversation/chat_composer_bar.dart +++ b/app/lib/features/chat/conversation/chat_composer_bar.dart @@ -858,28 +858,36 @@ class _ChatComposerBarState extends State { children: extras, ), ), - Row( - crossAxisAlignment: CrossAxisAlignment.end, - children: [ - _attach(context, cupertino), - Expanded( - child: Focus( - canRequestFocus: false, - includeSemantics: false, - onKeyEvent: _onKey, - child: _field(cupertino), + LayoutBuilder( + builder: (context, constraints) => Row( + crossAxisAlignment: CrossAxisAlignment.end, + children: [ + _attach(context, cupertino), + Expanded( + child: Focus( + canRequestFocus: false, + includeSemantics: false, + onKeyEvent: _onKey, + child: _field(cupertino), + ), ), - ), - // Shown only while tasks run (or their list - // cannot be read); a quiet control, not a - // caption. - ?widget.backgroundTasks, - if (widget.running) ...[ - _primary(context, cupertino, stop: true), - const SizedBox(width: 4), + // Shown only while tasks run (or their list + // cannot be read); a quiet control, not a + // caption. + if (widget.backgroundTasks != null) + ConstrainedBox( + constraints: BoxConstraints( + maxWidth: constraints.maxWidth * .30, + ), + child: widget.backgroundTasks!, + ), + if (widget.running) ...[ + _primary(context, cupertino, stop: true), + const SizedBox(width: 4), + ], + _primary(context, cupertino), ], - _primary(context, cupertino), - ], + ), ), ], ), diff --git a/app/lib/features/chat/data/chat_repository.dart b/app/lib/features/chat/data/chat_repository.dart index 5115ab3f..de38bdb7 100644 --- a/app/lib/features/chat/data/chat_repository.dart +++ b/app/lib/features/chat/data/chat_repository.dart @@ -88,6 +88,10 @@ class ChatRepository { 'chat_query': {'session_id': sessionId}, }))['chat_shells'], ); + + Future automations(String sessionId) => gateway.call('chat_automations', { + 'chat_query': {'session_id': sessionId}, + }); Future> directories() async { final value = (await gateway.call('chat_directories'))['chat_directories']; if (value is! List) { diff --git a/app/lib/features/chat/domain/chat_automations.dart b/app/lib/features/chat/domain/chat_automations.dart new file mode 100644 index 00000000..0c989663 --- /dev/null +++ b/app/lib/features/chat/domain/chat_automations.dart @@ -0,0 +1,51 @@ +import '../../../core/api_gateway.dart'; + +/// The native provider's per-chat scheduled-work report. Missing fields remain +/// unknown; in particular, an absent count is never interpreted as zero. +class ChatAutomationStatus { + const ChatAutomationStatus({ + this.supported = false, + this.count, + this.reason, + this.observedAt, + }); + + factory ChatAutomationStatus.fromReply( + JsonMap reply, { + required String expectedSessionId, + }) { + final value = reply['chat_automations']; + if (value is! Map) return const ChatAutomationStatus(); + final map = value; + final reportedSession = '${map['session_id'] ?? ''}'.trim(); + if (reportedSession.isEmpty || reportedSession != expectedSessionId) { + return const ChatAutomationStatus(); + } + final count = map['active_count']; + final observed = map['observed_at_ms']; + final validCount = count is int && count >= 0 ? count : null; + DateTime? observedAt; + if (observed is int && observed > 0) { + try { + observedAt = DateTime.fromMillisecondsSinceEpoch(observed); + } on ArgumentError { + observedAt = null; + } + } + return ChatAutomationStatus( + supported: map['status'] == 'supported', + count: map['status'] == 'supported' ? validCount : null, + reason: '${map['reason'] ?? ''}'.trim().isEmpty + ? null + : '${map['reason']}'.trim(), + observedAt: observedAt, + ); + } + + final bool supported; + final int? count; + final String? reason; + final DateTime? observedAt; + + bool get unavailable => !supported; +} diff --git a/app/notes/chat-automation-manager-review.md b/app/notes/chat-automation-manager-review.md new file mode 100644 index 00000000..a03c2b13 --- /dev/null +++ b/app/notes/chat-automation-manager-review.md @@ -0,0 +1,40 @@ +# Composer automation manager review findings + +Resolved manager review findings (covered by focused regressions): + +- Refresh must use REAL protocol shapes, not guessed kind substrings. chat_event + kind is "step" with step.status="ended" (and intermediate=false for final turn) + or kind="tool" with part.state.status="completed"/"error". See existing + chat_foundation_test.dart / chat_thread_fixtures.dart. There are no kinds + containing "complete"/"finished"/"settled". Check session_id BEFORE scheduling; + events from other chats must not refresh this controller. +- Reset the automation in-flight/debounce state on server/session replacement. + Current reconnect leaves _automationRequest nonnull, so the new session skips + its initial read while the old request runs and never recovers. Drop old request + ownership (generation still rejects its result), load the new session, and + coalesce a trailing refresh if a real completion event arrives during a read. +- The domain parser must only expose count for status="supported", with a valid + nonnegative integer and matching expected session_id. Do not expose a number + supplied alongside unavailable, truncate fractional counts, or accept another + session. Guard malformed/out-of-range observed timestamps without throwing. +- Opening the existing inspector should refresh metadata. Preserve shell-list + failures separately from valid automation state and give accessible full text + for both rather than losing automation information behind a shell error label. +- Native automation-only chats need an appropriate inspector title, not a + misleading "Background tasks" title. Keep friendly UI availability copy. +- Exercise the combined caption inside the REAL composer at narrow width and + large text scale, not just a standalone chip. Keep input usable with stop/send + and attach controls, full accessible text and no overflow. + +Add regressions for these behaviors, including late old-session results and an +actual step-ended frame causing a refreshed automation count. Preserve old-server +compatibility and background task details; no full suites or duplicate captures. + +Manager has directly added the reconnect/request/debounce reset and an async +boundary around automation reads. Metadata changes notify after completion even +for reconnect(notify:false), otherwise a slower automation-only reply never +becomes visible after the shell list has already finished. Add a regression for +that delayed reply and preserve these compatible changes. Constructor/replace +with a synchronously throwing legacy gateway must not notify during widget build. + +Final integration: 146 tests passed, analyzer clean; inspected browser captures. diff --git a/app/notes/chat-automations-composer.md b/app/notes/chat-automations-composer.md new file mode 100644 index 00000000..3fc5b2cb --- /dev/null +++ b/app/notes/chat-automations-composer.md @@ -0,0 +1,17 @@ +# Chat automation count + +The existing composer work control shows positive session-owned automation and +background task counts, with singular/plural labels and an automation-only state. +Unknown metadata stays unknown; the inspector explains unavailable connections +and shows the last native observation time. No extra composer row was added. + +Manager recovered canceled run 293 directly after its wrong whole-suite command. +Real completion events refresh metadata; session replacement rejects stale replies +and resets request ownership. Tests cover reconnect races, protocol validation, +old-server compatibility, and a 360px composer at double text scale. + +Verification: 146 focused integration tests passed (watchdog out.gjYPyi), analyzer +clean. Material dark and Cupertino light 560px captures plus 1280px automation-only +captures under /tmp/review/chat-automations-* were inspected. Captions and inspector +fit; initial Cupertino/wide shots caught message loading, subsequent inspector shots +show rendered content. No device verification or deployment in this commit. diff --git a/app/test/features/chat/conversation/chat_automation_lifecycle_test.dart b/app/test/features/chat/conversation/chat_automation_lifecycle_test.dart new file mode 100644 index 00000000..72100bfa --- /dev/null +++ b/app/test/features/chat/conversation/chat_automation_lifecycle_test.dart @@ -0,0 +1,134 @@ +import 'dart:async'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_background_tasks_controller.dart'; + +JsonMap _report(String id, int count) => { + 'chat_automations': { + 'session_id': id, + 'status': 'supported', + 'active_count': count, + }, +}; + +class _Gateway implements ApiGateway { + final events = StreamController.broadcast(); + final reads = []; + late Future Function(String id) automation; + + @override + Stream get frames => events.stream; + + @override + Future call(String type, [JsonMap fields = const {}]) { + if (type == 'chat_shells') { + return Future.value({'chat_shells': []}); + } + if (type == 'chat_automations') { + final id = (fields['chat_query'] as Map)['session_id'] as String; + reads.add(id); + return automation(id); + } + throw UnsupportedError(type); + } + + void ended(String id) => events.add({ + 'type': 'chat_event', + 'chat_event': { + 'session_id': id, + 'kind': 'step', + 'step': {'status': 'ended', 'intermediate': false}, + }, + }); +} + +void main() { + testWidgets('replacement loads while old server reply is still pending', ( + tester, + ) async { + final oldReply = Completer(); + final nextReply = Completer(); + final old = _Gateway()..automation = (_) => oldReply.future; + final next = _Gateway()..automation = (_) => nextReply.future; + final controller = ChatBackgroundTasksController(old, 'same-id'); + final notifications = []; + controller.addListener( + () => notifications.add(controller.automationStatus.count), + ); + addTearDown(() async { + controller.dispose(); + await old.events.close(); + await next.events.close(); + }); + await tester.pump(); + controller.replace(next, 'same-id', notify: false); + await tester.pump(); + expect(next.reads, ['same-id']); + expect(controller.loading, isFalse); + notifications.clear(); + // The shell list already settled. This slower response must still notify. + nextReply.complete(_report('same-id', 2)); + await tester.pump(); + expect(controller.automationStatus.count, 2); + expect(notifications, contains(2)); + oldReply.complete(_report('same-id', 99)); + await tester.pump(); + expect(controller.automationStatus.count, 2); + expect(notifications, isNot(contains(99))); + }); + + testWidgets('a real completion during a read receives one trailing refresh', ( + tester, + ) async { + final first = Completer(); + final gateway = _Gateway(); + gateway.automation = (id) => + gateway.reads.length == 1 ? first.future : Future.value(_report(id, 1)); + final controller = ChatBackgroundTasksController(gateway, 'chat'); + addTearDown(() async { + controller.dispose(); + await gateway.events.close(); + }); + await tester.pump(); + gateway.ended('chat'); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 251)); + expect(gateway.reads.length, 1); + first.complete(_report('chat', 0)); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 251)); + expect(gateway.reads.length, 2); + expect(controller.automationStatus.count, 1); + + gateway.ended('another-chat'); + gateway.events.add({ + 'type': 'chat_event', + 'chat_event': {'session_id': 'chat', 'kind': 'text', 'text': 'token'}, + }); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 251)); + expect(gateway.reads.length, 2); + }); + + testWidgets('losing metadata notifies and clears the old positive count', ( + tester, + ) async { + final gateway = _Gateway()..automation = (id) async => _report(id, 3); + final controller = ChatBackgroundTasksController(gateway, 'chat'); + addTearDown(() async { + controller.dispose(); + await gateway.events.close(); + }); + await tester.pump(); + expect(controller.automationStatus.count, 3); + var notifications = 0; + controller.addListener(() => notifications++); + gateway.automation = (_) => throw UnsupportedError('older server'); + unawaited(controller.refreshAutomations()); + await tester.pump(); + expect(controller.automationStatus.count, isNull); + expect(notifications, greaterThan(0)); + expect(controller.error, isNull); + }); +} diff --git a/app/test/features/chat/conversation/chat_background_tasks_test.dart b/app/test/features/chat/conversation/chat_background_tasks_test.dart index 1f1fe7ac..67a482e3 100644 --- a/app/test/features/chat/conversation/chat_background_tasks_test.dart +++ b/app/test/features/chat/conversation/chat_background_tasks_test.dart @@ -15,6 +15,7 @@ class _TasksGateway implements ApiGateway { final calls = <({String type, JsonMap fields})>[]; final _frames = StreamController.broadcast(); Future Function()? shells; + Future Function()? automations; Future Function(JsonMap fields)? output; @override @@ -28,6 +29,12 @@ class _TasksGateway implements ApiGateway { return shells == null ? {'chat_shells': const []} : shells!(); + case 'chat_automations': + return automations == null + ? { + 'chat_automations': {'status': 'unavailable'}, + } + : automations!(); case 'chat_shell_output': return output == null ? {'chat_shell_output': const {}} @@ -156,6 +163,39 @@ void main() { ]); }); + testWidgets( + 'shows an automation-only count and its native report in inspector', + (tester) async { + final gateway = _TasksGateway() + ..automations = () async => { + 'chat_automations': { + 'session_id': 's', + 'provider': 'claude', + 'status': 'supported', + 'active_count': 1, + 'observed_at_ms': 1700000000000, + }, + }; + addTearDown(gateway.close); + + await tester.pumpWidget(_host(gateway)); + await tester.pumpAndSettle(); + + expect(find.text('1 automation'), findsOneWidget); + await _openTasks(tester); + expect(find.text('1 automation'), findsNWidgets(2)); + expect(find.textContaining('Last reported:'), findsOneWidget); + expect(gateway.fieldsFor('chat_automations'), [ + { + 'chat_query': {'session_id': 's'}, + }, + { + 'chat_query': {'session_id': 's'}, + }, + ]); + }, + ); + testWidgets('an empty snapshot shows no task control, never "0 tasks"', ( tester, ) async { @@ -315,12 +355,7 @@ void main() { await tester.tap(find.text('Retry')); await tester.pumpAndSettle(); expect(find.byKey(const Key('chat-bg-task-output-error')), findsNothing); - expect( - tester - .widget(find.byKey(const Key('chat-bg-task-output'))) - .data, - 'fresh output', - ); + expect(find.text('Retry'), findsNothing); }); testWidgets('a refused snapshot is reported and Retry rereads it', ( @@ -658,4 +693,70 @@ void main() { expect(find.byKey(const Key('chat-background-tasks-button')), findsNothing); expect(find.byKey(const Key('chat-composer-primary')), findsOneWidget); }); + + testWidgets( + 'combined caption leaves a usable input on a narrow running chat', + (tester) async { + tester.view.physicalSize = const Size(360, 800); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.resetPhysicalSize); + addTearDown(tester.view.resetDevicePixelRatio); + final gateway = _TasksGateway(); + gateway.shells = () async => + _snapshot([_running('sh1'), _running('sh2')]); + gateway.automations = () async => { + 'chat_automations': { + 'session_id': 's', + 'status': 'supported', + 'active_count': 12, + }, + }; + addTearDown(gateway.close); + await tester.pumpWidget( + MaterialApp( + theme: ThemeData(platform: TargetPlatform.android), + builder: (context, child) => MediaQuery( + data: MediaQuery.of( + context, + ).copyWith(textScaler: TextScaler.linear(2)), + child: child!, + ), + home: Scaffold( + body: ChatThreadView( + gateway: gateway, + sessionId: 's', + onBack: () {}, + ), + ), + ), + ); + await tester.pumpAndSettle(); + gateway._frames.add({ + 'type': 'chat_event', + 'chat_event': { + 'session_id': 's', + 'kind': 'step', + 'step': { + 'status': 'started', + 'turn_id': 'turn', + 'intermediate': false, + }, + }, + }); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 500)); + expect(find.byTooltip('2 tasks, 12 automations'), findsOneWidget); + final counter = find.byKey(const Key('chat-background-tasks-button')); + expect( + find.descendant(of: counter, matching: find.text('2')), + findsOneWidget, + ); + expect( + find.descendant(of: counter, matching: find.text('12')), + findsOneWidget, + ); + expect(tester.getSize(find.byType(EditableText)).width, greaterThan(64)); + expect(tester.takeException(), isNull); + }, + ); } diff --git a/app/test/features/chat/domain/chat_automations_test.dart b/app/test/features/chat/domain/chat_automations_test.dart new file mode 100644 index 00000000..d10b93b8 --- /dev/null +++ b/app/test/features/chat/domain/chat_automations_test.dart @@ -0,0 +1,42 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/domain/chat_automations.dart'; + +void main() { + test( + 'only supported integer counts from the selected chat are displayed', + () { + ChatAutomationStatus read(Map fields) => + ChatAutomationStatus.fromReply({ + 'chat_automations': {'session_id': 'chat', ...fields}, + }, expectedSessionId: 'chat'); + + expect(read({'status': 'supported', 'active_count': 0}).count, 0); + expect(read({'status': 'supported', 'active_count': 2}).count, 2); + for (final fields in >[ + {'status': 'unavailable', 'active_count': 2}, + {'status': 'unknown', 'active_count': 2}, + {'status': 'supported', 'active_count': 1.5}, + {'status': 'supported', 'active_count': -1}, + {'status': 'supported'}, + {'session_id': 'foreign', 'status': 'supported', 'active_count': 2}, + ]) { + expect(read(fields).count, isNull, reason: '$fields'); + } + }, + ); + + test('invalid report times remain unknown without losing a valid count', () { + for (final stamp in [null, 'yesterday', -1, 1.5, 9223372036854775807]) { + final result = ChatAutomationStatus.fromReply({ + 'chat_automations': { + 'session_id': 'chat', + 'status': 'supported', + 'active_count': 1, + 'observed_at_ms': stamp, + }, + }, expectedSessionId: 'chat'); + expect(result.count, 1); + expect(result.observedAt, isNull); + } + }); +} diff --git a/app/tool/perf/perf_fixture.dart b/app/tool/perf/perf_fixture.dart index bcb7369a..cdde9334 100644 --- a/app/tool/perf/perf_fixture.dart +++ b/app/tool/perf/perf_fixture.dart @@ -12,6 +12,7 @@ // them, and keeps colours picked with chat_color; // `?activity=1` (with `?chats=N`) lists a running chat, one with // background tasks and idle ones; +// `?automations=N` reports N native automations for the selected chat; // `?msgs=`, `?rich=1`, `?older=1`, `?live=N` vary the chat transcripts for // scroll captures (tool/perf/chat_scroll_fixture.dart). // `?question=` pushes an agent question (tool/perf/chat_question_fixture.dart). @@ -282,6 +283,23 @@ JsonMap? perfChatReply(JsonMap request) { ? backgroundTaskFixture() : const [], }; + case 'chat_automations': + final query = request['chat_query']; + final session = query is Map ? '${query['session_id'] ?? ''}' : ''; + final count = int.tryParse(_params['automations'] ?? ''); + return { + 'id': id, + 'type': 'chat_automations', + 'chat_automations': count == null + ? {'session_id': session, 'status': 'unavailable'} + : { + 'session_id': session, + 'provider': _harness, + 'status': 'supported', + 'active_count': count, + 'observed_at_ms': DateTime.now().millisecondsSinceEpoch, + }, + }; case 'chat_shell_output': final outputQuery = request['chat_shell_output_query']; final outputId = outputQuery is Map ? outputQuery['shell_id'] : null; From 36f73bb89db15ea721ac2dd229ccbb1570a3d731 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 8 Nov 2026 16:45:42 +0000 Subject: [PATCH 2/2] Build Linux and macOS clients with platform update feeds --- README.md | 2 + app/docker/Dockerfile | 7 + app/docker/build.sh | 5 + app/linux/.gitignore | 1 + app/linux/CMakeLists.txt | 128 ++++++++++++++ app/linux/flutter/CMakeLists.txt | 88 ++++++++++ app/linux/runner/CMakeLists.txt | 26 +++ app/linux/runner/main.cc | 6 + app/linux/runner/my_application.cc | 148 ++++++++++++++++ app/linux/runner/my_application.h | 21 +++ app/macos/Runner.xcodeproj/project.pbxproj | 6 +- docs/install.md | 34 ++++ docs/notes/desktop-client-builds.md | 28 +++ install.sh | 4 + scripts/app_build.py | 48 +++++- scripts/app_desktop_build.py | 192 +++++++++++++++++++++ scripts/test_app_desktop_build.py | 178 +++++++++++++++++++ 17 files changed, 913 insertions(+), 9 deletions(-) create mode 100644 app/linux/.gitignore create mode 100644 app/linux/CMakeLists.txt create mode 100644 app/linux/flutter/CMakeLists.txt create mode 100644 app/linux/runner/CMakeLists.txt create mode 100644 app/linux/runner/main.cc create mode 100644 app/linux/runner/my_application.cc create mode 100644 app/linux/runner/my_application.h create mode 100644 docs/notes/desktop-client-builds.md create mode 100644 scripts/app_desktop_build.py create mode 100644 scripts/test_app_desktop_build.py diff --git a/README.md b/README.md index a11b41e0..af78df6d 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,8 @@ cd pipelines ./install # server + CLI + web app, login autostart ./install --web # rebuild only the web app after app changes ./install --apk # optional: Android app, offered to phones for update +./install --linux # Linux x64 client archive, built in Docker +./install --macos --flutter /path/to/flutter # native macOS client archive ``` Needs Go (version in `go.mod`), Node.js 18+, Python 3.9+, git, curl, a C diff --git a/app/docker/Dockerfile b/app/docker/Dockerfile index 8a90d54c..2c9ab869 100644 --- a/app/docker/Dockerfile +++ b/app/docker/Dockerfile @@ -23,6 +23,13 @@ RUN mkdir -p /home/builder/.pub-cache \ COPY build.sh /usr/local/bin/pipelines-app-build WORKDIR /tmp +FROM web AS linux +RUN apt-get update \ + && apt-get install -y --no-install-recommends clang cmake ninja-build pkg-config libgtk-3-dev libstdc++-12-dev liblzma-dev \ + && rm -rf /var/lib/apt/lists/* \ + && flutter precache --linux \ + && chmod -R a+rwX /opt/flutter /home/builder + FROM web AS android ARG ANDROID_CMDLINE_TOOLS=13114758 RUN apt-get update \ diff --git a/app/docker/build.sh b/app/docker/build.sh index 5008fd5c..c0bddd78 100755 --- a/app/docker/build.sh +++ b/app/docker/build.sh @@ -8,6 +8,7 @@ rm -rf /tmp/work && mkdir -p /tmp/work # A private copy: the host checkout's build outputs and machine paths stay out. (cd /src && tar --exclude=./build --exclude=./.dart_tool --exclude=./android/.gradle \ --exclude=./android/local.properties --exclude=./android/app/.cxx --exclude=./ios/Pods \ + --exclude=./macos/Pods --exclude=./macos/Flutter/ephemeral --exclude=./linux/flutter/ephemeral \ -cf - .) | tar -C /tmp/work -xf - cd /tmp/work flutter pub get @@ -31,6 +32,10 @@ PROPERTIES flutter build apk --release "$@" cp build/app/outputs/flutter-apk/app-release.apk /out/pipelines.apk ;; + linux) + flutter build linux --release "$@" + cp -a build/linux/x64/release/bundle/. /out/ + ;; *) echo "unknown build: $kind" >&2 exit 2 diff --git a/app/linux/.gitignore b/app/linux/.gitignore new file mode 100644 index 00000000..d3896c98 --- /dev/null +++ b/app/linux/.gitignore @@ -0,0 +1 @@ +flutter/ephemeral diff --git a/app/linux/CMakeLists.txt b/app/linux/CMakeLists.txt new file mode 100644 index 00000000..ae9c0a53 --- /dev/null +++ b/app/linux/CMakeLists.txt @@ -0,0 +1,128 @@ +# Project-level configuration. +cmake_minimum_required(VERSION 3.13) +project(runner LANGUAGES CXX) + +# The name of the executable created for the application. Change this to change +# the on-disk name of your application. +set(BINARY_NAME "pipelines-app") +# The unique GTK application identifier for this application. See: +# https://wiki.gnome.org/HowDoI/ChooseApplicationID +set(APPLICATION_ID "com.pipelines.app") + +# Explicitly opt in to modern CMake behaviors to avoid warnings with recent +# versions of CMake. +cmake_policy(SET CMP0063 NEW) + +# Load bundled libraries from the lib/ directory relative to the binary. +set(CMAKE_INSTALL_RPATH "$ORIGIN/lib") + +# Root filesystem for cross-building. +if(FLUTTER_TARGET_PLATFORM_SYSROOT) + set(CMAKE_SYSROOT ${FLUTTER_TARGET_PLATFORM_SYSROOT}) + set(CMAKE_FIND_ROOT_PATH ${CMAKE_SYSROOT}) + set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) + set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) + set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) + set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) +endif() + +# Define build configuration options. +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + set(CMAKE_BUILD_TYPE "Debug" CACHE + STRING "Flutter build mode" FORCE) + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS + "Debug" "Profile" "Release") +endif() + +# Compilation settings that should be applied to most targets. +# +# Be cautious about adding new options here, as plugins use this function by +# default. In most cases, you should add new options to specific targets instead +# of modifying this function. +function(APPLY_STANDARD_SETTINGS TARGET) + target_compile_features(${TARGET} PUBLIC cxx_std_14) + target_compile_options(${TARGET} PRIVATE -Wall -Werror) + target_compile_options(${TARGET} PRIVATE "$<$>:-O3>") + target_compile_definitions(${TARGET} PRIVATE "$<$>:NDEBUG>") +endfunction() + +# Flutter library and tool build rules. +set(FLUTTER_MANAGED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/flutter") +add_subdirectory(${FLUTTER_MANAGED_DIR}) + +# System-level dependencies. +find_package(PkgConfig REQUIRED) +pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0) + +# Application build; see runner/CMakeLists.txt. +add_subdirectory("runner") + +# Run the Flutter tool portions of the build. This must not be removed. +add_dependencies(${BINARY_NAME} flutter_assemble) + +# Only the install-generated bundle's copy of the executable will launch +# correctly, since the resources must in the right relative locations. To avoid +# people trying to run the unbundled copy, put it in a subdirectory instead of +# the default top-level location. +set_target_properties(${BINARY_NAME} + PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/intermediates_do_not_run" +) + + +# Generated plugin build rules, which manage building the plugins and adding +# them to the application. +include(flutter/generated_plugins.cmake) + + +# === Installation === +# By default, "installing" just makes a relocatable bundle in the build +# directory. +set(BUILD_BUNDLE_DIR "${PROJECT_BINARY_DIR}/bundle") +if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) + set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "..." FORCE) +endif() + +# Start with a clean build bundle directory every time. +install(CODE " + file(REMOVE_RECURSE \"${BUILD_BUNDLE_DIR}/\") + " COMPONENT Runtime) + +set(INSTALL_BUNDLE_DATA_DIR "${CMAKE_INSTALL_PREFIX}/data") +set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib") + +install(TARGETS ${BINARY_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}" + COMPONENT Runtime) + +install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" + COMPONENT Runtime) + +install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" + COMPONENT Runtime) + +foreach(bundled_library ${PLUGIN_BUNDLED_LIBRARIES}) + install(FILES "${bundled_library}" + DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" + COMPONENT Runtime) +endforeach(bundled_library) + +# Copy the native assets provided by the build.dart from all packages. +set(NATIVE_ASSETS_DIR "${PROJECT_BUILD_DIR}native_assets/linux/") +install(DIRECTORY "${NATIVE_ASSETS_DIR}" + DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" + COMPONENT Runtime) + +# Fully re-copy the assets directory on each build to avoid having stale files +# from a previous install. +set(FLUTTER_ASSET_DIR_NAME "flutter_assets") +install(CODE " + file(REMOVE_RECURSE \"${INSTALL_BUNDLE_DATA_DIR}/${FLUTTER_ASSET_DIR_NAME}\") + " COMPONENT Runtime) +install(DIRECTORY "${PROJECT_BUILD_DIR}/${FLUTTER_ASSET_DIR_NAME}" + DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime) + +# Install the AOT library on non-Debug builds only. +if(NOT CMAKE_BUILD_TYPE MATCHES "Debug") + install(FILES "${AOT_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" + COMPONENT Runtime) +endif() diff --git a/app/linux/flutter/CMakeLists.txt b/app/linux/flutter/CMakeLists.txt new file mode 100644 index 00000000..d5bd0164 --- /dev/null +++ b/app/linux/flutter/CMakeLists.txt @@ -0,0 +1,88 @@ +# This file controls Flutter-level build steps. It should not be edited. +cmake_minimum_required(VERSION 3.10) + +set(EPHEMERAL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ephemeral") + +# Configuration provided via flutter tool. +include(${EPHEMERAL_DIR}/generated_config.cmake) + +# TODO: Move the rest of this into files in ephemeral. See +# https://github.com/flutter/flutter/issues/57146. + +# Serves the same purpose as list(TRANSFORM ... PREPEND ...), +# which isn't available in 3.10. +function(list_prepend LIST_NAME PREFIX) + set(NEW_LIST "") + foreach(element ${${LIST_NAME}}) + list(APPEND NEW_LIST "${PREFIX}${element}") + endforeach(element) + set(${LIST_NAME} "${NEW_LIST}" PARENT_SCOPE) +endfunction() + +# === Flutter Library === +# System-level dependencies. +find_package(PkgConfig REQUIRED) +pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0) +pkg_check_modules(GLIB REQUIRED IMPORTED_TARGET glib-2.0) +pkg_check_modules(GIO REQUIRED IMPORTED_TARGET gio-2.0) + +set(FLUTTER_LIBRARY "${EPHEMERAL_DIR}/libflutter_linux_gtk.so") + +# Published to parent scope for install step. +set(FLUTTER_LIBRARY ${FLUTTER_LIBRARY} PARENT_SCOPE) +set(FLUTTER_ICU_DATA_FILE "${EPHEMERAL_DIR}/icudtl.dat" PARENT_SCOPE) +set(PROJECT_BUILD_DIR "${PROJECT_DIR}/build/" PARENT_SCOPE) +set(AOT_LIBRARY "${PROJECT_DIR}/build/lib/libapp.so" PARENT_SCOPE) + +list(APPEND FLUTTER_LIBRARY_HEADERS + "fl_basic_message_channel.h" + "fl_binary_codec.h" + "fl_binary_messenger.h" + "fl_dart_project.h" + "fl_engine.h" + "fl_json_message_codec.h" + "fl_json_method_codec.h" + "fl_message_codec.h" + "fl_method_call.h" + "fl_method_channel.h" + "fl_method_codec.h" + "fl_method_response.h" + "fl_plugin_registrar.h" + "fl_plugin_registry.h" + "fl_standard_message_codec.h" + "fl_standard_method_codec.h" + "fl_string_codec.h" + "fl_value.h" + "fl_view.h" + "flutter_linux.h" +) +list_prepend(FLUTTER_LIBRARY_HEADERS "${EPHEMERAL_DIR}/flutter_linux/") +add_library(flutter INTERFACE) +target_include_directories(flutter INTERFACE + "${EPHEMERAL_DIR}" +) +target_link_libraries(flutter INTERFACE "${FLUTTER_LIBRARY}") +target_link_libraries(flutter INTERFACE + PkgConfig::GTK + PkgConfig::GLIB + PkgConfig::GIO +) +add_dependencies(flutter flutter_assemble) + +# === Flutter tool backend === +# _phony_ is a non-existent file to force this command to run every time, +# since currently there's no way to get a full input/output list from the +# flutter tool. +add_custom_command( + OUTPUT ${FLUTTER_LIBRARY} ${FLUTTER_LIBRARY_HEADERS} + ${CMAKE_CURRENT_BINARY_DIR}/_phony_ + COMMAND ${CMAKE_COMMAND} -E env + ${FLUTTER_TOOL_ENVIRONMENT} + "${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.sh" + ${FLUTTER_TARGET_PLATFORM} ${CMAKE_BUILD_TYPE} + VERBATIM +) +add_custom_target(flutter_assemble DEPENDS + "${FLUTTER_LIBRARY}" + ${FLUTTER_LIBRARY_HEADERS} +) diff --git a/app/linux/runner/CMakeLists.txt b/app/linux/runner/CMakeLists.txt new file mode 100644 index 00000000..e97dabc7 --- /dev/null +++ b/app/linux/runner/CMakeLists.txt @@ -0,0 +1,26 @@ +cmake_minimum_required(VERSION 3.13) +project(runner LANGUAGES CXX) + +# Define the application target. To change its name, change BINARY_NAME in the +# top-level CMakeLists.txt, not the value here, or `flutter run` will no longer +# work. +# +# Any new source files that you add to the application should be added here. +add_executable(${BINARY_NAME} + "main.cc" + "my_application.cc" + "${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc" +) + +# Apply the standard set of build settings. This can be removed for applications +# that need different build settings. +apply_standard_settings(${BINARY_NAME}) + +# Add preprocessor definitions for the application ID. +add_definitions(-DAPPLICATION_ID="${APPLICATION_ID}") + +# Add dependency libraries. Add any application-specific dependencies here. +target_link_libraries(${BINARY_NAME} PRIVATE flutter) +target_link_libraries(${BINARY_NAME} PRIVATE PkgConfig::GTK) + +target_include_directories(${BINARY_NAME} PRIVATE "${CMAKE_SOURCE_DIR}") diff --git a/app/linux/runner/main.cc b/app/linux/runner/main.cc new file mode 100644 index 00000000..e7c5c543 --- /dev/null +++ b/app/linux/runner/main.cc @@ -0,0 +1,6 @@ +#include "my_application.h" + +int main(int argc, char** argv) { + g_autoptr(MyApplication) app = my_application_new(); + return g_application_run(G_APPLICATION(app), argc, argv); +} diff --git a/app/linux/runner/my_application.cc b/app/linux/runner/my_application.cc new file mode 100644 index 00000000..3e1ed334 --- /dev/null +++ b/app/linux/runner/my_application.cc @@ -0,0 +1,148 @@ +#include "my_application.h" + +#include +#ifdef GDK_WINDOWING_X11 +#include +#endif + +#include "flutter/generated_plugin_registrant.h" + +struct _MyApplication { + GtkApplication parent_instance; + char** dart_entrypoint_arguments; +}; + +G_DEFINE_TYPE(MyApplication, my_application, GTK_TYPE_APPLICATION) + +// Called when first Flutter frame received. +static void first_frame_cb(MyApplication* self, FlView* view) { + gtk_widget_show(gtk_widget_get_toplevel(GTK_WIDGET(view))); +} + +// Implements GApplication::activate. +static void my_application_activate(GApplication* application) { + MyApplication* self = MY_APPLICATION(application); + GtkWindow* window = + GTK_WINDOW(gtk_application_window_new(GTK_APPLICATION(application))); + + // Use a header bar when running in GNOME as this is the common style used + // by applications and is the setup most users will be using (e.g. Ubuntu + // desktop). + // If running on X and not using GNOME then just use a traditional title bar + // in case the window manager does more exotic layout, e.g. tiling. + // If running on Wayland assume the header bar will work (may need changing + // if future cases occur). + gboolean use_header_bar = TRUE; +#ifdef GDK_WINDOWING_X11 + GdkScreen* screen = gtk_window_get_screen(window); + if (GDK_IS_X11_SCREEN(screen)) { + const gchar* wm_name = gdk_x11_screen_get_window_manager_name(screen); + if (g_strcmp0(wm_name, "GNOME Shell") != 0) { + use_header_bar = FALSE; + } + } +#endif + if (use_header_bar) { + GtkHeaderBar* header_bar = GTK_HEADER_BAR(gtk_header_bar_new()); + gtk_widget_show(GTK_WIDGET(header_bar)); + gtk_header_bar_set_title(header_bar, "Pipelines"); + gtk_header_bar_set_show_close_button(header_bar, TRUE); + gtk_window_set_titlebar(window, GTK_WIDGET(header_bar)); + } else { + gtk_window_set_title(window, "Pipelines"); + } + + gtk_window_set_default_size(window, 1280, 720); + + g_autoptr(FlDartProject) project = fl_dart_project_new(); + fl_dart_project_set_dart_entrypoint_arguments( + project, self->dart_entrypoint_arguments); + + FlView* view = fl_view_new(project); + GdkRGBA background_color; + // Background defaults to black, override it here if necessary, e.g. #00000000 + // for transparent. + gdk_rgba_parse(&background_color, "#000000"); + fl_view_set_background_color(view, &background_color); + gtk_widget_show(GTK_WIDGET(view)); + gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(view)); + + // Show the window when Flutter renders. + // Requires the view to be realized so we can start rendering. + g_signal_connect_swapped(view, "first-frame", G_CALLBACK(first_frame_cb), + self); + gtk_widget_realize(GTK_WIDGET(view)); + + fl_register_plugins(FL_PLUGIN_REGISTRY(view)); + + gtk_widget_grab_focus(GTK_WIDGET(view)); +} + +// Implements GApplication::local_command_line. +static gboolean my_application_local_command_line(GApplication* application, + gchar*** arguments, + int* exit_status) { + MyApplication* self = MY_APPLICATION(application); + // Strip out the first argument as it is the binary name. + self->dart_entrypoint_arguments = g_strdupv(*arguments + 1); + + g_autoptr(GError) error = nullptr; + if (!g_application_register(application, nullptr, &error)) { + g_warning("Failed to register: %s", error->message); + *exit_status = 1; + return TRUE; + } + + g_application_activate(application); + *exit_status = 0; + + return TRUE; +} + +// Implements GApplication::startup. +static void my_application_startup(GApplication* application) { + // MyApplication* self = MY_APPLICATION(object); + + // Perform any actions required at application startup. + + G_APPLICATION_CLASS(my_application_parent_class)->startup(application); +} + +// Implements GApplication::shutdown. +static void my_application_shutdown(GApplication* application) { + // MyApplication* self = MY_APPLICATION(object); + + // Perform any actions required at application shutdown. + + G_APPLICATION_CLASS(my_application_parent_class)->shutdown(application); +} + +// Implements GObject::dispose. +static void my_application_dispose(GObject* object) { + MyApplication* self = MY_APPLICATION(object); + g_clear_pointer(&self->dart_entrypoint_arguments, g_strfreev); + G_OBJECT_CLASS(my_application_parent_class)->dispose(object); +} + +static void my_application_class_init(MyApplicationClass* klass) { + G_APPLICATION_CLASS(klass)->activate = my_application_activate; + G_APPLICATION_CLASS(klass)->local_command_line = + my_application_local_command_line; + G_APPLICATION_CLASS(klass)->startup = my_application_startup; + G_APPLICATION_CLASS(klass)->shutdown = my_application_shutdown; + G_OBJECT_CLASS(klass)->dispose = my_application_dispose; +} + +static void my_application_init(MyApplication* self) {} + +MyApplication* my_application_new() { + // Set the program name to the application ID, which helps various systems + // like GTK and desktop environments map this running application to its + // corresponding .desktop file. This ensures better integration by allowing + // the application to be recognized beyond its binary name. + g_set_prgname(APPLICATION_ID); + + return MY_APPLICATION(g_object_new(my_application_get_type(), + "application-id", APPLICATION_ID, "flags", + G_APPLICATION_NON_UNIQUE, nullptr)); +} diff --git a/app/linux/runner/my_application.h b/app/linux/runner/my_application.h new file mode 100644 index 00000000..db16367a --- /dev/null +++ b/app/linux/runner/my_application.h @@ -0,0 +1,21 @@ +#ifndef FLUTTER_MY_APPLICATION_H_ +#define FLUTTER_MY_APPLICATION_H_ + +#include + +G_DECLARE_FINAL_TYPE(MyApplication, + my_application, + MY, + APPLICATION, + GtkApplication) + +/** + * my_application_new: + * + * Creates a new Flutter-based application. + * + * Returns: a new #MyApplication. + */ +MyApplication* my_application_new(); + +#endif // FLUTTER_MY_APPLICATION_H_ diff --git a/app/macos/Runner.xcodeproj/project.pbxproj b/app/macos/Runner.xcodeproj/project.pbxproj index 49f6bf74..f2a52d37 100644 --- a/app/macos/Runner.xcodeproj/project.pbxproj +++ b/app/macos/Runner.xcodeproj/project.pbxproj @@ -471,7 +471,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - MACOSX_DEPLOYMENT_TARGET = 10.15; + MACOSX_DEPLOYMENT_TARGET = 12.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = macosx; SWIFT_COMPILATION_MODE = wholemodule; @@ -553,7 +553,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - MACOSX_DEPLOYMENT_TARGET = 10.15; + MACOSX_DEPLOYMENT_TARGET = 12.0; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = macosx; @@ -603,7 +603,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - MACOSX_DEPLOYMENT_TARGET = 10.15; + MACOSX_DEPLOYMENT_TARGET = 12.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = macosx; SWIFT_COMPILATION_MODE = wholemodule; diff --git a/docs/install.md b/docs/install.md index fdf32f03..37abcd93 100644 --- a/docs/install.md +++ b/docs/install.md @@ -154,6 +154,40 @@ signed with the key in `~/.config/pipelines/android/` (seeded from `~/.android/debug.keystore` if present), so each build installs over the last. Options: `--server-url ws(s)://host:port/connect`, `--notes TEXT`, `--prefix`. +### Desktop clients + +```sh +./install --linux --output ./dist +./install --macos --flutter /path/to/flutter --output ./dist +# Offer this desktop build through an installed server's update feed: +./install --linux --publish --notes "Release notes" +./install --macos --flutter /path/to/flutter --publish +``` + +Linux builds use Docker on Linux or macOS and target **Linux x64**. Extract the +entire `pipelines-linux-x64.tar.gz` archive and run `pipelines-linux-x64/pipelines-app`; +keep its `lib/` and `data/` siblings together. The executable has a distinct name +from the `pipelines` server CLI. The target Linux desktop needs GTK 3 and its +runtime libraries (Debian/Ubuntu: `libgtk-3-0`, `libstdc++6`, `liblzma5`). This is a +portable bundle, not a package-manager installation or a server installation. + +macOS 12 or newer is required. macOS builds run natively with Xcode and the pinned Flutter version from +`app/.flutter-version`; they do not require Docker. The build targets the build +machine's architecture and records the actual executable architecture in the +feed. Unzip `Pipelines-macos.zip` and open `Pipelines.app`; move it to Applications +if desired. Local builds are not Developer ID notarized distribution packages. +Framework symlinks and executable permissions are preserved in the archive. + +Both commands build in isolated source copies/containers, retain cached releases, +and do not install, launch or restart the server. `--server-url` optionally sets +the first server address; otherwise choose it in the app. `--output` copies the +archive to a chosen directory. `--publish` additionally requires an existing +installation (`--prefix` supported), copies a versioned archive into its update +directory, then atomically updates `/updates/linux-x64/version.json` or +`/updates/macos/version.json`. Feeds include platform, actual architectures, +version, SHA-256, and release notes. Android's existing feed remains separate. +Previously published archives remain available for downloads already in progress. + `./install --docker` is the optional Compose installation with its own container harness credentials; see [Docker](docker.md). Do not run it alongside a native install on the same ports. Its harnesses are set up with diff --git a/docs/notes/desktop-client-builds.md b/docs/notes/desktop-client-builds.md new file mode 100644 index 00000000..c97dbb71 --- /dev/null +++ b/docs/notes/desktop-client-builds.md @@ -0,0 +1,28 @@ +# Desktop client build options + +Added `./install --linux` (Docker Linux x64) and `./install --macos` +(native pinned Flutter + Xcode, host architecture). Both produce full client +archives without installing or restarting a server. macOS requires 12+; +its existing 10.15 project target failed against Xcode 27 and was corrected. +The supported Flutter `FLUTTER_XCODE_ARCHS` setting explicitly selects the +native host architecture; the cache includes that host identity. This avoids +claiming a universal binary, and feeds use actual `lipo -archs` output. + +`--publish` offers versioned archives via the existing authenticated update +file server. Feeds carry platform, actual architectures, epoch-minute build +code, SHA-256, release notes, and relative archive filename. The same build +code is compiled into the client. Archive publication precedes atomic feed +replacement; old filenames remain available for in-flight downloads. Older +builds cannot overwrite newer feeds. Builds work without a server manifest +unless publication is requested. + +Verification: 15 Python builder/cache/packaging/publication tests passed; +Python compilation, shell syntax, and diff checks passed. Real Docker x64 +Linux release and native macOS arm64 release both built with Flutter 3.44.6. +The macOS archive retained nine framework symlinks, and `codesign --verify +--deep --strict` passed. Linux archive retained the complete lib/data bundle +and executable mode 0755; `ldd` inside the Debian builder found no missing +libraries. Logs: /tmp/pipelines-{linux,macos}-build.log and +/tmp/pipelines-linux-linkage.log. No desktop UI/device interaction was run; +these build checks do not establish runtime UI parity or public deployment. +Final app-source integration requires rebuilding changed-source artifacts. diff --git a/install.sh b/install.sh index c5b6cda7..a4ed7927 100755 --- a/install.sh +++ b/install.sh @@ -27,6 +27,10 @@ case "$mode" in shift exec python3 scripts/app_build.py apk "$@" ;; + --linux|linux|--macos|macos) + shift + exec python3 scripts/app_build.py "${mode#--}" "$@" + ;; --native|native) shift exec python3 scripts/setup-native.py "$@" diff --git a/scripts/app_build.py b/scripts/app_build.py index 11a9faef..61120bc4 100644 --- a/scripts/app_build.py +++ b/scripts/app_build.py @@ -13,6 +13,7 @@ unchanged app is never rebuilt. """ import argparse import hashlib +import importlib.util import json import os from pathlib import Path @@ -34,10 +35,12 @@ KEEP = 3 # Inputs that change what a build produces. Generated and machine-local paths # (build/, .dart_tool/, android/local.properties) are left out. SOURCES = { - "web": ("lib", "web", "assets", "pubspec.yaml", "pubspec.lock", ".flutter-version", "docker"), - "apk": ("lib", "android", "assets", "pubspec.yaml", "pubspec.lock", ".flutter-version", "docker"), + "web": ("lib", "web", "assets", "fonts", "pubspec.yaml", "pubspec.lock", ".flutter-version", "docker"), + "apk": ("lib", "android", "assets", "fonts", "pubspec.yaml", "pubspec.lock", ".flutter-version", "docker"), + "linux": ("lib", "linux", "assets", "fonts", "pubspec.yaml", "pubspec.lock", ".flutter-version", "docker"), + "macos": ("lib", "macos", "assets", "fonts", "pubspec.yaml", "pubspec.lock", ".flutter-version"), } -SKIPPED = {"build", ".dart_tool", ".gradle", ".cxx", "local.properties", ".DS_Store"} +SKIPPED = {"build", ".dart_tool", ".gradle", ".cxx", "local.properties", ".DS_Store", "ephemeral", "Pods", ".symlinks"} # This is part of the web cache key so entries made before release metadata was # generated cannot be mistaken for complete web releases. WEB_CACHE_SCHEMA = "web-release-metadata-v1" @@ -54,6 +57,12 @@ def digest(kind, app=APP, extra=()): hasher.update(kind.encode()) if kind == "web": hasher.update(b"\0cache-schema\0" + WEB_CACHE_SCHEMA.encode()) + if kind in ("linux", "macos"): + hasher.update(b"\0cache-schema\0desktop-v1") + for script in (Path(__file__), Path(__file__).with_name("app_desktop_build.py")): + hasher.update(b"\0builder\0" + script.read_bytes()) + if kind == "linux": + hasher.update(b"\0platform\0linux/amd64") for item in extra: hasher.update(b"\0arg\0" + item.encode()) for name in SOURCES[kind]: @@ -107,12 +116,12 @@ def docker_available(runner=subprocess.run): def image(kind, runner, app=APP): """Builds (or reuses, via Docker's layer cache) the build image for [kind].""" - target = "android" if kind == "apk" else "web" + target = {"apk": "android", "linux": "linux"}.get(kind, "web") version = flutter_version(app) tag = f"pipelines-flutter-{target}:{version}" args = ["build", "--target", target, "--build-arg", f"FLUTTER_VERSION={version}", "-t", tag, "-f", str(app / "docker/Dockerfile")] - if kind == "apk": + if kind in ("apk", "linux"): # Android's build tools (aapt2) ship for x86-64 Linux only. args += ["--platform", "linux/amd64"] docker(runner, *args, str(app / "docker")) @@ -140,6 +149,8 @@ def build(kind, runner=subprocess.run, app=APP, cache=None, arguments=(), log=pr key = digest(kind, app, arguments) entry = cache / f"{kind}-{key}" usable = web_cache_complete(entry) if kind == "web" else (entry / ".complete").is_file() + if kind == "linux": + usable = usable and (entry / "pipelines-app").is_file() and (entry / "data/flutter_assets").is_dir() and (entry / "lib").is_dir() if usable: log(f"Reusing the {kind} build {key} (app sources unchanged).") return entry @@ -152,6 +163,8 @@ def build(kind, runner=subprocess.run, app=APP, cache=None, arguments=(), log=pr command = ["run", "--rm", "--user", f"{os.getuid()}:{os.getgid()}", "-v", f"{app}:/src:ro", "-v", f"{work}:/out", "-v", "pipelines-flutter-pub:/home/builder/.pub-cache"] + if kind == "linux": + command += ["--platform", "linux/amd64"] if kind == "apk": command += ["--platform", "linux/amd64", "-v", "pipelines-flutter-gradle:/home/builder/.gradle", @@ -165,6 +178,8 @@ def build(kind, runner=subprocess.run, app=APP, cache=None, arguments=(), log=pr # this avoids circular hashing while keeping the compiled app and # its published metadata on the same identity. build_arguments += (f"--dart-define=PIPELINES_WEB_BUILD_ID={web_build_id(key)}",) + if kind == "linux": + build_arguments += (f"--dart-define=PIPELINES_NATIVE_BUILD_CODE={built // 60}",) if kind == "apk": # Android installs only a higher build number over the last one, so # each build is stamped when it is made (minutes since the epoch); @@ -172,6 +187,9 @@ def build(kind, runner=subprocess.run, app=APP, cache=None, arguments=(), log=pr code = built // 60 stamp = (f"--build-number={code}", f"--build-name=0.1.{code}") docker(runner, *command, tag, "pipelines-app-build", kind, *build_arguments, *stamp) + if kind == "linux" and not ((work / "pipelines-app").is_file() and + (work / "data/flutter_assets").is_dir() and (work / "lib").is_dir()): + raise RuntimeError("Linux build did not produce its complete executable/data/lib bundle") if kind == "web": (work / "release.json").write_text(json.dumps(web_release_metadata(key)) + "\n") (work / ".complete").write_text(json.dumps({"key": key, "built": built}) + "\n") @@ -270,11 +288,29 @@ def print_web_urls(manifest, lan_host=None): def main(argv=None): parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) - parser.add_argument("kind", choices=("web", "apk")) + parser.add_argument("kind", choices=("web", "apk", "linux", "macos")) parser.add_argument("--prefix", type=Path, default=DEFAULT_PREFIX) parser.add_argument("--server-url", help="APK default server (default: this installation's web address)") parser.add_argument("--notes", default="", help="APK release notes shown in Check for updates") + parser.add_argument("--publish", action="store_true", help="Publish desktop archive and update feed to this installation") + parser.add_argument("--output", type=Path, help="Desktop archive output directory (default: build cache)") + parser.add_argument("--flutter", help="Native Flutter executable for macOS (or PIPELINES_FLUTTER/PATH)") args = parser.parse_args(argv) + if args.kind in ("linux", "macos"): + spec = importlib.util.spec_from_file_location("pipelines_desktop_build", Path(__file__).with_name("app_desktop_build.py")) + desktop = importlib.util.module_from_spec(spec) + spec.loader.exec_module(desktop) + bundle, archive = desktop.build_desktop(args.kind, app=APP, cache=default_cache(), runner=subprocess.run, + source_digest=digest, docker_build=build, docker_ready=docker_available, + server_url=args.server_url, flutter=args.flutter, output=args.output, + prune_cache=prune) + if args.publish: + manifest = load_manifest(args.prefix) + updates = (manifest.get("web") or {}).get("updates") + if not updates: + raise ValueError("This installation has no app update directory; rerun ./install") + desktop.publish_desktop(args.kind, bundle, archive, Path(updates), notes=args.notes) + return if not docker_available(): raise RuntimeError("Docker is required to build the app; start Docker and retry") if args.kind == "apk": diff --git a/scripts/app_desktop_build.py b/scripts/app_desktop_build.py new file mode 100644 index 00000000..500fb664 --- /dev/null +++ b/scripts/app_desktop_build.py @@ -0,0 +1,192 @@ +"""Standalone desktop client artifacts; never installs or restarts a server.""" +import hashlib +import json +import os +from pathlib import Path +import platform +import shutil +import subprocess +import tarfile +import tempfile +import time +from urllib.parse import urlsplit + + +def _run(runner, command, **kwargs): + try: + result = runner(command, **kwargs) + except subprocess.TimeoutExpired as error: + raise RuntimeError(f"{command[0]} timed out") from error + if result.returncode: + raise RuntimeError(f"{command[0]} failed; see its build output") + return result + + +def _arguments(server_url): + if not server_url: + return () + parsed = urlsplit(server_url) + if parsed.scheme not in ("ws", "wss", "http", "https") or not parsed.hostname or parsed.username or parsed.password or parsed.fragment: + raise ValueError("--server-url must be a server address without embedded credentials or fragment") + return (f"--dart-define=PIPELINES_SERVER_URL={server_url}",) + + +def _linux_archive(entry): + archive = entry / "pipelines-linux-x64.tar.gz" + if archive.is_file(): + return archive + staged = archive.with_suffix(".gz.next") + try: + with tarfile.open(staged, "w:gz") as output: + for child in sorted(entry.iterdir()): + if child.name not in (".complete", archive.name, staged.name): + output.add(child, arcname=f"pipelines-linux-x64/{child.name}") + os.replace(staged, archive) + finally: + staged.unlink(missing_ok=True) + return archive + + +def _macos_build(*, app, cache, source_digest, runner, arguments, flutter, log): + if platform.system() != "Darwin": + raise ValueError("macOS clients must be built natively on macOS with Xcode") + selected = flutter or os.environ.get("PIPELINES_FLUTTER") or shutil.which("flutter") + if not selected: + raise ValueError("Flutter is required; pass --flutter /path/to/flutter or set PIPELINES_FLUTTER") + selected = str(Path(selected).expanduser()) + version = _run(runner, [selected, "--version", "--machine"], capture_output=True, text=True, timeout=60) + try: + actual = json.loads(version.stdout)["frameworkVersion"] + except (ValueError, KeyError, TypeError) as error: + raise ValueError("The selected Flutter SDK did not report its version") from error + pinned = (app / ".flutter-version").read_text().strip() + if actual != pinned: + raise ValueError(f"Flutter {pinned} is required; selected SDK is {actual}. No SDK was changed.") + xcode = _run(runner, ["xcodebuild", "-version"], capture_output=True, text=True, timeout=30) + identity = ("native-macos-v1", platform.machine(), xcode.stdout.strip(), actual) + key = source_digest("macos", app=app, extra=arguments + identity) + entry = cache / f"macos-{key}" + bundle = entry / "Pipelines.app" + archive = entry / "Pipelines-macos.zip" + if (entry / ".complete").is_file() and (bundle / "Contents/MacOS/Pipelines").is_file() and archive.is_file(): + log(f"Reusing native macOS build {key}.") + return entry, bundle, archive + cache.mkdir(parents=True, exist_ok=True) + staging = Path(tempfile.mkdtemp(prefix="macos-", dir=cache)) + try: + with tempfile.TemporaryDirectory(prefix="pipelines-macos-source-") as temporary: + work = Path(temporary) / "app" + shutil.copytree(app, work, symlinks=True, ignore=shutil.ignore_patterns( + "build", ".dart_tool", "Pods", "ephemeral", ".symlinks", ".gradle", ".cxx", "local.properties", ".DS_Store")) + _run(runner, [selected, "pub", "get"], cwd=work, timeout=300) + built = int(time.time()) + _run(runner, [selected, "build", "macos", "--release", "--no-pub", *arguments, + f"--build-number={built // 60}", + f"--dart-define=PIPELINES_NATIVE_BUILD_CODE={built // 60}"], cwd=work, timeout=1200, + env={**os.environ, "FLUTTER_XCODE_ARCHS": {"aarch64": "arm64", "x86_64": "x86_64"}.get(platform.machine(), platform.machine())}) + native = work / "build/macos/Build/Products/Release/Pipelines.app" + if not (native / "Contents/MacOS/Pipelines").is_file(): + raise RuntimeError("Flutter did not produce a complete Pipelines.app") + arches = _run(runner, ["lipo", "-archs", str(native / "Contents/MacOS/Pipelines")], + capture_output=True, text=True, timeout=30).stdout.split() + architectures = sorted({{"x86_64": "x64", "arm64": "arm64"}.get(a, a) for a in arches}) + if not architectures or any(a not in ("x64", "arm64") for a in architectures): + raise RuntimeError("macOS bundle has unsupported or missing architectures") + # Framework symlinks and executable modes are part of the bundle. + shutil.copytree(native, staging / "Pipelines.app", symlinks=True) + _run(runner, ["ditto", "-c", "-k", "--keepParent", str(staging / "Pipelines.app"), + str(staging / archive.name)], timeout=120) + if not (staging / archive.name).is_file(): + raise RuntimeError("macOS archive was not created") + (staging / ".complete").write_text(json.dumps({"key": key, "built": built, "architectures": architectures}) + "\n") + if entry.exists(): + previous = entry.with_name(entry.name + ".previous") + if previous.exists(): + raise RuntimeError(f"A previous build is retained at {previous}; no files were replaced") + entry.rename(previous) + try: + staging.rename(entry) + except BaseException: + previous.rename(entry) + raise + shutil.rmtree(previous) + else: + staging.rename(entry) + finally: + if staging.exists(): + shutil.rmtree(staging) + return entry, bundle, archive + + +def build_desktop(kind, *, app, cache, runner, source_digest, docker_build, + docker_ready, server_url=None, flutter=None, output=None, log=print, + prune_cache=None): + arguments = _arguments(server_url) + if kind == "macos": + entry, bundle, archive = _macos_build(app=app, cache=cache, runner=runner, + source_digest=source_digest, arguments=arguments, flutter=flutter, log=log) + elif kind == "linux": + if not docker_ready(runner): + raise RuntimeError("Docker is required for the Linux x64 client; start Docker and retry") + entry = docker_build("linux", app=app, cache=cache, runner=runner, arguments=arguments, log=log) + if not (entry / "pipelines-app").is_file() or not (entry / "data/flutter_assets").is_dir() or not (entry / "lib").is_dir(): + raise RuntimeError("Linux build is missing its executable, data or libraries") + bundle, archive = entry, _linux_archive(entry) + else: + raise ValueError(f"Unknown desktop target: {kind}") + if prune_cache is not None: + prune_cache(cache, kind, keep=entry) + if output is not None: + output = Path(output).expanduser() + output.mkdir(parents=True, exist_ok=True) + published = output / archive.name + descriptor, temporary = tempfile.mkstemp(prefix=archive.name + ".", dir=output) + os.close(descriptor) + try: + shutil.copy2(archive, temporary) + os.replace(temporary, published) + finally: + Path(temporary).unlink(missing_ok=True) + archive = published + log(f"Desktop client bundle: {bundle}") + log(f"Desktop client archive: {archive}") + log("No server was installed or restarted. Choose your server in the app.") + return bundle, archive + + +def publish_desktop(kind, bundle, archive, updates, *, notes="", log=print): + """Publish archive before its feed; versioned names preserve in-flight reads.""" + entry = bundle.parent if kind == "macos" else bundle + complete = json.loads((entry / ".complete").read_text()) + code = int(complete["built"]) // 60 + architectures = complete.get("architectures") if kind == "macos" else ["x64"] + if not architectures or any(a not in ("x64", "arm64") for a in architectures): + raise ValueError("Build metadata has no supported architecture") + digest = hashlib.sha256(archive.read_bytes()).hexdigest() + suffix = ".zip" if kind == "macos" else ".tar.gz" + stem = "Pipelines-macos" if kind == "macos" else "pipelines-linux-x64" + filename = f"{stem}-{code}-{digest[:8]}{suffix}" + target = updates / ("macos" if kind == "macos" else "linux-x64") + target.mkdir(parents=True, exist_ok=True) + metadata = {"versionCode": code, "versionName": f"0.1.{code}", + "platform": kind, "architectures": architectures, "file": filename, + "sha256": digest, "notes": notes} + feed = target / "version.json" + if feed.exists(): + previous = json.loads(feed.read_text()) + if previous.get("versionCode", 0) > code: + raise ValueError("Refusing to publish an older build over a newer update") + for destination, content in ((target / filename, archive.read_bytes()), + (feed, (json.dumps(metadata) + "\n").encode())): + descriptor, temporary = tempfile.mkstemp(prefix=destination.name + ".", dir=target) + try: + with os.fdopen(descriptor, "wb") as output: + output.write(content) + output.flush() + os.fsync(output.fileno()) + os.chmod(temporary, 0o644) + os.replace(temporary, destination) + finally: + Path(temporary).unlink(missing_ok=True) + log(f"Published {kind} update {code} at {feed}") + return metadata diff --git a/scripts/test_app_desktop_build.py b/scripts/test_app_desktop_build.py new file mode 100644 index 00000000..3af49e31 --- /dev/null +++ b/scripts/test_app_desktop_build.py @@ -0,0 +1,178 @@ +import importlib.util +import json +from pathlib import Path +import subprocess +import tarfile +import tempfile +import unittest +from unittest.mock import patch + + +def load(name): + spec = importlib.util.spec_from_file_location(name, Path(__file__).with_name(name + ".py")) + result = importlib.util.module_from_spec(spec) + spec.loader.exec_module(result) + return result + + +core = load("app_build") +desktop = load("app_desktop_build") + + +class Runner: + def __init__(self): + self.calls = [] + self.native_env = None + self.fail_native = False + self.version = "3.44.6" + + def __call__(self, command, **kwargs): + self.calls.append(command) + stdout = "" + code = 0 + if "--machine" in command: + stdout = json.dumps({"frameworkVersion": self.version}) + elif command[:2] == ["xcodebuild", "-version"]: + stdout = "Xcode 27.0\nBuild version 27A266a" + elif command[:2] == ["docker", "run"]: + output = Path(next(arg[:-5] for arg in command if arg.endswith(":/out"))) + (output / "pipelines-app").write_bytes(b"linux-client") + (output / "pipelines-app").chmod(0o755) + (output / "data/flutter_assets").mkdir(parents=True) + (output / "data/flutter_assets/font.ttf").write_bytes(b"font") + (output / "lib").mkdir() + (output / "lib/engine.so").write_bytes(b"engine") + elif command[1:3] == ["build", "macos"]: + self.native_env = kwargs.get("env") + if self.fail_native: + code = 1 + else: + app = Path(kwargs["cwd"]) / "build/macos/Build/Products/Release/Pipelines.app" + (app / "Contents/MacOS").mkdir(parents=True) + (app / "Contents/MacOS/Pipelines").write_bytes(b"macos-client") + (app / "Contents/MacOS/Pipelines").chmod(0o755) + framework = app / "Contents/Frameworks/Fixture.framework" + (framework / "Versions/A").mkdir(parents=True) + (framework / "Versions/A/Fixture").write_bytes(b"framework") + (framework / "Versions/Current").symlink_to("A") + (framework / "Fixture").symlink_to("Versions/Current/Fixture") + elif command[:2] == ["lipo", "-archs"]: + stdout = "x86_64 arm64" + elif command[0] == "ditto": + Path(command[-1]).write_bytes(b"archive") + return subprocess.CompletedProcess(command, code, stdout=stdout, stderr="") + + +class DesktopBuildTests(unittest.TestCase): + def setUp(self): + self.temporary = tempfile.TemporaryDirectory() + self.addCleanup(self.temporary.cleanup) + self.root = Path(self.temporary.name) + self.app = self.root / "app" + for directory in ("lib", "fonts", "linux", "macos", "docker"): + (self.app / directory).mkdir(parents=True, exist_ok=True) + (self.app / "lib/main.dart").write_text("main() {}") + (self.app / ".flutter-version").write_text("3.44.6\n") + self.runner = Runner() + + def build(self, kind, **kwargs): + return desktop.build_desktop(kind, app=self.app, cache=self.root / "cache", + runner=self.runner, source_digest=core.digest, docker_build=core.build, + docker_ready=lambda *_: True, log=lambda *_: None, **kwargs) + + def test_linux_is_x64_and_packages_full_bundle_without_installation(self): + bundle, archive = self.build("linux", output=self.root / "artifacts") + builds = [c for c in self.runner.calls if c[:2] == ["docker", "build"]] + self.assertEqual(builds[0][builds[0].index("--target") + 1], "linux") + for command in [builds[0], next(c for c in self.runner.calls if c[:2] == ["docker", "run"])]: + self.assertEqual(command[command.index("--platform") + 1], "linux/amd64") + with tarfile.open(archive) as package: + self.assertTrue({"pipelines-linux-x64/pipelines-app", "pipelines-linux-x64/lib/engine.so", + "pipelines-linux-x64/data/flutter_assets/font.ttf"}.issubset(package.getnames())) + self.assertEqual(package.getmember("pipelines-linux-x64/pipelines-app").mode & 0o111, 0o111) + self.assertTrue(bundle.is_dir()) + before = len(self.runner.calls) + self.build("linux") + self.assertEqual(len(self.runner.calls), before) + + def test_font_and_target_sources_invalidate_correct_caches(self): + keys = {kind: core.digest(kind, self.app) for kind in core.SOURCES} + (self.app / "fonts/font.ttf").write_bytes(b"new font") + for kind in core.SOURCES: + self.assertNotEqual(keys[kind], core.digest(kind, self.app), kind) + linux = core.digest("linux", self.app) + macos = core.digest("macos", self.app) + (self.app / "linux/CMakeLists.txt").write_text("changed native runner") + self.assertNotEqual(linux, core.digest("linux", self.app)) + self.assertEqual(macos, core.digest("macos", self.app)) + generated = self.app / "macos/Flutter/ephemeral" + generated.mkdir(parents=True) + (generated / "local-path").write_text("machine-specific generated value") + self.assertEqual(macos, core.digest("macos", self.app)) + + @patch.object(desktop.platform, "system", return_value="Darwin") + def test_native_macos_preserves_framework_symlinks_and_reuses_build(self, _): + bundle, archive = self.build("macos", flutter="/fixture/flutter") + self.assertTrue((bundle / "Contents/Frameworks/Fixture.framework/Versions/Current").is_symlink()) + self.assertEqual((bundle / "Contents/MacOS/Pipelines").stat().st_mode & 0o111, 0o111) + self.assertTrue(archive.is_file()) + self.build("macos", flutter="/fixture/flutter") + self.assertEqual(sum(c[1:3] == ["build", "macos"] for c in self.runner.calls), 1) + self.assertFalse(any(c[0] == "docker" for c in self.runner.calls)) + self.assertFalse((self.app / "build").exists()) + expected = {"aarch64": "arm64", "x86_64": "x86_64"}.get(desktop.platform.machine(), desktop.platform.machine()) + self.assertEqual(self.runner.native_env["FLUTTER_XCODE_ARCHS"], expected) + + @patch.object(desktop.platform, "system", return_value="Darwin") + def test_failed_native_build_preserves_previous_artifact(self, _): + bundle, archive = self.build("macos", flutter="/fixture/flutter") + previous = archive.read_bytes() + (self.app / "lib/main.dart").write_text("changed application") + self.runner.fail_native = True + with self.assertRaisesRegex(RuntimeError, "failed"): + self.build("macos", flutter="/fixture/flutter") + self.assertEqual(archive.read_bytes(), previous) + self.assertTrue((bundle / "Contents/MacOS/Pipelines").is_file()) + + @patch.object(desktop.platform, "system", return_value="Darwin") + def test_wrong_sdk_fails_before_building_or_changing_any_sdk(self, _): + self.runner.version = "3.47.0" + with self.assertRaisesRegex(ValueError, "3.44.6 is required"): + self.build("macos", flutter="/fixture/flutter") + self.assertEqual(len(self.runner.calls), 1) + + @patch.object(desktop.platform, "system", return_value="Linux") + def test_macos_refuses_non_macos_host_without_docker_fallback(self, _): + with self.assertRaisesRegex(ValueError, "natively on macOS"): + self.build("macos") + self.assertEqual(self.runner.calls, []) + + def test_linux_publication_has_matching_hash_and_rejects_downgrade(self): + bundle, archive = self.build("linux") + updates = self.root / "updates" + metadata = desktop.publish_desktop("linux", bundle, archive, updates, log=lambda *_: None) + target = updates / "linux-x64" + self.assertEqual(json.loads((target / "version.json").read_text()), metadata) + self.assertEqual(metadata["architectures"], ["x64"]) + self.assertEqual(metadata["sha256"], desktop.hashlib.sha256((target / metadata["file"]).read_bytes()).hexdigest()) + metadata["versionCode"] += 1 + (target / "version.json").write_text(json.dumps(metadata)) + with self.assertRaisesRegex(ValueError, "older build"): + desktop.publish_desktop("linux", bundle, archive, updates) + + @patch.object(desktop.platform, "system", return_value="Darwin") + def test_macos_feed_uses_actual_architectures_and_native_build_stamp(self, _): + bundle, archive = self.build("macos", flutter="/fixture/flutter") + metadata = desktop.publish_desktop("macos", bundle, archive, self.root / "updates", log=lambda *_: None) + self.assertEqual(metadata["architectures"], ["arm64", "x64"]) + command = next(c for c in self.runner.calls if c[1:3] == ["build", "macos"]) + self.assertIn(f"--dart-define=PIPELINES_NATIVE_BUILD_CODE={metadata['versionCode']}", command) + + def test_desktop_server_prefill_rejects_embedded_credentials(self): + with self.assertRaisesRegex(ValueError, "embedded credentials"): + self.build("linux", server_url="wss://secret@example.com/connect") + self.assertEqual(self.runner.calls, []) + + +if __name__ == "__main__": + unittest.main()