anomalyco / anomalyco/opencode
[BUG]: session list endpoint sorts the whole session table on every request
@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
- Accumulate a few thousand sessions in the database.
- Request
GET /api/session?limit=5000(the SDK web client does this on the session list screen). - Check
EXPLAIN QUERY PLANfor 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.