You are helping the user configure session plugin hooks in their project's `.claude/settings.json` file. ## Task: Setup Session Plugin Hooks This command manages the automatic configuration of session plugin hooks that enable auto-tracking of sessions, user interactions, and file modifications. **IMPORTANT:** This command uses the session CLI tool to perform hook management operations safely with atomic writes and backups. ### Parse Command Arguments Extract the operation from the command arguments. Format: `/session:setup [options]` **Supported options:** - No arguments: Install/configure hooks (default) - `--permissions`: Install hooks + permission bypasses (eliminates ALL prompts) - `--remove`: Remove hooks from settings.json - `--status`: Show current hook and permission configuration - `--force-cleanup`: Force cleanup of orphaned hooks - `--dry-run`: Preview changes without applying them ### Step 1: Determine Operation Mode 1. Check arguments to determine which operation to perform: - **Install mode** (default/no args): Add hooks to settings.json - **Remove mode** (`--remove`): Remove hooks from settings.json - **Status mode** (`--status`): Show current configuration - **Cleanup mode** (`--force-cleanup`): Clean up orphaned hooks - **Dry run** (`--dry-run`): Can be combined with other modes to preview ### Step 2: Execute CLI Command Run the appropriate CLI command based on the operation mode: **For Install (hooks only):** ```bash node ${CLAUDE_PLUGIN_ROOT}/cli/session-cli.js setup-hooks ``` **For Install (hooks + permissions):** ```bash node ${CLAUDE_PLUGIN_ROOT}/cli/session-cli.js setup-hooks --permissions ``` **For Remove:** ```bash node ${CLAUDE_PLUGIN_ROOT}/cli/session-cli.js setup-hooks --remove ``` **For Status:** ```bash node ${CLAUDE_PLUGIN_ROOT}/cli/session-cli.js setup-hooks --status ``` **For Force Cleanup:** ```bash node ${CLAUDE_PLUGIN_ROOT}/cli/session-cli.js setup-hooks --force-cleanup ``` **For Dry Run (example with install):** ```bash node ${CLAUDE_PLUGIN_ROOT}/cli/session-cli.js setup-hooks --dry-run ``` The CLI will return JSON with the operation result. ### Step 3: Display Results Parse the JSON response and display user-friendly output based on the action: #### Install Success (action: "installed") **If permissions were added (permissionsRequested: true, permissionsCount > 0):** ``` โœ… Session plugin configured successfully! ๐Ÿ“‹ Hooks added to .claude/settings.json: โœ“ SessionStart - Auto-clears session on /clear โœ“ SessionEnd - Cleanup on Claude Code exit โœ“ UserPromptSubmit - Tracks user interactions โœ“ PostToolUse (Write, Edit, NotebookEdit) - Tracks file changes โœ“ Stop - Captures Claude's responses for self-contained logs ๐Ÿ”“ Permissions added ({permissionsCount} entries): โœ“ Read(.claude/sessions/**) โœ“ Bash(git log --oneline:*) โœ“ Bash(git status --porcelain:*) โœ“ Bash(git diff --stat:*) โœ“ Bash(git branch -vv:*) โœ“ Bash(git rev-parse --abbrev-ref:*) โšก Result: ZERO permission prompts during: โ€ข /session:continue (no Read prompts, no git prompts) โ€ข /session:save (no Read prompts) โ€ข /session:status (no Read prompts) โ€ข All session operations run silently ๐Ÿ’พ Backup saved: {backupPath} ๐Ÿ“ Settings: {settingsPath} โš ๏ธ IMPORTANT: Restart Claude Code for changes to take effect! Hooks and permissions are loaded at startup. ๐Ÿ’ก To disable temporarily: Set "disableAllHooks": true in settings.json ๐Ÿ’ก To remove these hooks: /session:setup --remove ``` **If permissions were NOT added (permissionsRequested: false):** ``` โœ… Session plugin hooks configured successfully! ๐Ÿ“‹ Hooks added to .claude/settings.json: โœ“ SessionStart - Auto-clears session on /clear โœ“ SessionEnd - Cleanup on Claude Code exit โœ“ UserPromptSubmit - Tracks user interactions โœ“ PostToolUse (Write, Edit, NotebookEdit) - Tracks file changes โœ“ Stop - Captures Claude's responses for self-contained logs ๐ŸŽฏ Sessions now automatically track: โ€ข Session state changes โ€ข User prompts and interactions โ€ข Claude's full responses โ€ข File modifications ๐Ÿ’ก TIP: Eliminate ALL permission prompts during session operations! Run: /session:setup --permissions This adds safe read-only permissions for: โ€ข .claude/sessions/** files (Read access) โ€ข git commands (log, status, diff, branch) Result: Zero prompts during /session:continue โšก ๐Ÿ’พ Backup saved: {backupPath} ๐Ÿ“ Settings: {settingsPath} โš ๏ธ IMPORTANT: Restart Claude Code for hooks to take effect! Hooks are loaded at startup and won't activate until you restart. ๐Ÿ’ก To disable temporarily: Set "disableAllHooks": true in settings.json ๐Ÿ’ก To remove these hooks: /session:setup --remove ``` #### Already Configured (action: "already_configured") ``` โ„น๏ธ Session plugin hooks are already configured! โœ“ All session plugin hooks are properly configured in settings.json Current hooks: โœ“ SessionStart - Active โœ“ UserPromptSubmit - Active โœ“ PostToolUse - Active โœ“ Stop - Active ๐Ÿ’ก To reinstall: /session:setup --remove, then /session:setup ๐Ÿ’ก To view details: /session:setup --status ``` #### Remove Success (action: "removed") ``` โœ… Session plugin hooks removed! ๐Ÿ“‹ Removed from .claude/settings.json: โœ“ SessionStart โœ“ UserPromptSubmit โœ“ PostToolUse โœ“ Stop ๐Ÿ’พ Backup saved: {backupPath} โš ๏ธ IMPORTANT: Restart Claude Code to stop hooks! Active hooks will continue running until you restart. ๐Ÿ’ก Sessions still work, but automatic tracking is disabled ๐Ÿ’ก To re-enable: /session:setup ``` #### Not Configured (action: "not_configured") ``` โ„น๏ธ No session plugin hooks found Session plugin hooks are not currently configured in settings.json ๐Ÿ’ก To install hooks: /session:setup ๐Ÿ’ก This will enable automatic session tracking ``` #### Status Display (action: "status") **When properly configured:** ``` โœ… Session Plugin Status ๐Ÿ“‹ Hooks configured in .claude/settings.json: โœ“ SessionStart โ†’ node ${CLAUDE_PLUGIN_ROOT}/hooks/session-start.js โœ“ SessionEnd โ†’ node ${CLAUDE_PLUGIN_ROOT}/hooks/session-end.js โœ“ UserPromptSubmit โ†’ node ${CLAUDE_PLUGIN_ROOT}/hooks/user-prompt-submit.js โœ“ PostToolUse (3 matchers) โ†’ Write: node ${CLAUDE_PLUGIN_ROOT}/hooks/post-tool-use.js โ†’ Edit: node ${CLAUDE_PLUGIN_ROOT}/hooks/post-tool-use.js โ†’ NotebookEdit: node ${CLAUDE_PLUGIN_ROOT}/hooks/post-tool-use.js โœ“ Stop โ†’ node ${CLAUDE_PLUGIN_ROOT}/hooks/stop.js ๐Ÿ“Š Hooks: {configuredHookTypes}/{totalHookTypes} hook types configured โœ… All hooks pointing to valid scripts ๐Ÿ”“ Permissions configured: {permissionsCount}/{totalPermissions} {if permissionsConfigured === true} โœ… All session permissions configured โ€ข Read(.claude/sessions/**) โ€ข Bash(git log --oneline:*) โ€ข Bash(git status --porcelain:*) โ€ข Bash(git diff --stat:*) โ€ข Bash(git branch -vv:*) โ€ข Bash(git rev-parse --abbrev-ref:*) โšก Result: Zero prompts during session operations {else if permissionsCount > 0 && permissionsCount < totalPermissions} โš ๏ธ Partial permissions configured ({permissionsCount}/{totalPermissions}) ๐Ÿ’ก To add missing permissions: /session:setup --permissions {else} โš ๏ธ No session permissions configured ๐Ÿ’ก To eliminate permission prompts: /session:setup --permissions This adds safe read-only permissions for session files and git commands {end if} ๐ŸŽฏ Plugin Status: Installed & Configured ๐Ÿ’ก To remove: /session:setup --remove ``` **When orphaned (plugin uninstalled but hooks remain):** ``` โš ๏ธ Session Plugin Hooks Status ๐Ÿ“‹ Found in .claude/settings.json: โš ๏ธ SessionStart โ†’ node ${CLAUDE_PLUGIN_ROOT}/hooks/session-start.js โŒ Script not found (plugin may be uninstalled) โš ๏ธ UserPromptSubmit โŒ Script not found โš ๏ธ PostToolUse (3 matchers) โŒ All scripts not found โš ๏ธ Stop โŒ Script not found ๐Ÿ” Plugin Status: Not Found โŒ Hooks are orphaned (pointing to missing files) Found {orphanedHooks.length} orphaned hook entries ๐Ÿงน Cleanup Options: 1. Auto-cleanup: /session:setup --force-cleanup 2. Manual cleanup: Edit .claude/settings.json 3. Reinstall plugin to restore functionality ``` #### Cleanup Success (action: "cleaned") ``` โœ… Orphaned hooks cleaned up! ๐Ÿงน Removed {removedCount} orphaned hook entries: {List each orphaned hook that was removed} ๐Ÿ’พ Backup saved: {backupPath} โš ๏ธ IMPORTANT: Restart Claude Code to apply cleanup! Settings changes take effect on next restart. ๐Ÿ’ก Settings.json has been cleaned ๐Ÿ’ก To reinstall hooks: /session:setup ``` #### No Orphans (action: "no_orphans") ``` โœ… No orphaned hooks found All hooks in settings.json are valid ๐Ÿ’ก Everything looks good! ``` #### Error Cases **On error (success: false):** ``` โŒ Error: {error message} {Provide helpful context based on the error:} Common issues: - **Malformed JSON**: settings.json has syntax errors โ†’ Fix: Restore from backup or manually repair JSON - **Permission denied**: No write access to .claude/settings.json โ†’ Fix: Check file permissions - **Plugin not found**: Session plugin may not be installed โ†’ Fix: Reinstall the session plugin ๐Ÿ’ก To restore from backup: cp .claude/settings.json.backup .claude/settings.json ๐Ÿ’ก Need help? Check the README or /session:setup --status ``` ### Important Notes - **Atomic Operations**: All writes use atomic operations with backups - **Idempotent**: Safe to run multiple times, won't create duplicates - **Portable Paths**: Uses `${CLAUDE_PLUGIN_ROOT}` variable for cross-platform compatibility - **Merge Strategy**: Preserves existing hooks when installing - **Safety**: Creates `.backup` file before any modifications ### Usage Examples ```bash # Install hooks only (basic setup) /session:setup # Install hooks + permissions (RECOMMENDED - eliminates ALL prompts) /session:setup --permissions # Check current status (shows hooks and permissions) /session:setup --status # Preview what would be installed (dry run) /session:setup --dry-run # Preview with permissions /session:setup --permissions --dry-run # Remove hooks before uninstalling plugin /session:setup --remove # Clean up orphaned hooks after plugin uninstall /session:setup --force-cleanup ``` **Recommended:** Always use `--permissions` flag for best experience: - Zero permission prompts during `/session:continue` - Zero permission prompts during `/session:save` - Zero permission prompts during `/session:status` - Completely silent session operations --- **CRITICAL for Plugin Uninstallation:** Before uninstalling the session plugin, users should run: ```bash /session:setup --remove ``` This ensures clean removal and prevents orphaned hook entries in settings.json.