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.

Pause PR status polling for hidden panes and background windows (#677)

Dee9c685dcd0ed6
diff --git a/desktop/integration_test/pr_polling_native_test.dart b/desktop/integration_test/pr_polling_native_test.dart
new file mode 100644
index 000000000..64e452c06
--- /dev/null
+++ b/desktop/integration_test/pr_polling_native_test.dart
@@ -0,0 +1,16 @@
+import 'package:flutter_test/flutter_test.dart';
+import 'package:integration_test/integration_test.dart';
+import 'package:harness/core/test_run.dart';
+
+import '../test/terminal_panel_header_test.dart' show verifyPanePrVisibility;
+
+void main() {
+ if (!kUnderTest) {
+ throw StateError('Run this synthetic fixture with FLUTTER_TEST=1.');
+ }
+ IntegrationTestWidgetsFlutterBinding.ensureInitialized();
+ testWidgets(
+ 'native pane PR refresh follows visibility and branch changes',
+ verifyPanePrVisibility,
+ );
+}
diff --git a/desktop/lib/state/workspace_pull_request.dart b/desktop/lib/state/workspace_pull_request.dart
index a8f6ae13e..96856bff0 100644
--- a/desktop/lib/state/workspace_pull_request.dart
+++ b/desktop/lib/state/workspace_pull_request.dart
@@ -14,17 +14,24 @@ class WorkspacePullRequest extends ChangeNotifier {
WorkspacePullRequest(
this.app, {
Future<Map<String, dynamic>> Function(String, String)? read,
- }) : _read = read ?? app.readAgentPullRequest {
+ DateTime Function()? now,
+ }) : _read = read ?? app.readAgentPullRequest,
+ _now = now ?? DateTime.now {
app.addListener(_focusChanged);
+ app.foreground.addListener(_scheduleRefresh);
_focusChanged();
}
final AppNotifier app;
final Future<Map<String, dynamic>> Function(String, String) _read;
+ final DateTime Function() _now;
+ static const _interval = Duration(seconds: 60);
final _cache = <_Identity, (DateTime, PullRequestStatus?)>{};
_Identity? _identity;
PullRequestStatus? value;
Timer? _timer;
int _revision = 0;
+ int? _pendingRevision;
+ bool _disposed = false;
void _focusChanged() {
final focused = WorkspacePaneContext.focused(app);
@@ -44,51 +51,63 @@ class WorkspacePullRequest extends ChangeNotifier {
);
if (identity == _identity) return;
_identity = identity;
- final revision = ++_revision;
- _timer?.cancel();
+ ++_revision;
final cached = _cache[identity];
value = cached?.$2;
notifyListeners();
+ _scheduleRefresh();
+ }
+
+ void _scheduleRefresh() {
+ _timer?.cancel();
+ _timer = null;
+ if (_disposed || !app.foreground.value) return;
+ final identity = _identity, revision = _revision;
if (identity == null) return;
- final age = cached == null
- ? const Duration(seconds: 60)
- : DateTime.now().difference(cached.$1);
- if (age < const Duration(seconds: 60)) {
- _timer = Timer(
- const Duration(seconds: 60) - age,
- () => _refresh(identity, revision),
- );
+ // Visibility changes do not cancel a request already sent to the daemon.
+ // Let that lookup fill this identity's cache without starting another one.
+ if (_pendingRevision == revision) return;
+ final cached = _cache[identity];
+ final age = cached == null ? _interval : _now().difference(cached.$1);
+ if (age >= Duration.zero && age < _interval) {
+ _timer = Timer(_interval - age, () => _refresh(identity, revision));
} else {
unawaited(_refresh(identity, revision));
}
}
Future<void> _refresh(_Identity identity, int revision) async {
+ if (_disposed ||
+ revision != _revision ||
+ !app.foreground.value ||
+ _pendingRevision == revision) {
+ return;
+ }
+ _pendingRevision = revision;
Map<String, dynamic>? result;
try {
result = await _read(identity.$1, identity.$2);
} catch (_) {
/* Hidden until available. */
}
- if (revision != _revision) return;
+ if (_pendingRevision == revision) _pendingRevision = null;
+ if (_disposed || revision != _revision) return;
value = PullRequestStatus.fromResult(result);
if (_cache.length >= 32 && !_cache.containsKey(identity)) {
_cache.remove(_cache.keys.first);
}
- _cache[identity] = (DateTime.now(), value);
+ _cache[identity] = (_now(), value);
notifyListeners();
- if (revision != _revision) return;
- _timer = Timer(
- const Duration(seconds: 60),
- () => _refresh(identity, revision),
- );
+ _scheduleRefresh();
}
@override
void dispose() {
+ _disposed = true;
_revision++;
_timer?.cancel();
app.removeListener(_focusChanged);
+ app.foreground.removeListener(_scheduleRefresh);
super.dispose();
}
}
diff --git a/desktop/lib/widgets/pull_request_badge.dart b/desktop/lib/widgets/pull_request_badge.dart
index 63a44952c..007008d22 100644
--- a/desktop/lib/widgets/pull_request_badge.dart
+++ b/desktop/lib/widgets/pull_request_badge.dart
@@ -1,5 +1,6 @@
import 'dart:async';
+import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
@@ -14,54 +15,135 @@ class PullRequestBadge extends StatefulWidget {
required this.read,
this.open,
this.compact = false,
+ this.foreground,
+ this.now,
});
final Object identity;
final bool compact;
final Future<Map<String, dynamic>> Function() read;
final Future<bool> Function(Uri)? open;
+ final ValueListenable<bool>? foreground;
+ final DateTime Function()? now;
@override
State<PullRequestBadge> createState() => _PullRequestBadgeState();
}
class _PullRequestBadgeState extends State<PullRequestBadge> {
+ static const _interval = Duration(seconds: 60);
Timer? _timer;
+ ValueListenable<TickerModeData>? _tickerMode;
int _revision = 0;
+ int? _pendingRevision;
+ DateTime? _updatedAt;
+ bool _attached = true;
Map<String, dynamic>? _result;
@override
void initState() {
super.initState();
+ widget.foreground?.addListener(_scheduleRefresh);
_restart();
}
+ @override
+ void didChangeDependencies() {
+ super.didChangeDependencies();
+ _updateTickerMode();
+ }
+
+ @override
+ void activate() {
+ super.activate();
+ _attached = true;
+ _updateTickerMode();
+ }
+
+ @override
+ void deactivate() {
+ _attached = false;
+ _timer?.cancel();
+ super.deactivate();
+ }
+
+ void _updateTickerMode() {
+ final mode = TickerMode.getValuesNotifier(context);
+ if (!identical(mode, _tickerMode)) {
+ _tickerMode?.removeListener(_scheduleRefresh);
+ _tickerMode = mode..addListener(_scheduleRefresh);
+ }
+ _scheduleRefresh();
+ }
+
@override
void didUpdateWidget(PullRequestBadge oldWidget) {
super.didUpdateWidget(oldWidget);
- if (oldWidget.identity != widget.identity) _restart();
+ if (!identical(oldWidget.foreground, widget.foreground)) {
+ oldWidget.foreground?.removeListener(_scheduleRefresh);
+ widget.foreground?.addListener(_scheduleRefresh);
+ }
+ if (oldWidget.identity != widget.identity) {
+ _restart();
+ } else {
+ _scheduleRefresh();
+ }
}
void _restart() {
_timer?.cancel();
_result = null;
- final revision = ++_revision;
- unawaited(_refresh(revision));
+ _updatedAt = null;
+ ++_revision;
+ _scheduleRefresh();
+ }
+
+ bool get _visible =>
+ _attached &&
+ (_tickerMode?.value.enabled ?? false) &&
+ (widget.foreground?.value ?? true);
+
+ DateTime get _now => widget.now?.call() ?? DateTime.now();
+
+ void _scheduleRefresh() {
+ _timer?.cancel();
+ _timer = null;
+ if (!mounted || !_visible || _pendingRevision == _revision) return;
+ final revision = _revision;
+ final age = _updatedAt == null ? _interval : _now.difference(_updatedAt!);
+ if (age >= Duration.zero && age < _interval) {
+ _timer = Timer(_interval - age, () => _refresh(revision));
+ } else {
+ unawaited(_refresh(revision));
+ }
}
Future<void> _refresh(int revision) async {
+ if (!mounted ||
+ !_visible ||
+ revision != _revision ||
+ _pendingRevision == revision) {
+ return;
+ }
+ _pendingRevision = revision;
Map<String, dynamic> result;
try {
result = await widget.read();
} catch (_) {
result = {'status': 'unavailable'};
}
+ if (_pendingRevision == revision) _pendingRevision = null;
if (!mounted || revision != _revision) return;
- setState(() => _result = result);
- _timer = Timer(const Duration(seconds: 60), () => _refresh(revision));
+ setState(() {
+ _result = result;
+ _updatedAt = _now;
+ });
+ _scheduleRefresh();
}
@override
void dispose() {
_revision++;
_timer?.cancel();
+ _tickerMode?.removeListener(_scheduleRefresh);
+ widget.foreground?.removeListener(_scheduleRefresh);
super.dispose();
}
diff --git a/desktop/lib/widgets/terminal_panel.dart b/desktop/lib/widgets/terminal_panel.dart
index f8778f684..5cf1978fc 100644
--- a/desktop/lib/widgets/terminal_panel.dart
+++ b/desktop/lib/widgets/terminal_panel.dart
@@ -3036,6 +3036,7 @@ class _TerminalHeader extends StatelessWidget {
constraints: BoxConstraints(maxWidth: badgeWidth),
child: PullRequestBadge(
compact: narrow,
+ foreground: notifier.foreground,
identity: (
session.machineId,
agent.id,
diff --git a/desktop/test/pull_request_badge_test.dart b/desktop/test/pull_request_badge_test.dart
index 5217cc7b1..47b7c4406 100644
--- a/desktop/test/pull_request_badge_test.dart
+++ b/desktop/test/pull_request_badge_test.dart
@@ -11,9 +11,21 @@ void main() {
Object id,
Future<Map<String, dynamic>> Function() read, {
Future<bool> Function(Uri)? open,
+ ValueNotifier<bool>? foreground,
+ DateTime Function()? now,
+ bool visible = true,
}) => MaterialApp(
home: Scaffold(
- body: PullRequestBadge(identity: id, read: read, open: open),
+ body: TickerMode(
+ enabled: visible,
+ child: PullRequestBadge(
+ identity: id,
+ read: read,
+ open: open,
+ foreground: foreground,
+ now: now,
+ ),
+ ),
),
);
testWidgets('standalone PR and branch remain beside the model selector', (
@@ -84,6 +96,255 @@ void main() {
expect(find.byType(TextButton), findsNothing);
await tester.pumpWidget(const SizedBox());
});
+ testWidgets('a retained hidden pane does not start PR lookups', (
+ tester,
+ ) async {
+ var reads = 0;
+ Widget pane(bool visible) => MaterialApp(
+ home: TickerMode(
+ enabled: visible,
+ child: PullRequestBadge(
+ identity: 'branch',
+ read: () async {
+ reads++;
+ return {'status': 'none'};
+ },
+ ),
+ ),
+ );
+ await tester.pumpWidget(pane(false));
+ await tester.pump(const Duration(minutes: 5));
+ expect(reads, 0);
+ await tester.pumpWidget(pane(true));
+ await tester.pump();
+ expect(reads, 1);
+ await tester.pumpWidget(const SizedBox());
+ });
+ testWidgets(
+ '48 retained panes poll only four visible badges and stop in background',
+ (tester) async {
+ final foreground = ValueNotifier(true);
+ final reads = List.filled(48, 0);
+ await tester.pumpWidget(
+ MaterialApp(
+ home: Column(
+ children: [
+ for (var i = 0; i < reads.length; i++)
+ TickerMode(
+ enabled: i < 4,
+ child: PullRequestBadge(
+ identity: i,
+ foreground: foreground,
+ now: tester.binding.clock.now,
+ read: () async {
+ reads[i]++;
+ return {'status': 'none'};
+ },
+ ),
+ ),
+ ],
+ ),
+ ),
+ );
+ await tester.pump(const Duration(minutes: 5));
+ expect(reads.take(4), everyElement(6));
+ expect(reads.skip(4), everyElement(0));
+ foreground.value = false;
+ await tester.pump(const Duration(minutes: 30));
+ expect(reads.take(4), everyElement(6));
+ expect(reads.skip(4), everyElement(0));
+ foreground.value = true;
+ await tester.pump();
+ expect(reads.take(4), everyElement(7));
+ expect(reads.skip(4), everyElement(0));
+ await tester.pumpWidget(const SizedBox());
+ foreground.dispose();
+ },
+ );
+ testWidgets(
+ 'brief visibility changes retain cache and the original refresh deadline',
+ (tester) async {
+ final foreground = ValueNotifier(true);
+ var reads = 0;
+ Future<Map<String, dynamic>> read() async {
+ reads++;
+ return {
+ 'status': 'found',
+ 'number': 12,
+ 'state': 'Open',
+ 'url': 'https://github.com/acme/repo/pull/12',
+ };
+ }
+
+ Widget pane(bool visible) => frame(
+ 'branch',
+ read,
+ foreground: foreground,
+ now: tester.binding.clock.now,
+ visible: visible,
+ );
+ await tester.pumpWidget(pane(true));
+ await tester.pump(const Duration(seconds: 10));
+ await tester.pumpWidget(pane(false));
+ foreground.value = false;
+ await tester.pump(const Duration(seconds: 40));
+ foreground.value = true;
+ await tester.pumpWidget(pane(true));
+ expect(reads, 1);
+ expect(find.text('#12 Open'), findsOneWidget);
+ await tester.pump(const Duration(seconds: 10));
+ expect(reads, 2);
+ await tester.pumpWidget(pane(false));
+ await tester.pump(const Duration(minutes: 2));
+ expect(reads, 2);
+ await tester.pumpWidget(pane(true));
+ await tester.pump();
+ expect(reads, 3);
+ await tester.pumpWidget(const SizedBox());
+ foreground.dispose();
+ },
+ );
+ testWidgets('background and resume join an outstanding request', (
+ tester,
+ ) async {
+ final foreground = ValueNotifier(true);
+ final pending = Completer<Map<String, dynamic>>();
+ var reads = 0;
+ await tester.pumpWidget(
+ frame(
+ 'branch',
+ () {
+ reads++;
+ return pending.future;
+ },
+ foreground: foreground,
+ now: tester.binding.clock.now,
+ ),
+ );
+ foreground.value = false;
+ await tester.pump(const Duration(minutes: 2));
+ foreground.value = true;
+ await tester.pump();
+ expect(reads, 1);
+ foreground.value = false;
+ pending.complete({'status': 'none'});
+ await tester.pump();
+ await tester.pump(const Duration(seconds: 20));
+ foreground.value = true;
+ await tester.pump();
+ expect(reads, 1);
+ await tester.pumpWidget(const SizedBox());
+ foreground.value = false;
+ foreground.value = true;
+ await tester.pump(const Duration(minutes: 2));
+ expect(reads, 1);
+ foreground.dispose();
+ });
+ testWidgets(
+ 'a branch changed while hidden reads only when shown and rejects its old reply',
+ (tester) async {
+ final pending = Completer<Map<String, dynamic>>();
+ var oldReads = 0, newReads = 0;
+ await tester.pumpWidget(
+ frame('old', () {
+ oldReads++;
+ return pending.future;
+ }),
+ );
+ Future<Map<String, dynamic>> readNew() async {
+ newReads++;
+ return {'status': 'none'};
+ }
+
+ await tester.pumpWidget(frame('new', readNew, visible: false));
+ pending.complete({
+ 'status': 'found',
+ 'number': 12,
+ 'state': 'Open',
+ 'url': 'https://github.com/acme/repo/pull/12',
+ });
+ await tester.pump(const Duration(minutes: 2));
+ expect(oldReads, 1);
+ expect(newReads, 0);
+ expect(find.text('#12 Open'), findsNothing);
+ await tester.pumpWidget(frame('new', readNew));
+ await tester.pump();
+ expect(newReads, 1);
+ await tester.pumpWidget(const SizedBox());
+ },
+ );
+ testWidgets(
+ 'moving a retained badge between visibility scopes updates its subscription',
+ (tester) async {
+ final key = GlobalKey();
+ var reads = 0;
+ Widget layout(bool left) {
+ final badge = PullRequestBadge(
+ key: key,
+ identity: 'branch',
+ now: tester.binding.clock.now,
+ read: () async {
+ reads++;
+ return {'status': 'none'};
+ },
+ );
+ return MaterialApp(
+ home: Row(
+ children: [
+ TickerMode(enabled: true, child: left ? badge : const SizedBox()),
+ TickerMode(
+ enabled: false,
+ child: left ? const SizedBox() : badge,
+ ),
+ ],
+ ),
+ );
+ }
+
+ await tester.pumpWidget(layout(true));
+ expect(reads, 1);
+ await tester.pumpWidget(layout(false));
+ await tester.pump(const Duration(minutes: 5));
+ expect(reads, 1);
+ await tester.pumpWidget(layout(true));
+ await tester.pump();
+ expect(reads, 2);
+ await tester.pumpWidget(const SizedBox());
+ },
+ );
+ testWidgets(
+ 'replacing the foreground source stops listening to the old source',
+ (tester) async {
+ final old = ValueNotifier(true), current = ValueNotifier(false);
+ var reads = 0;
+ Future<Map<String, dynamic>> read() async {
+ reads++;
+ return {'status': 'none'};
+ }
+
+ await tester.pumpWidget(
+ frame('branch', read, foreground: old, now: tester.binding.clock.now),
+ );
+ await tester.pumpWidget(
+ frame(
+ 'branch',
+ read,
+ foreground: current,
+ now: tester.binding.clock.now,
+ ),
+ );
+ old.value = false;
+ old.value = true;
+ await tester.pump(const Duration(minutes: 2));
+ expect(reads, 1);
+ current.value = true;
+ await tester.pump();
+ expect(reads, 2);
+ await tester.pumpWidget(const SizedBox());
+ old.dispose();
+ current.dispose();
+ },
+ );
testWidgets('unavailable stays hidden and recovers on the next refresh', (
tester,
) async {
diff --git a/desktop/test/terminal_panel_header_test.dart b/desktop/test/terminal_panel_header_test.dart
index 254d5f089..29cdfa57c 100644
--- a/desktop/test/terminal_panel_header_test.dart
+++ b/desktop/test/terminal_panel_header_test.dart
@@ -24,20 +24,104 @@ class _PrNotifier extends AppNotifier {
authSession: AuthSession(),
configStore: null,
);
+ int prReads = 0;
@override
Future<Map<String, dynamic>> readAgentPullRequest(
String machineId,
String agentId,
- ) async => {
- 'status': 'found',
- 'number': 260,
- 'state': 'Draft',
- 'url': 'https://github.com/autonomous-ai/openharness/pull/260',
- };
+ ) async {
+ prReads++;
+ return {
+ 'status': 'found',
+ 'number': 260,
+ 'state': 'Draft',
+ 'url': 'https://github.com/autonomous-ai/openharness/pull/260',
+ };
+ }
+}
+
+/// Shared by the VM and native fixture: exercise the retained terminal header's
+/// actual visibility and foreground wiring with synthetic requests only.
+Future<void> verifyPanePrVisibility(WidgetTester tester) async {
+ final notifier = _PrNotifier();
+ final session = TerminalSession(
+ machineId: 'local',
+ agentId: 'agent-1',
+ agentName: 'Fixture',
+ engineId: 'codex',
+ send: (_, _) async => true,
+ sendBinary: (_) async => true,
+ )..status = TerminalSessionStatus.controlling;
+ notifier.machineStates['local'] = MachineState(
+ const Machine(
+ machineId: 'local',
+ name: 'Fixture',
+ authMode: MachineAuthMode.remote,
+ ),
+ );
+ void branch(String name) {
+ notifier.machineStates['local']!.agents = [
+ Agent(
+ id: 'agent-1',
+ name: 'Fixture',
+ engine: 'codex',
+ project: AgentProject(name: 'fixture', cwd: '/fixture', branch: name),
+ ),
+ ];
+ }
+
+ Widget frame(bool visible) => MaterialApp(
+ home: Scaffold(
+ body: SizedBox(
+ width: 900,
+ height: 320,
+ child: TerminalPanel(
+ notifier: notifier,
+ session: session,
+ focused: true,
+ visible: visible,
+ ),
+ ),
+ ),
+ );
+ try {
+ branch('first');
+ notifier.appLifecycleChanged(AppLifecycleState.hidden);
+ await tester.pumpWidget(frame(false));
+ await tester.pump();
+ expect(notifier.prReads, 0);
+ notifier.appLifecycleChanged(AppLifecycleState.resumed);
+ await tester.pump();
+ expect(notifier.prReads, 0);
+ await tester.pumpWidget(frame(true));
+ await tester.pump();
+ expect(notifier.prReads, 1);
+ expect(find.text('#260 Draft'), findsOneWidget);
+ notifier.appLifecycleChanged(AppLifecycleState.hidden);
+ branch('second');
+ await tester.pumpWidget(frame(true));
+ await tester.pump();
+ expect(notifier.prReads, 1);
+ expect(find.text('#260 Draft'), findsNothing);
+ notifier.appLifecycleChanged(AppLifecycleState.resumed);
+ await tester.pump();
+ expect(notifier.prReads, 2);
+ await tester.pump();
+ expect(find.text('#260 Draft'), findsOneWidget);
+ expect(tester.takeException(), isNull);
+ } finally {
+ await tester.pumpWidget(const SizedBox());
+ session.dispose();
+ notifier.dispose();
+ }
}
void main() {
setUpAll(loadRealFonts);
+ testWidgets(
+ 'retained terminal header respects pane and app visibility',
+ verifyPanePrVisibility,
+ );
TerminalSession sessionNamed(String name) {
final session = TerminalSession(
machineId: 'local',
diff --git a/desktop/test/workspace_pull_request_test.dart b/desktop/test/workspace_pull_request_test.dart
index 090dfd228..a4a72d0f5 100644
--- a/desktop/test/workspace_pull_request_test.dart
+++ b/desktop/test/workspace_pull_request_test.dart
@@ -1,5 +1,6 @@
import 'dart:async';
+import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:harness/core/models.dart';
import 'package:harness/core/pull_request_status.dart';
@@ -16,6 +17,157 @@ Map<String, dynamic> found(int number, [String state = 'Open']) => {
};
void main() {
+ testWidgets(
+ 'background workspace keeps its cached PR without polling and refreshes on return',
+ (tester) async {
+ final app = createApp();
+ app.stateOf('m')!.agents = const [
+ Agent(
+ id: 'a',
+ name: 'A',
+ engine: 'codex',
+ project: AgentProject(
+ name: 'repo',
+ cwd: '/repo',
+ root: '/repo',
+ branch: 'main',
+ ),
+ ),
+ ];
+ app.activeSwarm.panes.add(
+ TerminalPane(id: 1, machineId: 'm', agentId: 'a'),
+ );
+ app.activeSwarm.focusedPaneId = 1;
+ app.appLifecycleChanged(AppLifecycleState.hidden);
+ var reads = 0;
+ final controller = WorkspacePullRequest(
+ app,
+ now: tester.binding.clock.now,
+ read: (_, _) async => found(++reads),
+ );
+ await tester.pump(const Duration(minutes: 5));
+ expect(reads, 0);
+ app.appLifecycleChanged(AppLifecycleState.resumed);
+ await tester.pump();
+ expect(controller.value!.number, 1);
+ await tester.pump(const Duration(seconds: 10));
+ app.appLifecycleChanged(AppLifecycleState.inactive);
+ await tester.pump(const Duration(seconds: 40));
+ app.appLifecycleChanged(AppLifecycleState.resumed);
+ await tester.pump();
+ expect(reads, 1);
+ expect(controller.value!.number, 1);
+ await tester.pump(const Duration(seconds: 10));
+ expect(reads, 2);
+ app.appLifecycleChanged(AppLifecycleState.hidden);
+ await tester.pump(const Duration(minutes: 5));
+ expect(reads, 2);
+ app.appLifecycleChanged(AppLifecycleState.resumed);
+ await tester.pump();
+ expect(controller.value!.number, 3);
+ controller.dispose();
+ app.dispose();
+ },
+ );
+ testWidgets(
+ 'resume joins an outstanding workspace lookup and disposal prevents another poll',
+ (tester) async {
+ final app = createApp();
+ app.stateOf('m')!.agents = const [
+ Agent(
+ id: 'a',
+ name: 'A',
+ engine: 'codex',
+ project: AgentProject(
+ name: 'repo',
+ cwd: '/repo',
+ root: '/repo',
+ branch: 'main',
+ ),
+ ),
+ ];
+ app.activeSwarm.panes.add(
+ TerminalPane(id: 1, machineId: 'm', agentId: 'a'),
+ );
+ app.activeSwarm.focusedPaneId = 1;
+ var reads = 0;
+ final reply = Completer<Map<String, dynamic>>();
+ final controller = WorkspacePullRequest(
+ app,
+ now: tester.binding.clock.now,
+ read: (_, _) {
+ reads++;
+ return reply.future;
+ },
+ );
+ app.appLifecycleChanged(AppLifecycleState.hidden);
+ await tester.pump(const Duration(minutes: 2));
+ app.appLifecycleChanged(AppLifecycleState.resumed);
+ await tester.pump();
+ expect(reads, 1);
+ app.appLifecycleChanged(AppLifecycleState.hidden);
+ reply.complete(found(298));
+ await tester.pump();
+ await tester.pump(const Duration(seconds: 20));
+ app.appLifecycleChanged(AppLifecycleState.resumed);
+ await tester.pump();
+ expect(reads, 1);
+ expect(controller.value!.number, 298);
+ controller.dispose();
+ app.appLifecycleChanged(AppLifecycleState.hidden);
+ app.appLifecycleChanged(AppLifecycleState.resumed);
+ await tester.pump(const Duration(minutes: 2));
+ expect(reads, 1);
+ app.dispose();
+ },
+ );
+ testWidgets(
+ 'focus changes in background defer lookups to the final identity',
+ (tester) async {
+ final app = createApp();
+ app.stateOf('m')!.agents = [
+ for (final id in ['a', 'b', 'c'])
+ Agent(
+ id: id,
+ name: id,
+ engine: 'codex',
+ project: AgentProject(
+ name: 'repo',
+ cwd: '/repo-$id',
+ root: '/repo-$id',
+ branch: id,
+ ),
+ ),
+ ];
+ app.activeSwarm.panes.addAll([
+ for (var i = 0; i < 3; i++)
+ TerminalPane(id: i + 1, machineId: 'm', agentId: ['a', 'b', 'c'][i]),
+ ]);
+ app.activeSwarm.focusedPaneId = 1;
+ final old = Completer<Map<String, dynamic>>();
+ final reads = <String>[];
+ final controller = WorkspacePullRequest(
+ app,
+ read: (_, id) {
+ reads.add(id);
+ return id == 'a' ? old.future : Future.value(found(3));
+ },
+ );
+ app.appLifecycleChanged(AppLifecycleState.hidden);
+ app.focusPane(2);
+ app.focusPane(3);
+ old.complete(found(1));
+ await tester.pump(const Duration(minutes: 5));
+ expect(reads, ['a']);
+ expect(controller.value, isNull);
+ app.appLifecycleChanged(AppLifecycleState.resumed);
+ await tester.pump();
+ expect(reads, ['a', 'c']);
+ expect(controller.value!.number, 3);
+ controller.dispose();
+ app.dispose();
+ },
+ );
test('only valid PR states and GitHub links are actionable', () {
for (final state in ['Draft', 'Open', 'Merged', 'Closed']) {
expect(