anomalyco / anomalyco/opencode

/sessions returns empty despite sessions existing in database (workspace_id = NULL, empty workspace table)

Open
#38,303 1 comment 0 reactions 1 assignee View on GitHub

@rekram1-node is already working on this.

Since Jul 22, 2026.

Dominant language
TypeScript
Stars
209k
Forks
27.5k
PR merge metrics
PR metrics pending

Description

Description

Environment

  • OpenCode 1.18.4 (npm)
  • Linux / WSL2
  • 7 sessions, 281 messages, 893 parts in DB

Steps

  1. Create multiple sessions over time in a workspace directory
  2. Run /sessions
  3. Shows "no sessions" but SELECT COUNT(*) FROM session returns 7

Root Cause

1. All sessions have workspace_id = NULL, workspace table empty

All 7 sessions have workspace_id=NULL. Workspace table has 0 rows.
Migration 20260227213759_add_session_workspace_id added FK column without backfill.

2. V2 state tables all empty

session_message: 0 rows, session_input: 0 rows, session_context_epoch: 0 rows
But message: 281 rows, part: 893 rows, all with valid session_id FKs.
Migration reset_v2_session_state cleared v2 tables without migrating legacy data.

3. Event table uses aggregate_id not session_id

If session listing joins on session_id against events, it fails.

Fix Suggestions

Short-term

Session listing should fall back to directory matching when workspace_id is NULL:
function listSessions(dir) {
let sessions = db.sessions.where({ workspace_id: currentWorkspaceId });
if (!sessions.length) sessions = db.sessions.where({ workspace_id: null, directory: dir });
if (!sessions.length) sessions = db.sessions.all();
return sessions;
}

Medium-term

Backfill workspace entries and link orphaned sessions via SQL migration.

Long-term

Ensure workspace exists and is linked at session creation time.

Impact

Sessions inaccessible via /sessions, /resume, /continue despite intact data.

Database inspection (via sqlite3) shows three compounding issues:

  1. All 7 sessions have workspace_id = NULL. The workspace table is
    empty (0 rows). Migration 20260227213759_add_session_workspace_id
    added the FK column without backfilling existing data.

  2. V2 state tables (session_message, session_input, session_context_
    epoch) are all 0 rows. But the message table has 281 rows and the
    part table has 893 rows, all with valid session_id FKs. Migration
    reset_v2_session_state cleared v2 tables without migrating legacy data.

  3. The event table uses aggregate_id not session_id. If /sessions joins
    on session_id against events, it will fail silently.

Fix: /sessions should fall back to directory-based matching when
workspace_id is NULL, so sessions are always discoverable regardless
of workspace migration state.

Plugins

none

OpenCode version

1.18.4

Steps to reproduce
  1. cd /home/samuel/Documents/Opencode\ Workspace
  2. opencode
  3. Create a session and do some work
  4. Quit and restart, or open a new session with /new
  5. Run /sessions
  6. Result: reports "no sessions"
  7. But sqlite3 ~/.local/share/opencode/opencode.db "SELECT COUNT(*) FROM session" returns 7

The bug is consistent — happens every time /sessions is invoked.

Screenshot and/or share link

No response

Operating System

Debian GNU/Linux 13 (trixie), x86_64, kernel 6.12.95

Terminal

xfce4-terminal (xterm-256color, X11)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.