Load shared PromptKit settings with native secrets bootstrap

This commit is contained in:
2026-11-23 03:25:05 +00:00
parent 90a3790f44
commit 69ef533d7e
5 changed files with 170 additions and 73 deletions

View File

@@ -3,7 +3,6 @@ package config
import (
"bytes"
"errors"
"reflect"
"strings"
"testing"
@@ -16,21 +15,17 @@ func loadDocumentBytes(content string) (*configkit.Result[Document], error) {
func TestDocumentDefaultsAndExplicitValues(t *testing.T) {
result, err := loadDocumentBytes("{}\n")
if err != nil || !reflect.DeepEqual(result.Config, documentDefaults()) {
if err != nil || result.Config.Server != documentDefaults().Server {
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")
result, err = loadDocumentBytes("transient_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 {
t.Fatal(err)
}
got := result.Config
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 {
if 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)
if settings.SchemaDir != "" || settings.PromptKitConfiguration != "" || settings.ServerAddr != "" || settings.MaxRequestBytes != 0 || settings.TransientRetries == nil || *settings.TransientRetries != 0 {
t.Fatalf("settings changed resolved values: %+v", settings)
}
result, err = loadDocumentBytes("transient_retries: null\n")
if err != nil || result.Config.TransientRetries != nil {
t.Fatalf("nullable retries: %+v %v", result, err)
@@ -38,15 +33,15 @@ func TestDocumentDefaultsAndExplicitValues(t *testing.T) {
}
func TestDocumentNormalizesWithoutAcquiringFramework(t *testing.T) {
result, err := loadDocumentBytes("promptkit:\n configuration: ' ./missing/../framework.yml '\n schema_dir: ' ./a/../schemas '\nserver:\n addr: ' :9000 '\n artifact_root: ' ./a/../artifacts '\n")
result, err := loadDocumentBytes("server:\n addr: ' :9000 '\n artifact_root: ' ./a/../artifacts '\n")
if err != nil {
t.Fatal(err)
}
if result.Config.PromptKit.Configuration != "framework.yml" || result.Config.PromptKit.SchemaDir != "schemas" || result.Config.Server.Addr != ":9000" || result.Config.Server.ArtifactRoot != "artifacts" {
if result.Config.Server.Addr != ":9000" || result.Config.Server.ArtifactRoot != "artifacts" {
t.Fatalf("normalized: %+v", result.Config)
}
for _, key := range []string{"configuration", "schema_dir"} {
explanation, ok := result.Explain(configkit.Keys("promptkit", key))
for _, key := range []string{"addr", "artifact_root"} {
explanation, ok := result.Explain(configkit.Keys("server", key))
if !ok {
t.Fatal("missing path evidence", key)
}
@@ -62,14 +57,14 @@ func TestDocumentNormalizesWithoutAcquiringFramework(t *testing.T) {
if err := result.WriteEffective(&output); err != nil {
t.Fatal(err)
}
if !strings.Contains(output.String(), "configuration: framework.yml") || !strings.Contains(output.String(), "schema_dir: schemas") {
if !strings.Contains(output.String(), "artifact_root: artifacts") {
t.Fatal(output.String())
}
unchanged, err := loadDocumentBytes("promptkit:\n configuration: framework.yml\n")
unchanged, err := loadDocumentBytes("server:\n artifact_root: artifacts\n")
if err != nil {
t.Fatal(err)
}
explanation, _ := unchanged.Explain(configkit.Keys("promptkit", "configuration"))
explanation, _ := unchanged.Explain(configkit.Keys("server", "artifact_root"))
for _, change := range explanation.History {
if change.Origin.Operation == configkit.ChangeNormalization {
t.Fatal("unchanged path was normalized")
@@ -107,7 +102,6 @@ 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",
@@ -117,3 +111,38 @@ func TestDocumentRejectsRetiredAndInvalidShapes(t *testing.T) {
}
}
}
func TestNativeSettingsExtensionsAndRawKeyBoundary(t *testing.T) {
result, err := loadDocumentBytes("promptkit:\n custom: permitted\n backends:\n - id: local\n endpoint: http://localhost:1234\n extension: retained\n extra_params: {ordinary: visible}\n")
if err != nil || result.Config.PromptKit.Application["custom"] != "permitted" || result.Config.PromptKit.Backends[0].Application["extension"] != "retained" {
t.Fatalf("native extensions: %+v %v", result, err)
}
var effective bytes.Buffer
if err := result.WriteEffective(&effective); err != nil || !strings.Contains(effective.String(), "visible") {
t.Fatalf("ordinary extra params: %q %v", effective.String(), err)
}
for _, test := range []struct{ body, path string }{
{"promptkit: {configuration: null}", "/promptkit/configuration"},
{"promptkit: {schema_dir: null}", "/promptkit/schema_dir"},
{"promptkit: {backends: [{id: b, api_key: forbidden-secret}]}", "/promptkit/backends/0/api_key"},
{"promptkit: {profiles: [{id: p, api_key: forbidden-secret}]}", "/promptkit/profiles/0/api_key"},
{"promptkit: {collections: {team: {backends: [{id: b, api_key: forbidden-secret}]}}}", "/promptkit/collections/team/backends/0/api_key"},
{"promptkit: {collections: {team: {profiles: [{id: p, api_key: forbidden-secret}]}}}", "/promptkit/collections/team/profiles/0/api_key"},
} {
_, err := loadDocumentBytes(test.body)
var native *configkit.LoadError
if !errors.As(err, &native) {
t.Fatalf("%s: %v", test.path, err)
}
found := false
for _, diagnostic := range native.Diagnostics() {
found = found || diagnostic.Path.String() == test.path && diagnostic.Code == configkit.CodeValidateError
if strings.Contains(diagnostic.Message, "forbidden-secret") {
t.Fatalf("secret leaked: %+v", diagnostic)
}
}
if !found {
t.Fatalf("missing diagnostic %s: %+v", test.path, native.Diagnostics())
}
}
}