[PR #8825] fix: skip corrupted session files when listing sessions #12885

Open
opened 2026-02-16 18:17:46 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/8825

State: open
Merged: No


Summary

When a session JSON file is empty or contains invalid JSON, the entire session list fails to load with Unexpected end of JSON input error. This causes all sessions to appear missing even though valid session files exist.

Problem

  • A single corrupted/empty session file causes Storage.read() to throw an exception
  • The exception propagates up and causes the entire list() generator to fail
  • Users see "No results found" when running /sessions even though they have many valid sessions
  • This is a data loss UX issue - users think their sessions are gone

Root Cause

The list() and children() functions in packages/opencode/src/session/index.ts don't handle JSON parsing errors:

export async function* list() {
  const project = Instance.project
  for (const item of await Storage.list(["session", project.id])) {
    yield Storage.read<Info>(item)  // No error handling
  }
}

Solution

Add try-catch blocks to gracefully skip corrupted session files:

  • Log a warning for each skipped file (helps users identify corrupted files)
  • Continue processing remaining valid sessions
  • Validate that the parsed session has required fields (id)

Testing

Manually tested by:

  1. Creating an empty session JSON file in the storage directory
  2. Verifying that /sessions now shows valid sessions instead of failing
  3. Confirming warning is logged for the corrupted file

Related

This addresses the scenario described in logs like:

ERROR service=server error=Unexpected end of JSON input failed
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/8825 **State:** open **Merged:** No --- ## Summary When a session JSON file is empty or contains invalid JSON, the entire session list fails to load with `Unexpected end of JSON input` error. This causes all sessions to appear missing even though valid session files exist. ## Problem - A single corrupted/empty session file causes `Storage.read()` to throw an exception - The exception propagates up and causes the entire `list()` generator to fail - Users see "No results found" when running `/sessions` even though they have many valid sessions - This is a data loss UX issue - users think their sessions are gone ## Root Cause The `list()` and `children()` functions in `packages/opencode/src/session/index.ts` don't handle JSON parsing errors: ```typescript export async function* list() { const project = Instance.project for (const item of await Storage.list(["session", project.id])) { yield Storage.read<Info>(item) // No error handling } } ``` ## Solution Add try-catch blocks to gracefully skip corrupted session files: - Log a warning for each skipped file (helps users identify corrupted files) - Continue processing remaining valid sessions - Validate that the parsed session has required fields (`id`) ## Testing Manually tested by: 1. Creating an empty session JSON file in the storage directory 2. Verifying that `/sessions` now shows valid sessions instead of failing 3. Confirming warning is logged for the corrupted file ## Related This addresses the scenario described in logs like: ``` ERROR service=server error=Unexpected end of JSON input failed ```
yindo added the pull-request label 2026-02-16 18:17:46 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12885