Files
OmniRoute/tests/unit/codex-synced-bare-model-routing.test.ts
Diego Rodrigues de Sa e Souza 4a3dcf6b0b fix(routing): only let Codex-native bare ids preempt a provider when codex is active (#9447)
* fix(routing): only let Codex-native bare ids preempt a provider when codex is active

#9275 widened CODEX_NATIVE_UNPREFIXED_MODELS from a single id to gpt-5.5 plus the
gpt-5.6-sol/terra/luna tiers, so bare Codex CLI ids would reach the ChatGPT
subscription instead of fanning out to whichever provider won the inference race.
The early return it added never consulted the active-provider set, which made the
codex-only guard 30 lines below unreachable for every id in the set:

  if (CODEX_NATIVE_UNPREFIXED_MODELS.has(modelId)) return { provider: "codex", ... }

An OpenAI-only install therefore had bare gpt-5.5 routed to codex and failed with
'no active credentials for provider: codex' on a model OpenAI serves, and an install
whose codex connection was merely inactive failed identically. This also silently
reverted #5887's compatibility boundary.

The preference now only PREEMPTS another provider when a codex connection is active.
Ids that no other provider catalogs (codex-auto-review) still resolve to codex with no
connection at all — there is nothing to preempt and 'no codex credentials' is the
honest error. With codex active the preference still beats OpenAI, which is the point
of #9275, and an explicit openai/ prefix overrides it either way.

Tests: the three assertions that encode the intended #9275 change now expect codex
(plus a new one pinning the explicit-prefix override); the rest were already correct
and pass again untouched. Adds a regression test for the OpenAI-only case.

* docs(changelog): correct fragment id to #9447

* test(routing): seed an active codex connection in the bare-precedence guards

The two files #9275 added assert that bare gpt-5.5 / gpt-5.6-sol reach codex, but
they ran against an empty database — so they also pinned 'codex wins with no codex
connection at all', which is the regression #9447 removes. That put them in direct
contradiction with plan3-p0 / chat-helpers / codex-gpt55-routing-5887, which assert
openai for the very same input: no implementation could satisfy both, which is why
the release could not go green.

Seeding an active codex connection keeps the contract these files were written to
guard (codex beats openai for a Codex-native bare id) while dropping the accidental
'even with no codex configured' half. Cases that need no connection are left as they
were: the tier-only ids and codex-auto-review have no alternative provider to preempt,
and the explicit-prefix overrides are unaffected.

---------

Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com>
2026-08-04 17:08:08 -03:00

162 lines
5.9 KiB
TypeScript

import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import test from "node:test";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-codex-synced-routing-"));
process.env.DATA_DIR = TEST_DATA_DIR;
const core = await import("../../src/lib/db/core.ts");
const modelsDb = await import("../../src/lib/db/models.ts");
const providersDb = await import("../../src/lib/db/providers.ts");
const { getModelInfoCore } = await import("../../open-sse/services/model.ts");
type TestProvider = "anthropic" | "codex" | "openai";
const GPT_56_CODEX_MODEL = "gpt-5.6-sol";
const FUTURE_CODEX_MODEL = "codex-next-preview";
const FUTURE_NON_GPT_MODEL = "orion-preview-2027";
async function seedConnection(provider: TestProvider, isActive = true) {
return providersDb.createProviderConnection({
provider,
authType: provider === "codex" ? "oauth" : "apikey",
name: `${provider}-routing-test`,
email: provider === "codex" ? `${provider}@example.com` : undefined,
apiKey: provider !== "codex" ? `sk-${provider}-routing-test` : undefined,
isActive,
providerSpecificData: provider === "codex" ? { workspaceId: "ws-routing-test" } : undefined,
});
}
async function seedSyncedModel(provider: TestProvider, modelId: string, isActive = true) {
const connection = await seedConnection(provider, isActive);
assert.ok(connection?.id, `${provider} connection must be created`);
await modelsDb.replaceSyncedAvailableModelsForConnection(provider, String(connection.id), [
{
id: modelId,
name: modelId,
apiFormat: "openai-responses",
supportedEndpoints: ["chat"],
},
]);
return connection;
}
test.beforeEach(() => {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
});
test.after(() => {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
});
test("bare GPT-5.6 model routes through Codex when it is the only active provider", async () => {
await seedSyncedModel("codex", GPT_56_CODEX_MODEL);
const info = await getModelInfoCore(GPT_56_CODEX_MODEL, null);
assert.equal(info.provider, "codex");
assert.equal(info.model, GPT_56_CODEX_MODEL);
});
// #9275 put the whole gpt-5.6-sol tier set into CODEX_NATIVE_UNPREFIXED_MODELS, so an
// active Codex connection now claims the bare id ahead of OpenAI. Before, OpenAI won the
// overlap; the escape hatch is the explicit prefix, covered by the last test in this file.
test("Codex claims the bare model when both providers advertise it", async () => {
await seedSyncedModel("codex", GPT_56_CODEX_MODEL);
await seedSyncedModel("openai", GPT_56_CODEX_MODEL);
const info = await getModelInfoCore(GPT_56_CODEX_MODEL, null);
assert.equal(info.provider, "codex");
assert.equal(info.model, GPT_56_CODEX_MODEL);
});
test("Codex is inferred when only its active catalog advertises the model", async () => {
await seedSyncedModel("codex", FUTURE_CODEX_MODEL);
await seedConnection("openai");
const info = await getModelInfoCore(FUTURE_CODEX_MODEL, null);
assert.equal(info.provider, "codex");
assert.equal(info.model, FUTURE_CODEX_MODEL);
});
test("non-GPT models use the same active synchronized-catalog inference", async () => {
await seedSyncedModel("anthropic", FUTURE_NON_GPT_MODEL);
const info = await getModelInfoCore(FUTURE_NON_GPT_MODEL, null);
assert.equal(info.provider, "anthropic");
assert.equal(info.model, FUTURE_NON_GPT_MODEL);
});
test("OpenAI remains selected when it is the only active provider advertising the model", async () => {
await seedSyncedModel("openai", GPT_56_CODEX_MODEL);
const info = await getModelInfoCore(GPT_56_CODEX_MODEL, null);
assert.equal(info.provider, "openai");
assert.equal(info.model, GPT_56_CODEX_MODEL);
});
test("inactive Codex synchronized models do not influence bare-model routing", async () => {
await seedSyncedModel("codex", GPT_56_CODEX_MODEL, false);
await seedSyncedModel("openai", GPT_56_CODEX_MODEL);
const info = await getModelInfoCore(GPT_56_CODEX_MODEL, null);
assert.equal(info.provider, "openai");
assert.equal(info.model, GPT_56_CODEX_MODEL);
});
test("Codex claims an overlapping static model when both connections are active", async () => {
await seedConnection("codex");
await seedConnection("openai");
const info = await getModelInfoCore("gpt-5.5", null);
assert.equal(info.provider, "codex");
assert.equal(info.model, "gpt-5.5");
});
// The regression #9275 introduced and this file now guards: the Codex-native set must
// never claim a bare id when no codex connection is active — an OpenAI-only install
// would get "no active credentials for provider: codex" for a model OpenAI serves.
test("a Codex-native bare id stays on OpenAI when no codex connection exists", async () => {
await seedConnection("openai");
const info = await getModelInfoCore("gpt-5.5", null);
assert.equal(info.provider, "openai");
assert.equal(info.model, "gpt-5.5");
});
test("OpenAI remains selected for an overlapping static model when Codex is inactive", async () => {
await seedConnection("codex", false);
await seedConnection("openai");
const info = await getModelInfoCore("gpt-5.5", null);
assert.equal(info.provider, "openai");
assert.equal(info.model, "gpt-5.5");
});
test("explicit Codex and OpenAI prefixes remain authoritative", async () => {
await seedSyncedModel("codex", GPT_56_CODEX_MODEL);
await seedSyncedModel("openai", GPT_56_CODEX_MODEL);
const codexAlias = await getModelInfoCore(`cx/${GPT_56_CODEX_MODEL}`, null);
const codexCanonical = await getModelInfoCore(`codex/${GPT_56_CODEX_MODEL}`, null);
const openai = await getModelInfoCore(`openai/${GPT_56_CODEX_MODEL}`, null);
assert.equal(codexAlias.provider, "codex");
assert.equal(codexCanonical.provider, "codex");
assert.equal(openai.provider, "openai");
});