Run native readiness diagnostics for selected targets
This commit is contained in:
84
internal/preflight/smoke.go
Normal file
84
internal/preflight/smoke.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package preflight
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
// SmokeObservation retains native evidence alongside an applicable check result.
|
||||
type SmokeObservation struct {
|
||||
Kind promptkit.SmokeTestKind
|
||||
ProfileID string
|
||||
BackendID string
|
||||
Bindings []BindingScope
|
||||
Report *promptkit.SmokeTestReport
|
||||
Err error
|
||||
Applicable bool
|
||||
}
|
||||
|
||||
// RunSmokeChecks executes each selected access and structured generation check
|
||||
// once, stopping at the first unsuccessful required observation.
|
||||
func RunSmokeChecks(ctx context.Context, engine *promptkit.Engine, targets Targets, execution *promptkit.ExecutionTargetOverride, retries *int) ([]SmokeObservation, error) {
|
||||
if ctx == nil {
|
||||
return nil, errors.New("preflight context must not be nil")
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var observations []SmokeObservation
|
||||
run := func(kind promptkit.SmokeTestKind, profile, backend string, bindings []BindingScope) error {
|
||||
report, err := engine.SmokeTest(ctx, promptkit.SmokeTestRequest{Kind: kind, ProfileID: profile, Execution: execution, TransientRetries: retries})
|
||||
observation := SmokeObservation{Kind: kind, ProfileID: profile, BackendID: backend, Bindings: bindings, Report: report}
|
||||
observation.Applicable, observation.Err = evaluateSmoke(kind, report, err)
|
||||
observations = append(observations, observation)
|
||||
return observation.Err
|
||||
}
|
||||
for _, target := range targets.Access {
|
||||
for _, kind := range []promptkit.SmokeTestKind{promptkit.SmokeTestReachability, promptkit.SmokeTestAuthentication} {
|
||||
if err := run(kind, target.ProfileID, target.BackendID, target.Bindings); err != nil {
|
||||
return observations, err
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, target := range targets.Profiles {
|
||||
if err := run(promptkit.SmokeTestStructuredOutput, target.ID, "", target.Bindings); err != nil {
|
||||
return observations, err
|
||||
}
|
||||
}
|
||||
return observations, nil
|
||||
}
|
||||
|
||||
func evaluateSmoke(kind promptkit.SmokeTestKind, report *promptkit.SmokeTestReport, err error) (bool, error) {
|
||||
if err != nil {
|
||||
return true, fmt.Errorf("%s diagnostic: %w", kind, err)
|
||||
}
|
||||
if report == nil {
|
||||
return true, fmt.Errorf("%s diagnostic returned no report", kind)
|
||||
}
|
||||
if report.Failure != "" {
|
||||
return true, fmt.Errorf("%s diagnostic failed (%s)", kind, report.Failure)
|
||||
}
|
||||
if !report.Supported || report.Applicable != nil && !*report.Applicable {
|
||||
return false, nil
|
||||
}
|
||||
if !report.Completed {
|
||||
return true, fmt.Errorf("%s diagnostic did not complete", kind)
|
||||
}
|
||||
positive := func(value *bool) bool { return value != nil && *value }
|
||||
passed := false
|
||||
switch kind {
|
||||
case promptkit.SmokeTestReachability:
|
||||
passed = positive(report.Facts.EndpointReachable)
|
||||
case promptkit.SmokeTestAuthentication:
|
||||
passed = positive(report.Facts.CredentialsAccepted)
|
||||
case promptkit.SmokeTestStructuredOutput:
|
||||
passed = positive(report.Facts.GenerationCompleted) && positive(report.Facts.ValidJSON) && positive(report.Facts.SchemaMatched)
|
||||
}
|
||||
if !passed {
|
||||
return true, fmt.Errorf("%s diagnostic did not establish a successful result", kind)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
Reference in New Issue
Block a user