From f107b96f9ad2e52ecd32adbdecd783e2aa2afd72 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Fri, 25 Sep 2026 20:14:28 +0530 Subject: [PATCH 1/6] perf(audit): shard mem:audit into month scopes so writes stop paying for the whole log The iii file store rewrites a whole scope on every flush when any key in it changed, and the audit log lived in one scope (mem:audit) forever, so a mature store paid O(log-size) disk I/O on every audited operation. One store had 583k rows, 534k of them index_persist noise written before that kind of row became opt-in. recordAudit now writes into a UTC month scope (mem:audit:YYYY-MM) derived from the entry timestamp, with a small index key (mem:audit:months) tracking which month scopes exist so readers never have to list_groups. queryAudit, and everything that goes through it (the memory_audit MCP tool, GET /agentmemory/audit, the viewer audit tab, mem::heal, diagnostics), reads month scopes newest-first and stops once it has the requested limit, falling back to the legacy mem:audit scope only if it still needs more. A background migration runs at boot, bounded to 200 rows per tick, moving legacy rows into their month scopes and dropping legacy index_persist rows outright (system noise, not deletion records); it records one summary audit row for the purge and leaves every other row alone. It resumes on the next boot if interrupted, since queryAudit's legacy fallback keeps unmigrated rows readable in the meantime. AGENTMEMORY_AUDIT_RETENTION_MONTHS (default 0, keep forever) adds a daily sweep that drops whole month scopes older than N months; off by default so no install loses audit history without asking for it. --- .env.example | 1 + plugin/skills/agentmemory-config/REFERENCE.md | 3 +- src/config.ts | 4 + src/functions/audit.ts | 200 +++++++++++- src/functions/retention.ts | 21 +- src/index.ts | 16 + src/state/schema.ts | 2 + src/types.ts | 3 +- test/audit-retention.test.ts | 299 ++++++++++++++++++ test/consolidation-pipeline.test.ts | 3 +- test/evict.test.ts | 3 +- test/helpers/mocks.ts | 5 + test/index-persistence.test.ts | 3 +- test/lessons.test.ts | 3 +- test/remember-forget-audit.test.ts | 9 +- test/retention.test.ts | 7 +- test/snapshot.test.ts | 3 +- 17 files changed, 555 insertions(+), 30 deletions(-) create mode 100644 test/audit-retention.test.ts diff --git a/.env.example b/.env.example index 9219a338a1..4f9181b888 100644 --- a/.env.example +++ b/.env.example @@ -129,6 +129,7 @@ # AGENTMEMORY_REFLECT=true # Periodically auto-synthesize lessons from memories # AGENTMEMORY_DROP_STALE_INDEX=true # Drop on-disk BM25 / vector index on startup if dim guard fires (#248). Recovery toggle for stuck-state debugging. # AGENTMEMORY_IMAGE_EMBEDDINGS=true # Enable image embeddings when an image provider is present (experimental). +# AGENTMEMORY_AUDIT_RETENTION_MONTHS=6 # Drop whole audit-log month scopes older than N months on a daily sweep. Default 0 = keep audit history forever. # ----------------------------------------------------------------------------- # 6. CLI / runtime knobs diff --git a/plugin/skills/agentmemory-config/REFERENCE.md b/plugin/skills/agentmemory-config/REFERENCE.md index d152a7c62b..32794d5174 100644 --- a/plugin/skills/agentmemory-config/REFERENCE.md +++ b/plugin/skills/agentmemory-config/REFERENCE.md @@ -3,11 +3,12 @@ Generated by scanning `src/` for `AGENTMEMORY_*` usage. Do not edit the block below by hand; run `npm run skills:gen` after adding or removing a variable. Internal markers ending in two underscores are excluded. -Configuration is read from the environment and from `~/.agentmemory/.env` (no `export` prefix). 43 recognized variables: +Configuration is read from the environment and from `~/.agentmemory/.env` (no `export` prefix). 44 recognized variables: - `AGENTMEMORY_AGENT_SCOPE` - `AGENTMEMORY_ALLOW_AGENT_SDK` - `AGENTMEMORY_AUDIT_INDEX_PERSIST` +- `AGENTMEMORY_AUDIT_RETENTION_MONTHS` - `AGENTMEMORY_AUTO_COMPRESS` - `AGENTMEMORY_COMMIT_SHA` - `AGENTMEMORY_CONSOLIDATION_COOLDOWN_MS` diff --git a/src/config.ts b/src/config.ts index a6c62ce55e..0f2e3fce2d 100644 --- a/src/config.ts +++ b/src/config.ts @@ -235,6 +235,10 @@ export function isDropStaleIndexEnabled(): boolean { return getMergedEnv()["AGENTMEMORY_DROP_STALE_INDEX"] === "true"; } +export function getAuditRetentionMonths(): number { + return safeParseInt(getMergedEnv()["AGENTMEMORY_AUDIT_RETENTION_MONTHS"], 0); +} + export function detectLlmProviderKind(): "llm" | "noop" { const env = getMergedEnv(); if ( diff --git a/src/functions/audit.ts b/src/functions/audit.ts index 1e75b4b7d2..5a7b86e050 100644 --- a/src/functions/audit.ts +++ b/src/functions/audit.ts @@ -31,6 +31,57 @@ import { logger } from "../logger.js"; // When adding a new deletion path, add an explicit recordAudit call // BEFORE kv.delete(...) and match one of the two shapes above. +const AUDIT_MIGRATE_FUNCTION_ID = "mem::audit-migrate"; +const AUDIT_MIGRATION_BATCH_SIZE = 200; +const AUDIT_MIGRATION_TICK_MS = 50; + +interface AuditMonthIndex { + months: string[]; +} + +export function auditMonthOf(timestamp: string): string { + const parsed = new Date(timestamp); + const iso = Number.isNaN(parsed.getTime()) + ? new Date().toISOString() + : parsed.toISOString(); + return iso.slice(0, 7); +} + +function monthStartMs(month: string): number { + const [year, mon] = month.split("-").map(Number); + return Date.UTC(year, mon - 1, 1); +} + +function monthEndMs(month: string): number { + const [year, mon] = month.split("-").map(Number); + return Date.UTC(year, mon, 1) - 1; +} + +function monthNMonthsAgo(count: number): string { + const now = new Date(); + const cutoff = new Date( + Date.UTC(now.getUTCFullYear(), now.getUTCMonth() - count, 1), + ); + return cutoff.toISOString().slice(0, 7); +} + +async function readAuditMonthIndex(kv: StateKV): Promise { + const index = await kv.get(KV.auditMonths, "index"); + return index?.months ?? []; +} + +async function markAuditMonth(kv: StateKV, month: string): Promise { + const months = await readAuditMonthIndex(kv); + if (months.includes(month)) return; + const next = [...months, month].sort(); + await kv.set(KV.auditMonths, "index", { months: next }); +} + +export async function listAuditMonthsDesc(kv: StateKV): Promise { + const months = await readAuditMonthIndex(kv); + return [...months].sort().reverse(); +} + export async function recordAudit( kv: StateKV, operation: AuditEntry["operation"], @@ -50,7 +101,9 @@ export async function recordAudit( details, qualityScore, }; - await kv.set(KV.audit, entry.id, entry); + const month = auditMonthOf(entry.timestamp); + await kv.set(KV.auditMonth(month), entry.id, entry); + await markAuditMonth(kv, month); return entry; } @@ -86,28 +139,145 @@ export async function queryAudit( limit?: number; }, ): Promise { - const all = await kv.list(KV.audit); - let entries = [...all].sort( - (a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime(), - ); + const limit = filter?.limit || 100; - if (filter?.operation) { - entries = entries.filter((e) => e.operation === filter.operation); - } + let fromMs: number | undefined; if (filter?.dateFrom) { - const from = new Date(filter.dateFrom).getTime(); - if (Number.isNaN(from)) { + fromMs = new Date(filter.dateFrom).getTime(); + if (Number.isNaN(fromMs)) { throw new Error(`Invalid dateFrom: ${filter.dateFrom}`); } - entries = entries.filter((e) => new Date(e.timestamp).getTime() >= from); } + + let toMs: number | undefined; if (filter?.dateTo) { - const to = new Date(filter.dateTo).getTime(); - if (Number.isNaN(to)) { + toMs = new Date(filter.dateTo).getTime(); + if (Number.isNaN(toMs)) { throw new Error(`Invalid dateTo: ${filter.dateTo}`); } - entries = entries.filter((e) => new Date(e.timestamp).getTime() <= to); } - return entries.slice(0, filter?.limit || 100); + const matches = (entry: AuditEntry): boolean => { + if (filter?.operation && entry.operation !== filter.operation) return false; + const t = new Date(entry.timestamp).getTime(); + if (fromMs !== undefined && t < fromMs) return false; + if (toMs !== undefined && t > toMs) return false; + return true; + }; + + const collected: AuditEntry[] = []; + const months = await listAuditMonthsDesc(kv); + for (const month of months) { + if (collected.length >= limit) break; + if (toMs !== undefined && monthStartMs(month) > toMs) continue; + if (fromMs !== undefined && monthEndMs(month) < fromMs) break; + const rows = await kv.list(KV.auditMonth(month)); + for (const row of rows) { + if (matches(row)) collected.push(row); + } + } + + if (collected.length < limit) { + const legacyRows = await kv.list(KV.audit); + for (const row of legacyRows) { + if (matches(row)) collected.push(row); + } + } + + collected.sort( + (a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime(), + ); + return collected.slice(0, limit); +} + +async function migrateAuditEntry( + kv: StateKV, + entry: AuditEntry, +): Promise<"migrated" | "purged"> { + if (entry.operation === "index_persist") { + await kv.delete(KV.audit, entry.id); + return "purged"; + } + const month = auditMonthOf(entry.timestamp); + await kv.set(KV.auditMonth(month), entry.id, entry); + await markAuditMonth(kv, month); + await kv.delete(KV.audit, entry.id); + return "migrated"; +} + +function delay(ms: number): Promise { + return new Promise((resolve) => { + const timer = setTimeout(resolve, ms); + timer.unref(); + }); +} + +export async function startAuditMigration(kv: StateKV): Promise { + let totalMigrated = 0; + let totalPurged = 0; + + try { + const queue = await kv.list(KV.audit); + let cursor = 0; + + while (cursor < queue.length) { + const batch = queue.slice(cursor, cursor + AUDIT_MIGRATION_BATCH_SIZE); + cursor += batch.length; + + for (const entry of batch) { + const outcome = await migrateAuditEntry(kv, entry); + if (outcome === "purged") totalPurged++; + else totalMigrated++; + } + + if (cursor < queue.length) { + await delay(AUDIT_MIGRATION_TICK_MS); + } + } + } catch (err) { + logger.warn("audit migration failed", { + error: err instanceof Error ? err.message : String(err), + migrated: totalMigrated, + purged: totalPurged, + }); + return; + } + + if (totalPurged > 0) { + await safeAudit(kv, "audit_migrate", AUDIT_MIGRATE_FUNCTION_ID, [], { + reason: "index_persist rows predate opt-in auditing", + purged: totalPurged, + migrated: totalMigrated, + }); + } +} + +export async function runAuditRetentionSweep( + kv: StateKV, + retentionMonths: number, +): Promise<{ droppedMonths: string[]; droppedRows: number }> { + if (!Number.isFinite(retentionMonths) || retentionMonths <= 0) { + return { droppedMonths: [], droppedRows: 0 }; + } + + const months = await readAuditMonthIndex(kv); + const cutoff = monthNMonthsAgo(retentionMonths); + const toDrop = months.filter((month) => month < cutoff).sort(); + if (toDrop.length === 0) { + return { droppedMonths: [], droppedRows: 0 }; + } + + let droppedRows = 0; + for (const month of toDrop) { + const rows = await kv.list(KV.auditMonth(month)); + for (const row of rows) { + await kv.delete(KV.auditMonth(month), row.id); + droppedRows++; + } + } + + const remaining = months.filter((month) => !toDrop.includes(month)).sort(); + await kv.set(KV.auditMonths, "index", { months: remaining }); + + return { droppedMonths: toDrop, droppedRows }; } diff --git a/src/functions/retention.ts b/src/functions/retention.ts index fac16796f1..b718cbf50c 100644 --- a/src/functions/retention.ts +++ b/src/functions/retention.ts @@ -13,9 +13,10 @@ import { deleteAccessLog, normalizeAccessLog, } from "./access-tracker.js"; -import { recordAudit } from "./audit.js"; +import { recordAudit, runAuditRetentionSweep } from "./audit.js"; import { getSearchIndex, vectorIndexRemove, flushIndexSave } from "./search.js"; import { logger } from "../logger.js"; +import { getAuditRetentionMonths } from "../config.js"; const DEFAULT_DECAY: DecayConfig = { lambda: 0.01, @@ -409,4 +410,22 @@ export function registerRetentionFunctions( return { success: true, evicted, evictedEpisodic, evictedSemantic }; }, ); + + sdk.registerFunction("mem::audit-retention-sweep", + async (data?: { retentionMonths?: number }) => { + const retentionMonths = + typeof data?.retentionMonths === "number" && + Number.isFinite(data.retentionMonths) + ? data.retentionMonths + : getAuditRetentionMonths(); + + const result = await runAuditRetentionSweep(kv, retentionMonths); + + if (result.droppedRows > 0) { + logger.info("Audit retention sweep dropped old month scopes", result); + } + + return { success: true, ...result }; + }, + ); } diff --git a/src/index.ts b/src/index.ts index ff465afe46..ee153e7092 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,7 @@ import { isConsolidationEnabled, isContextInjectionEnabled, isDropStaleIndexEnabled, + getAuditRetentionMonths, } from "./config.js"; import { createProvider, @@ -91,6 +92,7 @@ import { registerSlidingWindowFunction } from "./functions/sliding-window.js"; import { registerQueryExpansionFunction } from "./functions/query-expansion.js"; import { registerTemporalGraphFunctions } from "./functions/temporal-graph.js"; import { registerRetentionFunctions } from "./functions/retention.js"; +import { startAuditMigration } from "./functions/audit.js"; import { registerCompressFileFunction } from "./functions/compress-file.js"; import { registerReplayFunctions } from "./functions/replay.js"; import { registerApiTriggers } from "./triggers/api.js"; @@ -336,6 +338,7 @@ async function main() { registerRetentionFunctions(sdk, kv); registerCompressFileFunction(sdk, kv, provider); registerReplayFunctions(sdk, kv); + void startAuditMigration(kv); bootLog( `v0.6 advanced retrieval: sliding-window, query-expansion, temporal-graph, retention-scoring`, ); @@ -576,6 +579,19 @@ async function main() { bootLog(`Auto-forget: enabled (every ${autoForgetIntervalMs / 60000}m)`); } + const auditRetentionMonths = getAuditRetentionMonths(); + if (auditRetentionMonths > 0) { + const auditRetentionTimer = setInterval(async () => { + try { + await sdk.trigger({ function_id: "mem::audit-retention-sweep", payload: {} }); + } catch {} + }, 86400000); + auditRetentionTimer.unref(); + bootLog( + `Audit retention sweep: enabled (drop month scopes older than ${auditRetentionMonths}mo, every 24h)`, + ); + } + if (process.env.LESSON_DECAY_ENABLED !== "false") { const lessonDecayTimer = setInterval(async () => { try { diff --git a/src/state/schema.ts b/src/state/schema.ts index 773f147ce6..2b9240404c 100644 --- a/src/state/schema.ts +++ b/src/state/schema.ts @@ -45,6 +45,8 @@ export const KV = { `mem:team:${teamId}:users:${userId}`, teamProfile: (teamId: string) => `mem:team:${teamId}:profile`, audit: "mem:audit", + auditMonth: (month: string) => `mem:audit:${month}`, + auditMonths: "mem:audit:months", actions: "mem:actions", actionEdges: "mem:action-edges", leases: "mem:leases", diff --git a/src/types.ts b/src/types.ts index 38b385f0aa..ef65eea7dd 100644 --- a/src/types.ts +++ b/src/types.ts @@ -631,7 +631,8 @@ export interface AuditEntry { | "slot_replace" | "slot_create" | "slot_delete" - | "slot_reflect"; + | "slot_reflect" + | "audit_migrate"; userId?: string; functionId: string; targetIds: string[]; diff --git a/test/audit-retention.test.ts b/test/audit-retention.test.ts new file mode 100644 index 0000000000..1817cb54d4 --- /dev/null +++ b/test/audit-retention.test.ts @@ -0,0 +1,299 @@ +import { describe, it, expect, beforeEach, vi } from "vitest"; + +vi.mock("../src/logger.js", () => ({ + logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, +})); + +import { + recordAudit, + queryAudit, + auditMonthOf, + listAuditMonthsDesc, + startAuditMigration, + runAuditRetentionSweep, +} from "../src/functions/audit.js"; +import { KV } from "../src/state/schema.js"; +import { mockKV } from "./helpers/mocks.js"; +import { getAuditRetentionMonths } from "../src/config.js"; +import type { AuditEntry } from "../src/types.js"; + +function seedEntry( + kv: ReturnType, + scope: string, + timestamp: string, + overrides: Partial = {}, +): AuditEntry { + const entry: AuditEntry = { + id: overrides.id ?? `aud_${Math.random().toString(36).slice(2)}`, + timestamp, + operation: overrides.operation ?? "delete", + functionId: overrides.functionId ?? "mem::test", + targetIds: overrides.targetIds ?? [], + details: overrides.details ?? {}, + userId: overrides.userId, + qualityScore: overrides.qualityScore, + }; + kv.store.set( + scope, + (kv.store.get(scope) ?? new Map()).set(entry.id, entry), + ); + return entry; +} + +async function seedMonthIndex( + kv: ReturnType, + months: string[], +): Promise { + await kv.set(KV.auditMonths, "index", { months }); +} + +describe("audit month buckets", () => { + let kv: ReturnType; + + beforeEach(() => { + kv = mockKV(); + }); + + it("recordAudit writes into the UTC month scope of the entry timestamp", async () => { + const entry = await recordAudit(kv as never, "delete", "mem::test", ["a"]); + const month = auditMonthOf(entry.timestamp); + + const rows = await kv.list(KV.auditMonth(month)); + expect(rows).toHaveLength(1); + expect(rows[0].id).toBe(entry.id); + + const legacy = await kv.list(KV.audit); + expect(legacy).toHaveLength(0); + }); + + it("recordAudit registers the month in the month index exactly once", async () => { + await recordAudit(kv as never, "delete", "mem::test", ["a"]); + await recordAudit(kv as never, "delete", "mem::test", ["b"]); + + const months = await listAuditMonthsDesc(kv as never); + expect(months).toHaveLength(1); + expect(months[0]).toBe(auditMonthOf(new Date().toISOString())); + }); + + it("auditMonthOf derives the month in UTC", () => { + expect(auditMonthOf("2026-01-31T23:30:00.000Z")).toBe("2026-01"); + expect(auditMonthOf("2026-02-01T00:00:00.000Z")).toBe("2026-02"); + }); +}); + +describe("queryAudit across months", () => { + let kv: ReturnType; + + beforeEach(() => { + kv = mockKV(); + }); + + it("reads the newest month first and stops once the limit is reached", async () => { + await seedMonthIndex(kv, ["2026-06", "2026-07", "2026-08"]); + seedEntry(kv, KV.auditMonth("2026-06"), "2026-06-15T00:00:00.000Z", { + id: "june_1", + }); + seedEntry(kv, KV.auditMonth("2026-07"), "2026-07-15T00:00:00.000Z", { + id: "july_1", + }); + seedEntry(kv, KV.auditMonth("2026-08"), "2026-08-01T00:00:00.000Z", { + id: "aug_1", + }); + seedEntry(kv, KV.auditMonth("2026-08"), "2026-08-02T00:00:00.000Z", { + id: "aug_2", + }); + + const listSpy = vi.spyOn(kv, "list"); + const entries = await queryAudit(kv as never, { limit: 2 }); + + expect(entries.map((e) => e.id).sort()).toEqual(["aug_1", "aug_2"]); + const listedScopes = listSpy.mock.calls.map((call) => call[0]); + expect(listedScopes).toContain(KV.auditMonth("2026-08")); + expect(listedScopes).not.toContain(KV.auditMonth("2026-07")); + expect(listedScopes).not.toContain(KV.auditMonth("2026-06")); + }); + + it("falls back to legacy rows once month scopes are exhausted", async () => { + await seedMonthIndex(kv, ["2026-08"]); + seedEntry(kv, KV.auditMonth("2026-08"), "2026-08-01T00:00:00.000Z", { + id: "aug_1", + }); + seedEntry(kv, KV.audit, "2026-05-01T00:00:00.000Z", { id: "legacy_1" }); + + const entries = await queryAudit(kv as never, { limit: 5 }); + expect(entries.map((e) => e.id).sort()).toEqual(["aug_1", "legacy_1"]); + }); + + it("never falls back to legacy once the month scopes already satisfy the limit", async () => { + await seedMonthIndex(kv, ["2026-08"]); + seedEntry(kv, KV.auditMonth("2026-08"), "2026-08-01T00:00:00.000Z", { + id: "aug_1", + }); + seedEntry(kv, KV.audit, "2026-05-01T00:00:00.000Z", { id: "legacy_1" }); + + const listSpy = vi.spyOn(kv, "list"); + const entries = await queryAudit(kv as never, { limit: 1 }); + + expect(entries.map((e) => e.id)).toEqual(["aug_1"]); + const listedScopes = listSpy.mock.calls.map((call) => call[0]); + expect(listedScopes).not.toContain(KV.audit); + }); + + it("respects operation and date filters across months", async () => { + await seedMonthIndex(kv, ["2026-07", "2026-08"]); + seedEntry(kv, KV.auditMonth("2026-07"), "2026-07-10T00:00:00.000Z", { + id: "july_delete", + operation: "delete", + }); + seedEntry(kv, KV.auditMonth("2026-08"), "2026-08-10T00:00:00.000Z", { + id: "aug_forget", + operation: "forget", + }); + + const entries = await queryAudit(kv as never, { + operation: "delete", + limit: 10, + }); + expect(entries.map((e) => e.id)).toEqual(["july_delete"]); + }); +}); + +describe("startAuditMigration", () => { + let kv: ReturnType; + + beforeEach(() => { + kv = mockKV(); + }); + + it("moves legacy rows into their month scopes and purges index_persist noise", async () => { + seedEntry(kv, KV.audit, "2020-03-01T00:00:00.000Z", { + id: "keep_1", + operation: "delete", + targetIds: ["mem_a"], + }); + seedEntry(kv, KV.audit, "2020-04-01T00:00:00.000Z", { + id: "keep_2", + operation: "forget", + targetIds: ["mem_b"], + }); + seedEntry(kv, KV.audit, "2020-03-05T00:00:00.000Z", { + id: "noise_1", + operation: "index_persist", + }); + seedEntry(kv, KV.audit, "2020-03-06T00:00:00.000Z", { + id: "noise_2", + operation: "index_persist", + }); + + await startAuditMigration(kv as never); + + const legacyRemaining = await kv.list(KV.audit); + expect(legacyRemaining).toHaveLength(0); + + const marchRows = await kv.list(KV.auditMonth("2020-03")); + expect(marchRows.map((r) => r.id)).toEqual(["keep_1"]); + + const aprilRows = await kv.list(KV.auditMonth("2020-04")); + expect(aprilRows.map((r) => r.id)).toEqual(["keep_2"]); + + const months = await listAuditMonthsDesc(kv as never); + expect(months).toContain("2020-03"); + expect(months).toContain("2020-04"); + + const currentMonth = auditMonthOf(new Date().toISOString()); + const currentMonthRows = await kv.list( + KV.auditMonth(currentMonth), + ); + const summaryRows = currentMonthRows.filter( + (r) => r.operation === "audit_migrate", + ); + expect(summaryRows).toHaveLength(1); + expect(summaryRows[0].details.purged).toBe(2); + expect(summaryRows[0].details.migrated).toBe(2); + }); + + it("records no summary row when there is nothing to purge", async () => { + seedEntry(kv, KV.audit, "2020-03-01T00:00:00.000Z", { + id: "keep_1", + operation: "delete", + }); + + await startAuditMigration(kv as never); + + const currentMonth = auditMonthOf(new Date().toISOString()); + const currentMonthRows = await kv.list( + KV.auditMonth(currentMonth), + ); + expect( + currentMonthRows.filter((r) => r.operation === "audit_migrate"), + ).toHaveLength(0); + }); + + it("is a no-op on a store with no legacy audit rows", async () => { + await startAuditMigration(kv as never); + const months = await listAuditMonthsDesc(kv as never); + expect(months).toHaveLength(0); + }); +}); + +describe("audit retention sweep", () => { + let kv: ReturnType; + + beforeEach(() => { + kv = mockKV(); + }); + + it("defaults to 0 (keep forever) when unset", () => { + expect(getAuditRetentionMonths()).toBe(0); + }); + + it("drops nothing when retentionMonths is 0", async () => { + await seedMonthIndex(kv, ["2020-01"]); + seedEntry(kv, KV.auditMonth("2020-01"), "2020-01-01T00:00:00.000Z"); + + const result = await runAuditRetentionSweep(kv as never, 0); + expect(result).toEqual({ droppedMonths: [], droppedRows: 0 }); + + const months = await listAuditMonthsDesc(kv as never); + expect(months).toEqual(["2020-01"]); + }); + + it("drops whole month scopes older than the retention window", async () => { + const now = new Date(); + const oldMonth = new Date( + Date.UTC(now.getUTCFullYear(), now.getUTCMonth() - 13, 1), + ) + .toISOString() + .slice(0, 7); + const recentMonth = new Date( + Date.UTC(now.getUTCFullYear(), now.getUTCMonth() - 1, 1), + ) + .toISOString() + .slice(0, 7); + + await seedMonthIndex(kv, [oldMonth, recentMonth]); + seedEntry(kv, KV.auditMonth(oldMonth), `${oldMonth}-05T00:00:00.000Z`, { + id: "old_1", + }); + seedEntry( + kv, + KV.auditMonth(recentMonth), + `${recentMonth}-05T00:00:00.000Z`, + { id: "recent_1" }, + ); + + const result = await runAuditRetentionSweep(kv as never, 12); + + expect(result.droppedMonths).toEqual([oldMonth]); + expect(result.droppedRows).toBe(1); + + const remainingMonths = await listAuditMonthsDesc(kv as never); + expect(remainingMonths).toEqual([recentMonth]); + + const oldRows = await kv.list(KV.auditMonth(oldMonth)); + expect(oldRows).toHaveLength(0); + + const recentRows = await kv.list(KV.auditMonth(recentMonth)); + expect(recentRows).toHaveLength(1); + }); +}); diff --git a/test/consolidation-pipeline.test.ts b/test/consolidation-pipeline.test.ts index 5f72090ced..5d43b885b5 100644 --- a/test/consolidation-pipeline.test.ts +++ b/test/consolidation-pipeline.test.ts @@ -11,6 +11,7 @@ vi.mock("../src/config.js", () => ({ import { registerConsolidationPipelineFunction } from "../src/functions/consolidation-pipeline.js"; import { isConsolidationEnabled } from "../src/config.js"; +import { currentAuditScope } from "./helpers/mocks.js"; import type { SessionSummary, Memory, SemanticMemory, ProceduralMemory } from "../src/types.js"; function mockKV() { @@ -206,7 +207,7 @@ describe("Consolidation Pipeline", () => { await sdk.trigger("mem::consolidate-pipeline", { tier: "semantic" }); - const audits = await kv.list("mem:audit"); + const audits = await kv.list(currentAuditScope()); expect(audits.length).toBe(1); }); diff --git a/test/evict.test.ts b/test/evict.test.ts index 690e8a7c1f..6e8b3868c3 100644 --- a/test/evict.test.ts +++ b/test/evict.test.ts @@ -6,6 +6,7 @@ import type { } from "../src/types.js"; import { registerEvictFunction } from "../src/functions/evict.js"; import { KV } from "../src/state/schema.js"; +import { currentAuditScope } from "./helpers/mocks.js"; vi.mock("../src/logger.js", () => ({ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, @@ -155,7 +156,7 @@ describe("mem::evict stale sessions", () => { expect(await kv.get(KV.sessions, sessionId)).toBeNull(); const audits = await kv.list<{ details: { reason: string }; - }>(KV.audit); + }>(currentAuditScope()); expect(audits[0].details.reason).toBe( "stale_session_recovered_then_evicted", ); diff --git a/test/helpers/mocks.ts b/test/helpers/mocks.ts index bc829ed5f6..7b3fd335c9 100644 --- a/test/helpers/mocks.ts +++ b/test/helpers/mocks.ts @@ -1,7 +1,12 @@ import { vi } from "vitest"; +import { KV } from "../../src/state/schema.js"; type Handler = (data: unknown) => Promise; +export function currentAuditScope(): string { + return KV.auditMonth(new Date().toISOString().slice(0, 7)); +} + export function mockKV() { const store = new Map>(); return { diff --git a/test/index-persistence.test.ts b/test/index-persistence.test.ts index ba47c3ef72..57d47e8b83 100644 --- a/test/index-persistence.test.ts +++ b/test/index-persistence.test.ts @@ -2,6 +2,7 @@ import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; import { IndexPersistence } from "../src/state/index-persistence.js"; import { SearchIndex } from "../src/state/search-index.js"; import { VectorIndex } from "../src/state/vector-index.js"; +import { currentAuditScope } from "./helpers/mocks.js"; import type { CompressedObservation } from "../src/types.js"; const BM25_SCOPE = "mem:index:bm25"; @@ -817,7 +818,7 @@ describe("index_persist audit gating", () => { }); async function indexPersistEntries(): Promise> { - const entries = await kv.list<{ operation: string }>("mem:audit"); + const entries = await kv.list<{ operation: string }>(currentAuditScope()); return entries.filter((entry) => entry.operation === "index_persist"); } diff --git a/test/lessons.test.ts b/test/lessons.test.ts index b55fb5976b..350bd9de41 100644 --- a/test/lessons.test.ts +++ b/test/lessons.test.ts @@ -5,6 +5,7 @@ vi.mock("../src/logger.js", () => ({ })); import { registerLessonsFunctions } from "../src/functions/lessons.js"; +import { currentAuditScope } from "./helpers/mocks.js"; import type { Lesson } from "../src/types.js"; function mockKV() { @@ -443,7 +444,7 @@ describe("Lessons", () => { await sdk.trigger("mem::lesson-delete", { lessonId: saved.lesson.id }); - const auditRows = (await kv.list("mem:audit")) as Array<{ + const auditRows = (await kv.list(currentAuditScope())) as Array<{ operation: string; targetIds: string[]; }>; diff --git a/test/remember-forget-audit.test.ts b/test/remember-forget-audit.test.ts index f875b7c071..f87ae47451 100644 --- a/test/remember-forget-audit.test.ts +++ b/test/remember-forget-audit.test.ts @@ -14,6 +14,7 @@ import { setIndexPersistence, } from "../src/functions/search.js"; import { memoryToObservation } from "../src/state/memory-utils.js"; +import { currentAuditScope } from "./helpers/mocks.js"; import type { Memory } from "../src/types.js"; function mockKV() { @@ -70,7 +71,7 @@ describe("mem::forget audit coverage (issue #125)", () => { functionId: string; targetIds: string[]; details: Record; - }>("mem:audit"); + }>(currentAuditScope()); expect(auditRows).toHaveLength(1); const [row] = auditRows; expect(row.operation).toBe("forget"); @@ -99,7 +100,7 @@ describe("mem::forget audit coverage (issue #125)", () => { const auditRows = await kv.list<{ targetIds: string[]; details: Record; - }>("mem:audit"); + }>(currentAuditScope()); expect(auditRows).toHaveLength(1); const [row] = auditRows; expect([...row.targetIds].sort()).toEqual(["obs_a", "obs_b"]); @@ -119,7 +120,7 @@ describe("mem::forget audit coverage (issue #125)", () => { payload: { sessionId: undefined, memoryId: undefined }, }); - const auditRows = await kv.list("mem:audit"); + const auditRows = await kv.list(currentAuditScope()); expect(auditRows).toHaveLength(0); }); @@ -153,7 +154,7 @@ describe("mem::forget audit coverage (issue #125)", () => { payload: { memoryId: "lsn_4f9cb07017a7c8ac" }, }); - const auditRows = await kv.list("mem:audit"); + const auditRows = await kv.list(currentAuditScope()); expect(auditRows).toHaveLength(0); }); }); diff --git a/test/retention.test.ts b/test/retention.test.ts index cf8cc681db..d71112e48a 100644 --- a/test/retention.test.ts +++ b/test/retention.test.ts @@ -4,6 +4,7 @@ import { setIndexPersistence, } from "../src/functions/search.js"; import { memoryToObservation } from "../src/state/memory-utils.js"; +import { currentAuditScope } from "./helpers/mocks.js"; import type { Memory, SemanticMemory } from "../src/types.js"; vi.mock("../src/logger.js", () => ({ @@ -358,7 +359,7 @@ describe("RetentionScoring", () => { functionId: string; targetIds: string[]; details: Record; - }>("mem:audit"); + }>(currentAuditScope()); const evictEntries = allEntries.filter( (e) => e.functionId === "mem::retention-evict", ); @@ -391,7 +392,7 @@ describe("RetentionScoring", () => { payload: { threshold: 0.0001 }, }); - const allEntries = await kv.list<{ functionId: string }>("mem:audit"); + const allEntries = await kv.list<{ functionId: string }>(currentAuditScope()); const evictEntries = allEntries.filter( (e) => e.functionId === "mem::retention-evict", ); @@ -417,7 +418,7 @@ describe("RetentionScoring", () => { functionId: string; targetIds: string[]; details: Record; - }>("mem:audit"); + }>(currentAuditScope()); const scoreEntries = allEntries.filter( (e) => e.functionId === "mem::retention-score", ); diff --git a/test/snapshot.test.ts b/test/snapshot.test.ts index 6a052cd54c..63c071c34a 100644 --- a/test/snapshot.test.ts +++ b/test/snapshot.test.ts @@ -33,6 +33,7 @@ vi.mock("node:fs", () => ({ })); import { registerSnapshotFunction } from "../src/functions/snapshot.js"; +import { currentAuditScope } from "./helpers/mocks.js"; import type { Session, Memory, SnapshotMeta } from "../src/types.js"; function mockKV() { @@ -160,7 +161,7 @@ describe("Snapshot Functions", () => { it("snapshot-create records an audit entry", async () => { await sdk.trigger("mem::snapshot-create", { message: "Audit test" }); - const audits = await kv.list("mem:audit"); + const audits = await kv.list(currentAuditScope()); expect(audits.length).toBe(1); }); }); From 9b8b1056fe068c56e32860a0dcc4bc2222439a70 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Fri, 25 Sep 2026 20:15:57 +0530 Subject: [PATCH 2/6] perf(audit): migrate the legacy audit log in two fast passes Moving one row at a time kept the legacy scope large for the whole migration, and the engine rewrites a changed scope in full on every 5 s flush, so a store with hundreds of thousands of rows would have rewritten that file hundreds of times. The migration now copies the rows it keeps into their month scopes, records each month in the index once, and then deletes the legacy keys, both passes with bounded concurrency and no pauses, so the legacy scope empties within a few flushes. A migration that stops early leaves the legacy rows readable and resumes on the next boot. --- src/functions/audit.ts | 85 ++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 45 deletions(-) diff --git a/src/functions/audit.ts b/src/functions/audit.ts index 5a7b86e050..087af3033f 100644 --- a/src/functions/audit.ts +++ b/src/functions/audit.ts @@ -32,8 +32,7 @@ import { logger } from "../logger.js"; // BEFORE kv.delete(...) and match one of the two shapes above. const AUDIT_MIGRATE_FUNCTION_ID = "mem::audit-migrate"; -const AUDIT_MIGRATION_BATCH_SIZE = 200; -const AUDIT_MIGRATION_TICK_MS = 50; +const AUDIT_MIGRATION_CONCURRENCY = 32; interface AuditMonthIndex { months: string[]; @@ -190,64 +189,60 @@ export async function queryAudit( return collected.slice(0, limit); } -async function migrateAuditEntry( - kv: StateKV, - entry: AuditEntry, -): Promise<"migrated" | "purged"> { - if (entry.operation === "index_persist") { - await kv.delete(KV.audit, entry.id); - return "purged"; - } - const month = auditMonthOf(entry.timestamp); - await kv.set(KV.auditMonth(month), entry.id, entry); - await markAuditMonth(kv, month); - await kv.delete(KV.audit, entry.id); - return "migrated"; -} - -function delay(ms: number): Promise { - return new Promise((resolve) => { - const timer = setTimeout(resolve, ms); - timer.unref(); +async function inParallel( + items: T[], + concurrency: number, + work: (item: T) => Promise, +): Promise { + let next = 0; + const runners = Array.from({ length: Math.min(concurrency, items.length) }, async () => { + while (next < items.length) { + const item = items[next++]; + await work(item); + } }); + await Promise.all(runners); } export async function startAuditMigration(kv: StateKV): Promise { - let totalMigrated = 0; - let totalPurged = 0; - + let legacy: AuditEntry[]; try { - const queue = await kv.list(KV.audit); - let cursor = 0; - - while (cursor < queue.length) { - const batch = queue.slice(cursor, cursor + AUDIT_MIGRATION_BATCH_SIZE); - cursor += batch.length; + legacy = await kv.list(KV.audit); + } catch (err) { + logger.warn("audit migration could not read the legacy audit log", { + error: err instanceof Error ? err.message : String(err), + }); + return; + } + if (legacy.length === 0) return; - for (const entry of batch) { - const outcome = await migrateAuditEntry(kv, entry); - if (outcome === "purged") totalPurged++; - else totalMigrated++; - } + const kept = legacy.filter((entry) => entry.operation !== "index_persist"); + const purged = legacy.length - kept.length; - if (cursor < queue.length) { - await delay(AUDIT_MIGRATION_TICK_MS); - } - } + try { + const months = new Set(); + await inParallel(kept, AUDIT_MIGRATION_CONCURRENCY, async (entry) => { + const month = auditMonthOf(entry.timestamp); + await kv.set(KV.auditMonth(month), entry.id, entry); + months.add(month); + }); + for (const month of months) await markAuditMonth(kv, month); + await inParallel(legacy, AUDIT_MIGRATION_CONCURRENCY, async (entry) => { + await kv.delete(KV.audit, entry.id); + }); } catch (err) { - logger.warn("audit migration failed", { + logger.warn("audit migration stopped before finishing; it resumes on the next boot", { error: err instanceof Error ? err.message : String(err), - migrated: totalMigrated, - purged: totalPurged, }); return; } - if (totalPurged > 0) { + logger.info("audit log moved to monthly scopes", { migrated: kept.length, purged }); + if (purged > 0) { await safeAudit(kv, "audit_migrate", AUDIT_MIGRATE_FUNCTION_ID, [], { reason: "index_persist rows predate opt-in auditing", - purged: totalPurged, - migrated: totalMigrated, + purged, + migrated: kept.length, }); } } From 5907780b998f827d1178b6e83cc0954440749aa1 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Sat, 26 Sep 2026 11:33:55 +0530 Subject: [PATCH 3/6] chore: credit co-authors The audit growth (583,035 rows, 534k index_persist) was measured and reported in discussion #1358. Co-authored-by: Vladimir <7998636+MarvinFS@users.noreply.github.com> From bb5f416129091b3938c83819aad0158c14091e84 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Sat, 26 Sep 2026 11:48:53 +0530 Subject: [PATCH 4/6] fix(audit): close the lost-update race on the month index markAuditMonth and runAuditRetentionSweep each did a read, modify, write round trip against the single mem:audit:months index key with no coordination between callers. startAuditMigration runs unawaited at boot alongside request-time recordAudit calls, and the retention sweep reads the index once at the start of a run that can take a while to delete rows. In every case, a second writer could read the stale index before the first writer's write landed, and the later write would silently drop the other writer's month from the index, making its rows unreachable by queryAudit and the retention sweep. Serialize index mutations through the existing keyed mutex (already used elsewhere in this file's neighborhood for the same class of read-modify-write race) and have the sweep re-read the index inside the lock right before its final write, instead of writing back a snapshot captured before the sweep started. Also run the audit retention sweep once at startup, after audit migration finishes, instead of only on a 24h interval. Deployments that restart daily before the first interval fires never get a sweep. --- src/functions/audit.ts | 19 +++++++++----- src/index.ts | 9 ++++--- test/audit-retention.test.ts | 50 +++++++++++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 10 deletions(-) diff --git a/src/functions/audit.ts b/src/functions/audit.ts index 087af3033f..1ad162c718 100644 --- a/src/functions/audit.ts +++ b/src/functions/audit.ts @@ -2,6 +2,7 @@ import type { AuditEntry } from "../types.js"; import { KV, generateId } from "../state/schema.js"; import type { StateKV } from "../state/kv.js"; import { logger } from "../logger.js"; +import { withKeyedLock } from "../state/keyed-mutex.js"; // Audit coverage policy (issue #125). // @@ -33,6 +34,7 @@ import { logger } from "../logger.js"; const AUDIT_MIGRATE_FUNCTION_ID = "mem::audit-migrate"; const AUDIT_MIGRATION_CONCURRENCY = 32; +const AUDIT_MONTHS_LOCK = KV.auditMonths; interface AuditMonthIndex { months: string[]; @@ -70,10 +72,12 @@ async function readAuditMonthIndex(kv: StateKV): Promise { } async function markAuditMonth(kv: StateKV, month: string): Promise { - const months = await readAuditMonthIndex(kv); - if (months.includes(month)) return; - const next = [...months, month].sort(); - await kv.set(KV.auditMonths, "index", { months: next }); + await withKeyedLock(AUDIT_MONTHS_LOCK, async () => { + const months = await readAuditMonthIndex(kv); + if (months.includes(month)) return; + const next = [...months, month].sort(); + await kv.set(KV.auditMonths, "index", { months: next }); + }); } export async function listAuditMonthsDesc(kv: StateKV): Promise { @@ -271,8 +275,11 @@ export async function runAuditRetentionSweep( } } - const remaining = months.filter((month) => !toDrop.includes(month)).sort(); - await kv.set(KV.auditMonths, "index", { months: remaining }); + await withKeyedLock(AUDIT_MONTHS_LOCK, async () => { + const current = await readAuditMonthIndex(kv); + const remaining = current.filter((month) => !toDrop.includes(month)).sort(); + await kv.set(KV.auditMonths, "index", { months: remaining }); + }); return { droppedMonths: toDrop, droppedRows }; } diff --git a/src/index.ts b/src/index.ts index ee153e7092..ace8cf312b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -338,7 +338,7 @@ async function main() { registerRetentionFunctions(sdk, kv); registerCompressFileFunction(sdk, kv, provider); registerReplayFunctions(sdk, kv); - void startAuditMigration(kv); + const auditMigration = startAuditMigration(kv).catch(() => {}); bootLog( `v0.6 advanced retrieval: sliding-window, query-expansion, temporal-graph, retention-scoring`, ); @@ -581,11 +581,14 @@ async function main() { const auditRetentionMonths = getAuditRetentionMonths(); if (auditRetentionMonths > 0) { - const auditRetentionTimer = setInterval(async () => { + const runAuditSweep = async () => { try { + await auditMigration; await sdk.trigger({ function_id: "mem::audit-retention-sweep", payload: {} }); } catch {} - }, 86400000); + }; + void runAuditSweep(); + const auditRetentionTimer = setInterval(runAuditSweep, 86400000); auditRetentionTimer.unref(); bootLog( `Audit retention sweep: enabled (drop month scopes older than ${auditRetentionMonths}mo, every 24h)`, diff --git a/test/audit-retention.test.ts b/test/audit-retention.test.ts index 1817cb54d4..884c6bad25 100644 --- a/test/audit-retention.test.ts +++ b/test/audit-retention.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect, beforeEach, vi } from "vitest"; +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; vi.mock("../src/logger.js", () => ({ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, @@ -296,4 +296,52 @@ describe("audit retention sweep", () => { const recentRows = await kv.list(KV.auditMonth(recentMonth)); expect(recentRows).toHaveLength(1); }); + + it("does not drop a month added by another writer while the sweep is running", async () => { + const oldMonth = "2020-01"; + const currentMonth = auditMonthOf(new Date().toISOString()); + await seedMonthIndex(kv, [oldMonth]); + seedEntry(kv, KV.auditMonth(oldMonth), `${oldMonth}-05T00:00:00.000Z`, { + id: "old_1", + }); + + let injected = false; + const raceKv = { + ...kv, + list: async (scope: string): Promise => { + if (!injected && scope === KV.auditMonth(oldMonth)) { + injected = true; + await recordAudit(kv as never, "delete", "mem::test", ["concurrent"]); + } + return kv.list(scope); + }, + }; + + const result = await runAuditRetentionSweep(raceKv as never, 1); + expect(result.droppedMonths).toEqual([oldMonth]); + + const months = await listAuditMonthsDesc(kv as never); + expect(months).toEqual([currentMonth]); + }); +}); + +describe("markAuditMonth concurrency", () => { + afterEach(() => { + vi.useRealTimers(); + }); + + it("keeps both months when two writers race different months onto the index", async () => { + const kv = mockKV(); + + vi.useFakeTimers(); + vi.setSystemTime(new Date("2020-01-15T00:00:00.000Z")); + const pA = recordAudit(kv as never, "delete", "mem::test", ["a"]); + vi.setSystemTime(new Date("2020-02-15T00:00:00.000Z")); + const pB = recordAudit(kv as never, "delete", "mem::test", ["b"]); + await Promise.all([pA, pB]); + vi.useRealTimers(); + + const months = await listAuditMonthsDesc(kv as never); + expect([...months].sort()).toEqual(["2020-01", "2020-02"]); + }); }); From 01a574d2c60a54b570fb1584530c07fb50f645d4 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Sat, 26 Sep 2026 13:33:40 +0530 Subject: [PATCH 5/6] perf(audit): gate the legacy audit migration on a local size probe The migration used to unconditionally state::list the legacy mem:audit scope on every boot, which can return a payload over the SDK socket's 100 MiB limit and hang boot for the invocation timeout, and it deleted legacy keys in insertion order, which is quadratic against the engine's IndexMap-backed store. queryAudit had the same unconditional list on its legacy fallback path, with no de-duplication against rows already copied into month scopes. Replace the unconditional list with a local file-system probe of the engine's own state store directory (mem%3Aaudit.bin) so a store's size is known before ever asking the engine for it. Under AGENTMEMORY_AUDIT_MIGRATE_MAX_BYTES (default 32 MiB) and locally readable, the migration copies rows into their month scopes, waits at least two engine save intervals so the copies are durable, then deletes the legacy keys in reverse insertion order with lower concurrency. Anything larger, or not locally readable (for example an engine on another host), is left frozen and reported through GET /agentmemory/status instead of migrated. Progress is tracked in a migration marker (mem:audit:months/migration) so a crash mid-pass resumes without re-deriving counts from a shrinking legacy scope, and the completion summary is written exactly once behind that marker. queryAudit only reads the legacy scope when the marker says it is safe, de-duplicates by id, and reports a legacyFrozen flag plus size when older rows are known to remain frozen. The retention sweep also deletes in reverse insertion order. Fixed a re-entrant deadlock this introduced: the migration's summary-write step called safeAudit under the same keyed lock used by markAuditMonth, which queued behind itself forever. It now uses its own lock key. AuditMonthIndex and the new migration/query types now live in src/types.ts per the KV-scope convention. standalone MCP's memory_audit handler now goes through queryAudit like every other reader instead of listing the legacy scope directly. --- .env.example | 1 + plugin/skills/agentmemory-config/REFERENCE.md | 3 +- src/cli/engine-config.ts | 5 + src/config.ts | 7 + src/functions/audit.ts | 302 ++++++++++++-- src/functions/status.ts | 12 + src/index.ts | 3 +- src/mcp/standalone.ts | 13 +- src/triggers/api.ts | 24 +- src/types.ts | 20 + test/audit-retention.test.ts | 389 +++++++++++++++++- test/audit.test.ts | 10 +- test/governance.test.ts | 4 +- test/status.test.ts | 4 +- 14 files changed, 725 insertions(+), 72 deletions(-) diff --git a/.env.example b/.env.example index 4f9181b888..b207db7ab9 100644 --- a/.env.example +++ b/.env.example @@ -130,6 +130,7 @@ # AGENTMEMORY_DROP_STALE_INDEX=true # Drop on-disk BM25 / vector index on startup if dim guard fires (#248). Recovery toggle for stuck-state debugging. # AGENTMEMORY_IMAGE_EMBEDDINGS=true # Enable image embeddings when an image provider is present (experimental). # AGENTMEMORY_AUDIT_RETENTION_MONTHS=6 # Drop whole audit-log month scopes older than N months on a daily sweep. Default 0 = keep audit history forever. +# AGENTMEMORY_AUDIT_MIGRATE_MAX_BYTES=33554432 # Largest legacy mem:audit scope file the one-time migration will read locally. Default 32 MiB; bigger or unreadable stores are left frozen. # ----------------------------------------------------------------------------- # 6. CLI / runtime knobs diff --git a/plugin/skills/agentmemory-config/REFERENCE.md b/plugin/skills/agentmemory-config/REFERENCE.md index 32794d5174..534b46ed84 100644 --- a/plugin/skills/agentmemory-config/REFERENCE.md +++ b/plugin/skills/agentmemory-config/REFERENCE.md @@ -3,11 +3,12 @@ Generated by scanning `src/` for `AGENTMEMORY_*` usage. Do not edit the block below by hand; run `npm run skills:gen` after adding or removing a variable. Internal markers ending in two underscores are excluded. -Configuration is read from the environment and from `~/.agentmemory/.env` (no `export` prefix). 44 recognized variables: +Configuration is read from the environment and from `~/.agentmemory/.env` (no `export` prefix). 45 recognized variables: - `AGENTMEMORY_AGENT_SCOPE` - `AGENTMEMORY_ALLOW_AGENT_SDK` - `AGENTMEMORY_AUDIT_INDEX_PERSIST` +- `AGENTMEMORY_AUDIT_MIGRATE_MAX_BYTES` - `AGENTMEMORY_AUDIT_RETENTION_MONTHS` - `AGENTMEMORY_AUTO_COMPRESS` - `AGENTMEMORY_COMMIT_SHA` diff --git a/src/cli/engine-config.ts b/src/cli/engine-config.ts index b48f09dfa5..f5d6d5a4d6 100644 --- a/src/cli/engine-config.ts +++ b/src/cli/engine-config.ts @@ -28,6 +28,11 @@ export function configuredPersistDir(renderedConfig: string): string | null { return null; } +export function configuredSaveIntervalMs(renderedConfig: string): number | null { + const match = renderedConfig.match(/save_interval_ms:\s*(\d+)/); + return match ? parseInt(match[1]!, 10) : null; +} + export function persistedBuiltinConfigDirs( engineCwd: string, configPath: string, diff --git a/src/config.ts b/src/config.ts index 0f2e3fce2d..d236a345e4 100644 --- a/src/config.ts +++ b/src/config.ts @@ -239,6 +239,13 @@ export function getAuditRetentionMonths(): number { return safeParseInt(getMergedEnv()["AGENTMEMORY_AUDIT_RETENTION_MONTHS"], 0); } +export function getAuditMigrateMaxBytes(): number { + return safeParseInt( + getMergedEnv()["AGENTMEMORY_AUDIT_MIGRATE_MAX_BYTES"], + 32 * 1024 * 1024, + ); +} + export function detectLlmProviderKind(): "llm" | "noop" { const env = getMergedEnv(); if ( diff --git a/src/functions/audit.ts b/src/functions/audit.ts index 1ad162c718..4f7d0f9b5d 100644 --- a/src/functions/audit.ts +++ b/src/functions/audit.ts @@ -1,8 +1,13 @@ -import type { AuditEntry } from "../types.js"; +import { stat, readFile } from "node:fs/promises"; +import { join } from "node:path"; +import type { AuditEntry, AuditMigrationState, AuditQueryResult } from "../types.js"; import { KV, generateId } from "../state/schema.js"; import type { StateKV } from "../state/kv.js"; import { logger } from "../logger.js"; import { withKeyedLock } from "../state/keyed-mutex.js"; +import { getEnvVar, getAuditMigrateMaxBytes } from "../config.js"; +import { runtimeConfigPath } from "../cli/engine-launch.js"; +import { configuredSaveIntervalMs } from "../cli/engine-config.js"; // Audit coverage policy (issue #125). // @@ -33,11 +38,88 @@ import { withKeyedLock } from "../state/keyed-mutex.js"; // BEFORE kv.delete(...) and match one of the two shapes above. const AUDIT_MIGRATE_FUNCTION_ID = "mem::audit-migrate"; -const AUDIT_MIGRATION_CONCURRENCY = 32; +const AUDIT_MIGRATION_COPY_CONCURRENCY = 32; +const AUDIT_MIGRATION_DELETE_CONCURRENCY = 8; const AUDIT_MONTHS_LOCK = KV.auditMonths; +const AUDIT_MIGRATION_LOCK = "mem:audit:migration-lock"; +export const AUDIT_MIGRATION_STATE_KEY = "migration"; +const DEFAULT_SAVE_INTERVAL_MS = 5000; +const AUDIT_MIGRATION_DELETE_WAIT_INTERVALS = 2; +const STATE_STORE_DIR_NAME = "state_store.db"; -interface AuditMonthIndex { - months: string[]; +function sleep(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +function encodeStateIndexSegment(index: string): string { + let out = ""; + for (const byte of Buffer.from(index, "utf8")) { + const ch = String.fromCharCode(byte); + out += /^[A-Za-z0-9\-_.]$/.test(ch) + ? ch + : `%${byte.toString(16).toUpperCase().padStart(2, "0")}`; + } + return out; +} + +function legacyAuditStateFilePath(dataDir: string): string { + return join(dataDir, STATE_STORE_DIR_NAME, `${encodeStateIndexSegment(KV.audit)}.bin`); +} + +export type LegacyAuditProbe = + | { safe: true; empty: true } + | { safe: true; empty: false; sizeBytes: number } + | { safe: false; reason: "too-large"; sizeBytes: number } + | { safe: false; reason: "unreadable" }; + +export async function probeLegacyAuditFile(): Promise { + const dataDir = getEnvVar("AGENTMEMORY_DATA_DIR"); + if (!dataDir) return { safe: false, reason: "unreadable" }; + + const storeDir = join(dataDir, STATE_STORE_DIR_NAME); + try { + await stat(storeDir); + } catch { + return { safe: false, reason: "unreadable" }; + } + + let fileStat; + try { + fileStat = await stat(legacyAuditStateFilePath(dataDir)); + } catch (err) { + if ((err as NodeJS.ErrnoException).code === "ENOENT") { + return { safe: true, empty: true }; + } + return { safe: false, reason: "unreadable" }; + } + + if (fileStat.size > getAuditMigrateMaxBytes()) { + return { safe: false, reason: "too-large", sizeBytes: fileStat.size }; + } + return { safe: true, empty: false, sizeBytes: fileStat.size }; +} + +async function auditMigrationDeleteDelayMs(): Promise { + const dataDir = getEnvVar("AGENTMEMORY_DATA_DIR"); + if (!dataDir) return DEFAULT_SAVE_INTERVAL_MS * AUDIT_MIGRATION_DELETE_WAIT_INTERVALS; + try { + const rendered = await readFile(runtimeConfigPath(dataDir), "utf-8"); + const interval = configuredSaveIntervalMs(rendered) ?? DEFAULT_SAVE_INTERVAL_MS; + return interval * AUDIT_MIGRATION_DELETE_WAIT_INTERVALS; + } catch { + return DEFAULT_SAVE_INTERVAL_MS * AUDIT_MIGRATION_DELETE_WAIT_INTERVALS; + } +} + +async function readAuditMigrationState(kv: StateKV): Promise { + return kv.get(KV.auditMonths, AUDIT_MIGRATION_STATE_KEY); +} + +async function writeAuditMigrationState( + kv: StateKV, + state: AuditMigrationState, +): Promise { + await kv.set(KV.auditMonths, AUDIT_MIGRATION_STATE_KEY, state); } export function auditMonthOf(timestamp: string): string { @@ -67,7 +149,7 @@ function monthNMonthsAgo(count: number): string { } async function readAuditMonthIndex(kv: StateKV): Promise { - const index = await kv.get(KV.auditMonths, "index"); + const index = await kv.get<{ months: string[] }>(KV.auditMonths, "index"); return index?.months ?? []; } @@ -141,7 +223,7 @@ export async function queryAudit( dateTo?: string; limit?: number; }, -): Promise { +): Promise { const limit = filter?.limit || 100; let fromMs: number | undefined; @@ -168,29 +250,45 @@ export async function queryAudit( return true; }; + const seenIds = new Set(); const collected: AuditEntry[] = []; + const addIfNew = (row: AuditEntry): void => { + if (seenIds.has(row.id)) return; + seenIds.add(row.id); + if (matches(row)) collected.push(row); + }; + const months = await listAuditMonthsDesc(kv); for (const month of months) { if (collected.length >= limit) break; if (toMs !== undefined && monthStartMs(month) > toMs) continue; if (fromMs !== undefined && monthEndMs(month) < fromMs) break; const rows = await kv.list(KV.auditMonth(month)); - for (const row of rows) { - if (matches(row)) collected.push(row); - } + for (const row of rows) addIfNew(row); } + let legacyFrozen = false; + let legacyFrozenBytes: number | undefined; + if (collected.length < limit) { - const legacyRows = await kv.list(KV.audit); - for (const row of legacyRows) { - if (matches(row)) collected.push(row); + const migrationState = await readAuditMigrationState(kv); + if (migrationState?.safeToListLegacy) { + const legacyRows = await kv.list(KV.audit); + for (const row of legacyRows) addIfNew(row); + } else if (migrationState && migrationState.status !== "done") { + legacyFrozen = true; + legacyFrozenBytes = migrationState.legacySizeBytes; } } collected.sort( (a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime(), ); - return collected.slice(0, limit); + return { + entries: collected.slice(0, limit), + legacyFrozen, + legacyFrozenBytes, + }; } async function inParallel( @@ -208,47 +306,167 @@ async function inParallel( await Promise.all(runners); } -export async function startAuditMigration(kv: StateKV): Promise { - let legacy: AuditEntry[]; - try { - legacy = await kv.list(KV.audit); - } catch (err) { - logger.warn("audit migration could not read the legacy audit log", { - error: err instanceof Error ? err.message : String(err), +export interface AuditMigrationDeps { + probe?: () => Promise; + deleteDelayMs?: () => Promise; +} + +export async function startAuditMigration( + kv: StateKV, + deps: AuditMigrationDeps = {}, +): Promise { + const runProbe = deps.probe ?? probeLegacyAuditFile; + const runDeleteDelayMs = deps.deleteDelayMs ?? auditMigrationDeleteDelayMs; + + const marker = await readAuditMigrationState(kv); + if (marker?.status === "done") return; + + const probe = await runProbe(); + + if (probe.safe && probe.empty) { + await writeAuditMigrationState(kv, { + status: "done", + safeToListLegacy: true, + legacySizeBytes: 0, + migrated: marker?.migrated ?? 0, + purged: marker?.purged ?? 0, + summaryWritten: marker?.summaryWritten ?? true, + checkedAt: new Date().toISOString(), + }); + return; + } + + if (!probe.safe) { + await writeAuditMigrationState(kv, { + status: probe.reason, + safeToListLegacy: false, + legacySizeBytes: probe.reason === "too-large" ? probe.sizeBytes : marker?.legacySizeBytes, + migrated: marker?.migrated ?? 0, + purged: marker?.purged ?? 0, + summaryWritten: marker?.summaryWritten ?? false, + checkedAt: new Date().toISOString(), + }); + logger.warn("legacy audit log left frozen; migration only reads it locally and under a size cap", { + reason: probe.reason, + sizeBytes: probe.reason === "too-large" ? probe.sizeBytes : undefined, }); return; } - if (legacy.length === 0) return; - const kept = legacy.filter((entry) => entry.operation !== "index_persist"); - const purged = legacy.length - kept.length; + let migrated = marker?.migrated ?? 0; + let purged = marker?.purged ?? 0; - try { - const months = new Set(); - await inParallel(kept, AUDIT_MIGRATION_CONCURRENCY, async (entry) => { - const month = auditMonthOf(entry.timestamp); - await kv.set(KV.auditMonth(month), entry.id, entry); - months.add(month); + if (marker?.status !== "copied") { + let legacy: AuditEntry[]; + try { + legacy = await kv.list(KV.audit); + } catch (err) { + logger.warn("audit migration could not read the legacy audit log", { + error: err instanceof Error ? err.message : String(err), + }); + return; + } + if (legacy.length === 0) { + await writeAuditMigrationState(kv, { + status: "done", + safeToListLegacy: true, + legacySizeBytes: 0, + migrated, + purged, + summaryWritten: marker?.summaryWritten ?? true, + checkedAt: new Date().toISOString(), + }); + return; + } + + const seen = new Set(); + const deduped = legacy.filter((entry) => { + if (seen.has(entry.id)) return false; + seen.add(entry.id); + return true; }); - for (const month of months) await markAuditMonth(kv, month); - await inParallel(legacy, AUDIT_MIGRATION_CONCURRENCY, async (entry) => { - await kv.delete(KV.audit, entry.id); + const kept = deduped.filter((entry) => entry.operation !== "index_persist"); + purged = deduped.length - kept.length; + migrated = kept.length; + + try { + const months = new Set(); + await inParallel(kept, AUDIT_MIGRATION_COPY_CONCURRENCY, async (entry) => { + const month = auditMonthOf(entry.timestamp); + await kv.set(KV.auditMonth(month), entry.id, entry); + months.add(month); + }); + for (const month of months) await markAuditMonth(kv, month); + } catch (err) { + logger.warn( + "audit migration stopped before finishing the copy pass; it resumes on the next boot", + { error: err instanceof Error ? err.message : String(err) }, + ); + return; + } + + await writeAuditMigrationState(kv, { + status: "copied", + safeToListLegacy: false, + legacySizeBytes: probe.sizeBytes, + migrated, + purged, + summaryWritten: false, + checkedAt: new Date().toISOString(), }); + } + + await sleep(await runDeleteDelayMs()); + + let remaining: AuditEntry[]; + try { + remaining = await kv.list(KV.audit); } catch (err) { - logger.warn("audit migration stopped before finishing; it resumes on the next boot", { - error: err instanceof Error ? err.message : String(err), - }); + logger.warn( + "audit migration could not re-read the legacy audit log before deleting; it resumes on the next boot", + { error: err instanceof Error ? err.message : String(err) }, + ); return; } - logger.info("audit log moved to monthly scopes", { migrated: kept.length, purged }); - if (purged > 0) { - await safeAudit(kv, "audit_migrate", AUDIT_MIGRATE_FUNCTION_ID, [], { - reason: "index_persist rows predate opt-in auditing", + try { + await inParallel( + remaining.slice().reverse(), + AUDIT_MIGRATION_DELETE_CONCURRENCY, + async (entry) => { + await kv.delete(KV.audit, entry.id); + }, + ); + } catch (err) { + logger.warn( + "audit migration stopped before finishing the delete pass; it resumes on the next boot", + { error: err instanceof Error ? err.message : String(err) }, + ); + return; + } + + await withKeyedLock(AUDIT_MIGRATION_LOCK, async () => { + const current = await readAuditMigrationState(kv); + if (current?.summaryWritten) return; + if (purged > 0) { + await safeAudit(kv, "audit_migrate", AUDIT_MIGRATE_FUNCTION_ID, [], { + reason: "index_persist rows predate opt-in auditing", + purged, + migrated, + }); + } + await writeAuditMigrationState(kv, { + status: "done", + safeToListLegacy: true, + legacySizeBytes: 0, + migrated, purged, - migrated: kept.length, + summaryWritten: true, + checkedAt: new Date().toISOString(), }); - } + }); + + logger.info("audit log moved to monthly scopes", { migrated, purged }); } export async function runAuditRetentionSweep( @@ -269,7 +487,7 @@ export async function runAuditRetentionSweep( let droppedRows = 0; for (const month of toDrop) { const rows = await kv.list(KV.auditMonth(month)); - for (const row of rows) { + for (const row of rows.slice().reverse()) { await kv.delete(KV.auditMonth(month), row.id); droppedRows++; } diff --git a/src/functions/status.ts b/src/functions/status.ts index 552fe6c07a..9e845f59a3 100644 --- a/src/functions/status.ts +++ b/src/functions/status.ts @@ -58,6 +58,7 @@ export interface StatusInputs { }; graph: GraphStatsInput | null; graphExtractionEnabled: boolean; + auditLegacy: { status: string; sizeBytes?: number } | null; } export interface StatusReport { @@ -197,6 +198,17 @@ export function evaluateStatus(input: StatusInputs): StatusReport { } } + if (input.auditLegacy && input.auditLegacy.status !== "done") { + const size = input.auditLegacy.sizeBytes; + const sizeText = typeof size === "number" ? ` (${Math.round(size / (1024 * 1024))} MiB)` : ""; + problems.push({ + level: "info", + code: "audit-legacy-frozen", + message: `An older audit log${sizeText} was left in place instead of being migrated into monthly scopes, so it is no longer rewritten but its rows do not show up in audit queries.`, + fix: "Safe to ignore. To remove it, stop agentmemory, delete mem%3Aaudit.bin from the state store directory, then start it again.", + }); + } + const status = problems.reduce( (worst, p) => (LEVEL_RANK[p.level] > LEVEL_RANK[worst] ? p.level : worst), "ok", diff --git a/src/index.ts b/src/index.ts index ace8cf312b..6343fe1780 100644 --- a/src/index.ts +++ b/src/index.ts @@ -338,7 +338,6 @@ async function main() { registerRetentionFunctions(sdk, kv); registerCompressFileFunction(sdk, kv, provider); registerReplayFunctions(sdk, kv); - const auditMigration = startAuditMigration(kv).catch(() => {}); bootLog( `v0.6 advanced retrieval: sliding-window, query-expansion, temporal-graph, retention-scoring`, ); @@ -469,6 +468,8 @@ async function main() { } } + const auditMigration = startAuditMigration(kv).catch(() => {}); + const needsRebuild = bm25Index.size === 0; if (needsRebuild) { diff --git a/src/mcp/standalone.ts b/src/mcp/standalone.ts index f425b159c4..601cc1ffaf 100644 --- a/src/mcp/standalone.ts +++ b/src/mcp/standalone.ts @@ -6,6 +6,7 @@ import { getAllTools } from "./tools-registry.js"; import { getStandalonePersistPath } from "../config.js"; import { VERSION } from "../version.js"; import { generateId } from "../state/schema.js"; +import { queryAudit } from "../functions/audit.js"; import { resolveHandle, invalidateHandle, @@ -337,14 +338,10 @@ async function handleLocal( } case "memory_audit": { - const entries = await kvInstance.list("mem:audit"); - const limit = v.limit ?? 50; - return textResponse( - { - entries: (entries as Array>).slice(0, limit), - }, - true, - ); + const result = await queryAudit(kvInstance as never, { + limit: v.limit ?? 50, + }); + return textResponse(result, true); } default: diff --git a/src/triggers/api.ts b/src/triggers/api.ts index d8a447f2e0..faf4350d01 100644 --- a/src/triggers/api.ts +++ b/src/triggers/api.ts @@ -1,7 +1,8 @@ import { TriggerAction, type IIIClient } from "iii-sdk"; import type { HttpRequest } from "@iii-dev/helpers/http"; import { randomBytes } from "node:crypto"; -import type { Session, CompressedObservation, HookPayload, CommitLink, SessionSummary } from "../types.js"; +import type { Session, CompressedObservation, HookPayload, CommitLink, SessionSummary, AuditQueryResult, AuditMigrationState } from "../types.js"; +import { AUDIT_MIGRATION_STATE_KEY } from "../functions/audit.js"; import { withKeyedLock } from "../state/keyed-mutex.js"; import { KV } from "../state/schema.js"; import { checkPayloadFrameSize } from "../state/frame-guard.js"; @@ -327,7 +328,7 @@ export function registerApiTriggers( const authErr = checkAuth(req, secret); if (authErr) return authErr; const idx = getSearchIndex(); - const [health, functionMetrics, graph, unindexed] = await Promise.all([ + const [health, functionMetrics, graph, unindexed, auditMigrationState] = await Promise.all([ valueWithin(getLatestHealth(kv), STATUS_CHECK_TIMEOUT_MS), metricsStore ? valueWithin(metricsStore.getAll(), STATUS_CHECK_TIMEOUT_MS) : Promise.resolve([]), valueWithin( @@ -335,6 +336,10 @@ export function registerApiTriggers( STATUS_CHECK_TIMEOUT_MS, ), valueWithin(sharedUnindexedScan(), STATUS_CHECK_TIMEOUT_MS), + valueWithin( + kv.get(KV.auditMonths, AUDIT_MIGRATION_STATE_KEY), + STATUS_CHECK_TIMEOUT_MS, + ), ]); const observationsIndexed = [...idx.observationCountsBySession().values()].reduce((a, n) => a + n, 0); const circuit = @@ -373,6 +378,9 @@ export function registerApiTriggers( }, graph, graphExtractionEnabled: isGraphExtractionEnabled(), + auditLegacy: auditMigrationState + ? { status: auditMigrationState.status, sizeBytes: auditMigrationState.legacySizeBytes } + : null, }); const accept = req.headers?.["accept"] ?? req.headers?.["Accept"]; const format = req.query_params?.["format"]; @@ -1897,11 +1905,19 @@ export function registerApiTriggers( const authErr = checkAuth(req, secret); if (authErr) return authErr; const parsedLimit = parseOptionalInt(req.query_params?.["limit"]); - const entries = await sdk.trigger({ function_id: "mem::audit-query", payload: { + const result = await sdk.trigger({ function_id: "mem::audit-query", payload: { operation: req.query_params?.["operation"], limit: parsedLimit ?? 50, } }); - return { status_code: 200, body: { entries, success: true } }; + return { + status_code: 200, + body: { + entries: result.entries, + legacyFrozen: result.legacyFrozen, + legacyFrozenBytes: result.legacyFrozenBytes, + success: true, + }, + }; }, ); sdk.registerTrigger({ diff --git a/src/types.ts b/src/types.ts index ef65eea7dd..b9fb6f9acb 100644 --- a/src/types.ts +++ b/src/types.ts @@ -640,6 +640,26 @@ export interface AuditEntry { qualityScore?: number; } +export interface AuditMonthIndex { + months: string[]; +} + +export interface AuditMigrationState { + status: "too-large" | "unreadable" | "copied" | "done"; + safeToListLegacy: boolean; + legacySizeBytes?: number; + migrated: number; + purged: number; + summaryWritten: boolean; + checkedAt: string; +} + +export interface AuditQueryResult { + entries: AuditEntry[]; + legacyFrozen: boolean; + legacyFrozenBytes?: number; +} + export interface GovernanceFilter { type?: string[]; dateFrom?: string; diff --git a/test/audit-retention.test.ts b/test/audit-retention.test.ts index 884c6bad25..70c1911889 100644 --- a/test/audit-retention.test.ts +++ b/test/audit-retention.test.ts @@ -1,4 +1,7 @@ import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; +import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from "node:fs"; +import { join } from "node:path"; +import { tmpdir } from "node:os"; vi.mock("../src/logger.js", () => ({ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, @@ -11,11 +14,13 @@ import { listAuditMonthsDesc, startAuditMigration, runAuditRetentionSweep, + probeLegacyAuditFile, + AUDIT_MIGRATION_STATE_KEY, } from "../src/functions/audit.js"; import { KV } from "../src/state/schema.js"; import { mockKV } from "./helpers/mocks.js"; import { getAuditRetentionMonths } from "../src/config.js"; -import type { AuditEntry } from "../src/types.js"; +import type { AuditEntry, AuditMigrationState } from "../src/types.js"; function seedEntry( kv: ReturnType, @@ -47,6 +52,18 @@ async function seedMonthIndex( await kv.set(KV.auditMonths, "index", { months }); } +async function seedMigrationState( + kv: ReturnType, + state: AuditMigrationState, +): Promise { + await kv.set(KV.auditMonths, AUDIT_MIGRATION_STATE_KEY, state); +} + +const SMALL_STORE_PROBE = async () => + ({ safe: true, empty: false, sizeBytes: 1024 }) as const; +const EMPTY_STORE_PROBE = async () => ({ safe: true, empty: true }) as const; +const NO_DELAY = async () => 0; + describe("audit month buckets", () => { let kv: ReturnType; @@ -104,7 +121,7 @@ describe("queryAudit across months", () => { }); const listSpy = vi.spyOn(kv, "list"); - const entries = await queryAudit(kv as never, { limit: 2 }); + const { entries } = await queryAudit(kv as never, { limit: 2 }); expect(entries.map((e) => e.id).sort()).toEqual(["aug_1", "aug_2"]); const listedScopes = listSpy.mock.calls.map((call) => call[0]); @@ -113,15 +130,25 @@ describe("queryAudit across months", () => { expect(listedScopes).not.toContain(KV.auditMonth("2026-06")); }); - it("falls back to legacy rows once month scopes are exhausted", async () => { + it("falls back to legacy rows when the migration marker says the legacy scope is safe to read", async () => { await seedMonthIndex(kv, ["2026-08"]); seedEntry(kv, KV.auditMonth("2026-08"), "2026-08-01T00:00:00.000Z", { id: "aug_1", }); seedEntry(kv, KV.audit, "2026-05-01T00:00:00.000Z", { id: "legacy_1" }); + await seedMigrationState(kv, { + status: "done", + safeToListLegacy: true, + legacySizeBytes: 0, + migrated: 0, + purged: 0, + summaryWritten: true, + checkedAt: new Date().toISOString(), + }); - const entries = await queryAudit(kv as never, { limit: 5 }); + const { entries, legacyFrozen } = await queryAudit(kv as never, { limit: 5 }); expect(entries.map((e) => e.id).sort()).toEqual(["aug_1", "legacy_1"]); + expect(legacyFrozen).toBe(false); }); it("never falls back to legacy once the month scopes already satisfy the limit", async () => { @@ -132,9 +159,56 @@ describe("queryAudit across months", () => { seedEntry(kv, KV.audit, "2026-05-01T00:00:00.000Z", { id: "legacy_1" }); const listSpy = vi.spyOn(kv, "list"); - const entries = await queryAudit(kv as never, { limit: 1 }); + const { entries } = await queryAudit(kv as never, { limit: 1 }); + + expect(entries.map((e) => e.id)).toEqual(["aug_1"]); + const listedScopes = listSpy.mock.calls.map((call) => call[0]); + expect(listedScopes).not.toContain(KV.audit); + }); + + it("does not list the legacy scope, and reports it frozen, when no marker says it is safe", async () => { + await seedMonthIndex(kv, ["2026-08"]); + seedEntry(kv, KV.auditMonth("2026-08"), "2026-08-01T00:00:00.000Z", { + id: "aug_1", + }); + seedEntry(kv, KV.audit, "2026-05-01T00:00:00.000Z", { id: "legacy_1" }); + + const listSpy = vi.spyOn(kv, "list"); + const { entries, legacyFrozen, legacyFrozenBytes } = await queryAudit(kv as never, { + limit: 5, + }); expect(entries.map((e) => e.id)).toEqual(["aug_1"]); + expect(legacyFrozen).toBe(false); + expect(legacyFrozenBytes).toBeUndefined(); + const listedScopes = listSpy.mock.calls.map((call) => call[0]); + expect(listedScopes).not.toContain(KV.audit); + }); + + it("reports the legacy scope as frozen when the migration marker says it is unsafe to read", async () => { + await seedMonthIndex(kv, ["2026-08"]); + seedEntry(kv, KV.auditMonth("2026-08"), "2026-08-01T00:00:00.000Z", { + id: "aug_1", + }); + seedEntry(kv, KV.audit, "2026-05-01T00:00:00.000Z", { id: "legacy_1" }); + await seedMigrationState(kv, { + status: "too-large", + safeToListLegacy: false, + legacySizeBytes: 99999999, + migrated: 0, + purged: 0, + summaryWritten: false, + checkedAt: new Date().toISOString(), + }); + + const listSpy = vi.spyOn(kv, "list"); + const { entries, legacyFrozen, legacyFrozenBytes } = await queryAudit(kv as never, { + limit: 5, + }); + + expect(entries.map((e) => e.id)).toEqual(["aug_1"]); + expect(legacyFrozen).toBe(true); + expect(legacyFrozenBytes).toBe(99999999); const listedScopes = listSpy.mock.calls.map((call) => call[0]); expect(listedScopes).not.toContain(KV.audit); }); @@ -150,7 +224,7 @@ describe("queryAudit across months", () => { operation: "forget", }); - const entries = await queryAudit(kv as never, { + const { entries } = await queryAudit(kv as never, { operation: "delete", limit: 10, }); @@ -158,6 +232,70 @@ describe("queryAudit across months", () => { }); }); +describe("probeLegacyAuditFile", () => { + let dir: string; + let originalDataDir: string | undefined; + let originalMaxBytes: string | undefined; + + beforeEach(() => { + dir = mkdtempSync(join(tmpdir(), "am-audit-probe-")); + originalDataDir = process.env["AGENTMEMORY_DATA_DIR"]; + originalMaxBytes = process.env["AGENTMEMORY_AUDIT_MIGRATE_MAX_BYTES"]; + }); + + afterEach(() => { + rmSync(dir, { recursive: true, force: true }); + if (originalDataDir === undefined) delete process.env["AGENTMEMORY_DATA_DIR"]; + else process.env["AGENTMEMORY_DATA_DIR"] = originalDataDir; + if (originalMaxBytes === undefined) delete process.env["AGENTMEMORY_AUDIT_MIGRATE_MAX_BYTES"]; + else process.env["AGENTMEMORY_AUDIT_MIGRATE_MAX_BYTES"] = originalMaxBytes; + }); + + it("reports unreadable when AGENTMEMORY_DATA_DIR is not set", async () => { + delete process.env["AGENTMEMORY_DATA_DIR"]; + const result = await probeLegacyAuditFile(); + expect(result).toEqual({ safe: false, reason: "unreadable" }); + }); + + it("reports unreadable when the engine's state store directory is not local", async () => { + process.env["AGENTMEMORY_DATA_DIR"] = join(dir, "does-not-exist"); + const result = await probeLegacyAuditFile(); + expect(result).toEqual({ safe: false, reason: "unreadable" }); + }); + + it("reports empty when the state store exists but the legacy scope file was never written", async () => { + mkdirSync(join(dir, "state_store.db"), { recursive: true }); + process.env["AGENTMEMORY_DATA_DIR"] = dir; + const result = await probeLegacyAuditFile(); + expect(result).toEqual({ safe: true, empty: true }); + }); + + it("reports safe with the measured size when the legacy scope file is under the cap", async () => { + mkdirSync(join(dir, "state_store.db"), { recursive: true }); + const content = JSON.stringify({ aud_1: { id: "aud_1" } }); + writeFileSync(join(dir, "state_store.db", "mem%3Aaudit.bin"), content); + process.env["AGENTMEMORY_DATA_DIR"] = dir; + + const result = await probeLegacyAuditFile(); + expect(result.safe).toBe(true); + if (result.safe && !result.empty) { + expect(result.sizeBytes).toBe(Buffer.byteLength(content)); + } else { + throw new Error("expected a non-empty safe probe result"); + } + }); + + it("reports too-large when the legacy scope file exceeds AGENTMEMORY_AUDIT_MIGRATE_MAX_BYTES", async () => { + mkdirSync(join(dir, "state_store.db"), { recursive: true }); + writeFileSync(join(dir, "state_store.db", "mem%3Aaudit.bin"), "x".repeat(1000)); + process.env["AGENTMEMORY_DATA_DIR"] = dir; + process.env["AGENTMEMORY_AUDIT_MIGRATE_MAX_BYTES"] = "10"; + + const result = await probeLegacyAuditFile(); + expect(result).toEqual({ safe: false, reason: "too-large", sizeBytes: 1000 }); + }); +}); + describe("startAuditMigration", () => { let kv: ReturnType; @@ -185,7 +323,10 @@ describe("startAuditMigration", () => { operation: "index_persist", }); - await startAuditMigration(kv as never); + await startAuditMigration(kv as never, { + probe: SMALL_STORE_PROBE, + deleteDelayMs: NO_DELAY, + }); const legacyRemaining = await kv.list(KV.audit); expect(legacyRemaining).toHaveLength(0); @@ -210,6 +351,13 @@ describe("startAuditMigration", () => { expect(summaryRows).toHaveLength(1); expect(summaryRows[0].details.purged).toBe(2); expect(summaryRows[0].details.migrated).toBe(2); + + const marker = await kv.get( + KV.auditMonths, + AUDIT_MIGRATION_STATE_KEY, + ); + expect(marker?.status).toBe("done"); + expect(marker?.safeToListLegacy).toBe(true); }); it("records no summary row when there is nothing to purge", async () => { @@ -218,7 +366,10 @@ describe("startAuditMigration", () => { operation: "delete", }); - await startAuditMigration(kv as never); + await startAuditMigration(kv as never, { + probe: SMALL_STORE_PROBE, + deleteDelayMs: NO_DELAY, + }); const currentMonth = auditMonthOf(new Date().toISOString()); const currentMonthRows = await kv.list( @@ -230,9 +381,203 @@ describe("startAuditMigration", () => { }); it("is a no-op on a store with no legacy audit rows", async () => { - await startAuditMigration(kv as never); + await startAuditMigration(kv as never, { + probe: EMPTY_STORE_PROBE, + deleteDelayMs: NO_DELAY, + }); const months = await listAuditMonthsDesc(kv as never); expect(months).toHaveLength(0); + const marker = await kv.get( + KV.auditMonths, + AUDIT_MIGRATION_STATE_KEY, + ); + expect(marker?.status).toBe("done"); + }); + + it("leaves the legacy scope untouched and frozen when the probe reports too-large", async () => { + seedEntry(kv, KV.audit, "2020-03-01T00:00:00.000Z", { id: "keep_1" }); + + await startAuditMigration(kv as never, { + probe: async () => ({ safe: false, reason: "too-large", sizeBytes: 99999999 }), + deleteDelayMs: NO_DELAY, + }); + + const legacy = await kv.list(KV.audit); + expect(legacy.map((r) => r.id)).toEqual(["keep_1"]); + + const months = await listAuditMonthsDesc(kv as never); + expect(months).toHaveLength(0); + + const marker = await kv.get( + KV.auditMonths, + AUDIT_MIGRATION_STATE_KEY, + ); + expect(marker).toEqual({ + status: "too-large", + safeToListLegacy: false, + legacySizeBytes: 99999999, + migrated: 0, + purged: 0, + summaryWritten: false, + checkedAt: marker?.checkedAt, + }); + }); + + it("leaves the legacy scope untouched and frozen when the probe reports unreadable", async () => { + seedEntry(kv, KV.audit, "2020-03-01T00:00:00.000Z", { id: "keep_1" }); + + await startAuditMigration(kv as never, { + probe: async () => ({ safe: false, reason: "unreadable" }), + deleteDelayMs: NO_DELAY, + }); + + const legacy = await kv.list(KV.audit); + expect(legacy.map((r) => r.id)).toEqual(["keep_1"]); + + const marker = await kv.get( + KV.auditMonths, + AUDIT_MIGRATION_STATE_KEY, + ); + expect(marker?.status).toBe("unreadable"); + expect(marker?.safeToListLegacy).toBe(false); + }); + + it("waits before deleting so the legacy rows survive until the delay elapses", async () => { + seedEntry(kv, KV.audit, "2020-03-01T00:00:00.000Z", { id: "keep_1" }); + + let releaseDelay: () => void = () => {}; + const delayGate = new Promise((resolve) => { + releaseDelay = resolve; + }); + + const migration = startAuditMigration(kv as never, { + probe: SMALL_STORE_PROBE, + deleteDelayMs: async () => { + await delayGate; + return 0; + }, + }); + + await new Promise((resolve) => setTimeout(resolve, 20)); + + const marker = await kv.get( + KV.auditMonths, + AUDIT_MIGRATION_STATE_KEY, + ); + expect(marker?.status).toBe("copied"); + + const midLegacy = await kv.list(KV.audit); + expect(midLegacy.map((r) => r.id)).toEqual(["keep_1"]); + const midMonthRows = await kv.list(KV.auditMonth("2020-03")); + expect(midMonthRows.map((r) => r.id)).toEqual(["keep_1"]); + + releaseDelay(); + await migration; + + const finalLegacy = await kv.list(KV.audit); + expect(finalLegacy).toHaveLength(0); + }); + + it("deletes legacy rows in reverse insertion order", async () => { + const first = seedEntry(kv, KV.audit, "2020-01-01T00:00:00.000Z", { + id: "first", + }); + const second = seedEntry(kv, KV.audit, "2020-01-02T00:00:00.000Z", { + id: "second", + }); + const third = seedEntry(kv, KV.audit, "2020-01-03T00:00:00.000Z", { + id: "third", + }); + void first; + void second; + void third; + + const deleteOrder: string[] = []; + const orderedKv = { + ...kv, + delete: async (scope: string, key: string) => { + if (scope === KV.audit) deleteOrder.push(key); + return kv.delete(scope, key); + }, + }; + + await startAuditMigration(orderedKv as never, { + probe: SMALL_STORE_PROBE, + deleteDelayMs: NO_DELAY, + }); + + expect(deleteOrder).toEqual(["third", "second", "first"]); + }); + + it("resumes after a crash mid-delete without double-counting or double-writing the summary", async () => { + seedEntry(kv, KV.audit, "2020-03-01T00:00:00.000Z", { + id: "keep_1", + operation: "delete", + }); + seedEntry(kv, KV.audit, "2020-03-02T00:00:00.000Z", { + id: "keep_2", + operation: "delete", + }); + seedEntry(kv, KV.audit, "2020-03-03T00:00:00.000Z", { + id: "noise_1", + operation: "index_persist", + }); + + let deleteCalls = 0; + const crashingKv = { + ...kv, + delete: async (scope: string, key: string) => { + if (scope === KV.audit) { + deleteCalls++; + if (deleteCalls === 2) throw new Error("simulated crash"); + } + return kv.delete(scope, key); + }, + }; + + await startAuditMigration(crashingKv as never, { + probe: SMALL_STORE_PROBE, + deleteDelayMs: NO_DELAY, + }); + + const midLegacy = await kv.list(KV.audit); + expect(midLegacy.length).toBeGreaterThan(0); + + const midMarker = await kv.get( + KV.auditMonths, + AUDIT_MIGRATION_STATE_KEY, + ); + expect(midMarker?.status).toBe("copied"); + expect(midMarker?.summaryWritten).toBe(false); + + await startAuditMigration(kv as never, { + probe: SMALL_STORE_PROBE, + deleteDelayMs: NO_DELAY, + }); + + const finalLegacy = await kv.list(KV.audit); + expect(finalLegacy).toHaveLength(0); + + const marchRows = await kv.list(KV.auditMonth("2020-03")); + expect(marchRows.map((r) => r.id).sort()).toEqual(["keep_1", "keep_2"]); + + const currentMonth = auditMonthOf(new Date().toISOString()); + const currentMonthRows = await kv.list( + KV.auditMonth(currentMonth), + ); + const summaryRows = currentMonthRows.filter( + (r) => r.operation === "audit_migrate", + ); + expect(summaryRows).toHaveLength(1); + expect(summaryRows[0].details.purged).toBe(1); + expect(summaryRows[0].details.migrated).toBe(2); + + const finalMarker = await kv.get( + KV.auditMonths, + AUDIT_MIGRATION_STATE_KEY, + ); + expect(finalMarker?.status).toBe("done"); + expect(finalMarker?.summaryWritten).toBe(true); }); }); @@ -297,6 +642,32 @@ describe("audit retention sweep", () => { expect(recentRows).toHaveLength(1); }); + it("deletes dropped rows in reverse insertion order", async () => { + const oldMonth = "2020-01"; + await seedMonthIndex(kv, [oldMonth]); + seedEntry(kv, KV.auditMonth(oldMonth), "2020-01-01T00:00:00.000Z", { + id: "first", + }); + seedEntry(kv, KV.auditMonth(oldMonth), "2020-01-02T00:00:00.000Z", { + id: "second", + }); + seedEntry(kv, KV.auditMonth(oldMonth), "2020-01-03T00:00:00.000Z", { + id: "third", + }); + + const deleteOrder: string[] = []; + const orderedKv = { + ...kv, + delete: async (scope: string, key: string) => { + deleteOrder.push(key); + return kv.delete(scope, key); + }, + }; + + await runAuditRetentionSweep(orderedKv as never, 1); + expect(deleteOrder).toEqual(["third", "second", "first"]); + }); + it("does not drop a month added by another writer while the sweep is running", async () => { const oldMonth = "2020-01"; const currentMonth = auditMonthOf(new Date().toISOString()); diff --git a/test/audit.test.ts b/test/audit.test.ts index 12cb66104f..6e9b38c5d6 100644 --- a/test/audit.test.ts +++ b/test/audit.test.ts @@ -60,7 +60,7 @@ describe("Audit Functions", () => { await new Promise((r) => setTimeout(r, 10)); await recordAudit(kv as never, "delete", "fn2", ["b"], {}); - const entries = await queryAudit(kv as never); + const { entries } = await queryAudit(kv as never); expect(entries.length).toBe(2); expect( new Date(entries[0].timestamp).getTime(), @@ -72,7 +72,7 @@ describe("Audit Functions", () => { await recordAudit(kv as never, "delete", "fn2", [], {}); await recordAudit(kv as never, "observe", "fn3", [], {}); - const entries = await queryAudit(kv as never, { operation: "observe" }); + const { entries } = await queryAudit(kv as never, { operation: "observe" }); expect(entries.length).toBe(2); expect(entries.every((e) => e.operation === "observe")).toBe(true); }); @@ -82,13 +82,13 @@ describe("Audit Functions", () => { await new Promise((r) => setTimeout(r, 20)); const late = await recordAudit(kv as never, "delete", "fn2", [], {}); - const entries = await queryAudit(kv as never, { + const { entries } = await queryAudit(kv as never, { dateFrom: late.timestamp, }); expect(entries.length).toBe(1); expect(entries[0].operation).toBe("delete"); - const entriesBefore = await queryAudit(kv as never, { + const { entries: entriesBefore } = await queryAudit(kv as never, { dateTo: early.timestamp, }); expect(entriesBefore.length).toBe(1); @@ -100,7 +100,7 @@ describe("Audit Functions", () => { await recordAudit(kv as never, "observe", `fn${i}`, [], {}); } - const entries = await queryAudit(kv as never, { limit: 3 }); + const { entries } = await queryAudit(kv as never, { limit: 3 }); expect(entries.length).toBe(3); }); }); diff --git a/test/governance.test.ts b/test/governance.test.ts index 6390bbeea6..76334ee329 100644 --- a/test/governance.test.ts +++ b/test/governance.test.ts @@ -247,7 +247,9 @@ describe("Governance Functions", () => { reason: "cleanup", }); - const entries = (await sdk.trigger("mem::audit-query", {})) as AuditEntry[]; + const { entries } = (await sdk.trigger("mem::audit-query", {})) as { + entries: AuditEntry[]; + }; expect(entries.length).toBe(1); expect(entries[0].operation).toBe("delete"); diff --git a/test/status.test.ts b/test/status.test.ts index 6ecfb50ca5..59b652bde4 100644 --- a/test/status.test.ts +++ b/test/status.test.ts @@ -35,6 +35,7 @@ function inputs(overrides: Partial = {}): StatusInputs { updatedAt: "2026-09-24T11:59:00.000Z", }, graphExtractionEnabled: true, + auditLegacy: null, ...overrides, }; } @@ -217,8 +218,9 @@ describe("status wiring", () => { it("time-boxes every status probe so a slow store cannot hang the page", () => { const handler = api.slice(api.indexOf('registerFunction("api::status"'), api.indexOf('function_id: "api::status"')); - expect(handler.match(/valueWithin\(/g)?.length).toBe(4); + expect(handler.match(/valueWithin\(/g)?.length).toBe(5); expect(handler).toMatch(/valueWithin\(sharedUnindexedScan\(\), STATUS_CHECK_TIMEOUT_MS\)/); + expect(handler).toMatch(/valueWithin\(\s*kv\.get\(KV\.auditMonths, AUDIT_MIGRATION_STATE_KEY\),\s*STATUS_CHECK_TIMEOUT_MS,\s*\)/); expect(api).toMatch(/const sharedUnindexedScan = singleFlight\(\(\) => findUnindexedObservations\(kv\), UNINDEXED_SCAN_REUSE_MS\);/); }); From edd239f4b138acac36d417c05c9a8040c9c0034c Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Mon, 28 Sep 2026 11:13:33 +0530 Subject: [PATCH 6/6] fix(audit): stop calling a copied legacy log frozen and export the deploy data dir A migration in the copied state has already moved every legacy row into the month scopes; only the delete pass is pending. queryAudit and the status page treated any state other than done as frozen, so a failed delete pass reported rows as missing from audit queries until the next boot. Only too-large and unreadable now count as frozen. The deploy entrypoints kept /data in a shell-only DATA_DIR variable while the engine config writes state under /data. With AGENTMEMORY_DATA_DIR unset, the CLI resolved a platform default directory instead, so the legacy audit probe looked in the wrong place and left the migration frozen. The entrypoints now export the directory they use. --- deploy/coolify/entrypoint.sh | 1 + deploy/fly/entrypoint.sh | 1 + deploy/railway/entrypoint.sh | 1 + deploy/render/entrypoint.sh | 1 + src/functions/audit.ts | 2 +- src/functions/status.ts | 2 +- test/audit-retention.test.ts | 30 +++++++++++++++++++++++++++++- test/status.test.ts | 15 +++++++++++++++ 8 files changed, 50 insertions(+), 3 deletions(-) diff --git a/deploy/coolify/entrypoint.sh b/deploy/coolify/entrypoint.sh index c21ec1d898..808f7fe131 100755 --- a/deploy/coolify/entrypoint.sh +++ b/deploy/coolify/entrypoint.sh @@ -16,6 +16,7 @@ set -eu DATA_DIR="${AGENTMEMORY_DATA_DIR:-/data}" +export AGENTMEMORY_DATA_DIR="$DATA_DIR" HMAC_FILE="${AGENTMEMORY_HMAC_FILE:-/data/.hmac}" RUN_AS="node:node" III_CONFIG="/opt/agentmemory/node_modules/@agentmemory/agentmemory/dist/iii-config.yaml" diff --git a/deploy/fly/entrypoint.sh b/deploy/fly/entrypoint.sh index 5b20f45113..befe7106d4 100755 --- a/deploy/fly/entrypoint.sh +++ b/deploy/fly/entrypoint.sh @@ -16,6 +16,7 @@ set -eu DATA_DIR="${AGENTMEMORY_DATA_DIR:-/data}" +export AGENTMEMORY_DATA_DIR="$DATA_DIR" HMAC_FILE="${AGENTMEMORY_HMAC_FILE:-/data/.hmac}" RUN_AS="node:node" III_CONFIG="/opt/agentmemory/node_modules/@agentmemory/agentmemory/dist/iii-config.yaml" diff --git a/deploy/railway/entrypoint.sh b/deploy/railway/entrypoint.sh index c21ec1d898..808f7fe131 100755 --- a/deploy/railway/entrypoint.sh +++ b/deploy/railway/entrypoint.sh @@ -16,6 +16,7 @@ set -eu DATA_DIR="${AGENTMEMORY_DATA_DIR:-/data}" +export AGENTMEMORY_DATA_DIR="$DATA_DIR" HMAC_FILE="${AGENTMEMORY_HMAC_FILE:-/data/.hmac}" RUN_AS="node:node" III_CONFIG="/opt/agentmemory/node_modules/@agentmemory/agentmemory/dist/iii-config.yaml" diff --git a/deploy/render/entrypoint.sh b/deploy/render/entrypoint.sh index c21ec1d898..808f7fe131 100755 --- a/deploy/render/entrypoint.sh +++ b/deploy/render/entrypoint.sh @@ -16,6 +16,7 @@ set -eu DATA_DIR="${AGENTMEMORY_DATA_DIR:-/data}" +export AGENTMEMORY_DATA_DIR="$DATA_DIR" HMAC_FILE="${AGENTMEMORY_HMAC_FILE:-/data/.hmac}" RUN_AS="node:node" III_CONFIG="/opt/agentmemory/node_modules/@agentmemory/agentmemory/dist/iii-config.yaml" diff --git a/src/functions/audit.ts b/src/functions/audit.ts index 4f7d0f9b5d..93395b8b0c 100644 --- a/src/functions/audit.ts +++ b/src/functions/audit.ts @@ -275,7 +275,7 @@ export async function queryAudit( if (migrationState?.safeToListLegacy) { const legacyRows = await kv.list(KV.audit); for (const row of legacyRows) addIfNew(row); - } else if (migrationState && migrationState.status !== "done") { + } else if (migrationState?.status === "too-large" || migrationState?.status === "unreadable") { legacyFrozen = true; legacyFrozenBytes = migrationState.legacySizeBytes; } diff --git a/src/functions/status.ts b/src/functions/status.ts index 5d2752b4a7..bed99af193 100644 --- a/src/functions/status.ts +++ b/src/functions/status.ts @@ -253,7 +253,7 @@ export function evaluateStatus(input: StatusInputs): StatusReport { } } - if (input.auditLegacy && input.auditLegacy.status !== "done") { + if (input.auditLegacy?.status === "too-large" || input.auditLegacy?.status === "unreadable") { const size = input.auditLegacy.sizeBytes; const sizeText = typeof size === "number" ? ` (${Math.round(size / (1024 * 1024))} MiB)` : ""; problems.push({ diff --git a/test/audit-retention.test.ts b/test/audit-retention.test.ts index 70c1911889..9aebd1d13a 100644 --- a/test/audit-retention.test.ts +++ b/test/audit-retention.test.ts @@ -166,7 +166,7 @@ describe("queryAudit across months", () => { expect(listedScopes).not.toContain(KV.audit); }); - it("does not list the legacy scope, and reports it frozen, when no marker says it is safe", async () => { + it("does not list the legacy scope, and does not report it frozen, before any migration marker exists", async () => { await seedMonthIndex(kv, ["2026-08"]); seedEntry(kv, KV.auditMonth("2026-08"), "2026-08-01T00:00:00.000Z", { id: "aug_1", @@ -213,6 +213,34 @@ describe("queryAudit across months", () => { expect(listedScopes).not.toContain(KV.audit); }); + it("does not report the legacy scope frozen once its rows are copied and only the delete pass remains", async () => { + await seedMonthIndex(kv, ["2026-05"]); + seedEntry(kv, KV.auditMonth("2026-05"), "2026-05-01T00:00:00.000Z", { + id: "legacy_1", + }); + seedEntry(kv, KV.audit, "2026-05-01T00:00:00.000Z", { id: "legacy_1" }); + await seedMigrationState(kv, { + status: "copied", + safeToListLegacy: false, + legacySizeBytes: 1024, + migrated: 1, + purged: 0, + summaryWritten: false, + checkedAt: new Date().toISOString(), + }); + + const listSpy = vi.spyOn(kv, "list"); + const { entries, legacyFrozen, legacyFrozenBytes } = await queryAudit(kv as never, { + limit: 5, + }); + + expect(entries.map((e) => e.id)).toEqual(["legacy_1"]); + expect(legacyFrozen).toBe(false); + expect(legacyFrozenBytes).toBeUndefined(); + const listedScopes = listSpy.mock.calls.map((call) => call[0]); + expect(listedScopes).not.toContain(KV.audit); + }); + it("respects operation and date filters across months", async () => { await seedMonthIndex(kv, ["2026-07", "2026-08"]); seedEntry(kv, KV.auditMonth("2026-07"), "2026-07-10T00:00:00.000Z", { diff --git a/test/status.test.ts b/test/status.test.ts index 560a810560..52392c76ec 100644 --- a/test/status.test.ts +++ b/test/status.test.ts @@ -139,6 +139,21 @@ describe("evaluateStatus", () => { expect(report.status).toBe("info"); expect(report.problems[0].code).toBe("no-llm-provider"); }); + + it("reports a frozen legacy audit log only when the migration left it in place", () => { + expect(codes(evaluateStatus(inputs({ auditLegacy: { status: "too-large", sizeBytes: 1 } })))).toContain( + "audit-legacy-frozen", + ); + expect(codes(evaluateStatus(inputs({ auditLegacy: { status: "unreadable" } })))).toContain( + "audit-legacy-frozen", + ); + expect(codes(evaluateStatus(inputs({ auditLegacy: { status: "copied", sizeBytes: 1 } })))).not.toContain( + "audit-legacy-frozen", + ); + expect(codes(evaluateStatus(inputs({ auditLegacy: { status: "done" } })))).not.toContain( + "audit-legacy-frozen", + ); + }); }); describe("missing health snapshot", () => {