Complete pipeline preflight verification
This commit is contained in:
@@ -96,38 +96,39 @@ func selectPipelineInvocation(ctx context.Context, loaded loadedPipelineInvocati
|
||||
}
|
||||
|
||||
type preparedPipelineInvocation struct {
|
||||
resolved pipeline.ResolvedPipeline
|
||||
prepared *pipeline.PreparedPipeline
|
||||
raw []byte
|
||||
sessionID string
|
||||
diagnostics []contracts.ProducerDiagnostic
|
||||
resolved pipeline.ResolvedPipeline
|
||||
prepared *pipeline.PreparedPipeline
|
||||
raw []byte
|
||||
sessionID string
|
||||
diagnostics []contracts.ProducerDiagnostic
|
||||
failureTarget string
|
||||
}
|
||||
|
||||
func preparePipelineInvocation(loaded loadedPipelineInvocation, selected selectedPipelineInvocation, input pipelineRunInput) (preparedPipelineInvocation, *commandkit.Outcome) {
|
||||
workingDirectory, err := os.Getwd()
|
||||
if err != nil {
|
||||
outcome := commandkit.Failure(err.Error()).WithCause(err)
|
||||
return preparedPipelineInvocation{}, &outcome
|
||||
return preparedPipelineInvocation{failureTarget: "preparation"}, &outcome
|
||||
}
|
||||
materialized, diagnostics, err := pipeline.MaterializeReferences(selected.effective.ResolvedPipeline, selected.catalog, pipeline.ReferenceMaterializationOptions{ConfigPath: loaded.loaded.MainSourcePath(), WorkingDir: workingDirectory})
|
||||
if err != nil {
|
||||
outcome := commandkit.Failure(err.Error()).WithCause(err)
|
||||
return preparedPipelineInvocation{}, &outcome
|
||||
return preparedPipelineInvocation{failureTarget: "references"}, &outcome
|
||||
}
|
||||
prepared, err := pipeline.Prepare(materialized, selected.registries, pipeline.ModuleDependencies{Engine: selected.engine})
|
||||
if err != nil {
|
||||
outcome := commandkit.Failure(err.Error()).WithCause(err)
|
||||
return preparedPipelineInvocation{}, &outcome
|
||||
return preparedPipelineInvocation{resolved: materialized, diagnostics: diagnostics, failureTarget: "modules"}, &outcome
|
||||
}
|
||||
raw, err := os.ReadFile(input.InputFile.Value)
|
||||
if err != nil {
|
||||
outcome := commandkit.Failure(fmt.Sprintf("Read input %q: %v", input.InputFile.Value, err)).WithCause(err)
|
||||
return preparedPipelineInvocation{}, &outcome
|
||||
return preparedPipelineInvocation{resolved: materialized, diagnostics: diagnostics, failureTarget: "input"}, &outcome
|
||||
}
|
||||
sessionID, err := runSessionID(input.Overrides.Resources.SessionID, materialized.Input.Module, raw)
|
||||
if err != nil {
|
||||
outcome := commandkit.Failure(err.Error()).WithCause(err)
|
||||
return preparedPipelineInvocation{}, &outcome
|
||||
return preparedPipelineInvocation{resolved: materialized, diagnostics: diagnostics, failureTarget: "input"}, &outcome
|
||||
}
|
||||
return preparedPipelineInvocation{resolved: materialized, prepared: prepared, raw: raw, sessionID: sessionID, diagnostics: diagnostics}, nil
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"text/template"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
@@ -12,11 +14,12 @@ import (
|
||||
)
|
||||
|
||||
type preflightScope struct {
|
||||
Pipeline string `json:"pipeline,omitempty" yaml:"pipeline,omitempty"`
|
||||
Path string `json:"path,omitempty" yaml:"path,omitempty"`
|
||||
Profile string `json:"profile,omitempty" yaml:"profile,omitempty"`
|
||||
Backend string `json:"backend,omitempty" yaml:"backend,omitempty"`
|
||||
Bindings []preflight.BindingScope `json:"bindings,omitempty" yaml:"bindings,omitempty"`
|
||||
Pipeline string `json:"pipeline,omitempty" yaml:"pipeline,omitempty"`
|
||||
Path string `json:"path,omitempty" yaml:"path,omitempty"`
|
||||
Reference string `json:"reference,omitempty" yaml:"reference,omitempty"`
|
||||
Profile string `json:"profile,omitempty" yaml:"profile,omitempty"`
|
||||
Backend string `json:"backend,omitempty" yaml:"backend,omitempty"`
|
||||
Bindings []preflight.BindingScope `json:"bindings,omitempty" yaml:"bindings,omitempty"`
|
||||
}
|
||||
|
||||
type preflightCheck struct {
|
||||
@@ -118,20 +121,29 @@ func executePreflight(invocation commandkit.Invocation, input pipelineRunInput,
|
||||
}
|
||||
add("selection", preflightScope{Pipeline: input.PipelineID}, "passed", "Pipeline selection and resources are valid.")
|
||||
local, failure := preparePipelineInvocation(loaded, selected, input)
|
||||
if failure != nil {
|
||||
add("preparation", preflightScope{Pipeline: input.PipelineID}, "failed", failure.Explanation.Message)
|
||||
return finish(*failure)
|
||||
}
|
||||
for _, diagnostic := range local.diagnostics {
|
||||
for _, sample := range diagnostic.Samples {
|
||||
message := sample.Message
|
||||
if diagnostic.Disposition == contracts.DiagnosticDispositionWarning {
|
||||
report.Checks = append(report.Checks, preflightCheck{ID: "reference", Scope: preflightScope{Path: sample.Scope}, Result: "warning", Code: diagnostic.ReasonCode, Message: message})
|
||||
report.Checks = append(report.Checks, preflightCheck{ID: "reference", Scope: preflightScope{Reference: sample.Scope}, Result: "warning", Code: diagnostic.ReasonCode, Message: message})
|
||||
} else {
|
||||
_ = invocation.Reporter().Info(message)
|
||||
}
|
||||
}
|
||||
}
|
||||
if failure != nil {
|
||||
scope := preflightScope{Pipeline: input.PipelineID}
|
||||
if local.failureTarget == "input" {
|
||||
scope.Path = input.InputFile.Value
|
||||
} else if local.failureTarget == "references" {
|
||||
var pathErr *os.PathError
|
||||
if errors.As(failure.Cause, &pathErr) {
|
||||
scope.Path = pathErr.Path
|
||||
}
|
||||
}
|
||||
add(local.failureTarget, scope, "failed", failure.Explanation.Message)
|
||||
return finish(*failure)
|
||||
}
|
||||
add("preparation", preflightScope{Pipeline: input.PipelineID}, "passed", "References and modules are prepared.")
|
||||
if _, err := local.prepared.ParseSource(ctx, contracts.ParseRequest{Path: input.InputFile.Value, Raw: local.raw}); err != nil {
|
||||
add("input", preflightScope{Path: input.InputFile.Value}, "failed", err.Error())
|
||||
@@ -153,9 +165,18 @@ func executePreflight(invocation commandkit.Invocation, input pipelineRunInput,
|
||||
}
|
||||
targets, err := preflight.DiscoverTargets(ctx, selected.effective.ResolvedPipeline, selected.catalog, selected.engine)
|
||||
if err != nil {
|
||||
add("targets", preflightScope{Pipeline: input.PipelineID}, "failed", err.Error())
|
||||
scope := preflightScope{Pipeline: input.PipelineID}
|
||||
var targetErr *preflight.TargetError
|
||||
if errors.As(err, &targetErr) {
|
||||
scope.Bindings = []preflight.BindingScope{targetErr.Scope}
|
||||
}
|
||||
add("targets", scope, "failed", err.Error())
|
||||
return finish(pipelineResourceFailure(err))
|
||||
}
|
||||
if err := capture.WriteJSON("targets.json", targets); err != nil {
|
||||
add("debug", preflightScope{Path: capture.Path()}, "failed", err.Error())
|
||||
return finish(commandkit.Failure(err.Error()).WithCause(err))
|
||||
}
|
||||
evidence := newRunEvidenceRecorder(capture, loaded.result)
|
||||
evidenceIndex := 0
|
||||
observations, err := preflight.RunSmokeChecksObserved(ctx, selected.engine, targets, input.Overrides.Execution.Execution, input.Overrides.Execution.TransientRetries, func(observation preflight.SmokeObservation) error {
|
||||
@@ -181,7 +202,11 @@ func executePreflight(invocation commandkit.Invocation, input pipelineRunInput,
|
||||
}
|
||||
if err != nil {
|
||||
if !failedCheck {
|
||||
add("debug", preflightScope{Path: capture.Path()}, "failed", err.Error())
|
||||
if capture.Err() != nil {
|
||||
add("debug", preflightScope{Path: capture.Path()}, "failed", err.Error())
|
||||
} else {
|
||||
add("diagnostics", preflightScope{Pipeline: input.PipelineID}, "failed", err.Error())
|
||||
}
|
||||
}
|
||||
return finish(commandkit.Failure(err.Error()).WithCause(err))
|
||||
}
|
||||
|
||||
@@ -90,11 +90,22 @@ func TestPreflightLocalFailureStopsBeforeProvider(t *testing.T) {
|
||||
if err := json.Unmarshal(out.Bytes(), &report); err != nil || outcome.Status == 0 || report.Ready || client.calls != 0 {
|
||||
t.Fatalf("outcome=%+v report=%+v error=%v calls=%d", outcome, report, err, client.calls)
|
||||
}
|
||||
if last := report.Checks[len(report.Checks)-1]; last.ID != "input" || last.Scope.Path != input+"-missing" {
|
||||
t.Fatalf("input failure scope = %+v", last)
|
||||
}
|
||||
if !errors.Is(outcome.Cause, context.Canceled) && outcome.Cause == nil {
|
||||
t.Fatal("missing input cause")
|
||||
}
|
||||
out.Reset()
|
||||
stderr.Reset()
|
||||
missingReference := filepath.Join(t.TempDir(), "missing.txt")
|
||||
outcome = app.Execute(context.Background(), []string{"preflight", "demo", "--config", config, "--input-file", input, "--reference", "context=" + missingReference, "--format", "json"}, commandkit.Streams{Out: &out, Err: &stderr})
|
||||
report = preflightReport{}
|
||||
if err := json.Unmarshal(out.Bytes(), &report); err != nil || outcome.Status == 0 || len(report.Checks) == 0 || report.Checks[len(report.Checks)-1].Scope.Path != missingReference || client.calls != 0 {
|
||||
t.Fatalf("reference failure: outcome=%+v report=%+v err=%v calls=%d", outcome, report, err, client.calls)
|
||||
}
|
||||
out.Reset()
|
||||
stderr.Reset()
|
||||
outcome = app.Execute(context.Background(), []string{"preflight", "demo", "--config", config}, commandkit.Streams{Out: &out, Err: &stderr})
|
||||
if outcome.Status == 0 || out.Len() != 0 || client.calls != 0 {
|
||||
t.Fatalf("missing required flag: %+v %q", outcome, out.String())
|
||||
@@ -187,3 +198,28 @@ func TestPreflightCacheFailureWarnsWithoutPreventingReadiness(t *testing.T) {
|
||||
t.Fatalf("missing cache warning: %+v", report)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreflightRetainsReferenceWarningWhenInputReadFails(t *testing.T) {
|
||||
probe := &runProbe{}
|
||||
_, config, input, _ := runFixture(t, probe)
|
||||
app, err := NewApplication("test", Options{Registries: runRegistries(t, probe), LLMClient: &readinessClient{content: `{"status":"ok"}`}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
empty := filepath.Join(t.TempDir(), "empty.txt")
|
||||
if err := os.WriteFile(empty, nil, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out, stderr bytes.Buffer
|
||||
outcome := app.Execute(context.Background(), []string{"preflight", "demo", "--config", config, "--input-file", input + "-missing", "--reference", "context=" + empty, "--format", "json"}, commandkit.Streams{Out: &out, Err: &stderr})
|
||||
var report preflightReport
|
||||
if err := json.Unmarshal(out.Bytes(), &report); err != nil || outcome.Status == 0 {
|
||||
t.Fatalf("outcome=%+v report=%+v err=%v", outcome, report, err)
|
||||
}
|
||||
for _, check := range report.Checks {
|
||||
if check.ID == "reference" && check.Result == "warning" && check.Code == "empty_reference" && check.Scope.Reference != "" {
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Fatalf("reference warning lost: %+v", report.Checks)
|
||||
}
|
||||
|
||||
@@ -35,6 +35,15 @@ type Targets struct {
|
||||
Access []AccessTarget
|
||||
}
|
||||
|
||||
// TargetError identifies the binding whose initial target could not be selected.
|
||||
type TargetError struct {
|
||||
Scope BindingScope
|
||||
Cause error
|
||||
}
|
||||
|
||||
func (err *TargetError) Error() string { return err.Cause.Error() }
|
||||
func (err *TargetError) Unwrap() error { return err.Cause }
|
||||
|
||||
// DiscoverTargets identifies initial request targets without invoking providers.
|
||||
func DiscoverTargets(ctx context.Context, resolved pipeline.ResolvedPipeline, catalog pipeline.ModuleCatalog, engine *promptkit.Engine) (Targets, error) {
|
||||
var targets Targets
|
||||
@@ -46,7 +55,12 @@ func DiscoverTargets(ctx context.Context, resolved pipeline.ResolvedPipeline, ca
|
||||
}
|
||||
profiles := make(map[string]int)
|
||||
access := make(map[string]int)
|
||||
add := func(scope BindingScope, class contracts.ExecutionClass, profile string, prompts []string) error {
|
||||
add := func(scope BindingScope, class contracts.ExecutionClass, profile string, prompts []string) (failure error) {
|
||||
defer func() {
|
||||
if failure != nil {
|
||||
failure = &TargetError{Scope: scope, Cause: failure}
|
||||
}
|
||||
}()
|
||||
if class != contracts.ExecutionClassLLMBacked {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -69,6 +69,11 @@ prompts:
|
||||
resolved.Chunk.LLMProfile = "missing"
|
||||
if _, err := DiscoverTargets(context.Background(), resolved, catalog, engine); !errors.Is(err, promptkit.ErrProfileNotFound) {
|
||||
t.Fatalf("missing profile cause = %v", err)
|
||||
} else {
|
||||
var targetErr *TargetError
|
||||
if !errors.As(err, &targetErr) || targetErr.Scope.Module != "chunk" {
|
||||
t.Fatalf("missing profile binding scope = %v", err)
|
||||
}
|
||||
}
|
||||
resolved.Chunk.LLMProfile = ""
|
||||
resolved.Chunk.Module = "dynamic"
|
||||
|
||||
Reference in New Issue
Block a user