Skip to content

Commit 47adae9

Browse files
authored
fix(auth-profiles): scope model_not_found cooldown to the failing model (#116768)
Treat model_not_found cooldowns as model-scoped so healthy sibling fallbacks remain available. Preserve profile-wide widening on a second distinct failure and missing model identity. Fixes #116464.
1 parent 749f5b2 commit 47adae9

3 files changed

Lines changed: 234 additions & 4 deletions

File tree

‎src/agents/auth-profiles.getsoonestcooldownexpiry.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,26 @@ describe("getSoonestCooldownExpiry", () => {
120120
).toBe(now + 10_000);
121121
});
122122

123+
it("uses the earliest matching model_not_found cooldown for the requested model", () => {
124+
const now = 1_700_000_000_000;
125+
const store = makeStore({
126+
"openai:p1": {
127+
cooldownUntil: now + 10_000,
128+
cooldownReason: "model_not_found",
129+
cooldownModel: "gpt-5.4",
130+
},
131+
"openai:p2": {
132+
cooldownUntil: now + 30_000,
133+
cooldownReason: "model_not_found",
134+
cooldownModel: "gpt-5.4",
135+
},
136+
});
137+
138+
expect(
139+
getSoonestCooldownExpiry(store, ["openai:p1", "openai:p2"], { now, forModel: "gpt-5.4" }),
140+
).toBe(now + 10_000);
141+
});
142+
123143
it("still counts profile-wide disables for other models", () => {
124144
const now = 1_700_000_000_000;
125145
const store = makeStore({

‎src/agents/auth-profiles/usage-state.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ export function isAuthCooldownBypassedForProvider(provider: string | undefined):
1313
return normalized === "openrouter" || normalized === "kilocode";
1414
}
1515

16-
// Per-attempt transient failures (#87462): block only the failing model so
17-
// fallback models on the same auth profile can still try. Other reasons (auth,
18-
// billing, format, server_error) remain profile-wide.
16+
// Per-attempt transient failures (#87462, #116464): block only the failing
17+
// model so fallback models on the same auth profile can still try. A model that
18+
// the provider does not serve (model_not_found) says nothing about sibling
19+
// models, so it stays model-scoped too. Other reasons (auth, billing, format,
20+
// server_error) remain profile-wide.
1921
/** Returns true when a failure should only cool down the failing model. */
2022
export function isModelScopedCooldownReason(reason: AuthProfileFailureReason | undefined): boolean {
21-
return reason === "rate_limit" || reason === "timeout";
23+
return reason === "rate_limit" || reason === "timeout" || reason === "model_not_found";
2224
}
2325

2426
/** Resolves the latest active blocked/cooldown/disabled timestamp for a profile. */

‎src/agents/auth-profiles/usage.test.ts‎

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { resolveProfileUnusableUntil } from "./usage-state.js";
1111
import {
1212
clearAuthProfileCooldown,
1313
clearExpiredCooldowns,
14+
getSoonestCooldownExpiry,
1415
isProfileInCooldown,
1516
markAuthProfileBlockedUntil,
1617
markAuthProfileFailure,
@@ -298,6 +299,40 @@ describe("isProfileInCooldown", () => {
298299
);
299300
});
300301

302+
it("returns false for a different model when cooldown is model-scoped (model_not_found) — #116464", () => {
303+
const store = makeStore({
304+
"github-copilot:github": {
305+
cooldownUntil: Date.now() + 60_000,
306+
cooldownReason: "model_not_found",
307+
cooldownModel: "claude-sonnet-4.6",
308+
},
309+
});
310+
// A healthy sibling model on the same auth profile bypasses the cooldown
311+
expect(isProfileInCooldown(store, "github-copilot:github", undefined, "gpt-4.1")).toBe(false);
312+
// The failed model itself stays blocked
313+
expect(
314+
isProfileInCooldown(store, "github-copilot:github", undefined, "claude-sonnet-4.6"),
315+
).toBe(true);
316+
// No model specified — blocked (conservative)
317+
expect(isProfileInCooldown(store, "github-copilot:github")).toBe(true);
318+
});
319+
320+
it("blocks all models when a model_not_found cooldown has no cooldownModel (profile-wide) — #116464", () => {
321+
const store = makeStore({
322+
"github-copilot:github": {
323+
cooldownUntil: Date.now() + 60_000,
324+
cooldownReason: "model_not_found",
325+
cooldownModel: undefined,
326+
},
327+
});
328+
// Without a scoped model, the cooldown stays profile-wide so neither a
329+
// sibling model nor the originally failing model can bypass it.
330+
expect(isProfileInCooldown(store, "github-copilot:github", undefined, "gpt-4.1")).toBe(true);
331+
expect(
332+
isProfileInCooldown(store, "github-copilot:github", undefined, "claude-sonnet-4.6"),
333+
).toBe(true);
334+
});
335+
301336
it("does not bypass model-scoped cooldown when disabledUntil is active", () => {
302337
const store = makeStore({
303338
"github-copilot:github": {
@@ -343,6 +378,47 @@ describe("isProfileInCooldown", () => {
343378
});
344379
});
345380

381+
describe("getSoonestCooldownExpiry", () => {
382+
it("treats a model_not_found cooldown for the requested model as model-scoped — #116464", () => {
383+
const now = Date.now();
384+
const store = makeStore({
385+
"github-copilot:github": {
386+
cooldownUntil: now + 60_000,
387+
cooldownReason: "model_not_found",
388+
cooldownModel: "claude-sonnet-4.6",
389+
},
390+
});
391+
// Same model: expiry is tracked as the matching model-scoped cooldown
392+
const sameModel = getSoonestCooldownExpiry(store, ["github-copilot:github"], {
393+
now,
394+
forModel: "claude-sonnet-4.6",
395+
});
396+
expect(sameModel).toBe(now + 60_000);
397+
// Different model bypasses the cooldown entirely
398+
const sibling = getSoonestCooldownExpiry(store, ["github-copilot:github"], {
399+
now,
400+
forModel: "gpt-4.1",
401+
});
402+
expect(sibling).toBeNull();
403+
});
404+
405+
it("keeps profile-wide cooldowns visible to all models", () => {
406+
const now = Date.now();
407+
const store = makeStore({
408+
"github-copilot:github": {
409+
cooldownUntil: now + 60_000,
410+
cooldownReason: "model_not_found",
411+
cooldownModel: undefined,
412+
},
413+
});
414+
const soonest = getSoonestCooldownExpiry(store, ["github-copilot:github"], {
415+
now,
416+
forModel: "gpt-4.1",
417+
});
418+
expect(soonest).toBe(now + 60_000);
419+
});
420+
});
421+
346422
describe("resolveProfilesUnavailableReason", () => {
347423
it("prefers active disabledReason when profiles are disabled", () => {
348424
const now = Date.now();
@@ -1631,6 +1707,138 @@ describe("markAuthProfileFailure — per-model cooldown metadata", () => {
16311707
expect(stats?.cooldownModel).toBe("claude-sonnet-4.6");
16321708
});
16331709

1710+
it("records cooldownModel on first model_not_found failure — #116464", async () => {
1711+
const now = 1_000_000;
1712+
const store = makeStoreWithCopilot({});
1713+
vi.useFakeTimers();
1714+
vi.setSystemTime(now);
1715+
mockLockedUpdateForStore(store);
1716+
try {
1717+
await markAuthProfileFailure({
1718+
store,
1719+
profileId: "github-copilot:github",
1720+
reason: "model_not_found",
1721+
modelId: "claude-sonnet-4.6",
1722+
});
1723+
} finally {
1724+
vi.useRealTimers();
1725+
}
1726+
const stats = store.usageStats?.["github-copilot:github"];
1727+
expect(stats?.cooldownReason).toBe("model_not_found");
1728+
expect(stats?.cooldownModel).toBe("claude-sonnet-4.6");
1729+
});
1730+
1731+
it("widens cooldownModel to undefined when a different model fails during active model_not_found cooldown", async () => {
1732+
const now = 1_000_000;
1733+
const store = makeStoreWithCopilot({
1734+
"github-copilot:github": {
1735+
cooldownUntil: now + 30_000,
1736+
cooldownReason: "model_not_found",
1737+
cooldownModel: "claude-sonnet-4.6",
1738+
errorCount: 1,
1739+
lastFailureAt: now - 1000,
1740+
},
1741+
});
1742+
vi.useFakeTimers();
1743+
vi.setSystemTime(now);
1744+
mockLockedUpdateForStore(store);
1745+
try {
1746+
await markAuthProfileFailure({
1747+
store,
1748+
profileId: "github-copilot:github",
1749+
reason: "model_not_found",
1750+
modelId: "gpt-4.1",
1751+
});
1752+
} finally {
1753+
vi.useRealTimers();
1754+
}
1755+
const stats = store.usageStats?.["github-copilot:github"];
1756+
// Scope widened to all models so the sibling failure cannot bypass stale metadata
1757+
expect(stats?.cooldownModel).toBeUndefined();
1758+
expect(stats?.cooldownReason).toBe("model_not_found");
1759+
});
1760+
1761+
it("preserves cooldownModel when the same model fails again during active model_not_found cooldown", async () => {
1762+
const now = 1_000_000;
1763+
const store = makeStoreWithCopilot({
1764+
"github-copilot:github": {
1765+
cooldownUntil: now + 30_000,
1766+
cooldownReason: "model_not_found",
1767+
cooldownModel: "claude-sonnet-4.6",
1768+
errorCount: 1,
1769+
lastFailureAt: now - 1000,
1770+
},
1771+
});
1772+
vi.useFakeTimers();
1773+
vi.setSystemTime(now);
1774+
mockLockedUpdateForStore(store);
1775+
try {
1776+
await markAuthProfileFailure({
1777+
store,
1778+
profileId: "github-copilot:github",
1779+
reason: "model_not_found",
1780+
modelId: "claude-sonnet-4.6",
1781+
});
1782+
} finally {
1783+
vi.useRealTimers();
1784+
}
1785+
const stats = store.usageStats?.["github-copilot:github"];
1786+
expect(stats?.cooldownModel).toBe("claude-sonnet-4.6");
1787+
});
1788+
1789+
it("widens cooldownModel when model_not_found failure during active cooldown has no modelId", async () => {
1790+
const now = 1_000_000;
1791+
const store = makeStoreWithCopilot({
1792+
"github-copilot:github": {
1793+
cooldownUntil: now + 30_000,
1794+
cooldownReason: "model_not_found",
1795+
cooldownModel: "claude-sonnet-4.6",
1796+
errorCount: 1,
1797+
lastFailureAt: now - 1000,
1798+
},
1799+
});
1800+
vi.useFakeTimers();
1801+
vi.setSystemTime(now);
1802+
mockLockedUpdateForStore(store);
1803+
try {
1804+
await markAuthProfileFailure({
1805+
store,
1806+
profileId: "github-copilot:github",
1807+
reason: "model_not_found",
1808+
modelId: undefined,
1809+
});
1810+
} finally {
1811+
vi.useRealTimers();
1812+
}
1813+
const stats = store.usageStats?.["github-copilot:github"];
1814+
expect(stats?.cooldownReason).toBe("model_not_found");
1815+
expect(stats?.cooldownModel).toBeUndefined();
1816+
});
1817+
1818+
it("keeps a healthy sibling model available after a model_not_found failure on the same profile — #116464", async () => {
1819+
const now = 1_000_000;
1820+
const store = makeStoreWithCopilot({});
1821+
vi.useFakeTimers();
1822+
vi.setSystemTime(now);
1823+
mockLockedUpdateForStore(store);
1824+
try {
1825+
await markAuthProfileFailure({
1826+
store,
1827+
profileId: "github-copilot:github",
1828+
reason: "model_not_found",
1829+
modelId: "claude-sonnet-4.6",
1830+
});
1831+
} finally {
1832+
vi.useRealTimers();
1833+
}
1834+
// The failed model stays blocked, but a sibling fallback on the same auth
1835+
// profile remains available — the exact fallback scenario from #116464.
1836+
expect(isProfileInCooldown(store, "github-copilot:github", now, "claude-sonnet-4.6")).toBe(
1837+
true,
1838+
);
1839+
expect(isProfileInCooldown(store, "github-copilot:github", now, "gpt-4.1")).toBe(false);
1840+
});
1841+
16341842
it("widens cooldownModel to undefined when a different model fails during active cooldown", async () => {
16351843
const now = 1_000_000;
16361844
const store = makeStoreWithCopilot({

0 commit comments

Comments
 (0)