diff --git a/app/lib/features/pipelines/pipeline_models.dart b/app/lib/features/pipelines/pipeline_models.dart index ed1c6ad8..ad0d2b49 100644 --- a/app/lib/features/pipelines/pipeline_models.dart +++ b/app/lib/features/pipelines/pipeline_models.dart @@ -25,6 +25,7 @@ class PipelineSummary { this.recordedStages, this.flowStages, this.shape, + this.archivedAt, }); factory PipelineSummary.fromJson(PipelineJson json) { @@ -47,6 +48,7 @@ class PipelineSummary { scheduleCount: scheduleCount, enabledScheduleCount: _optionalInt(json['enabled_schedule_count']), shape: _jsonObject(json['shape']), + archivedAt: _optionalString(json['archived_at']), ); } @@ -77,6 +79,12 @@ class PipelineSummary { /// empty flow. 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 /// 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. @@ -146,6 +154,7 @@ class PipelineDetail extends PipelineSummary { super.recordedStages, super.flowStages, super.shape, + super.archivedAt, required this.latestVersion, required this.versionCreatedAt, required this.contentHash, @@ -170,6 +179,7 @@ class PipelineDetail extends PipelineSummary { recordedStages: summary.recordedStages, flowStages: summary.flowStages, shape: summary.shape, + archivedAt: summary.archivedAt, latestVersion: _int(json['latest_version']), versionCreatedAt: _string(json['version_created_at']), contentHash: _string(json['content_hash']), @@ -270,6 +280,9 @@ int? _optionalInt(Object? 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`), /// or null when that flow was not recorded with a stage list. int? _flowStageCount(Object? value) { diff --git a/app/lib/features/pipelines/pipeline_repository.dart b/app/lib/features/pipelines/pipeline_repository.dart index 4f3d8e58..017e4cfe 100644 --- a/app/lib/features/pipelines/pipeline_repository.dart +++ b/app/lib/features/pipelines/pipeline_repository.dart @@ -11,16 +11,19 @@ class PipelineRequestException implements Exception { String toString() => message; } -/// Read-only pipeline requests. Request shapes mirror `web/src/repository.js` -/// exactly; no mutation of server state is offered here. +/// Pipeline requests. Reads mirror `web/src/repository.js`; the one mutation +/// is archiving, which keeps every version and run. class PipelinesRepository { PipelinesRepository(this._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> 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.'); final type = envelope['type']; if (type != null && type != 'pipelines') { @@ -69,6 +72,28 @@ class PipelinesRepository { return PipelineDetail.fromJson(Map.from(detail)); } + /// `archive_pipeline` / `unarchive_pipeline`, guarded by the latest + /// version the operator saw. Replies with the updated detail. + Future 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.from(detail)); + } + /// `list_runs` is read only here for the pipeline list's last-run join. Future> listRuns() async { final envelope = await _gateway.call('list_runs'); diff --git a/app/lib/features/pipelines/pipelines_controller.dart b/app/lib/features/pipelines/pipelines_controller.dart index e95817ba..8ca3d68f 100644 --- a/app/lib/features/pipelines/pipelines_controller.dart +++ b/app/lib/features/pipelines/pipelines_controller.dart @@ -27,7 +27,9 @@ class PipelinesController extends ChangeNotifier { final Set _loadedPendingRuns = {}; final Map _pendingRunGenerations = {}; - List _pipelines = const []; + /// Every row the server listed, archived ones included. + List _allPipelines = const []; + bool _showArchived = false; List _runs = const []; PipelineFilters _filters = emptyPipelineFilters; PipelineSort _sort = PipelineSort.serverOrder; @@ -60,7 +62,13 @@ class PipelinesController extends ChangeNotifier { final Map _pendingRuns = {}; - List 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 get pipelines => List.unmodifiable( + _allPipelines.where((pipeline) => pipeline.archived == _showArchived), + ); + + bool get showArchived => _showArchived; PipelineFilters get filters => _filters; PipelineSort get sort => _sort; 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. List get visiblePipelines { final runs = latestRuns; - final sorted = sortPipelines(_pipelines, _sort, runs); + final sorted = sortPipelines(pipelines, _sort, runs); return filterPipelines(sorted, _filters, runs, _runsLoaded); } - PipelineEmptyState? get emptyState => - pipelineListEmptyState(_pipelines, visiblePipelines, _filters); + PipelineEmptyState? get emptyState => _showArchived && pipelines.isEmpty + ? const PipelineEmptyState('No archived pipelines.') + : pipelineListEmptyState(pipelines, visiblePipelines, _filters); /// Reloads the list and runs and re-reads the current detail. The selection /// (pipeline id and requested version) is deliberately preserved. @@ -110,7 +119,7 @@ class PipelinesController extends ChangeNotifier { try { final rows = await _repository.listPipelines(); if (!_current(_listSequence, token)) return; - _pipelines = rows; + _allPipelines = rows; _pipelinesLoaded = true; _listError = null; } catch (error) { @@ -213,6 +222,35 @@ class PipelinesController extends ChangeNotifier { _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 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) { _sort = sort; _notify(); diff --git a/app/lib/features/pipelines/widgets/pipeline_archive_action.dart b/app/lib/features/pipelines/widgets/pipeline_archive_action.dart new file mode 100644 index 00000000..940359a5 --- /dev/null +++ b/app/lib/features/pipelines/widgets/pipeline_archive_action.dart @@ -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( + 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 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 _confirmArchive(BuildContext context, String name) async { + final title = Text('Archive $name?'); + final body = Text(archiveConfirmation(name)); + final confirmed = useCupertinoControls(context) + ? await showCupertinoDialog( + 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( + 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 _showRefusal(BuildContext context, String title, String message) { + if (useCupertinoControls(context)) { + return showCupertinoDialog( + 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( + context: context, + builder: (context) => AlertDialog( + title: Text(title), + content: Text(message), + actions: [ + TextButton( + onPressed: () => Navigator.pop(context), + child: const Text('OK'), + ), + ], + ), + ); +} diff --git a/app/lib/features/pipelines/widgets/pipeline_detail_panel.dart b/app/lib/features/pipelines/widgets/pipeline_detail_panel.dart index 57463fab..84ae6883 100644 --- a/app/lib/features/pipelines/widgets/pipeline_detail_panel.dart +++ b/app/lib/features/pipelines/widgets/pipeline_detail_panel.dart @@ -14,6 +14,7 @@ import '../pipeline_models.dart'; import '../pipeline_query.dart'; import '../pipelines_controller.dart'; import '../ui/run_pipeline_action.dart'; +import 'pipeline_archive_action.dart'; import 'pipeline_controls.dart'; import 'pipeline_bar_buttons.dart'; import '../../../ui/text/markdown_source_view.dart'; @@ -114,6 +115,8 @@ class _PipelineDetailPanelState extends State { _applyStageRequest(context, detail); final notices = [ if (!inBar) _header(context, detail), + if (detail.archived) + PipelineArchivedNotice(controller: _controller, detail: detail), if (_controller.detailLoading) const Padding( padding: EdgeInsets.symmetric(horizontal: 16), @@ -394,8 +397,9 @@ class _PipelineDetailPanelState extends State { ); } - /// Runs and the primary Run: the top bar's actions, never floating over - /// the content (where they used to cover the version history). + /// Runs, the primary Run and the pipeline's actions menu: the top bar's + /// 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 _actions(BuildContext context, PipelineDetail detail) => [ pipelineBarIconButton( context, @@ -409,13 +413,15 @@ class _PipelineDetailPanelState extends State { ? null : () => widget.onViewPipelineRuns!(detail.id), ), - RunPipelineAction( - controller: _controller, - detail: detail, - gateway: widget.gateway, - onRunStarted: widget.onRunStarted, - inBar: true, - ), + if (!detail.archived) + RunPipelineAction( + controller: _controller, + detail: detail, + gateway: widget.gateway, + onRunStarted: widget.onRunStarted, + inBar: true, + ), + pipelineActionsMenu(context, _controller, detail), ]; Widget _source(BuildContext context, PipelineDetail detail) { diff --git a/app/lib/features/pipelines/widgets/pipeline_list_panel.dart b/app/lib/features/pipelines/widgets/pipeline_list_panel.dart index e3744358..bbadc56b 100644 --- a/app/lib/features/pipelines/widgets/pipeline_list_panel.dart +++ b/app/lib/features/pipelines/widgets/pipeline_list_panel.dart @@ -82,18 +82,24 @@ class PipelineListPanel extends StatelessWidget { /// 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. - /// 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) { - final scheduled = filters.schedule == ScheduleFilter.scheduled; - void pick(bool value) => controller.setFilters( - filters.copyWith( - schedule: value ? ScheduleFilter.scheduled : ScheduleFilter.any, - ), - ); - final control = PipelineScopeControl( - scheduled: scheduled, - onSelected: pick, - ); + final scope = controller.showArchived + ? PipelineScope.archived + : filters.schedule == ScheduleFilter.scheduled + ? PipelineScope.scheduled + : PipelineScope.all; + void pick(PipelineScope value) { + controller.setShowArchived(value == PipelineScope.archived); + final schedule = value == PipelineScope.scheduled + ? ScheduleFilter.scheduled + : ScheduleFilter.any; + if (filters.schedule != schedule) { + controller.setFilters(filters.copyWith(schedule: schedule)); + } + } + + final control = PipelineScopeControl(scope: scope, onSelected: pick); return Padding( padding: const EdgeInsets.fromLTRB( kPipelineListInset, diff --git a/app/lib/features/pipelines/widgets/pipeline_scope_control.dart b/app/lib/features/pipelines/widgets/pipeline_scope_control.dart index a7fc0601..ae1b5afe 100644 --- a/app/lib/features/pipelines/widgets/pipeline_scope_control.dart +++ b/app/lib/features/pipelines/widgets/pipeline_scope_control.dart @@ -3,18 +3,32 @@ import 'package:flutter/material.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 /// text scale. The decision is content-measured, not a fixed pane width. class PipelineScopeControl extends StatelessWidget { const PipelineScopeControl({ super.key, - required this.scheduled, + required this.scope, required this.onSelected, }); - final bool scheduled; - final ValueChanged onSelected; + final PipelineScope scope; + final ValueChanged onSelected; - static const labels = {false: 'All', true: 'Scheduled'}; + static final labels = { + for (final scope in PipelineScope.values) scope: scope.label, + }; @override Widget build(BuildContext context) { @@ -36,30 +50,31 @@ class PipelineScopeControl extends StatelessWidget { builder: (context, constraints) { if (constraints.maxWidth >= requiredWidth) { return cupertino - ? CupertinoSlidingSegmentedControl( + ? CupertinoSlidingSegmentedControl( key: const Key('pipeline-scope'), - groupValue: scheduled, - onValueChanged: (value) => onSelected(value ?? false), + groupValue: scope, + onValueChanged: (value) => + onSelected(value ?? PipelineScope.all), children: { for (final entry in labels.entries) entry.key: Text(entry.value), }, ) - : SegmentedButton( + : SegmentedButton( key: const Key('pipeline-scope'), showSelectedIcon: false, segments: [ for (final entry in labels.entries) ButtonSegment(value: entry.key, label: Text(entry.value)), ], - selected: {scheduled}, + selected: {scope}, onSelectionChanged: (value) => onSelected(value.single), ); } if (!cupertino) { - return DropdownButton( + return DropdownButton( key: const Key('pipeline-scope'), - value: scheduled, + value: scope, isExpanded: true, items: [ for (final entry in labels.entries) @@ -104,7 +119,7 @@ class PipelineScopeControl extends StatelessWidget { children: [ Expanded( child: Text( - labels[scheduled]!, + labels[scope]!, maxLines: 1, overflow: TextOverflow.ellipsis, ), diff --git a/app/test/features/pipelines/pipeline_archive_test.dart b/app/test/features/pipelines/pipeline_archive_test.dart new file mode 100644 index 00000000..489f78b2 --- /dev/null +++ b/app/test/features/pipelines/pipeline_archive_test.dart @@ -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 = {}; + Map mark(Map 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 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 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; + } + }); +} diff --git a/app/test/features/pipelines/pipeline_scope_control_test.dart b/app/test/features/pipelines/pipeline_scope_control_test.dart index 9167cfa8..bd4db29b 100644 --- a/app/test/features/pipelines/pipeline_scope_control_test.dart +++ b/app/test/features/pipelines/pipeline_scope_control_test.dart @@ -9,14 +9,14 @@ void main() { testWidgets( 'native scope remains usable at $width, Cupertino=$cupertino', (tester) async { - var scheduled = false; + var scope = PipelineScope.all; final control = StatefulBuilder( builder: (context, setState) => Center( child: SizedBox( width: width, child: PipelineScopeControl( - scheduled: scheduled, - onSelected: (value) => setState(() => scheduled = value), + scope: scope, + onSelected: (value) => setState(() => scope = value), ), ), ), @@ -35,7 +35,7 @@ void main() { await tester.tap(find.text('Scheduled')); } await tester.pumpAndSettle(); - expect(scheduled, isTrue); + expect(scope, PipelineScope.scheduled); expect(tester.takeException(), isNull); }, ); @@ -50,7 +50,10 @@ void main() { width: 500, child: Align( alignment: AlignmentDirectional.centerStart, - child: PipelineScopeControl(scheduled: false, onSelected: (_) {}), + child: PipelineScopeControl( + scope: PipelineScope.all, + onSelected: (_) {}, + ), ), ), ), diff --git a/app/test/features/pipelines/pipelines_controller_load_test.dart b/app/test/features/pipelines/pipelines_controller_load_test.dart index 7662f6c8..9ce8ad47 100644 --- a/app/test/features/pipelines/pipelines_controller_load_test.dart +++ b/app/test/features/pipelines/pipelines_controller_load_test.dart @@ -31,7 +31,7 @@ void main() { 'list_pipelines', 'list_runs', ]); - expect(gateway.calls[0].fields, isEmpty); + expect(gateway.calls[0].fields, {'include_archived': true}); expect(gateway.calls[1].fields, isEmpty); expect(controller.pipelines.map((pipeline) => pipeline.id), [1, 2, 3]); expect(controller.pipelinesLoaded, isTrue); diff --git a/app/test/features/pipelines/pipelines_screen_filter_test.dart b/app/test/features/pipelines/pipelines_screen_filter_test.dart index 4233d6f8..0ac94f43 100644 --- a/app/test/features/pipelines/pipelines_screen_filter_test.dart +++ b/app/test/features/pipelines/pipelines_screen_filter_test.dart @@ -127,24 +127,12 @@ void main() { expect(find.text('Alpha review'), findsOneWidget); expect(find.text('Gamma legacy'), findsOneWidget); - await tester.tap( - find.descendant( - of: find.byKey(const Key('pipeline-scope')), - matching: find.text('Scheduled'), - ), - ); - await tester.pumpAndSettle(); + await pickScope(tester, 'Scheduled'); // Beta has no schedule in the fixture; Alpha has one. expect(find.text('Alpha review'), findsOneWidget); expect(find.text('Beta deploy'), findsNothing); - await tester.tap( - find.descendant( - of: find.byKey(const Key('pipeline-scope')), - matching: find.text('All'), - ), - ); - await tester.pumpAndSettle(); + await pickScope(tester, 'All'); expect(find.text('Beta deploy'), findsOneWidget); }); diff --git a/app/test/features/pipelines/pipelines_screen_test.dart b/app/test/features/pipelines/pipelines_screen_test.dart index f1086d3f..a8c451b9 100644 --- a/app/test/features/pipelines/pipelines_screen_test.dart +++ b/app/test/features/pipelines/pipelines_screen_test.dart @@ -115,13 +115,16 @@ void main() { expect(find.bySemanticsLabel('View runs of Alpha review'), findsOneWidget); // 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 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; expect(runs.right, lessThanOrEqualTo(run.left)); + expect(run.right, lessThanOrEqualTo(menu.left)); 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)); await tester.tap(find.byKey(const Key('pipeline-view-runs'))); expect(opened, [1]); diff --git a/app/test/features/pipelines/test_helpers.dart b/app/test/features/pipelines/test_helpers.dart index f3d566e2..0751bfd7 100644 --- a/app/test/features/pipelines/test_helpers.dart +++ b/app/test/features/pipelines/test_helpers.dart @@ -44,3 +44,20 @@ Future pumpWide(WidgetTester tester, FakeGateway gateway) async { Finder selectableText(String text) => find.byWidgetPredicate( (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 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(); +} diff --git a/app/tool/search_selection_probe_entry.dart b/app/tool/search_selection_probe_entry.dart index 7b7f438e..91aa59fd 100644 --- a/app/tool/search_selection_probe_entry.dart +++ b/app/tool/search_selection_probe_entry.dart @@ -225,7 +225,19 @@ List probeRuns() => [ ..._fillerRuns(), ]; +/// Pipelines this fixture session archived, with their archive time. +final _probeArchived = {}; + List probePipelines() => [ + for (final pipeline in _probePipelineRows()) + { + ...pipeline, + if (_probeArchived[pipeline['id']] != null) + 'archived_at': _probeArchived[pipeline['id']], + }, +]; + +List _probePipelineRows() => [ { 'id': 24, 'name': 'Release train', @@ -503,6 +515,29 @@ JsonMap? probeReply(JsonMap request) { 'type': 'pipeline', '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': // The app must send exactly the browser's SearchRequest body; anything // else is answered with a visible refusal so the capture cannot pass. diff --git a/docs/cli.md b/docs/cli.md index c94ac3ee..983334dd 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -329,6 +329,7 @@ Submitting activates included schedules, so review them before publishing. | Server/ntfy status popup | `status --json` | | Usage statistics/charts | `stats --json` | | 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` | 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 - ``` -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`, `cancel_run`, `retry_run`, `decide_run`, `set_run_title`, `get_run_evidence`, `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 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 Schedules are independent pipeline/version + run-information + timer records, diff --git a/internal/author/skill/references/operations.md b/internal/author/skill/references/operations.md index 8e66ffeb..d5807d6c 100644 --- a/internal/author/skill/references/operations.md +++ b/internal/author/skill/references/operations.md @@ -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 template changes no installed or published definition; only explicit authoring 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. 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 diff --git a/internal/cli/app.go b/internal/cli/app.go index 82f518a4..f85516bc 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -52,6 +52,7 @@ type options struct { all bool debugFiles bool json, source, noOpen, yes, allow, createOnly, force, wait bool + archived bool version, expected, limit, retryVisit int timeout, waitTimeout time.Duration 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)") case "template": 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": 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)") @@ -513,8 +519,10 @@ func (a App) Run(ctx context.Context, args []string) error { defer cancel() 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) { - r, err := call(protocol.Message{Type: protocol.List}) + r, err := call(protocol.Message{Type: protocol.List, IncludeArchived: true}) if err != nil { return protocol.PipelineSummary{}, err } @@ -760,6 +768,8 @@ func (a App) Run(ctx context.Context, args []string) error { } } return nil + case "archive", "unarchive": + return a.archive(command, pos, o, call, resolve) case "list", "templates", "runs", "ping", "connection": if command == "runs" && len(pos) > 0 && pos[0] == "rm" { 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) } 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 { return err } + if command == "list" && o.archived && !o.all { + r.Pipelines = archivedOnly(r.Pipelines) + } if command == "runs" && o.name != "" { p, err := resolve(o.name) if err != nil { @@ -791,7 +804,11 @@ func (a App) Run(ctx context.Context, args []string) error { switch command { case "list": 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": 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") { 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 { return err } for _, p := range r.Pipelines { + if p.Name == o.name && p.ArchivedAt != "" { + return &pipeline.ArchivedError{Name: p.Name} + } if p.Name == o.name { return fmt.Errorf("pipeline already exists; use edit or choose another name") } diff --git a/internal/cli/archive.go b/internal/cli/archive.go new file mode 100644 index 00000000..9a699109 --- /dev/null +++ b/internal/cli/archive.go @@ -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 +} diff --git a/internal/cli/commands.go b/internal/cli/commands.go index c661f3c3..ea154fb6 100644 --- a/internal/cli/commands.go +++ b/internal/cli/commands.go @@ -10,7 +10,9 @@ var usage = map[string]string{ "draft-delete": "draft-delete [--json]", "validate": "validate [--json]", "submit": "submit [--name NAME] [--create-only] [--yes] [--json]", - "list": "list [--json]", + "list": "list [--archived | --all] [--json]", + "archive": "archive [--yes] [--json]", + "unarchive": "unarchive [--json]", "search": "search QUERY [--type KIND] [--limit N] [--debug-files] [--json]", "show": "show [--version N] [--json]", "versions": "versions [--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.", "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 --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.", + "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 ` 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.", "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).", diff --git a/internal/cli/help.go b/internal/cli/help.go index f5c8c221..9e2f5b3f 100644 --- a/internal/cli/help.go +++ b/internal/cli/help.go @@ -25,7 +25,7 @@ EXECUTE rpc Send any protocol command (automation escape hatch) BROWSE - list List registered pipelines + list List active pipelines; --archived/--all show archived ones search QUERY Search pipelines, versions, runs, steps, field text, artifacts show Inspect graph, settings, source and history versions 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 MAINTAIN + archive Hide an unused pipeline; versions and runs are kept + unarchive Return an archived pipeline to lists and runs retention report Show what a run-history policy would remove (read-only) retention apply Remove it after approval; artifacts go with their run diff --git a/internal/cli/transfer.go b/internal/cli/transfer.go index ff26d759..fc8db8cf 100644 --- a/internal/cli/transfer.go +++ b/internal/cli/transfer.go @@ -139,7 +139,8 @@ func (a App) importBundle(ctx context.Context, o *options, pos []string, call ca entries = keep } // 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 { return err } diff --git a/internal/pipeline/archive.go b/internal/pipeline/archive.go new file mode 100644 index 00000000..a907341e --- /dev/null +++ b/internal/pipeline/archive.go @@ -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 +} diff --git a/internal/pipeline/archive_test.go b/internal/pipeline/archive_test.go new file mode 100644 index 00000000..6021ecf8 --- /dev/null +++ b/internal/pipeline/archive_test.go @@ -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() + } +} diff --git a/internal/pipeline/saved_schedules.go b/internal/pipeline/saved_schedules.go index 624acb9c..bc893740 100644 --- a/internal/pipeline/saved_schedules.go +++ b/internal/pipeline/saved_schedules.go @@ -267,6 +267,15 @@ func (s *Store) SaveSchedule(ctx context.Context, spec protocol.SavedSchedule, n return nil, err } 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 != "" { var id int64 var oldHash string diff --git a/internal/pipeline/store.go b/internal/pipeline/store.go index a9d043ff..9a78b245 100644 --- a/internal/pipeline/store.go +++ b/internal/pipeline/store.go @@ -61,6 +61,10 @@ CREATE TABLE IF NOT EXISTS submissions (id INTEGER PRIMARY KEY, name TEXT NOT NU return nil, err } 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 { db.Close() return nil, err @@ -173,8 +177,15 @@ func OpenMaintenance(path string) (*Store, error) { // adjacent server state such as stage artifacts. func (s *Store) Directory() string { return s.dir } +// List returns the active (unarchived) pipelines. 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'), strftime('%Y-%m-%dT%H:%M:%SZ', p.created_at), strftime('%Y-%m-%dT%H:%M:%SZ', v.created_at), 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) 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) -ORDER BY p.name, p.id`) +AND (? OR p.archived_at IS NULL) +ORDER BY p.name, p.id`, includeArchived) if err != nil { return nil, err } @@ -191,7 +203,7 @@ ORDER BY p.name, p.id`) for rows.Next() { var item protocol.PipelineSummary 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 } var shape protocol.Graph @@ -231,10 +243,10 @@ func (s *Store) Get(ctx context.Context, id int64, version int) (*protocol.Pipel defer tx.Rollback() detail := &protocol.PipelineDetail{} 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 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 { 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 versionID any 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 { _, err = tx.ExecContext(ctx, "INSERT INTO pipelines(name) VALUES (?) ON CONFLICT(name) DO NOTHING", name) if err != nil { diff --git a/internal/protocol/message.go b/internal/protocol/message.go index 8e0d7780..8a7d6f74 100644 --- a/internal/protocol/message.go +++ b/internal/protocol/message.go @@ -16,6 +16,13 @@ const ( Templates = "templates" Validate = "validate" 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 { @@ -103,6 +110,10 @@ type PipelineSummary struct { Stages int `json:"stages"` CreatedAt string `json:"created_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 // a miniature without one detail request per row. Detail responses keep the // full Graph field; this one is deliberately optional and additive. @@ -131,6 +142,7 @@ type Message struct { ExpectedVersion int `json:"expected_version,omitempty"` Templates []Template `json:"templates,omitempty"` CreateOnly bool `json:"create_only,omitempty"` + IncludeArchived bool `json:"include_archived,omitempty"` NotificationEndpoints []NotificationEndpoint `json:"notification_endpoints,omitempty"` Type string `json:"type"` ID string `json:"id,omitempty"` diff --git a/internal/server/archive.go b/internal/server/archive.go new file mode 100644 index 00000000..f2cdbe1c --- /dev/null +++ b/internal/server/archive.go @@ -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) +} diff --git a/internal/server/archive_test.go b/internal/server/archive_test.go new file mode 100644 index 00000000..82a775a8 --- /dev/null +++ b/internal/server/archive_test.go @@ -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) + } +} diff --git a/internal/server/runner.go b/internal/server/runner.go index d38c32b8..6b8df66c 100644 --- a/internal/server/runner.go +++ b/internal/server/runner.go @@ -212,6 +212,9 @@ func (r *runner) startWithSchedule(request protocol.Message, origin *pipeline.Sc if err != nil { return nil, err } + if d.ArchivedAt != "" { + return nil, &pipeline.ArchivedError{Name: d.Name} + } if _, err := pipeline.Parse(pipeline.ExecutionSource(d.Source)); err != nil { return nil, err } diff --git a/internal/server/websocket.go b/internal/server/websocket.go index 58e1ca22..abff045d 100644 --- a/internal/server/websocket.go +++ b/internal/server/websocket.go @@ -354,13 +354,24 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin } else if request.Type == protocol.List && len(stores) > 0 { response.Type = protocol.PipelineList response.DefaultModel, response.DefaultHarness = starterDefaults() - items, err := stores[0].List(ctx) + items, err := stores[0].ListPipelines(ctx, request.IncludeArchived) if err != nil { response.Type = protocol.Error response.Error = err.Error() } else { 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 { if len(stores) == 0 { response.Type = protocol.Error