fix: debounce session-save hook to once per 10 minutes
Prevents flooding OpenBrain with near-identical context memories during long sessions. Uses per-project timestamp file for debounce. Also includes recent commits in saved context. Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
93415257f3
commit
8e63c19137
1 changed files with 22 additions and 2 deletions
|
|
@ -2,11 +2,14 @@
|
|||
# Stop hook: Save session context to OpenBrain after each response
|
||||
# Runs async so it doesn't block the conversation
|
||||
#
|
||||
# Only saves if there are git changes (indicates real work happened)
|
||||
# Debounced: only saves once per 10 minutes per project
|
||||
# Only saves if there are git changes (real work happened)
|
||||
|
||||
BRAIN_URL="${CORE_BRAIN_URL:-https://api.lthn.sh}"
|
||||
BRAIN_KEY="${CORE_BRAIN_KEY:-}"
|
||||
BRAIN_KEY_FILE="${HOME}/.claude/brain.key"
|
||||
DEBOUNCE_DIR="${HOME}/.claude/sessions/debounce"
|
||||
DEBOUNCE_SECONDS=600 # 10 minutes
|
||||
|
||||
# Load API key
|
||||
if [[ -z "$BRAIN_KEY" && -f "$BRAIN_KEY_FILE" ]]; then
|
||||
|
|
@ -32,10 +35,24 @@ case "$CWD" in
|
|||
esac
|
||||
[[ -z "$PROJECT" ]] && PROJECT="unknown"
|
||||
|
||||
# Debounce: skip if we saved this project less than 10 min ago
|
||||
mkdir -p "$DEBOUNCE_DIR"
|
||||
DEBOUNCE_FILE="${DEBOUNCE_DIR}/${PROJECT}"
|
||||
NOW=$(date '+%s')
|
||||
|
||||
if [[ -f "$DEBOUNCE_FILE" ]]; then
|
||||
LAST_SAVE=$(cat "$DEBOUNCE_FILE" 2>/dev/null)
|
||||
if [[ -n "$LAST_SAVE" ]]; then
|
||||
AGE=$((NOW - LAST_SAVE))
|
||||
[[ $AGE -lt $DEBOUNCE_SECONDS ]] && exit 0
|
||||
fi
|
||||
fi
|
||||
|
||||
BRANCH=$(git branch --show-current 2>/dev/null || echo "none")
|
||||
CHANGED_FILES=$(git diff --name-only 2>/dev/null | head -10 | tr '\n' ', ')
|
||||
RECENT_COMMITS=$(git log --oneline -3 2>/dev/null | tr '\n' '; ')
|
||||
|
||||
CONTENT="Working on ${PROJECT} (branch: ${BRANCH}). Changed files: ${CHANGED_FILES}"
|
||||
CONTENT="Session on ${PROJECT} (branch: ${BRANCH}). Changed: ${CHANGED_FILES}Recent commits: ${RECENT_COMMITS}"
|
||||
|
||||
curl -s --max-time 5 "${BRAIN_URL}/v1/brain/remember" \
|
||||
-X POST \
|
||||
|
|
@ -50,4 +67,7 @@ curl -s --max-time 5 "${BRAIN_URL}/v1/brain/remember" \
|
|||
\"tags\": [\"auto-save\", \"session\"]
|
||||
}" >/dev/null 2>&1
|
||||
|
||||
# Update debounce timestamp
|
||||
echo "$NOW" > "$DEBOUNCE_FILE"
|
||||
|
||||
exit 0
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue