From 3a943faea473ee9d8ff721ce8adabca3eb087e70 Mon Sep 17 00:00:00 2001 From: sbynode-ux Date: Tue, 11 Aug 2026 21:16:02 -0500 Subject: [PATCH 1/5] fix: evict lowest-value observations at the session cap instead of refusing writes MAX_OBS_PER_SESSION currently makes mem::observe return "Session observation limit reached" once a session holds its cap, so every later observation in that session is dropped. The observations lost are the NEWEST ones in the LONGEST sessions, which is the part a memory system is least able to afford losing, and nothing is logged server-side, so a capped session looks identical to a quiet one. Evict instead: at the cap, drop the least valuable rows (lowest importance first, oldest breaking ties) to make room, clear them from the BM25 and vector indexes so search cannot return dead ids, and warn with the session id and the number evicted. The bound on how much a session stores is unchanged; only the choice of which observation is sacrificed changes. Three sessions in a real install had been silently pinned at exactly 500 rows for days before this was noticed. Signed-off-by: sbynode-ux --- src/functions/observe.ts | 54 +++++++++-- test/observe-session-cap.test.ts | 157 +++++++++++++++++++++++++++++++ 2 files changed, 205 insertions(+), 6 deletions(-) create mode 100644 test/observe-session-cap.test.ts diff --git a/src/functions/observe.ts b/src/functions/observe.ts index 8ad4ba0ff5..16121aa297 100644 --- a/src/functions/observe.ts +++ b/src/functions/observe.ts @@ -7,11 +7,18 @@ import { DedupMap } from "./dedup.js"; import { withKeyedLock } from "../state/keyed-mutex.js"; import { isAutoCompressEnabled } from "../config.js"; import { buildSyntheticCompression } from "./compress-synthetic.js"; -import { getSearchIndex, vectorIndexAddGuarded } from "./search.js"; +import { + getSearchIndex, + vectorIndexAddGuarded, + vectorIndexRemove, +} from "./search.js"; import { getAgentId } from "../config.js"; import { logger } from "../logger.js"; import { saveImageToDisk } from "../utils/image-store.js"; +/** Importance assumed for an observation that has not been compressed yet. */ +const DEFAULT_EVICTION_IMPORTANCE = 3; + export function extractImage(d: unknown): string | undefined { if (!d) return undefined; if (typeof d === "string") { @@ -126,12 +133,47 @@ export function registerObserveFunction( return withKeyedLock(`obs:${payload.sessionId}`, async () => { if (maxObservationsPerSession && maxObservationsPerSession > 0) { - const existing = await kv.list(KV.observations(payload.sessionId)); + const existing = await kv.list( + KV.observations(payload.sessionId), + ); if (existing.length >= maxObservationsPerSession) { - return { - success: false, - error: `Session observation limit reached (${maxObservationsPerSession})`, - }; + // Refusing the write silently drops the NEWEST observations of the + // longest sessions — exactly the part worth keeping. Evict the + // lowest-value rows instead so capture keeps working inside the + // same bound. Least valuable first: lowest importance, then oldest. + const victims = [...existing] + .sort((a, b) => { + const ia = a.importance ?? DEFAULT_EVICTION_IMPORTANCE; + const ib = b.importance ?? DEFAULT_EVICTION_IMPORTANCE; + if (ia !== ib) return ia - ib; + return ( + new Date(a.timestamp ?? 0).getTime() - + new Date(b.timestamp ?? 0).getTime() + ); + }) + .slice(0, existing.length - maxObservationsPerSession + 1); + + let evicted = 0; + for (const victim of victims) { + try { + await kv.delete(KV.observations(payload.sessionId), victim.id); + evicted++; + getSearchIndex().remove(victim.id); + vectorIndexRemove(victim.id); + } catch { + // A failed delete just leaves the cap tight for this write; + // the next observation retries the eviction. + } + } + + logger.warn( + "Session observation cap reached — evicted lowest-value observations", + { + sessionId: payload.sessionId, + cap: maxObservationsPerSession, + evicted, + }, + ); } } diff --git a/test/observe-session-cap.test.ts b/test/observe-session-cap.test.ts new file mode 100644 index 0000000000..a974d2dbfb --- /dev/null +++ b/test/observe-session-cap.test.ts @@ -0,0 +1,157 @@ +import { describe, it, expect, vi, beforeEach } from "vitest"; + +vi.mock("../src/logger.js", () => ({ + logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, +})); + +const removedFromBm25: string[] = []; +const removedFromVector: string[] = []; + +vi.mock("../src/functions/search.js", () => ({ + getSearchIndex: () => ({ + remove: (id: string) => { + removedFromBm25.push(id); + }, + add: () => {}, + }), + vectorIndexAddGuarded: async () => false, + vectorIndexRemove: (id: string) => { + removedFromVector.push(id); + }, +})); + +function mockKV() { + const store = new Map>(); + return { + store, + get: async (scope: string, key: string): Promise => + (store.get(scope)?.get(key) as T) ?? null, + set: async (scope: string, key: string, data: T): Promise => { + if (!store.has(scope)) store.set(scope, new Map()); + store.get(scope)!.set(key, data); + return data; + }, + update: async () => {}, + delete: async (scope: string, key: string) => { + store.get(scope)?.delete(key); + }, + list: async (scope: string): Promise => { + const m = store.get(scope); + return m ? (Array.from(m.values()) as T[]) : []; + }, + }; +} + +function mockSdk() { + const fns = new Map(); + return { + fns, + registerFunction: (idOrOpts: string | { id: string }, fn: Function) => { + const id = typeof idOrOpts === "string" ? idOrOpts : idOrOpts.id; + fns.set(id, fn); + }, + trigger: async ( + idOrInput: string | { function_id: string; payload: unknown }, + data?: unknown, + ) => { + const id = typeof idOrInput === "string" ? idOrInput : idOrInput.function_id; + const payload = typeof idOrInput === "string" ? data : idOrInput.payload; + const fn = fns.get(id); + if (fn) return fn(payload); + return null; + }, + triggerVoid: () => {}, + }; +} + +const SESSION = "ses_capped"; +const SCOPE = `mem:obs:${SESSION}`; + +function seed(kv: ReturnType, rows: Array<{ id: string; importance?: number; ts: string }>) { + for (const row of rows) { + kv.store.set(SCOPE, kv.store.get(SCOPE) ?? new Map()); + kv.store.get(SCOPE)!.set(row.id, { + id: row.id, + sessionId: SESSION, + timestamp: row.ts, + hookType: "post_tool_use", + importance: row.importance, + raw: {}, + }); + } +} + +async function observeOnce(sdk: ReturnType, text: string) { + return (await sdk.trigger("mem::observe", { + sessionId: SESSION, + hookType: "post_tool_use", + timestamp: new Date().toISOString(), + data: { tool_name: "Bash", tool_input: { command: text } }, + })) as { success?: boolean; observationId?: string; error?: string }; +} + +describe("session observation cap evicts instead of refusing", () => { + beforeEach(() => { + vi.resetModules(); + removedFromBm25.length = 0; + removedFromVector.length = 0; + }); + + it("accepts the new observation at the cap and drops the least valuable row", async () => { + const { registerObserveFunction } = await import("../src/functions/observe.js"); + const sdk = mockSdk(); + const kv = mockKV(); + registerObserveFunction(sdk as never, kv as never, undefined, 3); + + seed(kv, [ + { id: "obs_keep_high", importance: 9, ts: "2026-01-01T00:00:00.000Z" }, + { id: "obs_evict_low", importance: 1, ts: "2026-01-02T00:00:00.000Z" }, + { id: "obs_keep_mid", importance: 5, ts: "2026-01-03T00:00:00.000Z" }, + ]); + + const result = await observeOnce(sdk, "the newest command"); + + expect(result.observationId).toBeTruthy(); + expect(result.error).toBeUndefined(); + + const ids = Array.from(kv.store.get(SCOPE)!.keys()); + expect(ids).toHaveLength(3); + expect(ids).not.toContain("obs_evict_low"); + expect(ids).toContain("obs_keep_high"); + expect(ids).toContain("obs_keep_mid"); + }); + + it("evicts the oldest row when importance ties, and clears it from both indexes", async () => { + const { registerObserveFunction } = await import("../src/functions/observe.js"); + const sdk = mockSdk(); + const kv = mockKV(); + registerObserveFunction(sdk as never, kv as never, undefined, 2); + + seed(kv, [ + { id: "obs_older", importance: 4, ts: "2026-01-01T00:00:00.000Z" }, + { id: "obs_newer", importance: 4, ts: "2026-02-01T00:00:00.000Z" }, + ]); + + await observeOnce(sdk, "another command"); + + const ids = Array.from(kv.store.get(SCOPE)!.keys()); + expect(ids).not.toContain("obs_older"); + expect(ids).toContain("obs_newer"); + expect(removedFromBm25).toContain("obs_older"); + expect(removedFromVector).toContain("obs_older"); + }); + + it("leaves capture untouched when the session is under the cap", async () => { + const { registerObserveFunction } = await import("../src/functions/observe.js"); + const sdk = mockSdk(); + const kv = mockKV(); + registerObserveFunction(sdk as never, kv as never, undefined, 10); + + seed(kv, [{ id: "obs_only", importance: 1, ts: "2026-01-01T00:00:00.000Z" }]); + + await observeOnce(sdk, "still room"); + + expect(Array.from(kv.store.get(SCOPE)!.keys())).toContain("obs_only"); + expect(removedFromBm25).toHaveLength(0); + }); +}); From 69b19f3a25c9e598ccbf10e18a729f95c7f0853e Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Wed, 11 Nov 2026 16:33:26 +0530 Subject: [PATCH 2/5] fix(observe): make session cap eviction complete and safe Eviction now removes each victim the same way mem::evict removes an observation. It marks the capture event deleted, deletes the KV row, removes the BM25 and vector entries, drops the obs-session index entry and releases image refs. One audit entry lists the evicted ids. A failed delete moves on to the next candidate. If eviction cannot free enough slots, the write is refused with a returned error, so the capture inbox does not retry it and the cap still holds. Nothing in the eviction path throws. The incoming observation is never a candidate. Rows without an importance score sort as 5, which matches the synthetic compression default and the other eviction paths. The status hint for cap dead letters now points at eviction failure. Tests cover failed deletes, an above-cap session, image refs, the audit entry, the obs-session index and concurrent writes at the cap. Signed-off-by: Rohit Ghumare --- src/functions/observe.ts | 131 +++++++++++------ src/functions/status.ts | 2 +- test/observe-session-cap.test.ts | 243 ++++++++++++++++++++++--------- 3 files changed, 260 insertions(+), 116 deletions(-) diff --git a/src/functions/observe.ts b/src/functions/observe.ts index 329d1ac77c..e269019f33 100644 --- a/src/functions/observe.ts +++ b/src/functions/observe.ts @@ -12,9 +12,11 @@ import { DedupMap, recordDedupSkip } from "./dedup.js"; import { afterKeyedLock, withKeyedLock } from "../state/keyed-mutex.js"; import { getFinalizeIdleMs, isAutoCompressEnabled } from "../config.js"; import { buildSyntheticCompression } from "./compress-synthetic.js"; -import { isCaptureKey } from "../capture/event-record.js"; +import { isCaptureKey, markCaptureEventDeleted } from "../capture/event-record.js"; import { claimBackfillPrompt, isBackfillPrompt, recordLivePrompt } from "../capture/prompt-ledger.js"; import { getSearchIndex, getVectorIndex, scheduleIndexSave, vectorIndexAddGuarded, vectorIndexRemove } from "./search.js"; +import { unindexObservationSession } from "../state/obs-index.js"; +import { safeAudit } from "./audit.js"; import { getAgentId } from "../config.js"; import { logger } from "../logger.js"; import { saveImageToDisk } from "../utils/image-store.js"; @@ -25,8 +27,74 @@ import { markCompressionDispatchFailed, pendingCompression } from "./compression import { requestSummaryRefresh } from "./summary-state.js"; import { scheduleSessionFinalize } from "./session-finalize.js"; -/** Importance assumed for an observation that has not been compressed yet. */ -const DEFAULT_EVICTION_IMPORTANCE = 3; +const DEFAULT_EVICTION_IMPORTANCE = 5; + +function byEvictionOrder(a: CompressedObservation, b: CompressedObservation): number { + const ia = a.importance ?? DEFAULT_EVICTION_IMPORTANCE; + const ib = b.importance ?? DEFAULT_EVICTION_IMPORTANCE; + if (ia !== ib) return ia - ib; + return new Date(a.timestamp ?? 0).getTime() - new Date(b.timestamp ?? 0).getTime(); +} + +async function evictForSessionCap( + sdk: IIIClient, + kv: StateKV, + sessionId: string, + incomingId: string, + existing: CompressedObservation[], + cap: number, +): Promise<{ evicted: CompressedObservation[]; needed: number }> { + const needed = existing.length - cap + 1; + const candidates = existing.filter((o) => o?.id && o.id !== incomingId).sort(byEvictionOrder); + const evicted: CompressedObservation[] = []; + for (const victim of candidates) { + if (evicted.length >= needed) break; + try { + await markCaptureEventDeleted(kv, victim); + await kv.delete(KV.observations(sessionId), victim.id); + } catch (err) { + logger.warn("Session cap eviction delete failed", { + sessionId, + obsId: victim.id, + error: err instanceof Error ? err.message : String(err), + }); + continue; + } + evicted.push(victim); + getSearchIndex().remove(victim.id); + vectorIndexRemove(victim.id); + } + if (evicted.length === 0) return { evicted, needed }; + + scheduleIndexSave(); + const { decrementImageRef } = await import("./image-refs.js"); + for (const victim of evicted) { + await unindexObservationSession(kv, victim.id).catch(() => {}); + const refs = new Set([victim.imageData, victim.imageRef].filter((r): r is string => typeof r === "string" && r.length > 0)); + for (const ref of refs) { + await decrementImageRef(kv, sdk, ref).catch((err) => { + logger.warn("Session cap eviction image ref release failed", { + sessionId, + obsId: victim.id, + error: err instanceof Error ? err.message : String(err), + }); + }); + } + } + await safeAudit(kv, "delete", "mem::observe", evicted.map((o) => o.id), { + resource: "observation", + reason: "session_observation_cap", + sessionId, + cap, + evicted: evicted.length, + }); + logger.warn("Session observation cap reached, evicted lowest-value observations", { + sessionId, + cap, + evicted: evicted.length, + }); + return { evicted, needed }; +} export function extractImage(d: unknown): string | undefined { if (!d) return undefined; @@ -190,7 +258,7 @@ export function registerObserveFunction( const promptRef = durable || typeof payload.eventId !== "string" ? obsId : payload.eventId; return withKeyedLock(`obs:${payload.sessionId}`, async () => { - const existing = await kv.list(KV.observations(payload.sessionId)); + let existing = await kv.list(KV.observations(payload.sessionId)); const stored = durable ? existing.find((o) => o?.id === obsId) : undefined; if (stored) { if (livePrompt) await recordLivePrompt(kv, payload.sessionId, livePrompt, promptRef); @@ -201,45 +269,22 @@ export function registerObserveFunction( recordDedupSkip(); return { deduplicated: true, sessionId: payload.sessionId }; } - if (maxObservationsPerSession && maxObservationsPerSession > 0) { - if (existing.length >= maxObservationsPerSession) { - // Refusing the write silently drops the NEWEST observations of the - // longest sessions — exactly the part worth keeping. Evict the - // lowest-value rows instead so capture keeps working inside the - // same bound. Least valuable first: lowest importance, then oldest. - const victims = [...existing] - .sort((a, b) => { - const ia = a.importance ?? DEFAULT_EVICTION_IMPORTANCE; - const ib = b.importance ?? DEFAULT_EVICTION_IMPORTANCE; - if (ia !== ib) return ia - ib; - return ( - new Date(a.timestamp ?? 0).getTime() - - new Date(b.timestamp ?? 0).getTime() - ); - }) - .slice(0, existing.length - maxObservationsPerSession + 1); - - let evicted = 0; - for (const victim of victims) { - try { - await kv.delete(KV.observations(payload.sessionId), victim.id); - evicted++; - getSearchIndex().remove(victim.id); - vectorIndexRemove(victim.id); - } catch { - // A failed delete just leaves the cap tight for this write; - // the next observation retries the eviction. - } - } - - logger.warn( - "Session observation cap reached — evicted lowest-value observations", - { - sessionId: payload.sessionId, - cap: maxObservationsPerSession, - evicted, - }, - ); + if (maxObservationsPerSession && maxObservationsPerSession > 0 && existing.length >= maxObservationsPerSession) { + const { evicted, needed } = await evictForSessionCap( + sdk, + kv, + payload.sessionId, + obsId, + existing, + maxObservationsPerSession, + ); + const evictedIds = new Set(evicted.map((o) => o.id)); + existing = existing.filter((o) => !evictedIds.has(o?.id)); + if (evicted.length < needed) { + return { + success: false, + error: `Session observation limit reached (${maxObservationsPerSession}) and eviction freed ${evicted.length} of ${needed} slots`, + }; } } diff --git a/src/functions/status.ts b/src/functions/status.ts index 6967f7249b..930e7c987c 100644 --- a/src/functions/status.ts +++ b/src/functions/status.ts @@ -570,7 +570,7 @@ function captureProblems(capture: CaptureStatus | null, now: Date, restPort: num code: "capture-dead-letters", message: `${plural(inbox.dead, "captured observation")} could not be stored and ${inbox.dead === 1 ? "is parked as a dead letter" : "are parked as dead letters"}${inbox.lastError ? `: ${inbox.lastError}` : "."}`, fix: sessionCap - ? `The session reached its MAX_OBS_PER_SESSION cap, so a retry is refused again until the cap is raised. Raise MAX_OBS_PER_SESSION in ~/.agentmemory/.env (0 = no cap) and restart agentmemory. ${listAndRetry}` + ? `The session reached its MAX_OBS_PER_SESSION cap and older observations could not be evicted to make room. Check the server log for "Session cap eviction delete failed", or raise MAX_OBS_PER_SESSION in ~/.agentmemory/.env (0 = no cap) and restart agentmemory. ${listAndRetry}` : listAndRetry, }); } diff --git a/test/observe-session-cap.test.ts b/test/observe-session-cap.test.ts index a974d2dbfb..23680e0370 100644 --- a/test/observe-session-cap.test.ts +++ b/test/observe-session-cap.test.ts @@ -1,11 +1,22 @@ import { describe, it, expect, vi, beforeEach } from "vitest"; +vi.mock("iii-sdk", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + getContext: () => ({ + logger: { info: vi.fn(), error: vi.fn(), warn: vi.fn() }, + }), + }; +}); + vi.mock("../src/logger.js", () => ({ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, })); const removedFromBm25: string[] = []; const removedFromVector: string[] = []; +const releasedImageRefs: string[] = []; vi.mock("../src/functions/search.js", () => ({ getSearchIndex: () => ({ @@ -13,71 +24,48 @@ vi.mock("../src/functions/search.js", () => ({ removedFromBm25.push(id); }, add: () => {}, + has: () => false, }), + getVectorIndex: () => null, + scheduleIndexSave: () => {}, vectorIndexAddGuarded: async () => false, vectorIndexRemove: (id: string) => { removedFromVector.push(id); }, })); -function mockKV() { - const store = new Map>(); - return { - store, - get: async (scope: string, key: string): Promise => - (store.get(scope)?.get(key) as T) ?? null, - set: async (scope: string, key: string, data: T): Promise => { - if (!store.has(scope)) store.set(scope, new Map()); - store.get(scope)!.set(key, data); - return data; - }, - update: async () => {}, - delete: async (scope: string, key: string) => { - store.get(scope)?.delete(key); - }, - list: async (scope: string): Promise => { - const m = store.get(scope); - return m ? (Array.from(m.values()) as T[]) : []; - }, - }; -} +vi.mock("../src/functions/image-refs.js", () => ({ + incrementImageRef: async () => {}, + decrementImageRef: async (_kv: unknown, _sdk: unknown, ref: string) => { + releasedImageRefs.push(ref); + }, +})); -function mockSdk() { - const fns = new Map(); - return { - fns, - registerFunction: (idOrOpts: string | { id: string }, fn: Function) => { - const id = typeof idOrOpts === "string" ? idOrOpts : idOrOpts.id; - fns.set(id, fn); - }, - trigger: async ( - idOrInput: string | { function_id: string; payload: unknown }, - data?: unknown, - ) => { - const id = typeof idOrInput === "string" ? idOrInput : idOrInput.function_id; - const payload = typeof idOrInput === "string" ? data : idOrInput.payload; - const fn = fns.get(id); - if (fn) return fn(payload); - return null; - }, - triggerVoid: () => {}, - }; -} +import { mockKV, mockSdk, currentAuditScope } from "./helpers/mocks.js"; +import { indexObservationSession, lookupObservationSession } from "../src/state/obs-index.js"; +import type { AuditEntry } from "../src/types.js"; const SESSION = "ses_capped"; const SCOPE = `mem:obs:${SESSION}`; -function seed(kv: ReturnType, rows: Array<{ id: string; importance?: number; ts: string }>) { +type Row = { id: string; importance?: number; ts: string; imageData?: string }; + +async function seed(kv: ReturnType, rows: Row[]) { for (const row of rows) { - kv.store.set(SCOPE, kv.store.get(SCOPE) ?? new Map()); - kv.store.get(SCOPE)!.set(row.id, { + await kv.set(SCOPE, row.id, { id: row.id, sessionId: SESSION, timestamp: row.ts, - hookType: "post_tool_use", + type: "command_run", + title: row.id, + facts: [], + narrative: "", + concepts: [], + files: [], importance: row.importance, - raw: {}, + ...(row.imageData ? { imageData: row.imageData } : {}), }); + await indexObservationSession(kv as never, row.id, SESSION); } } @@ -90,20 +78,29 @@ async function observeOnce(sdk: ReturnType, text: string) { })) as { success?: boolean; observationId?: string; error?: string }; } +async function setup(cap: number) { + const { registerObserveFunction } = await import("../src/functions/observe.js"); + const sdk = mockSdk({ looseTrigger: true }); + const kv = mockKV(); + registerObserveFunction(sdk as never, kv as never, undefined, cap); + return { sdk, kv }; +} + +function storedIds(kv: ReturnType): string[] { + return Array.from(kv.store.get(SCOPE)?.keys() ?? []); +} + describe("session observation cap evicts instead of refusing", () => { beforeEach(() => { vi.resetModules(); removedFromBm25.length = 0; removedFromVector.length = 0; + releasedImageRefs.length = 0; }); it("accepts the new observation at the cap and drops the least valuable row", async () => { - const { registerObserveFunction } = await import("../src/functions/observe.js"); - const sdk = mockSdk(); - const kv = mockKV(); - registerObserveFunction(sdk as never, kv as never, undefined, 3); - - seed(kv, [ + const { sdk, kv } = await setup(3); + await seed(kv, [ { id: "obs_keep_high", importance: 9, ts: "2026-01-01T00:00:00.000Z" }, { id: "obs_evict_low", importance: 1, ts: "2026-01-02T00:00:00.000Z" }, { id: "obs_keep_mid", importance: 5, ts: "2026-01-03T00:00:00.000Z" }, @@ -113,45 +110,147 @@ describe("session observation cap evicts instead of refusing", () => { expect(result.observationId).toBeTruthy(); expect(result.error).toBeUndefined(); - - const ids = Array.from(kv.store.get(SCOPE)!.keys()); + const ids = storedIds(kv); expect(ids).toHaveLength(3); expect(ids).not.toContain("obs_evict_low"); expect(ids).toContain("obs_keep_high"); expect(ids).toContain("obs_keep_mid"); + expect(ids).toContain(result.observationId); }); - it("evicts the oldest row when importance ties, and clears it from both indexes", async () => { - const { registerObserveFunction } = await import("../src/functions/observe.js"); - const sdk = mockSdk(); - const kv = mockKV(); - registerObserveFunction(sdk as never, kv as never, undefined, 2); - - seed(kv, [ + it("evicts the oldest row on an importance tie and clears it from every index", async () => { + const { sdk, kv } = await setup(2); + await seed(kv, [ { id: "obs_older", importance: 4, ts: "2026-01-01T00:00:00.000Z" }, { id: "obs_newer", importance: 4, ts: "2026-02-01T00:00:00.000Z" }, ]); await observeOnce(sdk, "another command"); - const ids = Array.from(kv.store.get(SCOPE)!.keys()); + const ids = storedIds(kv); expect(ids).not.toContain("obs_older"); expect(ids).toContain("obs_newer"); - expect(removedFromBm25).toContain("obs_older"); - expect(removedFromVector).toContain("obs_older"); + expect(removedFromBm25).toEqual(["obs_older"]); + expect(removedFromVector).toEqual(["obs_older"]); + expect(await lookupObservationSession(kv as never, "obs_older")).toBeNull(); + expect(await lookupObservationSession(kv as never, "obs_newer")).toBe(SESSION); }); - it("leaves capture untouched when the session is under the cap", async () => { - const { registerObserveFunction } = await import("../src/functions/observe.js"); - const sdk = mockSdk(); - const kv = mockKV(); - registerObserveFunction(sdk as never, kv as never, undefined, 10); + it("treats a missing importance like the synthetic default", async () => { + const { sdk, kv } = await setup(2); + await seed(kv, [ + { id: "obs_unscored", ts: "2026-01-01T00:00:00.000Z" }, + { id: "obs_low", importance: 4, ts: "2026-02-01T00:00:00.000Z" }, + ]); + + await observeOnce(sdk, "next command"); + + expect(storedIds(kv)).toContain("obs_unscored"); + expect(storedIds(kv)).not.toContain("obs_low"); + }); + + it("records one audit entry for the eviction", async () => { + const { sdk, kv } = await setup(1); + await seed(kv, [{ id: "obs_victim", importance: 2, ts: "2026-01-01T00:00:00.000Z" }]); - seed(kv, [{ id: "obs_only", importance: 1, ts: "2026-01-01T00:00:00.000Z" }]); + await observeOnce(sdk, "audited command"); + + const audits = await kv.list(currentAuditScope()); + const evictions = audits.filter((a) => a.functionId === "mem::observe" && a.operation === "delete"); + expect(evictions).toHaveLength(1); + expect(evictions[0]!.targetIds).toEqual(["obs_victim"]); + expect(evictions[0]!.details).toMatchObject({ reason: "session_observation_cap", sessionId: SESSION, evicted: 1 }); + }); + + it("releases the image ref held by an evicted observation", async () => { + const { sdk, kv } = await setup(1); + await seed(kv, [ + { id: "obs_with_image", importance: 1, ts: "2026-01-01T00:00:00.000Z", imageData: "/tmp/agentmemory-test/img.png" }, + ]); + + await observeOnce(sdk, "text only"); + + expect(releasedImageRefs).toEqual(["/tmp/agentmemory-test/img.png"]); + }); + + it("evicts down to the cap when the session is already above it", async () => { + const { sdk, kv } = await setup(2); + await seed(kv, [ + { id: "obs_a", importance: 1, ts: "2026-01-01T00:00:00.000Z" }, + { id: "obs_b", importance: 2, ts: "2026-01-02T00:00:00.000Z" }, + { id: "obs_c", importance: 8, ts: "2026-01-03T00:00:00.000Z" }, + { id: "obs_d", importance: 9, ts: "2026-01-04T00:00:00.000Z" }, + ]); + + const result = await observeOnce(sdk, "over the cap"); + + const ids = storedIds(kv); + expect(ids).toHaveLength(2); + expect(ids).toContain("obs_d"); + expect(ids).toContain(result.observationId); + }); + + it("moves to the next candidate when a delete fails and keeps the cap", async () => { + const { sdk, kv } = await setup(2); + await seed(kv, [ + { id: "obs_stuck", importance: 1, ts: "2026-01-01T00:00:00.000Z" }, + { id: "obs_next", importance: 2, ts: "2026-01-02T00:00:00.000Z" }, + ]); + const realDelete = kv.delete; + kv.delete = async (scope: string, key: string) => { + if (key === "obs_stuck") throw new Error("disk busy"); + return realDelete(scope, key); + }; + + const result = await observeOnce(sdk, "after a failed delete"); + + expect(result.observationId).toBeTruthy(); + const ids = storedIds(kv); + expect(ids).toHaveLength(2); + expect(ids).toContain("obs_stuck"); + expect(ids).not.toContain("obs_next"); + expect(removedFromBm25).toEqual(["obs_next"]); + }); + + it("returns a cap error without writing or throwing when no row can be evicted", async () => { + const { sdk, kv } = await setup(1); + await seed(kv, [{ id: "obs_stuck", importance: 1, ts: "2026-01-01T00:00:00.000Z" }]); + kv.delete = async () => { + throw new Error("disk busy"); + }; + + const result = await observeOnce(sdk, "cannot fit"); + + expect(result.success).toBe(false); + expect(result.observationId).toBeUndefined(); + expect(result.error).toMatch(/^Session observation limit reached \(1\)/); + expect(storedIds(kv)).toEqual(["obs_stuck"]); + expect(removedFromBm25).toHaveLength(0); + }); + + it("keeps the cap when several observations arrive at once", async () => { + const { sdk, kv } = await setup(3); + await seed(kv, [ + { id: "obs_1", importance: 5, ts: "2026-01-01T00:00:00.000Z" }, + { id: "obs_2", importance: 5, ts: "2026-01-02T00:00:00.000Z" }, + { id: "obs_3", importance: 5, ts: "2026-01-03T00:00:00.000Z" }, + ]); + + const results = await Promise.all( + Array.from({ length: 5 }, (_, i) => observeOnce(sdk, `parallel ${i}`)), + ); + + expect(results.every((r) => typeof r.observationId === "string")).toBe(true); + expect(storedIds(kv)).toHaveLength(3); + }); + + it("leaves capture untouched when the session is under the cap", async () => { + const { sdk, kv } = await setup(10); + await seed(kv, [{ id: "obs_only", importance: 1, ts: "2026-01-01T00:00:00.000Z" }]); await observeOnce(sdk, "still room"); - expect(Array.from(kv.store.get(SCOPE)!.keys())).toContain("obs_only"); + expect(storedIds(kv)).toContain("obs_only"); expect(removedFromBm25).toHaveLength(0); }); }); From c5b3ef09b54cb62544501ce5bf1b2012a8703277 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Wed, 11 Nov 2026 16:33:27 +0530 Subject: [PATCH 3/5] docs: sync stats Output of npm run docs:sync after the new session cap tests. Signed-off-by: Rohit Ghumare --- AGENTS.md | 4 ++-- README.md | 4 ++-- READMEs/README.ar-SA.md | 4 ++-- READMEs/README.bn-BD.md | 4 ++-- READMEs/README.cs-CZ.md | 4 ++-- READMEs/README.da-DK.md | 4 ++-- READMEs/README.de-DE.md | 4 ++-- READMEs/README.el-GR.md | 4 ++-- READMEs/README.es-ES.md | 4 ++-- READMEs/README.fi-FI.md | 4 ++-- READMEs/README.fr-FR.md | 4 ++-- READMEs/README.he-IL.md | 4 ++-- READMEs/README.hi-IN.md | 4 ++-- READMEs/README.hu-HU.md | 4 ++-- READMEs/README.id-ID.md | 4 ++-- READMEs/README.it-IT.md | 4 ++-- READMEs/README.ja-JP.md | 4 ++-- READMEs/README.ko-KR.md | 4 ++-- READMEs/README.nb-NO.md | 4 ++-- READMEs/README.nl-NL.md | 4 ++-- READMEs/README.pl-PL.md | 4 ++-- READMEs/README.pt-BR.md | 4 ++-- READMEs/README.pt-PT.md | 4 ++-- READMEs/README.ro-RO.md | 4 ++-- READMEs/README.ru-RU.md | 4 ++-- READMEs/README.sv-SE.md | 4 ++-- READMEs/README.th-TH.md | 4 ++-- READMEs/README.tl-PH.md | 4 ++-- READMEs/README.tr-TR.md | 4 ++-- READMEs/README.uk-UA.md | 4 ++-- READMEs/README.ur-PK.md | 4 ++-- READMEs/README.vi-VN.md | 4 ++-- READMEs/README.zh-CN.md | 4 ++-- READMEs/README.zh-TW.md | 4 ++-- assets/tags/light/stat-tests.svg | 4 ++-- assets/tags/stat-tests.svg | 4 ++-- scripts/docs-sync.state.json | 2 +- 37 files changed, 73 insertions(+), 73 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 3897c7910e..81b4eed6fa 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -101,7 +101,7 @@ Hook scripts in `src/hooks/` are standalone Node.js scripts (no iii-sdk import). ## Testing -- Before every commit run, in order: `npm run build`, `npm run skills:gen`, `npm run docs:sync`, `npm run skills:check`, `npm test` (4,700+ tests). Commit anything the generators changed. CI runs the same build, skills check and tests. +- Before every commit run, in order: `npm run build`, `npm run skills:gen`, `npm run docs:sync`, `npm run skills:check`, `npm test` (4,800+ tests). Commit anything the generators changed. CI runs the same build, skills check and tests. - Before any npm publish: `npm run release:gate` must pass. It installs the packed tarballs into a clean home and runs capture, recovery, restart, export/import, MCP and status checks against the installed CLI (see CONTRIBUTING.md, Release process) - Mock pattern: `vi.mock("iii-sdk")` with mock `sdk.trigger`, `kv.get/set/list` - Test files go in `test/` with `.test.ts` extension @@ -114,4 +114,4 @@ Hook scripts in `src/hooks/` are standalone Node.js scripts (no iii-sdk import). - 6 MCP resources, 3 MCP prompts - 12 hooks, 17 skills - 322+ iii functions -- 4,700+ tests +- 4,800+ tests diff --git a/README.md b/README.md index cd40701a05..7590f939b8 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,700+ tests passing + 4,800+ tests passing

@@ -1313,7 +1313,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.ar-SA.md b/READMEs/README.ar-SA.md index 19dfdad2f8..40b6c0699d 100644 --- a/READMEs/README.ar-SA.md +++ b/READMEs/README.ar-SA.md @@ -71,7 +71,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,700+ tests passing + 4,800+ tests passing

@@ -1244,7 +1244,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.bn-BD.md b/READMEs/README.bn-BD.md index a0881d0c27..b16b1f7bd4 100644 --- a/READMEs/README.bn-BD.md +++ b/READMEs/README.bn-BD.md @@ -69,7 +69,7 @@ 54 MCP টুল 12 অটো হুক 0 এক্সটার্নাল ডিবি - 4,700+ টেস্ট পাস + 4,800+ টেস্ট পাস

@@ -1257,7 +1257,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.cs-CZ.md b/READMEs/README.cs-CZ.md index 3837c5eb6e..41c98ab4df 100644 --- a/READMEs/README.cs-CZ.md +++ b/READMEs/README.cs-CZ.md @@ -69,7 +69,7 @@ 54 MCP nástrojů 12 automatických hooks 0 externích databází - 4,700+ testů úspěšně proběhlo + 4,800+ testů úspěšně proběhlo

@@ -1243,7 +1243,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.da-DK.md b/READMEs/README.da-DK.md index 209257de1f..6e96f1ad65 100644 --- a/READMEs/README.da-DK.md +++ b/READMEs/README.da-DK.md @@ -69,7 +69,7 @@ 54 MCP-værktøjer 12 automatiske hooks 0 eksterne databaser - 4,700+ beståede tests + 4,800+ beståede tests

@@ -1251,7 +1251,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.de-DE.md b/READMEs/README.de-DE.md index a0b4b683b8..f09b2211b6 100644 --- a/READMEs/README.de-DE.md +++ b/READMEs/README.de-DE.md @@ -69,7 +69,7 @@ 54 MCP-Tools 12 Auto-Hooks 0 externe DBs - 4,700+ bestandene Tests + 4,800+ bestandene Tests

@@ -1249,7 +1249,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.el-GR.md b/READMEs/README.el-GR.md index 49f0fb50b2..20190d727a 100644 --- a/READMEs/README.el-GR.md +++ b/READMEs/README.el-GR.md @@ -69,7 +69,7 @@ 54 εργαλεία MCP 12 αυτόματα hooks 0 εξωτερικές βάσεις δεδομένων - 4,700+ επιτυχημένες δοκιμές + 4,800+ επιτυχημένες δοκιμές

@@ -1243,7 +1243,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.es-ES.md b/READMEs/README.es-ES.md index 0cf25dab97..6ca88703a2 100644 --- a/READMEs/README.es-ES.md +++ b/READMEs/README.es-ES.md @@ -69,7 +69,7 @@ 54 tools MCP 12 hooks automáticos 0 bases de datos externas - 4,700+ tests superados + 4,800+ tests superados

@@ -1245,7 +1245,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.fi-FI.md b/READMEs/README.fi-FI.md index c580718e9f..c82e700071 100644 --- a/READMEs/README.fi-FI.md +++ b/READMEs/README.fi-FI.md @@ -69,7 +69,7 @@ 54 MCP-työkalua 12 automaattista hookia 0 ulkoista tietokantaa - 4,700+ testiä läpäisty + 4,800+ testiä läpäisty

@@ -1243,7 +1243,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.fr-FR.md b/READMEs/README.fr-FR.md index 3f508c8454..981e72e4f9 100644 --- a/READMEs/README.fr-FR.md +++ b/READMEs/README.fr-FR.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,700+ tests passing + 4,800+ tests passing

@@ -1252,7 +1252,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.he-IL.md b/READMEs/README.he-IL.md index 73f6e4a233..313f456302 100644 --- a/READMEs/README.he-IL.md +++ b/READMEs/README.he-IL.md @@ -71,7 +71,7 @@ 54 כלי MCP 12 hooks אוטומטיים 0 מסדי נתונים חיצוניים - 4,700+ בדיקות עוברות + 4,800+ בדיקות עוברות

@@ -1244,7 +1244,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.hi-IN.md b/READMEs/README.hi-IN.md index 4c3a8d31e2..9bed3ca5eb 100644 --- a/READMEs/README.hi-IN.md +++ b/READMEs/README.hi-IN.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,700+ tests passing + 4,800+ tests passing

@@ -1258,7 +1258,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.hu-HU.md b/READMEs/README.hu-HU.md index 20d0e86604..819b196d68 100644 --- a/READMEs/README.hu-HU.md +++ b/READMEs/README.hu-HU.md @@ -69,7 +69,7 @@ 54 MCP eszköz 12 automatikus hook 0 külső adatbázis - 4,700+ sikeres teszt + 4,800+ sikeres teszt

@@ -1241,7 +1241,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.id-ID.md b/READMEs/README.id-ID.md index 855cd4dd52..a5813dcfc8 100644 --- a/READMEs/README.id-ID.md +++ b/READMEs/README.id-ID.md @@ -69,7 +69,7 @@ 54 tool MCP 12 hook otomatis 0 DB eksternal - 4,700+ pengujian lolos + 4,800+ pengujian lolos

@@ -1258,7 +1258,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.it-IT.md b/READMEs/README.it-IT.md index cd396e06e9..a2e6f3aa8e 100644 --- a/READMEs/README.it-IT.md +++ b/READMEs/README.it-IT.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,700+ tests passing + 4,800+ tests passing

@@ -1251,7 +1251,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.ja-JP.md b/READMEs/README.ja-JP.md index 9dfe1e726c..412d9f4224 100644 --- a/READMEs/README.ja-JP.md +++ b/READMEs/README.ja-JP.md @@ -69,7 +69,7 @@ 54 MCP ツール 12 自動 hooks 外部 DB 0 個 - 4,700+ 件のテストが成功 + 4,800+ 件のテストが成功

@@ -1257,7 +1257,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.ko-KR.md b/READMEs/README.ko-KR.md index 4c77e312b0..db1bee6bd2 100644 --- a/READMEs/README.ko-KR.md +++ b/READMEs/README.ko-KR.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,700+ tests passing + 4,800+ tests passing

@@ -1256,7 +1256,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.nb-NO.md b/READMEs/README.nb-NO.md index e617bec5e1..def9fa24d0 100644 --- a/READMEs/README.nb-NO.md +++ b/READMEs/README.nb-NO.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,700+ tests passing + 4,800+ tests passing

@@ -1249,7 +1249,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.nl-NL.md b/READMEs/README.nl-NL.md index ab0aa4972b..a41ab0bf2e 100644 --- a/READMEs/README.nl-NL.md +++ b/READMEs/README.nl-NL.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,700+ tests passing + 4,800+ tests passing

@@ -1245,7 +1245,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.pl-PL.md b/READMEs/README.pl-PL.md index c24834e624..be1064ea75 100644 --- a/READMEs/README.pl-PL.md +++ b/READMEs/README.pl-PL.md @@ -69,7 +69,7 @@ 54 narzędzia MCP 12 automatycznych hooków 0 zewnętrznych baz danych - 4,700+ zaliczonych testów + 4,800+ zaliczonych testów

@@ -1241,7 +1241,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.pt-BR.md b/READMEs/README.pt-BR.md index 8cba9a4b83..14023e43ce 100644 --- a/READMEs/README.pt-BR.md +++ b/READMEs/README.pt-BR.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,700+ tests passing + 4,800+ tests passing

@@ -1249,7 +1249,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.pt-PT.md b/READMEs/README.pt-PT.md index c237b780d8..3f2e79bb91 100644 --- a/READMEs/README.pt-PT.md +++ b/READMEs/README.pt-PT.md @@ -69,7 +69,7 @@ 54 ferramentas MCP 12 hooks automáticos 0 bases de dados externas - 4,700+ testes aprovados + 4,800+ testes aprovados

@@ -1245,7 +1245,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.ro-RO.md b/READMEs/README.ro-RO.md index 620d55913d..91d70e2ffe 100644 --- a/READMEs/README.ro-RO.md +++ b/READMEs/README.ro-RO.md @@ -69,7 +69,7 @@ 54 de instrumente MCP 12 hook-uri automate 0 baze de date externe - 4,700+ teste trecute + 4,800+ teste trecute

@@ -1237,7 +1237,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.ru-RU.md b/READMEs/README.ru-RU.md index 3086998cdd..aed56590dc 100644 --- a/READMEs/README.ru-RU.md +++ b/READMEs/README.ru-RU.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,700+ tests passing + 4,800+ tests passing

@@ -1243,7 +1243,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.sv-SE.md b/READMEs/README.sv-SE.md index c29d00dbee..fc2f4d5b80 100644 --- a/READMEs/README.sv-SE.md +++ b/READMEs/README.sv-SE.md @@ -69,7 +69,7 @@ 54 MCP-verktyg 12 automatiska hooks 0 externa databaser - 4,700+ godkända tester + 4,800+ godkända tester

@@ -1251,7 +1251,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.th-TH.md b/READMEs/README.th-TH.md index 49a0e9ce46..fa1bd6d710 100644 --- a/READMEs/README.th-TH.md +++ b/READMEs/README.th-TH.md @@ -69,7 +69,7 @@ 54 เครื่องมือ MCP 12 hook อัตโนมัติ ไม่ต้องพึ่ง external DB เลย - ผ่านการทดสอบ 4,700+ รายการ + ผ่านการทดสอบ 4,800+ รายการ

@@ -1244,7 +1244,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.tl-PH.md b/READMEs/README.tl-PH.md index 9eacba7baf..27cec827c0 100644 --- a/READMEs/README.tl-PH.md +++ b/READMEs/README.tl-PH.md @@ -69,7 +69,7 @@ 54 MCP tool 12 awtomatikong hook 0 panlabas na DB - 4,700+ pumapasang test + 4,800+ pumapasang test

@@ -1253,7 +1253,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.tr-TR.md b/READMEs/README.tr-TR.md index f1aa62a40a..d26df964c2 100644 --- a/READMEs/README.tr-TR.md +++ b/READMEs/README.tr-TR.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,700+ tests passing + 4,800+ tests passing

@@ -1243,7 +1243,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.uk-UA.md b/READMEs/README.uk-UA.md index 11019c61fe..08832163f6 100644 --- a/READMEs/README.uk-UA.md +++ b/READMEs/README.uk-UA.md @@ -69,7 +69,7 @@ 54 інструменти MCP 12 автоматичних хуків 0 зовнішніх БД - 4,700+ тестів пройдено + 4,800+ тестів пройдено

@@ -1244,7 +1244,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.ur-PK.md b/READMEs/README.ur-PK.md index a4a9b37b3c..c61fdbc3ef 100644 --- a/READMEs/README.ur-PK.md +++ b/READMEs/README.ur-PK.md @@ -71,7 +71,7 @@ 54 MCP ٹولز 12 خودکار ہکس 0 خارجی ڈیٹابیسز - 4,700+ ٹیسٹس پاس ہو رہے ہیں + 4,800+ ٹیسٹس پاس ہو رہے ہیں

@@ -1248,7 +1248,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.vi-VN.md b/READMEs/README.vi-VN.md index 47177b4c47..6b3d54b63f 100644 --- a/READMEs/README.vi-VN.md +++ b/READMEs/README.vi-VN.md @@ -69,7 +69,7 @@ 54 MCP tools 12 auto hooks 0 external DBs - 4,700+ tests passing + 4,800+ tests passing

@@ -1253,7 +1253,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.zh-CN.md b/READMEs/README.zh-CN.md index 0a5f9c2d05..e7a3f90896 100644 --- a/READMEs/README.zh-CN.md +++ b/READMEs/README.zh-CN.md @@ -69,7 +69,7 @@ 54 个 MCP 工具 12 个自动 hooks 0 个外部数据库 - 4,700+ 项测试通过 + 4,800+ 项测试通过

@@ -1254,7 +1254,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/READMEs/README.zh-TW.md b/READMEs/README.zh-TW.md index c2e315c886..74e1eca426 100644 --- a/READMEs/README.zh-TW.md +++ b/READMEs/README.zh-TW.md @@ -69,7 +69,7 @@ 54 個 MCP 工具 12 個自動 hooks 0 個外部資料庫 - 4,700+ 測試通過 + 4,800+ 測試通過

@@ -1246,7 +1246,7 @@ curl -X POST http://localhost:3111/agentmemory/graph/compact -H "Authorization: ```bash npm run dev # Hot reload npm run build # Production build -npm test # 4,700+ tests +npm test # 4,800+ tests npm run test:integration # API tests (requires running services) ``` diff --git a/assets/tags/light/stat-tests.svg b/assets/tags/light/stat-tests.svg index 1b4179cfc2..743b0d095d 100644 --- a/assets/tags/light/stat-tests.svg +++ b/assets/tags/light/stat-tests.svg @@ -1,5 +1,5 @@ - + - 4700+ + 4800+ TESTS PASSING diff --git a/assets/tags/stat-tests.svg b/assets/tags/stat-tests.svg index 2b679ac742..7b4a1c20eb 100644 --- a/assets/tags/stat-tests.svg +++ b/assets/tags/stat-tests.svg @@ -1,5 +1,5 @@ - + - 4700+ + 4800+ TESTS PASSING diff --git a/scripts/docs-sync.state.json b/scripts/docs-sync.state.json index ef3f2b4c6b..6d5580a194 100644 --- a/scripts/docs-sync.state.json +++ b/scripts/docs-sync.state.json @@ -20,7 +20,7 @@ "12" ], "tests": [ - "4700+" + "4800+" ], "functions": [ "323" From f0efd075037eea44ed25ee42710b01183980dc3b Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Wed, 11 Nov 2026 18:14:33 +0530 Subject: [PATCH 4/5] fix(observe): evict only after the incoming observation is stored Eviction ran before the new observation was written, so a failed write lost the evicted rows and their image files. The write now happens first, under the same session lock, and eviction runs only after it succeeds. If eviction frees too few slots, the write is kept and the next write evicts again. The session observation count now drops by the number of evicted rows, so the unindexed-observation scan can skip fully indexed capped sessions again. Rows that are really missing from the index still make the count exceed the indexed count. Signed-off-by: Rohit Ghumare --- src/functions/observe.ts | 43 ++++++++++++++++-------------- test/observe-session-cap.test.ts | 45 ++++++++++++++++++++++++++++---- 2 files changed, 63 insertions(+), 25 deletions(-) diff --git a/src/functions/observe.ts b/src/functions/observe.ts index e269019f33..a7e165615b 100644 --- a/src/functions/observe.ts +++ b/src/functions/observe.ts @@ -258,7 +258,7 @@ export function registerObserveFunction( const promptRef = durable || typeof payload.eventId !== "string" ? obsId : payload.eventId; return withKeyedLock(`obs:${payload.sessionId}`, async () => { - let existing = await kv.list(KV.observations(payload.sessionId)); + const existing = await kv.list(KV.observations(payload.sessionId)); const stored = durable ? existing.find((o) => o?.id === obsId) : undefined; if (stored) { if (livePrompt) await recordLivePrompt(kv, payload.sessionId, livePrompt, promptRef); @@ -269,24 +269,6 @@ export function registerObserveFunction( recordDedupSkip(); return { deduplicated: true, sessionId: payload.sessionId }; } - if (maxObservationsPerSession && maxObservationsPerSession > 0 && existing.length >= maxObservationsPerSession) { - const { evicted, needed } = await evictForSessionCap( - sdk, - kv, - payload.sessionId, - obsId, - existing, - maxObservationsPerSession, - ); - const evictedIds = new Set(evicted.map((o) => o.id)); - existing = existing.filter((o) => !evictedIds.has(o?.id)); - if (evicted.length < needed) { - return { - success: false, - error: `Session observation limit reached (${maxObservationsPerSession}) and eviction freed ${evicted.length} of ${needed} slots`, - }; - } - } // Existing session is the source of truth for agentId (even // undefined). Env AGENT_ID only fires when no session row @@ -370,6 +352,27 @@ export function registerObserveFunction( throw error; } + let evictedCount = 0; + if (maxObservationsPerSession && maxObservationsPerSession > 0 && existing.length >= maxObservationsPerSession) { + const { evicted, needed } = await evictForSessionCap( + sdk, + kv, + payload.sessionId, + obsId, + existing, + maxObservationsPerSession, + ); + evictedCount = evicted.length; + if (evicted.length < needed) { + logger.warn("Session observation cap eviction freed fewer slots than needed", { + sessionId: payload.sessionId, + cap: maxObservationsPerSession, + evicted: evicted.length, + needed, + }); + } + } + if (dedupMap && dedupHash) { dedupMap.record(dedupHash); } @@ -388,7 +391,7 @@ export function registerObserveFunction( { type: "set", path: "observationCount", - value: (session.observationCount || 0) + 1, + value: Math.max(1, (session.observationCount || 0) + 1 - evictedCount), }, ]; if (session.status !== "active" && finalizeIdleMs > 0) { diff --git a/test/observe-session-cap.test.ts b/test/observe-session-cap.test.ts index 23680e0370..f6ed61df9c 100644 --- a/test/observe-session-cap.test.ts +++ b/test/observe-session-cap.test.ts @@ -212,20 +212,55 @@ describe("session observation cap evicts instead of refusing", () => { expect(removedFromBm25).toEqual(["obs_next"]); }); - it("returns a cap error without writing or throwing when no row can be evicted", async () => { + it("keeps the incoming write and retries eviction later when no row can be evicted", async () => { const { sdk, kv } = await setup(1); await seed(kv, [{ id: "obs_stuck", importance: 1, ts: "2026-01-01T00:00:00.000Z" }]); + const realDelete = kv.delete.bind(kv); kv.delete = async () => { throw new Error("disk busy"); }; const result = await observeOnce(sdk, "cannot fit"); - expect(result.success).toBe(false); - expect(result.observationId).toBeUndefined(); - expect(result.error).toMatch(/^Session observation limit reached \(1\)/); - expect(storedIds(kv)).toEqual(["obs_stuck"]); + expect(typeof result.observationId).toBe("string"); + expect(storedIds(kv).sort()).toEqual([result.observationId!, "obs_stuck"].sort()); expect(removedFromBm25).toHaveLength(0); + + kv.delete = realDelete; + const next = await observeOnce(sdk, "after recovery"); + + expect(storedIds(kv)).toEqual([next.observationId!]); + }); + + it("evicts nothing when the incoming write fails", async () => { + const { sdk, kv } = await setup(1); + await seed(kv, [{ id: "obs_keep", importance: 1, ts: "2026-01-01T00:00:00.000Z" }]); + const realSet = kv.set.bind(kv); + kv.set = (async (scope: string, key: string, value: unknown) => { + if (scope === SCOPE) throw new Error("write failed"); + return realSet(scope, key, value); + }) as typeof kv.set; + + await expect(observeOnce(sdk, "will fail")).rejects.toThrow("write failed"); + + expect(storedIds(kv)).toEqual(["obs_keep"]); + expect(removedFromBm25).toHaveLength(0); + expect(removedFromVector).toHaveLength(0); + }); + + it("keeps the session observation count equal to retained rows after eviction", async () => { + const { sdk, kv } = await setup(2); + await seed(kv, [ + { id: "obs_a", importance: 1, ts: "2026-01-01T00:00:00.000Z" }, + { id: "obs_b", importance: 5, ts: "2026-01-02T00:00:00.000Z" }, + ]); + await kv.set("mem:sessions", SESSION, { id: SESSION, status: "active", observationCount: 2 }); + + await observeOnce(sdk, "third"); + + const session = await kv.get<{ observationCount: number }>("mem:sessions", SESSION); + expect(storedIds(kv)).toHaveLength(2); + expect(session?.observationCount).toBe(2); }); it("keeps the cap when several observations arrive at once", async () => { From 0936756a7a3575051ff168c633ca2cb9ff7a1a53 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Wed, 11 Nov 2026 18:21:29 +0530 Subject: [PATCH 5/5] fix(observe): budget a capped write's source against the retained rows At the session cap, the incoming observation source was budgeted against every existing row, including the rows that eviction removes right after the write. Near the session source limit, this truncated or dropped the new source even when eviction freed enough space. The eviction victims are now selected before budgeting, and the source is budgeted against the rows that remain. Victims are still deleted only after the write succeeds. If a planned victim cannot be deleted, the source is budgeted again against the rows that really remain and the stored row is rewritten. Signed-off-by: Rohit Ghumare --- src/functions/observe.ts | 36 ++++++++++++++++++++++++++----- test/observe-session-cap.test.ts | 37 ++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/src/functions/observe.ts b/src/functions/observe.ts index a7e165615b..82022e86cd 100644 --- a/src/functions/observe.ts +++ b/src/functions/observe.ts @@ -36,6 +36,10 @@ function byEvictionOrder(a: CompressedObservation, b: CompressedObservation): nu return new Date(a.timestamp ?? 0).getTime() - new Date(b.timestamp ?? 0).getTime(); } +function evictionCandidates(existing: CompressedObservation[], incomingId: string): CompressedObservation[] { + return existing.filter((o) => o?.id && o.id !== incomingId).sort(byEvictionOrder); +} + async function evictForSessionCap( sdk: IIIClient, kv: StateKV, @@ -45,7 +49,7 @@ async function evictForSessionCap( cap: number, ): Promise<{ evicted: CompressedObservation[]; needed: number }> { const needed = existing.length - cap + 1; - const candidates = existing.filter((o) => o?.id && o.id !== incomingId).sort(byEvictionOrder); + const candidates = evictionCandidates(existing, incomingId); const evicted: CompressedObservation[] = []; for (const victim of candidates) { if (evicted.length >= needed) break; @@ -317,7 +321,13 @@ export function registerObserveFunction( await requestSummaryRefresh(kv, payload.sessionId, dueAt); if (finalizeIdleMs > 0) await scheduleSessionFinalize(kv, payload.sessionId, dueAt); } - const synthetic = budgetLiveObservationSource(buildSyntheticCompression(raw), existing); + const cap = maxObservationsPerSession && maxObservationsPerSession > 0 ? maxObservationsPerSession : 0; + const plannedVictims = cap > 0 && existing.length >= cap + ? evictionCandidates(existing, obsId).slice(0, existing.length - cap + 1) + : []; + const plannedIds = new Set(plannedVictims.map((o) => o.id)); + const unbudgeted = buildSyntheticCompression(raw); + const synthetic = budgetLiveObservationSource(unbudgeted, existing.filter((o) => !plannedIds.has(o?.id))); if (autoCompress) synthetic.compression = pendingCompression(); try { await kv.set(KV.observations(payload.sessionId), obsId, synthetic); @@ -353,24 +363,40 @@ export function registerObserveFunction( } let evictedCount = 0; - if (maxObservationsPerSession && maxObservationsPerSession > 0 && existing.length >= maxObservationsPerSession) { + if (plannedVictims.length > 0) { const { evicted, needed } = await evictForSessionCap( sdk, kv, payload.sessionId, obsId, existing, - maxObservationsPerSession, + cap, ); evictedCount = evicted.length; if (evicted.length < needed) { logger.warn("Session observation cap eviction freed fewer slots than needed", { sessionId: payload.sessionId, - cap: maxObservationsPerSession, + cap, evicted: evicted.length, needed, }); } + const evictedIds = new Set(evicted.map((o) => o.id)); + if (plannedVictims.some((o) => !evictedIds.has(o.id))) { + const retained = existing.filter((o) => !evictedIds.has(o?.id)); + const rebudgeted = budgetLiveObservationSource(unbudgeted, retained); + if (JSON.stringify(rebudgeted.source) !== JSON.stringify(synthetic.source)) { + if (rebudgeted.source) synthetic.source = rebudgeted.source; + else delete synthetic.source; + await kv.set(KV.observations(payload.sessionId), obsId, synthetic).catch((err) => { + logger.warn("Session cap source re-budget write failed", { + sessionId: payload.sessionId, + obsId, + error: err instanceof Error ? err.message : String(err), + }); + }); + } + } } if (dedupMap && dedupHash) { diff --git a/test/observe-session-cap.test.ts b/test/observe-session-cap.test.ts index f6ed61df9c..50ff9f879c 100644 --- a/test/observe-session-cap.test.ts +++ b/test/observe-session-cap.test.ts @@ -44,6 +44,7 @@ vi.mock("../src/functions/image-refs.js", () => ({ import { mockKV, mockSdk, currentAuditScope } from "./helpers/mocks.js"; import { indexObservationSession, lookupObservationSession } from "../src/state/obs-index.js"; import type { AuditEntry } from "../src/types.js"; +import { SESSION_SOURCE_MAX_BYTES } from "../src/functions/observation-source-budget.js"; const SESSION = "ses_capped"; const SCOPE = `mem:obs:${SESSION}`; @@ -248,6 +249,42 @@ describe("session observation cap evicts instead of refusing", () => { expect(removedFromVector).toHaveLength(0); }); + it("budgets the incoming source against the rows that remain after eviction", async () => { + const { sdk, kv } = await setup(1); + await seed(kv, [{ id: "obs_big", importance: 1, ts: "2026-01-01T00:00:00.000Z" }]); + const big = (await kv.get>(SCOPE, "obs_big"))!; + await kv.set(SCOPE, "obs_big", { + ...big, + source: { hookType: "post_tool_use", toolOutput: "x".repeat(SESSION_SOURCE_MAX_BYTES - 1024) }, + }); + + const result = await observeOnce(sdk, "y".repeat(8 * 1024)); + + expect(storedIds(kv)).toEqual([result.observationId!]); + const stored = await kv.get<{ source?: { truncated?: boolean } }>(SCOPE, result.observationId!); + expect(stored?.source).toBeDefined(); + expect(stored?.source?.truncated).toBe(false); + }); + + it("re-budgets the incoming source when the planned victim cannot be evicted", async () => { + const { sdk, kv } = await setup(1); + await seed(kv, [{ id: "obs_big", importance: 1, ts: "2026-01-01T00:00:00.000Z" }]); + const big = (await kv.get>(SCOPE, "obs_big"))!; + await kv.set(SCOPE, "obs_big", { + ...big, + source: { hookType: "post_tool_use", toolOutput: "x".repeat(SESSION_SOURCE_MAX_BYTES - 1024) }, + }); + kv.delete = async () => { + throw new Error("disk busy"); + }; + + const result = await observeOnce(sdk, "y".repeat(8 * 1024)); + + expect(storedIds(kv).sort()).toEqual([result.observationId!, "obs_big"].sort()); + const stored = await kv.get<{ source?: { truncated?: boolean } }>(SCOPE, result.observationId!); + expect(stored?.source === undefined || stored.source.truncated === true).toBe(true); + }); + it("keeps the session observation count equal to retained rows after eviction", async () => { const { sdk, kv } = await setup(2); await seed(kv, [