|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { |
| 3 | + buildCircuitBreakerKey, |
| 4 | + getCircuitBreakerEntry, |
| 5 | + isCircuitBreakerOpen, |
| 6 | + recordCircuitBreakerTimeout, |
| 7 | + resetActiveRecallStateForTests, |
| 8 | +} from "./recall-state.js"; |
| 9 | +import { DEFAULT_MAX_CACHE_ENTRIES } from "./types.js"; |
| 10 | + |
| 11 | +describe("active-memory timeout circuit breakers", () => { |
| 12 | + beforeEach(() => { |
| 13 | + resetActiveRecallStateForTests(); |
| 14 | + vi.useFakeTimers(); |
| 15 | + vi.setSystemTime(new Date("2026-01-01T00:00:00.000Z")); |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + resetActiveRecallStateForTests(); |
| 20 | + vi.useRealTimers(); |
| 21 | + }); |
| 22 | + |
| 23 | + it.each([5_000, 120_000])("expires other entries after the %d ms cooldown", (cooldownMs) => { |
| 24 | + recordCircuitBreakerTimeout("old", cooldownMs); |
| 25 | + vi.advanceTimersByTime(cooldownMs - 1); |
| 26 | + recordCircuitBreakerTimeout("recent", cooldownMs); |
| 27 | + vi.advanceTimersByTime(1); |
| 28 | + recordCircuitBreakerTimeout("new", cooldownMs); |
| 29 | + |
| 30 | + expect(getCircuitBreakerEntry("old")).toBeUndefined(); |
| 31 | + expect(getCircuitBreakerEntry("recent")?.consecutiveTimeouts).toBe(1); |
| 32 | + expect(getCircuitBreakerEntry("new")?.consecutiveTimeouts).toBe(1); |
| 33 | + }); |
| 34 | + |
| 35 | + it("restarts an expired key before adding a new timeout", () => { |
| 36 | + const cooldownMs = 5_000; |
| 37 | + recordCircuitBreakerTimeout("same", cooldownMs); |
| 38 | + vi.advanceTimersByTime(cooldownMs); |
| 39 | + recordCircuitBreakerTimeout("same", cooldownMs); |
| 40 | + |
| 41 | + expect(getCircuitBreakerEntry("same")?.consecutiveTimeouts).toBe(1); |
| 42 | + expect(isCircuitBreakerOpen("same", 2, cooldownMs)).toBe(false); |
| 43 | + }); |
| 44 | + |
| 45 | + it("evicts the oldest key when the existing cache capacity is exceeded", () => { |
| 46 | + const cooldownMs = 120_000; |
| 47 | + for (let index = 0; index <= DEFAULT_MAX_CACHE_ENTRIES; index++) { |
| 48 | + recordCircuitBreakerTimeout(`model-${index}`, cooldownMs); |
| 49 | + } |
| 50 | + |
| 51 | + expect(getCircuitBreakerEntry("model-0")).toBeUndefined(); |
| 52 | + expect(getCircuitBreakerEntry("model-1")?.consecutiveTimeouts).toBe(1); |
| 53 | + expect(getCircuitBreakerEntry(`model-${DEFAULT_MAX_CACHE_ENTRIES}`)?.consecutiveTimeouts).toBe( |
| 54 | + 1, |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it("refreshes an existing key before evicting the least recently updated peer", () => { |
| 59 | + const cooldownMs = 120_000; |
| 60 | + for (let index = 0; index < DEFAULT_MAX_CACHE_ENTRIES; index++) { |
| 61 | + recordCircuitBreakerTimeout(`model-${index}`, cooldownMs); |
| 62 | + } |
| 63 | + vi.advanceTimersByTime(1); |
| 64 | + recordCircuitBreakerTimeout("model-0", cooldownMs); |
| 65 | + recordCircuitBreakerTimeout("newest", cooldownMs); |
| 66 | + |
| 67 | + expect(getCircuitBreakerEntry("model-0")?.consecutiveTimeouts).toBe(2); |
| 68 | + expect(getCircuitBreakerEntry("model-1")).toBeUndefined(); |
| 69 | + expect(getCircuitBreakerEntry("newest")?.consecutiveTimeouts).toBe(1); |
| 70 | + }); |
| 71 | + |
| 72 | + it("keeps circuit-breaker decisions scoped to the agent, provider, and model", () => { |
| 73 | + const cooldownMs = 5_000; |
| 74 | + const timedOutKey = buildCircuitBreakerKey("agent-a", "provider-a", "model-a"); |
| 75 | + const otherProviderKey = buildCircuitBreakerKey("agent-a", "provider-b", "model-a"); |
| 76 | + const otherModelKey = buildCircuitBreakerKey("agent-a", "provider-a", "model-b"); |
| 77 | + const otherAgentKey = buildCircuitBreakerKey("agent-b", "provider-a", "model-a"); |
| 78 | + |
| 79 | + recordCircuitBreakerTimeout(timedOutKey, cooldownMs); |
| 80 | + |
| 81 | + expect(isCircuitBreakerOpen(timedOutKey, 1, cooldownMs)).toBe(true); |
| 82 | + expect(isCircuitBreakerOpen(otherProviderKey, 1, cooldownMs)).toBe(false); |
| 83 | + expect(isCircuitBreakerOpen(otherModelKey, 1, cooldownMs)).toBe(false); |
| 84 | + expect(isCircuitBreakerOpen(otherAgentKey, 1, cooldownMs)).toBe(false); |
| 85 | + }); |
| 86 | +}); |
0 commit comments