Build preflight target associations in linear time

This commit is contained in:
2026-11-28 16:14:09 +00:00
parent 95d46c7da3
commit 00112f6fc2
3 changed files with 73 additions and 34 deletions

View File

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