From 10221b06242b818e28c1dff3deac450a839c79d6 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Sun, 29 Nov 2026 17:53:36 +0000 Subject: [PATCH] Carry collection selections through configuration --- docs/roadmap/implementation.md | 2 + internal/core/config/document.go | 5 +- internal/core/config/loaded_config.go | 6 +- internal/core/config/pipeline_document.go | 8 +++ .../core/config/reference_collections_test.go | 57 +++++++++++++++++++ internal/framework/pipeline/profile.go | 14 +++++ internal/framework/pipeline/profile_copy.go | 3 + internal/framework/pipeline/profile_values.go | 14 ++++- .../pipeline/reference_resolution.go | 19 ++++++- .../pipeline/reference_resolution_test.go | 50 ++++++++++++++++ 10 files changed, 172 insertions(+), 6 deletions(-) create mode 100644 internal/core/config/reference_collections_test.go diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index aea153f7..fbd84fef 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -212,6 +212,8 @@ aligned contracts. Domain checks add no new authoring format or decoding path. ## Stage 4 — Carry Collection Selection Through Configuration +**Status:** Complete. CLI path overrides and materialization are wired separately below. + **Goal:** Preserve binding selection and its presence through normal configuration resolution without changing ConfigKit ownership. diff --git a/internal/core/config/document.go b/internal/core/config/document.go index 1f019d3c..3a537880 100644 --- a/internal/core/config/document.go +++ b/internal/core/config/document.go @@ -69,8 +69,9 @@ type ValidatorDocument struct { // validation establishes which locations permit generated references and // requires exactly one populated branch. type ReferenceDocument struct { - Path *string `yaml:"path,omitempty"` - Artifact *ArtifactReferenceDocument `yaml:"artifact,omitempty"` + Path *string `yaml:"path,omitempty"` + Artifact *ArtifactReferenceDocument `yaml:"artifact,omitempty"` + Collections []string `yaml:"collections,omitempty"` } type ArtifactReferenceDocument struct { diff --git a/internal/core/config/loaded_config.go b/internal/core/config/loaded_config.go index dc308838..459c72af 100644 --- a/internal/core/config/loaded_config.go +++ b/internal/core/config/loaded_config.go @@ -243,7 +243,11 @@ func documentReferencesToRuntime(documents map[string]ReferenceDocument) map[str continue } if document.Path != nil { - references[name] = pipeline.ExternalReference(*document.Path) + reference := pipeline.ExternalReference(*document.Path) + if document.Collections != nil { + reference.Collections = append([]string{}, document.Collections...) + } + references[name] = reference } } return references diff --git a/internal/core/config/pipeline_document.go b/internal/core/config/pipeline_document.go index c7972dd1..c81b5b33 100644 --- a/internal/core/config/pipeline_document.go +++ b/internal/core/config/pipeline_document.go @@ -403,6 +403,14 @@ func validateDocumentReferences(references map[string]ReferenceDocument, path co sourcePath := path.Key(name) hasPath := source.Path != nil hasArtifact := source.Artifact != nil + for index, collection := range source.Collections { + if collection == "" { + validation.Error(sourcePath.Key("collections").Index(index), "must not be empty") + } + } + if hasArtifact && source.Collections != nil { + validation.Error(sourcePath.Key("collections"), "is only supported with an external path") + } if hasPath == hasArtifact { validation.Error(sourcePath, "must select exactly one reference source") continue diff --git a/internal/core/config/reference_collections_test.go b/internal/core/config/reference_collections_test.go new file mode 100644 index 00000000..6b2bbfc7 --- /dev/null +++ b/internal/core/config/reference_collections_test.go @@ -0,0 +1,57 @@ +package config + +import ( + "bytes" + "strings" + "testing" + + "go.maximumdirect.net/configkit" +) + +func TestReferenceCollectionsBindingAndInspection(t *testing.T) { + loaded, err := loadConfigSource(configkit.Bytes("settings.yml", []byte(`version: 6 +pipelines: + main: + input: {module: input} + references: + omitted: {path: a.yml} + "null": {path: b.yml, collections: null} + empty: {path: c.yml, collections: []} + named: {path: d.yml, collections: [spells, " Mixed "]} +`)), LoadOptions{}) + if err != nil { + if native, ok := err.(*configkit.LoadError); ok { + t.Fatalf("%v: %#v", err, native.Diagnostics()) + } + t.Fatal(err) + } + refs := loaded.Config.Pipelines["main"].References + if refs["omitted"].Collections != nil || refs["null"].Collections != nil || refs["empty"].Collections == nil || len(refs["empty"].Collections) != 0 || refs["named"].Collections[1] != " Mixed " { + t.Fatalf("unexpected collection selections: %#v", refs) + } + copy := loaded.RuntimeConfig() + copy.Pipelines["main"].References["named"].Collections[0] = "changed" + if refs["named"].Collections[0] != "spells" { + t.Fatal("detached runtime selection mutated source") + } + var effective bytes.Buffer + if err := loaded.WriteEffective(&effective); err != nil || !strings.Contains(effective.String(), "collections: []") { + t.Fatalf("effective config = %s, %v", effective.String(), err) + } +} + +func TestReferenceCollectionsRejectInvalidInput(t *testing.T) { + for _, value := range []string{ + "{path: a.yml, collections: [\"\"]}", + "{path: a.yml, collections: nope}", + "{path: a.yml, collections: [3]}", + "{artifact: {step: earlier, lane: output}, collections: []}", + } { + t.Run(value, func(t *testing.T) { + content := "version: 6\npipelines:\n main:\n input: {module: input}\n artifacts:\n output:\n extract:\n module: extract\n references:\n ref: " + value + "\n" + if _, err := loadConfigSource(configkit.Bytes("settings.yml", []byte(content)), LoadOptions{}); err == nil { + t.Fatal("accepted invalid reference selection") + } + }) + } +} diff --git a/internal/framework/pipeline/profile.go b/internal/framework/pipeline/profile.go index a5372282..c51fccb0 100644 --- a/internal/framework/pipeline/profile.go +++ b/internal/framework/pipeline/profile.go @@ -25,10 +25,24 @@ type ReferenceBinding struct { LaneID string `json:"lane_id,omitempty"` SlotName string `json:"slot_name"` Source string `json:"source,omitempty"` + Collections []string `json:"collections,omitempty"` Artifact *ArtifactReference `json:"artifact,omitempty"` BindingSource string `json:"binding_source,omitempty"` } +// MarshalJSON preserves the distinction between default and empty selections. +func (binding ReferenceBinding) MarshalJSON() ([]byte, error) { + type bindingFields ReferenceBinding + var collections *[]string + if binding.Collections != nil { + collections = &binding.Collections + } + return json.Marshal(struct { + bindingFields + Collections *[]string `json:"collections,omitempty"` + }{bindingFields: bindingFields(binding), Collections: collections}) +} + type ReferenceUnbind struct { Stage ModuleStage `json:"stage,omitempty"` LaneID string `json:"lane_id,omitempty"` diff --git a/internal/framework/pipeline/profile_copy.go b/internal/framework/pipeline/profile_copy.go index 698ad0e3..a39b5f9b 100644 --- a/internal/framework/pipeline/profile_copy.go +++ b/internal/framework/pipeline/profile_copy.go @@ -75,6 +75,9 @@ func cloneReferenceSources(references map[string]ReferenceSource) map[string]Ref } func cloneReferenceSource(source ReferenceSource) ReferenceSource { + if source.Collections != nil { + source.Collections = append([]string{}, source.Collections...) + } if source.Artifact != nil { artifact := *source.Artifact source.Artifact = &artifact diff --git a/internal/framework/pipeline/profile_values.go b/internal/framework/pipeline/profile_values.go index f1ea6956..c646af21 100644 --- a/internal/framework/pipeline/profile_values.go +++ b/internal/framework/pipeline/profile_values.go @@ -37,8 +37,9 @@ type ArtifactReference struct { // ReferenceSource is the discriminated source form used by configured // reference maps. Exactly one of Path and Artifact is set after resolution. type ReferenceSource struct { - Path string `json:"path,omitempty"` - Artifact *ArtifactReference `json:"artifact,omitempty"` + Path string `json:"path,omitempty"` + Artifact *ArtifactReference `json:"artifact,omitempty"` + Collections []string `json:"collections,omitempty"` } func ExternalReference(path string) ReferenceSource { @@ -64,10 +65,19 @@ func (source ReferenceSource) MarshalJSON() ([]byte, error) { return nil, fmt.Errorf("reference source must not contain both an external path and an artifact selector") } if source.Artifact != nil { + if source.Collections != nil { + return nil, fmt.Errorf("collection selection requires an external path") + } return json.Marshal(struct { Artifact *ArtifactReference `json:"artifact"` }{Artifact: source.Artifact}) } + if source.Collections != nil { + return json.Marshal(struct { + Path string `json:"path"` + Collections []string `json:"collections"` + }{Path: source.Path, Collections: source.Collections}) + } return json.Marshal(source.Path) } diff --git a/internal/framework/pipeline/reference_resolution.go b/internal/framework/pipeline/reference_resolution.go index 73ebe77f..68cfbc24 100644 --- a/internal/framework/pipeline/reference_resolution.go +++ b/internal/framework/pipeline/reference_resolution.go @@ -9,11 +9,17 @@ import ( ) func referenceTarget(stage ModuleStage, laneID string, module string, bindings []ReferenceBinding) ResolvedReferenceTarget { + bindings = append([]ReferenceBinding(nil), bindings...) + for i := range bindings { + if bindings[i].Collections != nil { + bindings[i].Collections = append([]string{}, bindings[i].Collections...) + } + } return ResolvedReferenceTarget{ Stage: stage, LaneID: strings.TrimSpace(laneID), Module: strings.TrimSpace(module), - Bindings: append([]ReferenceBinding(nil), bindings...), + Bindings: bindings, } } @@ -187,12 +193,20 @@ func normalizeTargetReferenceSource(target referenceResolutionTarget, slotName s if slotName == "" { return "", ReferenceSource{}, fmt.Errorf("%s reference slot name must not be empty", referenceTargetErrorContext(target)) } + for _, name := range source.Collections { + if name == "" { + return "", ReferenceSource{}, fmt.Errorf("%s reference slot %q collection name must not be empty", referenceTargetErrorContext(target), slotName) + } + } if source.Artifact == nil { source.Path = strings.TrimSpace(source.Path) if source.Path == "" { return "", ReferenceSource{}, fmt.Errorf("%s reference slot %q source must not be empty", referenceTargetErrorContext(target), slotName) } } else { + if source.Collections != nil { + return "", ReferenceSource{}, fmt.Errorf("%s reference slot %q collection selection requires an external path", referenceTargetErrorContext(target), slotName) + } if strings.TrimSpace(source.Path) != "" { return "", ReferenceSource{}, fmt.Errorf("%s reference slot %q must contain exactly one source form", referenceTargetErrorContext(target), slotName) } @@ -222,6 +236,9 @@ func addReferenceBinding(target referenceResolutionTarget, slots map[string]cont binding.Artifact = source.Artifact } else { binding.Source = source.Path + if source.Collections != nil { + binding.Collections = append([]string{}, source.Collections...) + } } bindings[slotName] = binding return nil diff --git a/internal/framework/pipeline/reference_resolution_test.go b/internal/framework/pipeline/reference_resolution_test.go index 92ae7310..bcc96e09 100644 --- a/internal/framework/pipeline/reference_resolution_test.go +++ b/internal/framework/pipeline/reference_resolution_test.go @@ -1,8 +1,11 @@ package pipeline import ( + "encoding/json" "strings" "testing" + + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" ) func TestReferenceSourceMapsConflictPreservesTrimmedSourceKinds(t *testing.T) { @@ -35,6 +38,53 @@ func TestReferenceSourceMapsConflictPreservesTrimmedSourceKinds(t *testing.T) { } } +func TestReferenceCollectionResolutionAndCopy(t *testing.T) { + pipelineSource := ReferenceSource{Path: "base.yml", Collections: []string{"base"}} + localSource := ReferenceSource{Path: "local.yml", Collections: []string{}} + target := referenceResolutionTarget{ + PipelineID: "demo", Stage: StageExtract, Module: "extractor", + Slots: []contracts.ReferenceSlot{{Name: "glossary"}}, + PipelineReferences: map[string]ReferenceSource{"glossary": pipelineSource}, + LocalReferences: map[string]ReferenceSource{"glossary": localSource}, + } + bindings, err := resolveReferenceTargetBindings(target) + if err != nil || len(bindings) != 1 || bindings[0].Source != "local.yml" || bindings[0].Collections == nil || len(bindings[0].Collections) != 0 { + t.Fatalf("local binding = %#v, %v", bindings, err) + } + target.LocalReferences["glossary"] = ReferenceSource{Path: "other.yml"} + bindings, err = resolveReferenceTargetBindings(target) + if err != nil || bindings[0].Collections != nil { + t.Fatalf("replacement inherited collections: %#v, %v", bindings, err) + } + target.LocalReferences = nil + bindings, err = resolveReferenceTargetBindings(target) + if err != nil { + t.Fatal(err) + } + bindings[0].Collections[0] = "changed" + if pipelineSource.Collections[0] != "base" { + t.Fatal("binding selection aliases input") + } + profile := ClonePipelineProfile(PipelineProfile{References: map[string]ReferenceSource{"glossary": pipelineSource, "empty": localSource}}) + profile.References["glossary"].Collections[0] = "changed" + if pipelineSource.Collections[0] != "base" || profile.References["empty"].Collections == nil { + t.Fatal("profile clone lost selection isolation or explicit emptiness") + } + withEmpty, err := json.Marshal(localSource) + if err != nil || !strings.Contains(string(withEmpty), `"collections":[]`) { + t.Fatalf("encoded reference = %s, %v", withEmpty, err) + } + for _, test := range []struct { + selection []string + wantEmpty bool + }{{nil, false}, {[]string{}, true}} { + encoded, err := json.Marshal(ReferenceBinding{SlotName: "glossary", Collections: test.selection}) + if err != nil || strings.Contains(string(encoded), `"collections":[]`) != test.wantEmpty { + t.Fatalf("encoded binding = %s, %v", encoded, err) + } + } +} + func TestNormalizeTargetReferenceSourceOwnsAndValidatesSelector(t *testing.T) { target := referenceResolutionTarget{PipelineID: "demo", StepID: "consumer", LaneID: "lane", Stage: StageExtract, Module: "extractor"} input := ReferenceSource{Artifact: &ArtifactReference{Step: " producer ", Lane: " output "}}