From a5874babc04b2b59d03997c039ae53d0f1219478 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Thu, 22 Oct 2026 23:48:33 +0100 Subject: [PATCH 1/2] feat(app): persist pending pipeline run intents --- .../features/pipelines/pending_run_store.dart | 88 +++++++++++++++ .../pipelines/pipelines_controller.dart | 43 +++++++- .../pipelines/run_pipeline_intent.dart | 43 ++++++++ app/lib/features/pipelines/screen.dart | 13 ++- .../pipelines/pending_run_store_test.dart | 102 ++++++++++++++++++ 5 files changed, 280 insertions(+), 9 deletions(-) create mode 100644 app/lib/features/pipelines/pending_run_store.dart create mode 100644 app/test/features/pipelines/pending_run_store_test.dart diff --git a/app/lib/features/pipelines/pending_run_store.dart b/app/lib/features/pipelines/pending_run_store.dart new file mode 100644 index 00000000..3a41e2e4 --- /dev/null +++ b/app/lib/features/pipelines/pending_run_store.dart @@ -0,0 +1,88 @@ +import 'dart:convert'; + +import 'package:shared_preferences/shared_preferences.dart'; + +import 'run_pipeline_intent.dart'; + +abstract interface class PendingRunStore { + Future read(int pipelineId, int version); + Future write(RunPipelineIntent intent); + Future remove(int pipelineId, int version); +} + +String pendingRunStorageKey(String endpoint, int pipelineId, int version) => + 'pipelines.web.pendingRun.v1:$endpoint:$pipelineId:$version'; + +class SharedPreferencesPendingRunStore implements PendingRunStore { + const SharedPreferencesPendingRunStore(this.endpoint); + final String endpoint; + + Future _preferences() async { + try { + return await SharedPreferences.getInstance(); + } catch (_) { + return null; + } + } + + String _key(int id, int version) => + pendingRunStorageKey(endpoint, id, version); + + @override + Future read(int pipelineId, int version) async { + try { + final raw = (await _preferences())?.getString(_key(pipelineId, version)); + if (raw == null) return null; + final intent = RunPipelineIntent.fromJson(jsonDecode(raw)); + return intent?.matches(pipelineId, version) == true ? intent : null; + } catch (_) { + return null; + } + } + + @override + Future write(RunPipelineIntent intent) async { + try { + await (await _preferences())?.setString( + _key(intent.pipelineId, intent.version), + jsonEncode(intent.toJson()), + ); + } catch (_) {} + } + + @override + Future remove(int pipelineId, int version) async { + try { + await (await _preferences())?.remove(_key(pipelineId, version)); + } catch (_) {} + } +} + +class MemoryPendingRunStore implements PendingRunStore { + final Map values = {}; + final String endpoint; + MemoryPendingRunStore({this.endpoint = ''}); + String _key(int id, int version) => + pendingRunStorageKey(endpoint, id, version); + @override + Future read(int id, int version) async { + try { + final intent = RunPipelineIntent.fromJson( + jsonDecode(values[_key(id, version)]!), + ); + return intent?.matches(id, version) == true ? intent : null; + } catch (_) { + return null; + } + } + + @override + Future write(RunPipelineIntent intent) async => + values[_key(intent.pipelineId, intent.version)] = jsonEncode( + intent.toJson(), + ); + @override + Future remove(int id, int version) async { + values.remove(_key(id, version)); + } +} diff --git a/app/lib/features/pipelines/pipelines_controller.dart b/app/lib/features/pipelines/pipelines_controller.dart index 1a489cd9..29455a72 100644 --- a/app/lib/features/pipelines/pipelines_controller.dart +++ b/app/lib/features/pipelines/pipelines_controller.dart @@ -4,6 +4,7 @@ import 'pipeline_models.dart'; import 'pipeline_query.dart'; import 'pipeline_repository.dart'; import 'run_pipeline_intent.dart'; +import 'pending_run_store.dart'; /// The user-facing message for a request failure, keeping server refusals /// verbatim and removing only the generic `Exception:` prefix. @@ -17,9 +18,14 @@ String pipelineErrorMessage(Object error) { /// every request is sequenced here so a late reply can never replace a newer /// selection. class PipelinesController extends ChangeNotifier { - PipelinesController(this._repository); + PipelinesController(this._repository, {PendingRunStore? pendingRunStore}) + : _pendingRunStore = + pendingRunStore ?? const SharedPreferencesPendingRunStore(''); final PipelinesRepository _repository; + final PendingRunStore _pendingRunStore; + final Set _loadedPendingRuns = {}; + final Map _pendingRunGenerations = {}; List _pipelines = const []; List _runs = const []; @@ -213,17 +219,44 @@ class PipelinesController extends ChangeNotifier { } /// The unresolved launch for one pinned definition, if any. - RunPipelineIntent? pendingRunFor(int pipelineId, int version) => - _pendingRuns['$pipelineId@$version']; + RunPipelineIntent? pendingRunFor(int pipelineId, int version) { + final key = '$pipelineId@$version'; + if (!_loadedPendingRuns.contains(key)) { + _loadedPendingRuns.add(key); + final generation = _pendingRunGenerations[key] ?? 0; + _pendingRunStore.read(pipelineId, version).then((intent) { + if (_disposed || (_pendingRunGenerations[key] ?? 0) != generation) { + return; + } + if (intent != null) _pendingRuns[key] = intent; + _notify(); + }); + } + return _pendingRuns[key]; + } /// Remembers an intent whose admission is unknown, or clears it once the /// server's truth is known. void rememberPendingRun(RunPipelineIntent intent) { - _pendingRuns['${intent.pipelineId}@${intent.version}'] = intent; + final key = '${intent.pipelineId}@${intent.version}'; + _pendingRunGenerations.update( + key, + (generation) => generation + 1, + ifAbsent: () => 1, + ); + _pendingRuns[key] = intent; + _pendingRunStore.write(intent); } void clearPendingRun(int pipelineId, int version) { - _pendingRuns.remove('$pipelineId@$version'); + final key = '$pipelineId@$version'; + _pendingRunGenerations.update( + key, + (generation) => generation + 1, + ifAbsent: () => 1, + ); + _pendingRuns.remove(key); + _pendingRunStore.remove(pipelineId, version); } bool _current(int sequence, int token) => !_disposed && sequence == token; diff --git a/app/lib/features/pipelines/run_pipeline_intent.dart b/app/lib/features/pipelines/run_pipeline_intent.dart index ce66548d..c9f8c749 100644 --- a/app/lib/features/pipelines/run_pipeline_intent.dart +++ b/app/lib/features/pipelines/run_pipeline_intent.dart @@ -58,6 +58,49 @@ class RunPipelineIntent { final RunOutputFormat outputFormat; final String requestKey; + JsonMap toJson() => { + 'pipelineId': pipelineId, + 'version': version, + 'prompt': prompt, + 'workspace': workspace, + 'notifyMode': notifyMode.name, + 'outputFormat': outputFormat.name, + 'requestKey': requestKey, + }; + + static RunPipelineIntent? fromJson(Object? value) { + if (value is! Map) return null; + final id = value['pipelineId']; + final version = value['version']; + final prompt = value['prompt']; + final workspace = value['workspace']; + final requestKey = value['requestKey']; + final notifyMode = RunNotifyMode.values.where( + (item) => item.name == value['notifyMode'], + ); + final outputFormat = RunOutputFormat.values.where( + (item) => item.name == value['outputFormat'], + ); + if (id is! int || + version is! int || + prompt is! String || + workspace is! String || + requestKey is! String || + notifyMode.length != 1 || + outputFormat.length != 1) { + return null; + } + return RunPipelineIntent( + pipelineId: id, + version: version, + prompt: prompt, + workspace: workspace, + requestKey: requestKey, + notifyMode: notifyMode.first, + outputFormat: outputFormat.first, + ); + } + /// The same key is only ever reused for the same pinned definition. bool matches(int pipelineId, int version) => this.pipelineId == pipelineId && this.version == version; diff --git a/app/lib/features/pipelines/screen.dart b/app/lib/features/pipelines/screen.dart index 590281c5..0aefb55e 100644 --- a/app/lib/features/pipelines/screen.dart +++ b/app/lib/features/pipelines/screen.dart @@ -5,6 +5,7 @@ import '../../core/api_gateway.dart'; import 'pipeline_models.dart'; import 'pipeline_repository.dart'; import 'pipelines_controller.dart'; +import 'pending_run_store.dart'; import 'widgets/pipeline_controls.dart'; import 'widgets/pipeline_detail_panel.dart'; import 'widgets/pipeline_list_panel.dart'; @@ -81,8 +82,10 @@ class _PipelinesScreenState extends State { @override void initState() { super.initState(); - _controller = PipelinesController(PipelinesRepository(widget.gateway)) - ..load(); + _controller = PipelinesController( + PipelinesRepository(widget.gateway), + pendingRunStore: const SharedPreferencesPendingRunStore(''), + )..load(); final id = widget.initialPipelineId; if (id != null) { // The detail read owns its own failure state; the list read continues. @@ -95,8 +98,10 @@ class _PipelinesScreenState extends State { super.didUpdateWidget(oldWidget); if (oldWidget.gateway != widget.gateway) { _controller.dispose(); - _controller = PipelinesController(PipelinesRepository(widget.gateway)) - ..load(); + _controller = PipelinesController( + PipelinesRepository(widget.gateway), + pendingRunStore: const SharedPreferencesPendingRunStore(''), + )..load(); } else if (oldWidget.syncToken != widget.syncToken) { // The socket was replaced; re-read through the existing controller so the // selected pipeline and requested version survive the reconnect. diff --git a/app/test/features/pipelines/pending_run_store_test.dart b/app/test/features/pipelines/pending_run_store_test.dart new file mode 100644 index 00000000..1a2aa71b --- /dev/null +++ b/app/test/features/pipelines/pending_run_store_test.dart @@ -0,0 +1,102 @@ +import 'dart:async'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/pipelines/pipeline_repository.dart'; +import 'package:pipelines_app/features/pipelines/pipelines_controller.dart'; +import 'package:pipelines_app/features/pipelines/pending_run_store.dart'; +import 'package:pipelines_app/features/pipelines/run_pipeline_intent.dart'; + +import 'fixtures.dart'; + +const _storedIntent = RunPipelineIntent( + pipelineId: 7, + version: 3, + prompt: 'stored', + workspace: '/work', + notifyMode: RunNotifyMode.notify, + requestKey: 'stored-key', +); + +class _DelayedStore implements PendingRunStore { + final Completer readResult = + Completer(); + + @override + Future read(int pipelineId, int version) => + readResult.future; + + @override + Future remove(int pipelineId, int version) async {} + + @override + Future write(RunPipelineIntent intent) async {} +} + +void main() { + test( + 'intent survives store reuse; clear and identities are isolated', + () async { + final store = MemoryPendingRunStore(); + const intent = RunPipelineIntent( + pipelineId: 7, + version: 3, + prompt: 'do it', + workspace: '/work', + notifyMode: RunNotifyMode.notify, + requestKey: 'same-key', + ); + await store.write(intent); + expect((await MemoryPendingRunStore(endpoint: '').read(7, 3)), isNull); + expect((await store.read(7, 3))?.requestKey, 'same-key'); + expect(await store.read(7, 4), isNull); + expect(await store.read(8, 3), isNull); + store.values[pendingRunStorageKey('', 7, 4)] = '{bad'; + expect(await store.read(7, 4), isNull); + store.values[pendingRunStorageKey('', 7, 4)] = intent.toJson().toString(); + expect(await store.read(7, 4), isNull); + await store.remove(7, 3); + expect(await store.read(7, 3), isNull); + }, + ); + + test( + 'a delayed read cannot restore an intent after it was cleared', + () async { + final store = _DelayedStore(); + final controller = PipelinesController( + PipelinesRepository( + FakeGateway((type, fields) => errorReply('unused')), + ), + pendingRunStore: store, + ); + expect(controller.pendingRunFor(7, 3), isNull); + controller.clearPendingRun(7, 3); + store.readResult.complete(_storedIntent); + await Future.delayed(Duration.zero); + expect(controller.pendingRunFor(7, 3), isNull); + controller.dispose(); + }, + ); + + test('a delayed read cannot replace a newer remembered intent', () async { + final store = _DelayedStore(); + final controller = PipelinesController( + PipelinesRepository(FakeGateway((type, fields) => errorReply('unused'))), + pendingRunStore: store, + ); + expect(controller.pendingRunFor(7, 3), isNull); + const newer = RunPipelineIntent( + pipelineId: 7, + version: 3, + prompt: 'newer', + workspace: '/work', + notifyMode: RunNotifyMode.notify, + requestKey: 'newer-key', + ); + controller.rememberPendingRun(newer); + store.readResult.complete(_storedIntent); + await Future.delayed(Duration.zero); + expect(controller.pendingRunFor(7, 3)?.requestKey, 'newer-key'); + controller.dispose(); + }); +} From a82560be1aa37387ef6a4a1597f142b280787988 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Thu, 22 Oct 2026 23:50:18 +0100 Subject: [PATCH 2/2] fix(app): the Run dialog waits for the stored pending intent after a reload --- .../pipelines/pipelines_controller.dart | 17 ++++++++ .../pipelines/ui/run_pipeline_action.dart | 7 ++- .../ui/run_pipeline_launch_test.dart | 43 +++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/app/lib/features/pipelines/pipelines_controller.dart b/app/lib/features/pipelines/pipelines_controller.dart index 29455a72..e95817ba 100644 --- a/app/lib/features/pipelines/pipelines_controller.dart +++ b/app/lib/features/pipelines/pipelines_controller.dart @@ -218,6 +218,23 @@ class PipelinesController extends ChangeNotifier { _notify(); } + /// The unresolved launch for one pinned definition, read from the device + /// store first, so the Run dialog opened right after a reload offers the + /// same request key instead of minting a new one. + Future loadPendingRun(int pipelineId, int version) async { + final key = '$pipelineId@$version'; + if (_loadedPendingRuns.add(key)) { + final generation = _pendingRunGenerations[key] ?? 0; + final intent = await _pendingRunStore.read(pipelineId, version); + if (!_disposed && + (_pendingRunGenerations[key] ?? 0) == generation && + intent != null) { + _pendingRuns[key] = intent; + } + } + return _pendingRuns[key]; + } + /// The unresolved launch for one pinned definition, if any. RunPipelineIntent? pendingRunFor(int pipelineId, int version) { final key = '$pipelineId@$version'; diff --git a/app/lib/features/pipelines/ui/run_pipeline_action.dart b/app/lib/features/pipelines/ui/run_pipeline_action.dart index 7a58cde5..c8e953fe 100644 --- a/app/lib/features/pipelines/ui/run_pipeline_action.dart +++ b/app/lib/features/pipelines/ui/run_pipeline_action.dart @@ -48,11 +48,16 @@ class _RunPipelineActionState extends State { if (_dialogOpen) return; _dialogOpen = true; try { + final pending = await widget.controller.loadPendingRun( + _pipelineId, + _version, + ); + if (!mounted) return; await showRunPipelineDialog( context: context, detail: widget.detail, gateway: widget.gateway, - pending: widget.controller.pendingRunFor(_pipelineId, _version), + pending: pending, onSubmit: _submit, onPendingChanged: (intent) { if (intent == null) { diff --git a/app/test/features/pipelines/ui/run_pipeline_launch_test.dart b/app/test/features/pipelines/ui/run_pipeline_launch_test.dart index c1724324..f065fbcb 100644 --- a/app/test/features/pipelines/ui/run_pipeline_launch_test.dart +++ b/app/test/features/pipelines/ui/run_pipeline_launch_test.dart @@ -435,6 +435,49 @@ void main() { expect(find.byType(AlertDialog), findsNothing); }); + testWidgets('an unknown launch survives a reload with the same key', ( + tester, + ) async { + SharedPreferences.setMockInitialValues({}); + final fixture = PipelineFixture(); + final navigated = []; + final keys = []; + var attempts = 0; + final gateway = launchGateway( + fixture, + onLaunch: (fields) { + attempts++; + keys.add(fields['request_key'] as String); + if (attempts == 1) throw StateError('Connection closed'); + return acceptedRun(87); + }, + onLookup: (_) => noRun(), + ); + await pumpScreen(tester, gateway, navigated); + await openDetail(tester); + await tester.tap(find.byKey(runPipelineActionKey)); + await tester.pumpAndSettle(); + await fillFields(tester); + await tester.tap(find.byKey(runPipelineConfirmKey)); + await tester.pumpAndSettle(); + expect(find.text(runPipelineCheckRetryLabel), findsOneWidget); + + // A reload: the screen and its controller are gone; only the device + // store remains. + await tester.pumpWidget(const SizedBox()); + await pumpScreen(tester, gateway, navigated); + await openDetail(tester); + await tester.tap(find.byKey(runPipelineActionKey)); + await tester.pumpAndSettle(); + expect(find.text(runPipelineCheckRetryLabel), findsOneWidget); + + await tester.tap(find.byKey(runPipelineConfirmKey)); + await tester.pumpAndSettle(); + expect(keys, hasLength(2)); + expect(keys[1], keys[0]); + expect(navigated, [87]); + }); + testWidgets('closing and reopening an unknown launch restores check/retry', ( tester, ) async {