Share typed publication completion evidence
This commit is contained in:
@@ -43,38 +43,12 @@ func committedPublishOutputOutcomes(m *manifest.Manifest) map[string]string {
|
||||
if m == nil || m.Stages["publish"] == nil {
|
||||
return out
|
||||
}
|
||||
raw := m.Stages["publish"].Metadata["publish_output_outcomes"]
|
||||
appendOutcome := func(item map[string]any) {
|
||||
source, sourceOK := item["source"].(string)
|
||||
status, statusOK := item["status"].(string)
|
||||
if sourceOK && statusOK && isPublishOutputOutcome(status) {
|
||||
out[source] = status
|
||||
}
|
||||
}
|
||||
switch items := raw.(type) {
|
||||
case []any:
|
||||
for _, rawItem := range items {
|
||||
if item, ok := rawItem.(map[string]any); ok {
|
||||
appendOutcome(item)
|
||||
}
|
||||
}
|
||||
case []map[string]any:
|
||||
for _, item := range items {
|
||||
appendOutcome(item)
|
||||
}
|
||||
for _, outcome := range artifacts.DecodePublicationOutcomes(m.Stages["publish"].Metadata) {
|
||||
out[outcome.Source] = string(outcome.Status)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func isPublishOutputOutcome(status string) bool {
|
||||
switch status {
|
||||
case "published", "carried_locked", "locked_unpublished", "optional_missing", "unselected":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func committedPublishedOutputs(commit *artifacts.RemoteCommitManifest) map[string]artifacts.RemoteArtifact {
|
||||
out := map[string]artifacts.RemoteArtifact{}
|
||||
if commit == nil {
|
||||
|
||||
@@ -208,11 +208,15 @@ func TestCommittedPublishOutputOutcomesRecognizesAllStates(t *testing.T) {
|
||||
for _, status := range statuses {
|
||||
items = append(items, map[string]any{"source": "source." + status, "status": status})
|
||||
}
|
||||
items = append(items, "malformed", map[string]any{"source": "invalid", "status": "unknown"})
|
||||
m := &manifest.Manifest{Stages: map[string]*manifest.StageRecord{
|
||||
"publish": {Metadata: map[string]any{"publish_output_outcomes": items}},
|
||||
}}
|
||||
|
||||
got := committedPublishOutputOutcomes(m)
|
||||
if len(got) != len(statuses) {
|
||||
t.Fatalf("unexpected outcomes = %#v", got)
|
||||
}
|
||||
for _, status := range statuses {
|
||||
if got["source."+status] != status {
|
||||
t.Fatalf("outcomes = %#v, missing %q", got, status)
|
||||
|
||||
125
internal/artifacts/publication_metadata.go
Normal file
125
internal/artifacts/publication_metadata.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package artifacts
|
||||
|
||||
// PublicationOutcomeStatus is the publication decision for one source.
|
||||
type PublicationOutcomeStatus string
|
||||
|
||||
const (
|
||||
PublicationPublished PublicationOutcomeStatus = "published"
|
||||
PublicationCarriedLocked PublicationOutcomeStatus = "carried_locked"
|
||||
PublicationLockedUnpublished PublicationOutcomeStatus = "locked_unpublished"
|
||||
PublicationOptionalMissing PublicationOutcomeStatus = "optional_missing"
|
||||
PublicationUnselected PublicationOutcomeStatus = "unselected"
|
||||
)
|
||||
|
||||
// PublicationOutcome describes a source's published or skipped outcome.
|
||||
type PublicationOutcome struct {
|
||||
Source string
|
||||
Status PublicationOutcomeStatus
|
||||
Destination string
|
||||
DeclaredKey string
|
||||
OriginRun string
|
||||
}
|
||||
|
||||
// PublicationBoolField preserves absence and invalid types independently.
|
||||
type PublicationBoolField struct {
|
||||
Value bool
|
||||
Present, Valid bool
|
||||
}
|
||||
|
||||
// PublicationStringField preserves absence and invalid types independently.
|
||||
type PublicationStringField struct {
|
||||
Value string
|
||||
Present, Valid bool
|
||||
}
|
||||
|
||||
// PublicationCompletion contains the fields used to authorize cleanup.
|
||||
type PublicationCompletion struct {
|
||||
Uploaded, Skipped PublicationBoolField
|
||||
RunID, CommitKey, PointerKey PublicationStringField
|
||||
}
|
||||
|
||||
const (
|
||||
PublicationUploadedKey = "uploaded"
|
||||
PublicationSkippedKey = "skipped"
|
||||
PublicationRunIDKey = "published_run_id"
|
||||
PublicationCommitKey = "remote_commit_key"
|
||||
PublicationPointerKey = "current_commit_pointer_key"
|
||||
PublicationOutcomesKey = "publish_output_outcomes"
|
||||
)
|
||||
|
||||
// DecodePublicationCompletion reads individual fields without deciding policy.
|
||||
func DecodePublicationCompletion(metadata map[string]any) PublicationCompletion {
|
||||
readBool := func(key string) PublicationBoolField {
|
||||
raw, present := metadata[key]
|
||||
value, valid := raw.(bool)
|
||||
return PublicationBoolField{Value: value, Present: present, Valid: valid}
|
||||
}
|
||||
readString := func(key string) PublicationStringField {
|
||||
raw, present := metadata[key]
|
||||
value, valid := raw.(string)
|
||||
return PublicationStringField{Value: value, Present: present, Valid: valid}
|
||||
}
|
||||
return PublicationCompletion{Uploaded: readBool(PublicationUploadedKey), Skipped: readBool(PublicationSkippedKey), RunID: readString(PublicationRunIDKey), CommitKey: readString(PublicationCommitKey), PointerKey: readString(PublicationPointerKey)}
|
||||
}
|
||||
|
||||
// PublicationIdentity identifies a successfully committed publication.
|
||||
type PublicationIdentity struct{ RunID, CommitKey, PointerKey string }
|
||||
|
||||
// PublicationCompletionMetadata encodes successful publication control evidence.
|
||||
func PublicationCompletionMetadata(identity PublicationIdentity) map[string]any {
|
||||
return map[string]any{PublicationUploadedKey: true, PublicationRunIDKey: identity.RunID, PublicationCommitKey: identity.CommitKey, PublicationPointerKey: identity.PointerKey}
|
||||
}
|
||||
|
||||
// PublicationOutcomeMetadata preserves the existing per-source map shape.
|
||||
func PublicationOutcomeMetadata(outcome PublicationOutcome) map[string]any {
|
||||
item := map[string]any{"source": outcome.Source, "status": string(outcome.Status)}
|
||||
if outcome.Destination != "" {
|
||||
item["destination"] = outcome.Destination
|
||||
}
|
||||
if outcome.DeclaredKey != "" {
|
||||
item["declared_key"] = outcome.DeclaredKey
|
||||
}
|
||||
if outcome.OriginRun != "" {
|
||||
item["origin_run"] = outcome.OriginRun
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
// DecodePublicationOutcomes skips malformed rows while retaining valid rows in order.
|
||||
func DecodePublicationOutcomes(metadata map[string]any) []PublicationOutcome {
|
||||
var result []PublicationOutcome
|
||||
appendRow := func(item map[string]any) {
|
||||
source, sourceOK := item["source"].(string)
|
||||
status, statusOK := item["status"].(string)
|
||||
if !sourceOK || !statusOK || !validPublicationOutcome(PublicationOutcomeStatus(status)) {
|
||||
return
|
||||
}
|
||||
outcome := PublicationOutcome{Source: source, Status: PublicationOutcomeStatus(status)}
|
||||
outcome.Destination, _ = item["destination"].(string)
|
||||
outcome.DeclaredKey, _ = item["declared_key"].(string)
|
||||
outcome.OriginRun, _ = item["origin_run"].(string)
|
||||
result = append(result, outcome)
|
||||
}
|
||||
switch rows := metadata[PublicationOutcomesKey].(type) {
|
||||
case []map[string]any:
|
||||
for _, row := range rows {
|
||||
appendRow(row)
|
||||
}
|
||||
case []any:
|
||||
for _, raw := range rows {
|
||||
if row, ok := raw.(map[string]any); ok {
|
||||
appendRow(row)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func validPublicationOutcome(status PublicationOutcomeStatus) bool {
|
||||
switch status {
|
||||
case PublicationPublished, PublicationCarriedLocked, PublicationLockedUnpublished, PublicationOptionalMissing, PublicationUnselected:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
39
internal/artifacts/publication_metadata_test.go
Normal file
39
internal/artifacts/publication_metadata_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package artifacts
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPublicationMetadataRoundTrip(t *testing.T) {
|
||||
metadata := PublicationCompletionMetadata(PublicationIdentity{RunID: "run", CommitKey: "commit", PointerKey: "pointer"})
|
||||
metadata[PublicationOutcomesKey] = []map[string]any{
|
||||
PublicationOutcomeMetadata(PublicationOutcome{Source: "one", Status: PublicationPublished, Destination: "a", DeclaredKey: "key", OriginRun: "run"}),
|
||||
{"source": "bad", "status": "invalid"},
|
||||
PublicationOutcomeMetadata(PublicationOutcome{Source: "two", Status: PublicationOptionalMissing}),
|
||||
}
|
||||
before := DecodePublicationCompletion(metadata)
|
||||
outcomes := DecodePublicationOutcomes(metadata)
|
||||
if !before.Uploaded.Valid || !before.Uploaded.Value || before.Skipped.Present || !before.RunID.Valid || len(outcomes) != 2 {
|
||||
t.Fatalf("completion = %+v, outcomes = %+v", before, outcomes)
|
||||
}
|
||||
body, err := json.Marshal(metadata)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var decoded map[string]any
|
||||
if err := json.Unmarshal(body, &decoded); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := DecodePublicationCompletion(decoded); !reflect.DeepEqual(got, before) {
|
||||
t.Fatalf("completion = %+v, want %+v", got, before)
|
||||
}
|
||||
if got := DecodePublicationOutcomes(decoded); !reflect.DeepEqual(got, outcomes) {
|
||||
t.Fatalf("outcomes = %+v, want %+v", got, outcomes)
|
||||
}
|
||||
decoded[PublicationSkippedKey] = "false"
|
||||
if field := DecodePublicationCompletion(decoded).Skipped; !field.Present || field.Valid {
|
||||
t.Fatalf("malformed skipped = %+v", field)
|
||||
}
|
||||
}
|
||||
@@ -93,32 +93,30 @@ func validateCleanupPublishEvidence(m *manifest.Manifest, campaign, sessionID st
|
||||
return "", fmt.Errorf("cleanup requires publish completion metadata")
|
||||
}
|
||||
|
||||
uploaded, ok := publishRecord.Metadata["uploaded"].(bool)
|
||||
if !ok || !uploaded {
|
||||
completion := artifacts.DecodePublicationCompletion(publishRecord.Metadata)
|
||||
if !completion.Uploaded.Valid || !completion.Uploaded.Value {
|
||||
return "", fmt.Errorf("cleanup requires publish metadata uploaded=true")
|
||||
}
|
||||
if skippedValue, exists := publishRecord.Metadata["skipped"]; exists {
|
||||
skipped, ok := skippedValue.(bool)
|
||||
if !ok {
|
||||
if completion.Skipped.Present {
|
||||
if !completion.Skipped.Valid {
|
||||
return "", fmt.Errorf("cleanup publish metadata skipped must be boolean")
|
||||
}
|
||||
if skipped {
|
||||
if completion.Skipped.Value {
|
||||
return "", fmt.Errorf("cleanup cannot use skipped publication evidence")
|
||||
}
|
||||
}
|
||||
|
||||
publishedRunID, err := canonicalCleanupMetadataString(publishRecord.Metadata, "published_run_id")
|
||||
publishedRunID, err := canonicalCleanupMetadataString(completion.RunID, artifacts.PublicationRunIDKey)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := artifacts.ValidateRunIdentity(publishedRunID); err != nil {
|
||||
return "", fmt.Errorf("cleanup publish metadata published_run_id: %w", err)
|
||||
}
|
||||
remoteCommitKey, err := canonicalCleanupMetadataString(publishRecord.Metadata, "remote_commit_key")
|
||||
remoteCommitKey, err := canonicalCleanupMetadataString(completion.CommitKey, artifacts.PublicationCommitKey)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
pointerKey, err := canonicalCleanupMetadataString(publishRecord.Metadata, "current_commit_pointer_key")
|
||||
pointerKey, err := canonicalCleanupMetadataString(completion.PointerKey, artifacts.PublicationPointerKey)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -138,9 +136,9 @@ func validateCleanupPublishEvidence(m *manifest.Manifest, campaign, sessionID st
|
||||
return publishedRunID, nil
|
||||
}
|
||||
|
||||
func canonicalCleanupMetadataString(metadata map[string]any, key string) (string, error) {
|
||||
value, ok := metadata[key].(string)
|
||||
if !ok || value == "" || strings.TrimSpace(value) != value {
|
||||
func canonicalCleanupMetadataString(field artifacts.PublicationStringField, key string) (string, error) {
|
||||
value := field.Value
|
||||
if !field.Valid || value == "" || strings.TrimSpace(value) != value {
|
||||
return "", fmt.Errorf("cleanup publish metadata %s must be a canonical nonempty string", key)
|
||||
}
|
||||
return value, nil
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package stage
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
@@ -33,6 +34,28 @@ func TestBuildCleanupPlanSelectsCanonicalSessionRunsTarget(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanupPublishEvidenceSurvivesJSONRoundTrip(t *testing.T) {
|
||||
env, m, runID := cleanupPlanningFixture(t)
|
||||
env.Config.Pipeline.Workspace.CleanupAfterPublish = true
|
||||
for _, roundTrip := range []bool{false, true} {
|
||||
if roundTrip {
|
||||
data, err := json.Marshal(m.Stages["publish"].Metadata)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var decoded map[string]any
|
||||
if err := json.Unmarshal(data, &decoded); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m.Stages["publish"].Metadata = decoded
|
||||
}
|
||||
plan, err := buildCleanupPlan(env, m)
|
||||
if err != nil || !plan.enabled || plan.publishedRunID != runID {
|
||||
t.Fatalf("plan = %+v, error = %v", plan, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildCleanupPlanRejectsInvalidPublishEvidenceWithoutExecutablePlan(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
@@ -311,21 +311,21 @@ func publishMetadata(
|
||||
commitKey string,
|
||||
pointerKey string,
|
||||
) map[string]any {
|
||||
return map[string]any{
|
||||
"stage": "publish",
|
||||
"uploaded": true,
|
||||
"s3_bucket": bucket,
|
||||
"s3_run_prefix": runPrefix,
|
||||
"published_run_id": runID,
|
||||
"run_files_uploaded": len(runUploaded),
|
||||
"run_uploaded_paths": append([]string(nil), runUploaded...),
|
||||
"published_files_uploaded": len(publishedUploaded),
|
||||
"published_paths": append([]string(nil), publishedUploaded...),
|
||||
"previous_files_uploaded": len(previousUploaded),
|
||||
"previous_uploaded_paths": append([]string(nil), previousUploaded...),
|
||||
"publish_output_outcomes": publishOutputOutcomeMetadata(outputPlan),
|
||||
"remote_commit_key": commitKey,
|
||||
"current_commit_pointer_key": pointerKey,
|
||||
"audio_upload_skipped": true,
|
||||
metadata := map[string]any{
|
||||
"stage": "publish",
|
||||
"s3_bucket": bucket,
|
||||
"s3_run_prefix": runPrefix,
|
||||
"run_files_uploaded": len(runUploaded),
|
||||
"run_uploaded_paths": append([]string(nil), runUploaded...),
|
||||
"published_files_uploaded": len(publishedUploaded),
|
||||
"published_paths": append([]string(nil), publishedUploaded...),
|
||||
"previous_files_uploaded": len(previousUploaded),
|
||||
"previous_uploaded_paths": append([]string(nil), previousUploaded...),
|
||||
artifacts.PublicationOutcomesKey: publishOutputOutcomeMetadata(outputPlan),
|
||||
"audio_upload_skipped": true,
|
||||
}
|
||||
for key, value := range artifacts.PublicationCompletionMetadata(artifacts.PublicationIdentity{RunID: runID, CommitKey: commitKey, PointerKey: pointerKey}) {
|
||||
metadata[key] = value
|
||||
}
|
||||
return metadata
|
||||
}
|
||||
|
||||
@@ -10,14 +10,14 @@ import (
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/pathsafe"
|
||||
)
|
||||
|
||||
type publishOutputStatus string
|
||||
type publishOutputStatus = artifacts.PublicationOutcomeStatus
|
||||
|
||||
const (
|
||||
publishOutputPublished publishOutputStatus = "published"
|
||||
publishOutputCarriedLocked publishOutputStatus = "carried_locked"
|
||||
publishOutputLockedUnpublished publishOutputStatus = "locked_unpublished"
|
||||
publishOutputOptionalMissing publishOutputStatus = "optional_missing"
|
||||
publishOutputUnselected publishOutputStatus = "unselected"
|
||||
publishOutputPublished = artifacts.PublicationPublished
|
||||
publishOutputCarriedLocked = artifacts.PublicationCarriedLocked
|
||||
publishOutputLockedUnpublished = artifacts.PublicationLockedUnpublished
|
||||
publishOutputOptionalMissing = artifacts.PublicationOptionalMissing
|
||||
publishOutputUnselected = artifacts.PublicationUnselected
|
||||
)
|
||||
|
||||
// PublicationAction describes a selected output mapping before remote publication.
|
||||
@@ -251,19 +251,10 @@ func publishOutputOutcomeMetadata(plan *publishOutputPlan) []map[string]any {
|
||||
}
|
||||
out := make([]map[string]any, 0, len(plan.Outcomes))
|
||||
for _, outcome := range plan.Outcomes {
|
||||
item := map[string]any{
|
||||
"source": outcome.Source,
|
||||
"status": string(outcome.Status),
|
||||
}
|
||||
if outcome.Destination != "" {
|
||||
item["destination"] = outcome.Destination
|
||||
}
|
||||
if outcome.DeclaredKey != "" {
|
||||
item["declared_key"] = outcome.DeclaredKey
|
||||
}
|
||||
if outcome.OriginRun != "" {
|
||||
item["origin_run"] = outcome.OriginRun
|
||||
}
|
||||
item := artifacts.PublicationOutcomeMetadata(artifacts.PublicationOutcome{
|
||||
Source: outcome.Source, Status: outcome.Status, Destination: outcome.Destination,
|
||||
DeclaredKey: outcome.DeclaredKey, OriginRun: outcome.OriginRun,
|
||||
})
|
||||
out = append(out, item)
|
||||
}
|
||||
return out
|
||||
|
||||
Reference in New Issue
Block a user