session-touched-ledger.sh 1.1 KB

123456789101112131415161718192021222324
  1. #!/bin/bash
  2. # hooks/session-touched-ledger.sh
  3. # PostToolUse(Edit|Write) — record the files THIS session has written, so the pre-write peer-guard
  4. # (hooks/pre-write-peer-guard.sh) can tell my own edits apart from a peer session's. Silent, never
  5. # blocks, exit 0 always. Companion: pre-write-peer-guard.sh. See rules/worktree-boundaries.md.
  6. #
  7. # Ledger: $HOME/.claude/.session-touched/<session_id>.list (one absolute file path per line).
  8. # Pruned opportunistically (files >2 days old) on the first write of each new session.
  9. set -uo pipefail
  10. INPUT=$(cat 2>/dev/null) || exit 0
  11. command -v jq >/dev/null 2>&1 || exit 0
  12. SID=$(printf '%s' "$INPUT" | jq -r '.session_id // empty' 2>/dev/null)
  13. FP=$(printf '%s' "$INPUT" | jq -r '.tool_input.file_path // empty' 2>/dev/null)
  14. [ -n "$SID" ] && [ -n "$FP" ] || exit 0
  15. DIR="$HOME/.claude/.session-touched"
  16. LEDGER="$DIR/$SID.list"
  17. mkdir -p "$DIR" 2>/dev/null || exit 0
  18. # First write of this session → prune stale ledgers from old sessions.
  19. [ -f "$LEDGER" ] || find "$DIR" -type f -mtime +2 -delete 2>/dev/null || true
  20. printf '%s\n' "$FP" >> "$LEDGER" 2>/dev/null || true
  21. exit 0