Nightly Dreaming Over Claude Code and Cursor Sessions

By | November 24, 2026

When you use multiple AI coding assistants—like Claude Code in the terminal and Cursor in the IDE—they each accumulate valuable context, debugging discoveries, and project conventions in separate, isolated transcripts. Without a unified memory layer, what you teach one tool is lost to the other.

To solve this, I built an out-of-band nightly dream pipeline (claude_dream.py) that runs every night via systemd. It harvests the day’s sessions from both Claude Code and Cursor, replays them to a model to distill durable facts and lessons, and commits them into a shared Obsidian knowledge base.

Architecture Overview

The nightly unit (claude-dream.service) runs around 02:50 AM and orchestrates three stages:

  1. Pre-flight assimilation (ExecStartPre): Imports any peer knowledge bundles from the local inbox.
  2. Nightly dream (ExecStart): Runs claude_dream.py --engine auto --limit 6 --budget-usd 1.00 across both Claude Code and Cursor transcripts.
  3. Export & backup (ExecStartPost): Exports updated curriculum bundles and triggers a backup snapshot.
Component Location Purpose
Dream script ~/scripts/claude_dream.py Harvests sessions, replays them to a model, and writes distilled memory.
Claude session index ~/.claude/session-archive.db SQLite index of Claude Code sessions with size, timestamp, and transcript path.
Claude transcripts ~/.claude/projects/<project>/<session>.jsonl Raw JSONL transcripts for Claude Code sessions.
Cursor transcripts ~/.cursor/projects/<project>/agent-transcripts/<id>/<id>.jsonl Raw JSONL transcripts for Cursor agent chats.
Watermark ledger Meta/states/dream-state.json Tracks which sessions and message pairs have already been dreamed.
Dream journal Meta/dream-journal.md Appends one Dream Cycle #N entry per night with links to session notes.
Memory outputs Meta/claude-memory.md, Meta/lessons.md, Meta/states/claude.md Stores durable facts, mistake/correction lessons, and recent milestones.

The 4-Step Dream Cycle

1. Harvest

  • Updates the Claude session index and reads sessions active within --since-days (default: 14 days).
  • Scans Cursor’s agent-transcripts for main-agent transcripts (where the .jsonl filename matches its parent folder name, excluding subagents/) within the same time window.
  • Merges both lists and sorts by last activity, newest first.
  • Skips sessions that are still active (within --min-idle, default 30 minutes), sessions tagged as the dream’s own calls, or sessions whose transcript size matches the ledger.
  • Reduces each session to (user_request, final_answer) pairs—dropping intermediate tool calls—and processes only pairs beyond the ledger’s watermark, stopping after --limit sessions (default: 6).

2. Dream

For each harvested session, the script sends one structured request to a model:

  • Engine selection: Uses llm (via claude -p with a pinned model like haiku, no tools, no hooks, and a strict JSON schema) when available, or falls back to a zero-cost heuristic engine that extracts explicit preferences (remember, always, never).
  • Redaction: Passwords, tokens, and API keys are redacted from requests, answers, and summaries for both sources before any model call.
  • Structured output: The model returns categorized learnings, potential contradictions with existing memory, procedural lessons (mistake + correction), a milestone, and a one-line summary.

3. Commit

  • Learnings are appended to Meta/claude-memory.md with duplicate checking and a (dream: <session_id8>) attribution tag.
  • Project-scoped facts are tagged with the project folder name (e.g., [my-service]) so memory filtering works identically across tools.
  • Lessons go to Meta/lessons.md; milestones go to Meta/states/claude.md (capped at the 8 most recent).
  • Contradictions never overwrite existing facts automatically—they are logged as - [ ] REVIEW items in the dream journal for human review.
  • Optimistic Concurrency Control: Every write re-reads the target file, applies the diff, verifies the content hash, and performs an atomic replace (retrying if a live session wrote concurrently).

4. Compact, Gate, and Journal

  • memory_compact.py merges redundant bullets that state the same fact.
  • An evaluation suite runs before and after the writes; if a previously passing probe fails after the commit, the cycle rolls back and leaves the watermarks unchanged.
  • Finally, a Dream Cycle #N entry is appended to Meta/dream-journal.md and logged.

Normalizing Cursor Chats Alongside Claude Sessions

Because Claude Code and Cursor store transcripts differently, the harvester normalizes both into a common format:

Aspect Claude Code Session Cursor Agent Chat
Source Archive DB + .jsonl .jsonl transcript, read directly
User request type: user record (cleaned) role: user record (<user_query>...</user_query>)
Final answer Last assistant text block before next request Same rule, from role: assistant records
Timestamp Archive DB (UTC) File modification time, normalized to UTC
Ledger key Session UUID Chat UUID (disjoint from Claude IDs)

One neat detail: Cursor names project folders by replacing /, _, and . with - (for example, home-user-workspace-my_project). The script’s cursor_cwd helper walks the filesystem level by level, matching tokens against actual directory names to reconstruct the true project tag (my_project) so both tools share the exact same project namespace.

What Changed in claude_dream.py

Comparing the updated claude_dream.py against the previous version (claude_dream.py.bak-20261124), the Cursor integration was added cleanly without breaking the existing Claude Code pipeline:

  • Cursor discovery & parsing: Added CURSOR_PROJECTS (~/.cursor/projects) and CURSOR_QUERY (<user_query>...</user_query>), plus cursor_rows() and extract_cursor_pairs() to scan main-agent .jsonl transcripts (excluding subagents/) and normalize file modification times to UTC.
  • Unified session routing: Added is_cursor(), note_rel(), and project_of() so journal links route to cursor-sessions/YYYY-MM/ vs. claude-sessions/YYYY-MM/ and project tags match across both tools.
  • Path reconstruction: Added cursor_cwd() to walk the filesystem and reverse Cursor’s hyphen-mangled project folder names back into exact project tags.
  • Opt-out flag: Extended harvest(..., cursor: bool = True) to merge and sort both sources by last activity, and added the --no-cursor CLI flag to revert to Claude-only dreaming at any time.

CLI Options & Manual Testing

You can dry-run the pipeline at any time without modifying memory or advancing watermarks:

# Free dry-run: see which sessions would be picked (no model call)
python3 ~/scripts/claude_dream.py --engine heuristic --dry-run --min-idle 0 --limit 20

# Real LLM dry-run on 2 sessions (costs a few cents, writes nothing)
python3 ~/scripts/claude_dream.py --engine llm --model haiku --dry-run --min-idle 0 --limit 2

# Dream only Claude Code sessions
python3 ~/scripts/claude_dream.py --dry-run --no-cursor

In a real test run replaying one Claude session and one Cursor chat, the entire cycle cost $0.04 and extracted 3 project facts, 3 lessons, a milestone, and a contradiction review item.

Operational Notes

  • Stale facts from older Cursor chats: Because a Cursor chat might contain work that a later session superseded, always check - [ ] REVIEW items in Meta/dream-journal.md after a cycle.
  • Budget & rate limits: --limit 6 and --budget-usd 1.00 cap each night’s spend; any remaining sessions are simply picked up on subsequent nights.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.