From 0d5ecfa842904c48468b78ada4374c42de63c3a7 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Sun, 4 Oct 2026 11:26:40 +0000 Subject: [PATCH] Replace legacy framework config with source selectors --- docs/roadmap/implementation.md | 8 +- internal/config/document.go | 44 +++++--- internal/config/document_test.go | 178 +++++++++++++------------------ internal/config/load.go | 23 +--- internal/config/load_test.go | 139 +++++------------------- internal/config/settings.go | 13 +-- 6 files changed, 140 insertions(+), 265 deletions(-) diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 897dd75..247e426 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -172,7 +172,13 @@ there are no unpublished substitutions or unrelated dependency upgrades. ## Stage 2: Cut Over The Application Schema And Its Tests -Status: Pending. Depends on Stage 1. +Status: Complete. `Document` and `AppSettings` now use ordered `promptkit_sources` +selectors with file/directory exclusivity, path normalization and history, and +no framework acquisition. Defaults, application imports/overlays, replacement +precedence, safe validation, and non-backend limits remain covered by config +tests. `go test ./internal/config` passes. Full baseline checks remain blocked +only by the expected CLI API/settings migration (Stage 3) and HTTP/report +fixtures (Stage 7); this intermediate revision is not buildable. Files: `internal/config/document.go`, `settings.go`, `load.go`, `document_test.go`, `load_test.go`; related obsolete default constants only if diff --git a/internal/config/document.go b/internal/config/document.go index 9aac2ce..96d5221 100644 --- a/internal/config/document.go +++ b/internal/config/document.go @@ -11,21 +11,17 @@ import ( // Document is Scriptorium's ConfigKit-native application configuration shape. type Document struct { - PromptDir string `yaml:"prompt_dir"` - ProfileDir string `yaml:"profile_dir"` - SchemaDir string `yaml:"schema_dir"` - TransientRetries *int `yaml:"transient_retries"` - Server ServerDocument `yaml:"server"` - Defaults DefaultsDocument `yaml:"defaults"` - Backends map[string]BackendDocument `yaml:"backends"` + PromptKitSources []SourceSelection `yaml:"promptkit_sources"` + SchemaDir string `yaml:"schema_dir"` + TransientRetries *int `yaml:"transient_retries"` + Server ServerDocument `yaml:"server"` + Defaults DefaultsDocument `yaml:"defaults"` } -type BackendDocument struct { - Endpoint string `yaml:"endpoint"` - APIKeyEnv string `yaml:"api_key_env"` - ExtraParams map[string]any `yaml:"extra_params"` - ConcurrencyLimit int `yaml:"concurrency_limit"` - QueueCapacity *int `yaml:"queue_capacity"` +// SourceSelection identifies one PromptKit configuration file or directory. +type SourceSelection struct { + File string `yaml:"file"` + Directory string `yaml:"directory"` } type ServerDocument struct { @@ -62,14 +58,16 @@ func documentDefaults() Document { } func resolveDocument(document *Document, resolution *configkit.Resolution) error { - normalizePath := func(path configkit.Path, value *string) { + normalizePath := func(path configkit.Path, value *string) bool { cleaned := strings.TrimSpace(*value) if cleaned != "" { cleaned = filepath.Clean(cleaned) } if cleaned != *value { configkit.Normalize(resolution, path, value, cleaned, "normalized application path") + return true } + return false } normalizeString := func(path configkit.Path, value *string, normalized, reason string) { if normalized != *value { @@ -77,8 +75,15 @@ func resolveDocument(document *Document, resolution *configkit.Resolution) error } } - normalizePath(configkit.Keys("prompt_dir"), &document.PromptDir) - normalizePath(configkit.Keys("profile_dir"), &document.ProfileDir) + sourcePathsChanged := false + for i := range document.PromptKitSources { + path := configkit.Keys("promptkit_sources").Index(i) + sourcePathsChanged = normalizePath(path.Key("file"), &document.PromptKitSources[i].File) || sourcePathsChanged + sourcePathsChanged = normalizePath(path.Key("directory"), &document.PromptKitSources[i].Directory) || sourcePathsChanged + } + if sourcePathsChanged { + resolution.Record(configkit.Keys("promptkit_sources"), configkit.ChangeNormalization, "normalized selected source paths") + } normalizePath(configkit.Keys("schema_dir"), &document.SchemaDir) normalizePath(configkit.Keys("server", "artifact_root"), &document.Server.ArtifactRoot) normalizeString(configkit.Keys("server", "addr"), &document.Server.Addr, @@ -89,6 +94,13 @@ func resolveDocument(document *Document, resolution *configkit.Resolution) error } func validateDocument(document *Document, validation *configkit.Validation) { + for i, source := range document.PromptKitSources { + hasFile := source.File != "" + hasDirectory := source.Directory != "" + if hasFile == hasDirectory { + validation.Error(configkit.Keys("promptkit_sources").Index(i), "exactly one of file or directory must be non-empty") + } + } if document.TransientRetries != nil && *document.TransientRetries < 0 { validation.Error(configkit.Keys("transient_retries"), "must be greater than or equal to zero") } diff --git a/internal/config/document_test.go b/internal/config/document_test.go index e94e5ce..8009150 100644 --- a/internal/config/document_test.go +++ b/internal/config/document_test.go @@ -25,8 +25,9 @@ func TestDocumentDefaultsAndExplicitValues(t *testing.T) { t.Fatalf("unexpected defaults-only result: %+v sources=%+v", defaultsOnly.Config, defaultsOnly.Sources()) } - result, err := loadDocumentBytes(`prompt_dir: "" -profile_dir: "" + result, err := loadDocumentBytes(`promptkit_sources: + - file: " ./bundle/../framework.yml " + - directory: "./profiles" schema_dir: "" transient_retries: 0 server: @@ -37,12 +38,6 @@ server: max_response_bytes: 0 defaults: render_format: " JSON " -backends: - local: - endpoint: "http://fixture.invalid/v1" - queue_capacity: 0 - extra_params: - sample_count: 23 `) if err != nil { t.Fatalf("explicit-value load: %v", err) @@ -54,114 +49,70 @@ backends: if config.TransientRetries == nil || *config.TransientRetries != 0 { t.Fatalf("explicit zero retry budget lost: %+v", config.TransientRetries) } - if backend := config.Backends["local"]; backend.QueueCapacity == nil || *backend.QueueCapacity != 0 || backend.ExtraParams["sample_count"] != int64(23) { - t.Fatalf("backend presence or dynamic integer changed: %+v", backend) + if want := []SourceSelection{{File: "framework.yml"}, {Directory: "profiles"}}; !reflect.DeepEqual(config.PromptKitSources, want) { + t.Fatalf("source selectors did not decode: %+v", config.PromptKitSources) } if config.Defaults.RenderFormat != "json" { t.Fatalf("render format normalization failed: %q", config.Defaults.RenderFormat) } - nullable, err := loadDocumentBytes("transient_retries: null\nbackends:\n local:\n queue_capacity: null\n") + nullable, err := loadDocumentBytes("transient_retries: null\npromptkit_sources: null\n") if err != nil { t.Fatalf("nullable-value load: %v", err) } - if nullable.Config.TransientRetries != nil || nullable.Config.Backends["local"].QueueCapacity != nil { + if nullable.Config.TransientRetries != nil || nullable.Config.PromptKitSources != nil { t.Fatalf("nullable fields did not remain nil: %+v", nullable.Config) } - - nullBackends, err := loadDocumentBytes("backends: null\n") - if err != nil || nullBackends.Config.Backends != nil { - t.Fatalf("nullable backend map did not remain nil: %+v error=%v", nullBackends, err) - } } -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) +func TestDocumentSourceSelectionsDoNotAcquirePaths(t *testing.T) { + for _, tc := range []struct { + name string + content string + want []SourceSelection + }{ + {name: "omitted", content: "{}\n"}, + {name: "null", content: "promptkit_sources: null\n"}, + {name: "empty", content: "promptkit_sources: []\n"}, + {name: "nonexistent file", content: "promptkit_sources:\n - file: ./missing/framework.yml\n", want: []SourceSelection{{File: "missing/framework.yml"}}}, + {name: "nonexistent directory", content: "promptkit_sources:\n - directory: ./missing/framework\n", want: []SourceSelection{{Directory: "missing/framework"}}}, + } { + t.Run(tc.name, func(t *testing.T) { + result, err := loadDocumentBytes(tc.content) + if err != nil { + t.Fatalf("load source selectors: %v", err) + } + if len(result.Config.PromptKitSources) != len(tc.want) || len(tc.want) > 0 && !reflect.DeepEqual(result.Config.PromptKitSources, tc.want) { + t.Fatalf("source selectors changed: got=%+v want=%+v", result.Config.PromptKitSources, tc.want) + } + }) } +} - 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) +func TestSettingsCopiesPromptKitSourcesInOrder(t *testing.T) { + document := Document{PromptKitSources: []SourceSelection{ + {File: "./first.yml"}, + {Directory: "./second"}, + }} + settings := Settings(document) + if !reflect.DeepEqual(settings.PromptKitSources, document.PromptKitSources) { + t.Fatalf("source ordering changed: got=%+v want=%+v", settings.PromptKitSources, document.PromptKitSources) } - if child.QueueCapacity == nil || *child.QueueCapacity != 0 { - t.Fatalf("explicit zero queue capacity was not retained: %+v", child.QueueCapacity) + document.PromptKitSources[0].File = "changed.yml" + if settings.PromptKitSources[0].File != "./first.yml" { + t.Fatalf("settings retained the document's source slice: %+v", settings.PromptKitSources) } - 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) + if empty := Settings(Document{PromptKitSources: []SourceSelection{}}).PromptKitSources; empty == nil { + t.Fatal("explicit empty source list was not copied") } } func TestDocumentRecordsEffectiveNormalization(t *testing.T) { - result, err := loadDocumentBytes(`prompt_dir: " ./one/../two " -profile_dir: " " + result, err := loadDocumentBytes(`promptkit_sources: + - file: " ./one/../two " + - file: "./blank.yml" + directory: " " + - directory: "./three/../three" server: artifact_root: " ./data/../artifacts " addr: " :9000 " @@ -171,19 +122,22 @@ defaults: if err != nil { t.Fatalf("load normalized document: %v", err) } - if result.Config.PromptDir != "two" || result.Config.ProfileDir != "" || result.Config.Server.ArtifactRoot != "artifacts" || result.Config.Server.Addr != ":9000" { + if want := []SourceSelection{{File: "two"}, {File: "blank.yml"}, {Directory: "three"}}; !reflect.DeepEqual(result.Config.PromptKitSources, want) { + t.Fatalf("unexpected normalized source selections: %+v", result.Config.PromptKitSources) + } + if result.Config.Server.ArtifactRoot != "artifacts" || result.Config.Server.Addr != ":9000" { t.Fatalf("unexpected normalized values: %+v", result.Config) } var effective bytes.Buffer if err := result.WriteEffective(&effective); err != nil { t.Fatalf("write effective configuration: %v", err) } - for _, fragment := range []string{"prompt_dir: two", "profile_dir: ''", "artifact_root: artifacts", "addr: :9000", "render_format: json"} { + for _, fragment := range []string{"file: two", "directory: ''", "directory: three", "artifact_root: artifacts", "addr: :9000", "render_format: json"} { if !strings.Contains(effective.String(), fragment) { t.Fatalf("effective output omitted %q: %s", fragment, effective.String()) } } - explanation, known := result.Explain(configkit.Keys("prompt_dir")) + explanation, known := result.Explain(configkit.Keys("promptkit_sources").Index(0).Key("file")) if !known || explanation.Effective.YAML != "two\n" { t.Fatalf("explanation does not agree with effective value: %+v known=%t", explanation, known) } @@ -196,10 +150,26 @@ defaults: if !found { t.Fatalf("normalization was not recorded: %+v", explanation.History) } + + unchanged, err := loadDocumentBytes("promptkit_sources:\n - file: framework.yml\n") + if err != nil { + t.Fatalf("load unchanged source path: %v", err) + } + unchangedExplanation, known := unchanged.Explain(configkit.Keys("promptkit_sources").Index(0).Key("file")) + if !known { + t.Fatal("unchanged source path was not inspectable") + } + for _, change := range unchangedExplanation.History { + if change.Origin.Operation == configkit.ChangeNormalization { + t.Fatalf("unchanged source path recorded a normalization: %+v", unchangedExplanation.History) + } + } } func TestDocumentValidationIsSafeAndPathSpecific(t *testing.T) { for _, tc := range []struct{ name, content, path string }{ + {"empty source selector", "promptkit_sources:\n - file: ''\n", "/promptkit_sources/0"}, + {"multiple source selectors", "promptkit_sources:\n - file: secret-path\n directory: private-path\n", "/promptkit_sources/0"}, {"negative retry", "transient_retries: -9173\n", "/transient_retries"}, {"negative request limit", "server:\n max_request_bytes: -9173\n", "/server/max_request_bytes"}, {"negative artifact limit", "server:\n max_artifact_bytes: -9173\n", "/server/max_artifact_bytes"}, @@ -218,7 +188,7 @@ func TestDocumentValidationIsSafeAndPathSpecific(t *testing.T) { if diagnostic.Code == configkit.CodeValidateError && diagnostic.Path.String() == tc.path { found = true } - if strings.Contains(diagnostic.Message, "9173") || strings.Contains(diagnostic.Message, "private-invalid-value") { + if strings.Contains(diagnostic.Message, "9173") || strings.Contains(diagnostic.Message, "private-invalid-value") || strings.Contains(diagnostic.Message, "secret-path") || strings.Contains(diagnostic.Message, "private-path") { t.Fatalf("diagnostic disclosed rejected value: %+v", diagnostic) } } @@ -237,7 +207,13 @@ func TestDocumentRejectsNativeShapeErrors(t *testing.T) { "server:\n max_request_bytes: null\n", "defaults:\n render_format: null\n", "server:\n unknown_field: 1\n", - "backends:\n local:\n unknown_field: 1\n", + "promptkit_sources:\n - file: ./selected.yml\n unknown_field: 1\n", + "promptkit_sources:\n - file: ./selected.yml\n directory: ./also-selected\n", + "promptkit_sources:\n - file: null\n", + "promptkit_sources: ./not-a-sequence.yml\n", + "prompt_dir: ./old-prompts\n", + "profile_dir: ./old-profiles\n", + "backends:\n local:\n endpoint: http://example.invalid/v1\n", } { _, err := loadDocumentBytes(content) if err == nil { diff --git a/internal/config/load.go b/internal/config/load.go index 3207bef..e7979bc 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "os" - "sort" configkit "gitea.maximumdirect.net/eric/ConfigKit" renderformat "gitea.maximumdirect.net/eric/scriptorium/internal/format" @@ -62,8 +61,6 @@ func Load(options LoadOptions) (*configkit.Result[Document], error) { // Settings converts a validated document without another merge or normalization. func Settings(document Document) AppSettings { settings := AppSettings{ - PromptDir: document.PromptDir, - ProfileDir: document.ProfileDir, SchemaDir: document.SchemaDir, ServerAddr: document.Server.Addr, ArtifactRoot: document.Server.ArtifactRoot, @@ -73,24 +70,8 @@ func Settings(document Document) AppSettings { DefaultRenderFormat: renderformat.PreparedRunOutputFormat(document.Defaults.RenderFormat), TransientRetries: document.TransientRetries, } - if document.Backends != nil { - ids := make([]string, 0, len(document.Backends)) - for id := range document.Backends { - ids = append(ids, id) - } - sort.Strings(ids) - settings.Backends = make([]BackendSettings, 0, len(ids)) - for _, id := range ids { - backend := document.Backends[id] - settings.Backends = append(settings.Backends, BackendSettings{ - ID: id, - Endpoint: backend.Endpoint, - APIKeyEnv: backend.APIKeyEnv, - ExtraParams: backend.ExtraParams, - ConcurrencyLimit: backend.ConcurrencyLimit, - QueueCapacity: backend.QueueCapacity, - }) - } + if document.PromptKitSources != nil { + settings.PromptKitSources = append([]SourceSelection{}, document.PromptKitSources...) } return settings } diff --git a/internal/config/load_test.go b/internal/config/load_test.go index e2236cb..4fea8d9 100644 --- a/internal/config/load_test.go +++ b/internal/config/load_test.go @@ -29,19 +29,19 @@ func TestLoadSelectsOrderedSourceAndKeepsItsSnapshot(t *testing.T) { dir := t.TempDir() first := filepath.Join(dir, "first.yml") second := filepath.Join(dir, "second.yml") - writeApplicationFile(t, first, "prompt_dir: ./first\n") - writeApplicationFile(t, second, "prompt_dir: ./second\n") + writeApplicationFile(t, first, "promptkit_sources:\n - file: ./first.yml\n") + writeApplicationFile(t, second, "promptkit_sources:\n - directory: ./second\n") useConfigSearchPaths(t, first, second) result, err := Load(LoadOptions{Path: filepath.Join(dir, "ignored.yml")}) if err != nil { t.Fatalf("load first implicit source: %v", err) } - if result.Config.PromptDir != "first" || len(result.Sources()) != 1 || result.Sources()[0].Path != first { + if !reflect.DeepEqual(result.Config.PromptKitSources, []SourceSelection{{File: "first.yml"}}) || len(result.Sources()) != 1 || result.Sources()[0].Path != first { t.Fatalf("unexpected implicit source: config=%+v sources=%+v", result.Config, result.Sources()) } - writeApplicationFile(t, first, "prompt_dir: ./changed\n") - if result.Config.PromptDir != "first" { + writeApplicationFile(t, first, "promptkit_sources:\n - file: ./changed.yml\n") + if !reflect.DeepEqual(result.Config.PromptKitSources, []SourceSelection{{File: "first.yml"}}) { t.Fatalf("result did not retain resolved snapshot: %+v", result.Config) } @@ -49,7 +49,7 @@ func TestLoadSelectsOrderedSourceAndKeepsItsSnapshot(t *testing.T) { t.Fatal(err) } result, err = Load(LoadOptions{}) - if err != nil || result.Config.PromptDir != "second" || result.Sources()[0].Path != second { + if err != nil || !reflect.DeepEqual(result.Config.PromptKitSources, []SourceSelection{{Directory: "second"}}) || result.Sources()[0].Path != second { t.Fatalf("second implicit source not selected: result=%+v err=%v", result, err) } @@ -124,8 +124,11 @@ func TestLoadDefaultsOnlyAndOverridesBeforeValidation(t *testing.T) { } } -func TestSettingsCopiesResolvedValuesAndSortsBackends(t *testing.T) { +func TestSettingsCopiesResolvedValuesAndSourceOrder(t *testing.T) { result, err := loadDocumentBytes(`schema_dir: "" +promptkit_sources: + - directory: ./profiles + - file: ./framework.yml transient_retries: 0 server: addr: "" @@ -133,13 +136,6 @@ server: max_request_bytes: 0 defaults: render_format: json -backends: - zed: - endpoint: http://zed.invalid/v1 - extra_params: - count: 4 - alpha: - queue_capacity: 0 `) if err != nil { t.Fatal(err) @@ -151,17 +147,11 @@ backends: if settings.TransientRetries == nil || *settings.TransientRetries != 0 { t.Fatalf("retry presence lost: %+v", settings.TransientRetries) } - if len(settings.Backends) != 2 || settings.Backends[0].ID != "alpha" || settings.Backends[1].ID != "zed" { - t.Fatalf("backend order changed: %+v", settings.Backends) - } - if settings.Backends[0].QueueCapacity == nil || *settings.Backends[0].QueueCapacity != 0 || settings.Backends[1].QueueCapacity != nil { - t.Fatalf("queue presence changed: %+v", settings.Backends) - } - if value := settings.Backends[1].ExtraParams["count"]; value != int64(4) { - t.Fatalf("dynamic number changed shape: %T %v", value, value) + if want := []SourceSelection{{Directory: "profiles"}, {File: "framework.yml"}}; !reflect.DeepEqual(settings.PromptKitSources, want) { + t.Fatalf("source ordering changed: got=%+v want=%+v", settings.PromptKitSources, want) } - if got := Settings(documentDefaults()); got.TransientRetries != nil || got.Backends != nil { + if got := Settings(documentDefaults()); got.TransientRetries != nil || got.PromptKitSources != nil { t.Fatalf("default nullable values changed: %+v", got) } } @@ -188,25 +178,29 @@ func TestLoadComposesExplicitImportsAndSelectedOverlay(t *testing.T) { t.Fatal(err) } base := filepath.Join(dir, "config.yml") - writeApplicationFile(t, base, "configkit:\n imports:\n - glob: fragments/*.yml\nprompt_dir: ./base-prompts\nserver:\n addr: :8081\n") - writeApplicationFile(t, filepath.Join(dir, "fragments", "a.yml"), "backends:\n alpha:\n endpoint: http://alpha.invalid/v1\n") - writeApplicationFile(t, filepath.Join(dir, "fragments", "b.yml"), "backends:\n beta:\n endpoint: http://beta.invalid/v1\n") - writeApplicationFile(t, filepath.Join(dir, "overlays", "local.yml"), "prompt_dir: ./overlay-prompts\nserver:\n addr: :9090\n") + writeApplicationFile(t, base, "configkit:\n imports:\n - glob: fragments/*.yml\npromptkit_sources:\n - file: ./base-framework.yml\n") + writeApplicationFile(t, filepath.Join(dir, "fragments", "a.yml"), "server:\n addr: :8081\n") + writeApplicationFile(t, filepath.Join(dir, "fragments", "b.yml"), "defaults:\n render_format: json\n") + writeApplicationFile(t, filepath.Join(dir, "overlays", "local.yml"), "promptkit_sources:\n - directory: ./overlay-framework\nserver:\n addr: :9090\n") result, err := Load(LoadOptions{Path: base, Explicit: true}) - if err != nil || result.Config.PromptDir != "base-prompts" || len(result.Config.Backends) != 2 || len(result.Sources()) != 3 { + if err != nil || !reflect.DeepEqual(result.Config.PromptKitSources, []SourceSelection{{File: "base-framework.yml"}}) || result.Config.Server.Addr != ":8081" || result.Config.Defaults.RenderFormat != "json" || len(result.Sources()) != 3 { t.Fatalf("disjoint imports did not compose: result=%+v err=%v", result, err) } result, err = Load(LoadOptions{Path: base, Explicit: true, Overlay: "local", Overrides: []configkit.Override{{ - Path: configkit.Keys("server", "addr"), Value: ":7070", Flag: "--addr", + Path: configkit.Keys("promptkit_sources"), Value: []any{map[string]any{"file": "./cli-framework.yml"}}, Flag: "--promptkit-source", }}}) - if err != nil || result.Config.PromptDir != "overlay-prompts" || result.Config.Server.Addr != ":7070" || len(result.Sources()) != 5 { + if err != nil || !reflect.DeepEqual(result.Config.PromptKitSources, []SourceSelection{{File: "cli-framework.yml"}}) || result.Config.Server.Addr != ":9090" || len(result.Sources()) != 5 { t.Fatalf("overlay or explicit override changed: result=%+v err=%v", result, err) } + result, err = Load(LoadOptions{Path: base, Explicit: true, Overlay: "local"}) + if err != nil || !reflect.DeepEqual(result.Config.PromptKitSources, []SourceSelection{{Directory: "overlay-framework"}}) { + t.Fatalf("overlay did not replace the source list: result=%+v err=%v", result, err) + } if _, err := Load(LoadOptions{Path: base, Explicit: true, Overlay: "missing"}); err == nil { t.Fatal("missing selected overlay accepted") } - writeApplicationFile(t, filepath.Join(dir, "fragments", "b.yml"), "backends:\n alpha:\n endpoint: http://conflict.invalid/v1\n") + writeApplicationFile(t, filepath.Join(dir, "fragments", "b.yml"), "server:\n addr: :8082\n") if _, err := Load(LoadOptions{Path: base, Explicit: true}); err == nil { t.Fatal("conflicting import accepted") } else { @@ -217,89 +211,6 @@ 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 { diff --git a/internal/config/settings.go b/internal/config/settings.go index df10488..9d34585 100644 --- a/internal/config/settings.go +++ b/internal/config/settings.go @@ -14,8 +14,7 @@ var defaultConfigSearchPaths = []string{ // AppSettings holds the validated application values used by executable adapters. type AppSettings struct { - PromptDir string - ProfileDir string + PromptKitSources []SourceSelection SchemaDir string ServerAddr string ArtifactRoot string @@ -23,15 +22,5 @@ type AppSettings struct { MaxArtifactBytes int64 MaxResponseBytes int64 DefaultRenderFormat renderformat.PreparedRunOutputFormat - Backends []BackendSettings TransientRetries *int } - -type BackendSettings struct { - ID string - Endpoint string - APIKeyEnv string - ExtraParams map[string]any - ConcurrencyLimit int - QueueCapacity *int -}