From b163fe41b1fd8e2c5df3b0d75fd710d2af91acb1 Mon Sep 17 00:00:00 2001 From: LeekJay <314964866@qq.com> Date: Fri, 16 Jan 2026 14:54:40 +0800 Subject: [PATCH] fix: skip corrupted session files when listing sessions 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. This fix adds try-catch blocks to the list() and children() functions to gracefully skip corrupted session files instead of failing the entire operation. A warning is logged for each skipped file to help users identify and clean up corrupted files. Fixes the issue where a single corrupted session file prevents all sessions from being displayed. --- packages/opencode/src/session/index.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/opencode/src/session/index.ts b/packages/opencode/src/session/index.ts index 3fcdab5238c..0c654382634 100644 --- a/packages/opencode/src/session/index.ts +++ b/packages/opencode/src/session/index.ts @@ -308,7 +308,14 @@ export namespace Session { export async function* list() { const project = Instance.project for (const item of await Storage.list(["session", project.id])) { - yield Storage.read(item) + try { + const session = await Storage.read(item) + if (session && session.id) { + yield session + } + } catch (e) { + log.warn("skipping corrupted session file", { path: item.join("/"), error: e }) + } } } @@ -316,9 +323,15 @@ export namespace Session { const project = Instance.project const result = [] as Session.Info[] for (const item of await Storage.list(["session", project.id])) { - const session = await Storage.read(item) - if (session.parentID !== parentID) continue - result.push(session) + try { + const session = await Storage.read(item) + if (!session || !session.id) continue + if (session.parentID !== parentID) continue + result.push(session) + } catch (e) { + log.warn("skipping corrupted session file", { path: item.join("/"), error: e }) + continue + } } return result })