fix(agents): avoid duplicate same-provider cooldown probes in fallback runs (#41711)

Merged via squash.

Prepared head SHA: 8be8967bcb
Co-authored-by: cgdusek <38732970+cgdusek@users.noreply.github.com>
Co-authored-by: altaywtf <9790196+altaywtf@users.noreply.github.com>
Reviewed-by: @altaywtf
This commit is contained in:
Charles Dusek
2026-03-10 07:26:47 -05:00
committed by GitHub
parent bda63c3c7f
commit 048e25c2b2
3 changed files with 129 additions and 0 deletions

View File

@@ -1318,6 +1318,86 @@ describe("runWithModelFallback", () => {
}); // Rate limit allows attempt
expect(run).toHaveBeenNthCalledWith(2, "groq", "llama-3.3-70b-versatile"); // Cross-provider works
});
it("limits cooldown probes to one per provider before moving to cross-provider fallback", async () => {
const { dir } = await makeAuthStoreWithCooldown("anthropic", "rate_limit");
const cfg = makeCfg({
agents: {
defaults: {
model: {
primary: "anthropic/claude-opus-4-6",
fallbacks: [
"anthropic/claude-sonnet-4-5",
"anthropic/claude-haiku-3-5",
"groq/llama-3.3-70b-versatile",
],
},
},
},
});
const run = vi
.fn()
.mockRejectedValueOnce(new Error("Still rate limited")) // First same-provider probe fails
.mockResolvedValueOnce("groq success"); // Next provider succeeds
const result = await runWithModelFallback({
cfg,
provider: "anthropic",
model: "claude-opus-4-6",
run,
agentDir: dir,
});
expect(result.result).toBe("groq success");
// Primary is skipped, first same-provider fallback is probed, second same-provider fallback
// is skipped (probe already attempted), then cross-provider fallback runs.
expect(run).toHaveBeenCalledTimes(2);
expect(run).toHaveBeenNthCalledWith(1, "anthropic", "claude-sonnet-4-5", {
allowTransientCooldownProbe: true,
});
expect(run).toHaveBeenNthCalledWith(2, "groq", "llama-3.3-70b-versatile");
});
it("does not consume transient probe slot when first same-provider probe fails with model_not_found", async () => {
const { dir } = await makeAuthStoreWithCooldown("anthropic", "rate_limit");
const cfg = makeCfg({
agents: {
defaults: {
model: {
primary: "anthropic/claude-opus-4-6",
fallbacks: [
"anthropic/claude-sonnet-4-5",
"anthropic/claude-haiku-3-5",
"groq/llama-3.3-70b-versatile",
],
},
},
},
});
const run = vi
.fn()
.mockRejectedValueOnce(new Error("Model not found: anthropic/claude-sonnet-4-5"))
.mockResolvedValueOnce("haiku success");
const result = await runWithModelFallback({
cfg,
provider: "anthropic",
model: "claude-opus-4-6",
run,
agentDir: dir,
});
expect(result.result).toBe("haiku success");
expect(run).toHaveBeenCalledTimes(2);
expect(run).toHaveBeenNthCalledWith(1, "anthropic", "claude-sonnet-4-5", {
allowTransientCooldownProbe: true,
});
expect(run).toHaveBeenNthCalledWith(2, "anthropic", "claude-haiku-3-5", {
allowTransientCooldownProbe: true,
});
});
});
});