File size: 16,938 Bytes
76289e7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 | /** Tests model fallback notice formatting and transition state tracking. */
import { afterEach, describe, expect, it } from "vitest";
import { testing as cliBackendsTesting } from "../agents/cli-backends.test-support.js";
import { canonicalizeProviderModelId } from "../agents/provider-model-route.js";
import {
resolveActiveFallbackState,
type FallbackNoticeState,
} from "../status/fallback-notice-state.js";
import {
buildFallbackClearedNotice,
buildFallbackNotice,
resolveFallbackTransition,
} from "./fallback-state.js";
const baseAttempt = {
provider: "demo-primary",
model: "demo-primary/model-a",
error: "Provider demo-primary is in cooldown (all profiles unavailable)",
reason: "rate_limit" as const,
};
const activeFallbackState: FallbackNoticeState = {
fallbackNotice: {
kind: "active",
selectedModel: "demo-primary/model-a",
activeModel: "demo-fallback/model-b",
reason: "rate limit",
},
};
function registerAnthropicCliBackendForTest(): void {
cliBackendsTesting.setDepsForTest({
resolveRuntimeCliBackends: () => [
{
id: "claude-cli",
modelProvider: "anthropic",
pluginId: "anthropic",
config: { command: "claude" },
bundleMcp: false,
},
],
});
}
function resolveDemoFallbackTransition(
overrides: Partial<Parameters<typeof resolveFallbackTransition>[0]> = {},
) {
return resolveFallbackTransition({
selectedProvider: "demo-primary",
selectedModel: "model-a",
activeProvider: "demo-fallback",
activeModel: "model-b",
attempts: [baseAttempt],
state: {},
...overrides,
});
}
describe("fallback-state", () => {
afterEach(() => {
cliBackendsTesting.resetDepsForTest();
});
it.each([
{
name: "treats fallback as active only when state matches selected and active refs",
state: activeFallbackState,
expected: { active: true, reason: "rate limit" },
expectedSetupLookups: 2,
},
{
name: "does not discover runtime aliases without persisted fallback state",
state: undefined,
expected: { active: false, reason: undefined },
expectedSetupLookups: 0,
},
{
name: "does not treat runtime drift as fallback when persisted state does not match",
state: {
fallbackNotice: {
kind: "active",
selectedModel: "other-provider/other-model",
activeModel: "demo-fallback/model-b",
reason: "rate limit",
},
} satisfies FallbackNoticeState,
expected: { active: false, reason: undefined },
expectedSetupLookups: 0,
},
{
name: "does not discover runtime aliases when the recorded active ref differs",
state: {
fallbackNotice: {
kind: "active",
selectedModel: "demo-primary/model-a",
activeModel: "other-provider/other-model",
reason: "rate limit",
},
} satisfies FallbackNoticeState,
expected: { active: false, reason: undefined },
expectedSetupLookups: 0,
},
{
name: "does not report a matching persisted CLI runtime alias as fallback",
selectedModelRef: "anthropic/claude-opus-4-7",
activeModelRef: "claude-cli/claude-opus-4-7",
state: {
fallbackNotice: {
kind: "active",
selectedModel: "anthropic/claude-opus-4-7",
activeModel: "claude-cli/claude-opus-4-7",
reason: "selected model unavailable",
},
} satisfies FallbackNoticeState,
expected: { active: false, reason: undefined },
expectedSetupLookups: 2,
},
])("$name", ({ state, expected, expectedSetupLookups, selectedModelRef, activeModelRef }) => {
let setupLookups = 0;
cliBackendsTesting.setDepsForTest({
resolveRuntimeCliBackends: () => [],
resolvePluginSetupCliBackend: ({ backend }) => {
setupLookups += 1;
return backend === "claude-cli"
? {
pluginId: "anthropic",
backend: {
id: "claude-cli",
modelProvider: "anthropic",
config: { command: "claude" },
bundleMcp: false,
},
}
: undefined;
},
});
const resolved = resolveActiveFallbackState({
selectedModelRef: selectedModelRef ?? "demo-primary/model-a",
activeModelRef: activeModelRef ?? "demo-fallback/model-b",
config: {},
state,
});
expect(resolved).toEqual(expected);
expect(setupLookups).toBe(expectedSetupLookups);
});
it("marks fallback transition when selected->active pair changes", () => {
const resolved = resolveDemoFallbackTransition();
expect(resolved.fallbackActive).toBe(true);
expect(resolved.fallbackTransitioned).toBe(true);
expect(resolved.fallbackCleared).toBe(false);
expect(resolved.stateChanged).toBe(true);
expect(resolved.reasonSummary).toBe("rate limit");
expect(resolved.nextState.selectedModel).toBe("demo-primary/model-a");
expect(resolved.nextState.activeModel).toBe("demo-fallback/model-b");
});
it("preserves provider-local model prefixes through fallback and recovery", () => {
const refs = {
selectedProvider: "custom",
selectedModel: "custom/model",
activeProvider: "custom",
activeModel: "model",
attempts: [{ ...baseAttempt, provider: "custom", model: "custom/model" }],
};
const activated = resolveDemoFallbackTransition(refs);
expect(activated).toMatchObject({
fallbackActive: true,
fallbackTransitioned: true,
nextState: { selectedModel: "custom/custom/model", activeModel: "custom/model" },
attemptSummaries: ["custom/custom/model rate limit"],
});
const state: FallbackNoticeState = {
fallbackNotice: {
kind: "active",
selectedModel: activated.selectedModelRef,
activeModel: activated.activeModelRef,
reason: activated.reasonSummary,
},
};
expect(resolveDemoFallbackTransition({ ...refs, state })).toMatchObject({
fallbackTransitioned: false,
stateChanged: false,
});
expect(
resolveDemoFallbackTransition({ ...refs, activeModel: refs.selectedModel, state }),
).toMatchObject({
fallbackCleared: true,
nextState: { selectedModel: undefined, activeModel: undefined, reason: undefined },
});
expect(buildFallbackNotice(refs)).toContain("selected custom/custom/model");
expect(
buildFallbackClearedNotice({ ...refs, previousActiveModel: activated.activeModelRef }),
).toBe("↪️ Model Fallback cleared: custom/custom/model (was custom/model)");
});
it("prefers formatted transient error details over generic rate-limit labels", () => {
const resolved = resolveDemoFallbackTransition({
attempts: [
{
...baseAttempt,
error: "429 Too Many Requests: Claude Max usage limit reached, try again in 6 minutes.",
},
],
});
expect(resolved.reasonSummary).toContain("HTTP 429: Too Many Requests");
expect(resolved.reasonSummary).toContain("Claude Max usage limit reached");
});
it.each([
// 真实 AWS Bedrock fixture,provenance 可追溯:
// src/agents/failover-error.test.ts:54(引用 AWS troubleshooting 文档)
// src/agents/failover-error.test.ts:688 / provider-error-patterns.test.ts:153
"ThrottlingException: Your request was denied due to exceeding the account quotas for Amazon Bedrock.",
"ThrottlingException: Too many concurrent requests",
])(
"preserves throttle-flavored transient details over the generic rate-limit label (%j)",
(error) => {
const resolved = resolveDemoFallbackTransition({
attempts: [{ ...baseAttempt, error }],
});
// 回归: TRANSIENT_ERROR_DETAIL_HINT_RE 必须命中 throttle 词族
// (throttle/throttling/throttled/ThrottlingException)。原先裸 `throttl\b`
// 仅匹配不存在的词干 "throttl",真实 Bedrock 消息全部失配,详细预览被
// 塌缩成通用 "rate limit" 标签。修复后预览得以保留。
expect(resolved.reasonSummary).toContain("ThrottlingException");
expect(resolved.reasonSummary).not.toBe("rate limit");
},
);
it("still collapses to the reason label when a transient reason lacks any transient-detail hint", () => {
// 防止过度匹配: 修复不得让门控对无 transient 提示的文本也放行。
const resolved = resolveDemoFallbackTransition({
attempts: [{ ...baseAttempt, error: "Unauthorized: invalid API key" }],
});
expect(resolved.reasonSummary).toBe("rate limit");
});
it("keeps truncated transient error details UTF-16 safe", () => {
const detail = "x".repeat(68);
const resolved = resolveDemoFallbackTransition({
attempts: [{ ...baseAttempt, error: `429 ${detail}😀tail` }],
});
expect(resolved.reasonSummary).toBe(`HTTP 429: ${detail}…`);
});
it("refreshes reason when fallback remains active with same model pair", () => {
const resolved = resolveDemoFallbackTransition({
attempts: [{ ...baseAttempt, reason: "timeout" }],
state: activeFallbackState,
});
expect(resolved.fallbackTransitioned).toBe(false);
expect(resolved.stateChanged).toBe(true);
expect(resolved.nextState.reason).toBe("timeout");
});
it("marks fallback as cleared when runtime returns to selected model", () => {
const resolved = resolveDemoFallbackTransition({
activeProvider: "demo-primary",
selectedModel: "model-a",
activeModel: "model-a",
attempts: [],
state: activeFallbackState,
});
expect(resolved.fallbackActive).toBe(false);
expect(resolved.fallbackCleared).toBe(true);
expect(resolved.fallbackTransitioned).toBe(false);
expect(resolved.stateChanged).toBe(true);
expect(resolved.nextState.selectedModel).toBeUndefined();
expect(resolved.nextState.activeModel).toBeUndefined();
expect(resolved.nextState.reason).toBeUndefined();
});
it("does not treat a CLI runtime alias as a model fallback", () => {
registerAnthropicCliBackendForTest();
const resolved = resolveFallbackTransition({
selectedProvider: "anthropic",
selectedModel: "claude-opus-4-7",
activeProvider: "claude-cli",
activeModel: "claude-opus-4-7",
attempts: [],
state: {
fallbackNotice: {
kind: "active",
selectedModel: "anthropic/claude-opus-4-7",
activeModel: "claude-cli/claude-opus-4-7",
reason: "selected model unavailable",
},
},
cfg: {},
});
expect(resolved.fallbackActive).toBe(false);
expect(resolved.fallbackCleared).toBe(false);
expect(resolved.stateChanged).toBe(true);
expect(resolved.nextState.selectedModel).toBeUndefined();
expect(resolved.nextState.activeModel).toBeUndefined();
});
it("does not repeat runtime alias comparison when persisted fallback refs match", () => {
let setupBackendLookups = 0;
cliBackendsTesting.setDepsForTest({
resolvePluginSetupCliBackend: ({ backend }) => {
setupBackendLookups += 1;
return backend === "claude-cli"
? {
pluginId: "anthropic",
backend: {
id: "claude-cli",
modelProvider: "anthropic",
config: { command: "claude" },
bundleMcp: false,
},
}
: undefined;
},
resolvePluginSetupRegistry: () => {
throw new Error("full setup registry should not load for a single runtime alias");
},
resolveRuntimeCliBackends: () => [],
});
const resolved = resolveFallbackTransition({
selectedProvider: "anthropic",
selectedModel: "claude-opus-4-7",
activeProvider: "claude-cli",
activeModel: "claude-opus-4-7",
attempts: [],
state: {
fallbackNotice: {
kind: "active",
selectedModel: "anthropic/claude-opus-4-7",
activeModel: "claude-cli/claude-opus-4-7",
reason: "selected model unavailable",
},
},
cfg: {},
});
expect(resolved.fallbackActive).toBe(false);
expect(setupBackendLookups).toBe(2);
});
it("does not build a fallback notice for equivalent CLI runtime aliases", () => {
registerAnthropicCliBackendForTest();
expect(
buildFallbackNotice({
selectedProvider: "anthropic",
selectedModel: "claude-opus-4-7",
activeProvider: "claude-cli",
activeModel: "claude-opus-4-7",
attempts: [],
}),
).toBeNull();
});
it.each(["gpt-5.5", "gpt-5.4", "gpt-5.4-mini", "o3"])(
"does not build a fallback notice for the OpenAI Codex runtime provider alias with %s",
(model) => {
expect(
buildFallbackNotice({
selectedProvider: "openai",
selectedModel: model,
activeProvider: "openai",
activeModel: model,
attempts: [],
}),
).toBeNull();
},
);
it("still reports fallback when the OpenAI Codex runtime switches model ids", () => {
expect(
buildFallbackNotice({
selectedProvider: "openai",
selectedModel: "gpt-5.5",
activeProvider: "openai",
activeModel: "gpt-5.4",
attempts: [],
}),
).toContain("selected openai/gpt-5.5");
});
describe("Arcee wire identity", () => {
it.each([
{
name: "fresh state",
state: {} satisfies FallbackNoticeState,
expectedStateChanged: false,
},
{
name: "captured alias-only state",
state: {
fallbackNotice: {
kind: "active",
selectedModel: "arcee/trinity-large-preview",
activeModel: "arcee/arcee-ai/trinity-large-preview",
reason: "selected model unavailable",
},
} satisfies FallbackNoticeState,
expectedStateChanged: true,
},
])("keeps $name out of fallback state", ({ state, expectedStateChanged }) => {
const params = {
selectedProvider: "arcee",
selectedModel: "trinity-large-preview",
activeProvider: "arcee",
activeModel: "arcee-ai/trinity-large-preview",
attempts: [],
cfg: {},
state,
};
expect(canonicalizeProviderModelId("arcee", "arcee-ai/trinity-large-preview")).toBe(
"trinity-large-preview",
);
const resolved = resolveFallbackTransition(params);
expect(resolved).toMatchObject({
fallbackActive: false,
fallbackTransitioned: false,
fallbackCleared: false,
stateChanged: expectedStateChanged,
});
expect(resolved.nextState).toEqual({
selectedModel: undefined,
activeModel: undefined,
reason: undefined,
});
expect(buildFallbackNotice(params)).toBeNull();
expect(
resolveActiveFallbackState({
selectedModelRef: "arcee/trinity-large-preview",
activeModelRef: "arcee/arcee-ai/trinity-large-preview",
config: {},
state,
}),
).toEqual({ active: false, reason: undefined });
});
it.each([
{
name: "different model",
activeProvider: "arcee",
activeModel: "arcee-ai/trinity-large-thinking",
activeRef: "arcee/arcee-ai/trinity-large-thinking",
},
{
name: "different provider",
activeProvider: "openrouter",
activeModel: "arcee-ai/trinity-large-preview",
activeRef: "openrouter/arcee-ai/trinity-large-preview",
},
])("keeps a $name as a real fallback", ({ activeProvider, activeModel, activeRef }) => {
const params = {
selectedProvider: "arcee",
selectedModel: "trinity-large-preview",
activeProvider,
activeModel,
attempts: [],
cfg: {},
state: {},
};
const resolved = resolveFallbackTransition(params);
expect(resolved).toMatchObject({
fallbackActive: true,
fallbackTransitioned: true,
fallbackCleared: false,
stateChanged: true,
nextState: {
selectedModel: "arcee/trinity-large-preview",
activeModel: activeRef,
},
});
expect(buildFallbackNotice(params)).toContain(activeRef);
expect(
resolveActiveFallbackState({
selectedModelRef: "arcee/trinity-large-preview",
activeModelRef: activeRef,
config: {},
state: {
fallbackNotice: {
kind: "active",
selectedModel: "arcee/trinity-large-preview",
activeModel: activeRef,
reason: "selected model unavailable",
},
},
}),
).toEqual({ active: true, reason: "selected model unavailable" });
});
});
});
|