diff --git a/internal/hubpublish/http.go b/internal/hubpublish/http.go index 00283644..4966b1c7 100644 --- a/internal/hubpublish/http.go +++ b/internal/hubpublish/http.go @@ -23,6 +23,7 @@ func (m *Manager) Handler() http.Handler { mux.HandleFunc("GET /akoflow-api/hub/publications/{publicationId}/", m.serveGetPublication) mux.HandleFunc("PATCH /akoflow-api/hub/publications/{publicationId}/", m.serveUpdatePublication) mux.HandleFunc("DELETE /akoflow-api/hub/publications/{publicationId}/", m.serveDeletePublication) + mux.HandleFunc("DELETE /akoflow-api/hub/publications/{publicationId}/versions/{version}/", m.serveRetireVersion) mux.HandleFunc("GET /akoflow-api/hub/publications/{publicationId}/versions/", m.serveListVersions) mux.HandleFunc("GET /akoflow-api/hub/publications/{publicationId}/results/", m.serveListResults) mux.HandleFunc("GET /akoflow-api/hub/publications/{publicationId}/compare/", m.serveCompareVersions) diff --git a/internal/hubpublish/http_publication_management.go b/internal/hubpublish/http_publication_management.go index 2da7f2d1..ea64f197 100644 --- a/internal/hubpublish/http_publication_management.go +++ b/internal/hubpublish/http_publication_management.go @@ -3,6 +3,7 @@ package hubpublish import ( "net/http" "net/url" + "strconv" ) func (m *Manager) serveGetPublication(w http.ResponseWriter, r *http.Request) { @@ -19,9 +20,29 @@ func (m *Manager) serveUpdatePublication(w http.ResponseWriter, r *http.Request) respond(w, value, err) } func (m *Manager) serveDeletePublication(w http.ResponseWriter, r *http.Request) { - err := m.DeletePublication(r.Context(), r.PathValue("publicationId")) + id := r.PathValue("publicationId") + if r.URL.EscapedPath() != "/akoflow-api/hub/publications/"+url.PathEscape(id)+"/" { + http.NotFound(w, r) + return + } + err := m.DeletePublication(r.Context(), id) respond(w, map[string]bool{"deleted": err == nil}, err) } +func (m *Manager) serveRetireVersion(w http.ResponseWriter, r *http.Request) { + id, rawVersion := r.PathValue("publicationId"), r.PathValue("version") + if r.URL.EscapedPath() != "/akoflow-api/hub/publications/"+url.PathEscape(id)+"/versions/"+url.PathEscape(rawVersion)+"/" { + http.NotFound(w, r) + return + } + version, versionErr := strconv.Atoi(rawVersion) + replacement, replacementErr := strconv.Atoi(r.URL.Query().Get("replacement")) + if versionErr != nil || replacementErr != nil { + respond(w, nil, failure(422, "invalid_version_retirement", "Choose an older version and its verified replacement.")) + return + } + err := m.RetireVersion(r.Context(), id, version, replacement) + respond(w, map[string]bool{"retired": err == nil}, err) +} func (m *Manager) serveListVersions(w http.ResponseWriter, r *http.Request) { value, err := m.ReadPublication(r.Context(), r.PathValue("publicationId"), "versions", nil) respond(w, value, err) diff --git a/internal/hubpublish/publication_management.go b/internal/hubpublish/publication_management.go index e8ce59b7..9fdc0030 100644 --- a/internal/hubpublish/publication_management.go +++ b/internal/hubpublish/publication_management.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "net/url" + "strconv" ) type PublicationPatch struct { @@ -101,6 +102,37 @@ func (m *Manager) DeletePublication(ctx context.Context, id string) error { return nil } +func (m *Manager) RetireVersion(ctx context.Context, id string, version, replacement int) error { + if m.options.ReadOnly { + return failure(403, "read_only", "Read-only instances cannot retire Hub workflow versions.") + } + if version < 1 || replacement <= version { + return failure(422, "invalid_version_retirement", "Choose an older version and its verified replacement.") + } + m.mu.Lock() + defer m.mu.Unlock() + if _, err := m.identityLocked(ctx); err != nil { + return err + } + publication, err := m.publicationLocked(ctx, id) + if err != nil { + return err + } + latest := 0 + for _, candidate := range publication.Versions { + latest = max(latest, candidate.Number) + } + if latest != replacement { + return failure(409, "replacement_version_changed", "The verified replacement is no longer the latest version.") + } + path := "/desktop/publications/" + url.PathEscape(id) + "/versions/" + strconv.Itoa(version) + if err := m.requestLocked(ctx, "DELETE", path, nil, nil, true, nil); err != nil { + return err + } + m.invalidatePublicationPreviewsLocked(id) + return nil +} + func (m *Manager) invalidatePublicationPreviewsLocked(id string) { for key, item := range m.previews { if item.Publication.ID == id { diff --git a/internal/hubpublish/publication_management_test.go b/internal/hubpublish/publication_management_test.go index 6c0fa12e..1b6066aa 100644 --- a/internal/hubpublish/publication_management_test.go +++ b/internal/hubpublish/publication_management_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "io" "net/http" + "net/http/httptest" "strings" "testing" ) @@ -139,3 +140,49 @@ func TestHubImportPreservesImageMetadataAndDoesNotOverwriteConflicts(t *testing. t.Fatal("conflict silently overwritten") } } + +func TestRetireVersionUsesOnlyNumberedHubRoute(t *testing.T) { + deleted := 0 + manager := testManager(t, func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == "GET" && r.URL.Path == "/api/v1/desktop/me": + _ = json.NewEncoder(w).Encode(Identity{Username: "researcher", ProjectID: "project"}) + case r.Method == "GET" && r.URL.Path == "/api/v1/desktop/publications/publication": + _ = json.NewEncoder(w).Encode(publicationInfo{Publication: Publication{ID: "publication"}, + Versions: []publicationVersion{{Number: 1}, {Number: 2}}}) + case r.Method == "DELETE" && r.URL.Path == "/api/v1/desktop/publications/publication/versions/1": + deleted++ + w.WriteHeader(http.StatusNoContent) + default: + t.Errorf("unexpected Hub request %s %s", r.Method, r.URL.Path) + } + }) + manager.state.AccessToken = "secret" + manager.state.DefaultPublicationID = "publication" + manager.previews["pending"] = preparedPublication{Publication: Publication{ID: "publication"}} + handler := manager.Handler() + for _, test := range []struct { + path string + want int + }{ + {"versions/", 404}, + {"versions/1/extra/?replacement=2", 404}, + {"versions/1/", 422}, + {"versions/1/?replacement=3", 409}, + {"versions/1/?replacement=2", 200}, + } { + recorder := httptest.NewRecorder() + handler.ServeHTTP(recorder, httptest.NewRequest("DELETE", + "/akoflow-api/hub/publications/publication/"+test.path, nil)) + if recorder.Code != test.want { + t.Errorf("%s status %d, want %d: %s", test.path, recorder.Code, test.want, recorder.Body) + } + } + if deleted != 1 || len(manager.previews) != 0 || manager.state.DefaultPublicationID != "publication" { + t.Fatal("version retirement did not preserve the publication or clear stale previews") + } + manager.options.ReadOnly = true + if err := manager.RetireVersion(context.Background(), "publication", 1, 2); err == nil || deleted != 1 { + t.Fatal("retirement succeeded in read-only mode") + } +} diff --git a/internal/mcpbridge/catalog.json b/internal/mcpbridge/catalog.json index 4aa45849..bb2c58f9 100644 --- a/internal/mcpbridge/catalog.json +++ b/internal/mcpbridge/catalog.json @@ -8962,6 +8962,16 @@ "type": "object" } }, + { + "name": "hub_retire_workflow_version", + "method": "DELETE", + "path": "/akoflow-api/hub/publications/{publicationId}/versions/{version}/", + "handler": "serveRetireVersion", + "description": "hub retire workflow version. DELETE /akoflow-api/hub/publications/{publicationId}/versions/{version}/. Uses the daemon API and its domain validation.", + "query": [ + "replacement" + ] + }, { "name": "hub_review_activity_estimates", "method": "POST", diff --git a/internal/mcpbridge/cataloggen/hub.go b/internal/mcpbridge/cataloggen/hub.go index 680d0238..81325763 100644 --- a/internal/mcpbridge/cataloggen/hub.go +++ b/internal/mcpbridge/cataloggen/hub.go @@ -14,6 +14,7 @@ func hubToolName(handler string) string { "serveCreate": "hub_create_publication", "serveGetPublication": "hub_get_publication", "serveUpdatePublication": "hub_update_publication", + "serveRetireVersion": "hub_retire_workflow_version", "serveDeletePublication": "hub_delete_publication", "serveContributionDestination": "hub_get_contribution_destination", "serveListVersions": "hub_list_versions", diff --git a/internal/mcpbridge/hub_test.go b/internal/mcpbridge/hub_test.go index 4b3489a2..93aeff45 100644 --- a/internal/mcpbridge/hub_test.go +++ b/internal/mcpbridge/hub_test.go @@ -13,7 +13,7 @@ func TestHubCatalogContractsAndPermissions(t *testing.T) { "hub_get_workflow_definition": Read, "inspect_artifact_image": Read, "hub_update_publication": Write, "hub_import_workflow": Write, "hub_preview_workflow": Write, "hub_preview_execution": Write, "hub_confirm_publication": Write, - "hub_delete_publication": Admin, "hub_disconnect": Admin, "hub_update_settings": Admin, + "hub_delete_publication": Admin, "hub_retire_workflow_version": Admin, "hub_disconnect": Admin, "hub_update_settings": Admin, "get_execution_status": Read, "get_execution_logs": Read, "get_execution_events": Read, "get_execution_artifacts": Read, "get_execution_activity": Read, }