Files
pipelines/app/lib/features/pipelines/run_pipeline_intent.dart
T
alan 53a8f668ca feat(app): start a pipeline from the detail view
Native Run action opens the web's prompt/workspace/notify dialog, sends the exact run_pipeline body against the pinned resolved version with one request key per confirmed attempt, keeps refusals in the dialog, reconciles a lost reply by key, and opens only the server-named run through the existing run route with one fresh Runs list read.

Adds a shared failure-reason helper so operators never see Dart runtime prefixes, and records the slice boundary in notes/P3-run.md.
2026-10-22 02:07:49 +01:00

95 lines
2.8 KiB
Dart

import 'dart:math';
import '../../core/api_gateway.dart';
/// The browser's three notification choices for a started run. `auto` omits
/// the wire field so the pipeline definition applies.
enum RunNotifyMode {
pipelineDefault('Pipeline default'),
notify('Notify me'),
silent('Silent');
const RunNotifyMode(this.label);
final String label;
/// The `notify_mode` value, or null when the definition decides.
String? get wireValue => switch (this) {
RunNotifyMode.pipelineDefault => null,
RunNotifyMode.notify => 'on',
RunNotifyMode.silent => 'off',
};
}
/// One confirmed launch intent: the pinned pipeline identity, the operator's
/// fields and the idempotency key that belongs to them.
///
/// A lost reply leaves this intent in place, so checking or retrying reuses
/// [requestKey] and the server can dedupe it. A confirmed refusal clears it;
/// the corrected attempt mints a fresh key.
class RunPipelineIntent {
const RunPipelineIntent({
required this.pipelineId,
required this.version,
required this.prompt,
required this.workspace,
required this.notifyMode,
required this.requestKey,
});
final int pipelineId;
final int version;
final String prompt;
final String workspace;
final RunNotifyMode notifyMode;
final String requestKey;
/// The same key is only ever reused for the same pinned definition.
bool matches(int pipelineId, int version) =>
this.pipelineId == pipelineId && this.version == version;
}
/// The entropy bound for a fresh key, written as a literal because a web
/// bitwise `1 << 32` wraps to 0 and makes `Random.nextInt` reject every call.
const _entropyBound = 0x100000000;
/// A fresh `request_key` for one confirmed launch. The server caps keys at 128
/// characters; this stays well inside that bound and is unique per call.
String newRunPipelineRequestKey(
int pipelineId,
int version, {
DateTime? now,
int? random,
}) {
final stamp = (now ?? DateTime.now()).microsecondsSinceEpoch;
final entropy = random ?? Random().nextInt(_entropyBound);
return 'app-run-$pipelineId-$version-$stamp-${entropy.toRadixString(36)}';
}
/// The id the server's run detail names, or null when it named no usable run.
int? runPipelineRunId(JsonMap run) {
final id = run['id'];
if (id is int && id > 0) return id;
if (id is num && id > 0) return id.toInt();
return null;
}
/// A launch attempt's outcome: either the server's run detail or a refusal
/// with the truth about whether admission is still unknown.
sealed class RunPipelineOutcome {
const RunPipelineOutcome();
}
class RunPipelineAccepted extends RunPipelineOutcome {
const RunPipelineAccepted(this.run);
final JsonMap run;
}
class RunPipelineRefused extends RunPipelineOutcome {
const RunPipelineRefused(this.message, {required this.admissionUnknown});
final String message;
final bool admissionUnknown;
}