Skip to content

fix(audit): make index_persist audit entries opt-in - #1182

Open
dmazhukov wants to merge 1 commit into
rohitg00:mainfrom
dmazhukov:fix/1181-index-persist-audit-optin
Open

fix(audit): make index_persist audit entries opt-in#1182
dmazhukov wants to merge 1 commit into
rohitg00:mainfrom
dmazhukov:fix/1181-index-persist-audit-optin

Conversation

@dmazhukov

@dmazhukov dmazhukov commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #1181.

IndexPersistence audits its own index flushes, and those rows dominate mem:audit — 59 876
of 84 028 entries (71%) on the store this was found on. They also sit outside what the log is
defined to hold: the policy at the top of src/functions/audit.ts scopes it to structural
deletions of user data, and an index shard write deletes none.

What this changes

auditIndexPersistence() returns early unless AGENTMEMORY_AUDIT_INDEX_PERSIST is set to
1 or true. Everything else is untouched — shards, manifests and cleanups are written
exactly as before, only the audit rows about them stop.

Off by default because the default should match the stated policy. The flag exists because
these rows are genuinely useful when debugging index persistence itself, which is when you
want them and the only time the volume is acceptable.

Why not retention

Retention would cap the symptom for every operation at once, which is a bigger design question
— what to keep, for how long, and whether a deletion record may ever expire. It deserves its
own issue. This change needs no such decision: the policy already exists and this code sits
outside it.

Behaviour

An install that wants the rows back sets one environment variable. An install that does not
loses nothing it was entitled to under the documented policy, and memory_audit stops paying
~2.2 s to read rows about shard writes.

Testing

test/index-persistence.test.ts gains three cases: no index_persist rows by default, rows
present when the flag is set, and the index still round-trips through save()/load() with
auditing off — the last one guards against gating more than the audit call.

  • npx vitest run --exclude test/integration.test.ts → 1599 passed, 0 failed.
  • npx tsc --noEmit → 25 errors, identical to main.
  • npm run skills:check and npm run build clean.

plugin/skills/agentmemory-config/REFERENCE.md is the generator's output for the new variable
(npm run skills:gen), not a hand edit.

Summary by CodeRabbit

  • New Features

    • Added an environment setting to control index-persistence auditing.
    • Auditing is enabled only when the setting is explicitly set to 1 or true.
  • Documentation

    • Updated the configuration reference with the new setting.
  • Bug Fixes

    • Index persistence continues to work when auditing is disabled.

Signed-off-by: Dmitrii Zhukov <dmitry0983@gmail.com>
@vercel

vercel Bot commented Aug 12, 2026

Copy link
Copy Markdown

@dmazhukov is attempting to deploy a commit to the rohitg00's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Index persistence auditing is now opt-in through AGENTMEMORY_AUDIT_INDEX_PERSIST. The configuration reference documents the variable, and tests cover disabled auditing, explicit enablement, and unaffected index persistence.

Changes

Index persistence audit gating

Layer / File(s) Summary
Add the audit environment switch
src/state/index-persistence.ts, plugin/skills/agentmemory-config/REFERENCE.md
Index persistence auditing runs only when AGENTMEMORY_AUDIT_INDEX_PERSIST is 1 or true, after trimming and case normalization. The configuration reference lists the variable.
Validate audit gating and persistence
test/index-persistence.test.ts
Tests verify default-disabled auditing, explicit enablement, and continued BM25 index save and load behavior when auditing is disabled.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: rohitg00, rokurolize

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly states that index_persist audit entries are now opt-in, which is the main change.
Linked Issues check ✅ Passed The changes implement the issue proposal by disabling index_persist auditing by default while preserving index persistence and supporting explicit opt-in.
Out of Scope Changes check ✅ Passed The implementation, documentation, and tests directly support the linked issue and do not introduce unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
test/index-persistence.test.ts (1)

825-836: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Cover all accepted environment values.

auditIndexPersistEnabled() accepts trimmed, case-insensitive "1" and "true". This test covers only "1". Add cases for "true", " TRUE ", and rejected values such as "0" and "yes".

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/index-persistence.test.ts` around lines 825 - 836, Expand the audit
index-persistence test around auditIndexPersistEnabled() to cover accepted
values "true" and whitespace-padded, case-insensitive " TRUE ", in addition to
"1". Add rejected-value cases for "0" and "yes", asserting enabled values
produce index_persist entries and rejected values do not.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@test/index-persistence.test.ts`:
- Around line 798-806: Update the test suite’s beforeEach and afterEach hooks to
isolate AGENTMEMORY_AUDIT_INDEX_PERSIST: capture its original value before
clearing it in beforeEach, then restore that captured value in afterEach instead
of always deleting the variable. Keep fake-timer setup and teardown unchanged.

---

Nitpick comments:
In `@test/index-persistence.test.ts`:
- Around line 825-836: Expand the audit index-persistence test around
auditIndexPersistEnabled() to cover accepted values "true" and
whitespace-padded, case-insensitive " TRUE ", in addition to "1". Add
rejected-value cases for "0" and "yes", asserting enabled values produce
index_persist entries and rejected values do not.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 41422e79-0301-4120-841f-d2b89a8941a9

📥 Commits

Reviewing files that changed from the base of the PR and between 2973e4e and efa8152.

📒 Files selected for processing (3)
  • plugin/skills/agentmemory-config/REFERENCE.md
  • src/state/index-persistence.ts
  • test/index-persistence.test.ts

Comment on lines +798 to +806
beforeEach(() => {
vi.useFakeTimers();
kv = mockKV();
});

afterEach(() => {
vi.useRealTimers();
delete process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST;
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Reset the audit flag before each test.

If the test process starts with AGENTMEMORY_AUDIT_INDEX_PERSIST=1 or true, the default-disabled test at Lines 813-823 and the audit-off round-trip at Lines 838-850 run with auditing enabled. afterEach also deletes any value that existed before this suite. Capture and restore the original value, and clear the variable in beforeEach.

Proposed isolation fix
   let kv: ReturnType<typeof mockKV>;
+  let previousAuditFlag: string | undefined;

   beforeEach(() => {
+    previousAuditFlag = process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST;
+    delete process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST;
     vi.useFakeTimers();
     kv = mockKV();
   });

   afterEach(() => {
     vi.useRealTimers();
-    delete process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST;
+    if (previousAuditFlag === undefined) {
+      delete process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST;
+    } else {
+      process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST = previousAuditFlag;
+    }
   });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
beforeEach(() => {
vi.useFakeTimers();
kv = mockKV();
});
afterEach(() => {
vi.useRealTimers();
delete process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST;
});
let kv: ReturnType<typeof mockKV>;
let previousAuditFlag: string | undefined;
beforeEach(() => {
previousAuditFlag = process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST;
delete process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST;
vi.useFakeTimers();
kv = mockKV();
});
afterEach(() => {
vi.useRealTimers();
if (previousAuditFlag === undefined) {
delete process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST;
} else {
process.env.AGENTMEMORY_AUDIT_INDEX_PERSIST = previousAuditFlag;
}
});
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/index-persistence.test.ts` around lines 798 - 806, Update the test
suite’s beforeEach and afterEach hooks to isolate
AGENTMEMORY_AUDIT_INDEX_PERSIST: capture its original value before clearing it
in beforeEach, then restore that captured value in afterEach instead of always
deleting the variable. Keep fake-timer setup and teardown unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mem:audit is 71% index_persist rows the audit policy does not cover

1 participant