cc-os/plugins/memory/hooks/post-tool-use-write.sh

56 lines
1.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# post-tool-use-write.sh — PostToolUse hook for Write/Edit tool calls
# Fires after every Write or Edit; updates the Graphify graph if the file
# is a .md file inside the configured vault.
# Ensure log directory exists
mkdir -p ~/.cache/graphify
# Parse config with python3+PyYAML; handles missing file, missing key, and tilde
VAULT_PATH=$(python3 -c '
import yaml, os
p = os.path.expanduser("~/.claude/plugins/memory/config.yaml")
cfg = (yaml.safe_load(open(p)) or {}) if os.path.exists(p) else {}
print(os.path.expanduser(cfg.get("vault_path", "~/Documents/SecondBrain")))
')
# Capture stdin
STDIN=$(cat)
# Extract fields
FILE_PATH=$(echo "$STDIN" | jq -r '.tool_input.file_path // empty')
SESSION_ID=$(echo "$STDIN" | jq -r '.session_id // empty')
# Guard: exit silently if FILE_PATH is empty
if [[ -z "$FILE_PATH" ]]; then
exit 0
fi
# Guard: exit silently if file does not end in .md
if [[ "$FILE_PATH" != *.md ]]; then
exit 0
fi
# Guard: exit silently if file is not under VAULT_PATH
if [[ "$FILE_PATH" != "$VAULT_PATH"* ]]; then
exit 0
fi
# Record vault write and invalidate rebuild stamp
LOG="$HOME/.cache/graphify/post-tool-use.log"
# Always record touched path for session-end reconciliation
TOUCH_FILE="/tmp/claude-vault-touched-$SESSION_ID"
echo "$FILE_PATH" >> "$TOUCH_FILE"
# Invalidate the rebuild stamp so next SessionStart triggers an incremental --update rebuild
STAMP_FILE="$HOME/.cache/graphify/vault-rebuild.stamp"
if rm -f "$STAMP_FILE"; then
echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] Invalidated rebuild stamp: $FILE_PATH" >> "$LOG"
else
# Deletion failed (shouldn't happen), but never block
echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] WARN: Could not delete rebuild stamp for $FILE_PATH" >> "$LOG"
fi
exit 0