From ba5ac651e2bac18595b4e42ebfb71b325e5f695d Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Sat, 21 Nov 2026 17:55:06 +0000 Subject: [PATCH] Share immutable backend defaults during profile capture --- docs/internal/sources.md | 4 ++- docs/roadmap/remediation.md | 8 +++++- internal/configuration/targets.go | 12 ++++---- internal/configuration/targets_test.go | 39 ++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/docs/internal/sources.md b/docs/internal/sources.md index 64586ef..b77944f 100644 --- a/docs/internal/sources.md +++ b/docs/internal/sources.md @@ -138,7 +138,9 @@ ConfigKit resolves profile inheritance before the handoff. Within a collection, `extends: {id: parent}` names a sibling local ID; it is not a cross-collection reference. The configuration adapter validates final effective resource references and field values, then captures backend definitions and profile target snapshots -with backend defaults and outbound-control presence. Null profile endpoint, +with backend defaults and outbound-control presence. Profiles without provider +overrides share capture-owned immutable backend parameter maps; target lookups +still detach mutable values before runtime use. Null profile endpoint, credential source, and whole provider map permit backend fallback after composition, subject to the required-credential rule. A profile can remain partial until runtime overrides complete it. Resource inspection selects indexed diff --git a/docs/roadmap/remediation.md b/docs/roadmap/remediation.md index 119c842..a16e755 100644 --- a/docs/roadmap/remediation.md +++ b/docs/roadmap/remediation.md @@ -173,7 +173,13 @@ real-world benchmark is needed to establish that algorithmic change. ### 5. Share Immutable Backend Defaults Across Profiles Without Overrides -Status: open for consideration. Priority: P3. Implementation stage: 6. +Status: complete. Priority: P3. Implementation stage: 6. + +Completion: profile construction now reads capture-owned backend definitions +directly and shares immutable provider maps when no overlay is needed. Registry +and runtime lookups remain defensive, and overlays still copy before merging. +Added lookup/overlay isolation coverage and updated internal documentation. +The full `go test -race ./...` suite and `go vet ./...` passed with Go 1.27.4. Locations: `internal/configuration/targets.go:27–29` and `68–78`, `internal/backend/registry.go:39–48`, and diff --git a/internal/configuration/targets.go b/internal/configuration/targets.go index 6d6fd8f..d3cbedc 100644 --- a/internal/configuration/targets.go +++ b/internal/configuration/targets.go @@ -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 diff --git a/internal/configuration/targets_test.go b/internal/configuration/targets_test.go index 3945112..6ec1f2f 100644 --- a/internal/configuration/targets_test.go +++ b/internal/configuration/targets_test.go @@ -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"}},