Centralize pure pipeline profile and binding copies
This commit is contained in:
@@ -100,6 +100,9 @@ overrides, and exact reference changes to `pipeline.ResolvePipeline`.
|
||||
The framework resolves module/default/validator/type/reference compatibility and
|
||||
concrete producer controls before execution. Input slices/maps and the original
|
||||
configuration do not become mutable aliases of the effective result.
|
||||
Pipeline-owned profile and binding copies use the framework's pure copy helpers;
|
||||
they preserve configured values and supported nil/empty containers, while
|
||||
interpretation and normalization remain separate responsibilities.
|
||||
|
||||
Presence-aware selections preserve explicit invocation profile clearing, cache
|
||||
overrides, and zero semantic-turn controls rather than collapsing into omission.
|
||||
|
||||
@@ -52,6 +52,9 @@ registries and the shared native engine. Builders receive independently owned
|
||||
build requests. Exact Go type, codec, evidence-projector, registration, and
|
||||
constructor checks happen before source parsing. There is no correction transport,
|
||||
asset registry, or persistent execution identity to collect.
|
||||
Pure [profile and binding copies](../../internal/framework/pipeline/profile_copy.go)
|
||||
detach supported mutable options, references, and controls without interpreting
|
||||
names or defaults; decoding and resolution perform their own normalization.
|
||||
Offline known-resource [preflight](../../internal/preflight/preflight.go) is
|
||||
a separate composition check, not request execution or module construction.
|
||||
|
||||
|
||||
@@ -405,6 +405,12 @@ results. Preserve fresh application of each proposal and independent family test
|
||||
|
||||
### CAT-017 — Pipeline Value Copying Has Multiple Owners And Mixed Semantics
|
||||
|
||||
**Status:** Resolved. Pipeline-owned pure profile and binding copies are reused
|
||||
by configuration and framework callers; trimming remains explicit in decoding
|
||||
and resolution. Mutation tests cover supported nested containers, references,
|
||||
validators, control pointers, whitespace, and nil/empty values, including
|
||||
effective configuration isolation. The prescribed package checks pass.
|
||||
|
||||
**Assessment:** Ownership/refactoring opportunity; original medium.
|
||||
|
||||
**Sources:** [CPLX-007](03-complexity.md#cplx-007--copying-pipeline-values-has-multiple-owners-and-mixed-semantics),
|
||||
|
||||
@@ -639,6 +639,12 @@ Update CLI/operations/subprocess guidance.
|
||||
|
||||
## Stage 22 — Centralize Pure Pipeline Copies
|
||||
|
||||
**Completed.** Pipeline-owned pure profile and binding copies are shared with
|
||||
configuration; explicit decoding/resolution owns name trimming. Supported
|
||||
option containers, nested references, validators, control pointers, whitespace,
|
||||
and nil/empty values have mutation and preservation coverage, including effective
|
||||
configuration isolation. Config, pipeline, and CLI checks pass.
|
||||
|
||||
**Coverage:** CAT-017. **Prerequisites:** Stage 3 where files overlap.
|
||||
|
||||
Inventory copy switches in config/config.go, pipeline/profile.go, and
|
||||
|
||||
@@ -39,59 +39,7 @@ func cloneConfig(in Config) Config {
|
||||
out := in
|
||||
out.Pipelines = make(map[string]pipeline.PipelineProfile, len(in.Pipelines))
|
||||
for key, profile := range in.Pipelines {
|
||||
out.Pipelines[key] = clonePipelineProfile(profile)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func clonePipelineProfile(in pipeline.PipelineProfile) pipeline.PipelineProfile {
|
||||
out := in
|
||||
out.Mode = cloneValue(in.Mode)
|
||||
out.MaxAdditionalTurns = cloneValue(in.MaxAdditionalTurns)
|
||||
out.Cache = cloneValue(in.Cache)
|
||||
out.Input = cloneModuleBinding(in.Input)
|
||||
out.Chunk = cloneModuleBinding(in.Chunk)
|
||||
out.Output = cloneModuleBinding(in.Output)
|
||||
out.References = cloneReferenceSourceMap(in.References)
|
||||
if len(in.Artifacts) > 0 {
|
||||
out.Artifacts = make(map[string]pipeline.ArtifactLaneProfile, len(in.Artifacts))
|
||||
for key, lane := range in.Artifacts {
|
||||
out.Artifacts[key] = cloneArtifactLaneProfile(lane)
|
||||
}
|
||||
}
|
||||
if in.Steps != nil {
|
||||
out.Steps = make([]pipeline.PipelineStepProfile, len(in.Steps))
|
||||
for i, step := range in.Steps {
|
||||
out.Steps[i] = clonePipelineStepProfile(step)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func clonePipelineStepProfile(in pipeline.PipelineStepProfile) pipeline.PipelineStepProfile {
|
||||
out := in
|
||||
out.ID = in.ID
|
||||
out.References = cloneReferenceSourceMap(in.References)
|
||||
if len(in.Artifacts) > 0 {
|
||||
out.Artifacts = make(map[string]pipeline.ArtifactLaneProfile, len(in.Artifacts))
|
||||
for key, lane := range in.Artifacts {
|
||||
out.Artifacts[key] = cloneArtifactLaneProfile(lane)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneArtifactLaneProfile(in pipeline.ArtifactLaneProfile) pipeline.ArtifactLaneProfile {
|
||||
out := in
|
||||
out.Extract = cloneModuleBinding(in.Extract)
|
||||
out.Merge = cloneModuleBinding(in.Merge)
|
||||
out.Normalize = cloneModuleBinding(in.Normalize)
|
||||
out.References = cloneReferenceSourceMap(in.References)
|
||||
if len(in.Validators) > 0 {
|
||||
out.Validators = make([]pipeline.ModuleBinding, len(in.Validators))
|
||||
for i, binding := range in.Validators {
|
||||
out.Validators[i] = cloneModuleBinding(binding)
|
||||
}
|
||||
out.Pipelines[key] = pipeline.ClonePipelineProfile(profile)
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -107,76 +55,6 @@ func cloneStringMap(in map[string]string) map[string]string {
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneReferenceSourceMap(in map[string]pipeline.ReferenceSource) map[string]pipeline.ReferenceSource {
|
||||
if len(in) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]pipeline.ReferenceSource, len(in))
|
||||
for key, source := range in {
|
||||
out[key] = cloneReferenceSource(source)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneReferenceSource(in pipeline.ReferenceSource) pipeline.ReferenceSource {
|
||||
out := in
|
||||
if in.Artifact != nil {
|
||||
artifact := *in.Artifact
|
||||
out.Artifact = &artifact
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneModuleBinding(in pipeline.ModuleBinding) pipeline.ModuleBinding {
|
||||
out := in
|
||||
out.Mode = cloneValue(in.Mode)
|
||||
out.MaxAdditionalTurns = cloneValue(in.MaxAdditionalTurns)
|
||||
out.Cache = cloneValue(in.Cache)
|
||||
if len(in.Options) > 0 {
|
||||
out.Options = cloneOptions(in.Options)
|
||||
}
|
||||
out.References = cloneReferenceSourceMap(in.References)
|
||||
out.Validators = cloneValidatorOverride(in.Validators)
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneValidatorOverride(in pipeline.ValidatorOverride) pipeline.ValidatorOverride {
|
||||
out := pipeline.ValidatorOverride{Set: in.Set}
|
||||
if len(in.Validators) > 0 {
|
||||
out.Validators = make([]pipeline.ModuleBinding, len(in.Validators))
|
||||
for i, binding := range in.Validators {
|
||||
out.Validators[i] = cloneModuleBinding(binding)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneOptions(in map[string]any) map[string]any {
|
||||
if len(in) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]any, len(in))
|
||||
for key, value := range in {
|
||||
out[key] = cloneOptionValue(value)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
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, item := range typed {
|
||||
out[i] = cloneOptionValue(item)
|
||||
}
|
||||
return out
|
||||
default:
|
||||
return typed
|
||||
}
|
||||
}
|
||||
|
||||
func cloneValue[T any](value *T) *T {
|
||||
if value == nil {
|
||||
return nil
|
||||
|
||||
@@ -50,7 +50,7 @@ func (c Config) Resolve(input ResolveInput) (EffectiveConfig, error) {
|
||||
if !ok {
|
||||
return EffectiveConfig{}, fmt.Errorf("pipeline %q is not configured", pipelineID)
|
||||
}
|
||||
profile = clonePipelineProfile(profile)
|
||||
profile = pipeline.ClonePipelineProfile(profile)
|
||||
profile.ID = pipelineID
|
||||
|
||||
resolved, err := pipeline.ResolvePipeline(profile, pipeline.ResolveOptions{
|
||||
|
||||
@@ -267,7 +267,7 @@ func TestEffectiveConfigValidatorOverridesRemainDistinctAndOrdered(t *testing.T)
|
||||
|
||||
func TestEffectiveConfigAndResolutionInputsDoNotAliasSource(t *testing.T) {
|
||||
profile := effectiveProfile()
|
||||
profile.Chunk.Options = map[string]any{"nested": map[string]any{"safe": "source"}}
|
||||
profile.Chunk.Options = map[string]any{"nested": map[string]any{"safe": "source", "strings": []string{"original"}, "bytes": []byte("data"), "raw": json.RawMessage(`{"key":1}`)}}
|
||||
profile.Chunk.References = pipeline.ExternalReferenceMap(map[string]string{"chunk-ref": "chunk.txt"})
|
||||
lane := profile.Artifacts["lane"]
|
||||
lane.Extract.Validators = pipeline.ValidatorOverride{
|
||||
@@ -292,6 +292,9 @@ func TestEffectiveConfigAndResolutionInputsDoNotAliasSource(t *testing.T) {
|
||||
}
|
||||
|
||||
effective.Config.Pipelines["main"].Chunk.Options["nested"].(map[string]any)["safe"] = "effective-config"
|
||||
effective.Config.Pipelines["main"].Chunk.Options["nested"].(map[string]any)["strings"].([]string)[0] = "changed"
|
||||
effective.Config.Pipelines["main"].Chunk.Options["nested"].(map[string]any)["bytes"].([]byte)[0] = 'X'
|
||||
effective.ResolvedPipeline.Chunk.Options["nested"].(map[string]any)["raw"].(json.RawMessage)[2] = 'X'
|
||||
effective.ResolvedPipeline.Chunk.Options["nested"].(map[string]any)["safe"] = "resolved-pipeline"
|
||||
effective.ResolvedPipeline.ChunkReferences.Bindings[0].Source = "resolved-reference"
|
||||
effective.ResolvedPipeline.ValidatorChains[1].Validators[0].Binding.Options["nested"].(map[string]any)["safe"] = "resolved-validator"
|
||||
@@ -301,6 +304,10 @@ func TestEffectiveConfigAndResolutionInputsDoNotAliasSource(t *testing.T) {
|
||||
if got := cfg.Pipelines["main"].Chunk.Options["nested"].(map[string]any)["safe"]; got != "source" {
|
||||
t.Fatalf("source config option was aliased: %v", got)
|
||||
}
|
||||
nested := cfg.Pipelines["main"].Chunk.Options["nested"].(map[string]any)
|
||||
if nested["strings"].([]string)[0] != "original" || string(nested["bytes"].([]byte)) != "data" || string(nested["raw"].(json.RawMessage)) != `{"key":1}` {
|
||||
t.Fatalf("source config typed options were aliased: %#v", nested)
|
||||
}
|
||||
if got := cfg.Pipelines["main"].Chunk.References["chunk-ref"].Path; got != "chunk.txt" {
|
||||
t.Fatalf("source config references were aliased: %v", got)
|
||||
}
|
||||
|
||||
@@ -205,7 +205,7 @@ func documentBindingToRuntime(document BindingDocument) pipeline.ModuleBinding {
|
||||
MaxAdditionalTurns: cloneValue(document.MaxAdditionalTurns),
|
||||
Cache: cloneValue(document.Cache),
|
||||
Module: document.Module,
|
||||
Options: documentOptions(document.Options),
|
||||
Options: pipeline.CloneModuleOptions(document.Options),
|
||||
References: documentReferencesToRuntime(document.References),
|
||||
}
|
||||
if document.LLMProfile != nil {
|
||||
@@ -224,7 +224,7 @@ func documentBindingToRuntime(document BindingDocument) pipeline.ModuleBinding {
|
||||
func documentValidatorToRuntime(document ValidatorDocument) pipeline.ModuleBinding {
|
||||
binding := pipeline.ModuleBinding{
|
||||
Module: document.Module,
|
||||
Options: documentOptions(document.Options),
|
||||
Options: pipeline.CloneModuleOptions(document.Options),
|
||||
}
|
||||
if document.LLMProfile != nil {
|
||||
binding.LLMProfile = *document.LLMProfile
|
||||
@@ -248,29 +248,3 @@ func documentReferencesToRuntime(documents map[string]ReferenceDocument) map[str
|
||||
}
|
||||
return references
|
||||
}
|
||||
|
||||
func documentOptions(values map[string]any) map[string]any {
|
||||
if values == nil {
|
||||
return nil
|
||||
}
|
||||
result := make(map[string]any, len(values))
|
||||
for key, value := range values {
|
||||
result[key] = cloneDocumentOption(value)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func cloneDocumentOption(value any) any {
|
||||
switch typed := value.(type) {
|
||||
case map[string]any:
|
||||
return documentOptions(typed)
|
||||
case []any:
|
||||
result := make([]any, len(typed))
|
||||
for index, item := range typed {
|
||||
result[index] = cloneDocumentOption(item)
|
||||
}
|
||||
return result
|
||||
default:
|
||||
return typed
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,7 +232,7 @@ pipelines:
|
||||
}
|
||||
|
||||
func pipelineProfileWithChangedOption(profile pipeline.PipelineProfile) pipeline.PipelineProfile {
|
||||
profile = clonePipelineProfile(profile)
|
||||
profile = pipeline.ClonePipelineProfile(profile)
|
||||
nested := profile.Artifacts["score"].Extract.Options["nested"].(map[string]any)
|
||||
nested["value"] = "changed"
|
||||
lane := profile.Artifacts["score"]
|
||||
|
||||
@@ -14,10 +14,10 @@ func (e EffectiveConfig) RedactedResolvedPipelinePayload() pipeline.ResolvedPipe
|
||||
|
||||
func cloneResolvedPipeline(in pipeline.ResolvedPipeline) pipeline.ResolvedPipeline {
|
||||
out := in
|
||||
out.Input = redactBinding(cloneModuleBinding(in.Input))
|
||||
out.Chunk = redactBinding(cloneModuleBinding(in.Chunk))
|
||||
out.Input = redactBinding(pipeline.CloneModuleBinding(in.Input))
|
||||
out.Chunk = redactBinding(pipeline.CloneModuleBinding(in.Chunk))
|
||||
out.ChunkReferences = pipeline.CloneReferenceTarget(in.ChunkReferences)
|
||||
out.Output = redactBinding(cloneModuleBinding(in.Output))
|
||||
out.Output = redactBinding(pipeline.CloneModuleBinding(in.Output))
|
||||
if len(in.ValidatorChains) > 0 {
|
||||
out.ValidatorChains = make([]pipeline.ResolvedValidatorChain, len(in.ValidatorChains))
|
||||
for i, chain := range in.ValidatorChains {
|
||||
@@ -45,7 +45,7 @@ func cloneResolvedValidatorChain(in pipeline.ResolvedValidatorChain) pipeline.Re
|
||||
out.Validators = make([]pipeline.ResolvedValidator, len(in.Validators))
|
||||
for i, validator := range in.Validators {
|
||||
out.Validators[i] = pipeline.ResolvedValidator{
|
||||
Binding: redactBinding(cloneModuleBinding(validator.Binding)),
|
||||
Binding: redactBinding(pipeline.CloneModuleBinding(validator.Binding)),
|
||||
ExecutionClass: validator.ExecutionClass,
|
||||
Target: validator.Target,
|
||||
ArtifactKind: validator.ArtifactKind,
|
||||
@@ -57,16 +57,16 @@ func cloneResolvedValidatorChain(in pipeline.ResolvedValidatorChain) pipeline.Re
|
||||
|
||||
func cloneResolvedArtifactLane(in pipeline.ResolvedArtifactLane) pipeline.ResolvedArtifactLane {
|
||||
out := in
|
||||
out.Extract = redactBinding(cloneModuleBinding(in.Extract))
|
||||
out.Merge = redactBinding(cloneModuleBinding(in.Merge))
|
||||
out.Normalize = redactBinding(cloneModuleBinding(in.Normalize))
|
||||
out.Extract = redactBinding(pipeline.CloneModuleBinding(in.Extract))
|
||||
out.Merge = redactBinding(pipeline.CloneModuleBinding(in.Merge))
|
||||
out.Normalize = redactBinding(pipeline.CloneModuleBinding(in.Normalize))
|
||||
out.ExtractReferences = pipeline.CloneReferenceTarget(in.ExtractReferences)
|
||||
out.MergeReferences = pipeline.CloneReferenceTarget(in.MergeReferences)
|
||||
out.NormalizeReferences = pipeline.CloneReferenceTarget(in.NormalizeReferences)
|
||||
if len(in.Validators) > 0 {
|
||||
out.Validators = make([]pipeline.ModuleBinding, len(in.Validators))
|
||||
for i, binding := range in.Validators {
|
||||
out.Validators[i] = redactBinding(cloneModuleBinding(binding))
|
||||
out.Validators[i] = redactBinding(pipeline.CloneModuleBinding(binding))
|
||||
}
|
||||
}
|
||||
return out
|
||||
|
||||
@@ -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