Report preflight smoke usage with its correct scope

This commit is contained in:
2026-11-27 04:12:41 +00:00
parent f5cef61bb5
commit b96eeb57ba
5 changed files with 146 additions and 9 deletions

View File

@@ -118,7 +118,13 @@ func buildApplicationWithClient(client *http.Client, extra ...commandkit.Declara
})
definitions = ckprompt.WithSmokeTesting(definitions, capturePromptEngine, nil)
definitions = withPromptBootstrapContext(definitions)
declarations := []commandkit.Declaration{configCommand, commandkit.Completion("audita"), commandkit.WithInvocationFeature(processCommand(), bootstrapFeature()), commandkit.WithInvocationFeature(preflightCommand(), bootstrapFeature()), commandkit.Alias("run", "process")}
declarations := []commandkit.Declaration{
configCommand,
commandkit.Completion("audita"),
commandkit.WithInvocationFeature(processCommand(), bootstrapFeature()),
commandkit.WithInvocationFeature(commandkit.WithInvocationFeature(preflightCommand(), bootstrapFeature()), preflightUsageFeature()),
commandkit.Alias("run", "process"),
}
declarations = append(declarations, ckprompt.Declarations(definitions)...)
declarations = append(declarations, extra...)
return commandkit.New(commandkit.Identity{Name: "audita", Version: buildinfo.Version()}, declarations...)

View File

@@ -1,9 +1,11 @@
package cli
import (
"context"
"fmt"
"io"
"strings"
"text/template"
"gitea.maximumdirect.net/eric/audita/v4/internal/core/diagnostics"
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/llm"
@@ -11,6 +13,33 @@ import (
pk "go.maximumdirect.net/promptkit"
)
// 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}
}
},
})
}
func runPreflight(inv commandkit.Invocation, input processInput) commandkit.Outcome {
reporter := inv.Reporter()
_ = reporter.Progress("Checking local processing readiness.")
@@ -37,6 +66,12 @@ func runPreflight(inv commandkit.Invocation, input processInput) commandkit.Outc
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))
})
if usage, ok := inv.Context.Value(preflightUsageKey{}).(*preflightUsage); ok {
usage.Failed = err != nil
for _, result := range results {
usage.Lines = append(usage.Lines, verificationLine(result))
}
}
if err != nil {
details := make([]commandkit.Detail, 0, len(results))
for _, result := range results {
@@ -45,9 +80,6 @@ func runPreflight(inv commandkit.Invocation, input processInput) commandkit.Outc
}
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()
}

View File

@@ -135,6 +135,10 @@ func TestPreflightVerifiesDistinctProfilesAfterLocalChecks(t *testing.T) {
t.Fatalf("invalid %d models=%v err=%s", status, models, stderr.String())
}
negative = true
requested := append([]string{"preflight", "--show-tokens"}, args[1:]...)
if status := invoke(requested); status != 1 || !strings.Contains(stderr.String(), "total=10 (complete)") || !strings.Contains(stderr.String(), "execution-observer totals exclude these calls") {
t.Fatalf("failed verification usage: status=%d diagnostic=%s", status, stderr.String())
}
quiet := append([]string{}, args...)
quiet = append(quiet[:1], append([]string{"--quiet"}, quiet[1:]...)...)
if status := invoke(quiet); status != 1 || len(models) != 2 || !strings.Contains(stderr.String(), `Profile "review": negative`) || strings.Contains(stderr.String(), "private transcript") || strings.Contains(stderr.String(), `"bad"`) {
@@ -228,3 +232,72 @@ func TestPreflightExplainsNativePreparationError(t *testing.T) {
}
}
}
func TestPreflightUsageReportScopesNativeObservations(t *testing.T) {
for _, tc := range []struct {
name, command string
flags []string
smoke bool
visible bool
}{
{"ordinary", "preflight", nil, true, true},
{"requested", "preflight", []string{"--show-tokens"}, true, true},
{"verbose", "preflight", []string{"--show-tokens", "--verbose"}, true, true},
{"quiet", "preflight", []string{"--show-tokens", "--quiet"}, true, false},
{"local only", "preflight", []string{"--show-tokens", "--smoke=false"}, false, false},
{"processing", "process", []string{"--show-tokens"}, false, false},
} {
t.Run(tc.name, func(t *testing.T) {
calls := 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
content := `{"status":"ok"}`
if tc.command == "process" {
content = `{"corrections":[]}`
}
_ = json.NewEncoder(w).Encode(map[string]any{"choices": []any{map[string]any{"message": map[string]any{"role": "assistant", "content": content}}}, "usage": map[string]int{"prompt_tokens": 7, "completion_tokens": 3, "total_tokens": 10}})
}))
defer server.Close()
dir := t.TempDir()
for name, body := range map[string]string{
"config.yml": "version: 3\nllm: {proposal_profile: fixture}\npipeline: {stages: [{module: grammar}]}\nreporting: {enabled: false}\n",
"prompts.yml": fmt.Sprintf("profiles: [{id: fixture, endpoint: '%s/v1', model: fixture}]\n", server.URL),
"transcript.json": `[{"speaker":"A","start":0,"end":1,"text":"hello"}]`,
} {
if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0600); err != nil {
t.Fatal(err)
}
}
app, err := buildApplication()
if err != nil {
t.Fatal(err)
}
args := append([]string{tc.command}, tc.flags...)
args = append(args, "--config", filepath.Join(dir, "config.yml"), "--output", filepath.Join(dir, "out.json"), filepath.Join(dir, "transcript.json"))
var out, diagnostic bytes.Buffer
result := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostic})
if result.Status != 0 {
t.Fatalf("status=%d diagnostic=%s", result.Status, diagnostic.String())
}
if strings.Contains(diagnostic.String(), "total=10 (complete)") != tc.visible || strings.Contains(diagnostic.String(), "execution-observer totals exclude these calls") != tc.visible {
t.Fatalf("diagnostic=%s", diagnostic.String())
}
wantCalls := 0
if tc.smoke || tc.command == "process" {
wantCalls = 1
}
if calls != wantCalls {
t.Fatalf("calls=%d want=%d", calls, wantCalls)
}
if tc.command == "process" && (!strings.Contains(diagnostic.String(), "Total tokens: 10") || strings.Contains(diagnostic.String(), "Preflight smoke usage")) {
t.Fatalf("processing diagnostic=%s", diagnostic.String())
}
if slices.Contains(tc.flags, "--quiet") && diagnostic.Len() != 0 {
t.Fatalf("quiet diagnostic=%s", diagnostic.String())
}
if out.Len() != 0 {
t.Fatalf("unexpected stdout: %s", out.String())
}
})
}
}