Share runtime glossaries with inspection commands
This commit is contained in:
@@ -181,6 +181,8 @@ Acceptance:
|
|||||||
|
|
||||||
## Stage 4: Wire native inspection, comparison and prompt commands
|
## Stage 4: Wire native inspection, comparison and prompt commands
|
||||||
|
|
||||||
|
Status: Complete.
|
||||||
|
|
||||||
Work:
|
Work:
|
||||||
|
|
||||||
1. Replace the currently empty `struct{}` acquisition input in
|
1. Replace the currently empty `struct{}` acquisition input in
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ type invocationState struct {
|
|||||||
}
|
}
|
||||||
type engineEntry struct {
|
type engineEntry struct {
|
||||||
selection ckconfig.Selection
|
selection ckconfig.Selection
|
||||||
|
input acquisitionInput
|
||||||
engine *pk.Engine
|
engine *pk.Engine
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,15 +49,15 @@ func acquireApplication(ctx context.Context, selection ckconfig.Selection, input
|
|||||||
return loaded, reportBootstrap(ctx, report, err)
|
return loaded, reportBootstrap(ctx, report, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadApplication(ctx context.Context, selection ckconfig.Selection, _ struct{}, _ *invocationState) (*ck.Result[appconfig.Config], error) {
|
func loadApplication(ctx context.Context, selection ckconfig.Selection, input acquisitionInput, _ *invocationState) (*ck.Result[appconfig.Config], error) {
|
||||||
loaded, err := acquireApplication(ctx, selection, acquisitionInput{})
|
loaded, err := acquireApplication(ctx, selection, input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return loaded.Result, nil
|
return loaded.Result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadPromptEngine(ctx context.Context, selection ckconfig.Selection, input struct{}, state *invocationState) (*pk.Engine, error) {
|
func loadPromptEngine(ctx context.Context, selection ckconfig.Selection, input acquisitionInput, state *invocationState) (*pk.Engine, error) {
|
||||||
loaded, err := loadApplication(ctx, selection, input, state)
|
loaded, err := loadApplication(ctx, selection, input, state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -68,10 +69,10 @@ func loadPromptEngine(ctx context.Context, selection ckconfig.Selection, input s
|
|||||||
return llm.NewEngine(ctx, loaded, client)
|
return llm.NewEngine(ctx, loaded, client)
|
||||||
}
|
}
|
||||||
|
|
||||||
func capturePromptEngine(ctx context.Context, selection ckconfig.Selection, input struct{}, state *invocationState) (*pk.Engine, error) {
|
func capturePromptEngine(ctx context.Context, selection ckconfig.Selection, input acquisitionInput, state *invocationState) (*pk.Engine, error) {
|
||||||
if state != nil {
|
if state != nil {
|
||||||
for _, entry := range state.engines {
|
for _, entry := range state.engines {
|
||||||
if reflect.DeepEqual(entry.selection, selection) {
|
if reflect.DeepEqual(entry.selection, selection) && reflect.DeepEqual(entry.input, input) {
|
||||||
return entry.engine, nil
|
return entry.engine, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -81,27 +82,38 @@ func capturePromptEngine(ctx context.Context, selection ckconfig.Selection, inpu
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if state != nil {
|
if state != nil {
|
||||||
state.engines = append(state.engines, engineEntry{selection: selection, engine: engine})
|
state.engines = append(state.engines, engineEntry{selection: selection, input: acquisitionInput{GlossaryFiles: append([]string(nil), input.GlossaryFiles...)}, engine: engine})
|
||||||
}
|
}
|
||||||
return engine, nil
|
return engine, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func shareComparisonGlossaries(scope ckconfig.Scope[acquisitionInput, *invocationState]) ckconfig.Scope[acquisitionInput, *invocationState] {
|
||||||
|
compare := scope.Compare
|
||||||
|
scope.Compare = func(ctx context.Context, request ckconfig.ComparisonRequest[acquisitionInput], state *invocationState) (commandkit.Acquired[[]ck.Difference], error) {
|
||||||
|
request.Right.Input.GlossaryFiles = append([]string(nil), request.Left.Input.GlossaryFiles...)
|
||||||
|
return compare(ctx, request, state)
|
||||||
|
}
|
||||||
|
return scope
|
||||||
|
}
|
||||||
|
|
||||||
func buildApplication(extra ...commandkit.Declaration) (*commandkit.App, error) {
|
func buildApplication(extra ...commandkit.Declaration) (*commandkit.App, error) {
|
||||||
return buildApplicationWithClient(nil, extra...)
|
return buildApplicationWithClient(nil, extra...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildApplicationWithClient(client *http.Client, extra ...commandkit.Declaration) (*commandkit.App, error) {
|
func buildApplicationWithClient(client *http.Client, extra ...commandkit.Declaration) (*commandkit.App, error) {
|
||||||
|
selector := commandkit.RepeatFlag(commandkit.FlagMeta{Name: "glossary-file", Usage: "Runtime glossary file (repeatable)."}, nil, func(s string) (string, error) { return s, nil }, func(in *acquisitionInput, value commandkit.Supplied[[]string]) { in.GlossaryFiles = value.Value })
|
||||||
appScope := ckconfig.FromConfigKit("app", "Audita application settings.", loadApplication)
|
appScope := ckconfig.FromConfigKit("app", "Audita application settings.", loadApplication)
|
||||||
appScope.Features.Overrides = true
|
appScope.Features.Overrides = true
|
||||||
appScope = withBootstrapContext(appScope)
|
appScope = shareComparisonGlossaries(withBootstrapContext(appScope))
|
||||||
promptScope := withBootstrapContext(ckprompt.ConfigScope(loadPromptEngine))
|
promptScope := shareComparisonGlossaries(withBootstrapContext(ckprompt.ConfigScope(loadPromptEngine)))
|
||||||
configCommand := ckconfig.Command(ckconfig.Suite[struct{}, *invocationState]{NewState: func() *invocationState { return &invocationState{client: client} }, Scopes: []ckconfig.Scope[struct{}, *invocationState]{appScope, promptScope}})
|
configCommand := ckconfig.Command(ckconfig.Suite[acquisitionInput, *invocationState]{NewState: func() *invocationState { return &invocationState{client: client} }, Selectors: []ckconfig.Selector[acquisitionInput]{{Binding: selector, Scopes: []string{"app", "promptkit"}}}, Scopes: []ckconfig.Scope[acquisitionInput, *invocationState]{appScope, promptScope}})
|
||||||
definitions := ckprompt.WithNamespaces(ckprompt.FromEngine(capturePromptEngine), pk.Namespaces{})
|
definitions := ckprompt.WithNamespaces(ckprompt.FromEngine(capturePromptEngine), pk.Namespaces{})
|
||||||
definitions.NewState = func() *invocationState { return &invocationState{client: client} }
|
definitions.NewState = func() *invocationState { return &invocationState{client: client} }
|
||||||
definitions = ckprompt.WithRequestValidation(definitions, capturePromptEngine, func(_ context.Context, seed ckprompt.ValidationRequest, _ struct{}, _ *invocationState) (pk.RunRequest, error) {
|
definitions.Selectors = []commandkit.Flag[acquisitionInput]{selector}
|
||||||
|
definitions = ckprompt.WithRequestValidation(definitions, capturePromptEngine, func(_ context.Context, seed ckprompt.ValidationRequest, _ acquisitionInput, _ *invocationState) (pk.RunRequest, error) {
|
||||||
return applicationPromptRequest(seed.Request, seed.Namespaces), nil
|
return applicationPromptRequest(seed.Request, seed.Namespaces), nil
|
||||||
})
|
})
|
||||||
definitions = ckprompt.WithRequestPreparation(definitions, capturePromptEngine, func(_ context.Context, seed ckprompt.PreparationRequest, _ struct{}, _ *invocationState) (pk.RunRequest, error) {
|
definitions = ckprompt.WithRequestPreparation(definitions, capturePromptEngine, func(_ context.Context, seed ckprompt.PreparationRequest, _ acquisitionInput, _ *invocationState) (pk.RunRequest, error) {
|
||||||
return applicationPromptRequest(seed.Request, seed.Namespaces), nil
|
return applicationPromptRequest(seed.Request, seed.Namespaces), nil
|
||||||
})
|
})
|
||||||
definitions = ckprompt.WithSmokeTesting(definitions, capturePromptEngine, nil)
|
definitions = ckprompt.WithSmokeTesting(definitions, capturePromptEngine, nil)
|
||||||
|
|||||||
@@ -98,15 +98,15 @@ func TestPromptConfigurationComparisonAcquiresEqualSelectionsIndependently(t *te
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
writeProfile("before")
|
writeProfile("before")
|
||||||
scope := ckprompt.ConfigScope(func(ctx context.Context, selection ckconfig.Selection, input struct{}, state *invocationState) (*pk.Engine, error) {
|
scope := ckprompt.ConfigScope(func(ctx context.Context, selection ckconfig.Selection, input acquisitionInput, state *invocationState) (*pk.Engine, error) {
|
||||||
engine, err := loadPromptEngine(ctx, selection, input, state)
|
engine, err := loadPromptEngine(ctx, selection, input, state)
|
||||||
writeProfile("after")
|
writeProfile("after")
|
||||||
return engine, err
|
return engine, err
|
||||||
})
|
})
|
||||||
side := ckconfig.ComparisonSide[struct{}]{Selection: ckconfig.Selection{
|
side := ckconfig.ComparisonSide[acquisitionInput]{Selection: ckconfig.Selection{
|
||||||
Config: commandkit.Supplied[string]{Value: main, Set: true},
|
Config: commandkit.Supplied[string]{Value: main, Set: true},
|
||||||
}}
|
}}
|
||||||
result, err := scope.Compare(context.Background(), ckconfig.ComparisonRequest[struct{}]{Left: side, Right: side}, &invocationState{})
|
result, err := scope.Compare(context.Background(), ckconfig.ComparisonRequest[acquisitionInput]{Left: side, Right: side}, &invocationState{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -118,6 +118,32 @@ func TestPromptConfigurationComparisonAcquiresEqualSelectionsIndependently(t *te
|
|||||||
t.Fatal("comparison did not capture the model before and after the resource changed")
|
t.Fatal("comparison did not capture the model before and after the resource changed")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPromptEngineCacheIncludesRuntimeGlossaryInput(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
main := filepath.Join(dir, "main.yml")
|
||||||
|
first := filepath.Join(dir, "first.yml")
|
||||||
|
second := filepath.Join(dir, "second.yml")
|
||||||
|
for path, contents := range map[string]string{main: "version: 3\n", first: "one: {entries: []}\n", second: "two: {entries: []}\n"} {
|
||||||
|
if err := os.WriteFile(path, []byte(contents), 0600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
selection := ckconfig.Selection{Config: commandkit.Supplied[string]{Value: main, Set: true}}
|
||||||
|
state := &invocationState{}
|
||||||
|
left, err := capturePromptEngine(context.Background(), selection, acquisitionInput{GlossaryFiles: []string{first}}, state)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
right, err := capturePromptEngine(context.Background(), selection, acquisitionInput{GlossaryFiles: []string{second}}, state)
|
||||||
|
if err != nil || left == right {
|
||||||
|
t.Fatalf("different inputs reused engine: %v", err)
|
||||||
|
}
|
||||||
|
matched, err := capturePromptEngine(context.Background(), selection, acquisitionInput{GlossaryFiles: []string{first}}, state)
|
||||||
|
if err != nil || matched != left {
|
||||||
|
t.Fatalf("matching input did not reuse engine: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRequestInspectionAndExplicitSmokeUseSameSources(t *testing.T) {
|
func TestRequestInspectionAndExplicitSmokeUseSameSources(t *testing.T) {
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
file := filepath.Join(dir, "config.yml")
|
file := filepath.Join(dir, "config.yml")
|
||||||
|
|||||||
@@ -88,3 +88,90 @@ func TestProcessRuntimeGlossaryFailures(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNativeCommandsUseRuntimeGlossary(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
for name, data := range map[string]string{
|
||||||
|
"left.yml": "version: 3\ncontext: {description: left}\nglossary_collections: {party: {entries: [{id: a, name: Left, category: C}]}}\n",
|
||||||
|
"right.yml": "version: 3\ncontext: {description: right}\nglossary_collections: {party: {entries: [{id: b, name: Right, category: C}]}}\n",
|
||||||
|
"input.yml": "party: {entries: [{id: c, name: Runtime, category: C}]}\n",
|
||||||
|
"bad.yml": "party: [\n",
|
||||||
|
} {
|
||||||
|
if err := os.WriteFile(filepath.Join(dir, name), []byte(data), 0600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
app, err := buildApplication()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
run := func(args ...string) (int, string, string) {
|
||||||
|
t.Helper()
|
||||||
|
var out, stderr bytes.Buffer
|
||||||
|
result := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &stderr})
|
||||||
|
return result.Status, out.String(), stderr.String()
|
||||||
|
}
|
||||||
|
main := filepath.Join(dir, "left.yml")
|
||||||
|
file := filepath.Join(dir, "input.yml")
|
||||||
|
code, out, stderr := run("config", "show", "--scope=app", "--config", main, "--glossary-file", file)
|
||||||
|
if code != 0 || !strings.Contains(out, "Runtime") || strings.Contains(out, "name: Left") {
|
||||||
|
t.Fatalf("show %d %s %s", code, out, stderr)
|
||||||
|
}
|
||||||
|
code, out, stderr = run("config", "explain", "--scope=app", "--config", main, "--glossary-file", file, "--path=/glossary_collections/party/entries")
|
||||||
|
if code != 0 || !strings.Contains(out, "--glossary-file") {
|
||||||
|
t.Fatalf("explain %d %s %s", code, out, stderr)
|
||||||
|
}
|
||||||
|
code, out, stderr = run("config", "diff", "--details", "--scope=app", "--config", main, "--against-config", filepath.Join(dir, "right.yml"), "--glossary-file", file)
|
||||||
|
if code != 0 || !strings.Contains(out, "left") || !strings.Contains(out, "right") || strings.Contains(out, "glossary_collections") {
|
||||||
|
t.Fatalf("diff %d %s %s", code, out, stderr)
|
||||||
|
}
|
||||||
|
for _, args := range [][]string{{"config", "show", "--scope=promptkit"}, {"promptkit", "list", "prompts"}} {
|
||||||
|
valid := append(append([]string(nil), args...), "--config", main, "--glossary-file", file)
|
||||||
|
code, _, stderr = run(valid...)
|
||||||
|
if code != 0 {
|
||||||
|
t.Fatalf("%v: %d %s", valid, code, stderr)
|
||||||
|
}
|
||||||
|
args = append(args, "--config", main, "--glossary-file", filepath.Join(dir, "bad.yml"))
|
||||||
|
code, _, stderr = run(args...)
|
||||||
|
if code == 0 || !strings.Contains(stderr, "bad.yml") {
|
||||||
|
t.Fatalf("%v: %d %s", args, code, stderr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, args := range [][]string{{"--help"}, {"version"}, {"config", "scopes"}, {"promptkit", "--help"}} {
|
||||||
|
code, _, stderr = run(args...)
|
||||||
|
if code != 0 {
|
||||||
|
t.Fatalf("%v: %d %s", args, code, stderr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRuntimeGlossaryUsesInvocationDirectory(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
project := filepath.Join(root, "project")
|
||||||
|
invocation := filepath.Join(root, "invocation")
|
||||||
|
for _, dir := range []string{project, invocation, filepath.Join(project, "overlays"), filepath.Join(project, "secrets")} {
|
||||||
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.Setenv("AUDITA_GLOSSARY_CONTEXT_TEST", "")
|
||||||
|
files := map[string]string{
|
||||||
|
filepath.Join(project, "config.yml"): "version: 3\nconfig: {secrets_dir: secrets, secrets_overwrite: true}\ncontext: {description: main}\n",
|
||||||
|
filepath.Join(project, "secrets", "AUDITA_GLOSSARY_CONTEXT_TEST"): "loaded",
|
||||||
|
filepath.Join(project, "glossary.yml"): "reference: {entries: [{id: r, name: Companion, category: C}]}\n",
|
||||||
|
filepath.Join(project, "overlays", "chosen.yml"): "context: {description: overlay}\n",
|
||||||
|
filepath.Join(invocation, "input.yml"): "config: {imports: [{path: imported.yml}]}\nparty: {entries: [{id: a, name: Incoming, category: C}]}\n",
|
||||||
|
filepath.Join(invocation, "imported.yml"): "lore: {entries: [{id: b, name: Imported, category: C}]}\n",
|
||||||
|
}
|
||||||
|
for path, data := range files {
|
||||||
|
if err := os.WriteFile(path, []byte(data), 0600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.Chdir(invocation)
|
||||||
|
var out, stderr bytes.Buffer
|
||||||
|
code := Run(context.Background(), []string{"config", "show", "--scope=app", "--config", filepath.Join(project, "config.yml"), "--overlay", "chosen", "--glossary-file", "input.yml"}, &out, &stderr)
|
||||||
|
if code != 0 || !strings.Contains(out.String(), "Incoming") || !strings.Contains(out.String(), "Imported") || !strings.Contains(out.String(), "Companion") || !strings.Contains(out.String(), "overlay") || os.Getenv("AUDITA_GLOSSARY_CONTEXT_TEST") != "loaded" {
|
||||||
|
t.Fatalf("status=%d out=%s err=%s", code, out.String(), stderr.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user