惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
M
MIT News - Artificial intelligence
D
Docker
S
SegmentFault 最新的问题
B
Blog
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
腾讯CDC
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
C
Check Point Blog
GbyAI
GbyAI
美团技术团队
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
量子位
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
test: streamline model fallback probe coverage · openclaw...
steipete · 2026-05-06 · via Recent Commits to openclaw:main

@@ -57,6 +57,7 @@ let mockedResolveAuthProfileOrder: ReturnType<

5757

typeof vi.mocked<AuthProfilesOrderModule["resolveAuthProfileOrder"]>

5858

>;

5959

let runWithModelFallback: ModelFallbackModule["runWithModelFallback"];

60+

let modelFallbackTesting: ModelFallbackModule["__testing"];

6061

let _probeThrottleInternals: ModelFallbackModule["_probeThrottleInternals"];

6162

let resetLogger: LoggerModule["resetLogger"];

6263

let setLoggerOverride: LoggerModule["setLoggerOverride"];

@@ -82,6 +83,7 @@ async function loadModelFallbackProbeModules() {

8283

);

8384

mockedResolveAuthProfileOrder = vi.mocked(authProfilesOrderModule.resolveAuthProfileOrder);

8485

runWithModelFallback = modelFallbackModule.runWithModelFallback;

86+

modelFallbackTesting = modelFallbackModule.__testing;

8587

_probeThrottleInternals = modelFallbackModule._probeThrottleInternals;

8688

resetLogger = loggerModule.resetLogger;

8789

setLoggerOverride = loggerModule.setLoggerOverride;

@@ -187,6 +189,34 @@ describe("runWithModelFallback – probe logic", () => {

187189

run,

188190

});

189191192+

function resolveOpenAiCooldownDecision(params: {

193+

reason: "rate_limit" | "overloaded" | "timeout" | "auth" | "billing";

194+

soonest: number | null;

195+

isPrimary?: boolean;

196+

hasFallbackCandidates?: boolean;

197+

requestedModel?: boolean;

198+

throttleKey?: string;

199+

}) {

200+

mockedGetSoonestCooldownExpiry.mockReturnValue(params.soonest);

201+

mockedResolveProfilesUnavailableReason.mockReturnValue(params.reason);

202+

return modelFallbackTesting.resolveCooldownDecision({

203+

candidate: { provider: "openai", model: "gpt-4.1-mini" },

204+

isPrimary: params.isPrimary ?? true,

205+

requestedModel: params.requestedModel ?? true,

206+

hasFallbackCandidates: params.hasFallbackCandidates ?? true,

207+

now: NOW,

208+

probeThrottleKey: params.throttleKey ?? "openai",

209+

authRuntime: {

210+

getSoonestCooldownExpiry: mockedGetSoonestCooldownExpiry,

211+

resolveProfilesUnavailableReason: mockedResolveProfilesUnavailableReason,

212+

} as unknown as Parameters<

213+

typeof modelFallbackTesting.resolveCooldownDecision

214+

>[0]["authRuntime"],

215+

authStore: { version: 1, profiles: {} },

216+

profileIds: ["openai-profile-1"],

217+

});

218+

}

219+190220

async function expectPrimarySkippedAfterLongCooldown(reason: "billing" | "rate_limit") {

191221

const cfg = makeCfg();

192222

const expiresIn30Min = NOW + 30 * 60 * 1000;

@@ -202,6 +232,7 @@ describe("runWithModelFallback – probe logic", () => {

202232

beforeEach(() => {

203233

realDateNow = Date.now;

204234

Date.now = vi.fn(() => NOW);

235+

setLoggerOverride({ level: "silent", consoleLevel: "silent" });

205236206237

// Clear throttle state between tests

207238

_probeThrottleInternals.lastProbeAttempt.clear();

@@ -261,16 +292,35 @@ describe("runWithModelFallback – probe logic", () => {

261292

await expectPrimarySkippedAfterLongCooldown("billing");

262293

});

263294264-

it("probes primary model when within 2-min margin of cooldown expiry", async () => {

265-

const cfg = makeCfg();

266-

// Cooldown expires in 1 minute — within 2-min probe margin

267-

const expiresIn1Min = NOW + 60 * 1000;

268-

mockedGetSoonestCooldownExpiry.mockReturnValue(expiresIn1Min);

269-270-

const run = vi.fn().mockResolvedValue("probed-ok");

271-272-

const result = await runPrimaryCandidate(cfg, run);

273-

expectPrimaryProbeSuccess(result, run, "probed-ok");

295+

it("decides when cooldowned primary probes are allowed", () => {

296+

expect(

297+

resolveOpenAiCooldownDecision({

298+

reason: "rate_limit",

299+

soonest: NOW + 60 * 1000,

300+

}),

301+

).toEqual({ type: "attempt", reason: "rate_limit", markProbe: true });

302+

expect(

303+

resolveOpenAiCooldownDecision({

304+

reason: "rate_limit",

305+

soonest: NOW - 5 * 60 * 1000,

306+

}),

307+

).toEqual({ type: "attempt", reason: "rate_limit", markProbe: true });

308+

expect(

309+

resolveOpenAiCooldownDecision({

310+

reason: "rate_limit",

311+

soonest: NOW + 30 * 1000,

312+

throttleKey: "recent-openai",

313+

}),

314+

).toEqual({ type: "attempt", reason: "rate_limit", markProbe: true });

315+316+

_probeThrottleInternals.lastProbeAttempt.set("recent-openai", NOW - 10_000);

317+

expect(

318+

resolveOpenAiCooldownDecision({

319+

reason: "rate_limit",

320+

soonest: NOW + 30 * 1000,

321+

throttleKey: "recent-openai",

322+

}),

323+

).toMatchObject({ type: "skip", reason: "rate_limit" });

274324

});

275325276326

it("logs primary metadata on probe success and failure fallback decisions", async () => {

@@ -399,18 +449,6 @@ describe("runWithModelFallback – probe logic", () => {

399449

);

400450

});

401451402-

it("probes primary model when cooldown already expired", async () => {

403-

const cfg = makeCfg();

404-

// Cooldown expired 5 min ago

405-

const expiredAlready = NOW - 5 * 60 * 1000;

406-

mockedGetSoonestCooldownExpiry.mockReturnValue(expiredAlready);

407-408-

const run = vi.fn().mockResolvedValue("recovered");

409-410-

const result = await runPrimaryCandidate(cfg, run);

411-

expectPrimaryProbeSuccess(result, run, "recovered");

412-

});

413-414452

it.each([

415453

{

416454

label: "rate-limit",

@@ -503,37 +541,6 @@ describe("runWithModelFallback – probe logic", () => {

503541

expect(run).toHaveBeenNthCalledWith(3, "deepseek", "deepseek-chat");

504542

});

505543506-

it("throttles probe when called within 30s interval", async () => {

507-

const cfg = makeCfg();

508-

// Cooldown just about to expire (within probe margin)

509-

const almostExpired = NOW + 30 * 1000;

510-

mockedGetSoonestCooldownExpiry.mockReturnValue(almostExpired);

511-512-

// Simulate a recent probe 10s ago

513-

_probeThrottleInternals.lastProbeAttempt.set("openai", NOW - 10_000);

514-515-

const run = vi.fn().mockResolvedValue("ok");

516-517-

const result = await runPrimaryCandidate(cfg, run);

518-519-

// Should be throttled → skip primary, use fallback

520-

expectFallbackUsed(result, run);

521-

});

522-523-

it("allows probe when 30s have passed since last probe", async () => {

524-

const cfg = makeCfg();

525-

const almostExpired = NOW + 30 * 1000;

526-

mockedGetSoonestCooldownExpiry.mockReturnValue(almostExpired);

527-528-

// Last probe was 31s ago — should NOT be throttled

529-

_probeThrottleInternals.lastProbeAttempt.set("openai", NOW - 31_000);

530-531-

const run = vi.fn().mockResolvedValue("probed-ok");

532-533-

const result = await runPrimaryCandidate(cfg, run);

534-

expectPrimaryProbeSuccess(result, run, "probed-ok");

535-

});

536-537544

it("prunes stale probe throttle entries before checking eligibility", () => {

538545

_probeThrottleInternals.lastProbeAttempt.set(

539546

"stale",

@@ -564,21 +571,21 @@ describe("runWithModelFallback – probe logic", () => {

564571

expect(_probeThrottleInternals.lastProbeAttempt.has("key-0")).toBe(true);

565572

});

566573567-

it("handles missing or non-finite soonest safely (treats as probe-worthy)", async () => {

568-

const cfg = makeCfg();

569-574+

it("handles missing or non-finite soonest safely (treats as probe-worthy)", () => {

570575

for (const [label, soonest] of [

571576

["infinity", Infinity],

572577

["nan", Number.NaN],

573578

["null", null],

574579

] as const) {

575580

_probeThrottleInternals.lastProbeAttempt.clear();

576-

mockedGetSoonestCooldownExpiry.mockReturnValue(soonest);

577-578-

const run = vi.fn().mockResolvedValue(`ok-${label}`);

579581580-

const result = await runPrimaryCandidate(cfg, run);

581-

expectPrimaryProbeSuccess(result, run, `ok-${label}`);

582+

expect(

583+

resolveOpenAiCooldownDecision({

584+

reason: "rate_limit",

585+

soonest,

586+

}),

587+

label,

588+

).toEqual({ type: "attempt", reason: "rate_limit", markProbe: true });

582589

}

583590

});

584591

@@ -612,84 +619,49 @@ describe("runWithModelFallback – probe logic", () => {

612619

expect(run).not.toHaveBeenCalled();

613620

});

614621615-

it("scopes probe throttling by agentDir to avoid cross-agent suppression", async () => {

616-

const cfg = makeCfg();

617-

const almostExpired = NOW + 30 * 1000;

618-

mockedGetSoonestCooldownExpiry.mockReturnValue(almostExpired);

619-620-

const run = vi.fn().mockResolvedValue("probed-ok");

621-622-

await runWithModelFallback({

623-

cfg,

624-

provider: "openai",

625-

model: "gpt-4.1-mini",

626-

agentDir: "/tmp/agent-a",

627-

run,

628-

});

622+

it("scopes probe throttling by agentDir to avoid cross-agent suppression", () => {

623+

const agentAKey = _probeThrottleInternals.resolveProbeThrottleKey("openai", "/tmp/agent-a");

624+

const agentBKey = _probeThrottleInternals.resolveProbeThrottleKey("openai", "/tmp/agent-b");

625+

_probeThrottleInternals.lastProbeAttempt.set(agentAKey, NOW - 10_000);

629626630-

await runWithModelFallback({

631-

cfg,

632-

provider: "openai",

633-

model: "gpt-4.1-mini",

634-

agentDir: "/tmp/agent-b",

635-

run,

636-

});

637-638-

expect(run).toHaveBeenNthCalledWith(1, "openai", "gpt-4.1-mini", {

639-

allowTransientCooldownProbe: true,

640-

});

641-

expect(run).toHaveBeenNthCalledWith(2, "openai", "gpt-4.1-mini", {

642-

allowTransientCooldownProbe: true,

643-

});

627+

expect(

628+

resolveOpenAiCooldownDecision({

629+

reason: "rate_limit",

630+

soonest: NOW + 30 * 1000,

631+

throttleKey: agentAKey,

632+

}),

633+

).toMatchObject({ type: "skip", reason: "rate_limit" });

634+

expect(

635+

resolveOpenAiCooldownDecision({

636+

reason: "rate_limit",

637+

soonest: NOW + 30 * 1000,

638+

throttleKey: agentBKey,

639+

}),

640+

).toEqual({ type: "attempt", reason: "rate_limit", markProbe: true });

644641

});

645642646-

it("probes billing-cooldowned primary when no fallback candidates exist", async () => {

647-

const cfg = makeCfg({

648-

agents: {

649-

defaults: {

650-

model: {

651-

primary: "openai/gpt-4.1-mini",

652-

fallbacks: [],

653-

},

654-

},

655-

},

656-

} as Partial<OpenClawConfig>);

657-643+

it("decides when billing cooldowns should probe", () => {

658644

// Single-provider setups need periodic probes even when the billing

659645

// cooldown is far from expiry, otherwise topping up credits never recovers

660646

// without a restart.

661-

const expiresIn30Min = NOW + 30 * 60 * 1000;

662-

mockedGetSoonestCooldownExpiry.mockReturnValue(expiresIn30Min);

663-

mockedResolveProfilesUnavailableReason.mockReturnValue("billing");

664-665-

const run = vi.fn().mockResolvedValue("billing-recovered");

666-667-

const result = await runWithModelFallback({

668-

cfg,

669-

provider: "openai",

670-

model: "gpt-4.1-mini",

671-

fallbacksOverride: [],

672-

run,

673-

});

674-675-

expect(result.result).toBe("billing-recovered");

676-

expect(run).toHaveBeenCalledTimes(1);

677-

expect(run).toHaveBeenCalledWith("openai", "gpt-4.1-mini", {

678-

allowTransientCooldownProbe: true,

679-

});

680-

});

681-682-

it("probes billing-cooldowned primary with fallbacks when near cooldown expiry", async () => {

683-

const cfg = makeCfg();

684-

// Cooldown expires in 1 minute — within 2-min probe margin

685-

const expiresIn1Min = NOW + 60 * 1000;

686-

mockedGetSoonestCooldownExpiry.mockReturnValue(expiresIn1Min);

687-

mockedResolveProfilesUnavailableReason.mockReturnValue("billing");

688-689-

const run = vi.fn().mockResolvedValue("billing-probe-ok");

690-691-

const result = await runPrimaryCandidate(cfg, run);

692-693-

expectPrimaryProbeSuccess(result, run, "billing-probe-ok");

647+

expect(

648+

resolveOpenAiCooldownDecision({

649+

reason: "billing",

650+

soonest: NOW + 30 * 60 * 1000,

651+

hasFallbackCandidates: false,

652+

}),

653+

).toEqual({ type: "attempt", reason: "billing", markProbe: true });

654+

expect(

655+

resolveOpenAiCooldownDecision({

656+

reason: "billing",

657+

soonest: NOW + 60 * 1000,

658+

}),

659+

).toEqual({ type: "attempt", reason: "billing", markProbe: true });

660+

expect(

661+

resolveOpenAiCooldownDecision({

662+

reason: "billing",

663+

soonest: NOW + 30 * 60 * 1000,

664+

}),

665+

).toMatchObject({ type: "skip", reason: "billing" });

694666

});

695667

});