diff --git a/README.md b/README.md index d7ed796..bd75d0b 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ Don't install this. Just steal what you like. | `file-history/` | Change tracking for edited files | | `session-env/` | Environment snapshots per session | | `sessions/` | Session state and conversation data | -| `logs/` | Session and commit history logs | +| `logs/` | Per-session hook and git command logs | | `debug/` | Session debug output | | `shell-snapshots/` | Shell environment captures | | `cache/` | Temporary cached data | @@ -177,7 +177,7 @@ This configuration includes 11 hooks: #### Logging Hooks (PreToolUse, PostToolUse, and others) -- **log-git-commands.sh** - Logs all git/gh commands to stderr for tracking +- **log-git-commands.sh** - Logs all git/gh commands to per-session log files - **log-hook-event.sh** - Companion logger on every lifecycle event for observability #### Formatting Hooks (PostToolUse) @@ -191,20 +191,23 @@ This configuration includes 11 hooks: - **load-session-context.sh** - Injects git repository context at session start - **evaluate-session.js** - Analyzes session for learnings and patterns -#### Notification Hooks (Notification) +#### Cleanup Hooks (SessionEnd) -- **notify-idle.sh** - macOS notification when Claude is ready for input +- **session-cleanup.sh** - Removes session log directories older than 7 days and cleans stale debug/snapshot files on session exit ## Common Operations ### Review Recent Activity ```bash -# Check recent session logs -tail -n 50 logs/session-log.txt +# List session log directories +ls logs/ -# View commit history -cat logs/commit-log.txt +# Check hook events for a session (last 8 chars of session ID) +tail -n 50 logs//hook-events.log + +# View git commands for a session +cat logs//git-commands.log ``` ### Inspect Project Metadata diff --git a/hooks/log-git-commands.sh b/hooks/log-git-commands.sh index 4cd2c33..ac27bf9 100755 --- a/hooks/log-git-commands.sh +++ b/hooks/log-git-commands.sh @@ -3,14 +3,21 @@ # # Note: Intentionally no 'set -euo pipefail' - hooks must always exit 0 -command=$(jq -r '.tool_input.command // empty' 2>/dev/null || echo "") -description=$(jq -r '.tool_input.description // "No description"' 2>/dev/null || echo "No description") +input=$(cat) +command=$(echo "$input" | jq -r '.tool_input.command // empty' 2>/dev/null || echo "") +description=$(echo "$input" | jq -r '.tool_input.description // "No description"' 2>/dev/null || echo "No description") # Only log git/gh/dot commands if echo "$command" | grep -qE '^(git|gh|dot)\s'; then timestamp=$(date '+%Y-%m-%d %H:%M:%S') - mkdir -p ~/.claude/logs - echo "[$timestamp] $command | $description" >>~/.claude/logs/git-commands.log + # Extract session ID from stdin JSON (last 8 chars) + session_id=$(echo "$input" | jq -r '.session_id // empty' 2>/dev/null) + session_id="${session_id: -8}" + session_id="${session_id//[^a-zA-Z0-9]/_}" + session_id="${session_id:-default}" + log_dir=~/.claude/logs/"$session_id" + mkdir -p "$log_dir" + echo "[$timestamp] $command | $description" >>"$log_dir"/git-commands.log fi exit 0 # Never block, just log diff --git a/hooks/log-hook-event.sh b/hooks/log-hook-event.sh index 37f220f..82492e0 100755 --- a/hooks/log-hook-event.sh +++ b/hooks/log-hook-event.sh @@ -4,12 +4,19 @@ event_name="$1" timestamp=$(date '+%Y-%m-%d %H:%M:%S') -mkdir -p ~/.claude/logs input=$(cat) tool="" tool_input="" +# Extract session ID from stdin JSON (last 8 chars) +session_id=$(echo "$input" | jq -r '.session_id // empty' 2>/dev/null) +session_id="${session_id: -8}" +session_id="${session_id//[^a-zA-Z0-9]/_}" +session_id="${session_id:-default}" +log_dir=~/.claude/logs/"$session_id" +mkdir -p "$log_dir" + if [ -n "$input" ]; then tool=$(echo "$input" | jq -r '.tool // .tool_name // empty' 2>/dev/null) tool_input=$(echo "$input" | jq -c '.tool_input // empty' 2>/dev/null) @@ -23,6 +30,6 @@ if [ -n "$tool_input" ] && [ "$tool_input" != "null" ]; then line="$line input=$tool_input" fi -echo "$line" >>~/.claude/logs/hook-events.log +echo "$line" >>"$log_dir"/hook-events.log exit 0 diff --git a/hooks/session-cleanup.sh b/hooks/session-cleanup.sh new file mode 100755 index 0000000..461f481 --- /dev/null +++ b/hooks/session-cleanup.sh @@ -0,0 +1,19 @@ +#!/bin/bash +# SessionEnd cleanup - delete logs and session data older than 7 days +# Note: Intentionally no 'set -euo pipefail' - hooks must always exit 0 + +log_dir=~/.claude/logs + +# Remove session log directories older than 7 days +find "$log_dir" -mindepth 1 -maxdepth 1 -type d -mtime +7 -exec rm -rf -- {} + 2>/dev/null || true + +# Remove legacy flat log files from before session-scoped logging +rm -f "$log_dir/hook-events.log" "$log_dir/hook-events.log.1" "$log_dir/git-commands.log" "$log_dir/git-commands.log.1" 2>/dev/null || true + +# Remove debug files older than 7 days +find ~/.claude/debug -name "*.txt" -mtime +7 -delete 2>/dev/null || true + +# Remove stale shell snapshots older than 7 days +find ~/.claude/shell-snapshots -type f -mtime +7 -delete 2>/dev/null || true + +exit 0 diff --git a/settings.json b/settings.json index bb3b30b..08aaf50 100644 --- a/settings.json +++ b/settings.json @@ -284,6 +284,15 @@ } ], "SessionEnd": [ + { + "hooks": [ + { + "type": "command", + "command": "~/.claude/hooks/session-cleanup.sh", + "timeout": 10 + } + ] + }, { "hooks": [ {