merge(app): land run-intent-persist (run 159)
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'run_pipeline_intent.dart';
|
||||
|
||||
abstract interface class PendingRunStore {
|
||||
Future<RunPipelineIntent?> read(int pipelineId, int version);
|
||||
Future<void> write(RunPipelineIntent intent);
|
||||
Future<void> 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<SharedPreferences?> _preferences() async {
|
||||
try {
|
||||
return await SharedPreferences.getInstance();
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
String _key(int id, int version) =>
|
||||
pendingRunStorageKey(endpoint, id, version);
|
||||
|
||||
@override
|
||||
Future<RunPipelineIntent?> 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<void> write(RunPipelineIntent intent) async {
|
||||
try {
|
||||
await (await _preferences())?.setString(
|
||||
_key(intent.pipelineId, intent.version),
|
||||
jsonEncode(intent.toJson()),
|
||||
);
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> remove(int pipelineId, int version) async {
|
||||
try {
|
||||
await (await _preferences())?.remove(_key(pipelineId, version));
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
|
||||
class MemoryPendingRunStore implements PendingRunStore {
|
||||
final Map<String, String> values = {};
|
||||
final String endpoint;
|
||||
MemoryPendingRunStore({this.endpoint = ''});
|
||||
String _key(int id, int version) =>
|
||||
pendingRunStorageKey(endpoint, id, version);
|
||||
@override
|
||||
Future<RunPipelineIntent?> 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<void> write(RunPipelineIntent intent) async =>
|
||||
values[_key(intent.pipelineId, intent.version)] = jsonEncode(
|
||||
intent.toJson(),
|
||||
);
|
||||
@override
|
||||
Future<void> remove(int id, int version) async {
|
||||
values.remove(_key(id, version));
|
||||
}
|
||||
}
|
||||
@@ -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<String> _loadedPendingRuns = {};
|
||||
final Map<String, int> _pendingRunGenerations = {};
|
||||
|
||||
List<PipelineSummary> _pipelines = const <PipelineSummary>[];
|
||||
List<PipelineRun> _runs = const <PipelineRun>[];
|
||||
@@ -212,18 +218,62 @@ 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<RunPipelineIntent?> 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) =>
|
||||
_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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<PipelinesScreen> {
|
||||
@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<PipelinesScreen> {
|
||||
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.
|
||||
|
||||
@@ -48,11 +48,16 @@ class _RunPipelineActionState extends State<RunPipelineAction> {
|
||||
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) {
|
||||
|
||||
@@ -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<RunPipelineIntent?> readResult =
|
||||
Completer<RunPipelineIntent?>();
|
||||
|
||||
@override
|
||||
Future<RunPipelineIntent?> read(int pipelineId, int version) =>
|
||||
readResult.future;
|
||||
|
||||
@override
|
||||
Future<void> remove(int pipelineId, int version) async {}
|
||||
|
||||
@override
|
||||
Future<void> 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<void>.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<void>.delayed(Duration.zero);
|
||||
expect(controller.pendingRunFor(7, 3)?.requestKey, 'newer-key');
|
||||
controller.dispose();
|
||||
});
|
||||
}
|
||||
@@ -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 = <int>[];
|
||||
final keys = <String>[];
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user