Repository navigation
Expand file tree
/
Copy pathinstall
More file actions
executable file
·429 lines (397 loc) · 17.5 KB
/
Copy pathinstall
File metadata and controls
executable file
·429 lines (397 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
70
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/bin/bash
#######################################################################
# LMF4 — Persistent memory for Claude Code (v4.1 standalone)
#
# Installs the persistent memory system for Claude Code.
# PAI and Fabric are NOT required. Works on any Linux with Claude Code.
#
# Prerequisites:
# - Linux (Debian/Ubuntu tested; any distro should work) with Claude Code installed
# - bun is installed automatically if missing
#
# What LMF4 adds:
# - memory.db SQLite + FTS5 database for persistent memory
# - 6 hooks Extraction, recall, compaction, failure tracking
# - MCP server memory_search + memory_recall tools for in-session use
# - mem CLI Command-line memory search (mem search "query")
# - systemd timers Catchup extraction every 4h, backup every 4h
# - backup repo Git-tracked full instance backup
#
# Idempotent: safe to run multiple times.
#
# Usage:
# cd LMF4 && ./install
#######################################################################
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
CLAUDE_DIR="$HOME/.claude"
HOOKS_DIR="$CLAUDE_DIR/hooks"
MEMORY_DIR="$CLAUDE_DIR/MEMORY"
BIN_DIR="$HOME/bin"
log() { echo "[LMF4] $1"; }
ok() { echo "[LMF4] ✓ $1"; }
err() { echo "[LMF4] ✗ $1" >&2; }
#######################################################################
# Preflight checks
#######################################################################
if [ ! -d "$CLAUDE_DIR" ]; then
err "~/.claude/ not found. Install Claude Code first."
exit 1
fi
ok "Preflight: Claude Code confirmed"
#######################################################################
# 0. Install bun if not present
#######################################################################
export BUN_INSTALL="$HOME/.bun"
export PATH="$BUN_INSTALL/bin:$HOME/bin:$HOME/.local/bin:$PATH"
if command -v bun &>/dev/null; then
ok "bun already installed: $(bun --version)"
else
log "Installing bun..."
# bun installer requires unzip
if ! command -v unzip &>/dev/null; then
log "Installing unzip (required by bun)..."
sudo apt-get install -y -qq unzip 2>/dev/null || { err "Cannot install unzip. Install it manually: sudo apt-get install unzip"; exit 1; }
fi
curl -fsSL https://bun.sh/install | bash
if ! grep -q 'BUN_INSTALL' ~/.bashrc 2>/dev/null; then
echo 'export BUN_INSTALL="$HOME/.bun"' >> ~/.bashrc
echo 'export PATH="$BUN_INSTALL/bin:$PATH"' >> ~/.bashrc
fi
ok "bun installed: $(bun --version)"
fi
mkdir -p "$HOOKS_DIR"
#######################################################################
# 1. Create MEMORY directory structure
#######################################################################
log "Creating MEMORY directory structure..."
mkdir -p "$MEMORY_DIR"/{WORK,LEARNING/{SYSTEM,ALGORITHM,FAILURES,SYNTHESIS,REFLECTIONS,SIGNALS},RESEARCH,SECURITY,STATE/{algorithms,kitty-sessions,tab-titles,progress,integrity},PAISYSTEMUPDATES,AUTO}
mkdir -p "$BIN_DIR"
ok "Directory structure"
#######################################################################
# 2. Initialize memory.db
#######################################################################
if [ -f "$CLAUDE_DIR/memory.db" ]; then
ok "memory.db already exists ($(du -sh "$CLAUDE_DIR/memory.db" | cut -f1))"
else
log "Creating memory.db with schema..."
bun -e "
const { Database } = require('bun:sqlite');
const db = new Database('$CLAUDE_DIR/memory.db');
db.exec(\`
CREATE TABLE IF NOT EXISTS sessions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT UNIQUE NOT NULL,
started_at DATETIME DEFAULT CURRENT_TIMESTAMP,
ended_at DATETIME, summary TEXT, project TEXT, cwd TEXT
);
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT NOT NULL, timestamp DATETIME NOT NULL,
role TEXT NOT NULL CHECK (role IN ('user', 'assistant', 'system')),
content TEXT NOT NULL, project TEXT,
FOREIGN KEY (session_id) REFERENCES sessions(session_id)
);
CREATE TABLE IF NOT EXISTS loa_entries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
title TEXT NOT NULL, fabric_extract TEXT NOT NULL,
session_id TEXT, project TEXT, tags TEXT
);
CREATE TABLE IF NOT EXISTS decisions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
session_id TEXT, project TEXT, decision TEXT NOT NULL, reasoning TEXT,
status TEXT DEFAULT 'active' CHECK (status IN ('active', 'superseded', 'reverted'))
);
CREATE TABLE IF NOT EXISTS errors (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
error TEXT NOT NULL, cause TEXT, fix TEXT,
frequency INTEGER DEFAULT 1, last_seen DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS learnings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
session_id TEXT, project TEXT, problem TEXT NOT NULL, solution TEXT, tags TEXT
);
CREATE TABLE IF NOT EXISTS embeddings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
source_table TEXT NOT NULL, source_id INTEGER NOT NULL,
model TEXT NOT NULL DEFAULT 'nomic-embed-text',
dimensions INTEGER NOT NULL DEFAULT 768,
embedding BLOB NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
UNIQUE(source_table, source_id)
);
CREATE VIRTUAL TABLE IF NOT EXISTS messages_fts USING fts5(content, content_rowid='id', tokenize='porter');
CREATE VIRTUAL TABLE IF NOT EXISTS loa_fts USING fts5(title, fabric_extract, content_rowid='id', tokenize='porter');
CREATE VIRTUAL TABLE IF NOT EXISTS decisions_fts USING fts5(decision, reasoning, content_rowid='id', tokenize='porter');
CREATE VIRTUAL TABLE IF NOT EXISTS errors_fts USING fts5(error, fix, content_rowid='id', tokenize='porter');
CREATE VIRTUAL TABLE IF NOT EXISTS learnings_fts USING fts5(problem, solution, content_rowid='id', tokenize='porter');
\`);
const triggers = [
['messages', 'content'],
['loa_entries:loa', 'title, fabric_extract'],
['decisions', 'decision, reasoning'],
['errors', 'error, fix'],
['learnings', 'problem, solution'],
];
for (const [spec, cols] of triggers) {
const [table, ftsName] = spec.includes(':') ? spec.split(':') : [spec, spec];
const fts = ftsName + '_fts';
const nc = cols.split(',').map(c => 'NEW.' + c.trim()).join(', ');
const oc = cols.split(',').map(c => 'OLD.' + c.trim()).join(', ');
try { db.exec(\`
CREATE TRIGGER \${fts}_insert AFTER INSERT ON \${table} BEGIN INSERT INTO \${fts}(rowid, \${cols}) VALUES (NEW.id, \${nc}); END;
CREATE TRIGGER \${fts}_delete AFTER DELETE ON \${table} BEGIN INSERT INTO \${fts}(\${fts}, rowid, \${cols}) VALUES ('delete', OLD.id, \${oc}); END;
\`); } catch(e) {}
}
db.close();
console.log('Done');
"
ok "memory.db initialized (8 tables, 5 FTS5, 10 triggers)"
fi
#######################################################################
# 3. Install hooks
#######################################################################
log "Installing memory hooks..."
HOOKS_INSTALLED=0
for hook in SessionExtract.hook.ts AssociativeRecall.hook.ts PreCompact.hook.sh PostCompact.hook.ts StopFailure.hook.ts; do
src="$SCRIPT_DIR/hooks/$hook"
dst="$HOOKS_DIR/$hook"
if [ -f "$src" ]; then
if [ -f "$dst" ] && cmp -s "$src" "$dst"; then
: # identical, skip
else
cp "$src" "$dst"
chmod +x "$dst"
HOOKS_INSTALLED=$((HOOKS_INSTALLED + 1))
fi
else
err "Missing: hooks/$hook"
fi
done
ok "Hooks ($HOOKS_INSTALLED new/updated, 6 total)"
#######################################################################
# 4. Install MCP server
#######################################################################
log "Installing MCP memory server..."
src="$SCRIPT_DIR/mcp/mem-mcp-server.ts"
dst="$HOOKS_DIR/mem-mcp-server.ts"
if [ -f "$src" ]; then
cp "$src" "$dst"
chmod +x "$dst"
ok "MCP server (memory_search, memory_recall)"
else
err "Missing: mcp/mem-mcp-server.ts"
fi
#######################################################################
# 5. Install extract prompt
#######################################################################
if [ -f "$SCRIPT_DIR/prompts/extract_prompt.md" ]; then
cp "$SCRIPT_DIR/prompts/extract_prompt.md" "$MEMORY_DIR/extract_prompt.md"
ok "Extract prompt"
fi
#######################################################################
# 6. Build and install mem CLI (always rebuild on upgrade)
#######################################################################
MEM_SRC="$SCRIPT_DIR/mem-cli"
if [ -d "$MEM_SRC/src" ]; then
log "Building mem CLI (lmf4-mem-cli)..."
MEM_DEST="$CLAUDE_DIR/memory-cli"
# Clean old src so removed commands (e.g. LMF3 import-telos) don't linger
rm -rf "$MEM_DEST/src"
mkdir -p "$MEM_DEST/src" "$MEM_DEST/dist"
cp -r "$MEM_SRC/src/." "$MEM_DEST/src/"
cp "$MEM_SRC/package.json" "$MEM_DEST/"
cd "$MEM_DEST"
bun install 2>/dev/null || true
bun build src/index.ts --outdir dist --target node 2>/dev/null
if [ -f dist/index.js ]; then
chmod +x dist/index.js
ln -sf "$MEM_DEST/dist/index.js" "$BIN_DIR/mem"
ok "mem CLI → ~/bin/mem (v$(grep '"version"' "$MEM_SRC/package.json" | head -1 | cut -d'"' -f4))"
else
err "mem CLI build failed"
fi
cd "$SCRIPT_DIR"
else
err "mem CLI source not found in $MEM_SRC"
fi
#######################################################################
# 7. Merge settings.json (hooks + MCP)
#######################################################################
log "Merging LMF4 into settings.json..."
bun -e "
const fs = require('fs');
const path = '$CLAUDE_DIR/settings.json';
let s = {};
try { s = JSON.parse(fs.readFileSync(path, 'utf8')); } catch {}
if (!s.hooks) s.hooks = {};
const HOME = process.env.HOME;
const hookDefs = {
'PreCompact': { type: 'command', command: HOME + '/.claude/hooks/PreCompact.hook.sh' },
'PostCompact': { type: 'command', command: 'bun run ' + HOME + '/.claude/hooks/PostCompact.hook.ts' },
'StopFailure': { type: 'command', command: 'bun run ' + HOME + '/.claude/hooks/StopFailure.hook.ts' },
'Stop': { type: 'command', command: 'bun run ' + HOME + '/.claude/hooks/SessionExtract.hook.ts' },
'UserPromptSubmit': { type: 'command', command: 'bun run ' + HOME + '/.claude/hooks/AssociativeRecall.hook.ts' },
};
// Claude Code hook format: each event is an array of matcher objects
// Each matcher has optional "matcher" string and required "hooks" array
for (const [event, hookDef] of Object.entries(hookDefs)) {
if (!s.hooks[event]) {
s.hooks[event] = [{ hooks: [hookDef] }];
} else {
// Check if hook already exists in any matcher
const file = hookDef.command.split('/').pop();
const exists = s.hooks[event].some(m =>
m.hooks && m.hooks.some(h => h.command && h.command.includes(file))
);
if (!exists) {
// Append to first matcher's hooks array, or create new matcher
if (s.hooks[event][0] && s.hooks[event][0].hooks) {
s.hooks[event][0].hooks.push(hookDef);
} else {
s.hooks[event].push({ hooks: [hookDef] });
}
}
}
}
if (!s.mcpServers) s.mcpServers = {};
if (!s.mcpServers['pai-memory']) {
s.mcpServers['pai-memory'] = {
type: 'stdio', command: 'bun',
args: ['run', process.env.HOME + '/.claude/hooks/mem-mcp-server.ts']
};
}
if (!s.env) s.env = {};
s.env.CLAUDE_CODE_MAX_OUTPUT_TOKENS = s.env.CLAUDE_CODE_MAX_OUTPUT_TOKENS || '128000';
s.autoMemoryDirectory = s.autoMemoryDirectory || process.env.HOME + '/.claude/MEMORY/AUTO';
fs.writeFileSync(path, JSON.stringify(s, null, 2) + '\n');
console.log('OK');
"
ok "settings.json (hooks + MCP + env)"
#######################################################################
# 8. Helper scripts
#######################################################################
log "Creating helper scripts..."
cat > "$BIN_DIR/memory-catchup" << 'EOF'
#!/bin/bash
# Thin wrapper — `mem catchup` runs the batch extraction in the mem CLI.
# Kept for back-compat with older systemd units that reference memory-catchup.
export BUN_INSTALL="$HOME/.bun"
export PATH="$HOME/bin:$BUN_INSTALL/bin:$HOME/.local/bin:$PATH"
if command -v mem >/dev/null 2>&1; then
exec mem catchup >> ~/.claude/MEMORY/EXTRACT_LOG.txt 2>&1
else
exec bun run ~/.claude/hooks/SessionExtract.hook.ts --batch >> ~/.claude/MEMORY/EXTRACT_LOG.txt 2>&1
fi
EOF
chmod +x "$BIN_DIR/memory-catchup"
cat > "$BIN_DIR/memory-backup" << 'BEOF'
#!/bin/bash
set -e
CLAUDE_DIR="$HOME/.claude"
BACKUP_DIR="$CLAUDE_DIR/conversations-backup"
LOG="$CLAUDE_DIR/MEMORY/EXTRACT_LOG.txt"
log() { echo "[$(date -Iseconds)] BACKUP: $1" >> "$LOG" 2>/dev/null; }
if [ ! -d "$BACKUP_DIR/.git" ]; then
echo "No backup repo. Run: mkdir -p $BACKUP_DIR && cd $BACKUP_DIR && git init && git remote add origin <url>"
exit 1
fi
cd "$BACKUP_DIR"
for f in settings.json CLAUDE.md keybindings.json memory.db; do
cp "$CLAUDE_DIR/$f" "$BACKUP_DIR/" 2>/dev/null || true
done
rsync -a --delete "$CLAUDE_DIR/hooks/" "$BACKUP_DIR/hooks/" 2>/dev/null || true
rsync -a --delete --exclude='.extraction_tracker.json' "$CLAUDE_DIR/MEMORY/" "$BACKUP_DIR/MEMORY/" 2>/dev/null || true
rsync -a --delete --exclude='tool-results/' --exclude='subagents/' "$CLAUDE_DIR/projects/" "$BACKUP_DIR/projects/" 2>/dev/null || true
git add -A 2>/dev/null
if ! git diff --cached --quiet 2>/dev/null; then
TS=$(date '+%Y-%m-%d %H:%M')
SC=$(find "$BACKUP_DIR/projects" -name "*.jsonl" 2>/dev/null | wc -l)
DS=$(du -sh "$BACKUP_DIR/memory.db" 2>/dev/null | cut -f1)
git commit -m "backup: $TS | ${SC} sessions | db: ${DS}" --no-gpg-sign 2>/dev/null
log "Committed: ${SC} sessions, db: ${DS}"
git push 2>/dev/null && log "Pushed" || log "Push failed"
else
log "No changes"
fi
BEOF
chmod +x "$BIN_DIR/memory-backup"
ok "Helper scripts (memory-catchup, memory-backup)"
#######################################################################
# 9. Systemd user timers
#######################################################################
log "Setting up systemd timers..."
SYSTEMD_DIR="$HOME/.config/systemd/user"
mkdir -p "$SYSTEMD_DIR"
for unit in memory-catchup.service memory-catchup.timer memory-backup.service memory-backup.timer; do
if [ -f "$SCRIPT_DIR/systemd/$unit" ]; then
cp "$SCRIPT_DIR/systemd/$unit" "$SYSTEMD_DIR/"
fi
done
loginctl enable-linger "$(whoami)" 2>/dev/null || true
systemctl --user daemon-reload 2>/dev/null || true
systemctl --user enable --now memory-catchup.timer 2>/dev/null || log "Note: timers need a login session"
systemctl --user enable --now memory-backup.timer 2>/dev/null || log "Note: timers need a login session"
ok "Systemd timers (4h catchup + 4h backup)"
#######################################################################
# 10. Git backup repo
#######################################################################
BACKUP_DIR="$CLAUDE_DIR/conversations-backup"
if [ -d "$BACKUP_DIR/.git" ]; then
ok "Backup repo already initialized"
else
log "Initializing backup repo..."
mkdir -p "$BACKUP_DIR"
cd "$BACKUP_DIR"
git init
echo "*.jsonl filter=lfs diff=lfs merge=lfs -text" > .gitattributes
echo "node_modules/" > .gitignore
git add -A && git commit -m "init: LMF4 memory backup" --no-gpg-sign 2>/dev/null || true
cd ~
ok "Backup repo (add remote: cd $BACKUP_DIR && git remote add origin <url>)"
fi
#######################################################################
# 11. Memory flat files
#######################################################################
touch "$MEMORY_DIR/EXTRACT_LOG.txt"
[ -f "$MEMORY_DIR/HOT_RECALL.md" ] || echo "# Hot Recall (Last 10 Sessions)" > "$MEMORY_DIR/HOT_RECALL.md"
[ -f "$MEMORY_DIR/DISTILLED.md" ] || echo "# Distilled Memory" > "$MEMORY_DIR/DISTILLED.md"
[ -f "$MEMORY_DIR/README.md" ] || echo "# LMF4 — Memory System" > "$MEMORY_DIR/README.md"
ok "Memory flat files"
#######################################################################
# Summary
#######################################################################
echo ""
echo "═══════════════════════════════════════════════════════════"
echo " LMF4 — Installed"
echo "═══════════════════════════════════════════════════════════"
echo ""
echo " Components:"
echo " memory.db $(du -sh "$CLAUDE_DIR/memory.db" 2>/dev/null | cut -f1 || echo 'new') SQLite + FTS5"
echo " 5 hooks SessionExtract, AssociativeRecall, PreCompact,"
echo " PostCompact, StopFailure"
echo " MCP server memory_search + memory_recall"
if command -v mem &>/dev/null; then
echo " mem CLI $(which mem)"
fi
echo " systemd memory-catchup (4h) + memory-backup (4h)"
echo " backup repo ~/.claude/conversations-backup/"
echo ""
echo " How it works:"
echo " Session ends → SessionExtract parses transcript → writes to memory.db"
echo " You type → AssociativeRecall searches memory → injects context"
echo " Mid-session → memory_search MCP tool for deeper queries"
echo " Compaction → PreCompact saves, PostCompact verifies"
echo " Every 4h → Catchup extracts missed sessions"
echo " Every 4h → Backup commits + pushes to git"
echo ""
echo " Add to your CLAUDE.md:"
echo " cat $(dirname "$0")/templates/CLAUDE.md.memory"
echo ""
echo " Test it:"
echo " mem search \"test\" # CLI search"
echo " echo '{...}' | bun run ~/.claude/hooks/mem-mcp-server.ts # MCP"
echo ""