lidwake keeps a MacBook awake with the lid closed while Claude Code work is active. When the work stops, lidwake restores normal sleep.
Every 30 seconds, lidwake reads these inputs:
- The force file
~/.lidwake/force(lidwake oncreates it,lidwake offremoves it). - The herdr socket
~/.config/herdr/herdr.sock. One or more agents with statusworkingcount as active. - Open Claude Code task output files
/private/tmp/claude-<uid>/**/tasks/*.output. A running background task keeps its file open. - The internal battery.
If one input is active, lidwake runs sudo -n /usr/bin/pmset -a disablesleep 1.
It also holds a PreventUserIdleSystemSleep power assertion.
After the work stops, lidwake waits 5 minutes. Then it runs pmset -a disablesleep 0.
On battery power below 20 % charge, lidwake always releases the hold.
lidwake writes ~/.lidwake/journal.json before it sets the flag.
If lidwake stops unexpectedly, the next run reads the journal and keeps ownership of the flag.
It clears the flag at the next idle check, and it keeps the ownership until pmset -a disablesleep 0 succeeds.
lidwake records each pmset child in the journal before the child runs. While a recorded child is still running, lidwake does not change the flag. A recorded child that is stopped never ran pmset, so lidwake kills it.
If the flag was already 1 before lidwake needed it, lidwake does not touch it.
If journal.json cannot be read, lidwake keeps a copy (journal.json.corrupt-*) and treats the flag as its own, so the idle and low-battery restore still work.
At start, run and restore delete leftover journal.json.tmp-* files from an interrupted save.
If the clock moves back, a last-active time in the future counts as now, so the 5-minute wait does not grow.
-
Build the binary:
swift build -c release sudo install -m 0755 .build/release/lidwake /usr/local/bin/lidwake
-
Install the sudoers rule. The rule allows only the two
pmsetcommands:sed "s/USERNAME/$(id -un)/" support/sudoers-lidwake > /tmp/sudoers-lidwake sudo visudo -cf /tmp/sudoers-lidwake sudo install -m 0440 -o root -g wheel /tmp/sudoers-lidwake /etc/sudoers.d/lidwake rm /tmp/sudoers-lidwake
-
Test without changes to the system:
lidwake status lidwake run --dry-run --ticks 3 --interval 1
-
Start the LaunchAgent:
cp support/com.lidwake.agent.plist ~/Library/LaunchAgents/ launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.lidwake.agent.plist
| command | action |
|---|---|
lidwake run [--dry-run] [--ticks N] [--interval S] |
Run the control loop. --dry-run prints the pmset commands and does not run them. |
lidwake status |
Show the inputs, the SleepDisabled flag, the journal, and the decision. |
lidwake on / lidwake off |
Force the hold on, or remove the force. |
lidwake restore |
Run pmset -a disablesleep 0 if lidwake owns the flag. If the command fails, lidwake keeps the ownership and exits with status 1. restore kills a recorded child that is stopped. If the child has not ended yet, restore changes nothing and exits with status 75; run it again. |
Set LIDWAKE_STATE_DIR to use a state directory other than ~/.lidwake.
Logs go to the unified log: log stream --predicate 'subsystem == "dev.lidwake"'.
launchctl bootout gui/$(id -u)/com.lidwake.agent
lidwake restore
rm ~/Library/LaunchAgents/com.lidwake.agent.plist
sudo rm /etc/sudoers.d/lidwake /usr/local/bin/lidwake
rm -rf ~/.lidwake
pmset -g | grep SleepDisabled # expect 0- A closed MacBook in a bag can overheat while it stays awake. Do not put the Mac in a bag while lidwake holds sleep.
SleepDisabledis a system-wide setting, and it persists until a process clears it. If lidwake getsSIGKILLor the Mac loses power, the flag can stay set until lidwake starts again. Runpmset -g | grep SleepDisabledto check. Runlidwake restoreto clear it; if the journal is gone, runsudo pmset -a disablesleep 0.- lidwake cannot tell its own
SleepDisabled=1from apmset -a disablesleep 1that you run while lidwake owns the flag. In that case, lidwake clears the flag when the work stops. - If
journal.jsonbecomes unreadable, lidwake treats the flag as its own. Apmset -a disablesleep 1that you set yourself can then be cleared once.lidwake statusshowsjournal: unreadablein that state, and after recovery akept unreadable journals:line that names the kept copies. - Run one non-dry
lidwake runper Mac. The lock is per state directory, butSleepDisabledis one machine-wide flag. - The sudoers rule lets every process of your user toggle
SleepDisabledwithout a password. - lidwake reads the battery state only every 30 seconds. The charge can drop below 20 % before the next check.
swift test: 185 tests (Swift Testing), written test-first fromdocs/SPEC.mdby a separate author from the implementation. They cover the decision rules, the herdr client (timeouts, size cap, no fd leaks), the libproc task scanner, the journal, the controller's crash-recovery rules with fakes, real child processes for the orphan guard, and the CLI as a subprocess. No test callssudoor changesSleepDisabled.tla/: a TLA+ model of the controller protocol, checked with TLC (tla/check.sh). Crashes may happen between any two steps;pmsetmay fail; the user may change the flag. Six properties hold (never undo a user setting, no unowned hold after a crash, sleep eventually restored, no unwanted hold in the end, assertion follows hold, type safety), and ten deliberately broken variants are each caught. A second group letsjournal.jsonbecome unreadable: no unowned hold and "no unwanted hold in the end" still hold, and the old rule (load as not owned) is caught. Seetla/README.md..claude/skills/verify-lidwake/: how to drive the built CLI on isolated state (doctor, feature map, journeys) without touching the installed agent.leaks --atExit -- lidwake run --dry-run --ticks 20 --interval 0.05reports 0 leaks.
MIT