From 74309cc72ca610ebd0c1df0ba52cd218eaa6ba7e Mon Sep 17 00:00:00 2001 From: Wesley Ferreira Date: Sat, 28 Nov 2026 03:48:03 +0000 Subject: [PATCH] AKO-518 Reuse portable build observations within preflight --- .../execution_preflight_test.go | 38 +++++++++++++++++++ .../preflight_images.go | 27 ++++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/internal/api/handlers/workflow_engine_api_handler/execution_preflight_test.go b/internal/api/handlers/workflow_engine_api_handler/execution_preflight_test.go index 8d881fae..a897c944 100644 --- a/internal/api/handlers/workflow_engine_api_handler/execution_preflight_test.go +++ b/internal/api/handlers/workflow_engine_api_handler/execution_preflight_test.go @@ -3,6 +3,7 @@ package workflow_engine_api_handler import ( "context" "errors" + "strconv" "strings" "testing" "time" @@ -131,6 +132,43 @@ func TestPreflightReusesCatalogArtifactWithoutRegistryAccess(t *testing.T) { t.Fatalf("cached artifact incorrectly required registry: %s %d", status, checker.calls) } } + +func TestPreflightChecksEachPortableBuildOncePerReport(t *testing.T) { + checker := &imageProbeStub{} + h := &Handler{build: checker} + request := preflightRequest() + request.Workflow.Activities = nil + for index := range 514 { + archive := []byte("shared archive") + if index == 513 { + archive = []byte("different archive with the same declared digest") + } + request.Workflow.Activities = append(request.Workflow.Activities, domain.Activity{ + ID: strconv.Itoa(index), + Command: domain.ActivityCommand{Executable: &domain.ExecutableReference{ + Source: domain.ExecutableSource{Origin: &domain.ArtifactOrigin{ + Build: &domain.PortableArtifactBuild{ContextArchive: archive, ContextDigest: "same"}, + }}, + }}, + }) + } + for report := 1; report <= 2; report++ { + probes := h.preflightImages(&request) + if len(probes) != 514 { + t.Fatalf("got %d activity checks, want 514", len(probes)) + } + for index, probe := range probes { + status, message, _ := probe.Run(context.Background()) + if probe.EntityID != strconv.Itoa(index) || status != "blocked" || message != "registry offline" { + t.Fatalf("unexpected shared observation for activity %d: %s %s", index, status, message) + } + } + if want := 2 * report; checker.calls != want { + t.Fatalf("got %d executable lookups, want %d", checker.calls, want) + } + } +} + func TestConnectionProbeUsesRefreshedResourceConfiguration(t *testing.T) { request := preflightRequest() request.Plan.Assignments = []domain.PlanAssignment{{ActivityID: "a", ResourceID: "resource"}} diff --git a/internal/api/handlers/workflow_engine_api_handler/preflight_images.go b/internal/api/handlers/workflow_engine_api_handler/preflight_images.go index c62b4b89..55a8c610 100644 --- a/internal/api/handlers/workflow_engine_api_handler/preflight_images.go +++ b/internal/api/handlers/workflow_engine_api_handler/preflight_images.go @@ -2,6 +2,8 @@ package workflow_engine_api_handler import ( "context" + "crypto/sha256" + "encoding/json" "github.com/UFFeScience/akoflow/internal/application/ports" apppreflight "github.com/UFFeScience/akoflow/internal/application/preflight" @@ -11,6 +13,11 @@ import ( func (h *Handler) preflightImages(request *ports.ExecutionRequest) []apppreflight.Probe { probes := []apppreflight.Probe{} + type imageObservation struct { + available bool + err error + } + observations := map[[sha256.Size]byte]imageObservation{} for index := range request.Workflow.Activities { activity := request.Workflow.Activities[index] probes = append(probes, apppreflight.Probe{Code: "executable_availability", Stage: "artifacts", EntityID: activity.ID, Run: func(ctx context.Context) (string, string, string) { @@ -38,7 +45,25 @@ func (h *Handler) preflightImages(request *ports.ExecutionRequest) []appprefligh if !ok { return "unverified", "Read-only executable availability checks are unavailable.", "Review artifact availability before execution." } - observed, err := checker.CheckExecutable(ctx, source) + var result imageObservation + if source.Origin != nil && source.Origin.Build != nil { + // The complete origin includes the archived bytes as well as the + // declared digests. Distinct or malformed archives must be checked independently. + encoded, err := json.Marshal(source.Origin) + if err != nil { + return "blocked", err.Error(), operationerror.Describe(err).NextAction + } + key := sha256.Sum256(encoded) + var checked bool + result, checked = observations[key] + if !checked { + result.available, result.err = checker.CheckExecutable(ctx, source) + observations[key] = result + } + } else { + result.available, result.err = checker.CheckExecutable(ctx, source) + } + observed, err := result.available, result.err if err != nil { return "blocked", err.Error(), operationerror.Describe(err).NextAction }