- Replace session.messages() fetch in context-window-monitor with message.updated event cache - Replace session.messages() fetch in preemptive-compaction with message.updated event cache - Add per-session transcript cache (5min TTL) to avoid full rebuild per tool call - Remove session.messages() from background-agent polling (use event-based progress) - Add TTL pruning to todo-continuation-enforcer session state Map - Add setInterval.unref() to tool-input-cache cleanup timer Fixes #1222
52 lines
1.2 KiB
TypeScript
52 lines
1.2 KiB
TypeScript
/**
|
|
* Caches tool_input from PreToolUse for PostToolUse
|
|
*/
|
|
|
|
interface CacheEntry {
|
|
toolInput: Record<string, unknown>
|
|
timestamp: number
|
|
}
|
|
|
|
const cache = new Map<string, CacheEntry>()
|
|
|
|
const CACHE_TTL = 60000 // 1 minute
|
|
|
|
export function cacheToolInput(
|
|
sessionId: string,
|
|
toolName: string,
|
|
invocationId: string,
|
|
toolInput: Record<string, unknown>
|
|
): void {
|
|
const key = `${sessionId}:${toolName}:${invocationId}`
|
|
cache.set(key, { toolInput, timestamp: Date.now() })
|
|
}
|
|
|
|
export function getToolInput(
|
|
sessionId: string,
|
|
toolName: string,
|
|
invocationId: string
|
|
): Record<string, unknown> | null {
|
|
const key = `${sessionId}:${toolName}:${invocationId}`
|
|
const entry = cache.get(key)
|
|
if (!entry) return null
|
|
|
|
cache.delete(key)
|
|
if (Date.now() - entry.timestamp > CACHE_TTL) return null
|
|
|
|
return entry.toolInput
|
|
}
|
|
|
|
// Periodic cleanup (every minute)
|
|
const cleanupInterval = setInterval(() => {
|
|
const now = Date.now()
|
|
for (const [key, entry] of cache.entries()) {
|
|
if (now - entry.timestamp > CACHE_TTL) {
|
|
cache.delete(key)
|
|
}
|
|
}
|
|
}, CACHE_TTL)
|
|
// Allow process to exit naturally even if interval is running
|
|
if (typeof cleanupInterval === "object" && "unref" in cleanupInterval) {
|
|
cleanupInterval.unref()
|
|
}
|