From 41746bd370ee4bc9019ffc60e00c2cc6070c5778 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Mon, 23 Nov 2026 03:38:02 +0000 Subject: [PATCH] Verify native execution usage and delivery behavior --- docs/roadmap/implementation.md | 8 ++ internal/adapter/cli/bootstrap_test.go | 29 ++++++ internal/adapter/cli/lifecycle_test.go | 4 +- internal/adapter/cli/native_run_test.go | 114 +++++++++++++++++++++++- 4 files changed, 151 insertions(+), 4 deletions(-) diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index d169549..203c2ee 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -395,6 +395,14 @@ protecting retained behavior. ## Stage 7 — Verify CLI Generation, Native Usage, And Delivery +**Status: Complete.** Migrated remaining CLI execution fixtures and retained +delivery, presence, cancellation, timeout, and finalization protection. Added +controlled-provider checks for native usage/cost reporting and suppression, +structured results/reports, failed-generation cost, zero-execution and help, +omitted versus zero repair budgets, vLLM metadata and authentication, and safe +bootstrap failure/presentation handling. The offline CLI package suite passes +on Go 1.27.4 without external inference or credentials. + **Files:** CLI `native_run_test.go`, `delivery_test.go`, `lifecycle_test.go`, `app_test.go`, and a focused native usage integration test file if helpful. diff --git a/internal/adapter/cli/bootstrap_test.go b/internal/adapter/cli/bootstrap_test.go index 7949a31..63357d9 100644 --- a/internal/adapter/cli/bootstrap_test.go +++ b/internal/adapter/cli/bootstrap_test.go @@ -161,3 +161,32 @@ func TestMaintainedSecretsExample(t *testing.T) { t.Fatalf("example: %+v %q", outcome, diagnostic.String()) } } + +func TestBootstrapDiagnosticsOnFailure(t *testing.T) { + t.Chdir(t.TempDir()) + const key = "SCRIPTORIUM_TEST_FAILURE_CREDENTIAL" + t.Setenv(key, "") + writeNativeFile(t, "secrets/"+key, "private-canary") + writeNativeFile(t, "bad-load.yml", "config: {secrets_dir: secrets, secrets_overwrite: true}\nserver: {max_request_bytes: -1}\n") + writeNativeFile(t, "bad-secrets.yml", "config: {secrets_dir: missing}\n") + for _, args := range [][]string{ + {"run", "selected", "--config=bad-load.yml", "--verbose"}, + {"config", "validate", "--config=bad-load.yml", "--verbose"}, + {"promptkit", "list", "prompts", "--config=bad-load.yml", "--verbose"}, + {"config", "validate", "--config=bad-secrets.yml", "--verbose"}, + } { + outcome, output, diagnostic := executeNative(t, args...) + if outcome.Status == 0 || strings.Contains(output+diagnostic, "private-canary") || diagnostic == "" { + t.Fatalf("%v: %+v %q %q", args, outcome, output, diagnostic) + } + if strings.Contains(strings.Join(args, " "), "bad-load") && !strings.Contains(diagnostic, key) { + t.Fatalf("missing bootstrap report: %q", diagnostic) + } + } + failure := fmt.Errorf("diagnostic writer failure") + var output bytes.Buffer + outcome := Execute(context.Background(), commandkit.Identity{Name: "scriptorium"}, []string{"config", "validate", "--config=bad-load.yml", "--verbose"}, &output, failedDeliveryWriter{failure}) + if outcome.Status != 1 || outcome.Presentation.Diagnostics.WriteError == nil { + t.Fatalf("presentation: %+v", outcome) + } +} diff --git a/internal/adapter/cli/lifecycle_test.go b/internal/adapter/cli/lifecycle_test.go index 7e8f0a7..d7d2ec3 100644 --- a/internal/adapter/cli/lifecycle_test.go +++ b/internal/adapter/cli/lifecycle_test.go @@ -220,7 +220,7 @@ func TestServeListenAndConfigurationFailures(t *testing.T) { t.Fatalf("serve configuration classification: %+v %q", outcome, diagnostics) } writeNativeFile(t, "bad-framework.yml", "profiles: [\n") - writeNativeFile(t, "bad-framework-app.yml", "promptkit:\n configuration: bad-framework.yml\n") + writeNativeFile(t, "bad-framework-app.yml", "config: {imports: [{path: bad-framework.yml, destination: [promptkit]}]}\n") for _, command := range []string{"run", "serve"} { args := []string{command} if command == "run" { @@ -228,7 +228,7 @@ func TestServeListenAndConfigurationFailures(t *testing.T) { } args = append(args, "--config=bad-framework-app.yml") outcome, _, diagnostics := executeNative(t, args...) - if outcome.Status != 2 || !strings.Contains(diagnostics, "PromptKit configuration") || !strings.Contains(diagnostics, "yaml.invalid") { + if outcome.Status != 2 || !strings.Contains(diagnostics, "Application configuration") || !strings.Contains(diagnostics, "yaml.invalid") { t.Fatalf("framework diagnostics: %+v %q", outcome, diagnostics) } } diff --git a/internal/adapter/cli/native_run_test.go b/internal/adapter/cli/native_run_test.go index 4cd0a43..db273dd 100644 --- a/internal/adapter/cli/native_run_test.go +++ b/internal/adapter/cli/native_run_test.go @@ -8,6 +8,7 @@ import ( "fmt" "net/http" "net/http/httptest" + "os" "strings" "sync/atomic" "testing" @@ -142,8 +143,8 @@ func TestRunDistinguishesCompletedInvalidContentAndOperationalFailure(t *testing })) defer server.Close() nativePromptFixture(t, server.URL+"/v1") - writeNativeFile(t, "invalid-content.yml", "profiles:\n - id: local\n endpoint: "+server.URL+"/v1\n model: controlled\nprompts:\n - id: json\n default_profile: local\n messages:\n - role: user\n content: Return JSON\n output:\n format: json\n validation_mode: json\n") - writeNativeFile(t, "json-app.yml", "promptkit:\n configuration: invalid-content.yml\ntransient_retries: 0\n") + writeNativeFile(t, "invalid-content.yml", "profiles:\n - id: local\n endpoint: "+server.URL+"/v1\n model: controlled\nprompts:\n - id: json\n default_profile: local\n messages:\n - role: user\n content: Return JSON\n output:\n format: json\n validation_mode: json\n repair_attempts: 0\n") + writeNativeFile(t, "json-app.yml", "config: {imports: [{path: invalid-content.yml, destination: [promptkit]}]}\ntransient_retries: 0\n") args := []string{"run", "json", "--config=json-app.yml"} outcome, output, diagnostics := executeNative(t, args...) if outcome.Status != 4 || outcome.Kind != commandkit.OutcomeDomain || outcome.Domain != "validation_failed" || output != "not-json" || diagnostics == "" { @@ -165,6 +166,115 @@ func TestRunDistinguishesCompletedInvalidContentAndOperationalFailure(t *testing } } +func TestNativeUsageReporting(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + fmt.Fprint(w, `{"usage":{"prompt_tokens":100,"completion_tokens":20,"total_tokens":120,"prompt_tokens_details":{"cached_tokens":40,"cache_write_tokens":5},"completion_tokens_details":{"reasoning_tokens":3},"cost":0.02},"choices":[{"message":{"content":"artifact"}}]}`) + })) + defer server.Close() + t.Chdir(t.TempDir()) + writeNativeFile(t, "app.yml", fmt.Sprintf("promptkit:\n backends:\n - {id: controlled, provider: openrouter, endpoint: %q}\n profiles:\n - {id: local, backend: controlled, model: model}\n prompts:\n - id: selected\n default_profile: local\n messages: [{role: user, content: test}]\n output: {format: text, validation_mode: none}\n", server.URL+"/v1")) + base := []string{"run", "selected", "--config=app.yml"} + for _, test := range []struct { + flags []string + visible bool + quiet bool + }{ + {nil, false, false}, {[]string{"--show-tokens=false"}, false, false}, {[]string{"--show-tokens"}, true, false}, + {[]string{"--show-tokens", "--show-tokens=false"}, false, false}, {[]string{"--show-tokens", "--quiet"}, false, true}, + } { + outcome, output, diagnostic := executeNative(t, append(append([]string{}, base...), test.flags...)...) + if outcome.Status != 0 || (!test.quiet && output != "artifact") || (test.quiet && output != "") || strings.Contains(diagnostic, "120") != test.visible { + t.Fatalf("%v: %+v %q %q", test.flags, outcome, output, diagnostic) + } + if test.visible && (!strings.Contains(diagnostic, "3") || !strings.Contains(diagnostic, "40") || !strings.Contains(diagnostic, "0.02")) { + t.Fatalf("usage detail: %q", diagnostic) + } + } + outcome, output, diagnostic := executeNative(t, append(append([]string{}, base...), "--show-tokens", "--format=json", "--report=report.json", "--report-format=json")...) + report, err := os.ReadFile("report.json") + if err != nil || outcome.Status != 0 || !strings.Contains(output, "reasoning_tokens") || !strings.Contains(string(report), "cost_usd") || !strings.Contains(diagnostic, "120") { + t.Fatalf("structured: %+v %q %q %q %v", outcome, output, diagnostic, report, err) + } +} + +func TestNativeRepairBudgetPresence(t *testing.T) { + var calls atomic.Int64 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + content := "invalid" + if calls.Add(1) > 1 { + content = `{"ok":true}` + } + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{"choices": []any{map[string]any{"message": map[string]any{"content": content}}}}) + })) + defer server.Close() + t.Chdir(t.TempDir()) + for _, test := range []struct { + name, budget string + status int + attempts int64 + }{{"default", "", 0, 2}, {"zero", " repair_attempts: 0\n", 4, 1}} { + t.Run(test.name, func(t *testing.T) { + calls.Store(0) + writeNativeFile(t, "app.yml", fmt.Sprintf("promptkit:\n profiles:\n - {id: local, endpoint: %q, model: model}\n prompts:\n - id: selected\n default_profile: local\n messages: [{role: user, content: test}]\n output:\n format: json\n validation_mode: json\n%s", server.URL+"/v1", strings.ReplaceAll(test.budget, " repair", " repair"))) + outcome, _, diagnostic := executeNative(t, "run", "selected", "--config=app.yml", "--format=json") + if outcome.Status != test.status || calls.Load() != test.attempts { + t.Fatalf("%s: %+v calls=%d %q", test.name, outcome, calls.Load(), diagnostic) + } + }) + } +} + +func TestNativeProviderMetadataInspection(t *testing.T) { + t.Chdir(t.TempDir()) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/v1/models" { + t.Errorf("unexpected diagnostic endpoint: %s", r.URL.Path) + } + fmt.Fprint(w, `{"object":"list","data":[]}`) + })) + defer server.Close() + writeNativeFile(t, "app.yml", fmt.Sprintf("promptkit:\n backends:\n - {id: local, provider: vllm, endpoint: %q, zero_cost: true}\n", server.URL+"/v1")) + outcome, output, diagnostic := executeNative(t, "promptkit", "show", "backend", "local", "--config=app.yml", "--format=json") + if outcome.Status != 0 || !strings.Contains(output, "vllm") || !strings.Contains(output, "zero_cost") { + t.Fatalf("provider: %+v %q %q", outcome, output, diagnostic) + } + outcome, output, diagnostic = executeNative(t, "promptkit", "test", "authentication", "--backend=local", "--config=app.yml", "--format=json") + if outcome.Status != 0 || !strings.Contains(output, `"credentials_accepted": true`) { + t.Fatalf("authentication: %+v %q %q", outcome, output, diagnostic) + } +} + +func TestNativeUsageSurvivesUnsuccessfulGeneration(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + fmt.Fprint(w, `{"usage":{"prompt_tokens":30,"completion_tokens":6,"total_tokens":36,"cost":0.01},"choices":[]}`) + })) + defer server.Close() + t.Chdir(t.TempDir()) + writeNativeFile(t, "app.yml", fmt.Sprintf("transient_retries: 0\npromptkit:\n backends:\n - {id: local, provider: openrouter, endpoint: %q}\n profiles:\n - {id: local, backend: local, model: model}\n prompts:\n - id: selected\n default_profile: local\n messages: [{role: user, content: test}]\n output: {format: text, validation_mode: none}\n", server.URL+"/v1")) + outcome, output, diagnostic := executeNative(t, "run", "selected", "--config=app.yml", "--show-tokens") + if outcome.Status != 1 || output != "" || !strings.Contains(diagnostic, "0.01") { + t.Fatalf("failure usage: %+v %q %q", outcome, output, diagnostic) + } +} + +func TestNativeUsageWithoutExecution(t *testing.T) { + t.Chdir(t.TempDir()) + writeNativeFile(t, "app.yml", "{}\n") + outcome, _, diagnostic := executeNative(t, "config", "validate", "--config=app.yml", "--show-tokens") + if outcome.Status != 0 || diagnostic == "" { + t.Fatalf("zero-execution usage: %+v %q", outcome, diagnostic) + } + for _, args := range [][]string{{"run", "--help", "--show-tokens"}, {"config", "explain", "--show-tokens"}} { + outcome, _, diagnostic := executeNative(t, args...) + if outcome.Status != 0 || diagnostic != "" { + t.Fatalf("help: %+v %q", outcome, diagnostic) + } + } +} + func TestRunUsageAndConfigurationFailures(t *testing.T) { t.Chdir(t.TempDir()) for _, args := range [][]string{