Compose application and prompt configuration together
This commit is contained in:
@@ -92,6 +92,14 @@ message, a main setting, and an overlay override. Main/companion behavior with
|
|||||||
no selected main is covered. Neither assembly nor these tests contacts a
|
no selected main is covered. Neither assembly nor these tests contacts a
|
||||||
provider. The former `WithSchemaFS` users will be removed in Stages 3 and 4.
|
provider. The former `WithSchemaFS` users will be removed in Stages 3 and 4.
|
||||||
|
|
||||||
|
**Complete.** The matched modules and combined application definition are in
|
||||||
|
place. Focused core and assembly tests pass, including main-relative glossary
|
||||||
|
imports, replacement and peer conflicts, prompt content files, overlay
|
||||||
|
selection, and absent-main discovery. The CLI and remaining runtime test
|
||||||
|
callers cannot yet compile against the new result and removed PromptKit APIs.
|
||||||
|
Their migration and integration checks are deferred to the following stages.
|
||||||
|
The superseded runtime companion assembly remains until its removal in Stage 3.
|
||||||
|
|
||||||
## Stage 2: Bootstrap Secrets and Wire Shared CLI Acquisitions
|
## Stage 2: Bootstrap Secrets and Wire Shared CLI Acquisitions
|
||||||
|
|
||||||
**Outcome:** Processing and native configuration commands use the same
|
**Outcome:** Processing and native configuration commands use the same
|
||||||
|
|||||||
10
go.mod
10
go.mod
@@ -4,11 +4,11 @@ go 1.27.0
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/santhosh-tekuri/jsonschema/v6 v6.0.3
|
github.com/santhosh-tekuri/jsonschema/v6 v6.0.3
|
||||||
go.maximumdirect.net/ckconfig v0.9.0
|
go.maximumdirect.net/ckconfig v0.11.0
|
||||||
go.maximumdirect.net/ckprompt v0.10.0
|
go.maximumdirect.net/ckprompt v0.12.0
|
||||||
go.maximumdirect.net/commandkit v0.7.0
|
go.maximumdirect.net/commandkit v0.8.0
|
||||||
go.maximumdirect.net/configkit v1.2.1
|
go.maximumdirect.net/configkit v1.4.0
|
||||||
go.maximumdirect.net/promptkit v0.23.2
|
go.maximumdirect.net/promptkit v0.25.0
|
||||||
go.maximumdirect.net/releasekit v0.10.0
|
go.maximumdirect.net/releasekit v0.10.0
|
||||||
go.yaml.in/yaml/v4 v4.0.0
|
go.yaml.in/yaml/v4 v4.0.0
|
||||||
)
|
)
|
||||||
|
|||||||
10
go.sum
10
go.sum
@@ -15,14 +15,24 @@ github.com/yuin/goldmark/v2 v2.1.16 h1:MXvCVo+Cb2SVJ5cwJUzXetDjI7HECRJCkcqylEQtc
|
|||||||
github.com/yuin/goldmark/v2 v2.1.16/go.mod h1:G6M4/qOFtfn01/o14BU1UR2Lo5N3S9Qo7xCuz/sHjGQ=
|
github.com/yuin/goldmark/v2 v2.1.16/go.mod h1:G6M4/qOFtfn01/o14BU1UR2Lo5N3S9Qo7xCuz/sHjGQ=
|
||||||
go.maximumdirect.net/ckconfig v0.9.0 h1:s9Ikga4wt4K1NpLCX9bLhm5vbcm1Fgqy9yk2lmO4Sss=
|
go.maximumdirect.net/ckconfig v0.9.0 h1:s9Ikga4wt4K1NpLCX9bLhm5vbcm1Fgqy9yk2lmO4Sss=
|
||||||
go.maximumdirect.net/ckconfig v0.9.0/go.mod h1:yGjFVr2UzjJjiwDdMwsKehuQfvvtv7rTEOIk5NQOY00=
|
go.maximumdirect.net/ckconfig v0.9.0/go.mod h1:yGjFVr2UzjJjiwDdMwsKehuQfvvtv7rTEOIk5NQOY00=
|
||||||
|
go.maximumdirect.net/ckconfig v0.11.0 h1:oTPW5R4vW5TNs97rqgABC/dir723rbGBz0FUyZ9QiNk=
|
||||||
|
go.maximumdirect.net/ckconfig v0.11.0/go.mod h1:9phvXeLAgQSBKpJWqPyIQASKVJr0nRhiPMIQkfNhaRo=
|
||||||
go.maximumdirect.net/ckprompt v0.10.0 h1:t2UyWSke+bWH9aTDrw5/bteD+TO0y5LD0NWw0y2KniQ=
|
go.maximumdirect.net/ckprompt v0.10.0 h1:t2UyWSke+bWH9aTDrw5/bteD+TO0y5LD0NWw0y2KniQ=
|
||||||
go.maximumdirect.net/ckprompt v0.10.0/go.mod h1:vx8lZnelASctQtyxY2yV4PuTZfLpE9EqDa+LPj3+SHM=
|
go.maximumdirect.net/ckprompt v0.10.0/go.mod h1:vx8lZnelASctQtyxY2yV4PuTZfLpE9EqDa+LPj3+SHM=
|
||||||
|
go.maximumdirect.net/ckprompt v0.12.0 h1:TPx4c0ohnG51F5eXk9SLHvkSKGNnWBhzkJAC5LdZCrE=
|
||||||
|
go.maximumdirect.net/ckprompt v0.12.0/go.mod h1:6DsEfIbKsFFzdAAV2TZD2LiwMvEG//s7zFOVe2wDvN0=
|
||||||
go.maximumdirect.net/commandkit v0.7.0 h1:toLDGhHoVe6gBX5HWuzQ/Joz0cBqEhJXolW21m3axBk=
|
go.maximumdirect.net/commandkit v0.7.0 h1:toLDGhHoVe6gBX5HWuzQ/Joz0cBqEhJXolW21m3axBk=
|
||||||
go.maximumdirect.net/commandkit v0.7.0/go.mod h1:sVp3JXKpm2YrP4Ie8Q3nWmQgwBdPN8uO+wQU72erDns=
|
go.maximumdirect.net/commandkit v0.7.0/go.mod h1:sVp3JXKpm2YrP4Ie8Q3nWmQgwBdPN8uO+wQU72erDns=
|
||||||
|
go.maximumdirect.net/commandkit v0.8.0 h1:EBDjr7SvfgvVDsA2S95hkOhgZe7oGZdDvURDJxHyX48=
|
||||||
|
go.maximumdirect.net/commandkit v0.8.0/go.mod h1:sVp3JXKpm2YrP4Ie8Q3nWmQgwBdPN8uO+wQU72erDns=
|
||||||
go.maximumdirect.net/configkit v1.2.1 h1:f+/6WjNl6CULbMr6ZSVwi4aty9rmuDfJlvpeU7I+HZM=
|
go.maximumdirect.net/configkit v1.2.1 h1:f+/6WjNl6CULbMr6ZSVwi4aty9rmuDfJlvpeU7I+HZM=
|
||||||
go.maximumdirect.net/configkit v1.2.1/go.mod h1:ewu6blSokUDCMovy8JBXAdXRikGmlHXAQpFghnPWRfs=
|
go.maximumdirect.net/configkit v1.2.1/go.mod h1:ewu6blSokUDCMovy8JBXAdXRikGmlHXAQpFghnPWRfs=
|
||||||
|
go.maximumdirect.net/configkit v1.4.0 h1:TfOupZbQ23/ZMs+xt5lmyukCF11ekqKep8KbmFQD63o=
|
||||||
|
go.maximumdirect.net/configkit v1.4.0/go.mod h1:ewu6blSokUDCMovy8JBXAdXRikGmlHXAQpFghnPWRfs=
|
||||||
go.maximumdirect.net/promptkit v0.23.2 h1:nE+Qqqbw5GN4PHD2e5OEWUHXcYkESZm3ryZVBsKWwFQ=
|
go.maximumdirect.net/promptkit v0.23.2 h1:nE+Qqqbw5GN4PHD2e5OEWUHXcYkESZm3ryZVBsKWwFQ=
|
||||||
go.maximumdirect.net/promptkit v0.23.2/go.mod h1:vPFg76VXcffP0JBFMS2WADCHwV5FYvO2O4bvIoiDlwc=
|
go.maximumdirect.net/promptkit v0.23.2/go.mod h1:vPFg76VXcffP0JBFMS2WADCHwV5FYvO2O4bvIoiDlwc=
|
||||||
|
go.maximumdirect.net/promptkit v0.25.0 h1:vJOfxauwASllOSDceR2oypJ4NEwIh2s6F8dDexEzdM8=
|
||||||
|
go.maximumdirect.net/promptkit v0.25.0/go.mod h1:o2axdkV11KTPDgk81i2vrJ66dtcedXRhau/Ko3YSZ4A=
|
||||||
go.maximumdirect.net/releasekit v0.10.0 h1:3GOgaIigXTkvYaRkntWZASE//2NTR1+IhkoKmt64aJo=
|
go.maximumdirect.net/releasekit v0.10.0 h1:3GOgaIigXTkvYaRkntWZASE//2NTR1+IhkoKmt64aJo=
|
||||||
go.maximumdirect.net/releasekit v0.10.0/go.mod h1:cT5NZNEcf7SKDx4MPG40YNK9V4bNyIxcLLFuNlbGQ90=
|
go.maximumdirect.net/releasekit v0.10.0/go.mod h1:cT5NZNEcf7SKDx4MPG40YNK9V4bNyIxcLLFuNlbGQ90=
|
||||||
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
|
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
|
||||||
|
|||||||
89
internal/appconfig/config.go
Normal file
89
internal/appconfig/config.go
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
// Package appconfig composes Audita and PromptKit configuration sources.
|
||||||
|
package appconfig
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/audita/v4/internal/core/config"
|
||||||
|
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/responseschema"
|
||||||
|
"gitea.maximumdirect.net/eric/audita/v4/internal/prompts"
|
||||||
|
ck "go.maximumdirect.net/configkit"
|
||||||
|
pk "go.maximumdirect.net/promptkit"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Config combines Audita's root settings with native PromptKit settings.
|
||||||
|
type Config struct {
|
||||||
|
Application config.Config `yaml:",inline"`
|
||||||
|
PromptKit pk.Settings `yaml:"promptkit"`
|
||||||
|
}
|
||||||
|
|
||||||
|
const glossaryImports = `config:
|
||||||
|
imports:
|
||||||
|
- path: glossary.yml
|
||||||
|
required: false
|
||||||
|
destination: [glossary_collections]
|
||||||
|
- dir: glossary
|
||||||
|
required: false
|
||||||
|
recursive: true
|
||||||
|
pattern: ["*.yml", "*.yaml"]
|
||||||
|
destination: [glossary_collections]
|
||||||
|
`
|
||||||
|
|
||||||
|
const operatorImports = `config:
|
||||||
|
imports:
|
||||||
|
- path: prompts.yml
|
||||||
|
required: false
|
||||||
|
- dir: prompts
|
||||||
|
required: false
|
||||||
|
recursive: true
|
||||||
|
pattern: ["*.yml", "*.yaml"]
|
||||||
|
`
|
||||||
|
|
||||||
|
// Definition combines application rules with PromptKit's native rules.
|
||||||
|
func Definition() ck.Definition[Config] {
|
||||||
|
domain := config.Definition()
|
||||||
|
definition := ck.Definition[Config]{
|
||||||
|
Defaults: func() Config { return Config{Application: domain.Defaults()} },
|
||||||
|
Version: domain.Version,
|
||||||
|
SequenceIdentities: domain.SequenceIdentities,
|
||||||
|
Sensitive: domain.Sensitive,
|
||||||
|
Resolve: func(value *Config, resolution *ck.Resolution) error {
|
||||||
|
return domain.Resolve(&value.Application, resolution)
|
||||||
|
},
|
||||||
|
Validate: func(value *Config, validation *ck.Validation) {
|
||||||
|
domain.Validate(&value.Application, validation)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return pk.Configure(definition, func(value *Config) *pk.Settings { return &value.PromptKit })
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load composes built-in resources and optional companions with the selected input.
|
||||||
|
func Load(input ck.Input, overlay string, overrides ...ck.Override) (*ck.Result[Config], error) {
|
||||||
|
embedded, err := pk.FilesystemInputs("Audita embedded prompts", prompts.Filesystem(), prompts.AssetRoot)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("discover embedded prompts: %w", err)
|
||||||
|
}
|
||||||
|
for i := range embedded {
|
||||||
|
embedded[i].Destination = ck.Keys("promptkit")
|
||||||
|
}
|
||||||
|
schemas := ck.NewFilesystem("Audita embedded schemas", responseschema.Filesystem())
|
||||||
|
embedded = append(embedded, ck.Input{
|
||||||
|
Source: pk.SchemaRootSource("Audita embedded schemas", responseschema.AssetRoot),
|
||||||
|
Destination: ck.Keys("promptkit"),
|
||||||
|
Filesystem: &ck.FilesystemContext{FS: &schemas, Root: ".", BaseDir: "."},
|
||||||
|
})
|
||||||
|
input.Imports = true
|
||||||
|
return ck.LoadLayered(input, ck.LayeredOptions[Config]{
|
||||||
|
Definition: Definition(),
|
||||||
|
Overlay: overlay,
|
||||||
|
Overrides: overrides,
|
||||||
|
Underlays: []ck.Underlay{
|
||||||
|
pk.BundledOpenRouterUnderlay(),
|
||||||
|
{Inputs: embedded},
|
||||||
|
{Inputs: []ck.Input{
|
||||||
|
{Source: ck.Bytes("Audita glossary imports", []byte(glossaryImports)), Imports: true, MainRelative: ck.MainRelative()},
|
||||||
|
{Source: ck.Bytes("Audita automatic prompt imports", []byte(operatorImports)), Destination: ck.Keys("promptkit"), Imports: true, MainRelative: ck.MainRelative()},
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
77
internal/appconfig/config_test.go
Normal file
77
internal/appconfig/config_test.go
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
package appconfig
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
ck "go.maximumdirect.net/configkit"
|
||||||
|
pk "go.maximumdirect.net/promptkit"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLoadCombinesApplicationAndPromptSettings(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
if err := os.Mkdir(filepath.Join(dir, "overlays"), 0755); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for name, data := range map[string]string{
|
||||||
|
"config.yml": "version: 3\nllm:\n transient_retries: 4\n",
|
||||||
|
"prompts.yml": "profiles:\n - id: local\n model: fixture\ncollections:\n example:\n prompts:\n - id: sample\n messages:\n - role: user\n content_file: message.txt\n output:\n format: text\n validation_mode: none\n",
|
||||||
|
"message.txt": "A source-local message.",
|
||||||
|
"overlays/selected.yml": "context:\n description: overlay\n",
|
||||||
|
} {
|
||||||
|
if err := os.WriteFile(filepath.Join(dir, name), []byte(data), 0600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "config.yml"))}, "selected")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if result.Config.Application.Version != 3 || result.Config.Application.LLM.TransientRetries != 4 {
|
||||||
|
t.Fatalf("application settings: %+v", result.Config.Application)
|
||||||
|
}
|
||||||
|
if result.Config.Application.Context.Description != "overlay" {
|
||||||
|
t.Fatalf("overlay description = %q", result.Config.Application.Context.Description)
|
||||||
|
}
|
||||||
|
settings, ok := result.Value(ck.Keys("promptkit"))
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("missing PromptKit settings")
|
||||||
|
}
|
||||||
|
if _, ok := settings.(pk.Settings); !ok {
|
||||||
|
t.Fatalf("PromptKit settings have type %T", settings)
|
||||||
|
}
|
||||||
|
engine, err := pk.NewFromConfig(result, pk.RuntimeConfig{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
prompt, err := engine.InspectPrompt(context.Background(), "example/sample")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("inspect source-local prompt: %v", err)
|
||||||
|
}
|
||||||
|
if len(prompt.Messages) != 1 || prompt.Messages[0].Content != "A source-local message." {
|
||||||
|
t.Fatalf("captured messages = %+v", prompt.Messages)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadWithoutMainDoesNotDiscoverCompanions(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
if err := os.WriteFile(filepath.Join(dir, "prompts.yml"), []byte("profiles: [\n"), 0600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Chdir(dir)
|
||||||
|
result, err := Load(ck.Input{Source: ck.NoSource()}, "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if result.Config.Application.Version != 3 {
|
||||||
|
t.Fatalf("version = %d", result.Config.Application.Version)
|
||||||
|
}
|
||||||
|
for _, source := range result.Sources() {
|
||||||
|
if strings.HasSuffix(source.Path, "prompts.yml") || strings.HasSuffix(source.Path, "glossary.yml") {
|
||||||
|
t.Fatalf("unexpected companion source: %+v", source)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package config
|
package appconfig
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
@@ -52,21 +52,21 @@ party:
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("%v: %+v", err, err.(*ck.LoadError).Diagnostics())
|
t.Fatalf("%v: %+v", err, err.(*ck.LoadError).Diagnostics())
|
||||||
}
|
}
|
||||||
party := result.Config.GlossaryCollections["party"].Entries
|
party := result.Config.Application.GlossaryCollections["party"].Entries
|
||||||
if len(party) != 2 || party[0].Name != "Revised" || len(party[0].Aliases) != 0 || party[1].ID != "other" {
|
if len(party) != 2 || party[0].Name != "Revised" || len(party[0].Aliases) != 0 || party[1].ID != "other" {
|
||||||
t.Fatalf("party=%+v", party)
|
t.Fatalf("party=%+v", party)
|
||||||
}
|
}
|
||||||
if party[0].Extra["level"] == nil {
|
if party[0].Extra["level"] == nil {
|
||||||
t.Fatalf("extension missing: %+v", party[0])
|
t.Fatalf("extension missing: %+v", party[0])
|
||||||
}
|
}
|
||||||
if got := result.Config.GlossaryCollections["lore"].Entries; len(got) != 1 || got[0].ID != "hero" {
|
if got := result.Config.Application.GlossaryCollections["lore"].Entries; len(got) != 1 || got[0].ID != "hero" {
|
||||||
t.Fatalf("lore=%+v", got)
|
t.Fatalf("lore=%+v", got)
|
||||||
}
|
}
|
||||||
discovered, err := Load(ck.Input{Source: ck.SearchConfig("audita", ck.SearchDirectory(dir))}, "")
|
discovered, err := Load(ck.Input{Source: ck.SearchConfig("audita", ck.SearchDirectory(dir))}, "")
|
||||||
if err != nil || len(discovered.Config.GlossaryCollections["party"].Entries) != 2 {
|
if err != nil || len(discovered.Config.Application.GlossaryCollections["party"].Entries) != 2 {
|
||||||
t.Fatalf("discovered glossary: %+v %v", discovered, err)
|
t.Fatalf("discovered glossary: %+v %v", discovered, err)
|
||||||
}
|
}
|
||||||
spells := result.Config.GlossaryCollections["spells"].Entries
|
spells := result.Config.Application.GlossaryCollections["spells"].Entries
|
||||||
if len(spells) != 1 || spells[0].Plural == nil || *spells[0].Plural != "Bolts" || spells[0].Extra["extra"] == nil {
|
if len(spells) != 1 || spells[0].Plural == nil || *spells[0].Plural != "Bolts" || spells[0].Extra["extra"] == nil {
|
||||||
t.Fatalf("spells=%+v", spells)
|
t.Fatalf("spells=%+v", spells)
|
||||||
}
|
}
|
||||||
@@ -133,7 +133,7 @@ func TestGlossaryUnderlaySkipsAbsentMain(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if len(result.Config.GlossaryCollections) != 0 {
|
if len(result.Config.Application.GlossaryCollections) != 0 {
|
||||||
t.Fatalf("unanchored glossary loaded: %+v", result.Config.GlossaryCollections)
|
t.Fatalf("unanchored glossary loaded: %+v", result.Config.Application.GlossaryCollections)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
113
internal/appconfig/sources_test.go
Normal file
113
internal/appconfig/sources_test.go
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
package appconfig
|
||||||
|
|
||||||
|
import (
|
||||||
|
ck "go.maximumdirect.net/configkit"
|
||||||
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMainContextImportsAndOverlay(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
if err := os.Mkdir(filepath.Join(dir, "overlays"), 0755); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for name, contents := range map[string]string{
|
||||||
|
"main.yml": "version: 3\nconfig:\n imports:\n - path: fragment.yml\noutput:\n schema: bare-segments\n",
|
||||||
|
"fragment.yml": "context:\n description: imported\n",
|
||||||
|
"overlays/selected.yml": "context:\n description: overlay\n",
|
||||||
|
} {
|
||||||
|
if err := os.WriteFile(filepath.Join(dir, name), []byte(contents), 0644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "main.yml"))}, "selected", ck.Override{Path: ck.Keys("context", "description"), Value: "override", Flag: "--description"})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if info, ok := result.Metadata().Lookup(ck.Keys("context", "description")); !ok || info.Origin.Layer != ck.LayerCLI {
|
||||||
|
t.Fatalf("override origin = %+v", info)
|
||||||
|
}
|
||||||
|
if result.Config.Application.Context.Description != "override" {
|
||||||
|
t.Fatalf("description = %q", result.Config.Application.Context.Description)
|
||||||
|
}
|
||||||
|
found := false
|
||||||
|
for _, source := range result.Sources() {
|
||||||
|
if source.Layer == ck.LayerMain && source.ImportedFrom == nil {
|
||||||
|
context, ok := result.SourceContext(source.ID)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("main context missing")
|
||||||
|
}
|
||||||
|
data, err := fs.ReadFile(context.FS, path.Join(context.BaseDir, "fragment.yml"))
|
||||||
|
if err != nil || len(data) == 0 {
|
||||||
|
t.Fatalf("main context cannot access companion: %v", err)
|
||||||
|
}
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
t.Fatal("main source missing")
|
||||||
|
}
|
||||||
|
if _, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "main.yml"))}, "missing"); err == nil {
|
||||||
|
t.Fatal("missing overlay accepted")
|
||||||
|
}
|
||||||
|
if _, err := Load(ck.Input{Source: ck.NoSource()}, "selected"); err == nil {
|
||||||
|
t.Fatal("overlay without context accepted")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDiscoveryAndSelectedFailure(t *testing.T) {
|
||||||
|
first, second := t.TempDir(), t.TempDir()
|
||||||
|
search := ck.Searches(ck.SearchDirectory(first), ck.SearchDirectory(second))
|
||||||
|
load := func() (*ck.Result[Config], error) {
|
||||||
|
return Load(ck.Input{Source: ck.SearchConfig("audita", search)}, "")
|
||||||
|
}
|
||||||
|
result, err := load()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, source := range result.Sources() {
|
||||||
|
if source.Layer == ck.LayerMain {
|
||||||
|
t.Fatalf("unexpected main source: %+v", source)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(filepath.Join(second, "config.yml"), []byte("context:\n description: selected\n"), 0644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
result, err = load()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if result.Config.Application.Context.Description != "selected" {
|
||||||
|
t.Fatalf("description = %q", result.Config.Application.Context.Description)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(filepath.Join(first, "config.yml"), []byte("version: 2\n"), 0644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := load(); err == nil {
|
||||||
|
t.Fatal("invalid selected file fell through")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOverlayUsesMainDirectoryWithinWiderRoot(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
dir := filepath.Join(root, "project")
|
||||||
|
if err := os.MkdirAll(filepath.Join(dir, "overlays"), 0755); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(filepath.Join(dir, "config.yml"), []byte("version: 3\n"), 0644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(filepath.Join(dir, "overlays", "local.yml"), []byte("context:\n description: project\n"), 0644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
result, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "config.yml")), Filesystem: &ck.FilesystemContext{Root: root}}, "local")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if result.Config.Application.Context.Description != "project" {
|
||||||
|
t.Fatalf("description = %q", result.Config.Application.Context.Description)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/config"
|
"gitea.maximumdirect.net/eric/audita/v4/internal/appconfig"
|
||||||
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/llm"
|
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/llm"
|
||||||
"gitea.maximumdirect.net/eric/audita/v4/internal/prompts"
|
"gitea.maximumdirect.net/eric/audita/v4/internal/prompts"
|
||||||
"go.maximumdirect.net/ckconfig"
|
"go.maximumdirect.net/ckconfig"
|
||||||
@@ -27,7 +27,7 @@ type engineEntry struct {
|
|||||||
engine *pk.Engine
|
engine *pk.Engine
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadApplication(ctx context.Context, selection ckconfig.Selection, _ struct{}, _ *invocationState) (*ck.Result[config.Config], error) {
|
func loadApplication(ctx context.Context, selection ckconfig.Selection, _ struct{}, _ *invocationState) (*ck.Result[appconfig.Config], error) {
|
||||||
if err := ctx.Err(); err != nil {
|
if err := ctx.Err(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -40,7 +40,7 @@ func loadApplication(ctx context.Context, selection ckconfig.Selection, _ struct
|
|||||||
} else {
|
} else {
|
||||||
source = ck.SearchConfig("audita", ck.Searches(ck.WorkingDirectory(), ck.UserConfigDirectory(), ck.UnixSystemDirectories()))
|
source = ck.SearchConfig("audita", ck.Searches(ck.WorkingDirectory(), ck.UserConfigDirectory(), ck.UnixSystemDirectories()))
|
||||||
}
|
}
|
||||||
return config.Load(ck.Input{Source: source}, selection.Overlay.Value, selection.Overrides...)
|
return appconfig.Load(ck.Input{Source: source}, selection.Overlay.Value, selection.Overrides...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadPromptEngine(ctx context.Context, selection ckconfig.Selection, input struct{}, state *invocationState) (*pk.Engine, error) {
|
func loadPromptEngine(ctx context.Context, selection ckconfig.Selection, input struct{}, state *invocationState) (*pk.Engine, error) {
|
||||||
|
|||||||
@@ -107,37 +107,20 @@ func defaults() Config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const glossaryImports = `config:
|
// Definition describes the application settings independently of their sources.
|
||||||
imports:
|
func Definition() ck.Definition[Config] {
|
||||||
- path: glossary.yml
|
return ck.Definition[Config]{
|
||||||
required: false
|
Defaults: defaults,
|
||||||
destination: [glossary_collections]
|
SequenceIdentities: []ck.SequenceIdentity{ck.SequenceIdentityMatching(ck.SequenceSelector{ck.SequenceLiteralKey("glossary_collections"), ck.SequenceAnyMappingKey(), ck.SequenceLiteralKey("entries")}, "id")},
|
||||||
- dir: glossary
|
Version: &ck.VersionRule{Path: ck.Keys("version"), Check: func(version ck.Version, v *ck.Validation) error {
|
||||||
required: false
|
if version.Present && (version.Null || version.Text != "3") {
|
||||||
recursive: true
|
v.Error(ck.Keys("version"), "use application configuration version 3; see docs/migrations/v3.md")
|
||||||
pattern: ["*.yml", "*.yaml"]
|
}
|
||||||
destination: [glossary_collections]
|
return nil
|
||||||
`
|
}},
|
||||||
|
Resolve: resolveSettings,
|
||||||
// Load resolves the selected main source and preserves native source evidence.
|
Validate: validate,
|
||||||
func Load(input ck.Input, overlay string, overrides ...ck.Override) (*ck.Result[Config], error) {
|
}
|
||||||
input.Imports = true
|
|
||||||
return ck.LoadLayered(input, ck.LayeredOptions[Config]{
|
|
||||||
Overlay: overlay, Overrides: overrides,
|
|
||||||
Underlays: []ck.Underlay{{Inputs: []ck.Input{{Source: ck.Bytes("Audita glossary imports", []byte(glossaryImports)), Imports: true, MainRelative: ck.MainRelative()}}}},
|
|
||||||
Definition: ck.Definition[Config]{
|
|
||||||
Defaults: defaults,
|
|
||||||
SequenceIdentities: []ck.SequenceIdentity{ck.SequenceIdentityMatching(ck.SequenceSelector{ck.SequenceLiteralKey("glossary_collections"), ck.SequenceAnyMappingKey(), ck.SequenceLiteralKey("entries")}, "id")},
|
|
||||||
Version: &ck.VersionRule{Path: ck.Keys("version"), Check: func(version ck.Version, v *ck.Validation) error {
|
|
||||||
if version.Present && (version.Null || version.Text != "3") {
|
|
||||||
v.Error(ck.Keys("version"), "use application configuration version 3; see docs/migrations/v3.md")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}},
|
|
||||||
Resolve: resolveSettings,
|
|
||||||
Validate: validate,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func validate(c *Config, v *ck.Validation) {
|
func validate(c *Config, v *ck.Validation) {
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/fs"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -40,109 +38,6 @@ func TestDefaultsAndVersion(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMainContextImportsAndOverlay(t *testing.T) {
|
|
||||||
dir := t.TempDir()
|
|
||||||
if err := os.Mkdir(filepath.Join(dir, "overlays"), 0755); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
for name, contents := range map[string]string{
|
|
||||||
"main.yml": "version: 3\nconfig:\n imports:\n - path: fragment.yml\noutput:\n schema: bare-segments\n",
|
|
||||||
"fragment.yml": "context:\n description: imported\n",
|
|
||||||
"overlays/selected.yml": "context:\n description: overlay\n",
|
|
||||||
} {
|
|
||||||
if err := os.WriteFile(filepath.Join(dir, name), []byte(contents), 0644); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "main.yml"))}, "selected", ck.Override{Path: ck.Keys("context", "description"), Value: "override", Flag: "--description"})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if info, ok := result.Metadata().Lookup(ck.Keys("context", "description")); !ok || info.Origin.Layer != ck.LayerCLI {
|
|
||||||
t.Fatalf("override origin = %+v", info)
|
|
||||||
}
|
|
||||||
if result.Config.Context.Description != "override" {
|
|
||||||
t.Fatalf("description = %q", result.Config.Context.Description)
|
|
||||||
}
|
|
||||||
found := false
|
|
||||||
for _, source := range result.Sources() {
|
|
||||||
if source.Layer == ck.LayerMain && source.ImportedFrom == nil {
|
|
||||||
context, ok := result.SourceContext(source.ID)
|
|
||||||
if !ok {
|
|
||||||
t.Fatal("main context missing")
|
|
||||||
}
|
|
||||||
data, err := fs.ReadFile(context.FS, path.Join(context.BaseDir, "fragment.yml"))
|
|
||||||
if err != nil || len(data) == 0 {
|
|
||||||
t.Fatalf("main context cannot access companion: %v", err)
|
|
||||||
}
|
|
||||||
found = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
t.Fatal("main source missing")
|
|
||||||
}
|
|
||||||
if _, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "main.yml"))}, "missing"); err == nil {
|
|
||||||
t.Fatal("missing overlay accepted")
|
|
||||||
}
|
|
||||||
if _, err := Load(ck.Input{Source: ck.NoSource()}, "selected"); err == nil {
|
|
||||||
t.Fatal("overlay without context accepted")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDiscoveryAndSelectedFailure(t *testing.T) {
|
|
||||||
first, second := t.TempDir(), t.TempDir()
|
|
||||||
search := ck.Searches(ck.SearchDirectory(first), ck.SearchDirectory(second))
|
|
||||||
load := func() (*ck.Result[Config], error) {
|
|
||||||
return Load(ck.Input{Source: ck.SearchConfig("audita", search)}, "")
|
|
||||||
}
|
|
||||||
result, err := load()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
for _, source := range result.Sources() {
|
|
||||||
if source.Layer == ck.LayerMain {
|
|
||||||
t.Fatalf("unexpected main source: %+v", source)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := os.WriteFile(filepath.Join(second, "config.yml"), []byte("context:\n description: selected\n"), 0644); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
result, err = load()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if result.Config.Context.Description != "selected" {
|
|
||||||
t.Fatalf("description = %q", result.Config.Context.Description)
|
|
||||||
}
|
|
||||||
if err := os.WriteFile(filepath.Join(first, "config.yml"), []byte("version: 2\n"), 0644); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, err := load(); err == nil {
|
|
||||||
t.Fatal("invalid selected file fell through")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestOverlayUsesMainDirectoryWithinWiderRoot(t *testing.T) {
|
|
||||||
root := t.TempDir()
|
|
||||||
dir := filepath.Join(root, "project")
|
|
||||||
if err := os.MkdirAll(filepath.Join(dir, "overlays"), 0755); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := os.WriteFile(filepath.Join(dir, "config.yml"), []byte("version: 3\n"), 0644); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := os.WriteFile(filepath.Join(dir, "overlays", "local.yml"), []byte("context:\n description: project\n"), 0644); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
result, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "config.yml")), Filesystem: &ck.FilesystemContext{Root: root}}, "local")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if result.Config.Context.Description != "project" {
|
|
||||||
t.Fatalf("description = %q", result.Config.Context.Description)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSchemaConstraintsAndExplicitValues(t *testing.T) {
|
func TestSchemaConstraintsAndExplicitValues(t *testing.T) {
|
||||||
path := filepath.Join(t.TempDir(), "config.yml")
|
path := filepath.Join(t.TempDir(), "config.yml")
|
||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
|
|||||||
12
internal/core/config/loader_test.go
Normal file
12
internal/core/config/loader_test.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import ck "go.maximumdirect.net/configkit"
|
||||||
|
|
||||||
|
func Load(input ck.Input, overlay string, overrides ...ck.Override) (*ck.Result[Config], error) {
|
||||||
|
input.Imports = true
|
||||||
|
return ck.LoadLayered(input, ck.LayeredOptions[Config]{
|
||||||
|
Definition: Definition(),
|
||||||
|
Overlay: overlay,
|
||||||
|
Overrides: overrides,
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user