mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-15 21:05:51 +00:00
将 performance-reporter.md 合入 memory-peak-analysis.md,统一分析文档。 代码优化包括:compact 峰值释放、GC 阈值触发、虚拟滚动参数调优、 HybridTransport 队列缩减、无界缓存加 LRU 淘汰、taskSummary 避免数组拷贝。 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
83 lines
2.3 KiB
TypeScript
83 lines
2.3 KiB
TypeScript
import { feature } from 'bun:bundle'
|
|
import { isBgSession, updateSessionActivity } from './concurrentSessions.js'
|
|
import { logForDebugging } from './debug.js'
|
|
|
|
/**
|
|
* Minimum interval between task summary generations (ms).
|
|
* Prevents excessive updates during rapid tool-call loops.
|
|
*/
|
|
const SUMMARY_INTERVAL_MS = 30_000
|
|
|
|
let lastSummaryTime = 0
|
|
|
|
/**
|
|
* Whether a task summary should be generated this turn.
|
|
* Only generates in bg sessions, and rate-limits to avoid churn.
|
|
*/
|
|
export function shouldGenerateTaskSummary(): boolean {
|
|
if (!feature('BG_SESSIONS')) return false
|
|
if (!isBgSession()) return false
|
|
|
|
const now = Date.now()
|
|
return now - lastSummaryTime >= SUMMARY_INTERVAL_MS
|
|
}
|
|
|
|
/**
|
|
* Generate a task summary from the current turn's messages and push it
|
|
* to the session registry so `claude ps` can display live status.
|
|
*
|
|
* Fire-and-forget from query.ts — errors are logged, never thrown.
|
|
*/
|
|
export function maybeGenerateTaskSummary(
|
|
options: Record<string, unknown>,
|
|
): void {
|
|
lastSummaryTime = Date.now()
|
|
|
|
try {
|
|
const messages = options.forkContextMessages as
|
|
| Array<{
|
|
type: string
|
|
message?: { content?: unknown }
|
|
}>
|
|
| undefined
|
|
|
|
if (!messages || messages.length === 0) return
|
|
|
|
// Extract a short status from the most recent assistant message
|
|
let lastAssistant: (typeof messages)[0] | undefined
|
|
for (let i = messages.length - 1; i >= 0; i--) {
|
|
if (messages[i]!.type === 'assistant') {
|
|
lastAssistant = messages[i]
|
|
break
|
|
}
|
|
}
|
|
|
|
let status: 'busy' | 'idle' = 'busy'
|
|
let waitingFor: string | undefined
|
|
|
|
if (lastAssistant?.message?.content) {
|
|
const content = lastAssistant.message.content
|
|
// Check if last block is tool_use
|
|
if (Array.isArray(content)) {
|
|
const lastBlock = content[content.length - 1] as
|
|
| Record<string, unknown>
|
|
| undefined
|
|
if (lastBlock?.type === 'tool_use') {
|
|
status = 'busy'
|
|
waitingFor = `tool: ${lastBlock.name || 'unknown'}`
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fire-and-forget update to session registry
|
|
void updateSessionActivity({
|
|
status,
|
|
waitingFor,
|
|
}).catch(err => {
|
|
logForDebugging(`[taskSummary] updateSessionActivity failed: ${err}`)
|
|
})
|
|
} catch (err) {
|
|
logForDebugging(`[taskSummary] error: ${err}`)
|
|
}
|
|
}
|