From 23a185e4ff472161e9629ea855c78a6bcf81e746 Mon Sep 17 00:00:00 2001 From: intel352 <77607+intel352@users.noreply.github.com> Date: Sat, 5 Dec 2026 09:11:31 -0500 Subject: [PATCH] fix: isolate chain input mappings across runs --- internal/steps_chain.go | 14 +- .../steps_chain_mapping_isolation_test.go | 250 ++++++++++++++++++ 2 files changed, 261 insertions(+), 3 deletions(-) create mode 100644 internal/steps_chain_mapping_isolation_test.go diff --git a/internal/steps_chain.go b/internal/steps_chain.go index 08d82a0..a1c6356 100644 --- a/internal/steps_chain.go +++ b/internal/steps_chain.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "slices" "time" "github.com/GoCodeAlone/workflow-compute/pkg/protocol" @@ -248,6 +249,12 @@ func applyComputeChainInputMappings(workload *protocol.WorkloadSpec, mappings [] if workload.Kind != protocol.WorkloadProvider || workload.Provider == nil { return errors.New("input_mappings require provider workload") } + // Execute reuses the configured provider. Keep appends private, including + // spare capacity, and publish only after every mapping succeeds. + provider := *workload.Provider + provider.ArtifactRefs = slices.Clone(provider.ArtifactRefs) + provider.ContentInputs = slices.Clone(provider.ContentInputs) + provider.StreamInputs = slices.Clone(provider.StreamInputs) for _, mapping := range mappings { source, ok := outputs[mapping.FromStep] if !ok { @@ -263,23 +270,24 @@ func applyComputeChainInputMappings(workload *protocol.WorkloadSpec, mappings [] if err != nil { return fmt.Errorf("%s: %w", mapping.To, err) } - workload.Provider.ArtifactRefs = append(workload.Provider.ArtifactRefs, refs...) + provider.ArtifactRefs = append(provider.ArtifactRefs, refs...) case "provider.content_inputs": inputs, err := typedSliceFromAny[coreprotocol.ContentInputRef](value) if err != nil { return fmt.Errorf("%s: %w", mapping.To, err) } - workload.Provider.ContentInputs = append(workload.Provider.ContentInputs, inputs...) + provider.ContentInputs = append(provider.ContentInputs, inputs...) case "provider.stream_inputs": inputs, err := typedSliceFromAny[coreprotocol.StreamInputRef](value) if err != nil { return fmt.Errorf("%s: %w", mapping.To, err) } - workload.Provider.StreamInputs = append(workload.Provider.StreamInputs, inputs...) + provider.StreamInputs = append(provider.StreamInputs, inputs...) default: return fmt.Errorf("unsupported mapping destination %q", mapping.To) } } + workload.Provider = &provider return nil } diff --git a/internal/steps_chain_mapping_isolation_test.go b/internal/steps_chain_mapping_isolation_test.go new file mode 100644 index 0000000..beada86 --- /dev/null +++ b/internal/steps_chain_mapping_isolation_test.go @@ -0,0 +1,250 @@ +package internal + +import ( + "bytes" + "encoding/json" + "reflect" + "slices" + "testing" + + coreprotocol "github.com/GoCodeAlone/workflow-plugin-compute-core/protocol" +) + +// Uses the production config constructor and mapping helper, without HTTP, +// listeners, execution, proof acceptance, or artifact-storage substitutes. +func TestComputeChainFanInMappingIsolation(t *testing.T) { + newChain := func(t *testing.T) *computeChainStep { + t.Helper() + raw := chainConfigMap("https://synthetic.invalid") + node := func(id string, dependencies ...string) map[string]any { + item := map[string]any{ + "id": id, "task_id": id + "-task", + "org_id": "org-1", "pool_id": "pool-1", "policy_id": "policy-1", + "timeout_seconds": 60, "workload": providerWorkloadMap("transform"), + } + if len(dependencies) != 0 { + deps, mappings := make([]any, 0, len(dependencies)), make([]any, 0, len(dependencies)) + for _, dependency := range dependencies { + deps = append(deps, dependency) + mappings = append(mappings, map[string]any{ + "from_step": dependency, "from": "result_preview.artifact_refs", + "to": "provider.artifact_refs", + }) + } + item["depends_on"], item["input_mappings"] = deps, mappings + } + return item + } + raw["steps"] = []any{ + node("source"), node("left", "source"), node("right", "source"), + node("join", "left", "right"), + } + serialized, err := json.Marshal(raw) + if err != nil { + t.Fatal(err) + } + var decoded map[string]any + if err := json.Unmarshal(serialized, &decoded); err != nil { + t.Fatal(err) + } + step, err := newComputeChainStep("fork-join", decoded) + if err != nil { + t.Fatal(err) + } + return step + } + encode := func(t *testing.T, value any) []byte { + t.Helper() + raw, err := json.Marshal(value) + if err != nil { + t.Fatal(err) + } + return raw + } + const left = "artifact://pool-1/tasks/left-task/proofs/left-proof/left/result.txt" + const right = "artifact://pool-1/tasks/right-task/proofs/right-proof/right/result.txt" + outputs := map[string]map[string]any{ + "left": {"result_preview": map[string]any{"artifact_refs": []any{left}}}, + "right": {"result_preview": map[string]any{"artifact_refs": []any{right}}}, + } + + t.Run("repeated_mapping_keeps_configuration", func(t *testing.T) { + step := newChain(t) + before := encode(t, step.config) + join := step.config.Steps[3] + for run := 1; run <= 2; run++ { + // Match Execute's real value-copy boundary. + workload := join.Workload + if err := applyComputeChainInputMappings(&workload, join.InputMappings, outputs); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(workload.Provider.ArtifactRefs, []string{left, right}) { + t.Errorf("run %d: inputs accumulated: %v", run, workload.Provider.ArtifactRefs) + } + if !bytes.Equal(before, encode(t, step.config)) { + t.Errorf("run %d: mapping changed reusable configuration", run) + } + } + }) + t.Run("later_mapping_failure_leaves_input_unchanged", func(t *testing.T) { + step := newChain(t) + before := encode(t, step.config) + join := step.config.Steps[3] + mappings := append([]computeChainInputMapping(nil), join.InputMappings...) + mappings[1].From = "result_preview.missing" + workload := join.Workload + original := encode(t, workload) + if err := applyComputeChainInputMappings(&workload, mappings, outputs); err == nil { + t.Fatal("missing second mapping source was accepted") + } + if !bytes.Equal(original, encode(t, workload)) { + t.Error("failed mapping partially changed its input workload") + } + if !bytes.Equal(before, encode(t, step.config)) { + t.Error("failed mapping changed reusable configuration") + } + }) +} + +// All values are inert. This exercises production construction and mapping, +// not transport, scheduler placement, proof acceptance or artifact storage. +func TestComputeChainMappingCopyOnWrite(t *testing.T) { + ref := "artifact://pool-1/tasks/source-task/proofs/source-proof/result.txt" + content := coreprotocol.ContentInputRef{Name: "source", Ref: "content://workloads/source.txt", TargetPath: "inputs/source.txt"} + stream := coreprotocol.StreamInputRef{Name: "source", Handle: coreprotocol.StreamHandle{ + URL: "rtmp://synthetic.invalid/source", Protocol: "rtmp", AuthTokenRef: "secret://synthetic/source", + Codecs: []string{"h264"}, ExpiresAt: "2099-01-01T00:00:00Z", + }} + for _, shape := range []string{"nil", "empty", "configured_spare_capacity"} { + for _, destination := range []string{"artifact_refs", "content_inputs", "stream_inputs"} { + for _, outcome := range []string{"success", "empty_value", "missing_step", "missing_path", "bad_value", "bad_destination"} { + t.Run(shape+"/"+destination+"/"+outcome, func(t *testing.T) { + step, err := newComputeChainStep("mapping", chainConfigMap("https://synthetic.invalid")) + if err != nil { + t.Fatal(err) + } + configured := step.config.Steps[1].Workload.Provider + switch shape { + case "nil": + configured.ArtifactRefs, configured.ContentInputs, configured.StreamInputs = nil, nil, nil + case "empty": + configured.ArtifactRefs = []string{} + configured.ContentInputs = []coreprotocol.ContentInputRef{} + configured.StreamInputs = []coreprotocol.StreamInputRef{} + case "configured_spare_capacity": + configured.ArtifactRefs = []string{"artifact://pool-1/tasks/config/proofs/proof/config.txt", "spare-1", "spare-2"}[:1] + configured.ContentInputs = []coreprotocol.ContentInputRef{{Name: "configured", Ref: "content://workloads/config.txt", TargetPath: "inputs/config.txt"}, {Name: "spare-1"}, {Name: "spare-2"}}[:1] + configured.StreamInputs = []coreprotocol.StreamInputRef{{Name: "configured", Handle: stream.Handle}, {Name: "spare-1"}, {Name: "spare-2"}}[:1] + } + original := *configured + before, err := json.Marshal(step.config) + if err != nil { + t.Fatal(err) + } + refs := slices.Clone(configured.ArtifactRefs[:cap(configured.ArtifactRefs)]) + contents := slices.Clone(configured.ContentInputs[:cap(configured.ContentInputs)]) + streams := slices.Clone(configured.StreamInputs[:cap(configured.StreamInputs)]) + unchanged := func() { + t.Helper() + after, err := json.Marshal(step.config) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(before, after) || + !reflect.DeepEqual(refs, configured.ArtifactRefs[:cap(configured.ArtifactRefs)]) || + !reflect.DeepEqual(contents, configured.ContentInputs[:cap(configured.ContentInputs)]) || + !reflect.DeepEqual(streams, configured.StreamInputs[:cap(configured.StreamInputs)]) { + t.Error("mapping changed configured values or their spare backing capacity") + } + } + values := map[string]any{"artifact_refs": []string{ref}, "content_inputs": []coreprotocol.ContentInputRef{content}, "stream_inputs": []coreprotocol.StreamInputRef{stream}} + if outcome == "empty_value" { + values[destination] = []any{} + } + outputs := map[string]map[string]any{"source": {"preview": values}} + first := computeChainInputMapping{FromStep: "source", From: "preview." + destination, To: "provider." + destination} + mappings := []computeChainInputMapping{first} + fails := outcome != "success" && outcome != "empty_value" + if fails { + later := first + switch outcome { + case "missing_step": + later.FromStep = "missing" + case "missing_path": + later.From = "preview.missing" + case "bad_value": + values["bad"] = 17 + later.From = "preview.bad" + case "bad_destination": + later.To = "unsupported" + } + mappings = append(mappings, later) + } + for run := 0; run < 2; run++ { + workload := step.config.Steps[1].Workload + err := applyComputeChainInputMappings(&workload, mappings, outputs) + if fails { + if err == nil { + t.Fatal("later invalid mapping succeeded") + } + if workload.Provider != configured { + t.Error("failed mapping published a provider") + } + } else { + if err != nil { + t.Fatal(err) + } + want := original + want.ArtifactRefs = slices.Clone(refs[:len(original.ArtifactRefs)]) + want.ContentInputs = slices.Clone(contents[:len(original.ContentInputs)]) + want.StreamInputs = slices.Clone(streams[:len(original.StreamInputs)]) + if outcome == "success" { + switch destination { + case "artifact_refs": + want.ArtifactRefs = append(want.ArtifactRefs, ref) + case "content_inputs": + want.ContentInputs = append(want.ContentInputs, content) + case "stream_inputs": + want.StreamInputs = append(want.StreamInputs, stream) + } + } + if !reflect.DeepEqual(*workload.Provider, want) { + t.Error("mapped order, values, nil/empty state or other fields changed") + } + // Returned append-target entries must not alias configured entries. + if len(workload.Provider.ArtifactRefs) > 0 { + workload.Provider.ArtifactRefs[0] = "mutated-result" + } + if len(workload.Provider.ContentInputs) > 0 { + workload.Provider.ContentInputs[0].Name = "mutated-result" + } + if len(workload.Provider.StreamInputs) > 0 { + workload.Provider.StreamInputs[0].Name = "mutated-result" + } + } + unchanged() + } + }) + } + } + } + for _, tc := range []struct { + name string + mappings []computeChainInputMapping + }{{"nil_mappings", nil}, {"empty_mappings", []computeChainInputMapping{}}} { + t.Run(tc.name, func(t *testing.T) { + step, err := newComputeChainStep("mapping", chainConfigMap("https://synthetic.invalid")) + if err != nil { + t.Fatal(err) + } + workload := step.config.Steps[1].Workload + before := workload.Provider + if err := applyComputeChainInputMappings(&workload, tc.mappings, nil); err != nil { + t.Fatal(err) + } + if workload.Provider != before { + t.Fatal("no-op changed provider identity") + } + }) + } +}