Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions internal/hubpublish/compact_workflow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package hubpublish

import (
"encoding/json"
"reflect"
"strconv"

"gopkg.in/yaml.v3"
)

// compactWorkflowYAML keeps the full build origin and inspection on every
// activity after decoding, while storing repeated payloads only once.
func compactWorkflowYAML(data []byte) ([]byte, error) {
var document yaml.Node
if err := yaml.Unmarshal(data, &document); err != nil {
return nil, err
}
if len(document.Content) == 0 {
return data, nil
}
root := document.Content[0]
activities := yamlField(yamlField(root, "spec"), "activities")
if activities == nil {
return data, nil
}
shared := make(map[string]*yaml.Node)
anchors := 0
for _, activity := range activities.Content {
command := yamlField(activity, "command")
source := yamlField(yamlField(command, "executable"), "source")
if source == nil || source.Kind != yaml.MappingNode {
continue
}
for index := 0; index < len(source.Content); index += 2 {
key, value := source.Content[index], source.Content[index+1]
if key.Value != "origin" && key.Value != "inspection" {
continue
}
if value.Kind != yaml.MappingNode {
continue
}
encoded, err := yaml.Marshal(value)
if err != nil {
return nil, err
}
identity := key.Value + string(encoded)
first, exists := shared[identity]
if !exists {
shared[identity] = value
continue
}
if first.Anchor == "" {
anchors++
first.Anchor = "artifact" + strconv.Itoa(anchors)
}
source.Content[index+1] = &yaml.Node{Kind: yaml.AliasNode, Value: first.Anchor, Alias: first}
}
}
if anchors == 0 {
return data, nil
}
return yaml.Marshal(&document)
}

func yamlField(node *yaml.Node, name string) *yaml.Node {
if node == nil || node.Kind != yaml.MappingNode {
return nil
}
for index := 0; index < len(node.Content); index += 2 {
if node.Content[index].Value == name {
return node.Content[index+1]
}
}
return nil
}

// compactWorkflowSnapshot removes duplicate payloads from the JSON views only.
// The authored workflow is stored separately and can still reconstruct every
// activity. Artifact origins in these views retain their recipe and digests.
func compactWorkflowSnapshot(version map[string]any) bool {
activities, _ := version["activities"].([]any)
archives := make(map[string]bool)
inspections := make(map[string]bool)
changed := false
for _, value := range activities {
activity, _ := value.(map[string]any)
command, _ := activity["command"].(map[string]any)
executable, _ := command["executable"].(map[string]any)
source, _ := executable["source"].(map[string]any)
origin, _ := source["origin"].(map[string]any)
build, _ := origin["build"].(map[string]any)
archive, _ := build["contextArchive"].(string)
if source["type"] != "build" || archive == "" {
continue
}
if archives[archive] {
delete(build, "contextArchive")
changed = true
}
archives[archive] = true
if inspection := source["inspection"]; inspection != nil {
encoded, err := json.Marshal(inspection)
if err != nil {
continue
}
key := string(encoded)
if inspections[key] {
delete(source, "inspection")
changed = true
}
inspections[key] = true
}
}
return changed
}

// The projected activity rows contain the same attempt data when the arrays
// are identical. Preserve a distinct attempt history whenever it exists.
func compactIdenticalActivityAttempts(execution map[string]any) bool {
activities, ok := execution["activities"].([]any)
if !ok || len(activities) == 0 || !reflect.DeepEqual(activities, execution["activityAttempts"]) {
return false
}
delete(execution, "activityAttempts")
return true
}
90 changes: 90 additions & 0 deletions internal/hubpublish/compact_workflow_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package hubpublish

import (
"encoding/json"
"fmt"
"strings"
"testing"

"gopkg.in/yaml.v3"
)

func TestLargeSharedBuildExperimentRemainsPortable(t *testing.T) {
const activityCount = 514
archive := strings.Repeat("a", 6500)
inspection := strings.Repeat("i", 11000)
activities := make([]any, 0, activityCount)
for i := 0; i < activityCount; i++ {
name := fmt.Sprintf("batch-%03d", i)
source := map[string]any{
"type": "build",
"origin": map[string]any{"build": map[string]any{
"contextArchive": archive, "contextDigest": "sha256:context",
}},
"inspection": map[string]any{"schemaVersion": 1, "data": map[string]any{"marker": inspection}},
}
activities = append(activities, map[string]any{
"id": name, "name": name,
"command": map[string]any{"executable": map[string]any{"source": source}},
})
}
workflow := map[string]any{"id": "workflow", "name": "Workflow", "version": map[string]any{
"id": "version", "activities": activities,
}}
context := map[string]any{"workflow": workflow, "plan": map[string]any{"id": "plan", "workflowVersionId": "version"}}
execution := map[string]any{"run": map[string]any{"id": "run", "schedulePlanId": "plan", "workflowVersionId": "version"}}
capture, err := buildExperiment(context, execution, nil, nil)
if err != nil {
t.Fatal(err)
}
if _, err := marshalExperiment(capture.bundle); err != nil {
t.Fatal(err)
}
var portable struct {
Spec struct {
Activities []struct {
Command struct {
Executable struct {
Source map[string]any
}
}
}
}
}
if err := yaml.Unmarshal([]byte(capture.yaml), &portable); err != nil {
t.Fatal(err)
}
if len(portable.Spec.Activities) != activityCount {
t.Fatalf("expected %d portable activities, got %d", activityCount, len(portable.Spec.Activities))
}
for i, activity := range portable.Spec.Activities {
source := activity.Command.Executable.Source
encoded, err := json.Marshal(source)
if err != nil || !strings.Contains(string(encoded), archive) || !strings.Contains(string(encoded), inspection) {
t.Fatalf("portable activity %d lost its build context or inspection", i)
}
}
for _, file := range capture.bundle.Files {
if file.Path == "workflow.json" || file.Path == "plans.json" {
if strings.Count(file.Content, archive) != 1 || strings.Count(file.Content, inspection) != 1 ||
strings.Count(file.Content, "sha256:context") != activityCount {
t.Fatalf("%s lost provenance or still repeats its payloads", file.Path)
}
}
}
}

func TestLargeCaptureDropsOnlyIdenticalAttemptProjection(t *testing.T) {
activity := map[string]any{"activityId": "block", "attempt": 1, "status": "completed"}
execution := map[string]any{"activities": []any{activity}, "activityAttempts": []any{activity}}
if !compactIdenticalActivityAttempts(execution) || execution["activityAttempts"] != nil {
t.Fatal("duplicate attempt projection remained")
}
execution["activityAttempts"] = []any{
map[string]any{"activityId": "block", "attempt": 1, "status": "failed"},
activity,
}
if compactIdenticalActivityAttempts(execution) || execution["activityAttempts"] == nil {
t.Fatal("distinct attempt history was lost")
}
}
29 changes: 27 additions & 2 deletions internal/hubpublish/experiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ func buildExperiment(context, execution map[string]any, metrics []any, selection
return result, failure(422, "invalid_workflow", "The captured workflow is invalid.")
}
yaml, err := apirequests.PortableWorkflow(definition).YAML()
if err != nil || len(yaml) > 8<<20 {
if err == nil && len(yaml) > packageLimit {
yaml, err = compactWorkflowYAML(yaml)
}
if err != nil || len(yaml) > packageLimit {
return result, failure(422, "invalid_workflow", "The captured workflow YAML is invalid or too large.")
}
environment := executionEnvironment(context, execution)
Expand All @@ -125,7 +128,29 @@ func buildExperiment(context, execution map[string]any, metrics []any, selection
}
}
plans := []any{map[string]any{"plan": plan, "context": context}}
return encodeExperiment(workflow, plans, execution, string(yaml), redactions)
capture, err := encodeExperiment(workflow, plans, execution, string(yaml), redactions)
if err != nil {
return result, err
}
if _, err := marshalExperiment(capture.bundle); err == nil {
return capture, nil
}
// The portable YAML retains complete origins for every activity. Keep one
// copy of each repeated payload in the JSON snapshot for inspection.
if compactWorkflowSnapshot(version) {
yaml, err = compactWorkflowYAML(yaml)
if err != nil {
return result, err
}
capture, err = encodeExperiment(workflow, plans, execution, string(yaml), redactions)
if err != nil {
return result, err
}
}
if _, err := marshalExperiment(capture.bundle); err != nil && compactIdenticalActivityAttempts(execution) {
return encodeExperiment(workflow, plans, execution, string(yaml), redactions)
}
return capture, nil
}
func exportExecution(detail map[string]any, metrics any) map[string]any {
keys := []string{"run", "activities",
Expand Down
8 changes: 7 additions & 1 deletion internal/hubpublish/preview_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ func (m *Manager) PreviewWorkflow(ctx context.Context, workflowID, publicationID
if err != nil {
return result, err
}
if len(content) > 8<<20 {
if len(content) > packageLimit {
content, err = compactWorkflowYAML(content)
if err != nil {
return result, failure(422, "invalid_workflow", "The portable workflow could not be encoded.")
}
}
if len(content) > packageLimit {
return result, failure(422, "workflow_too_large", "The portable workflow exceeds 8 MiB.")
}
input := publicationVersionInput{YAML: string(content), Message: message}
Expand Down
Loading