Skip to content

Commit 74a599c

Browse files
Add Claude Code integration with auto-save scripts
- Shell wrapper for automatic context saving on exit - Background daemon for continuous auto-save - Git hooks for commit-triggered saves - Documentation in README with setup instructions
1 parent da5d38b commit 74a599c

4 files changed

Lines changed: 180 additions & 0 deletions

File tree

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,85 @@ On every message/tool call:
251251

252252
---
253253

254+
## Claude Code Integration
255+
256+
StackMemory can automatically save context when using Claude Code, ensuring your AI assistant always has access to previous context and decisions.
257+
258+
### Quick Setup
259+
260+
1. **Install the wrapper script**:
261+
```bash
262+
# Make scripts executable
263+
chmod +x scripts/claude-code-wrapper.sh scripts/stackmemory-daemon.sh
264+
265+
# Add alias to your shell config
266+
echo 'alias claude="~/Dev/stackmemory/scripts/claude-code-wrapper.sh"' >> ~/.zshrc
267+
source ~/.zshrc
268+
```
269+
270+
2. **Use Claude Code with auto-save**:
271+
```bash
272+
# Instead of: claude-code
273+
# Use: claude
274+
275+
# Context is automatically saved on exit (Ctrl+C)
276+
```
277+
278+
### Integration Methods
279+
280+
#### 1. Shell Wrapper (Recommended)
281+
Automatically saves context when Claude Code exits:
282+
```bash
283+
# Use the wrapper
284+
./scripts/claude-code-wrapper.sh
285+
286+
# Or with the alias
287+
claude
288+
```
289+
290+
#### 2. Background Daemon
291+
Continuously saves context every 5 minutes:
292+
```bash
293+
# Start daemon
294+
./scripts/stackmemory-daemon.sh &
295+
296+
# Custom interval (60 seconds)
297+
./scripts/stackmemory-daemon.sh 60 &
298+
299+
# Stop daemon
300+
kill $(cat /tmp/stackmemory-daemon.pid)
301+
```
302+
303+
#### 3. Git Hooks
304+
Save context automatically on git commits:
305+
```bash
306+
# Install in current repo
307+
./scripts/setup-git-hooks.sh
308+
```
309+
310+
#### 4. Manual Function
311+
Add to `~/.zshrc`:
312+
```bash
313+
claude_with_sm() {
314+
claude "$@"
315+
local exit_code=$?
316+
if [ -d ".stackmemory" ]; then
317+
stackmemory status
318+
[ -n "$LINEAR_API_KEY" ] && stackmemory linear sync
319+
fi
320+
return $exit_code
321+
}
322+
```
323+
324+
### Features
325+
326+
- **Automatic context preservation** - Saves on exit (including Ctrl+C)
327+
- **Linear sync** - Syncs tasks if LINEAR_API_KEY is set
328+
- **Smart detection** - Only runs in StackMemory-enabled projects
329+
- **Zero overhead** - No performance impact during Claude Code sessions
330+
331+
---
332+
254333
## Guarantees
255334

256335
* ✅ Lossless storage (no destructive compaction)

scripts/claude-code-wrapper.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
3+
# Claude Code wrapper with StackMemory integration
4+
# Usage: Add alias to ~/.zshrc: alias claude='~/Dev/stackmemory/scripts/claude-code-wrapper.sh'
5+
6+
cleanup() {
7+
echo "📝 Saving StackMemory context..."
8+
9+
# Check if in a git repo with stackmemory
10+
if [ -d ".stackmemory" ] && [ -f "stackmemory.json" ]; then
11+
# Save current context
12+
stackmemory status 2>/dev/null
13+
14+
# If Linear API key is set, sync
15+
if [ -n "$LINEAR_API_KEY" ]; then
16+
echo "🔄 Syncing with Linear..."
17+
stackmemory linear sync 2>/dev/null
18+
fi
19+
20+
echo "✅ StackMemory context saved"
21+
fi
22+
}
23+
24+
# Set trap for exit signals
25+
trap cleanup EXIT INT TERM
26+
27+
# Run Claude Code (try multiple possible command names)
28+
if command -v claude-code &> /dev/null; then
29+
claude-code "$@"
30+
elif command -v claude &> /dev/null; then
31+
claude "$@"
32+
else
33+
echo "❌ Claude Code not found. Please install it first."
34+
echo " Visit: https://github.com/anthropics/claude-code"
35+
exit 1
36+
fi

scripts/setup-git-hooks.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
# Setup git hooks for StackMemory auto-save
4+
# This runs StackMemory commands on git operations
5+
6+
HOOK_DIR="$(git rev-parse --git-dir 2>/dev/null)/hooks"
7+
8+
if [ -z "$HOOK_DIR" ] || [ ! -d "$HOOK_DIR" ]; then
9+
echo "❌ Not in a git repository"
10+
exit 1
11+
fi
12+
13+
# Create pre-commit hook
14+
cat > "$HOOK_DIR/pre-commit" << 'EOF'
15+
#!/bin/bash
16+
# StackMemory pre-commit hook
17+
18+
if [ -d ".stackmemory" ]; then
19+
echo "📝 Saving StackMemory context before commit..."
20+
stackmemory status 2>/dev/null || true
21+
fi
22+
EOF
23+
24+
chmod +x "$HOOK_DIR/pre-commit"
25+
echo "✅ Git hooks installed for StackMemory"

scripts/stackmemory-daemon.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
# StackMemory background daemon
4+
# Automatically saves context every 5 minutes and on exit
5+
6+
INTERVAL=${1:-300} # Default 5 minutes
7+
PID_FILE="/tmp/stackmemory-daemon.pid"
8+
9+
cleanup() {
10+
echo "🛑 Stopping StackMemory daemon..."
11+
if [ -d ".stackmemory" ]; then
12+
stackmemory status
13+
[ -n "$LINEAR_API_KEY" ] && stackmemory linear sync
14+
fi
15+
rm -f "$PID_FILE"
16+
exit 0
17+
}
18+
19+
trap cleanup EXIT INT TERM
20+
21+
# Save PID
22+
echo $$ > "$PID_FILE"
23+
24+
echo "🚀 StackMemory daemon started (PID: $$)"
25+
echo " Auto-save interval: ${INTERVAL}s"
26+
echo " Press Ctrl+C to stop"
27+
28+
while true; do
29+
sleep "$INTERVAL"
30+
31+
if [ -d ".stackmemory" ]; then
32+
echo "[$(date)] Auto-saving StackMemory context..."
33+
stackmemory status 2>/dev/null || true
34+
35+
# Only sync with Linear once per hour
36+
if [ $(($(date +%s) % 3600)) -lt "$INTERVAL" ] && [ -n "$LINEAR_API_KEY" ]; then
37+
stackmemory linear sync 2>/dev/null || true
38+
fi
39+
fi
40+
done

0 commit comments

Comments
 (0)