From 6a090ba571ba45b78f7e1b8b718acc2566c9b649 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Sat, 21 Nov 2026 19:46:21 +0000 Subject: [PATCH] Share application loading in composed example --- docs/roadmap/implementation.md | 11 +- .../examples/composed/fixtures/after.yml | 3 + .../examples/composed/fixtures/before.yml | 3 + .../ckprompt/examples/composed/main.go | 122 +++++++++++------- .../ckprompt/examples/composed/main_test.go | 75 ++++++++++- 5 files changed, 158 insertions(+), 56 deletions(-) diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 4764b60..86dd7a0 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -249,7 +249,16 @@ The composed example is completed in Stage 5. ## Stage 5: Demonstrate shared loading and the subtree adapter -Status: pending. +Status: complete. + +Evidence: The composed example loads application defaults, bundled and local +resources, campaign-derived settings, and storage in one ConfigKit result; +the engine consumes that result and the storage scope uses the subtree adapter. +Updated fixtures and tests exercise native paths, shared inspection and reuse, +storage show/explain/diff and overrides, whole-load failure evidence, and the +existing campaign, comparison, catalog, and lifecycle cases. With Go 1.27.4, +`GOTOOLCHAIN=local go test ./integrations/ckprompt/examples/composed` passed in +the aligned workspace. Update the maintained [composed example](../../integrations/ckprompt/examples/composed/main.go), its fixtures, and its existing tests. A separate consumer example is unnecessary. diff --git a/integrations/ckprompt/examples/composed/fixtures/after.yml b/integrations/ckprompt/examples/composed/fixtures/after.yml index e1c77fd..903b700 100644 --- a/integrations/ckprompt/examples/composed/fixtures/after.yml +++ b/integrations/ckprompt/examples/composed/fixtures/after.yml @@ -8,3 +8,6 @@ campaigns: prompt_id: briefing profile_id: autumn model: autumn-model +storage: + driver: local + location: ./after diff --git a/integrations/ckprompt/examples/composed/fixtures/before.yml b/integrations/ckprompt/examples/composed/fixtures/before.yml index 9fc0c68..ee3909e 100644 --- a/integrations/ckprompt/examples/composed/fixtures/before.yml +++ b/integrations/ckprompt/examples/composed/fixtures/before.yml @@ -8,3 +8,6 @@ campaigns: prompt_id: briefing profile_id: autumn model: autumn-model +storage: + driver: local + location: ./before diff --git a/integrations/ckprompt/examples/composed/main.go b/integrations/ckprompt/examples/composed/main.go index 3a7692c..24c8aed 100644 --- a/integrations/ckprompt/examples/composed/main.go +++ b/integrations/ckprompt/examples/composed/main.go @@ -22,9 +22,16 @@ type campaign struct { Model string `yaml:"model"` } +type storageConfiguration struct { + Driver string `yaml:"driver"` + Location string `yaml:"location"` +} + type appConfiguration struct { - DefaultCampaign string `yaml:"default_campaign"` - Campaigns map[string]campaign `yaml:"campaigns"` + PromptKit promptkit.Settings `yaml:"promptkit"` + Storage storageConfiguration `yaml:"storage"` + DefaultCampaign string `yaml:"default_campaign"` + Campaigns map[string]campaign `yaml:"campaigns"` } type inspectionInput struct { @@ -95,26 +102,75 @@ func loadApplicationConfig(ctx context.Context, selection ckconfig.Selection, in } } } - source := configkit.Mapping("composed application settings", map[string]any{ - "default_campaign": "spring", - "campaigns": map[string]any{ - "spring": map[string]any{"prompt_id": "briefing", "profile_id": "spring", "model": "spring-model"}, - "autumn": map[string]any{"prompt_id": "briefing", "profile_id": "autumn", "model": "autumn-model"}, + definition := configkit.Definition[appConfiguration]{ + Defaults: func() appConfiguration { + return appConfiguration{DefaultCampaign: "spring", Storage: storageConfiguration{Driver: "local", Location: "./data"}, Campaigns: map[string]campaign{ + "spring": {PromptID: "briefing", ProfileID: "spring", Model: "spring-model"}, + "autumn": {PromptID: "briefing", ProfileID: "autumn", Model: "autumn-model"}, + }} }, - }) + Resolve: func(value *appConfiguration, resolution *configkit.Resolution) error { + name, chosen, err := selectedCampaign(*value, commandkit.Supplied[string]{}) + if err != nil { + return nil + } + deps := []configkit.Path{configkit.Keys("default_campaign"), configkit.Keys("campaigns", name)} + profilePath := configkit.Keys("promptkit", "profiles") + profileFound := false + for _, profile := range value.PromptKit.Profiles { + if profile.ID == chosen.ProfileID { + profileFound = true + break + } + } + if !profileFound { + value.PromptKit.Profiles = append(value.PromptKit.Profiles, promptkit.ProfileSettings{ID: chosen.ProfileID, Backend: new("local"), Endpoint: new("https://example.invalid/v1"), Model: new(chosen.Model), ExtraParams: map[string]any{"campaign": name}}) + resolution.RecordNormalization(profilePath, "Selected campaign profile", deps...) + } + promptPath := configkit.Keys("promptkit", "prompts") + for index := range value.PromptKit.Prompts { + prompt := &value.PromptKit.Prompts[index] + if prompt.ID != "briefing" { + continue + } + if chosen.PromptID != prompt.ID { + prompt.ID = chosen.PromptID + resolution.RecordNormalization(promptPath.Index(index).Key("id"), "Selected campaign prompt", deps...) + } + if prompt.DefaultProfile == nil { + prompt.DefaultProfile = new(chosen.ProfileID) + resolution.RecordDerivedDefault(promptPath.Index(index).Key("default_profile"), "Selected campaign profile", deps...) + } + break + } + return nil + }, + } + definition = promptkit.Configure(definition, func(value *appConfiguration) *promptkit.Settings { return &value.PromptKit }) + resources := promptkit.SettingsInput(configkit.Mapping("example resources", map[string]any{ + "collections": map[string]any{"field-notes": map[string]any{ + "release_version": "1", + "backends": []any{map[string]any{"id": "demo", "endpoint": "https://example.invalid/v1"}}, + "profiles": []any{map[string]any{"id": "assistant", "backend": "field-notes/demo", "model": "example-model"}}, + "prompts": []any{map[string]any{"id": "briefing", "default_profile": "field-notes/assistant", "inputs": []any{map[string]any{"name": "topic", "required": true, "content_type": "text/plain", "description": "Topic to brief"}}, "messages": []any{map[string]any{"role": "user", "content": "Write a concise briefing about {{.topic}}."}}, "output": map[string]any{"format": "text", "validation_mode": "none"}}}, + }}, + "prompts": []any{map[string]any{"id": "briefing", "inputs": []any{map[string]any{"name": "topic", "required": true, "content_type": "text/plain", "description": "Topic to brief"}}, "messages": []any{map[string]any{"role": "user", "content": "Write a concise briefing about {{.topic}}. {{if .mode}}{{input \"notes\"}}{{end}}"}}, "output": map[string]any{"format": "text", "validation_mode": "none"}}}, + "backends": []any{map[string]any{"id": "local", "endpoint": "https://example.invalid/v1"}}, + })) + source := configkit.NoSource() if selection.Config.Set { source = configkit.File(selection.Config.Value) } - options := configkit.Options[appConfiguration]{} + options := configkit.LayeredOptions[appConfiguration]{Definition: definition, Underlays: []configkit.Underlay{promptkit.BundledOpenRouterUnderlay(), {Inputs: []configkit.Input{resources}}}} if selection.Overlay.Set { options.Overlay = selection.Overlay.Value - options.Filesystem = &configkit.FilesystemContext{Root: ".", BaseDir: "."} + options.OverlayFilesystem = &configkit.FilesystemContext{Root: ".", BaseDir: "."} } if campaignInput.Set { options.Overrides = []configkit.Override{{Path: configkit.Keys("default_campaign"), Value: campaignInput.Value, Flag: "--campaign"}} } options.Overrides = append(options.Overrides, selection.Overrides...) - result, err := configkit.Load(source, options) + result, err := configkit.LoadLayered(configkit.Input{Source: source}, options) if err == nil && result != nil && cache != nil { cache.applications = append(cache.applications, applicationCacheEntry{selection: selection, campaign: campaignInput, result: result}) } @@ -126,48 +182,14 @@ func loadPromptEngine(ctx context.Context, selection ckconfig.Selection, input * if err != nil { return nil, err } - name, chosen, err := selectedCampaign(application.Config, input.Campaign) - if err != nil { + if _, _, err := selectedCampaign(application.Config, commandkit.Supplied[string]{}); err != nil { return nil, err } - engineConfig := configkit.Mapping("campaign "+name, map[string]any{ - "collections": map[string]any{ - "field-notes": map[string]any{ - "release_version": "1", - "backends": []any{map[string]any{"id": "demo", "endpoint": "https://example.invalid/v1"}}, - "profiles": []any{map[string]any{"id": "assistant", "backend": "field-notes/demo", "model": "example-model"}}, - "prompts": []any{map[string]any{ - "id": "briefing", - "default_profile": "field-notes/assistant", - "inputs": []any{map[string]any{"name": "topic", "required": true, "content_type": "text/plain", "description": "Topic to brief"}}, - "messages": []any{map[string]any{"role": "user", "content": "Write a concise briefing about {{.topic}}."}}, - "output": map[string]any{"format": "text", "validation_mode": "none"}, - }}, - }, - }, - "backends": []any{map[string]any{"id": "local", "endpoint": "https://example.invalid/v1"}}, - "profiles": []any{map[string]any{ - "id": chosen.ProfileID, - "backend": "local", - "endpoint": "https://example.invalid/v1", - "model": chosen.Model, - "extra_params": map[string]any{ - "campaign": name, - }, - }}, - "prompts": []any{map[string]any{ - "id": chosen.PromptID, - "default_profile": chosen.ProfileID, - "inputs": []any{map[string]any{"name": "topic", "required": true, "content_type": "text/plain", "description": "Topic to brief"}}, - "messages": []any{map[string]any{"role": "user", "content": "Write a concise briefing about {{.topic}}. {{if .mode}}{{input \"notes\"}}{{end}}"}}, - "output": map[string]any{"format": "text", "validation_mode": "none"}, - }}, - }) var client promptkit.LLMClient = &exampleLLMClient{} if cache != nil && cache.llmClient != nil { client = cache.llmClient } - return promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &configkit.Input{Source: engineConfig}}}, promptkit.WithLLMClient(client)) + return promptkit.NewFromConfig(application, promptkit.RuntimeConfig{}, promptkit.WithLLMClient(client)) } func selectedCampaign(configuration appConfiguration, supplied commandkit.Supplied[string]) (string, campaign, error) { @@ -231,6 +253,8 @@ func newAppWithEngineLoader( appScope := ckconfig.FromConfigKit("app", "Application settings.", loadApplicationConfig) appScope.Features.Overrides = true + storageScope := ckconfig.FromConfigKitSubtree("storage", "Storage settings.", configkit.Keys("storage"), loadApplicationConfig) + storageScope.Features.Overrides = true engineScope := ckprompt.ConfigScope(engineLoader) configCommand := ckconfig.Command(ckconfig.Suite[*inspectionInput, *runCache]{ NewInput: inputFactory, @@ -242,10 +266,10 @@ func newAppWithEngineLoader( return nil }, Selectors: []ckconfig.Selector[*inspectionInput]{ - {Binding: campaignSelector(), AgainstName: "against-campaign", Scopes: []string{"app", "promptkit"}}, + {Binding: campaignSelector(), AgainstName: "against-campaign", Scopes: []string{"app", "storage", "promptkit"}}, {Binding: limitFlag, AgainstName: "against-limit", Scopes: []string{"app"}}, }, - Scopes: []ckconfig.Scope[*inspectionInput, *runCache]{appScope, engineScope}, + Scopes: []ckconfig.Scope[*inspectionInput, *runCache]{appScope, storageScope, engineScope}, }) definitions := ckprompt.FromEngine(engineLoader) diff --git a/integrations/ckprompt/examples/composed/main_test.go b/integrations/ckprompt/examples/composed/main_test.go index 0275041..dabad2c 100644 --- a/integrations/ckprompt/examples/composed/main_test.go +++ b/integrations/ckprompt/examples/composed/main_test.go @@ -294,7 +294,7 @@ func TestComposedPromptKitAndDomainCommandsRemainAvailable(t *testing.T) { {"config", "validate", "--scope", "all", "--campaign", "spring", "--limit=0"}, {"config", "show", "--scope", "promptkit", "--campaign", "spring"}, {"config", "sources", "--scope", "promptkit", "--campaign", "spring"}, - {"config", "explain", "--scope", "promptkit", "--path", fmt.Sprintf("/profiles/%d/model", index), "--campaign", "spring"}, + {"config", "explain", "--scope", "promptkit", "--path", fmt.Sprintf("/promptkit/profiles/%d/model", index), "--campaign", "spring"}, {"promptkit", "show", "prompt", "briefing", "--campaign", "spring", "--format", "yaml"}, {"promptkit", "show", "profile", "spring", "--campaign", "spring"}, {"session", "restore", "session-42", "--campaign", "spring"}, @@ -461,17 +461,19 @@ func TestPromptKitScopeDiffAndCatalogsUseSelectedCampaign(t *testing.T) { } var diff struct { Changes struct { - Profiles []struct { - ID string `yaml:"id"` - Model string `yaml:"model"` - } `yaml:"profiles"` + PromptKit struct { + Profiles []struct { + ID string `yaml:"id"` + Model string `yaml:"model"` + } `yaml:"profiles"` + } `yaml:"promptkit"` } `yaml:"changes"` } if err := yaml.Unmarshal(output.Bytes(), &diff); err != nil { t.Fatal(err) } foundChange := false - for _, profile := range diff.Changes.Profiles { + for _, profile := range diff.Changes.PromptKit.Profiles { if profile.ID == "spring" && profile.Model == "spring-model-after" { foundChange = true } @@ -740,3 +742,64 @@ func composedField(mapping *yaml.Node, name string) *yaml.Node { func ckconfigSelection(path string) ckconfig.Selection { return ckconfig.Selection{Config: commandkit.Supplied[string]{Value: path, Set: true}} } + +func TestComposedStorageScopeAndSharedResult(t *testing.T) { + cache := &runCache{} + selection := ckconfigSelection("fixtures/before.yml") + input := &inspectionInput{Campaign: commandkit.Supplied[string]{Value: "spring", Set: true}} + result, err := loadApplicationConfig(context.Background(), selection, input, cache) + if err != nil { + t.Fatal(err) + } + engine, err := loadPromptEngine(context.Background(), selection, input, cache) + if err != nil { + t.Fatal(err) + } + if len(cache.applications) != 1 || cache.applications[0].result != result { + t.Fatal("engine did not use the selected application result") + } + inspection, err := engine.InspectConfiguration() + if err != nil { + t.Fatal(err) + } + appView, ok := result.InspectSubtree(configkit.Keys("promptkit")) + if !ok { + t.Fatal("missing application inspection") + } + var appYAML, promptYAML bytes.Buffer + if err := appView.WriteEffective(&appYAML); err != nil { + t.Fatal(err) + } + if err := inspection.WriteEffective(&promptYAML); err != nil { + t.Fatal(err) + } + if appYAML.String() != promptYAML.String() { + t.Fatal("engine inspection differs from shared application result") + } + app, err := newApp() + if err != nil { + t.Fatal(err) + } + for _, tc := range []struct { + args []string + want string + }{ + {[]string{"config", "show", "--scope=storage", "--config=fixtures/before.yml"}, "./before"}, + {[]string{"config", "explain", "--scope=storage", "--config=fixtures/before.yml", "--path=/storage/location"}, "/storage/location"}, + {[]string{"config", "diff", "--scope=storage", "--config=fixtures/before.yml", "--against-config=fixtures/after.yml"}, "./after"}, + {[]string{"config", "show", "--scope=storage", `--set=/storage/location="custom"`, "--campaign=spring"}, "custom"}, + {[]string{"config", "show", "--scope=app"}, "promptkit:"}, + {[]string{"config", "validate", "--scope=all"}, "storage"}, + } { + var output, diagnostics bytes.Buffer + outcome := app.Execute(context.Background(), tc.args, commandkit.Streams{Out: &output, Err: &diagnostics}) + if outcome.Kind != commandkit.OutcomeSuccess || !strings.Contains(output.String(), tc.want) { + t.Errorf("%v: %#v output=%q diagnostics=%q", tc.args, outcome, output.String(), diagnostics.String()) + } + } + var diagnostics bytes.Buffer + outcome := app.Execute(context.Background(), []string{"config", "show", "--scope=storage", "--config=fixtures/missing.yml"}, commandkit.Streams{Out: io.Discard, Err: &diagnostics}) + if outcome.Kind != commandkit.OutcomeOperational || !strings.Contains(diagnostics.String(), "missing.yml") { + t.Fatalf("failed load: %#v %q", outcome, diagnostics.String()) + } +}