From 217156c3c8e535280a1ab8676451f8497916e9f2 Mon Sep 17 00:00:00 2001 From: Spencer Tang Date: Wed, 8 Apr 2026 17:06:35 -0400 Subject: [PATCH] fix(core): cap chat history at 1000 items to prevent unbounded memory growth --- packages/cli/src/ui/hooks/useHistoryManager.ts | 7 ++++++- packages/core/src/services/chatRecordingService.ts | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/ui/hooks/useHistoryManager.ts b/packages/cli/src/ui/hooks/useHistoryManager.ts index c6ceabb920..4035264484 100644 --- a/packages/cli/src/ui/hooks/useHistoryManager.ts +++ b/packages/cli/src/ui/hooks/useHistoryManager.ts @@ -83,7 +83,12 @@ export function useHistory({ return prevHistory; // Don't add the duplicate } } - return [...prevHistory, newItem]; + const newHistory = [...prevHistory, newItem]; + // Enforce a hard limit of 1000 items to prevent unbounded memory growth + if (newHistory.length > 1000) { + return newHistory.slice(newHistory.length - 1000); + } + return newHistory; }); // Record UI-specific messages, but don't do it if we're actually loading diff --git a/packages/core/src/services/chatRecordingService.ts b/packages/core/src/services/chatRecordingService.ts index c71519f858..c2afc8867a 100644 --- a/packages/core/src/services/chatRecordingService.ts +++ b/packages/core/src/services/chatRecordingService.ts @@ -577,6 +577,12 @@ export class ChatRecordingService { ) { const conversation = this.readConversation(); updateFn(conversation); + // Enforce a hard limit of 1000 items to prevent unbounded memory and file growth + if (conversation.messages.length > 1000) { + conversation.messages = conversation.messages.slice( + conversation.messages.length - 1000, + ); + } this.writeConversation(conversation); }