diff --git a/docs/operating.md b/docs/operating.md index c0bbb87..9b2a668 100644 --- a/docs/operating.md +++ b/docs/operating.md @@ -266,6 +266,12 @@ output does. A stage says which check refused: it does not claim the failure is transient or persistent, and it is not a remedy. Re-read the same assets and fix whichever one the stage names. +`cortex doctor` reports the same refusals for its probe role: an asset check that +cannot conclude — `binding_invalid`, `install_root_invalid`, +`current_directory_invalid`, `actor_guidance_invalid`, `actor_shadow_unverified` — +is reported as `qa_availability=assets_unverifiable` with the same `qa_assets_stage`, +rather than as a digest mismatch that was never observed. A foreign catalog keeps +naming both fingerprints (`assets_mismatch` with `expected` and `found`). ## Walk the QA fleet end to end @@ -277,13 +283,20 @@ run looks like, and where it currently stops: from an empty directory. Expect `status=completed`, per-runtime lines, and the `warning=uncertified_admission` disclosure. Verify readiness with `cortex doctor`: every runtime reports `qa_availability` for the probe role only — it is a model - availability probe, not an admission guarantee. + availability probe, not an admission guarantee. `qa_route_scope=embedded_default_route` + states what was probed: the binary's embedded catalog and the policy default route. + Doctor reads no caller catalog and no `--profile` route, names the routed pair only + when the probe's verdict carried one, and is not a per-run profile check. 2. **Configure the route.** The fleet ships no models, so pick a provider/model pair from the runtime's extension-free surface and run - `cortex qa setup --catalog /catalog --provider

--model `. Use an - **absolute** catalog path: `qa run` only reads the route profile from a canonical - absolute root (#324). `qa setup` writes `/cortex/qa-profiles.json` with one - route per fleet role, pi-backend scoped (#328). + `cortex qa setup --catalog --provider

--model `. Relative and + absolute roots are both accepted: the command resolves the path before it writes, + so `qa setup --catalog catalog` and `qa run --catalog catalog` from the same + directory address the same file (#324). The root must already exist and contain + `catalog.json`; `qa setup` validates it before probing models and refuses to create + the tree, so a typo fails instead of writing a profile no run reads. `qa setup` + writes `/cortex/qa-profiles.json` with one route per fleet role, + pi-backend scoped (#328). 3. **Run one role with evidence.** Write a short bounded `request.txt`, then: ```sh diff --git a/internal/cli/doctor.go b/internal/cli/doctor.go index 524a2f5..5296ddc 100644 --- a/internal/cli/doctor.go +++ b/internal/cli/doctor.go @@ -638,7 +638,14 @@ func probeCompatibilityMatrix(ctx context.Context, runner runtimeprobe.Runner, p // The note is separate from the per-runtime records: their machine-readable // fields and order stay intact, while the scope and safe next check remain // visible even if every probed backend reports ready. -const doctorScopeNote = "note=qa_availability is backend/model readiness for qa_probe_role only (not_probed means no probe); it does not check asset ownership or install/update conflicts; for a separate read-only ownership plan run cortex update --runtime pi --catalog ./catalog\n" +// +// It states the route scope explicitly (#374 S7). Doctor resolves the policy +// default route against the binary's embedded catalog: it reads no caller +// catalog and no `--profile` route, so a maintainer must not read its answer as +// an evaluation of either. The availability lines name the routed pair only +// when the probe's verdict carried one, which is why the scope belongs here +// rather than guessed per line. +const doctorScopeNote = "note=qa_availability is backend/model readiness for qa_probe_role only (not_probed means no probe); qa_route_scope=embedded_default_route means doctor probes the binary's embedded catalog and the policy default route, never a caller catalog or a --profile route; it does not check asset ownership or install/update conflicts; for a separate read-only ownership plan run cortex update --runtime pi --catalog ./catalog\n" // doctorReport extends the shared per-runtime line with what doctor alone is // asked for: whether that runtime's QA backend could actually run a report. diff --git a/internal/cli/doctor_test.go b/internal/cli/doctor_test.go index 1cb2038..4387f10 100644 --- a/internal/cli/doctor_test.go +++ b/internal/cli/doctor_test.go @@ -12,6 +12,8 @@ import ( "testing" "github.com/refactor-ia/cortex/internal/installobserve" + "github.com/refactor-ia/cortex/internal/qaadmission" + "github.com/refactor-ia/cortex/internal/qapi" "github.com/refactor-ia/cortex/internal/runtimecompat" "github.com/refactor-ia/cortex/internal/runtimematrix" "github.com/refactor-ia/cortex/internal/runtimeprobe" @@ -79,12 +81,15 @@ func stubQABackendProbe(t *testing.T, answers map[string]string) { originalAssetCheck := qaAssetCheck qaAssetCheck = func(_ string) error { return nil } t.Cleanup(func() { qaAssetCheck = originalAssetCheck }) - qaBackendProbe = func(_ context.Context, backend string) string { + qaBackendProbe = func(_ context.Context, backend string) qapi.AvailabilityVerdict { answer, known := answers[backend] if !known { t.Fatalf("doctor probed an unexpected backend: %q", backend) } - return answer + if answer == qaAvailabilityReady { + return qapi.AvailabilityVerdict{Available: true} + } + return qapi.AvailabilityVerdict{Code: qaadmission.Code(answer)} } t.Cleanup(func() { qaBackendProbe = original }) } @@ -189,6 +194,10 @@ func TestQAAssetAvailabilityFromError(t *testing.T) { {name: "missing skill is assets_missing", err: &installobserve.AdmissionUnavailableError{Stage: "skill_missing", Expected: "aa"}, token: "assets_missing", diagnostic: " qa_assets_stage=skill_missing expected=aa"}, {name: "foreign catalog is assets_mismatch with identity", err: &installobserve.AdmissionUnavailableError{Stage: "catalog_fingerprint", Expected: "cc11", Found: "ff22"}, token: "assets_mismatch", diagnostic: " qa_assets_stage=catalog_fingerprint expected=cc11 found=ff22"}, {name: "skill drift is assets_mismatch", err: &installobserve.AdmissionUnavailableError{Stage: "skill_mismatch", Expected: "aa", Found: "bb"}, token: "assets_mismatch", diagnostic: " qa_assets_stage=skill_mismatch expected=aa found=bb"}, + {name: "unadmitted binding is unverifiable, not a digest mismatch", err: &installobserve.AdmissionUnavailableError{Stage: "binding_invalid"}, token: "assets_unverifiable", diagnostic: " qa_assets_stage=binding_invalid"}, + {name: "non-canonical root is unverifiable", err: &installobserve.AdmissionUnavailableError{Stage: "current_directory_invalid"}, token: "assets_unverifiable", diagnostic: " qa_assets_stage=current_directory_invalid"}, + {name: "unproven actor guidance is unverifiable", err: &installobserve.AdmissionUnavailableError{Stage: "actor_guidance_invalid"}, token: "assets_unverifiable", diagnostic: " qa_assets_stage=actor_guidance_invalid"}, + {name: "unproven shadow scan is unverifiable, not a catalog mismatch", err: &installobserve.AdmissionUnavailableError{Stage: "actor_shadow_unverified"}, token: "assets_unverifiable", diagnostic: " qa_assets_stage=actor_shadow_unverified"}, {name: "untyped check failure fails closed", err: errors.New("unrelated"), token: "assets_check_failed", diagnostic: " qa_assets_stage=check_failed"}, } { token, diagnostic := qaAssetAvailabilityFromError(tc.err) @@ -202,7 +211,9 @@ func TestDoctorQALineGatesReadyOnInstalledAssets(t *testing.T) { originalProbe := qaBackendProbe originalCheck := qaAssetCheck t.Cleanup(func() { qaBackendProbe = originalProbe; qaAssetCheck = originalCheck }) - qaBackendProbe = func(_ context.Context, _ string) string { return qaAvailabilityReady } + qaBackendProbe = func(_ context.Context, _ string) qapi.AvailabilityVerdict { + return qapi.AvailabilityVerdict{Available: true} + } qaAssetCheck = func(_ string) error { return &installobserve.AdmissionUnavailableError{Stage: "catalog_fingerprint", Expected: "cc11", Found: "ff22"} } @@ -228,6 +239,40 @@ func TestDoctorQALineGatesReadyOnInstalledAssets(t *testing.T) { } } +// TestDoctorNamesTheProbedRouteAndItsScope pins #374 S7: doctor probes the +// binary's embedded catalog and the policy default route, so the line names the +// routed pair its verdict answered about (which doctor used to discard) and the +// note discloses that scope instead of implying a caller catalog or `--profile` +// route was evaluated. +func TestDoctorNamesTheProbedRouteAndItsScope(t *testing.T) { + originalProbe := qaBackendProbe + originalCheck := qaAssetCheck + t.Cleanup(func() { qaBackendProbe = originalProbe; qaAssetCheck = originalCheck }) + qaAssetCheck = func(_ string) error { return nil } + qaBackendProbe = func(_ context.Context, _ string) qapi.AvailabilityVerdict { + return qapi.AvailabilityVerdict{Code: qaadmission.CodeModelUnavailable, Provider: "nan", Model: "glm5.3"} + } + line := qaBackendLine(context.Background(), runtimematrix.RuntimePi, true) + for _, want := range []string{"qa_availability=model_unavailable", "qa_route_provider=nan qa_route_model=glm5.3"} { + if !strings.Contains(line, want) { + t.Fatalf("line = %q, want it to name %q", line, want) + } + } + // A ready verdict names no pair, so the line must not invent one. + qaBackendProbe = func(_ context.Context, _ string) qapi.AvailabilityVerdict { + return qapi.AvailabilityVerdict{Available: true} + } + line = qaBackendLine(context.Background(), runtimematrix.RuntimePi, true) + if strings.Contains(line, "qa_route_provider") || strings.Contains(line, "qa_route_model") { + t.Fatalf("line = %q, want no claimed route pair for a ready verdict", line) + } + for _, want := range []string{"qa_route_scope=embedded_default_route", "never a caller catalog", "--profile"} { + if !strings.Contains(doctorScopeNote, want) { + t.Fatalf("doctor note = %q, want it to disclose %q", doctorScopeNote, want) + } + } +} + func TestQARouteSetupNoteNamesProfilePathAndShape(t *testing.T) { note := qaRouteSetupNote("/home/user/fleet-catalog") for _, want := range []string{ @@ -300,9 +345,9 @@ func TestDoctorDoesNotProbeAbsentRuntimes(t *testing.T) { originalAssetCheck := qaAssetCheck qaAssetCheck = func(_ string) error { return nil } t.Cleanup(func() { qaAssetCheck = originalAssetCheck }) - qaBackendProbe = func(_ context.Context, backend string) string { + qaBackendProbe = func(_ context.Context, backend string) qapi.AvailabilityVerdict { probed = append(probed, backend) - return "ready" + return qapi.AvailabilityVerdict{Available: true} } t.Cleanup(func() { qaBackendProbe = original }) stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{} diff --git a/internal/cli/qa_test.go b/internal/cli/qa_test.go index f3e690c..5feee75 100644 --- a/internal/cli/qa_test.go +++ b/internal/cli/qa_test.go @@ -648,6 +648,86 @@ func TestQARunProfileFlagSelectsTheRequestedProfile(t *testing.T) { }) } +// TestQARunWithoutAProfileUsesTheDocumentedPlaceholder keeps the legitimate +// default: with no profile source at all, the policy placeholder still routes +// the run (issue #298). +func TestQARunWithoutAProfileUsesTheDocumentedPlaceholder(t *testing.T) { + args := reportFixture(t) + stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{} + if code := Run(context.Background(), args, stdout, stderr, nil); code != exitOK || stdout.String() != fixtureReport+"\n" || stderr.Len() != 0 { + t.Fatalf("placeholder run = %d stdout %q stderr %q", code, stdout.String(), stderr.String()) + } + frame := must(os.ReadFile("qa-report-frame")) + for _, want := range []string{"route_profile role-default", "route_model qwen3.6"} { + if !bytes.Contains(frame, []byte(want)) { + t.Fatalf("frame does not carry the placeholder %q: %q", want, frame) + } + } +} + +// TestQARunRefusesAnUnusableDefaultProfileInsteadOfFallingBack pins the other +// half of #324: a profile source that exists but cannot be read safely is a +// named refusal, never a silent placeholder route and never a launch. +func TestQARunRefusesAnUnusableDefaultProfileInsteadOfFallingBack(t *testing.T) { + for _, testCase := range []struct { + name string + install func(t *testing.T, path string) + }{ + {name: "symlinked profile file", install: func(t *testing.T, path string) { + mustOK(os.Symlink(path+".elsewhere", path)) + }}, + {name: "malformed profile file", install: func(t *testing.T, path string) { + mustOK(os.WriteFile(path, []byte(`{"schemaVersion":1,"defaultProfile":"9bad","profiles":{}}`), 0o600)) + }}, + } { + t.Run(testCase.name, func(t *testing.T) { + args := roleReportFixture(t, qarole.RequirementsAnalyst) + // Stage the profile in a disposable copy: the repository catalog + // must never be mutated by a test that plants an unsafe file. + catalogRoot := filepath.Join(must(os.Getwd()), "catalog-copy") + mustOK(os.CopyFS(catalogRoot, os.DirFS(qaCatalogArgument(args)))) + for index, arg := range args { + if arg == "--catalog" && index+1 < len(args) { + args[index+1] = catalogRoot + } + } + profileDirectory := filepath.Join(catalogRoot, "cortex") + mustOK(os.MkdirAll(profileDirectory, 0o700)) + testCase.install(t, filepath.Join(profileDirectory, "qa-profiles.json")) + + stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{} + code := Run(context.Background(), args, stdout, stderr, nil) + want := "error=profile_invalid\n" + qaReportNote + "stage=route_resolution\nprofile_source=unusable route_setup=" + filepath.Join(catalogRoot, "cortex", "qa-profiles.json") + "\n" + if code != exitFailure || stdout.Len() != 0 || stderr.String() != want { + t.Fatalf("unusable default profile = %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("a refused default profile must not launch the backend") + } + }) + } +} + +// TestQARunProfileRefusalNamesTheSelectedProfile pins the refusal half of S6 +// (#374): an unknown --profile id names the selection, what the profile source +// actually offers, and the file to edit, without echoing anything unbounded. +func TestQARunProfileRefusalNamesTheSelectedProfile(t *testing.T) { + args := profileReportFixture(t) + stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{} + code := Run(context.Background(), append(append([]string{}, args...), "--profile", "no-such-profile"), stdout, stderr, nil) + want := "error=profile_invalid\n" + qaReportNote + "stage=route_resolution\nprofile_selected=no-such-profile profile_source=readable profile_default=fleet-default profile_valid_ids=fleet-default,qa-fleet route_setup=" + filepath.Join(qaCatalogArgument(args), "cortex", "qa-profiles.json") + "\n" + if code != exitFailure || stdout.Len() != 0 || stderr.String() != want { + t.Fatalf("unknown --profile = %d stdout %q stderr %q, want %q", code, stdout.String(), stderr.String(), want) + } + // A hostile id is never echoed: the diagnostic is bounded to the profile-id + // character set and carries no escape or control bytes. + stdout, stderr = &bytes.Buffer{}, &bytes.Buffer{} + code = Run(context.Background(), append(append([]string{}, args...), "--profile", "../\x1b[2Jevil"), stdout, stderr, nil) + if code != exitFailure || strings.Contains(stderr.String(), "\x1b") || !strings.Contains(stderr.String(), "profile_selected= ") { + t.Fatalf("hostile --profile = %d stdout %q stderr %q", code, stdout.String(), stderr.String()) + } +} + // 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 diff --git a/internal/cli/qabackend.go b/internal/cli/qabackend.go index e6611de..812e33c 100644 --- a/internal/cli/qabackend.go +++ b/internal/cli/qabackend.go @@ -72,8 +72,10 @@ func qaBackendIdentity(backend string) string { // qaBackendProbe is the backend availability seam. It is a variable so a test // can answer for a fixture runtime without launching a real one; nil selects -// the production probe. -var qaBackendProbe func(ctx context.Context, backend string) string +// the production probe. It answers with the whole verdict and not its code +// alone: a refusal carries the routed pair it was about (#339, #374 S7), and +// discarding that here is what left doctor unable to name it. +var qaBackendProbe func(ctx context.Context, backend string) qapi.AvailabilityVerdict // qaAssetCheck is the installed-asset seam for the doctor QA line. It runs the // same read-only admission check qa run performs: the binary's embedded catalog @@ -106,6 +108,13 @@ func checkQAAssets(backend string) error { // qaAssetAvailabilityFromError maps an admission failure onto the doctor QA // availability token and its bounded diagnostic. Missing assets and mismatched // assets are two tokens; the diagnostic carries only stages and hex digests. +// +// A refusal whose stage says the check itself could not conclude — the binding +// is not one this binary admits, a root is not canonical, the actor's guidance +// or shadow status could not be proven — gets its own token. Reporting those as +// assets_mismatch implied a digest disagreement that was never observed (#374 +// S7); the stage name is the only detail that claim can carry. +// // Any other check failure fails closed (issue #361): a failed check never // degrades to ready. func qaAssetAvailabilityFromError(err error) (string, string) { @@ -120,7 +129,16 @@ func qaAssetAvailabilityFromError(err error) (string, string) { diagnostic += " expected=" + unavailable.Expected } return "assets_missing", diagnostic + case "binding_invalid", "install_root_invalid", "current_directory_invalid", "actor_guidance_invalid", "actor_shadow_unverified": + return "assets_unverifiable", qaAssetStageDiagnostic(unavailable) } + return "assets_mismatch", qaAssetStageDiagnostic(unavailable) +} + +// qaAssetStageDiagnostic renders the stage and whatever identity the refusal +// carried. Expected and Found are hex digests or the catalog fingerprint, so +// the foreign-catalog case keeps naming both. +func qaAssetStageDiagnostic(unavailable *installobserve.AdmissionUnavailableError) string { diagnostic := " qa_assets_stage=" + unavailable.Stage if unavailable.Expected != "" { diagnostic += " expected=" + unavailable.Expected @@ -128,19 +146,24 @@ func qaAssetAvailabilityFromError(err error) (string, string) { if unavailable.Found != "" { diagnostic += " found=" + unavailable.Found } - return "assets_mismatch", diagnostic + return diagnostic } // probeQABackend answers whether one backend could run a report, without // running one. It binds the runtime and issues that backend's own fixed, // bounded availability probes — the read-only prefix of a report run, and // nothing past it. -func probeQABackend(ctx context.Context, backend string) string { +func probeQABackend(ctx context.Context, backend string) qapi.AvailabilityVerdict { cwd, err := os.Getwd() if err != nil { - return string(qaadmission.CodeUnsupportedRuntime) + return qapi.AvailabilityVerdict{Code: qaadmission.CodeUnsupportedRuntime} } - verdict := qapi.ProbeBackendAvailability(ctx, backend, qaProbeRole, cwd, nil) + return qapi.ProbeBackendAvailability(ctx, backend, qaProbeRole, cwd, nil) +} + +// qaAvailabilityToken renders one verdict as the doctor availability token: +// ready, or the fixed admission code the probe reached. One vocabulary, not two. +func qaAvailabilityToken(verdict qapi.AvailabilityVerdict) string { if verdict.Available { return qaAvailabilityReady } @@ -169,8 +192,9 @@ func qaBackendLine(ctx context.Context, id runtimematrix.RuntimeID, present bool if probe == nil { probe = probeQABackend } - availability = probe(ctx, backend) - if availability == qaAvailabilityReady { + verdict := probe(ctx, backend) + availability = qaAvailabilityToken(verdict) + if verdict.Available { check := qaAssetCheck if check == nil { check = checkQAAssets @@ -181,6 +205,9 @@ func qaBackendLine(ctx context.Context, id runtimematrix.RuntimeID, present bool } } } + if diagnostic == "" { + diagnostic = qaRouteIdentity(verdict) + } } return " qa_backend=" + backend + " qa_identity=" + qaBackendIdentity(backend) + " qa_probe_role=" + string(qaProbeRole) + " qa_availability=" + availability + diagnostic @@ -206,3 +233,12 @@ func qaInstallRoot(backend string) (string, error) { } return "", errUnknownQABackend } + +// qaRouteIdentity renders the routed pair one availability verdict answered +// about, so a doctor line names what it probed instead of discarding it. +func qaRouteIdentity(verdict qapi.AvailabilityVerdict) string { + if verdict.Available || verdict.Provider == "" || verdict.Model == "" { + return "" + } + return " qa_route_provider=" + sanitizeRouteIdentity(verdict.Provider) + " qa_route_model=" + sanitizeRouteIdentity(verdict.Model) +}