Skip to content

Commit 649e906

Browse files
committed
fix(proposal-a): session_end hook clean composite keys to prevent memory leak
When config.autoCapture === false, the auto-capture session_end (priority 10) was skipped, leaving only the Phase 1 session_end (priority 20) to clean up. The old code only deleted pendingRecall[sessionKey] - a simple key - but not composite keys (sessionKey:agentId). Now uses pattern matching (startsWith) to clean all related keys regardless of format. Fixes: P1 issue from Phase 1 audit
1 parent bd0c582 commit 649e906

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

index.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3206,9 +3206,20 @@ const memoryLanceDBProPlugin = {
32063206
// Proposal A Phase 1: session_end hook - Clean up pending recalls
32073207
// ========================================================================
32083208
api.on("session_end", (_event: any, ctx: any) => {
3209-
const sessionKey = ctx?.sessionKey || ctx?.sessionId || "default";
3210-
if (sessionKey) {
3211-
pendingRecall.delete(sessionKey);
3209+
// P1 fix: clean all pendingRecall entries for this session, including composite keys.
3210+
// When autoCapture is false, the auto-capture session_end (priority 10) is skipped,
3211+
// so this hook must handle composite keys (sessionKey:agentId) as well.
3212+
const sessionId = ctx?.sessionId || "";
3213+
const sessionKey = ctx?.sessionKey || "";
3214+
for (const key of pendingRecall.keys()) {
3215+
if (
3216+
key === sessionKey ||
3217+
key === sessionId ||
3218+
key.startsWith(`${sessionKey}:`) ||
3219+
key.startsWith(`${sessionId}:`)
3220+
) {
3221+
pendingRecall.delete(key);
3222+
}
32123223
}
32133224
}, { priority: 20 });
32143225

0 commit comments

Comments
 (0)