Capture owned resources from resolved settings

This commit is contained in:
2026-11-21 14:20:46 +00:00
parent 34c0cf3f14
commit 4d5841e087
6 changed files with 324 additions and 23 deletions

View File

@@ -357,6 +357,10 @@ capturing another load's resource state or taking over application version polic
## Stage 6 — Capture Owned Resources From Final Settings And A Native View ## Stage 6 — Capture Owned Resources From Final Settings And A Native View
Status: complete. Focused capture tests passed under the race detector, including
mutation isolation and effective cross-filesystem source contexts. The former
generic inspection bridge is removed in the next stage.
Focus: `internal/configuration/load.go`, `prompts.go`, `targets.go`, and Focus: `internal/configuration/load.go`, `prompts.go`, `targets.go`, and
`resources.go`. `resources.go`.

View File

@@ -0,0 +1,107 @@
package capturetest
import (
"context"
"io/fs"
"testing"
"testing/fstest"
"go.maximumdirect.net/configkit"
"go.maximumdirect.net/promptkit"
"go.maximumdirect.net/promptkit/internal/configuration"
)
type application struct {
PromptKit promptkit.Settings `yaml:"promptkit"`
Other string `yaml:"other"`
}
func TestCaptureUsesEffectiveSourceContexts(t *testing.T) {
baseFiles := fstest.MapFS{
"catalog/base.yaml": {Data: []byte("promptkit:\n prompts:\n - id: base\n messages: [{role: user, content_file: original.txt}]\n output: {format: json, validation_mode: json_schema, schema_file: schema.json}\n")},
"catalog/original.txt": {Data: []byte("original")},
"catalog/schema.json": {Data: []byte(`{"type":"object"}`)},
}
overrideFiles := fstest.MapFS{
"operator/main.yaml": {Data: []byte("promptkit:\n prompts:\n - id: child\n extends: {id: base}\n messages: [{role: user, content_file: replacement.txt}]\n")},
"operator/replacement.txt": {Data: []byte("replacement")},
}
base := configkit.NewFilesystem("base", baseFiles)
override := configkit.NewFilesystem("operator", overrideFiles)
definition := promptkit.Configure(configkit.Definition[application]{}, func(value *application) *promptkit.Settings { return &value.PromptKit })
result, err := configkit.LoadLayered(configkit.Input{Source: override.File("operator/main.yaml"), Filesystem: &configkit.FilesystemContext{FS: &override, Root: "."}}, configkit.LayeredOptions[application]{
Underlays: []configkit.Underlay{{Inputs: []configkit.Input{{Source: base.File("catalog/base.yaml"), Filesystem: &configkit.FilesystemContext{FS: &base, Root: "."}}}}}, Definition: definition,
})
if err != nil {
t.Fatal(err)
}
view, ok := result.InspectSubtree(configkit.Keys("promptkit"))
if !ok {
t.Fatal("inspection unavailable")
}
state, err := configuration.Capture(&result.Config.PromptKit, view, nil)
if err != nil {
t.Fatal(err)
}
child, _, err := state.GetPromptSnapshot(context.Background(), "child")
if err != nil || child.Templates[0].Content != "replacement" || child.SchemaFile == nil || child.SchemaFile.Path != "catalog/schema.json" {
t.Fatalf("child = %+v, %v", child, err)
}
data, err := fs.ReadFile(child.SchemaFile.FS, child.SchemaFile.Path)
if err != nil || string(data) != `{"type":"object"}` {
t.Fatalf("schema = %q, %v", data, err)
}
}
func TestCaptureOwnsEffectiveResources(t *testing.T) {
definition := promptkit.Configure(configkit.Definition[application]{}, func(value *application) *promptkit.Settings { return &value.PromptKit })
result, err := configkit.Load(configkit.Bytes("application", []byte(`other: application
promptkit:
backends:
- id: local
endpoint: https://example.test/v1
extra_params: {nested: {value: original}}
profiles:
- id: profile
backend: local
model: original
prompts:
- id: prompt
inputs: [{name: document, required: false, description: text}]
messages: [{role: user, content: hello}]
output:
format: json
validation_mode: json_schema
schema: {type: object, properties: {answer: {type: string}}}
`)), configkit.Options[application]{Definition: definition})
if err != nil {
t.Fatal(err)
}
view, ok := result.InspectSubtree(configkit.Keys("promptkit"))
if !ok {
t.Fatal("inspection unavailable")
}
state, err := configuration.Capture(&result.Config.PromptKit, view, result.Diagnostics())
if err != nil {
t.Fatal(err)
}
settings := &result.Config.PromptKit
*settings.Backends[0].Endpoint = "https://changed.test/v1"
settings.Backends[0].ExtraParams["nested"].(map[string]any)["value"] = "changed"
*settings.Profiles[0].Model = "changed"
*settings.Prompts[0].Messages[0].Content = "changed"
settings.Prompts[0].Inputs[0].Required = true
settings.Prompts[0].Output.Schema.(map[string]any)["type"] = "changed"
backend, err := state.InspectBackend(context.Background(), "local")
if err != nil || backend.Endpoint != "https://example.test/v1" || backend.ExtraParams["nested"].(map[string]any)["value"] != "original" {
t.Fatalf("backend = %+v, %v", backend, err)
}
profile, err := state.GetProfile(context.Background(), "profile")
if err != nil || profile.ResolvedTarget.Model != "original" {
t.Fatalf("profile = %+v, %v", profile, err)
}
prompt, err := state.InspectPrompt(context.Background(), "prompt")
if err != nil || prompt.Messages[0].Content != "hello" || prompt.Output.Schema.(map[string]any)["type"] != "object" || prompt.Inputs == nil || (*prompt.Inputs)[0].Required == nil || *(*prompt.Inputs)[0].Required {
t.Fatalf("prompt = %+v, %v", prompt, err)
}
}

View File

@@ -42,6 +42,8 @@ const (
type loaded struct { type loaded struct {
result *configkit.Result[document] result *configkit.Result[document]
view configkit.Inspection
diagnostics []configkit.Diagnostic
promptMetadata map[string][]promptInputPresence promptMetadata map[string][]promptInputPresence
prompts map[string]*promptEntry prompts map[string]*promptEntry
registry *backend.Registry registry *backend.Registry
@@ -187,9 +189,6 @@ func configurationDefinition() configkit.Definition[document] {
// load performs one ConfigKit lifecycle over the selected source tiers. // load performs one ConfigKit lifecycle over the selected source tiers.
func load(main configkit.Input, underlays []configkit.Underlay) (*loaded, error) { func load(main configkit.Input, underlays []configkit.Underlay) (*loaded, error) {
state := &loaded{
promptMetadata: make(map[string][]promptInputPresence),
}
result, err := configkit.LoadLayered(main, configkit.LayeredOptions[document]{ result, err := configkit.LoadLayered(main, configkit.LayeredOptions[document]{
Underlays: underlays, Underlays: underlays,
Definition: configurationDefinition(), Definition: configurationDefinition(),
@@ -197,14 +196,40 @@ func load(main configkit.Input, underlays []configkit.Underlay) (*loaded, error)
if err != nil { if err != nil {
return nil, fmt.Errorf("load configuration: %w", err) return nil, fmt.Errorf("load configuration: %w", err)
} }
view, ok := result.InspectSubtree(configkit.Keys("promptkit"))
if !ok {
return nil, fmt.Errorf("load configuration: inspection unavailable")
}
state, err := Capture(&result.Config.PromptKit, view, result.Diagnostics())
if err != nil {
return nil, fmt.Errorf("load configuration: %w", err)
}
state.result = result state.result = result
if err := state.materializePrompts(); err != nil { return state, nil
}
// Capture builds owned runtime resources from effective settings and native evidence.
func Capture(input *settings.Settings, view configkit.Inspection, diagnostics []configkit.Diagnostic) (*loaded, error) {
config := ownSettings(input)
state := &loaded{view: view, diagnostics: ownDiagnostics(diagnostics), promptMetadata: make(map[string][]promptInputPresence)}
metadata := view.Metadata()
for _, resources := range resourceSets(&config) {
for index, entry := range resources.prompts {
presence := make([]promptInputPresence, len(entry.Inputs))
for i := range entry.Inputs {
path := resources.bases.prompts.Index(index).Key("inputs").Index(i)
presence[i] = promptInputPresence{required: finalFieldState(metadata, path.Key("required")), contentType: finalFieldState(metadata, path.Key("content_type")), description: finalFieldState(metadata, path.Key("description"))}
}
state.promptMetadata[indexedResourceID(resources.namespace, entry.ID)] = presence
}
}
if err := state.materializePrompts(&config, metadata); err != nil {
return nil, fmt.Errorf("load configuration: %w", err) return nil, fmt.Errorf("load configuration: %w", err)
} }
if err := state.buildExecutionProfiles(); err != nil { if err := state.buildExecutionProfiles(&config); err != nil {
return nil, fmt.Errorf("load configuration: %w", err) return nil, fmt.Errorf("load configuration: %w", err)
} }
state.backendIDs = sortedKeys(indexedBackends(&state.result.Config.PromptKit)) state.backendIDs = sortedKeys(indexedBackends(&config))
state.profileIDs = sortedKeys(state.indexedProfiles) state.profileIDs = sortedKeys(state.indexedProfiles)
for _, id := range sortedKeys(state.prompts) { for _, id := range sortedKeys(state.prompts) {
state.promptIDs = append(state.promptIDs, domain.PromptIdentity{ID: id}) state.promptIDs = append(state.promptIDs, domain.PromptIdentity{ID: id})
@@ -214,8 +239,8 @@ func load(main configkit.Input, underlays []configkit.Underlay) (*loaded, error)
"profile": resourceNamespaces(state.profileIDs), "profile": resourceNamespaces(state.profileIDs),
"prompt": promptNamespaces(state.promptIDs), "prompt": promptNamespaces(state.promptIDs),
} }
for _, namespace := range sortedKeys(result.Config.PromptKit.Collections) { for _, namespace := range sortedKeys(config.Collections) {
collection := result.Config.PromptKit.Collections[namespace] collection := config.Collections[namespace]
state.collections = append(state.collections, CollectionIdentity{ state.collections = append(state.collections, CollectionIdentity{
Namespace: namespace, ReleaseVersion: stringPointerValue(collection.ReleaseVersion), Namespace: namespace, ReleaseVersion: stringPointerValue(collection.ReleaseVersion),
}) })
@@ -223,6 +248,29 @@ func load(main configkit.Input, underlays []configkit.Underlay) (*loaded, error)
return state, nil return state, nil
} }
func finalFieldState(metadata configkit.Metadata, path configkit.Path) fieldState {
info, exists := metadata.Lookup(path)
if !exists || !info.Explicit {
return fieldAbsent
}
if info.Null {
return fieldNull
}
return fieldValue
}
func ownDiagnostics(values []configkit.Diagnostic) []configkit.Diagnostic {
if values == nil {
return nil
}
result := append([]configkit.Diagnostic(nil), values...)
for i := range result {
result[i].Primary = copyPointer(result[i].Primary)
result[i].Related = append([]configkit.Location(nil), result[i].Related...)
}
return result
}
func resourceNamespaces(ids []string) []string { func resourceNamespaces(ids []string) []string {
namespaces := make(map[string]struct{}) namespaces := make(map[string]struct{})
for _, id := range ids { for _, id := range ids {

View File

@@ -0,0 +1,145 @@
package configuration
import (
"go.maximumdirect.net/promptkit/internal/jsonvalue"
"go.maximumdirect.net/promptkit/internal/settings"
)
func copyPointer[T any](value *T) *T {
if value == nil {
return nil
}
copy := *value
return &copy
}
func copyProviderMap(value map[string]any) map[string]any {
if value == nil {
return nil
}
return jsonvalue.CloneTrusted(value).(map[string]any)
}
func ownBackendSettings(in settings.BackendSettings) settings.BackendSettings {
in.Endpoint = copyPointer(in.Endpoint)
in.APIKeyEnv = copyPointer(in.APIKeyEnv)
in.ConcurrencyLimit = copyPointer(in.ConcurrencyLimit)
in.QueueCapacity = copyPointer(in.QueueCapacity)
in.ExtraParams = copyProviderMap(in.ExtraParams)
in.Application = nil
if in.SmokeTests != nil {
smoke := *in.SmokeTests
smoke.DefaultProfile = copyPointer(smoke.DefaultProfile)
if smoke.Reachability != nil {
reach := *smoke.Reachability
reach.Endpoint = copyPointer(reach.Endpoint)
smoke.Reachability = &reach
}
if smoke.Authentication != nil {
auth := *smoke.Authentication
auth.Kind = copyPointer(auth.Kind)
auth.Endpoint = copyPointer(auth.Endpoint)
smoke.Authentication = &auth
}
in.SmokeTests = &smoke
}
return in
}
func ownProfileSettings(in settings.ProfileSettings) settings.ProfileSettings {
in.Backend = copyPointer(in.Backend)
in.Endpoint = copyPointer(in.Endpoint)
in.Model = copyPointer(in.Model)
in.Temperature = copyPointer(in.Temperature)
in.TopP = copyPointer(in.TopP)
in.MaxTokens = copyPointer(in.MaxTokens)
in.TimeoutSeconds = copyPointer(in.TimeoutSeconds)
in.ServiceTier = copyPointer(in.ServiceTier)
in.ReasoningEffort = copyPointer(in.ReasoningEffort)
in.APIKeyEnv = copyPointer(in.APIKeyEnv)
in.APIKeyRequired = copyPointer(in.APIKeyRequired)
in.ExtraParams = copyProviderMap(in.ExtraParams)
in.Application = nil
return in
}
func ownPromptSettings(in settings.PromptSettings) settings.PromptSettings {
in.DefaultProfile = copyPointer(in.DefaultProfile)
in.Description = copyPointer(in.Description)
in.SessionID = copyPointer(in.SessionID)
in.Application = nil
if in.Inputs != nil {
in.Inputs = append(make([]settings.PromptInputSettings, 0, len(in.Inputs)), in.Inputs...)
for i := range in.Inputs {
in.Inputs[i].ContentType = copyPointer(in.Inputs[i].ContentType)
in.Inputs[i].Description = copyPointer(in.Inputs[i].Description)
}
}
if in.Messages != nil {
in.Messages = append(make([]settings.PromptMessageSettings, 0, len(in.Messages)), in.Messages...)
for i := range in.Messages {
message := &in.Messages[i]
message.Role = copyPointer(message.Role)
message.Content = copyPointer(message.Content)
message.ContentFile = copyPointer(message.ContentFile)
if message.CacheControl != nil {
cache := *message.CacheControl
cache.Type = copyPointer(cache.Type)
cache.TTL = copyPointer(cache.TTL)
message.CacheControl = &cache
}
}
}
if in.Output != nil {
output := *in.Output
output.Format = copyPointer(output.Format)
output.ValidationMode = copyPointer(output.ValidationMode)
output.Schema = jsonvalue.CloneTrusted(output.Schema)
output.SchemaFile = copyPointer(output.SchemaFile)
output.SchemaPath = copyPointer(output.SchemaPath)
output.RepairAttempts = copyPointer(output.RepairAttempts)
in.Output = &output
}
return in
}
func ownSettings(in *settings.Settings) settings.Settings {
if in == nil {
return settings.Settings{}
}
out := settings.Settings{SchemaRoot: copyPointer(in.SchemaRoot)}
copyResources := func(backends []settings.BackendSettings, profiles []settings.ProfileSettings, prompts []settings.PromptSettings) ([]settings.BackendSettings, []settings.ProfileSettings, []settings.PromptSettings) {
var b []settings.BackendSettings
if backends != nil {
b = make([]settings.BackendSettings, len(backends))
for i := range backends {
b[i] = ownBackendSettings(backends[i])
}
}
var p []settings.ProfileSettings
if profiles != nil {
p = make([]settings.ProfileSettings, len(profiles))
for i := range profiles {
p[i] = ownProfileSettings(profiles[i])
}
}
var q []settings.PromptSettings
if prompts != nil {
q = make([]settings.PromptSettings, len(prompts))
for i := range prompts {
q[i] = ownPromptSettings(prompts[i])
}
}
return b, p, q
}
out.Backends, out.Profiles, out.Prompts = copyResources(in.Backends, in.Profiles, in.Prompts)
if in.Collections != nil {
out.Collections = make(map[string]settings.CollectionSettings, len(in.Collections))
for name, collection := range in.Collections {
copy := settings.CollectionSettings{ReleaseVersion: copyPointer(collection.ReleaseVersion), SchemaRoot: copyPointer(collection.SchemaRoot)}
copy.Backends, copy.Profiles, copy.Prompts = copyResources(collection.Backends, collection.Profiles, collection.Prompts)
out.Collections[name] = copy
}
}
return out
}

View File

@@ -14,6 +14,7 @@ import (
"go.maximumdirect.net/promptkit/internal/jsonvalue" "go.maximumdirect.net/promptkit/internal/jsonvalue"
"go.maximumdirect.net/promptkit/internal/prompt" "go.maximumdirect.net/promptkit/internal/prompt"
"go.maximumdirect.net/promptkit/internal/promptdef" "go.maximumdirect.net/promptkit/internal/promptdef"
"go.maximumdirect.net/promptkit/internal/settings"
) )
var ( var (
@@ -30,30 +31,25 @@ type promptEntry struct {
messages []*template.Template messages []*template.Template
} }
func (state *loaded) materializePrompts() error { func (state *loaded) materializePrompts(config *settings.Settings, metadata configkit.Metadata) error {
state.prompts = make(map[string]*promptEntry) state.prompts = make(map[string]*promptEntry)
var metadata *configkit.Metadata
lookupOrigin := func(path configkit.Path) (configkit.FieldInfo, bool) { lookupOrigin := func(path configkit.Path) (configkit.FieldInfo, bool) {
if metadata == nil {
captured := state.result.Metadata()
metadata = &captured
}
return metadata.Lookup(path) return metadata.Lookup(path)
} }
applicationRoot, err := state.captureSchemaRoot(state.result.Config.PromptKit.SchemaRoot, configkit.Keys("promptkit", "schema_root"), lookupOrigin) applicationRoot, err := state.captureSchemaRoot(config.SchemaRoot, configkit.Keys("promptkit", "schema_root"), lookupOrigin)
if err != nil { if err != nil {
return err return err
} }
roots := make(map[string]*domain.ResourceBinding, len(state.result.Config.PromptKit.Collections)) roots := make(map[string]*domain.ResourceBinding, len(config.Collections))
for _, name := range sortedKeys(state.result.Config.PromptKit.Collections) { for _, name := range sortedKeys(config.Collections) {
collection := state.result.Config.PromptKit.Collections[name] collection := config.Collections[name]
root, err := state.captureSchemaRoot(collection.SchemaRoot, configkit.Keys("promptkit", "collections", name, "schema_root"), lookupOrigin) root, err := state.captureSchemaRoot(collection.SchemaRoot, configkit.Keys("promptkit", "collections", name, "schema_root"), lookupOrigin)
if err != nil { if err != nil {
return err return err
} }
roots[name] = root roots[name] = root
} }
for _, resources := range resourceSets(&state.result.Config.PromptKit) { for _, resources := range resourceSets(config) {
root := applicationRoot root := applicationRoot
if selected := roots[resources.namespace]; resources.isCollection && selected != nil { if selected := roots[resources.namespace]; resources.isCollection && selected != nil {
root = selected root = selected
@@ -284,7 +280,7 @@ func (state *loaded) bindResource(fieldPath configkit.Path, name, label string,
return nil, "", fmt.Errorf("%s: %s needs one contributing source with a filesystem context", fieldPath, label) return nil, "", fmt.Errorf("%s: %s needs one contributing source with a filesystem context", fieldPath, label)
} }
sourceID := field.Origin.Locations[0].SourceID sourceID := field.Origin.Locations[0].SourceID
context, found := state.result.SourceContext(sourceID) context, found := state.view.SourceContext(sourceID)
if !found || context.FS == nil { if !found || context.FS == nil {
return nil, "", fmt.Errorf("%s: %s source %s has no filesystem context", fieldPath, label, sourceID) return nil, "", fmt.Errorf("%s: %s source %s has no filesystem context", fieldPath, label, sourceID)
} }

View File

@@ -9,11 +9,12 @@ import (
"go.maximumdirect.net/promptkit/internal/domain" "go.maximumdirect.net/promptkit/internal/domain"
"go.maximumdirect.net/promptkit/internal/jsonvalue" "go.maximumdirect.net/promptkit/internal/jsonvalue"
profilepkg "go.maximumdirect.net/promptkit/internal/profile" profilepkg "go.maximumdirect.net/promptkit/internal/profile"
"go.maximumdirect.net/promptkit/internal/settings"
) )
func (state *loaded) buildExecutionProfiles() error { func (state *loaded) buildExecutionProfiles(config *settings.Settings) error {
backendConfigs := indexedBackends(&state.result.Config.PromptKit) backendConfigs := indexedBackends(config)
profileConfigs := indexedProfiles(&state.result.Config.PromptKit) profileConfigs := indexedProfiles(config)
state.backendRecords = backendConfigs state.backendRecords = backendConfigs
state.profileRecords = profileConfigs state.profileRecords = profileConfigs
definitions := make(map[string]domain.Backend, len(backendConfigs)) definitions := make(map[string]domain.Backend, len(backendConfigs))