Centralize pure pipeline profile and binding copies
This commit is contained in:
@@ -34,13 +34,13 @@ func validateRegisteredOptions(validator OptionValidator, options map[string]any
|
||||
if validator == nil {
|
||||
return fmt.Errorf("option validator must not be nil")
|
||||
}
|
||||
return validator(cloneOptions(options))
|
||||
return validator(CloneModuleOptions(options))
|
||||
}
|
||||
|
||||
func cloneBuildRequest(request BuildRequest) BuildRequest {
|
||||
return BuildRequest{
|
||||
Dependencies: request.Dependencies,
|
||||
Options: cloneOptions(request.Options),
|
||||
Options: CloneModuleOptions(request.Options),
|
||||
References: CloneReferenceSet(request.References),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,7 +142,7 @@ func (r *OutputEncoderRegistry) ValidateProfileOptions(key string, context Outpu
|
||||
if validator == nil {
|
||||
return nil
|
||||
}
|
||||
return validator(OutputProfileOptionContext{LaneIDs: append([]string(nil), context.LaneIDs...)}, cloneOptions(options))
|
||||
return validator(OutputProfileOptionContext{LaneIDs: append([]string(nil), context.LaneIDs...)}, CloneModuleOptions(options))
|
||||
}
|
||||
|
||||
func (r *OutputEncoderRegistry) Spec(key string) (ModuleSpec, bool) {
|
||||
|
||||
@@ -99,10 +99,10 @@ func Prepare(resolved ResolvedPipeline, registries Registries, deps ModuleDepend
|
||||
}
|
||||
stable := cloneResolvedPipeline(resolved)
|
||||
prepared := &PreparedPipeline{
|
||||
Input: cloneModuleBinding(stable.Input),
|
||||
Chunk: cloneModuleBinding(stable.Chunk),
|
||||
Input: CloneModuleBinding(stable.Input),
|
||||
Chunk: CloneModuleBinding(stable.Chunk),
|
||||
ChunkPrompts: append([]string(nil), stable.ChunkPrompts...),
|
||||
Output: cloneModuleBinding(stable.Output),
|
||||
Output: CloneModuleBinding(stable.Output),
|
||||
resolved: stable,
|
||||
dependencies: deps,
|
||||
artifactCodecs: registries.ArtifactCodecs,
|
||||
@@ -423,9 +423,9 @@ func validateRegistrySet(resolved ResolvedPipeline, registries Registries) error
|
||||
func cloneResolvedPipeline(in ResolvedPipeline) ResolvedPipeline {
|
||||
out := in
|
||||
out.ChunkPrompts = append([]string(nil), in.ChunkPrompts...)
|
||||
out.Input = cloneModuleBinding(in.Input)
|
||||
out.Chunk = cloneModuleBinding(in.Chunk)
|
||||
out.Output = cloneModuleBinding(in.Output)
|
||||
out.Input = CloneModuleBinding(in.Input)
|
||||
out.Chunk = CloneModuleBinding(in.Chunk)
|
||||
out.Output = CloneModuleBinding(in.Output)
|
||||
out.ChunkReferences = CloneReferenceTarget(in.ChunkReferences)
|
||||
out.ValidatorChains = cloneResolvedValidatorChains(in.ValidatorChains)
|
||||
if len(in.Steps) > 0 {
|
||||
@@ -448,9 +448,9 @@ func cloneResolvedArtifactLane(in ResolvedArtifactLane) ResolvedArtifactLane {
|
||||
out.ExtractPrompts = append([]string(nil), in.ExtractPrompts...)
|
||||
out.MergePrompts = append([]string(nil), in.MergePrompts...)
|
||||
out.NormalizePrompts = append([]string(nil), in.NormalizePrompts...)
|
||||
out.Extract = cloneModuleBinding(in.Extract)
|
||||
out.Merge = cloneModuleBinding(in.Merge)
|
||||
out.Normalize = cloneModuleBinding(in.Normalize)
|
||||
out.Extract = CloneModuleBinding(in.Extract)
|
||||
out.Merge = CloneModuleBinding(in.Merge)
|
||||
out.Normalize = CloneModuleBinding(in.Normalize)
|
||||
out.Validators = cloneModuleBindings(in.Validators)
|
||||
out.ExtractReferences = CloneReferenceTarget(in.ExtractReferences)
|
||||
out.MergeReferences = CloneReferenceTarget(in.MergeReferences)
|
||||
|
||||
@@ -141,10 +141,19 @@ func (binding *ModuleBinding) UnmarshalJSON(data []byte) error {
|
||||
}
|
||||
out.Validators = ValidatorOverride{Set: true, Validators: validators}
|
||||
}
|
||||
*binding = cloneModuleBinding(out)
|
||||
*binding = CloneModuleBinding(out)
|
||||
normalizeBindingNames(binding)
|
||||
return nil
|
||||
}
|
||||
|
||||
func normalizeBindingNames(binding *ModuleBinding) {
|
||||
binding.Module = strings.TrimSpace(binding.Module)
|
||||
binding.LLMProfile = strings.TrimSpace(binding.LLMProfile)
|
||||
for i := range binding.Validators.Validators {
|
||||
normalizeBindingNames(&binding.Validators.Validators[i])
|
||||
}
|
||||
}
|
||||
|
||||
type ArtifactLaneProfile struct {
|
||||
Extract ModuleBinding `json:"extract"`
|
||||
Merge ModuleBinding `json:"merge,omitempty"`
|
||||
@@ -935,7 +944,7 @@ func resolveValidatorChain(pipelineID string, laneID string, stage ModuleStage,
|
||||
return ResolvedValidatorChain{}, fmt.Errorf("pipeline %q %s validator chain for module %q: %w", pipelineID, stage, chain.ModuleKey, err)
|
||||
}
|
||||
chain.Validators = append(chain.Validators, ResolvedValidator{
|
||||
Binding: cloneModuleBinding(validator),
|
||||
Binding: CloneModuleBinding(validator),
|
||||
ExecutionClass: spec.ExecutionClass,
|
||||
Prompts: append([]string(nil), spec.Prompts...),
|
||||
Target: target,
|
||||
@@ -968,7 +977,7 @@ func cloneResolvedValidators(validators []ResolvedValidator) []ResolvedValidator
|
||||
out := make([]ResolvedValidator, len(validators))
|
||||
for i, validator := range validators {
|
||||
out[i] = ResolvedValidator{
|
||||
Binding: cloneModuleBinding(validator.Binding),
|
||||
Binding: CloneModuleBinding(validator.Binding),
|
||||
ExecutionClass: validator.ExecutionClass,
|
||||
Prompts: append([]string(nil), validator.Prompts...),
|
||||
Target: validator.Target,
|
||||
@@ -1343,6 +1352,10 @@ func sortedReferenceBindingKeys(values map[string]ReferenceBinding) []string {
|
||||
func resolveBinding(binding ModuleBinding, defaultModule string, referenceSlotLabel string) (ModuleBinding, error) {
|
||||
module := resolveModuleKey(binding.Module, defaultModule)
|
||||
llmProfile := strings.TrimSpace(binding.LLMProfile)
|
||||
validators := cloneValidatorOverride(binding.Validators)
|
||||
for i := range validators.Validators {
|
||||
normalizeBindingNames(&validators.Validators[i])
|
||||
}
|
||||
references, err := normalizeReferenceMap(binding.References, referenceSlotLabel)
|
||||
if err != nil {
|
||||
return ModuleBinding{}, err
|
||||
@@ -1353,9 +1366,9 @@ func resolveBinding(binding ModuleBinding, defaultModule string, referenceSlotLa
|
||||
MaxAdditionalTurns: cloneControl(binding.MaxAdditionalTurns),
|
||||
Cache: cloneControl(binding.Cache),
|
||||
LLMProfile: llmProfile,
|
||||
Options: cloneOptions(binding.Options),
|
||||
Options: CloneModuleOptions(binding.Options),
|
||||
References: references,
|
||||
Validators: cloneValidatorOverride(binding.Validators),
|
||||
Validators: validators,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -1442,39 +1455,6 @@ func resolveBindings(bindings []ModuleBinding, defaultModule string, referenceSl
|
||||
return resolved, nil
|
||||
}
|
||||
|
||||
func cloneOptions(options map[string]any) map[string]any {
|
||||
if len(options) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
copied := make(map[string]any, len(options))
|
||||
for key, value := range options {
|
||||
copied[key] = cloneOptionValue(value)
|
||||
}
|
||||
return copied
|
||||
}
|
||||
|
||||
func cloneOptionValue(value any) any {
|
||||
switch typed := value.(type) {
|
||||
case map[string]any:
|
||||
return cloneOptions(typed)
|
||||
case []any:
|
||||
out := make([]any, len(typed))
|
||||
for i := range typed {
|
||||
out[i] = cloneOptionValue(typed[i])
|
||||
}
|
||||
return out
|
||||
case []string:
|
||||
return append([]string(nil), typed...)
|
||||
case []byte:
|
||||
return append([]byte(nil), typed...)
|
||||
case json.RawMessage:
|
||||
return append(json.RawMessage(nil), typed...)
|
||||
default:
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
func selectedArtifactLanes(pipelineID string, artifacts map[string]ArtifactLaneProfile, options ResolveOptions) (map[string]ArtifactLaneProfile, []string, error) {
|
||||
lanesByID := make(map[string]ArtifactLaneProfile, len(artifacts))
|
||||
for rawLaneID, lane := range artifacts {
|
||||
|
||||
139
internal/framework/pipeline/profile_copy.go
Normal file
139
internal/framework/pipeline/profile_copy.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package pipeline
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
// ClonePipelineProfile detaches the supported mutable profile values without
|
||||
// interpreting defaults, normalizing identifiers, or changing nil/empty values.
|
||||
func ClonePipelineProfile(profile PipelineProfile) PipelineProfile {
|
||||
profile.Mode = cloneControl(profile.Mode)
|
||||
profile.MaxAdditionalTurns = cloneControl(profile.MaxAdditionalTurns)
|
||||
profile.Cache = cloneControl(profile.Cache)
|
||||
profile.Input = CloneModuleBinding(profile.Input)
|
||||
profile.Chunk = CloneModuleBinding(profile.Chunk)
|
||||
profile.Output = CloneModuleBinding(profile.Output)
|
||||
profile.References = cloneReferenceSources(profile.References)
|
||||
profile.Artifacts = cloneArtifactProfiles(profile.Artifacts)
|
||||
if profile.Steps != nil {
|
||||
steps := make([]PipelineStepProfile, len(profile.Steps))
|
||||
for i, step := range profile.Steps {
|
||||
step.References = cloneReferenceSources(step.References)
|
||||
step.Artifacts = cloneArtifactProfiles(step.Artifacts)
|
||||
steps[i] = step
|
||||
}
|
||||
profile.Steps = steps
|
||||
}
|
||||
return profile
|
||||
}
|
||||
|
||||
func cloneArtifactProfiles(artifacts map[string]ArtifactLaneProfile) map[string]ArtifactLaneProfile {
|
||||
if artifacts == nil {
|
||||
return nil
|
||||
}
|
||||
cloned := make(map[string]ArtifactLaneProfile, len(artifacts))
|
||||
for key, lane := range artifacts {
|
||||
lane.Extract = CloneModuleBinding(lane.Extract)
|
||||
lane.Merge = CloneModuleBinding(lane.Merge)
|
||||
lane.Normalize = CloneModuleBinding(lane.Normalize)
|
||||
lane.Validators = cloneModuleBindings(lane.Validators)
|
||||
lane.References = cloneReferenceSources(lane.References)
|
||||
cloned[key] = lane
|
||||
}
|
||||
return cloned
|
||||
}
|
||||
|
||||
// CloneModuleBinding copies supported mutable binding values without normalization.
|
||||
func CloneModuleBinding(binding ModuleBinding) ModuleBinding {
|
||||
binding.Mode = cloneControl(binding.Mode)
|
||||
binding.MaxAdditionalTurns = cloneControl(binding.MaxAdditionalTurns)
|
||||
binding.Cache = cloneControl(binding.Cache)
|
||||
binding.Options = CloneModuleOptions(binding.Options)
|
||||
binding.References = cloneReferenceSources(binding.References)
|
||||
binding.Validators = cloneValidatorOverride(binding.Validators)
|
||||
return binding
|
||||
}
|
||||
|
||||
func cloneModuleBindings(bindings []ModuleBinding) []ModuleBinding {
|
||||
if bindings == nil {
|
||||
return nil
|
||||
}
|
||||
out := make([]ModuleBinding, len(bindings))
|
||||
for i, binding := range bindings {
|
||||
out[i] = CloneModuleBinding(binding)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneReferenceSources(references map[string]ReferenceSource) map[string]ReferenceSource {
|
||||
if references == nil {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]ReferenceSource, len(references))
|
||||
for key, source := range references {
|
||||
out[key] = cloneReferenceSource(source)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneReferenceSource(source ReferenceSource) ReferenceSource {
|
||||
if source.Artifact != nil {
|
||||
artifact := *source.Artifact
|
||||
source.Artifact = &artifact
|
||||
}
|
||||
return source
|
||||
}
|
||||
|
||||
func cloneValidatorOverride(override ValidatorOverride) ValidatorOverride {
|
||||
override.Validators = cloneModuleBindings(override.Validators)
|
||||
return override
|
||||
}
|
||||
|
||||
// CloneModuleOptions detaches the supported option containers without interpretation.
|
||||
func CloneModuleOptions(options map[string]any) map[string]any {
|
||||
if options == nil {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]any, len(options))
|
||||
for key, value := range options {
|
||||
out[key] = cloneOptionValue(value)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneOptionValue(value any) any {
|
||||
switch typed := value.(type) {
|
||||
case map[string]any:
|
||||
return CloneModuleOptions(typed)
|
||||
case []any:
|
||||
if typed == nil {
|
||||
return []any(nil)
|
||||
}
|
||||
out := make([]any, len(typed))
|
||||
for i, item := range typed {
|
||||
out[i] = cloneOptionValue(item)
|
||||
}
|
||||
return out
|
||||
case []string:
|
||||
if typed == nil {
|
||||
return []string(nil)
|
||||
}
|
||||
out := make([]string, len(typed))
|
||||
copy(out, typed)
|
||||
return out
|
||||
case []byte:
|
||||
if typed == nil {
|
||||
return []byte(nil)
|
||||
}
|
||||
out := make([]byte, len(typed))
|
||||
copy(out, typed)
|
||||
return out
|
||||
case json.RawMessage:
|
||||
if typed == nil {
|
||||
return json.RawMessage(nil)
|
||||
}
|
||||
out := make(json.RawMessage, len(typed))
|
||||
copy(out, typed)
|
||||
return out
|
||||
default:
|
||||
return value
|
||||
}
|
||||
}
|
||||
63
internal/framework/pipeline/profile_copy_test.go
Normal file
63
internal/framework/pipeline/profile_copy_test.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
)
|
||||
|
||||
func TestClonePipelineProfilePreservesAndDetachesMutableValues(t *testing.T) {
|
||||
mode, cache, turns := contracts.ExecutionMode(" permissive "), contracts.CacheMode(" bypass "), 0
|
||||
profile := PipelineProfile{
|
||||
ID: " profile ", LLMProfile: " profile-name ", Mode: &mode, Cache: &cache, MaxAdditionalTurns: &turns,
|
||||
References: map[string]ReferenceSource{"top": {Artifact: &ArtifactReference{Step: " step ", Lane: " lane "}}},
|
||||
Artifacts: map[string]ArtifactLaneProfile{},
|
||||
Steps: []PipelineStepProfile{{ID: " step ", References: map[string]ReferenceSource{"external": {Path: " path "}}, Artifacts: map[string]ArtifactLaneProfile{
|
||||
" lane ": {References: map[string]ReferenceSource{"lane": {Artifact: &ArtifactReference{Step: " source ", Lane: " value "}}},
|
||||
Extract: ModuleBinding{Module: " extractor ", LLMProfile: " model ", Mode: &mode, Cache: &cache, MaxAdditionalTurns: &turns,
|
||||
Options: map[string]any{"nested": map[string]any{"list": []any{[]string{"a"}, []byte("b"), json.RawMessage(`{"c":1}`)}}, "empty": []any{}, "scalar": 42},
|
||||
References: map[string]ReferenceSource{"input": {Artifact: &ArtifactReference{Step: " producer ", Lane: " output "}}},
|
||||
Validators: ValidatorOverride{Set: true, Validators: []ModuleBinding{{Module: " validator ", Options: map[string]any{"bytes": []byte("v")}}}},
|
||||
}, Validators: []ModuleBinding{{Module: " lane-validator "}}},
|
||||
}}},
|
||||
}
|
||||
copy := ClonePipelineProfile(profile)
|
||||
if !reflect.DeepEqual(copy, profile) {
|
||||
t.Fatalf("clone changed values: %#v", copy)
|
||||
}
|
||||
*copy.Mode = contracts.ExecutionMode("strict")
|
||||
*copy.MaxAdditionalTurns = 2
|
||||
*copy.Cache = contracts.CacheMode("refresh")
|
||||
copy.References["top"].Artifact.Step = "changed"
|
||||
step := ©.Steps[0]
|
||||
step.References["external"] = ReferenceSource{Path: "changed"}
|
||||
lane := step.Artifacts[" lane "]
|
||||
lane.References["lane"].Artifact.Step = "changed"
|
||||
lane.Extract.References["input"].Artifact.Lane = "changed"
|
||||
list := lane.Extract.Options["nested"].(map[string]any)["list"].([]any)
|
||||
list[0].([]string)[0] = "changed"
|
||||
list[1].([]byte)[0] = 'x'
|
||||
list[2].(json.RawMessage)[2] = 'x'
|
||||
lane.Extract.Validators.Validators[0].Options["bytes"].([]byte)[0] = 'x'
|
||||
lane.Validators[0].Module = "changed"
|
||||
step.Artifacts[" lane "] = lane
|
||||
original := profile.Steps[0].Artifacts[" lane "]
|
||||
originalList := original.Extract.Options["nested"].(map[string]any)["list"].([]any)
|
||||
if *profile.Mode != " permissive " || *profile.Cache != " bypass " || *profile.MaxAdditionalTurns != 0 || profile.References["top"].Artifact.Step != " step " || profile.Steps[0].References["external"].Path != " path " || original.References["lane"].Artifact.Step != " source " || original.Extract.References["input"].Artifact.Lane != " output " || originalList[0].([]string)[0] != "a" || string(originalList[1].([]byte)) != "b" || string(originalList[2].(json.RawMessage)) != `{"c":1}` || string(original.Extract.Validators.Validators[0].Options["bytes"].([]byte)) != "v" || original.Validators[0].Module != " lane-validator " {
|
||||
t.Fatal("clone mutation reached the original profile")
|
||||
}
|
||||
}
|
||||
|
||||
func TestClonePipelineProfilePreservesNilAndEmptyContainers(t *testing.T) {
|
||||
for _, profile := range []PipelineProfile{
|
||||
{},
|
||||
{Artifacts: map[string]ArtifactLaneProfile{}, Steps: []PipelineStepProfile{}, References: map[string]ReferenceSource{}, Input: ModuleBinding{Options: map[string]any{"list": []any{}, "strings": []string{}, "bytes": []byte{}, "raw": json.RawMessage{}}, References: map[string]ReferenceSource{}, Validators: ValidatorOverride{Set: true, Validators: []ModuleBinding{}}}},
|
||||
{Input: ModuleBinding{Options: map[string]any{"list": []any(nil), "strings": []string(nil), "bytes": []byte(nil), "raw": json.RawMessage(nil)}}},
|
||||
} {
|
||||
if copy := ClonePipelineProfile(profile); !reflect.DeepEqual(copy, profile) {
|
||||
t.Fatalf("clone = %#v, want %#v", copy, profile)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,48 +79,3 @@ func normalizeValidatorChainMapping(mapping ValidatorChainMapping) (ValidatorCha
|
||||
}
|
||||
return normalized, nil
|
||||
}
|
||||
|
||||
func cloneModuleBindings(bindings []ModuleBinding) []ModuleBinding {
|
||||
if len(bindings) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([]ModuleBinding, len(bindings))
|
||||
for i, binding := range bindings {
|
||||
out[i] = cloneModuleBinding(binding)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneModuleBinding(binding ModuleBinding) ModuleBinding {
|
||||
binding.Module = strings.TrimSpace(binding.Module)
|
||||
binding.LLMProfile = strings.TrimSpace(binding.LLMProfile)
|
||||
binding.Mode = cloneControl(binding.Mode)
|
||||
binding.MaxAdditionalTurns = cloneControl(binding.MaxAdditionalTurns)
|
||||
binding.Cache = cloneControl(binding.Cache)
|
||||
binding.Options = cloneOptions(binding.Options)
|
||||
if len(binding.References) > 0 {
|
||||
references := make(map[string]ReferenceSource, len(binding.References))
|
||||
for key, value := range binding.References {
|
||||
references[key] = cloneReferenceSource(value)
|
||||
}
|
||||
binding.References = references
|
||||
}
|
||||
binding.Validators = cloneValidatorOverride(binding.Validators)
|
||||
return binding
|
||||
}
|
||||
|
||||
func cloneReferenceSource(source ReferenceSource) ReferenceSource {
|
||||
out := source
|
||||
if source.Artifact != nil {
|
||||
artifact := *source.Artifact
|
||||
out.Artifact = &artifact
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneValidatorOverride(override ValidatorOverride) ValidatorOverride {
|
||||
return ValidatorOverride{
|
||||
Set: override.Set,
|
||||
Validators: cloneModuleBindings(override.Validators),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user