feat(app): add native retry and restart action
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../data/runs_repository.dart';
|
||||
import 'action_refusal.dart';
|
||||
import 'retry_dialog.dart';
|
||||
import 'retry_eligibility.dart';
|
||||
import 'retry_intent.dart';
|
||||
import 'retry_wording.dart';
|
||||
|
||||
/// The run detail's deliberate retry entry point.
|
||||
///
|
||||
/// Renders one native button only while [run] is a stopped `failed` run. Opening
|
||||
/// the resume or restart confirmation sends nothing; only the confirm action
|
||||
/// asks the server to retry. A refusal is returned to the kept-open prompt
|
||||
/// immediately and the run is re-read separately, so [onRunChanged] still
|
||||
/// receives server truth without locking the prompt while that read is pending.
|
||||
class RetryRunAction extends StatefulWidget {
|
||||
const RetryRunAction({
|
||||
super.key,
|
||||
required this.run,
|
||||
required this.gateway,
|
||||
required this.onRunChanged,
|
||||
});
|
||||
|
||||
final JsonMap run;
|
||||
final ApiGateway gateway;
|
||||
final ValueChanged<JsonMap> onRunChanged;
|
||||
|
||||
@override
|
||||
State<RetryRunAction> createState() => _RetryRunActionState();
|
||||
}
|
||||
|
||||
class _RetryRunActionState extends State<RetryRunAction> {
|
||||
bool _dialogOpen = false;
|
||||
Future<bool?>? _resync;
|
||||
|
||||
bool _usesCupertino(BuildContext context) {
|
||||
final material = Localizations.of<MaterialLocalizations>(
|
||||
context,
|
||||
MaterialLocalizations,
|
||||
);
|
||||
return material == null;
|
||||
}
|
||||
|
||||
Future<void> _open() async {
|
||||
final id = retryRunId(widget.run);
|
||||
if (id == null || !canRetryRun(widget.run) || _dialogOpen) return;
|
||||
_dialogOpen = true;
|
||||
try {
|
||||
await showRetryRunPrompt(
|
||||
context: context,
|
||||
runId: id,
|
||||
cupertino: _usesCupertino(context),
|
||||
onSubmit: (intent) => _submit(id, intent),
|
||||
);
|
||||
} finally {
|
||||
_dialogOpen = false;
|
||||
if (mounted) setState(() {});
|
||||
}
|
||||
}
|
||||
|
||||
Future<ActionRefusal?> _submit(num id, RetryIntent intent) async {
|
||||
final repository = RunsRepository(widget.gateway);
|
||||
try {
|
||||
final run = await repository.retryRun(
|
||||
id,
|
||||
requestKey: intent.requestKey,
|
||||
mode: intent.mode.wireMode,
|
||||
);
|
||||
if (!mounted) return null;
|
||||
if (_owns(id)) widget.onRunChanged(run);
|
||||
return null;
|
||||
} catch (error) {
|
||||
return ActionRefusal(
|
||||
describeRetryError(error),
|
||||
stillPossible: _resyncRun(id),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
bool _owns(num id) => mounted && retryRunId(widget.run) == id;
|
||||
|
||||
/// One bounded re-read per refusal. A refusal that arrives while a read is
|
||||
/// already in flight observes that same read instead of starting another.
|
||||
Future<bool?> _resyncRun(num id) {
|
||||
return _resync ??= _readRun(id);
|
||||
}
|
||||
|
||||
Future<bool?> _readRun(num id) async {
|
||||
try {
|
||||
final run = await RunsRepository(widget.gateway).getRun(id);
|
||||
if (!_owns(id)) return null;
|
||||
widget.onRunChanged(run);
|
||||
return canRetryRun(run);
|
||||
} catch (_) {
|
||||
// Unknown server truth: keep the refusal and leave a deliberate retry.
|
||||
return null;
|
||||
} finally {
|
||||
_resync = null;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (retryRunId(widget.run) == null || !canRetryRun(widget.run)) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
if (_usesCupertino(context)) {
|
||||
return CupertinoButton.filled(
|
||||
key: retryActionKey,
|
||||
onPressed: _open,
|
||||
child: const Text(retryActionLabel),
|
||||
);
|
||||
}
|
||||
return FilledButton(
|
||||
key: retryActionKey,
|
||||
onPressed: _open,
|
||||
child: const Text(retryActionLabel),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'action_refusal.dart';
|
||||
import 'retry_intent.dart';
|
||||
import 'retry_wording.dart';
|
||||
|
||||
/// Sends the confirmed retry intent. Returns null once the server has accepted
|
||||
/// it, or an [ActionRefusal] carrying the server's words verbatim; the prompt
|
||||
/// then stays open with dismissal available immediately.
|
||||
typedef RetrySubmit = Future<ActionRefusal?> Function(RetryIntent intent);
|
||||
|
||||
/// Opens the native retry confirmation for [runId].
|
||||
///
|
||||
/// The first confirmation offers resume (the default) and a secondary
|
||||
/// `Restart from start` confirmation, exactly like the browser's retry dialog.
|
||||
/// Nothing is sent until the operator presses a confirm action; `Keep` only
|
||||
/// closes the prompt.
|
||||
Future<void> showRetryRunPrompt({
|
||||
required BuildContext context,
|
||||
required num runId,
|
||||
required bool cupertino,
|
||||
required RetrySubmit onSubmit,
|
||||
}) {
|
||||
final prompt = _RetryPrompt(
|
||||
runId: runId,
|
||||
cupertino: cupertino,
|
||||
onSubmit: onSubmit,
|
||||
);
|
||||
if (cupertino) {
|
||||
return showCupertinoDialog<void>(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (_) => prompt,
|
||||
);
|
||||
}
|
||||
return showDialog<void>(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (_) => prompt,
|
||||
);
|
||||
}
|
||||
|
||||
class _RetryPrompt extends StatefulWidget {
|
||||
const _RetryPrompt({
|
||||
required this.runId,
|
||||
required this.cupertino,
|
||||
required this.onSubmit,
|
||||
});
|
||||
|
||||
final num runId;
|
||||
final bool cupertino;
|
||||
final RetrySubmit onSubmit;
|
||||
|
||||
@override
|
||||
State<_RetryPrompt> createState() => _RetryPromptState();
|
||||
}
|
||||
|
||||
class _RetryPromptState extends State<_RetryPrompt> {
|
||||
RetryMode _mode = RetryMode.resume;
|
||||
String? _requestKey;
|
||||
bool _sending = false;
|
||||
bool _checking = false;
|
||||
bool _blocked = false;
|
||||
bool _attempted = false;
|
||||
String? _error;
|
||||
|
||||
void _keep() {
|
||||
if (!_sending) Navigator.of(context).pop();
|
||||
}
|
||||
|
||||
void _restart() {
|
||||
if (_sending || _checking || _attempted) return;
|
||||
setState(() {
|
||||
_mode = RetryMode.restart;
|
||||
_error = null;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _confirm() async {
|
||||
if (_sending || _checking || _blocked) return;
|
||||
setState(() {
|
||||
_sending = true;
|
||||
_attempted = true;
|
||||
_error = null;
|
||||
});
|
||||
final intent = RetryIntent(
|
||||
mode: _mode,
|
||||
requestKey: _requestKey ??= newRetryRequestKey(widget.runId),
|
||||
);
|
||||
final refusal = await widget.onSubmit(intent);
|
||||
if (!mounted) return;
|
||||
if (refusal == null) {
|
||||
Navigator.of(context).pop();
|
||||
return;
|
||||
}
|
||||
final check = refusal.stillPossible;
|
||||
setState(() {
|
||||
_sending = false;
|
||||
_error = refusal.message;
|
||||
_checking = check != null;
|
||||
});
|
||||
if (check == null) return;
|
||||
bool? stillPossible;
|
||||
try {
|
||||
stillPossible = await check;
|
||||
} catch (_) {
|
||||
// The action reports re-read failures as null; never fabricate one here.
|
||||
}
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_checking = false;
|
||||
_blocked = stillPossible == false;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final dialog = widget.cupertino ? _cupertino(context) : _material(context);
|
||||
return PopScope(canPop: !_sending, child: dialog);
|
||||
}
|
||||
|
||||
bool get _confirmDisabled => _sending || _checking || _blocked;
|
||||
bool get _showRestart => _mode == RetryMode.resume && !_attempted;
|
||||
|
||||
Widget _material(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
return AlertDialog(
|
||||
scrollable: true,
|
||||
title: Text(_dialogTitle),
|
||||
content: _body(errorStyle: TextStyle(color: theme.colorScheme.error)),
|
||||
actions: [
|
||||
if (_showRestart)
|
||||
TextButton(
|
||||
key: retryRestartKey,
|
||||
onPressed: _sending || _checking ? null : _restart,
|
||||
child: const Text(restartConfirmLabel),
|
||||
),
|
||||
TextButton(
|
||||
key: retryKeepKey,
|
||||
onPressed: _sending ? null : _keep,
|
||||
child: const Text(retryKeepLabel),
|
||||
),
|
||||
FilledButton(
|
||||
key: retryConfirmKey,
|
||||
onPressed: _confirmDisabled ? null : _confirm,
|
||||
child: Text(_confirmLabel),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _cupertino(BuildContext context) {
|
||||
return CupertinoAlertDialog(
|
||||
title: Text(_dialogTitle),
|
||||
content: _body(
|
||||
errorStyle: TextStyle(
|
||||
color: CupertinoColors.systemRed.resolveFrom(context),
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
if (_showRestart)
|
||||
CupertinoDialogAction(
|
||||
key: retryRestartKey,
|
||||
onPressed: _sending || _checking ? null : _restart,
|
||||
child: const Text(restartConfirmLabel),
|
||||
),
|
||||
CupertinoDialogAction(
|
||||
key: retryKeepKey,
|
||||
onPressed: _sending ? null : _keep,
|
||||
child: const Text(retryKeepLabel),
|
||||
),
|
||||
CupertinoDialogAction(
|
||||
key: retryConfirmKey,
|
||||
isDestructiveAction: _mode == RetryMode.restart,
|
||||
onPressed: _confirmDisabled ? null : _confirm,
|
||||
child: Text(_confirmLabel),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _body({required TextStyle errorStyle}) {
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Text(_dialogMessage),
|
||||
if (_error case final error?)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Text(error, key: retryErrorKey, style: errorStyle),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
String get _dialogTitle => _mode == RetryMode.restart
|
||||
? restartDialogTitle(widget.runId)
|
||||
: retryDialogTitle(widget.runId);
|
||||
|
||||
String get _dialogMessage =>
|
||||
_mode == RetryMode.restart ? restartDialogMessage : retryDialogMessage;
|
||||
|
||||
String get _confirmLabel =>
|
||||
_mode == RetryMode.restart ? restartConfirmLabel : retryConfirmLabel;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
|
||||
/// Whether the operator may retry [run], matching `canRetry` in
|
||||
/// `web/src/actions.js` exactly: a loaded run that is not executing and whose
|
||||
/// status is `failed`. A stale `failed` status while the run still executes is
|
||||
/// not retryable.
|
||||
bool canRetryRun(JsonMap? run) {
|
||||
if (run == null) return false;
|
||||
return run['executing'] != true && run['status'] == 'failed';
|
||||
}
|
||||
|
||||
/// The run id used by the retry request, or null for a malformed run.
|
||||
num? retryRunId(JsonMap run) {
|
||||
final id = run['id'];
|
||||
return id is num ? id : null;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import 'dart:math';
|
||||
|
||||
/// The two deliberate retry choices. `resume` is the server's default, so its
|
||||
/// wire field is omitted; `restart` replays the whole pipeline.
|
||||
enum RetryMode {
|
||||
resume,
|
||||
restart;
|
||||
|
||||
/// The `retry_mode` value to send, or null to let the server resume.
|
||||
String? get wireMode => this == RetryMode.restart ? 'restart' : null;
|
||||
}
|
||||
|
||||
/// One confirmed retry intent: the chosen mode plus the idempotency key that
|
||||
/// belongs to it. Retrying the same uncertain intent reuses [requestKey] so the
|
||||
/// server can dedupe it; a fresh deliberate intent uses a new key.
|
||||
class RetryIntent {
|
||||
const RetryIntent({required this.mode, required this.requestKey});
|
||||
|
||||
final RetryMode mode;
|
||||
final String requestKey;
|
||||
}
|
||||
|
||||
/// A fresh `request_key` for one retry intent. The server requires one and caps
|
||||
/// it at 128 characters; this stays well inside that bound and unique per call.
|
||||
String newRetryRequestKey(num runId, {DateTime? now, int? random}) {
|
||||
final stamp = (now ?? DateTime.now()).microsecondsSinceEpoch;
|
||||
final entropy = random ?? Random().nextInt(1 << 32);
|
||||
return 'app-retry-$runId-$stamp-${entropy.toRadixString(36)}';
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
/// The browser client's retry wording, kept verbatim.
|
||||
const retryActionLabel = 'Retry';
|
||||
const retryDialogMessage =
|
||||
'Only failed runs can be retried. The server starts a new run from the '
|
||||
'same input.';
|
||||
const retryConfirmLabel = 'Retry';
|
||||
const restartDialogMessage =
|
||||
'This replays the whole pipeline from the start instead of resuming the '
|
||||
'failed stage.';
|
||||
const restartConfirmLabel = 'Restart from start';
|
||||
const retryKeepLabel = 'Keep';
|
||||
|
||||
/// Stable test/semantics keys shared by the action and its prompt.
|
||||
const retryActionKey = ValueKey<String>('retry-run-action');
|
||||
const retryKeepKey = ValueKey<String>('retry-run-keep');
|
||||
const retryConfirmKey = ValueKey<String>('retry-run-confirm');
|
||||
const retryRestartKey = ValueKey<String>('retry-run-restart');
|
||||
const retryErrorKey = ValueKey<String>('retry-run-error');
|
||||
|
||||
/// The retry dialog's title: `Retry run {id}?`.
|
||||
String retryDialogTitle(num runId) => 'Retry run $runId?';
|
||||
|
||||
/// The restart dialog's title: `Restart run {id}?`.
|
||||
String restartDialogTitle(num runId) => 'Restart run $runId?';
|
||||
|
||||
/// The server's refusal, verbatim. Only a blank error gets a generic sentence
|
||||
/// so the prompt never claims a retry that did not happen.
|
||||
String describeRetryError(Object error) {
|
||||
final message = '$error'.trim();
|
||||
return message.isEmpty ? 'The server did not retry the run.' : message;
|
||||
}
|
||||
Reference in New Issue
Block a user