Expose configuration metadata and findings
This commit is contained in:
@@ -267,6 +267,7 @@ func ConfigScope[I, S any](load func(context.Context, ckconfig.Selection, I, S)
|
||||
Name: "promptkit",
|
||||
Summary: "PromptKit engine configuration.",
|
||||
Operations: []ckconfig.Operation{ckconfig.Validate, ckconfig.Show, ckconfig.Sources, ckconfig.Explain, ckconfig.Diff},
|
||||
Features: ckconfig.ScopeFeatures{PathDiscovery: true, PathShow: true},
|
||||
}
|
||||
if load == nil {
|
||||
return ckconfig.WithScopeError(scope, errors.New("PromptKit configuration loader is nil"))
|
||||
@@ -275,15 +276,25 @@ func ConfigScope[I, S any](load func(context.Context, ckconfig.Selection, I, S)
|
||||
scope.Acquire = func(ctx context.Context, request ckconfig.Request, input I, state S) (commandkit.Acquired[ckconfig.View], error) {
|
||||
_, inspection, messages, err := loadPromptKitEngine(ctx, request.Selection, input, state, "", load)
|
||||
if err != nil {
|
||||
return commandkit.Acquired[ckconfig.View]{Diagnostics: messages.diagnostics, Explanation: messages.explanation}, err
|
||||
acquired := commandkit.Acquired[ckconfig.View]{Explanation: messages.explanation}
|
||||
var loadError *configkit.LoadError
|
||||
if errors.As(err, &loadError) && loadError != nil {
|
||||
acquired.Value.Sources = loadError
|
||||
acquired.Value.Findings = &ckconfig.Findings{Records: loadError.Diagnostics(), Messages: messages.diagnostics}
|
||||
} else {
|
||||
acquired.Diagnostics = messages.diagnostics
|
||||
}
|
||||
return acquired, err
|
||||
}
|
||||
metadata := inspection.Metadata()
|
||||
return commandkit.Acquired[ckconfig.View]{
|
||||
Value: ckconfig.View{
|
||||
Effective: inspection,
|
||||
Sources: inspection,
|
||||
Explanation: inspection,
|
||||
Metadata: &metadata,
|
||||
Findings: &ckconfig.Findings{Records: inspection.Diagnostics(), Messages: messages.diagnostics},
|
||||
},
|
||||
Diagnostics: messages.diagnostics,
|
||||
Explanation: messages.explanation,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -288,6 +288,9 @@ func TestConfigScopeUsesNativeInspectionFacetsAndInvocationValues(t *testing.T)
|
||||
if err != nil || loaderCalls != 1 {
|
||||
t.Fatalf("scope acquisition = (%#v, %v), calls=%d", acquired, err, loaderCalls)
|
||||
}
|
||||
if acquired.Value.Metadata == nil || acquired.Value.Findings == nil || !scope.Features.PathDiscovery || !scope.Features.PathShow {
|
||||
t.Fatalf("missing native metadata or findings: %#v", acquired.Value)
|
||||
}
|
||||
var effective bytes.Buffer
|
||||
if err := acquired.Value.Effective.WriteEffective(&effective); err != nil || !strings.Contains(effective.String(), "id: briefing") {
|
||||
t.Fatalf("native effective report = %q, %v", effective.String(), err)
|
||||
@@ -931,3 +934,25 @@ type failingDiagnosticWriter struct{ err error }
|
||||
func (writer failingDiagnosticWriter) Write([]byte) (int, error) { return 0, writer.err }
|
||||
|
||||
var _ io.Writer = failingDiagnosticWriter{}
|
||||
|
||||
func TestConfigScopeRetainsFailedLoadSnapshot(t *testing.T) {
|
||||
_, loadErr := configkit.Load[map[string]any](configkit.Bytes("broken", []byte("broken: [")))
|
||||
if loadErr == nil {
|
||||
t.Fatal("fixture unexpectedly loaded")
|
||||
}
|
||||
var snapshot *configkit.LoadError
|
||||
if !errors.As(loadErr, &snapshot) {
|
||||
t.Fatalf("unexpected load error: %v", loadErr)
|
||||
}
|
||||
wrapped := errors.Join(errors.New("loader context"), loadErr)
|
||||
scope := ConfigScope(func(context.Context, ckconfig.Selection, struct{}, struct{}) (*promptkit.Engine, error) {
|
||||
return nil, wrapped
|
||||
})
|
||||
got, err := scope.Acquire(context.Background(), ckconfig.Request{}, struct{}{}, struct{}{})
|
||||
if !errors.Is(err, loadErr) || got.Value.Sources == nil || got.Value.Findings == nil || got.Value.Effective != nil || got.Value.Metadata != nil || got.Value.Explanation != nil || len(got.Diagnostics) != 0 {
|
||||
t.Fatalf("failed acquisition = %#v, %v", got, err)
|
||||
}
|
||||
if !reflect.DeepEqual(got.Value.Sources.Sources(), snapshot.Sources()) || !reflect.DeepEqual(got.Value.Findings.Records, snapshot.Diagnostics()) {
|
||||
t.Fatalf("failed snapshot = %#v", got.Value)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user