mirror of
https://github.com/rohitg00/agentmemory.git
synced 2026-09-14 20:16:33 +08:00
57d6f087ad
Add production-grade self-evaluation, health monitoring, and resilience: - Zod input/output validation for all 11 functions with quality scoring (0-100) - Self-correcting LLM retry on validation failure - Per-function metrics tracking (success rate, latency, quality) in KV - Circuit breaker for LLM providers (closed→open→half-open state machine) - ResilientProvider wrapper with automatic circuit breaker integration - Periodic health monitoring (CPU, memory, event loop lag, connection state) - Health threshold evaluation (healthy/degraded/critical with alerts) - Local observation queue for engine disconnection recovery - SHA-256 content-hash deduplication with 5min TTL window - Memory lifecycle eviction (stale sessions, low-importance obs, per-project caps) - BM25 search scoring replacing basic TF (k1=1.2, b=0.75, IDF weighting) - OTEL telemetry setup with counters and histograms - Enhanced /health endpoint returning real metrics, function stats, circuit state - New /evict endpoint for memory lifecycle management - 82 tests passing (37 new: eval schemas, quality scoring, circuit breaker)
108 lines
3.0 KiB
TypeScript
108 lines
3.0 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
import { CircuitBreaker } from "../src/providers/circuit-breaker.js";
|
|
|
|
describe("CircuitBreaker", () => {
|
|
beforeEach(() => {
|
|
vi.useFakeTimers();
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it("starts in closed state", () => {
|
|
const cb = new CircuitBreaker();
|
|
expect(cb.getState().state).toBe("closed");
|
|
expect(cb.isAllowed).toBe(true);
|
|
});
|
|
|
|
it("stays closed after fewer than 3 failures", () => {
|
|
const cb = new CircuitBreaker();
|
|
cb.recordFailure();
|
|
cb.recordFailure();
|
|
expect(cb.getState().state).toBe("closed");
|
|
expect(cb.isAllowed).toBe(true);
|
|
});
|
|
|
|
it("opens after 3 failures within the window", () => {
|
|
const cb = new CircuitBreaker();
|
|
cb.recordFailure();
|
|
cb.recordFailure();
|
|
cb.recordFailure();
|
|
expect(cb.getState().state).toBe("open");
|
|
expect(cb.isAllowed).toBe(false);
|
|
});
|
|
|
|
it("resets failure count when failures are outside the window", () => {
|
|
const cb = new CircuitBreaker();
|
|
cb.recordFailure();
|
|
cb.recordFailure();
|
|
vi.advanceTimersByTime(61_000);
|
|
cb.recordFailure();
|
|
expect(cb.getState().state).toBe("closed");
|
|
expect(cb.getState().failures).toBe(1);
|
|
});
|
|
|
|
it("transitions to half-open after recovery timeout", () => {
|
|
const cb = new CircuitBreaker();
|
|
cb.recordFailure();
|
|
cb.recordFailure();
|
|
cb.recordFailure();
|
|
expect(cb.isAllowed).toBe(false);
|
|
vi.advanceTimersByTime(30_000);
|
|
expect(cb.isAllowed).toBe(true);
|
|
expect(cb.getState().state).toBe("half-open");
|
|
});
|
|
|
|
it("closes on success in half-open state", () => {
|
|
const cb = new CircuitBreaker();
|
|
cb.recordFailure();
|
|
cb.recordFailure();
|
|
cb.recordFailure();
|
|
vi.advanceTimersByTime(30_000);
|
|
cb.isAllowed;
|
|
cb.recordSuccess();
|
|
expect(cb.getState().state).toBe("closed");
|
|
expect(cb.getState().failures).toBe(0);
|
|
expect(cb.getState().lastFailureAt).toBeNull();
|
|
});
|
|
|
|
it("reopens on failure in half-open state", () => {
|
|
const cb = new CircuitBreaker();
|
|
cb.recordFailure();
|
|
cb.recordFailure();
|
|
cb.recordFailure();
|
|
vi.advanceTimersByTime(30_000);
|
|
cb.isAllowed;
|
|
cb.recordFailure();
|
|
expect(cb.getState().state).toBe("open");
|
|
});
|
|
|
|
it("records lastFailureAt timestamp", () => {
|
|
const cb = new CircuitBreaker();
|
|
vi.setSystemTime(new Date("2026-01-15T10:00:00Z"));
|
|
cb.recordFailure();
|
|
expect(cb.getState().lastFailureAt).toBe(
|
|
new Date("2026-01-15T10:00:00Z").getTime(),
|
|
);
|
|
});
|
|
|
|
it("records openedAt timestamp", () => {
|
|
const cb = new CircuitBreaker();
|
|
vi.setSystemTime(new Date("2026-01-15T10:00:00Z"));
|
|
cb.recordFailure();
|
|
cb.recordFailure();
|
|
cb.recordFailure();
|
|
expect(cb.getState().openedAt).toBe(
|
|
new Date("2026-01-15T10:00:00Z").getTime(),
|
|
);
|
|
});
|
|
|
|
it("success in closed state is a no-op", () => {
|
|
const cb = new CircuitBreaker();
|
|
cb.recordSuccess();
|
|
expect(cb.getState().state).toBe("closed");
|
|
expect(cb.getState().failures).toBe(0);
|
|
});
|
|
});
|