Class: ClaudeMemory::Commands::Initializers::MemoryInstructionsWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/claude_memory/commands/initializers/memory_instructions_writer.rb

Overview

Writes ClaudeMemory instructions to CLAUDE.md files

Instance Method Summary collapse

Constructor Details

#initialize(stdout) ⇒ MemoryInstructionsWriter

Returns a new instance of MemoryInstructionsWriter.



10
11
12
# File 'lib/claude_memory/commands/initializers/memory_instructions_writer.rb', line 10

def initialize(stdout)
  @stdout = stdout
end

Instance Method Details

#write_global_instructionsObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/claude_memory/commands/initializers/memory_instructions_writer.rb', line 71

def write_global_instructions
  global_claude_dir = File.join(Dir.home, ".claude")
  claude_md_path = File.join(global_claude_dir, "CLAUDE.md")

  memory_instruction = <<~MD
    <!-- ClaudeMemory v#{ClaudeMemory::VERSION} -->
    # ClaudeMemory

    ClaudeMemory provides long-term memory across all your sessions.

    ## Memory-First Workflow

    **IMPORTANT: Always check memory BEFORE reading files or exploring code.**

    When you receive a question or task:
    1. **First**: Use `memory.recall` with a relevant query
    2. **Then**: If memory is insufficient, explore with Read/Grep/Glob
    3. **Combine**: Use recalled facts + code exploration for complete context

    ### When to Check Memory

    - Before answering "How does X work?" questions
    - Before implementing features (check for patterns and decisions)
    - Before debugging (check for known issues)
    - When you make a mistake (recall correct approach)

    ### Quick-Access Tools

    - `memory.recall` - General knowledge search (USE THIS FIRST)
    - `memory.decisions` - Architectural decisions and constraints
    - `memory.conventions` - Coding style and preferences
    - `memory.architecture` - Framework and pattern choices
    - `memory.explain` - Detailed provenance for specific facts
    - `memory.conflicts` - Open contradictions
    - `memory.status` - System health check

    ### Example Queries

    ```
    memory.recall "authentication flow"
    memory.recall "error handling patterns"
    memory.recall "database setup"
    memory.decisions (before implementing features)
    memory.conventions (before writing code)
    ```

    The memory system contains distilled knowledge from previous sessions. Using it saves time and provides better answers.
  MD

  FileUtils.mkdir_p(global_claude_dir)
  if File.exist?(claude_md_path)
    content = File.read(claude_md_path)
    unless content.include?("ClaudeMemory")
      File.write(claude_md_path, content + "\n\n" + memory_instruction)
    end
  else
    File.write(claude_md_path, memory_instruction)
  end

  @stdout.puts "✓ Updated #{claude_md_path}"
end

#write_project_instructionsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/claude_memory/commands/initializers/memory_instructions_writer.rb', line 14

def write_project_instructions
  claude_dir = ".claude"
  claude_md_path = File.join(claude_dir, "CLAUDE.md")

  memory_instruction = <<~MD
    <!-- ClaudeMemory v#{ClaudeMemory::VERSION} -->
    # ClaudeMemory

    This project has ClaudeMemory enabled for both project-specific and global knowledge.

    ## Memory-First Workflow

    **IMPORTANT: Check memory BEFORE reading files or exploring code.**

    ### Workflow Pattern

    1. **Query memory first**: `memory.recall "<topic>"` or use shortcuts
    2. **Review results**: Understand existing knowledge and decisions
    3. **Explore if needed**: Use Read/Grep only if memory is insufficient
    4. **Combine context**: Merge recalled facts with code exploration

    ### Specialized Shortcuts

    - `memory.decisions` - Project decisions (ALWAYS check before implementing)
    - `memory.conventions` - Global coding preferences
    - `memory.architecture` - Framework choices and patterns
    - `memory.conflicts` - Contradictions that need resolution

    ### Scope Awareness

    - **Project facts**: Apply only to this project (e.g., "uses PostgreSQL")
    - **Global facts**: Apply everywhere (e.g., "prefers 4-space tabs")
    - Use `scope: "project"` or `scope: "global"` to filter queries

    ### When Memory Helps Most

    - "Where is X handled?" → `memory.recall "X handling"`
    - "How do we do Y?" → `memory.recall "Y pattern"`
    - "Why did we choose Z?" → `memory.decisions`
    - Before writing code → `memory.conventions`

    See published snapshot: `.claude/rules/claude_memory.generated.md`
  MD

  FileUtils.mkdir_p(claude_dir)
  if File.exist?(claude_md_path)
    content = File.read(claude_md_path)
    unless content.include?("ClaudeMemory")
      File.write(claude_md_path, content + "\n\n" + memory_instruction)
    end
  else
    File.write(claude_md_path, memory_instruction)
  end

  @stdout.puts "✓ Updated #{claude_md_path}"
end