diff --git a/app/lib/features/runs/ui/runs_controller.dart b/app/lib/features/runs/ui/runs_controller.dart index 9eac5db5..977d8223 100644 --- a/app/lib/features/runs/ui/runs_controller.dart +++ b/app/lib/features/runs/ui/runs_controller.dart @@ -19,10 +19,16 @@ class RunsController extends ChangeNotifier { timeRange = 'any_time'; final Set statuses = {}; int? pipelineId; - int _selection = 0; + // List and detail requests advance independent generations so neither can + // make the other's in-flight reply stale. + int _listGeneration = 0, _detailGeneration = 0; num? _selectedId; - Timer? _refresh; - bool _refreshInFlight = false, _refreshAgain = false, _disposed = false; + Timer? _detailRefresh; + bool _listResyncing = false, _listAgain = false; + bool _detailRefreshing = false, _detailAgain = false; + bool _disposed = false; + + num? get selectedRunId => _selectedId; void listen(Stream frames) { _subscription?.cancel(); @@ -32,56 +38,56 @@ class RunsController extends ChangeNotifier { } Future load([int? initialId]) async { - final token = ++_selection; + final token = ++_listGeneration; + _listAgain = false; loading = true; error = null; notifyListeners(); try { final result = await repository.listRuns(); - if (_disposed || token != _selection) return; + if (!_listOwned(token)) return; runs = result.runs; usage = result.usage; - if (initialId != null) { - // Keep list and detail request generations independent: selecting the - // initial row must not make this list request's completion stale. - await select(initialId); - } } catch (e) { - if (!_disposed && token == _selection) error = '$e'; - } - if (!_disposed && token == _selection) { - loading = false; - notifyListeners(); + if (!_listOwned(token)) return; + error = '$e'; } + if (!_listOwned(token)) return; + loading = false; + notifyListeners(); + // A run event may have arrived while the list request was in flight; one + // resync after it keeps the pushed state without dropping the event. + _resyncAgainIfNeeded(); + if (initialId != null) await select(initialId); } Future select(num id) async { - final token = ++_selection; - _refresh?.cancel(); - _refresh = null; - _refreshAgain = false; + final token = ++_detailGeneration; + _cancelDetailRefresh(); + // Keep a same-run detail while it refreshes, but never present the previous + // run as if it were the newly selected one. + if (_selectedId != id) detail = null; _selectedId = id; detailLoading = true; error = null; notifyListeners(); try { final value = await repository.getRun(id); - if (!_disposed && token == _selection) detail = value; + if (!_detailOwned(id, token)) return; + detail = value; } catch (e) { - if (!_disposed && token == _selection) error = '$e'; - } - if (!_disposed && token == _selection) { - detailLoading = false; - notifyListeners(); + if (!_detailOwned(id, token)) return; + error = '$e'; } + if (!_detailOwned(id, token)) return; + detailLoading = false; + notifyListeners(); } void back() { - ++_selection; + ++_detailGeneration; + _cancelDetailRefresh(); _selectedId = null; - _refresh?.cancel(); - _refresh = null; - _refreshAgain = false; detail = null; detailLoading = false; error = null; @@ -126,77 +132,107 @@ class RunsController extends ChangeNotifier { if (frame['run_id'] == null || folded.needsResync) { _scheduleListRefresh(); } - if (_selectedId != null && - frame['run_id'] == _selectedId && + final selected = _selectedId; + if (selected != null && + frame['run_id'] == selected && (folded.needsResync || runDetailNeedsFullSync(frame))) { - if (_refreshInFlight) { - _refreshAgain = true; - } else { - _refresh?.cancel(); - final id = _selectedId!; - final generation = _selection; - _refresh = Timer( - const Duration(milliseconds: 150), - () => _refreshDetail(id, generation), - ); - } + _scheduleDetailRefresh(selected); } notifyListeners(); } void _scheduleListRefresh() { - if (_disposed || loading) return; + if (_disposed) return; + if (_listResyncing || loading) { + _listAgain = true; + return; + } + _listResyncing = true; loading = true; notifyListeners(); unawaited(_reloadList()); } Future _reloadList() async { + final token = ++_listGeneration; + _listAgain = false; try { final result = await repository.listRuns(); - if (_disposed) return; + if (!_listOwned(token)) return; runs = result.runs; usage = result.usage; error = null; } catch (e) { - if (!_disposed) error = '$e'; + if (_listOwned(token)) error = '$e'; } finally { - if (!_disposed) { + _listResyncing = false; + if (_listOwned(token)) { loading = false; notifyListeners(); } + // A followup may have been requested while this (possibly superseded) + // request was still the one marked in flight. + _resyncAgainIfNeeded(); } } - Future _refreshDetail(num id, int generation) async { - if (_disposed || generation != _selection || _selectedId != id) return; - _refreshInFlight = true; + void _resyncAgainIfNeeded() { + if (_disposed || !_listAgain) return; + _listAgain = false; + _scheduleListRefresh(); + } + + void _scheduleDetailRefresh(num id) { + if (_disposed || _selectedId != id) return; + if (_detailRefreshing) { + _detailAgain = true; + return; + } + _detailRefresh?.cancel(); + _detailRefresh = Timer( + const Duration(milliseconds: 150), + () => _refreshDetail(id, _detailGeneration), + ); + } + + Future _refreshDetail(num id, int token) async { + _detailRefresh = null; + if (!_detailOwned(id, token)) return; + _detailRefreshing = true; try { final value = await repository.getRun(id); - if (!_disposed && generation == _selection && _selectedId == id) { + if (_detailOwned(id, token)) { detail = value; notifyListeners(); } } catch (e) { - if (!_disposed && generation == _selection && _selectedId == id) { + if (_detailOwned(id, token)) { error = '$e'; notifyListeners(); } } finally { - _refreshInFlight = false; - if (!_disposed && - _refreshAgain && - generation == _selection && - _selectedId == id) { - _refreshAgain = false; - _refresh = Timer( - const Duration(milliseconds: 150), - () => _refreshDetail(id, generation), - ); + if (_detailOwned(id, token)) { + _detailRefreshing = false; + if (_detailAgain) { + _detailAgain = false; + unawaited(_refreshDetail(id, token)); + } } } } + void _cancelDetailRefresh() { + _detailRefresh?.cancel(); + _detailRefresh = null; + _detailAgain = false; + _detailRefreshing = false; + } + + bool _listOwned(int token) => !_disposed && token == _listGeneration; + + bool _detailOwned(num id, int token) => + !_disposed && token == _detailGeneration && _selectedId == id; + List get visibleRuns => sortRuns( filterRuns( runs, @@ -218,7 +254,7 @@ class RunsController extends ChangeNotifier { void dispose() { _disposed = true; _subscription?.cancel(); - _refresh?.cancel(); + _detailRefresh?.cancel(); super.dispose(); } } diff --git a/app/notes/R1b.md b/app/notes/R1b.md new file mode 100644 index 00000000..406dec62 --- /dev/null +++ b/app/notes/R1b.md @@ -0,0 +1,55 @@ +# R1b — runs controller request independence + +Focused correctness fix on retained candidate `6b8d5ea`. Files: +`lib/features/runs/ui/runs_controller.dart`, +`test/features/runs/ui/runs_controller_test.dart`. + +## Fixes + +- List and detail requests now use independent generations (`_listGeneration`, + `_detailGeneration`). `load(initialId)` no longer bumps the detail generation + that `select(initialId)` also advances, so both `loading` and `detailLoading` + clear when the requests resolve. Manual list loads also no longer invalidate + valid detail refreshes. +- List resyncs (`_reloadList`) capture and check a list generation, so a stale + reply cannot overwrite a newer `load()`. Concurrent resync requests are + coalesced into exactly one followup; a resync requested during a manual load + runs after that load instead of being dropped. +- Detail refreshes capture the current detail generation and selected run. + Same-id refreshes keep the existing detail and are coalesced (in-flight plus + one followup). Selecting a different run clears the previous detail instead of + presenting it as the new run. Back/dispose invalidate pending replies, and + stale requests cannot clear another generation's loading flags. +- Added `num? get selectedRunId` for the native UI lane. All existing public + methods and fields are preserved. + +The existing 150 ms event debounce is retained; no polling or extra timers were +introduced. File is 260 lines; the fix stays within the controller responsibility +rather than fragmenting it. + +## Tests + +Extended `test/features/runs/ui/runs_controller_test.dart` with regressions for: + +- `load(initialId)` resolves with `loading == false` and `detailLoading == false`. +- Overlapping list loads with reversed completion order: newer reply wins. +- List resync leaves the selected detail request valid. +- Manual `load()` does not invalidate an in-flight detail refresh. +- Sparse unknown run events coalesce into one followup resync. +- A followup requested while a superseded resync is still marked in flight + starts once that stale reply arrives instead of being stranded. +- Selected refresh coalescing (in-flight plus followup) and Back ignoring the + pending reply. +- Choosing another run does not display the previous run's detail. +- Pending detail refresh is safe after dispose. + +Existing six regression tests remain and pass. + +## Verification + +- `/Users/alan/development/flutter/bin/flutter test test/features/runs/ui` — 15 tests passed. +- `/Users/alan/development/flutter/bin/flutter test test/features/runs` — 26 tests passed. +- `/Users/alan/development/flutter/bin/flutter analyze` — no issues found. +- `flutter build web` not run in this NONVISUAL controller slice; manager + integration/UI review owns rendered and build acceptance. +- Mobile remains unverified; no device or browser tools used. diff --git a/app/test/features/runs/ui/runs_controller_test.dart b/app/test/features/runs/ui/runs_controller_test.dart index 3b764422..0945f77b 100644 --- a/app/test/features/runs/ui/runs_controller_test.dart +++ b/app/test/features/runs/ui/runs_controller_test.dart @@ -129,4 +129,216 @@ void main() { expect(controller.detail?['status'], 'succeeded'); }, ); + + test('load with initial id clears list and detail loading', () async { + final load = controller.load(7); + expect(controller.loading, isTrue); + gateway.listReply(0, [ + {'id': 7}, + {'id': 8}, + ]); + await Future.delayed(Duration.zero); + expect(gateway.details[7], hasLength(1)); + expect(controller.loading, isFalse); + expect(controller.detailLoading, isTrue); + gateway.detailReply(7, 0, {'id': 7}); + await load; + expect(controller.loading, isFalse); + expect(controller.detailLoading, isFalse); + expect(controller.detail?['id'], 7); + expect(controller.selectedRunId, 7); + }); + + test('newer list load wins when replies complete out of order', () async { + final first = controller.load(); + final second = controller.load(); + gateway.listReply(1, [ + {'id': 2}, + ]); + await second; + gateway.listReply(0, [ + {'id': 1}, + ]); + await first; + expect(controller.runs.single['id'], 2); + expect(controller.loading, isFalse); + }); + + test('list resync leaves the selected detail request valid', () async { + final select = controller.select(1); + gateway.detailReply(1, 0, {'id': 1, 'status': 'running'}); + await select; + controller.listen(gateway.frames); + gateway.framesController.add({'type': 'run_event', 'status': 'running'}); + await Future.delayed(Duration.zero); + expect(gateway.listCalls, 1); + gateway.listReply(0, [ + {'id': 1, 'status': 'running'}, + ]); + await Future.delayed(Duration.zero); + expect(controller.detail?['id'], 1); + expect(controller.selectedRunId, 1); + expect(gateway.details[1], hasLength(1)); + }); + + test('manual list load keeps an in-flight detail refresh valid', () async { + final select = controller.select(1); + gateway.detailReply(1, 0, {'id': 1, 'status': 'running'}); + await select; + controller.listen(gateway.frames); + gateway.framesController.add({ + 'type': 'run_event', + 'run_id': 1, + 'status': 'succeeded', + }); + await Future.delayed(const Duration(milliseconds: 180)); + expect(gateway.details[1], hasLength(2)); + final load = controller.load(); + gateway.listReply(1, [ + {'id': 1}, + ]); + await load; + gateway.detailReply(1, 1, {'id': 1, 'status': 'succeeded'}); + await Future.delayed(Duration.zero); + expect(controller.detail?['status'], 'succeeded'); + expect(controller.detailLoading, isFalse); + gateway.listReply(0, [ + {'id': 1}, + ]); + }); + + test('event during a superseded resync still runs one followup', () async { + controller.listen(gateway.frames); + gateway.framesController.add({ + 'type': 'run_event', + 'run_id': 9, + 'status': 'running', + }); + await Future.delayed(Duration.zero); + expect(gateway.listCalls, 1); + final load = controller.load(); + expect(gateway.listCalls, 2); + gateway.listReply(1, [ + {'id': 1}, + ]); + await load; + gateway.framesController.add({ + 'type': 'run_event', + 'run_id': 10, + 'status': 'running', + }); + await Future.delayed(Duration.zero); + expect(gateway.listCalls, 2); + gateway.listReply(0, [ + {'id': 1}, + ]); + await Future.delayed(Duration.zero); + expect(gateway.listCalls, 3); + gateway.listReply(2, [ + {'id': 1}, + {'id': 2}, + ]); + await Future.delayed(Duration.zero); + expect(controller.runs, hasLength(2)); + expect(controller.loading, isFalse); + }); + + test('unknown run events coalesce into one followup resync', () async { + controller.listen(gateway.frames); + gateway.framesController.add({ + 'type': 'run_event', + 'run_id': 9, + 'status': 'running', + }); + await Future.delayed(Duration.zero); + expect(gateway.listCalls, 1); + expect(controller.loading, isTrue); + gateway.framesController.add({ + 'type': 'run_event', + 'run_id': 10, + 'status': 'running', + }); + await Future.delayed(Duration.zero); + expect(gateway.listCalls, 1); + gateway.listReply(0, [ + {'id': 3}, + ]); + await Future.delayed(Duration.zero); + expect(gateway.listCalls, 2); + expect(controller.loading, isTrue); + gateway.listReply(1, [ + {'id': 3}, + {'id': 4}, + ]); + await Future.delayed(Duration.zero); + expect(gateway.listCalls, 2); + expect(controller.loading, isFalse); + expect(controller.runs, hasLength(2)); + }); + + test( + 'selected refresh coalesces and Back ignores the pending reply', + () async { + final select = controller.select(1); + gateway.detailReply(1, 0, {'id': 1, 'status': 'running'}); + await select; + controller.listen(gateway.frames); + gateway.framesController.add({ + 'type': 'run_event', + 'run_id': 1, + 'status': 'succeeded', + }); + await Future.delayed(const Duration(milliseconds: 180)); + expect(gateway.details[1], hasLength(2)); + gateway.framesController.add({ + 'type': 'run_event', + 'run_id': 1, + 'status': 'failed', + }); + await Future.delayed(Duration.zero); + expect(gateway.details[1], hasLength(2)); + gateway.detailReply(1, 1, {'id': 1, 'status': 'succeeded'}); + await Future.delayed(Duration.zero); + expect(gateway.details[1], hasLength(3)); + controller.back(); + gateway.detailReply(1, 2, {'id': 1, 'status': 'failed'}); + await Future.delayed(Duration.zero); + expect(controller.detail, isNull); + expect(controller.detailLoading, isFalse); + expect(controller.error, isNull); + }, + ); + + test('choosing another run does not display the previous detail', () async { + final first = controller.select(1); + gateway.detailReply(1, 0, {'id': 1}); + await first; + final second = controller.select(2); + expect(controller.detail, isNull); + expect(controller.selectedRunId, 2); + expect(controller.detailLoading, isTrue); + gateway.detailReply(2, 0, {'id': 2}); + await second; + expect(controller.detail?['id'], 2); + expect(controller.detailLoading, isFalse); + }); + + test('pending detail refresh is safe after dispose', () async { + final select = controller.select(1); + gateway.detailReply(1, 0, {'id': 1, 'status': 'running'}); + await select; + controller.listen(gateway.frames); + gateway.framesController.add({ + 'type': 'run_event', + 'run_id': 1, + 'status': 'succeeded', + }); + await Future.delayed(const Duration(milliseconds: 180)); + expect(gateway.details[1], hasLength(2)); + controller.dispose(); + disposed = true; + gateway.detailReply(1, 1, {'id': 1, 'status': 'succeeded'}); + await Future.delayed(Duration.zero); + expect(controller.detail?['id'], 1); + }); }