SABRAMAN

SABRAMAN DANYA YUDIN КАРТОН SABRAMAN DANYA YUDIN КАРТОН SABRAMAN DANYA YUDIN КАРТОН SABRAMAN DANYA YUDIN КАРТОН SABRAMAN DANYA YUDIN КАРТОН SABRAMAN DANYA YUDIN КАРТОН
macOS Sleep Utility
Case Study

SleepOff

A lightweight macOS Swift menu bar utility that prevents closed-lid sleep and dims the built-in display, auto-triggered by active AI coding agents.

Problem and context

macOS puts MacBooks to sleep when the lid is closed unless connected to an external display. For users running long-running local AI development agents (like Antigravity or Codex), this stops active work as soon as the laptop is closed.

Existing utilities require manual toggles and keep the built-in screen active when closed, wasting energy and causing heat issues inside the closed laptop.

The goal was to build a lightweight Swift menu bar app that detects active AI agents, dynamically blocks sleep, and forces screen brightness to zero only while the lid is closed.

Architecture and implementation

Built as a native Swift macOS App. It reads clamshell state from `AppleClamshellState` via IOKit and monitors running processes to identify active AI agents.

When active, sleep is blocked using `pmset -a disablesleep`. To avoid repeated password prompts, a helper script installs a narrow one-time `sudoers` privilege rule for `pmset` commands.

Display brightness is managed using CoreDisplay APIs, forcing brightness to 0 on lid close and restoring the original level when the laptop opens.

  • Native Swift menu bar implementation
  • CoreDisplay brightness level overrides
  • Narrow sudoers privilege rules for pmset

Feature deep dive

The app cycles through three modes: Off (allows sleep), On (always blocks sleep), and Auto AI (blocks sleep only when supported AI agents are running).

Supports active monitoring of Antigravity IDE and Codex CLI agents, dynamically changing the menu bar icon from a single sparkle to multiple sparkles during generation.

Includes custom keyboard shortcuts (defaults to Control + Command + S), LaunchAgent integration for autostart, and a lightweight Nix Flake package.

Tooling and integrations

Written in Swift without external frameworks to keep resource usage minimal. Nix Flake and Homebrew Cask configurations handle deployment.

System calls interact with AppleClamshellState and use IOPSNotificationCreateRunLoopSource to react to power adapter changes.

A custom installation script configures the launch plist and configures sudoers permissions for secure, quiet operations.

Outcome and impact

MacBooks can compile, run tests, and process AI prompts with closed lids, avoiding thermal throttling and saving energy by forcing the screen off.

No more manual terminal commands: the application starts on boot and toggles sleep settings in the background depending on active coding tasks.

Developers running local LLMs and agentic helpers can close their laptops and continue coding without losing terminal sessions.

Constraints and tradeoffs

Modifying `pmset disablesleep` requires sudo permissions, which necessitates a one-time administrative password during setup.

Forcing brightness to 0 is done programmatically; if the app crashes while closed, the original brightness must be manually restored on open.

Process detection monitors agent binary names, which means renamed executables will not trigger the Auto AI mode.

FAQ

How does the app detect AI agents?

It scans running process identifiers (PIDs) for matching executable names like Antigravity or Codex CLI helper processes.

Is it safe for the MacBook to run closed?

Yes, because the display is turned off to prevent screen heating, and the CPU is allowed normal thermal control by macOS.

How do I install the pmset privilege helper?

The app provides a setup button or command that adds the narrow sudoers rule for the pmset binary path.

INTERESTED IN WORKING TOGETHER?

Let's build high-performance web platforms, custom Telegram Mini Apps, or premium design systems for your business.