feat(sessions): SQLite-backed two-tier session store — fixes 140%+ CPU at scale#1748
Open
BingqingLyu wants to merge 5 commits intomainfrom
Open
feat(sessions): SQLite-backed two-tier session store — fixes 140%+ CPU at scale#1748BingqingLyu wants to merge 5 commits intomainfrom
BingqingLyu wants to merge 5 commits intomainfrom
Conversation
Replace flat sessions.json with SQLite hot index + existing .jsonl cold storage. Problem: - sessions.json grows to 42MB+ with 1000+ sessions - Every operation reads/writes the entire file (O(n)) - Causes 140%+ CPU and 6s+ response times at scale - Related: openclaw#58534, openclaw#57497 Solution: - SQLite hot index for session metadata (O(1) lookups, indexed queries) - Existing .jsonl cold storage unchanged (only loaded on explicit history requests) - Auto-migration from sessions.json on first boot - Opt-in via config: session.storeType = 'sqlite' - Fallback to JSON if SQLite unavailable (Node < 22.5) - CLI: openclaw sessions migrate [--all-agents] [--dry-run] Performance (1000 sessions): - Load: 800ms → 15ms - Single update: 800ms → 5ms - Memory: 42MB → 2MB - CPU on save: 100%+ → <5%
…ty check
bare require('node:sqlite') throws ReferenceError in ESM modules.
Replace with requireNodeSqlite() from src/infra/node-sqlite.ts which
uses createRequire(import.meta.url) — already the established pattern
in this codebase.
Also remove unused resolveEffectiveStoreType import in sessions-migrate.ts.
Fixes isSqliteAvailable() always returning false in ESM context.
- Add storeType to SessionSchema zod validation so session.storeType: sqlite does not cause INVALID_CONFIG on loadConfig() (Codex P1) - Throw instead of swallowing errors in loadSessionStoreSqlite so the JSON fallback in loadSessionStoreWithFacade can actually trigger (Codex P1) - Remove unused cfg variable in sessionsStoreInfoCommand (Greptile P2)
- Fix import: use `warn` instead of non-existent `warning` from globals - Fix JSON output: use `writeRuntimeJson()` helper instead of `runtime.writeJson()` - Remove invalid `allowMulti` option from resolveSessionStoreTargetsOrExit call - Add null check for targets before iteration - Fix Windows path test: use path.join() for platform-agnostic assertions - Add session.storeType to schema labels, help, and generated schema Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SQLite-backed Session Store
Problem
The flat
sessions.jsonfile causes severe performance issues at scale:Related issues: openclaw#58534 (perf), openclaw#57497 (Postgres request)
Solution: Two-tier Architecture
Hot Index (SQLite)
A lightweight SQLite database replaces
sessions.jsonfor metadata:Schema columns:
session_key(PRIMARY KEY) - session identifiersession_id- UUIDupdated_at,created_at- timestamps (indexed)channel,last_channel,last_to,last_account_id,last_thread_id- routinglabel,display_name,status- display infomodel,model_provider,total_tokens,input_tokens,output_tokens- model statemessage_count,archived- metadataentry_json- full SessionEntry blob for complex fieldsBenefits:
Cold Storage (unchanged)
Existing
.jsonltranscript files stay as-is:sessions_historycallsConfiguration
Add to
openclaw.json:{ "session": { "storeType": "sqlite" // "json" (default) or "sqlite" } }Migration
Automatic (on first access)
When
storeType: "sqlite"is set, existingsessions.jsonis automatically migrated to SQLite on first load.Manual (CLI)
Fallback Behavior
sessions.jsonis preserved during migration (not deleted)Files Changed
New Files
src/config/sessions/store-sqlite.ts- SQLite storage implementationsrc/config/sessions/store-facade.ts- Backend abstraction layersrc/commands/sessions-migrate.ts- Migration commandModified Files
src/config/types.base.ts- AddedSessionStoreTypeandstoreTypeconfigsrc/config/sessions/store.ts- Integrated facade for load/savesrc/cli/program/register.status-health-sessions.ts- CLI commandsPerformance Expectations
Testing
Backward Compatibility
storeType: "json"for backward compatibilitysessions.jsonfiles continue to worknode:sqlite)Closes openclaw#58534
Related openclaw#57497