Skip to content

Commit b3d28bc

Browse files
Evsdrgdeepseek-v4-pro[1m]
andcommitted
fix: 为 cacheWarningStateBySource Map 设置上限防止内存泄漏
Map 以 querySource 为 key 存储每个来源的缓存命中率历史状态, 但 querySource 类型为 `any`,长时间会话中可能产生大量唯一值, Map 持续增长永不清理。 新增 MAX_SOURCE_ENTRIES = 50 上限,新增条目时若达到上限则 逐出最早插入的条目(Map 按插入顺序迭代)。 同时也新增 _resetCacheWarningStateForTest() 用于测试隔离。 Co-Authored-By: deepseek-v4-pro[1m] <deepseek-ai@claude-code-best.win>
1 parent 1f80043 commit b3d28bc

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

src/utils/cacheWarning.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ interface CacheWarningState {
2424
// 模块级状态,每个 querySource 独立跟踪
2525
const cacheWarningStateBySource = new Map<string, CacheWarningState>()
2626

27+
// Limit the number of tracked sources to prevent unbounded Map growth.
28+
// querySource strings are effectively unbounded (typed as `any`), so a
29+
// long-running session that spawns many subagents could leak memory.
30+
// Evict the oldest entry (by insertion order) when the limit is exceeded.
31+
const MAX_SOURCE_ENTRIES = 50
32+
2733
const DEFAULT_CACHE_THRESHOLD = 80
2834

2935
/**
@@ -81,6 +87,13 @@ export function shouldShowCacheWarning(
8187
let state = cacheWarningStateBySource.get(querySource)
8288
if (!state) {
8389
state = { lastHitRate: null, lastTimestamp: null }
90+
// Evict oldest entry when at capacity so the Map stays bounded
91+
if (cacheWarningStateBySource.size >= MAX_SOURCE_ENTRIES) {
92+
const oldestKey = cacheWarningStateBySource.keys().next().value
93+
if (oldestKey !== undefined) {
94+
cacheWarningStateBySource.delete(oldestKey)
95+
}
96+
}
8497
cacheWarningStateBySource.set(querySource, state)
8598
}
8699

@@ -132,3 +145,10 @@ export function createCacheWarningMessage(info: CacheHitRateInfo): Message {
132145
isMeta: false,
133146
} as Message
134147
}
148+
149+
/**
150+
* Reset the per-source tracking state — only used in tests.
151+
*/
152+
export function _resetCacheWarningStateForTest(): void {
153+
cacheWarningStateBySource.clear()
154+
}

0 commit comments

Comments
 (0)