Skip to content

Commit e1afbe8

Browse files
ZOOWHsteipete
andcommitted
fix(active-memory): bound timeout circuit breakers
Co-authored-by: Peter Steinberger <[email protected]>
1 parent 9719616 commit e1afbe8

3 files changed

Lines changed: 105 additions & 7 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
});

‎extensions/active-memory/recall-state.ts‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,25 @@ function isCircuitBreakerOpen(key: string, maxTimeouts: number, cooldownMs: numb
4343
return true;
4444
}
4545

46-
function recordCircuitBreakerTimeout(key: string): void {
46+
function recordCircuitBreakerTimeout(key: string, cooldownMs: number): void {
47+
const now = Date.now();
48+
for (const [entryKey, entry] of timeoutCircuitBreaker) {
49+
if (now - entry.lastTimeoutAt >= cooldownMs) {
50+
timeoutCircuitBreaker.delete(entryKey);
51+
}
52+
}
4753
const entry = timeoutCircuitBreaker.get(key);
48-
if (entry) {
49-
entry.consecutiveTimeouts++;
50-
entry.lastTimeoutAt = Date.now();
51-
} else {
52-
timeoutCircuitBreaker.set(key, { consecutiveTimeouts: 1, lastTimeoutAt: Date.now() });
54+
// Reinsertion keeps refreshed keys newer than peers when capacity eviction runs.
55+
timeoutCircuitBreaker.delete(key);
56+
timeoutCircuitBreaker.set(key, {
57+
consecutiveTimeouts: (entry?.consecutiveTimeouts ?? 0) + 1,
58+
lastTimeoutAt: now,
59+
});
60+
if (timeoutCircuitBreaker.size > DEFAULT_MAX_CACHE_ENTRIES) {
61+
const oldestKey = timeoutCircuitBreaker.keys().next().value;
62+
if (oldestKey !== undefined) {
63+
timeoutCircuitBreaker.delete(oldestKey);
64+
}
5365
}
5466
}
5567

‎extensions/active-memory/recall.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ async function resolveActiveRecall(
194194
const recordRecallTimeout = () => {
195195
if (!circuitBreakerTimeoutRecorded) {
196196
circuitBreakerTimeoutRecorded = true;
197-
recordCircuitBreakerTimeout(cbKey);
197+
recordCircuitBreakerTimeout(cbKey, params.config.circuitBreakerCooldownMs);
198198
}
199199
scheduleTimeoutCleanup();
200200
};

0 commit comments

Comments
 (0)