fix: reduce session.messages() calls with event-based caching to prevent memory leaks #1787
+700
−371
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Eliminate redundant
session.messages()API calls in hot paths to fix memory leaks during extended sessions.Root Cause
Multiple hooks call
session.messages()(full session history fetch) on every tool execution, causing memory to grow proportionally with session length. In a 200+ message session, each tool call triggered 3-4 full fetches of the entire message history.Changes
context-window-monitor.tssession.messages()withmessage.updatedevent cachepreemptive-compaction.tstranscript.tsbackground-agent/manager.tssession.messages()from polling looptodo-continuation-enforcer/session-state.tstool-input-cache.tssetInterval.unref()Impact
session.messages()calls per tool execution + continuous polling fetchesTesting
bun run typecheck✅bun run build✅Verification
Related Issues
Fixes #1222
Related: #1769, #1752, #1486, #361
Summary by cubic
Replaced repeated session.messages() fetches with event-based caching to stop memory leaks in long sessions and cut API calls to near-zero during tool runs. Addresses #1222 by moving hot paths to use message.updated data and adding TTL-based cleanup.
Written for commit eb56701. Summary will update on new commits.