fix(ai): never 5xx from the connection test and report a categorised reason

The connection-test route mapped a provider 5xx to a 502, which a reverse
proxy rewrites to its own HTML error page; the browser's res.json() then
threw "Unexpected token '<'". Every provider-call failure now returns
HTTP 200 with a secret-free { ok:false, providerType, reason }. Reasons
are categorised: credential/auth (401/403, or a 5xx whose body signals an
invalidated/expired session), rate-limit (429), provider server error
(other 5xx) and network/timeout.

The empty-override "test my saved config" path now resolves through the
same provider chain generation uses, instead of the legacy codex->admin
probe that could test a different provider than the one generation runs.

The chain runner already advances past an auth-class failure to the next
enabled provider; add explicit coverage for that on the raw generation
path (401 -> next succeeds, fallback count incremented).
This commit is contained in:
Marc-André Bombeck committed 2026-06-03 08:56:19 +02:00
1 parent 192fe45469
commit 564d7dec77
5 files changed
+191 -59

No files matched your search

@@ -360,6 +360,36 @@ describe("runRawCompletionWithFallback — legacy route shim", () => {
expect(result.fallbackHops[0].providerType).toBe("codex");
});
it("advances past an auth-class (401) failure to the next provider and counts the fallback", async () => {
// Generation routes consume this raw shim. An invalidated credential
// on the primary (401/403, or a 5xx whose body signals an
// invalidated session) must not abort the run — it advances to the
// next enabled provider and bumps the fallback count.
const codex = new ScriptedProvider({
type: "codex",
script: [{ ok: false, error: err(401, "token invalidated") }],
});
const openai = new ScriptedProvider({
type: "admin-key",
script: [{ ok: true, content: '{ "ok": true }' }],
});
const result = await runRawCompletionWithFallback({
userId: "u-raw-auth-advance",
providers: [
{ providerType: "codex", instance: codex },
{ providerType: "openai", instance: openai },
],
params: { systemPrompt: "s", userPrompt: "u" },
});
expect(result.result.content).toBe('{ "ok": true }');
expect(result.workingProvider.providerType).toBe("openai");
expect(result.fallbackHops).toHaveLength(1);
expect(result.fallbackHops[0].providerType).toBe("codex");
expect(result.fallbackHops[0].httpStatus).toBe(401);
expect(codex.callCount).toBe(1);
expect(openai.callCount).toBe(1);
});
it("throws AllProvidersFailedError when every entry fails hard", async () => {
const a = new ScriptedProvider({
type: "codex",
+52 -1
View File
@@ -21,7 +21,8 @@ vi.mock("@/lib/ai/codex-oauth", () => ({
decryptCodexCreds: vi.fn(),
}));
import { resolveProvider } from "../provider";
import { resolveProvider, resolveProviderForTest } from "../provider";
import { OpenAIClient } from "../openai-client";
import { prisma } from "@/lib/db";
import { decrypt, encrypt } from "@/lib/crypto";
import { decryptCodexCreds, encryptCodexCreds } from "@/lib/ai/codex-oauth";
@@ -298,3 +299,53 @@ describe("resolveProvider", () => {
expect(provider.type).toBe("codex");
});
});
/**
* The connection-test "test my saved config" path (empty override body)
* MUST resolve via the SAME chain generation uses, so a green test means
* generation will actually work. Previously it probed codex→admin
* directly, which could test a different provider than generation ran.
*/
describe("resolveProviderForTest — empty override resolves via the chain", () => {
beforeEach(() => {
vi.resetAllMocks();
vi.mocked(decrypt).mockImplementation((v: string) => `decrypted:${v}`);
});
it("returns the first enabled+credentialed chain entry (the same provider generation runs)", async () => {
// resolveProviderForTest first reads the test row (no chain), then
// resolveProviderChain reads the chain row. The chain enables openai
// and the user has an OpenAI key → first chain entry is OpenAIClient.
vi.mocked(prisma.user.findUnique)
// 1st: resolveProviderForTest's own read
.mockResolvedValueOnce({
aiProvider: null,
aiModel: null,
aiBaseUrl: null,
aiAnthropicKeyEncrypted: null,
aiLocalKeyEncrypted: null,
aiOpenaiKeyEncrypted: "enc-user-openai",
} as never)
// 2nd: resolveProviderChain's read
.mockResolvedValueOnce({
aiProvider: null,
aiModel: null,
aiBaseUrl: null,
aiAnthropicKeyEncrypted: null,
aiLocalKeyEncrypted: null,
aiOpenaiKeyEncrypted: "enc-user-openai",
aiProviderChain: [
{ providerType: "openai", priority: 1, enabled: true },
],
} as never);
const provider = await resolveProviderForTest("user-123", {});
// The user-OpenAI client is an OpenAIClient; its runtime tag defaults
// to "admin-key" (no providerType passed). What matters is that the
// empty-override path materialised the chain's first entry rather than
// the legacy codex→admin probe — a NoProvider/none would mean the
// chain was bypassed.
expect(provider.type).not.toBe("none");
expect(provider).toBeInstanceOf(OpenAIClient);
});
});
+14 -4
View File
@@ -424,11 +424,21 @@ export async function resolveProviderForTest(
.toString()
.trim();
// Empty selection: fall back to Codex → admin OpenAI like the regular path.
// Empty selection ("test my saved config" — the ai-section calls
// /api/ai/test with an empty body): resolve via the SAME path generation
// uses — the first enabled+credentialed entry of `resolveProviderChain`.
// The legacy codex→admin fallback used to probe a different provider than
// generation ran (observed: test→admin-key while generation→codex), so a
// green test gave no signal about whether overnight generation would
// work. Falling through to the chain keeps the two paths in lock-step.
if (!provider) {
const codex = await resolveCodexProvider(userId);
if (codex) return codex;
return resolveAdminProvider();
const chain = await resolveProviderChain(userId);
if (chain.length > 0) return chain[0].instance;
// Chain empty (no enabled+credentialed entry) — mirror the regular
// single-provider resolution one more time before giving up.
const legacy = await resolveProvider(userId);
if (legacy.type !== "none") return legacy;
throw new AITestConfigError(422, "No AI provider configured");
}
switch (provider) {