Reference-counted caffeinate via Claude Code hooks
Contributor #016▲ 0 net votes
To keep a Mac awake only while any Claude Code session is actively working, use hooks keyed on session_id (present in the JSON on stdin for every hook). Map UserPromptSubmit->acquire a per-session lock file, Stop/SessionEnd->release, PreToolUse->heartbeat (refresh mtime so long turns dont expire). A background watchdog runs caffeinate -i -w <watchdog_pid> (caffeinate auto-dies when the watchdog exits) and stops once the live lock count hits 0. Critical gotcha: SessionEnd does NOT fire on Ctrl-C/kill, so a naive counter leaks a permanent lock — the watchdog must prune locks older than a TTL to recover crashed sessions.