2026-02-16 10:03:35 -03:00
|
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
|
|
import type { OpenClawConfig } from "../config/config.js";
|
|
|
|
|
|
import type { AuthProfileStore } from "./auth-profiles.js";
|
2026-02-22 20:01:43 +00:00
|
|
|
|
import { makeModelFallbackCfg } from "./test-helpers/model-fallback-config-fixture.js";
|
2026-02-16 10:03:35 -03:00
|
|
|
|
|
|
|
|
|
|
// Mock auth-profiles module — must be before importing model-fallback
|
|
|
|
|
|
vi.mock("./auth-profiles.js", () => ({
|
|
|
|
|
|
ensureAuthProfileStore: vi.fn(),
|
|
|
|
|
|
getSoonestCooldownExpiry: vi.fn(),
|
|
|
|
|
|
isProfileInCooldown: vi.fn(),
|
|
|
|
|
|
resolveAuthProfileOrder: vi.fn(),
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
|
ensureAuthProfileStore,
|
|
|
|
|
|
getSoonestCooldownExpiry,
|
|
|
|
|
|
isProfileInCooldown,
|
|
|
|
|
|
resolveAuthProfileOrder,
|
|
|
|
|
|
} from "./auth-profiles.js";
|
|
|
|
|
|
import { _probeThrottleInternals, runWithModelFallback } from "./model-fallback.js";
|
|
|
|
|
|
|
|
|
|
|
|
const mockedEnsureAuthProfileStore = vi.mocked(ensureAuthProfileStore);
|
|
|
|
|
|
const mockedGetSoonestCooldownExpiry = vi.mocked(getSoonestCooldownExpiry);
|
|
|
|
|
|
const mockedIsProfileInCooldown = vi.mocked(isProfileInCooldown);
|
|
|
|
|
|
const mockedResolveAuthProfileOrder = vi.mocked(resolveAuthProfileOrder);
|
|
|
|
|
|
|
2026-02-22 20:01:43 +00:00
|
|
|
|
const makeCfg = makeModelFallbackCfg;
|
2026-02-16 10:03:35 -03:00
|
|
|
|
|
2026-02-22 17:11:17 +00:00
|
|
|
|
function expectFallbackUsed(
|
|
|
|
|
|
result: { result: unknown; attempts: Array<{ reason?: string }> },
|
|
|
|
|
|
run: {
|
|
|
|
|
|
(...args: unknown[]): unknown;
|
|
|
|
|
|
mock: { calls: unknown[][] };
|
|
|
|
|
|
},
|
|
|
|
|
|
) {
|
|
|
|
|
|
expect(result.result).toBe("ok");
|
|
|
|
|
|
expect(run).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
expect(run).toHaveBeenCalledWith("anthropic", "claude-haiku-3-5");
|
|
|
|
|
|
expect(result.attempts[0]?.reason).toBe("rate_limit");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-22 20:01:43 +00:00
|
|
|
|
function expectPrimaryProbeSuccess(
|
|
|
|
|
|
result: { result: unknown },
|
|
|
|
|
|
run: {
|
|
|
|
|
|
(...args: unknown[]): unknown;
|
|
|
|
|
|
mock: { calls: unknown[][] };
|
|
|
|
|
|
},
|
|
|
|
|
|
expectedResult: unknown,
|
|
|
|
|
|
) {
|
|
|
|
|
|
expect(result.result).toBe(expectedResult);
|
|
|
|
|
|
expect(run).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
expect(run).toHaveBeenCalledWith("openai", "gpt-4.1-mini");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-16 10:03:35 -03:00
|
|
|
|
describe("runWithModelFallback – probe logic", () => {
|
|
|
|
|
|
let realDateNow: () => number;
|
|
|
|
|
|
const NOW = 1_700_000_000_000;
|
|
|
|
|
|
|
2026-02-22 20:01:43 +00:00
|
|
|
|
const runPrimaryCandidate = (
|
|
|
|
|
|
cfg: OpenClawConfig,
|
|
|
|
|
|
run: (provider: string, model: string) => Promise<unknown>,
|
|
|
|
|
|
) =>
|
|
|
|
|
|
runWithModelFallback({
|
|
|
|
|
|
cfg,
|
|
|
|
|
|
provider: "openai",
|
|
|
|
|
|
model: "gpt-4.1-mini",
|
|
|
|
|
|
run,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-16 10:03:35 -03:00
|
|
|
|
beforeEach(() => {
|
|
|
|
|
|
realDateNow = Date.now;
|
|
|
|
|
|
Date.now = vi.fn(() => NOW);
|
|
|
|
|
|
|
|
|
|
|
|
// Clear throttle state between tests
|
|
|
|
|
|
_probeThrottleInternals.lastProbeAttempt.clear();
|
|
|
|
|
|
|
|
|
|
|
|
// Default: ensureAuthProfileStore returns a fake store
|
|
|
|
|
|
const fakeStore: AuthProfileStore = {
|
|
|
|
|
|
version: 1,
|
|
|
|
|
|
profiles: {},
|
|
|
|
|
|
};
|
|
|
|
|
|
mockedEnsureAuthProfileStore.mockReturnValue(fakeStore);
|
|
|
|
|
|
|
|
|
|
|
|
// Default: resolveAuthProfileOrder returns profiles only for "openai" provider
|
|
|
|
|
|
mockedResolveAuthProfileOrder.mockImplementation(({ provider }: { provider: string }) => {
|
|
|
|
|
|
if (provider === "openai") {
|
|
|
|
|
|
return ["openai-profile-1"];
|
|
|
|
|
|
}
|
|
|
|
|
|
if (provider === "anthropic") {
|
|
|
|
|
|
return ["anthropic-profile-1"];
|
|
|
|
|
|
}
|
|
|
|
|
|
if (provider === "google") {
|
|
|
|
|
|
return ["google-profile-1"];
|
|
|
|
|
|
}
|
|
|
|
|
|
return [];
|
|
|
|
|
|
});
|
|
|
|
|
|
// Default: only openai profiles are in cooldown; fallback providers are available
|
|
|
|
|
|
mockedIsProfileInCooldown.mockImplementation((_store, profileId: string) => {
|
|
|
|
|
|
return profileId.startsWith("openai");
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
|
Date.now = realDateNow;
|
|
|
|
|
|
vi.restoreAllMocks();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("skips primary model when far from cooldown expiry (30 min remaining)", async () => {
|
|
|
|
|
|
const cfg = makeCfg();
|
|
|
|
|
|
// Cooldown expires in 30 min — well beyond the 2-min margin
|
|
|
|
|
|
const expiresIn30Min = NOW + 30 * 60 * 1000;
|
|
|
|
|
|
mockedGetSoonestCooldownExpiry.mockReturnValue(expiresIn30Min);
|
|
|
|
|
|
|
|
|
|
|
|
const run = vi.fn().mockResolvedValue("ok");
|
|
|
|
|
|
|
2026-02-22 20:01:43 +00:00
|
|
|
|
const result = await runPrimaryCandidate(cfg, run);
|
2026-02-16 10:03:35 -03:00
|
|
|
|
|
|
|
|
|
|
// Should skip primary and use fallback
|
2026-02-22 17:11:17 +00:00
|
|
|
|
expectFallbackUsed(result, run);
|
2026-02-16 10:03:35 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("probes primary model when within 2-min margin of cooldown expiry", async () => {
|
|
|
|
|
|
const cfg = makeCfg();
|
|
|
|
|
|
// Cooldown expires in 1 minute — within 2-min probe margin
|
|
|
|
|
|
const expiresIn1Min = NOW + 60 * 1000;
|
|
|
|
|
|
mockedGetSoonestCooldownExpiry.mockReturnValue(expiresIn1Min);
|
|
|
|
|
|
|
|
|
|
|
|
const run = vi.fn().mockResolvedValue("probed-ok");
|
|
|
|
|
|
|
2026-02-22 20:01:43 +00:00
|
|
|
|
const result = await runPrimaryCandidate(cfg, run);
|
|
|
|
|
|
expectPrimaryProbeSuccess(result, run, "probed-ok");
|
2026-02-16 10:03:35 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("probes primary model when cooldown already expired", async () => {
|
|
|
|
|
|
const cfg = makeCfg();
|
|
|
|
|
|
// Cooldown expired 5 min ago
|
|
|
|
|
|
const expiredAlready = NOW - 5 * 60 * 1000;
|
|
|
|
|
|
mockedGetSoonestCooldownExpiry.mockReturnValue(expiredAlready);
|
|
|
|
|
|
|
|
|
|
|
|
const run = vi.fn().mockResolvedValue("recovered");
|
|
|
|
|
|
|
2026-02-22 20:01:43 +00:00
|
|
|
|
const result = await runPrimaryCandidate(cfg, run);
|
|
|
|
|
|
expectPrimaryProbeSuccess(result, run, "recovered");
|
2026-02-16 10:03:35 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("does NOT probe non-primary candidates during cooldown", async () => {
|
|
|
|
|
|
const cfg = makeCfg({
|
|
|
|
|
|
agents: {
|
|
|
|
|
|
defaults: {
|
|
|
|
|
|
model: {
|
|
|
|
|
|
primary: "openai/gpt-4.1-mini",
|
|
|
|
|
|
fallbacks: ["anthropic/claude-haiku-3-5", "google/gemini-2-flash"],
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
} as Partial<OpenClawConfig>);
|
|
|
|
|
|
|
|
|
|
|
|
// Override: ALL providers in cooldown for this test
|
|
|
|
|
|
mockedIsProfileInCooldown.mockReturnValue(true);
|
|
|
|
|
|
|
|
|
|
|
|
// All profiles in cooldown, cooldown just about to expire
|
|
|
|
|
|
const almostExpired = NOW + 30 * 1000; // 30s remaining
|
|
|
|
|
|
mockedGetSoonestCooldownExpiry.mockReturnValue(almostExpired);
|
|
|
|
|
|
|
|
|
|
|
|
// Primary probe fails with 429
|
|
|
|
|
|
const run = vi
|
|
|
|
|
|
.fn()
|
|
|
|
|
|
.mockRejectedValueOnce(Object.assign(new Error("rate limited"), { status: 429 }))
|
|
|
|
|
|
.mockResolvedValue("should-not-reach");
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
await runWithModelFallback({
|
|
|
|
|
|
cfg,
|
|
|
|
|
|
provider: "openai",
|
|
|
|
|
|
model: "gpt-4.1-mini",
|
|
|
|
|
|
run,
|
|
|
|
|
|
});
|
|
|
|
|
|
expect.unreachable("should have thrown since all candidates exhausted");
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
// Primary was probed (i === 0 + within margin), non-primary were skipped
|
|
|
|
|
|
expect(run).toHaveBeenCalledTimes(1); // only primary was actually called
|
|
|
|
|
|
expect(run).toHaveBeenCalledWith("openai", "gpt-4.1-mini");
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("throttles probe when called within 30s interval", async () => {
|
|
|
|
|
|
const cfg = makeCfg();
|
|
|
|
|
|
// Cooldown just about to expire (within probe margin)
|
|
|
|
|
|
const almostExpired = NOW + 30 * 1000;
|
|
|
|
|
|
mockedGetSoonestCooldownExpiry.mockReturnValue(almostExpired);
|
|
|
|
|
|
|
|
|
|
|
|
// Simulate a recent probe 10s ago
|
|
|
|
|
|
_probeThrottleInternals.lastProbeAttempt.set("openai", NOW - 10_000);
|
|
|
|
|
|
|
|
|
|
|
|
const run = vi.fn().mockResolvedValue("ok");
|
|
|
|
|
|
|
2026-02-22 20:01:43 +00:00
|
|
|
|
const result = await runPrimaryCandidate(cfg, run);
|
2026-02-16 10:03:35 -03:00
|
|
|
|
|
|
|
|
|
|
// Should be throttled → skip primary, use fallback
|
2026-02-22 17:11:17 +00:00
|
|
|
|
expectFallbackUsed(result, run);
|
2026-02-16 10:03:35 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("allows probe when 30s have passed since last probe", async () => {
|
|
|
|
|
|
const cfg = makeCfg();
|
|
|
|
|
|
const almostExpired = NOW + 30 * 1000;
|
|
|
|
|
|
mockedGetSoonestCooldownExpiry.mockReturnValue(almostExpired);
|
|
|
|
|
|
|
|
|
|
|
|
// Last probe was 31s ago — should NOT be throttled
|
|
|
|
|
|
_probeThrottleInternals.lastProbeAttempt.set("openai", NOW - 31_000);
|
|
|
|
|
|
|
|
|
|
|
|
const run = vi.fn().mockResolvedValue("probed-ok");
|
|
|
|
|
|
|
2026-02-22 20:01:43 +00:00
|
|
|
|
const result = await runPrimaryCandidate(cfg, run);
|
|
|
|
|
|
expectPrimaryProbeSuccess(result, run, "probed-ok");
|
2026-02-16 10:03:35 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("handles non-finite soonest safely (treats as probe-worthy)", async () => {
|
|
|
|
|
|
const cfg = makeCfg();
|
|
|
|
|
|
|
|
|
|
|
|
// Return Infinity — should be treated as "probe" per the guard
|
|
|
|
|
|
mockedGetSoonestCooldownExpiry.mockReturnValue(Infinity);
|
|
|
|
|
|
|
|
|
|
|
|
const run = vi.fn().mockResolvedValue("ok-infinity");
|
|
|
|
|
|
|
2026-02-22 20:01:43 +00:00
|
|
|
|
const result = await runPrimaryCandidate(cfg, run);
|
|
|
|
|
|
expectPrimaryProbeSuccess(result, run, "ok-infinity");
|
2026-02-16 10:03:35 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("handles NaN soonest safely (treats as probe-worthy)", async () => {
|
|
|
|
|
|
const cfg = makeCfg();
|
|
|
|
|
|
|
|
|
|
|
|
mockedGetSoonestCooldownExpiry.mockReturnValue(NaN);
|
|
|
|
|
|
|
|
|
|
|
|
const run = vi.fn().mockResolvedValue("ok-nan");
|
|
|
|
|
|
|
2026-02-22 20:01:43 +00:00
|
|
|
|
const result = await runPrimaryCandidate(cfg, run);
|
|
|
|
|
|
expectPrimaryProbeSuccess(result, run, "ok-nan");
|
2026-02-16 10:03:35 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("handles null soonest safely (treats as probe-worthy)", async () => {
|
|
|
|
|
|
const cfg = makeCfg();
|
|
|
|
|
|
|
|
|
|
|
|
mockedGetSoonestCooldownExpiry.mockReturnValue(null);
|
|
|
|
|
|
|
|
|
|
|
|
const run = vi.fn().mockResolvedValue("ok-null");
|
|
|
|
|
|
|
2026-02-22 20:01:43 +00:00
|
|
|
|
const result = await runPrimaryCandidate(cfg, run);
|
|
|
|
|
|
expectPrimaryProbeSuccess(result, run, "ok-null");
|
2026-02-16 10:03:35 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-16 08:07:14 -05:00
|
|
|
|
it("single candidate skips with rate_limit and exhausts candidates", async () => {
|
2026-02-16 10:03:35 -03:00
|
|
|
|
const cfg = makeCfg({
|
|
|
|
|
|
agents: {
|
|
|
|
|
|
defaults: {
|
|
|
|
|
|
model: {
|
|
|
|
|
|
primary: "openai/gpt-4.1-mini",
|
2026-02-16 08:07:14 -05:00
|
|
|
|
fallbacks: [],
|
2026-02-16 10:03:35 -03:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
} as Partial<OpenClawConfig>);
|
|
|
|
|
|
|
|
|
|
|
|
const almostExpired = NOW + 30 * 1000;
|
|
|
|
|
|
mockedGetSoonestCooldownExpiry.mockReturnValue(almostExpired);
|
|
|
|
|
|
|
2026-02-16 08:07:14 -05:00
|
|
|
|
const run = vi.fn().mockResolvedValue("unreachable");
|
2026-02-16 10:03:35 -03:00
|
|
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
|
runWithModelFallback({
|
|
|
|
|
|
cfg,
|
|
|
|
|
|
provider: "openai",
|
|
|
|
|
|
model: "gpt-4.1-mini",
|
|
|
|
|
|
fallbacksOverride: [],
|
|
|
|
|
|
run,
|
|
|
|
|
|
}),
|
2026-02-16 08:07:14 -05:00
|
|
|
|
).rejects.toThrow("All models failed");
|
2026-02-16 10:03:35 -03:00
|
|
|
|
|
2026-02-16 08:07:14 -05:00
|
|
|
|
expect(run).not.toHaveBeenCalled();
|
2026-02-16 10:03:35 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-16 08:07:14 -05:00
|
|
|
|
it("scopes probe throttling by agentDir to avoid cross-agent suppression", async () => {
|
|
|
|
|
|
const cfg = makeCfg();
|
2026-02-16 10:03:35 -03:00
|
|
|
|
const almostExpired = NOW + 30 * 1000;
|
|
|
|
|
|
mockedGetSoonestCooldownExpiry.mockReturnValue(almostExpired);
|
|
|
|
|
|
|
2026-02-16 08:07:14 -05:00
|
|
|
|
const run = vi.fn().mockResolvedValue("probed-ok");
|
2026-02-16 10:03:35 -03:00
|
|
|
|
|
2026-02-16 08:07:14 -05:00
|
|
|
|
await runWithModelFallback({
|
|
|
|
|
|
cfg,
|
|
|
|
|
|
provider: "openai",
|
|
|
|
|
|
model: "gpt-4.1-mini",
|
|
|
|
|
|
agentDir: "/tmp/agent-a",
|
|
|
|
|
|
run,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await runWithModelFallback({
|
|
|
|
|
|
cfg,
|
|
|
|
|
|
provider: "openai",
|
|
|
|
|
|
model: "gpt-4.1-mini",
|
|
|
|
|
|
agentDir: "/tmp/agent-b",
|
|
|
|
|
|
run,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
expect(run).toHaveBeenNthCalledWith(1, "openai", "gpt-4.1-mini");
|
|
|
|
|
|
expect(run).toHaveBeenNthCalledWith(2, "openai", "gpt-4.1-mini");
|
2026-02-16 10:03:35 -03:00
|
|
|
|
});
|
|
|
|
|
|
});
|