diff options
| author | Daisy Hollman <daisy@anthropic.com> | 2026-01-09 22:48:26 +0000 |
|---|---|---|
| committer | Daisy Hollman <daisy@anthropic.com> | 2026-01-09 22:48:26 +0000 |
| commit | 95380b3cd58f7fbf575ebda0fe291224ba028317 (patch) | |
| tree | 36532bcaf5fdea7785ca3fae94214bf00356d2b7 /plugins/caffeinate/hooks | |
| parent | f1be96f0fb58d5aaf2840ca7d7036d5c0923742c (diff) | |
Add caffeinate plugin to prevent blocking sleep commandsdaisy/caffeinate/main
This plugin intercepts sleep commands in Bash and prompts Claude to use
background execution (run_in_background: true) instead. This keeps the
session responsive during long-running operations.
Features:
- Blocks sleep commands with helpful retry message
- Allows sleep in quoted strings (false positive prevention)
- Catches sleep in compound commands (&&, ||, ;) and loops
- Allows background commands through (run_in_background: true)
:house: Remote-Dev: homespace
Claude-Generated-By: Claude Code (cli/claude-opus-4-5=100%)
Claude-Steers: 6
Claude-Permission-Prompts: 0
Claude-Escapes: 0
Diffstat (limited to 'plugins/caffeinate/hooks')
| -rw-r--r-- | plugins/caffeinate/hooks/hooks.json | 17 | ||||
| -rwxr-xr-x | plugins/caffeinate/hooks/scripts/check-sleep.sh | 61 |
2 files changed, 78 insertions, 0 deletions
diff --git a/plugins/caffeinate/hooks/hooks.json b/plugins/caffeinate/hooks/hooks.json new file mode 100644 index 00000000..e4418276 --- /dev/null +++ b/plugins/caffeinate/hooks/hooks.json @@ -0,0 +1,17 @@ +{ + "description": "Blocks sleep commands and reminds Claude to use background tasks", + "hooks": { + "PreToolUse": [ + { + "matcher": "Bash", + "hooks": [ + { + "type": "command", + "command": "bash ${CLAUDE_PLUGIN_ROOT}/hooks/scripts/check-sleep.sh", + "timeout": 5 + } + ] + } + ] + } +} diff --git a/plugins/caffeinate/hooks/scripts/check-sleep.sh b/plugins/caffeinate/hooks/scripts/check-sleep.sh new file mode 100755 index 00000000..18113be6 --- /dev/null +++ b/plugins/caffeinate/hooks/scripts/check-sleep.sh @@ -0,0 +1,61 @@ +#!/bin/bash +# Caffeinate hook: Blocks sleep commands and suggests background tasks +set -euo pipefail + +input=$(cat) +command=$(echo "$input" | jq -r '.tool_input.command // empty') + +if [[ -z "$command" ]]; then + exit 0 +fi + +# Allow sleep if run_in_background is true - that's the whole point! +run_in_bg=$(echo "$input" | jq -r '.tool_input.run_in_background // false') +if [[ "$run_in_bg" == "true" ]]; then + exit 0 +fi + +block_message='ACTION REQUIRED: Your sleep command was blocked. You MUST now call Bash again with the SAME command but add the parameter run_in_background: true. Do this immediately in your next tool call. Do not explain or apologize - just retry with run_in_background: true.' + +output_block() { + # Output JSON to stdout for blocking decision + # Output message to stderr to feed back to Claude + cat << EOF +{ + "hookSpecificOutput": { + "hookEventName": "PreToolUse", + "permissionDecision": "deny", + "permissionDecisionReason": "$block_message" + } +} +EOF + exit 0 +} + +# Remove all quoted strings to avoid false positives +# Replace single and double quoted strings with placeholder +cmd_no_quotes=$(echo "$command" | sed -E "s/\"[^\"]*\"//g; s/'[^']*'//g") + +# Check for sleep command patterns in the de-quoted command +# Pattern 1: Command starts with sleep +if [[ "$cmd_no_quotes" =~ (^|[[:space:]])sleep[[:space:]]+[0-9] ]]; then + output_block +fi + +# Pattern 2: sleep after any common separator (&&, ||, ;, |, do, then) +if [[ "$cmd_no_quotes" =~ (\&\&|;\||[[:space:]]do[[:space:]]|[[:space:]]then[[:space:]])[[:space:]]*sleep[[:space:]] ]]; then + output_block +fi + +# Pattern 3: Simple contains check - if "sleep " followed by number appears anywhere +if [[ "$cmd_no_quotes" =~ sleep[[:space:]]+[0-9] ]]; then + output_block +fi + +# Pattern 4: sleep with variable like $TIMEOUT or ${DELAY} +if [[ "$cmd_no_quotes" =~ sleep[[:space:]]+\$ ]]; then + output_block +fi + +# No sleep command detected - allow +exit 0 |
