2026-11-27 01:19:17 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
|
|
import (
|
2026-11-27 04:12:41 +00:00
|
|
|
"context"
|
2026-11-27 01:22:58 +00:00
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"strings"
|
2026-11-27 04:12:41 +00:00
|
|
|
"text/template"
|
2026-11-27 01:22:58 +00:00
|
|
|
|
2026-11-27 01:19:17 +00:00
|
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/core/diagnostics"
|
2026-11-27 01:22:58 +00:00
|
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/llm"
|
2026-11-27 01:19:17 +00:00
|
|
|
commandkit "go.maximumdirect.net/commandkit"
|
2026-11-27 01:22:58 +00:00
|
|
|
pk "go.maximumdirect.net/promptkit"
|
2026-11-27 01:19:17 +00:00
|
|
|
)
|
|
|
|
|
|
2026-11-27 04:12:41 +00:00
|
|
|
// preflightUsage carries native observations to CommandKit's diagnostic report.
|
|
|
|
|
// Smoke calls are deliberately absent from PromptKit's execution observer.
|
|
|
|
|
type preflightUsageKey struct{}
|
|
|
|
|
type preflightUsage struct {
|
|
|
|
|
Lines []string
|
|
|
|
|
Failed bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var preflightUsageTemplate = template.Must(commandkit.ParseTextTemplate("preflight smoke usage", `{{if .Lines}}Preflight smoke usage; execution-observer totals exclude these calls.
|
|
|
|
|
{{if .Failed}}See per-profile failure details below.
|
|
|
|
|
{{else}}{{range .Lines}}{{.}}
|
|
|
|
|
{{end}}{{end}}{{end}}`, nil))
|
|
|
|
|
|
|
|
|
|
func preflightUsageFeature() commandkit.InvocationFeature {
|
|
|
|
|
return commandkit.NewInvocationFeature(commandkit.InvocationFeatureSpec[struct{}]{
|
|
|
|
|
Start: func(inv commandkit.Invocation, _ struct{}) (context.Context, func() commandkit.Report) {
|
|
|
|
|
if inv.CanonicalPath() != "preflight" {
|
|
|
|
|
return inv.Context, nil
|
|
|
|
|
}
|
|
|
|
|
usage := new(preflightUsage)
|
|
|
|
|
return context.WithValue(inv.Context, preflightUsageKey{}, usage), func() commandkit.Report {
|
|
|
|
|
return commandkit.Report{Value: usage, TextTemplate: preflightUsageTemplate}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-27 01:19:17 +00:00
|
|
|
func runPreflight(inv commandkit.Invocation, input processInput) commandkit.Outcome {
|
2026-11-27 01:22:58 +00:00
|
|
|
reporter := inv.Reporter()
|
|
|
|
|
_ = reporter.Progress("Checking local processing readiness.")
|
2026-11-27 01:19:17 +00:00
|
|
|
loaded, err := loadProcessInputs(inv.Context, input, inv.ReportingMode())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return acquisitionOutcome(inv, err)
|
|
|
|
|
}
|
2026-11-27 01:22:58 +00:00
|
|
|
prepared, err := prepareLocal(inv.Context, input, loaded, reporter.Output())
|
2026-11-27 01:19:17 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return preparationOutcome(inv, err)
|
|
|
|
|
}
|
|
|
|
|
if err := diagnostics.ProbeOutput(inv.Context, prepared.Destinations); err != nil {
|
|
|
|
|
return commandkit.Failure(err.Error()).WithCause(err)
|
|
|
|
|
}
|
2026-11-27 01:22:58 +00:00
|
|
|
if !input.Smoke.Value {
|
|
|
|
|
_ = reporter.AtEnd().Info("Local preflight checks passed; provider verification was skipped.")
|
|
|
|
|
return commandkit.Success()
|
|
|
|
|
}
|
|
|
|
|
if len(prepared.Plan.Profiles) == 0 {
|
|
|
|
|
_ = reporter.AtEnd().Info("Local preflight checks passed; no profiles were selected for provider verification.")
|
|
|
|
|
return commandkit.Success()
|
|
|
|
|
}
|
|
|
|
|
_ = reporter.Progress("Checking structured output for selected profiles.")
|
2026-11-27 04:04:30 +00:00
|
|
|
results, err := llm.VerifyStructuredOutput(inv.Context, loaded.Runtime.Engine, prepared.Plan.Profiles, func(id string) {
|
|
|
|
|
_ = reporter.Progress(fmt.Sprintf("Checking structured output for profile %q.", id))
|
|
|
|
|
})
|
2026-11-27 04:12:41 +00:00
|
|
|
if usage, ok := inv.Context.Value(preflightUsageKey{}).(*preflightUsage); ok {
|
|
|
|
|
usage.Failed = err != nil
|
|
|
|
|
for _, result := range results {
|
|
|
|
|
usage.Lines = append(usage.Lines, verificationLine(result))
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-27 01:22:58 +00:00
|
|
|
if err != nil {
|
|
|
|
|
details := make([]commandkit.Detail, 0, len(results))
|
|
|
|
|
for _, result := range results {
|
|
|
|
|
line := verificationLine(result)
|
|
|
|
|
details = append(details, func(w io.Writer) error { _, err := io.WriteString(w, line+"\n"); return err })
|
|
|
|
|
}
|
|
|
|
|
return commandkit.Failure("Structured-output verification failed.", details...).WithCause(err)
|
|
|
|
|
}
|
|
|
|
|
_ = reporter.AtEnd().Info("Local and structured-output preflight checks passed.")
|
2026-11-27 01:19:17 +00:00
|
|
|
return commandkit.Success()
|
|
|
|
|
}
|
2026-11-27 01:22:58 +00:00
|
|
|
|
|
|
|
|
func verificationLine(result llm.ProfileVerification) string {
|
|
|
|
|
line := fmt.Sprintf("Profile %q: %s", result.ProfileID, result.Status)
|
2026-11-27 04:03:24 +00:00
|
|
|
if result.Err != nil {
|
|
|
|
|
line += ": " + result.Err.Error()
|
|
|
|
|
}
|
2026-11-27 01:22:58 +00:00
|
|
|
if result.Report == nil {
|
|
|
|
|
return line + "; smoke usage: unknown"
|
|
|
|
|
}
|
|
|
|
|
report := result.Report
|
|
|
|
|
if result.Status != llm.VerificationSuccess {
|
|
|
|
|
line += fmt.Sprintf(" (completed=%t, generation=%s, JSON=%s, schema=%s", report.Completed, boolObservation(report.Facts.GenerationCompleted), boolObservation(report.Facts.ValidJSON), boolObservation(report.Facts.SchemaMatched))
|
|
|
|
|
if report.Failure != pk.ExecutionFailureNone {
|
|
|
|
|
line += ", failure=" + string(report.Failure)
|
|
|
|
|
}
|
|
|
|
|
if report.Reason != pk.SmokeTestReasonNone {
|
|
|
|
|
line += ", reason=" + string(report.Reason)
|
|
|
|
|
}
|
|
|
|
|
line += ")"
|
|
|
|
|
}
|
|
|
|
|
usage := report.Usage
|
|
|
|
|
counts := []struct {
|
|
|
|
|
name string
|
|
|
|
|
count pk.ObservedTokenCount
|
|
|
|
|
}{
|
|
|
|
|
{"prompt", usage.PromptTokens}, {"completion", usage.CompletionTokens}, {"reasoning", usage.ReasoningTokens}, {"total", usage.TotalTokens}, {"cached", usage.CachedTokens}, {"cache-write", usage.CacheWriteTokens},
|
|
|
|
|
}
|
|
|
|
|
parts := make([]string, 0, len(counts))
|
|
|
|
|
for _, count := range counts {
|
|
|
|
|
value := "unknown"
|
|
|
|
|
if count.count.Known {
|
|
|
|
|
value = fmt.Sprintf("%d", count.count.Value)
|
2026-11-27 01:26:07 +00:00
|
|
|
}
|
|
|
|
|
if count.count.Complete {
|
|
|
|
|
value += " (complete)"
|
|
|
|
|
} else {
|
|
|
|
|
value += " (incomplete)"
|
2026-11-27 01:22:58 +00:00
|
|
|
}
|
|
|
|
|
parts = append(parts, count.name+"="+value)
|
|
|
|
|
}
|
|
|
|
|
return line + "; smoke usage: " + strings.Join(parts, ", ")
|
|
|
|
|
}
|
|
|
|
|
func boolObservation(value *bool) string {
|
|
|
|
|
if value == nil {
|
|
|
|
|
return "unknown"
|
|
|
|
|
}
|
|
|
|
|
if *value {
|
|
|
|
|
return "true"
|
|
|
|
|
}
|
|
|
|
|
return "false"
|
|
|
|
|
}
|