Skip to content

fix(hooks): skip capturing memory_* MCP tools and add capture config env vars#994

Open
joyjit wants to merge 2 commits into
rohitg00:mainfrom
joyjit:fix/hooks-capture-filter-993
Open

fix(hooks): skip capturing memory_* MCP tools and add capture config env vars#994
joyjit wants to merge 2 commits into
rohitg00:mainfrom
joyjit:fix/hooks-capture-filter-993

Conversation

@joyjit

@joyjit joyjit commented Jun 29, 2026

Copy link
Copy Markdown

Summary

Fixes #993.

  • Bug fix: post-tool-use and post-tool-failure now skip agentmemory's own MCP tools (memory_*, plus plumbing like ToolSearch) so recall/search calls are not written back into the observation store.
  • Config knobs: new env vars for capture filtering and limits:
    • AGENTMEMORY_CAPTURE_DENY — extra comma/whitespace-separated deny patterns (default deny includes memory_* and plumbing tools even when unset)
    • AGENTMEMORY_CAPTURE_ALLOW — if set, only listed tools are captured (overrides deny defaults)
    • AGENTMEMORY_CAPTURE_OUTPUT_MAX — per-observation output cap (default 8000)
    • AGENTMEMORY_PRE_COMPACT_BUDGET/agentmemory/context budget on /compact (default 1500; 0 disables injection)

Test plan

  • vitest run test/capture-filter.test.ts test/post-tool-use-capture.test.ts
  • memory_* MCP tool names are skipped by default (including mcp__agentmemory__* prefixed forms)
  • normal tools (Bash, Edit, etc.) still captured
  • hook integration test confirms no /observe POST for filtered tools

Summary by CodeRabbit

  • New Features

    • Added environment-configurable allow/deny filtering to control which tool events are recorded.
    • Added configurable maximum captured tool-output size (with truncation).
    • Added runtime control for the pre-compact context budget, including a “0” option to disable it.
  • Bug Fixes

    • Skips recording for non-captured tools to avoid unnecessary outbound observations.
    • Prevents sending tool output that exceeds the configured limit.
  • Tests

    • Added unit and integration coverage for filtering, output limits, and pre-compact budget behavior.

…env vars

Closes rohitg00#993.

post-tool-use and post-tool-failure now filter agentmemory's own MCP tools
(memory_*, ToolSearch, etc.) so recall/search calls are not written back
into the observation store. Adds AGENTMEMORY_CAPTURE_DENY/ALLOW,
AGENTMEMORY_CAPTURE_OUTPUT_MAX, and AGENTMEMORY_PRE_COMPACT_BUDGET.
@vercel

vercel Bot commented Jun 29, 2026

Copy link
Copy Markdown

@joyjit 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 Jun 29, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7395607f-5285-4ab8-abf1-984b8f005e75

📥 Commits

Reviewing files that changed from the base of the PR and between 948be10 and 09f7fdb.

📒 Files selected for processing (1)
  • test/post-tool-use-capture.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • test/post-tool-use-capture.test.ts

📝 Walkthrough

Walkthrough

Adds a new _capture-filter.ts module exporting shouldCaptureTool, captureOutputMax, and preCompactBudget helpers driven by env vars. Each hook gains early-return or computed-value logic from these helpers. The bundled plugin scripts mirror the same behavior, and unit plus integration tests are added.

Changes

Configurable Tool Capture Filter

Layer / File(s) Summary
Capture filter module
src/hooks/_capture-filter.ts
Defines DEFAULT_DENY_PATTERNS, parseEnvList, bareToolName, wildcard matching, matchesAny, shouldCaptureTool, captureOutputMax, and preCompactBudget.
Hook early-return guards and budgets
src/hooks/post-tool-use.ts, src/hooks/post-tool-failure.ts, src/hooks/pre-compact.ts
Imports filter helpers, adds capture guards, truncates output with captureOutputMax(), and sends the computed pre-compact budget with early exit on 0.
Bundled plugin scripts
plugin/scripts/post-tool-failure.mjs, plugin/scripts/post-tool-use.mjs, plugin/scripts/pre-compact.mjs
Inlines the same capture-filter logic into each bundled MJS file and updates region markers plus module export endings.
Tests
test/capture-filter.test.ts, test/post-tool-use-capture.test.ts
Unit tests cover all capture-filter helpers; integration tests verify filtered and captured tool names through a spawned hook process and local HTTP server.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • rohitg00/agentmemory#688: Modifies the same post-tool-use and post-tool-failure hooks, changing how /agentmemory/observe requests are issued.

Poem

🐇 I hop through hooks both swift and sly,
With deny and allow lists passing by.
memory_* takes a quieter seat,
While budgets and outputs stay neat.
No self-capture loops in sight tonight ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 17.07% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately summarizes the main change: skipping agentmemory MCP captures and adding capture config env vars.
Linked Issues check ✅ Passed The PR implements the requested MCP capture filtering, env-driven limits, and pre-compact budget handling, with tests covering the behavior.
Out of Scope Changes check ✅ Passed The changes stay focused on hook capture filtering and budget/output limits; no clearly unrelated code paths were introduced.
✨ Finishing Touches
🧪 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

🤖 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/post-tool-use-capture.test.ts`:
- Around line 42-57: The observe assertions in the test are relying on fixed
sleeps after the fire-and-forget fetch from the hook script, which makes the
test flaky. Update the test setup around startServer and the observe call checks
to wait on an explicit server-side signal from the request handler, or use a
bounded polling helper, so the assertions do not depend on scheduler timing.
Make the change consistently in the shared test flow that exercises the observe
request, including the repeated sections noted in the comment.
🪄 Autofix (Beta)

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

Run ID: 55e767f3-ff15-4ed3-b5a2-cca6f54d15fa

📥 Commits

Reviewing files that changed from the base of the PR and between 93ae9bc and 948be10.

📒 Files selected for processing (9)
  • plugin/scripts/post-tool-failure.mjs
  • plugin/scripts/post-tool-use.mjs
  • plugin/scripts/pre-compact.mjs
  • src/hooks/_capture-filter.ts
  • src/hooks/post-tool-failure.ts
  • src/hooks/post-tool-use.ts
  • src/hooks/pre-compact.ts
  • test/capture-filter.test.ts
  • test/post-tool-use-capture.test.ts

Comment thread test/post-tool-use-capture.test.ts
Address CodeRabbit review on rohitg00#994 — integration test now resolves when
the mock server receives /observe (or confirms none arrive after hook exit).
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.

post-tool-use captures agentmemory MCP calls as observations, polluting recall results

1 participant