Adopt native configuration discovery and a single PromptKit entry file

This commit is contained in:
2026-11-20 04:26:17 +00:00
parent adbe62a1e9
commit 733540f936
16 changed files with 585 additions and 697 deletions

View File

@@ -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 ## Stage 2: Adapt Authoritative Application Loading And Shared Selectors
**Status: In progress; override decision resolved.** Adopt ConfigKit v1.2.1's **Status: Complete.** Application loading uses the selected native directory
native conflict behavior; no upstream change or dependency upgrade is required. presets, strict nested `promptkit` acquisition settings, and ConfigKit's native
The former named-selector/`--set` precedence test is obsolete: configuration override/conflict handling. The shared engine uses one optional import-enabled
commands no longer expose named application overrides. Cover native override main file and retains the bundled underlay. Removed the render default,
conflicts directly and HTTP named overrides on `serve`. Previously reported source/schema bindings, source classification, and parser-owned application
configuration and metadata/selection-error checks do not establish completion override collector. HTTP bindings retain explicit presence and `int64` limits.
of these revised requirements.
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 **Discovery and source decisions: Settled.** Implement the ordered native search
and single external framework entry file specified above. Remove source/schema and single external framework entry file specified above. Remove source/schema

View File

@@ -2,15 +2,11 @@ package cli
import ( import (
"context" "context"
"errors"
"io" "io"
appconfig "gitea.maximumdirect.net/eric/scriptorium/internal/config"
"go.maximumdirect.net/ckconfig" "go.maximumdirect.net/ckconfig"
"go.maximumdirect.net/ckprompt" "go.maximumdirect.net/ckprompt"
"go.maximumdirect.net/commandkit" "go.maximumdirect.net/commandkit"
"go.maximumdirect.net/configkit"
"go.maximumdirect.net/promptkit"
) )
// Execute composes the native CLI and returns its finalized outcome. The caller // 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) { func newApp(identity commandkit.Identity) (*commandkit.App, error) {
application := ckconfig.FromConfigKit("app", "Scriptorium application configuration.", loadApplication) application := ckconfig.FromConfigKit("app", "Scriptorium application configuration.", loadApplication)
framework := ckprompt.ConfigScope(loadEngine) framework := ckprompt.ConfigScope(loadEngine)
configuration := ckconfig.Command(ckconfig.Suite[struct{}, struct{}]{ configuration := ckconfig.Command(ckconfig.Suite[applicationInput, struct{}]{
Scopes: []ckconfig.Scope[struct{}, struct{}]{application, framework}, NewInput: func() applicationInput { return applicationInput{} },
Scopes: []ckconfig.Scope[applicationInput, struct{}]{application, framework},
}) })
definitions := ckprompt.FromEngine(loadEngine) definitions := ckprompt.FromEngine(loadEngine)
declarations := []commandkit.Declaration{ declarations := []commandkit.Declaration{
commandkit.NewCommand(commandkit.CommandSpec[struct{}]{Name: "run", Summary: "Run a stored prompt.", Run: commandNotImplemented}), 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, configuration,
commandkit.Completion("scriptorium"), commandkit.Completion("scriptorium"),
} }
@@ -40,14 +39,14 @@ func newApp(identity commandkit.Identity) (*commandkit.App, error) {
return commandkit.New(identity, declarations...) return commandkit.New(identity, declarations...)
} }
// Application acquisition and effects are filled in independently of command type serveInput struct {
// declaration construction; metadata operations must never reach these callbacks. Selection ckconfig.Selection
func loadApplication(context.Context, ckconfig.Selection, struct{}, struct{}) (*configkit.Result[appconfig.Document], error) { Application applicationInput
return nil, errors.New("application configuration acquisition is not implemented")
} }
func loadEngine(context.Context, ckconfig.Selection, struct{}, struct{}) (*promptkit.Engine, error) { func serveFlags() []commandkit.Flag[serveInput] {
return nil, errors.New("engine acquisition is not implemented") 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 { func commandNotImplemented(commandkit.Invocation, struct{}) commandkit.Outcome {

View File

@@ -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)
}
}
}
}

View File

@@ -2,7 +2,6 @@ package cli
import ( import (
"errors" "errors"
"flag"
"fmt" "fmt"
"io" "io"
@@ -10,36 +9,6 @@ import (
"go.maximumdirect.net/configkit" "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) { func loadApplicationSettings(configPath, overlay string, explicit bool, overrides []configkit.Override) (appconfig.AppSettings, []configkit.Diagnostic, error) {
result, err := appconfig.Load(appconfig.LoadOptions{ result, err := appconfig.Load(appconfig.LoadOptions{
Path: configPath, Overlay: overlay, Explicit: explicit, Overrides: overrides, Path: configPath, Overlay: overlay, Explicit: explicit, Overrides: overrides,

View File

@@ -93,7 +93,6 @@ func configCommand(args []string, stdout, stderr io.Writer) int {
} }
func registerConfigurationOverrides(fs *flag.FlagSet) { func registerConfigurationOverrides(fs *flag.FlagSet) {
fs.String("schema-dir", "", "schema directory override")
fs.String("addr", "", "HTTP listen address override") fs.String("addr", "", "HTTP listen address override")
fs.String("artifact-root", "", "HTTP artifact root override") fs.String("artifact-root", "", "HTTP artifact root override")
fs.Int64("max-request-bytes", 0, "HTTP request byte limit override") fs.Int64("max-request-bytes", 0, "HTTP request byte limit override")

View File

@@ -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
}

View File

@@ -110,9 +110,6 @@ func parseFrameworkConfigurationArgs(args []string) (*frameworkConfigurationConf
fs := flag.NewFlagSet("inspect configuration "+cfg.mode, flag.ContinueOnError) fs := flag.NewFlagSet("inspect configuration "+cfg.mode, flag.ContinueOnError)
fs.SetOutput(io.Discard) fs.SetOutput(io.Discard)
registerConfigSelectionFlags(fs, &cfg.configPath, &cfg.configOverlay) 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)") fs.StringVar(&cfg.path, "path", "", "JSON Pointer to explain (required for explain)")
if err := fs.Parse(args[1:]); err != nil { if err := fs.Parse(args[1:]); err != nil {
return nil, err return nil, err

View File

@@ -1,71 +1,13 @@
package cli package cli
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"os"
"path/filepath"
"strings"
"go.maximumdirect.net/configkit" "go.maximumdirect.net/configkit"
"go.maximumdirect.net/promptkit" "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 { func writeFrameworkDiagnostics(output io.Writer, engine *promptkit.Engine) error {
view, err := engine.InspectConfiguration() view, err := engine.InspectConfiguration()
if err != nil { if err != nil {

View File

@@ -288,7 +288,6 @@ func parseRunArgs(args []string) (*runConfig, error) {
registerConfigSelectionFlags(fs, &cfg.configPath, &cfg.configOverlay) registerConfigSelectionFlags(fs, &cfg.configPath, &cfg.configOverlay)
registerExecutionRequestFlags(fs, cfg) 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.DurationVar(&cfg.runTimeout, "run-timeout", 0, "whole-run execution deadline")
fs.StringVar(&cfg.executionReportPath, "execution-report", "", "JSON execution report file path") 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) registerConfigSelectionFlags(fs, &cfg.configPath, &cfg.configOverlay)
registerExecutionRequestFlags(fs, &cfg.runConfig) registerExecutionRequestFlags(fs, &cfg.runConfig)
fs.StringVar(&cfg.schemaDir, "schema-dir", "", "base directory for validation schemas")
var rawFormat string var rawFormat string
fs.StringVar(&rawFormat, "format", "", "render output format (text|json)") 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) registerConfigSelectionFlags(fs, &cfg.configPath, &cfg.configOverlay)
fs.StringVar(&cfg.addr, "addr", "", "HTTP listen address") 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.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.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") 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) { 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.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.profileID, "profile", "", "optional execution profile ID; if omitted, prompt default_profile is used")
fs.StringVar(&cfg.sessionID, "session-id", "", "optional session ID") 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, maxRequestBytes: settings.MaxRequestBytes,
maxArtifactBytes: settings.MaxArtifactBytes, maxArtifactBytes: settings.MaxArtifactBytes,
maxResponseBytes: settings.MaxResponseBytes, maxResponseBytes: settings.MaxResponseBytes,
defaultRenderFormat: settings.DefaultRenderFormat, defaultRenderFormat: renderformat.DefaultPreparedRunOutputFormat,
transientRetries: settings.TransientRetries, transientRetries: settings.TransientRetries,
diagnostics: diagnostics, diagnostics: diagnostics,
}, nil }, nil
@@ -515,36 +508,6 @@ func (c serveConfig) engineSettings() engineSettings {
return engineSettings{promptkitSources: c.promptkitSources, schemaDir: c.schemaDir, transientRetries: c.transientRetries} 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) { func buildRunRequestFromConfig(cfg *runConfig) (promptkit.RunRequest, error) {
var inputMappings map[string]string var inputMappings map[string]string

View File

@@ -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)
}

View File

@@ -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())
}
}

View File

@@ -5,23 +5,20 @@ import (
"strings" "strings"
"gitea.maximumdirect.net/eric/scriptorium/internal/defaults" "gitea.maximumdirect.net/eric/scriptorium/internal/defaults"
renderformat "gitea.maximumdirect.net/eric/scriptorium/internal/format"
"go.maximumdirect.net/configkit" "go.maximumdirect.net/configkit"
) )
// Document is Scriptorium's ConfigKit-native application configuration shape. // Document is Scriptorium's ConfigKit-native application configuration shape.
type Document struct { type Document struct {
PromptKitSources []SourceSelection `yaml:"promptkit_sources"` PromptKit PromptKitDocument `yaml:"promptkit"`
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"`
} }
// SourceSelection identifies one PromptKit configuration file or directory. // PromptKitDocument selects framework acquisition, not embedded definitions.
type SourceSelection struct { type PromptKitDocument struct {
File string `yaml:"file"` Configuration string `yaml:"configuration"`
Directory string `yaml:"directory"` SchemaDir string `yaml:"schema_dir"`
} }
type ServerDocument struct { type ServerDocument struct {
@@ -32,10 +29,6 @@ type ServerDocument struct {
MaxResponseBytes int64 `yaml:"max_response_bytes"` MaxResponseBytes int64 `yaml:"max_response_bytes"`
} }
type DefaultsDocument struct {
RenderFormat string `yaml:"render_format"`
}
func documentDefinition() configkit.Definition[Document] { func documentDefinition() configkit.Definition[Document] {
return configkit.Definition[Document]{ return configkit.Definition[Document]{
Defaults: documentDefaults, Defaults: documentDefaults,
@@ -46,14 +39,13 @@ func documentDefinition() configkit.Definition[Document] {
func documentDefaults() Document { func documentDefaults() Document {
return Document{ return Document{
SchemaDir: defaults.SchemaDirDefault, PromptKit: PromptKitDocument{SchemaDir: defaults.SchemaDirDefault},
Server: ServerDocument{ Server: ServerDocument{
Addr: defaults.HTTPAddrDefault, Addr: defaults.HTTPAddrDefault,
MaxRequestBytes: defaults.HTTPMaxRequestBytesDefault, MaxRequestBytes: defaults.HTTPMaxRequestBytesDefault,
MaxArtifactBytes: defaults.HTTPMaxArtifactBytesDefault, MaxArtifactBytes: defaults.HTTPMaxArtifactBytesDefault,
MaxResponseBytes: defaults.HTTPMaxResponseBytesDefault, 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 { normalizePath(configkit.Keys("promptkit", "configuration"), &document.PromptKit.Configuration)
path := configkit.Keys("promptkit_sources").Index(i) normalizePath(configkit.Keys("promptkit", "schema_dir"), &document.PromptKit.SchemaDir)
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("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,
strings.TrimSpace(document.Server.Addr), "trimmed server address") 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 return nil
} }
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")
} }
@@ -107,9 +86,4 @@ func validateDocument(document *Document, validation *configkit.Validation) {
if document.Server.MaxResponseBytes < 0 { if document.Server.MaxResponseBytes < 0 {
validation.Error(configkit.Keys("server", "max_response_bytes"), "must be greater than or equal to zero") 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")
}
} }

View File

@@ -11,213 +11,109 @@ import (
) )
func loadDocumentBytes(content string) (*configkit.Result[Document], error) { func loadDocumentBytes(content string) (*configkit.Result[Document], error) {
return configkit.Load(configkit.Bytes("application.yml", []byte(content)), configkit.Options[Document]{ return configkit.Load(configkit.Bytes("application.yml", []byte(content)), configkit.Options[Document]{Definition: documentDefinition()})
Definition: documentDefinition(),
})
} }
func TestDocumentDefaultsAndExplicitValues(t *testing.T) { 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 { if err != nil {
t.Fatalf("defaults-only load: %v", err) t.Fatal(err)
} }
if !reflect.DeepEqual(defaultsOnly.Config, documentDefaults()) || len(defaultsOnly.Sources()) != 0 { got := result.Config
t.Fatalf("unexpected defaults-only result: %+v sources=%+v", defaultsOnly.Config, defaultsOnly.Sources()) 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)
} }
settings := Settings(got)
result, err := loadDocumentBytes(`promptkit_sources: if settings.SchemaDir != "" || settings.PromptKitConfiguration != "" || settings.ServerAddr != "" || settings.MaxRequestBytes != 0 || settings.TransientRetries == nil || *settings.TransientRetries != 0 {
- file: " ./bundle/../framework.yml " t.Fatalf("settings changed resolved values: %+v", settings)
- 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)
} }
config := result.Config result, err = loadDocumentBytes("transient_retries: null\n")
if config.SchemaDir != "" || config.Server.Addr != "" || config.Server.MaxRequestBytes != 0 || config.Server.MaxArtifactBytes != 0 || config.Server.MaxResponseBytes != 0 { if err != nil || result.Config.TransientRetries != nil {
t.Fatalf("explicit empty and zero values did not replace defaults: %+v", config) t.Fatalf("nullable retries: %+v %v", result, err)
}
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)
} }
} }
func TestDocumentSourceSelectionsDoNotAcquirePaths(t *testing.T) { func TestDocumentNormalizesWithoutAcquiringFramework(t *testing.T) {
for _, tc := range []struct { result, err := loadDocumentBytes("promptkit:\n configuration: ' ./missing/../framework.yml '\n schema_dir: ' ./a/../schemas '\nserver:\n addr: ' :9000 '\n artifact_root: ' ./a/../artifacts '\n")
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 "
`)
if err != nil { 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) { if result.Config.PromptKit.Configuration != "framework.yml" || result.Config.PromptKit.SchemaDir != "schemas" || result.Config.Server.Addr != ":9000" || result.Config.Server.ArtifactRoot != "artifacts" {
t.Fatalf("unexpected normalized source selections: %+v", result.Config.PromptKitSources) t.Fatalf("normalized: %+v", result.Config)
} }
if result.Config.Server.ArtifactRoot != "artifacts" || result.Config.Server.Addr != ":9000" { for _, key := range []string{"configuration", "schema_dir"} {
t.Fatalf("unexpected normalized values: %+v", result.Config) explanation, ok := result.Explain(configkit.Keys("promptkit", key))
} if !ok {
var effective bytes.Buffer t.Fatal("missing path evidence", key)
if err := result.WriteEffective(&effective); err != nil { }
t.Fatalf("write effective configuration: %v", err) normalized := false
} for _, change := range explanation.History {
for _, fragment := range []string{"file: two", "directory: ''", "directory: three", "artifact_root: artifacts", "addr: :9000", "render_format: json"} { normalized = normalized || change.Origin.Operation == configkit.ChangeNormalization
if !strings.Contains(effective.String(), fragment) { }
t.Fatalf("effective output omitted %q: %s", fragment, effective.String()) if !normalized {
t.Fatalf("normalization evidence missing: %+v", explanation)
} }
} }
explanation, known := result.Explain(configkit.Keys("promptkit_sources").Index(0).Key("file")) var output bytes.Buffer
if !known || explanation.Effective.YAML != "two\n" { if err := result.WriteEffective(&output); err != nil {
t.Fatalf("explanation does not agree with effective value: %+v known=%t", explanation, known) 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 { for _, change := range explanation.History {
if change.Origin.Operation == configkit.ChangeNormalization { if change.Origin.Operation == configkit.ChangeNormalization {
found = true t.Fatal("unchanged path was normalized")
}
}
if !found {
t.Fatalf("normalization was not recorded: %+v", explanation.History)
}
unchanged, err := loadDocumentBytes("promptkit_sources:\n - file: framework.yml\n")
if err != nil {
t.Fatalf("load unchanged source path: %v", err)
}
unchangedExplanation, known := unchanged.Explain(configkit.Keys("promptkit_sources").Index(0).Key("file"))
if !known {
t.Fatal("unchanged source path was not inspectable")
}
for _, change := range unchangedExplanation.History {
if change.Origin.Operation == configkit.ChangeNormalization {
t.Fatalf("unchanged source path recorded a normalization: %+v", unchangedExplanation.History)
} }
} }
} }
func TestDocumentValidationIsSafeAndPathSpecific(t *testing.T) { func TestDocumentValidationIsSafeAndPathSpecific(t *testing.T) {
for _, tc := range []struct{ name, content, path string }{ for _, key := range []string{"transient_retries", "max_request_bytes", "max_artifact_bytes", "max_response_bytes"} {
{"empty source selector", "promptkit_sources:\n - file: ''\n", "/promptkit_sources/0"}, content := key + ": -9173\n"
{"multiple source selectors", "promptkit_sources:\n - file: secret-path\n directory: private-path\n", "/promptkit_sources/0"}, path := "/" + key
{"negative retry", "transient_retries: -9173\n", "/transient_retries"}, if key != "transient_retries" {
{"negative request limit", "server:\n max_request_bytes: -9173\n", "/server/max_request_bytes"}, content = "server:\n " + content
{"negative artifact limit", "server:\n max_artifact_bytes: -9173\n", "/server/max_artifact_bytes"}, path = "/server" + path
{"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",
} {
_, err := loadDocumentBytes(content) _, err := loadDocumentBytes(content)
if err == nil { var native *configkit.LoadError
t.Fatalf("unexpected acceptance of %q", content) 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)
} }
} }
} }

View File

@@ -3,7 +3,6 @@ package config
import ( import (
"fmt" "fmt"
renderformat "gitea.maximumdirect.net/eric/scriptorium/internal/format"
"go.maximumdirect.net/configkit" "go.maximumdirect.net/configkit"
) )
@@ -15,13 +14,18 @@ type LoadOptions struct {
Overrides []configkit.Override 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. // Load resolves one ConfigKit document and retains its inspection snapshot.
func Load(options LoadOptions) (*configkit.Result[Document], error) { func Load(options LoadOptions) (*configkit.Result[Document], error) {
var source configkit.Source var source configkit.Source
if options.Explicit { if options.Explicit {
source = configkit.File(options.Path) source = configkit.File(options.Path)
} else { } else {
source = configkit.SearchConfig("scriptorium", configkit.SearchPaths(defaultConfigSearchPaths...)) source = configkit.SearchConfig("scriptorium", applicationSearch)
} }
result, err := configkit.Load(source, configkit.Options[Document]{ 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. // Settings converts a validated document without another merge or normalization.
func Settings(document Document) AppSettings { func Settings(document Document) AppSettings {
settings := AppSettings{ settings := AppSettings{
SchemaDir: document.SchemaDir, SchemaDir: document.PromptKit.SchemaDir,
ServerAddr: document.Server.Addr, PromptKitConfiguration: document.PromptKit.Configuration,
ArtifactRoot: document.Server.ArtifactRoot, ServerAddr: document.Server.Addr,
MaxRequestBytes: document.Server.MaxRequestBytes, ArtifactRoot: document.Server.ArtifactRoot,
MaxArtifactBytes: document.Server.MaxArtifactBytes, MaxRequestBytes: document.Server.MaxRequestBytes,
MaxResponseBytes: document.Server.MaxResponseBytes, MaxArtifactBytes: document.Server.MaxArtifactBytes,
DefaultRenderFormat: renderformat.PreparedRunOutputFormat(document.Defaults.RenderFormat), MaxResponseBytes: document.Server.MaxResponseBytes,
TransientRetries: document.TransientRetries, TransientRetries: document.TransientRetries,
}
if document.PromptKitSources != nil {
settings.PromptKitSources = append([]SourceSelection{}, document.PromptKitSources...)
} }
return settings return settings
} }

View File

@@ -5,333 +5,195 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"strings"
"testing" "testing"
"go.maximumdirect.net/configkit" "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) { func writeApplicationFile(t *testing.T, path, body string) {
t.Helper() 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) t.Fatal(err)
} }
} }
func TestLoadSelectsOrderedSourceAndKeepsItsSnapshot(t *testing.T) { func isolatedDiscovery(t *testing.T) string {
dir := t.TempDir() t.Helper()
first := filepath.Join(dir, "first.yml") root := t.TempDir()
second := filepath.Join(dir, "second.yml") t.Chdir(root)
writeApplicationFile(t, first, "promptkit_sources:\n - file: ./first.yml\n") t.Setenv("HOME", filepath.Join(root, "home"))
writeApplicationFile(t, second, "promptkit_sources:\n - directory: ./second\n") t.Setenv("XDG_CONFIG_HOME", filepath.Join(root, "xdg"))
useConfigSearchPaths(t, first, second) return root
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 TestLoadRejectsExplicitEmptyAndDistinguishesEmptyFile(t *testing.T) { func useIsolatedSearch(t *testing.T) {
useConfigSearchPaths(t, filepath.Join(t.TempDir(), "absent.yml")) t.Helper()
if _, err := Load(LoadOptions{Explicit: true, Path: ""}); err == nil { previous := applicationSearch
t.Fatal("explicit empty path was accepted") applicationSearch = configkit.WorkingDirectory()
} else { t.Cleanup(func() { applicationSearch = previous })
var loadError *configkit.LoadError }
if !errors.As(err, &loadError) {
t.Fatalf("expected upstream source diagnostic, got %v", err) 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)
} }
} }
// Do not let a host's system configuration influence exhaustion checks.
dir := t.TempDir() // The system preset's filename expansion remains upstream-owned.
empty := filepath.Join(dir, "empty.yml") useIsolatedSearch(t)
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"))
result, err := Load(LoadOptions{}) result, err := Load(LoadOptions{})
if err != nil || !reflect.DeepEqual(result.Config, documentDefaults()) || len(result.Sources()) != 0 { if err != nil || len(result.Sources()) != 0 || !reflect.DeepEqual(result.Config, documentDefaults()) {
t.Fatalf("defaults-only load: result=%+v err=%v", result, err) t.Fatalf("exhaustion: %+v %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)
} }
} }
func TestLoadDoesNotFallThroughSelectedInvalidSource(t *testing.T) { func TestLoadDoesNotFallThroughSelectedInvalidSource(t *testing.T) {
dir := t.TempDir() root := isolatedDiscovery(t)
first, second := filepath.Join(dir, "first.yml"), filepath.Join(dir, "second.yml") writeApplicationFile(t, filepath.Join(root, "scriptorium.yml"), "server: [\n")
writeApplicationFile(t, first, "server: [\n") writeApplicationFile(t, filepath.Join(root, "scriptorium.yaml"), "{}\n")
writeApplicationFile(t, second, "server:\n addr: ':9090'\n")
useConfigSearchPaths(t, first, second)
if _, err := Load(LoadOptions{}); err == nil { 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) { func TestLoadImportRequirednessAndDestination(t *testing.T) {
dir := t.TempDir() root := t.TempDir()
main := filepath.Join(dir, "config.yml") main := filepath.Join(root, "config.yml")
for _, tc := range []struct { writeApplicationFile(t, main, "config:\n imports:\n - path: absent.yml\n")
name, declaration string if _, err := Load(LoadOptions{Explicit: true, Path: main}); err != nil {
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 {
t.Fatal(err) 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") 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
info, ok := result.Metadata().Lookup(configkit.Keys("server", "addr")) info, ok := result.Metadata().Lookup(configkit.Keys("server", "addr"))
if result.Config.Server.Addr != ":8181" || !ok || len(info.Origin.Locations) == 0 { 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) t.Fatalf("value/origin: %+v %+v", result.Config, 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")
} }
} }

View File

@@ -1,26 +1,13 @@
package config 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. // AppSettings holds the validated application values used by executable adapters.
type AppSettings struct { type AppSettings struct {
PromptKitSources []SourceSelection PromptKitConfiguration string
SchemaDir string SchemaDir string
ServerAddr string ServerAddr string
ArtifactRoot string ArtifactRoot string
MaxRequestBytes int64 MaxRequestBytes int64
MaxArtifactBytes int64 MaxArtifactBytes int64
MaxResponseBytes int64 MaxResponseBytes int64
DefaultRenderFormat renderformat.PreparedRunOutputFormat TransientRetries *int
TransientRetries *int
} }