Backfill all your existing OpenClaw session history into lossless-claw (LCM — Lossless Context Management).
By default, the LCM plugin only captures conversations going forward from when you install it. This tool imports your previous sessions so nothing is lost.
- Reads all
.jsonlsession files from your OpenClaw sessions directory - Imports every message into the LCM SQLite database
- Indexes everything for full-text search (
lcm_grep,lcm_expand,lcm_describe) - Handles edge cases: malformed surrogates, empty sessions, already-imported sessions
- Idempotent — safe to run multiple times, skips sessions already in the DB
- Atomic — each session is a single transaction; one bad session won't corrupt others
- Creates a timestamped backup of your DB before writing anything
- OpenClaw installed and running
- lossless-claw plugin installed:
openclaw plugins install @martian-engineering/lossless-claw openclaw gateway restart
- Python 3.9+ (standard library only — no pip installs needed)
Always run this first:
python3 lcm_import.py --dry-runYou'll see every session listed with message counts and any parse errors. Nothing is written to disk.
python3 lcm_import.pyA timestamped backup of your lcm.db is created automatically before any writes.
sqlite3 ~/.openclaw/lcm.db \
"SELECT COUNT(*) AS conversations FROM conversations;
SELECT COUNT(*) AS messages FROM messages;"--dry-run Validate all sessions without writing to DB
--sessions-dir Path to JSONL session files
(default: ~/.openclaw/agents/main/sessions)
--db Path to LCM SQLite DB
(default: ~/.openclaw/lcm.db)
If you install lossless-claw and run openclaw gateway restart but haven't had any conversations yet, the LCM database tables may not exist yet. The script will detect this and exit with an error. Fix: send one message to your agent first, then run the import.
Alternatively, the script will work fine if you manually trigger the gateway to bootstrap the DB by sending any message after installing the plugin.
| Table | What |
|---|---|
conversations |
One row per session file |
messages |
Every user/assistant/tool message |
message_parts |
Text, reasoning, tool calls, tool results, images |
messages_fts |
Full-text search index |
Not imported: summaries, summary_parents, context_items — these are built by the LCM plugin at runtime as new messages arrive and trigger compaction. No need to reconstruct them manually.
Tested against OpenClaw session files with:
user,assistant,toolResultrolestext,thinking,toolCall,imagecontent part types- Lone surrogate characters in content (sanitized automatically)
- Backup created at
~/.openclaw/lcm.db.bak.YYYYMMDD_HHMMSSbefore any writes - Foreign key enforcement enabled during import
- WAL journal mode for crash safety
- Each session wrapped in
BEGIN IMMEDIATE … COMMIT— rollback on any error - Sessions already in the DB are skipped (idempotent)
=== DRY RUN MODE — no writes will occur ===
Found 27 session file(s) in /Users/you/.openclaw/agents/main/sessions
[abc123...]
294 messages to import
DRY-RUN OK — 294 messages, 377 parts
[def456...]
867 messages to import
DRY-RUN OK — 867 messages, 1224 parts
...
============================================================
IMPORT SUMMARY
============================================================
Sessions imported: 27
Sessions skipped: 0
Sessions errored: 0
Total messages: 13722
Total parts: 16516
DRY RUN complete — no data written.
Run without --dry-run to import.
MIT