Skip to content

fix: resolve Issue #56 - fix stack overflow on large session files#76

Open
caiyongtian wants to merge 1 commit into
agegr:mainfrom
caiyongtian:fix-issue-56
Open

fix: resolve Issue #56 - fix stack overflow on large session files#76
caiyongtian wants to merge 1 commit into
agegr:mainfrom
caiyongtian:fix-issue-56

Conversation

@caiyongtian

Copy link
Copy Markdown

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

### 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant