From 2ca73aa3ee34e27d450a0e610218cddd4e8c93b0 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Sat, 28 Nov 2026 04:11:33 +0000 Subject: [PATCH] Discover selected pipeline model targets --- docs/roadmap/implementation.md | 2 + internal/framework/pipeline/module.go | 6 +- internal/modules/dnd/chunk/scenes/chunker.go | 2 +- .../modules/dnd/chunk/scenes/chunker_test.go | 2 +- .../dnd/extract/combatturns/extractor.go | 2 +- .../dnd/extract/enemyevents/extractor.go | 2 +- .../dnd/extract/itemoccurrences/extractor.go | 2 +- .../dnd/extract/itemregistry/extractor.go | 2 +- .../dnd/extract/itemregistry/registry_test.go | 2 +- .../extract/locationoccurrences/extractor.go | 2 +- .../dnd/extract/locationregistry/extractor.go | 2 +- .../extract/locationregistry/registry_test.go | 2 +- .../dnd/extract/npcoccurrences/extractor.go | 2 +- .../dnd/extract/npcregistry/extractor.go | 2 +- .../dnd/extract/npcregistry/registry_test.go | 8 +- .../extract/scenedescriptions/extractor.go | 2 +- .../scenedescriptions/registry_test.go | 2 +- .../modules/dnd/extract/spells/extractor.go | 2 +- .../dnd/extract/spells/registry_test.go | 2 +- .../dnd/normalize/itemregistry/normalizer.go | 2 +- .../normalize/itemregistry/normalizer_test.go | 2 +- .../normalize/locationregistry/normalizer.go | 2 +- .../locationregistry/normalizer_test.go | 2 +- .../dnd/normalize/npcregistry/normalizer.go | 2 +- .../normalize/npcregistry/normalizer_test.go | 2 +- internal/preflight/targets.go | 156 ++++++++++++++++++ internal/preflight/targets_test.go | 78 +++++++++ 27 files changed, 267 insertions(+), 27 deletions(-) create mode 100644 internal/preflight/targets.go create mode 100644 internal/preflight/targets_test.go diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index ffcbb259..df49835e 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -161,6 +161,8 @@ limit domain changes to checks needed by the accepted local-preparation contract ## Stage 3: Discover Selected Initial Targets Without Executing Work +**Status:** Complete. + **Goal:** Produce scoped, deduplicated profile/backend selections for preflight. **Inspect:** `ModuleSpec`, `ValidatorSpec`, resolved pipeline/validator structures, diff --git a/internal/framework/pipeline/module.go b/internal/framework/pipeline/module.go index 79f4afd8..fad965b8 100644 --- a/internal/framework/pipeline/module.go +++ b/internal/framework/pipeline/module.go @@ -26,7 +26,9 @@ type ModuleSpec struct { ExecutionClass contracts.ExecutionClass // Prompts lists statically selected initial/correction IDs shared with the // module's request builders. Dynamic selections are checked at execution. - Prompts []string + Prompts []string + // InitialPrompts identifies requests that independently select a target. + InitialPrompts []string ArtifactKind contracts.ArtifactKind Provides []string Requires []string @@ -40,6 +42,7 @@ func normalizeModuleSpec(spec ModuleSpec) ModuleSpec { Stage: spec.Stage, ExecutionClass: executionClass, Prompts: normalizeStringSet(spec.Prompts), + InitialPrompts: normalizeStringSet(spec.InitialPrompts), ArtifactKind: normalizeArtifactKind(spec.ArtifactKind), Provides: normalizeCapabilities(spec.Provides), Requires: normalizeCapabilities(spec.Requires), @@ -78,6 +81,7 @@ func cloneModuleSpec(spec ModuleSpec) ModuleSpec { Stage: spec.Stage, ExecutionClass: spec.ExecutionClass, Prompts: append([]string(nil), spec.Prompts...), + InitialPrompts: append([]string(nil), spec.InitialPrompts...), ArtifactKind: spec.ArtifactKind, Provides: append([]string(nil), spec.Provides...), Requires: append([]string(nil), spec.Requires...), diff --git a/internal/modules/dnd/chunk/scenes/chunker.go b/internal/modules/dnd/chunk/scenes/chunker.go index c314910d..02ca5db7 100644 --- a/internal/modules/dnd/chunk/scenes/chunker.go +++ b/internal/modules/dnd/chunk/scenes/chunker.go @@ -105,7 +105,7 @@ func ModuleSpec() pipeline.ModuleSpec { Key: Key, Stage: pipeline.StageChunk, ExecutionClass: contracts.ExecutionClassLLMBacked, - Prompts: []string{PromptID, CorrectionPromptID}, + Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: append([]string(nil), requiredCapabilities...), Provides: append([]string(nil), providedCapabilities...), ReferenceSlots: shared.ReferenceSlots(referenceSlotDescriptions), diff --git a/internal/modules/dnd/chunk/scenes/chunker_test.go b/internal/modules/dnd/chunk/scenes/chunker_test.go index 0a9e466f..5df3b5ef 100644 --- a/internal/modules/dnd/chunk/scenes/chunker_test.go +++ b/internal/modules/dnd/chunk/scenes/chunker_test.go @@ -28,7 +28,7 @@ func TestNewModuleSpecAndRegister(t *testing.T) { Key: Key, Stage: pipeline.StageChunk, ExecutionClass: contracts.ExecutionClassLLMBacked, - Prompts: []string{PromptID, CorrectionPromptID}, + Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: []string{"source.transcript"}, Provides: []string{"chunks"}, ReferenceSlots: wantReferenceSlots(), diff --git a/internal/modules/dnd/extract/combatturns/extractor.go b/internal/modules/dnd/extract/combatturns/extractor.go index 570c3c9c..eb8cbdb9 100644 --- a/internal/modules/dnd/extract/combatturns/extractor.go +++ b/internal/modules/dnd/extract/combatturns/extractor.go @@ -172,7 +172,7 @@ func ModuleSpec() pipeline.ModuleSpec { Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, - Prompts: []string{PromptID, CorrectionPromptID}, + Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: append([]string(nil), requiredCapabilities...), Provides: append([]string(nil), providedCapabilities...), ArtifactKind: dnd.CombatTurnListKind, diff --git a/internal/modules/dnd/extract/enemyevents/extractor.go b/internal/modules/dnd/extract/enemyevents/extractor.go index ef305135..4913f421 100644 --- a/internal/modules/dnd/extract/enemyevents/extractor.go +++ b/internal/modules/dnd/extract/enemyevents/extractor.go @@ -138,7 +138,7 @@ func ModuleSpec() pipeline.ModuleSpec { Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, - Prompts: []string{PromptID, CorrectionPromptID}, + Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: append([]string(nil), requiredCapabilities...), Provides: append([]string(nil), providedCapabilities...), ArtifactKind: dnd.EnemyEventListKind, diff --git a/internal/modules/dnd/extract/itemoccurrences/extractor.go b/internal/modules/dnd/extract/itemoccurrences/extractor.go index 69245068..3057ef17 100644 --- a/internal/modules/dnd/extract/itemoccurrences/extractor.go +++ b/internal/modules/dnd/extract/itemoccurrences/extractor.go @@ -132,7 +132,7 @@ func ModuleSpec() pipeline.ModuleSpec { Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, - Prompts: []string{PromptID, CorrectionPromptID}, + Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: append([]string(nil), requiredCapabilities...), Provides: append([]string(nil), providedCapabilities...), ArtifactKind: dnd.ItemOccurrenceListKind, diff --git a/internal/modules/dnd/extract/itemregistry/extractor.go b/internal/modules/dnd/extract/itemregistry/extractor.go index b4402857..ea33b3ef 100644 --- a/internal/modules/dnd/extract/itemregistry/extractor.go +++ b/internal/modules/dnd/extract/itemregistry/extractor.go @@ -86,7 +86,7 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract func ModuleSpec() pipeline.ModuleSpec { return pipeline.ModuleSpec{ Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, - Prompts: []string{PromptID, CorrectionPromptID}, + Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: append([]string(nil), requiredCapabilities...), Provides: append([]string(nil), providedCapabilities...), ArtifactKind: dnd.ItemRegistryKind, ReferenceSlots: referenceSlots(), } diff --git a/internal/modules/dnd/extract/itemregistry/registry_test.go b/internal/modules/dnd/extract/itemregistry/registry_test.go index b5df66df..2ebd85e9 100644 --- a/internal/modules/dnd/extract/itemregistry/registry_test.go +++ b/internal/modules/dnd/extract/itemregistry/registry_test.go @@ -14,7 +14,7 @@ func TestModuleRegistrationAndNativeDependencies(t *testing.T) { if _, err := New(Options{}, contracts.ReferenceSet{}, contracts.ReferenceSet{}); err == nil || !strings.Contains(err.Error(), "at most one") { t.Fatalf("New() error = %v, want reference-set rejection", err) } - want := pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, Requires: []string{"chunks", "source.transcript"}, Provides: []string{"dnd.item_registry"}, ArtifactKind: dnd.ItemRegistryKind, ReferenceSlots: referenceSlots()} + want := pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: []string{"chunks", "source.transcript"}, Provides: []string{"dnd.item_registry"}, ArtifactKind: dnd.ItemRegistryKind, ReferenceSlots: referenceSlots()} if got := ModuleSpec(); !reflect.DeepEqual(got, want) { t.Fatalf("ModuleSpec() = %#v, want %#v", got, want) } diff --git a/internal/modules/dnd/extract/locationoccurrences/extractor.go b/internal/modules/dnd/extract/locationoccurrences/extractor.go index c7db9468..76e08644 100644 --- a/internal/modules/dnd/extract/locationoccurrences/extractor.go +++ b/internal/modules/dnd/extract/locationoccurrences/extractor.go @@ -134,7 +134,7 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract func ModuleSpec() pipeline.ModuleSpec { return pipeline.ModuleSpec{ Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, - Prompts: []string{PromptID, CorrectionPromptID}, + Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: append([]string(nil), requiredCapabilities...), Provides: append([]string(nil), providedCapabilities...), ArtifactKind: dnd.LocationOccurrenceListKind, ReferenceSlots: referenceSlots(), } diff --git a/internal/modules/dnd/extract/locationregistry/extractor.go b/internal/modules/dnd/extract/locationregistry/extractor.go index 6f1c9e3a..c8cc7e2d 100644 --- a/internal/modules/dnd/extract/locationregistry/extractor.go +++ b/internal/modules/dnd/extract/locationregistry/extractor.go @@ -86,7 +86,7 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract func ModuleSpec() pipeline.ModuleSpec { return pipeline.ModuleSpec{ Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, - Prompts: []string{PromptID, CorrectionPromptID}, + Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: append([]string(nil), requiredCapabilities...), Provides: append([]string(nil), providedCapabilities...), ArtifactKind: dnd.LocationRegistryKind, ReferenceSlots: referenceSlots(), } diff --git a/internal/modules/dnd/extract/locationregistry/registry_test.go b/internal/modules/dnd/extract/locationregistry/registry_test.go index 5409a35c..df0b820d 100644 --- a/internal/modules/dnd/extract/locationregistry/registry_test.go +++ b/internal/modules/dnd/extract/locationregistry/registry_test.go @@ -14,7 +14,7 @@ func TestModuleRegistrationAndNativeDependencies(t *testing.T) { if _, err := New(Options{}, contracts.ReferenceSet{}, contracts.ReferenceSet{}); err == nil || !strings.Contains(err.Error(), "at most one") { t.Fatalf("New() error = %v, want reference-set rejection", err) } - want := pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, Requires: []string{"chunks", "source.transcript"}, Provides: []string{"dnd.location_registry"}, ArtifactKind: dnd.LocationRegistryKind, ReferenceSlots: referenceSlots()} + want := pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: []string{"chunks", "source.transcript"}, Provides: []string{"dnd.location_registry"}, ArtifactKind: dnd.LocationRegistryKind, ReferenceSlots: referenceSlots()} if got := ModuleSpec(); !reflect.DeepEqual(got, want) { t.Fatalf("ModuleSpec() = %#v, want %#v", got, want) } diff --git a/internal/modules/dnd/extract/npcoccurrences/extractor.go b/internal/modules/dnd/extract/npcoccurrences/extractor.go index d7325139..eac5e7d1 100644 --- a/internal/modules/dnd/extract/npcoccurrences/extractor.go +++ b/internal/modules/dnd/extract/npcoccurrences/extractor.go @@ -131,7 +131,7 @@ func ModuleSpec() pipeline.ModuleSpec { Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, - Prompts: []string{PromptID, CorrectionPromptID}, + Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: append([]string(nil), requiredCapabilities...), Provides: append([]string(nil), providedCapabilities...), ArtifactKind: dnd.NPCOccurrenceListKind, diff --git a/internal/modules/dnd/extract/npcregistry/extractor.go b/internal/modules/dnd/extract/npcregistry/extractor.go index f302ee04..d78c5d9c 100644 --- a/internal/modules/dnd/extract/npcregistry/extractor.go +++ b/internal/modules/dnd/extract/npcregistry/extractor.go @@ -94,7 +94,7 @@ func ModuleSpec() pipeline.ModuleSpec { Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, - Prompts: []string{PromptID, CorrectionPromptID}, + Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: append([]string(nil), requiredCapabilities...), Provides: append([]string(nil), providedCapabilities...), ArtifactKind: dnd.NPCRegistryKind, diff --git a/internal/modules/dnd/extract/npcregistry/registry_test.go b/internal/modules/dnd/extract/npcregistry/registry_test.go index 86072c2e..d129ae54 100644 --- a/internal/modules/dnd/extract/npcregistry/registry_test.go +++ b/internal/modules/dnd/extract/npcregistry/registry_test.go @@ -25,10 +25,10 @@ func TestModuleSpecAndReferenceSlots(t *testing.T) { Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, - Prompts: []string{PromptID, CorrectionPromptID}, - Requires: []string{"chunks", "source.transcript"}, - Provides: []string{"dnd.npc_registry"}, - ArtifactKind: dnd.NPCRegistryKind, + Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, + Requires: []string{"chunks", "source.transcript"}, + Provides: []string{"dnd.npc_registry"}, + ArtifactKind: dnd.NPCRegistryKind, ReferenceSlots: []contracts.ReferenceSlot{ {Name: "glossary", Description: "Optional campaign glossary reference material used only for NPC disambiguation.", AcceptedMediaTypes: []string{"application/json", "application/x-yaml", "application/yaml", "text/markdown", "text/plain"}}, {Name: "party", Description: "Optional party roster reference material used only for NPC disambiguation.", AcceptedMediaTypes: []string{"application/json", "application/x-yaml", "application/yaml", "text/markdown", "text/plain"}}, diff --git a/internal/modules/dnd/extract/scenedescriptions/extractor.go b/internal/modules/dnd/extract/scenedescriptions/extractor.go index b05b86af..21950dbb 100644 --- a/internal/modules/dnd/extract/scenedescriptions/extractor.go +++ b/internal/modules/dnd/extract/scenedescriptions/extractor.go @@ -126,7 +126,7 @@ func ModuleSpec() pipeline.ModuleSpec { Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, - Prompts: []string{PromptID, CorrectionPromptID}, + Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: append([]string(nil), requiredCapabilities...), Provides: append([]string(nil), providedCapabilities...), ArtifactKind: dnd.SceneDescriptionListKind, diff --git a/internal/modules/dnd/extract/scenedescriptions/registry_test.go b/internal/modules/dnd/extract/scenedescriptions/registry_test.go index a36cce2f..c2badb28 100644 --- a/internal/modules/dnd/extract/scenedescriptions/registry_test.go +++ b/internal/modules/dnd/extract/scenedescriptions/registry_test.go @@ -21,7 +21,7 @@ func TestNewRejectsAmbiguousReferences(t *testing.T) { func TestModuleSpecAndReferenceSlots(t *testing.T) { want := pipeline.ModuleSpec{ - Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, Requires: []string{"chunks", "source.transcript"}, Provides: []string{"dnd.scene_descriptions"}, ArtifactKind: dnd.SceneDescriptionListKind, + Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: []string{"chunks", "source.transcript"}, Provides: []string{"dnd.scene_descriptions"}, ArtifactKind: dnd.SceneDescriptionListKind, ReferenceSlots: []contracts.ReferenceSlot{ {Name: "glossary", Description: "Optional campaign glossary reference material used only to disambiguate scene descriptions.", AcceptedMediaTypes: []string{"application/json", "application/x-yaml", "application/yaml", "text/markdown", "text/plain"}}, {Name: "party", Description: "Optional party roster reference material used only to disambiguate scene descriptions.", AcceptedMediaTypes: []string{"application/json", "application/x-yaml", "application/yaml", "text/markdown", "text/plain"}}, diff --git a/internal/modules/dnd/extract/spells/extractor.go b/internal/modules/dnd/extract/spells/extractor.go index 71ea5464..684d5b06 100644 --- a/internal/modules/dnd/extract/spells/extractor.go +++ b/internal/modules/dnd/extract/spells/extractor.go @@ -147,7 +147,7 @@ func ModuleSpec() pipeline.ModuleSpec { Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, - Prompts: []string{PromptID, CorrectionPromptID}, + Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: append([]string(nil), requiredCapabilities...), Provides: append([]string(nil), providedCapabilities...), ArtifactKind: dnd.SpellListKind, diff --git a/internal/modules/dnd/extract/spells/registry_test.go b/internal/modules/dnd/extract/spells/registry_test.go index 6aa77ec9..7b3d3b47 100644 --- a/internal/modules/dnd/extract/spells/registry_test.go +++ b/internal/modules/dnd/extract/spells/registry_test.go @@ -23,7 +23,7 @@ func TestModuleSpec(t *testing.T) { Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, - Prompts: []string{PromptID, CorrectionPromptID}, + Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: []string{ "chunks", "source.transcript", diff --git a/internal/modules/dnd/normalize/itemregistry/normalizer.go b/internal/modules/dnd/normalize/itemregistry/normalizer.go index 2de6a87c..8de157b8 100644 --- a/internal/modules/dnd/normalize/itemregistry/normalizer.go +++ b/internal/modules/dnd/normalize/itemregistry/normalizer.go @@ -312,7 +312,7 @@ func duplicateFinding(retainedIndex int, removed []int) diagnostics.Finding { func itemScope(index int) string { return fmt.Sprintf("items[%d]", index) } func ModuleSpec() pipeline.ModuleSpec { - return pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageNormalize, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, Requires: append([]string(nil), requiredCapabilities...), Provides: append([]string(nil), providedCapabilities...), ArtifactKind: dnd.ItemRegistryKind} + return pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageNormalize, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: append([]string(nil), requiredCapabilities...), Provides: append([]string(nil), providedCapabilities...), ArtifactKind: dnd.ItemRegistryKind} } func Register(registry *pipeline.NormalizerRegistry) error { return pipeline.RegisterNormalizerBuilder(registry, ModuleSpec(), validateOptions, func(request pipeline.BuildRequest) (contracts.Normalizer[dnd.ItemRegistry], error) { diff --git a/internal/modules/dnd/normalize/itemregistry/normalizer_test.go b/internal/modules/dnd/normalize/itemregistry/normalizer_test.go index 9dcaaa94..4001ea12 100644 --- a/internal/modules/dnd/normalize/itemregistry/normalizer_test.go +++ b/internal/modules/dnd/normalize/itemregistry/normalizer_test.go @@ -22,7 +22,7 @@ import ( ) func TestModuleContractAndNativeDependencies(t *testing.T) { - want := pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageNormalize, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, Requires: []string{"merged"}, Provides: []string{"normalized"}, ArtifactKind: dnd.ItemRegistryKind} + want := pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageNormalize, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: []string{"merged"}, Provides: []string{"normalized"}, ArtifactKind: dnd.ItemRegistryKind} if got := ModuleSpec(); !reflect.DeepEqual(got, want) { t.Fatalf("ModuleSpec() = %#v, want %#v", got, want) } diff --git a/internal/modules/dnd/normalize/locationregistry/normalizer.go b/internal/modules/dnd/normalize/locationregistry/normalizer.go index 6924a83c..4574ac71 100644 --- a/internal/modules/dnd/normalize/locationregistry/normalizer.go +++ b/internal/modules/dnd/normalize/locationregistry/normalizer.go @@ -322,7 +322,7 @@ func duplicateFinding(retainedIndex int, removed []int) diagnostics.Finding { func locationScope(index int) string { return fmt.Sprintf("locations[%d]", index) } func ModuleSpec() pipeline.ModuleSpec { - return pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageNormalize, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, Requires: append([]string(nil), requiredCapabilities...), Provides: append([]string(nil), providedCapabilities...), ArtifactKind: dnd.LocationRegistryKind} + return pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageNormalize, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: append([]string(nil), requiredCapabilities...), Provides: append([]string(nil), providedCapabilities...), ArtifactKind: dnd.LocationRegistryKind} } func Register(registry *pipeline.NormalizerRegistry) error { return pipeline.RegisterNormalizerBuilder(registry, ModuleSpec(), validateOptions, func(request pipeline.BuildRequest) (contracts.Normalizer[dnd.LocationRegistry], error) { diff --git a/internal/modules/dnd/normalize/locationregistry/normalizer_test.go b/internal/modules/dnd/normalize/locationregistry/normalizer_test.go index e5af315c..d93cdc10 100644 --- a/internal/modules/dnd/normalize/locationregistry/normalizer_test.go +++ b/internal/modules/dnd/normalize/locationregistry/normalizer_test.go @@ -18,7 +18,7 @@ import ( ) func TestModuleContractAndNativeDependencies(t *testing.T) { - want := pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageNormalize, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, Requires: []string{"merged"}, Provides: []string{"normalized"}, ArtifactKind: dnd.LocationRegistryKind} + want := pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageNormalize, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: []string{"merged"}, Provides: []string{"normalized"}, ArtifactKind: dnd.LocationRegistryKind} if got := ModuleSpec(); !reflect.DeepEqual(got, want) { t.Fatalf("ModuleSpec() = %#v, want %#v", got, want) } diff --git a/internal/modules/dnd/normalize/npcregistry/normalizer.go b/internal/modules/dnd/normalize/npcregistry/normalizer.go index 8e8bab37..9c3619bd 100644 --- a/internal/modules/dnd/normalize/npcregistry/normalizer.go +++ b/internal/modules/dnd/normalize/npcregistry/normalizer.go @@ -321,7 +321,7 @@ func duplicateFinding(retainedIndex int, removed []int) diagnostics.Finding { func npcScope(index int) string { return fmt.Sprintf("npcs[%d]", index) } func ModuleSpec() pipeline.ModuleSpec { - return pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageNormalize, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, Requires: append([]string(nil), requiredCapabilities...), Provides: append([]string(nil), providedCapabilities...), ArtifactKind: dnd.NPCRegistryKind} + return pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageNormalize, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: append([]string(nil), requiredCapabilities...), Provides: append([]string(nil), providedCapabilities...), ArtifactKind: dnd.NPCRegistryKind} } func Register(registry *pipeline.NormalizerRegistry) error { diff --git a/internal/modules/dnd/normalize/npcregistry/normalizer_test.go b/internal/modules/dnd/normalize/npcregistry/normalizer_test.go index 0933402e..07dea300 100644 --- a/internal/modules/dnd/normalize/npcregistry/normalizer_test.go +++ b/internal/modules/dnd/normalize/npcregistry/normalizer_test.go @@ -25,7 +25,7 @@ func TestModuleContractAndIdentity(t *testing.T) { if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil { t.Fatal("DecodeOptions() accepted unknown option") } - want := pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageNormalize, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, Requires: []string{"merged"}, Provides: []string{"normalized"}, ArtifactKind: dnd.NPCRegistryKind} + want := pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageNormalize, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: []string{"merged"}, Provides: []string{"normalized"}, ArtifactKind: dnd.NPCRegistryKind} if got := ModuleSpec(); !reflect.DeepEqual(got, want) { t.Fatalf("ModuleSpec() = %#v, want %#v", got, want) } diff --git a/internal/preflight/targets.go b/internal/preflight/targets.go new file mode 100644 index 00000000..b4672345 --- /dev/null +++ b/internal/preflight/targets.go @@ -0,0 +1,156 @@ +package preflight + +import ( + "context" + "errors" + "fmt" + + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" + "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" + "go.maximumdirect.net/promptkit" +) + +// BindingScope identifies selected work associated with a native target. +type BindingScope struct { + Step string `json:"step,omitempty" yaml:"step,omitempty"` + Lane string `json:"lane,omitempty" yaml:"lane,omitempty"` + Role pipeline.ModuleStage `json:"role" yaml:"role"` + Module string `json:"module" yaml:"module"` + Validator string `json:"validator,omitempty" yaml:"validator,omitempty"` +} + +type ProfileTarget struct { + ID string + Bindings []BindingScope +} + +type AccessTarget struct { + BackendID string + ProfileID string + Bindings []BindingScope +} + +type Targets struct { + Profiles []ProfileTarget + Access []AccessTarget +} + +// DiscoverTargets identifies initial request targets without invoking providers. +func DiscoverTargets(ctx context.Context, resolved pipeline.ResolvedPipeline, catalog pipeline.ModuleCatalog, engine *promptkit.Engine) (Targets, error) { + var targets Targets + if ctx == nil { + return targets, errors.New("preflight context must not be nil") + } + if err := ctx.Err(); err != nil { + return targets, err + } + profiles := make(map[string]int) + access := make(map[string]int) + add := func(scope BindingScope, class contracts.ExecutionClass, profile string, prompts []string) error { + if class != contracts.ExecutionClassLLMBacked { + return nil + } + if err := ctx.Err(); err != nil { + return err + } + if engine == nil { + return fmt.Errorf("%v: %w: native engine is required", scope, promptkit.ErrInvalidConfig) + } + ids := prompts + if len(ids) == 0 { + ids = []string{""} + } + for _, promptID := range ids { + selected := profile + if selected == "" && promptID != "" { + prompt, err := engine.InspectPrompt(ctx, promptID) + if err != nil { + return resourceFinding(fmt.Errorf("%v prompt %q: %w", scope, promptID, err)) + } + if prompt.DefaultProfileID != nil { + selected = *prompt.DefaultProfileID + } + } + if selected == "" { + return fmt.Errorf("%v: no statically selected profile for initial prompt %q", scope, promptID) + } + p, err := engine.InspectProfile(ctx, selected) + if err != nil { + return resourceFinding(fmt.Errorf("%v profile %q: %w", scope, selected, err)) + } + pi, ok := profiles[selected] + if !ok { + pi = len(targets.Profiles) + profiles[selected] = pi + targets.Profiles = append(targets.Profiles, ProfileTarget{ID: selected}) + } + targets.Profiles[pi].Bindings = appendScope(targets.Profiles[pi].Bindings, scope) + key := "profile:" + selected + backend := "" + if p.Backend != nil && *p.Backend != "" { + backend = *p.Backend + key = "backend:" + backend + } + ai, ok := access[key] + if !ok { + ai = len(targets.Access) + access[key] = ai + targets.Access = append(targets.Access, AccessTarget{BackendID: backend, ProfileID: selected}) + } + targets.Access[ai].Bindings = appendScope(targets.Access[ai].Bindings, scope) + } + return nil + } + input, _ := catalog.Inputs.Spec(resolved.Input.Module) + if err := add(BindingScope{Role: pipeline.StageInput, Module: resolved.Input.Module}, resolved.InputExecutionClass, resolved.Input.LLMProfile, input.InitialPrompts); err != nil { + return targets, err + } + chunk, _ := catalog.Chunkers.Spec(resolved.Chunk.Module) + if err := add(BindingScope{Role: pipeline.StageChunk, Module: resolved.Chunk.Module}, resolved.ChunkExecutionClass, resolved.Chunk.LLMProfile, chunk.InitialPrompts); err != nil { + return targets, err + } + steps := make(map[string]string) + for _, step := range resolved.Steps { + for _, lane := range step.ArtifactLanes { + steps[lane.ID] = step.ID + extract, _ := catalog.Extractors.Spec(lane.Extract.Module) + merge, _ := catalog.Mergers.Spec(lane.Merge.Module) + normalize, _ := catalog.Normalizers.Spec(lane.Normalize.Module) + for _, item := range []struct { + role pipeline.ModuleStage + binding pipeline.ModuleBinding + class contracts.ExecutionClass + prompts []string + }{ + {pipeline.StageExtract, lane.Extract, lane.ExtractExecutionClass, extract.InitialPrompts}, + {pipeline.StageMerge, lane.Merge, lane.MergeExecutionClass, merge.InitialPrompts}, + {pipeline.StageNormalize, lane.Normalize, lane.NormalizeExecutionClass, normalize.InitialPrompts}, + } { + if err := add(BindingScope{Step: step.ID, Lane: lane.ID, Role: item.role, Module: item.binding.Module}, item.class, item.binding.LLMProfile, item.prompts); err != nil { + return targets, err + } + } + } + } + for _, chain := range resolved.ValidatorChains { + for _, validator := range chain.Validators { + if err := add(BindingScope{Step: steps[chain.LaneID], Lane: chain.LaneID, Role: chain.Stage, Module: chain.ModuleKey, Validator: validator.Binding.Module}, validator.ExecutionClass, validator.Binding.LLMProfile, validator.Prompts); err != nil { + return targets, err + } + } + } + output, _ := catalog.Outputs.Spec(resolved.Output.Module) + if err := add(BindingScope{Role: pipeline.StageOutput, Module: resolved.Output.Module}, resolved.OutputExecutionClass, resolved.Output.LLMProfile, output.InitialPrompts); err != nil { + return targets, err + } + return targets, nil +} + +func appendScope(scopes []BindingScope, scope BindingScope) []BindingScope { + for _, existing := range scopes { + if existing == scope { + return scopes + } + } + return append(scopes, scope) +} diff --git a/internal/preflight/targets_test.go b/internal/preflight/targets_test.go new file mode 100644 index 00000000..093533f7 --- /dev/null +++ b/internal/preflight/targets_test.go @@ -0,0 +1,78 @@ +package preflight + +import ( + "context" + "errors" + "testing" + + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" + "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" + "go.maximumdirect.net/configkit" + "go.maximumdirect.net/promptkit" +) + +func TestDiscoverTargetsUsesInitialPromptAndDeduplicates(t *testing.T) { + engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Underlays: []configkit.Underlay{{Inputs: []configkit.Input{promptkit.SettingsInput(configkit.Bytes("targets", []byte(`backends: + - id: shared + endpoint: https://example.invalid +profiles: + - id: primary + backend: shared + - id: secondary + backend: shared + - id: endpoint-only + endpoint: https://example.invalid + - id: unused + backend: shared +prompts: + - id: initial + default_profile: primary + messages: [{role: user, content: 'Initial'}] + output: {format: text, validation_mode: none} + - id: correction + default_profile: unused + messages: [{role: user, content: 'Correct'}] + output: {format: text, validation_mode: none} + - id: validator + default_profile: secondary + messages: [{role: user, content: 'Assess'}] + output: {format: text, validation_mode: none} +`)))}}}}}, promptkit.WithBundledOpenRouter(false), promptkit.WithLLMClient(noGeneration{t})) + if err != nil { + t.Fatal(err) + } + chunkers := pipeline.NewChunkerRegistry() + err = chunkers.RegisterWithSpec(pipeline.ModuleSpec{Key: "chunk", Stage: pipeline.StageChunk, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{"initial", "correction"}, InitialPrompts: []string{"initial"}}, func() (contracts.Chunker, error) { t.Fatal("constructed chunker"); return nil, nil }) + if err != nil { + t.Fatal(err) + } + catalog := pipeline.ModuleCatalog{Chunkers: chunkers} + resolved := pipeline.ResolvedPipeline{Chunk: pipeline.Binding("chunk"), ChunkExecutionClass: contracts.ExecutionClassLLMBacked, ValidatorChains: []pipeline.ResolvedValidatorChain{{Stage: pipeline.StageChunk, ModuleKey: "chunk", Validators: []pipeline.ResolvedValidator{{Binding: pipeline.Binding("validator"), ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{"validator"}}}}}} + targets, err := DiscoverTargets(context.Background(), resolved, catalog, engine) + if err != nil { + t.Fatal(err) + } + if len(targets.Profiles) != 2 || targets.Profiles[0].ID != "primary" || targets.Profiles[1].ID != "secondary" || len(targets.Access) != 1 || targets.Access[0].BackendID != "shared" || len(targets.Access[0].Bindings) != 2 { + t.Fatalf("targets = %+v", targets) + } + resolved.ValidatorChains[0].Validators[0].Binding.LLMProfile = "primary" + targets, err = DiscoverTargets(context.Background(), resolved, catalog, engine) + if err != nil || len(targets.Profiles) != 1 || len(targets.Profiles[0].Bindings) != 2 { + t.Fatalf("shared profile = %+v, %v", targets, err) + } + resolved.ValidatorChains[0].Validators[0].Binding.LLMProfile = "" + resolved.Chunk.LLMProfile = "endpoint-only" + targets, err = DiscoverTargets(context.Background(), resolved, catalog, engine) + if err != nil || len(targets.Access) != 2 || targets.Access[0].BackendID != "" || targets.Access[0].ProfileID != "endpoint-only" { + t.Fatalf("endpoint target = %+v, %v", targets, err) + } + resolved.Chunk.LLMProfile = "missing" + if _, err := DiscoverTargets(context.Background(), resolved, catalog, engine); !errors.Is(err, promptkit.ErrProfileNotFound) { + t.Fatalf("missing profile cause = %v", err) + } + resolved.Chunk.LLMProfile = "" + resolved.Chunk.Module = "dynamic" + if _, err := DiscoverTargets(context.Background(), resolved, catalog, engine); err == nil { + t.Fatal("unresolved target was silently omitted") + } +}