diff --git a/docs/roadmap/remediation.md b/docs/roadmap/remediation.md index ddee42bb..c36f18fe 100644 --- a/docs/roadmap/remediation.md +++ b/docs/roadmap/remediation.md @@ -187,6 +187,12 @@ immutability assertions over full snapshots of the private capture layout. ## Finding 4: Build Target Associations With Ordered Sets +**Status: Complete.** Ordered scope slices now use companion membership sets; +invocation-local maps reuse prompt defaults and profile/access indexes. Association +insertion is expected-linear without changing native probe identity or ordering. +Tests cover distinct profiles sharing a backend, shared profiles, multiple initial +prompts, duplicate bindings, endpoint-only selections, and scoped failures. + **Priority:** P2 — scalability; plan Stage 3. **Owner:** [Target discovery](../../internal/preflight/targets.go), especially diff --git a/internal/preflight/targets.go b/internal/preflight/targets.go index 286d743e..ce19d1c3 100644 --- a/internal/preflight/targets.go +++ b/internal/preflight/targets.go @@ -53,8 +53,12 @@ func DiscoverTargets(ctx context.Context, resolved pipeline.ResolvedPipeline, ca if err := ctx.Err(); err != nil { return targets, err } - profiles := make(map[string]int) + 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 { @@ -75,43 +79,54 @@ func DiscoverTargets(ctx context.Context, resolved pipeline.ResolvedPipeline, ca ids = []string{""} } for _, promptID := range ids { + if err := ctx.Err(); err != nil { + return err + } 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 + 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) } - p, err := engine.InspectProfile(ctx, selected) - if err != nil { - return resourceFinding(fmt.Errorf("%v profile %q: %w", scope, selected, err)) - } - pi, ok := profiles[selected] + indices, ok := profiles[selected] if !ok { - pi = len(targets.Profiles) - profiles[selected] = pi + 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 } - 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) + 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 } @@ -160,11 +175,10 @@ func DiscoverTargets(ctx context.Context, resolved pipeline.ResolvedPipeline, ca return targets, nil } -func appendScope(scopes []BindingScope, scope BindingScope) []BindingScope { - for _, existing := range scopes { - if existing == scope { - return scopes - } +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) } diff --git a/internal/preflight/targets_test.go b/internal/preflight/targets_test.go index d886fca1..43104292 100644 --- a/internal/preflight/targets_test.go +++ b/internal/preflight/targets_test.go @@ -3,6 +3,7 @@ package preflight import ( "context" "errors" + "reflect" "testing" "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" @@ -33,6 +34,10 @@ prompts: default_profile: unused messages: [{role: user, content: 'Correct'}] output: {format: text, validation_mode: none} + - id: initial-second + default_profile: primary + messages: [{role: user, content: 'Another initial request'}] + output: {format: text, validation_mode: none} - id: validator default_profile: secondary messages: [{role: user, content: 'Assess'}] @@ -42,7 +47,7 @@ prompts: 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 }) + err = chunkers.RegisterWithSpec(pipeline.ModuleSpec{Key: "chunk", Stage: pipeline.StageChunk, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{"initial", "initial-second", "correction"}, InitialPrompts: []string{"initial", "initial-second"}}, func() (contracts.Chunker, error) { t.Fatal("constructed chunker"); return nil, nil }) if err != nil { t.Fatal(err) } @@ -55,6 +60,20 @@ prompts: 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) } + chunkScope := BindingScope{Role: pipeline.StageChunk, Module: "chunk"} + validatorScope := BindingScope{Role: pipeline.StageChunk, Module: "chunk", Validator: "validator"} + if !reflect.DeepEqual(targets.Profiles[0].Bindings, []BindingScope{chunkScope}) || + !reflect.DeepEqual(targets.Profiles[1].Bindings, []BindingScope{validatorScope}) || + !reflect.DeepEqual(targets.Access[0].Bindings, []BindingScope{chunkScope, validatorScope}) || targets.Access[0].ProfileID != "primary" { + t.Fatalf("ordered unique associations = %+v", targets) + } + // Repeated declarations must not duplicate a binding or change first use. + resolved.ValidatorChains[0].Validators = append(resolved.ValidatorChains[0].Validators, resolved.ValidatorChains[0].Validators[0]) + duplicateTargets, err := DiscoverTargets(context.Background(), resolved, catalog, engine) + if err != nil || !reflect.DeepEqual(duplicateTargets, targets) { + t.Fatalf("duplicate associations = %+v, %v", duplicateTargets, err) + } + resolved.ValidatorChains[0].Validators = resolved.ValidatorChains[0].Validators[:1] 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 {