Browse Source

fix: bound session metadata maps (#908)

Ulises Millan Guerrero 2 days ago
parent
commit
ab4286c087
2 changed files with 22 additions and 0 deletions
  1. 7 0
      src/config/constants.ts
  2. 15 0
      src/index.ts

+ 7 - 0
src/config/constants.ts

@@ -93,6 +93,13 @@ export const DEFAULT_READ_CONTEXT_MIN_LINES = 10;
 export const DEFAULT_READ_CONTEXT_MAX_FILES = 8;
 export const DEFAULT_MAX_RETAINED_SNAPSHOTS = 20;
 
+/**
+ * Maximum session-directory mappings retained per plugin instance.
+ * Prevents unbounded growth when session.deleted events are missed.
+ * Oldest entries are evicted first when this threshold is reached.
+ */
+export const DEFAULT_MAX_SESSION_DIRECTORIES = 1000;
+
 export type ImageRouting = 'auto' | 'direct';
 
 /**

+ 15 - 0
src/index.ts

@@ -18,6 +18,7 @@ import { parseList } from './config/agent-mcps';
 import {
   AGENT_ALIASES,
   DEFAULT_MAX_RETAINED_SNAPSHOTS,
+  DEFAULT_MAX_SESSION_DIRECTORIES,
   DEFAULT_MAX_SESSIONS_PER_AGENT,
   DEFAULT_READ_CONTEXT_MAX_FILES,
   DEFAULT_READ_CONTEXT_MIN_LINES,
@@ -972,6 +973,20 @@ const OhMyOpenCodeLite: Plugin = async (ctx) => {
         const createdSessionId = event.properties?.info?.id;
         const createdSessionDir = event.properties?.info?.directory;
         if (createdSessionId && createdSessionDir) {
+          // Guard against unbounded growth when session.deleted events
+          // are missed: evict the oldest entry when the threshold is
+          // reached, keeping both maps aligned.
+          if (sessionDirectories.size >= DEFAULT_MAX_SESSION_DIRECTORIES) {
+            const oldestId = sessionDirectories.keys().next().value;
+            if (oldestId !== undefined) {
+              sessionDirectories.delete(oldestId);
+              sessionAgentMap.delete(oldestId);
+              log('[session] evicted oldest session-directory mapping', {
+                threshold: DEFAULT_MAX_SESSION_DIRECTORIES,
+                droppedSessionId: oldestId,
+              });
+            }
+          }
           sessionDirectories.set(createdSessionId, createdSessionDir);
         }
       }