From d406aa6cd2f6b22ef6f4e38e8cbaddd82bc9ccb4 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Sun, 29 Nov 2026 17:59:01 +0000 Subject: [PATCH] Preserve selected collections in materialized references --- docs/roadmap/implementation.md | 2 + internal/cli/reference_contract_test.go | 45 +++++++++++++++++++ internal/framework/contracts/contracts.go | 8 ++++ .../framework/contracts/contracts_test.go | 28 ++++++++++++ .../pipeline/reference_resolution.go | 6 ++- .../pipeline/reference_resolution_test.go | 18 ++++++++ internal/framework/pipeline/references.go | 6 +++ .../framework/pipeline/references_test.go | 24 ++++++++++ internal/modules/dnd/chunk/scenes/chunker.go | 3 ++ .../dnd/extract/combatturns/extractor.go | 3 ++ .../dnd/extract/enemyevents/extractor.go | 3 ++ .../dnd/extract/itemoccurrences/extractor.go | 3 ++ .../dnd/extract/itemregistry/extractor.go | 3 ++ .../extract/locationoccurrences/extractor.go | 3 ++ .../dnd/extract/locationregistry/extractor.go | 3 ++ .../dnd/extract/npcoccurrences/extractor.go | 3 ++ .../dnd/extract/npcregistry/extractor.go | 3 ++ .../extract/scenedescriptions/extractor.go | 3 ++ .../modules/dnd/extract/spells/extractor.go | 3 ++ .../dnd/normalize/combatturns/normalizer.go | 3 ++ .../dnd/normalize/enemyevents/normalizer.go | 3 ++ .../normalize/itemoccurrences/normalizer.go | 3 ++ .../locationoccurrences/normalizer.go | 3 ++ .../normalize/npcoccurrences/normalizer.go | 3 ++ .../dnd/normalize/spells/normalizer.go | 3 ++ internal/modules/dnd/shared/references.go | 27 ++++++++++- .../modules/dnd/shared/references_test.go | 16 +++++++ .../enemyevents/invariants/validator.go | 3 ++ .../itemoccurrences/registry/validator.go | 4 ++ .../locationoccurrences/registry/validator.go | 4 ++ .../npcoccurrences/invariants/validator.go | 3 ++ .../npcoccurrences/registry/validator.go | 4 ++ .../dnd/validate/spells/catalog/validator.go | 4 ++ 33 files changed, 251 insertions(+), 2 deletions(-) diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index fbd84fef..10fc581b 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -248,6 +248,8 @@ from later wiring must be identified, not hidden with compatibility scaffolding. ## Stage 5 — Preserve Selections Through CLI And Materialization +**Status:** Complete. + **Goal:** Deliver selected input views consistently to consumers. **Inspect:** `internal/cli/reference_selectors.go`, framework reference resolution diff --git a/internal/cli/reference_contract_test.go b/internal/cli/reference_contract_test.go index b000680c..197e646b 100644 --- a/internal/cli/reference_contract_test.go +++ b/internal/cli/reference_contract_test.go @@ -86,6 +86,51 @@ func TestReferenceSelectorSpecificityAndFinalOccurrenceChooseConcreteBindings(t } } +func TestCLIReferenceOverridesRetainEachTargetSelection(t *testing.T) { + cfg := referenceContractConfig() + profile := cfg.Pipelines["demo"] + profile.References = map[string]pipeline.ReferenceSource{"shared": {Path: "base.yml", Collections: []string{"campaign"}}} + alpha := profile.Artifacts["alpha"] + alpha.Extract.References["shared"] = pipeline.ReferenceSource{Path: "local.yml", Collections: []string{}} + profile.Artifacts["alpha"] = alpha + cfg.Pipelines["demo"] = profile + catalog := referenceContractCatalog(t, true, true) + requests := []cliReferenceRequest{ + {Selector: mustParseReferenceSelector(t, "shared", "--reference"), Source: "broad.yml"}, + {Selector: mustParseReferenceSelector(t, "alpha.extract.shared", "--reference"), Source: "narrow.yml"}, + } + overrides, unbinds, err := resolveCLIReferenceRequests(cfg, "demo", nil, catalog, requests, nil) + if err != nil { + t.Fatal(err) + } + effective, err := cfg.Resolve(config.ResolveInput{PipelineID: "demo", Catalog: catalog, ReferenceOverrides: overrides, ReferenceUnbinds: unbinds}) + if err != nil { + t.Fatal(err) + } + count := 0 + for _, id := range []string{"alpha", "beta"} { + lane := referenceContractLane(t, effective.ResolvedPipeline, id) + for _, target := range []pipeline.ResolvedReferenceTarget{lane.ExtractReferences, lane.MergeReferences, lane.NormalizeReferences} { + for _, binding := range target.Bindings { + if binding.SlotName != "shared" { + continue + } + count++ + if id == "alpha" && target.Stage == pipeline.StageExtract { + if binding.Source != "narrow.yml" || binding.Collections == nil || len(binding.Collections) != 0 { + t.Fatalf("narrow binding = %#v", binding) + } + } else if binding.Source != "broad.yml" || !slices.Equal(binding.Collections, []string{"campaign"}) { + t.Fatalf("broad binding = %#v", binding) + } + } + } + } + if count != 6 { + t.Fatalf("resolved %d shared bindings, want 6", count) + } +} + func TestRequiredReferencesCanBeSuppliedOnlyByCLI(t *testing.T) { cfg := referenceContractConfig() profile := cfg.Pipelines["demo"] diff --git a/internal/framework/contracts/contracts.go b/internal/framework/contracts/contracts.go index a1cf674a..55908c4c 100644 --- a/internal/framework/contracts/contracts.go +++ b/internal/framework/contracts/contracts.go @@ -109,6 +109,7 @@ type ReferenceProducer struct { type ReferenceItem struct { SlotName string `json:"slot_name"` + Collections []string `json:"collections,omitempty"` MediaType string `json:"media_type,omitempty"` Content []byte `json:"-"` Digest string `json:"digest,omitempty"` @@ -122,6 +123,9 @@ type ReferenceItem struct { func CloneReferenceItem(item ReferenceItem) ReferenceItem { item.Content = append([]byte(nil), item.Content...) + if item.Collections != nil { + item.Collections = append([]string{}, item.Collections...) + } item.ArtifactSchema = CloneArtifactSchema(item.ArtifactSchema) return item } @@ -129,6 +133,7 @@ func CloneReferenceItem(item ReferenceItem) ReferenceItem { func (item ReferenceItem) MarshalJSON() ([]byte, error) { type referenceItemJSON struct { SlotName string `json:"slot_name"` + Collections *[]string `json:"collections,omitempty"` MediaType string `json:"media_type,omitempty"` Digest string `json:"digest,omitempty"` Origin ReferenceOrigin `json:"origin"` @@ -147,6 +152,9 @@ func (item ReferenceItem) MarshalJSON() ([]byte, error) { BindingSource: item.BindingSource, ArtifactKind: item.ArtifactKind, } + if item.Collections != nil { + encoded.Collections = &item.Collections + } if item.ArtifactSchema.ID != "" || item.ArtifactSchema.Name != "" || item.ArtifactSchema.Version != "" || len(item.ArtifactSchema.JSONSchema) > 0 { schema := CloneArtifactSchema(item.ArtifactSchema) encoded.ArtifactSchema = &schema diff --git a/internal/framework/contracts/contracts_test.go b/internal/framework/contracts/contracts_test.go index 6764caed..f5c6bc2b 100644 --- a/internal/framework/contracts/contracts_test.go +++ b/internal/framework/contracts/contracts_test.go @@ -123,6 +123,34 @@ func TestReferenceItemJSONOmitsContent(t *testing.T) { } } +func TestReferenceItemCollectionSelectionCopyAndJSON(t *testing.T) { + for _, selection := range [][]string{nil, {}, {"campaign"}} { + item := ReferenceItem{SlotName: "glossary", Collections: selection} + clone := CloneReferenceItem(item) + if (clone.Collections == nil) != (selection == nil) || len(clone.Collections) != len(selection) { + t.Fatalf("cloned selection = %#v, want %#v", clone.Collections, selection) + } + encoded, err := json.Marshal(item) + if err != nil { + t.Fatal(err) + } + var fields map[string]any + if err := json.Unmarshal(encoded, &fields); err != nil { + t.Fatal(err) + } + _, present := fields["collections"] + if present != (selection != nil) { + t.Fatalf("encoded selection = %s", encoded) + } + if len(selection) > 0 { + clone.Collections[0] = "changed" + if selection[0] != "campaign" { + t.Fatal("copy mutated original selection") + } + } + } +} + func TestLLMInputMaterialCopiesContentAndOmitsContentFromJSON(t *testing.T) { content := []byte("raw source bytes") material := NewLLMInputMaterial("transcript", "application/json", content, "sha256:source", "file:///tmp/source.json") diff --git a/internal/framework/pipeline/reference_resolution.go b/internal/framework/pipeline/reference_resolution.go index 68cfbc24..1ffb76d6 100644 --- a/internal/framework/pipeline/reference_resolution.go +++ b/internal/framework/pipeline/reference_resolution.go @@ -152,7 +152,11 @@ func resolveReferenceTargetBindings(target referenceResolutionTarget) ([]Referen if strings.TrimSpace(source) == "" { source = contracts.ReferenceBindingSourceCLI } - if err := addReferenceBinding(target, slotByName, bindings, override.SlotName, ExternalReference(override.Source), strings.TrimSpace(source)); err != nil { + replacement := ExternalReference(override.Source) + if previous, exists := bindings[strings.TrimSpace(override.SlotName)]; exists && previous.Artifact == nil && previous.Collections != nil { + replacement.Collections = append([]string{}, previous.Collections...) + } + if err := addReferenceBinding(target, slotByName, bindings, override.SlotName, replacement, strings.TrimSpace(source)); err != nil { return nil, err } } diff --git a/internal/framework/pipeline/reference_resolution_test.go b/internal/framework/pipeline/reference_resolution_test.go index bcc96e09..b6f9130f 100644 --- a/internal/framework/pipeline/reference_resolution_test.go +++ b/internal/framework/pipeline/reference_resolution_test.go @@ -85,6 +85,24 @@ func TestReferenceCollectionResolutionAndCopy(t *testing.T) { } } +func TestReferenceOverrideKeepsTargetCollectionSelection(t *testing.T) { + for _, selection := range [][]string{nil, {}, {"campaign"}} { + target := referenceResolutionTarget{ + PipelineID: "demo", LaneID: "lane", Stage: StageExtract, Module: "extractor", + Slots: []contracts.ReferenceSlot{{Name: "glossary"}}, + LocalReferences: map[string]ReferenceSource{"glossary": {Path: "old.yml", Collections: selection}}, + Options: ResolveOptions{ReferenceOverrides: []ReferenceBinding{{Stage: StageExtract, LaneID: "lane", SlotName: "glossary", Source: "new.yml"}}}, + } + bindings, err := resolveReferenceTargetBindings(target) + if err != nil || len(bindings) != 1 || bindings[0].Source != "new.yml" || (bindings[0].Collections == nil) != (selection == nil) || len(bindings[0].Collections) != len(selection) { + t.Fatalf("override with %#v = %#v, %v", selection, bindings, err) + } + if len(selection) > 0 && bindings[0].Collections[0] != selection[0] { + t.Fatalf("selection changed: %#v", bindings) + } + } +} + 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 "}} diff --git a/internal/framework/pipeline/references.go b/internal/framework/pipeline/references.go index cdf05974..821f680a 100644 --- a/internal/framework/pipeline/references.go +++ b/internal/framework/pipeline/references.go @@ -170,6 +170,9 @@ func materializeReferenceTarget( SizeBytes: int64(len(content)), BindingSource: strings.TrimSpace(binding.BindingSource), } + if binding.Collections != nil { + item.Collections = append([]string{}, binding.Collections...) + } set.Slots[slotName] = contracts.ResolvedReferenceSlot{ Slot: cloneReferenceSlot(slot), Items: []contracts.ReferenceItem{item}, @@ -369,6 +372,9 @@ func CloneReferenceTarget(in ResolvedReferenceTarget) ResolvedReferenceTarget { out.Bindings = make([]ReferenceBinding, len(in.Bindings)) for i, binding := range in.Bindings { out.Bindings[i] = binding + if binding.Collections != nil { + out.Bindings[i].Collections = append([]string{}, binding.Collections...) + } if binding.Artifact != nil { artifact := *binding.Artifact out.Bindings[i].Artifact = &artifact diff --git a/internal/framework/pipeline/references_test.go b/internal/framework/pipeline/references_test.go index de168a49..f9672ea6 100644 --- a/internal/framework/pipeline/references_test.go +++ b/internal/framework/pipeline/references_test.go @@ -344,6 +344,30 @@ func TestMaterializeReferencesAcceptsDeclaredJSONMediaType(t *testing.T) { } } +func TestMaterializeReferencesCarriesCollectionsWithoutChangingContent(t *testing.T) { + dir := t.TempDir() + content := []byte("collections: {campaign: {entries: []}}\n") + writeReferenceFile(t, filepath.Join(dir, "terms.yml"), content) + slot := contracts.ReferenceSlot{Name: "glossary", AcceptedMediaTypes: []string{"application/yaml"}} + resolved := resolvedPipelineWithReference(t, "glossary", "terms.yml", contracts.ReferenceBindingSourceConfig, slot) + resolved.Steps[0].ArtifactLanes[0].ExtractReferences.Bindings[0].Collections = []string{"campaign"} + materialized, _, err := MaterializeReferences(resolved, referenceCatalog(t, []contracts.ReferenceSlot{slot}), ReferenceMaterializationOptions{ConfigPath: filepath.Join(dir, "config.yml")}) + if err != nil { + t.Fatal(err) + } + target := materialized.Steps[0].ArtifactLanes[0].ExtractReferences + item := target.ReferenceSet.Slots["glossary"].Items[0] + if string(item.Content) != string(content) || item.Digest != referenceDigest(content) || len(item.Collections) != 1 || item.Collections[0] != "campaign" { + t.Fatalf("materialized item = %#v", item) + } + clone := CloneReferenceTarget(target) + clone.Bindings[0].Collections[0] = "changed" + clone.ReferenceSet.Slots["glossary"].Items[0].Collections[0] = "changed" + if target.Bindings[0].Collections[0] != "campaign" || item.Collections[0] != "campaign" { + t.Fatal("cloned selections alias original") + } +} + func TestMaterializeReferencesAcceptsDeclaredYAMLMediaType(t *testing.T) { configDir := t.TempDir() path := filepath.Join(configDir, "roster.yaml") diff --git a/internal/modules/dnd/chunk/scenes/chunker.go b/internal/modules/dnd/chunk/scenes/chunker.go index 02ca5db7..26d0a1c5 100644 --- a/internal/modules/dnd/chunk/scenes/chunker.go +++ b/internal/modules/dnd/chunk/scenes/chunker.go @@ -118,6 +118,9 @@ func Register(registry *pipeline.ChunkerRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options) }) } diff --git a/internal/modules/dnd/extract/combatturns/extractor.go b/internal/modules/dnd/extract/combatturns/extractor.go index eb8cbdb9..f54aac3f 100644 --- a/internal/modules/dnd/extract/combatturns/extractor.go +++ b/internal/modules/dnd/extract/combatturns/extractor.go @@ -186,6 +186,9 @@ func Register(registry *pipeline.ExtractorRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) } diff --git a/internal/modules/dnd/extract/enemyevents/extractor.go b/internal/modules/dnd/extract/enemyevents/extractor.go index 4913f421..0e1541fa 100644 --- a/internal/modules/dnd/extract/enemyevents/extractor.go +++ b/internal/modules/dnd/extract/enemyevents/extractor.go @@ -152,6 +152,9 @@ func Register(registry *pipeline.ExtractorRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) } diff --git a/internal/modules/dnd/extract/itemoccurrences/extractor.go b/internal/modules/dnd/extract/itemoccurrences/extractor.go index 3057ef17..9fa07b60 100644 --- a/internal/modules/dnd/extract/itemoccurrences/extractor.go +++ b/internal/modules/dnd/extract/itemoccurrences/extractor.go @@ -146,6 +146,9 @@ func Register(registry *pipeline.ExtractorRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) } diff --git a/internal/modules/dnd/extract/itemregistry/extractor.go b/internal/modules/dnd/extract/itemregistry/extractor.go index ea33b3ef..4357f101 100644 --- a/internal/modules/dnd/extract/itemregistry/extractor.go +++ b/internal/modules/dnd/extract/itemregistry/extractor.go @@ -98,6 +98,9 @@ func Register(registry *pipeline.ExtractorRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) } diff --git a/internal/modules/dnd/extract/locationoccurrences/extractor.go b/internal/modules/dnd/extract/locationoccurrences/extractor.go index 76e08644..a22cba4a 100644 --- a/internal/modules/dnd/extract/locationoccurrences/extractor.go +++ b/internal/modules/dnd/extract/locationoccurrences/extractor.go @@ -146,6 +146,9 @@ func Register(registry *pipeline.ExtractorRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) } diff --git a/internal/modules/dnd/extract/locationregistry/extractor.go b/internal/modules/dnd/extract/locationregistry/extractor.go index c8cc7e2d..32968bc6 100644 --- a/internal/modules/dnd/extract/locationregistry/extractor.go +++ b/internal/modules/dnd/extract/locationregistry/extractor.go @@ -98,6 +98,9 @@ func Register(registry *pipeline.ExtractorRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) } diff --git a/internal/modules/dnd/extract/npcoccurrences/extractor.go b/internal/modules/dnd/extract/npcoccurrences/extractor.go index eac5e7d1..e3ab7b54 100644 --- a/internal/modules/dnd/extract/npcoccurrences/extractor.go +++ b/internal/modules/dnd/extract/npcoccurrences/extractor.go @@ -145,6 +145,9 @@ func Register(registry *pipeline.ExtractorRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) } diff --git a/internal/modules/dnd/extract/npcregistry/extractor.go b/internal/modules/dnd/extract/npcregistry/extractor.go index d78c5d9c..4fa77853 100644 --- a/internal/modules/dnd/extract/npcregistry/extractor.go +++ b/internal/modules/dnd/extract/npcregistry/extractor.go @@ -108,6 +108,9 @@ func Register(registry *pipeline.ExtractorRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) } diff --git a/internal/modules/dnd/extract/scenedescriptions/extractor.go b/internal/modules/dnd/extract/scenedescriptions/extractor.go index 21950dbb..c12d5062 100644 --- a/internal/modules/dnd/extract/scenedescriptions/extractor.go +++ b/internal/modules/dnd/extract/scenedescriptions/extractor.go @@ -140,6 +140,9 @@ func Register(registry *pipeline.ExtractorRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) } diff --git a/internal/modules/dnd/extract/spells/extractor.go b/internal/modules/dnd/extract/spells/extractor.go index 684d5b06..5a99ca3f 100644 --- a/internal/modules/dnd/extract/spells/extractor.go +++ b/internal/modules/dnd/extract/spells/extractor.go @@ -161,6 +161,9 @@ func Register(registry *pipeline.ExtractorRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) } diff --git a/internal/modules/dnd/normalize/combatturns/normalizer.go b/internal/modules/dnd/normalize/combatturns/normalizer.go index 4d308cbc..7c7fb3b2 100644 --- a/internal/modules/dnd/normalize/combatturns/normalizer.go +++ b/internal/modules/dnd/normalize/combatturns/normalizer.go @@ -298,6 +298,9 @@ func Register(registry *pipeline.NormalizerRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) } diff --git a/internal/modules/dnd/normalize/enemyevents/normalizer.go b/internal/modules/dnd/normalize/enemyevents/normalizer.go index 82e1804e..4c63eb3a 100644 --- a/internal/modules/dnd/normalize/enemyevents/normalizer.go +++ b/internal/modules/dnd/normalize/enemyevents/normalizer.go @@ -262,6 +262,9 @@ func Register(registry *pipeline.NormalizerRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) } diff --git a/internal/modules/dnd/normalize/itemoccurrences/normalizer.go b/internal/modules/dnd/normalize/itemoccurrences/normalizer.go index a5ca2e80..2cb862f5 100644 --- a/internal/modules/dnd/normalize/itemoccurrences/normalizer.go +++ b/internal/modules/dnd/normalize/itemoccurrences/normalizer.go @@ -269,6 +269,9 @@ func Register(registry *pipeline.NormalizerRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) } diff --git a/internal/modules/dnd/normalize/locationoccurrences/normalizer.go b/internal/modules/dnd/normalize/locationoccurrences/normalizer.go index 1faa1f4e..8724a5a9 100644 --- a/internal/modules/dnd/normalize/locationoccurrences/normalizer.go +++ b/internal/modules/dnd/normalize/locationoccurrences/normalizer.go @@ -320,6 +320,9 @@ func Register(registry *pipeline.NormalizerRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) } diff --git a/internal/modules/dnd/normalize/npcoccurrences/normalizer.go b/internal/modules/dnd/normalize/npcoccurrences/normalizer.go index ecb946b9..7e2d585c 100644 --- a/internal/modules/dnd/normalize/npcoccurrences/normalizer.go +++ b/internal/modules/dnd/normalize/npcoccurrences/normalizer.go @@ -239,6 +239,9 @@ func Register(registry *pipeline.NormalizerRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) } diff --git a/internal/modules/dnd/normalize/spells/normalizer.go b/internal/modules/dnd/normalize/spells/normalizer.go index 280b57be..f09311a2 100644 --- a/internal/modules/dnd/normalize/spells/normalizer.go +++ b/internal/modules/dnd/normalize/spells/normalizer.go @@ -256,6 +256,9 @@ func Register(registry *pipeline.NormalizerRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) } diff --git a/internal/modules/dnd/shared/references.go b/internal/modules/dnd/shared/references.go index d9388176..ea6e04b3 100644 --- a/internal/modules/dnd/shared/references.go +++ b/internal/modules/dnd/shared/references.go @@ -1,6 +1,31 @@ package shared -import "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" +import ( + "fmt" + "sort" + + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" +) + +// ValidateReferenceSelections rejects collection metadata on non-glossary inputs. +func ValidateReferenceSelections(references contracts.ReferenceSet) error { + names := make([]string, 0, len(references.Slots)) + for name := range references.Slots { + names = append(names, name) + } + sort.Strings(names) + for _, name := range names { + if name == "glossary" || name == "spell_catalog" { + continue + } + for _, item := range references.Slots[name].Items { + if item.Collections != nil { + return fmt.Errorf("reference slot %q does not support collection selection", name) + } + } + } + return nil +} type ReferenceSlotDescriptions struct { Glossary string diff --git a/internal/modules/dnd/shared/references_test.go b/internal/modules/dnd/shared/references_test.go index 5a080a1a..1b1cd9ac 100644 --- a/internal/modules/dnd/shared/references_test.go +++ b/internal/modules/dnd/shared/references_test.go @@ -2,11 +2,27 @@ package shared import ( "reflect" + "strings" "testing" "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" ) +func TestValidateReferenceSelections(t *testing.T) { + for _, slot := range []string{"party", "roster", "players", "npc_registry"} { + references := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{slot: {Items: []contracts.ReferenceItem{{Collections: []string{}}}}}} + if err := ValidateReferenceSelections(references); err == nil || !strings.Contains(err.Error(), slot) { + t.Fatalf("slot %s: error = %v", slot, err) + } + } + for _, slot := range []string{"glossary", "spell_catalog"} { + references := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{slot: {Items: []contracts.ReferenceItem{{Collections: []string{"chosen"}}}}}} + if err := ValidateReferenceSelections(references); err != nil { + t.Fatalf("slot %s: %v", slot, err) + } + } +} + func TestReferenceMediaTypesReturnsDefensiveCopy(t *testing.T) { want := []string{ "application/json", diff --git a/internal/modules/dnd/validate/enemyevents/invariants/validator.go b/internal/modules/dnd/validate/enemyevents/invariants/validator.go index 206b758b..f07e7901 100644 --- a/internal/modules/dnd/validate/enemyevents/invariants/validator.go +++ b/internal/modules/dnd/validate/enemyevents/invariants/validator.go @@ -151,6 +151,9 @@ func Register(registry *pipeline.ValidatorRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) } diff --git a/internal/modules/dnd/validate/itemoccurrences/registry/validator.go b/internal/modules/dnd/validate/itemoccurrences/registry/validator.go index 6e16e847..bd98cbff 100644 --- a/internal/modules/dnd/validate/itemoccurrences/registry/validator.go +++ b/internal/modules/dnd/validate/itemoccurrences/registry/validator.go @@ -9,6 +9,7 @@ import ( "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd" itemregistry "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/items/registry" + "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics" occurrenceshape "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/itemoccurrences/shape" ) @@ -92,6 +93,9 @@ func Register(registry *pipeline.ValidatorRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) } diff --git a/internal/modules/dnd/validate/locationoccurrences/registry/validator.go b/internal/modules/dnd/validate/locationoccurrences/registry/validator.go index e10bd47b..63355675 100644 --- a/internal/modules/dnd/validate/locationoccurrences/registry/validator.go +++ b/internal/modules/dnd/validate/locationoccurrences/registry/validator.go @@ -9,6 +9,7 @@ import ( "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd" locationregistry "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/locations/registry" + "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics" occurrenceshape "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/locationoccurrences/shape" ) @@ -93,6 +94,9 @@ func Register(registry *pipeline.ValidatorRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) } diff --git a/internal/modules/dnd/validate/npcoccurrences/invariants/validator.go b/internal/modules/dnd/validate/npcoccurrences/invariants/validator.go index b7144b3a..69678dfc 100644 --- a/internal/modules/dnd/validate/npcoccurrences/invariants/validator.go +++ b/internal/modules/dnd/validate/npcoccurrences/invariants/validator.go @@ -137,6 +137,9 @@ func Register(registry *pipeline.ValidatorRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) } diff --git a/internal/modules/dnd/validate/npcoccurrences/registry/validator.go b/internal/modules/dnd/validate/npcoccurrences/registry/validator.go index 2bc97f36..eb856147 100644 --- a/internal/modules/dnd/validate/npcoccurrences/registry/validator.go +++ b/internal/modules/dnd/validate/npcoccurrences/registry/validator.go @@ -9,6 +9,7 @@ import ( "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd" npcregistry "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/npcs/registry" + "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics" occurrenceshape "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/npcoccurrences/shape" ) @@ -102,6 +103,9 @@ func Register(registry *pipeline.ValidatorRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) } diff --git a/internal/modules/dnd/validate/spells/catalog/validator.go b/internal/modules/dnd/validate/spells/catalog/validator.go index 0ba26da2..91a4b7c7 100644 --- a/internal/modules/dnd/validate/spells/catalog/validator.go +++ b/internal/modules/dnd/validate/spells/catalog/validator.go @@ -9,6 +9,7 @@ import ( "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd" + "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics" spellcatalog "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/spells/catalog" spellshape "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/spells/shape" @@ -105,6 +106,9 @@ func Register(registry *pipeline.ValidatorRegistry) error { if err != nil { return nil, err } + if err := shared.ValidateReferenceSelections(request.References); err != nil { + return nil, err + } return New(options, request.References) }) }