Skip to content

feat(claudecode): bidirectional sync with Claude Code native memory folder#203

Open
jorgehara wants to merge 3 commits intoGentleman-Programming:mainfrom
jorgehara:feat/claude-code-memory-sync
Open

feat(claudecode): bidirectional sync with Claude Code native memory folder#203
jorgehara wants to merge 3 commits intoGentleman-Programming:mainfrom
jorgehara:feat/claude-code-memory-sync

Conversation

@jorgehara
Copy link
Copy Markdown

Summary

Add bidirectional sync between Engram and Claude Code's native memory folder (~/.claude/projects/{slug}/memory/).

Problem

Users who switch between Claude Code native and other agents (OpenCode, VS Code, JetBrains, etc.) have two disconnected memory systems:

  • Claude Code stores memories as .md files in ~/.claude/projects/{slug}/memory/
  • Engram stores memories in ~/.engram/engram.db

This means:

  • Memories saved in Engram are invisible to Claude Code native
  • Memories saved in Claude Code native are invisible to other agents

Solution

New engram claude-code-sync command (and claude-code-export / claude-code-import variants) that synchronizes memories bidirectionally:

# Bidirectional sync (export Engram → Claude Code, then import new Claude Code → Engram)
engram claude-code-sync

# Export only (Engram → Claude Code memory folder)
engram claude-code-export

# Import only (Claude Code memory folder → Engram)
engram claude-code-import

Options:

  • --claude-projects-dir — Override default ~/.claude/projects
  • --project — Filter sync to a specific project
  • --dry-run — Preview without making changes

Changes

File Change
internal/claudecode/claudecode.go Package documentation
internal/claudecode/markdown.go Memory file format (Claude Code's .md format)
internal/claudecode/exporter.go Engram → Claude Code memory folder export
internal/claudecode/importer.go Claude Code memory folder → Engram import
internal/claudecode/sync.go Bidirectional sync orchestration
internal/claudecode/claudecode_test.go Unit tests for slug/name conversion and markdown format
cmd/engram/main.go CLI commands integration

Test Plan

  • go test ./internal/claudecode/... passes
  • go build ./cmd/engram/... succeeds
  • Manual testing: export Engram memories to Claude Code folder, verify .md files are created
  • Manual testing: import Claude Code memories to Engram, verify observations appear in engram search

This PR resolves a gap for users who prefer working in multiple IDEs/agents but want a unified memory system that works regardless of which agent they're using at any given moment.

- Add internal/claudecode package with export/import/sync functionality
- Export Engram observations to Claude Code's native memory folder as .md files
- Import memories from Claude Code's memory folder into Engram
- Add CLI commands: claude-code-export, claude-code-import, claude-code-sync
- Support bidirectional sync between Engram and Claude Code memory
- Allow users who switch between Claude Code native and other agents to have unified memory
- Add lazy bidirectional sync: when mem_search is called, automatically
  import new memories from Claude Code's native memory folder
- Consistent with Engram philosophy: agent-driven, no auto-capture,
  import only on explicit user request (search)
- Add comprehensive tests for Claude Code memory import
- Add documentation explaining the feature and design decisions for Alan

This enables users who switch between Claude Code native and other
agents to have unified memory without manual sync steps.
@jorgehara
Copy link
Copy Markdown
Author

PR Update: Lazy Import for Claude Code Memory Sync ✅

Hi Alan! This PR adds bidirectional sync between Engram and Claude Code's native memory folder.

Test Results

All tests passing:

ok   github.com/Gentleman-Programming/engram/internal/claudecode     ✓
ok   github.com/Gentleman-Programming/engram/internal/mcp            ✓
     - TestClaudeCodeLazyImport_MemSearch                       PASS ✓
     - TestClaudeCodeLazyImport_OnlyImportsNewMemories           PASS ✓
     - TestClaudeCodeLazyImport_MultipleProjects                PASS ✓
     - TestClaudeCodeLazyImport_ProjectFilter                  PASS ✓
     - TestClaudeCodeLazyImport_InvalidFrontmatter             PASS ✓
     - TestClaudeCodeLazyImport_EmptyMemoryDirectory           PASS ✓
     - TestClaudeCodeLazyImport_MemoryIndexFileIgnored        PASS ✓
     - TestClaudeCodeLazyImport_PreservedMetadata              PASS ✓
     - TestClaudeCodeLazyImport_Context                       PASS ✓

go build ./cmd/engram/...                                      SUCCESS ✓

Key Design Decision

Instead of auto-sync on startup (which goes against Engram's "no auto-capture" philosophy), we use lazy import: when user calls mem_search, a goroutine imports Claude Code memories before searching.

This is consistent with how the OpenCode plugin handles memories.json — import on demand, not auto-capture.

Bug Fixed During Testing

Found and fixed: store.Config{DataDir: ...} sets MaxObservationLength to 0 (zero value), causing ALL content to truncate. Must use store.FallbackConfig() instead.

Files

  • internal/claudecode/ — new sync package
  • internal/mcp/mcp.go — lazy import in handleSearch (31 lines)
  • internal/mcp/mcp_claudecode_import_test.go — 511 lines of tests
  • docs/CLAUDE-CODE-MEMORY-SYNC.md — documentation

Documentation

See docs/CLAUDE-CODE-MEMORY-SYNC.md for full technical docs including memory file format, import/export flows, and design rationale.

Looking forward to your feedback! 🙏

Add 'engram agents' command that detects and shows usage statistics
for all AI coding agents on the system (Claude Code, Gemini CLI,
OpenCode, Cursor, Codex).

Features:
- Reads native history files from each agent
- Shows sessions, projects, and activity range per agent
- ASCII bar chart visualization for easy comparison
- JSON output for scripting (--json flag)
- Read-only, no data written to Engram store

Consistent with Engram philosophy: simple, agent-driven, no auto-capture.
@jorgehara
Copy link
Copy Markdown
Author

Closes #212

@jorgehara jorgehara closed this Apr 21, 2026
@jorgehara jorgehara reopened this Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant