Skip to content

feat: add event cleanup mechanism for memory store#98

Open
bianbiandashen wants to merge 1 commit intobenjitaylor:mainfrom
bianbiandashen:feat/memory-store-event-cleanup
Open

feat: add event cleanup mechanism for memory store#98
bianbiandashen wants to merge 1 commit intobenjitaylor:mainfrom
bianbiandashen:feat/memory-store-event-cleanup

Conversation

@bianbiandashen
Copy link

Summary

Add automatic event cleanup for the in-memory store to prevent unbounded memory growth.

Problem

The in-memory store's events array grows indefinitely:

const events: AFSEvent[] = [];  // Never cleaned up

In long-running processes, this leads to:

  • Gradual memory increase
  • Eventually OOM crashes
  • Performance degradation as the array grows

Solution

Implement a dual-strategy cleanup mechanism:

1. Count-based Cleanup

  • Configurable maximum event count
  • Triggers immediate cleanup when at 120% capacity
  • Oldest events are removed first (FIFO)

2. Time-based Cleanup

  • Configurable TTL for events
  • Periodic background cleanup removes expired events
  • Default: 1 hour retention

Configuration (Environment Variables)

Variable Default Description
AGENTATION_MAX_EVENTS 10000 Maximum events to retain
AGENTATION_EVENT_TTL_MS 3600000 Event TTL (1 hour)
AGENTATION_CLEANUP_INTERVAL_MS 300000 Cleanup frequency (5 min)

Implementation Details

  • cleanupEvents(): Removes expired and over-limit events
  • addEvent(): Wrapper that triggers cleanup when needed
  • Cleanup timer properly stopped on close()
  • Logging when events are cleaned up for observability

Changes

  • mcp/src/server/store.ts: Add cleanup logic to memory store

Test plan

  • Verified normal event creation still works
  • Verified events are cleaned up when over limit
  • Verified cleanup timer stops on close()
  • Verified configuration via environment variables

Prevent unbounded memory growth in long-running processes by adding
automatic event cleanup based on count and age limits.

Features:
- Maximum event count limit (triggers cleanup at 120% capacity)
- Time-based expiration (removes events older than TTL)
- Periodic background cleanup to catch gradual accumulation
- Detailed logging when events are cleaned up

Configuration (environment variables):
- AGENTATION_MAX_EVENTS: Max events to retain (default: 10000)
- AGENTATION_EVENT_TTL_MS: Event time-to-live (default: 1 hour)
- AGENTATION_CLEANUP_INTERVAL_MS: Cleanup frequency (default: 5 min)

The cleanup timer is properly stopped when the store is closed.
@vercel
Copy link

vercel bot commented Feb 14, 2026

@bianbiandashen is attempting to deploy a commit to the Benji Taylor's Projects Team on Vercel.

A member of the Team first needs to authorize it.

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.

1 participant