Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

Merge origin/main (v10.0.1) into worktree-tui

Jack Rudenkof3ba188fac04
diff --cc packages/cli/src/model-loader.ts
index 1682ad5b,469c955d..d6991533
--- a/packages/cli/src/model-loader.ts
+++ b/packages/cli/src/model-loader.ts
@@@ -920,54 -995,48 +995,85 @@@ export async function getProviderList()
return data.providers ?? [];
}
+ /** Fetch every active model for a provider from one pinned generation. */
+ export async function getModelsByProvider(provider: string, pageSize = 200): Promise<ModelDoc[]> {
+ const base = `${FIREBASE_BASE_URL}?provider=${encodeURIComponent(provider)}&status=active&limit=${pageSize}`;
++ return fetchPinnedModelDocs(base, "provider catalog");
++}
++
+/**
- * Fetch active models for a given provider.
++ * Every row of one cursor-paginated `queryModels` read, pinned to ONE generation.
++ *
++ * Shared by {@link getModelsByProvider} and {@link getAllModelDocs} because the
++ * checks are the subtle part and must not drift between two copies: the total
++ * may not change mid-read, a cursor may not repeat, and the rows collected must
++ * match the total with no id twice. `label` only names the read in the errors.
+ */
- export async function getModelsByProvider(provider: string, limit = 200): Promise<ModelDoc[]> {
- const url = `${FIREBASE_BASE_URL}?provider=${encodeURIComponent(
- provider
- )}&status=active&limit=${limit}`;
- const response = await fetch(url, {
- signal: AbortSignal.timeout(SEARCH_FETCH_TIMEOUT_MS),
- });
- if (!response.ok) {
- throw new Error(`Firebase provider query returned ${response.status} ${response.statusText}`);
++async function fetchPinnedModelDocs(base: string, label: string): Promise<ModelDoc[]> {
++ const Label = label.charAt(0).toUpperCase() + label.slice(1);
+ const models: ModelDoc[] = [];
+ const seenCursors = new Set<string>();
+ let cursor: string | undefined;
+ let generationId: string | undefined;
+ let expectedTotal: number | undefined;
+ for (let page = 0; page < 40; page++) {
+ const url = new URL(base);
+ if (cursor) url.searchParams.set("cursor", cursor);
+ const envelope = await fetchCatalogPage<{
+ models: ModelDoc[];
+ total: number;
+ nextCursor?: string;
+ }>(url.toString(), SEARCH_FETCH_TIMEOUT_MS, generationId);
+ if (!Array.isArray(envelope.data.models) || !Number.isSafeInteger(envelope.data.total)) {
- throw new Error("Incomplete provider catalog response");
++ throw new Error(`Incomplete ${label} response`);
+ }
+ generationId = envelope.generationId;
+ expectedTotal ??= envelope.data.total;
+ if (envelope.data.total !== expectedTotal)
- throw new Error("Provider catalog total changed during pagination");
++ throw new Error(`${Label} total changed during pagination`);
+ models.push(...envelope.data.models);
+ const nextCursor = envelope.data.nextCursor;
+ if (!nextCursor) {
+ if (
+ models.length !== expectedTotal ||
+ new Set(models.map((model) => model.modelId)).size !== models.length
+ ) {
- throw new Error("Incomplete provider catalog snapshot");
++ throw new Error(`Incomplete ${label} snapshot`);
+ }
+ return models;
+ }
- if (seenCursors.has(nextCursor)) throw new Error("Provider catalog cursor repeated");
++ if (seenCursors.has(nextCursor)) throw new Error(`${Label} cursor repeated`);
+ seenCursors.add(nextCursor);
+ cursor = nextCursor;
}
- const data = (await response.json()) as ModelDoc[] | { models?: ModelDoc[] };
- if (Array.isArray(data)) return data;
- return data.models ?? [];
- throw new Error("Provider catalog exceeded the pagination limit");
++ throw new Error(`${Label} exceeded the pagination limit`);
+}
+
+/**
- * Every active model the catalog knows, in ONE request.
++ * Every active model the cloud models catalog knows, in as few pages as it allows.
+ *
- * WHY A BULK PAGE RATHER THAN N QUERIES. The picker needs one editorial fact per
++ * WHY A BULK READ RATHER THAN N QUERIES. The picker needs one editorial fact per
+ * model — the description — for every row it can draw. The slim catalog
+ * (`?catalog=slim`, the file behind `all-models.json`) deliberately carries none:
- * `model-catalog.ts:servedByVendor` says so in its own doc comment, and a check
- * of the on-disk cache confirms 0 of 704 entries have one. Asking per model, or
- * per provider, is the fan-out this picker already measured at 10 018 ms.
++ * `model-catalog.ts:servedByVendor` says so in its own doc comment. Asking per
++ * model, or per provider, is the fan-out this picker already measured at
++ * 10 018 ms.
+ *
- * MEASURED 2026-09-10 against the live endpoint: `limit=1500` returns all 1016
- * active models, 1.06 MB, in 2.8 s; `limit=1000` returns 1000 with
- * `hasMore: true`, so the limit is honoured rather than capped. The caller caches
- * the projection it needs on disk under the shared Firebase TTL, so this runs at
- * most once a day and never blocks a first paint.
++ * A v3 read like every other query here: the contract `Accept` header, cursor
++ * pages pinned to one generation (via {@link fetchPinnedModelDocs}). A bare
++ * `fetch` with no `Accept` is refused outright — measured 2026-09-18, HTTP 426
++ * `catalog_client_upgrade_required`. MEASURED 2026-09-23 against the live v3
++ * endpoint with this exact query: the rich projection serves at most 200 rows a
++ * page (`limit=1000` returned 200 of 1267, plus a `nextCursor`), so this is about
++ * seven pages. The caller caches the projection it needs on disk under the
++ * shared TTL, so this runs at most once a day and never blocks a first paint.
+ */
- export async function getAllModelDocs(limit = 1500): Promise<ModelDoc[]> {
- const url = `${FIREBASE_BASE_URL}?status=active&limit=${limit}`;
- const response = await fetch(url, {
- signal: AbortSignal.timeout(SEARCH_FETCH_TIMEOUT_MS),
- });
- if (!response.ok) {
- throw new Error(`Firebase catalog query returned ${response.status} ${response.statusText}`);
- }
- const data = (await response.json()) as ModelDoc[] | { models?: ModelDoc[] };
- if (Array.isArray(data)) return data;
- return data.models ?? [];
++export async function getAllModelDocs(pageSize = 200): Promise<ModelDoc[]> {
++ const base = `${FIREBASE_BASE_URL}?status=active&limit=${pageSize}`;
++ return fetchPinnedModelDocs(base, "model catalog");
}
- // ─── Legacy loaders retained for cli.ts --model flag validation ──────────────
+ // ─── Model loaders for cli.ts --model flag validation ────────────────────────
/**
* Load ModelMetadata keyed by model ID for the --model flag help text.
diff --cc packages/cli/src/model-selector-discovery.test.ts
index 8149f685,00000000..3e1ea0bd
mode 100644,000000..100644
--- a/packages/cli/src/model-selector-discovery.test.ts
+++ b/packages/cli/src/model-selector-discovery.test.ts
@@@ -1,532 -1,0 +1,538 @@@
+/**
+ * The picker's discovery outcome — `buildDiscoveredModelOutcome`, the notice
+ * formatter behind it, and the chat-capability chokepoint both doors return
+ * through.
+ *
+ * The defect these pin: `buildDiscoveredModelRows` returned `[]` for five
+ * distinguishable states — a rejected key, an unreachable endpoint, a genuinely
+ * empty roster, a roster where nothing was chat-capable, and a roster that
+ * collapsed to nothing — and the caller fell through to the cloud catalog for
+ * all of them in silence. The user saw "fewer model names, like the provider
+ * does not have any models". So every test here is a state that used to be
+ * indistinguishable from the other four.
+ *
+ * Offline by construction: `globalThis.fetch`, `credentials.getRequestAuth` and
+ * the `CatalogClient` are all substituted directly. No `mock.module()` — it
+ * bleeds into sibling Bun test files.
+ *
+ * Nothing here asserts a price, a context window or a capability flag: those
+ * come from the on-disk model catalog, which is warm on a dev machine and cold
+ * on a fresh clone. Ids, counts and outcome kinds are the assertions.
+ *
+ * Run: bun test packages/cli/src/model-selector-discovery.test.ts
+ */
+
+import { afterEach, beforeEach, describe, expect, mock, spyOn, test } from "bun:test";
+
+import { credentials } from "./auth/credentials/authority.js";
+import {
+ _rosterCollapse,
+ buildDiscoveredModelOutcome,
+ buildDiscoveredModelRows,
+ formatDiscoveryFailureNotice,
+ toPickerRows,
+ warnDiscoveryFailure,
+} from "./model-selector.js";
+import type { ModelInfo } from "./model-selector.js";
+import type { CatalogClient, CatalogModel } from "./providers/model-catalog.js";
+import {
+ type DiscoveryFailure,
+ type DiscoveryFailureKind,
+ describeDiscoveryFailure,
+ invalidateModelDiscovery,
+} from "./providers/model-discovery.js";
+import type { ProviderDefinition } from "./providers/provider-definitions.js";
+import { clearRuntimeRegistry, registerRuntimeProvider } from "./providers/runtime-providers.js";
+
+const PROVIDER = "picker-outcome-test";
+const DISPLAY_NAME = "Picker Outcome Test";
+const ENV_VAR = "PICKER_OUTCOME_TEST_API_KEY";
+const KEY_URL = "https://picker-outcome.invalid/key";
+
+const realFetch = globalThis.fetch;
+const realGetRequestAuth = credentials.getRequestAuth;
+const realCollapse = _rosterCollapse.collapse;
+
+function defineProvider(overrides: Partial<ProviderDefinition> = {}): ProviderDefinition {
+ return {
+ name: PROVIDER,
+ displayName: DISPLAY_NAME,
+ transport: "openai",
+ baseUrl: "https://picker-outcome.invalid",
+ apiPath: "/v1/chat/completions",
+ apiKeyEnvVar: ENV_VAR,
+ apiKeyDescription: "Offline test key",
+ apiKeyUrl: KEY_URL,
+ shortcuts: [],
+ legacyPrefixes: [],
+ modelDiscovery: { path: "/v1/models", format: "openai-models-list" },
+ createHandler: { kind: "none", reason: "virtual", note: "Offline test fixture" },
+ isDirectApi: true,
+ ...overrides,
+ };
+}
+
+/** The provider's live endpoint answers with exactly these ids. */
+function serves(ids: string[]): void {
+ globalThis.fetch = mock(
+ async () =>
+ new Response(JSON.stringify({ data: ids.map((id) => ({ id })) }), {
+ status: 200,
+ headers: { "Content-Type": "application/json" },
+ })
+ ) as unknown as typeof fetch;
+}
+
+function rejectsWith(status: number): void {
+ globalThis.fetch = mock(
+ async () => new Response(JSON.stringify({ error: "rejected" }), { status })
+ ) as unknown as typeof fetch;
+}
+
+/** A CatalogClient that answers from memory. `null` rows means "catalog fails". */
+function stubCatalog(rows: CatalogModel[] | null): CatalogClient {
+ return {
+ async modelsByVendor() {
+ if (rows === null) throw new Error("catalog unavailable");
+ return rows;
+ },
+ async vendorsForModel() {
+ return null;
+ },
+ async searchModels() {
+ return [];
+ },
+ servedByVendor() {
+ return rows === null ? [] : rows;
+ },
+ };
+}
+
+const CATALOG_ROWS: CatalogModel[] = [
+ { modelId: "catalog-chat-1", displayName: "Catalog Chat 1", provider: "moonshotai" },
+ { modelId: "catalog-chat-2", displayName: "Catalog Chat 2", provider: "moonshotai" },
+];
+
+beforeEach(() => {
+ invalidateModelDiscovery();
+ clearRuntimeRegistry();
+ registerRuntimeProvider(defineProvider());
+ credentials.getRequestAuth = mock(async () => ({
+ headers: { Authorization: "Bearer offline-test-token" },
+ }));
+ globalThis.fetch = mock(async () => {
+ throw new Error("Unexpected fetch call");
+ }) as unknown as typeof fetch;
+});
+
+afterEach(() => {
+ invalidateModelDiscovery();
+ clearRuntimeRegistry();
+ globalThis.fetch = realFetch;
+ credentials.getRequestAuth = realGetRequestAuth;
+ _rosterCollapse.collapse = realCollapse;
+});
+
+describe("buildDiscoveredModelOutcome — all five states, told apart", () => {
+ test("STATE A · a healthy roster is `rows`, non-empty, with servedCount === chatCount", async () => {
+ serves(["alpha-chat", "beta-chat", "gamma-chat"]);
+
+ const outcome = await buildDiscoveredModelOutcome(
+ PROVIDER,
+ DISPLAY_NAME,
+ stubCatalog(CATALOG_ROWS)
+ );
+
+ expect(outcome.kind).toBe("rows");
+ if (outcome.kind !== "rows") throw new Error("unreachable");
+ expect(outcome.rows.map((r) => r.id).sort()).toEqual(["alpha-chat", "beta-chat", "gamma-chat"]);
+ expect(outcome.rows.length).toBeGreaterThan(0);
+ expect(outcome.servedCount).toBe(3);
+ expect(outcome.chatCount).toBe(3);
+ });
+
+ test("a PARTIALLY filtered roster keeps the two counts apart — the fact discarded today", async () => {
+ serves(["alpha-chat", "text-embedding-3-large", "whisper-1"]);
+
+ const outcome = await buildDiscoveredModelOutcome(
+ PROVIDER,
+ DISPLAY_NAME,
+ stubCatalog(CATALOG_ROWS)
+ );
+
+ expect(outcome.kind).toBe("rows");
+ if (outcome.kind !== "rows") throw new Error("unreachable");
+ expect(outcome.rows.map((r) => r.id)).toEqual(["alpha-chat"]);
+ expect(outcome.servedCount).toBe(3);
+ expect(outcome.chatCount).toBe(1);
+ expect(outcome.chatCount).toBeLessThan(outcome.servedCount);
+ });
+
+ test("STATE B · served N, none chat-capable → all-filtered with the count and ≤3 sample ids", async () => {
+ // Invisible today: the provider is healthy, the key is fine, and the picker
+ // silently shows the cloud catalog instead. The sample ids are what make the
+ // notice self-explaining — they are usually embeddings or route wildcards.
+ serves(["text-embedding-3-large", "whisper-1", "dall-e-3", "nomic-embed-text"]);
+
+ const outcome = await buildDiscoveredModelOutcome(
+ PROVIDER,
+ DISPLAY_NAME,
+ stubCatalog(CATALOG_ROWS)
+ );
+
+ expect(outcome.kind).toBe("all-filtered");
+ if (outcome.kind !== "all-filtered") throw new Error("unreachable");
+ expect(outcome.servedCount).toBe(4);
+ expect(outcome.sampleIds.length).toBe(3);
+ for (const id of outcome.sampleIds) {
+ expect(["text-embedding-3-large", "whisper-1", "dall-e-3", "nomic-embed-text"]).toContain(id);
+ }
+ expect(outcome.fallbackRows.map((r) => r.id).sort()).toEqual([
+ "catalog-chat-1",
+ "catalog-chat-2",
+ ]);
+ });
+
+ test("STATE C · a genuinely empty roster is its own variant, carrying the failure", async () => {
+ serves([]);
+
+ const outcome = await buildDiscoveredModelOutcome(
+ PROVIDER,
+ DISPLAY_NAME,
+ stubCatalog(CATALOG_ROWS)
+ );
+
+ expect(outcome.kind).toBe("empty-roster");
+ if (outcome.kind !== "empty-roster") throw new Error("unreachable");
- expect(outcome.failure.kind).toBe("empty-roster");
++ expect(outcome.failure.kind).toBe("empty-models-catalog");
+ expect(outcome.failure.endpoint).toContain("/v1/models");
+ expect(outcome.fallbackRows.length).toBe(2);
+ });
+
+ test("STATE D · a rejected key is `failed` — a DIFFERENT variant from empty-roster", async () => {
+ rejectsWith(401);
+
+ const outcome = await buildDiscoveredModelOutcome(
+ PROVIDER,
+ DISPLAY_NAME,
+ stubCatalog(CATALOG_ROWS)
+ );
+
+ expect(outcome.kind).toBe("failed");
+ if (outcome.kind !== "failed") throw new Error("unreachable");
+ expect(outcome.failure.kind).toBe("unauthorized");
+ expect(outcome.failure.status).toBe(401);
+ // The whole point of the split: a 401 and an empty list are not the same
+ // thing, and the type no longer lets a renderer treat them as one.
+ expect(outcome.kind).not.toBe("empty-roster");
+ expect(outcome.notice.join("")).toContain(DISPLAY_NAME);
+ expect(outcome.notice.join("")).toContain(ENV_VAR);
+ expect(outcome.fallbackRows.map((r) => r.id).sort()).toEqual([
+ "catalog-chat-1",
+ "catalog-chat-2",
+ ]);
+ });
+
+ test("STATE E · a roster that collapses to nothing is collapsed-empty, never rows:[]", async () => {
+ // Unreachable through the one shipped resolver (every entry lands in a group
+ // and every group yields a choice), and modelled anyway: `{kind:"rows",
+ // rows: []}` would be an empty panel with no explanation — the exact defect
+ // class this type exists to remove, reintroduced inside it.
+ _rosterCollapse.collapse = () => [];
+ serves(["alpha-chat", "beta-chat"]);
+
+ const outcome = await buildDiscoveredModelOutcome(
+ PROVIDER,
+ DISPLAY_NAME,
+ stubCatalog(CATALOG_ROWS)
+ );
+
+ expect(outcome.kind).toBe("collapsed-empty");
+ if (outcome.kind !== "collapsed-empty") throw new Error("unreachable");
+ expect(outcome.servedCount).toBe(2);
+ expect(outcome.chatCount).toBe(2);
+ expect(outcome.fallbackRows.length).toBe(2);
+ });
+
+ test("a provider with no modelDiscovery is `unsupported`, which renders nothing", async () => {
+ clearRuntimeRegistry();
+ registerRuntimeProvider(defineProvider({ modelDiscovery: undefined }));
+
+ expect(
+ await buildDiscoveredModelOutcome(PROVIDER, DISPLAY_NAME, stubCatalog(CATALOG_ROWS))
+ ).toEqual({ kind: "unsupported", reason: "no-descriptor" });
+ });
+
+ test("`rows` is non-empty on every path that produces it", async () => {
+ // The invariant `rows: [ModelInfo, ...ModelInfo[]]` states at the type level;
+ // this asserts it at runtime across every shape that reaches the branch.
+ for (const ids of [["only-one"], ["a-chat", "b-chat"], ["a-chat", "whisper-1"]]) {
+ invalidateModelDiscovery();
+ serves(ids);
+ const outcome = await buildDiscoveredModelOutcome(
+ PROVIDER,
+ DISPLAY_NAME,
+ stubCatalog(CATALOG_ROWS)
+ );
+ expect(outcome.kind).toBe("rows");
+ if (outcome.kind !== "rows") throw new Error("unreachable");
+ expect(outcome.rows.length).toBeGreaterThan(0);
+ }
+ });
+});
+
+describe("buildDiscoveredModelOutcome — the fallback leg", () => {
+ test("a failing catalog leaves fallbackRows empty instead of rejecting", async () => {
+ rejectsWith(500);
+
+ const outcome = await buildDiscoveredModelOutcome(PROVIDER, DISPLAY_NAME, stubCatalog(null));
+
+ expect(outcome.kind).toBe("failed");
+ if (outcome.kind !== "failed") throw new Error("unreachable");
+ expect(outcome.fallbackRows).toEqual([]);
+ // With nothing to show, "manual model entry" is finally the true sentence.
+ expect(outcome.notice.join("")).toContain("Falling back to manual model entry.");
+ });
+
+ test("with catalog rows in hand the notice names the catalog, not manual entry", async () => {
+ rejectsWith(401);
+
+ const outcome = await buildDiscoveredModelOutcome(
+ PROVIDER,
+ DISPLAY_NAME,
+ stubCatalog(CATALOG_ROWS)
+ );
+
+ if (outcome.kind !== "failed") throw new Error("unreachable");
+ const notice = outcome.notice.join("");
+ expect(notice).toContain("cloud-catalog entries below — not its live roster.");
+ expect(notice).not.toContain("Falling back to manual model entry.");
+ });
+
+ test("the catalog fallback is chat-filtered too, via the same chokepoint", async () => {
+ rejectsWith(401);
+
+ const outcome = await buildDiscoveredModelOutcome(
+ PROVIDER,
+ DISPLAY_NAME,
+ stubCatalog([
+ ...CATALOG_ROWS,
+ { modelId: "gpt-image-2.5-flare", displayName: "Image", provider: "openai" },
+ ])
+ );
+
+ if (outcome.kind !== "failed") throw new Error("unreachable");
+ expect(outcome.fallbackRows.map((r) => r.id)).not.toContain("gpt-image-2.5-flare");
+ });
+});
+
+describe("buildDiscoveredModelRows stays the wrapper the classic path uses", () => {
+ test("rows on success, [] for every other outcome", async () => {
+ serves(["alpha-chat"]);
+ expect(
+ (await buildDiscoveredModelRows(PROVIDER, DISPLAY_NAME, stubCatalog(CATALOG_ROWS))).map(
+ (r) => r.id
+ )
+ ).toEqual(["alpha-chat"]);
+
+ invalidateModelDiscovery();
+ rejectsWith(401);
+ expect(
+ await buildDiscoveredModelRows(PROVIDER, DISPLAY_NAME, stubCatalog(CATALOG_ROWS))
+ ).toEqual([]);
+ });
+
+ test("the failure is still RECORDED, so warnDiscoveryFailure can find it afterwards", async () => {
+ // The classic path throws the outcome away and asks the module-global map
+ // why the list was empty. Moving the classification must not break that.
+ rejectsWith(401);
+ await buildDiscoveredModelRows(PROVIDER, DISPLAY_NAME, stubCatalog(CATALOG_ROWS));
+
+ const stderr = spyOn(process.stderr, "write").mockImplementation(() => true);
+ try {
+ warnDiscoveryFailure(PROVIDER, DISPLAY_NAME, {
+ apiKeyEnvVar: ENV_VAR,
+ apiKeyUrl: KEY_URL,
+ });
+ expect(stderr.mock.calls.length).toBeGreaterThan(0);
+ expect(stderr.mock.calls.map(([c]) => String(c)).join("")).toContain("HTTP 401");
+ } finally {
+ stderr.mockRestore();
+ }
+ });
+});
+
+describe("formatDiscoveryFailureNotice", () => {
+ const def = { apiKeyEnvVar: ENV_VAR, apiKeyUrl: KEY_URL };
+
+ function failure(kind: DiscoveryFailureKind): DiscoveryFailure {
+ return {
+ kind,
+ provider: PROVIDER,
+ endpoint: "https://picker-outcome.invalid/v1/models",
+ status: kind === "unauthorized" || kind === "http-error" ? 401 : undefined,
+ detail: "upstream said no",
+ };
+ }
+
+ test.each(["unauthorized", "no-credentials"] as const)(
+ "%s earns the env var and the key URL — the two actionable kinds",
+ (kind) => {
+ const text = formatDiscoveryFailureNotice(DISPLAY_NAME, failure(kind), def).join("");
+
+ expect(text).toContain(DISPLAY_NAME);
+ expect(text).toContain(ENV_VAR);
+ expect(text).toContain(KEY_URL);
+ expect(text).toContain("Falling back to manual model entry.");
+ }
+ );
+
+ test.each(["http-error", "unreachable", "malformed"] as const)(
+ "%s omits credential guidance, because there is no credential step to take",
+ (kind) => {
+ const text = formatDiscoveryFailureNotice(DISPLAY_NAME, failure(kind), def).join("");
+
+ expect(text).toContain(DISPLAY_NAME);
+ expect(text).not.toContain(ENV_VAR);
+ expect(text).not.toContain(KEY_URL);
+ expect(text).toContain("Falling back to manual model entry.");
+ }
+ );
+
+ test("the three fallbacks produce three different final lines", () => {
+ const lines = (f: Parameters<typeof formatDiscoveryFailureNotice>[3]) =>
+ formatDiscoveryFailureNotice(DISPLAY_NAME, failure("unauthorized"), def, f).join("");
+
+ expect(lines("manual-entry")).toContain("Falling back to manual model entry.");
+ expect(lines("catalog")).toContain(
+ `Showing ${DISPLAY_NAME}'s cloud-catalog entries below — not its live roster.`
+ );
+ // "unknown" means nothing has decided yet, so it claims nothing.
+ expect(lines("unknown")).not.toContain("Falling back");
+ expect(lines("unknown")).not.toContain("cloud-catalog");
+ });
+
+ test("the default is byte-identical to what warnDiscoveryFailure writes to stderr", async () => {
+ // The coupling between the formatter and the sink, asserted rather than
+ // assumed: the classic path's wording must not drift when the TUI's does.
+ rejectsWith(401);
+ await buildDiscoveredModelRows(PROVIDER, DISPLAY_NAME, stubCatalog(null));
+
+ const stderr = spyOn(process.stderr, "write").mockImplementation(() => true);
+ let written: string;
+ try {
+ warnDiscoveryFailure(PROVIDER, DISPLAY_NAME, def);
+ written = stderr.mock.calls.map(([chunk]) => String(chunk)).join("");
+ } finally {
+ stderr.mockRestore();
+ }
+
+ const recorded: DiscoveryFailure = {
+ kind: "unauthorized",
+ provider: PROVIDER,
+ endpoint: "https://picker-outcome.invalid/v1/models",
+ status: 401,
+ detail: '{"error":"rejected"}',
+ };
+ expect(written).toBe(formatDiscoveryFailureNotice(DISPLAY_NAME, recorded, def).join(""));
+ });
+});
+
+describe("no rendered notice interpolates `undefined`", () => {
- const KINDS: DiscoveryFailureKind[] = [
- "no-credentials",
- "unauthorized",
- "http-error",
- "unreachable",
- "malformed",
- "empty-roster",
- ];
++ // A Record, not an array, so the COMPILER enforces "every kind": adding a
++ // kind to `DiscoveryFailureKind` without listing it here fails typecheck. An
++ // array did not — `incomplete` arrived in the union and this list, which
++ // calls itself exhaustive, kept passing without it.
++ const EVERY_KIND: Record<DiscoveryFailureKind, true> = {
++ "no-credentials": true,
++ unauthorized: true,
++ "http-error": true,
++ unreachable: true,
++ malformed: true,
++ incomplete: true,
++ "empty-models-catalog": true,
++ };
++ const KINDS = Object.keys(EVERY_KIND) as DiscoveryFailureKind[];
+
+ test("every kind × endpoint × status × detail × fallback", () => {
+ // The `empty-roster` records on the fetcher half carried no endpoint for
+ // years, so any copy that names one renders the literal string `undefined`
+ // to the user. Exhaustive rather than representative, because the optional
+ // fields are independent and a single missed guard is invisible in review.
+ let checked = 0;
+ for (const kind of KINDS) {
+ for (const endpoint of [undefined, "https://picker-outcome.invalid/v1/models"]) {
+ for (const status of [undefined, 401, 500]) {
+ for (const detail of [undefined, "", "upstream said no"]) {
+ const f: DiscoveryFailure = { kind, provider: PROVIDER, endpoint, status, detail };
+
+ expect(describeDiscoveryFailure(f)).not.toContain("undefined");
+
+ for (const fallback of ["manual-entry", "catalog", "unknown"] as const) {
+ for (const def of [
+ { apiKeyEnvVar: ENV_VAR, apiKeyUrl: KEY_URL },
+ { apiKeyEnvVar: "", apiKeyUrl: "" },
+ ]) {
+ const text = formatDiscoveryFailureNotice(DISPLAY_NAME, f, def, fallback).join("");
+ expect(text).not.toContain("undefined");
+ checked++;
+ }
+ }
+ }
+ }
+ }
+ }
+ expect(checked).toBe(KINDS.length * 2 * 3 * 3 * 3 * 2);
+ });
+});
+
+describe("toPickerRows — one chat-capability chokepoint for both doors", () => {
+ const row = (id: string): ModelInfo => ({
+ id,
+ name: id,
+ description: id,
+ provider: "test",
+ });
+
+ test("drops real non-chat ids", () => {
+ // `gpt-image-2.5-flare` is the model F1 observed being selected by pressing
+ // Enter twice at launch: the catalog paths applied no capability filter at
+ // all, only the live-discovery path did.
+ const dropped = [
+ "gpt-image-2.5-flare",
+ "text-embedding-3-large",
+ "whisper-1",
+ "dall-e-3",
+ "tts-1-hd",
+ "nomic-embed-text",
+ "bge-m3",
+ "gemini/*",
+ ];
+
+ expect(toPickerRows(dropped.map(row))).toEqual([]);
+ });
+
+ test("keeps real chat ids", () => {
+ const kept = [
+ "claude-opus-5",
+ "gpt-5.2-codex",
+ "gemini-3-pro",
+ "kimi-k3",
+ "deepseek-v3",
+ "llama3.2",
+ "MiniMax-M2",
+ ];
+
+ expect(
+ toPickerRows(kept.map(row))
+ .map((r) => r.id)
+ .sort()
+ ).toEqual([...kept].sort());
+ });
+
+ test("dedupes by id while filtering", () => {
+ expect(
+ toPickerRows([row("claude-opus-5"), row("claude-opus-5"), row("whisper-1")]).map((r) => r.id)
+ ).toEqual(["claude-opus-5"]);
+ });
+});
diff --cc packages/cli/src/model-selector.ts
index 19778bc3,0527ba12..b6f00843
--- a/packages/cli/src/model-selector.ts
+++ b/packages/cli/src/model-selector.ts
@@@ -32,16 -32,14 +33,16 @@@ import
} from "./providers/model-catalog.js";
import {
type DiscoveredModel,
+ type DiscoveryFailure,
+ type RosterOutcome,
describeDiscoveryFailure,
- discoverProviderModels,
+ discoverProviderRoster,
getDiscoveryFailure,
rankDiscoveredModels,
- toRosterEntry,
+ toModelsCatalogEntry,
} from "./providers/model-discovery.js";
import { compareByReleaseDateDesc } from "./providers/model-ordering.js";
- import { collapseRoster } from "./providers/model-resolvers/registry.js";
+ import { collapseModelsCatalog } from "./providers/model-resolvers/registry.js";
import { type ModelOffer, offerIsLive } from "./providers/model-resolvers/types.js";
import { PROVIDER_FILTER_ALIAS_EXTRA } from "./providers/picker-alias-extra.js";
import {
@@@ -1556,15 -1297,9 +1565,15 @@@ export function formatDiscoveryFailureN
* inquirer on stdout, and a model spec is what this module ultimately RETURNS
* — a notice must not be capturable as part of a piped answer.
*
- * `empty-roster` is deliberately silent: an endpoint that answers correctly
+ * `empty-models-catalog` is deliberately silent: an endpoint that answers correctly
* with nothing chat-capable is not an error, and the free-text prompt that
* follows is the right affordance for it. Only actionable failures are named.
+ * (That judgement is right for stderr, where a notice scrolls past; a rendered
+ * panel can afford to say it in one dim row, and the picker does.)
+ *
+ * Reads the module-global failure map rather than taking a failure, because its
+ * call site has already thrown the outcome away. New callers should prefer
+ * `buildDiscoveredModelOutcome`, which carries the failure with it.
*
* Exported for unit tests.
*/
@@@ -1574,161 -1309,27 +1583,163 @@@ export function warnDiscoveryFailure
def: Pick<ProviderDefinition, "apiKeyEnvVar" | "apiKeyUrl">
): void {
const failure = getDiscoveryFailure(provider);
- if (!failure || failure.kind === "empty-roster") return;
+ if (!failure || failure.kind === "empty-models-catalog") return;
- process.stderr.write(
- `\n⚠ ${displayName} could not list its models: ${describeDiscoveryFailure(failure)}\n`
- );
+ // Default `fallback`, i.e. "Falling back to manual model entry." — which is
+ // not always what happens next (the catalog usually answers), but this call
+ // site runs before that is decided and cannot know. Correcting the string is
+ // a follow-up with a named trigger: when `selectModelClassic` is deleted,
+ // this function and the assertion on that sentence go with it.
+ for (const line of formatDiscoveryFailureNotice(displayName, failure, def)) {
+ process.stderr.write(line);
+ }
+}
- // A credential problem is the one case with a concrete next step, so name the
- // exact env var and where a key comes from. Naming the variable matters more
- // than it looks: a provider's key is frequently shadowed by a stale value in
- // the shell, and "check your API key" gives no clue which name to inspect.
- if (failure.kind === "unauthorized" || failure.kind === "no-credentials") {
- if (def.apiKeyEnvVar) {
- process.stderr.write(
- ` Check ${def.apiKeyEnvVar} (a value in your shell overrides stored credentials).\n`
- );
+/**
+ * Roster → picker rows, as an injectable step.
+ *
- * A seam, not a design: `collapseRoster` folding a non-empty roster to `[]` is
++ * A seam, not a design: `collapseModelsCatalog` folding a non-empty roster to `[]` is
+ * the one state behind `PickerDiscoveryOutcome.collapsed-empty`, and the single
+ * shipped resolver cannot produce it (every entry lands in a group and every
+ * group yields a choice), so the variant is otherwise untestable. The
+ * alternative, `mock.module()` on the resolver registry, bleeds into sibling Bun
+ * test files and is banned here. Replace the property in a test and restore it;
+ * the same shape the discovery tests already use on `credentials.getRequestAuth`.
+ */
- export const _rosterCollapse = { collapse: collapseRoster };
++export const _rosterCollapse = { collapse: collapseModelsCatalog };
+
+/**
+ * What one discovery provider's model list actually is — all five states that
+ * used to be the same empty array, plus the one where nothing was attempted.
+ *
+ * `buildDiscoveredModelRows` returned `[]` for a rejected API key, an
+ * unreachable endpoint, a genuinely empty roster, a roster where nothing was
+ * chat-capable, and a roster that collapsed to nothing — and its caller fell
+ * through to the cloud catalog for all of them, silently. The user's report was
+ * *"it just shows fewer model names, like the provider does not have any
+ * models"*: information destroyed at a return statement, which no amount of UI
+ * can recover. Hence a discriminated union produced where the information still
+ * exists.
+ *
+ * - `rows` is **non-empty by construction** — the type says so, and the three
+ * empty cases below exist precisely so it can.
+ * - `servedCount` / `chatCount` make a PARTIAL filter visible too: today the
+ * difference between "served 40" and "40 of which 12 are chat models" is
+ * discarded.
+ * - `fallbackRows` rides along on the four recoverable variants, resolved
+ * CONCURRENTLY with discovery rather than after it (§3.6 of the design): a
+ * serial second fetch would put the feature's worst latency on exactly the
+ * path the complaint is about. Empty when the catalog has nothing either, in
+ * which case the only remaining affordance is free-text entry.
+ * - `unsupported` is not a failure. ~25 pickable providers declare no
+ * `modelDiscovery`; for them the catalog list is the normal, correct UI and a
+ * notice would be noise on the majority case.
+ */
+export type PickerDiscoveryOutcome =
+ | { kind: "rows"; rows: [ModelInfo, ...ModelInfo[]]; servedCount: number; chatCount: number }
+ | { kind: "all-filtered"; servedCount: number; sampleIds: string[]; fallbackRows: ModelInfo[] }
+ | { kind: "collapsed-empty"; servedCount: number; chatCount: number; fallbackRows: ModelInfo[] }
+ | { kind: "empty-roster"; failure: DiscoveryFailure; fallbackRows: ModelInfo[] }
+ | { kind: "failed"; failure: DiscoveryFailure; notice: string[]; fallbackRows: ModelInfo[] }
- | { kind: "unsupported"; reason: "no-descriptor" | "no-base-url" | "no-fetcher" };
++ | { kind: "unsupported"; reason: "no-descriptor" | "no-fetcher" };
+
+/**
+ * One discovery provider's list, with the reason when there isn't one.
+ *
+ * Starts both legs at once — the live roster and the cloud-catalog fallback —
+ * and returns one settled answer, so a view has one state and one affordance per
+ * provider instead of two spinners to reconcile. The catalog leg costs a lookup
+ * a healthy roster does not need; that is cheaper than doubling the wait on the
+ * failure path, and `catalog.modelsByVendor` caches.
+ */
+export async function buildDiscoveredModelOutcome(
+ provider: string,
+ displayName: string,
+ catalog: CatalogClient
+): Promise<PickerDiscoveryOutcome> {
+ const [rosterResult, fallbackResult] = await Promise.allSettled([
+ discoverProviderRoster(provider),
+ loadModelsForPickerProvider(provider, catalog),
+ ]);
+
+ const fallbackRows = fallbackResult.status === "fulfilled" ? fallbackResult.value : [];
+ // `discoverProviderRoster` never rejects by contract, but a contract is not a
+ // type: honour it rather than asserting it, so a future regression there shows
+ // up as a named failure instead of an unhandled rejection behind a renderer.
+ const roster: RosterOutcome =
+ rosterResult.status === "fulfilled"
+ ? rosterResult.value
+ : {
+ kind: "failed",
+ failure: {
+ kind: "unreachable",
+ provider,
+ detail: String((rosterResult.reason as Error)?.message ?? rosterResult.reason),
+ },
+ };
+
+ if (roster.kind === "unsupported") return { kind: "unsupported", reason: roster.reason };
+
+ if (roster.kind === "failed") {
+ const { failure } = roster;
+ // `empty-roster` is its own variant rather than a `failed` with a kind to
+ // switch on, because it is not an error: the endpoint answered. It renders
+ // in the NOTICE tier, visibly distinct from a rejected key.
- if (failure.kind === "empty-roster") return { kind: "empty-roster", failure, fallbackRows };
++ if (failure.kind === "empty-models-catalog") {
++ return { kind: "empty-roster", failure, fallbackRows };
+ }
- if (def.apiKeyUrl) process.stderr.write(` Get a key: ${def.apiKeyUrl}\n`);
+ const def = getProviderByName(provider);
+ const notice = formatDiscoveryFailureNotice(
+ displayName,
+ failure,
+ // A runtime custom endpoint may not be in the definition table at all;
+ // empty strings suppress the two guidance lines, which is right — there is
+ // no env var or key URL to name.
+ def ?? { apiKeyEnvVar: "", apiKeyUrl: "" },
+ fallbackRows.length > 0 ? "catalog" : "manual-entry"
+ );
+ return { kind: "failed", failure, notice, fallbackRows };
+ }
+
+ const served = rankDiscoveredModels(roster.models);
+ const servedCount = served.length;
+ const discovered = served.filter((m) => isChatCapable(m.id));
+ const chatCount = discovered.length;
+ if (chatCount === 0) {
+ // Served N, none of them chat-capable. Invisible today, and self-explaining
+ // only if the ids come with it — they are usually embeddings or wildcard
+ // routes, which a reader recognises at a glance.
+ return {
+ kind: "all-filtered",
+ servedCount,
+ sampleIds: served
+ .filter((m) => !isChatCapable(m.id))
+ .slice(0, 3)
+ .map((m) => m.id),
+ fallbackRows,
+ };
+ }
+
+ const [head, ...tail] = buildRowsFromDiscovered(provider, displayName, discovered, fallbackRows);
+ if (head === undefined) {
+ // Unreachable through the one shipped resolver, and deliberately still
+ // modelled: `{kind:"rows", rows: []}` would be an empty panel with no
+ // explanation, which is the exact defect class this type removes. Destructured
+ // rather than length-checked so the non-empty tuple needs no cast to assert.
+ return { kind: "collapsed-empty", servedCount, chatCount, fallbackRows };
}
- process.stderr.write(" Falling back to manual model entry.\n\n");
+ return { kind: "rows", rows: [head, ...tail], servedCount, chatCount };
}
+/**
+ * The picker rows for a provider's live roster, or `[]` for any of the six
+ * reasons there isn't one.
+ *
+ * A wrapper over {@link buildDiscoveredModelOutcome} since the outcome type
+ * landed. The classic inquirer path keeps using it and keeps asking
+ * `warnDiscoveryFailure` why the list was empty afterwards — which still works,
+ * because every failure is still recorded in `model-discovery.ts`'s map.
+ */
export async function buildDiscoveredModelRows(
provider: string,
displayName: string,
@@@ -1768,7 -1364,7 +1779,7 @@@ function buildRowsFromDiscovered
// where 167 served uids are ~39 real choices multiplied out by reasoning tier
// and speed premium. The chosen id is always a real wire id, so it still
// round-trips through buildExplicitModelSpec and argv unchanged.
- const choices = _rosterCollapse.collapse(provider, discovered.map(toRosterEntry));
- const choices = collapseModelsCatalog(provider, discovered.map(toModelsCatalogEntry));
++ const choices = _rosterCollapse.collapse(provider, discovered.map(toModelsCatalogEntry));
const discoveredById = new Map(discovered.map((m) => [m.id, m]));
// The live endpoint decides WHICH models appear (entitlement) and overrides
diff --cc packages/cli/src/picker/ModelPicker.test.tsx
index 818e6b4c,00000000..51a2a316
mode 100644,000000..100644
--- a/packages/cli/src/picker/ModelPicker.test.tsx
+++ b/packages/cli/src/picker/ModelPicker.test.tsx
@@@ -1,1541 -1,0 +1,1541 @@@
+import { describe, expect, test } from "bun:test";
+/** @jsxImportSource @opentui/react */
+/**
+ * Tier 2 — `testRender` over the real component tree, with the loaders injected.
+ *
+ * `renderOnce()` IS MANDATORY before every capture, or every cell comes back as
+ * unpainted filler and the assertion measures nothing.
+ *
+ * TWO CAPTURES, TWO BLINDNESSES. `captureCharFrame()` sees text and layout and is
+ * BLIND to a wash of coloured spaces and to a one-column stub of background left
+ * behind by a squeezed widget — a whole character-level suite once passed over a
+ * mangled row. `captureSpans()` sees colour, which is where the severity tiers and
+ * the selection wash live. Both are used here, for the things each can decide.
+ *
+ * THE ASSERTIONS THAT MATTER MOST ARE THE ONES ABOUT THE REJECTED BUILD. The owner's
+ * report was "super unclear what is happening"; the diagnosis was two panes each
+ * holding a cursor, and a meter on every row carrying a value that was identical on
+ * 19 of 32 rows. So: exactly ONE cursor glyph on screen, and NO block-fill glyph in
+ * any list row. Both are decidable from a frame, and both would have failed the
+ * build that shipped.
+ *
+ * AND THE ONES ABOUT THE ROUND-TWO CORRECTION, which are about what the picker does
+ * NOT do. `calls` counts every loader the fake is asked for, so "nothing is fetched
+ * before the user asks for it" is an assertion rather than a claim in a comment —
+ * and it is the one that would have caught the build the owner rejected, which
+ * looked correct and simply did too much.
+ *
+ * THE DATA SOURCE IS A PARAMETER, NEVER `mock.module()`: mocking shared
+ * infrastructure bleeds across Bun's module registry and breaks sibling e2e files.
+ */
+import type { CapturedFrame } from "@opentui/core";
+import { testRender } from "@opentui/react/test-utils";
+import type { ReactNode } from "react";
+import type { ModelInfo, PickerDiscoveryOutcome } from "../model-selector.js";
+import type { DescriptionIndex } from "../providers/model-descriptions.js";
+import { C } from "../tui/theme.js";
+import { ModelPicker } from "./ModelPicker.js";
+import type { PickerDataSource, PickerProviderChoice } from "./PickerDataSource.js";
+import { type Hint, hintsWidth } from "./chrome.js";
+import { MAX_DIALOG_ROWS, deriveDialogLayout } from "./layout.js";
+import { CHIP_FILL_CELLS } from "./rows.js";
+
+// ── a scriptable source ──────────────────────────────────────────────────────────
+
+const provider = (over: Partial<PickerProviderChoice> = {}): PickerProviderChoice => ({
+ value: "kimi",
+ label: "Kimi / Moonshot",
+ shortcut: "kimi@",
+ description: "Direct API",
+ billing: "metered",
+ envVar: "MOONSHOT_API_KEY",
+ hasDiscovery: true,
+ discoveryShape: "deadline",
+ ...over,
+});
+
+const model = (over: Partial<ModelInfo> = {}): ModelInfo => ({
+ id: "kimi-k3",
+ name: "kimi-k3",
+ description: "a model",
+ provider: "Kimi",
+ contextLength: 1_000_000,
+ context: "1M",
+ supportsTools: true,
+ pricing: { input: "$1", output: "$2", average: "$9.00/1M" },
+ ...over,
+});
+
+interface FakeOpts {
+ roster?: PickerProviderChoice[];
+ ready?: Record<string, boolean>;
+ outcome?: PickerDiscoveryOutcome;
+ /** Per-provider discovery outcomes. Falls back to `outcome`. */
+ outcomes?: Record<string, PickerDiscoveryOutcome>;
+ /** Per-provider served lists; falls back to `served`. */
+ byProvider?: Record<string, ModelInfo[]>;
+ served?: ModelInfo[];
+ /** `modelId` → prose sentence, as the description index answers. */
+ descriptions?: Record<string, string>;
+ /** Built-in local providers the profile config has not opted into. */
+ notEnabledLocal?: string[];
+ /** Never settles — the in-flight states. */
+ hang?: boolean;
+}
+
+/** Every loader the picker may reach for, counted. See the file header. */
+interface Calls {
+ catalog: number;
+ descriptions: number;
+ discover: string[];
+}
+
+function fakeSource(opts: FakeOpts = {}): PickerDataSource & { calls: Calls } {
+ const roster = opts.roster ?? [provider()];
+ const never = new Promise<never>(() => {});
+ const descriptions = opts.descriptions ?? {};
+ const calls: Calls = { catalog: 0, descriptions: 0, discover: [] };
+ return {
+ calls,
+ providerRoster: () => roster,
+ notEnabledLocalProviders: () => opts.notEnabledLocal ?? [],
+ displayName: (p) => roster.find((r) => r.value === p)?.label ?? p,
+ async *probeCredentials(names) {
+ if (opts.hang) await never;
+ for (const n of names) yield [n, opts.ready?.[n] ?? true] as [string, boolean];
+ },
+ ensureCatalog: (): Promise<void> => {
+ calls.catalog++;
+ return opts.hang ? never : Promise.resolve();
+ },
+ servedModels: (p) => opts.byProvider?.[p] ?? opts.served ?? [model()],
+ discoverRoster: (p): Promise<PickerDiscoveryOutcome> => {
+ calls.discover.push(p);
+ return opts.hang
+ ? never
+ : Promise.resolve(
+ opts.outcomes?.[p] ??
+ opts.outcome ?? { kind: "rows", rows: [model()], servedCount: 1, chatCount: 1 }
+ );
+ },
+ descriptions: (): Promise<DescriptionIndex> => {
+ calls.descriptions++;
+ return opts.hang
+ ? never
+ : Promise.resolve({
+ get: (id: string) => descriptions[id],
+ size: Object.keys(descriptions).length,
+ });
+ },
+ };
+}
+
+interface Drawn {
+ text: string[];
+ frame: CapturedFrame;
+ press: (keys: string[]) => Promise<void>;
+ /** Re-render until the frame satisfies `pred`, or give up and let the assert speak. */
+ until: (pred: (text: string[]) => boolean) => Promise<void>;
+ recapture: () => { text: string[]; frame: CapturedFrame };
+ destroy: () => void;
+}
+
+/** Always destroy in `finally`: a leaked renderer keeps native threads alive and hangs `bun test`. */
+async function draw(node: ReactNode, width = 80, height = 24, settleMs = 30): Promise<Drawn> {
+ const { renderOnce, captureCharFrame, captureSpans, renderer, mockInput } = await testRender(
+ node,
+ { width, height }
+ );
+ await renderOnce();
+ if (settleMs > 0) {
+ await new Promise((r) => setTimeout(r, settleMs));
+ await renderOnce();
+ }
+ const recapture = (): { text: string[]; frame: CapturedFrame } => ({
+ text: captureCharFrame().split("\n"),
+ frame: captureSpans(),
+ });
+ return {
+ ...recapture(),
+ press: async (keys) => {
+ await mockInput.pressKeys(keys as never);
+ await new Promise((r) => setTimeout(r, 20));
+ await renderOnce();
+ },
+ /**
+ * A FIXED SLEEP AFTER A KEYSTROKE IS A FLAKE, and this suite proved it: the
+ * keystroke that opens a list starts an effect, and on a loaded machine the
+ * 20 ms after it is not always enough for the effect, the state update and the
+ * next paint. Under `bun test` with 212 files in flight it failed once and
+ * passed alone, which is the worst signal there is. Polling for the CONDITION
+ * the test is about takes the load out of the assertion; the deadline is short
+ * enough that a genuine regression still fails in well under a second.
+ */
+ until: async (pred) => {
+ for (let i = 0; i < 40; i++) {
+ if (pred(captureCharFrame().split("\n"))) return;
+ await new Promise((r) => setTimeout(r, 25));
+ await renderOnce();
+ }
+ },
+ recapture,
+ destroy: () => renderer.destroy(),
+ };
+}
+
+const joined = (text: string[]): string => text.join("\n");
+const bgs = (f: CapturedFrame): Set<string> =>
+ new Set(f.lines.flatMap((l) => l.spans.map((s) => s.bg.toInts().slice(0, 3).join())));
+const rgb = (hex: string): string =>
+ [1, 3, 5].map((i) => Number.parseInt(hex.slice(i, i + 2), 16)).join();
+/**
+ * HAS A MODEL LIST FINISHED PAINTING? The filter row's `newest first` is drawn by
+ * the list phase and by nothing else — not by the loading dialog, not by the
+ * provider list — so it is the one string that separates "the view flipped" from
+ * "the view flipped and the rows are there".
+ *
+ * `▶` would not do: every list in this picker draws a cursor, including the one the
+ * keystroke just left, so a predicate on it is satisfied before anything has
+ * happened and waits for nothing.
+ */
+/** Every span in the frame, flattened — chips are spans, not rows. */
+const spansOf = (f: CapturedFrame): CapturedFrame["lines"][number]["spans"] =>
+ f.lines.flatMap((l) => l.spans);
+/** The spans whose LABEL is `label`, whatever fill they carry. */
+const labelled = (f: CapturedFrame, label: string): CapturedFrame["lines"][number]["spans"] =>
+ spansOf(f).filter((sp) => sp.text.trim() === label);
+const bgOf = (sp: CapturedFrame["lines"][number]["spans"][number]): string =>
+ sp.bg.toInts().slice(0, 3).join();
+const listPainted = (t: string[]): boolean => t.join("").includes("newest first");
+const providerListPainted = (t: string[]): boolean => t.join("").includes("choose a provider");
+/** Rows that carry painted content — the dialog's actual footprint. */
+const painted = (text: string[]): string[] => text.filter((l) => l.trim() !== "");
+/**
+ * The frame with its border glyphs and line breaks flattened away.
+ *
+ * A wrapped sentence is still ONE sentence to the reader, and an assertion that
+ * cannot see across a wrap would pass a build that had truncated the half that
+ * matters.
+ */
+const flat = (text: string[]): string =>
+ text
+ .join(" ")
+ .replace(/[│┃▌╭╮╰╯─]/g, " ")
+ .replace(/\s+/g, " ");
+
+// ── the shape of the thing ───────────────────────────────────────────────────────
+
+describe("the dialog", () => {
+ test("is COMPACT and INLINE — it does not fill a 45-row terminal", async () => {
+ // The whole correction, in one assertion. The rejected build was
+ // `height={terminalHeight}` on the alternate screen; this one is a box that
+ // leaves the user's shell on screen around it.
+ const d = await draw(<ModelPicker source={fakeSource()} onDone={() => {}} />, 145, 45);
+ try {
+ expect(painted(d.text).length).toBeLessThanOrEqual(20);
+ // …and it is horizontally bounded too, not a full-width strip.
+ expect(Math.max(...painted(d.text).map((l) => l.trimEnd().length))).toBeLessThan(145);
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("has EXACTLY ONE cursor — the direct answer to “unclear what is happening”", async () => {
+ // Two panes each held a cursor and only a border colour said which one the arrow
+ // keys drove. One list, one `▶` — on the landing screen and inside a list alike.
+ const roster = ["openrouter", "kimi", "google"].map((v) =>
+ provider({ value: v, label: v, shortcut: `${v}@`, hasDiscovery: false })
+ );
+ const d = await draw(
+ <ModelPicker
+ source={fakeSource({ roster, served: [model({ id: "a" }), model({ id: "b" })] })}
+ onDone={() => {}}
+ />
+ );
+ try {
+ expect(joined(d.text).split("▶").length - 1).toBe(1);
+ await d.press(["a"]);
+ await d.until(listPainted);
+ expect(joined(d.recapture().text).split("▶").length - 1).toBe(1);
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("NO list row carries a bar, a gradient or a block fill", async () => {
+ // The rejected build put a context meter and a price meter on every row because a
+ // whole-frame graphics-density gate counted a row with a bar as a graphics row.
+ // Of 32 visible rows, 19 read `1M` and their bars were indistinguishable.
+ const d = await draw(
+ <ModelPicker
+ source={fakeSource({
+ roster: [provider({ hasDiscovery: false })],
+ served: [
+ model({ id: "a", contextLength: 1_000_000, context: "1M" }),
+ model({ id: "b", contextLength: 8_000, context: "8K" }),
+ ],
+ })}
+ onDone={() => {}}
+ />
+ );
+ try {
+ await d.press(["a"]);
+ await d.until(listPainted);
+ const rows = d.recapture().text.filter((l) => /\ba\b|\bb\b/.test(l));
+ expect(rows.length).toBeGreaterThan(0);
+ for (const row of rows) expect(row).not.toMatch(/[█▓▒░╌]/);
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("no rendered row exceeds the frame width, at 80 and at 145", async () => {
+ for (const [width, height] of [
+ [80, 24],
+ [145, 45],
+ ] as const) {
+ const d = await draw(<ModelPicker source={fakeSource()} onDone={() => {}} />, width, height);
+ try {
+ expect({ width, over: d.text.filter((l) => l.length > width) }).toEqual({
+ width,
+ over: [],
+ });
+ await d.press(["a"]);
+ await d.until(listPainted);
+ expect({
+ width,
+ over: d.recapture().text.filter((l) => l.length > width),
+ }).toEqual({ width, over: [] });
+ } finally {
+ d.destroy();
+ }
+ }
+ });
+
+ test("the key hints are present and unclipped at 80×24, in BOTH lists", async () => {
+ const d = await draw(
+ <ModelPicker
+ source={fakeSource({ roster: [provider({ hasDiscovery: false })] })}
+ onDone={() => {}}
+ />
+ );
+ try {
+ // The hints row is the last PAINTED row above the bottom border.
+ const footer = (): string => painted(d.recapture().text).at(-2) ?? "";
+ // THE PROVIDER ROW DOES NOT SAY `move`, AND THAT IS THE ONE DELIBERATE
+ // DIFFERENCE BETWEEN THE TWO FOOTERS. Announcing `/ filter` cost 11 cells on a
+ // row that was already 70 against 72, and the arrows are the hint that repeats
+ // what every list in this program does. The budget half of that claim is the
+ // test below; this half is that the row still names every OTHER action.
+ for (const hint of ["open", "filter", "all models", "show", "custom", "quit"]) {
+ expect(footer()).toContain(hint);
+ }
+ expect(footer()).not.toContain("move");
+ await d.press(["a"]);
+ await d.until(listPainted);
+ for (const hint of ["move", "select", "filter", "custom", "back"]) {
+ expect(footer()).toContain(hint);
+ }
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("THE SIX-PILL FOOTER — the widest row any screen builds — is unclipped at 80", async () => {
+ // THE TEST ABOVE CANNOT CATCH THIS. It renders `hasDiscovery: false`, so `r
+ // retry` is absent and the row is FIVE pills. The row that actually overflowed is
+ // the six-pill one a DISCOVERY provider draws: it rendered `esc provider` at 80
+ // columns and nothing reported it, because `Hints` sets `overflow="hidden"` and a
+ // dropped cell is not an error. A screenshot found it, which is the wrong place
+ // to find arithmetic.
+ //
+ // The pill construction is what made the row expensive — each hint went from
+ // `key + label + 3` cells to `key + label + 4`, six of them — and the next hint
+ // anyone adds will cost the same.
+ const d = await draw(
+ <ModelPicker
+ source={fakeSource({ roster: [provider({ hasDiscovery: true })], served: [model()] })}
+ onDone={() => {}}
+ />
+ );
+ try {
+ await d.until(providerListPainted);
+ await d.press(["RETURN"]);
+ await d.until(listPainted);
+ const footer = painted(d.recapture().text).at(-2) ?? "";
+ // `r retry` proves this really is the six-pill row and not the five-pill one.
+ for (const hint of ["move", "select", "filter", "retry", "custom", "back"]) {
+ expect({ hint, footer, present: footer.includes(hint) }).toEqual({
+ hint,
+ footer,
+ present: true,
+ });
+ }
+ } finally {
+ d.destroy();
+ }
+
+ // AND THE BUDGET IS GENUINELY TIGHT — the half that stops the fit above from
+ // being a coincidence. These are the two labels the rows gave up to pay for the
+ // pills; each one, put back, overflows the dialog's inner width on its own.
+ const inner = deriveDialogLayout(80, 24).inner;
+ const withLabel = (hints: Hint[], key: string, label: string): Hint[] =>
+ hints.map((h) => (h.key === key ? { ...h, label } : h));
+ const providerList: Hint[] = [
+ { key: "⏎", label: "open" },
+ { key: "/", label: "filter" },
+ { key: "a", label: "all models" },
+ { key: "k", label: "show" },
+ { key: "c", label: "custom" },
+ { key: "esc", label: "quit" },
+ ];
+ const scopedList: Hint[] = [
+ { key: "↑↓", label: "move" },
+ { key: "⏎", label: "select" },
+ { key: "/", label: "filter" },
+ { key: "r", label: "retry" },
+ { key: "c", label: "custom" },
+ { key: "esc", label: "back" },
+ ];
+ expect({
+ providerRowFits: hintsWidth(providerList) <= inner,
+ // …in BOTH of its states: `esc` says `clear` while a filter is on, which is one
+ // cell more than `quit` and lands exactly on the budget.
+ providerRowFitsWhileFiltering: hintsWidth(withLabel(providerList, "esc", "clear")) <= inner,
+ scopedRowFits: hintsWidth(scopedList) <= inner,
+ needsKeyWouldNotFit: hintsWidth(withLabel(providerList, "k", "needs key")) > inner,
+ providersWouldNotFit: hintsWidth(withLabel(scopedList, "esc", "providers")) > inner,
+ // AND THE HINT THE PROVIDER ROW GAVE UP TO CARRY `/ filter` REALLY DOES NOT
+ // FIT. `↑↓ move` is on the model list's footer and not on this one; that asymmetry
+ // is a measurement, and this is the measurement.
+ arrowsWouldNotFit:
+ hintsWidth([{ key: "↑↓", label: "move" }, ...providerList]) > inner &&
+ hintsWidth([{ key: "↑↓", label: "move" }, ...withLabel(providerList, "esc", "clear")]) >
+ inner,
+ }).toEqual({
+ providerRowFits: true,
+ providerRowFitsWhileFiltering: true,
+ scopedRowFits: true,
+ needsKeyWouldNotFit: true,
+ providersWouldNotFit: true,
+ arrowsWouldNotFit: true,
+ });
+ });
+});
+
+// ── the chips: a fill is invisible to a character frame ──────────────────────────
+
+describe("the billing and prefix CHIPS", () => {
+ const roster = [
+ provider({ value: "cx", label: "OpenAI Codex", shortcut: "cx@", billing: "sub" }),
+ provider({ value: "or", label: "OpenRouter", shortcut: "or@", billing: "metered" }),
+ ];
+
+ test("THE PREFIX COLUMN IS NOT A BAND: a routing identifier carries no fill", async () => {
+ // THE REJECTED PASS CHIPPED IT, and `captureCharFrame` was blind to the result:
+ // every one of the 17 provider rows had a fill in the prefix cell, with no blank
+ // row between them, so the column fused into a solid grey vertical band with the
+ // labels floating inside. The owner's verdict was "that is super ugly". A fill
+ // marks a STATE; a prefix is an identifier with no states, so there is nothing
+ // in that column for a fill to mark. (The billing cell beside it is the opposite
+ // case and is chipped — see the two tests below.)
+ const d = await draw(<ModelPicker source={fakeSource({ roster })} onDone={() => {}} />);
+ try {
+ await d.until(providerListPainted);
+ const f = d.recapture().frame;
+ for (const token of ["cx@", "or@"]) {
+ const spans = spansOf(f).filter((sp) => sp.text.includes(token));
+ expect({ token, found: spans.length > 0 }).toEqual({ token, found: true });
+ for (const sp of spans) {
+ // The row's own background — the panel, or the selection wash on the row
+ // under the cursor — and never a fill of the cell's own.
+ expect({ token, bg: bgOf(sp) }).toEqual({
+ token,
+ bg: bgOf(sp) === rgb(C.bgHighlight) ? rgb(C.bgHighlight) : rgb(C.bgAlt),
+ });
+ }
+ }
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("`$$$` IS A CHIP TOO, ON THE COST FILL — and it is not the failure red", async () => {
+ // The owner's instruction, verbatim: "instead of $ it should be '$$$' with light
+ // reddish colour". The LABEL is the half a character frame can see; the FILL is
+ // the half it cannot, and the fill is where this goes wrong expensively —
+ // reddish is the hue the `HTTP 401` badge and the discovery banner already own,
+ // so a cost chip that picked one of those up would paint a failure on every
+ // healthy metered row. Hence both assertions, on the spans.
+ const d = await draw(<ModelPicker source={fakeSource({ roster })} onDone={() => {}} />);
+ try {
+ await d.until(providerListPainted);
+ const f = d.recapture().frame;
+ const dollars = labelled(f, "$$$");
+ expect(dollars.length).toBeGreaterThan(0);
+ // And the label it REPLACED is gone from the column rather than merely joined
+ // by the new one: a lone `$` chip surviving here is the half-applied edit.
+ expect(labelled(f, "$")).toEqual([]);
+ for (const chip of dollars) {
+ expect(bgOf(chip)).toBe(rgb(C.pillCostBg));
+ // Never the no-charge family's fill, and never a row background: a `$$$` that
+ // picked up `pillKeyBg` would claim the route costs nothing.
+ expect(bgOf(chip)).not.toBe(rgb(C.pillKeyBg));
+ expect(bgOf(chip)).not.toBe(rgb(C.bgAlt));
+ // NEVER THE FAILURE FILLS. `theme-contrast.test.ts` measures how far apart
+ // they have to be; this pins that they are not literally the same object,
+ // which is the version of the mistake a hurried edit actually makes.
+ expect(bgOf(chip)).not.toBe(rgb(C.red));
+ expect(bgOf(chip)).not.toBe(rgb(C.bgError));
+ expect(chip.fg.toInts().slice(0, 3).join()).toBe(rgb(C.pillCostFg));
+ }
+ expect(rgb(C.pillCostBg)).not.toBe(rgb(C.pillKeyBg));
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("`SUB` IS PAINTED ON THE POSITIVE FILL, AND NEVER ON THE FAILURE ONE", async () => {
+ // THE DEFECT THIS PINS SHIPPED, AND `captureCharFrame` IS BLIND TO IT. The frame
+ // reads `SUB` either way; only the FILL says whether the user is being told
+ // "you already pay for this" or "something went wrong". It was `tokens.warn` —
+ // `C.orange`, the hue this dialog spends on a failure — under a label that means
+ // the opposite of a failure.
+ const d = await draw(<ModelPicker source={fakeSource({ roster })} onDone={() => {}} />);
+ try {
+ await d.until(providerListPainted);
+ const chips = labelled(d.recapture().frame, "SUB");
+ expect(chips.length).toBeGreaterThan(0);
+ for (const chip of chips) {
+ expect(bgOf(chip)).toBe(rgb(C.pillKeyBg));
+ expect(bgOf(chip)).not.toBe(rgb(C.red));
+ expect(bgOf(chip)).not.toBe(rgb(C.orange));
+ }
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("EVERY STATUS CHIP IS THE SAME WIDTH, and the cell's surplus stays unfilled", async () => {
+ // ONE COLUMN, ONE FILL WIDTH — the owner asked for `$$$` and `SUB` at the same
+ // width, and a 1-cell fill beside a 5-cell one is what he was looking at. The
+ // label is centred INSIDE the fill, which is the one sanctioned place for that
+ // (`rows.tsx` header): a column wants straight edges, and the rule it bends was
+ // measured on a ROW of 24 identical chips.
+ //
+ // WHAT IS STILL FORBIDDEN, AND WHAT THIS STILL GUARDS: padding the fill out to
+ // the CELL. `label={padTo("SUB", 9)}` looks like the same thing and is not — the
+ // price cell is 9 columns and the billing cell 7, so a fill sized to the cell
+ // would paint two different widths and, in a column of one repeated word, one
+ // unbroken rectangle. The surplus is a plain filler span outside `bg`.
+ const d = await draw(<ModelPicker source={fakeSource({ roster })} onDone={() => {}} />);
+ try {
+ await d.until(providerListPainted);
+ const f = d.recapture().frame;
+ const status = spansOf(f).filter(
+ (sp) => bgOf(sp) === rgb(C.pillKeyBg) || bgOf(sp) === rgb(C.pillCostBg)
+ );
+ expect(status.length).toBeGreaterThan(0);
+ // Every fill in the column is CHIP_FILL_CELLS wide, whatever its label…
+ expect([...new Set(status.map((sp) => sp.text.length))]).toEqual([CHIP_FILL_CELLS]);
+ // …and the label is centred in it rather than padded to one side.
+ for (const sp of status) {
+ const label = sp.text.trim();
+ const slack = CHIP_FILL_CELLS - 2 - label.length;
+ const left = Math.floor(slack / 2);
+ expect({ text: sp.text }).toEqual({
+ text: `${" ".repeat(left + 1)}${label}${" ".repeat(slack - left + 1)}`,
+ });
+ }
+ // A footer keycap is a DIFFERENT object with the original contract: each of the
+ // pill's two segments is exactly `text + 2`, because a row of keys is not a
+ // column of states — nothing there has to line up with anything below it.
+ const caps = spansOf(f).filter(
+ (sp) => bgOf(sp) === rgb(C.keycapKeyBg) || bgOf(sp) === rgb(C.keycapLabelBg)
+ );
+ expect(caps.length).toBeGreaterThan(0);
+ for (const sp of caps) {
+ expect({ text: sp.text }).toEqual({ text: ` ${sp.text.trim()} ` });
+ }
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("A FOOTER KEYCAP IS A TWO-SEGMENT PILL — key filled, label filled, no gap", async () => {
+ // The owner asked for chips, twice. First the key was a grey word (1.50:1 on a
+ // dark page, 2.38:1 on a light one — invisible on whichever terminal it was not
+ // tuned for), then a lone purple block beside bare text. What he asked for is one
+ // pill of two halves: *"the key itself brighter colour and label has backdrop but
+ // not as bright"*.
+ //
+ // `captureCharFrame` reads ` esc quit ` under every one of those builds. Only
+ // the FILLS say whether it is a pill, and only their ADJACENCY says whether it is
+ // one object or two — which is why this asserts the two segments touch.
+ const d = await draw(<ModelPicker source={fakeSource({ roster })} onDone={() => {}} />);
+ try {
+ await d.until(providerListPainted);
+ const f = d.recapture().frame;
+ const caps = labelled(f, "esc");
+ expect(caps.length).toBeGreaterThan(0);
+ for (const cap of caps) {
+ expect(bgOf(cap)).toBe(rgb(C.keycapKeyBg));
+ expect(bgOf(cap)).not.toBe(rgb(C.chipKeyBg));
+ expect(cap.fg.toInts().slice(0, 3).join()).toBe(rgb(C.keycapKeyFg));
+ }
+ // THE SEAM. The label segment is the NEXT span after the key segment, with no
+ // unfilled span between them — a single space on the panel background there is
+ // what splits the pill into two objects, and it is invisible in a char frame.
+ const line = f.lines.find((l) => l.spans.some((sp) => sp.text.trim() === "esc"));
+ expect(line).toBeDefined();
+ const at = line!.spans.findIndex((sp) => sp.text.trim() === "esc");
+ const next = line!.spans[at + 1];
+ expect({ text: next?.text, bg: next === undefined ? null : bgOf(next) }).toEqual({
+ text: " quit ",
+ bg: rgb(C.keycapLabelBg),
+ });
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("NO CHIP COLUMN FUSES: a flat-rate roster prints words, not a green slab", async () => {
+ // A scoped subscription provider answers `SUB` for EVERY row, so a chipped price
+ // column there is the rectangle by construction — and no amount of correct
+ // padding prevents it, because the padding is not what makes the fills identical.
+ // `kimi-coding` is a real `SUBSCRIPTION_PROVIDERS` uid, so the price resolver
+ // answers `SUB` here exactly as it does on the live screen the owner was reading.
+ const d = await draw(
+ <ModelPicker
+ source={fakeSource({
+ roster: [
+ provider({
+ value: "kimi-coding",
+ label: "Kimi Coding",
+ shortcut: "kc@",
+ billing: "sub",
+ hasDiscovery: false,
+ }),
+ ],
+ served: Array.from({ length: 8 }, (_, i) => model({ id: `m-${i}` })),
+ byProvider: {
+ "kimi-coding": Array.from({ length: 8 }, (_, i) => model({ id: `m-${i}` })),
+ },
+ })}
+ onDone={() => {}}
+ />,
+ 145,
+ 45
+ );
+ try {
+ await d.press(["RETURN"]);
+ await d.until(listPainted);
+ const shot = d.recapture();
+ // Every row says SUB in words…
+ expect(shot.text.filter((l) => /\bm-\d\b/.test(l) && l.includes("SUB")).length).toBe(8);
+ // …and not one of them carries a FILL, so there is no column to fuse.
+ expect(spansOf(shot.frame).filter((sp) => bgOf(sp) === rgb(C.pillKeyBg))).toEqual([]);
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("INSIDE A PROVIDER THE PROVIDER COLUMN IS GONE, and the id takes its cells", async () => {
+ // `OpenAI Codex` printed 49 times under a dialog titled `OpenAI Codex`. The header
+ // must lose the label with the cells, or it points at a column that is not there.
+ const d = await draw(
+ <ModelPicker
+ source={fakeSource({
+ roster: [provider({ value: "openrouter", label: "OpenRouter", hasDiscovery: false })],
+ byProvider: { openrouter: [model({ id: "gpt-6-astra" })] },
+ })}
+ onDone={() => {}}
+ />,
+ 145,
+ 45
+ );
+ try {
+ await d.press(["RETURN"]);
+ await d.until(listPainted);
+ const scoped = d.recapture().text;
+ const header = scoped.find((l) => l.includes("ctx") && l.includes("$/1M")) ?? "";
+ expect(header).not.toContain("provider");
+ expect(scoped.filter((l) => /gpt-6-astra\s{2,}/.test(l) && l.includes("OpenRouter"))).toEqual(
+ []
+ );
+ // It is still the whole point of the CROSS-PROVIDER list, so it stays there.
+ await d.press(["ESCAPE"]);
+ await d.until(providerListPainted);
+ await d.press(["a"]);
+ await d.until(listPainted);
+ const flat = d.recapture().text;
+ expect(flat.find((l) => l.includes("ctx") && l.includes("$/1M")) ?? "").toContain("provider");
+ } finally {
+ d.destroy();
+ }
+ });
+});
+
+// ── the landing screen, and everything it does NOT do ────────────────────────────
+
+describe("the picker opens on the PROVIDER list", () => {
+ const roster = [
+ provider({
+ value: "openrouter",
+ label: "OpenRouter",
+ shortcut: "or@",
+ envVar: "OPENROUTER_API_KEY",
+ hasDiscovery: false,
+ }),
+ provider({ value: "kimi-coding", label: "Kimi Coding", shortcut: "kc@", hasDiscovery: true }),
+ ];
+
+ test("lands on providers — not on a 574-row list of every model", async () => {
+ // The owner, verbatim: "we should not show the full list of models, we should
+ // show a list of providers by default and only when we go inside we load and
+ // resolve all models".
+ const d = await draw(
+ <ModelPicker
+ source={fakeSource({ roster, byProvider: { openrouter: [model({ id: "glm-5.3" })] } })}
+ onDone={() => {}}
+ />
+ );
+ try {
+ const all = joined(d.text);
+ expect(all).toContain("choose a provider");
+ expect(all).toContain("OpenRouter");
+ // Not one model id, because nothing has been asked for one.
+ expect(all).not.toContain("glm-5.3");
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("FETCHES NOTHING before the user asks — no catalog, no roster, no descriptions", async () => {
+ // "why we prefetching? we should not, as we show on demand". The build this
+ // replaces warmed the cloud catalog AND fanned out one roster request per
+ // credentialled provider, on a screen that could show neither.
+ const source = fakeSource({ roster });
+ const d = await draw(<ModelPicker source={source} onDone={() => {}} />);
+ try {
+ expect(source.calls).toEqual({ catalog: 0, descriptions: 0, discover: [] });
+ // …and the screen does not claim to be doing any of it.
+ expect(joined(d.text)).not.toContain("cloud catalog");
+ expect(joined(d.text)).not.toContain("live rosters");
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("a provider with NO credential is off the list, and `k` reveals it WITH its reason", async () => {
+ // "we should not show unsetted providers, just active". Not deleted — reachable,
+ // because an unexplained absence is the defect class this feature exists to
+ // remove.
+ //
+ // THE SUMMARY LINE THAT USED TO COUNT THEM IS GONE, on the owner's instruction
+ // ("remove this, 5 lines which has no value"), and this test is where that has to
+ // be paid for: the whole explanation now lives on the REVEALED ROW, so `k` must
+ // still reach it and the row must still say why. A build that hid the count and
+ // also dropped the reason would have looked fine on a screenshot of the default
+ // state, which is the only state a screenshot was ever taken of.
+ const d = await draw(
+ <ModelPicker
+ source={fakeSource({ roster, ready: { openrouter: true, "kimi-coding": false } })}
+ onDone={() => {}}
+ />
+ );
+ try {
+ const before = joined(d.text);
+ expect(before).toContain("OpenRouter");
+ expect(before).not.toContain("Kimi Coding");
+ expect(before).not.toContain("MOONSHOT_API_KEY");
+ // And the deleted line is really deleted, in the state that used to print it.
+ expect(before).not.toContain("more need a key");
+
+ await d.press(["k"]);
+ const after = joined(d.recapture().text);
+ expect(after).toContain("Kimi Coding");
+ // Revealed WITH the exact variable to inspect, which is the whole value of
+ // showing them at all — and now the ONLY place that fact appears.
+ expect(after).toContain("MOONSHOT_API_KEY");
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("NO model count is printed for a provider nothing has counted yet", async () => {
+ // "we could not show number of models for some of them". A `0`, a `—` or a guess
+ // in that column is a claim; silence is not.
+ const d = await draw(<ModelPicker source={fakeSource({ roster })} onDone={() => {}} />);
+ try {
+ const row = d.text.find((l) => l.includes("OpenRouter")) ?? "";
+ expect(row).toContain("OpenRouter");
+ expect(row).not.toMatch(/\bmodels?\b/);
+ expect(row).not.toContain("—");
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("Escape on the landing screen CANCELS by returning null — never `process.exit`", async () => {
+ let got: string | null | undefined;
+ const d = await draw(
+ <ModelPicker
+ source={fakeSource({ roster })}
+ onDone={(spec) => {
+ got = spec;
+ }}
+ />
+ );
+ try {
+ await d.press(["ESCAPE"]);
+ expect(got).toBeNull();
+ } finally {
+ d.destroy();
+ }
+ });
+});
+
+// ── the two summary lines are gone, and their content moved onto the rows ───────
+
+describe("`k` reveals BOTH kinds of unavailable provider, each with its own reason", () => {
+ // The owner deleted the two lines under the list — *"remove this, 5 lines which has
+ // no value"* — and they were the only place the screen distinguished a provider
+ // missing a CREDENTIAL from a local one that is merely not ENABLED. The distinction
+ // is real (one sends you to find an API key, the other to a config flag), so it had
+ // to land somewhere: it is now each row's own tail.
+ const roster = [
+ provider({ value: "openrouter", label: "OpenRouter", shortcut: "or@", hasDiscovery: false }),
+ provider({ value: "kimi-coding", label: "Kimi Coding", shortcut: "kc@", hasDiscovery: false }),
+ provider({
+ value: "ollama",
+ label: "Ollama (local)",
+ shortcut: "ollama@",
+ billing: "local",
+ envVar: "",
+ hasDiscovery: true,
+ }),
+ ];
+
+ test("neither summary line is on the screen any more", async () => {
+ const d = await draw(
+ <ModelPicker
+ source={fakeSource({
+ roster,
+ ready: { openrouter: true, "kimi-coding": false, ollama: false },
+ notEnabledLocal: ["ollama"],
+ })}
+ onDone={() => {}}
+ />
+ );
+ try {
+ await d.until(providerListPainted);
+ const before = joined(d.recapture().text);
+ expect(before).not.toContain("more need a key");
+ expect(before).not.toContain("not enabled in your config");
+ // And the rows themselves are still hidden, which is what the lines described.
+ expect(before).not.toContain("Kimi Coding");
+ expect(before).not.toContain("Ollama");
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("and `k` brings both back, each saying WHY it cannot be used", async () => {
+ const d = await draw(
+ <ModelPicker
+ source={fakeSource({
+ roster,
+ ready: { openrouter: true, "kimi-coding": false, ollama: false },
+ notEnabledLocal: ["ollama"],
+ })}
+ onDone={() => {}}
+ />
+ );
+ try {
+ await d.until(providerListPainted);
+ await d.press(["k"]);
+ await d.until((t) => t.join("").includes("Ollama"));
+ const after = joined(d.recapture().text);
+ // A cloud provider names the credential — the actionable fact.
+ expect(after).toContain("Kimi Coding");
+ expect(after).toContain("MOONSHOT_API_KEY");
+ // A local one names the config, because nothing is missing: it is opt-in and
+ // has not been opted into. Sending this user to look for an API key would send
+ // him after something that does not exist.
+ expect(after).toContain("Ollama");
+ expect(after).toContain("not enabled in config");
+ expect(after).not.toContain("needs sign-in");
+ } finally {
+ d.destroy();
+ }
+ });
+});
+
+// ── the provider list filters, in the model list's own idiom ────────────────────
+
+describe("the PROVIDER list filters as you type", () => {
+ // The owner: *"we need inline search in list of providers as well"*. It was a
+ // deliberate omission — 17 named rows do not need narrowing — and the point of this
+ // block is that what was added is the SAME interaction as the model list, not a
+ // second one to learn.
+ const roster = [
+ provider({ value: "openrouter", label: "OpenRouter", shortcut: "or@", hasDiscovery: false }),
+ provider({ value: "kimi-coding", label: "Kimi Coding", shortcut: "kc@", hasDiscovery: false }),
+ provider({ value: "google", label: "Google Gemini", shortcut: "go@", hasDiscovery: false }),
+ ];
+
+ test("typing NARROWS the list, and the count on the right follows it", async () => {
+ const d = await draw(<ModelPicker source={fakeSource({ roster })} onDone={() => {}} />);
+ try {
+ await d.until(providerListPainted);
+ expect(joined(d.recapture().text)).toContain("OpenRouter");
+
+ // `im`, not `kimi`: `k` is a COMMAND while the filter is empty (see the test
+ // below), so a bare `k` would toggle the hidden providers instead of typing.
+ await d.press(["i", "m"]);
+ const after = joined(d.recapture().text);
+ expect(after).toContain("Kimi Coding");
+ expect(after).not.toContain("OpenRouter");
+ expect(after).not.toContain("Google Gemini");
+ // The same `matches of total` the model list prints, from the same component.
+ expect(after).toContain("1 of 3");
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("the SHORTCUT matches too, because `kc@` is what the row shows beside the name", async () => {
+ // Two vocabularies reach the same row — what it is called and how it is routed —
+ // and a user who knows the prefix should not have to remember the display name.
+ const d = await draw(<ModelPicker source={fakeSource({ roster })} onDone={() => {}} />);
+ try {
+ await d.until(providerListPainted);
+ // `/` first, because `kc@` starts with one of the three command letters — which
+ // is precisely the case the escape hatch exists for.
+ await d.press(["/"]);
+ await d.press(["k", "c"]);
+ const after = joined(d.recapture().text);
+ expect(after).toContain("Kimi Coding");
+ expect(after).not.toContain("OpenRouter");
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("`k` TYPES once the filter is live — it does not toggle the hidden providers", async () => {
+ // THE RULE THE MODEL LIST ALREADY HAD, AND THE REASON IT EXISTS. A letter that is
+ // a command while the filter is empty must become a letter once it is not, or
+ // narrowing to `kimi` fires `k` (reveal), `i`, `m`, `i` and the user watches the
+ // screen do something else entirely. `k` is the sharpest case here because its
+ // command is a VISIBLE toggle.
+ const d = await draw(
+ <ModelPicker
+ source={fakeSource({
+ roster,
+ ready: { openrouter: true, "kimi-coding": true, google: false },
+ })}
+ onDone={() => {}}
+ />
+ );
+ try {
+ await d.until(providerListPainted);
+ // First `k` is the command: the credential-less provider appears.
+ await d.press(["k"]);
+ expect(joined(d.recapture().text)).toContain("Google Gemini");
+ await d.press(["ESCAPE"]);
+ // `/` focuses the filter, so the NEXT `k` is a letter even though the filter is
+ // still empty — which is the whole point of the escape hatch.
+ await d.press(["/"]);
+ await d.press(["k"]);
+ const after = joined(d.recapture().text);
+ expect(after).toContain("Kimi Coding");
+ expect(after).not.toContain("OpenRouter");
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("`esc` CLEARS the filter first and only then quits — the model list's rule", async () => {
+ let got: string | null | undefined;
+ const d = await draw(
+ <ModelPicker
+ source={fakeSource({ roster })}
+ onDone={(spec) => {
+ got = spec;
+ }}
+ />
+ );
+ try {
+ await d.until(providerListPainted);
+ await d.press(["i", "m"]);
+ expect(joined(d.recapture().text)).not.toContain("OpenRouter");
+
+ await d.press(["ESCAPE"]);
+ await d.until((t) => t.join("").includes("OpenRouter"));
+ // Cleared, NOT cancelled: the whole list is back and the picker is still open.
+ expect(got).toBeUndefined();
+ const cleared = joined(d.recapture().text);
+ expect(cleared).toContain("OpenRouter");
+ expect(cleared).toContain("Google Gemini");
+
+ // And the second `esc`, with nothing to clear, is the landing view's cancel.
+ await d.press(["ESCAPE"]);
+ expect(got).toBeNull();
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("a filter that matches nothing SAYS SO, and says how to get out", async () => {
+ const d = await draw(<ModelPicker source={fakeSource({ roster })} onDone={() => {}} />);
+ try {
+ await d.until(providerListPainted);
+ await d.press(["z", "z"]);
+ const after = joined(d.recapture().text);
+ expect(after).toContain("no provider matches");
+ expect(after).toContain("esc clears the filter");
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("entering a provider does not carry the filter into its model list", async () => {
+ // One `filter` state serves both views, which is what keeps the two screens
+ // behaving identically — and it is only safe because every transition clears it.
+ // A leaked filter would open a provider onto `no model matches “kimi”`.
+ const d = await draw(
+ <ModelPicker
+ source={fakeSource({ roster, byProvider: { "kimi-coding": [model({ id: "kimi-k3" })] } })}
+ onDone={() => {}}
+ />
+ );
+ try {
+ await d.until(providerListPainted);
+ await d.press(["i", "m"]);
+ await d.press(["RETURN"]);
+ await d.until(listPainted);
+ const after = joined(d.recapture().text);
+ expect(after).toContain("kimi-k3");
+ expect(after).not.toContain("no model matches");
+ // `newest first` is the model list's idle caption — it is only drawn when the
+ // filter is empty, so its presence IS the assertion that nothing leaked.
+ expect(after).toContain("newest first");
+ } finally {
+ d.destroy();
+ }
+ });
+});
+
+// ── entering a provider is what fetches it ───────────────────────────────────────
+
+describe("entering a provider loads THAT provider, on demand", () => {
+ const roster = [
+ provider({ value: "openrouter", label: "OpenRouter", shortcut: "or@", hasDiscovery: false }),
+ provider({ value: "devin", label: "Devin", shortcut: "dv@", hasDiscovery: true }),
+ ];
+
+ test("`⏎` asks the provider under the cursor, and ONLY that one", async () => {
+ const source = fakeSource({
+ roster,
+ byProvider: { openrouter: [model({ id: "glm-5.3" })], devin: [] },
+ outcomes: {
+ devin: { kind: "rows", rows: [model({ id: "devin-1" })], servedCount: 1, chatCount: 1 },
+ },
+ });
+ const d = await draw(<ModelPicker source={source} onDone={() => {}} />);
+ try {
+ await d.press(["ARROW_DOWN"]);
+ await d.press(["RETURN"]);
+ await d.until(listPainted);
+ expect(source.calls.discover).toEqual(["devin"]);
+ const all = joined(d.recapture().text);
+ expect(all).toContain("Devin");
+ expect(all).toContain("devin-1");
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("inside one provider a model appears exactly ONCE", async () => {
+ // The owner's rule: "if we enter to provider catalog, not all models — then the
+ // model will be just one".
+ const d = await draw(
+ <ModelPicker
+ source={fakeSource({
+ roster: [provider({ value: "openrouter", label: "OpenRouter", hasDiscovery: false })],
+ byProvider: {
+ openrouter: [model({ id: "gpt-6-astra" }), model({ id: "gpt-6-astra" })],
+ },
+ })}
+ onDone={() => {}}
+ />,
+ 145,
+ 45
+ );
+ try {
+ await d.press(["RETURN"]);
+ await d.until(listPainted);
+ // ONE LIST ROW. The provider column is no longer drawn inside a provider — it
+ // said `OpenRouter` 49 times under a dialog titled `OpenRouter` — so the row is
+ // identified by its padded id cell instead: the id followed by the gap before
+ // `ctx`. The detail line below prints `openrouter@gpt-6-astra · tools`, one
+ // space and a `·`, and is not counted by that shape.
+ const rows = d.recapture().text.filter((l) => /gpt-6-astra\s{2,}/.test(l));
+ expect(rows.length).toBe(1);
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("`esc` comes back to the provider list rather than quitting", async () => {
+ let got: string | null | undefined = undefined;
+ const d = await draw(
+ <ModelPicker
+ source={fakeSource({ roster })}
+ onDone={(spec) => {
+ got = spec;
+ }}
+ />
+ );
+ try {
+ await d.press(["RETURN"]);
+ await d.until(listPainted);
+ await d.press(["ESCAPE"]);
+ // A LONE `\x1B` IS AMBIGUOUS UNTIL THE NEXT BYTE OR A TIMEOUT: the parser has
+ // to wait to see whether it is the head of `\x1B[B`. One more render pass
+ // after that timeout is what makes a bare Escape observable in a test — a
+ // real keyboard gets the same delay and nobody notices.
+ await d.press([]);
+ await d.until(providerListPainted);
+ expect(joined(d.recapture().text)).toContain("choose a provider");
+ expect(got).toBeUndefined();
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("Enter returns the provider-scoped spec, never a bare model id", async () => {
+ // A bare Claude-shaped name reaching `route()` degrades to OpenRouter, so the
+ // picker must never emit one.
+ let got: string | null | undefined;
+ const d = await draw(
+ <ModelPicker
+ source={fakeSource({
+ roster: [provider({ value: "openrouter", label: "OpenRouter", hasDiscovery: false })],
+ byProvider: { openrouter: [model({ id: "gpt-6" })] },
+ })}
+ onDone={(spec) => {
+ got = spec;
+ }}
+ />
+ );
+ try {
+ await d.press(["RETURN"]);
+ await d.until(listPainted);
+ await d.press(["RETURN"]);
+ // `openrouter@`, not the column's `or@`: `buildExplicitModelSpec` applies the
+ // readability OVERRIDES because this is the string the user copies off their
+ // screen, while a fixed-width column wants the short form. Both parse back to
+ // the same provider — `providerShortcut` records why they differ.
+ expect(got).toBe("openrouter@gpt-6");
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("typing filters immediately — there is no search mode to enter", async () => {
+ const d = await draw(
+ <ModelPicker
+ source={fakeSource({
+ roster: [provider({ value: "openrouter", label: "OpenRouter", hasDiscovery: false })],
+ byProvider: {
+ openrouter: [model({ id: "glm-5.3-flash" }), model({ id: "kimi-k3" })],
+ },
+ })}
+ onDone={() => {}}
+ />
+ );
+ try {
+ await d.press(["RETURN"]);
+ await d.until(listPainted);
+ await d.press(["g", "l", "m"]);
+ const all = joined(d.recapture().text);
+ expect(all).toContain("glm-5.3-flash");
+ expect(all).not.toContain("kimi-k3");
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("`/` makes the letter commands typeable — `codex` is a real model name", async () => {
+ // The one collision filter-first creates: `c` opens the custom-spec dialog, so
+ // the first keystroke of `codex` would navigate instead of filter. `/` is the
+ // escape hatch, and without a test it is the kind of thing that quietly stops
+ // working.
+ const source = (): PickerDataSource =>
+ fakeSource({
+ roster: [provider({ value: "openrouter", label: "OpenRouter", hasDiscovery: false })],
+ byProvider: { openrouter: [model({ id: "codex-2" }), model({ id: "kimi-k3" })] },
+ });
+ const d = await draw(<ModelPicker source={source()} onDone={() => {}} />);
+ try {
+ await d.press(["RETURN"]);
+ await d.until(listPainted);
+ // Bare `c` NAVIGATES — that is the collision, asserted so the escape hatch has
+ // something to be an escape from.
+ await d.press(["c"]);
+ expect(joined(d.recapture().text)).toContain("type a model spec");
+ d.destroy();
+ } finally {
+ /* destroyed above; the second half needs a fresh tree */
+ }
+ const e = await draw(<ModelPicker source={source()} onDone={() => {}} />);
+ try {
+ await e.press(["RETURN"]);
+ await e.until(listPainted);
+ await e.press(["/"]);
+ await e.press(["c"]);
+ await e.press(["o"]);
+ const all = joined(e.recapture().text);
+ expect(all).toContain("codex-2");
+ expect(all).not.toContain("kimi-k3");
+ } finally {
+ e.destroy();
+ }
+ });
+});
+
+// ── the cross-provider list, which is no longer the default ──────────────────────
+
+describe("`a` opens the cross-provider list", () => {
+ const roster = [
+ provider({ value: "openrouter", label: "OpenRouter", shortcut: "or@", hasDiscovery: false }),
+ provider({ value: "kimi-coding", label: "Kimi Coding", shortcut: "kc@", hasDiscovery: false }),
+ ];
+
+ test("it is ONE cached catalog fetch, not a fan-out across providers", async () => {
+ const source = fakeSource({
+ roster,
+ byProvider: {
+ openrouter: [model({ id: "glm-5.3-flash" })],
+ "kimi-coding": [model({ id: "kimi-k3" })],
+ },
+ });
+ const d = await draw(<ModelPicker source={source} onDone={() => {}} />);
+ try {
+ await d.press(["a"]);
+ await d.until(listPainted);
+ const all = joined(d.recapture().text);
+ expect(all).toContain("all models");
+ expect(all).toContain("glm-5.3-flash");
+ expect(all).toContain("kimi-k3");
+ // ONE warm. And not a single roster request: no provider was opened.
+ expect(source.calls.catalog).toBe(1);
+ expect(source.calls.discover).toEqual([]);
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("one model on two providers is TWO rows, and the title counts both honestly", async () => {
+ // The owner's rule for this list, and the count that goes with it: the rows are
+ // ROUTES, so labelling their number "models" would be off by exactly the amount
+ // that makes the list useful.
+ const d = await draw(
+ <ModelPicker
+ source={fakeSource({
+ roster,
+ byProvider: {
+ openrouter: [model({ id: "gpt-6-astra" })],
+ "kimi-coding": [model({ id: "gpt-6-astra" })],
+ },
+ })}
+ onDone={() => {}}
+ />,
+ 145,
+ 45
+ );
+ try {
+ await d.press(["a"]);
+ await d.until(listPainted);
+ const all = joined(d.recapture().text);
+ expect(all).toContain("1 models · 2 routes");
+ expect(all).toContain("OpenRouter");
+ expect(all).toContain("Kimi Coding");
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("a provider with no credential contributes NO rows, and the count says so", async () => {
+ const d = await draw(
+ <ModelPicker
+ source={fakeSource({
+ roster,
+ ready: { openrouter: true, "kimi-coding": false },
+ byProvider: {
+ openrouter: [model({ id: "glm-5.3-flash" })],
+ "kimi-coding": [model({ id: "kimi-k3" })],
+ },
+ })}
+ onDone={() => {}}
+ />
+ );
+ try {
+ await d.press(["a"]);
+ await d.until(listPainted);
+ const all = joined(d.recapture().text);
+ expect(all).toContain("glm-5.3-flash");
+ expect(all).not.toContain("kimi-k3");
+ // The absence is EXPLAINED, in the title, rather than silent.
+ expect(all).toContain("1/2 providers");
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("A ROSTER ALREADY OPENED IS MERGED IN — and one not opened costs no request", async () => {
+ // The `gemini` report — "why i search gemini i see only open router models, no
+ // models from antigravity and devin and subscriptions" — was first answered by
+ // fetching every roster at startup, which the owner then rejected as
+ // prefetching. This is the answer that survives both: a roster fetched because
+ // he OPENED that provider improves this list for free, and a provider he never
+ // opened is simply not represented here.
+ const withDiscovery = [
+ provider({ value: "openrouter", label: "OpenRouter", shortcut: "or@", hasDiscovery: false }),
+ provider({
+ value: "antigravity",
+ label: "Antigravity",
+ shortcut: "ag@",
+ billing: "sub",
+ envVar: "",
+ hasDiscovery: true,
+ }),
+ ];
+ const source = fakeSource({
+ roster: withDiscovery,
+ byProvider: {
+ openrouter: [model({ id: "gemini-3.8-flash" })],
+ // No catalog entries whatsoever — the real Antigravity's situation.
+ antigravity: [],
+ },
+ outcomes: {
+ antigravity: {
+ kind: "rows",
+ rows: [model({ id: "gemini-3.8-flash-tiered", context: "1M" })],
+ servedCount: 1,
+ chatCount: 1,
+ },
+ },
+ });
+ const d = await draw(<ModelPicker source={source} onDone={() => {}} />, 145, 45);
+ try {
+ // Open Antigravity, look at it, come back out, then ask for all models.
+ await d.press(["ARROW_DOWN"]);
+ await d.press(["RETURN"]);
+ await d.until(listPainted);
+ await d.press(["ESCAPE"]);
+ // The bare-Escape flush — see the `esc comes back` test. Without it the
+ // NEXT key is read as `\x1B` + `a`, which is alt-a and not two keystrokes.
+ await d.press([]);
+ await d.until(providerListPainted);
+ await d.press(["a"]);
+ await d.until(listPainted);
+ await d.press(["g", "e", "m", "i", "n", "i"]);
+ const all = joined(d.recapture().text);
+ // BOTH routes, from two different sources, in one filtered list.
+ expect(all).toContain("gemini-3.8-flash");
+ expect(all).toContain("gemini-3.8-flash-tiered");
+ expect(all).toContain("OpenRouter");
+ expect(all).toContain("Antigravity");
+ // Exactly one roster request, and it was the one he opened.
+ expect(source.calls.discover).toEqual(["antigravity"]);
+ } finally {
+ d.destroy();
+ }
+ });
+});
+
+// ── provenance: the user's actual complaint ──────────────────────────────────────
+
+describe("a fallback list says it is a fallback — and that it may not work", () => {
+ const failed: PickerDiscoveryOutcome = {
+ kind: "failed",
+ failure: {
+ kind: "unauthorized",
+ provider: "kimi",
+ status: 401,
+ endpoint: "https://api.moonshot.ai/v1/models",
+ },
+ notice: [
+ "\n⚠ Kimi / Moonshot could not list its models: the API key was rejected (HTTP 401)\n",
+ " Check MOONSHOT_API_KEY (a value in your shell overrides stored credentials).\n",
+ " Get a key: https://platform.moonshot.cn/\n",
+ " Showing Kimi / Moonshot's cloud-catalog entries below — not its live roster.\n\n",
+ ],
+ fallbackRows: [model(), model({ id: "kimi-k2.6" })],
+ };
+
+ /**
+ * Reach the scoped view: Enter on the first provider.
+ *
+ * ONE PRESS, and the failure it renders is now a failure the user ASKED for —
+ * which is the round-two improvement that came free with on-demand loading. The
+ * red panel, the `catalog` rows and `r retry` are unchanged; only the moment
+ * they appear is.
+ */
+ async function scoped(outcome: PickerDiscoveryOutcome, width = 80, height = 24): Promise<Drawn> {
+ const d = await draw(
+ <ModelPicker source={fakeSource({ outcome })} onDone={() => {}} />,
+ width,
+ height
+ );
+ await d.press(["RETURN"]);
+ await d.until(listPainted);
+ return { ...d, ...d.recapture() };
+ }
+
+ test("the title, the per-row mark and the banner all say it", async () => {
+ const d = await scoped(failed);
+ try {
+ const all = joined(d.text);
+ expect(all).toContain("live roster unavailable");
+ expect(all).toContain("catalog");
+ // The sentence WRAPS across two rows, so it is matched against the frame with
+ // its border glyphs and line breaks flattened away. A substring test over raw
+ // rows would fail on the wrap and say nothing about whether a reader can read
+ // it.
+ expect(flat(d.text)).toContain("not Kimi / Moonshot's live roster");
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("THE HONESTY LINE IS ON SCREEN AT 80 COLUMNS", async () => {
+ // Neither the shipped build nor the rejected one said this. Catalog rows are not
+ // merely differently-sourced: discovery failed on the credential, so nothing has
+ // confirmed the account can call any of them.
+ const d = await scoped(failed);
+ try {
+ expect(joined(d.text)).toContain("may still fail");
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("the banner names the provider, the cause, the env var and the key URL", async () => {
+ const d = await scoped(failed, 145, 45);
+ try {
+ const all = joined(d.text);
+ expect(all).toContain("Kimi / Moonshot");
+ expect(all).toContain("the API key was rejected");
+ expect(all).toContain("MOONSHOT_API_KEY");
+ expect(all).toContain("platform.moonshot.cn");
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("`r retry` is offered — the rejected build had no way back from a failure", async () => {
+ const d = await scoped(failed);
+ try {
+ expect(joined(d.text)).toContain("retry");
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("a LIVE roster carries no mark — which is what makes the mark a signal", async () => {
+ const d = await scoped({
+ kind: "rows",
+ rows: [model()],
+ servedCount: 1,
+ chatCount: 1,
+ });
+ try {
+ expect(joined(d.text)).not.toContain("catalog");
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("a banner NEVER makes the dialog taller — its rows come out of the list's", async () => {
+ // The inline-mode bound. `main-screen` reserves rows by scrolling the user's
+ // terminal, so the dialog is allowed to shrink below its cap (a four-row
+ // fallback list should look like four rows) but never to exceed it.
+ const withBanner = await scoped(failed);
+ try {
+ expect(painted(withBanner.text).length).toBeLessThanOrEqual(MAX_DIALOG_ROWS);
+ } finally {
+ withBanner.destroy();
+ }
+ });
+
+ test("`empty-roster` renders in the NOTICE tier, without the error wash", async () => {
+ // `captureCharFrame` is blind to this: the two tiers differ in COLOUR as well as in
+ // words, and the colour is the half a reader sees first.
+ const failedFrame = await scoped(failed);
+ const failedBgs = bgs(failedFrame.frame);
+ failedFrame.destroy();
+ const emptyFrame = await scoped({
+ kind: "empty-roster",
- failure: { kind: "empty-roster", provider: "kimi", endpoint: "https://api.test/v1/models" },
++ failure: { kind: "empty-models-catalog", provider: "kimi", endpoint: "https://api.test/v1/models" },
+ fallbackRows: [model()],
+ });
+ try {
+ expect(failedBgs.has(rgb(C.bgError))).toBe(true);
+ expect(bgs(emptyFrame.frame).has(rgb(C.bgError))).toBe(false);
+ expect(joined(emptyFrame.text)).toContain("empty");
+ } finally {
+ emptyFrame.destroy();
+ }
+ });
+});
+
+// ── what the screen says while it is waiting ─────────────────────────────────────
+
+describe("the loading states", () => {
+ test("startup names the ONE thing it is doing, and claims no other", async () => {
+ // The screen the owner rejected said `cloud catalog fetching…` and `live rosters
+ // 0/11 providers` before he had asked for either. Startup now probes credentials
+ // and nothing else, and the screen says so.
+ const d = await draw(
+ <ModelPicker source={fakeSource({ hang: true })} onDone={() => {}} />,
+ 80,
+ 24,
+ 0
+ );
+ try {
+ const all = joined(d.text);
+ expect(all).toContain("credentials");
+ expect(all).toContain("checking");
+ expect(all).not.toContain("cloud catalog");
+ expect(all).not.toContain("live rosters");
+ } finally {
+ d.destroy();
+ }
+ });
+
+ test("a provider still being listed says which provider, and for how long", async () => {
+ // The per-provider in-flight state, which is now reachable because the fetch
+ // happens in response to a keystroke.
+ const d = await draw(
+ <ModelPicker
+ source={{
+ ...fakeSource({ roster: [provider({ label: "Kimi / Moonshot" })] }),
+ discoverRoster: () => new Promise<never>(() => {}),
+ }}
+ onDone={() => {}}
+ />
+ );
+ try {
+ await d.press(["RETURN"]);
+ const all = joined(d.recapture().text);
+ expect(all).toContain("Kimi / Moonshot");
+ expect(all).toContain("deadline");
+ } finally {
+ d.destroy();
+ }
+ });
+});
diff --cc packages/cli/src/picker/fixtures/fixture-source.ts
index 1b55dc31,00000000..ca4a1da6
mode 100644,000000..100644
--- a/packages/cli/src/picker/fixtures/fixture-source.ts
+++ b/packages/cli/src/picker/fixtures/fixture-source.ts
@@@ -1,186 -1,0 +1,186 @@@
+/**
+ * picker/fixtures/fixture-source.ts — a scriptable `PickerDataSource`, for the states
+ * a live provider cannot be made to produce.
+ *
+ * DEV-ONLY. It is reached exclusively through a dynamic `await import()` behind the
+ * `CLAUDISH_PICKER_FIXTURE` check in `model-picker-run.tsx`, so `bun build --compile`
+ * never walks into this directory and the shipped binary carries no fixture data.
+ *
+ * IT WRAPS THE PRODUCTION SOURCE AND OVERRIDES ONLY THE SHAPE OF AN ANSWER — never
+ * the data. The roster is still derived from the provider definitions, every model row
+ * is still the real catalog's, every price and context window is still whatever the
+ * backend says. What a scenario forges is the OUTCOME VARIANT (an empty roster, a
+ * timeout, a roster where nothing is chat-capable), the readiness answers, and
+ * latency. That keeps the "never hardcode rosters, context windows or pricing" rule
+ * intact inside a capture harness, which matters because a screenshot of invented
+ * prices proves nothing about the layout of real ones.
+ *
+ * WHY A FAKE AT ALL — the four states below are the whole point of the feature and
+ * none of them is reachable from outside:
+ *
+ * · `loading` the credential sweep held mid-`done/total` — the landing screen's
+ * only in-flight state, and on a real machine it is over in a second.
+ * · `discovering` credentials settle normally and the ROSTER never comes back.
+ * This is the per-provider wait, which exists only because a roster
+ * is now fetched when the user OPENS that provider.
+ * · `empty` a provider that answers correctly with nothing chat-capable.
+ * · `filtered` a provider that serves only embeddings or wildcard routes.
+ * · `timeout` an unreachable endpoint, on demand.
+ *
+ * V5 (unauthorized) is deliberately NOT the primary use: it is reproducible live with
+ * a bogus key against the real endpoint, which is how the "before" frame was measured,
+ * and a live capture beats a forged one every time. The scenario exists as a control.
+ */
+
+import type { ModelInfo, PickerDiscoveryOutcome } from "../../model-selector.js";
+import type { DescriptionIndex } from "../../providers/model-descriptions.js";
+import {
+ type PickerDataSource,
+ type PickerProviderChoice,
+ createPickerDataSource,
+} from "../PickerDataSource.js";
+
+export type FixtureName =
+ | "ready"
+ | "loading"
+ | "discovering"
+ | "timeout"
+ | "empty"
+ | "filtered"
+ | "unauthorized";
+
+const NEVER = new Promise<never>(() => {
+ /* deliberately never settles — a capture window is finite, this is not */
+});
+
+const sleep = (ms: number): Promise<void> => new Promise((r) => setTimeout(r, ms));
+
+/**
+ * Scenarios that forge a discovery OUTCOME also claim discovery for every provider,
+ * so the state is on screen wherever the cursor happens to be scoped. Without that, a
+ * capture would have to drive keystrokes to a particular provider and would break the
+ * moment the picker order changed.
+ */
+const FORCED_OUTCOME = new Set<FixtureName>(["timeout", "empty", "filtered", "unauthorized"]);
+
+export function createFixtureDataSource(name: string): PickerDataSource {
+ const scenario = (name as FixtureName) ?? "ready";
+ const real = createPickerDataSource();
+ const forced = FORCED_OUTCOME.has(scenario);
+ // `loading` claims discovery too — not to forge an outcome (it has none; it never
+ // settles) but so that a scoped provider exercises the ROSTER indicator, whose
+ // deadline shape is otherwise unreachable in a capture.
+ const claimsDiscovery = forced || scenario === "loading" || scenario === "discovering";
+
+ return {
+ providerRoster(): PickerProviderChoice[] {
+ return real
+ .providerRoster()
+ .map((r) =>
+ claimsDiscovery ? { ...r, hasDiscovery: true, discoveryShape: "deadline" as const } : r
+ );
+ },
+
+ notEnabledLocalProviders: () => real.notEnabledLocalProviders(),
+ displayName: (p) => real.displayName(p),
+
+ async *probeCredentials(names: string[]): AsyncIterable<[string, boolean]> {
+ // EVERY PROVIDER READY, and that is a forgery with a purpose: a capture runs with
+ // credential sources disabled so it cannot raise a 1Password or Keychain prompt on
+ // the user's desktop, which leaves the real roster three rows long and says nothing
+ // about how the list looks for a configured user.
+ for (const n of names) {
+ if (scenario === "loading") await sleep(600);
+ yield [n, true];
+ }
+ if (scenario === "loading") await NEVER; // hold the `done/total` meter mid-sweep
+ },
+
+ ensureCatalog(): Promise<void> {
+ return scenario === "loading" ? NEVER : real.ensureCatalog();
+ },
+
+ servedModels(provider: string): ModelInfo[] {
+ return real.servedModels(provider);
+ },
+
+ descriptions(): Promise<DescriptionIndex> {
+ // NOT forged: a description is editorial text from the real catalog, and a
+ // capture of an invented sentence proves nothing about how a real one wraps.
+ return scenario === "loading" ? NEVER : real.descriptions();
+ },
+
+ async discoverRoster(provider: string): Promise<PickerDiscoveryOutcome> {
+ // `discovering` is the whole point of that scenario: the credential sweep
+ // finishes, the provider list is on screen and usable, and the roster the
+ // user just asked for never lands. It is the only way to photograph the
+ // per-provider wait, which against a real provider is over in a second.
+ if (scenario === "loading" || scenario === "discovering") return NEVER;
+ if (!forced) return real.discoverRoster(provider);
+
+ // The fallback list is the REAL vendor catalog for this provider, because the
+ // defect being rendered is precisely that an unmarked fallback list reads as a
+ // healthy short roster — and that only shows on real rows.
+ const fallbackRows = real.servedModels(provider);
+ const displayName = real.displayName(provider);
+
+ if (scenario === "empty") {
+ return {
+ kind: "empty-roster",
+ failure: {
- kind: "empty-roster",
++ kind: "empty-models-catalog",
+ provider,
+ endpoint: "https://api.example.test/v1/models",
+ },
+ fallbackRows,
+ };
+ }
+ if (scenario === "filtered") {
+ return {
+ kind: "all-filtered",
+ servedCount: 7,
+ sampleIds: ["text-embedding-3-large", "whisper-1", "tts-1-hd"],
+ fallbackRows,
+ };
+ }
+ if (scenario === "timeout") {
+ return {
+ kind: "failed",
+ failure: {
+ kind: "unreachable",
+ provider,
+ endpoint: "https://api.example.test/v1/models",
+ detail: "The operation was aborted due to timeout",
+ },
+ notice: [
+ `\n⚠ ${displayName} could not list its models: the model list was unreachable at https://api.example.test/v1/models — The operation was aborted due to timeout\n`,
+ fallbackRows.length > 0
+ ? ` Showing ${displayName}'s cloud-catalog entries below — not its live roster.\n\n`
+ : " Falling back to manual model entry.\n\n",
+ ],
+ fallbackRows,
+ };
+ }
+ return {
+ kind: "failed",
+ failure: {
+ kind: "unauthorized",
+ provider,
+ status: 401,
+ endpoint: "https://api.example.test/v1/models",
+ detail:
+ '{"error":{"message":"Invalid Authentication","type":"invalid_authentication_error"}}',
+ },
+ notice: [
+ `\n⚠ ${displayName} could not list its models: the API key was rejected (HTTP 401) at https://api.example.test/v1/models\n`,
+ " Check EXAMPLE_API_KEY (a value in your shell overrides stored credentials).\n",
+ " Get a key: https://platform.example.test/keys\n",
+ fallbackRows.length > 0
+ ? ` Showing ${displayName}'s cloud-catalog entries below — not its live roster.\n\n`
+ : " Falling back to manual model entry.\n\n",
+ ],
+ fallbackRows,
+ };
+ },
+ };
+}
diff --cc packages/cli/src/picker/row-semantics.test.ts
index 43617d68,00000000..2177e72b
mode 100644,000000..100644
--- a/packages/cli/src/picker/row-semantics.test.ts
+++ b/packages/cli/src/picker/row-semantics.test.ts
@@@ -1,736 -1,0 +1,736 @@@
+import { describe, expect, test } from "bun:test";
+import type { ModelInfo, PickerDiscoveryOutcome } from "../model-selector.js";
+/**
+ * The pure semantics behind a row, a detail line and a discovery notice — the tier
+ * that needs no renderer, and the tier where the honesty rules are actually
+ * decidable.
+ *
+ * Three of these assertions exist because the alternative reading is silently
+ * plausible: a fallback list must say it MAY NOT WORK and not merely that it came
+ * from somewhere else; a capability the catalog is silent about must not be
+ * rendered as absent; and no rendered notice may contain the substring `undefined`.
+ */
+import type { DiscoveryFailure } from "../providers/model-discovery.js";
+import { resetThemeModeForTests, setThemeMode } from "../theme/theme-mode.js";
+import { C } from "../tui/theme.js";
+import { displayWidth, truncate } from "../tui/viz/text.js";
+import { tokens } from "../tui/viz/tokens.js";
+import {
+ discoveryNoticeContent,
+ mergeCredentialLines,
+ noticeRows,
+ wrapWords,
+} from "./DiscoveryNotice.js";
+import { capabilityWords, descriptionLines, detailText, providerFactsText } from "./detail.js";
+import { type PickerRow, dedupeByModelId, dedupeByProviderModel } from "./hooks/usePickerModels.js";
+import { deriveProviderRowLayout, deriveRowLayout, providerColumn } from "./layout.js";
+import {
+ CHIP_COLUMN_LABELS,
+ CHIP_FILL_CELLS,
+ billingLabel,
+ priceChipBg,
+ priceFg,
+ priceLabel,
+ priceVaries,
+ providerCountText,
+ readinessGlyph,
+} from "./rows.js";
+
+const model = (over: Partial<ModelInfo> = {}): ModelInfo => ({
+ id: "m",
+ name: "m",
+ description: "",
+ provider: "p",
+ ...over,
+});
+
+describe("priceLabel", () => {
+ test("the `/1M` suffix goes — the column header says it once for the whole list", () => {
+ expect(priceLabel("$9.00/1M", "metered")).toBe("$9.00");
+ expect(priceLabel("$0.15/1M", "metered")).toBe("$0.15");
+ });
+
+ test("a flat-rate plan keeps its word instead of a number it does not have", () => {
+ expect(priceLabel("SUB", "sub")).toBe("SUB");
+ expect(priceLabel("FREE", "metered")).toBe("FREE");
+ // A local model costs nothing per token and says so in lower case: shouting is
+ // reserved for the two labels that are about money.
+ expect(priceLabel("$3.00/1M", "local")).toBe("local");
+ expect(priceLabel("LOCAL", "metered")).toBe("local");
+ });
+
+ test("an absent price is `N/A`, and never an empty cell", () => {
+ // An empty price column would read as "free", which is a claim about money.
+ expect(priceLabel("N/A", "metered")).toBe("N/A");
+ expect(priceLabel("", "metered")).toBe("N/A");
+ });
+});
+
+describe("priceFg / priceChipBg", () => {
+ test("`SUB` IS NEVER THE FAILURE HUE — in either form, in either theme", () => {
+ // THE DEFECT THIS PINS SHIPPED. `SUB` was `tokens.warn`, which is `C.orange`:
+ // `#ff8800` on dark and a rust `#c2410c` on light. Red-orange is what this app
+ // spends on a failure, and `SUB` is the opposite of one — it is the route the
+ // user already pays for, free at the point of use. Three reviewers read the old
+ // colour as correct, so the rule is asserted rather than described.
+ for (const mode of ["dark", "light"] as const) {
+ setThemeMode(mode);
+ expect(priceFg("SUB")).not.toBe(tokens.warn);
+ expect(priceFg("SUB")).not.toBe(tokens.error);
+ expect(priceChipBg("SUB")).not.toBe(tokens.warn);
+ expect(priceChipBg("SUB")).not.toBe(tokens.error);
+ }
+ resetThemeModeForTests();
+ });
+
+ test("`FREE`, `SUB` and `local` share ONE POSITIVE tier, because they make one claim", () => {
+ // All three say "running this costs you no per-token money"; the label says by
+ // which mechanism. Three hues for one claim is rule 1 read backwards, and the
+ // owner asked for them as one family in as many words.
+ expect(priceFg("SUB")).toBe(priceFg("FREE"));
+ expect(priceFg("SUB")).toBe(priceFg("local"));
+ expect(priceFg("SUB")).toBe(tokens.success);
+ expect(priceChipBg("SUB")).toBe(priceChipBg("FREE"));
+ expect(priceChipBg("SUB")).toBe(priceChipBg("local"));
+ });
+
+ test("a chip FILL is a muted pill, never the neon foreground", () => {
+ // `aesthetics-and-color.md`: a colour tuned to read as text is harsh as a solid
+ // block. `C.pillKeyBg` is already the `FREE` chip on the session summary card,
+ // so the picker borrows that object rather than inventing a second green.
+ expect(priceChipBg("FREE")).toBe(C.pillKeyBg);
+ expect(priceChipBg("FREE")).not.toBe(C.green);
+ });
+
+ test("A VALUE IS NOT A TOKEN, so a price numeral gets no chip at all", () => {
+ // A chip is for a fixed, small vocabulary. Chipping `$0.75` would put a fill
+ // behind every row of the column — the rectangle — and would claim the numeral
+ // is one of a handful of states rather than a measured amount.
+ expect(priceChipBg("$2.25")).toBeNull();
+ expect(priceChipBg("$30.00")).toBeNull();
+ // `N/A` is the ABSENCE of the fact, not a state of it.
+ expect(priceChipBg("N/A")).toBeNull();
+ });
+
+ test("`N/A` is the ABSENCE of the fact, so it stays in the dim tier alone", () => {
+ // `local` used to share this tier on the grounds that both mean "there is no
+ // number here". They do not mean the same thing: `local` is a positive claim the
+ // reader acts on — this route costs nothing and needs no network — while `N/A`
+ // is the catalog declining to answer. So `local` joined the positive family and
+ // `N/A` kept the dim tier, which is where an unanswered question belongs.
+ expect(priceFg("N/A")).toBe(tokens.dead);
+ expect(priceFg("N/A")).not.toBe(priceFg("local"));
+ expect(priceFg("N/A")).not.toBe(priceFg("$2.25"));
+ });
+
+ test("AN EXPENSIVE PRICE IS NOT PAINTED AS AN ALARM — one colour, one meaning", () => {
+ // A prototype of this dialog bucketed metered prices and drew anything over
+ // $10/1M in orange; the owner and the reviewer both read `$30.00` as an error,
+ // because red-orange is what this app spends on a failure and on `SUB`. A
+ // number is a number: the numeral is body ink at every magnitude, and the
+ // comparison the reader is making is between the DIGITS.
+ const prices = ["$0.15", "$2.25", "$12.50", "$15.00", "$30.00"].map(priceFg);
+ expect(new Set(prices).size).toBe(1);
+ });
+});
+
+describe("CHIP_FILL_CELLS — one fill width for the whole status column", () => {
+ test("it fits the LONGEST label the column can print, and is never typed out", () => {
+ // The owner asked for one width across the column and the obvious number is 5 —
+ // `SUB` plus a space each side — which clips `FREE` by one cell and `local` by
+ // two. Yoga does not report a clipped cell; it takes the columns out of a
+ // NEIGHBOURING one and leaves a stub of background under its first letter, which
+ // a character frame cannot see. So the width is derived, and a fifth state added
+ // to the vocabulary widens the column instead of overflowing it.
+ expect(CHIP_FILL_CELLS).toBe(7);
+ for (const label of CHIP_COLUMN_LABELS) {
+ expect({ label, fits: displayWidth(label) + 2 <= CHIP_FILL_CELLS }).toEqual({
+ label,
+ fits: true,
+ });
+ }
+ });
+
+ test("the VOCABULARY is exactly what the two chipped columns answer", () => {
+ // A label outside this list would be sized by a constant that never saw it.
+ const words = new Set<string>(CHIP_COLUMN_LABELS);
+ for (const mode of ["sub", "local", "metered"] as const) {
+ expect(words.has(billingLabel(mode).text)).toBe(true);
+ }
+ for (const price of ["FREE", "SUB", "local"]) {
+ expect(priceChipBg(price)).not.toBeNull();
+ expect(words.has(price)).toBe(true);
+ }
+ });
+
+ test("BOTH columns that print a chip are at least that wide, at 80 and at 145", () => {
+ // The fill is the one cell count a chip owns; the CELL it sits in is a budget in
+ // `layout.ts`, and the two numbers drifting apart is how a chip comes to overflow
+ // its column. 76 and 141 are the dialog's inner widths at 80x24 and 145x45.
+ for (const inner of [76, 141]) {
+ expect({
+ inner,
+ billing: deriveProviderRowLayout(inner).billing >= CHIP_FILL_CELLS,
+ price: deriveRowLayout(inner).price >= CHIP_FILL_CELLS,
+ }).toEqual({ inner, billing: true, price: true });
+ }
+ });
+});
+
+describe("providerCountText — the tail says what you GET, never how it is fetched", () => {
+ test("a known count is a count, and one model is not `1 models`", () => {
+ expect(providerCountText({ count: 42, hasDiscovery: true, billing: "metered" })).toBe(
+ "42 models"
+ );
+ expect(providerCountText({ count: 1, hasDiscovery: false, billing: "sub" })).toBe("1 model");
+ // `null` is "nobody has counted this yet" and prints NOTHING — not `0`, not a dash.
+ expect(providerCountText({ count: null, hasDiscovery: true, billing: "sub" })).toBe("");
+ });
+
+ test("`asks its own roster` is gone — a subscription provider says `subscription models`", () => {
+ // The owner read the shipped row on a live run: `asks its own roster` describes
+ // claudish's fetch, not the user's plan. His replacement, verbatim: *"subscription
+ // models"*. This is the case he was looking at — Grok Build, Devin, Antigravity,
+ // OpenCode Zen Go: flat-rate, no catalog entries, a roster of their own.
+ expect(providerCountText({ count: 0, hasDiscovery: true, billing: "sub" })).toBe(
+ "subscription models"
+ );
+ for (const billing of ["sub", "metered", "local"] as const) {
+ expect(providerCountText({ count: 0, hasDiscovery: true, billing })).not.toContain("roster");
+ }
+ });
+
+ test("and a NON-subscription provider gets its own words, because the other would be false", () => {
+ // `hasDiscovery` is a property of the provider DEFINITION, not of its billing, so
+ // this branch is reachable by a metered or local provider with no catalog entries
+ // — a self-hosted vLLM, an Ollama daemon. "Subscription models" there would be the
+ // screen stating a plan the user does not have.
+ expect(providerCountText({ count: 0, hasDiscovery: true, billing: "metered" })).toBe(
+ "lists its own models"
+ );
+ expect(providerCountText({ count: 0, hasDiscovery: true, billing: "local" })).toBe(
+ "lists its own models"
+ );
+ expect(providerCountText({ count: 0, hasDiscovery: true, billing: "metered" })).not.toContain(
+ "subscription"
+ );
+ });
+
+ test("a REAL zero from a provider that does not discover is still a zero", () => {
+ // Without `hasDiscovery` there is nothing else to ask: the catalog is the answer
+ // and the answer is none. Saying "lists its own models" there would be the
+ // opposite lie to the one above.
+ expect(providerCountText({ count: 0, hasDiscovery: false, billing: "metered" })).toBe(
+ "0 models"
+ );
+ });
+
+ test("every phrasing fits the tail cell at 80 columns, where it is narrowest", () => {
+ // `deriveProviderRowLayout` gives the tail 25 cells at the narrowest supported
+ // width and grows it from there. A phrase longer than that would be truncated with
+ // an ellipsis and nothing would report it.
+ const tail = deriveProviderRowLayout(76).tail;
+ for (const billing of ["sub", "metered", "local"] as const) {
+ const text = providerCountText({ count: 0, hasDiscovery: true, billing });
+ expect({ billing, text, fits: displayWidth(text) <= tail }).toEqual({
+ billing,
+ text,
+ fits: true,
+ });
+ }
+ });
+});
+
+describe("priceVaries — which view earns chips", () => {
+ test("a flat-rate or local provider FIXES the column, so it gets no chips", () => {
+ // `resolveProviderDisplayPrice` answers `SUB` for every row of a subscription
+ // roster. 49 identical fills stacked with no gap between rows is the failure the
+ // skill MEASURED — chips fusing into one solid rectangle with the labels
+ // floating in it — and no amount of correct padding prevents it, because the
+ // padding is not what makes them identical.
+ expect(priceVaries("sub")).toBe(false);
+ expect(priceVaries("local")).toBe(false);
+ });
+
+ test("the cross-provider list and a METERED provider vary, so they do", () => {
+ expect(priceVaries(null)).toBe(true);
+ expect(priceVaries("metered")).toBe(true);
+ });
+});
+
+describe("billingLabel / readinessGlyph", () => {
+ test("each billing mode has its own WORD, and ALL THREE carry a fill", () => {
+ const tags = (["sub", "local", "metered"] as const).map(billingLabel);
+ expect(tags.map((t) => t.text)).toEqual(["SUB", "local", "$$$"]);
+ // `$` USED TO BE `bg: null` — drawn as text, on the grounds that a fill on every
+ // row of the column is the banding defect. The owner read the shipped screen and
+ // asked for the opposite: "make $$$ the same width and badge as well", because a
+ // filled `SUB` beside a bare `$` is a column with no edges. What stops the column
+ // banding is now the DISTANCE between the two fills (ΔE76 44.9 light / 63.4 dark,
+ // pinned in `theme-contrast.test.ts`) rather than the absence of one of them.
+ //
+ // THE LABEL IS `$$$`, WHICH IS THE OWNER'S NEXT INSTRUCTION AND NOT A TYPO FOR
+ // THE PRICE. Real prices are numerals (`$2.25`) built by `priceLabel`, and this
+ // chip is the provider's BILLING MODE. Three characters, the same as `SUB` — a
+ // coincidence the column is glad of and does not depend on, because
+ // `CHIP_FILL_CELLS` still derives from `local`.
+ expect(tags.map((t) => t.bg)).toEqual([C.pillKeyBg, C.pillKeyBg, C.pillCostBg]);
+ expect(C.pillCostBg).not.toBe(C.pillKeyBg);
+ });
+
+ test("METERED COSTS MONEY and SUB DOES NOT — but neither is a FAILURE", () => {
+ // `$$$` states that the route bills per token, in the hue that says so. What it
+ // must never become is the hue that says something BROKE: `tokens.error` is the
+ // `HTTP 401` badge and the discovery banner, and a chip wearing it would put a
+ // failure on 13 of 17 healthy rows. The register that keeps the two apart is
+ // measured in `theme-contrast.test.ts`; that they are not the literal same token
+ // is cheap to state here, and it is the check that fails first and reads clearest.
+ expect(billingLabel("metered").fg).toBe(C.pillCostFg);
+ expect(billingLabel("metered").bg).toBe(C.pillCostBg);
+ for (const mode of ["sub", "local", "metered"] as const) {
+ const tag = billingLabel(mode);
+ for (const colour of [tag.bg, tag.fg]) {
+ expect(colour).not.toBe(tokens.error);
+ expect(colour).not.toBe(tokens.warn);
+ expect(colour).not.toBe(C.red);
+ expect(colour).not.toBe(C.bgError);
+ }
+ }
+ });
+
+ test("EVERY CHIP STATES ITS OWN INK, and the ink follows the palette", () => {
+ // We choose the fill, so we own the ink: `pickInk` would answer white on the dark
+ // palette and BLACK on the light one for the same chip — one chip with two inks,
+ // and the worse ratio (4.02 vs 5.02) on the lighter page.
+ //
+ // AND THE INK IS NOT ONE COLOUR ANY MORE. The dark palette builds a chip as a
+ // saturated fill with light ink; the light palette builds it as a pale TINT with
+ // deep ink of the same hue, because a saturated block glares on a white page
+ // (`theme.ts`). So "white on everything" is exactly the rule that had to go, and
+ // what replaces it is: the ink is the fill's OWN declared partner, and the pair
+ // moves together when the theme flips.
+ for (const mode of ["dark", "light"] as const) {
+ setThemeMode(mode);
+ expect({
+ mode,
+ sub: billingLabel("sub").fg,
+ local: billingLabel("local").fg,
+ metered: billingLabel("metered").fg,
+ }).toEqual({ mode, sub: C.pillKeyFg, local: C.pillKeyFg, metered: C.pillCostFg });
+ // The two halves never share an ink — that would erase the distinction the
+ // column exists to make.
+ expect(C.pillKeyFg).not.toBe(C.pillCostFg);
+ }
+ // And the flip actually MOVED them: a light-palette `SUB` painted in the dark
+ // palette's white ink is the module-level-const bug this repo has found six times.
+ setThemeMode("dark");
+ const darkInk = billingLabel("sub").fg;
+ setThemeMode("light");
+ expect(billingLabel("sub").fg).not.toBe(darkInk);
+ resetThemeModeForTests();
+ });
+
+ test("readiness has three states, not two — pending is not missing", () => {
+ const g = (["pending", "ready", "missing"] as const).map(readinessGlyph);
+ expect(new Set(g.map((x) => x.glyph)).size).toBe(3);
+ expect(new Set(g.map((x) => x.fg)).size).toBe(3);
+ });
+
+ test("A PROBE IN FLIGHT IS NOT A WARNING — `◌` is `running`, not `warn`", () => {
+ // The same call `chrome.tsx` already made for the catalog shimmer. Spending the
+ // failure hue on "still checking" teaches the reader to discount it everywhere.
+ expect(readinessGlyph("pending").fg).toBe(tokens.running);
+ expect(readinessGlyph("pending").fg).not.toBe(tokens.warn);
+ });
+});
+
+describe("capabilityWords", () => {
+ test("a capability the catalog is SILENT about is not rendered as absent", () => {
+ // `undefined` is not `false`. The slim catalog carries `supportsTools` for most
+ // models and nothing for some, and printing an absence would be claiming a fact
+ // nobody has.
+ expect(capabilityWords(model({ supportsTools: true }))).toEqual(["tools"]);
+ expect(capabilityWords(model({}))).toEqual([]);
+ expect(capabilityWords(model({ supportsTools: false }))).toEqual([]);
+ });
+
+ test("all three are listed in a fixed order when all three are known", () => {
+ expect(
+ capabilityWords(model({ supportsTools: true, supportsReasoning: true, supportsVision: true }))
+ ).toEqual(["tools", "reasoning", "vision"]);
+ });
+});
+
+describe("detailText", () => {
+ test("the exact spec leads, so the picker never hides its own return value", () => {
+ const { text } = detailText("[email protected]", model({ releaseDate: "2026-09-14" }));
+ expect(text.startsWith("[email protected]")).toBe(true);
+ expect(text).toContain("2026-09");
+ });
+
+ test("`supportsTools: false` is DISQUALIFYING and says so in words", () => {
+ // A model that cannot take tool definitions cannot drive Claude Code at all,
+ // which is a different claim from "it is a bit less capable".
+ expect(detailText("x@y", model({ supportsTools: false })).warn).toContain("no tool support");
+ expect(detailText("x@y", model({ supportsTools: true })).warn).toBe("");
+ expect(detailText("x@y", model({})).warn).toBe("");
+ });
+});
+
+describe("providerFactsText", () => {
+ test("billing is a SENTENCE, not a symbol — a flat rate says it costs nothing per token", () => {
+ const sub = providerFactsText({
+ label: "Kimi Coding",
+ shortcut: "kc@",
+ billing: "sub",
+ envVar: "KIMI_CODING_API_KEY",
+ });
+ expect(sub.billing).toContain("flat-rate");
+ expect(sub.billing).toContain("no per-token");
+ // The short form makes the SAME claim in fewer words, for a row that cannot
+ // afford the sentence — never a different claim, and never silence.
+ expect(sub.billingShort).toContain("flat-rate");
+ expect(sub.billingShort.length).toBeLessThan(sub.billing.length);
+ expect(
+ providerFactsText({ label: "", shortcut: "", billing: "metered", envVar: "" }).billing
+ ).toContain("per token");
+ expect(
+ providerFactsText({ label: "", shortcut: "", billing: "local", envVar: "" }).billing
+ ).toContain("this machine");
+ });
+
+ test("the credential is NAMED, and a sign-in provider says so instead of naming nothing", () => {
+ // A truncated or absent variable name sends the reader hunting for a variable
+ // that does not exist — a defect this dialog has already shipped once.
+ expect(
+ providerFactsText({
+ label: "OpenRouter",
+ shortcut: "or@",
+ billing: "metered",
+ envVar: "OPENROUTER_API_KEY",
+ }).auth
+ ).toBe("OPENROUTER_API_KEY");
+ expect(
+ providerFactsText({ label: "Devin", shortcut: "dv@", billing: "sub", envVar: "" }).auth
+ ).toContain("signs in");
+ });
+});
+
+describe("descriptionLines", () => {
+ test("a short sentence is one row and is not padded into two", () => {
+ expect(descriptionLines("A fast model.", 40, 2)).toEqual(["A fast model."]);
+ });
+
+ test("no row exceeds the width, and an over-long blurb ends in an ellipsis", () => {
+ const long =
+ "Kimi's most capable model to date, with 2.8 trillion parameters, native visual reasoning, " +
+ "and a context window measured in millions of tokens across every supported modality.";
+ const rows = descriptionLines(long, 40, 2);
+ expect(rows.length).toBe(2);
+ for (const r of rows) expect(r.length).toBeLessThanOrEqual(40);
+ expect(rows[1]?.endsWith("…")).toBe(true);
+ });
+
+ test("an absent description is NO rows — the block draws its own blanks", () => {
+ // Returning `[""]` would be indistinguishable from a one-row description at the
+ // call site, and the block's fixed height is what keeps the footer still.
+ expect(descriptionLines(" ", 40, 2)).toEqual([]);
+ });
+});
+
+describe("row identity depends on the VIEW", () => {
+ const row = (provider: string, id: string, spec: string): PickerRow =>
+ ({ provider, shortcut: "", spec, price: "", model: model({ id }) }) as PickerRow;
+
+ test("ALL MODELS: one model on N providers is N rows, with N distinct specs", () => {
+ // The owner's rule, verbatim: "if model has more than one provider that going
+ // to be two lines in 'all models' list". `gpt-6-astra` at $30.00 on OpenRouter
+ // beside the same model as SUB on Codex is the single most useful thing this
+ // list does, and keying on the model id alone would delete two of the three.
+ const kept = dedupeByProviderModel([
+ row("openrouter", "gpt-6-astra", "openrouter@openai/gpt-6-astra"),
+ row("openai", "gpt-6-astra", "oai@gpt-6-astra"),
+ row("openai-codex", "gpt-6-astra", "cx@gpt-6-astra"),
+ ]);
+ expect(kept.length).toBe(3);
+ expect(new Set(kept.map((r) => r.spec)).size).toBe(3);
+ });
+
+ test("ALL MODELS: the same model TWICE under ONE provider is still one row", () => {
+ // A provider's live roster and its catalog entries overlap, and that overlap
+ // is not two routes — it is one route described twice.
+ const kept = dedupeByProviderModel([
+ row("kimi", "kimi-k3", "kimi@kimi-k3"),
+ row("kimi", "kimi-k3", "kimi@kimi-k3"),
+ row("kimi", "kimi-k2.6", "[email protected]"),
+ ]);
+ expect(kept.map((r) => r.model.id)).toEqual(["kimi-k3", "kimi-k2.6"]);
+ });
+
+ test("PROVIDER CATALOG: one row per model id, roster and catalog collapsed", () => {
+ // "and if we enter to provider catalog, not all models - then the model will
+ // be just one". One route is in scope, so a second row would mean nothing —
+ // even when the two sources spell the spec differently.
+ const kept = dedupeByModelId([
+ row("kimi", "kimi-k3", "kimi@kimi-k3"),
+ row("kimi", "kimi-k3", "kimi@moonshot/kimi-k3"),
+ row("kimi", "kimi-k2.6", "[email protected]"),
+ ]);
+ expect(kept.length).toBe(2);
+ // The FIRST wins, and the caller puts the live roster first — what the
+ // endpoint answered for THESE credentials beats what the catalog says.
+ expect(kept[0]?.spec).toBe("kimi@kimi-k3");
+ });
+});
+
+describe("providerColumn", () => {
+ const entry = (value: string, label: string, shortcut: string) => ({ value, label, shortcut });
+
+ test("NO TWO PROVIDERS EVER RENDER THE SAME STRING — the rejected rail's defect", () => {
+ // `OpenCode Zen` and `OpenCode Zen Go` share a twelve-character prefix, which is
+ // exactly the collision that rendered two different providers as `opencod…`.
+ const { cells, text } = providerColumn(
+ [
+ entry("opencode-zen", "OpenCode Zen", "zen@"),
+ entry("opencode-zen-go", "OpenCode Zen Go", "zengo@"),
+ entry("sakana", "Sakana Fugu", "fugu@"),
+ entry("sakana-subscription", "Sakana Fugu Subscription", "sc@"),
+ entry("openrouter", "OpenRouter", "or@"),
+ ],
+ truncate
+ );
+ const drawn = [...text.values()];
+ expect(new Set(drawn).size).toBe(drawn.length);
+ for (const s of drawn) expect(s.length).toBeLessThanOrEqual(cells);
+ });
+
+ test("a name that FITS is printed whole — truncation is the exception, not the rule", () => {
+ const { text } = providerColumn(
+ [entry("openrouter", "OpenRouter", "or@"), entry("openai", "OpenAI", "oai@")],
+ truncate
+ );
+ expect(text.get("openrouter")).toBe("OpenRouter");
+ expect(text.get("openai")).toBe("OpenAI");
+ });
+
+ test("names that cannot be separated at ANY width fall back to the unique shortcut", () => {
+ // Two custom endpoints with the same long name is a real configuration; the
+ // shortcut is unique by construction because it is the shortest prefix that
+ // parses back to exactly one provider.
+ const same = "A Very Long Custom Endpoint Name";
+ const { text } = providerColumn([entry("a", same, "aa@"), entry("b", same, "bb@")], truncate);
+ const drawn = [...text.values()];
+ expect(new Set(drawn).size).toBe(2);
+ expect(drawn.some((s) => s.includes("bb@"))).toBe(true);
+ });
+});
+
+const failure = (over: Partial<DiscoveryFailure> = {}): DiscoveryFailure => ({
+ kind: "unauthorized",
+ provider: "kimi",
+ ...over,
+});
+
+describe("discoveryNoticeContent", () => {
+ test("`failed` is the ERROR tier; the three empty states are the NOTICE tier", () => {
+ const rows: PickerDiscoveryOutcome[] = [
+ { kind: "failed", failure: failure(), notice: ["⚠ x failed"], fallbackRows: [] },
- { kind: "empty-roster", failure: failure({ kind: "empty-roster" }), fallbackRows: [] },
++ { kind: "empty-roster", failure: failure({ kind: "empty-models-catalog" }), fallbackRows: [] },
+ { kind: "all-filtered", servedCount: 3, sampleIds: ["e"], fallbackRows: [] },
+ { kind: "collapsed-empty", servedCount: 3, chatCount: 3, fallbackRows: [] },
+ ];
+ expect(rows.map((o) => discoveryNoticeContent(o, "Kimi")?.severity)).toEqual([
+ "error",
+ "notice",
+ "notice",
+ "notice",
+ ]);
+ });
+
+ test("`rows` and `unsupported` render nothing at all", () => {
+ expect(
+ discoveryNoticeContent(
+ { kind: "rows", rows: [model()], servedCount: 1, chatCount: 1 },
+ "Kimi"
+ )
+ ).toBeNull();
+ expect(
+ discoveryNoticeContent({ kind: "unsupported", reason: "no-descriptor" }, "Kimi")
+ ).toBeNull();
+ });
+
+ test("`empty-roster` and `all-filtered` say DIFFERENT things", () => {
+ const empty = discoveryNoticeContent(
- { kind: "empty-roster", failure: failure({ kind: "empty-roster" }), fallbackRows: [] },
++ { kind: "empty-roster", failure: failure({ kind: "empty-models-catalog" }), fallbackRows: [] },
+ "Kimi"
+ );
+ const filtered = discoveryNoticeContent(
+ {
+ kind: "all-filtered",
+ servedCount: 7,
+ sampleIds: ["text-embedding-3-large"],
+ fallbackRows: [],
+ },
+ "Kimi"
+ );
+ expect(empty?.lines[0]).toContain("empty");
+ expect(filtered?.lines[0]).toContain("none of them chat-capable");
+ expect(filtered?.lines.join(" ")).toContain("text-embedding-3-large");
+ expect(empty?.lines[0]).not.toBe(filtered?.lines[0]);
+ });
+
+ test("A FALLBACK LIST SAYS IT MAY NOT WORK, not merely where it came from", () => {
+ // The single most useful sentence in this state, and the one the shipped stderr
+ // wording never carried: discovery failed because the credential was rejected,
+ // so nothing has confirmed the account can call any of these models. A reader
+ // told only "catalog entries" concludes the list is differently-sourced, picks
+ // one, and finds out at launch.
+ const c = discoveryNoticeContent(
+ {
+ kind: "failed",
+ failure: failure({ status: 401 }),
+ notice: [
+ "\n⚠ Kimi could not list its models: the API key was rejected (HTTP 401)\n",
+ " Showing Kimi's cloud-catalog entries below — not its live roster.\n\n",
+ ],
+ fallbackRows: [model(), model({ id: "b" }), model({ id: "c" }), model({ id: "d" })],
+ },
+ "Kimi"
+ );
+ const joined = c?.lines.join(" ") ?? "";
+ expect(joined).toContain("The 4 rows below");
+ expect(joined).toContain("not Kimi's live roster");
+ expect(joined).toContain("may still fail");
+ // The formatter's own shorter sentence is REPLACED, not printed beside it.
+ expect(joined).not.toContain("Showing Kimi's cloud-catalog entries");
+ });
+
+ test("no fallback list gets a next step instead of a provenance sentence", () => {
+ const without = discoveryNoticeContent(
- { kind: "empty-roster", failure: failure({ kind: "empty-roster" }), fallbackRows: [] },
++ { kind: "empty-roster", failure: failure({ kind: "empty-models-catalog" }), fallbackRows: [] },
+ "Kimi"
+ );
+ expect(without?.lines.join(" ")).toContain("Press c");
+ expect(without?.lines.join(" ")).not.toContain("may still fail");
+ });
+
+ test("the dialog status for a failure says UNAVAILABLE, not broken", () => {
+ const c = discoveryNoticeContent(
+ { kind: "failed", failure: failure(), notice: ["⚠ x"], fallbackRows: [model()] },
+ "Kimi"
+ );
+ expect(c?.title).toBe("live roster unavailable");
+ });
+
+ test("the HTTP status becomes a badge and leaves the headline once", () => {
+ const c = discoveryNoticeContent(
+ {
+ kind: "failed",
+ failure: failure({ status: 401 }),
+ notice: ["\n⚠ Kimi could not list its models: the API key was rejected (HTTP 401)\n"],
+ fallbackRows: [model()],
+ },
+ "Kimi"
+ );
+ expect(c?.badge).toBe("HTTP 401");
+ expect(c?.lines[0]).not.toContain("(HTTP 401)");
+ expect(c?.lines[0]).toContain("the API key was rejected");
+ });
+
+ test("NO rendered notice ever contains the substring `undefined`", () => {
+ // Every variant × endpoint present/absent × status present/absent. `endpoint` and
+ // `status` are both optional and a registered fetcher may report neither.
+ const variants: PickerDiscoveryOutcome[] = [];
+ for (const endpoint of [undefined, "https://api.test/v1/models"]) {
+ for (const status of [undefined, 500]) {
+ const f = failure({
+ ...(endpoint === undefined ? {} : { endpoint }),
+ ...(status === undefined ? {} : { status }),
+ });
+ variants.push({ kind: "failed", failure: f, notice: ["⚠ x"], fallbackRows: [] });
+ variants.push({
+ kind: "empty-roster",
- failure: { ...f, kind: "empty-roster" },
++ failure: { ...f, kind: "empty-models-catalog" },
+ fallbackRows: [],
+ });
+ }
+ }
+ variants.push({ kind: "all-filtered", servedCount: 1, sampleIds: [], fallbackRows: [] });
+ variants.push({ kind: "collapsed-empty", servedCount: 1, chatCount: 1, fallbackRows: [] });
+ for (const v of variants) {
+ const c = discoveryNoticeContent(v, "Kimi");
+ expect([v.kind, c?.lines.join(" ").includes("undefined") ?? false]).toEqual([v.kind, false]);
+ }
+ });
+});
+
+describe("wrapWords / noticeRows", () => {
+ test("a line that fits is returned untouched", () => {
+ expect(wrapWords("short enough", 40)).toEqual(["short enough"]);
+ });
+
+ test("no wrapped row exceeds the width", () => {
+ const long =
+ "The 4 rows below are catalog entries, not Kimi / Moonshot's live roster — they do not confirm access, so launching one may still fail.";
+ for (const w of [30, 48, 70]) {
+ for (const line of wrapWords(long, w)) expect(line.length).toBeLessThanOrEqual(w);
+ }
+ });
+
+ test("THE `may still fail` CLAUSE SURVIVES AT 80 COLUMNS", () => {
+ // It is the tail of the sentence, so a truncating banner would take it first —
+ // which is exactly the failure this whole state exists to prevent.
+ const content = discoveryNoticeContent(
+ {
+ kind: "failed",
+ failure: failure({ status: 401 }),
+ notice: [
+ "\n⚠ Kimi could not list its models: the API key was rejected (HTTP 401)\n",
+ " Check MOONSHOT_API_KEY (a value in your shell overrides stored credentials).\n",
+ " Get a key: https://platform.moonshot.cn/\n",
+ ],
+ fallbackRows: [model(), model({ id: "b" }), model({ id: "c" }), model({ id: "d" })],
+ },
+ "Kimi"
+ );
+ // 76-column dialog: 2 border, 2 padding, 2 for the banner's own rule + gutter.
+ const rows = noticeRows(content!, 70, 5);
+ expect(rows.lines.join(" ")).toContain("may still fail");
+ expect(rows.lines.length).toBeLessThanOrEqual(5);
+ expect(rows.badge).toBe("HTTP 401");
+ });
+
+ test("the row count is knowable BEFORE render — the inline row budget depends on it", () => {
+ const content = discoveryNoticeContent(
+ { kind: "failed", failure: failure(), notice: ["⚠ x"], fallbackRows: [] },
+ "Kimi"
+ );
+ const rows = noticeRows(content!, 70, 5);
+ expect(rows.lines.length).toBeGreaterThan(0);
+ expect(rows.lines.length).toBeLessThanOrEqual(5);
+ });
+});
+
+describe("mergeCredentialLines", () => {
+ test("merges the env-var and key-URL rows when they fit, dropping the parenthetical", () => {
+ const lines = [
+ "⚠ Kimi could not list its models: the API key was rejected",
+ "Check MOONSHOT_API_KEY (a value in your shell overrides stored credentials).",
+ "Get a key: https://platform.moonshot.cn/",
+ "Showing Kimi's cloud-catalog entries below — not its live roster.",
+ ];
+ const merged = mergeCredentialLines(lines, 78);
+ expect(merged.length).toBe(3);
+ expect(merged[1]).toBe("Check MOONSHOT_API_KEY · Get a key: https://platform.moonshot.cn/");
+ });
+
+ test("leaves them apart when the merge would not fit", () => {
+ const lines = [
+ "head",
+ "Check A_VERY_LONG_ENVIRONMENT_VARIABLE_NAME_INDEED (advice).",
+ "Get a key: https://example.test/an/extremely/long/path/to/the/api/keys/page",
+ "tail",
+ ];
+ expect(mergeCredentialLines(lines, 40)).toEqual(lines);
+ });
+});
diff --cc packages/cli/src/providers/model-catalog.ts
index b26789da,7e3512e4..b01def11
--- a/packages/cli/src/providers/model-catalog.ts
+++ b/packages/cli/src/providers/model-catalog.ts
@@@ -82,29 -83,6 +83,29 @@@ export interface CatalogClient
* Cross-vendor search. Delegates to Firebase `?search=...` for live results.
*/
searchModels(term: string, limit?: number): Promise<CatalogModel[]>;
+
+ /**
+ * Every model the slim catalog says this vendor SERVES — synchronous, local,
+ * no network, for EVERY slug rather than only the aggregator ones.
+ *
+ * WHY IT EXISTS SEPARATELY FROM `modelsByVendor`. The picker draws ONE flat
+ * cross-provider list, so it asks this question about ~31 providers at once.
+ * `modelsByVendor` answers it for an owner slug with a live `?provider=` query,
+ * and 22 of those fired together took 10 s and returned nothing (measured:
+ * every one aborted on its shared timeout, while the same query alone takes
+ * ~1.1 s). A picker cannot spend that, and it must not degrade to "this
+ * provider has no models", which is the exact complaint this feature exists to
+ * remove.
+ *
- * IT IS NOT A SECOND AUTHORITY. `aggregators[].provider` is already the
- * served-by index — `modelsByVendor`'s own owner branch filters its rich
- * lineage list THROUGH it (`filterToServedByProvider`) precisely because
- * lineage is not service. This reads the same field for the same question and
++ * IT IS NOT A SECOND AUTHORITY. Each `aggregators[]` row's route binding is
++ * already the served-by index — `modelsByVendor`'s own owner branch filters its
++ * rich lineage list THROUGH it (`filterToServedByProvider`) precisely because
++ * lineage is not service. This applies the same rule to the same question and
+ * skips the lineage query, so its membership is the served-by set exactly. The
+ * cost is per-model richness: `description` and the owner slug are absent from
+ * the slim payload, so a row's blurb is derived rather than editorial.
+ */
+ servedByVendor(vendorSlug: string): CatalogModel[];
}
// ─── Slug classification ─────────────────────────────────────────────────────
@@@ -401,21 -364,5 +405,23 @@@ export function createCatalogClient(dep
const docs = await _searchModels(term, limit);
return docs.map(modelDocToCatalogModel);
},
+
+ servedByVendor(vendorSlug: string): CatalogModel[] {
+ const slug = vendorSlug.toLowerCase();
+ // Local-only vendors are absent from the catalog BY DESIGN, so an empty
+ // answer here is a fact, not a miss. Their rosters come from their own
+ // daemons through `modelDiscovery`.
+ if (NO_CATALOG_VENDOR_SLUGS.has(slug)) return [];
+ const { entries } = readSlimCacheWithFreshness(_readSlimCache);
+ const out: CatalogModel[] = [];
+ for (const entry of entries) {
- if (entry.aggregators?.some((agg) => agg.provider.toLowerCase() === slug)) {
++ // The same rule `filterToServedByProvider` applies, so the two answers
++ // cannot drift: a v3 row names who serves it by its route binding.
++ if (entry.aggregators?.some((agg) => catalogRouteMatchesProvider(agg.route, slug))) {
+ out.push(slimEntryToCatalogModel(entry));
+ }
+ }
+ return out;
+ },
};
}
diff --cc packages/cli/src/providers/model-discovery-builtins.ts
index cd092045,10454159..c5502e02
--- a/packages/cli/src/providers/model-discovery-builtins.ts
+++ b/packages/cli/src/providers/model-discovery-builtins.ts
@@@ -18,77 -18,26 +18,77 @@@
* property the original dynamic `import()` calls existed to protect.
*/
-import type { DiscoveredModel } from "./model-discovery.js";
+import type { FetcherResult } from "./model-discovery.js";
import { registerModelDiscoveryFetcher } from "./model-discovery.js";
+/**
+ * Upstream error text as one readable line.
+ *
+ * A private copy rather than an import: `model-discovery.ts`'s `oneLine` is
+ * module-private there, and exporting it to share four lines would widen that
+ * module's surface for no gain.
+ */
+function oneLine(text: string, max = 200): string {
+ const flat = text.replace(/\s+/g, " ").trim();
+ return flat.length > max ? `${flat.slice(0, max)}…` : flat;
+}
+
/**
- * Devin's roster is capability ∩ entitlement over two protobuf rpcs.
+ * Devin's dynamic models catalog is capability ∩ entitlement over two protobuf rpcs.
*
* Carries the full variant metadata, not just id/name/window: the picker folds
* ~170 uids into ~42 rows and needs the group label, the cost multiplier, the
* promo and the vendor's own default flag to do it.
+ *
+ * **Honest limit on the classification.** `getServedDevinModels` catches its own
- * rpc errors and returns `rosterCache ?? []` (devin-models.ts:411-414), and
++ * rpc errors and returns `modelsCatalogCache ?? []` (devin-models.ts), and
+ * `postUnary` answers `null` rather than throwing, so a failed rpc still arrives
- * here as an empty list and is reported as `empty-roster`. What this DOES
++ * here as an empty list and is reported as `empty-models-catalog`. What this DOES
+ * distinguish is the two states that are knowable without touching that module:
+ * no credential at all, and a throw from anywhere in the chain.
*/
- async function fetchDevinRoster(): Promise<FetcherResult> {
-async function fetchDevinModelsCatalog(): Promise<DiscoveredModel[]> {
- const { getServedDevinModels } = await import("./devin/devin-models.js");
- const served = await getServedDevinModels();
- if (served.length === 0) return [];
++async function fetchDevinModelsCatalog(): Promise<FetcherResult> {
+ let endpoint: string | undefined;
+ try {
+ const { readDevinApiKey, readDevinServerUrl } = await import("./devin/devin-credentials.js");
+ endpoint = readDevinServerUrl();
+ // A logged-out account is a CREDENTIAL problem, and it is the kind that
+ // earns a "how to fix it" line. Reporting it as an empty roster says the
+ // subscription serves no models, which is a claim about Devin rather than
+ // about this machine.
+ if (!readDevinApiKey()) {
+ return {
+ kind: "failed",
+ failure: {
+ kind: "no-credentials",
+ endpoint,
+ // The definition's own `apiKeyDescription`, verbatim: there is no
+ // `claudish login devin` to point at — the token is the Devin CLI's,
+ // and claudish only reads it.
+ detail: "no Devin CLI session token (~/.local/share/devin/credentials.toml)",
+ },
+ };
+ }
+
+ const { getServedDevinModels } = await import("./devin/devin-models.js");
+ const served = await getServedDevinModels();
+ if (served.length === 0) return { kind: "models", models: [], endpoint };
- const { devinRosterEntry } = await import("./model-resolvers/devin.js");
- const { devinModelsCatalogEntry } = await import("./model-resolvers/devin.js");
- return served.map((model) => {
- const { wireId, ...rest } = devinModelsCatalogEntry(model);
- return { id: wireId, ...rest };
- });
++ const { devinModelsCatalogEntry } = await import("./model-resolvers/devin.js");
+ return {
+ kind: "models",
+ endpoint,
+ models: served.map((model) => {
- const { wireId, ...rest } = devinRosterEntry(model);
++ const { wireId, ...rest } = devinModelsCatalogEntry(model);
+ return { id: wireId, ...rest };
+ }),
+ };
+ } catch (err: unknown) {
+ return {
+ kind: "failed",
+ failure: { kind: "unreachable", endpoint, detail: oneLine(String(err)) },
+ };
+ }
}
/**
@@@ -117,96 -67,47 +118,96 @@@ function isUndeclaredEditorInternal(id
* in the catalog. `resolveDiscoveredContextLength` already prefers a discovered
* window over the catalog; this is what gives it one to prefer.
*/
- async function fetchAntigravityRoster(): Promise<FetcherResult> {
-async function fetchAntigravityModelsCatalog(): Promise<DiscoveredModel[]> {
- const { getValidAntigravityAccessToken } = await import("../auth/antigravity-token.js");
- const { setupAntigravityUser, getServedAntigravityModels } = await import(
- "../auth/antigravity-user.js"
- );
- const token = await getValidAntigravityAccessToken();
- if (!token) return [];
++async function fetchAntigravityModelsCatalog(): Promise<FetcherResult> {
+ let endpoint: string | undefined;
+ try {
+ const { getValidAntigravityAccessToken } = await import("../auth/antigravity-token.js");
+ const { setupAntigravityUser, getServedAntigravityModels, antigravityHost } = await import(
+ "../auth/antigravity-user.js"
+ );
+ endpoint = `${antigravityHost()}/v1internal:fetchAvailableModels`;
+ const token = await getValidAntigravityAccessToken();
+ // No token is exactly "logged out", which is one of the two kinds that earns
+ // the env-var / key-URL guidance. It used to read as an empty roster, i.e.
+ // "your subscription serves nothing".
+ if (!token) {
+ return {
+ kind: "failed",
+ failure: {
+ kind: "no-credentials",
+ endpoint,
+ detail: "not signed in — run `claudish login antigravity`",
+ },
+ };
+ }
- const { projectId } = await setupAntigravityUser(token);
- const { servedIds, meta, excludedIds } = await getServedAntigravityModels(token, projectId);
- // Declared first, guess second. `excludedIds` is the backend's own verdict —
- // internal flags, per-feature role bindings, and retired ids (which look
- // entirely normal but answer 400).
- const declaredExcluded = excludedIds ?? new Set<string>();
- const selectable = servedIds.filter(
- (id) => !declaredExcluded.has(id) && !isUndeclaredEditorInternal(id)
- );
- return selectable.map((id) => {
- const m = meta[id];
- // contextWindow is left UNSET when the backend reported none
- // (gemini-3.1-flash-image does), so the catalog still gets its turn rather
- // than the row rendering a fabricated 0 as "N/A".
- // Every id here is a tuned variant (`-high`, `-tiered`) the catalog does not
- // carry; see `ignoreCatalogReleaseDate` for the ordering this protects.
- return m?.contextWindow
- ? { id, contextWindow: m.contextWindow, ignoreCatalogReleaseDate: true }
- : { id, ignoreCatalogReleaseDate: true };
- });
+ const { projectId } = await setupAntigravityUser(token);
+ const { servedIds, meta, excludedIds } = await getServedAntigravityModels(token, projectId);
+ // Declared first, guess second. `excludedIds` is the backend's own verdict —
+ // internal flags, per-feature role bindings, and retired ids (which look
+ // entirely normal but answer 400).
+ const declaredExcluded = excludedIds ?? new Set<string>();
+ const selectable = servedIds.filter(
+ (id) => !declaredExcluded.has(id) && !isUndeclaredEditorInternal(id)
+ );
+ return {
+ kind: "models",
+ endpoint,
+ models: selectable.map((id) => {
+ const m = meta[id];
+ // contextWindow is left UNSET when the backend reported none
+ // (gemini-3.1-flash-image does), so the catalog still gets its turn rather
+ // than the row rendering a fabricated 0 as "N/A".
+ // Every id here is a tuned variant (`-high`, `-tiered`) the catalog does not
+ // carry; see `ignoreCatalogReleaseDate` for the ordering this protects.
+ return m?.contextWindow
+ ? { id, contextWindow: m.contextWindow, ignoreCatalogReleaseDate: true }
+ : { id, ignoreCatalogReleaseDate: true };
+ }),
+ };
+ } catch (err: unknown) {
+ return {
+ kind: "failed",
+ failure: { kind: "unreachable", endpoint, detail: oneLine(String(err)) },
+ };
+ }
}
-/** Ollama's daemon speaks its own listing shape and carries capability data no OpenAI list has. */
-async function fetchOllamaModelsCatalog(): Promise<DiscoveredModel[]> {
- const { fetchOllamaModels } = await import("./ollama-discovery.js");
- const installed = await fetchOllamaModels({ enrichCapabilities: false });
- return installed.map((model) => ({
- id: model.name,
- displayName: model.name,
- supportsTools: model.supportsTools,
- }));
+/**
+ * Ollama's daemon speaks its own listing shape and carries capability data no OpenAI list has.
+ *
+ * `throwOnError` is what makes "the daemon is not running" distinguishable from
+ * "the daemon is running and nothing is pulled". `fetchOllamaModels` is
+ * documented as never throwing, and every other caller still gets that; only
+ * this one opts in, because only this one has somewhere to report the
- * difference. Both states used to be `empty-roster`, so a stopped daemon read as
- * "this provider has no models" — the complaint, restated.
++ * difference. Both states used to be `empty-models-catalog`, so a stopped daemon
++ * read as "this provider has no models" — the complaint, restated.
+ */
- async function fetchOllamaRoster(): Promise<FetcherResult> {
++async function fetchOllamaModelsCatalog(): Promise<FetcherResult> {
+ const { fetchOllamaModels, ollamaBaseUrl } = await import("./ollama-discovery.js");
+ const endpoint = `${ollamaBaseUrl()}/api/tags`;
+ try {
+ const installed = await fetchOllamaModels({
+ enrichCapabilities: false,
+ throwOnError: true,
+ });
+ return {
+ kind: "models",
+ endpoint,
+ models: installed.map((model) => ({
+ id: model.name,
+ displayName: model.name,
+ supportsTools: model.supportsTools,
+ })),
+ };
+ } catch (err: unknown) {
+ return {
+ kind: "failed",
+ failure: { kind: "unreachable", endpoint, detail: oneLine(String(err)) },
+ };
+ }
}
- registerModelDiscoveryFetcher("devin-connect", fetchDevinRoster);
- registerModelDiscoveryFetcher("antigravity", fetchAntigravityRoster);
- registerModelDiscoveryFetcher("ollama-tags", fetchOllamaRoster);
+ registerModelDiscoveryFetcher("devin-connect", fetchDevinModelsCatalog);
+ registerModelDiscoveryFetcher("antigravity", fetchAntigravityModelsCatalog);
+ registerModelDiscoveryFetcher("ollama-tags", fetchOllamaModelsCatalog);
diff --cc packages/cli/src/providers/model-discovery-roster.test.ts
index 8dc6d0ac,00000000..568a43ca
mode 100644,000000..100644
--- a/packages/cli/src/providers/model-discovery-roster.test.ts
+++ b/packages/cli/src/providers/model-discovery-roster.test.ts
@@@ -1,360 -1,0 +1,367 @@@
+/**
+ * `discoverProviderRoster` — the discriminated outcome behind the empty array.
+ *
+ * `discoverProviderModels` answers `[]` for six structurally different things,
+ * and its callers could not tell them apart. These tests pin which outcome each
+ * condition produces, and in particular the three the fetcher half could not
+ * express at all before: a failing fetcher, a reachable-but-empty one, and a
+ * declared format nothing claims.
+ *
+ * Offline by construction: `globalThis.fetch` and `credentials.getRequestAuth`
+ * are replaced directly rather than through `mock.module()`, which bleeds into
+ * sibling Bun test files.
+ *
+ * Run: bun test packages/cli/src/providers/model-discovery-roster.test.ts
+ */
+
+import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";
+
+import { credentials } from "../auth/credentials/authority.js";
+import {
+ type FetcherResult,
+ type ModelDiscoveryDescriptor,
+ discoverProviderModels,
+ discoverProviderRoster,
+ getDiscoveryFailure,
+ invalidateModelDiscovery,
+ registerModelDiscoveryFetcher,
+} from "./model-discovery.js";
+import type { ProviderDefinition } from "./provider-definitions.js";
+import { clearRuntimeRegistry, registerRuntimeProvider } from "./runtime-providers.js";
+
+const realFetch = globalThis.fetch;
+const realGetRequestAuth = credentials.getRequestAuth;
+
+/**
+ * A provider definition that can never reach the network by accident: its host
+ * is `.invalid`, it builds no handler, and every test that lets it get as far as
+ * a request stubs `fetch` first.
+ */
+function defineProvider(
+ name: string,
+ overrides: Partial<ProviderDefinition> = {}
+): ProviderDefinition {
+ return {
+ name,
+ displayName: `Roster Test (${name})`,
+ transport: "openai",
+ baseUrl: "https://roster-test.invalid",
+ apiPath: "/v1/chat/completions",
+ apiKeyEnvVar: "ROSTER_TEST_API_KEY",
+ apiKeyDescription: "Offline test key",
+ apiKeyUrl: "https://roster-test.invalid/key",
+ shortcuts: [],
+ legacyPrefixes: [],
+ modelDiscovery: { path: "/v1/models", format: "openai-models-list" },
+ createHandler: { kind: "none", reason: "virtual", note: "Offline test fixture" },
+ isDirectApi: true,
+ ...overrides,
+ };
+}
+
+/** A format name no shipped provider declares, so the registry stays honest. */
+function testFormat(name: string): ModelDiscoveryDescriptor["format"] {
+ return name as ModelDiscoveryDescriptor["format"];
+}
+
+function stubResponse(body: string, status = 200): void {
+ globalThis.fetch = mock(
+ async () => new Response(body, { status, headers: { "Content-Type": "application/json" } })
+ ) as unknown as typeof fetch;
+}
+
+beforeEach(() => {
+ invalidateModelDiscovery();
+ clearRuntimeRegistry();
+ credentials.getRequestAuth = mock(async () => ({
+ headers: { Authorization: "Bearer offline-test-token" },
+ }));
+ globalThis.fetch = mock(async () => {
+ throw new Error("Unexpected fetch call");
+ }) as unknown as typeof fetch;
+});
+
+afterEach(() => {
+ invalidateModelDiscovery();
+ clearRuntimeRegistry();
+ globalThis.fetch = realFetch;
+ credentials.getRequestAuth = realGetRequestAuth;
+});
+
+describe("discoverProviderRoster — the GET half", () => {
+ test("a non-empty roster is `served`, and `served.models` is never empty", async () => {
+ stubResponse(JSON.stringify({ data: [{ id: "k3", context_length: 1_048_576 }] }));
+
+ const outcome = await discoverProviderRoster("kimi-coding");
+
+ expect(outcome.kind).toBe("served");
+ if (outcome.kind !== "served") throw new Error("unreachable");
+ expect(outcome.models.map((m) => m.id)).toEqual(["k3"]);
+ expect(outcome.models.length).toBeGreaterThan(0);
+ expect(getDiscoveryFailure("kimi-coding")).toBeUndefined();
+ });
+
+ test.each([
+ [401, "unauthorized"],
+ [403, "unauthorized"],
+ [500, "http-error"],
+ [404, "http-error"],
+ ] as const)("HTTP %i is failed{%s}", async (status, kind) => {
+ stubResponse(JSON.stringify({ error: "rejected" }), status);
+
+ const outcome = await discoverProviderRoster("kimi-coding");
+
+ expect(outcome).toMatchObject({
+ kind: "failed",
+ failure: { kind, provider: "kimi-coding", status },
+ });
+ });
+
+ test("a fetch rejection is failed{unreachable} carrying the message", async () => {
+ globalThis.fetch = mock(async () => {
+ throw new Error("Offline");
+ }) as unknown as typeof fetch;
+
+ expect(await discoverProviderRoster("kimi-coding")).toMatchObject({
+ kind: "failed",
+ failure: { kind: "unreachable", provider: "kimi-coding", detail: "Offline" },
+ });
+ });
+
+ test("a non-JSON 200 is failed{malformed}", async () => {
+ globalThis.fetch = mock(
+ async () => new Response("{not-json", { status: 200 })
+ ) as unknown as typeof fetch;
+
+ expect(await discoverProviderRoster("kimi-coding")).toMatchObject({
+ kind: "failed",
+ failure: { kind: "malformed", provider: "kimi-coding" },
+ });
+ });
+
+ test("an empty data array is failed{empty-roster}, not `served` with []", async () => {
+ stubResponse(JSON.stringify({ data: [] }));
+
+ const outcome = await discoverProviderRoster("kimi-coding");
+
+ expect(outcome).toMatchObject({
+ kind: "failed",
- failure: { kind: "empty-roster", provider: "kimi-coding" },
++ failure: { kind: "empty-models-catalog", provider: "kimi-coding" },
+ });
+ // The GET half has always had a URL to name, and still does.
+ if (outcome.kind !== "failed") throw new Error("unreachable");
+ expect(outcome.failure.endpoint).toContain("/v1/models");
+ });
+
+ test("blank credentials are failed{no-credentials} without a request", async () => {
+ const fetchMock = mock(async () => new Response("should not be called"));
+ globalThis.fetch = fetchMock as unknown as typeof fetch;
+ credentials.getRequestAuth = mock(async () => ({ headers: { authorization: " " } }));
+
+ expect(await discoverProviderRoster("kimi-coding")).toMatchObject({
+ kind: "failed",
+ failure: { kind: "no-credentials", provider: "kimi-coding" },
+ });
+ expect(fetchMock).not.toHaveBeenCalled();
+ });
+});
+
+describe("discoverProviderRoster — `unsupported` is not a failure", () => {
+ test.each(["openrouter", "not-a-real-provider"])(
+ "%s declares no discovery, so nothing is attempted or recorded",
+ async (provider) => {
+ const fetchMock = mock(async () => new Response("should not be called"));
+ globalThis.fetch = fetchMock as unknown as typeof fetch;
+
+ expect(await discoverProviderRoster(provider)).toEqual({
+ kind: "unsupported",
+ reason: "no-descriptor",
+ });
+ expect(fetchMock).not.toHaveBeenCalled();
+ // Nothing to explain to the user, so nothing is recorded for the classic
+ // path's warning to pick up either.
+ expect(getDiscoveryFailure(provider)).toBeUndefined();
+ }
+ );
+
- test("a declared descriptor with no resolvable base URL is unsupported{no-base-url}", async () => {
++ test("a declared descriptor with no resolvable base URL is failed{unreachable}, naming where to fix it", async () => {
++ // Not `unsupported`: the provider DID declare discovery, so this is a setup
++ // fault the user can fix — reporting it silently read as "the plan lists no
++ // models". With no override variables to name, the detail names the field.
+ const provider = defineProvider("roster-no-base-url-test", {
+ baseUrl: "",
+ baseUrlEnvVars: [],
+ });
+ registerRuntimeProvider(provider);
+
+ expect(await discoverProviderRoster(provider.name)).toEqual({
- kind: "unsupported",
- reason: "no-base-url",
++ kind: "failed",
++ failure: {
++ kind: "unreachable",
++ provider: provider.name,
++ detail: "no base URL resolved — check the provider's baseUrl",
++ },
+ });
- expect(getDiscoveryFailure(provider.name)).toBeUndefined();
++ expect(getDiscoveryFailure(provider.name)?.kind).toBe("unreachable");
+ });
+
+ test("a declared format nothing claims is unsupported{no-fetcher}, not an empty roster", async () => {
+ // This is a PACKAGING bug — a definition opted into a format whose owner was
+ // never bundled. It used to be recorded as `empty-roster`, i.e. as a claim
+ // about the user's subscription.
+ const provider = defineProvider("roster-unclaimed-format-test", {
+ modelDiscovery: { path: "", format: testFormat("roster-unclaimed-format") },
+ });
+ registerRuntimeProvider(provider);
+
+ expect(await discoverProviderRoster(provider.name)).toEqual({
+ kind: "unsupported",
+ reason: "no-fetcher",
+ });
+ expect(getDiscoveryFailure(provider.name)).toBeUndefined();
+ });
+});
+
+describe("discoverProviderRoster — the fetcher half can finally classify", () => {
+ test("a fetcher reporting failure surfaces as failed with ITS kind, not empty-roster", async () => {
+ const format = testFormat("roster-failing-fetcher");
+ registerModelDiscoveryFetcher(format, async (): Promise<FetcherResult> => {
+ return {
+ kind: "failed",
+ failure: {
+ kind: "unreachable",
+ endpoint: "http://localhost:11434/api/tags",
+ detail: "connect ECONNREFUSED",
+ },
+ };
+ });
+ const provider = defineProvider("roster-failing-fetcher-test", {
+ modelDiscovery: { path: "", format },
+ });
+ registerRuntimeProvider(provider);
+
+ const outcome = await discoverProviderRoster(provider.name);
+
+ expect(outcome).toMatchObject({
+ kind: "failed",
+ failure: {
+ kind: "unreachable",
+ // The caller fills in the provider — the fetcher cannot know its own
+ // registration name.
+ provider: provider.name,
+ endpoint: "http://localhost:11434/api/tags",
+ detail: "connect ECONNREFUSED",
+ },
+ });
+ expect(getDiscoveryFailure(provider.name)?.kind).toBe("unreachable");
+ });
+
+ test("a fetcher reporting no-credentials earns the credential kind", async () => {
+ const format = testFormat("roster-logged-out-fetcher");
+ registerModelDiscoveryFetcher(format, async () => ({
+ kind: "failed" as const,
+ failure: { kind: "no-credentials" as const, detail: "no OAuth token" },
+ }));
+ registerRuntimeProvider(
+ defineProvider("roster-logged-out-test", { modelDiscovery: { path: "", format } })
+ );
+
+ expect(await discoverProviderRoster("roster-logged-out-test")).toMatchObject({
+ kind: "failed",
+ failure: { kind: "no-credentials", provider: "roster-logged-out-test" },
+ });
+ });
+
+ test("a reachable-but-empty fetcher gives empty-roster WITH the endpoint it asked", async () => {
+ const format = testFormat("roster-empty-fetcher");
+ registerModelDiscoveryFetcher(format, async () => ({
+ kind: "models" as const,
+ models: [],
+ endpoint: "http://localhost:11434/api/tags",
+ }));
+ registerRuntimeProvider(
+ defineProvider("roster-empty-fetcher-test", { modelDiscovery: { path: "", format } })
+ );
+
+ expect(await discoverProviderRoster("roster-empty-fetcher-test")).toEqual({
+ kind: "failed",
+ failure: {
- kind: "empty-roster",
++ kind: "empty-models-catalog",
+ provider: "roster-empty-fetcher-test",
+ // Before `FetcherResult`, this half recorded empty-roster with NO
+ // endpoint, so any copy naming one rendered the literal `undefined`.
+ endpoint: "http://localhost:11434/api/tags",
+ },
+ });
+ });
+
+ test("a fetcher returning models is served", async () => {
+ const format = testFormat("roster-serving-fetcher");
+ registerModelDiscoveryFetcher(format, async () => ({
+ kind: "models" as const,
+ models: [{ id: "llama3.2", supportsTools: true }],
+ endpoint: "http://localhost:11434/api/tags",
+ }));
+ registerRuntimeProvider(
+ defineProvider("roster-serving-fetcher-test", { modelDiscovery: { path: "", format } })
+ );
+
+ expect(await discoverProviderRoster("roster-serving-fetcher-test")).toEqual({
+ kind: "served",
+ models: [{ id: "llama3.2", supportsTools: true }],
+ });
+ });
+
+ test("NEVER REJECTS: a throwing fetcher resolves to failed{unreachable}", async () => {
+ const format = testFormat("roster-throwing-fetcher");
+ registerModelDiscoveryFetcher(format, async () => {
+ throw new Error("protobuf decode blew up");
+ });
+ registerRuntimeProvider(
+ defineProvider("roster-throwing-test", { modelDiscovery: { path: "", format } })
+ );
+
+ // `.resolves` is the assertion, not a convenience: before the wrapping try,
+ // this rejection propagated out of the picker and, behind a live renderer
+ // with no stderr, would leave a progress indicator running forever.
+ await expect(discoverProviderRoster("roster-throwing-test")).resolves.toMatchObject({
+ kind: "failed",
+ failure: {
+ kind: "unreachable",
+ provider: "roster-throwing-test",
+ detail: "protobuf decode blew up",
+ },
+ });
+ });
+});
+
+describe("discoverProviderModels stays the fail-soft wrapper", () => {
+ test.each([
+ ["served", JSON.stringify({ data: [{ id: "k3" }] }), 200, ["k3"]],
+ ["failed", JSON.stringify({ error: "nope" }), 401, []],
+ ["empty-roster", JSON.stringify({ data: [] }), 200, []],
+ ] as const)("%s → the models or []", async (_label, body, status, ids) => {
+ stubResponse(body, status);
+
+ expect((await discoverProviderModels("kimi-coding")).map((m) => m.id)).toEqual([...ids]);
+ });
+
+ test("unsupported → []", async () => {
+ expect(await discoverProviderModels("openrouter")).toEqual([]);
+ });
+
+ test("both entry points share ONE cache, so asking twice costs one request", async () => {
+ let calls = 0;
+ globalThis.fetch = mock(async () => {
+ calls++;
+ return new Response(JSON.stringify({ data: [{ id: `model-${calls}` }] }), { status: 200 });
+ }) as unknown as typeof fetch;
+
+ const first = await discoverProviderRoster("kimi-coding");
+ const second = await discoverProviderModels("kimi-coding");
+
+ expect(calls).toBe(1);
+ expect(first).toMatchObject({ kind: "served" });
+ expect(second.map((m) => m.id)).toEqual(["model-1"]);
+ });
+});
diff --cc packages/cli/src/providers/model-discovery.ts
index b7879a0c,9b80b18d..12533ff7
--- a/packages/cli/src/providers/model-discovery.ts
+++ b/packages/cli/src/providers/model-discovery.ts
@@@ -136,34 -134,12 +137,34 @@@ export interface ModelDiscoveryDescript
}
/**
- * A dynamic models catalog fetcher for a format this module does not know how to speak.
+ * What a registered fetcher answers with.
*
- * Returning `[]` means "asked, got nothing" — the caller records an
- * `empty-models-catalog` failure. Throwing is also fine; the caller converts it.
+ * It used to be a bare `DiscoveredModel[]`, and that shape could not express
+ * failure: `[]` was the only thing a fetcher could say, so the caller recorded
- * `empty-roster` for it — the one kind that means "the endpoint answered
++ * `empty-models-catalog` for it — the one kind that means "the endpoint answered
+ * correctly and has nothing to offer". Every fetcher on this path swallows its
+ * own errors on the way in (`fetchOllamaModels` is documented "never throws";
+ * `getServedDevinModels` and `getServedAntigravityModels` both degrade to an
+ * empty list), so a STOPPED Ollama daemon and a logged-out Antigravity account
+ * both arrived as "the endpoint answered and listed nothing" about an endpoint
+ * that never answered. That is the same information loss the discriminated
+ * outcome below exists to remove, one layer further in.
+ *
- * - `models` with a non-empty list is a served roster.
- * - `models` with `[]` is a genuine empty roster — and `endpoint` is what makes
++ * - `models` with a non-empty list is a served dynamic models catalog.
++ * - `models` with `[]` is a genuinely empty one — and `endpoint` is what makes
+ * it sayable: the notice can finally name the URL that answered.
- * - `failed` carries any of the six kinds. `provider` is filled in by the
++ * - `failed` carries any of the seven kinds. `provider` is filled in by the
+ * caller, which is the only place that knows it.
+ *
+ * Throwing is still fine: {@link discoverProviderRoster} maps a rejection to
+ * `unreachable` and never rejects itself.
*/
-export type ModelDiscoveryFetcher = (providerName: string) => Promise<DiscoveredModel[]>;
+export type FetcherResult =
+ | { kind: "models"; models: DiscoveredModel[]; endpoint?: string }
+ | { kind: "failed"; failure: Omit<DiscoveryFailure, "provider"> };
+
+/** A roster fetcher for a format this module does not know how to speak. */
+export type ModelDiscoveryFetcher = (providerName: string) => Promise<FetcherResult>;
const _fetchers = new Map<string, ModelDiscoveryFetcher>();
@@@ -240,38 -218,6 +243,40 @@@ export interface DiscoveryFailure
detail?: string;
}
+/**
+ * Why a discovery attempt produced what it produced — the whole answer, in one
+ * value, handed to the caller rather than left in module state to be re-read.
+ *
+ * `discoverProviderModels` answers `[]` to all of "this provider declares no
+ * discovery", "its endpoint rejected your key", "it was unreachable", "it
+ * answered with garbage" and "it answered correctly with nothing", and no
+ * caller can tell them apart. The picker rendered every one of them as "here
+ * are fewer models", which is the complaint this type exists to answer.
+ *
+ * Three variants, because there are three different things to say:
+ *
+ * - `served` — a roster. **Non-empty by construction**: every path that ends up
+ * with zero models records a failure instead, so there is no `served` with
+ * `[]` to guard against downstream.
- * - `failed` — one of the six {@link DiscoveryFailureKind}s, with the failure
++ * - `failed` — one of the seven {@link DiscoveryFailureKind}s, with the failure
+ * captured AT THE CALL. Read it from here, never from
+ * {@link getDiscoveryFailure} afterwards: that map is module-global and its
+ * entry is deleted by the next successful call for the same provider, so a
+ * re-read races every concurrent caller.
+ * - `unsupported` — nothing was attempted, and nothing is wrong. ~25 pickable
+ * providers declare no `modelDiscovery`; for them the cloud catalog is the
+ * normal, correct answer and a notice would be noise on the majority case.
+ * `no-fetcher` is the exception that IS a bug — a declared format nothing
+ * claims is a packaging mistake, not a roster fact — which is why it is here
- * rather than folded into `empty-roster` as it used to be.
++ * rather than folded into `empty-models-catalog` as it used to be. A declared
++ * descriptor whose base URL does not resolve is NOT here: that is a setup
++ * fault the user can fix, so it is `failed{unreachable}` naming the variable.
+ */
+export type RosterOutcome =
+ | { kind: "served"; models: DiscoveredModel[] }
+ | { kind: "failed"; failure: DiscoveryFailure }
- | { kind: "unsupported"; reason: "no-descriptor" | "no-base-url" | "no-fetcher" };
++ | { kind: "unsupported"; reason: "no-descriptor" | "no-fetcher" };
+
/**
* Last failure per provider.
*
@@@ -447,91 -454,63 +528,103 @@@ function describeIncompleteness(body: u
}
/**
- * List the models this provider serves for the CURRENT credentials.
+ * The non-GET half: a format this module cannot speak itself, served by a
+ * registered fetcher.
*
- * Devin's roster is two protobuf rpcs (capability ∩ entitlement); Antigravity's
- * is an OAuth POST; Ollama's is its daemon's own listing shape. The imports are
- * DYNAMIC to keep the codec and the OAuth path off the cold-start path, and this
- * module deliberately knows NONE of them by name — the builtin bundle is
- * imported on first miss, which inverts the dependency so that adding provider
- * #4 means editing provider #4.
- * Returns [] when the provider declares no `modelDiscovery`, has no usable
- * credentials, or the endpoint is unreachable/malformed/incomplete — callers
- * fall back to the cloud models catalog. Every one of those except "declares no
- * `modelDiscovery`" records a reason for `getDiscoveryFailure`, so `[]` is
- * never silently read as "the plan lists nothing".
++ * Devin's dynamic models catalog is two protobuf rpcs (capability ∩
++ * entitlement); Antigravity's is an OAuth POST; Ollama's is its daemon's own
++ * listing shape. The imports are DYNAMIC to keep the codec and the OAuth path
++ * off the cold-start path, and this module deliberately knows NONE of them by
++ * name — the builtin bundle is imported on first miss, which inverts the
++ * dependency so that adding provider #4 means editing provider #4.
+ *
+ * Never rejects: the dynamic import and the fetcher call are both inside the
+ * `try`, and both can throw.
*/
-export async function discoverProviderModels(providerName: string): Promise<DiscoveredModel[]> {
- const cached = _cache.get(providerName);
- if (cached && cached.expiresAt > Date.now()) return cached.models;
-
- const def = getProviderByName(providerName);
- const descriptor = def?.modelDiscovery;
- if (!def || !descriptor) return [];
-
- // Devin's dynamic models catalog is not a GET — it is two protobuf rpcs (capability ∩
- // entitlement), owned by the module that speaks that wire. The import is
- // DYNAMIC to keep the codec off the cold-start path and out of a static
- // cycle. (If a third such provider ever appears, replace this branch with a
- // `registerModelDiscoveryFetcher(name, fn)` seam — not worth it for one.)
- // Formats this module cannot speak itself are served by registered fetchers.
- // It deliberately knows NONE of them by name: the builtin bundle is imported
- // on first miss, which both inverts the dependency and keeps the protobuf
- // codec and the Antigravity OAuth path off the cold-start path.
- if (descriptor.format !== "openai-models-list") {
- let fetcher = getModelDiscoveryFetcher(descriptor.format);
+async function discoverViaFetcher(providerName: string, format: string): Promise<RosterOutcome> {
+ let result: FetcherResult;
+ try {
+ let fetcher = getModelDiscoveryFetcher(format);
if (!fetcher) {
await import("./model-discovery-builtins.js");
- fetcher = getModelDiscoveryFetcher(descriptor.format);
+ fetcher = getModelDiscoveryFetcher(format);
}
if (!fetcher) {
- // A declared format nothing claims. Report it as an empty dynamic models
- // catalog rather than falling through to the GET path, which would hit a bogus URL.
- return recordFailure({ kind: "empty-models-catalog", provider: providerName });
- }
- const models = await fetcher(providerName);
- if (models.length === 0) {
- return recordFailure({ kind: "empty-models-catalog", provider: providerName });
+ // A declared format nothing claims. This is a PACKAGING bug — a
+ // definition opted into a format whose owner was never bundled — not a
- // statement about the user's roster, so it is `unsupported`, not the
- // `empty-roster` it used to be recorded as. It renders nothing and is
- // caught by a build-integrity test rather than shown to the user.
++ // statement about the user's dynamic models catalog, so it is
++ // `unsupported`, not the `empty-models-catalog` it used to be recorded as.
++ // It renders nothing and is caught by a build-integrity test rather than
++ // shown to the user.
+ log(`[model-discovery:${providerName}] no fetcher claims format "${format}"`);
+ return { kind: "unsupported", reason: "no-fetcher" };
}
- _failures.delete(providerName);
- log(`[model-discovery:${providerName}] discovered ${models.length} models`);
- _cache.set(providerName, { models, expiresAt: Date.now() + CACHE_TTL_MS });
- return models;
+ result = await fetcher(providerName);
+ } catch (e: unknown) {
+ return recordFailure({
+ kind: "unreachable",
+ provider: providerName,
+ detail: oneLine((e as Error)?.message ?? String(e)),
+ });
+ }
+
+ if (result.kind === "failed") {
+ return recordFailure({ ...result.failure, provider: providerName });
+ }
+ const models = result.models;
+ if (models.length === 0) {
+ // A reachable endpoint that listed nothing — and now it can say WHERE,
+ // because the fetcher reports the URL it asked.
+ return recordFailure({
- kind: "empty-roster",
++ kind: "empty-models-catalog",
+ provider: providerName,
+ endpoint: result.endpoint,
+ });
+ }
+ _failures.delete(providerName);
+ log(`[model-discovery:${providerName}] discovered ${models.length} models`);
+ _cache.set(providerName, { models, expiresAt: Date.now() + CACHE_TTL_MS });
+ return { kind: "served", models };
+}
+
+/**
+ * List the models this provider serves for the CURRENT credentials, as a
+ * discriminated outcome — see {@link RosterOutcome}.
+ *
+ * **This function NEVER REJECTS.** Every `await` is inside a `try`, including
+ * the dynamic import of the builtin fetcher bundle and the fetcher call itself,
+ * and a throw becomes `failed{kind:"unreachable"}`. Two of those awaits used to
+ * be bare: Devin speaks protobuf rpcs and Antigravity does an OAuth POST, so a
+ * rejection propagated out of the picker. A line-oriented prompt survives that;
+ * a live renderer with no stderr does not — it leaves a progress indicator
+ * running against a promise that never settles. Callers may therefore have no
+ * rejection branch, and a Tier-1 test registers a throwing fetcher to pin it.
+ */
+export async function discoverProviderRoster(providerName: string): Promise<RosterOutcome> {
+ const cached = _cache.get(providerName);
+ if (cached && cached.expiresAt > Date.now()) return { kind: "served", models: cached.models };
+
+ const def = getProviderByName(providerName);
+ const descriptor = def?.modelDiscovery;
+ if (!def || !descriptor) return { kind: "unsupported", reason: "no-descriptor" };
+
+ if (descriptor.format !== "openai-models-list") {
+ return await discoverViaFetcher(providerName, descriptor.format);
}
const baseUrl = resolveBaseUrl(providerName);
- if (!baseUrl) return { kind: "unsupported", reason: "no-base-url" };
+ if (!baseUrl) {
+ // This used to return [] with nothing recorded, i.e. "this plan lists no
- // models" — a configuration fault reported as an entitlement fact.
++ // models" — a configuration fault reported as an entitlement fact. It is a
++ // `failed` outcome, not `unsupported`: the provider DID declare discovery,
++ // so the user has something to fix, and the notice names where to fix it.
+ const overrides = (def.baseUrlEnvVars ?? []).join(", ");
+ return recordFailure({
+ kind: "unreachable",
+ provider: providerName,
+ detail: `no base URL resolved — check ${overrides || "the provider's baseUrl"}`,
+ });
+ }
const endpoint = `${baseUrl}${descriptor.path}`;
// Auth via the credential authority — it owns OAuth-vs-API-key precedence
@@@ -648,25 -644,7 +758,27 @@@
models.map((m) => `${m.id}(${m.contextWindow ?? "?"})`).join(", ")
);
_cache.set(providerName, { models, expiresAt: Date.now() + CACHE_TTL_MS });
- return models;
+ return { kind: "served", models };
+}
+
+/**
+ * List the models this provider serves for the CURRENT credentials.
+ *
+ * Returns [] when the provider declares no `modelDiscovery`, has no usable
- * credentials, or the endpoint is unreachable/malformed — callers fall back to
- * the cloud catalog.
++ * credentials, or the endpoint is unreachable/malformed/incomplete — callers
++ * fall back to the cloud models catalog. Every one of those except "declares no
++ * `modelDiscovery`" records a reason for `getDiscoveryFailure`, so `[]` is
++ * never silently read as "the plan lists nothing".
+ *
+ * The fail-soft shape every non-picker caller wants (the launcher, the status
+ * line, `discoverContextWindow`): discovery is an enhancement and must never
+ * block a launch. A caller that needs to TELL the user why there is nothing —
+ * i.e. the picker — calls {@link discoverProviderRoster} instead and keeps the
+ * distinction. Both share one cache, so asking twice costs one request.
+ */
+export async function discoverProviderModels(providerName: string): Promise<DiscoveredModel[]> {
+ const outcome = await discoverProviderRoster(providerName);
+ return outcome.kind === "served" ? outcome.models : [];
}
/**
diff --cc packages/cli/src/providers/route-candidates.test.ts
index 00000000,efb44b83..53b0e1df
mode 000000,100644..100644
--- a/packages/cli/src/providers/route-candidates.test.ts
+++ b/packages/cli/src/providers/route-candidates.test.ts
@@@ -1,0 -1,416 +1,419 @@@
+ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
+ import { mkdtempSync, rmSync } from "node:fs";
+ import { tmpdir } from "node:os";
+ import { join } from "node:path";
+ import { credentials } from "../auth/credentials/authority.js";
+ import { type DiskCacheV3, type SlimModelEntry, writeAllModelsCache } from "./all-models-cache.js";
+ import { _resetCatalogClient, _setCatalogEntriesForTest } from "./catalog-client.js";
+ import { providersForCatalogRoute } from "./catalog-route-bindings.js";
+ import {
+ getModelDiscoveryFetcher,
+ invalidateModelDiscovery,
+ registerModelDiscoveryFetcher,
+ } from "./model-discovery.js";
+ import "./model-discovery-builtins.js";
+ import {
+ type RouteCandidate,
+ catalogDeniesProvider,
+ compareRouteCandidates,
+ gatherRouteCandidates,
+ routeOwnership,
+ } from "./route-candidates.js";
+ import { buildCatalogChain, route } from "./routing-rules.js";
+
+ const GENERATION = "g-20260921062451697-f490edba";
+
+ /**
+ * Trimmed verbatim from ~/.claudish/cloud-models-catalog-v3.json at GENERATION.
+ * The comments name every canonical model id used by a test; fields irrelevant
+ * to gathering are omitted, while route bindings, wire ids, prices, vendor and
+ * context windows remain exactly as published.
+ */
+ const REAL_ENTRIES: SlimModelEntry[] = [
+ // gpt-6-astra: subscription + native API + gateways.
+ {
+ modelId: "gpt-6-astra",
+ provider: "openai",
+ aliases: ["openai/gpt-6-astra", "~openai/gpt-astra-latest"],
+ contextWindow: 1_050_000,
+ subscriptionPlanIds: ["openai-codex"],
+ aggregators: [
+ {
+ sourceProviderId: "openai-codex",
+ sourceCollectorId: "openai-codex-models",
+ confidence: "scrape_verified",
+ routeStatus: "mapped",
+ route: { routeId: "openai", routeProfileId: "codex-subscription" },
+ externalModelId: "gpt-6-astra",
+ },
+ {
+ sourceProviderId: "openai",
+ sourceCollectorId: "openai-api",
+ confidence: "api_official",
+ routeStatus: "mapped",
+ route: { routeId: "openai", routeProfileId: "direct-api" },
+ externalModelId: "gpt-6-astra",
+ pricing: { type: "flat", input: 10, output: 50 },
+ },
+ {
+ sourceProviderId: "opencode-zen",
+ sourceCollectorId: "opencode-zen-pricing-scrape",
+ confidence: "gateway_official",
+ routeStatus: "mapped",
+ route: { routeId: "opencode", routeProfileId: "zen" },
+ externalModelId: "gpt-6-astra",
+ pricing: { type: "flat", input: 10, output: 50 },
+ },
+ {
+ sourceProviderId: "openrouter",
+ sourceCollectorId: "openrouter-api",
+ confidence: "aggregator_reported",
+ routeStatus: "mapped",
+ route: { routeId: "openrouter", routeProfileId: "gateway" },
+ externalModelId: "openai/gpt-6-astra",
+ pricing: { type: "flat", input: 10, output: 50 },
+ },
+ ],
+ },
+ // glm-4.7: z-ai/direct-api is bound to both z-ai and glm credential silos.
+ {
+ modelId: "glm-4.7",
+ provider: "z-ai",
+ aliases: ["z-ai/glm-4.7", "zai-org/GLM-4.7", "GLM-4.7"],
+ contextWindow: 202_752,
+ aggregators: [
+ {
+ sourceProviderId: "openrouter",
+ sourceCollectorId: "openrouter-api",
+ confidence: "aggregator_reported",
+ routeStatus: "mapped",
+ route: { routeId: "openrouter", routeProfileId: "gateway" },
+ externalModelId: "z-ai/glm-4.7",
+ pricing: { type: "flat", input: 0.4, output: 1.75 },
+ },
+ {
+ sourceProviderId: "z-ai",
+ sourceCollectorId: "zhipu-api",
+ confidence: "api_official",
+ routeStatus: "mapped",
+ route: { routeId: "z-ai", routeProfileId: "direct-api" },
+ externalModelId: "glm-4.7",
+ pricing: { type: "unavailable" },
+ },
+ ],
+ },
+ // kimi-k3: vendor connection plus priced and unpriced alternatives.
+ {
+ modelId: "kimi-k3",
+ provider: "moonshotai",
+ aliases: ["moonshotai/kimi-k3", "~moonshotai/kimi-latest", "k3"],
+ contextWindow: 1_048_576,
+ aggregators: [
+ {
+ sourceProviderId: "moonshotai",
+ sourceCollectorId: "moonshot-api",
+ confidence: "api_official",
+ routeStatus: "mapped",
+ route: { routeId: "moonshotai", routeProfileId: "direct-api" },
+ externalModelId: "kimi-k3",
+ pricing: { type: "unavailable" },
+ },
+ {
+ sourceProviderId: "opencode-zen",
+ sourceCollectorId: "opencode-zen-pricing-scrape",
+ confidence: "gateway_official",
+ routeStatus: "mapped",
+ route: { routeId: "opencode", routeProfileId: "zen" },
+ externalModelId: "kimi-k3",
+ pricing: { type: "flat", input: 3, output: 15 },
+ },
+ {
+ sourceProviderId: "openrouter",
+ sourceCollectorId: "openrouter-api",
+ confidence: "aggregator_reported",
+ routeStatus: "mapped",
+ route: { routeId: "openrouter", routeProfileId: "gateway" },
+ externalModelId: "moonshotai/kimi-k3",
+ pricing: { type: "flat", input: 1.7, output: 8.5 },
+ },
+ ],
+ },
+ // qwen3.8-max: subscriptions + native API, positively no OpenRouter connection.
+ {
+ modelId: "qwen3.8-max",
+ provider: "qwen",
+ aliases: ["accounts/fireworks/models/qwen3p8-max"],
+ contextWindow: 1_000_000,
+ subscriptionPlanIds: [
+ "alibaba-token-plan-individual",
+ "alibaba-token-plan-team-edition",
+ "opencode-go",
+ ],
+ aggregators: [
+ {
+ sourceProviderId: "opencode-go",
+ sourceCollectorId: "popular-coding-subscriptions",
+ confidence: "gateway_official",
+ routeStatus: "mapped",
+ route: { routeId: "opencode", routeProfileId: "go-subscription" },
+ externalModelId: "qwen3.8-max",
+ },
+ {
+ sourceProviderId: "qwen-cloud",
+ sourceCollectorId: "qwen-cloud-token-plan-scrape",
+ confidence: "scrape_verified",
+ routeStatus: "mapped",
+ route: { routeId: "qwen", routeProfileId: "qwencloud-token-plan" },
+ externalModelId: "qwen3.8-max",
+ },
+ {
+ sourceProviderId: "qwen",
+ sourceCollectorId: "dashscope-api",
+ confidence: "api_official",
+ routeStatus: "mapped",
+ route: { routeId: "qwen", routeProfileId: "dashscope-direct" },
+ externalModelId: "qwen3.8-max",
+ pricing: { type: "flat", input: 2, output: 6 },
+ },
+ ],
+ },
+ // gemini-3.8-flash: metered connections plus Antigravity's namespace claim.
+ {
+ modelId: "gemini-3.8-flash",
+ provider: "google",
+ aliases: ["google/gemini-3.8-flash"],
+ contextWindow: 1_048_576,
+ aggregators: [
+ {
+ sourceProviderId: "google",
+ sourceCollectorId: "google-model-get-api",
+ confidence: "api_official",
+ routeStatus: "mapped",
+ route: { routeId: "google", routeProfileId: "direct-api" },
+ externalModelId: "gemini-3.8-flash",
+ pricing: { type: "flat", input: 0.75, output: 3.75 },
+ },
+ {
+ sourceProviderId: "openrouter",
+ sourceCollectorId: "openrouter-api",
+ confidence: "aggregator_reported",
+ routeStatus: "mapped",
+ route: { routeId: "openrouter", routeProfileId: "gateway" },
+ externalModelId: "google/gemini-3.8-flash",
+ pricing: { type: "flat", input: 0.75, output: 3.75 },
+ },
+ ],
+ },
+ ];
+
+ const tempDir = mkdtempSync(join(tmpdir(), "claudish-route-candidates-"));
+ const cachePath = join(tempDir, "cloud-models-catalog-v3.json");
+ const missingCachePath = join(tempDir, "missing-catalog.json");
+
+ beforeAll(() => {
+ const cache: DiskCacheV3 = {
+ version: 3,
+ catalogGenerationId: GENERATION,
+ lastUpdated: "2026-09-21T06:24:51.697Z",
+ entries: REAL_ENTRIES,
+ models: [],
+ plans: [],
+ };
+ writeAllModelsCache(cache, cachePath);
+ });
+
+ afterAll(() => {
+ _resetCatalogClient();
+ rmSync(tempDir, { recursive: true, force: true });
+ });
+
+ function gatheredCandidate(model: string, provider: string): RouteCandidate {
+ const candidate = gatherRouteCandidates(model, cachePath).candidates.find(
+ (item) => item.provider === provider
+ );
+ if (!candidate) throw new Error(`fixture produced no ${provider} candidate for ${model}`);
+ return candidate;
+ }
+
+ describe("catalog route candidate gathering and order", () => {
+ test("puts a flat-rate subscription before a metered connection", () => {
+ const candidates = gatherRouteCandidates("gpt-6-astra", cachePath).candidates;
+
+ expect(candidates[0]?.provider).toBe("openai-codex");
+ expect(candidates.map(({ provider }) => provider)).toContain("openrouter");
+ });
+
+ test("gathers every provider bound to z-ai/direct-api", () => {
+ const binding = { routeId: "z-ai", routeProfileId: "direct-api" };
+ expect(providersForCatalogRoute(binding)).toEqual(["z-ai", "glm"]);
+
+ const direct = gatherRouteCandidates("glm-4.7", cachePath).candidates.filter(
+ ({ wireId }) => wireId === "glm-4.7"
+ );
+ expect(direct.map(({ provider }) => provider).sort()).toEqual(["glm", "z-ai"]);
+ });
+
+ test("the vendor comes first when candidates share a tier", () => {
+ const vendor = gatheredCandidate("kimi-k3", "kimi");
+ const gateway = gatheredCandidate("kimi-k3", "openrouter");
+
+ // No current row puts a vendor-native connection and a gateway in one tier:
+ // RouteTier defines those separately. Equalising only the tier isolates the
+ // comparator contract while retaining kimi-k3's real wire ids, prices and
+ // moonshotai vendor binding.
+ const tied = [
+ { ...gateway, tier: "gateway" as const },
+ { ...vendor, tier: "gateway" as const },
+ ].sort(compareRouteCandidates);
+ expect(tied[0]?.provider).toBe("kimi");
+ });
+
+ test("orders known prices cheapest-first and every unknown price last", () => {
+ const openrouter = gatheredCandidate("kimi-k3", "openrouter");
+ const zen = gatheredCandidate("kimi-k3", "opencode-zen");
+ const unknown = gatheredCandidate("kimi-k3", "kimi");
+ const comparable = [openrouter, zen, unknown]
+ .map((candidate) => ({ ...candidate, tier: "gateway" as const, isVendorOwn: false }))
+ .sort(compareRouteCandidates);
+
+ expect(comparable.map(({ provider }) => provider)).toEqual([
+ "openrouter",
+ "opencode-zen",
+ "kimi",
+ ]);
+ expect(comparable.at(-1)?.price.known).toBe(false);
+ });
+
+ test("uses larger context next and provider name as the deterministic final tie-break", () => {
+ const kimi = gatheredCandidate("kimi-k3", "kimi");
+ const zai = gatheredCandidate("glm-4.7", "z-ai");
+
+ // Generation g-20260921062451697-f490edba publishes zero per-connection
+ // windows. The AggregatorEntry contract says omission inherits the model's
+ // headline window, so these are the real 1,048,576 and 202,752 values.
+ const neitherVendor = [zai, kimi].map((candidate) => ({
+ ...candidate,
+ isVendorOwn: false,
+ }));
+ expect(neitherVendor.sort(compareRouteCandidates).map(({ provider }) => provider)).toEqual([
+ "kimi",
+ "z-ai",
+ ]);
+
+ const sameContext = neitherVendor
+ .map((candidate) => ({ ...candidate, contextWindow: kimi.contextWindow }))
+ .sort(compareRouteCandidates);
+ expect(sameContext.map(({ provider }) => provider)).toEqual(["kimi", "z-ai"]);
+ });
+ });
+
+ describe("namespace claims and availability", () => {
+ test("a namespace claim is gathered but the dynamic models catalog may remove it", async () => {
+ const gathered = gatherRouteCandidates("gemini-3.8-flash", cachePath);
+ expect(gathered.candidates).toContainEqual(
+ expect.objectContaining({
+ provider: "antigravity",
+ tier: "dynamic-subscription",
+ source: "namespace-claim",
+ })
+ );
+
+ const originalFetcher = getModelDiscoveryFetcher("antigravity");
+ const originalIsAvailable = credentials.isAvailable;
+ const originalDescribeReadiness = credentials.describeReadiness;
+ if (!originalFetcher) throw new Error("Antigravity discovery fetcher was not registered");
+
+ try {
- registerModelDiscoveryFetcher("antigravity", async () => [{ id: "gemini-2.5-flash" }]);
++ registerModelDiscoveryFetcher("antigravity", async () => ({
++ kind: "models",
++ models: [{ id: "gemini-2.5-flash" }],
++ }));
+ invalidateModelDiscovery("antigravity");
+ credentials.isAvailable = async (provider: string) => provider === "antigravity";
+ credentials.describeReadiness = async (provider: string) => ({
+ readiness: provider === "antigravity" ? "present" : "absent",
+ });
+
+ const plan = await route("gemini-3.8-flash", {}, "", cachePath);
+ expect(plan.kind).toBe("no-route");
+ if (plan.kind === "no-route") {
+ expect(plan.reason).toBe('No provider serves "gemini-3.8-flash" (checked: antigravity).');
+ }
+ } finally {
+ registerModelDiscoveryFetcher("antigravity", originalFetcher);
+ invalidateModelDiscovery("antigravity");
+ credentials.isAvailable = originalIsAvailable;
+ credentials.describeReadiness = originalDescribeReadiness;
+ }
+ });
+ });
+
+ describe("fallback placement and no-catalog behavior", () => {
+ test("appends the fallback last and does not duplicate an existing candidate", () => {
+ const missingModel = buildCatalogChain("swe-1.7", undefined, cachePath);
+ expect(missingModel.routes.map(({ provider }) => provider)).toEqual(["devin", "openrouter"]);
+
+ const alreadyGathered = buildCatalogChain("gpt-6-astra", "openrouter", cachePath);
+ expect(alreadyGathered.routes.at(-1)?.provider).toBe("openrouter");
+ expect(alreadyGathered.routes.filter(({ provider }) => provider === "openrouter")).toHaveLength(
+ 1
+ );
+ });
+
+ test("does not append a fallback when defaultProvider is explicitly empty", () => {
+ expect(
+ buildCatalogChain("swe-1.7", "", cachePath).routes.map(({ provider }) => provider)
+ ).toEqual(["devin"]);
+ });
+
+ test.skipIf(routeOwnership("openrouter") !== "backend-owned")(
+ "does not append a backend-owned gateway the cloud models catalog positively denies",
+ () => {
+ // The probe ownership cache and qwen3.8-max row are both generation
+ // g-20260921062451697-f490edba. The branch cannot be isolated from the
+ // default probe-cache path because production exposes no injection seam.
+ expect(catalogDeniesProvider("openrouter", "qwen3.8-max", cachePath)).toBe(true);
+ expect(
+ buildCatalogChain("qwen3.8-max", "openrouter", cachePath).routes.map(
+ ({ provider }) => provider
+ )
+ ).not.toContain("openrouter");
+ }
+ );
+
+ test("a matching empty user rule suppresses every fallback", async () => {
+ const originalIsAvailable = credentials.isAvailable;
+ try {
+ credentials.isAvailable = async () => true;
+ const plan = await route("gpt-6-astra", { "*": [] }, "openrouter", cachePath);
+ expect(plan).toEqual({
+ kind: "no-route",
+ reason: 'A routing rule matched "gpt-6-astra" and named no provider.',
+ hint: expect.any(String),
+ });
+ } finally {
+ credentials.isAvailable = originalIsAvailable;
+ }
+ });
+
+ test("no cloud models catalog blocks bare names but explicit model specs still work", async () => {
+ const originalIsAvailable = credentials.isAvailable;
+ _setCatalogEntriesForTest([]);
+ try {
+ credentials.isAvailable = async (provider: string) => provider === "openai";
+ const bare = await route("gpt-5", {}, undefined, missingCachePath);
+ expect(bare.kind).toBe("no-route");
+ if (bare.kind === "no-route") {
+ expect(bare.reason).toContain("No model catalog available");
+ expect(bare.hint).toContain("claudish --models-refresh");
+ }
+
+ const explicit = await route("openai@gpt-5", {}, undefined, missingCachePath);
+ expect(explicit.kind).toBe("ok");
+ if (explicit.kind === "ok") expect(explicit.primary.modelSpec).toBe("oai@gpt-5");
+ } finally {
+ credentials.isAvailable = originalIsAvailable;
+ _resetCatalogClient();
+ }
+ });
+ });
diff --cc packages/cli/src/providers/transport/probe-discovery.test.ts
index b980b115,3d31ae23..0eff4563
--- a/packages/cli/src/providers/transport/probe-discovery.test.ts
+++ b/packages/cli/src/providers/transport/probe-discovery.test.ts
@@@ -10,6 -10,6 +10,7 @@@ import { afterEach, beforeEach, describ
import {
_clearProbeDiscoveryCache,
++ classifyChatCapability,
discoverViaLMStudio,
discoverViaOllama,
discoverViaOpenAIModels,
@@@ -18,6 -17,6 +19,14 @@@
rankProbeCandidates,
} from "./probe-discovery.js";
++/**
++ * A cloud models catalog cache path that does not exist, so a name-rule test
++ * sees only the name rules. `readAllModelsCache` answers `null` for a missing
++ * file; the classifier's memo is keyed by path, so this never collides with a
++ * test that seeds a real cache.
++ */
++const NO_CLOUD_CATALOG = "/nonexistent/claudish-test/cloud-models-catalog-v3.json";
++
describe("rankProbeCandidates", () => {
test("prefers small-name patterns", () => {
const ranked = rankProbeCandidates([
@@@ -65,49 -64,6 +74,58 @@@
expect(ranked).toEqual(["claude-haiku-4", "gpt-4o-mini"]);
});
+ test("drops the REALTIME/VOICE family — observed leaking into the Codex roster", () => {
+ // All five were on screen in a live provider-scoped list, offered as launchable
+ // coding models. The selected row's own catalog sentence disqualified it: "a
+ // distilled reasoning model for faster, lower-cost realtime VOICE interactions...
+ // audio and text inputs over WebRTC, WebSocket, or SIP".
++ //
++ // This pins the NAME FALLBACK, so the catalog is pinned EMPTY. Without a path
++ // the classifier reads the developer's real cloud models catalog cache, where
++ // these ids publish output `["audio","text"]` — and published output modality
++ // outranks every name rule — so the test passed on a clean CI machine and
++ // failed on any machine that had fetched the v3 catalog.
+ for (const id of [
+ "gpt-live-1",
+ "gpt-realtime-2",
+ "gpt-realtime-2.1",
+ "gpt-realtime-2.1-mini",
+ "gpt-realtime-translate",
+ ]) {
- expect({ id, chat: isChatCapable(id) }).toEqual({ id, chat: false });
++ expect({ id, chat: classifyChatCapability(id, NO_CLOUD_CATALOG) }).toEqual({
++ id,
++ chat: "not-chat",
++ });
+ }
+ });
+
+ test("`live` IS A BOUNDED TOKEN — a substring would eat four plausible ids", () => {
+ // `-` is a non-word character, so `\blive\b` matches `gpt-live-1` and does NOT
+ // match any of these. A bare `/live/` would drop all four, and each of them is a
+ // shape a real roster produces: a benchmark-tuned fine-tune, a deployment slug,
+ // a vendor codename, a probe endpoint.
+ for (const id of [
+ "livecodebench-qwen-32b",
+ "mistral-delivery-agent",
+ "olive-7b",
+ "liveness-probe-1",
+ ]) {
+ expect({ id, chat: isChatCapable(id) }).toEqual({ id, chat: true });
+ }
+ });
+
+ test("`realtime` and `translate` are bounded too, and keep their near-misses", () => {
+ expect(isChatCapable("gemini-live-2.5-flash")).toBe(false);
+ expect(isChatCapable("seed-translate-1")).toBe(false);
+ // Pins that the patterns are not `/real/` and `/trans/`, which would have been
+ // the obvious sloppy spelling and would have dropped both of these.
+ expect(isChatCapable("unreal-coder-7b")).toBe(true);
+ expect(isChatCapable("trans-coder-7b")).toBe(true);
+ // And the ids the whole list exists to keep.
+ expect(isChatCapable("gpt-5-codex")).toBe(true);
+ expect(isChatCapable("kimi-k2.7-code-highspeed")).toBe(true);
+ });
+
test("returns empty when all candidates are non-chat", () => {
const ranked = rankProbeCandidates(["text-embedding-3-large", "whisper-1", "tts-1-hd"]);
expect(ranked).toEqual([]);
diff --cc packages/cli/src/providers/transport/probe-discovery.ts
index 84091e46,53124b62..b0d0e8ab
--- a/packages/cli/src/providers/transport/probe-discovery.ts
+++ b/packages/cli/src/providers/transport/probe-discovery.ts
@@@ -38,34 -39,16 +39,22 @@@ const SMALL_MODEL_PATTERNS =
];
/**
- * Models that can't handle a `/v1/chat/completions` probe request: image
- * generation, embeddings, TTS, ASR, speech-to-text, video generation. These
- * often appear in /v1/models lists alongside chat models (especially on
- * LiteLLM aggregators) but will 404 or 400 the probe. Filter them out before
- * ranking.
+ * Models that cannot answer a chat turn: image generation, embeddings, TTS,
+ * speech-to-text. They appear in `/v1/models` lists beside chat models and 404
+ * or 400 a probe.
*
- * WHAT THIS CANNOT DO, STATED RATHER THAN IMPLIED. Every pattern here reads a
- * model ID, and an ID is not a modality contract. The three families below were
- * added after a screenshot of the model picker listed all seven of
- * `mai-transcribe-2`, `mai-transcribe-1.5`, `gpt-transcribe`, `mai-voice-2`,
- * `mai-voice-2-flash`, `flux-3-video` and `seedance-2.5` as launchable coding
- * models — and only six of those seven say what they are in their name.
- * `seedance-2.5` is a video generator whose ID carries no signal at all, and
- * `flux-2-*` is an image generator for the same reason; neither can be excluded
- * here without a vendor-family blacklist, which is the workaround this file has
- * already paid for three times (`:498`, then `pplx-embed`, then `bge`).
+ * A NEGATIVE name rule: it can deny, never confirm. It is now the FALLBACK, not
+ * the rule: the cloud models catalog publishes output modalities, so for a model
+ * it describes the published list decides and this list is never consulted. It
+ * still decides for what the catalog leaves without an output modality — local
+ * servers (Ollama, LM Studio), custom endpoints, LiteLLM deployments, and catalog
+ * rows that carry no modality field.
+ *
- * THE SAME BOUNDARY HELD FOR THE REALTIME FAMILY. `gpt-live-1` and the four
- * `gpt-realtime-*` entries say what they are in their names, so they are excluded
- * here; an id that carries no modality signal at all is still LOGGED by name
- * (`toPickerRows`) for the models-index bug report rather than guessed at.
- *
- * The complete fix is a modality field on the catalog document — `ModelDoc`
- * already carries `capabilities.imageGeneration` / `audioInput` / `audioOutput`
- * / `embedding`, but the SLIM payload every aggregator list is built from
- * carries none of them. That is a models-index gap and it is filed there, not
- * patched here. `toPickerRows` (model-selector.ts) logs the residue once per
- * process so the gap is visible in a debug log rather than only in a screenshot.
++ * VIDEO IS DELIBERATELY NOT HERE. It lives in {@link VIDEO_OUTPUT_NAME_PATTERNS},
++ * which `classifyChatCapability` consults only while the catalog is silent on
++ * `videoOutput`. A `video` rule in THIS list would run first and override a
++ * published `videoOutput: false` — excluding a model that merely READS video,
++ * which is still a chat model.
*/
const NON_CHAT_PATTERNS = [
/\bimage\b/i,
@@@ -82,37 -65,24 +71,48 @@@
/\bmoderation\b/i,
/\brerank/i,
/\bspeech\b/i,
- /-(image|tts|audio|embedding|vision-only)(-|$)/i,
+ // Speech-to-text. `gemini-3.5-transcribe` is the case `toPickerRows` named as
+ // getting through, and `mai-transcribe-*` / `gpt-transcribe` are the observed
+ // leaks. No chat model in any served roster carries the word.
+ /\btranscribe\b/i,
+ /\btranscription\b/i,
+ // Text-to-speech under a name that does not say `tts`: `mai-voice-2`,
+ // `mai-voice-2-flash`.
+ /\bvoice\b/i,
- // Video GENERATION (`flux-3-video`, `*-video-gen`). Accepted risk, recorded:
- // a future video-UNDERSTANDING chat model named `video-something` would be
- // dropped too. No such model is in any roster today, and the alternative —
- // listing a generator as a coding target — fails at launch instead of in the
- // list.
- /\bvideo\b/i,
+ // REALTIME / LIVE / TRANSLATE — observed leaking into the OpenAI Codex roster as
+ // `gpt-live-1`, `gpt-realtime-2`, `gpt-realtime-2.1`, `gpt-realtime-2.1-mini` and
+ // `gpt-realtime-translate`, all five offered as launchable coding models. The
+ // selected row's own catalog sentence disqualified it: "a distilled reasoning
+ // model for faster, lower-cost realtime VOICE interactions... audio and text
+ // inputs over WebRTC, WebSocket, or SIP". None of them speaks
+ // `/v1/chat/completions` the way an agent needs.
+ /\brealtime\b/i,
+ // `\b` IS THE WHOLE POINT ON THIS ONE. `-` is a non-word character, so `\blive\b`
+ // matches `gpt-live-1` and `x-live-2` and does NOT match `delivery`, `olive`,
+ // `livecodebench` or `liveness` — every one of which is a plausible model id and
+ // none of which is a realtime endpoint. A bare substring `/live/` would eat all
+ // four.
+ /\blive\b/i,
+ /\btranslate\b/i,
+ /\btranslation\b/i,
- /-(image|tts|audio|embedding|vision-only|transcribe|voice|video|speech|realtime|live|translate)(-|$)/i,
++ /-(image|tts|audio|embedding|vision-only|transcribe|voice|speech|realtime|live|translate)(-|$)/i,
+ ];
+
+ /**
+ * Ids that LOOK like video generators — a fallback the catalog overrides.
+ *
+ * Kept apart from {@link NON_CHAT_PATTERNS} because the catalog publishes
+ * `videoOutput` (a defined boolean, `false` included), so for any model it knows
+ * the fact replaces this guess. The guess still matters for models the catalog
+ * does not cover. One word, two directions: `\bvideo\b` cannot tell a video
+ * GENERATOR from a model that READS video, and video input alone never excludes a
+ * chat model — which is why a published `videoOutput: false` switches this off.
+ */
+ const VIDEO_OUTPUT_NAME_PATTERNS = [
+ /\bvideo\b/i, // video-01, wan2.2-video, hunyuan-video
+ /(^|[-_.])(t2v|i2v|r2v|v2v)([-_.]|$)/i, // happyhorse-1.1-t2v / -i2v / -r2v; MiniMax T2V-01
+ /\bveo\b/i, // Google Veo
+ /\bsora\b/i, // OpenAI Sora
];
function isSmallName(name: string): boolean {