Skip to content

Commit 50c0158

Browse files
committed
fix(catalog): log provider discovery failures once per signature (#395)
Anthropic-adapter providers whose baseUrl lacks /v1/models (e.g. Azure AI Foundry) return HTTP 404 forever; the 30s discovery cooldown re-probed and re-logged the same warning every ~30s, flooding the log. Add shouldLogDiscoveryFailure() (log-once-per-signature): a failure warns only when its signature (reason + httpStatus) differs from the last observed discovery status. The 30s re-probe cooldown and the observable discovery status (/api/providers) are unchanged; a first occurrence, a changed status, a recovery, or a cache clear all log again. Gates all five failure-branch warnings. Regression: tests/provider-discovery-log-suppression.test.ts.
1 parent 8c49dcc commit 50c0158

3 files changed

Lines changed: 109 additions & 21 deletions

File tree

‎src/codex/catalog/provider-fetch.ts‎

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
markModelsFetchFailure,
1515
markProviderDiscoveryFailed,
1616
markProviderDiscoveryOk,
17+
shouldLogDiscoveryFailure,
1718
setCached,
1819
type ProviderModelDiscoveryFailure,
1920
} from "../model-cache";
@@ -302,7 +303,11 @@ export async function fetchProviderModels(name: string, prov: OcxProviderConfig,
302303
: "provider-models";
303304
const failedDiscoveryFallback = (
304305
failure: ProviderModelDiscoveryFailure,
305-
): { models: CatalogModel[]; fallback: "stale" | "configured" } => {
306+
): { models: CatalogModel[]; fallback: "stale" | "configured"; shouldLog: boolean } => {
307+
// Decide logging BEFORE recording the new status, so we can compare against the prior one and
308+
// suppress an identical repeated failure (#395 log flood). The failure stays observable via the
309+
// discovery-status API regardless.
310+
const shouldLog = shouldLogDiscoveryFailure(name, failure);
306311
markModelsFetchFailure(name);
307312
markProviderDiscoveryFailed(name, failure);
308313
const stale = getStaleCached(name);
@@ -311,6 +316,7 @@ export async function fetchProviderModels(name: string, prov: OcxProviderConfig,
311316
? withVertexDefaultSeed(applyConfigHintsToCachedModels(name, prov, stale, contextCap))
312317
: failedDiscoveryConfigured,
313318
fallback: stale ? "stale" : "configured",
319+
shouldLog,
314320
};
315321
};
316322
try {
@@ -319,19 +325,23 @@ export async function fetchProviderModels(name: string, prov: OcxProviderConfig,
319325
allowPrivateNetwork: prov.allowPrivateNetwork,
320326
});
321327
if (destinationError) {
322-
const { models, fallback } = failedDiscoveryFallback({ reason: "blocked" });
323-
console.warn(
324-
`[opencodex] Provider model discovery for "${name}" was blocked by destination policy: ${destinationError} [urlClass=${urlClass}, fallback=${fallback}].`,
325-
);
328+
const { models, fallback, shouldLog } = failedDiscoveryFallback({ reason: "blocked" });
329+
if (shouldLog) {
330+
console.warn(
331+
`[opencodex] Provider model discovery for "${name}" was blocked by destination policy: ${destinationError} [urlClass=${urlClass}, fallback=${fallback}].`,
332+
);
333+
}
326334
return models;
327335
}
328336

329337
const res = await fetch(url, { headers, signal: AbortSignal.timeout(8000) });
330338
if (!res.ok) {
331-
const { models, fallback } = failedDiscoveryFallback({ reason: "http", httpStatus: res.status });
332-
console.warn(
333-
`[opencodex] Provider model discovery for "${name}" failed with HTTP ${res.status} [urlClass=${urlClass}, fallback=${fallback}].`,
334-
);
339+
const { models, fallback, shouldLog } = failedDiscoveryFallback({ reason: "http", httpStatus: res.status });
340+
if (shouldLog) {
341+
console.warn(
342+
`[opencodex] Provider model discovery for "${name}" failed with HTTP ${res.status} [urlClass=${urlClass}, fallback=${fallback}].`,
343+
);
344+
}
335345
return models;
336346
}
337347

@@ -343,23 +353,27 @@ export async function fetchProviderModels(name: string, prov: OcxProviderConfig,
343353
try {
344354
json = JSON.parse(body) as unknown;
345355
} catch {
346-
const { models, fallback } = failedDiscoveryFallback({ reason: "invalid_response" });
356+
const { models, fallback, shouldLog } = failedDiscoveryFallback({ reason: "invalid_response" });
347357
const diagnostic = contentType === "application/json" || contentType.endsWith("+json")
348358
? "returned invalid JSON in a 2xx response"
349359
: "returned a non-JSON 2xx response";
350-
console.warn(
351-
`[opencodex] Provider model discovery for "${name}" ${diagnostic} [status=${res.status}, contentType=${contentType}, urlClass=${urlClass}, fallback=${fallback}].`,
352-
);
360+
if (shouldLog) {
361+
console.warn(
362+
`[opencodex] Provider model discovery for "${name}" ${diagnostic} [status=${res.status}, contentType=${contentType}, urlClass=${urlClass}, fallback=${fallback}].`,
363+
);
364+
}
353365
return models;
354366
}
355367
const data = json !== null && typeof json === "object" && !Array.isArray(json)
356368
? (json as { data?: unknown }).data
357369
: undefined;
358370
if (!isProviderModelsApiItems(data)) {
359-
const { models, fallback } = failedDiscoveryFallback({ reason: "invalid_response" });
360-
console.warn(
361-
`[opencodex] Provider model discovery for "${name}" returned malformed 2xx data [status=${res.status}, contentType=${contentType}, urlClass=${urlClass}, fallback=${fallback}].`,
362-
);
371+
const { models, fallback, shouldLog } = failedDiscoveryFallback({ reason: "invalid_response" });
372+
if (shouldLog) {
373+
console.warn(
374+
`[opencodex] Provider model discovery for "${name}" returned malformed 2xx data [status=${res.status}, contentType=${contentType}, urlClass=${urlClass}, fallback=${fallback}].`,
375+
);
376+
}
363377
return models;
364378
}
365379
const items = data;
@@ -402,10 +416,12 @@ export async function fetchProviderModels(name: string, prov: OcxProviderConfig,
402416
setCached(name, live);
403417
return live;
404418
} catch (error) {
405-
const { models, fallback } = failedDiscoveryFallback({ reason: "network" });
406-
console.warn(
407-
`[opencodex] Provider model discovery for "${name}" threw ${error instanceof Error ? error.name : "unknown"} [urlClass=${urlClass}, fallback=${fallback}].`,
408-
);
419+
const { models, fallback, shouldLog } = failedDiscoveryFallback({ reason: "network" });
420+
if (shouldLog) {
421+
console.warn(
422+
`[opencodex] Provider model discovery for "${name}" threw ${error instanceof Error ? error.name : "unknown"} [urlClass=${urlClass}, fallback=${fallback}].`,
423+
);
424+
}
409425
return models;
410426
}
411427
}

‎src/codex/model-cache.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,29 @@ export function markProviderDiscoveryFailed(
6363
discoveryStatus.set(provider, { status: "failed", ...failure });
6464
}
6565

66+
/**
67+
* Decide whether a discovery FAILURE should be logged, to avoid flooding the log with an identical
68+
* warning on every poll (#395: an anthropic-adapter baseUrl without `/v1/models`, e.g. Azure AI
69+
* Foundry, returns HTTP 404 forever; the 30s cooldown re-probes and previously re-logged each time).
70+
*
71+
* Returns true only when the failure SIGNATURE changed since the last observed status — i.e. the
72+
* previous state was ok/undefined, or a different reason/httpStatus. Repeated identical failures
73+
* stay observable through `getProviderDiscoveryStatus()` / the providers API without log spam.
74+
* Call this BEFORE `markProviderDiscoveryFailed` so it can see the prior state.
75+
*/
76+
export function shouldLogDiscoveryFailure(
77+
provider: string,
78+
failure: ProviderModelDiscoveryFailure,
79+
): boolean {
80+
const prev = discoveryStatus.get(provider);
81+
if (!prev || prev.status !== "failed") return true;
82+
if (prev.reason !== failure.reason) return true;
83+
if (prev.reason === "http" && failure.reason === "http") {
84+
return prev.httpStatus !== failure.httpStatus;
85+
}
86+
return false;
87+
}
88+
6689
export function clearProviderDiscoveryStatus(provider: string): void {
6790
discoveryStatus.delete(provider);
6891
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { afterEach, describe, expect, test } from "bun:test";
2+
import {
3+
clearModelCache,
4+
markProviderDiscoveryFailed,
5+
markProviderDiscoveryOk,
6+
shouldLogDiscoveryFailure,
7+
} from "../src/codex/model-cache";
8+
9+
const P = "azure-foundry-anthropic";
10+
11+
afterEach(() => clearModelCache(P));
12+
13+
describe("shouldLogDiscoveryFailure (#395 log flood)", () => {
14+
test("logs the first failure, then suppresses an identical repeat", () => {
15+
// First 404: no prior status -> log.
16+
expect(shouldLogDiscoveryFailure(P, { reason: "http", httpStatus: 404 })).toBe(true);
17+
markProviderDiscoveryFailed(P, { reason: "http", httpStatus: 404 });
18+
// Same 404 signature on the next poll -> suppressed.
19+
expect(shouldLogDiscoveryFailure(P, { reason: "http", httpStatus: 404 })).toBe(false);
20+
markProviderDiscoveryFailed(P, { reason: "http", httpStatus: 404 });
21+
expect(shouldLogDiscoveryFailure(P, { reason: "http", httpStatus: 404 })).toBe(false);
22+
});
23+
24+
test("logs again when the HTTP status changes", () => {
25+
markProviderDiscoveryFailed(P, { reason: "http", httpStatus: 404 });
26+
expect(shouldLogDiscoveryFailure(P, { reason: "http", httpStatus: 500 })).toBe(true);
27+
});
28+
29+
test("logs again when the failure reason changes", () => {
30+
markProviderDiscoveryFailed(P, { reason: "http", httpStatus: 404 });
31+
expect(shouldLogDiscoveryFailure(P, { reason: "network" })).toBe(true);
32+
markProviderDiscoveryFailed(P, { reason: "network" });
33+
expect(shouldLogDiscoveryFailure(P, { reason: "network" })).toBe(false);
34+
});
35+
36+
test("a recovery (ok) resets suppression so the next failure logs", () => {
37+
markProviderDiscoveryFailed(P, { reason: "http", httpStatus: 404 });
38+
expect(shouldLogDiscoveryFailure(P, { reason: "http", httpStatus: 404 })).toBe(false);
39+
markProviderDiscoveryOk(P);
40+
expect(shouldLogDiscoveryFailure(P, { reason: "http", httpStatus: 404 })).toBe(true);
41+
});
42+
43+
test("clearing the cache resets suppression", () => {
44+
markProviderDiscoveryFailed(P, { reason: "http", httpStatus: 404 });
45+
expect(shouldLogDiscoveryFailure(P, { reason: "http", httpStatus: 404 })).toBe(false);
46+
clearModelCache(P);
47+
expect(shouldLogDiscoveryFailure(P, { reason: "http", httpStatus: 404 })).toBe(true);
48+
});
49+
});

0 commit comments

Comments
 (0)