Add SQLite storage layer with FTS5 for memory-efficient data persistence#16913
Add SQLite storage layer with FTS5 for memory-efficient data persistence#16913BowTiedSwan wants to merge 1 commit intoanomalyco:devfrom
Conversation
This change addresses the RAM usage issues by implementing a SQLite-based storage layer with FTS5 full-text search support. Key improvements: - Add SQLiteStorage module with memory-efficient indexing - Replace Array.fromAsync patterns with streaming generators - Add automatic migration from file-based to SQLite storage - Fix duplicate Session.list() calls in server.ts - Use SQLite indexes for session/message/part lookups The SQLite implementation uses a fixed-size page cache (~20MB) regardless of data size, replacing the previous linear memory scaling where the entire dataset was loaded into the V8 heap. https://claude.ai/code/session_01MJwfUnQNfEDqAz8Ld3We9J
|
Hey! Your PR title Please update it to start with one of:
Where See CONTRIBUTING.md for details. |
|
This PR doesn't fully meet our contributing guidelines and PR template. What needs to be fixed:
Please edit this PR description to address the above within 2 hours, or it will be automatically closed. If you believe this was flagged incorrectly, please let a maintainer know. |
|
The following comment was made by an LLM, it may be inaccurate: Based on my search, I found several related PRs that are worth noting: Potentially Related PRs:
These appear to be complementary improvements rather than true duplicates, but PR #16884 (the migration fix) and #15922 (storage interface) are most closely related to the changes in PR #16913. |
|
This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window. Feel free to open a new pull request that follows our guidelines. |
Issue for this PR
Closes #
Type of change
What does this PR do?
This PR introduces a new SQLite-based storage layer (
SQLiteStorage) that provides memory-efficient persistence with full-text search capabilities, while maintaining backward compatibility with the existing file-based storage system.Key improvements:
Memory Efficiency: SQLite with configurable page cache (5000 pages = ~20MB max) replaces unbounded in-memory operations. Large datasets no longer require loading all data into memory.
Optimized Queries: Dedicated tables and indexes for sessions, messages, and parts enable fast lookups and pagination without scanning the entire file system.
Full-Text Search: FTS5 virtual table with trigram tokenization for session title search.
Streaming APIs: New async generator functions (
listSessionIds,listMessageIds,listPartIds) yield results one at a time instead of loading all into memory.Automatic Migration: Migration 2 incrementally indexes existing file-based storage into SQLite on startup.
Backward Compatible: File-based storage remains the primary write target; SQLite acts as an indexed cache with fallback support.
Changes made:
packages/opencode/src/storage/sqlite.tswith complete SQLite schema, prepared statements, and optimized CRUD operationsStoragenamespace with new methods:writeSession,writeMessage,writePart,removeSession,removeMessage,removePart,listStream,countSession.list()andMessageV2.stream()to use SQLite's streaming APIsSession.children()to stream sessions instead of loading all at onceSession.remove()to use streaming for message/part cleanupMessageV2.parts()to use SQLite's optimized parts listHow did you verify your code works?
The changes maintain backward compatibility by:
The migration system automatically indexes existing data on startup, and all new writes are indexed in both storage systems.
Checklist
https://claude.ai/code/session_01MJwfUnQNfEDqAz8Ld3We9J