From 2951d5932900c4d4bde00f53343887e2338e3897 Mon Sep 17 00:00:00 2001 From: Dmitrii Zhukov Date: Fri, 25 Sep 2026 18:52:21 +0700 Subject: [PATCH 1/2] chore(audit): drop the code comments added in #1182 Requested in the #1182 review: the project keeps comments out of code, and the reasoning already lives in that PR's description. Signed-off-by: Dmitrii Zhukov --- src/state/index-persistence.ts | 7 ------- test/index-persistence.test.ts | 7 ------- 2 files changed, 14 deletions(-) diff --git a/src/state/index-persistence.ts b/src/state/index-persistence.ts index 19b05cb598..c689ef7b8c 100644 --- a/src/state/index-persistence.ts +++ b/src/state/index-persistence.ts @@ -17,13 +17,6 @@ const VECTOR_SHARD_SCOPE_PREFIX = `${KV.bm25Index}:vectors:`; const INDEX_SHARD_KEY = "data"; const DEFAULT_INDEX_SHARD_CHARS = 2_000_000; -// mem:audit exists to record structural deletions of user data — that is -// the policy stated at the top of src/functions/audit.ts. Index shard -// writes and manifest publishes remove no user rows, so they fall outside -// it, yet a single save() emits three of them: on a real store they -// reached 59876 of 84028 entries (71%), which is what makes the audit log -// slow to query and bloats startup. Off by default; set -// AGENTMEMORY_AUDIT_INDEX_PERSIST=1 when debugging index persistence. function auditIndexPersistEnabled(): boolean { const raw = process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST; if (!raw) return false; diff --git a/test/index-persistence.test.ts b/test/index-persistence.test.ts index ba47c3ef72..a7e5550d54 100644 --- a/test/index-persistence.test.ts +++ b/test/index-persistence.test.ts @@ -797,10 +797,6 @@ describe("index_persist audit gating", () => { let previousFlag: string | undefined; beforeEach(() => { - // AGENTMEMORY_* variables are documented as living in - // ~/.agentmemory/.env, so a developer running the suite on a - // configured machine can inherit this one. Clear it going in and put - // whatever was there back on the way out. previousFlag = process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST; delete process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST; vi.useFakeTimers(); @@ -849,9 +845,6 @@ describe("index_persist audit gating", () => { }, ); - // Anything that is not an affirmative stays off. "0" and "false" are the - // ones an operator is likely to reach for to disable it, and they must - // not read as "present, therefore enabled". it.each(["0", "false", "yes", "", " "])( "keeps auditing off when set to %j", async (value) => { From 3affe3d00e40f46d5040466f43a0e15ec1a4417e Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Thu, 1 Oct 2026 13:24:04 +0530 Subject: [PATCH 2/2] test(audit): restore index_persist audit gating coverage Main's index persistence rewrite removed the #1182 comments along with the audit gating tests. Restore the tests without comments against the bucketed storage API: index_persist entries are written on legacy migration only when AGENTMEMORY_AUDIT_INDEX_PERSIST is 1 or true. --- test/index-persistence.test.ts | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/test/index-persistence.test.ts b/test/index-persistence.test.ts index 568d180006..031b6df100 100644 --- a/test/index-persistence.test.ts +++ b/test/index-persistence.test.ts @@ -486,3 +486,60 @@ 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; + } + }); + + function indexPersistEntries(): Array<{ operation: string }> { + const entries: Array<{ operation: string }> = []; + for (const [scope, rows] of kv.store) { + if (!scope.startsWith("mem:audit:") || scope === "mem:audit:months") continue; + for (const row of rows.values()) entries.push(row as { operation: string }); + } + return entries.filter((entry) => entry.operation === "index_persist"); + } + + async function migrateLegacySnapshot(): Promise { + await writeLegacyVectorSnapshot(kv, vectorWith([["obs_a", [0.1, 0.2, 0.3]]])); + const loaded = await new IndexPersistence(kv as never, new VectorIndex(), { bucketSize: 16 }).load(); + expect(loaded.state).toBe("migrated"); + } + + it("writes no index_persist audit entries by default", async () => { + await migrateLegacySnapshot(); + expect(indexPersistEntries()).toEqual([]); + }); + + it.each(["1", " 1 ", "true", "TRUE", " true "])( + "writes index_persist audit entries when set to %j", + async (value) => { + process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST = value; + await migrateLegacySnapshot(); + expect(indexPersistEntries().length).toBeGreaterThan(0); + }, + ); + + it.each(["0", "false", "yes", "", " "])( + "keeps auditing off when set to %j", + async (value) => { + process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST = value; + await migrateLegacySnapshot(); + expect(indexPersistEntries()).toEqual([]); + }, + ); +});