Run native readiness diagnostics for selected targets

This commit is contained in:
2026-11-28 04:13:18 +00:00
parent 2ca73aa3ee
commit 62929fc766
3 changed files with 186 additions and 0 deletions

View File

@@ -215,6 +215,8 @@ metadata edits are mechanical, and smoke execution belongs to Stage 4.
## Stage 4: Orchestrate Native Smoke Checks And Interpret Observations ## Stage 4: Orchestrate Native Smoke Checks And Interpret Observations
**Status:** Complete.
**Goal:** Convert applicable native diagnostics into a correct readiness decision. **Goal:** Convert applicable native diagnostics into a correct readiness decision.
**Inspect:** Native `SmokeTestRequest`, `SmokeTestReport`, facts and reason GoDoc, **Inspect:** Native `SmokeTestRequest`, `SmokeTestReport`, facts and reason GoDoc,

View 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
}

View File

@@ -0,0 +1,100 @@
package preflight
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"go.maximumdirect.net/configkit"
"go.maximumdirect.net/promptkit"
)
type cancelingClient struct{ cancel context.CancelFunc }
func (client cancelingClient) Generate(ctx context.Context, _ promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
client.cancel()
<-ctx.Done()
return nil, ctx.Err()
}
func TestSmokeCancellationRetainsPartialEvidence(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
input := configkit.Input{Destination: configkit.Keys("promptkit"), Source: configkit.Mapping("smoke", map[string]any{
"profiles": []any{map[string]any{"id": "profile", "endpoint": "http://example.invalid/v1", "model": "fixture"}},
})}
engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &input}}, promptkit.WithBundledOpenRouter(false), promptkit.WithLLMClient(cancelingClient{cancel}))
if err != nil {
t.Fatal(err)
}
observations, err := RunSmokeChecks(ctx, engine, Targets{Access: []AccessTarget{{ProfileID: "profile"}}, Profiles: []ProfileTarget{{ID: "profile"}}}, nil, nil)
if !errors.Is(err, context.Canceled) || len(observations) != 3 || observations[0].Applicable || observations[1].Applicable || observations[2].Report == nil || observations[2].Report.Completed {
t.Fatalf("canceled checks = %+v, %v", observations, err)
}
}
func TestSmokeChecksUseSelectedProfileForAccessAndGeneration(t *testing.T) {
var gets, posts atomic.Int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
gets.Add(1)
fmt.Fprint(w, `{"data":{"label":"member","is_free_tier":false,"usage":0}}`)
return
}
posts.Add(1)
fmt.Fprint(w, `{"choices":[{"message":{"role":"assistant","content":"{\"status\":\"ok\"}"},"finish_reason":"stop"}]}`)
}))
defer server.Close()
input := configkit.Input{Destination: configkit.Keys("promptkit"), Source: configkit.Mapping("smoke", map[string]any{
"backends": []any{map[string]any{"id": "backend", "provider": "openrouter", "endpoint": "http://127.0.0.1:1/v1"}},
"profiles": []any{map[string]any{"id": "first", "backend": "backend", "endpoint": server.URL + "/v1", "model": "one"}, map[string]any{"id": "second", "backend": "backend", "endpoint": server.URL + "/v1", "model": "two"}},
})}
engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &input}}, promptkit.WithBundledOpenRouter(false))
if err != nil {
t.Fatal(err)
}
targets := Targets{Access: []AccessTarget{{BackendID: "backend", ProfileID: "first"}}, Profiles: []ProfileTarget{{ID: "first"}, {ID: "second"}}}
model := "override"
retries := 0
observations, err := RunSmokeChecks(context.Background(), engine, targets, &promptkit.ExecutionTargetOverride{Model: model}, &retries)
if err != nil {
t.Fatalf("checks: %v; observations: %+v", err, observations)
}
if gets.Load() != 2 || posts.Load() != 2 || len(observations) != 4 {
t.Fatalf("gets=%d posts=%d observations=%+v", gets.Load(), posts.Load(), observations)
}
for _, observation := range observations {
if observation.Report.Target.EffectiveSettings.Model != model {
t.Fatalf("override missing: %+v", observation.Report.Target)
}
}
}
func TestSmokeVerdictsRequireAffirmativeFacts(t *testing.T) {
yes, no := true, false
for _, test := range []struct {
name string
report *promptkit.SmokeTestReport
err error
applicable, failed bool
}{
{"passed", &promptkit.SmokeTestReport{Supported: true, Completed: true, Facts: promptkit.SmokeTestFacts{GenerationCompleted: &yes, ValidJSON: &yes, SchemaMatched: &yes}}, nil, true, false},
{"negative", &promptkit.SmokeTestReport{Supported: true, Completed: true, Facts: promptkit.SmokeTestFacts{GenerationCompleted: &yes, ValidJSON: &yes, SchemaMatched: &no}}, nil, true, true},
{"unknown", &promptkit.SmokeTestReport{Supported: true, Completed: true}, nil, true, true},
{"unfinished", &promptkit.SmokeTestReport{Supported: true}, nil, true, true},
{"unsupported", &promptkit.SmokeTestReport{}, nil, false, false},
{"inapplicable", &promptkit.SmokeTestReport{Supported: true, Applicable: &no}, nil, false, false},
{"canceled", &promptkit.SmokeTestReport{}, context.Canceled, true, true},
} {
t.Run(test.name, func(t *testing.T) {
applicable, err := evaluateSmoke(promptkit.SmokeTestStructuredOutput, test.report, test.err)
if applicable != test.applicable || (err != nil) != test.failed || test.err != nil && !errors.Is(err, test.err) {
t.Fatalf("verdict = %v, %v", applicable, err)
}
})
}
}