Share immutable backend defaults during profile capture

This commit is contained in:
2026-11-21 17:55:06 +00:00
parent 6f45fd9fd0
commit ba5ac651e2
4 changed files with 56 additions and 7 deletions

View File

@@ -26,7 +26,7 @@ func (state *loaded) buildExecutionProfiles(config *settings.Settings) error {
state.indexedProfiles = make(map[string]domain.ExecutionProfile, len(profileConfigs))
for _, id := range sortedKeys(profileConfigs) {
config := profileConfigs[id]
profile, err := captureExecutionProfile(id, config, state.registry)
profile, err := captureExecutionProfile(id, config, definitions)
if err != nil {
return err
}
@@ -65,12 +65,14 @@ func captureBackend(id string, config backendConfig) domain.Backend {
return definition
}
func captureExecutionProfile(id string, config profileConfig, registry *backend.Registry) (domain.ExecutionProfile, error) {
func captureExecutionProfile(id string, config profileConfig, backends map[string]domain.Backend) (domain.ExecutionProfile, error) {
target := defaults.ExecutionTargetDefault()
if config.Backend != nil && *config.Backend != "" {
selected, err := registry.GetBackend(*config.Backend)
if err != nil {
return domain.ExecutionProfile{}, fmt.Errorf("profile %q backend: %w", id, err)
// These defaults are capture-owned and immutable. Public and runtime
// lookups still detach mutable values; profile overlays copy before merging.
selected, found := backends[*config.Backend]
if !found {
return domain.ExecutionProfile{}, fmt.Errorf("profile %q backend: %w: %q", id, backend.ErrBackendNotFound, *config.Backend)
}
target.BackendID = selected.ID
target.Endpoint = selected.Endpoint

View File

@@ -64,6 +64,45 @@ func TestOverlayProviderMapOwnsInputsAndReplacesValues(t *testing.T) {
}
}
func TestProfilesWithSharedDefaultsRemainIsolatedAtLookup(t *testing.T) {
state, err := load(mapping("shared defaults", map[string]any{
"backends": []any{map[string]any{"id": "backend", "endpoint": "https://example.test/v1",
"extra_params": map[string]any{"nested": map[string]any{"value": "backend"}}}},
"profiles": []any{
map[string]any{"id": "first", "backend": "backend"},
map[string]any{"id": "second", "backend": "backend"},
map[string]any{"id": "overlay", "backend": "backend", "extra_params": map[string]any{"nested": map[string]any{"value": "profile"}}},
},
}), nil)
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
for _, id := range []string{"first", "second", "overlay"} {
profile, err := state.GetProfile(ctx, id)
if err != nil {
t.Fatal(err)
}
want := "backend"
if id == "overlay" {
want = "profile"
}
if profile.ResolvedTarget.ExtraParams["nested"].(map[string]any)["value"] != want {
t.Fatalf("%s defaults changed: %+v", id, profile)
}
profile.ResolvedTarget.ExtraParams["nested"].(map[string]any)["value"] = "caller mutation"
}
backend, err := state.GetBackend("backend")
if err != nil {
t.Fatal(err)
}
backend.ExtraParams["nested"].(map[string]any)["value"] = "backend caller mutation"
profile, err := state.GetProfile(ctx, "first")
if err != nil || profile.ResolvedTarget.ExtraParams["nested"].(map[string]any)["value"] != "backend" {
t.Fatalf("lookup mutation changed retained defaults: %+v, %v", profile, err)
}
}
func TestRequestCompletesLoadedPartialProfile(t *testing.T) {
state, err := load(mapping("partial profile", map[string]any{
"profiles": []any{map[string]any{"id": "partial"}},