diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 6e41b5f..e2b3462 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -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 +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 `resources.go`. diff --git a/internal/configuration/capturetest/capture_test.go b/internal/configuration/capturetest/capture_test.go new file mode 100644 index 0000000..271cdc1 --- /dev/null +++ b/internal/configuration/capturetest/capture_test.go @@ -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) + } +} diff --git a/internal/configuration/load.go b/internal/configuration/load.go index a8d5e96..ba3c2e0 100644 --- a/internal/configuration/load.go +++ b/internal/configuration/load.go @@ -42,6 +42,8 @@ const ( type loaded struct { result *configkit.Result[document] + view configkit.Inspection + diagnostics []configkit.Diagnostic promptMetadata map[string][]promptInputPresence prompts map[string]*promptEntry registry *backend.Registry @@ -187,9 +189,6 @@ func configurationDefinition() configkit.Definition[document] { // load performs one ConfigKit lifecycle over the selected source tiers. 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]{ Underlays: underlays, Definition: configurationDefinition(), @@ -197,14 +196,40 @@ func load(main configkit.Input, underlays []configkit.Underlay) (*loaded, error) if err != nil { 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 - 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) } - if err := state.buildExecutionProfiles(); err != nil { + if err := state.buildExecutionProfiles(&config); err != nil { 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) for _, id := range sortedKeys(state.prompts) { 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), "prompt": promptNamespaces(state.promptIDs), } - for _, namespace := range sortedKeys(result.Config.PromptKit.Collections) { - collection := result.Config.PromptKit.Collections[namespace] + for _, namespace := range sortedKeys(config.Collections) { + collection := config.Collections[namespace] state.collections = append(state.collections, CollectionIdentity{ Namespace: namespace, ReleaseVersion: stringPointerValue(collection.ReleaseVersion), }) @@ -223,6 +248,29 @@ func load(main configkit.Input, underlays []configkit.Underlay) (*loaded, error) 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 { namespaces := make(map[string]struct{}) for _, id := range ids { diff --git a/internal/configuration/ownership.go b/internal/configuration/ownership.go new file mode 100644 index 0000000..8df54d7 --- /dev/null +++ b/internal/configuration/ownership.go @@ -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 © +} + +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 +} diff --git a/internal/configuration/prompts.go b/internal/configuration/prompts.go index 9c73921..4f98f9d 100644 --- a/internal/configuration/prompts.go +++ b/internal/configuration/prompts.go @@ -14,6 +14,7 @@ import ( "go.maximumdirect.net/promptkit/internal/jsonvalue" "go.maximumdirect.net/promptkit/internal/prompt" "go.maximumdirect.net/promptkit/internal/promptdef" + "go.maximumdirect.net/promptkit/internal/settings" ) var ( @@ -30,30 +31,25 @@ type promptEntry struct { 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) - var metadata *configkit.Metadata lookupOrigin := func(path configkit.Path) (configkit.FieldInfo, bool) { - if metadata == nil { - captured := state.result.Metadata() - metadata = &captured - } 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 { return err } - roots := make(map[string]*domain.ResourceBinding, len(state.result.Config.PromptKit.Collections)) - for _, name := range sortedKeys(state.result.Config.PromptKit.Collections) { - collection := state.result.Config.PromptKit.Collections[name] + roots := make(map[string]*domain.ResourceBinding, len(config.Collections)) + for _, name := range sortedKeys(config.Collections) { + collection := config.Collections[name] root, err := state.captureSchemaRoot(collection.SchemaRoot, configkit.Keys("promptkit", "collections", name, "schema_root"), lookupOrigin) if err != nil { return err } roots[name] = root } - for _, resources := range resourceSets(&state.result.Config.PromptKit) { + for _, resources := range resourceSets(config) { root := applicationRoot if selected := roots[resources.namespace]; resources.isCollection && selected != nil { 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) } sourceID := field.Origin.Locations[0].SourceID - context, found := state.result.SourceContext(sourceID) + context, found := state.view.SourceContext(sourceID) if !found || context.FS == nil { return nil, "", fmt.Errorf("%s: %s source %s has no filesystem context", fieldPath, label, sourceID) } diff --git a/internal/configuration/targets.go b/internal/configuration/targets.go index fc6910a..6d6fd8f 100644 --- a/internal/configuration/targets.go +++ b/internal/configuration/targets.go @@ -9,11 +9,12 @@ import ( "go.maximumdirect.net/promptkit/internal/domain" "go.maximumdirect.net/promptkit/internal/jsonvalue" profilepkg "go.maximumdirect.net/promptkit/internal/profile" + "go.maximumdirect.net/promptkit/internal/settings" ) -func (state *loaded) buildExecutionProfiles() error { - backendConfigs := indexedBackends(&state.result.Config.PromptKit) - profileConfigs := indexedProfiles(&state.result.Config.PromptKit) +func (state *loaded) buildExecutionProfiles(config *settings.Settings) error { + backendConfigs := indexedBackends(config) + profileConfigs := indexedProfiles(config) state.backendRecords = backendConfigs state.profileRecords = profileConfigs definitions := make(map[string]domain.Backend, len(backendConfigs))