Skip to content

Commit 835dd2d

Browse files
Evsdrgdeepseek-v4-pro[1m]
andauthored
fix: 为 sessionStorage existingSessionFiles Map 添加容量上限 (#1227)
* fix: 修复子代理 token 消耗在主 spinner 中始终显示为 0 Spinner.tsx 的 token 聚合循环仅统计 in_process_teammate 类型任务, 漏掉了 local_agent(后台代理/verification agent)类型。当后台代理 运行时,主界面 spinner 一直显示 "↓ 0 tokens",因为 background agent 的 token 消耗未被纳入 teammateTokens 聚合。 同时在 inProcessRunner.ts 中,进程内队友完成时计算并设置 result (含 totalTokens/totalToolUseCount/content/usage),使详情弹窗可以 正确展示累计 token 消耗,不再仅依赖 progress.tokenCount 间歇更新。 Co-Authored-By: deepseek-v4-pro[1m] <deepseek-ai@claude-code-best.win> * 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> * fix: 为 sessionStorage existingSessionFiles Map 添加容量上限 existingSessionFiles Map 缓存 sessionId → 文件路径映射以避免重复 stat 调用,但在 coordinator/swarm 模式下,每个子代理产生独立 sessionId,长时间运行的 daemon 会话可能累积数千条目。 新增 MAX_CACHED_SESSION_FILES = 200 上限,新增条目时若达到上限则 逐出最早插入的条目。同时在 _resetFlushState() 中清除此缓存以保证 测试隔离。 Co-Authored-By: deepseek-v4-pro[1m] <deepseek-ai@claude-code-best.win> --------- Co-authored-by: deepseek-v4-pro[1m] <deepseek-ai@claude-code-best.win>
1 parent 0face46 commit 835dd2d

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

src/utils/sessionStorage.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,10 @@ export function setRemoteIngressUrlForTesting(url: string): void {
529529

530530
const REMOTE_FLUSH_INTERVAL_MS = 10
531531

532+
// Limit the number of cached session-file lookups to prevent unbounded Map growth
533+
// in long-running daemon / swarm sessions that spawn many sub-agents.
534+
const MAX_CACHED_SESSION_FILES = 200
535+
532536
class Project {
533537
// Minimal cache for current session only (not all sessions)
534538
currentSessionTag: string | undefined
@@ -577,6 +581,7 @@ class Project {
577581
this.flushTimer = null
578582
this.activeDrain = null
579583
this.writeQueues = new Map()
584+
this.existingSessionFiles = new Map()
580585
}
581586

582587
private incrementPendingWrites(): void {
@@ -1288,6 +1293,9 @@ class Project {
12881293
* Returns the session file path if it exists, null otherwise.
12891294
* Used for writing to sessions other than the current one.
12901295
* Caches positive results so we only stat once per session.
1296+
*
1297+
* The cache is bounded at MAX_CACHED_SESSION_FILES to prevent unbounded
1298+
* growth in long-running daemon / swarm sessions that spawn many agents.
12911299
*/
12921300
private existingSessionFiles = new Map<string, string>()
12931301
private async getExistingSessionFile(
@@ -1299,6 +1307,13 @@ class Project {
12991307
const targetFile = getTranscriptPathForSession(sessionId)
13001308
try {
13011309
await stat(targetFile)
1310+
// Evict oldest entry when at capacity so the Map stays bounded
1311+
if (this.existingSessionFiles.size >= MAX_CACHED_SESSION_FILES) {
1312+
const oldestKey = this.existingSessionFiles.keys().next().value
1313+
if (oldestKey !== undefined) {
1314+
this.existingSessionFiles.delete(oldestKey)
1315+
}
1316+
}
13021317
this.existingSessionFiles.set(sessionId, targetFile)
13031318
return targetFile
13041319
} catch (e) {

0 commit comments

Comments
 (0)