fix: 修复内存溢出问题,compact 时清理持久增长数据结构

- compact 时清理 contentReplacementState(seenIds/replacements)
- logError() 使用 shortErrorStack 替代完整 err.stack,减少 GC 压力
- permissionDenials 每次 submitMessage 清空,防止无限增长
- SSE 缓冲区添加 1MB 上限,防止畸形数据无限累积

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
claude-code-best
2026-05-01 22:24:18 +08:00
parent a81995052f
commit ab0bbbc4b5
5 changed files with 35 additions and 3 deletions

View File

@@ -10,6 +10,17 @@ import { clearSessionMessagesCache } from '../../utils/sessionStorage.js'
import { clearBetaTracingState } from '../../utils/telemetry/betaSessionTracing.js'
import { resetMicrocompactState } from './microCompact.js'
/**
* Compact-scoped cleanup callbacks registered by REPL or other long-lived
* components. Called during runPostCompactCleanup() so instance-scoped state
* (e.g. contentReplacementState) is freed alongside module-level caches.
*/
const compactCleanupCallbacks: Array<() => void> = []
export function registerCompactCleanup(callback: () => void): void {
compactCleanupCallbacks.push(callback)
}
/**
* Run cleanup of caches and tracking state after compaction.
* Call this after both auto-compact and manual /compact to free memory
@@ -88,4 +99,11 @@ export function runPostCompactCleanup(querySource?: QuerySource): void {
})
}
clearSessionMessagesCache()
for (const cb of compactCleanupCallbacks) {
try {
cb()
} catch (error) {
logError(error)
}
}
}