Skip to content

Commit cf5e00a

Browse files
feat(plugin): inject Memory Protocol in session hooks for proactive saves
The skill file alone wasn't enough — Claude Code read it passively and only saved memories when asked. Now the Memory Protocol instructions are injected as additionalContext at session start AND post-compaction, so the agent always has explicit triggers for when to call mem_save. Changes: - SKILL.md: explicit trigger list with self-check prompt - session-start.sh: injects protocol + memory context - post-compaction.sh: injects protocol + compaction instruction + context
1 parent 2abf1ef commit cf5e00a

3 files changed

Lines changed: 88 additions & 17 deletions

File tree

plugin/claude-code/scripts/post-compaction.sh

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22
# Engram — Post-compaction hook for Claude Code
33
#
4-
# When compaction happens, inject previous session context and instruct
4+
# When compaction happens, inject Memory Protocol + context and instruct
55
# the agent to persist the compacted summary via mem_session_summary.
66

77
ENGRAM_PORT="${ENGRAM_PORT:-7437}"
@@ -22,18 +22,45 @@ if [ -n "$SESSION_ID" ] && [ -n "$PROJECT" ]; then
2222
> /dev/null 2>&1
2323
fi
2424

25-
# Inject context from previous sessions
25+
# Fetch context from previous sessions
2626
CONTEXT=$(curl -sf "${ENGRAM_URL}/context?project=${PROJECT}" --max-time 3 2>/dev/null | jq -r '.context // empty')
2727

28-
# Build output for Claude
29-
cat <<EOF
30-
${CONTEXT}
28+
# Inject Memory Protocol + compaction instruction + context
29+
cat <<PROTOCOL
30+
## Engram Persistent Memory — ACTIVE PROTOCOL
31+
32+
You have engram memory tools (mem_save, mem_search, mem_context, mem_session_summary).
33+
This protocol is MANDATORY and ALWAYS ACTIVE.
34+
35+
### PROACTIVE SAVE — do NOT wait for user to ask
36+
Call \`mem_save\` IMMEDIATELY after ANY of these:
37+
- Decision made (architecture, convention, workflow, tool choice)
38+
- Bug fixed (include root cause)
39+
- Convention or workflow documented/updated
40+
- Notion/Jira/GitHub artifact created or updated with significant content
41+
- Non-obvious discovery, gotcha, or edge case found
42+
- Pattern established (naming, structure, approach)
43+
- User preference or constraint learned
44+
- Feature implemented with non-obvious approach
45+
46+
**Self-check after EVERY task**: "Did I just make a decision, fix a bug, learn something, or establish a convention? If yes → mem_save NOW."
47+
48+
### SEARCH MEMORY when:
49+
- User asks to recall anything ("remember", "what did we do", "acordate", "qué hicimos")
50+
- Starting work on something that might have been done before
51+
- User mentions a topic you have no context on
52+
53+
### SESSION CLOSE — before saying "done"/"listo":
54+
Call \`mem_session_summary\` with: Goal, Discoveries, Accomplished, Next Steps, Relevant Files.
55+
56+
---
3157
3258
CRITICAL INSTRUCTION POST-COMPACTION:
33-
You have access to Engram persistent memory via MCP tools (mem_save, mem_search, mem_session_summary, etc.).
3459
FIRST ACTION REQUIRED: Call mem_session_summary with the content of the compacted summary above. Use project: '${PROJECT}'.
3560
This preserves what was accomplished before compaction. Do this BEFORE any other work.
3661
This is NOT optional. Without this, everything done before compaction is lost from memory.
37-
EOF
62+
63+
${CONTEXT}
64+
PROTOCOL
3865

3966
exit 0

plugin/claude-code/scripts/session-start.sh

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# 1. Ensures the engram server is running
55
# 2. Creates a session in engram
66
# 3. Auto-imports git-synced chunks if .engram/manifest.json exists
7-
# 4. Injects memory context into the session
7+
# 4. Injects Memory Protocol instructions + memory context
88

99
ENGRAM_PORT="${ENGRAM_PORT:-7437}"
1010
ENGRAM_URL="http://127.0.0.1:${ENGRAM_PORT}"
@@ -35,13 +35,41 @@ if [ -f "${CWD}/.engram/manifest.json" ]; then
3535
engram sync --import 2>/dev/null
3636
fi
3737

38-
# Inject memory context — stdout goes to Claude as additionalContext
38+
# Fetch memory context
3939
CONTEXT=$(curl -sf "${ENGRAM_URL}/context?project=${PROJECT}" --max-time 3 2>/dev/null | jq -r '.context // empty')
4040

41+
# Inject Memory Protocol + context — stdout goes to Claude as additionalContext
42+
cat <<'PROTOCOL'
43+
## Engram Persistent Memory — ACTIVE PROTOCOL
44+
45+
You have engram memory tools (mem_save, mem_search, mem_context, mem_session_summary).
46+
This protocol is MANDATORY and ALWAYS ACTIVE.
47+
48+
### PROACTIVE SAVE — do NOT wait for user to ask
49+
Call `mem_save` IMMEDIATELY after ANY of these:
50+
- Decision made (architecture, convention, workflow, tool choice)
51+
- Bug fixed (include root cause)
52+
- Convention or workflow documented/updated
53+
- Notion/Jira/GitHub artifact created or updated with significant content
54+
- Non-obvious discovery, gotcha, or edge case found
55+
- Pattern established (naming, structure, approach)
56+
- User preference or constraint learned
57+
- Feature implemented with non-obvious approach
58+
59+
**Self-check after EVERY task**: "Did I just make a decision, fix a bug, learn something, or establish a convention? If yes → mem_save NOW."
60+
61+
### SEARCH MEMORY when:
62+
- User asks to recall anything ("remember", "what did we do", "acordate", "qué hicimos")
63+
- Starting work on something that might have been done before
64+
- User mentions a topic you have no context on
65+
66+
### SESSION CLOSE — before saying "done"/"listo":
67+
Call `mem_session_summary` with: Goal, Discoveries, Accomplished, Next Steps, Relevant Files.
68+
PROTOCOL
69+
70+
# Inject memory context if available
4171
if [ -n "$CONTEXT" ]; then
42-
cat <<EOF
43-
${CONTEXT}
44-
EOF
72+
printf "\n%s\n" "$CONTEXT"
4573
fi
4674

4775
exit 0

plugin/claude-code/skills/memory/SKILL.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
11
---
22
name: engram-memory
3-
description: Persistent memory protocol for AI coding agents. Automatically activated to manage memory across sessions — saving decisions, searching past work, and persisting session summaries.
3+
description: "ALWAYS ACTIVE — Persistent memory protocol. You MUST save decisions, conventions, bugs, and discoveries to engram proactively. Do NOT wait for the user to ask."
44
---
55

66
# Engram Persistent Memory — Protocol
77

88
You have access to Engram, a persistent memory system that survives across sessions and compactions.
9+
This protocol is MANDATORY and ALWAYS ACTIVE — not something you activate on demand.
910

10-
## WHEN TO SAVE (mandatory — not optional)
11+
## PROACTIVE SAVE TRIGGERS (mandatory — do NOT wait for user to ask)
1112

12-
Call `mem_save` IMMEDIATELY after any of these:
13-
- Bug fix completed
13+
Call `mem_save` IMMEDIATELY and WITHOUT BEING ASKED after any of these:
14+
15+
### After decisions or conventions
1416
- Architecture or design decision made
17+
- Team convention documented or established
18+
- Workflow change agreed upon
19+
- Tool or library choice made with tradeoffs
20+
21+
### After completing work
22+
- Bug fix completed (include root cause)
23+
- Feature implemented with non-obvious approach
24+
- Notion/Jira/GitHub artifact created or updated with significant content
25+
- Configuration change or environment setup done
26+
27+
### After discoveries
1528
- Non-obvious discovery about the codebase
16-
- Configuration change or environment setup
29+
- Gotcha, edge case, or unexpected behavior found
1730
- Pattern established (naming, structure, convention)
1831
- User preference or constraint learned
1932

33+
### Self-check — ask yourself after EVERY task:
34+
> "Did I just make a decision, fix a bug, learn something non-obvious, or establish a convention? If yes, call mem_save NOW."
35+
2036
Format for `mem_save`:
2137
- **title**: Verb + what — short, searchable (e.g. "Fixed N+1 query in UserList", "Chose Zustand over Redux")
2238
- **type**: bugfix | decision | architecture | discovery | pattern | config | preference

0 commit comments

Comments
 (0)