Share one native application configuration load and captured engine
This commit is contained in:
@@ -1,36 +1,159 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/assets"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/config"
|
||||
"go.maximumdirect.net/ckconfig"
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
func loadConfigKit(configPath, overlay string, overrides []configkit.Override, opts Options) (*config.LoadedConfig, string, error) {
|
||||
var source configkit.Source
|
||||
if path := strings.TrimSpace(configPath); path != "" {
|
||||
source = configkit.File(path)
|
||||
} else if path, ok := opts.LookupEnv("NOTARIUS_CONFIG"); ok && strings.TrimSpace(path) != "" {
|
||||
source = configkit.File(strings.TrimSpace(path))
|
||||
} else {
|
||||
source = configkit.SearchConfig("notarius", configkit.Searches(
|
||||
configkit.WorkingDirectory(), configkit.UserConfigDirectory(), configkit.UnixSystemDirectories(),
|
||||
), configkit.SearchOptions{Required: true})
|
||||
// nativeLoaders belongs to one invocation. Construction performs no load,
|
||||
// engine construction, or run-storage allocation.
|
||||
type nativeLoaders struct {
|
||||
lookupEnv func(string) (string, bool)
|
||||
runtime promptkit.RuntimeConfig
|
||||
engineOptions []promptkit.Option
|
||||
selections []loadedSelection
|
||||
}
|
||||
|
||||
type loadedSelection struct {
|
||||
selection ckconfig.Selection
|
||||
result *configkit.Result[config.Document]
|
||||
engine *promptkit.Engine
|
||||
}
|
||||
|
||||
func (loaders *nativeLoaders) application(ctx context.Context, selection ckconfig.Selection) (*configkit.Result[config.Document], error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
workingDirectory, err := os.Getwd()
|
||||
for _, captured := range loaders.selections {
|
||||
if reflect.DeepEqual(captured.selection, selection) {
|
||||
return captured.result, nil
|
||||
}
|
||||
}
|
||||
source, err := loaders.mainSource(selection)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("determine invocation directory: %w", err)
|
||||
return nil, err
|
||||
}
|
||||
loaded, err := config.LoadSource(source, config.LoadOptions{Overlay: overlay, Overrides: overrides, InvocationDirectory: workingDirectory})
|
||||
invocationDirectory, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
return nil, fmt.Errorf("determine invocation directory: %w", err)
|
||||
}
|
||||
options := configkit.LayeredOptions[config.Document]{
|
||||
Definition: config.Definition(invocationDirectory),
|
||||
Underlays: nativeUnderlays(),
|
||||
Overrides: selection.Overrides,
|
||||
}
|
||||
if selection.Overlay.Set {
|
||||
options.Overlay = selection.Overlay.Value
|
||||
}
|
||||
result, err := configkit.LoadLayered(configkit.Input{Source: source, Imports: true}, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
loaders.selections = append(loaders.selections, loadedSelection{selection: captureSelection(selection), result: result})
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (loaders *nativeLoaders) engine(ctx context.Context, selection ckconfig.Selection) (*promptkit.Engine, error) {
|
||||
result, err := loaders.application(ctx, selection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for index := range loaders.selections {
|
||||
captured := &loaders.selections[index]
|
||||
if captured.result != result {
|
||||
continue
|
||||
}
|
||||
if captured.engine == nil {
|
||||
captured.engine, err = promptkit.NewFromConfig(result, loaders.runtime, loaders.engineOptions...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return captured.engine, nil
|
||||
}
|
||||
return nil, fmt.Errorf("application selection was not retained")
|
||||
}
|
||||
|
||||
func (loaders *nativeLoaders) mainSource(selection ckconfig.Selection) (configkit.Source, error) {
|
||||
if selection.Config.Set {
|
||||
if strings.TrimSpace(selection.Config.Value) == "" {
|
||||
return configkit.Source{}, fmt.Errorf("--config must not be empty")
|
||||
}
|
||||
return configkit.File(strings.TrimSpace(selection.Config.Value)), nil
|
||||
}
|
||||
lookup := loaders.lookupEnv
|
||||
if lookup == nil {
|
||||
lookup = os.LookupEnv
|
||||
}
|
||||
if path, ok := lookup("NOTARIUS_CONFIG"); ok && strings.TrimSpace(path) != "" {
|
||||
return configkit.File(strings.TrimSpace(path)), nil
|
||||
}
|
||||
return configkit.SearchConfig("notarius", configkit.Searches(
|
||||
configkit.WorkingDirectory(), configkit.UserConfigDirectory(), configkit.UnixSystemDirectories(),
|
||||
), configkit.SearchOptions{Required: true}), nil
|
||||
}
|
||||
|
||||
func nativeUnderlays() []configkit.Underlay {
|
||||
filesystem := configkit.NewFilesystem("Notarius assets", assets.FS())
|
||||
// Both companion locations are imports of one native source in one strict
|
||||
// peer group. MainRelative uses the actual selected main source's directory.
|
||||
companions := promptkit.SettingsInput(configkit.Bytes("operator PromptKit companions", []byte(`config:
|
||||
imports:
|
||||
- path: prompts.yml
|
||||
required: false
|
||||
- dir: prompts
|
||||
pattern: ["*.yml", "*.yaml"]
|
||||
recursive: true
|
||||
required: false
|
||||
`)))
|
||||
companions.MainRelative = &configkit.MainRelativeContext{RequireBase: true}
|
||||
companions.Imports = true
|
||||
return []configkit.Underlay{
|
||||
promptkit.BundledOpenRouterUnderlay(),
|
||||
{Inputs: []configkit.Input{{
|
||||
Source: filesystem.File("notarius.yml"), Imports: true,
|
||||
Filesystem: &configkit.FilesystemContext{FS: &filesystem, Root: "."},
|
||||
}}},
|
||||
{Inputs: []configkit.Input{companions}},
|
||||
}
|
||||
}
|
||||
|
||||
func captureSelection(selection ckconfig.Selection) ckconfig.Selection {
|
||||
selection.Overrides = append([]configkit.Override(nil), selection.Overrides...)
|
||||
for index := range selection.Overrides {
|
||||
selection.Overrides[index].Value = captureOverrideValue(selection.Overrides[index].Value)
|
||||
}
|
||||
return selection
|
||||
}
|
||||
|
||||
func captureOverrideValue(value any) any {
|
||||
switch value := value.(type) {
|
||||
case map[string]any:
|
||||
copy := make(map[string]any, len(value))
|
||||
for key, item := range value {
|
||||
copy[key] = captureOverrideValue(item)
|
||||
}
|
||||
return copy
|
||||
case []any:
|
||||
copy := make([]any, len(value))
|
||||
for index, item := range value {
|
||||
copy[index] = captureOverrideValue(item)
|
||||
}
|
||||
return copy
|
||||
default:
|
||||
return value
|
||||
}
|
||||
return loaded, loaded.MainSourcePath(), nil
|
||||
}
|
||||
|
||||
func writeConfigLoadError(output io.Writer, err error) {
|
||||
|
||||
173
internal/cli/native_load_test.go
Normal file
173
internal/cli/native_load_test.go
Normal file
@@ -0,0 +1,173 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go.maximumdirect.net/ckconfig"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
"go.maximumdirect.net/configkit"
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
func nativeSelection(path string) ckconfig.Selection {
|
||||
return ckconfig.Selection{Config: commandkit.Supplied[string]{Set: true, Value: path}}
|
||||
}
|
||||
|
||||
func TestNativeLoadUsesOptionalCompanionsAndStrictPeers(t *testing.T) {
|
||||
t.Parallel()
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
files map[string]string
|
||||
invalid bool
|
||||
}{
|
||||
{"absent", nil, false},
|
||||
{"recursive", map[string]string{"prompts/nested/resources.yaml": "profiles: [{id: partial, model: fixture}]\n"}, false},
|
||||
{"conflicting", map[string]string{
|
||||
"prompts.yml": "profiles: [{id: collision, model: first}]\n",
|
||||
"prompts/nested/resources.yaml": "profiles: [{id: collision, model: second}]\n",
|
||||
}, true},
|
||||
{"malformed", map[string]string{"prompts.yml": "profiles: [\n"}, true},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
main := filepath.Join(dir, "config.yml")
|
||||
writeConfigKitFile(t, main, "version: 6\n")
|
||||
for file, contents := range test.files {
|
||||
writeConfigKitFile(t, filepath.Join(dir, file), contents)
|
||||
}
|
||||
loaders := &nativeLoaders{}
|
||||
result, err := loaders.application(context.Background(), nativeSelection(main))
|
||||
if test.invalid {
|
||||
if err == nil {
|
||||
t.Fatal("invalid companion group fell back")
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(result.Config.PromptKit.Collections["notarius"].Prompts) == 0 {
|
||||
t.Fatal("embedded collection missing")
|
||||
}
|
||||
if test.name == "recursive" {
|
||||
if len(result.Config.PromptKit.Profiles) != 1 || result.Config.PromptKit.Profiles[0].ID != "partial" {
|
||||
t.Fatal("recursive companion not loaded")
|
||||
}
|
||||
}
|
||||
for _, surface := range []string{"notarius-output", "notarius-debug", "notarius-cache"} {
|
||||
if _, err := os.Stat(filepath.Join(dir, surface)); !os.IsNotExist(err) {
|
||||
t.Fatalf("loader created run storage %q: %v", surface, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNativeLoadRetainsImportsInheritanceAndSourceRelativeMessages(t *testing.T) {
|
||||
t.Parallel()
|
||||
dir := t.TempDir()
|
||||
main := filepath.Join(dir, "config.yml")
|
||||
writeConfigKitFile(t, main, "version: 6\n")
|
||||
writeConfigKitFile(t, filepath.Join(dir, "prompts.yml"), `config:
|
||||
imports:
|
||||
- path: resources/custom.yml
|
||||
required: true
|
||||
`)
|
||||
writeConfigKitFile(t, filepath.Join(dir, "resources", "message.md"), "Imported native message")
|
||||
writeConfigKitFile(t, filepath.Join(dir, "resources", "custom.yml"), `collections:
|
||||
notarius:
|
||||
profiles:
|
||||
- id: child
|
||||
extends: {id: dnd-extraction}
|
||||
timeout_seconds: 7
|
||||
prompts:
|
||||
- id: custom
|
||||
default_profile: notarius/child
|
||||
messages: [{role: user, content_file: message.md}]
|
||||
output: {format: text, validation_mode: none}
|
||||
`)
|
||||
loaders := &nativeLoaders{}
|
||||
selection := nativeSelection(main)
|
||||
engine, err := loaders.engine(context.Background(), selection)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
prepared, err := engine.Prepare(context.Background(), promptkit.RunRequest{PromptID: "notarius/custom", APIKey: "fixture"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(prepared.Messages) != 1 || prepared.Messages[0].Content != "Imported native message" || prepared.EffectiveModelParams.TimeoutSeconds != 7 {
|
||||
t.Fatalf("native context lost: %#v", prepared)
|
||||
}
|
||||
writeConfigKitFile(t, filepath.Join(dir, "resources", "message.md"), "Changed message")
|
||||
same, err := loaders.engine(context.Background(), selection)
|
||||
if err != nil || same != engine {
|
||||
t.Fatalf("invocation engine was replaced: %v", err)
|
||||
}
|
||||
again, err := same.Prepare(context.Background(), promptkit.RunRequest{PromptID: "notarius/custom", APIKey: "fixture"})
|
||||
if err != nil || again.Messages[0].Content != "Imported native message" {
|
||||
t.Fatalf("captured engine changed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNativeLoadDistinguishesOverlayAndOverrideSelections(t *testing.T) {
|
||||
t.Parallel()
|
||||
dir := t.TempDir()
|
||||
main := filepath.Join(dir, "config.yml")
|
||||
writeConfigKitFile(t, main, "version: 6\n")
|
||||
writeConfigKitFile(t, filepath.Join(dir, "overlays", "alternate.yml"), "concurrency: {workers: 3}\n")
|
||||
loaders := &nativeLoaders{}
|
||||
base := nativeSelection(main)
|
||||
left, err := loaders.application(context.Background(), base)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rightSelection := base
|
||||
rightSelection.Overlay = commandkit.Supplied[string]{Set: true, Value: "alternate"}
|
||||
right, err := loaders.application(context.Background(), rightSelection)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if left == right || *right.Config.Concurrency.Workers != 3 {
|
||||
t.Fatal("diff selections were collapsed")
|
||||
}
|
||||
rightSelection.Overrides = []configkit.Override{{Path: configkit.Keys("concurrency", "workers"), Value: 1, Flag: "--set"}}
|
||||
overridden, err := loaders.application(context.Background(), rightSelection)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if overridden == right || *overridden.Config.Concurrency.Workers != 1 {
|
||||
t.Fatal("override selection was collapsed")
|
||||
}
|
||||
same, err := loaders.application(context.Background(), base)
|
||||
if err != nil || same != left {
|
||||
t.Fatal("complete selection was not reused")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNativeSourceRejectsExplicitEmptyAndHonorsEnvironment(t *testing.T) {
|
||||
t.Parallel()
|
||||
main := filepath.Join(t.TempDir(), "config.yml")
|
||||
writeConfigKitFile(t, main, "version: 6\n")
|
||||
loaders := &nativeLoaders{lookupEnv: func(key string) (string, bool) { return main, key == "NOTARIUS_CONFIG" }}
|
||||
if _, err := loaders.application(context.Background(), nativeSelection("")); err == nil || !strings.Contains(err.Error(), "--config") {
|
||||
t.Fatalf("explicit empty config discovered another file: %v", err)
|
||||
}
|
||||
result, err := loaders.application(context.Background(), ckconfig.Selection{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
found := false
|
||||
for _, source := range result.Sources() {
|
||||
if source.Layer == configkit.LayerMain && source.Path == main {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("environment selection lost")
|
||||
}
|
||||
}
|
||||
57
internal/cli/native_schema_load_test.go
Normal file
57
internal/cli/native_schema_load_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
func TestNativeLoadPreparesOperatorSchemaSelections(t *testing.T) {
|
||||
t.Parallel()
|
||||
for _, test := range []struct {
|
||||
name, schema, root string
|
||||
missing bool
|
||||
}{
|
||||
{"inline", "schema: {type: object}", "", false},
|
||||
{"source file", "schema_file: response.json", "", false},
|
||||
{"external root", "schema_path: response.json", "schema_root: external-schemas", false},
|
||||
{"replaced root missing embedded schema", "schema_path: dnd/scenes/schemas/dnd_scenes_llm.v1.json", "schema_root: external-schemas", true},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
main := filepath.Join(dir, "config.yml")
|
||||
writeConfigKitFile(t, main, "version: 6\n")
|
||||
writeConfigKitFile(t, filepath.Join(dir, "response.json"), `{"type":"object"}`)
|
||||
writeConfigKitFile(t, filepath.Join(dir, "external-schemas", "response.json"), `{"type":"object"}`)
|
||||
writeConfigKitFile(t, filepath.Join(dir, "prompts.yml"), "collections:\n notarius:\n "+test.root+"\n prompts:\n - id: dnd.scenes\n default_profile: notarius/dnd-extraction\n messages: [{role: user, content: operator-message}]\n output:\n format: json\n validation_mode: json_schema\n "+test.schema+"\n")
|
||||
loaders := &nativeLoaders{}
|
||||
engine, err := loaders.engine(context.Background(), nativeSelection(main))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request := promptkit.RunRequest{PromptID: "notarius/dnd.scenes", APIKey: "fixture"}
|
||||
prepared, err := engine.PrepareExecution(context.Background(), request)
|
||||
if test.missing {
|
||||
if err == nil {
|
||||
prepared.Discard()
|
||||
t.Fatal("root replacement silently retained embedded schema root")
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer prepared.Discard()
|
||||
details := prepared.Details()
|
||||
if len(details.Messages) != 1 || details.Messages[0].Content != "operator-message" || details.StructuredOutput == nil {
|
||||
t.Fatalf("replacement contract = %#v", details)
|
||||
}
|
||||
// Replacing one identified prompt must not discard unrelated embedded prompts.
|
||||
if _, err := engine.InspectPrompt(context.Background(), "notarius/dnd.spells"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -70,6 +70,12 @@ func documentDefinition(state *documentLoadState) configkit.Definition[Document]
|
||||
return promptkit.Configure(definition, func(document *Document) *promptkit.Settings { return &document.PromptKit })
|
||||
}
|
||||
|
||||
// Definition supplies the application's native composition rules. The caller
|
||||
// owns source selection and loading; each definition owns its resolution state.
|
||||
func Definition(invocationDirectory string) configkit.Definition[Document] {
|
||||
return documentDefinition(newDocumentLoadState(invocationDirectory))
|
||||
}
|
||||
|
||||
func documentDefaults() Document {
|
||||
return Document{
|
||||
Pipelines: map[string]PipelineDocument{},
|
||||
|
||||
@@ -59,7 +59,7 @@ func loadConfig(path string, options LoadOptions) (*LoadedConfig, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newLoadedConfig(result)
|
||||
return FromResult(result)
|
||||
}
|
||||
|
||||
// Load reads a version 6 configuration file into runtime configuration and a
|
||||
@@ -74,7 +74,7 @@ func LoadSource(source configkit.Source, options LoadOptions) (*LoadedConfig, er
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newLoadedConfig(result)
|
||||
return FromResult(result)
|
||||
}
|
||||
|
||||
// MainSourcePath returns the selected main file's recorded path, if any.
|
||||
@@ -95,10 +95,12 @@ func loadConfigSource(source configkit.Source, options LoadOptions) (*LoadedConf
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newLoadedConfig(result)
|
||||
return FromResult(result)
|
||||
}
|
||||
|
||||
func newLoadedConfig(result *configkit.Result[Document]) (*LoadedConfig, error) {
|
||||
// FromResult derives runtime pipeline settings without loading or serializing
|
||||
// the application result again.
|
||||
func FromResult(result *configkit.Result[Document]) (*LoadedConfig, error) {
|
||||
if result == nil {
|
||||
return nil, fmt.Errorf("configuration result is unavailable")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user