From 733540f936493032a60b789f45d46a87da373742 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Fri, 20 Nov 2026 04:26:17 +0000 Subject: [PATCH] Adopt native configuration discovery and a single PromptKit entry file --- docs/roadmap/implementation.md | 24 +- internal/adapter/cli/app.go | 25 +- internal/adapter/cli/app_test.go | 13 + internal/adapter/cli/application_config.go | 31 -- internal/adapter/cli/config.go | 1 - internal/adapter/cli/engine.go | 24 ++ internal/adapter/cli/framework_config.go | 3 - internal/adapter/cli/framework_sources.go | 58 --- internal/adapter/cli/run.go | 39 +- internal/adapter/cli/selection.go | 97 +++++ internal/adapter/cli/selection_test.go | 155 +++++++ internal/config/document.go | 42 +- internal/config/document_test.go | 262 ++++-------- internal/config/load.go | 27 +- internal/config/load_test.go | 452 +++++++-------------- internal/config/settings.go | 29 +- 16 files changed, 585 insertions(+), 697 deletions(-) create mode 100644 internal/adapter/cli/engine.go create mode 100644 internal/adapter/cli/selection.go create mode 100644 internal/adapter/cli/selection_test.go diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index a8346c2..a95355c 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -239,13 +239,23 @@ paths against the selected commits. Final composed behavior is checked in Stage ## Stage 2: Adapt Authoritative Application Loading And Shared Selectors -**Status: In progress; override decision resolved.** Adopt ConfigKit v1.2.1's -native conflict behavior; no upstream change or dependency upgrade is required. -The former named-selector/`--set` precedence test is obsolete: configuration -commands no longer expose named application overrides. Cover native override -conflicts directly and HTTP named overrides on `serve`. Previously reported -configuration and metadata/selection-error checks do not establish completion -of these revised requirements. +**Status: Complete.** Application loading uses the selected native directory +presets, strict nested `promptkit` acquisition settings, and ConfigKit's native +override/conflict handling. The shared engine uses one optional import-enabled +main file and retains the bundled underlay. Removed the render default, +source/schema bindings, source classification, and parser-owned application +override collector. HTTP bindings retain explicit presence and `int64` limits. + +Verified on Go 1.27.4: configuration package tests and race checks, plus focused +native composition/loading/engine tests and race checks using the new source +files directly. Coverage includes discovery order and filenames, optional search +exhaustion isolated from host system configuration, explicit-empty selections, +imports/overlays, duplicate and overlapping override evidence, normalization, +HTTP empty/zero/full-width values, fresh loads, cancellation, removed flags, and +collection-relative message resources. Full-package compilation is intentionally +deferred: the unreachable old CLI files/tests still reference retired settings +and helpers and will be removed/replaced in Stage 8. Do not interpret the focused +checks as an assembled-application validation; finish that in Stage 9. **Discovery and source decisions: Settled.** Implement the ordered native search and single external framework entry file specified above. Remove source/schema diff --git a/internal/adapter/cli/app.go b/internal/adapter/cli/app.go index b4dba74..ca2a83c 100644 --- a/internal/adapter/cli/app.go +++ b/internal/adapter/cli/app.go @@ -2,15 +2,11 @@ package cli import ( "context" - "errors" "io" - appconfig "gitea.maximumdirect.net/eric/scriptorium/internal/config" "go.maximumdirect.net/ckconfig" "go.maximumdirect.net/ckprompt" "go.maximumdirect.net/commandkit" - "go.maximumdirect.net/configkit" - "go.maximumdirect.net/promptkit" ) // Execute composes the native CLI and returns its finalized outcome. The caller @@ -26,13 +22,16 @@ func Execute(ctx context.Context, identity commandkit.Identity, args []string, s func newApp(identity commandkit.Identity) (*commandkit.App, error) { application := ckconfig.FromConfigKit("app", "Scriptorium application configuration.", loadApplication) framework := ckprompt.ConfigScope(loadEngine) - configuration := ckconfig.Command(ckconfig.Suite[struct{}, struct{}]{ - Scopes: []ckconfig.Scope[struct{}, struct{}]{application, framework}, + configuration := ckconfig.Command(ckconfig.Suite[applicationInput, struct{}]{ + NewInput: func() applicationInput { return applicationInput{} }, + Scopes: []ckconfig.Scope[applicationInput, struct{}]{application, framework}, }) definitions := ckprompt.FromEngine(loadEngine) declarations := []commandkit.Declaration{ commandkit.NewCommand(commandkit.CommandSpec[struct{}]{Name: "run", Summary: "Run a stored prompt.", Run: commandNotImplemented}), - commandkit.NewCommand(commandkit.CommandSpec[struct{}]{Name: "serve", Summary: "Serve the HTTP API.", Run: commandNotImplemented}), + commandkit.NewCommand(commandkit.CommandSpec[serveInput]{Name: "serve", Summary: "Serve the HTTP API.", Flags: serveFlags(), Run: func(commandkit.Invocation, serveInput) commandkit.Outcome { + return commandkit.Failure("Server startup is not implemented.") + }}), configuration, commandkit.Completion("scriptorium"), } @@ -40,14 +39,14 @@ func newApp(identity commandkit.Identity) (*commandkit.App, error) { return commandkit.New(identity, declarations...) } -// Application acquisition and effects are filled in independently of command -// declaration construction; metadata operations must never reach these callbacks. -func loadApplication(context.Context, ckconfig.Selection, struct{}, struct{}) (*configkit.Result[appconfig.Document], error) { - return nil, errors.New("application configuration acquisition is not implemented") +type serveInput struct { + Selection ckconfig.Selection + Application applicationInput } -func loadEngine(context.Context, ckconfig.Selection, struct{}, struct{}) (*promptkit.Engine, error) { - return nil, errors.New("engine acquisition is not implemented") +func serveFlags() []commandkit.Flag[serveInput] { + flags := configurationSelectors(func(input *serveInput) *ckconfig.Selection { return &input.Selection }) + return append(flags, serverSelectors(func(input *serveInput) *applicationInput { return &input.Application })...) } func commandNotImplemented(commandkit.Invocation, struct{}) commandkit.Outcome { diff --git a/internal/adapter/cli/app_test.go b/internal/adapter/cli/app_test.go index 3c667e1..a6c998d 100644 --- a/internal/adapter/cli/app_test.go +++ b/internal/adapter/cli/app_test.go @@ -29,3 +29,16 @@ func TestNativeMetadataWithoutConfiguration(t *testing.T) { }) } } + +func TestRemovedSourceAndSchemaFlagsAreRejected(t *testing.T) { + for _, command := range [][]string{{"run"}, {"serve"}, {"config", "show"}, {"promptkit", "list", "prompts"}} { + for _, flag := range []string{"--promptkit-source=missing.yml", "--schema-dir=schemas"} { + args := append(append([]string{}, command...), flag) + var output, diagnostics bytes.Buffer + outcome := Execute(context.Background(), commandkit.Identity{Name: "scriptorium"}, args, &output, &diagnostics) + if outcome.Kind != commandkit.OutcomeUsage || outcome.Status != 2 { + t.Fatalf("%v: %+v", args, outcome) + } + } + } +} diff --git a/internal/adapter/cli/application_config.go b/internal/adapter/cli/application_config.go index cfe2133..ed82519 100644 --- a/internal/adapter/cli/application_config.go +++ b/internal/adapter/cli/application_config.go @@ -2,7 +2,6 @@ package cli import ( "errors" - "flag" "fmt" "io" @@ -10,36 +9,6 @@ import ( "go.maximumdirect.net/configkit" ) -// applicationOverrides keeps explicit application flags separate from invocation state. -func applicationOverrides(fs *flag.FlagSet) []configkit.Override { - var overrides []configkit.Override - fs.Visit(func(selected *flag.Flag) { - var path configkit.Path - switch selected.Name { - case "promptkit-source": - path = configkit.Keys("promptkit_sources") - case "schema-dir": - path = configkit.Keys("schema_dir") - case "addr": - path = configkit.Keys("server", "addr") - case "artifact-root": - path = configkit.Keys("server", "artifact_root") - case "max-request-bytes": - path = configkit.Keys("server", "max_request_bytes") - case "max-artifact-bytes": - path = configkit.Keys("server", "max_artifact_bytes") - case "max-response-bytes": - path = configkit.Keys("server", "max_response_bytes") - default: - return - } - overrides = append(overrides, configkit.Override{ - Path: path, Value: selected.Value.(flag.Getter).Get(), Flag: "--" + selected.Name, - }) - }) - return overrides -} - func loadApplicationSettings(configPath, overlay string, explicit bool, overrides []configkit.Override) (appconfig.AppSettings, []configkit.Diagnostic, error) { result, err := appconfig.Load(appconfig.LoadOptions{ Path: configPath, Overlay: overlay, Explicit: explicit, Overrides: overrides, diff --git a/internal/adapter/cli/config.go b/internal/adapter/cli/config.go index 82fca0b..9a60229 100644 --- a/internal/adapter/cli/config.go +++ b/internal/adapter/cli/config.go @@ -93,7 +93,6 @@ func configCommand(args []string, stdout, stderr io.Writer) int { } func registerConfigurationOverrides(fs *flag.FlagSet) { - fs.String("schema-dir", "", "schema directory override") fs.String("addr", "", "HTTP listen address override") fs.String("artifact-root", "", "HTTP artifact root override") fs.Int64("max-request-bytes", 0, "HTTP request byte limit override") diff --git a/internal/adapter/cli/engine.go b/internal/adapter/cli/engine.go new file mode 100644 index 0000000..11e2dc6 --- /dev/null +++ b/internal/adapter/cli/engine.go @@ -0,0 +1,24 @@ +package cli + +import ( + "fmt" + + appconfig "gitea.maximumdirect.net/eric/scriptorium/internal/config" + "go.maximumdirect.net/configkit" + "go.maximumdirect.net/promptkit" +) + +func newEngine(settings appconfig.AppSettings, options ...promptkit.Option) (*promptkit.Engine, error) { + var main *configkit.Input + if settings.PromptKitConfiguration != "" { + main = &configkit.Input{Source: configkit.File(settings.PromptKitConfiguration), Imports: true} + } + engine, err := promptkit.NewEngine(promptkit.Config{ + Sources: promptkit.ConfigurationSources{Main: main}, + SchemaDir: settings.SchemaDir, TransientRetries: settings.TransientRetries, + }, options...) + if err != nil { + return nil, fmt.Errorf("engine initialization from application configuration: %w", err) + } + return engine, nil +} diff --git a/internal/adapter/cli/framework_config.go b/internal/adapter/cli/framework_config.go index 3bd1c29..fbef4bc 100644 --- a/internal/adapter/cli/framework_config.go +++ b/internal/adapter/cli/framework_config.go @@ -110,9 +110,6 @@ func parseFrameworkConfigurationArgs(args []string) (*frameworkConfigurationConf fs := flag.NewFlagSet("inspect configuration "+cfg.mode, flag.ContinueOnError) fs.SetOutput(io.Discard) registerConfigSelectionFlags(fs, &cfg.configPath, &cfg.configOverlay) - var promptkitSources promptKitSourceFlagValue - fs.Var(&promptkitSources, "promptkit-source", "selected PromptKit source path (repeatable)") - fs.StringVar(&cfg.schemaDir, "schema-dir", "", "base directory for validation schemas") fs.StringVar(&cfg.path, "path", "", "JSON Pointer to explain (required for explain)") if err := fs.Parse(args[1:]); err != nil { return nil, err diff --git a/internal/adapter/cli/framework_sources.go b/internal/adapter/cli/framework_sources.go index 5a161a6..088f777 100644 --- a/internal/adapter/cli/framework_sources.go +++ b/internal/adapter/cli/framework_sources.go @@ -1,71 +1,13 @@ package cli import ( - "errors" "fmt" "io" - "os" - "path/filepath" - "strings" "go.maximumdirect.net/configkit" "go.maximumdirect.net/promptkit" ) -type promptKitSourceFlagEntry struct { - selector string - path string -} - -// promptKitSourceFlagValue treats each occurrence as one selected source path. -type promptKitSourceFlagValue struct { - entries []promptKitSourceFlagEntry -} - -func (value *promptKitSourceFlagValue) Set(path string) error { - trimmed := strings.TrimSpace(path) - if trimmed == "" { - return errors.New("--promptkit-source requires a nonblank path") - } - - info, err := os.Stat(filepath.Clean(trimmed)) - if err != nil { - reason := "unavailable" - if errors.Is(err, os.ErrPermission) { - reason = "permission denied" - } - return fmt.Errorf("--promptkit-source %q: %s", path, reason) - } - - selector := "" - switch { - case info.IsDir(): - selector = "directory" - case info.Mode().IsRegular(): - selector = "file" - default: - return fmt.Errorf("--promptkit-source %q must be a regular file or directory", path) - } - value.entries = append(value.entries, promptKitSourceFlagEntry{selector: selector, path: path}) - return nil -} - -func (value *promptKitSourceFlagValue) Get() any { - selections := make([]any, 0, len(value.entries)) - for _, entry := range value.entries { - selections = append(selections, map[string]any{entry.selector: entry.path}) - } - return selections -} - -func (value *promptKitSourceFlagValue) String() string { - paths := make([]string, 0, len(value.entries)) - for _, entry := range value.entries { - paths = append(paths, entry.path) - } - return fmt.Sprint(paths) -} - func writeFrameworkDiagnostics(output io.Writer, engine *promptkit.Engine) error { view, err := engine.InspectConfiguration() if err != nil { diff --git a/internal/adapter/cli/run.go b/internal/adapter/cli/run.go index b3ee022..25bdbad 100644 --- a/internal/adapter/cli/run.go +++ b/internal/adapter/cli/run.go @@ -288,7 +288,6 @@ func parseRunArgs(args []string) (*runConfig, error) { registerConfigSelectionFlags(fs, &cfg.configPath, &cfg.configOverlay) registerExecutionRequestFlags(fs, cfg) - fs.StringVar(&cfg.schemaDir, "schema-dir", "", "base directory for validation schemas") fs.DurationVar(&cfg.runTimeout, "run-timeout", 0, "whole-run execution deadline") fs.StringVar(&cfg.executionReportPath, "execution-report", "", "JSON execution report file path") @@ -345,7 +344,6 @@ func parseRenderArgs(args []string) (*renderConfig, error) { registerConfigSelectionFlags(fs, &cfg.configPath, &cfg.configOverlay) registerExecutionRequestFlags(fs, &cfg.runConfig) - fs.StringVar(&cfg.schemaDir, "schema-dir", "", "base directory for validation schemas") var rawFormat string fs.StringVar(&rawFormat, "format", "", "render output format (text|json)") @@ -376,9 +374,6 @@ func parseServeArgs(args []string) (*serveConfig, error) { registerConfigSelectionFlags(fs, &cfg.configPath, &cfg.configOverlay) fs.StringVar(&cfg.addr, "addr", "", "HTTP listen address") - var promptkitSources promptKitSourceFlagValue - fs.Var(&promptkitSources, "promptkit-source", "selected PromptKit source path (repeatable)") - fs.StringVar(&cfg.schemaDir, "schema-dir", "", "base directory for validation schemas") fs.StringVar(&cfg.artifactRoot, "artifact-root", "", "base directory for HTTP file input artifacts") fs.Int64Var(&cfg.maxRequestBytes, "max-request-bytes", 0, "maximum HTTP request body bytes; 0 disables the limit") fs.Int64Var(&cfg.maxArtifactBytes, "max-artifact-bytes", 0, "maximum HTTP file artifact bytes; 0 disables the limit") @@ -410,8 +405,6 @@ func parseServeArgs(args []string) (*serveConfig, error) { } func registerExecutionRequestFlags(fs *flag.FlagSet, cfg *runConfig) { - var promptkitSources promptKitSourceFlagValue - fs.Var(&promptkitSources, "promptkit-source", "selected PromptKit source path (repeatable)") fs.StringVar(&cfg.promptID, "prompt", "", "prompt ID to run") fs.StringVar(&cfg.profileID, "profile", "", "optional execution profile ID; if omitted, prompt default_profile is used") fs.StringVar(&cfg.sessionID, "session-id", "", "optional session ID") @@ -495,7 +488,7 @@ func resolveCommonSettings(fs *flag.FlagSet, configPath, overlay string) (common maxRequestBytes: settings.MaxRequestBytes, maxArtifactBytes: settings.MaxArtifactBytes, maxResponseBytes: settings.MaxResponseBytes, - defaultRenderFormat: settings.DefaultRenderFormat, + defaultRenderFormat: renderformat.DefaultPreparedRunOutputFormat, transientRetries: settings.TransientRetries, diagnostics: diagnostics, }, nil @@ -515,36 +508,6 @@ func (c serveConfig) engineSettings() engineSettings { return engineSettings{promptkitSources: c.promptkitSources, schemaDir: c.schemaDir, transientRetries: c.transientRetries} } -func newEngine(settings engineSettings, options ...promptkit.Option) (*promptkit.Engine, error) { - underlays := make([]configkit.Underlay, 0, len(settings.promptkitSources)) - for index, selection := range settings.promptkitSources { - var inputs []configkit.Input - switch { - case selection.File != "" && selection.Directory == "": - inputs = []configkit.Input{{Source: configkit.File(selection.File), Imports: true}} - case selection.Directory != "" && selection.File == "": - var err error - inputs, err = promptkit.DirectoryInputs(selection.Directory) - if err != nil { - cause := fmt.Errorf("PromptKit source %d directory %q: %w", index+1, selection.Directory, err) - return nil, fmt.Errorf("engine initialization from application configuration: %w", errors.Join(promptkit.ErrInvalidConfig, cause)) - } - default: - cause := fmt.Errorf("PromptKit source %d must select exactly one file or directory", index+1) - return nil, fmt.Errorf("engine initialization from application configuration: %w", errors.Join(promptkit.ErrInvalidConfig, cause)) - } - underlays = append(underlays, configkit.Underlay{Inputs: inputs}) - } - engine, err := promptkit.NewEngine(promptkit.Config{ - Sources: promptkit.ConfigurationSources{Underlays: underlays}, - SchemaDir: settings.schemaDir, - TransientRetries: settings.transientRetries, - }, options...) - if err != nil { - return nil, fmt.Errorf("engine initialization from application configuration: %w", err) - } - return engine, nil -} func buildRunRequestFromConfig(cfg *runConfig) (promptkit.RunRequest, error) { var inputMappings map[string]string diff --git a/internal/adapter/cli/selection.go b/internal/adapter/cli/selection.go new file mode 100644 index 0000000..92a8ffe --- /dev/null +++ b/internal/adapter/cli/selection.go @@ -0,0 +1,97 @@ +package cli + +import ( + "context" + "strconv" + + appconfig "gitea.maximumdirect.net/eric/scriptorium/internal/config" + "go.maximumdirect.net/ckconfig" + "go.maximumdirect.net/commandkit" + "go.maximumdirect.net/configkit" + "go.maximumdirect.net/promptkit" +) + +// applicationInput holds only application-owned selectors. Supplied retains the +// distinction between omission and a caller's empty or zero value. +type applicationInput struct { + Addr commandkit.Supplied[string] + ArtifactRoot commandkit.Supplied[string] + MaxRequestBytes commandkit.Supplied[int64] + MaxArtifactBytes commandkit.Supplied[int64] + MaxResponseBytes commandkit.Supplied[int64] +} + +func configurationSelectors[I any](target func(*I) *ckconfig.Selection) []commandkit.Flag[I] { + return []commandkit.Flag[I]{ + commandkit.ValueFlag(commandkit.FlagMeta{Name: "config", Usage: "Select the application configuration file.", Completion: commandkit.CompleteFiles()}, (*string)(nil), parseString, + func(input *I, value commandkit.Supplied[string]) { target(input).Config = value }), + commandkit.ValueFlag(commandkit.FlagMeta{Name: "overlay", Usage: "Select an application overlay by name."}, (*string)(nil), parseString, + func(input *I, value commandkit.Supplied[string]) { target(input).Overlay = value }), + } +} + +func serverSelectors[I any](target func(*I) *applicationInput) []commandkit.Flag[I] { + stringFlag := func(name, usage string, completion commandkit.CompletionHint, field func(*applicationInput) *commandkit.Supplied[string]) commandkit.Flag[I] { + return commandkit.ValueFlag(commandkit.FlagMeta{Name: name, Usage: usage, Completion: completion}, (*string)(nil), parseString, + func(input *I, value commandkit.Supplied[string]) { *field(target(input)) = value }) + } + limitFlag := func(name, usage string, field func(*applicationInput) *commandkit.Supplied[int64]) commandkit.Flag[I] { + return commandkit.ValueFlag(commandkit.FlagMeta{Name: name, Usage: usage}, (*int64)(nil), func(raw string) (int64, error) { return strconv.ParseInt(raw, 10, 64) }, + func(input *I, value commandkit.Supplied[int64]) { *field(target(input)) = value }) + } + return []commandkit.Flag[I]{ + stringFlag("addr", "HTTP listen address.", commandkit.NoCompletion(), func(input *applicationInput) *commandkit.Supplied[string] { return &input.Addr }), + stringFlag("artifact-root", "HTTP artifact root.", commandkit.CompleteDirectories(), func(input *applicationInput) *commandkit.Supplied[string] { return &input.ArtifactRoot }), + limitFlag("max-request-bytes", "HTTP request byte limit (zero is unlimited).", func(input *applicationInput) *commandkit.Supplied[int64] { return &input.MaxRequestBytes }), + limitFlag("max-artifact-bytes", "HTTP artifact byte limit (zero is unlimited).", func(input *applicationInput) *commandkit.Supplied[int64] { return &input.MaxArtifactBytes }), + limitFlag("max-response-bytes", "HTTP response byte limit (zero is unlimited).", func(input *applicationInput) *commandkit.Supplied[int64] { return &input.MaxResponseBytes }), + } +} + +func parseString(raw string) (string, error) { return raw, nil } + +func namedApplicationOverrides(input applicationInput) []configkit.Override { + var records []configkit.Override + appendString := func(path configkit.Path, flag string, value commandkit.Supplied[string]) { + if value.Set { + records = append(records, configkit.Override{Path: path, Value: value.Value, Flag: flag}) + } + } + appendLimit := func(key, flag string, value commandkit.Supplied[int64]) { + if value.Set { + records = append(records, configkit.Override{Path: configkit.Keys("server", key), Value: value.Value, Flag: flag}) + } + } + appendString(configkit.Keys("server", "addr"), "--addr", input.Addr) + appendString(configkit.Keys("server", "artifact_root"), "--artifact-root", input.ArtifactRoot) + appendLimit("max_request_bytes", "--max-request-bytes", input.MaxRequestBytes) + appendLimit("max_artifact_bytes", "--max-artifact-bytes", input.MaxArtifactBytes) + appendLimit("max_response_bytes", "--max-response-bytes", input.MaxResponseBytes) + return records +} + +func loadApplication(ctx context.Context, selection ckconfig.Selection, input applicationInput, _ struct{}) (*configkit.Result[appconfig.Document], error) { + if err := ctx.Err(); err != nil { + return nil, err + } + result, err := appconfig.Load(appconfig.LoadOptions{ + Path: selection.Config.Value, Explicit: selection.Config.Set, Overlay: selection.Overlay.Value, + Overrides: append(namedApplicationOverrides(input), selection.Overrides...), + }) + if err != nil { + return nil, err + } + if err := ctx.Err(); err != nil { + return nil, err + } + return result, nil +} + +func loadEngine(ctx context.Context, selection ckconfig.Selection, input applicationInput, state struct{}) (*promptkit.Engine, error) { + result, err := loadApplication(ctx, selection, input, state) + if err != nil { + return nil, err + } + settings := appconfig.Settings(result.Config) + return newEngine(settings) +} diff --git a/internal/adapter/cli/selection_test.go b/internal/adapter/cli/selection_test.go new file mode 100644 index 0000000..745ec24 --- /dev/null +++ b/internal/adapter/cli/selection_test.go @@ -0,0 +1,155 @@ +package cli + +import ( + "context" + "errors" + "io" + "math" + "os" + "path/filepath" + "testing" + + appconfig "gitea.maximumdirect.net/eric/scriptorium/internal/config" + "go.maximumdirect.net/ckconfig" + "go.maximumdirect.net/commandkit" + "go.maximumdirect.net/configkit" + "go.maximumdirect.net/promptkit" +) + +func TestServerSelectorsPreservePresenceAndEvidence(t *testing.T) { + path := filepath.Join(t.TempDir(), "app.yml") + if err := os.WriteFile(path, []byte("server:\n addr: :1234\n"), 0600); err != nil { + t.Fatal(err) + } + var result *configkit.Result[appconfig.Document] + var loadErr error + var captured serveInput + app, err := commandkit.New(commandkit.Identity{Name: "selectors"}, commandkit.NewCommand(commandkit.CommandSpec[serveInput]{ + Name: "serve", Flags: serveFlags(), + Run: func(inv commandkit.Invocation, input serveInput) commandkit.Outcome { + captured = input + result, loadErr = loadApplication(inv.Context, input.Selection, input.Application, struct{}{}) + if loadErr != nil { + return commandkit.Failure("load failed").WithCause(loadErr) + } + return commandkit.Success() + }, + })) + if err != nil { + t.Fatal(err) + } + args := []string{"serve", "--config", path, "--overlay=", "--addr=", "--artifact-root", " ./a/../artifacts ", "--max-request-bytes=0", "--max-artifact-bytes=9223372036854775807", "--max-response-bytes=4294967296"} + outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: io.Discard, Err: io.Discard}) + if outcome.Status != 0 { + t.Fatalf("outcome=%+v err=%v", outcome, loadErr) + } + if !captured.Selection.Config.Set || !captured.Selection.Overlay.Set || !captured.Application.Addr.Set || !captured.Application.MaxRequestBytes.Set { + t.Fatalf("presence lost: %+v", captured) + } + if result.Config.Server.Addr != "" || result.Config.Server.MaxRequestBytes != 0 || result.Config.Server.MaxArtifactBytes != math.MaxInt64 || result.Config.Server.MaxResponseBytes != 4294967296 || result.Config.Server.ArtifactRoot != "artifacts" { + t.Fatal(result.Config) + } + explanation, ok := result.Explain(configkit.Keys("server", "artifact_root")) + var cli, normalized bool + for _, entry := range explanation.History { + cli = cli || entry.Origin.Layer == configkit.LayerCLI + normalized = normalized || entry.Origin.Operation == configkit.ChangeNormalization + } + if !ok || !cli || !normalized { + t.Fatalf("evidence lost: %+v", explanation) + } + if len(namedApplicationOverrides(applicationInput{})) != 0 { + t.Fatal("omitted HTTP flags became overrides") + } + if err := os.WriteFile(path, []byte("transient_retries: 7\n"), 0600); err != nil { + t.Fatal(err) + } + fresh, err := loadApplication(context.Background(), captured.Selection, captured.Application, struct{}{}) + if err != nil || fresh.Config.TransientRetries == nil || *fresh.Config.TransientRetries != 7 || fresh == result { + t.Fatalf("fresh load: %+v %v", fresh, err) + } +} + +func TestApplicationSelectionErrorsAndCancellation(t *testing.T) { + path := filepath.Join(t.TempDir(), "app.yml") + if err := os.WriteFile(path, []byte("{}\n"), 0600); err != nil { + t.Fatal(err) + } + selection := ckconfig.Selection{Config: commandkit.Supplied[string]{Set: true, Value: path}} + for _, overlay := range []commandkit.Supplied[string]{{}, {Set: true}, {Set: true, Value: "default"}} { + selection.Overlay = overlay + if _, err := loadApplication(context.Background(), selection, applicationInput{}, struct{}{}); err != nil { + t.Fatalf("overlay=%+v: %v", overlay, err) + } + } + selection.Overlay = commandkit.Supplied[string]{Set: true, Value: "missing"} + _, err := loadApplication(context.Background(), selection, applicationInput{}, struct{}{}) + var native *configkit.LoadError + if !errors.As(err, &native) { + t.Fatalf("missing overlay lost typed error: %v", err) + } + selection = ckconfig.Selection{Config: commandkit.Supplied[string]{Set: true}} + if _, err := loadApplication(context.Background(), selection, applicationInput{}, struct{}{}); !errors.As(err, &native) { + t.Fatalf("empty config lost typed error: %v", err) + } + ctx, cancel := context.WithCancel(context.Background()) + cancel() + if _, err := loadApplication(ctx, ckconfig.Selection{}, applicationInput{}, struct{}{}); !errors.Is(err, context.Canceled) { + t.Fatalf("cancellation lost: %v", err) + } +} + +func TestEngineUsesOptionalMainAndNativeResourceContexts(t *testing.T) { + root := t.TempDir() + t.Chdir(root) + write := func(path, body string) { + t.Helper() + if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(body), 0600); err != nil { + t.Fatal(err) + } + } + settings := appconfig.Settings(appconfig.Document{}) + engine, err := newEngine(settings) + if err != nil { + t.Fatal(err) + } + if _, err := engine.InspectProfile(context.Background(), "auto"); err != nil { + t.Fatalf("bundle missing without external main: %v", err) + } + settings.PromptKitConfiguration = "missing.yml" + if _, err := newEngine(settings); err == nil { + t.Fatal("missing selected main accepted") + } + write("framework/main.yml", "config:\n imports:\n - glob: collections/*/collection.yml\n - glob: collections/*/collection.yaml\n") + write("framework/collections/example/collection.yaml", "config:\n imports:\n - path: definitions.yml\ncollections:\n review:\n release_version: '1'\n") + write("framework/collections/example/definitions.yml", "collections:\n review:\n prompts:\n - id: summary\n messages:\n - role: user\n content_file: message.txt\n output:\n format: text\n validation_mode: none\n") + write("framework/collections/example/message.txt", "source-relative body") + settings.PromptKitConfiguration = "framework/main.yml" + engine, err = newEngine(settings) + if err != nil { + t.Fatal(err) + } + _, err = engine.InspectPrompt(context.Background(), "review/summary") + if err != nil { + t.Fatal(err) + } + // Native preparation uses the captured source-relative message resource, + // not the process cwd or application configuration's directory. + prepared, err := engine.Prepare(context.Background(), promptkit.RunRequest{PromptID: "review/summary", ProfileID: "auto"}) + if err != nil { + t.Fatal(err) + } + if len(prepared.Messages) != 1 || prepared.Messages[0].Content != "source-relative body" { + t.Fatalf("resource context lost: %+v", prepared) + } + view, err := engine.InspectConfiguration() + if err != nil { + t.Fatal(err) + } + if len(view.Sources()) < 3 { + t.Fatalf("native source evidence missing: %+v", view.Sources()) + } +} diff --git a/internal/config/document.go b/internal/config/document.go index de67b62..5f98e17 100644 --- a/internal/config/document.go +++ b/internal/config/document.go @@ -5,23 +5,20 @@ import ( "strings" "gitea.maximumdirect.net/eric/scriptorium/internal/defaults" - renderformat "gitea.maximumdirect.net/eric/scriptorium/internal/format" "go.maximumdirect.net/configkit" ) // Document is Scriptorium's ConfigKit-native application configuration shape. type Document struct { - PromptKitSources []SourceSelection `yaml:"promptkit_sources"` - SchemaDir string `yaml:"schema_dir"` + PromptKit PromptKitDocument `yaml:"promptkit"` TransientRetries *int `yaml:"transient_retries"` Server ServerDocument `yaml:"server"` - Defaults DefaultsDocument `yaml:"defaults"` } -// SourceSelection identifies one PromptKit configuration file or directory. -type SourceSelection struct { - File string `yaml:"file"` - Directory string `yaml:"directory"` +// PromptKitDocument selects framework acquisition, not embedded definitions. +type PromptKitDocument struct { + Configuration string `yaml:"configuration"` + SchemaDir string `yaml:"schema_dir"` } type ServerDocument struct { @@ -32,10 +29,6 @@ type ServerDocument struct { MaxResponseBytes int64 `yaml:"max_response_bytes"` } -type DefaultsDocument struct { - RenderFormat string `yaml:"render_format"` -} - func documentDefinition() configkit.Definition[Document] { return configkit.Definition[Document]{ Defaults: documentDefaults, @@ -46,14 +39,13 @@ func documentDefinition() configkit.Definition[Document] { func documentDefaults() Document { return Document{ - SchemaDir: defaults.SchemaDirDefault, + PromptKit: PromptKitDocument{SchemaDir: defaults.SchemaDirDefault}, Server: ServerDocument{ Addr: defaults.HTTPAddrDefault, MaxRequestBytes: defaults.HTTPMaxRequestBytesDefault, MaxArtifactBytes: defaults.HTTPMaxArtifactBytesDefault, MaxResponseBytes: defaults.HTTPMaxResponseBytesDefault, }, - Defaults: DefaultsDocument{RenderFormat: string(renderformat.DefaultPreparedRunOutputFormat)}, } } @@ -73,28 +65,15 @@ func resolveDocument(document *Document, resolution *configkit.Resolution) error } } - for i := range document.PromptKitSources { - path := configkit.Keys("promptkit_sources").Index(i) - normalizePath(path.Key("file"), &document.PromptKitSources[i].File) - normalizePath(path.Key("directory"), &document.PromptKitSources[i].Directory) - } - normalizePath(configkit.Keys("schema_dir"), &document.SchemaDir) + normalizePath(configkit.Keys("promptkit", "configuration"), &document.PromptKit.Configuration) + normalizePath(configkit.Keys("promptkit", "schema_dir"), &document.PromptKit.SchemaDir) normalizePath(configkit.Keys("server", "artifact_root"), &document.Server.ArtifactRoot) normalizeString(configkit.Keys("server", "addr"), &document.Server.Addr, strings.TrimSpace(document.Server.Addr), "trimmed server address") - normalizeString(configkit.Keys("defaults", "render_format"), &document.Defaults.RenderFormat, - strings.ToLower(strings.TrimSpace(document.Defaults.RenderFormat)), "normalized render format") return nil } 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") } @@ -107,9 +86,4 @@ func validateDocument(document *Document, validation *configkit.Validation) { if document.Server.MaxResponseBytes < 0 { validation.Error(configkit.Keys("server", "max_response_bytes"), "must be greater than or equal to zero") } - switch document.Defaults.RenderFormat { - case string(renderformat.OutputFormatText), string(renderformat.OutputFormatJSON): - default: - validation.Error(configkit.Keys("defaults", "render_format"), "must be text or json") - } } diff --git a/internal/config/document_test.go b/internal/config/document_test.go index ec932ca..1f96936 100644 --- a/internal/config/document_test.go +++ b/internal/config/document_test.go @@ -11,213 +11,109 @@ import ( ) func loadDocumentBytes(content string) (*configkit.Result[Document], error) { - return configkit.Load(configkit.Bytes("application.yml", []byte(content)), configkit.Options[Document]{ - Definition: documentDefinition(), - }) + return configkit.Load(configkit.Bytes("application.yml", []byte(content)), configkit.Options[Document]{Definition: documentDefinition()}) } func TestDocumentDefaultsAndExplicitValues(t *testing.T) { - defaultsOnly, err := configkit.Load(configkit.NoSource(), configkit.Options[Document]{Definition: documentDefinition()}) + result, err := loadDocumentBytes("{}\n") + if err != nil || !reflect.DeepEqual(result.Config, documentDefaults()) { + t.Fatalf("defaults: %+v %v", result, err) + } + result, err = loadDocumentBytes("promptkit:\n configuration: ''\n schema_dir: ''\ntransient_retries: 0\nserver:\n addr: ''\n artifact_root: ''\n max_request_bytes: 0\n max_artifact_bytes: 0\n max_response_bytes: 0\n") if err != nil { - t.Fatalf("defaults-only load: %v", err) + t.Fatal(err) } - if !reflect.DeepEqual(defaultsOnly.Config, documentDefaults()) || len(defaultsOnly.Sources()) != 0 { - t.Fatalf("unexpected defaults-only result: %+v sources=%+v", defaultsOnly.Config, defaultsOnly.Sources()) + got := result.Config + if got.PromptKit.Configuration != "" || got.PromptKit.SchemaDir != "" || got.Server.Addr != "" || got.Server.ArtifactRoot != "" || got.Server.MaxRequestBytes != 0 || got.Server.MaxArtifactBytes != 0 || got.Server.MaxResponseBytes != 0 || got.TransientRetries == nil || *got.TransientRetries != 0 { + t.Fatalf("explicit values: %+v", got) } - - result, err := loadDocumentBytes(`promptkit_sources: - - file: " ./bundle/../framework.yml " - - directory: "./profiles" -schema_dir: "" -transient_retries: 0 -server: - addr: "" - artifact_root: "" - max_request_bytes: 0 - max_artifact_bytes: 0 - max_response_bytes: 0 -defaults: - render_format: " JSON " -`) - if err != nil { - t.Fatalf("explicit-value load: %v", err) + settings := Settings(got) + if settings.SchemaDir != "" || settings.PromptKitConfiguration != "" || settings.ServerAddr != "" || settings.MaxRequestBytes != 0 || settings.TransientRetries == nil || *settings.TransientRetries != 0 { + t.Fatalf("settings changed resolved values: %+v", settings) } - config := result.Config - if config.SchemaDir != "" || config.Server.Addr != "" || config.Server.MaxRequestBytes != 0 || config.Server.MaxArtifactBytes != 0 || config.Server.MaxResponseBytes != 0 { - t.Fatalf("explicit empty and zero values did not replace defaults: %+v", config) - } - if config.TransientRetries == nil || *config.TransientRetries != 0 { - t.Fatalf("explicit zero retry budget lost: %+v", config.TransientRetries) - } - 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\npromptkit_sources: null\n") - if err != nil { - t.Fatalf("nullable-value load: %v", err) - } - if nullable.Config.TransientRetries != nil || nullable.Config.PromptKitSources != nil { - t.Fatalf("nullable fields did not remain nil: %+v", nullable.Config) + result, err = loadDocumentBytes("transient_retries: null\n") + if err != nil || result.Config.TransientRetries != nil { + t.Fatalf("nullable retries: %+v %v", result, 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) - } - }) - } -} - -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) - } - 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 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(`promptkit_sources: - - file: " ./one/../two " - - file: "./blank.yml" - directory: " " - - directory: "./three/../three" -server: - artifact_root: " ./data/../artifacts " - addr: " :9000 " -defaults: - render_format: " JSON " -`) +func TestDocumentNormalizesWithoutAcquiringFramework(t *testing.T) { + result, err := loadDocumentBytes("promptkit:\n configuration: ' ./missing/../framework.yml '\n schema_dir: ' ./a/../schemas '\nserver:\n addr: ' :9000 '\n artifact_root: ' ./a/../artifacts '\n") if err != nil { - t.Fatalf("load normalized document: %v", err) + t.Fatal(err) } - 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.PromptKit.Configuration != "framework.yml" || result.Config.PromptKit.SchemaDir != "schemas" || result.Config.Server.Addr != ":9000" || result.Config.Server.ArtifactRoot != "artifacts" { + t.Fatalf("normalized: %+v", result.Config) } - 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{"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()) + for _, key := range []string{"configuration", "schema_dir"} { + explanation, ok := result.Explain(configkit.Keys("promptkit", key)) + if !ok { + t.Fatal("missing path evidence", key) + } + normalized := false + for _, change := range explanation.History { + normalized = normalized || change.Origin.Operation == configkit.ChangeNormalization + } + if !normalized { + t.Fatalf("normalization evidence missing: %+v", explanation) } } - 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) + var output bytes.Buffer + if err := result.WriteEffective(&output); err != nil { + t.Fatal(err) } - found := false + if !strings.Contains(output.String(), "configuration: framework.yml") || !strings.Contains(output.String(), "schema_dir: schemas") { + t.Fatal(output.String()) + } + unchanged, err := loadDocumentBytes("promptkit:\n configuration: framework.yml\n") + if err != nil { + t.Fatal(err) + } + explanation, _ := unchanged.Explain(configkit.Keys("promptkit", "configuration")) for _, change := range explanation.History { if change.Origin.Operation == configkit.ChangeNormalization { - found = true - } - } - 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) + t.Fatal("unchanged path was normalized") } } } 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"}, - {"negative response limit", "server:\n max_response_bytes: -9173\n", "/server/max_response_bytes"}, - {"empty format", "defaults:\n render_format: \"\"\n", "/defaults/render_format"}, - {"unknown format", "defaults:\n render_format: private-invalid-value\n", "/defaults/render_format"}, - } { - t.Run(tc.name, func(t *testing.T) { - _, err := loadDocumentBytes(tc.content) - var loadError *configkit.LoadError - if !errors.As(err, &loadError) { - t.Fatalf("expected typed validation error, got %v", err) - } - found := false - for _, diagnostic := range loadError.Diagnostics() { - 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") || strings.Contains(diagnostic.Message, "secret-path") || strings.Contains(diagnostic.Message, "private-path") { - t.Fatalf("diagnostic disclosed rejected value: %+v", diagnostic) - } - } - if !found { - t.Fatalf("expected validation at %s, got %+v", tc.path, loadError.Diagnostics()) - } - }) - } -} - -func TestDocumentRejectsNativeShapeErrors(t *testing.T) { - for _, content := range []string{ - "server: null\n", - "defaults: null\n", - "server:\n addr: null\n", - "server:\n max_request_bytes: null\n", - "defaults:\n render_format: null\n", - "server:\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", - } { + for _, key := range []string{"transient_retries", "max_request_bytes", "max_artifact_bytes", "max_response_bytes"} { + content := key + ": -9173\n" + path := "/" + key + if key != "transient_retries" { + content = "server:\n " + content + path = "/server" + path + } _, err := loadDocumentBytes(content) - if err == nil { - t.Fatalf("unexpected acceptance of %q", content) + var native *configkit.LoadError + if !errors.As(err, &native) { + t.Fatalf("typed error: %v", err) + } + found := false + for _, diagnostic := range native.Diagnostics() { + found = found || diagnostic.Code == configkit.CodeValidateError && diagnostic.Path.String() == path + if strings.Contains(diagnostic.Message, "9173") { + t.Fatalf("rejected value leaked: %+v", diagnostic) + } + } + if !found { + t.Fatalf("no validation evidence for %s: %+v", path, native.Diagnostics()) + } + } +} + +func TestDocumentRejectsRetiredAndInvalidShapes(t *testing.T) { + for _, content := range []string{ + "promptkit_sources: []\n", "schema_dir: .\n", "defaults:\n render_format: text\n", + "promptkit: null\n", "promptkit:\n configuration: null\n", "promptkit:\n schema_dir: null\n", + "promptkit:\n profiles: []\n", "promptkit:\n backends: []\n", "promptkit:\n collections: {}\n", + "server: null\n", "server:\n addr: null\n", "server:\n max_request_bytes: null\n", + "server:\n unknown_field: 1\n", "prompt_dir: ./old-prompts\n", + "profile_dir: ./old-profiles\n", "backends: []\n", + } { + if _, err := loadDocumentBytes(content); err == nil { + t.Fatalf("accepted %q", content) } } } diff --git a/internal/config/load.go b/internal/config/load.go index 51f67b7..ceb948a 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -3,7 +3,6 @@ package config import ( "fmt" - renderformat "gitea.maximumdirect.net/eric/scriptorium/internal/format" "go.maximumdirect.net/configkit" ) @@ -15,13 +14,18 @@ type LoadOptions struct { Overrides []configkit.Override } +var applicationSearch = configkit.Searches( + configkit.WorkingDirectory(), configkit.UserConfigDirectory(), + configkit.HomeDirectory(), configkit.UnixSystemDirectories(), +) + // Load resolves one ConfigKit document and retains its inspection snapshot. func Load(options LoadOptions) (*configkit.Result[Document], error) { var source configkit.Source if options.Explicit { source = configkit.File(options.Path) } else { - source = configkit.SearchConfig("scriptorium", configkit.SearchPaths(defaultConfigSearchPaths...)) + source = configkit.SearchConfig("scriptorium", applicationSearch) } result, err := configkit.Load(source, configkit.Options[Document]{ @@ -39,17 +43,14 @@ 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{ - SchemaDir: document.SchemaDir, - ServerAddr: document.Server.Addr, - ArtifactRoot: document.Server.ArtifactRoot, - MaxRequestBytes: document.Server.MaxRequestBytes, - MaxArtifactBytes: document.Server.MaxArtifactBytes, - MaxResponseBytes: document.Server.MaxResponseBytes, - DefaultRenderFormat: renderformat.PreparedRunOutputFormat(document.Defaults.RenderFormat), - TransientRetries: document.TransientRetries, - } - if document.PromptKitSources != nil { - settings.PromptKitSources = append([]SourceSelection{}, document.PromptKitSources...) + SchemaDir: document.PromptKit.SchemaDir, + PromptKitConfiguration: document.PromptKit.Configuration, + ServerAddr: document.Server.Addr, + ArtifactRoot: document.Server.ArtifactRoot, + MaxRequestBytes: document.Server.MaxRequestBytes, + MaxArtifactBytes: document.Server.MaxArtifactBytes, + MaxResponseBytes: document.Server.MaxResponseBytes, + TransientRetries: document.TransientRetries, } return settings } diff --git a/internal/config/load_test.go b/internal/config/load_test.go index d5a542a..631c934 100644 --- a/internal/config/load_test.go +++ b/internal/config/load_test.go @@ -5,333 +5,195 @@ import ( "os" "path/filepath" "reflect" - "strings" "testing" "go.maximumdirect.net/configkit" ) -func useConfigSearchPaths(t *testing.T, paths ...string) { - t.Helper() - previous := defaultConfigSearchPaths - defaultConfigSearchPaths = paths - t.Cleanup(func() { defaultConfigSearchPaths = previous }) -} - func writeApplicationFile(t *testing.T, path, body string) { t.Helper() - if err := os.WriteFile(path, []byte(body), 0o600); err != nil { + if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(body), 0600); err != nil { t.Fatal(err) } } -func TestLoadSelectsOrderedSourceAndKeepsItsSnapshot(t *testing.T) { - dir := t.TempDir() - first := filepath.Join(dir, "first.yml") - second := filepath.Join(dir, "second.yml") - 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 !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, "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) - } - - if err := os.Remove(first); err != nil { - t.Fatal(err) - } - result, err = Load(LoadOptions{}) - 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) - } - - result, err = Load(LoadOptions{Path: first, Explicit: true}) - if result != nil || err == nil { - t.Fatalf("missing explicit source was accepted: result=%+v err=%v", result, err) - } - var loadError *configkit.LoadError - if !errors.As(err, &loadError) { - t.Fatalf("typed load error was lost: %v", err) - } +func isolatedDiscovery(t *testing.T) string { + t.Helper() + root := t.TempDir() + t.Chdir(root) + t.Setenv("HOME", filepath.Join(root, "home")) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(root, "xdg")) + return root } -func TestLoadRejectsExplicitEmptyAndDistinguishesEmptyFile(t *testing.T) { - useConfigSearchPaths(t, filepath.Join(t.TempDir(), "absent.yml")) - if _, err := Load(LoadOptions{Explicit: true, Path: ""}); err == nil { - t.Fatal("explicit empty path was accepted") - } else { - var loadError *configkit.LoadError - if !errors.As(err, &loadError) { - t.Fatalf("expected upstream source diagnostic, got %v", err) +func useIsolatedSearch(t *testing.T) { + t.Helper() + previous := applicationSearch + applicationSearch = configkit.WorkingDirectory() + t.Cleanup(func() { applicationSearch = previous }) +} + +func TestLoadNativeDiscoveryOrderAndFilenames(t *testing.T) { + root := isolatedDiscovery(t) + // A later candidate must never be loaded as a configuration layer. + paths := []string{ + filepath.Join(root, "scriptorium.yaml"), + filepath.Join(root, "xdg", "scriptorium", "main.yaml"), + filepath.Join(root, "home", ".scriptorium", "config.yaml"), + } + for index, path := range paths { + writeApplicationFile(t, path, "server:\n addr: '"+string(rune('a'+index))+"'\n") + } + for index, path := range paths { + result, err := Load(LoadOptions{Path: "ignored.yml"}) + if err != nil { + t.Fatal(err) + } + if result.Config.Server.Addr != string(rune('a'+index)) || len(result.Sources()) != 1 || result.Sources()[0].Path != path { + t.Fatalf("selected wrong source: %+v %+v", result.Config, result.Sources()) + } + if err := os.Remove(path); err != nil { + t.Fatal(err) } } - - dir := t.TempDir() - empty := filepath.Join(dir, "empty.yml") - writeApplicationFile(t, empty, "") - if _, err := Load(LoadOptions{Path: empty, Explicit: true}); err == nil { - t.Fatal("empty file was treated as defaults-only") - } - writeApplicationFile(t, empty, "{}\n") - result, err := Load(LoadOptions{Path: empty, Explicit: true}) - if err != nil || !reflect.DeepEqual(result.Config, documentDefaults()) { - t.Fatalf("empty mapping did not load defaults: result=%+v err=%v", result, err) - } -} - -func TestLoadDefaultsOnlyAndOverridesBeforeValidation(t *testing.T) { - useConfigSearchPaths(t, filepath.Join(t.TempDir(), "absent.yml")) + // Do not let a host's system configuration influence exhaustion checks. + // The system preset's filename expansion remains upstream-owned. + useIsolatedSearch(t) result, err := Load(LoadOptions{}) - if err != nil || !reflect.DeepEqual(result.Config, documentDefaults()) || len(result.Sources()) != 0 { - t.Fatalf("defaults-only load: result=%+v err=%v", result, err) - } - var effective strings.Builder - if err := result.WriteEffective(&effective); err != nil || !strings.Contains(effective.String(), "render_format: text") || len(result.Sources()) != 0 { - t.Fatalf("defaults-only inspection changed: effective=%q err=%v sources=%+v", effective.String(), err, result.Sources()) - } - - result, err = Load(LoadOptions{Overrides: []configkit.Override{{ - Path: configkit.Keys("server", "max_request_bytes"), Value: int64(0), Flag: "--max-request-bytes", - }}}) - if err != nil || result.Config.Server.MaxRequestBytes != 0 || len(result.Sources()) != 1 || result.Sources()[0].Layer != configkit.LayerCLI { - t.Fatalf("defaults-only override lost: result=%+v err=%v", result, err) - } - - path := filepath.Join(t.TempDir(), "invalid.yml") - writeApplicationFile(t, path, "defaults:\n render_format: invalid-choice\n") - result, err = Load(LoadOptions{Path: path, Explicit: true, Overrides: []configkit.Override{{ - Path: configkit.Keys("defaults", "render_format"), Value: "json", Flag: "--render-format", - }}}) - if err != nil || result.Config.Defaults.RenderFormat != "json" { - t.Fatalf("override did not precede validation: result=%+v err=%v", result, err) - } - if len(result.Sources()) != 2 || result.Sources()[0].Layer != configkit.LayerMain || result.Sources()[1].Layer != configkit.LayerCLI { - t.Fatalf("source order changed: %+v", result.Sources()) - } - writeApplicationFile(t, path, "defaults: [\n") - if _, err := Load(LoadOptions{Path: path, Explicit: true, Overrides: []configkit.Override{{ - Path: configkit.Keys("defaults", "render_format"), Value: "json", Flag: "--render-format", - }}}); err == nil { - t.Fatal("override bypassed malformed lower-level YAML") - } -} - -func TestSettingsCopiesResolvedValuesAndSourceOrder(t *testing.T) { - result, err := loadDocumentBytes(`schema_dir: "" -promptkit_sources: - - directory: ./profiles - - file: ./framework.yml -transient_retries: 0 -server: - addr: "" - artifact_root: "" - max_request_bytes: 0 -defaults: - render_format: json -`) - if err != nil { - t.Fatal(err) - } - settings := Settings(result.Config) - if settings.SchemaDir != "" || settings.ServerAddr != "" || settings.MaxRequestBytes != 0 || settings.DefaultRenderFormat != "json" { - t.Fatalf("resolved scalar values changed: %+v", settings) - } - if settings.TransientRetries == nil || *settings.TransientRetries != 0 { - t.Fatalf("retry presence lost: %+v", settings.TransientRetries) - } - 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.PromptKitSources != nil { - t.Fatalf("default nullable values changed: %+v", got) - } -} - -func TestLoadDiscoveryErrorQuotesPath(t *testing.T) { - dir := t.TempDir() - path := filepath.Join(dir, "directory-\n.yml") - if err := os.Mkdir(path, 0o700); err != nil { - t.Fatal(err) - } - useConfigSearchPaths(t, path) - _, err := Load(LoadOptions{}) - var loadError *configkit.LoadError - if !errors.As(err, &loadError) { - t.Fatalf("expected structured error: %v", err) - } - var diagnostics strings.Builder - if err := configkit.WriteDiagnostics(&diagnostics, loadError.Diagnostics()); err != nil { - t.Fatal(err) - } - if !strings.Contains(diagnostics.String(), string(configkit.CodeSourceNotRegular)) || strings.Contains(diagnostics.String(), "directory-\n.yml") { - t.Fatalf("discovery diagnostics exposed an unescaped path: %s", diagnostics.String()) - } - - // A decoding diagnostic can contain an authored configuration path even - // though the nonregular-file diagnostic omits its filesystem path. - file := filepath.Join(dir, "invalid.yml") - writeApplicationFile(t, file, "server:\n \"bad\\nkey\": value\n") - _, err = Load(LoadOptions{Path: file, Explicit: true}) - if !errors.As(err, &loadError) { - t.Fatalf("expected decoding diagnostic: %v", err) - } - diagnostics.Reset() - if err := configkit.WriteDiagnostics(&diagnostics, loadError.Diagnostics()); err != nil { - t.Fatal(err) - } - if !strings.Contains(diagnostics.String(), `\n`) || strings.Contains(diagnostics.String(), "bad\nkey") { - t.Fatalf("diagnostic path was not escaped: %q", diagnostics.String()) - } -} - -func TestLoadComposesExplicitImportsAndSelectedOverlay(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, "config:\n imports:\n - glob: fragments/*.yml\n required: true\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 || !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("promptkit_sources"), Value: []any{map[string]any{"file": "./cli-framework.yml"}}, Flag: "--promptkit-source", - }}}) - 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"), "server:\n addr: :8082\n") - if _, err := Load(LoadOptions{Path: base, Explicit: true}); err == nil { - t.Fatal("conflicting import accepted") - } else { - var loadError *configkit.LoadError - if !errors.As(err, &loadError) { - t.Fatalf("conflict lost structured diagnostic: %v", err) - } - } -} - -func TestLoadOverlayWithoutMainContext(t *testing.T) { - useConfigSearchPaths(t, filepath.Join(t.TempDir(), "absent.yml")) - if _, err := Load(LoadOptions{Overlay: "local"}); err == nil { - t.Fatal("overlay without a main context accepted") - } else { - var loadError *configkit.LoadError - if !errors.As(err, &loadError) { - t.Fatalf("no-context overlay lost structured diagnostic: %v", err) - } - } - if _, err := Load(LoadOptions{Overlay: "default"}); err != nil { - t.Fatalf("default must select no overlay: %v", err) + if err != nil || len(result.Sources()) != 0 || !reflect.DeepEqual(result.Config, documentDefaults()) { + t.Fatalf("exhaustion: %+v %v", result, err) } } func TestLoadDoesNotFallThroughSelectedInvalidSource(t *testing.T) { - dir := t.TempDir() - first, second := filepath.Join(dir, "first.yml"), filepath.Join(dir, "second.yml") - writeApplicationFile(t, first, "server: [\n") - writeApplicationFile(t, second, "server:\n addr: ':9090'\n") - useConfigSearchPaths(t, first, second) + root := isolatedDiscovery(t) + writeApplicationFile(t, filepath.Join(root, "scriptorium.yml"), "server: [\n") + writeApplicationFile(t, filepath.Join(root, "scriptorium.yaml"), "{}\n") if _, err := Load(LoadOptions{}); err == nil { - t.Fatal("malformed selected file fell through to the second candidate") + t.Fatal("invalid first candidate fell through") + } +} + +func TestLoadExplicitSelectionAndOverlayPresence(t *testing.T) { + root := isolatedDiscovery(t) + useIsolatedSearch(t) + for _, content := range []string{"", "server: [\n"} { + path := filepath.Join(root, "selected.yml") + writeApplicationFile(t, path, content) + if _, err := Load(LoadOptions{Explicit: true, Path: path}); err == nil { + t.Fatal("accepted invalid explicit file") + } + } + _, err := Load(LoadOptions{Explicit: true}) + var native *configkit.LoadError + if !errors.As(err, &native) { + t.Fatalf("empty path lost typed error: %v", err) + } + base := filepath.Join(root, "selected.yml") + writeApplicationFile(t, base, "{}\n") + for _, overlay := range []string{"", "default"} { + result, err := Load(LoadOptions{Explicit: true, Path: base, Overlay: overlay}) + if err != nil || !reflect.DeepEqual(result.Config, documentDefaults()) { + t.Fatalf("no overlay: %+v %v", result, err) + } + } + if _, err := Load(LoadOptions{Explicit: true, Path: base, Overlay: "missing"}); err == nil { + t.Fatal("missing overlay accepted") + } + if _, err := Load(LoadOptions{Overlay: "local"}); err == nil { + t.Fatal("overlay without main context accepted") + } +} + +func TestLoadImportsOverlayAndNativeOverrides(t *testing.T) { + root := t.TempDir() + main := filepath.Join(root, "config.yml") + writeApplicationFile(t, main, "config:\n imports:\n - glob: fragments/*.yml\n required: true\npromptkit:\n configuration: base.yml\n") + writeApplicationFile(t, filepath.Join(root, "fragments", "a.yml"), "server:\n addr: :8081\n") + writeApplicationFile(t, filepath.Join(root, "fragments", "b.yml"), "promptkit:\n schema_dir: ./schemas\n") + writeApplicationFile(t, filepath.Join(root, "overlays", "local.yml"), "promptkit:\n configuration: overlay.yml\nserver:\n addr: :9090\n") + result, err := Load(LoadOptions{Explicit: true, Path: main, Overlay: "local", Overrides: []configkit.Override{{Path: configkit.Keys("promptkit", "configuration"), Value: "native.yml", Flag: "--set"}}}) + if err != nil { + t.Fatal(err) + } + if result.Config.PromptKit.Configuration != "native.yml" || result.Config.PromptKit.SchemaDir != "schemas" || result.Config.Server.Addr != ":9090" || len(result.Sources()) != 5 { + t.Fatalf("composition: %+v %+v", result.Config, result.Sources()) + } + writeApplicationFile(t, filepath.Join(root, "fragments", "b.yml"), "server:\n addr: :8082\n") + if _, err := Load(LoadOptions{Explicit: true, Path: main}); err == nil { + t.Fatal("peer conflict accepted") + } +} + +func TestLoadNativeOverrideEvidenceAndConflicts(t *testing.T) { + root := isolatedDiscovery(t) + useIsolatedSearch(t) + result, err := Load(LoadOptions{Overrides: []configkit.Override{ + {Path: configkit.Keys("server", "max_request_bytes"), Value: int64(0), Flag: "--set"}, + {Path: configkit.Keys("promptkit", "configuration"), Value: " ./one/../framework.yml ", Flag: "--set"}, + }}) + if err != nil { + t.Fatal(err) + } + if result.Config.Server.MaxRequestBytes != 0 || result.Config.PromptKit.Configuration != "framework.yml" { + t.Fatal(result.Config) + } + explanation, ok := result.Explain(configkit.Keys("promptkit", "configuration")) + if !ok || len(explanation.History) < 2 { + t.Fatalf("native evidence missing: %+v", explanation) + } + for index, records := range [][]configkit.Override{ + {{Path: configkit.Keys("server", "addr"), Value: "a", Flag: "--set"}, {Path: configkit.Keys("server", "addr"), Value: "b", Flag: "--set"}}, + {{Path: configkit.Keys("server"), Value: map[string]any{"addr": "a"}, Flag: "--set"}, {Path: configkit.Keys("server", "addr"), Value: "b", Flag: "--set"}}, + } { + _, err := Load(LoadOptions{Overrides: records}) + var native *configkit.LoadError + if !errors.As(err, &native) { + t.Fatalf("native conflict lost: %v", err) + } + found := false + want := []configkit.Code{configkit.CodeOverrideDuplicate, configkit.CodeOverrideOverlap}[index] + for _, diagnostic := range native.Diagnostics() { + found = found || diagnostic.Code == want + } + if !found { + t.Fatalf("missing conflict diagnostics: %+v", native.Diagnostics()) + } + } + path := filepath.Join(root, "invalid.yml") + writeApplicationFile(t, path, "server:\n max_request_bytes: -1\n") + result, err = Load(LoadOptions{Explicit: true, Path: path, Overrides: []configkit.Override{{Path: configkit.Keys("server", "max_request_bytes"), Value: int64(0), Flag: "--set"}}}) + if err != nil || result.Config.Server.MaxRequestBytes != 0 { + t.Fatalf("override did not precede validation: %+v %v", result, err) + } + writeApplicationFile(t, path, "server:\n max_request_bytes: not-a-number\n") + if _, err := Load(LoadOptions{Explicit: true, Path: path, Overrides: []configkit.Override{{Path: configkit.Keys("server", "max_request_bytes"), Value: int64(0), Flag: "--set"}}}); err == nil { + t.Fatal("override repaired decode failure") } } func TestLoadImportRequirednessAndDestination(t *testing.T) { - dir := t.TempDir() - main := filepath.Join(dir, "config.yml") - for _, tc := range []struct { - name, declaration string - wantError bool - }{ - {"optional by default", "- path: absent.yml", false}, - {"required", "- path: absent.yml\n required: true", true}, - {"removed optional field", "- path: absent.yml\n optional: true", true}, - } { - t.Run(tc.name, func(t *testing.T) { - writeApplicationFile(t, main, "config:\n imports:\n "+tc.declaration+"\n") - _, err := Load(LoadOptions{Path: main, Explicit: true}) - if (err != nil) != tc.wantError { - t.Fatalf("load error = %v, want error %t", err, tc.wantError) - } - }) - } - fragments := filepath.Join(dir, "fragments") - if err := os.Mkdir(fragments, 0o700); err != nil { + root := t.TempDir() + main := filepath.Join(root, "config.yml") + writeApplicationFile(t, main, "config:\n imports:\n - path: absent.yml\n") + if _, err := Load(LoadOptions{Explicit: true, Path: main}); err != nil { t.Fatal(err) } - writeApplicationFile(t, filepath.Join(fragments, "server.yml"), "addr: ':8181'\n") + writeApplicationFile(t, main, "config:\n imports:\n - path: absent.yml\n required: true\n") + if _, err := Load(LoadOptions{Explicit: true, Path: main}); err == nil { + t.Fatal("required import accepted") + } + writeApplicationFile(t, filepath.Join(root, "fragments", "server.yml"), "addr: ':8181'\n") writeApplicationFile(t, main, "config:\n imports:\n - dir: fragments\n pattern: '*.yml'\n destination: [server]\n required: true\n") - result, err := Load(LoadOptions{Path: main, Explicit: true}) + result, err := Load(LoadOptions{Explicit: true, Path: main}) if err != nil { t.Fatal(err) } info, ok := result.Metadata().Lookup(configkit.Keys("server", "addr")) if result.Config.Server.Addr != ":8181" || !ok || len(info.Origin.Locations) == 0 { - t.Fatalf("mounted value or origin missing: config=%+v origin=%+v", result.Config.Server, info) - } -} - -func TestLoadAuthoredSelectorCollection(t *testing.T) { - dir := t.TempDir() - for _, subdir := range []string{"fragments", "overlays"} { - if err := os.Mkdir(filepath.Join(dir, subdir), 0o700); err != nil { - t.Fatal(err) - } - } - main := filepath.Join(dir, "config.yml") - writeApplicationFile(t, main, "config:\n sequences:\n - destination: [promptkit_sources]\n key: file\n mode: upsert\n imports:\n - dir: fragments\n pattern: '*.yml'\n required: true\n") - writeApplicationFile(t, filepath.Join(dir, "fragments", "a.yml"), "promptkit_sources:\n - file: a.yml\n") - peer := filepath.Join(dir, "fragments", "b.yml") - writeApplicationFile(t, peer, "promptkit_sources:\n - file: b.yml\n") - writeApplicationFile(t, filepath.Join(dir, "overlays", "local.yml"), "promptkit_sources:\n - file: c.yml\n") - result, err := Load(LoadOptions{Path: main, Explicit: true, Overlay: "local"}) - if err != nil || !reflect.DeepEqual(result.Config.PromptKitSources, []SourceSelection{{File: "a.yml"}, {File: "b.yml"}, {File: "c.yml"}}) { - t.Fatalf("authored upsert did not retain selectors: result=%+v err=%v", result, err) - } - result, err = Load(LoadOptions{Path: main, Explicit: true, Overlay: "local", Overrides: []configkit.Override{{ - Path: configkit.Keys("promptkit_sources"), Value: []any{map[string]any{"file": "cli.yml"}}, Flag: "--promptkit-source", - }}}) - if err != nil || !reflect.DeepEqual(result.Config.PromptKitSources, []SourceSelection{{File: "cli.yml"}}) { - t.Fatalf("CLI list did not replace selectors: result=%+v err=%v", result, err) - } - writeApplicationFile(t, peer, "promptkit_sources:\n - file: a.yml\n") - if _, err := Load(LoadOptions{Path: main, Explicit: true}); err == nil { - t.Fatal("duplicate authored peer identity accepted") - } -} - -func TestLoadOverrideCannotRepairDecodeError(t *testing.T) { - path := filepath.Join(t.TempDir(), "config.yml") - writeApplicationFile(t, path, "server:\n max_request_bytes: not-a-number\n") - _, err := Load(LoadOptions{Path: path, Explicit: true, Overrides: []configkit.Override{{ - Path: configkit.Keys("server", "max_request_bytes"), Value: int64(12), Flag: "--max-request-bytes", - }}}) - if err == nil { - t.Fatal("override repaired an undecodable source value") + t.Fatalf("value/origin: %+v %+v", result.Config, info) } } diff --git a/internal/config/settings.go b/internal/config/settings.go index 9d34585..ec2803a 100644 --- a/internal/config/settings.go +++ b/internal/config/settings.go @@ -1,26 +1,13 @@ package config -import renderformat "gitea.maximumdirect.net/eric/scriptorium/internal/format" - -const ( - DefaultConfigPath = "/etc/scriptorium/config.yml" - DefaultConfigPathLocal = "/usr/local/etc/scriptorium/config.yml" -) - -var defaultConfigSearchPaths = []string{ - DefaultConfigPathLocal, - DefaultConfigPath, -} - // AppSettings holds the validated application values used by executable adapters. type AppSettings struct { - PromptKitSources []SourceSelection - SchemaDir string - ServerAddr string - ArtifactRoot string - MaxRequestBytes int64 - MaxArtifactBytes int64 - MaxResponseBytes int64 - DefaultRenderFormat renderformat.PreparedRunOutputFormat - TransientRetries *int + PromptKitConfiguration string + SchemaDir string + ServerAddr string + ArtifactRoot string + MaxRequestBytes int64 + MaxArtifactBytes int64 + MaxResponseBytes int64 + TransientRetries *int }