fix: resolve Issue #56 - fix stack overflow on large session files#76
Open
caiyongtian wants to merge 1 commit into
Open
fix: resolve Issue #56 - fix stack overflow on large session files#76caiyongtian wants to merge 1 commit into
caiyongtian wants to merge 1 commit into
Conversation
### Problem Accessing sessions with large message history (e.g. 1068+ messages) caused HTTP 500 errors due to RangeError: Maximum call stack size exceeded during JSON serialization. ### Root Cause - Massive session objects from buildSessionContext could not be serialized by NextResponse.json - No error handling for corrupted session files - No fallback scanning when SessionManager fails - Deep agent state objects caused additional serialization issues ### Solution #### 1. lib/session-reader.ts - Added 5-second TTL cache for listAllSessions() to avoid repeated expensive scans - Implemented safeManualScan() - async fallback scanner using fs/promises - Reads only session headers (not full message history) - Prevents symlink loops with visitedDirs tracking - Non-blocking: runs asynchronously to not block the event loop - Added sessions cache integration to resolveSessionPath() #### 2. app/api/sessions/[id]/route.ts - Granular try-catch blocks for: SessionManager.open, getEntries, getTree, getLeafId, getHeader, buildSessionContext, getSessionName, getRpcSession - Message truncation: sessions with >500 messages are truncated to last 500 - Serialization safety net: pre-checks JSON.stringify, falls back to minimal response (empty tree, last 50 messages) if serialization fails - Simplified agentState extraction: only includes isStreaming, isCompacting, contextUsage, systemPrompt, thinkingLevel (primitive-safe) - Better error responses with details and stack traces #### 3. next.config.ts - Added '127.0.0.1' and 'localhost' to allowedDevOrigins to fix HMR WebSocket errors ### Testing - Verified GET /api/sessions/019eb1da-0e62-775b-a744-520fb1da496d (1068 messages) now returns 200 with truncated data instead of 500 error - Verified GET /api/sessions works correctly with caching - Verified GET /api/sessions/:id?includeState works with simplified state
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Accessing sessions with large message history (e.g. 1068+ messages) caused HTTP 500 errors due to RangeError: Maximum call stack size exceeded during JSON serialization.
Root Cause
Solution
1. lib/session-reader.ts
2. app/api/sessions/[id]/route.ts
3. next.config.ts
Testing