74e9cbcfd3
Adds a `hook_silent_save` mode (default `true` in new installs) where
the stop and precompact hooks write diary entries directly via the
Python API — no AI block, no MCP tool roundtrip, no possibility of the
AI forgetting or ignoring the save instruction.
**Two modes, controlled by `hook_silent_save` in `~/.mempalace/config.json`:**
1. **Silent mode** (default): Direct call to `tool_diary_write()`. Plain
text, no AI involved, deterministic. Save marker advances only after
the write is confirmed, so mid-save failures do not lose exchanges.
Shows `"✦ N memories woven into the palace"` as a systemMessage
notification so the user knows the save fired.
2. **Block mode** (legacy): Returns `{"decision": "block"}` asking the
AI to call the MCP tool chain. Non-deterministic — the AI may ignore,
summarize lossy, or fail. Kept for backward compatibility.
**Extras rolled in:**
- Block reasons name "MemPalace" explicitly and instruct the AI not to
write to Claude Code's native auto-memory (.md files) — prevents the
two memory systems from stepping on each other.
- Codex transcript handling (`event_msg` payloads) in
`_count_human_messages` + `_extract_recent_messages`.
- Tightened stopword leak in diary summaries; docstring polish; test
hermeticity fixes (per-test `STATE_DIR` patching).
**Tests:** hooks_cli tests cover silent-save path, save-marker
advancement after confirmed write only, and systemMessage formatting.
Rebased fresh on upstream/develop. Only touches files germane to the
feature (hooks_cli.py, tests, hooks/README.md, HOOKS_TUTORIAL.md) —
stale fork-local `.sh` wrapper and plugin manifest changes dropped.
51 lines
1.8 KiB
Markdown
51 lines
1.8 KiB
Markdown
# How to Use MemPalace Hooks (Auto-Save)
|
|
|
|
MemPalace hooks act as an "Auto-Save" feature. They help your AI keep a permanent memory without you needing to run manual commands.
|
|
|
|
### 1. What are these hooks?
|
|
* **Save Hook** (`mempal_save_hook.sh`): Saves new facts and decisions every 15 messages.
|
|
* **PreCompact Hook** (`mempal_precompact_hook.sh`): Saves your context right before the AI's memory window fills up.
|
|
|
|
### 2. Setup for Claude Code
|
|
Add this to your configuration file to enable automatic background saving:
|
|
|
|
```json
|
|
{
|
|
"hooks": {
|
|
"Stop": [
|
|
{
|
|
"matcher": "",
|
|
"hooks": [{"type": "command", "command": "./hooks/mempal_save_hook.sh"}]
|
|
}
|
|
],
|
|
"PreCompact": [
|
|
{
|
|
"matcher": "",
|
|
"hooks": [{"type": "command", "command": "./hooks/mempal_precompact_hook.sh"}]
|
|
}
|
|
]
|
|
}
|
|
}
|
|
```
|
|
|
|
### 3. What changed (v3.1.0+)
|
|
|
|
Both hooks now have **two-layer capture**:
|
|
|
|
1. **Auto-mine**: Before blocking the AI, the hook runs the normalizer on the JSONL transcript and upserts chunks directly into the palace. This captures raw tool output (Bash results, search findings, build errors) that the AI would otherwise summarize away.
|
|
|
|
2. **Updated reason messages**: The block reason now explicitly tells the AI to save tool output verbatim — not just topics and decisions.
|
|
|
|
### 4. Backfill past conversations (one-time)
|
|
|
|
The hooks capture conversations going forward, but you probably have months of past sessions. Run this once to mine them all:
|
|
|
|
```bash
|
|
mempalace mine ~/.claude/projects/ --mode convos
|
|
```
|
|
|
|
### 5. Configuration
|
|
|
|
- **`SAVE_INTERVAL=15`** — How many human messages between saves
|
|
- **`MEMPALACE_PYTHON`** — Python interpreter with mempalace + chromadb. Auto-detects: env var → repo venv → system python3
|
|
- **`MEMPAL_DIR`** — Optional directory for auto-ingest via `mempalace mine` |