ed5ee7b9ff
Hooks previously fell back to $USER when MEM0_USER_ID wasn't set,
producing a different user_id on every machine for the same person.
Result: a single account with memories scattered across many user
buckets, none of which can see each other.
Resolution priority (same in bash and python):
1. MEM0_USER_ID env var (explicit override)
2. ~/.mem0/identity.json cache (pinned to MEM0_API_KEY fingerprint)
3. Derived: "mem0-" + sha256(MEM0_API_KEY)[:12]
4. Fallback: $USER, else "default"
Same MEM0_API_KEY across machines now yields the same user_id without
the user having to set MEM0_USER_ID by hand on every laptop.
Resolver shipped as two tiny files instead of a shared module:
_identity.sh -- sourced by bash hooks, exports MEM0_RESOLVED_USER_ID
_identity.py -- imported by on_pre_compact.py, exposes resolve_user_id()
Hook integration:
on_user_prompt.sh -- sources resolver, USER_ID interpolated into rubric
on_session_start.sh -- emits an "Active user_id: <X>" header before the
bootstrap text, so the agent's MCP search_memories/add_memory calls
use the same bucket the hooks write to (closes the agent-side half
of the symptom)
on_pre_compact.py -- replaces inline env lookup with resolve_user_id()
Existing memories under previous $USER values are not auto-migrated.
The cache file regenerates on key rotation (fingerprint mismatch).
CI nudge in pyproject.toml because path filters in ci.yml exclude
plugin-only PRs but build_mem0/build_embedchain are required.
Manual verification: same key on different $USER values resolves to
identical user_id; MEM0_USER_ID override bypasses cache and key
derivation; cache invalidates on key change; bash and python
implementations produce identical output for all four priority levels.
69 lines
2.7 KiB
Bash
Executable File
69 lines
2.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Hook: UserPromptSubmit
|
|
#
|
|
# Fires on every user message. Instead of pre-searching mem0 with the
|
|
# raw prompt, this injects a decision rubric telling the agent when
|
|
# and how to search itself. The agent has more context than this
|
|
# script does -- let it decide.
|
|
#
|
|
# Input: JSON on stdin (prompt, session_id, cwd, transcript_path)
|
|
# Output: Decision rubric injected into Claude's context (exit 0)
|
|
|
|
# Intentionally omit -e so the script always exits 0 even if jq fails --
|
|
# must never block the user's prompt.
|
|
set -uo pipefail
|
|
|
|
INPUT=$(cat)
|
|
PROMPT=$(echo "$INPUT" | jq -r '.prompt // ""' 2>/dev/null || echo "")
|
|
|
|
# Acknowledgements and short replies don't warrant memory context
|
|
if [ ${#PROMPT} -lt 20 ]; then
|
|
exit 0
|
|
fi
|
|
|
|
# No API key means the agent can't search anyway
|
|
if [ -z "${MEM0_API_KEY:-}" ]; then
|
|
exit 0
|
|
fi
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
|
# shellcheck source=_identity.sh
|
|
. "$SCRIPT_DIR/_identity.sh"
|
|
USER_ID="$MEM0_RESOLVED_USER_ID"
|
|
|
|
cat <<EOF
|
|
## Memory check
|
|
|
|
Before responding, decide whether persistent memory context from mem0 would
|
|
improve your answer. The agent -- not this hook -- owns this decision.
|
|
|
|
**Search WHEN** the user:
|
|
- references past work, decisions, or things "we" built
|
|
- asks "how should we...", "best way to...", or any decision-style question
|
|
- hits an error, bug, or asks for debugging help
|
|
- requests work that touches their stack, tools, conventions, or preferences
|
|
- starts a non-trivial task in a known project
|
|
|
|
**Skip WHEN:**
|
|
- the prompt is an acknowledgement or continuation
|
|
- the user is *stating* new info -- that's a write trigger (\`add_memory\`), not a search
|
|
- it's a pure syntax / factual question answerable from general knowledge
|
|
- you already searched this scope earlier in the turn
|
|
|
|
**If searching, do it well:**
|
|
- Run **2-4 parallel** \`search_memories\` calls with different angles, not one
|
|
query that echoes the user's prompt.
|
|
- Phrase queries as **nouns** ("auth module decisions"), not full sentences.
|
|
- Filter shape: the root must be a logical operator (\`AND\` / \`OR\` / \`NOT\`)
|
|
with an array, and metadata uses a **nested** object (not dotted keys).
|
|
Combine \`user_id\` with one \`metadata.type\` clause per call:
|
|
- \`{"AND": [{"user_id": "$USER_ID"}, {"metadata": {"type": "decision"}}]}\` -- design / architecture
|
|
- \`{"AND": [{"user_id": "$USER_ID"}, {"metadata": {"type": "anti_pattern"}}]}\` -- debugging, error handling
|
|
- \`{"AND": [{"user_id": "$USER_ID"}, {"metadata": {"type": "user_preference"}}]}\` -- tooling, stack, style
|
|
- \`{"AND": [{"user_id": "$USER_ID"}, {"metadata": {"type": "convention"}}]}\` -- established patterns
|
|
- Or scope with just \`{"AND": [{"user_id": "$USER_ID"}]}\` when no metadata filter fits.
|
|
- Empty results are normal -- proceed without context.
|
|
EOF
|
|
|
|
exit 0
|