Compose application and prompt configuration together

This commit is contained in:
2026-11-23 04:14:15 +00:00
parent 82c52c8e7f
commit f87c45331c
11 changed files with 338 additions and 151 deletions

View 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()},
}},
},
})
}

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

View File

@@ -1,4 +1,4 @@
package config
package appconfig
import (
"os"
@@ -52,21 +52,21 @@ party:
if err != nil {
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" {
t.Fatalf("party=%+v", party)
}
if party[0].Extra["level"] == nil {
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)
}
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)
}
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 {
t.Fatalf("spells=%+v", spells)
}
@@ -133,7 +133,7 @@ func TestGlossaryUnderlaySkipsAbsentMain(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if len(result.Config.GlossaryCollections) != 0 {
t.Fatalf("unanchored glossary loaded: %+v", result.Config.GlossaryCollections)
if len(result.Config.Application.GlossaryCollections) != 0 {
t.Fatalf("unanchored glossary loaded: %+v", result.Config.Application.GlossaryCollections)
}
}

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

View File

@@ -7,7 +7,7 @@ import (
"reflect"
"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/prompts"
"go.maximumdirect.net/ckconfig"
@@ -27,7 +27,7 @@ type engineEntry struct {
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 {
return nil, err
}
@@ -40,7 +40,7 @@ func loadApplication(ctx context.Context, selection ckconfig.Selection, _ struct
} else {
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) {

View File

@@ -107,37 +107,20 @@ func defaults() Config {
}
}
const glossaryImports = `config:
imports:
- path: glossary.yml
required: false
destination: [glossary_collections]
- dir: glossary
required: false
recursive: true
pattern: ["*.yml", "*.yaml"]
destination: [glossary_collections]
`
// Load resolves the selected main source and preserves native source evidence.
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,
},
})
// Definition describes the application settings independently of their sources.
func Definition() ck.Definition[Config] {
return 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) {

View File

@@ -1,9 +1,7 @@
package config
import (
"io/fs"
"os"
"path"
"path/filepath"
"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) {
path := filepath.Join(t.TempDir(), "config.yml")
for _, tc := range []struct {

View 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,
})
}