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 } // TargetError identifies the binding whose initial target could not be selected. type TargetError struct { Scope BindingScope Cause error } func (err *TargetError) Error() string { return err.Cause.Error() } func (err *TargetError) Unwrap() error { return err.Cause } // 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 } type selection struct{ profile, access int } profiles := make(map[string]selection) access := make(map[string]int) profileScopes := []map[BindingScope]struct{}{} accessScopes := []map[BindingScope]struct{}{} promptDefaults := make(map[string]string) add := func(scope BindingScope, class contracts.ExecutionClass, profile string, prompts []string) (failure error) { defer func() { if failure != nil { failure = &TargetError{Scope: scope, Cause: failure} } }() 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 { if err := ctx.Err(); err != nil { return err } selected := profile if selected == "" && promptID != "" { var known bool selected, known = promptDefaults[promptID] if !known { 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 } promptDefaults[promptID] = selected } } if selected == "" { return fmt.Errorf("%v: no statically selected profile for initial prompt %q", scope, promptID) } indices, ok := profiles[selected] if !ok { p, err := engine.InspectProfile(ctx, selected) if err != nil { return resourceFinding(fmt.Errorf("%v profile %q: %w", scope, selected, err)) } indices.profile = len(targets.Profiles) targets.Profiles = append(targets.Profiles, ProfileTarget{ID: selected}) profileScopes = append(profileScopes, make(map[BindingScope]struct{})) key, backend := "profile:"+selected, "" if p.Backend != nil && *p.Backend != "" { backend = *p.Backend key = "backend:" + backend } ai, exists := access[key] if !exists { ai = len(targets.Access) access[key] = ai targets.Access = append(targets.Access, AccessTarget{BackendID: backend, ProfileID: selected}) accessScopes = append(accessScopes, make(map[BindingScope]struct{})) } indices.access = ai profiles[selected] = indices } pi, ai := indices.profile, indices.access targets.Profiles[pi].Bindings = appendScope(targets.Profiles[pi].Bindings, profileScopes[pi], scope) targets.Access[ai].Bindings = appendScope(targets.Access[ai].Bindings, accessScopes[ai], 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, seen map[BindingScope]struct{}, scope BindingScope) []BindingScope { if _, exists := seen[scope]; exists { return scopes } seen[scope] = struct{}{} return append(scopes, scope) }