From d9d6743fc77d3044097194e9ebfbf5f7752164d4 Mon Sep 17 00:00:00 2001 From: Tars Date: Mon, 23 Nov 2026 22:41:40 -0300 Subject: [PATCH] fix(cli): name early QA preflight refusal stages --- docs/operating.md | 8 +++-- internal/cli/doctor.go | 5 ++- internal/cli/qa_test.go | 70 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/docs/operating.md b/docs/operating.md index 8cb44ea..fb16d5b 100644 --- a/docs/operating.md +++ b/docs/operating.md @@ -252,14 +252,16 @@ fixed vocabulary and no guessed cause: unusable `--profile`, a backend route the profile does not carry, or a route the policy does not admit. Nothing is launched and no provider is claimed, because none was resolved. -- `stage=actor_admission` — the installed assets of the selected role were refused; - the following `admission_stage` names the check. +- `stage=actor_admission` — the selected runtime's install root could not be + resolved, or the installed assets of the selected role were refused. Asset + refusals also include `admission_stage` to name the check. - `stage=model_probe` — the backend's own availability probe answered that the routed pair is unusable. `route_provider` and `route_model` name the pair that was actually routed (nothing else), escaped for the terminal, and `model_unavailable` or `auth_not_ready` prints its own remedy. - `stage=evidence_admission` — the supplied evidence sidecar was refused before - launch, with the `evidence_field` and `evidence_reason` that refused it. + launch, including failures to read the file, with the `evidence_field` and + `evidence_reason` that refused it. The vocabulary is preflight-only. A post-launch outcome (execution, timeout, stream, normalization) prints no stage, because no preflight question refused it. diff --git a/internal/cli/doctor.go b/internal/cli/doctor.go index 98da2ff..592e563 100644 --- a/internal/cli/doctor.go +++ b/internal/cli/doctor.go @@ -359,6 +359,7 @@ func runQA(ctx context.Context, args []string, stdout, stderr io.Writer) int { evidence, err = readQAFile(evidencePath, qapi.MaxEvidenceBytes) if err != nil { failure := qaFailure(stderr, "invalid_request") + _, _ = fmt.Fprintf(stderr, "stage=%s\n", qaStageEvidenceAdmission) if os.IsNotExist(err) || errors.Is(err, os.ErrNotExist) { _, _ = fmt.Fprintf(stderr, "evidence_field=path evidence_reason=file_not_found\n") } else if stat, statErr := os.Stat(evidencePath); statErr == nil && stat.Size() > int64(qapi.MaxEvidenceBytes) { @@ -379,7 +380,9 @@ func runQA(ctx context.Context, args []string, stdout, stderr io.Writer) int { } installRoot, err := qaInstallRoot(backend) if err != nil { - return qaFailure(stderr, "actor_unavailable") + failure := qaFailure(stderr, "actor_unavailable") + _, _ = fmt.Fprintf(stderr, "stage=%s\n", qaStageActorAdmission) + return failure } report, code, err := qapi.RunLocalReportOn(ctx, qapi.ReportRequest{ Role: role, Profile: profile, CatalogRoot: catalogRoot, InstallRoot: installRoot, diff --git a/internal/cli/qa_test.go b/internal/cli/qa_test.go index da651f9..d84268d 100644 --- a/internal/cli/qa_test.go +++ b/internal/cli/qa_test.go @@ -868,6 +868,76 @@ func TestQARunProfileRefusalNamesTheSelectedProfile(t *testing.T) { } } +// Early file refusals happen before the report pipeline can classify them. +func TestQARunEarlyEvidenceRefusalNamesStage(t *testing.T) { + for _, tc := range []struct { + name, path, field, reason string + }{ + {"missing", "missing.json", "path", "file_unavailable"}, + {"directory", ".", "path", "not_a_file"}, + {"oversized", "oversized.json", "top-level", "exceeds_size"}, + } { + t.Run(tc.name, func(t *testing.T) { + args := reportFixture(t) + if tc.name == "oversized" { + mustOK(os.WriteFile(tc.path, bytes.Repeat([]byte(" "), qapi.MaxEvidenceBytes+1), 0o600)) + } + stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{} + code := Run(context.Background(), append(args, "--evidence", tc.path), stdout, stderr, nil) + want := "error=invalid_request\n" + qaReportNote + "stage=evidence_admission\n" + fmt.Sprintf("evidence_field=%s evidence_reason=%s\n", tc.field, tc.reason) + if code != exitFailure || stdout.Len() != 0 || !strings.HasPrefix(stderr.String(), want) || strings.Count(stderr.String(), "stage=") != 1 { + t.Fatalf("evidence refusal = %d stdout %q stderr %q, want prefix %q", code, stdout.String(), stderr.String(), want) + } + if _, err := os.Stat("qa-report-frame"); !os.IsNotExist(err) { + t.Fatal("evidence refusal must not launch the backend") + } + }) + } +} + +func TestQARunEarlyInstallRootRefusalNamesStage(t *testing.T) { + args := reportFixture(t) + t.Setenv("HOME", "") + mustOK(os.Unsetenv("HOME")) + stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{} + code := Run(context.Background(), args, stdout, stderr, nil) + want := "error=actor_unavailable\n" + qaReportNote + "stage=actor_admission\n" + if code != exitFailure || stdout.Len() != 0 || stderr.String() != want { + t.Fatalf("install-root refusal = %d stdout %q stderr %q, want %q", code, stdout.String(), stderr.String(), want) + } + if _, err := os.Stat("qa-report-frame"); !os.IsNotExist(err) { + t.Fatal("install-root refusal must not launch the backend") + } +} + +func TestQARunModelRefusalEscapesProfileRouteIdentity(t *testing.T) { + args := profileReportFixture(t) + // Both fields originate in a caller-owned profile; neither may emit a + // terminal command, a new diagnostic line, or a literal backslash escape. + route, err := json.Marshal(map[string]string{ + "provider": "\x1b]52;c;SGVsbG8=\x07", + "model": "m\n\r\x1b]0;title\x07\\tail", + "effort": "low", + }) + mustOK(err) + qaProfileBodyFixture(t, qaCatalogArgument(args), fmt.Sprintf(`{"schemaVersion":1,"defaultProfile":"fleet","profiles":{"fleet":{"routes":{"requirements-analyst":{"pi":%s}}}}}`, route)) + mustOK(os.WriteFile("qa-report-scenario", []byte("model-unavailable"), 0o600)) + stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{} + code := Run(context.Background(), args, stdout, stderr, nil) + want := `route_provider=\x1b]52;c;SGVsbG8=\x07 route_model=m\x0a\x0d\x1b]0;title\x07\x5ctail` + "\n" + if code != exitFailure || stdout.Len() != 0 || !strings.Contains(stderr.String(), "error=model_unavailable\n") || !strings.Contains(stderr.String(), "stage=model_probe\n") || !strings.Contains(stderr.String(), want) { + t.Fatalf("hostile route refusal = %d stdout %q stderr %q, want %q", code, stdout.String(), stderr.String(), want) + } + for _, control := range []string{"\x1b", "\x07", "\r"} { + if strings.Contains(stderr.String(), control) { + t.Fatalf("refusal contains raw terminal control %q: %q", control, stderr.String()) + } + } + if _, err := os.Stat("qa-report-frame"); !os.IsNotExist(err) { + t.Fatal("model refusal must not launch report execution") + } +} + // TestQARunNamesThePreflightRefusalStage pins #374 S4: each preflight refusal // says which question refused — route resolution, actor admission, model probe, // or evidence admission — using the stage vocabulary the pipeline already