8cb4f09ded
Six independent fixes that make hook behaviour deterministic, debuggable, and free of silent footguns. Persist session_id in stored memories on_pre_compact.py now extracts session_id from the hook input and writes it under metadata.session_id, enabling search-by-session. Skip SessionStart bootstrap when MEM0_API_KEY is missing on_session_start.sh exits 0 early instead of emitting bootstrap text that instructs the agent to call mem0 MCP tools that would all fail without a key. Tighten block_memory_write.sh regex The old `*/memory/*.md` pattern blocked legitimate paths like docs/memory/architecture.md. New pattern blocks `*/MEMORY.md` and `*/.claude/memory/*` only -- the actual surfaces we want to protect. Opt-in debug logging via MEM0_DEBUG=1 Each hook (bash + python) redirects stderr / adds a FileHandler to ~/.mem0/hooks.log when MEM0_DEBUG is set. No helper module, no rotation -- KISS. Default behaviour is unchanged. Drop the duplicate PreCompact registration hooks.json and cursor-hooks.json had on_pre_compact.sh AND on_pre_compact.py registered on PreCompact, producing duplicate session-state writes. Drop the python entry. The python script stays alive -- on_stop.sh still spawns it as a session-end safety net. CI nudge Adds a single comment line to pyproject.toml because the repo's ci.yml path filters exclude mem0-plugin/-only PRs but the build_mem0/build_embedchain status checks are required by branch protection. Track the proper fix (path-conditional CI) as a follow-up.
68 lines
2.2 KiB
Bash
Executable File
68 lines
2.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Hook: PreCompact
|
|
#
|
|
# Fires BEFORE context compaction. This is the last chance to capture
|
|
# the full context before it gets compressed.
|
|
#
|
|
# Output: Text instructions injected into Claude's context.
|
|
# Claude still has the full conversation and can write an accurate summary.
|
|
# A companion Python script (on_pre_compact.py) also runs to capture
|
|
# transcript state directly via the Mem0 REST API as a safety net.
|
|
|
|
set -euo pipefail
|
|
|
|
if [ -n "${MEM0_DEBUG:-}" ]; then
|
|
mkdir -p "$HOME/.mem0" && exec 2>>"$HOME/.mem0/hooks.log"
|
|
fi
|
|
|
|
cat <<'EOF'
|
|
## CRITICAL: Pre-Compaction Session Summary
|
|
|
|
Context compaction is about to happen. You are about to lose most of your conversation history. You MUST store a comprehensive session summary NOW using the mem0 `add_memory` tool.
|
|
|
|
### Step 1: Store session summary
|
|
|
|
Call `add_memory` with a thorough summary covering ALL of the following:
|
|
|
|
```
|
|
## Session Summary (Pre-Compaction)
|
|
|
|
### User's Goal
|
|
[What the user originally asked for and their intent]
|
|
|
|
### What Was Accomplished
|
|
[Numbered list of tasks completed, features built, bugs fixed]
|
|
|
|
### Key Decisions Made
|
|
[Architectural choices, design decisions, trade-offs discussed]
|
|
|
|
### Files Created or Modified
|
|
[List of important file paths with what changed in each]
|
|
|
|
### Current State
|
|
[What is in progress RIGHT NOW — the task you were in the middle of]
|
|
[Any pending items, blockers, or next steps]
|
|
|
|
### Important Context
|
|
[User preferences observed, coding patterns, anything that would help
|
|
the post-compaction agent continue without asking redundant questions]
|
|
```
|
|
|
|
Include metadata: `{"type": "session_state", "source": "pre-compaction"}`
|
|
|
|
### Step 2: Store any unstored learnings
|
|
|
|
If there are learnings from this session that you haven't stored yet, store them as separate memories:
|
|
- Failed approaches -> metadata `{"type": "anti_pattern"}`
|
|
- Successful strategies -> metadata `{"type": "task_learning"}`
|
|
- Architecture decisions -> metadata `{"type": "decision"}`
|
|
|
|
### Step 3: Acknowledge
|
|
|
|
After storing, briefly tell the user that session state has been saved and you're ready for compaction.
|
|
|
|
Do this NOW. Do not skip any section. The quality of this summary directly determines whether you can continue the user's task after compaction.
|
|
EOF
|
|
|
|
exit 0
|