feat: scope hook logs to per-session directories#209
Conversation
Derive session directory from $CLAUDE_SESSION_ID (last 8 chars) so each session writes to its own logs/<session-id>/ directory instead of shared flat files. Replace size-based log rotation with 7-day age-based cleanup of session directories, consistent with debug and shell-snapshot retention. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
@claude please look at this one |
|
Claude finished @philoserf's task in 1m 10s —— View job PR Review: feat: scope hook logs to per-session directories
Overall this is clean, well-scoped work. The approach is consistent with the existing debug/shell-snapshot retention pattern. A few observations: hooks/log-hook-event.sh & hooks/log-git-commands.shFallback to session_id="${CLAUDE_SESSION_ID: -8}"
session_id="${session_id:-default}"This means logs from all unidentified invocations accumulate in a single 8-char suffix collision: Truncating to 8 chars is practically fine (4.3B combinations), but worth a comment explaining the tradeoff if anyone ever wonders why it's 8 and not the full ID.
hooks/session-cleanup.shThe script is straightforward and well-guarded. A couple notes:
Legacy file cleanup is one-time: The settings.json
README.mdThe updated examples are accurate. The SummaryNo blocking issues. The main practical consideration is the |
CLAUDE_SESSION_ID is not exposed as an environment variable to hooks. The session_id is available in the JSON payload on stdin. Read stdin first, then extract session_id from the parsed JSON. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR scopes hook-related logs to per-session directories under ~/.claude/logs/<session-id>/ (based on the last 8 chars of $CLAUDE_SESSION_ID) and introduces a SessionEnd cleanup hook that removes old session log directories and legacy flat log files.
Changes:
- Write
hook-events.logandgit-commands.loginto~/.claude/logs/<session-id>/instead of a single flatlogs/location. - Add
session-cleanup.shand register it as aSessionEndhook to remove logs/debug/snapshots older than 7 days and delete legacy flat log files. - Update README wording/examples to reflect per-session log layout.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
settings.json |
Registers session-cleanup.sh to run on SessionEnd. |
hooks/session-cleanup.sh |
New cleanup script for old session log directories and legacy flat logs (plus debug/snapshot retention). |
hooks/log-hook-event.sh |
Routes hook event logs into a per-session directory. |
hooks/log-git-commands.sh |
Routes git command logs into a per-session directory. |
README.md |
Updates docs to describe per-session logs and new cleanup hook. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@claude We added some changes. Look again. |
|
Claude finished @philoserf's task in 1m 40s —— View job Re-review: feat/session-scoped-hook-logs
Good progress. Here's what changed and what remains. What's new in this revision
Session ID source: Both scripts now extract Legacy cleanup: Remaining items
rm -f "$log_dir/hook-events.log" "$log_dir/hook-events.log.1" "$log_dir/git-commands.log"
It no longer logs to stderr — it writes to Header comment misleads ( # SessionEnd cleanup - rotate logs and archive stale session dataThe script deletes, not rotates or archives. Still unaddressed from the Copilot review. Fix this → No session_id sanitization (both logging scripts): session_id="${session_id//[^a-zA-Z0-9]/_}"Up to you whether the defense-in-depth is worth it for an internal tool.
Hook countCounted 11 hooks listed in the README (validate-config, validate-bash-commands, protect-secrets, log-git-commands, log-hook-event, auto-format, session-start, session-end, load-session-context, evaluate-session, session-cleanup). Count is correct — SummaryThe two substantive issues worth fixing before merge: the stale |
- Update session-cleanup.sh header comment to match actual behavior - Add git-commands.log.1 to legacy file cleanup - Fix README log-git-commands.sh description (no longer logs to stderr) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Strip non-alphanumeric characters from session_id to prevent path traversal. Add -- to rm -rf in find -exec to handle directory names starting with dashes. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary
hook-events.log,git-commands.log) now write to~/.claude/logs/<session-id>/using the last 8 chars of$CLAUDE_SESSION_IDsession-cleanup.shas a SessionEnd hook insettings.jsonTest plan
~/.claude/logs/<session-id>/$CLAUDE_SESSION_IDlast 8 chars match the directory namelogs/are cleaned up after session ends🤖 Generated with Claude Code