From f90844040450faede1e604a6c78cbc96043a044e Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Thu, 1 Oct 2026 17:04:53 +0530 Subject: [PATCH] test(audit): restore the index_persist audit gating coverage The bucketed index rewrite (#1416) replaced test/index-persistence.test.ts and dropped the gating suite that #1182 added. Bring it back against the current storage: index_persist rows now come from the legacy migration and its cleanup deletes, and audit rows land in the month scope. The suite clears AGENTMEMORY_AUDIT_INDEX_PERSIST going in and restores it on the way out, then checks that auditing is off by default, that trimmed and case-insensitive "1" and "true" turn it on (with migrate and delete rows), that "0", "false", "yes" and blank values keep it off, and that the index still migrates and reloads from buckets when auditing is off. Co-authored-by: Dmitrii Zhukov --- test/index-persistence.test.ts | 80 ++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/test/index-persistence.test.ts b/test/index-persistence.test.ts index 568d180006..e80327bde5 100644 --- a/test/index-persistence.test.ts +++ b/test/index-persistence.test.ts @@ -3,6 +3,7 @@ import { IndexPersistence, vectorBucketScope } from "../src/state/index-persiste import { SearchIndex } from "../src/state/search-index.js"; import { VectorIndex } from "../src/state/vector-index.js"; import type { CompressedObservation } from "../src/types.js"; +import { currentAuditScope } from "./helpers/mocks.js"; const INDEX_SCOPE = "mem:index:bm25"; const META_KEY = "vectors:meta"; @@ -486,3 +487,82 @@ describe("IndexPersistence scheduling", () => { expect(await kv.get(INDEX_SCOPE, META_KEY)).toBeNull(); }); }); + +describe("index_persist audit gating", () => { + let kv: MockKV; + let previousFlag: string | undefined; + + beforeEach(() => { + previousFlag = process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST; + delete process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST; + kv = mockKV(); + }); + + afterEach(() => { + if (previousFlag === undefined) { + delete process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST; + } else { + process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST = previousFlag; + } + }); + + async function migrateLegacySnapshot(): Promise { + const legacy = vectorWith([ + ["obs_a", [0.1, 0.2, 0.3]], + ["obs_b", [0.4, 0.5, 0.6]], + ]); + await writeLegacyVectorSnapshot(kv, legacy); + await writeLegacyBm25Snapshot(kv); + const loaded = await new IndexPersistence(kv as never, new VectorIndex(), { bucketSize: 16 }).load(); + expect(loaded.state).toBe("migrated"); + return legacy; + } + + async function indexPersistEntries(): Promise> { + const entries = await kv.list<{ operation: string; details: { action?: string } }>(currentAuditScope()); + return entries.filter((entry) => entry.operation === "index_persist"); + } + + it("writes no index_persist audit entries by default", async () => { + await migrateLegacySnapshot(); + + expect(await indexPersistEntries()).toEqual([]); + }); + + it.each(["1", " 1 ", "true", "TRUE", " true "])( + "writes index_persist audit entries to the current month scope when set to %j", + async (value) => { + process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST = value; + + await migrateLegacySnapshot(); + + const entries = await indexPersistEntries(); + expect(entries.length).toBeGreaterThan(0); + const actions = new Set(entries.map((entry) => entry.details.action)); + expect(actions.has("migrate")).toBe(true); + expect(actions.has("delete")).toBe(true); + }, + ); + + it.each(["0", "false", "yes", "", " "])( + "keeps auditing off when set to %j", + async (value) => { + process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST = value; + + await migrateLegacySnapshot(); + + expect(await indexPersistEntries()).toEqual([]); + }, + ); + + it("still persists the index when auditing is off", async () => { + const legacy = await migrateLegacySnapshot(); + + const reloaded = await new IndexPersistence(kv as never, new VectorIndex(), { bucketSize: 16 }).load(); + + expect(reloaded.state).toBe("buckets"); + expectSameVectors(reloaded.vector, legacy); + expect(await kv.get(INDEX_SCOPE, "vectors:manifest")).toBeNull(); + expect(await indexPersistEntries()).toEqual([]); + }); +});