anomalyco / anomalyco/opencode

[BUG]: session list endpoint sorts the whole session table on every request

Open
#44,400 0 comments 0 reactions 1 assignee View on GitHub

@rekram1-node is already working on this.

Since Aug 23, 2026.

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

Description

Description

/api/session (the v2 session list used by the SDK/web client) is noticeably slow on servers with many sessions. With ~2k sessions on a low-power box, each list render spends most of its time inside the DB query.

SessionV2.list orders by (time_created, id), but the session table only has indexes on project_id, workspace_id and parent_id. So every list call sorts the entire table through a temp B-tree:

EXPLAIN QUERY PLAN SELECT * FROM session ORDER BY time_created DESC, id DESC LIMIT 5000
SCAN session
USE TEMP B-TREE FOR ORDER BY

The cursor-anchored pagination predicate ((time_created, id) vs the anchor) also has no index to use.

A composite index on (time_created, id) fixes both: the ordered scan and the cursor predicate. Measured on a 3.2k-session database: ~22ms → ~9ms per query, and the plan flips to SCAN session USING INDEX.

Plugins

None.

OpenCode version

v1.18.18 (server, opencode serve)

Steps to reproduce
  1. Accumulate a few thousand sessions in the database.
  2. Request GET /api/session?limit=5000 (the SDK web client does this on the session list screen).
  3. Check EXPLAIN QUERY PLAN for the underlying query, or profile the request — the sort dominates.
Screenshot and/or share link

N/A (server-side; query plan shown above).

Operating System

Any (reproduced on Linux and Windows).

Terminal

N/A.

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.