Replace legacy framework config with source selectors

This commit is contained in:
2026-10-04 11:26:40 +00:00
parent c8e2b972f3
commit 0d5ecfa842
6 changed files with 140 additions and 265 deletions

View File

@@ -172,7 +172,13 @@ there are no unpublished substitutions or unrelated dependency upgrades.
## Stage 2: Cut Over The Application Schema And Its Tests ## 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`, Files: `internal/config/document.go`, `settings.go`, `load.go`,
`document_test.go`, `load_test.go`; related obsolete default constants only if `document_test.go`, `load_test.go`; related obsolete default constants only if

View File

@@ -11,21 +11,17 @@ import (
// Document is Scriptorium's ConfigKit-native application configuration shape. // Document is Scriptorium's ConfigKit-native application configuration shape.
type Document struct { type Document struct {
PromptDir string `yaml:"prompt_dir"` PromptKitSources []SourceSelection `yaml:"promptkit_sources"`
ProfileDir string `yaml:"profile_dir"`
SchemaDir string `yaml:"schema_dir"` SchemaDir string `yaml:"schema_dir"`
TransientRetries *int `yaml:"transient_retries"` TransientRetries *int `yaml:"transient_retries"`
Server ServerDocument `yaml:"server"` Server ServerDocument `yaml:"server"`
Defaults DefaultsDocument `yaml:"defaults"` Defaults DefaultsDocument `yaml:"defaults"`
Backends map[string]BackendDocument `yaml:"backends"`
} }
type BackendDocument struct { // SourceSelection identifies one PromptKit configuration file or directory.
Endpoint string `yaml:"endpoint"` type SourceSelection struct {
APIKeyEnv string `yaml:"api_key_env"` File string `yaml:"file"`
ExtraParams map[string]any `yaml:"extra_params"` Directory string `yaml:"directory"`
ConcurrencyLimit int `yaml:"concurrency_limit"`
QueueCapacity *int `yaml:"queue_capacity"`
} }
type ServerDocument struct { type ServerDocument struct {
@@ -62,14 +58,16 @@ func documentDefaults() Document {
} }
func resolveDocument(document *Document, resolution *configkit.Resolution) error { 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) cleaned := strings.TrimSpace(*value)
if cleaned != "" { if cleaned != "" {
cleaned = filepath.Clean(cleaned) cleaned = filepath.Clean(cleaned)
} }
if cleaned != *value { if cleaned != *value {
configkit.Normalize(resolution, path, value, cleaned, "normalized application path") configkit.Normalize(resolution, path, value, cleaned, "normalized application path")
return true
} }
return false
} }
normalizeString := func(path configkit.Path, value *string, normalized, reason string) { normalizeString := func(path configkit.Path, value *string, normalized, reason string) {
if normalized != *value { if normalized != *value {
@@ -77,8 +75,15 @@ func resolveDocument(document *Document, resolution *configkit.Resolution) error
} }
} }
normalizePath(configkit.Keys("prompt_dir"), &document.PromptDir) sourcePathsChanged := false
normalizePath(configkit.Keys("profile_dir"), &document.ProfileDir) 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("schema_dir"), &document.SchemaDir)
normalizePath(configkit.Keys("server", "artifact_root"), &document.Server.ArtifactRoot) normalizePath(configkit.Keys("server", "artifact_root"), &document.Server.ArtifactRoot)
normalizeString(configkit.Keys("server", "addr"), &document.Server.Addr, 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) { 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 { if document.TransientRetries != nil && *document.TransientRetries < 0 {
validation.Error(configkit.Keys("transient_retries"), "must be greater than or equal to zero") validation.Error(configkit.Keys("transient_retries"), "must be greater than or equal to zero")
} }

View File

@@ -25,8 +25,9 @@ func TestDocumentDefaultsAndExplicitValues(t *testing.T) {
t.Fatalf("unexpected defaults-only result: %+v sources=%+v", defaultsOnly.Config, defaultsOnly.Sources()) t.Fatalf("unexpected defaults-only result: %+v sources=%+v", defaultsOnly.Config, defaultsOnly.Sources())
} }
result, err := loadDocumentBytes(`prompt_dir: "" result, err := loadDocumentBytes(`promptkit_sources:
profile_dir: "" - file: " ./bundle/../framework.yml "
- directory: "./profiles"
schema_dir: "" schema_dir: ""
transient_retries: 0 transient_retries: 0
server: server:
@@ -37,12 +38,6 @@ server:
max_response_bytes: 0 max_response_bytes: 0
defaults: defaults:
render_format: " JSON " render_format: " JSON "
backends:
local:
endpoint: "http://fixture.invalid/v1"
queue_capacity: 0
extra_params:
sample_count: 23
`) `)
if err != nil { if err != nil {
t.Fatalf("explicit-value load: %v", err) t.Fatalf("explicit-value load: %v", err)
@@ -54,114 +49,70 @@ backends:
if config.TransientRetries == nil || *config.TransientRetries != 0 { if config.TransientRetries == nil || *config.TransientRetries != 0 {
t.Fatalf("explicit zero retry budget lost: %+v", config.TransientRetries) 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) { if want := []SourceSelection{{File: "framework.yml"}, {Directory: "profiles"}}; !reflect.DeepEqual(config.PromptKitSources, want) {
t.Fatalf("backend presence or dynamic integer changed: %+v", backend) t.Fatalf("source selectors did not decode: %+v", config.PromptKitSources)
} }
if config.Defaults.RenderFormat != "json" { if config.Defaults.RenderFormat != "json" {
t.Fatalf("render format normalization failed: %q", config.Defaults.RenderFormat) 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 { if err != nil {
t.Fatalf("nullable-value load: %v", err) 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) 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) { func TestDocumentSourceSelectionsDoNotAcquirePaths(t *testing.T) {
result, err := loadDocumentBytes(`backends: for _, tc := range []struct {
base: name string
endpoint: http://base.invalid/v1 content string
api_key_env: BASE_API_KEY want []SourceSelection
concurrency_limit: 2 }{
queue_capacity: 4 {name: "omitted", content: "{}\n"},
extra_params: {name: "null", content: "promptkit_sources: null\n"},
enabled: true {name: "empty", content: "promptkit_sources: []\n"},
shared: visible {name: "nonexistent file", content: "promptkit_sources:\n - file: ./missing/framework.yml\n", want: []SourceSelection{{File: "missing/framework.yml"}}},
count: 7 {name: "nonexistent directory", content: "promptkit_sources:\n - directory: ./missing/framework\n", want: []SourceSelection{{Directory: "missing/framework"}}},
child: } {
extends: base t.Run(tc.name, func(t *testing.T) {
queue_capacity: 0 result, err := loadDocumentBytes(tc.content)
extra_params:
enabled: false
nullable-child:
extends: base
queue_capacity: null
`)
if err != nil { if err != nil {
t.Fatalf("load inherited document: %v", err) 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"] func TestSettingsCopiesPromptKitSourcesInOrder(t *testing.T) {
if child.Endpoint != "http://base.invalid/v1" || child.APIKeyEnv != "BASE_API_KEY" || child.ConcurrencyLimit != 2 { document := Document{PromptKitSources: []SourceSelection{
t.Fatalf("child did not inherit backend settings: %+v", child) {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 { document.PromptKitSources[0].File = "changed.yml"
t.Fatalf("explicit zero queue capacity was not retained: %+v", child.QueueCapacity) 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) { if empty := Settings(Document{PromptKitSources: []SourceSelection{}}).PromptKitSources; empty == nil {
t.Fatalf("child parameters were not combined with explicit presence: %#v", child.ExtraParams) t.Fatal("explicit empty source list was not copied")
}
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) { func TestDocumentRecordsEffectiveNormalization(t *testing.T) {
result, err := loadDocumentBytes(`prompt_dir: " ./one/../two " result, err := loadDocumentBytes(`promptkit_sources:
profile_dir: " " - file: " ./one/../two "
- file: "./blank.yml"
directory: " "
- directory: "./three/../three"
server: server:
artifact_root: " ./data/../artifacts " artifact_root: " ./data/../artifacts "
addr: " :9000 " addr: " :9000 "
@@ -171,19 +122,22 @@ defaults:
if err != nil { if err != nil {
t.Fatalf("load normalized document: %v", err) 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) t.Fatalf("unexpected normalized values: %+v", result.Config)
} }
var effective bytes.Buffer var effective bytes.Buffer
if err := result.WriteEffective(&effective); err != nil { if err := result.WriteEffective(&effective); err != nil {
t.Fatalf("write effective configuration: %v", err) 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) { if !strings.Contains(effective.String(), fragment) {
t.Fatalf("effective output omitted %q: %s", fragment, effective.String()) 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" { if !known || explanation.Effective.YAML != "two\n" {
t.Fatalf("explanation does not agree with effective value: %+v known=%t", explanation, known) t.Fatalf("explanation does not agree with effective value: %+v known=%t", explanation, known)
} }
@@ -196,10 +150,26 @@ defaults:
if !found { if !found {
t.Fatalf("normalization was not recorded: %+v", explanation.History) 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) { func TestDocumentValidationIsSafeAndPathSpecific(t *testing.T) {
for _, tc := range []struct{ name, content, path string }{ 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 retry", "transient_retries: -9173\n", "/transient_retries"},
{"negative request limit", "server:\n max_request_bytes: -9173\n", "/server/max_request_bytes"}, {"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"}, {"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 { if diagnostic.Code == configkit.CodeValidateError && diagnostic.Path.String() == tc.path {
found = true 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) t.Fatalf("diagnostic disclosed rejected value: %+v", diagnostic)
} }
} }
@@ -237,7 +207,13 @@ func TestDocumentRejectsNativeShapeErrors(t *testing.T) {
"server:\n max_request_bytes: null\n", "server:\n max_request_bytes: null\n",
"defaults:\n render_format: null\n", "defaults:\n render_format: null\n",
"server:\n unknown_field: 1\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) _, err := loadDocumentBytes(content)
if err == nil { if err == nil {

View File

@@ -4,7 +4,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"os" "os"
"sort"
configkit "gitea.maximumdirect.net/eric/ConfigKit" configkit "gitea.maximumdirect.net/eric/ConfigKit"
renderformat "gitea.maximumdirect.net/eric/scriptorium/internal/format" 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. // Settings converts a validated document without another merge or normalization.
func Settings(document Document) AppSettings { func Settings(document Document) AppSettings {
settings := AppSettings{ settings := AppSettings{
PromptDir: document.PromptDir,
ProfileDir: document.ProfileDir,
SchemaDir: document.SchemaDir, SchemaDir: document.SchemaDir,
ServerAddr: document.Server.Addr, ServerAddr: document.Server.Addr,
ArtifactRoot: document.Server.ArtifactRoot, ArtifactRoot: document.Server.ArtifactRoot,
@@ -73,24 +70,8 @@ func Settings(document Document) AppSettings {
DefaultRenderFormat: renderformat.PreparedRunOutputFormat(document.Defaults.RenderFormat), DefaultRenderFormat: renderformat.PreparedRunOutputFormat(document.Defaults.RenderFormat),
TransientRetries: document.TransientRetries, TransientRetries: document.TransientRetries,
} }
if document.Backends != nil { if document.PromptKitSources != nil {
ids := make([]string, 0, len(document.Backends)) settings.PromptKitSources = append([]SourceSelection{}, document.PromptKitSources...)
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,
})
}
} }
return settings return settings
} }

View File

@@ -29,19 +29,19 @@ func TestLoadSelectsOrderedSourceAndKeepsItsSnapshot(t *testing.T) {
dir := t.TempDir() dir := t.TempDir()
first := filepath.Join(dir, "first.yml") first := filepath.Join(dir, "first.yml")
second := filepath.Join(dir, "second.yml") second := filepath.Join(dir, "second.yml")
writeApplicationFile(t, first, "prompt_dir: ./first\n") writeApplicationFile(t, first, "promptkit_sources:\n - file: ./first.yml\n")
writeApplicationFile(t, second, "prompt_dir: ./second\n") writeApplicationFile(t, second, "promptkit_sources:\n - directory: ./second\n")
useConfigSearchPaths(t, first, second) useConfigSearchPaths(t, first, second)
result, err := Load(LoadOptions{Path: filepath.Join(dir, "ignored.yml")}) result, err := Load(LoadOptions{Path: filepath.Join(dir, "ignored.yml")})
if err != nil { if err != nil {
t.Fatalf("load first implicit source: %v", err) 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()) t.Fatalf("unexpected implicit source: config=%+v sources=%+v", result.Config, result.Sources())
} }
writeApplicationFile(t, first, "prompt_dir: ./changed\n") writeApplicationFile(t, first, "promptkit_sources:\n - file: ./changed.yml\n")
if result.Config.PromptDir != "first" { if !reflect.DeepEqual(result.Config.PromptKitSources, []SourceSelection{{File: "first.yml"}}) {
t.Fatalf("result did not retain resolved snapshot: %+v", result.Config) t.Fatalf("result did not retain resolved snapshot: %+v", result.Config)
} }
@@ -49,7 +49,7 @@ func TestLoadSelectsOrderedSourceAndKeepsItsSnapshot(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
result, err = Load(LoadOptions{}) 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) 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: "" result, err := loadDocumentBytes(`schema_dir: ""
promptkit_sources:
- directory: ./profiles
- file: ./framework.yml
transient_retries: 0 transient_retries: 0
server: server:
addr: "" addr: ""
@@ -133,13 +136,6 @@ server:
max_request_bytes: 0 max_request_bytes: 0
defaults: defaults:
render_format: json render_format: json
backends:
zed:
endpoint: http://zed.invalid/v1
extra_params:
count: 4
alpha:
queue_capacity: 0
`) `)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@@ -151,17 +147,11 @@ backends:
if settings.TransientRetries == nil || *settings.TransientRetries != 0 { if settings.TransientRetries == nil || *settings.TransientRetries != 0 {
t.Fatalf("retry presence lost: %+v", settings.TransientRetries) t.Fatalf("retry presence lost: %+v", settings.TransientRetries)
} }
if len(settings.Backends) != 2 || settings.Backends[0].ID != "alpha" || settings.Backends[1].ID != "zed" { if want := []SourceSelection{{Directory: "profiles"}, {File: "framework.yml"}}; !reflect.DeepEqual(settings.PromptKitSources, want) {
t.Fatalf("backend order changed: %+v", settings.Backends) t.Fatalf("source ordering changed: got=%+v want=%+v", settings.PromptKitSources, want)
}
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 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) t.Fatalf("default nullable values changed: %+v", got)
} }
} }
@@ -188,25 +178,29 @@ func TestLoadComposesExplicitImportsAndSelectedOverlay(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
base := filepath.Join(dir, "config.yml") 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, base, "configkit:\n imports:\n - glob: fragments/*.yml\npromptkit_sources:\n - file: ./base-framework.yml\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", "a.yml"), "server:\n addr: :8081\n")
writeApplicationFile(t, filepath.Join(dir, "fragments", "b.yml"), "backends:\n beta:\n endpoint: http://beta.invalid/v1\n") writeApplicationFile(t, filepath.Join(dir, "fragments", "b.yml"), "defaults:\n render_format: json\n")
writeApplicationFile(t, filepath.Join(dir, "overlays", "local.yml"), "prompt_dir: ./overlay-prompts\nserver:\n addr: :9090\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}) 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) 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{{ 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) 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 { if _, err := Load(LoadOptions{Path: base, Explicit: true, Overlay: "missing"}); err == nil {
t.Fatal("missing selected overlay accepted") 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 { if _, err := Load(LoadOptions{Path: base, Explicit: true}); err == nil {
t.Fatal("conflicting import accepted") t.Fatal("conflicting import accepted")
} else { } 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) { func TestLoadRejectsOverlayWithoutBase(t *testing.T) {
useConfigSearchPaths(t, filepath.Join(t.TempDir(), "absent.yml")) useConfigSearchPaths(t, filepath.Join(t.TempDir(), "absent.yml"))
if _, err := Load(LoadOptions{Overlay: "local"}); err == nil { if _, err := Load(LoadOptions{Overlay: "local"}); err == nil {

View File

@@ -14,8 +14,7 @@ var defaultConfigSearchPaths = []string{
// AppSettings holds the validated application values used by executable adapters. // AppSettings holds the validated application values used by executable adapters.
type AppSettings struct { type AppSettings struct {
PromptDir string PromptKitSources []SourceSelection
ProfileDir string
SchemaDir string SchemaDir string
ServerAddr string ServerAddr string
ArtifactRoot string ArtifactRoot string
@@ -23,15 +22,5 @@ type AppSettings struct {
MaxArtifactBytes int64 MaxArtifactBytes int64
MaxResponseBytes int64 MaxResponseBytes int64
DefaultRenderFormat renderformat.PreparedRunOutputFormat DefaultRenderFormat renderformat.PreparedRunOutputFormat
Backends []BackendSettings
TransientRetries *int TransientRetries *int
} }
type BackendSettings struct {
ID string
Endpoint string
APIKeyEnv string
ExtraParams map[string]any
ConcurrencyLimit int
QueueCapacity *int
}