Compose runtime glossary files with native configuration

This commit is contained in:
2026-11-26 18:17:16 +00:00
parent b1cde637a2
commit a4b8fd0976
26 changed files with 292 additions and 92 deletions

View File

@@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"path/filepath"
"gitea.maximumdirect.net/eric/audita/v4/internal/core/config"
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/responseschema"
@@ -30,6 +31,23 @@ type Config struct {
PromptKit pk.Settings `yaml:"promptkit"`
}
type Options struct {
Overlay string
Overrides []ck.Override
GlossaryFiles []string
}
type Acquisition struct {
*ck.Result[Config]
GlossarySources []string
}
// GlossaryInputError identifies failures in the separate runtime input load.
type GlossaryInputError struct{ Err error }
func (e *GlossaryInputError) Error() string { return e.Err.Error() }
func (e *GlossaryInputError) Unwrap() error { return e.Err }
const glossaryImports = `config:
imports:
- path: glossary.yml
@@ -71,7 +89,7 @@ func Definition() ck.Definition[Config] {
}
// Load composes built-in resources and optional companions with the selected input.
func Load(ctx context.Context, input ck.Input, overlay string, overrides ...ck.Override) (*ck.Result[Config], *ck.SecretsReport, error) {
func Load(ctx context.Context, input ck.Input, options Options) (*Acquisition, *ck.SecretsReport, error) {
if err := ctx.Err(); err != nil {
return nil, nil, err
}
@@ -83,6 +101,38 @@ func Load(ctx context.Context, input ck.Input, overlay string, overrides ...ck.O
return nil, report, err
}
input.Source = prepared
var glossarySources []string
overrides := append([]ck.Override(nil), options.Overrides...)
if len(options.GlossaryFiles) > 0 {
inputs := make([]ck.Input, len(options.GlossaryFiles))
for i, path := range options.GlossaryFiles {
absolute, err := filepath.Abs(path)
if err != nil {
return nil, report, &GlossaryInputError{Err: err}
}
inputs[i] = ck.Input{Source: ck.File(absolute), Imports: true}
}
underlays := make([]ck.Underlay, 0, len(inputs)-1)
for _, item := range inputs[:len(inputs)-1] {
underlays = append(underlays, ck.Underlay{Inputs: []ck.Input{item}})
}
glossaries, err := ck.LoadLayered(inputs[len(inputs)-1], ck.LayeredOptions[map[string]any]{
Definition: ck.Definition[map[string]any]{SequenceIdentities: []ck.SequenceIdentity{config.GlossaryEntryIdentity(nil)}},
Underlays: underlays,
})
if canceled := ctx.Err(); canceled != nil {
return nil, report, errors.Join(err, canceled)
}
if err != nil {
return nil, report, &GlossaryInputError{Err: err}
}
for _, source := range glossaries.Sources() {
if source.Kind == ck.SourceKindFile {
glossarySources = append(glossarySources, source.Path)
}
}
overrides = append(overrides, ck.Override{Path: ck.Keys("glossary_collections"), Value: glossaries.Config, Flag: "--glossary-file"})
}
embedded, err := pk.FilesystemInputs("Audita embedded prompts", prompts.Filesystem(), prompts.AssetRoot)
if err != nil {
return nil, report, &AssetError{Err: fmt.Errorf("discover embedded prompts: %w", err)}
@@ -99,7 +149,7 @@ func Load(ctx context.Context, input ck.Input, overlay string, overrides ...ck.O
input.Imports = true
result, err := ck.LoadLayered(input, ck.LayeredOptions[Config]{
Definition: Definition(),
Overlay: overlay,
Overlay: options.Overlay,
Overrides: overrides,
Underlays: []ck.Underlay{
pk.BundledOpenRouterUnderlay(),
@@ -113,5 +163,8 @@ func Load(ctx context.Context, input ck.Input, overlay string, overrides ...ck.O
if canceled := ctx.Err(); canceled != nil {
return nil, report, errors.Join(err, canceled)
}
return result, report, err
if err != nil {
return nil, report, err
}
return &Acquisition{Result: result, GlossarySources: glossarySources}, report, nil
}

View File

@@ -26,7 +26,7 @@ func TestLoadCombinesApplicationAndPromptSettings(t *testing.T) {
t.Fatal(err)
}
}
result, _, err := Load(context.Background(), ck.Input{Source: ck.File(filepath.Join(dir, "config.yml"))}, "selected")
result, _, err := Load(context.Background(), ck.Input{Source: ck.File(filepath.Join(dir, "config.yml"))}, Options{Overlay: "selected"})
if err != nil {
t.Fatal(err)
}
@@ -62,7 +62,7 @@ func TestLoadWithoutMainDoesNotDiscoverCompanions(t *testing.T) {
t.Fatal(err)
}
t.Chdir(dir)
result, _, err := Load(context.Background(), ck.Input{Source: ck.NoSource()}, "")
result, _, err := Load(context.Background(), ck.Input{Source: ck.NoSource()}, Options{})
if err != nil {
t.Fatal(err)
}

View File

@@ -50,7 +50,7 @@ party:
`)
write("shared/extra.yml", "lore:\n entries:\n - id: hero\n name: Ancient\n category: lore\n")
write("glossary/nested/spells.yaml", "spells:\n entries:\n - id: bolt\n name: Bolt\n category: spell\n aliases: [Spark]\n plural: Bolts\n extra: {rank: 2}\n")
result, _, err := Load(context.Background(), ck.Input{Source: ck.File(filepath.Join(dir, "config.yml"))}, "")
result, _, err := Load(context.Background(), ck.Input{Source: ck.File(filepath.Join(dir, "config.yml"))}, Options{})
if err != nil {
t.Fatalf("%v: %+v", err, err.(*ck.LoadError).Diagnostics())
}
@@ -67,7 +67,7 @@ party:
if got := result.Config.Application.GlossaryCollections["lore"].Entries; len(got) != 1 || got[0].ID != "hero" {
t.Fatalf("lore=%+v", got)
}
discovered, _, err := Load(context.Background(), ck.Input{Source: ck.SearchConfig("audita", ck.SearchDirectory(dir))}, "")
discovered, _, err := Load(context.Background(), ck.Input{Source: ck.SearchConfig("audita", ck.SearchDirectory(dir))}, Options{})
if err != nil || len(discovered.Config.Application.GlossaryCollections["party"].Entries) != 2 {
t.Fatalf("discovered glossary: %+v %v", discovered, err)
}
@@ -102,7 +102,7 @@ func TestGlossaryValidationAndPeerConflicts(t *testing.T) {
"glossary_collections:\n x:\n entries:\n - {id: 'id with spaces', name: N, category: C}\n",
} {
write(main, data)
if _, _, err := Load(context.Background(), ck.Input{Source: ck.File(main)}, ""); err != nil {
if _, _, err := Load(context.Background(), ck.Input{Source: ck.File(main)}, Options{}); err != nil {
t.Fatalf("valid %q: %v", data, err)
}
}
@@ -113,7 +113,7 @@ func TestGlossaryValidationAndPeerConflicts(t *testing.T) {
"glossary_collections:\n x:\n entries:\n - {id: x, name: N, category: C, plural: ''}\n",
} {
write(main, data)
if _, _, err := Load(context.Background(), ck.Input{Source: ck.File(main)}, ""); err == nil {
if _, _, err := Load(context.Background(), ck.Input{Source: ck.File(main)}, Options{}); err == nil {
t.Fatalf("accepted %q", data)
}
}
@@ -123,7 +123,7 @@ func TestGlossaryValidationAndPeerConflicts(t *testing.T) {
t.Fatal(err)
}
write(filepath.Join(dir, "glossary", "other.yml"), "x:\n entries:\n - {id: same, name: Two, category: C}\n")
if _, _, err := Load(context.Background(), ck.Input{Source: ck.File(main)}, ""); err == nil {
if _, _, err := Load(context.Background(), ck.Input{Source: ck.File(main)}, Options{}); err == nil {
t.Fatal("peer conflict accepted")
}
}
@@ -134,7 +134,7 @@ func TestGlossaryUnderlaySkipsAbsentMain(t *testing.T) {
t.Fatal(err)
}
t.Chdir(dir)
result, _, err := Load(context.Background(), ck.Input{Source: ck.NoSource()}, "")
result, _, err := Load(context.Background(), ck.Input{Source: ck.NoSource()}, Options{})
if err != nil {
t.Fatal(err)
}

View File

@@ -0,0 +1,136 @@
package appconfig
import (
"context"
"errors"
"os"
"path/filepath"
"slices"
"testing"
ck "go.maximumdirect.net/configkit"
)
func TestRuntimeGlossaryComposition(t *testing.T) {
dir := t.TempDir()
files := map[string]string{
"main.yml": `version: 3
glossary_collections:
party:
instruction: Keep the configured guidance
entries: [{id: configured, name: Configured, category: person}]
reference:
entries: [{id: reference, name: Reference, category: lore}]
`,
"overlays/selected.yml": `glossary_collections:
party:
entries: [{id: overlay, name: Overlay, category: person}]
`,
"first.yml": `config:
imports: [{path: imported.yml, required: true}]
party:
entries:
- {id: shared, name: Old, category: person, aliases: [Former]}
- {id: retained, name: Retained, category: person}
`,
"imported.yml": `other:
entries: [{id: imported, name: Imported, category: lore}]
`,
"last.yml": `party:
entries:
- {id: shared, name: New, category: person, note: kept}
`,
}
for name, contents := range files {
path := filepath.Join(dir, name)
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(contents), 0644); err != nil {
t.Fatal(err)
}
}
paths := []string{filepath.Join(dir, "first.yml"), filepath.Join(dir, "last.yml")}
acquired, _, err := Load(context.Background(), ck.Input{Source: ck.File(filepath.Join(dir, "main.yml"))}, Options{Overlay: "selected", GlossaryFiles: paths})
if err != nil {
t.Fatal(err)
}
collections := acquired.Config.Application.GlossaryCollections
party := collections["party"]
if party.Instruction != "Keep the configured guidance" || len(party.Entries) != 2 {
t.Fatalf("party = %+v", party)
}
if party.Entries[0].ID != "shared" || party.Entries[0].Name != "New" || len(party.Entries[0].Aliases) != 0 || party.Entries[0].Extra["note"] != "kept" || party.Entries[1].ID != "retained" {
t.Fatalf("entries = %+v", party.Entries)
}
if len(collections["reference"].Entries) != 1 || len(collections["other"].Entries) != 1 {
t.Fatalf("collections = %+v", collections)
}
for _, name := range []string{"first.yml", "last.yml", "imported.yml"} {
path := filepath.Join(dir, name)
if !slices.Contains(acquired.GlossarySources, path) {
t.Errorf("missing source %s: %v", path, acquired.GlossarySources)
}
for _, source := range acquired.Sources() {
if source.Path == path {
t.Errorf("preliminary source leaked into final result: %s", path)
}
}
}
info, ok := acquired.Metadata().Lookup(ck.Keys("glossary_collections", "party", "entries"))
if !ok || info.Origin.Layer != ck.LayerCLI {
t.Fatalf("origin = %+v", info)
}
}
func TestRuntimeGlossaryLoadErrors(t *testing.T) {
dir := t.TempDir()
main := filepath.Join(dir, "main.yml")
if err := os.WriteFile(main, []byte("version: 3\n"), 0644); err != nil {
t.Fatal(err)
}
malformed := filepath.Join(dir, "bad.yml")
if err := os.WriteFile(malformed, []byte("party: [\n"), 0644); err != nil {
t.Fatal(err)
}
for _, path := range []string{malformed, filepath.Join(dir, "missing.yml")} {
_, report, err := Load(context.Background(), ck.Input{Source: ck.File(main)}, Options{GlossaryFiles: []string{path}})
var inputErr *GlossaryInputError
var loadErr *ck.LoadError
if !errors.As(err, &inputErr) || !errors.As(err, &loadErr) || report == nil {
t.Fatalf("path %s: report=%v error=%v", path, report, err)
}
}
}
func TestRuntimeGlossaryPresence(t *testing.T) {
for _, tc := range []struct {
name, body string
wantInstruction string
wantEntries int
}{
{"omitted", "party: {}\n", "original", 1},
{"empty", "party: {instruction: '', entries: []}\n", "", 0},
{"null entries", "party: {entries: null}\n", "original", 0},
} {
t.Run(tc.name, func(t *testing.T) {
dir := t.TempDir()
main := filepath.Join(dir, "main.yml")
incoming := filepath.Join(dir, "input.yml")
if err := os.WriteFile(main, []byte("glossary_collections:\n party:\n instruction: original\n entries: [{id: a, name: A, category: C}]\n"), 0644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(incoming, []byte(tc.body), 0644); err != nil {
t.Fatal(err)
}
got, _, err := Load(context.Background(), ck.Input{Source: ck.File(main)}, Options{GlossaryFiles: []string{incoming}})
if err != nil {
t.Fatalf("%v: %+v", err, err.(*ck.LoadError).Diagnostics())
}
party := got.Config.Application.GlossaryCollections["party"]
if party.Instruction != tc.wantInstruction || len(party.Entries) != tc.wantEntries {
t.Fatalf("party = %+v", party)
}
})
}
}

View File

@@ -24,7 +24,7 @@ func TestLoadBootstrapsSecrets(t *testing.T) {
if err := os.WriteFile(main, []byte("version: 3\nconfig:\n secrets_dir: secrets\n secrets_overwrite: true\n"), 0600); err != nil {
t.Fatal(err)
}
result, report, err := Load(context.Background(), ck.Input{Source: ck.File(main)}, "")
result, report, err := Load(context.Background(), ck.Input{Source: ck.File(main)}, Options{})
if err != nil {
t.Fatal(err)
}
@@ -39,14 +39,14 @@ func TestLoadRetainsBootstrapErrorAndCancellation(t *testing.T) {
if err := os.WriteFile(main, []byte("version: 3\nconfig:\n secrets_dir: missing\n"), 0600); err != nil {
t.Fatal(err)
}
result, report, err := Load(context.Background(), ck.Input{Source: ck.File(main)}, "")
result, report, err := Load(context.Background(), ck.Input{Source: ck.File(main)}, Options{})
var secretsErr *ck.SecretsError
if result != nil || report == nil || !errors.As(err, &secretsErr) {
t.Fatalf("result=%v report=%v error=%v", result, report, err)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
result, _, err = Load(ctx, ck.Input{Source: ck.File(main)}, "")
result, _, err = Load(ctx, ck.Input{Source: ck.File(main)}, Options{})
if result != nil || !errors.Is(err, context.Canceled) {
t.Fatalf("result=%v error=%v", result, err)
}
@@ -65,7 +65,7 @@ func TestLoadReusesPreparedMainSnapshot(t *testing.T) {
if err := os.WriteFile(main, []byte("version: 3\ncontext:\n description: changed\n"), 0600); err != nil {
t.Fatal(err)
}
result, _, err := Load(context.Background(), ck.Input{Source: prepared, Filesystem: &ck.FilesystemContext{Root: dir}}, "")
result, _, err := Load(context.Background(), ck.Input{Source: prepared, Filesystem: &ck.FilesystemContext{Root: dir}}, Options{})
if err != nil {
t.Fatal(err)
}

View File

@@ -24,7 +24,7 @@ func TestMainContextImportsAndOverlay(t *testing.T) {
t.Fatal(err)
}
}
result, _, err := Load(context.Background(), ck.Input{Source: ck.File(filepath.Join(dir, "main.yml"))}, "selected", ck.Override{Path: ck.Keys("context", "description"), Value: "override", Flag: "--description"})
result, _, err := Load(context.Background(), ck.Input{Source: ck.File(filepath.Join(dir, "main.yml"))}, Options{Overlay: "selected", Overrides: []ck.Override{{Path: ck.Keys("context", "description"), Value: "override", Flag: "--description"}}})
if err != nil {
t.Fatal(err)
}
@@ -51,10 +51,10 @@ func TestMainContextImportsAndOverlay(t *testing.T) {
if !found {
t.Fatal("main source missing")
}
if _, _, err := Load(context.Background(), ck.Input{Source: ck.File(filepath.Join(dir, "main.yml"))}, "missing"); err == nil {
if _, _, err := Load(context.Background(), ck.Input{Source: ck.File(filepath.Join(dir, "main.yml"))}, Options{Overlay: "missing"}); err == nil {
t.Fatal("missing overlay accepted")
}
if _, _, err := Load(context.Background(), ck.Input{Source: ck.NoSource()}, "selected"); err == nil {
if _, _, err := Load(context.Background(), ck.Input{Source: ck.NoSource()}, Options{Overlay: "selected"}); err == nil {
t.Fatal("overlay without context accepted")
}
}
@@ -63,8 +63,11 @@ 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) {
result, _, err := Load(context.Background(), ck.Input{Source: ck.SearchConfig("audita", search)}, "")
return result, err
result, _, err := Load(context.Background(), ck.Input{Source: ck.SearchConfig("audita", search)}, Options{})
if err != nil {
return nil, err
}
return result.Result, nil
}
result, err := load()
if err != nil {
@@ -105,7 +108,7 @@ func TestOverlayUsesMainDirectoryWithinWiderRoot(t *testing.T) {
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(context.Background(), ck.Input{Source: ck.File(filepath.Join(dir, "config.yml")), Filesystem: &ck.FilesystemContext{Root: root}}, "local")
result, _, err := Load(context.Background(), ck.Input{Source: ck.File(filepath.Join(dir, "config.yml")), Filesystem: &ck.FilesystemContext{Root: root}}, Options{Overlay: "local"})
if err != nil {
t.Fatal(err)
}