#1 Persistent memory for AI coding agents based on real-world benchmarks
TypeScript agentmemoryagentsaiclaudeclaudecodecodexcopilotcursorgenaiharnesshermesmemoryompopenclawopencodepi
address CodeRabbit review on #188
Rohit Ghumare81be40707860
diff --git a/package.json b/package.jsonindex dedb13ed..8b49e9db 100644--- a/package.json+++ b/package.json@@ -62,7 +62,9 @@"zod": "^4.0.0"},"optionalDependencies": {- "@xenova/transformers": "^2.17.2"+ "@xenova/transformers": "^2.17.2",+ "onnxruntime-node": "^1.14.0",+ "onnxruntime-web": "^1.14.0"},"devDependencies": {"@types/node": "^22.0.0",diff --git a/src/functions/disk-size-manager.ts b/src/functions/disk-size-manager.tsindex a3329dba..16adaaa5 100644--- a/src/functions/disk-size-manager.ts+++ b/src/functions/disk-size-manager.ts@@ -4,8 +4,9 @@ import { StateKV } from "../state/kv.js";import { getMaxBytes } from "../utils/image-store.js";import { withKeyedLock } from "../state/keyed-mutex.js";import { logger } from "../logger.js";+import type { StateScope, StateScopeKey } from "../types.js";-const DISK_SIZE_KEY = "system:currentDiskSize";+const DISK_SIZE_KEY: StateScopeKey = "system:currentDiskSize";export function registerDiskSizeManager(sdk: ISdk, kv: StateKV): void {sdk.registerFunction(@@ -16,12 +17,13 @@ export function registerDiskSizeManager(sdk: ISdk, kv: StateKV): void {}return withKeyedLock(DISK_SIZE_KEY, async () => {- const currentTotal = (await kv.get<number>(KV.state, DISK_SIZE_KEY)) || 0;+ const currentTotal =+ (await kv.get<StateScope[typeof DISK_SIZE_KEY]>(KV.state, DISK_SIZE_KEY)) || 0;let newTotal = currentTotal + data.deltaBytes;if (newTotal < 0) newTotal = 0;- await kv.set(KV.state, DISK_SIZE_KEY, newTotal);+ await kv.set<StateScope[typeof DISK_SIZE_KEY]>(KV.state, DISK_SIZE_KEY, newTotal);if (data.deltaBytes > 0 && newTotal > getMaxBytes()) {sdk.triggerVoid("mem::image-quota-cleanup", {});diff --git a/src/functions/image-quota-cleanup.ts b/src/functions/image-quota-cleanup.tsindex 1a2e39a4..f2e5e491 100644--- a/src/functions/image-quota-cleanup.ts+++ b/src/functions/image-quota-cleanup.ts@@ -54,11 +54,19 @@ export function registerImageQuotaCleanup(sdk: ISdk, kv: StateKV): void {}await withKeyedLock(`imgRef:${f.filePath}`, async () => {- let refCount = 0;+ let refCount: number;try {refCount = await getImageRefCount(kv, f.filePath);} catch (err) {- logger.error("Failed to read refCount", { filePath: f.filePath, error: err instanceof Error ? err.message : String(err) });+ // Fail-closed: if we cannot determine refCount we must NOT+ // delete the image. Previously we let refCount fall through+ // to the default 0 and evicted, which risks deleting+ // still-referenced images on transient KV errors.+ logger.error("Failed to read refCount; skipping eviction", {+ filePath: f.filePath,+ error: err instanceof Error ? err.message : String(err),+ });+ return;}if (refCount > 0) {diff --git a/src/functions/observe.ts b/src/functions/observe.tsindex 4319e4be..951bf5f5 100644--- a/src/functions/observe.ts+++ b/src/functions/observe.ts@@ -203,12 +203,15 @@ export function registerObserveFunction(value: (session.observationCount || 0) + 1,},];- if (!session.firstPrompt && raw.userPrompt) {- updates.push({- type: "set",- path: "firstPrompt",- value: raw.userPrompt.replace(/\s+/g, " ").trim().slice(0, 200),- });+ if (!session.firstPrompt && typeof raw.userPrompt === "string") {+ const trimmed = raw.userPrompt.replace(/\s+/g, " ").trim();+ if (trimmed.length > 0) {+ updates.push({+ type: "set",+ path: "firstPrompt",+ value: trimmed.slice(0, 200),+ });+ }}await kv.update(KV.sessions, payload.sessionId, updates);}diff --git a/src/functions/replay.ts b/src/functions/replay.tsindex 08f083f3..2f0d556a 100644--- a/src/functions/replay.ts+++ b/src/functions/replay.ts@@ -10,7 +10,7 @@ import type {Session,} from "../types.js";import type { StateKV } from "../state/kv.js";-import { KV, generateId } from "../state/schema.js";+import { KV, generateId, fingerprintId } from "../state/schema.js";import { parseJsonlText } from "../replay/jsonl-parser.js";import { projectTimeline, type Timeline } from "../replay/timeline.js";import { safeAudit } from "./audit.js";@@ -110,28 +110,54 @@ async function deriveCrystalAndLessons(const lessonEntries = Array.from(lessonMatches.values()).slice(0, 20);const lessonIds: string[] = [];for (const content of lessonEntries) {- const lessonId = generateId("lesson");- const lesson: Lesson = {- id: lessonId,- content,- context: firstPrompt || project,- confidence: 0.4,- reinforcements: 0,- source: "consolidation",- sourceIds: [sessionId],- project,- tags: ["auto-import"],- createdAt,- updatedAt: createdAt,- decayRate: 0.05,- };+ // Content-addressed ID so re-importing the same JSONL does not+ // duplicate lessons. fingerprintId hashes the normalized content,+ // giving a stable lesson_xxx for identical text.+ const lessonId = fingerprintId("lesson", content.trim().toLowerCase());try {- await kv.set(KV.lessons, lessonId, lesson);+ const existing = await kv.get<Lesson>(KV.lessons, lessonId);+ if (existing) {+ const existingSources = existing.sourceIds || [];+ const mergedSources = existingSources.includes(sessionId)+ ? existingSources+ : [...existingSources, sessionId];+ const existingTags = existing.tags || [];+ const mergedTags = existingTags.includes("auto-import")+ ? existingTags+ : [...existingTags, "auto-import"];+ const merged: Lesson = {+ ...existing,+ sourceIds: mergedSources,+ tags: mergedTags,+ reinforcements: (existing.reinforcements || 0) + 1,+ updatedAt: createdAt,+ lastReinforcedAt: createdAt,+ };+ await kv.set(KV.lessons, lessonId, merged);+ } else {+ const lesson: Lesson = {+ id: lessonId,+ content,+ context: firstPrompt || project,+ confidence: 0.4,+ reinforcements: 0,+ source: "consolidation",+ sourceIds: [sessionId],+ project,+ tags: ["auto-import"],+ createdAt,+ updatedAt: createdAt,+ decayRate: 0.05,+ };+ await kv.set(KV.lessons, lessonId, lesson);+ }lessonIds.push(lessonId);} catch {}}- const crystalId = generateId("crystal");+ // Content-addressed on sessionId so re-importing the same session+ // upserts the crystal in place instead of creating a new one.+ const crystalId = fingerprintId("crystal", sessionId);const narrativePreview = firstPrompt? firstPrompt.slice(0, 300): compressed@@ -141,18 +167,19 @@ async function deriveCrystalAndLessons(.join(" · ").slice(0, 300);- const crystal: Crystal = {- id: crystalId,- narrative: narrativePreview || `Session ${sessionId.slice(0, 12)} (${rawObs.length} observations)`,- keyOutcomes: Array.from(tools).slice(0, 8),- filesAffected: Array.from(files).slice(0, 20),- lessons: lessonIds,- sourceActionIds: [],- sessionId,- project,- createdAt,- };try {+ const existingCrystal = await kv.get<Crystal>(KV.crystals, crystalId);+ const crystal: Crystal = {+ id: crystalId,+ narrative: narrativePreview || `Session ${sessionId.slice(0, 12)} (${rawObs.length} observations)`,+ keyOutcomes: Array.from(tools).slice(0, 8),+ filesAffected: Array.from(files).slice(0, 20),+ lessons: lessonIds,+ sourceActionIds: existingCrystal?.sourceActionIds ?? [],+ sessionId,+ project,+ createdAt: existingCrystal?.createdAt ?? createdAt,+ };await kv.set(KV.crystals, crystalId, crystal);} catch {}}diff --git a/src/types.ts b/src/types.tsindex 599ed136..d3a9bb6c 100644--- a/src/types.ts+++ b/src/types.ts@@ -851,3 +851,15 @@ export interface DecayConfig {cold: number;};}++/**+ * KV.state scope — long-lived system counters + flags keyed by string.+ * Keep keys/types in sync with the state-scope callers (e.g.,+ * disk-size-manager) so TypeScript enforces consistent value shapes+ * instead of every caller using ad-hoc `<number>` generics.+ */+export interface StateScope {+ "system:currentDiskSize": number;+}++export type StateScopeKey = keyof StateScope;diff --git a/src/viewer/index.html b/src/viewer/index.htmlindex b15a6cd9..b08d5ced 100644--- a/src/viewer/index.html+++ b/src/viewer/index.html@@ -2687,7 +2687,21 @@if (search) {items = items.filter(function(c) {- return ((c.narrative || '') + ' ' + (c.keyOutcomes || []).join(' ') + ' ' + (c.lessons || []).join(' ')).toLowerCase().indexOf(search) >= 0;+ var lessonText = (c.lessons || [])+ .map(function(lid) {+ var l = lessonMap[lid];+ return l && typeof l.content === 'string' ? l.content : lid;+ })+ .join(' ');+ var filesText = (c.filesAffected || []).join(' ');+ var haystack = [+ c.narrative || '',+ (c.keyOutcomes || []).join(' '),+ lessonText,+ filesText,+ c.project || '',+ ].join(' ').toLowerCase();+ return haystack.indexOf(search) >= 0;});}diff --git a/tsdown.config.ts b/tsdown.config.tsindex 0e7f97e1..2c30a48b 100644--- a/tsdown.config.ts+++ b/tsdown.config.ts@@ -19,9 +19,14 @@ const shared = {format: ["esm"] as const,target: "node20" as const,inlineOnly: false as const,- // Keep as node_modules imports. Bundling inlines relative paths like+ // Keep as node_modules imports. We never import onnxruntime-{node,web}+ // directly; they come in transitively through @xenova/transformers, which+ // is lazy-loaded from src/providers/embedding/{clip,local}.ts and+ // src/state/reranker.ts. Bundling inlines relative paths like// `../bin/napi-v3/darwin/arm64/onnxruntime_binding.node` that no longer- // resolve from dist/. CLIP + local embeddings lazy-load these anyway.+ // resolve from dist/. All three are declared as optionalDependencies in+ // package.json so users can install them only when they enable local+ // embeddings / CLIP / reranker.external: ["@xenova/transformers","onnxruntime-node",