Merge: archive and unarchive pipelines

# Conflicts:
#	internal/cli/commands.go
#	internal/pipeline/store.go
This commit is contained in:
2026-11-04 23:39:02 +00:00
30 changed files with 1206 additions and 78 deletions
@@ -25,6 +25,7 @@ class PipelineSummary {
this.recordedStages, this.recordedStages,
this.flowStages, this.flowStages,
this.shape, this.shape,
this.archivedAt,
}); });
factory PipelineSummary.fromJson(PipelineJson json) { factory PipelineSummary.fromJson(PipelineJson json) {
@@ -47,6 +48,7 @@ class PipelineSummary {
scheduleCount: scheduleCount, scheduleCount: scheduleCount,
enabledScheduleCount: _optionalInt(json['enabled_schedule_count']), enabledScheduleCount: _optionalInt(json['enabled_schedule_count']),
shape: _jsonObject(json['shape']), shape: _jsonObject(json['shape']),
archivedAt: _optionalString(json['archived_at']),
); );
} }
@@ -77,6 +79,12 @@ class PipelineSummary {
/// empty flow. /// empty flow.
final PipelineJson? shape; final PipelineJson? shape;
/// When the pipeline was archived, or null while it is active. An archived
/// pipeline keeps its versions, runs and name but cannot start runs.
final String? archivedAt;
bool get archived => archivedAt != null;
/// The list row's stage count from recorded values only: the summary's own /// The list row's stage count from recorded values only: the summary's own
/// count, else the recorded flow's count, else its plain [stages] field. /// count, else the recorded flow's count, else its plain [stages] field.
/// Zero only when none was recorded, which is the web row's own fallback. /// Zero only when none was recorded, which is the web row's own fallback.
@@ -146,6 +154,7 @@ class PipelineDetail extends PipelineSummary {
super.recordedStages, super.recordedStages,
super.flowStages, super.flowStages,
super.shape, super.shape,
super.archivedAt,
required this.latestVersion, required this.latestVersion,
required this.versionCreatedAt, required this.versionCreatedAt,
required this.contentHash, required this.contentHash,
@@ -170,6 +179,7 @@ class PipelineDetail extends PipelineSummary {
recordedStages: summary.recordedStages, recordedStages: summary.recordedStages,
flowStages: summary.flowStages, flowStages: summary.flowStages,
shape: summary.shape, shape: summary.shape,
archivedAt: summary.archivedAt,
latestVersion: _int(json['latest_version']), latestVersion: _int(json['latest_version']),
versionCreatedAt: _string(json['version_created_at']), versionCreatedAt: _string(json['version_created_at']),
contentHash: _string(json['content_hash']), contentHash: _string(json['content_hash']),
@@ -270,6 +280,9 @@ int? _optionalInt(Object? value) {
String _string(Object? value) => value is String ? value : ''; String _string(Object? value) => value is String ? value : '';
String? _optionalString(Object? value) =>
value is String && value.isNotEmpty ? value : null;
/// The stage count recorded by a list row's optional flow summary (`shape`), /// The stage count recorded by a list row's optional flow summary (`shape`),
/// or null when that flow was not recorded with a stage list. /// or null when that flow was not recorded with a stage list.
int? _flowStageCount(Object? value) { int? _flowStageCount(Object? value) {
@@ -11,16 +11,19 @@ class PipelineRequestException implements Exception {
String toString() => message; String toString() => message;
} }
/// Read-only pipeline requests. Request shapes mirror `web/src/repository.js` /// Pipeline requests. Reads mirror `web/src/repository.js`; the one mutation
/// exactly; no mutation of server state is offered here. /// is archiving, which keeps every version and run.
class PipelinesRepository { class PipelinesRepository {
PipelinesRepository(this._gateway); PipelinesRepository(this._gateway);
final ApiGateway _gateway; final ApiGateway _gateway;
/// `list_pipelines` -> the whole `pipelines` array. /// `list_pipelines` -> the whole `pipelines` array, archived ones included
/// (each marked by `archived_at`) so the list can offer an Archived view.
Future<List<PipelineSummary>> listPipelines() async { Future<List<PipelineSummary>> listPipelines() async {
final envelope = await _gateway.call('list_pipelines'); final envelope = await _gateway.call('list_pipelines', {
'include_archived': true,
});
_throwIfError(envelope, 'The server returned no pipeline list.'); _throwIfError(envelope, 'The server returned no pipeline list.');
final type = envelope['type']; final type = envelope['type'];
if (type != null && type != 'pipelines') { if (type != null && type != 'pipelines') {
@@ -69,6 +72,28 @@ class PipelinesRepository {
return PipelineDetail.fromJson(Map<String, dynamic>.from(detail)); return PipelineDetail.fromJson(Map<String, dynamic>.from(detail));
} }
/// `archive_pipeline` / `unarchive_pipeline`, guarded by the latest
/// version the operator saw. Replies with the updated detail.
Future<PipelineDetail> setArchived(
int id, {
required bool archived,
required int expectedVersion,
}) async {
final type = archived ? 'archive_pipeline' : 'unarchive_pipeline';
final envelope = await _gateway.call(type, {
'pipeline_id': id,
if (expectedVersion > 0) 'expected_version': expectedVersion,
});
const fallback = 'The server did not confirm the change.';
_throwIfError(envelope, fallback);
final expected = archived ? 'pipeline_archived' : 'pipeline_unarchived';
final detail = envelope['detail'];
if (envelope['type'] != expected || detail is! Map) {
throw const PipelineRequestException(fallback);
}
return PipelineDetail.fromJson(Map<String, dynamic>.from(detail));
}
/// `list_runs` is read only here for the pipeline list's last-run join. /// `list_runs` is read only here for the pipeline list's last-run join.
Future<List<PipelineRun>> listRuns() async { Future<List<PipelineRun>> listRuns() async {
final envelope = await _gateway.call('list_runs'); final envelope = await _gateway.call('list_runs');
@@ -27,7 +27,9 @@ class PipelinesController extends ChangeNotifier {
final Set<String> _loadedPendingRuns = {}; final Set<String> _loadedPendingRuns = {};
final Map<String, int> _pendingRunGenerations = {}; final Map<String, int> _pendingRunGenerations = {};
List<PipelineSummary> _pipelines = const <PipelineSummary>[]; /// Every row the server listed, archived ones included.
List<PipelineSummary> _allPipelines = const <PipelineSummary>[];
bool _showArchived = false;
List<PipelineRun> _runs = const <PipelineRun>[]; List<PipelineRun> _runs = const <PipelineRun>[];
PipelineFilters _filters = emptyPipelineFilters; PipelineFilters _filters = emptyPipelineFilters;
PipelineSort _sort = PipelineSort.serverOrder; PipelineSort _sort = PipelineSort.serverOrder;
@@ -60,7 +62,13 @@ class PipelinesController extends ChangeNotifier {
final Map<String, RunPipelineIntent> _pendingRuns = final Map<String, RunPipelineIntent> _pendingRuns =
<String, RunPipelineIntent>{}; <String, RunPipelineIntent>{};
List<PipelineSummary> get pipelines => _pipelines; /// The rows of the current view: active pipelines, or with [showArchived]
/// only the archived ones. Archived pipelines never mix into the main list.
List<PipelineSummary> get pipelines => List.unmodifiable(
_allPipelines.where((pipeline) => pipeline.archived == _showArchived),
);
bool get showArchived => _showArchived;
PipelineFilters get filters => _filters; PipelineFilters get filters => _filters;
PipelineSort get sort => _sort; PipelineSort get sort => _sort;
bool get listLoading => _listLoading; bool get listLoading => _listLoading;
@@ -86,12 +94,13 @@ class PipelinesController extends ChangeNotifier {
/// The rows after sorting and filtering, using the same pure rules as web. /// The rows after sorting and filtering, using the same pure rules as web.
List<PipelineSummary> get visiblePipelines { List<PipelineSummary> get visiblePipelines {
final runs = latestRuns; final runs = latestRuns;
final sorted = sortPipelines(_pipelines, _sort, runs); final sorted = sortPipelines(pipelines, _sort, runs);
return filterPipelines(sorted, _filters, runs, _runsLoaded); return filterPipelines(sorted, _filters, runs, _runsLoaded);
} }
PipelineEmptyState? get emptyState => PipelineEmptyState? get emptyState => _showArchived && pipelines.isEmpty
pipelineListEmptyState(_pipelines, visiblePipelines, _filters); ? const PipelineEmptyState('No archived pipelines.')
: pipelineListEmptyState(pipelines, visiblePipelines, _filters);
/// Reloads the list and runs and re-reads the current detail. The selection /// Reloads the list and runs and re-reads the current detail. The selection
/// (pipeline id and requested version) is deliberately preserved. /// (pipeline id and requested version) is deliberately preserved.
@@ -110,7 +119,7 @@ class PipelinesController extends ChangeNotifier {
try { try {
final rows = await _repository.listPipelines(); final rows = await _repository.listPipelines();
if (!_current(_listSequence, token)) return; if (!_current(_listSequence, token)) return;
_pipelines = rows; _allPipelines = rows;
_pipelinesLoaded = true; _pipelinesLoaded = true;
_listError = null; _listError = null;
} catch (error) { } catch (error) {
@@ -213,6 +222,35 @@ class PipelinesController extends ChangeNotifier {
_notify(); _notify();
} }
/// Switches between the active list and the Archived view.
void setShowArchived(bool value) {
if (_showArchived == value) return;
_showArchived = value;
_notify();
}
/// Archives or unarchives [detail] and refreshes the list. Returns the
/// server's refusal (an enabled schedule, a newer version) verbatim, or
/// null on success.
Future<String?> setArchived(PipelineDetail detail, bool archived) async {
try {
final updated = await _repository.setArchived(
detail.id,
archived: archived,
expectedVersion: detail.latestVersion,
);
if (_disposed) return null;
if (_selectedId == detail.id && _selectedVersion == 0) {
_detail = updated;
}
_notify();
} catch (error) {
return pipelineErrorMessage(error);
}
await load();
return null;
}
void setSort(PipelineSort sort) { void setSort(PipelineSort sort) {
_sort = sort; _sort = sort;
_notify(); _notify();
@@ -0,0 +1,179 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/segmented_bar_overflow.dart';
import '../../../ui/layout/app_overflow_menu.dart';
import '../pipeline_models.dart';
import '../pipelines_controller.dart';
import 'pipeline_controls.dart';
import 'pipeline_detail_styles.dart';
/// The confirmation body: archiving hides, it never deletes.
String archiveConfirmation(String name) =>
'$name leaves the pipeline list and can no longer start runs. Its '
'versions, runs, artifacts and search history are kept, and you can '
'unarchive it from the Archived list at any time.';
/// The open pipeline's own actions menu in its bar: Archive or Unarchive.
/// A narrow bar moves it into the bar's More menu as one entry.
Widget pipelineActionsMenu(
BuildContext context,
PipelinesController controller,
PipelineDetail detail,
) {
final archived = detail.archived;
final label = archived ? 'Unarchive' : 'Archive…';
void run(BuildContext context) =>
archiveOrUnarchive(context, controller, detail);
return SegmentedOverflowAction(
overflowEntry: SegmentedOverflowEntry(
label: archived ? 'Unarchive pipeline' : 'Archive pipeline…',
icon: useCupertinoControls(context)
? CupertinoIcons.archivebox
: Icons.archive_outlined,
onPressed: () => run(context),
),
child: AppOverflowMenu<bool>(
key: const Key('pipeline-actions-menu'),
label: 'Pipeline actions',
sheetTitle: detail.displayName,
cupertino: useCupertinoControls(context),
actions: [
AppMenuAction(
key: Key(archived ? 'pipeline-unarchive' : 'pipeline-archive'),
value: true,
label: label,
),
],
onSelected: (context, _) => run(context),
),
);
}
/// Archives (after confirmation) or unarchives [detail], reporting a server
/// refusal such as an enabled schedule in a dialog.
Future<void> archiveOrUnarchive(
BuildContext context,
PipelinesController controller,
PipelineDetail detail,
) async {
final archiving = !detail.archived;
if (archiving && !await _confirmArchive(context, detail.displayName)) return;
final error = await controller.setArchived(detail, archiving);
if (error == null || !context.mounted) return;
await _showRefusal(
context,
archiving ? 'Could not archive' : 'Could not unarchive',
error,
);
}
/// The notice an archived pipeline's detail leads with.
class PipelineArchivedNotice extends StatelessWidget {
const PipelineArchivedNotice({
super.key,
required this.controller,
required this.detail,
});
final PipelinesController controller;
final PipelineDetail detail;
@override
Widget build(BuildContext context) {
return Padding(
key: const Key('pipeline-archived-notice'),
padding: const EdgeInsets.fromLTRB(16, 8, 16, 4),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Archived. Its history is kept; unarchive it to run it again.',
style: pipelineMutedStyle(context),
),
PipelineTextButton(
key: const Key('pipeline-archived-unarchive'),
label: 'Unarchive',
onPressed: () => archiveOrUnarchive(context, controller, detail),
),
],
),
);
}
}
Future<bool> _confirmArchive(BuildContext context, String name) async {
final title = Text('Archive $name?');
final body = Text(archiveConfirmation(name));
final confirmed = useCupertinoControls(context)
? await showCupertinoDialog<bool>(
context: context,
builder: (context) => CupertinoAlertDialog(
title: title,
content: body,
actions: [
CupertinoDialogAction(
onPressed: () => Navigator.pop(context, false),
child: const Text('Cancel'),
),
CupertinoDialogAction(
key: const Key('pipeline-archive-confirm'),
isDefaultAction: true,
onPressed: () => Navigator.pop(context, true),
child: const Text('Archive'),
),
],
),
)
: await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: title,
content: body,
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text('Cancel'),
),
FilledButton(
key: const Key('pipeline-archive-confirm'),
onPressed: () => Navigator.pop(context, true),
child: const Text('Archive'),
),
],
),
);
return confirmed == true;
}
Future<void> _showRefusal(BuildContext context, String title, String message) {
if (useCupertinoControls(context)) {
return showCupertinoDialog<void>(
context: context,
builder: (context) => CupertinoAlertDialog(
title: Text(title),
content: Text(message),
actions: [
CupertinoDialogAction(
isDefaultAction: true,
onPressed: () => Navigator.pop(context),
child: const Text('OK'),
),
],
),
);
}
return showDialog<void>(
context: context,
builder: (context) => AlertDialog(
title: Text(title),
content: Text(message),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('OK'),
),
],
),
);
}
@@ -14,6 +14,7 @@ import '../pipeline_models.dart';
import '../pipeline_query.dart'; import '../pipeline_query.dart';
import '../pipelines_controller.dart'; import '../pipelines_controller.dart';
import '../ui/run_pipeline_action.dart'; import '../ui/run_pipeline_action.dart';
import 'pipeline_archive_action.dart';
import 'pipeline_controls.dart'; import 'pipeline_controls.dart';
import 'pipeline_bar_buttons.dart'; import 'pipeline_bar_buttons.dart';
import '../../../ui/text/markdown_source_view.dart'; import '../../../ui/text/markdown_source_view.dart';
@@ -114,6 +115,8 @@ class _PipelineDetailPanelState extends State<PipelineDetailPanel> {
_applyStageRequest(context, detail); _applyStageRequest(context, detail);
final notices = [ final notices = [
if (!inBar) _header(context, detail), if (!inBar) _header(context, detail),
if (detail.archived)
PipelineArchivedNotice(controller: _controller, detail: detail),
if (_controller.detailLoading) if (_controller.detailLoading)
const Padding( const Padding(
padding: EdgeInsets.symmetric(horizontal: 16), padding: EdgeInsets.symmetric(horizontal: 16),
@@ -394,8 +397,9 @@ class _PipelineDetailPanelState extends State<PipelineDetailPanel> {
); );
} }
/// Runs and the primary Run: the top bar's actions, never floating over /// Runs, the primary Run and the pipeline's actions menu: the top bar's
/// the content (where they used to cover the version history). /// actions, never floating over the content (where they used to cover the
/// version history). An archived pipeline cannot run, so it offers no Run.
List<Widget> _actions(BuildContext context, PipelineDetail detail) => [ List<Widget> _actions(BuildContext context, PipelineDetail detail) => [
pipelineBarIconButton( pipelineBarIconButton(
context, context,
@@ -409,13 +413,15 @@ class _PipelineDetailPanelState extends State<PipelineDetailPanel> {
? null ? null
: () => widget.onViewPipelineRuns!(detail.id), : () => widget.onViewPipelineRuns!(detail.id),
), ),
RunPipelineAction( if (!detail.archived)
controller: _controller, RunPipelineAction(
detail: detail, controller: _controller,
gateway: widget.gateway, detail: detail,
onRunStarted: widget.onRunStarted, gateway: widget.gateway,
inBar: true, onRunStarted: widget.onRunStarted,
), inBar: true,
),
pipelineActionsMenu(context, _controller, detail),
]; ];
Widget _source(BuildContext context, PipelineDetail detail) { Widget _source(BuildContext context, PipelineDetail detail) {
@@ -82,18 +82,24 @@ class PipelineListPanel extends StatelessWidget {
/// The web's compact toolbar: filter (whose page carries the name search), /// The web's compact toolbar: filter (whose page carries the name search),
/// sort and clear, on the list's 12px inset with a separator underneath. /// sort and clear, on the list's 12px inset with a separator underneath.
/// All pipelines, or only those with a schedule. /// All pipelines, only those with a schedule, or the archived ones.
Widget _scope(BuildContext context, PipelineFilters filters) { Widget _scope(BuildContext context, PipelineFilters filters) {
final scheduled = filters.schedule == ScheduleFilter.scheduled; final scope = controller.showArchived
void pick(bool value) => controller.setFilters( ? PipelineScope.archived
filters.copyWith( : filters.schedule == ScheduleFilter.scheduled
schedule: value ? ScheduleFilter.scheduled : ScheduleFilter.any, ? PipelineScope.scheduled
), : PipelineScope.all;
); void pick(PipelineScope value) {
final control = PipelineScopeControl( controller.setShowArchived(value == PipelineScope.archived);
scheduled: scheduled, final schedule = value == PipelineScope.scheduled
onSelected: pick, ? ScheduleFilter.scheduled
); : ScheduleFilter.any;
if (filters.schedule != schedule) {
controller.setFilters(filters.copyWith(schedule: schedule));
}
}
final control = PipelineScopeControl(scope: scope, onSelected: pick);
return Padding( return Padding(
padding: const EdgeInsets.fromLTRB( padding: const EdgeInsets.fromLTRB(
kPipelineListInset, kPipelineListInset,
@@ -3,18 +3,32 @@ import 'package:flutter/material.dart';
import 'pipeline_controls.dart'; import 'pipeline_controls.dart';
/// The list's scope: every active pipeline, only scheduled ones, or the
/// archived pipelines (which the other two never show).
enum PipelineScope {
all('All'),
scheduled('Scheduled'),
archived('Archived');
const PipelineScope(this.label);
final String label;
}
/// Uses a native picker when the segmented labels cannot fit at the user's /// Uses a native picker when the segmented labels cannot fit at the user's
/// text scale. The decision is content-measured, not a fixed pane width. /// text scale. The decision is content-measured, not a fixed pane width.
class PipelineScopeControl extends StatelessWidget { class PipelineScopeControl extends StatelessWidget {
const PipelineScopeControl({ const PipelineScopeControl({
super.key, super.key,
required this.scheduled, required this.scope,
required this.onSelected, required this.onSelected,
}); });
final bool scheduled; final PipelineScope scope;
final ValueChanged<bool> onSelected; final ValueChanged<PipelineScope> onSelected;
static const labels = {false: 'All', true: 'Scheduled'}; static final labels = {
for (final scope in PipelineScope.values) scope: scope.label,
};
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -36,30 +50,31 @@ class PipelineScopeControl extends StatelessWidget {
builder: (context, constraints) { builder: (context, constraints) {
if (constraints.maxWidth >= requiredWidth) { if (constraints.maxWidth >= requiredWidth) {
return cupertino return cupertino
? CupertinoSlidingSegmentedControl<bool>( ? CupertinoSlidingSegmentedControl<PipelineScope>(
key: const Key('pipeline-scope'), key: const Key('pipeline-scope'),
groupValue: scheduled, groupValue: scope,
onValueChanged: (value) => onSelected(value ?? false), onValueChanged: (value) =>
onSelected(value ?? PipelineScope.all),
children: { children: {
for (final entry in labels.entries) for (final entry in labels.entries)
entry.key: Text(entry.value), entry.key: Text(entry.value),
}, },
) )
: SegmentedButton<bool>( : SegmentedButton<PipelineScope>(
key: const Key('pipeline-scope'), key: const Key('pipeline-scope'),
showSelectedIcon: false, showSelectedIcon: false,
segments: [ segments: [
for (final entry in labels.entries) for (final entry in labels.entries)
ButtonSegment(value: entry.key, label: Text(entry.value)), ButtonSegment(value: entry.key, label: Text(entry.value)),
], ],
selected: {scheduled}, selected: {scope},
onSelectionChanged: (value) => onSelected(value.single), onSelectionChanged: (value) => onSelected(value.single),
); );
} }
if (!cupertino) { if (!cupertino) {
return DropdownButton<bool>( return DropdownButton<PipelineScope>(
key: const Key('pipeline-scope'), key: const Key('pipeline-scope'),
value: scheduled, value: scope,
isExpanded: true, isExpanded: true,
items: [ items: [
for (final entry in labels.entries) for (final entry in labels.entries)
@@ -104,7 +119,7 @@ class PipelineScopeControl extends StatelessWidget {
children: [ children: [
Expanded( Expanded(
child: Text( child: Text(
labels[scheduled]!, labels[scope]!,
maxLines: 1, maxLines: 1,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
), ),
@@ -0,0 +1,184 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/pipelines/screen.dart';
import 'package:pipelines_app/features/pipelines/ui/run_pipeline_action.dart';
import 'fixtures.dart';
import 'test_helpers.dart';
/// A fixture server that remembers archive state, like the real one: the list
/// carries `archived_at` and archive replies return the updated detail.
FakeGateway archiveGateway(PipelineFixture fixture, {String? refusal}) {
final archived = <int, String>{};
Map<String, dynamic> mark(Map<String, dynamic> row) => {
...row,
if (archived[row['id']] != null) 'archived_at': archived[row['id']],
};
return FakeGateway((type, fields) {
switch (type) {
case 'list_pipelines':
return pipelineListReply([
for (final row in fixture.summaries) mark(row),
]);
case 'list_runs':
return runListReply(fixture.runs);
case 'get_pipeline':
return pipelineDetailReply(
mark(
fixture.detailFor(
fields['pipeline_id'] as int,
fields['version'] as int,
)!,
),
);
case 'archive_pipeline' || 'unarchive_pipeline':
if (refusal != null) return errorReply(refusal);
final id = fields['pipeline_id'] as int;
if (type == 'archive_pipeline') {
archived[id] = '2026-11-04T12:00:00Z';
} else {
archived.remove(id);
}
return {
'type': type == 'archive_pipeline'
? 'pipeline_archived'
: 'pipeline_unarchived',
'detail': mark(fixture.detailFor(id, 0)!),
};
}
return errorReply('unexpected $type');
});
}
Future<void> pump(
WidgetTester tester,
FakeGateway gateway, {
bool cupertino = false,
}) async {
tester.view.physicalSize = const Size(1100, 900);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final screen = PipelinesScreen(gateway: gateway);
await tester.pumpWidget(
cupertino ? CupertinoApp(home: screen) : MaterialApp(home: screen),
);
await tester.pumpAndSettle();
}
Finder rowNamed(String name) => find.descendant(
of: find.byKey(const Key('pipeline-list')),
matching: find.text(name),
);
Future<void> openMenuItem(WidgetTester tester, String key) async {
await tester.tap(find.byKey(const Key('pipeline-actions-menu')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(Key(key)));
await tester.pumpAndSettle();
}
void main() {
testWidgets('Archive from the pipeline menu hides it after confirmation', (
tester,
) async {
final gateway = archiveGateway(PipelineFixture());
await pump(tester, gateway);
expect(gateway.callsOf('list_pipelines').first.fields, {
'include_archived': true,
});
await tester.tap(rowNamed('Beta deploy'));
await tester.pumpAndSettle();
await openMenuItem(tester, 'pipeline-archive');
expect(find.text('Archive Beta deploy?'), findsOneWidget);
expect(
find.textContaining('runs, artifacts and search history are kept'),
findsOneWidget,
);
await tester.tap(find.text('Cancel'));
await tester.pumpAndSettle();
expect(gateway.callsOf('archive_pipeline'), isEmpty);
await openMenuItem(tester, 'pipeline-archive');
await tester.tap(find.byKey(const Key('pipeline-archive-confirm')));
await tester.pumpAndSettle();
expect(gateway.callsOf('archive_pipeline').single.fields, {
'pipeline_id': 2,
'expected_version': 1,
});
expect(rowNamed('Beta deploy'), findsNothing);
expect(rowNamed('Alpha review'), findsOneWidget);
// The open detail stays, says it is archived and offers no Run.
expect(find.byKey(const Key('pipeline-archived-notice')), findsOneWidget);
expect(find.byType(RunPipelineAction), findsNothing);
});
testWidgets('the Archived scope lists archived pipelines and unarchives', (
tester,
) async {
final gateway = archiveGateway(PipelineFixture());
await pump(tester, gateway);
await tester.tap(rowNamed('Gamma legacy'));
await tester.pumpAndSettle();
await openMenuItem(tester, 'pipeline-archive');
await tester.tap(find.byKey(const Key('pipeline-archive-confirm')));
await tester.pumpAndSettle();
await pickScope(tester, 'Archived');
expect(rowNamed('Gamma legacy'), findsOneWidget);
expect(rowNamed('Alpha review'), findsNothing);
expect(rowNamed('Beta deploy'), findsNothing);
await openMenuItem(tester, 'pipeline-unarchive');
expect(gateway.callsOf('unarchive_pipeline').single.fields, {
'pipeline_id': 3,
'expected_version': 1,
});
expect(find.text('No archived pipelines.'), findsOneWidget);
expect(find.byKey(const Key('pipeline-archived-notice')), findsNothing);
await pickScope(tester, 'All');
expect(rowNamed('Gamma legacy'), findsOneWidget);
});
testWidgets('a refused archive shows the server reason', (tester) async {
const reason =
'pipeline "Alpha review" has enabled schedule(s) "nightly" (#4); '
'pause or repoint them before archiving';
final gateway = archiveGateway(PipelineFixture(), refusal: reason);
await pump(tester, gateway);
await tester.tap(rowNamed('Alpha review'));
await tester.pumpAndSettle();
await openMenuItem(tester, 'pipeline-archive');
await tester.tap(find.byKey(const Key('pipeline-archive-confirm')));
await tester.pumpAndSettle();
expect(find.text('Could not archive'), findsOneWidget);
expect(find.text(reason), findsOneWidget);
await tester.tap(find.text('OK'));
await tester.pumpAndSettle();
expect(rowNamed('Alpha review'), findsOneWidget);
});
testWidgets('Cupertino archives through the action sheet and alert', (
tester,
) async {
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
try {
final gateway = archiveGateway(PipelineFixture());
await pump(tester, gateway, cupertino: true);
await tester.tap(rowNamed('Beta deploy'));
await tester.pumpAndSettle();
await openMenuItem(tester, 'pipeline-archive');
expect(find.byType(CupertinoAlertDialog), findsOneWidget);
await tester.tap(find.byKey(const Key('pipeline-archive-confirm')));
await tester.pumpAndSettle();
expect(gateway.callsOf('archive_pipeline'), hasLength(1));
expect(rowNamed('Beta deploy'), findsNothing);
} finally {
debugDefaultTargetPlatformOverride = null;
}
});
}
@@ -9,14 +9,14 @@ void main() {
testWidgets( testWidgets(
'native scope remains usable at $width, Cupertino=$cupertino', 'native scope remains usable at $width, Cupertino=$cupertino',
(tester) async { (tester) async {
var scheduled = false; var scope = PipelineScope.all;
final control = StatefulBuilder( final control = StatefulBuilder(
builder: (context, setState) => Center( builder: (context, setState) => Center(
child: SizedBox( child: SizedBox(
width: width, width: width,
child: PipelineScopeControl( child: PipelineScopeControl(
scheduled: scheduled, scope: scope,
onSelected: (value) => setState(() => scheduled = value), onSelected: (value) => setState(() => scope = value),
), ),
), ),
), ),
@@ -35,7 +35,7 @@ void main() {
await tester.tap(find.text('Scheduled')); await tester.tap(find.text('Scheduled'));
} }
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(scheduled, isTrue); expect(scope, PipelineScope.scheduled);
expect(tester.takeException(), isNull); expect(tester.takeException(), isNull);
}, },
); );
@@ -50,7 +50,10 @@ void main() {
width: 500, width: 500,
child: Align( child: Align(
alignment: AlignmentDirectional.centerStart, alignment: AlignmentDirectional.centerStart,
child: PipelineScopeControl(scheduled: false, onSelected: (_) {}), child: PipelineScopeControl(
scope: PipelineScope.all,
onSelected: (_) {},
),
), ),
), ),
), ),
@@ -31,7 +31,7 @@ void main() {
'list_pipelines', 'list_pipelines',
'list_runs', 'list_runs',
]); ]);
expect(gateway.calls[0].fields, isEmpty); expect(gateway.calls[0].fields, {'include_archived': true});
expect(gateway.calls[1].fields, isEmpty); expect(gateway.calls[1].fields, isEmpty);
expect(controller.pipelines.map((pipeline) => pipeline.id), [1, 2, 3]); expect(controller.pipelines.map((pipeline) => pipeline.id), [1, 2, 3]);
expect(controller.pipelinesLoaded, isTrue); expect(controller.pipelinesLoaded, isTrue);
@@ -127,24 +127,12 @@ void main() {
expect(find.text('Alpha review'), findsOneWidget); expect(find.text('Alpha review'), findsOneWidget);
expect(find.text('Gamma legacy'), findsOneWidget); expect(find.text('Gamma legacy'), findsOneWidget);
await tester.tap( await pickScope(tester, 'Scheduled');
find.descendant(
of: find.byKey(const Key('pipeline-scope')),
matching: find.text('Scheduled'),
),
);
await tester.pumpAndSettle();
// Beta has no schedule in the fixture; Alpha has one. // Beta has no schedule in the fixture; Alpha has one.
expect(find.text('Alpha review'), findsOneWidget); expect(find.text('Alpha review'), findsOneWidget);
expect(find.text('Beta deploy'), findsNothing); expect(find.text('Beta deploy'), findsNothing);
await tester.tap( await pickScope(tester, 'All');
find.descendant(
of: find.byKey(const Key('pipeline-scope')),
matching: find.text('All'),
),
);
await tester.pumpAndSettle();
expect(find.text('Beta deploy'), findsOneWidget); expect(find.text('Beta deploy'), findsOneWidget);
}); });
@@ -115,13 +115,16 @@ void main() {
expect(find.bySemanticsLabel('View runs of Alpha review'), findsOneWidget); expect(find.bySemanticsLabel('View runs of Alpha review'), findsOneWidget);
// Run and Runs share the detail pane's actions pill at the top right, // Run and Runs share the detail pane's actions pill at the top right,
// Runs before the primary Run; nothing floats over the content. // Runs before the primary Run and the pipeline's overflow menu last;
// nothing floats over the content.
final runs = tester.getRect(find.byKey(const Key('pipeline-view-runs'))); final runs = tester.getRect(find.byKey(const Key('pipeline-view-runs')));
final run = tester.getRect(find.byKey(runPipelineActionKey)); final run = tester.getRect(find.byKey(runPipelineActionKey));
final menu = tester.getRect(find.byKey(const Key('pipeline-actions-menu')));
final screen = tester.view.physicalSize / tester.view.devicePixelRatio; final screen = tester.view.physicalSize / tester.view.devicePixelRatio;
expect(runs.right, lessThanOrEqualTo(run.left)); expect(runs.right, lessThanOrEqualTo(run.left));
expect(run.right, lessThanOrEqualTo(menu.left));
expect((runs.center.dy - run.center.dy).abs(), lessThan(1)); expect((runs.center.dy - run.center.dy).abs(), lessThan(1));
expect(run.right, greaterThan(screen.width - 40)); expect(menu.right, greaterThan(screen.width - 40));
expect(run.bottom, lessThan(screen.height / 4)); expect(run.bottom, lessThan(screen.height / 4));
await tester.tap(find.byKey(const Key('pipeline-view-runs'))); await tester.tap(find.byKey(const Key('pipeline-view-runs')));
expect(opened, [1]); expect(opened, [1]);
@@ -44,3 +44,20 @@ Future<void> pumpWide(WidgetTester tester, FakeGateway gateway) async {
Finder selectableText(String text) => find.byWidgetPredicate( Finder selectableText(String text) => find.byWidgetPredicate(
(widget) => widget is SelectableText && widget.data == text, (widget) => widget is SelectableText && widget.data == text,
); );
/// Picks a scope from the segments, or from the menu a pane too narrow for
/// the segments shows instead.
Future<void> pickScope(WidgetTester tester, String label) async {
final segment = find.descendant(
of: find.byKey(const Key('pipeline-scope')),
matching: find.text(label),
);
if (segment.evaluate().isEmpty) {
await tester.tap(find.byKey(const Key('pipeline-scope')));
await tester.pumpAndSettle();
await tester.tap(find.text(label).last);
} else {
await tester.tap(segment);
}
await tester.pumpAndSettle();
}
@@ -225,7 +225,19 @@ List<JsonMap> probeRuns() => [
..._fillerRuns(), ..._fillerRuns(),
]; ];
/// Pipelines this fixture session archived, with their archive time.
final _probeArchived = <int, String>{};
List<JsonMap> probePipelines() => [ List<JsonMap> probePipelines() => [
for (final pipeline in _probePipelineRows())
{
...pipeline,
if (_probeArchived[pipeline['id']] != null)
'archived_at': _probeArchived[pipeline['id']],
},
];
List<JsonMap> _probePipelineRows() => [
{ {
'id': 24, 'id': 24,
'name': 'Release train', 'name': 'Release train',
@@ -503,6 +515,29 @@ JsonMap? probeReply(JsonMap request) {
'type': 'pipeline', 'type': 'pipeline',
'detail': probePipelineDetail(pipelineId, version is int ? version : 0), 'detail': probePipelineDetail(pipelineId, version is int ? version : 0),
}; };
case 'archive_pipeline':
case 'unarchive_pipeline':
// Mirrors the server: an enabled schedule blocks archiving.
final pipelineId = request['pipeline_id'];
final row = probePipelines().where((p) => p['id'] == pipelineId);
if (row.isEmpty) return error('pipeline not found');
final archive = type == 'archive_pipeline';
if (archive && (row.first['enabled_schedule_count'] as int) > 0) {
return error(
'pipeline "${row.first['name']}" has enabled schedule(s) '
'"Nightly docs" (#3); pause or repoint them before archiving',
);
}
if (archive) {
_probeArchived[pipelineId as int] = '2026-10-20T09:30:00Z';
} else {
_probeArchived.remove(pipelineId);
}
return {
'id': id,
'type': archive ? 'pipeline_archived' : 'pipeline_unarchived',
'detail': probePipelineDetail(pipelineId as int, 0),
};
case 'search': case 'search':
// The app must send exactly the browser's SearchRequest body; anything // The app must send exactly the browser's SearchRequest body; anything
// else is answered with a visible refusal so the capture cannot pass. // else is answered with a visible refusal so the capture cannot pass.
+41 -1
View File
@@ -329,6 +329,7 @@ Submitting activates included schedules, so review them before publishing.
| Server/ntfy status popup | `status --json` | | Server/ntfy status popup | `status --json` |
| Usage statistics/charts | `stats --json` | | Usage statistics/charts | `stats --json` |
| Run-history retention | `retention report --keep N --json`, `retention apply --keep N --yes` | | Run-history retention | `retention report --keep N --json`, `retention apply --keep N --yes` |
| Retire an unused pipeline | `archive NAME --yes`, `unarchive NAME`, `list --archived` |
| Legacy operational diagnostics | managed `logs`, `doctor` | | Legacy operational diagnostics | managed `logs`, `doctor` |
The TUI does not spawn CLI processes for each request. Both front ends share The TUI does not spawn CLI processes for each request. Both front ends share
@@ -340,7 +341,8 @@ exposes the full protocol as an escape hatch, so no server command is TUI-only:
printf '%s' '{"type":"get_pipeline","pipeline_id":1,"version":1}' | pipelines rpc - printf '%s' '{"type":"get_pipeline","pipeline_id":1,"version":1}' | pipelines rpc -
``` ```
Existing protocol commands: `ping`, `list_pipelines`, `get_pipeline`, Existing protocol commands: `ping`, `list_pipelines` (optional `include_archived`),
`get_pipeline`, `archive_pipeline`, `unarchive_pipeline`,
`list_templates`, `validate`, `submit`, `run_pipeline`, `list_runs`, `get_run`, `list_templates`, `validate`, `submit`, `run_pipeline`, `list_runs`, `get_run`,
`cancel_run`, `retry_run`, `decide_run`, `set_run_title`, `get_run_evidence`, `cancel_run`, `retry_run`, `decide_run`, `set_run_title`, `get_run_evidence`,
`get_status`, `search`. `search` takes a `search` request object (`query`, with an `get_status`, `search`. `search` takes a `search` request object (`query`, with an
@@ -397,6 +399,44 @@ whose scope covers the runs receive the `runs_removed` push. `pipelines runs rm
have real effects; they are not previews. TUI-only animation, scrolling, and have real effects; they are not previews. TUI-only animation, scrolling, and
transient client logs are presentation, not separate server capabilities. transient client logs are presentation, not separate server capabilities.
## Archiving pipelines
There is no pipeline deletion: an unused pipeline is archived instead.
```sh
pipelines archive "old flow" --yes # hide it; history is kept
pipelines list --archived # only archived ones (--all lists both)
pipelines unarchive "old flow"
```
Archiving sets `archived_at` on the pipeline and nothing else. Its versions,
runs, artifacts, `inspect`, `runs --pipeline` and search history keep working and
keep showing its name. While archived:
- `list_pipelines` omits it unless the request sets `include_archived`; then the
summary carries `archived_at` (RFC 3339). The TUI and the app's main list hide it;
the app's Archived filter shows and unarchives it.
- `run_pipeline` and `retry_run` are refused: `pipeline "X" is archived; unarchive
it first`.
- Its name stays reserved: `new`, `submit` (with or without `--create-only`) and
import are refused with the same message rather than creating a second pipeline.
- Archiving is refused while an enabled schedule points at it, naming each
schedule; pause or repoint them first. Enabling a schedule for an archived
pipeline is refused too. Refusing is deliberate rather than pausing schedules
implicitly: the operator sees exactly which timers stop, and unarchiving never
needs to guess which schedules to turn back on.
| Command | Payload | Reply |
| --- | --- | --- |
| `archive_pipeline` | `pipeline_id` or `name`, optional `expected_version` | `pipeline_archived` with `detail` |
| `unarchive_pipeline` | `pipeline_id` or `name`, optional `expected_version` | `pipeline_unarchived` with `detail` |
`name` matches exactly first and then as a numeric ID, as in the CLI. A positive
`expected_version` refuses the change when the latest version differs (the CLI
sends the version it resolved). Repeating a request already in effect succeeds
without change. Like schedule saves, these are lifecycle controls and are not
gated by authoring being disabled.
## Saved run schedules ## Saved run schedules
Schedules are independent pipeline/version + run-information + timer records, Schedules are independent pipeline/version + run-information + timer records,
@@ -16,6 +16,9 @@ parallel/distributed task engine or a guarantee that model reports are correct.
an editable prefilled name or customized first. Editing a bundled repository an editable prefilled name or customized first. Editing a bundled repository
template changes no installed or published definition; only explicit authoring template changes no installed or published definition; only explicit authoring
then submission creates a new immutable version. then submission creates a new immutable version.
- Retire an unused pipeline with `pipelines archive NAME --yes` (no deletion exists):
it leaves lists and cannot run, its history stays, its name stays reserved, and
enabled schedules must be paused first; `unarchive` and `list --archived` undo/show it.
- A run pins its exact definition version, initial input and resolved directory. - A run pins its exact definition version, initial input and resolved directory.
The client invocation directory is the default, but files/tools execute on the The client invocation directory is the default, but files/tools execute on the
server. Docker needs that directory mounted at the same absolute path. A remote server. Docker needs that directory mounted at the same absolute path. A remote
+24 -4
View File
@@ -52,6 +52,7 @@ type options struct {
all bool all bool
debugFiles bool debugFiles bool
json, source, noOpen, yes, allow, createOnly, force, wait bool json, source, noOpen, yes, allow, createOnly, force, wait bool
archived bool
version, expected, limit, retryVisit int version, expected, limit, retryVisit int
timeout, waitTimeout time.Duration timeout, waitTimeout time.Duration
stageModels stageModelFlags stageModels stageModelFlags
@@ -138,6 +139,11 @@ func (a App) flags(command string) (*flag.FlagSet, *options) {
f.BoolVar(&o.yes, "yes", false, "Confirm publication (schedules may activate)") f.BoolVar(&o.yes, "yes", false, "Confirm publication (schedules may activate)")
case "template": case "template":
f.BoolVar(&o.source, "source", false, "Print original Markdown instead of metadata") f.BoolVar(&o.source, "source", false, "Print original Markdown instead of metadata")
case "list":
f.BoolVar(&o.archived, "archived", false, "List only archived pipelines")
f.BoolVar(&o.all, "all", false, "List active and archived pipelines (archived ones are marked)")
case "archive":
f.BoolVar(&o.yes, "yes", false, "Confirm archiving without an interactive prompt")
case "submit": case "submit":
f.BoolVar(&o.yes, "yes", false, "Confirm publication without an interactive prompt") f.BoolVar(&o.yes, "yes", false, "Confirm publication without an interactive prompt")
f.StringVar(&o.name, "name", "", "Name for an external Markdown file (managed drafts already know their name)") f.StringVar(&o.name, "name", "", "Name for an external Markdown file (managed drafts already know their name)")
@@ -513,8 +519,10 @@ func (a App) Run(ctx context.Context, args []string) error {
defer cancel() defer cancel()
return c.Call(requestCtx, request) return c.Call(requestCtx, request)
} }
// Resolution includes archived pipelines so history, show and unarchive
// still address them; the server refuses runs and publications for them.
resolve := func(value string) (protocol.PipelineSummary, error) { resolve := func(value string) (protocol.PipelineSummary, error) {
r, err := call(protocol.Message{Type: protocol.List}) r, err := call(protocol.Message{Type: protocol.List, IncludeArchived: true})
if err != nil { if err != nil {
return protocol.PipelineSummary{}, err return protocol.PipelineSummary{}, err
} }
@@ -760,6 +768,8 @@ func (a App) Run(ctx context.Context, args []string) error {
} }
} }
return nil return nil
case "archive", "unarchive":
return a.archive(command, pos, o, call, resolve)
case "list", "templates", "runs", "ping", "connection": case "list", "templates", "runs", "ping", "connection":
if command == "runs" && len(pos) > 0 && pos[0] == "rm" { if command == "runs" && len(pos) > 0 && pos[0] == "rm" {
return a.removeRuns(o, pos[1:], call) return a.removeRuns(o, pos[1:], call)
@@ -768,10 +778,13 @@ func (a App) Run(ctx context.Context, args []string) error {
return fmt.Errorf("%s takes no positional arguments", command) return fmt.Errorf("%s takes no positional arguments", command)
} }
typeName := map[string]string{"list": protocol.List, "templates": protocol.ListTemplates, "runs": protocol.ListRuns, "ping": protocol.Ping, "connection": protocol.List}[command] typeName := map[string]string{"list": protocol.List, "templates": protocol.ListTemplates, "runs": protocol.ListRuns, "ping": protocol.Ping, "connection": protocol.List}[command]
r, err := call(protocol.Message{Type: typeName}) r, err := call(protocol.Message{Type: typeName, IncludeArchived: command == "list" && (o.all || o.archived)})
if err != nil { if err != nil {
return err return err
} }
if command == "list" && o.archived && !o.all {
r.Pipelines = archivedOnly(r.Pipelines)
}
if command == "runs" && o.name != "" { if command == "runs" && o.name != "" {
p, err := resolve(o.name) p, err := resolve(o.name)
if err != nil { if err != nil {
@@ -791,7 +804,11 @@ func (a App) Run(ctx context.Context, args []string) error {
switch command { switch command {
case "list": case "list":
for _, p := range r.Pipelines { for _, p := range r.Pipelines {
fmt.Fprintf(a.Out, "%d\t%s\tv%d\t%d stages\n", p.ID, p.Name, p.Version, p.Stages) note := ""
if p.ArchivedAt != "" {
note = "\tarchived " + p.ArchivedAt
}
fmt.Fprintf(a.Out, "%d\t%s\tv%d\t%d stages%s\n", p.ID, p.Name, p.Version, p.Stages, note)
} }
case "templates": case "templates":
for _, t := range r.Templates { for _, t := range r.Templates {
@@ -1207,11 +1224,14 @@ func (a App) author(ctx context.Context, command string, pos []string, o *option
if (o.name != "" && strings.TrimSpace(o.name) == "") || len(o.name) > 200 || strings.ContainsAny(o.name, "\r\n\x00") { if (o.name != "" && strings.TrimSpace(o.name) == "") || len(o.name) > 200 || strings.ContainsAny(o.name, "\r\n\x00") {
return fmt.Errorf("name must be 1–200 bytes without line breaks or NUL") return fmt.Errorf("name must be 1–200 bytes without line breaks or NUL")
} }
r, err := call(protocol.Message{Type: protocol.List}) r, err := call(protocol.Message{Type: protocol.List, IncludeArchived: true})
if err != nil { if err != nil {
return err return err
} }
for _, p := range r.Pipelines { for _, p := range r.Pipelines {
if p.Name == o.name && p.ArchivedAt != "" {
return &pipeline.ArchivedError{Name: p.Name}
}
if p.Name == o.name { if p.Name == o.name {
return fmt.Errorf("pipeline already exists; use edit or choose another name") return fmt.Errorf("pipeline already exists; use edit or choose another name")
} }
+52
View File
@@ -0,0 +1,52 @@
package cli
import (
"fmt"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// archive implements `pipelines archive` and `pipelines unarchive`. The
// server keeps every version and run; the latest version is the guard.
func (a App) archive(command string, pos []string, o *options, call callFunc, resolve resolveFunc) error {
if len(pos) != 1 {
return fmt.Errorf("%s requires one pipeline name or ID; use pipelines help %s", command, command)
}
p, err := resolve(pos[0])
if err != nil {
return err
}
request := protocol.Message{Type: protocol.UnarchivePipeline, PipelineID: p.ID, ExpectedVersion: p.Version}
if command == "archive" {
request.Type = protocol.ArchivePipeline
if err := a.confirmAction("Archive "+p.Name+"? It leaves the pipeline list and cannot start runs; its versions, runs and artifacts are kept and it can be unarchived.", o.yes); err != nil {
return err
}
}
r, err := call(request)
if err != nil {
return err
}
if r.Detail == nil {
return fmt.Errorf("server did not return the pipeline; update pipelines-server")
}
if o.json {
return a.output(r.Detail.PipelineSummary)
}
if command == "archive" {
fmt.Fprintf(a.Out, "Archived %s (history kept; pipelines unarchive %d restores it)\n", r.Detail.Name, r.Detail.ID)
} else {
fmt.Fprintf(a.Out, "Unarchived %s\n", r.Detail.Name)
}
return nil
}
func archivedOnly(items []protocol.PipelineSummary) []protocol.PipelineSummary {
kept := []protocol.PipelineSummary{}
for _, p := range items {
if p.ArchivedAt != "" {
kept = append(kept, p)
}
}
return kept
}
+6 -1
View File
@@ -10,7 +10,9 @@ var usage = map[string]string{
"draft-delete": "draft-delete <draft.md> [--json]", "draft-delete": "draft-delete <draft.md> [--json]",
"validate": "validate <draft.md> [--json]", "validate": "validate <draft.md> [--json]",
"submit": "submit <draft.md> [--name NAME] [--create-only] [--yes] [--json]", "submit": "submit <draft.md> [--name NAME] [--create-only] [--yes] [--json]",
"list": "list [--json]", "list": "list [--archived | --all] [--json]",
"archive": "archive <name|id> [--yes] [--json]",
"unarchive": "unarchive <name|id> [--json]",
"search": "search QUERY [--type KIND] [--limit N] [--debug-files] [--json]", "search": "search QUERY [--type KIND] [--limit N] [--debug-files] [--json]",
"show": "show <name|id> [--version N] [--json]", "show": "show <name|id> [--version N] [--json]",
"versions": "versions <name|id> [--json]", "versions": "versions <name|id> [--json]",
@@ -52,6 +54,9 @@ var notes = map[string]string{
"submit": "Confirm publication at the prompt, or use --yes for explicit automation; schedules activate on acceptance. Managed drafts provide the name and base-version guard. --force never bypasses validation.", "submit": "Confirm publication at the prompt, or use --yes for explicit automation; schedules activate on acceptance. Managed drafts provide the name and base-version guard. --force never bypasses validation.",
"run": "Runs in your current directory after action confirmation (--yes skips it). Select a pipeline and compose a multiline task with Ctrl+S. Stage tools are enabled automatically; no file list or typed RUN confirmation. Cancellation does not undo edits. --title sets an explicit run title; without it the title is derived from the prompt. --model PROVIDER/MODEL overrides the model for every stage of that run; --stage-model STAGE=PROVIDER/MODEL overrides one stage and is repeatable, with a repeated key last-wins. Precedence is per-stage > whole-run > the model pinned in the pipeline version; overrides apply to that run only and never change the pinned definition. An unknown stage key or a malformed provider/model is refused before the run starts, naming the valid stage keys. In JSON mode --wait polls until terminal; --wait-timeout bounds only the client wait after admission and is independent of per-request --timeout. A reached wait limit leaves the run active: inspect read-only with `inspect <run-id> --json`, or reattach with --wait reusing the original --request-key (never an automatic replay or cancel).", "run": "Runs in your current directory after action confirmation (--yes skips it). Select a pipeline and compose a multiline task with Ctrl+S. Stage tools are enabled automatically; no file list or typed RUN confirmation. Cancellation does not undo edits. --title sets an explicit run title; without it the title is derived from the prompt. --model PROVIDER/MODEL overrides the model for every stage of that run; --stage-model STAGE=PROVIDER/MODEL overrides one stage and is repeatable, with a repeated key last-wins. Precedence is per-stage > whole-run > the model pinned in the pipeline version; overrides apply to that run only and never change the pinned definition. An unknown stage key or a malformed provider/model is refused before the run starts, naming the valid stage keys. In JSON mode --wait polls until terminal; --wait-timeout bounds only the client wait after admission and is independent of per-request --timeout. A reached wait limit leaves the run active: inspect read-only with `inspect <run-id> --json`, or reattach with --wait reusing the original --request-key (never an automatic replay or cancel).",
"retry": "Resume a failed stage by default, restoring the completed prefix handoff; use --mode=restart to start at entry. --stage selects a recorded stage (use --visit for repeated stages). --input-file accepts a bounded JSON object, merged with replacement at the selected stage; '-' reads stdin. Original history and pinned identity are retained; --version selects an explicit published version of the same pipeline only. --wait-timeout leaves an active retry for later read-only inspection or reattachment with the original --request-key, rather than starting a duplicate retry.", "retry": "Resume a failed stage by default, restoring the completed prefix handoff; use --mode=restart to start at entry. --stage selects a recorded stage (use --visit for repeated stages). --input-file accepts a bounded JSON object, merged with replacement at the selected stage; '-' reads stdin. Original history and pinned identity are retained; --version selects an explicit published version of the same pipeline only. --wait-timeout leaves an active retry for later read-only inspection or reattachment with the original --request-key, rather than starting a duplicate retry.",
"list": "Lists active pipelines. Archived pipelines are hidden; --archived lists only them and --all lists both, marking archived ones with their archive time.",
"archive": "Retire a pipeline from lists after confirmation (--yes for scripts). Nothing is deleted: versions, runs, artifacts and search history stay and keep its name. An archived pipeline cannot start runs and its name stays reserved (new/submit say so). Archiving is refused while an enabled schedule points at it, naming the schedule; pause or repoint it first. The latest version is sent as a guard, so a publication in between is refused.",
"unarchive": "Return an archived pipeline to lists; it can run and take new versions again. Schedules stay paused until you enable them.",
"runs": "Lists runs newest first; --json includes each run's size_bytes (bytes on disk: artifacts, session exports, server-created Pi session directories and an estimate of its database rows; 0 until first measured) and, when any model stage recorded usage, its usage total (tokens_total, split and cache components, cost). `runs rm <id...>` permanently deletes finished runs (succeeded, failed, canceled, interrupted) with every database row and server-owned file for them, after confirmation (--yes for scripts). Active runs, runs an unfinished retry depends on and unknown ids are refused individually with a reason, and the command then exits 1. User workspaces are never touched. Database pages are reused by SQLite; `retention apply` additionally VACUUMs to shrink the file.", "runs": "Lists runs newest first; --json includes each run's size_bytes (bytes on disk: artifacts, session exports, server-created Pi session directories and an estimate of its database rows; 0 until first measured) and, when any model stage recorded usage, its usage total (tokens_total, split and cache components, cost). `runs rm <id...>` permanently deletes finished runs (succeeded, failed, canceled, interrupted) with every database row and server-owned file for them, after confirmation (--yes for scripts). Active runs, runs an unfinished retry depends on and unknown ids are refused individually with a reason, and the command then exits 1. User workspaces are never touched. Database pages are reused by SQLite; `retention apply` additionally VACUUMs to shrink the file.",
"title": "Rename a run's operator-facing title after the fact without touching its input, steps or execution. The title is collapsed to a single bounded line and recorded as explicitly set. `pipelines runs` and `pipelines inspect` then show it.", "title": "Rename a run's operator-facing title after the fact without touching its input, steps or execution. The title is collapsed to a single bounded line and recorded as explicitly set. `pipelines runs` and `pipelines inspect` then show it.",
"wait": "Block until each named run reaches a terminal status - succeeded, failed, canceled or interrupted, and also through a run waiting on a person - then print one line per run with its id, status and outcome, its stage and how long it took, and exit 0 even for a run that finished unsuccessfully. Several ids wait together in one command. The exit is non-zero only for a client-side failure: an unknown run id, a connection that could not be established, or --timeout expiring while a run is still active; a timeout says so plainly, never cancels, retries or otherwise touches a run, and is never reported as its outcome. Read-only: it needs no workspace, is not refused by the workspace guard and writes nothing. --json prints the final run envelope instead of the line (one envelope for one id, an array for several).", "wait": "Block until each named run reaches a terminal status - succeeded, failed, canceled or interrupted, and also through a run waiting on a person - then print one line per run with its id, status and outcome, its stage and how long it took, and exit 0 even for a run that finished unsuccessfully. Several ids wait together in one command. The exit is non-zero only for a client-side failure: an unknown run id, a connection that could not be established, or --timeout expiring while a run is still active; a timeout says so plainly, never cancels, retries or otherwise touches a run, and is never reported as its outcome. Read-only: it needs no workspace, is not refused by the workspace guard and writes nothing. --json prints the final run envelope instead of the line (one envelope for one id, an array for several).",
+3 -1
View File
@@ -25,7 +25,7 @@ EXECUTE
rpc <request.json|-> Send any protocol command (automation escape hatch) rpc <request.json|-> Send any protocol command (automation escape hatch)
BROWSE BROWSE
list List registered pipelines list List active pipelines; --archived/--all show archived ones
search QUERY Search pipelines, versions, runs, steps, field text, artifacts search QUERY Search pipelines, versions, runs, steps, field text, artifacts
show <name|id> Inspect graph, settings, source and history show <name|id> Inspect graph, settings, source and history
versions <name|id> List immutable versions versions <name|id> List immutable versions
@@ -42,6 +42,8 @@ AUTHORING — only when the user explicitly asks to create or change a definitio
import [FILE.zip] Select/import definitions; explicitly resolve conflicts import [FILE.zip] Select/import definitions; explicitly resolve conflicts
MAINTAIN MAINTAIN
archive <name|id> Hide an unused pipeline; versions and runs are kept
unarchive <name|id> Return an archived pipeline to lists and runs
retention report Show what a run-history policy would remove (read-only) retention report Show what a run-history policy would remove (read-only)
retention apply Remove it after approval; artifacts go with their run retention apply Remove it after approval; artifacts go with their run
+2 -1
View File
@@ -139,7 +139,8 @@ func (a App) importBundle(ctx context.Context, o *options, pos []string, call ca
entries = keep entries = keep
} }
// All selected sources/names are checked before any publication. // All selected sources/names are checked before any publication.
existing, err := call(protocol.Message{Type: protocol.List}) // Archived names stay reserved, so they count as existing here.
existing, err := call(protocol.Message{Type: protocol.List, IncludeArchived: true})
if err != nil { if err != nil {
return err return err
} }
+140
View File
@@ -0,0 +1,140 @@
package pipeline
import (
"context"
"database/sql"
"fmt"
"strconv"
"strings"
"time"
)
// Archiving retires a pipeline from everyday lists without deleting anything:
// its versions, runs, artifacts and search history stay as they are, and its
// name stays reserved. Only the pipelines row carries the archived_at stamp.
func (s *Store) initArchive() error {
return s.addColumnIfMissing("pipelines", "archived_at", "TEXT")
}
// ArchivedError reports that a name or ID belongs to an archived pipeline.
type ArchivedError struct{ Name string }
func (e *ArchivedError) Error() string {
return fmt.Sprintf("pipeline %q is archived; unarchive it first (pipelines unarchive %s)", e.Name, quoteArg(e.Name))
}
func quoteArg(name string) string {
if strings.ContainsAny(name, " \t'\"\\$`") {
return "'" + strings.ReplaceAll(name, "'", `'\''`) + "'"
}
return name
}
// ResolvePipelineID finds a pipeline by exact name first, then by numeric ID,
// archived or not, matching the CLI's name-over-ID rule.
func (s *Store) ResolvePipelineID(ctx context.Context, id int64, name string) (int64, error) {
name = strings.TrimSpace(name)
if name != "" {
var found int64
err := s.db.QueryRowContext(ctx, `SELECT id FROM pipelines WHERE name=?`, name).Scan(&found)
if err == nil {
return found, nil
}
if err != sql.ErrNoRows {
return 0, err
}
if id == 0 {
id, _ = strconv.ParseInt(name, 10, 64)
}
if id <= 0 {
return 0, fmt.Errorf("pipeline %q not found", name)
}
}
if id <= 0 {
return 0, fmt.Errorf("a pipeline name or ID is required")
}
var found int64
err := s.db.QueryRowContext(ctx, `SELECT id FROM pipelines WHERE id=?`, id).Scan(&found)
if err == sql.ErrNoRows {
return 0, fmt.Errorf("pipeline not found")
}
return found, err
}
// SetArchived archives or unarchives one pipeline. A positive expectedVersion
// refuses the change when a newer version was published meanwhile. Archiving
// is refused while an enabled schedule still points at the pipeline, naming
// the schedules, so nothing silently stops firing; repeating a request that is
// already in effect is a no-op.
func (s *Store) SetArchived(ctx context.Context, id int64, archived bool, expectedVersion int, now time.Time) error {
if expectedVersion < 0 {
return fmt.Errorf("expected version must be nonnegative")
}
s.mu.Lock()
defer s.mu.Unlock()
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
var name string
var stamp sql.NullString
var latest int
err = tx.QueryRowContext(ctx, `SELECT p.name,p.archived_at,COALESCE((SELECT MAX(version) FROM pipeline_versions WHERE pipeline_id=p.id),0) FROM pipelines p WHERE p.id=?`, id).Scan(&name, &stamp, &latest)
if err == sql.ErrNoRows {
return fmt.Errorf("pipeline not found")
}
if err != nil {
return err
}
if expectedVersion > 0 && latest != expectedVersion {
return fmt.Errorf("pipeline %q changed: expected latest version %d, found %d; reload first", name, expectedVersion, latest)
}
if stamp.Valid == archived {
return tx.Commit()
}
if !archived {
if _, err = tx.ExecContext(ctx, `UPDATE pipelines SET archived_at=NULL WHERE id=?`, id); err != nil {
return err
}
return tx.Commit()
}
rows, err := tx.QueryContext(ctx, savedScheduleSelect+` WHERE pipeline_id=? AND deleted=0 AND enabled=1 ORDER BY id`, id)
if err != nil {
return err
}
var enabled []string
for rows.Next() {
item, err := scanSavedSchedule(rows)
if err != nil {
rows.Close()
return err
}
enabled = append(enabled, fmt.Sprintf("%q (#%d)", item.Name, item.ID))
}
err = rows.Err()
rows.Close()
if err != nil {
return err
}
if len(enabled) > 0 {
return fmt.Errorf("pipeline %q has enabled schedule(s) %s; pause or repoint them before archiving", name, strings.Join(enabled, ", "))
}
if _, err = tx.ExecContext(ctx, `UPDATE pipelines SET archived_at=? WHERE id=?`, now.UTC().Format(time.RFC3339), id); err != nil {
return err
}
return tx.Commit()
}
// archivedName reports whether the pipeline row is archived inside a
// transaction, so a submission or schedule edit cannot race an archive.
func archivedName(ctx context.Context, tx *sql.Tx, query string, arg any) (string, bool, error) {
var name string
var stamp sql.NullString
err := tx.QueryRowContext(ctx, `SELECT name,archived_at FROM pipelines WHERE `+query, arg).Scan(&name, &stamp)
if err == sql.ErrNoRows {
return "", false, nil
}
return name, stamp.Valid, err
}
+117
View File
@@ -0,0 +1,117 @@
package pipeline
import (
"context"
"database/sql"
"errors"
"path/filepath"
"strings"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
_ "modernc.org/sqlite"
)
func TestArchiveHidesReservesAndGuardsSchedules(t *testing.T) {
ctx := context.Background()
s, err := Open(filepath.Join(t.TempDir(), "archive.db"))
if err != nil {
t.Fatal(err)
}
defer s.Close()
if _, err := s.Submit(ctx, "old", Template([]string{"one"}, true)); err != nil {
t.Fatal(err)
}
if _, err := s.Submit(ctx, "keep", Template([]string{"one"}, true)); err != nil {
t.Fatal(err)
}
id, err := s.ResolvePipelineID(ctx, 0, "old")
if err != nil {
t.Fatal(err)
}
if byID, err := s.ResolvePipelineID(ctx, 0, "1"); err != nil || byID != id {
t.Fatal("numeric name did not resolve as an ID", byID, err)
}
spec := protocol.SavedSchedule{Name: "nightly", PipelineID: id, Version: 1, Enabled: true, Workspace: t.TempDir(), Input: map[string]any{"prompt": "go"}, Rule: protocol.Schedule{Every: "1h"}}
saved, err := s.SaveSchedule(ctx, spec, time.Now())
if err != nil {
t.Fatal(err)
}
err = s.SetArchived(ctx, id, true, 0, time.Now())
if err == nil || !strings.Contains(err.Error(), `"nightly"`) || !strings.Contains(err.Error(), "pause") {
t.Fatal("archive with an enabled schedule was not refused by name", err)
}
paused := *saved
paused.Enabled = false
if _, err = s.SaveSchedule(ctx, paused, time.Now()); err != nil {
t.Fatal(err)
}
if err = s.SetArchived(ctx, id, true, 7, time.Now()); err == nil || !strings.Contains(err.Error(), "expected latest version 7") {
t.Fatal("stale expected version accepted", err)
}
if err = s.SetArchived(ctx, id, true, 1, time.Now()); err != nil {
t.Fatal(err)
}
if err = s.SetArchived(ctx, id, true, 0, time.Now()); err != nil {
t.Fatal("repeated archive must be a no-op", err)
}
active, _ := s.List(ctx)
if len(active) != 1 || active[0].Name != "keep" {
t.Fatal("archived pipeline listed by default", active)
}
all, _ := s.ListPipelines(ctx, true)
if len(all) != 2 || all[1].Name != "old" || all[1].ArchivedAt == "" || all[0].ArchivedAt != "" {
t.Fatal("include_archived did not mark the archived pipeline", all)
}
detail, err := s.Get(ctx, id, 1)
if err != nil || detail.ArchivedAt == "" || detail.Source == "" {
t.Fatal("archived detail lost history", detail, err)
}
var archived *ArchivedError
if _, err = s.Submit(ctx, "old", Template([]string{"one", "two"}, true), true); !errors.As(err, &archived) || !strings.Contains(err.Error(), "unarchive") {
t.Fatal("create-only reuse of an archived name not refused", err)
}
if _, err = s.Submit(ctx, "old", Template([]string{"one", "two"}, true)); !errors.As(err, &archived) {
t.Fatal("new version of an archived pipeline accepted", err)
}
enabled := paused
enabled.Revision++
enabled.Enabled = true
if _, err = s.SaveSchedule(ctx, enabled, time.Now()); !errors.As(err, &archived) {
t.Fatal("enabled schedule saved for an archived pipeline", err)
}
if err = s.SetArchived(ctx, id, false, 1, time.Now()); err != nil {
t.Fatal(err)
}
if active, _ = s.List(ctx); len(active) != 2 {
t.Fatal("unarchived pipeline still hidden", active)
}
if v, err := s.Submit(ctx, "old", Template([]string{"one", "two"}, true)); err != nil || v != 2 {
t.Fatal("unarchived pipeline cannot publish", v, err)
}
}
func TestArchiveColumnMigratesExistingDatabase(t *testing.T) {
path := filepath.Join(t.TempDir(), "legacy.db")
db, err := sql.Open("sqlite", path)
if err != nil {
t.Fatal(err)
}
if _, err = db.Exec(`CREATE TABLE pipelines (id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE, created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP);
INSERT INTO pipelines(name) VALUES ('legacy');`); err != nil {
t.Fatal(err)
}
db.Close()
for range 2 {
s, err := Open(path)
if err != nil {
t.Fatal(err)
}
var stamp sql.NullString
if err = s.db.QueryRow(`SELECT archived_at FROM pipelines WHERE name='legacy'`).Scan(&stamp); err != nil || stamp.Valid {
t.Fatal("legacy pipeline not migrated as active", stamp, err)
}
s.Close()
}
}
+9
View File
@@ -267,6 +267,15 @@ func (s *Store) SaveSchedule(ctx context.Context, spec protocol.SavedSchedule, n
return nil, err return nil, err
} }
defer tx.Rollback() defer tx.Rollback()
// An archived pipeline cannot gain an enabled schedule; a paused one may
// still be saved so the operator can repoint or tidy it.
if spec.Enabled {
if name, archived, err := archivedName(ctx, tx, "id=?", spec.PipelineID); err != nil {
return nil, err
} else if archived {
return nil, &ArchivedError{Name: name}
}
}
if spec.ID == 0 && requestKey != "" { if spec.ID == 0 && requestKey != "" {
var id int64 var id int64
var oldHash string var oldHash string
+26 -5
View File
@@ -61,6 +61,10 @@ CREATE TABLE IF NOT EXISTS submissions (id INTEGER PRIMARY KEY, name TEXT NOT NU
return nil, err return nil, err
} }
s := &Store{db: db, dir: filepath.Dir(path), path: path, done: make(chan struct{}), created: existing == 0} s := &Store{db: db, dir: filepath.Dir(path), path: path, done: make(chan struct{}), created: existing == 0}
if err := s.initArchive(); err != nil {
db.Close()
return nil, err
}
if err := s.initRuns(); err != nil { if err := s.initRuns(); err != nil {
db.Close() db.Close()
return nil, err return nil, err
@@ -173,8 +177,15 @@ func OpenMaintenance(path string) (*Store, error) {
// adjacent server state such as stage artifacts. // adjacent server state such as stage artifacts.
func (s *Store) Directory() string { return s.dir } func (s *Store) Directory() string { return s.dir }
// List returns the active (unarchived) pipelines.
func (s *Store) List(ctx context.Context) ([]protocol.PipelineSummary, error) { func (s *Store) List(ctx context.Context) ([]protocol.PipelineSummary, error) {
rows, err := s.db.QueryContext(ctx, `SELECT p.id, p.name, v.version, return s.ListPipelines(ctx, false)
}
// ListPipelines returns the latest version of every pipeline; archived ones
// are included, marked by ArchivedAt, only when includeArchived is set.
func (s *Store) ListPipelines(ctx context.Context, includeArchived bool) ([]protocol.PipelineSummary, error) {
rows, err := s.db.QueryContext(ctx, `SELECT p.id, p.name, v.version, COALESCE(p.archived_at, ''),
json_array_length(v.graph_json, '$.stages'), json_array_length(v.graph_json, '$.stages'),
strftime('%Y-%m-%dT%H:%M:%SZ', p.created_at), strftime('%Y-%m-%dT%H:%M:%SZ', v.created_at), strftime('%Y-%m-%dT%H:%M:%SZ', p.created_at), strftime('%Y-%m-%dT%H:%M:%SZ', v.created_at),
v.graph_json, v.graph_json,
@@ -182,7 +193,8 @@ v.graph_json,
(SELECT COUNT(*) FROM saved_schedules WHERE pipeline_id=p.id AND deleted=0 AND enabled=1) (SELECT COUNT(*) FROM saved_schedules WHERE pipeline_id=p.id AND deleted=0 AND enabled=1)
FROM pipelines p JOIN pipeline_versions v ON v.pipeline_id=p.id FROM pipelines p JOIN pipeline_versions v ON v.pipeline_id=p.id
WHERE v.version=(SELECT MAX(version) FROM pipeline_versions WHERE pipeline_id=p.id) WHERE v.version=(SELECT MAX(version) FROM pipeline_versions WHERE pipeline_id=p.id)
ORDER BY p.name, p.id`) AND (? OR p.archived_at IS NULL)
ORDER BY p.name, p.id`, includeArchived)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -191,7 +203,7 @@ ORDER BY p.name, p.id`)
for rows.Next() { for rows.Next() {
var item protocol.PipelineSummary var item protocol.PipelineSummary
var graphJSON string var graphJSON string
if err := rows.Scan(&item.ID, &item.Name, &item.Version, &item.Stages, &item.CreatedAt, &item.UpdatedAt, &graphJSON, &item.ScheduleCount, &item.EnabledScheduleCount); err != nil { if err := rows.Scan(&item.ID, &item.Name, &item.Version, &item.ArchivedAt, &item.Stages, &item.CreatedAt, &item.UpdatedAt, &graphJSON, &item.ScheduleCount, &item.EnabledScheduleCount); err != nil {
return nil, err return nil, err
} }
var shape protocol.Graph var shape protocol.Graph
@@ -231,10 +243,10 @@ func (s *Store) Get(ctx context.Context, id int64, version int) (*protocol.Pipel
defer tx.Rollback() defer tx.Rollback()
detail := &protocol.PipelineDetail{} detail := &protocol.PipelineDetail{}
err = tx.QueryRowContext(ctx, `SELECT p.id,p.name,strftime('%Y-%m-%dT%H:%M:%SZ',p.created_at), err = tx.QueryRowContext(ctx, `SELECT p.id,p.name,strftime('%Y-%m-%dT%H:%M:%SZ',p.created_at),
v.version,strftime('%Y-%m-%dT%H:%M:%SZ',v.created_at) v.version,strftime('%Y-%m-%dT%H:%M:%SZ',v.created_at),COALESCE(p.archived_at,'')
FROM pipelines p JOIN pipeline_versions v ON v.pipeline_id=p.id FROM pipelines p JOIN pipeline_versions v ON v.pipeline_id=p.id
WHERE p.id=? ORDER BY v.version DESC LIMIT 1`, id). WHERE p.id=? ORDER BY v.version DESC LIMIT 1`, id).
Scan(&detail.ID, &detail.Name, &detail.CreatedAt, &detail.LatestVersion, &detail.UpdatedAt) Scan(&detail.ID, &detail.Name, &detail.CreatedAt, &detail.LatestVersion, &detail.UpdatedAt, &detail.ArchivedAt)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return nil, fmt.Errorf("pipeline not found") return nil, fmt.Errorf("pipeline not found")
} }
@@ -337,6 +349,15 @@ func (s *Store) submit(ctx context.Context, name, source string, createOnly bool
var version int var version int
var versionID any var versionID any
hash := fmt.Sprintf("%x", sha256.Sum256([]byte(source))) hash := fmt.Sprintf("%x", sha256.Sum256([]byte(source)))
if validation == nil {
// An archived name stays reserved: neither a fresh pipeline nor a new
// version may take it until the operator unarchives it.
if _, archived, err := archivedName(ctx, tx, "name=?", name); err != nil {
return 0, err
} else if archived {
validation = &ArchivedError{Name: name}
}
}
if validation == nil { if validation == nil {
_, err = tx.ExecContext(ctx, "INSERT INTO pipelines(name) VALUES (?) ON CONFLICT(name) DO NOTHING", name) _, err = tx.ExecContext(ctx, "INSERT INTO pipelines(name) VALUES (?) ON CONFLICT(name) DO NOTHING", name)
if err != nil { if err != nil {
+12
View File
@@ -16,6 +16,13 @@ const (
Templates = "templates" Templates = "templates"
Validate = "validate" Validate = "validate"
Validated = "validated" Validated = "validated"
// Archive and unarchive name the pipeline by PipelineID or Name; an
// optional ExpectedVersion guards against a concurrent publication. Both
// reply with the pipeline detail (ArchivedAt set while archived).
ArchivePipeline = "archive_pipeline"
PipelineArchived = "pipeline_archived"
UnarchivePipeline = "unarchive_pipeline"
PipelineUnarchived = "pipeline_unarchived"
) )
type Stage struct { type Stage struct {
@@ -103,6 +110,10 @@ type PipelineSummary struct {
Stages int `json:"stages"` Stages int `json:"stages"`
CreatedAt string `json:"created_at"` CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"` UpdatedAt string `json:"updated_at"`
// ArchivedAt is set (RFC 3339) while the pipeline is archived: it is
// hidden from list_pipelines unless include_archived is set, cannot start
// runs and keeps its name reserved. History is untouched.
ArchivedAt string `json:"archived_at,omitempty"`
// Shape is the current version's parsed graph, included so a list can draw // Shape is the current version's parsed graph, included so a list can draw
// a miniature without one detail request per row. Detail responses keep the // a miniature without one detail request per row. Detail responses keep the
// full Graph field; this one is deliberately optional and additive. // full Graph field; this one is deliberately optional and additive.
@@ -131,6 +142,7 @@ type Message struct {
ExpectedVersion int `json:"expected_version,omitempty"` ExpectedVersion int `json:"expected_version,omitempty"`
Templates []Template `json:"templates,omitempty"` Templates []Template `json:"templates,omitempty"`
CreateOnly bool `json:"create_only,omitempty"` CreateOnly bool `json:"create_only,omitempty"`
IncludeArchived bool `json:"include_archived,omitempty"`
NotificationEndpoints []NotificationEndpoint `json:"notification_endpoints,omitempty"` NotificationEndpoints []NotificationEndpoint `json:"notification_endpoints,omitempty"`
Type string `json:"type"` Type string `json:"type"`
ID string `json:"id,omitempty"` ID string `json:"id,omitempty"`
+23
View File
@@ -0,0 +1,23 @@
package server
import (
"context"
"time"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// archivePipeline serves archive_pipeline and unarchive_pipeline. Like saving
// a schedule it is a lifecycle change, not authoring: no definition or version
// is written, so it is not gated by AllowAuthoring.
func archivePipeline(ctx context.Context, store *pipeline.Store, request protocol.Message) (*protocol.PipelineDetail, error) {
id, err := store.ResolvePipelineID(ctx, request.PipelineID, request.Name)
if err != nil {
return nil, err
}
if err := store.SetArchived(ctx, id, request.Type == protocol.ArchivePipeline, request.ExpectedVersion, time.Now()); err != nil {
return nil, err
}
return store.Get(ctx, id, 0)
}
+157
View File
@@ -0,0 +1,157 @@
package server
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http/httptest"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/cli"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
)
func TestArchivePipelineOverCLIKeepsHistoryAndRefusesRuns(t *testing.T) {
t.Setenv("PIPELINES_HARNESS_CONFIG", filepath.Join(t.TempDir(), "harnesses.json"))
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
store, err := pipeline.Open(filepath.Join(t.TempDir(), "archive.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
if _, err = store.Submit(ctx, "old flow", pipeline.Template([]string{"worker"}, true)); err != nil {
t.Fatal(err)
}
if _, err = store.Submit(ctx, "current", pipeline.Template([]string{"worker"}, true)); err != nil {
t.Fatal(err)
}
handler := Handler(ctx, store)
r := handler.(*runHandler).runner
// The first run fails so a retry of it can be attempted after archiving.
var failing atomic.Bool
failing.Store(true)
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
if failing.Load() {
return stage.Result{}, fmt.Errorf("stage failed")
}
return stage.Result{Outcome: "success", Output: stage.Values{"summary": "done"}}, nil
}
srv := httptest.NewServer(handler)
defer srv.Close()
defer func() { cancel(); WaitRuns(handler) }()
url := "ws" + strings.TrimPrefix(srv.URL, "http") + "/connect"
run := func(args ...string) (string, error) {
var out bytes.Buffer
app := cli.App{Out: &out, Err: &out, In: strings.NewReader("")}
err := app.Run(ctx, append([]string{"--url", url}, args...))
return out.String(), err
}
workspace := t.TempDir()
output, err := run("run", "old flow", "before archive", "--yes", "--json", "--workspace", workspace)
if err != nil {
t.Fatal(output, err)
}
var started protocol.Message
if err = json.Unmarshal([]byte(output), &started); err != nil || started.RunDetail == nil {
t.Fatal(output, err)
}
runID := started.RunDetail.ID
if detail := waitTerminal(t, r, runID); detail.Status != "failed" {
t.Fatal(detail.Status)
}
failing.Store(false)
items, _ := store.List(ctx)
schedule := protocol.SavedSchedule{Name: "nightly", PipelineID: items[1].ID, Version: 1, Enabled: true, Workspace: workspace, Input: map[string]any{"prompt": "go"}, Rule: protocol.Schedule{Every: "24h"}}
if items[1].Name != "old flow" {
t.Fatal(items)
}
saved, err := store.SaveSchedule(ctx, schedule, time.Now())
if err != nil {
t.Fatal(err)
}
if output, err = run("archive", "old flow", "--yes"); err == nil || !strings.Contains(err.Error(), `"nightly"`) {
t.Fatal("archive with an enabled schedule not refused by name", output, err)
}
saved.Enabled = false
if _, err = store.SaveSchedule(ctx, *saved, time.Now()); err != nil {
t.Fatal(err)
}
if output, err = run("archive", "old flow", "--yes"); err != nil || !strings.Contains(output, "Archived old flow") {
t.Fatal(output, err)
}
if output, _ = run("list"); strings.Contains(output, "old flow") || !strings.Contains(output, "current") {
t.Fatal("default list shows archived pipeline", output)
}
if output, _ = run("list", "--archived"); !strings.Contains(output, "old flow\tv1\t1 stages\tarchived ") || strings.Contains(output, "current") {
t.Fatal("list --archived", output)
}
if output, _ = run("list", "--all"); !strings.Contains(output, "old flow") || !strings.Contains(output, "current") {
t.Fatal("list --all", output)
}
if output, err = run("run", "old flow", "after archive", "--yes", "--json", "--workspace", workspace); err == nil || !strings.Contains(err.Error(), "is archived; unarchive it first") {
t.Fatal("archived pipeline started a run", output, err)
}
if output, err = run("retry", fmt.Sprint(runID), "--yes", "--mode", "restart", "--json"); err == nil || !strings.Contains(err.Error(), "archived") {
t.Fatal("archived pipeline retried a run", output, err)
}
// History stays: the run, its inspection, filtering by name and search.
if output, err = run("inspect", fmt.Sprint(runID), "--json"); err != nil || !strings.Contains(output, `"old flow"`) {
t.Fatal("archived run inspection", output, err)
}
if output, err = run("runs", "--pipeline", "old flow"); err != nil || !strings.Contains(output, "old flow") {
t.Fatal("archived run history", output, err)
}
if output, err = run("show", "old flow", "--json"); err != nil || !strings.Contains(output, "archived_at") {
t.Fatal("archived show", output, err)
}
if output, err = run("search", "old flow", "--type", "run"); err != nil || !strings.Contains(output, fmt.Sprintf("run:%d", runID)) {
t.Fatal("archived run search", output, err)
}
if output, err = run("new", "old flow", "--no-open", "--yes"); err == nil || !strings.Contains(err.Error(), "archived") {
t.Fatal("archived name reused by new", output, err)
}
if output, err = run("unarchive", "old flow"); err != nil || !strings.Contains(output, "Unarchived old flow") {
t.Fatal(output, err)
}
if output, err = run("run", "old flow", "after unarchive", "--yes", "--json", "--workspace", workspace); err != nil {
t.Fatal("unarchived pipeline cannot run", output, err)
}
}
func TestArchiveProtocolByNameReportsDetail(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
store, err := pipeline.Open(filepath.Join(t.TempDir(), "archive-rpc.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
if _, err = store.Submit(ctx, "retire", pipeline.Template([]string{"one"}, true)); err != nil {
t.Fatal(err)
}
handler := HandlerWithOptions(ctx, Options{}, store)
defer func() { cancel(); WaitRuns(handler) }()
srv := httptest.NewServer(handler)
defer srv.Close()
var out bytes.Buffer
app := cli.App{Out: &out, Err: &out, In: strings.NewReader(`{"type":"archive_pipeline","name":"retire","expected_version":1}`)}
if err = app.Run(ctx, []string{"--url", "ws" + strings.TrimPrefix(srv.URL, "http") + "/connect", "rpc", "-"}); err != nil {
t.Fatal(out.String(), err)
}
var reply protocol.Message
if err = json.Unmarshal(out.Bytes(), &reply); err != nil || reply.Type != protocol.PipelineArchived || reply.Detail == nil || reply.Detail.ArchivedAt == "" {
t.Fatal("archive reply", out.String(), err)
}
all, _ := store.ListPipelines(ctx, true)
if len(all) != 1 || all[0].ArchivedAt == "" {
t.Fatal(all)
}
}
+3
View File
@@ -212,6 +212,9 @@ func (r *runner) startWithSchedule(request protocol.Message, origin *pipeline.Sc
if err != nil { if err != nil {
return nil, err return nil, err
} }
if d.ArchivedAt != "" {
return nil, &pipeline.ArchivedError{Name: d.Name}
}
if _, err := pipeline.Parse(pipeline.ExecutionSource(d.Source)); err != nil { if _, err := pipeline.Parse(pipeline.ExecutionSource(d.Source)); err != nil {
return nil, err return nil, err
} }
+12 -1
View File
@@ -354,13 +354,24 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
} else if request.Type == protocol.List && len(stores) > 0 { } else if request.Type == protocol.List && len(stores) > 0 {
response.Type = protocol.PipelineList response.Type = protocol.PipelineList
response.DefaultModel, response.DefaultHarness = starterDefaults() response.DefaultModel, response.DefaultHarness = starterDefaults()
items, err := stores[0].List(ctx) items, err := stores[0].ListPipelines(ctx, request.IncludeArchived)
if err != nil { if err != nil {
response.Type = protocol.Error response.Type = protocol.Error
response.Error = err.Error() response.Error = err.Error()
} else { } else {
response.Pipelines = items response.Pipelines = items
} }
} else if (request.Type == protocol.ArchivePipeline || request.Type == protocol.UnarchivePipeline) && len(stores) > 0 {
detail, err := archivePipeline(ctx, stores[0], request)
response.Detail = detail
response.Type = protocol.PipelineArchived
if request.Type == protocol.UnarchivePipeline {
response.Type = protocol.PipelineUnarchived
}
if err != nil {
response.Type = protocol.Error
response.Error = err.Error()
}
} else if request.Type == protocol.Search { } else if request.Type == protocol.Search {
if len(stores) == 0 { if len(stores) == 0 {
response.Type = protocol.Error response.Type = protocol.Error