From 24288cecf9689c3643c5f818250e39e50b4e5c25 Mon Sep 17 00:00:00 2001 From: fullex <0xfullex@gmail.com> Date: Sun, 4 Jan 2026 11:33:50 +0800 Subject: [PATCH] refactor: simplify error handling and session finalization in StreamingService - Removed retry logic for finalizing streaming sessions, streamlining the process to handle success and error cases more clearly. - Enhanced error logging to provide immediate feedback on failures without retaining session data, preventing potential memory leaks. - Improved code readability by consolidating the success and error handling paths, ensuring a more straightforward flow in the session finalization logic. --- .../messageStreaming/StreamingService.ts | 50 ++++++------------- 1 file changed, 16 insertions(+), 34 deletions(-) diff --git a/src/renderer/src/services/messageStreaming/StreamingService.ts b/src/renderer/src/services/messageStreaming/StreamingService.ts index 9dd4bb3835..1dc31fee35 100644 --- a/src/renderer/src/services/messageStreaming/StreamingService.ts +++ b/src/renderer/src/services/messageStreaming/StreamingService.ts @@ -206,43 +206,25 @@ class StreamingService { return } - const maxRetries = 3 - let lastError: Error | null = null - - for (let attempt = 1; attempt <= maxRetries; attempt++) { - try { + try { + // Route to appropriate data source based on topic type + // TEMPORARY: Agent sessions use dbService until migration to Data API is complete + if (isAgentSessionTopicId(session.topicId)) { const updatePayload = this.convertToUpdatePayload(session, status) - - // Route to appropriate data source based on topic type - // TEMPORARY: Agent sessions use dbService until migration to Data API is complete - if (isAgentSessionTopicId(session.topicId)) { - await dbService.updateMessageAndBlocks(session.topicId, updatePayload.messageUpdates, updatePayload.blocks) - } else { - // Normal topic → Use Data API for persistence (v2 target architecture) - const dataApiPayload = this.convertToDataApiFormat(session, status) - await dataApiService.patch(`/messages/${session.messageId}`, { body: dataApiPayload }) - } - - // Success - cleanup session - this.clearSession(messageId) - logger.debug('Finalized streaming session', { messageId, status }) - return - } catch (error) { - lastError = error as Error - logger.warn(`finalize attempt ${attempt}/${maxRetries} failed:`, error as Error) - - if (attempt < maxRetries) { - // Exponential backoff - await new Promise((resolve) => setTimeout(resolve, 1000 * attempt)) - } + await dbService.updateMessageAndBlocks(session.topicId, updatePayload.messageUpdates, updatePayload.blocks) + } else { + // Normal topic → Use Data API for persistence (has built-in retry) + const dataApiPayload = this.convertToDataApiFormat(session, status) + await dataApiService.patch(`/messages/${session.messageId}`, { body: dataApiPayload }) } - } - // All retries failed - logger.error(`finalize failed after ${maxRetries} attempts:`, lastError) - // TRADEOFF: Don't clear session to allow manual retry - // TTL will auto-clean to prevent permanent memory leak - throw lastError + this.clearSession(messageId) + logger.debug('Finalized streaming session', { messageId, status }) + } catch (error) { + logger.error('finalize failed:', error as Error) + // Don't clear session on error - TTL will auto-clean to prevent memory leak + throw error + } } /**