diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index d6a6074..2aa4609 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -141,7 +141,13 @@ The application retains the existing single loading pipeline. ## Stage 2: Protect Application Loading, Presence, And Runtime Conversion -Status: Pending. Depends on Stage 1. +Status: Complete. Depends on Stage 1. + +Completion evidence: configuration tests pass for inherited settings and +presence, `Origin.Inheritance` provenance, sorted runtime conversion, imports +and overlays before inheritance, CLI overrides, working-directory-relative +paths, cancellation, and structured reserved-key diagnostics. Focused and full +tests, race tests, vet, build, and `git diff --check` passed. Files: primarily `internal/config/load_test.go` and `internal/config/document_test.go`; production configuration files only if a diff --git a/internal/config/document_test.go b/internal/config/document_test.go index 32e125e..e94e5ce 100644 --- a/internal/config/document_test.go +++ b/internal/config/document_test.go @@ -75,6 +75,90 @@ backends: } } +func TestDocumentResolvesBackendInheritance(t *testing.T) { + result, err := loadDocumentBytes(`backends: + base: + endpoint: http://base.invalid/v1 + api_key_env: BASE_API_KEY + concurrency_limit: 2 + queue_capacity: 4 + extra_params: + enabled: true + shared: visible + count: 7 + child: + extends: base + queue_capacity: 0 + extra_params: + enabled: false + nullable-child: + extends: base + queue_capacity: null +`) + if err != nil { + t.Fatalf("load inherited document: %v", err) + } + + child := result.Config.Backends["child"] + if child.Endpoint != "http://base.invalid/v1" || child.APIKeyEnv != "BASE_API_KEY" || child.ConcurrencyLimit != 2 { + t.Fatalf("child did not inherit backend settings: %+v", child) + } + if child.QueueCapacity == nil || *child.QueueCapacity != 0 { + t.Fatalf("explicit zero queue capacity was not retained: %+v", child.QueueCapacity) + } + if child.ExtraParams["enabled"] != false || child.ExtraParams["shared"] != "visible" || child.ExtraParams["count"] != int64(7) { + t.Fatalf("child parameters were not combined with explicit presence: %#v", child.ExtraParams) + } + nullableChild := result.Config.Backends["nullable-child"] + if nullableChild.QueueCapacity != nil { + t.Fatalf("explicit null inherited the parent's queue capacity: %+v", nullableChild.QueueCapacity) + } + + metadata := result.Metadata() + endpointPath := configkit.Keys("backends", "child", "endpoint") + field, known := metadata.Lookup(endpointPath) + if !known || !field.Explicit || !metadata.IsExplicit(endpointPath) { + t.Fatalf("inherited endpoint presence was lost: field=%+v known=%t", field, known) + } + wantFrom := configkit.Keys("backends", "base", "endpoint").String() + wantTo := endpointPath.String() + wantReference := configkit.Keys("backends", "child", "extends").String() + foundInheritance := false + for _, step := range field.Origin.Inheritance { + if step.From.String() == wantFrom && step.To.String() == wantTo && step.Reference.String() == wantReference { + foundInheritance = true + break + } + } + if !foundInheritance { + t.Fatalf("inherited endpoint provenance missing: %+v", field.Origin.Inheritance) + } + + settings := Settings(result.Config) + wantIDs := []string{"base", "child", "nullable-child"} + if len(settings.Backends) != len(wantIDs) { + t.Fatalf("backend definitions were dropped: %+v", settings.Backends) + } + for i, want := range wantIDs { + if settings.Backends[i].ID != want { + t.Fatalf("backend IDs are not sorted: %+v", settings.Backends) + } + } + convertedChild := settings.Backends[1] + if convertedChild.Endpoint != child.Endpoint || convertedChild.APIKeyEnv != child.APIKeyEnv || convertedChild.ConcurrencyLimit != 2 { + t.Fatalf("inherited settings changed during conversion: %+v", convertedChild) + } + if convertedChild.QueueCapacity == nil || *convertedChild.QueueCapacity != 0 { + t.Fatalf("zero queue capacity presence changed during conversion: %+v", convertedChild.QueueCapacity) + } + if value := convertedChild.ExtraParams["count"]; value != int64(7) { + t.Fatalf("dynamic integer changed during conversion: %T %v", value, value) + } + if settings.Backends[2].QueueCapacity != nil { + t.Fatalf("nullable queue capacity changed during conversion: %+v", settings.Backends[2].QueueCapacity) + } +} + func TestDocumentRecordsEffectiveNormalization(t *testing.T) { result, err := loadDocumentBytes(`prompt_dir: " ./one/../two " profile_dir: " " diff --git a/internal/config/load_test.go b/internal/config/load_test.go index 3ec4cd2..e2236cb 100644 --- a/internal/config/load_test.go +++ b/internal/config/load_test.go @@ -217,6 +217,89 @@ func TestLoadComposesExplicitImportsAndSelectedOverlay(t *testing.T) { } } +func TestLoadComposesInheritanceAfterImportsOverlayAndOverrides(t *testing.T) { + dir := t.TempDir() + if err := os.MkdirAll(filepath.Join(dir, "fragments"), 0o700); err != nil { + t.Fatal(err) + } + if err := os.MkdirAll(filepath.Join(dir, "overlays"), 0o700); err != nil { + t.Fatal(err) + } + base := filepath.Join(dir, "config.yml") + writeApplicationFile(t, base, "configkit:\n imports:\n - path: fragments/base.yml\nbackends:\n child:\n extends: base\n") + writeApplicationFile(t, filepath.Join(dir, "fragments", "base.yml"), `prompt_dir: ./fragment-prompts +backends: + base: + endpoint: http://base.invalid/v1 + api_key_env: BASE_API_KEY + concurrency_limit: 2 + queue_capacity: 4 +`) + writeApplicationFile(t, filepath.Join(dir, "overlays", "local.yml"), `server: + addr: :9090 +backends: + base: + endpoint: http://overlay.invalid/v1 + child: + queue_capacity: 0 +`) + writeApplicationFile(t, filepath.Join(dir, "overlays", "cancel.yml"), `backends: + child: + extends: null + endpoint: http://child.invalid/v1 +`) + + addrOverride := []configkit.Override{{ + Path: configkit.Keys("server", "addr"), Value: ":7070", Flag: "--addr", + }} + result, err := Load(LoadOptions{Path: base, Explicit: true, Overlay: "local", Overrides: addrOverride}) + if err != nil { + t.Fatalf("load imported and overlaid inheritance: %v", err) + } + child := result.Config.Backends["child"] + if result.Config.PromptDir != "fragment-prompts" || result.Config.Server.Addr != ":7070" { + t.Fatalf("path or CLI override changed during assembly: %+v", result.Config) + } + if child.Endpoint != "http://overlay.invalid/v1" || child.APIKeyEnv != "BASE_API_KEY" || child.ConcurrencyLimit != 2 { + t.Fatalf("child did not inherit the assembled parent: %+v", child) + } + if child.QueueCapacity == nil || *child.QueueCapacity != 0 { + t.Fatalf("overlay zero did not replace the inherited capacity: %+v", child.QueueCapacity) + } + + result, err = Load(LoadOptions{Path: base, Explicit: true, Overlay: "cancel", Overrides: addrOverride}) + if err != nil { + t.Fatalf("load cancelled inheritance: %v", err) + } + child = result.Config.Backends["child"] + if child.Endpoint != "http://child.invalid/v1" || child.APIKeyEnv != "" || child.ConcurrencyLimit != 0 || child.QueueCapacity != nil { + t.Fatalf("cancelled child retained parent settings: %+v", child) + } +} + +func TestLoadRejectsReservedInheritanceKeyInExtraParams(t *testing.T) { + result, err := loadDocumentBytes(`backends: + worker: + endpoint: http://worker.invalid/v1 + extra_params: + extends: missing +`) + if err == nil || result != nil { + t.Fatalf("missing sibling in reserved key accepted: result=%+v err=%v", result, err) + } + var loadError *configkit.LoadError + if !errors.As(err, &loadError) { + t.Fatalf("structured inheritance error was lost: %v", err) + } + wantPath := configkit.Keys("backends", "worker", "extra_params", "extends").String() + for _, diagnostic := range loadError.Diagnostics() { + if diagnostic.Code == configkit.CodeInheritanceMissing && diagnostic.Path.String() == wantPath { + return + } + } + t.Fatalf("missing inheritance diagnostic at %s: %+v", wantPath, loadError.Diagnostics()) +} + func TestLoadRejectsOverlayWithoutBase(t *testing.T) { useConfigSearchPaths(t, filepath.Join(t.TempDir(), "absent.yml")) if _, err := Load(LoadOptions{Overlay: "local"}); err == nil {