Discover selected pipeline model targets

This commit is contained in:
2026-11-28 04:11:33 +00:00
parent 8617f936a4
commit 2ca73aa3ee
27 changed files with 267 additions and 27 deletions

View File

@@ -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)
}

View File

@@ -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")
}
}