Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(routing): honour CLAUDISH_DEFAULT_PROVIDER in route()
Jack Rudenko8eb8897940f0
diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.tsindex 440c5bd1..1df129d3 100644--- a/packages/cli/src/cli.ts+++ b/packages/cli/src/cli.ts@@ -67,6 +67,7 @@ import { resolveProviderSlug } from "./providers/provider-slug-resolve.js";import {buildCatalogChain,buildRoutingChain,+ effectiveDefaultProvider,hasCredentialsForProvider,loadRoutingRules,matchRoutingRule,@@ -1560,9 +1561,10 @@ async function probeModelRouting(// would be the second copy of a routing decision, and the two would// disagree the first time either changed. `--probe` still owns the// credential-provenance display below, which is why it builds a chain at- // all instead of calling `route()`.+ // all instead of calling `route()`. The fallback hop is `route()`'s too:+ // `effectiveDefaultProvider()` reads the env variable before the config.return {- routes: buildCatalogChain(parsed.model, loadConfig().defaultProvider)+ routes: buildCatalogChain(parsed.model, effectiveDefaultProvider()).routes as FallbackRoute[],source: "auto-chain" as const,matchedPattern: undefined,diff --git a/packages/cli/src/providers/routing-rules.test.ts b/packages/cli/src/providers/routing-rules.test.tsindex 224768fb..57b4d1e5 100644--- a/packages/cli/src/providers/routing-rules.test.ts+++ b/packages/cli/src/providers/routing-rules.test.ts@@ -9,7 +9,15 @@ import { catalogRouteForProvider } from "./catalog-route-bindings.js";*/import { afterEach, beforeEach, describe, expect, test } from "bun:test";-import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";+import {+ copyFileSync,+ existsSync,+ mkdirSync,+ mkdtempSync,+ readFileSync,+ rmSync,+ writeFileSync,+} from "node:fs";import { homedir, tmpdir } from "node:os";import { join } from "node:path";@@ -29,6 +37,7 @@ import {import "./model-discovery-builtins.js";import type { ProviderDefinition } from "./provider-definitions.js";import {+ type RoutePlan,buildRoutingChain,loadRoutingRules,matchRoutingRule,@@ -41,6 +50,12 @@ import { clearRuntimeRegistry, registerRuntimeProvider } from "./runtime-provideconst keychainGuardAtFileLoad = process.env.CLAUDISH_DISABLE_KEYCHAIN;const SYNTHETIC_MODEL_ID = "acme-x1.0";const SYNTHETIC_MINIMAX_EXTERNAL_ID = "ACME-X1.0";+const STAGE4_CATALOG_FIXTURE = join(+ import.meta.dir,+ "..",+ "test-fixtures",+ "stage4-default-provider-catalog.json"+);function seedDefaultCatalog(entries: DiskCacheV3["entries"]): () => void {_setCatalogEntriesForTest(entries);return _resetCatalogClient;@@ -129,6 +144,61 @@ function makeTempCatalog(return { path, cleanup: () => rmSync(dir, { recursive: true, force: true }) };}+function routeInSandbox(defaultProvider: string): RoutePlan {+ const home = mkdtempSync(join(tmpdir(), "claudish-default-provider-route-"));+ const configDir = join(home, ".claudish");+ mkdirSync(configDir, { recursive: true });+ copyFileSync(STAGE4_CATALOG_FIXTURE, join(configDir, "cloud-models-catalog-v3.json"));+ writeFileSync(+ join(configDir, "config.json"),+ JSON.stringify({+ version: "1.0.0",+ defaultProfile: "default",+ profiles: {},+ customEndpoints: {+ x: {+ kind: "simple",+ url: "https://stage4-x.invalid/v1",+ format: "openai",+ apiKey: "stage4-test-key",+ },+ },+ }),+ "utf8"+ );++ const routingModuleUrl = new URL("./routing-rules.ts", import.meta.url).href;+ const script = `+ const { route } = await import(${JSON.stringify(routingModuleUrl)});+ const plan = await route("no-such-model-xyz");+ process.stdout.write(JSON.stringify(plan));+ `;+ const env: Record<string, string> = {+ HOME: home,+ PATH: process.env.PATH ?? "",+ TMPDIR: process.env.TMPDIR ?? tmpdir(),+ CLAUDISH_DEFAULT_PROVIDER: defaultProvider,+ CLAUDISH_DISABLE_CATALOG_WARM: "1",+ CLAUDISH_DISABLE_KEYCHAIN: "1",+ CLAUDISH_DISABLE_OP: "1",+ };++ try {+ const result = Bun.spawnSync([process.execPath, "-e", script], {+ cwd: join(import.meta.dir, "../../../.."),+ env,+ stdout: "pipe",+ stderr: "pipe",+ });+ const stdout = result.stdout.toString();+ const stderr = result.stderr.toString();+ expect(result.exitCode, stderr || stdout).toBe(0);+ return JSON.parse(stdout) as RoutePlan;+ } finally {+ rmSync(home, { recursive: true, force: true });+ }+}+// ---------------------------------------------------------------------------// matchRoutingRule — pattern matching// ---------------------------------------------------------------------------@@ -1099,6 +1169,61 @@ describe("route() with defaultProvider", () => {});});+describe("route() reads the resolved defaultProvider only without overrides", () => {+ test("an env provider registered by sandbox config is the fallback position", () => {+ const plan = routeInSandbox("x");+ expect(plan.kind).toBe("ok");+ if (plan.kind !== "ok") return;+ expect([plan.primary.provider, ...plan.fallbacks.map((entry) => entry.provider)]).toEqual([+ "x",+ ]);+ });++ test("an empty env value produces the catalog-empty no-route", () => {+ expect(routeInSandbox("")).toEqual({+ kind: "no-route",+ reason: 'No provider in the catalog serves "no-such-model-xyz".',+ hint:+ 'No credentials found for "no-such-model-xyz". Options:\n' ++ " Use: claudish --model or@no-such-model-xyz (route via OpenRouter)",+ });+ });++ test("rules passed without a third argument keep the openrouter guard", async () => {+ const priorDefault = process.env.CLAUDISH_DEFAULT_PROVIDER;+ const priorOpenRouter = process.env.OPENROUTER_API_KEY;+ const priorKeychainGuard = process.env.CLAUDISH_DISABLE_KEYCHAIN;+ const priorOpGuard = process.env.CLAUDISH_DISABLE_OP;+ process.env.CLAUDISH_DEFAULT_PROVIDER = "x";+ process.env.OPENROUTER_API_KEY = "stage4-openrouter-test-key";+ process.env.CLAUDISH_DISABLE_KEYCHAIN = "1";+ process.env.CLAUDISH_DISABLE_OP = "1";+ credentials.invalidate();+ ensureEndpointsRegistered({+ config: { version: "1.0.0", defaultProfile: "default", profiles: {} },+ force: true,+ });++ try {+ const plan = await route("no-such-model-xyz", {}, undefined, STAGE4_CATALOG_FIXTURE);+ expect(plan.kind).toBe("ok");+ if (plan.kind !== "ok") return;+ expect(plan.primary.provider).toBe("openrouter");+ expect(plan.fallbacks).toEqual([]);+ } finally {+ if (priorDefault === undefined) delete process.env.CLAUDISH_DEFAULT_PROVIDER;+ else process.env.CLAUDISH_DEFAULT_PROVIDER = priorDefault;+ if (priorOpenRouter === undefined) delete process.env.OPENROUTER_API_KEY;+ else process.env.OPENROUTER_API_KEY = priorOpenRouter;+ if (priorKeychainGuard === undefined) delete process.env.CLAUDISH_DISABLE_KEYCHAIN;+ else process.env.CLAUDISH_DISABLE_KEYCHAIN = priorKeychainGuard;+ if (priorOpGuard === undefined) delete process.env.CLAUDISH_DISABLE_OP;+ else process.env.CLAUDISH_DISABLE_OP = priorOpGuard;+ credentials.invalidate();+ }+ });+});+// ---------------------------------------------------------------------------// PROVIDER_SHORTCUTS / PROVIDER_TO_PREFIX sanity checks// (ensure imports are consistent — routing-rules depends on these)diff --git a/packages/cli/src/providers/routing-rules.ts b/packages/cli/src/providers/routing-rules.tsindex e0274f2c..f98b0415 100644--- a/packages/cli/src/providers/routing-rules.ts+++ b/packages/cli/src/providers/routing-rules.ts@@ -1,6 +1,7 @@import { resolveSubscriptionRouting } from "../adapters/model-catalog.js";import { credentials } from "../auth/credentials/authority.js";import type { ReadinessResult } from "../auth/credentials/types.js";+import { resolveDefaultProvider } from "../default-provider.js";import { isSubscriptionProvider } from "../handlers/shared/remote-provider-types.js";import { log, logStderr } from "../logger.js";import type { RecommendedModelsDoc } from "../model-loader.js";@@ -379,6 +380,19 @@ function fallbackProviderFor(defaultProvider: string | undefined): string | null*/export const DEFAULT_FALLBACK_PROVIDER = "openrouter";+/**+ * The `defaultProvider` this process routes with when no caller named one:+ * `CLAUDISH_DEFAULT_PROVIDER`, then `defaultProvider` in the config file, then+ * `openrouter` (`resolveDefaultProvider`). May be `""`, which disables the+ * fallback hop.+ *+ * Read per call, like the rules. `route()` without overrides and `--probe`'s+ * chain both read it here, so the two cannot name different fallback hops.+ */+export function effectiveDefaultProvider(): string {+ return resolveDefaultProvider({ config: loadConfig(), env: process.env }).provider;+}+/** A chain assembled from the catalog, plus whether a catalog could be read. */export interface CatalogChain {routes: Route[];@@ -731,8 +745,9 @@ function wireIdOf(route: Route): string {* → no-route with hints. See `routeBare`.** Rules and the default provider are loaded fresh each call (via `loadRoutingRules()`- * and `loadConfig()`) unless overrides are supplied. Tests should pass overrides- * to avoid disk lookups.+ * and `effectiveDefaultProvider()`, which reads CLAUDISH_DEFAULT_PROVIDER and then+ * the config) unless overrides are supplied. Tests should pass overrides to avoid+ * disk and environment lookups.*//*** Rewrite a dash-slugified GLM version to its canonical dotted form@@ -788,15 +803,17 @@ export async function route(const rules = rulesOverride ?? loadRoutingRules();// When tests pass an explicit `rulesOverride`, treat the rule set as the- // authoritative source of truth and do not read `loadConfig().defaultProvider`- // off disk — that would leak the host machine's config into unit tests.- // Production callers (via `loadRoutingRules()`) get the disk-loaded default.+ // authoritative source of truth and read the default provider from neither the+ // environment nor the config file — either would leak this machine's setting+ // into unit tests. A caller that passes rules and wants a fallback+ // passes it as the third argument. Callers with no overrides+ // get `effectiveDefaultProvider()`: the env variable, then the config.const defaultProvider =defaultProviderOverride !== undefined? defaultProviderOverride: rulesOverride !== undefined? undefined- : loadConfig().defaultProvider;+ : effectiveDefaultProvider();return routeBare(normalizeGlmSlug(parsed.model),parsed.provider,