package cli import ( "fmt" "io" "strings" "gitea.maximumdirect.net/eric/audita/v4/internal/core/diagnostics" "gitea.maximumdirect.net/eric/audita/v4/internal/framework/llm" commandkit "go.maximumdirect.net/commandkit" pk "go.maximumdirect.net/promptkit" ) func runPreflight(inv commandkit.Invocation, input processInput) commandkit.Outcome { reporter := inv.Reporter() _ = reporter.Progress("Checking local processing readiness.") loaded, err := loadProcessInputs(inv.Context, input, inv.ReportingMode()) if err != nil { return acquisitionOutcome(inv, err) } prepared, err := prepareLocal(inv.Context, input, loaded, reporter.Output()) if err != nil { return preparationOutcome(inv, err) } if err := diagnostics.ProbeOutput(inv.Context, prepared.Destinations); err != nil { return commandkit.Failure(err.Error()).WithCause(err) } 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.") results, err := llm.VerifyStructuredOutput(inv.Context, loaded.Runtime.Engine, prepared.Plan.Profiles) 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) } for _, result := range results { _ = reporter.Info(verificationLine(result)) } _ = reporter.AtEnd().Info("Local and structured-output preflight checks passed.") return commandkit.Success() } func verificationLine(result llm.ProfileVerification) string { line := fmt.Sprintf("Profile %q: %s", result.ProfileID, result.Status) 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) if !count.count.Complete { value += " (partial)" } } 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" }