Skip to content

#1 Persistent memory for AI coding agents based on real-world benchmarks

refactor: simplify resilient provider, telemetry setup, and eviction

Rohit Ghumare30d795681c8a
diff --git a/src/functions/evict.ts b/src/functions/evict.ts
index cfe7846b..2744a247 100644
--- a/src/functions/evict.ts
+++ b/src/functions/evict.ts
@@ -1,6 +1,10 @@
import type { ISdk } from "iii-sdk";
import { getContext } from "iii-sdk";
-import type { Session, CompressedObservation, SessionSummary } from "../types.js";
+import type {
+ Session,
+ CompressedObservation,
+ SessionSummary,
+} from "../types.js";
import { KV } from "../state/schema.js";
import { StateKV } from "../state/kv.js";
@@ -11,6 +15,8 @@ interface EvictionConfig {
maxObservationsPerProject: number;
}
+const MS_PER_DAY = 24 * 60 * 60 * 1000;
+
const DEFAULTS: EvictionConfig = {
staleSessionDays: 30,
lowImportanceMaxDays: 90,
@@ -27,25 +33,37 @@ interface EvictionStats {
export function registerEvictFunction(sdk: ISdk, kv: StateKV): void {
sdk.registerFunction(
- { id: "mem::evict", description: "Evict stale memories based on age and importance" },
+ {
+ id: "mem::evict",
+ description: "Evict stale memories based on age and importance",
+ },
async (data: { dryRun?: boolean }): Promise<EvictionStats> => {
const ctx = getContext();
const dryRun = data?.dryRun ?? false;
- const configOverride = await kv.get<Partial<EvictionConfig>>(KV.config, "eviction").catch(() => null);
+ const configOverride = await kv
+ .get<Partial<EvictionConfig>>(KV.config, "eviction")
+ .catch(() => null);
const cfg = { ...DEFAULTS, ...configOverride };
const now = Date.now();
- const stats: EvictionStats = { staleSessions: 0, lowImportanceObs: 0, capEvictions: 0, dryRun };
+ const stats: EvictionStats = {
+ staleSessions: 0,
+ lowImportanceObs: 0,
+ capEvictions: 0,
+ dryRun,
+ };
const sessions = await kv.list<Session>(KV.sessions).catch(() => []);
- const summaries = await kv.list<SessionSummary>(KV.summaries).catch(() => []);
+ const summaries = await kv
+ .list<SessionSummary>(KV.summaries)
+ .catch(() => []);
const summaryIds = new Set(summaries.map((s) => s.sessionId));
for (const session of sessions) {
if (!session.startedAt) continue;
const age = now - new Date(session.startedAt).getTime();
- const staleDays = cfg.staleSessionDays * 24 * 60 * 60 * 1000;
+ const staleDays = cfg.staleSessionDays * MS_PER_DAY;
if (age > staleDays && !summaryIds.has(session.id)) {
stats.staleSessions++;
if (!dryRun) {
@@ -56,17 +74,24 @@ export function registerEvictFunction(sdk: ISdk, kv: StateKV): void {
const projectObs = new Map<string, CompressedObservation[]>();
for (const session of sessions) {
- const obs = await kv.list<CompressedObservation>(KV.observations(session.id)).catch(() => []);
+ const obs = await kv
+ .list<CompressedObservation>(KV.observations(session.id))
+ .catch(() => []);
const compressed = obs.filter((o) => o.title);
for (const o of compressed) {
if (!o.timestamp) continue;
const age = now - new Date(o.timestamp).getTime();
- const maxAge = cfg.lowImportanceMaxDays * 24 * 60 * 60 * 1000;
- if (age > maxAge && (o.importance ?? 5) < cfg.lowImportanceThreshold) {
+ const maxAge = cfg.lowImportanceMaxDays * MS_PER_DAY;
+ if (
+ age > maxAge &&
+ (o.importance ?? 5) < cfg.lowImportanceThreshold
+ ) {
stats.lowImportanceObs++;
if (!dryRun) {
- await kv.delete(KV.observations(session.id), o.id).catch(() => {});
+ await kv
+ .delete(KV.observations(session.id), o.id)
+ .catch(() => {});
}
}
}
@@ -79,12 +104,19 @@ export function registerEvictFunction(sdk: ISdk, kv: StateKV): void {
for (const [, obs] of projectObs) {
if (obs.length > cfg.maxObservationsPerProject) {
- const sorted = obs.sort((a, b) => (a.importance ?? 5) - (b.importance ?? 5));
- const toEvict = sorted.slice(0, obs.length - cfg.maxObservationsPerProject);
+ const sorted = obs.sort(
+ (a, b) => (a.importance ?? 5) - (b.importance ?? 5),
+ );
+ const toEvict = sorted.slice(
+ 0,
+ obs.length - cfg.maxObservationsPerProject,
+ );
stats.capEvictions += toEvict.length;
if (!dryRun) {
for (const o of toEvict) {
- await kv.delete(KV.observations(o.sessionId), o.id).catch(() => {});
+ await kv
+ .delete(KV.observations(o.sessionId), o.id)
+ .catch(() => {});
}
}
}
diff --git a/src/providers/resilient.ts b/src/providers/resilient.ts
index d7e2a02a..95ece40c 100644
--- a/src/providers/resilient.ts
+++ b/src/providers/resilient.ts
@@ -9,12 +9,12 @@ export class ResilientProvider implements MemoryProvider {
this.name = `resilient(${inner.name})`;
}
- async compress(systemPrompt: string, userPrompt: string): Promise<string> {
+ private async call(fn: () => Promise<string>): Promise<string> {
if (!this.breaker.isAllowed) {
throw new Error("circuit_breaker_open");
}
try {
- const result = await this.inner.compress(systemPrompt, userPrompt);
+ const result = await fn();
this.breaker.recordSuccess();
return result;
} catch (err) {
@@ -23,18 +23,12 @@ export class ResilientProvider implements MemoryProvider {
}
}
+ async compress(systemPrompt: string, userPrompt: string): Promise<string> {
+ return this.call(() => this.inner.compress(systemPrompt, userPrompt));
+ }
+
async summarize(systemPrompt: string, userPrompt: string): Promise<string> {
- if (!this.breaker.isAllowed) {
- throw new Error("circuit_breaker_open");
- }
- try {
- const result = await this.inner.summarize(systemPrompt, userPrompt);
- this.breaker.recordSuccess();
- return result;
- } catch (err) {
- this.breaker.recordFailure();
- throw err;
- }
+ return this.call(() => this.inner.summarize(systemPrompt, userPrompt));
}
get circuitState(): CircuitBreakerState {
diff --git a/src/telemetry/setup.ts b/src/telemetry/setup.ts
index bc442dd2..c6ddc7fe 100644
--- a/src/telemetry/setup.ts
+++ b/src/telemetry/setup.ts
@@ -10,87 +10,86 @@ export const OTEL_CONFIG: OtelConfig = {
metricsExportIntervalMs: 30_000,
};
+interface Counter {
+ add: (n: number) => void;
+}
+interface Histogram {
+ record: (v: number) => void;
+}
+
interface Counters {
- observationsTotal: { add: (n: number) => void };
- compressionSuccess: { add: (n: number) => void };
- compressionFailure: { add: (n: number) => void };
- searchTotal: { add: (n: number) => void };
- dedupSkipped: { add: (n: number) => void };
- evictionTotal: { add: (n: number) => void };
- circuitBreakerOpen: { add: (n: number) => void };
+ observationsTotal: Counter;
+ compressionSuccess: Counter;
+ compressionFailure: Counter;
+ searchTotal: Counter;
+ dedupSkipped: Counter;
+ evictionTotal: Counter;
+ circuitBreakerOpen: Counter;
}
interface Histograms {
- compressionLatency: { record: (v: number) => void };
- searchLatency: { record: (v: number) => void };
- contextTokens: { record: (v: number) => void };
- qualityScore: { record: (v: number) => void };
+ compressionLatency: Histogram;
+ searchLatency: Histogram;
+ contextTokens: Histogram;
+ qualityScore: Histogram;
}
+type Meter = {
+ createCounter: (name: string) => Counter;
+ createHistogram: (name: string) => Histogram;
+};
+
let counters: Counters | null = null;
let histograms: Histograms | null = null;
-function noopCounter() {
- return { add: () => {} };
-}
+const NOOP_COUNTER: Counter = { add: () => {} };
+const NOOP_HISTOGRAM: Histogram = { record: () => {} };
-function noopHistogram() {
- return { record: () => {} };
-}
+const COUNTER_NAMES: Array<[keyof Counters, string]> = [
+ ["observationsTotal", "observations.total"],
+ ["compressionSuccess", "compression.success"],
+ ["compressionFailure", "compression.failure"],
+ ["searchTotal", "search.total"],
+ ["dedupSkipped", "dedup.skipped"],
+ ["evictionTotal", "eviction.total"],
+ ["circuitBreakerOpen", "circuit_breaker.open"],
+];
+
+const HISTOGRAM_NAMES: Array<[keyof Histograms, string]> = [
+ ["compressionLatency", "compression.latency_ms"],
+ ["searchLatency", "search.latency_ms"],
+ ["contextTokens", "context.tokens"],
+ ["qualityScore", "quality.score"],
+];
+
+export function initMetrics(
+ getMeter?: (name: string) => Meter,
+): { counters: Counters; histograms: Histograms } {
+ const meter = getMeter?.("agentmemory");
+
+ counters = Object.fromEntries(
+ COUNTER_NAMES.map(([key, name]) => [
+ key,
+ meter ? meter.createCounter(name) : NOOP_COUNTER,
+ ]),
+ ) as Counters;
+
+ histograms = Object.fromEntries(
+ HISTOGRAM_NAMES.map(([key, name]) => [
+ key,
+ meter ? meter.createHistogram(name) : NOOP_HISTOGRAM,
+ ]),
+ ) as Histograms;
-export function initMetrics(getMeter?: (name: string) => {
- createCounter: (name: string) => { add: (n: number) => void };
- createHistogram: (name: string) => { record: (v: number) => void };
-}): { counters: Counters; histograms: Histograms } {
- if (getMeter) {
- const meter = getMeter("agentmemory");
- counters = {
- observationsTotal: meter.createCounter("observations.total"),
- compressionSuccess: meter.createCounter("compression.success"),
- compressionFailure: meter.createCounter("compression.failure"),
- searchTotal: meter.createCounter("search.total"),
- dedupSkipped: meter.createCounter("dedup.skipped"),
- evictionTotal: meter.createCounter("eviction.total"),
- circuitBreakerOpen: meter.createCounter("circuit_breaker.open"),
- };
- histograms = {
- compressionLatency: meter.createHistogram("compression.latency_ms"),
- searchLatency: meter.createHistogram("search.latency_ms"),
- contextTokens: meter.createHistogram("context.tokens"),
- qualityScore: meter.createHistogram("quality.score"),
- };
- } else {
- counters = {
- observationsTotal: noopCounter(),
- compressionSuccess: noopCounter(),
- compressionFailure: noopCounter(),
- searchTotal: noopCounter(),
- dedupSkipped: noopCounter(),
- evictionTotal: noopCounter(),
- circuitBreakerOpen: noopCounter(),
- };
- histograms = {
- compressionLatency: noopHistogram(),
- searchLatency: noopHistogram(),
- contextTokens: noopHistogram(),
- qualityScore: noopHistogram(),
- };
- }
return { counters, histograms };
}
export function getCounters(): Counters {
- if (!counters) {
- const { counters: c } = initMetrics();
- return c;
- }
- return counters;
+ if (!counters) initMetrics();
+ return counters!;
}
export function getHistograms(): Histograms {
- if (!histograms) {
- const { histograms: h } = initMetrics();
- return h;
- }
- return histograms;
+ if (!histograms) initMetrics();
+ return histograms!;
}