The short version
Battery and thermal limits, plus a watchdog that puts sleep back even if the app crashes.
- Swift targets
- 4
- launchd jobs
- 2
- Lease
- 16 s
What it is
LidGuard is a native macOS menu bar app that keeps a MacBook awake with the lid closed, for a build, a download or a server you don’t want cut off when you shut the laptop. The usual tools, caffeinate and the apps built on it, only hold off idle sleep, and that ends when the lid closes.
Turning sleep off is the easy part. The hard part is making sure it always comes back on.
What I built
I designed and built it end to end, from the power monitoring to the menu bar.
- Timed sessions: start one from the menu bar or a global shortcut, pick a length, and LidGuard ends it on time.
- Lock & Keep Awake: locks the Mac with the native lock screen, checks the lock actually took, then keeps it awake.
- Battery limits: by default a session needs stable external power to start, and stops at a 30% reserve or after using 10 percentage points in one outage, whichever comes first. Limits on total use and time on battery are optional.
- Thermal stop: if macOS reports a serious thermal state, LidGuard turns sleep back on and puts the Mac to sleep.
- Diagnostics and history: watchdog status, the live sleep setting read back from the system, and the last 500 sessions, all stored on the Mac.
- Simulation: try every limit with simulated power and thermal readings, without touching system settings.
How it works
A Swift 6 package in four targets: a policy core with no system access, a layer over IOKit and the power APIs, the SwiftUI menu bar app, and a privileged daemon.
The daemon runs as two separate launchd jobs. The helper writes a root-owned journal of what it’s about to change before it disables sleep, then reads the setting back to confirm it. The app renews a lease about every four seconds; if renewals stop for 16 seconds, because the app crashed or the user logged out, the session is over. The watchdog checks the journal every three seconds and restores the original settings after an expired lease, a reboot or a stuck helper. Neither process ever resumes an old session.
The app and the daemon talk over authenticated XPC. The daemon only takes requests from the user signed in at the console, and only restores the settings it changed.
Where it stands
Version 0.1, running on Apple Silicon. Closed-lid behavior varies by model and macOS release, so it ships with a hardware checklist and a safety guide. The code is on GitHub.
Next project →
ContribuX