# Your agent didn't fail. You closed the lid

> Source: <https://dev.to/dmitryganin/your-agent-didnt-fail-you-closed-the-lid-g8o>
> Published: 2026-08-28 22:33:41+00:00

SleepSwitch is a macOS menu bar toggle that stops your Mac from sleeping — lid closed included. One click, no password prompts, and a battery guard so you don't cook your laptop in a bag.

You kick off a long agent run — Claude Code refactoring half a service, Codex grinding through a test suite, some pipeline that's going to take 40 minutes. You close the lid and walk away.

You come back to a session that died somewhere in the middle. Half the edits applied. The agent's context gone. Whatever it was in the middle of writing, gone with it.

Your Mac went to sleep. That's it. That's the whole bug.

`caffeinate`

doesn't save you
The standard answer is `caffeinate -dimsu`

. It works — right up until you close the lid, at which point the Mac sleeps anyway.

Lid-close sleep is a **separate setting**, it lives behind `sudo`

, and no user-level assertion touches it:

```
sudo pmset -a disablesleep 1   # the one that actually matters
```

So the real workflow becomes: remember the flag, type your password, remember to turn it back off later, and — the fun part — forget once, drop the laptop in a bag, and it runs hot in there until the battery is flat.

I got tired of that loop and wrote ** SleepSwitch**. One menu bar icon, one click, both layers at once.

| Icon | Meaning |
|---|---|
| 🌙 | Normal — your Mac sleeps as configured |
| ☕️ | Mode on — sleep fully blocked, lid included |
| ⚠️ | Partial — idle sleep blocked, but the lid still sleeps |

Left click toggles. Right click is the menu — launch behaviour, login item, updates, the `sudo`

rule.

Close the lid with the mode on and you get a muted porcelain tone; open it and a higher one. Enough to confirm the machine stayed awake without opening anything to check.

This started as a "keep the build running" utility. Then AI coding agents happened, and it turned out they're the worst possible workload for macOS power management:

So: hit the switch, close the lid, come back to a finished run. Same for `npm run build`

on a monorepo, a long download, a render, an SSH session you need to survive lunch.

| Layer | Blocks | Needs root |
|---|---|---|
`pmset -a disablesleep 1` |
Lid-close sleep, and all sleep | Yes |
`PreventUserIdleSystemSleep` assertion |
Idle sleep | No |
`PreventUserIdleDisplaySleep` assertion |
Display turning off | No |

The IOKit assertions are held by the process, so they evaporate the moment the app dies — they can't get stuck. The `pmset`

setting is different: it's a system setting that outlives the process. So the app clears it on quit, on `SIGTERM`

, and reports the real state at launch by reading `SleepDisabled`

straight from `IOPMrootDomain`

rather than trusting its own memory of what it did.

`pmset disablesleep`

needs root, and prompting on every toggle makes the whole thing pointless. So the installer writes `/etc/sudoers.d/sleepswitch`

:

```
you ALL=(root) NOPASSWD: /usr/bin/pmset -a disablesleep 0
you ALL=(root) NOPASSWD: /usr/bin/pmset -a disablesleep 1
```

Two commands, no wildcards. `sudo`

matches arguments exactly, so this grants "toggle one sleep setting" and nothing else — `pmset -a sleep 0`

still asks for a password.

The rule is written, validated with `visudo`

and installed entirely as root inside a mode-`0700`

temp directory, so there's no user-writable file to swap in between the check and the install. The username is validated before it reaches `sudoers`

, and only an account in the `admin`

group gets it.

Don't want it? Untick the box at install time, remove it later from the menu, or:

```
sudo rm /etc/sudoers.d/sleepswitch
```

The failure mode I mentioned above is real and it's the reason this feature exists: mode on, lid closed, laptop in a bag, cooking.

So the mode switches itself off once the battery drops to a threshold you pick — 20% out of the box — and, optionally, the instant the power adapter is unplugged. A notification tells you which of the two fired.

Two small decisions I'd defend:

Grab the `.pkg`

from the [latest release](https://github.com/AppsGanin/SleepSwitch/releases/latest) and open it.

It's not signed with an Apple Developer certificate, so macOS blocks the first open. On Sequoia and newer, Control-click no longer overrides that: open the `.pkg`

, let it be refused, then **System Settings → Privacy & Security → Open Anyway**. One time.

Or from source:

```
./make-installer.sh     # → dist/SleepSwitch-<version>.pkg
./install.sh            # or straight into /Applications
```

Command Line Tools is the only requirement. The icon is drawn in code at build time, the lid tones are synthesised at build time — no binary assets in the repo. A missing translation fails the build.

**One warning worth repeating:** turn the mode off *before* deleting the app. The `pmset`

setting is a system setting; dragging the app to the Trash while the mode is on leaves you with a Mac that won't sleep and no switch left to turn it off. The **Uninstall SleepSwitch…** menu item handles the ordering for you.

`.pkg`

and hands it to the system Installer. Requests pinned to `https`

on GitHub hosts, redirects included. Turn the check off and nothing goes over the network.** github.com/AppsGanin/SleepSwitch** — stars are appreciated, issues more so. ☕️
