Show HN:Adrafinil —— 让合上盖子的 Mac 仅在程序运行时保持唤醒
Show HN: Adrafinil – keep a lid-closed Mac awake only while agents work

原始链接: https://github.com/kageroumado/adrafinil

Adrafinil 是一款 macOS 工具,它仅在 AI 编程助手(如 Claude Code、Cursor 或 Aider)处于活跃工作状态时,防止您的电脑进入睡眠模式(包括合上盖子的“翻盖”模式)。 与 Amphetamine 或 caffeinate 等传统的“始终唤醒”类软件不同,Adrafinil 仅在助手执行任务时进行干预。它采用引用计数系统,确保 Mac 仅在任务进行期间保持唤醒。一旦助手完成工作或进入闲置状态,系统便会恢复正常的睡眠行为。 **主要功能包括:** * **安全性:** 内置过热保护机制,如果合盖状态下 CPU 温度过高,会自动解除睡眠限制。 * **高效性:** 具备响应时间低于 50 毫秒的命令行界面(CLI),可集成至助手钩子(hooks)中,确保工作流零延迟。 * **安全性:** 使用精简且经过审计的特权助手来管理睡眠设置,将系统级更改与策略逻辑相隔离。 * **实用性:** 提供“开盖摘要”功能,可报告您离开期间助手完成的工作内容,以及期间的峰值温度。 Adrafinil 专为开发者设计,确保您的机器仅在工作时保持唤醒,并在您休息时随之进入睡眠。

**Adrafinil** 是一款全新的开源 macOS 实用工具,旨在解决 AI 代理在合盖时强制 MacBook 进入睡眠状态的常见痛点。 尽管目前已有如 *Amphetamine* 或硬件“虚拟显示器插头”等解决方案,但它们往往会导致电池电量耗尽,或无法识别 AI 代理是否处于空闲状态。Adrafinil 的独特之处在于它能智能地挂载到 Claude Code 和 Codex 等代理程序上。它利用 `pmset` 仅在代理程序主动工作时阻止系统睡眠,并在任务完成后自动恢复到正常的节能设置。 主要功能包括: * **上下文感知:** 仅在代理活动时保持电脑唤醒。 * **安全性:** 包含过热保护功能,防止电脑在放入包中时过热。 * **透明度:** 提供菜单栏状态显示、声音提示,以及通过 MCP 进行的可选手动控制。 虽然 Hacker News 上的一些评论者认为通过简单的脚本或现有应用程序即可解决该问题,但开发者强调,Adrafinil 提供了一种更自动化、针对代理程序的防睡眠方案。
相关文章

原文

服用注意 ・ for machines that keep watch after you've gone to sleep.

It's 3 a.m. You're asleep. The agent isn't — it's still mid-thought in a session you started hours ago, and you've closed the lid over it like an eyelid that won't quite shut. caffeinate and Amphetamine are stimulants: they keep the machine wired forever, whether or not anyone's home. Adrafinil is the eugeroic. It does nothing until an agent acquires it, keeps your Mac awake through a closed lid only for as long as that work lives, and clears the moment the last session releases. It only ever wakes for the work — then you both sleep. ♡


Keep your Mac awake only while AI agents are working.

Adrafinil is a macOS menu bar app that prevents the system from sleeping — including clamshell (lid-closed) sleep — exclusively while an AI coding agent has an active session. When no agent is working, sleep behavior is untouched: close the lid and the Mac sleeps normally.

It's the opposite of always-on wake utilities like caffeinate or Amphetamine. Adrafinil only intervenes when an agent (Claude Code, Codex, Cursor, …) is mid-task, and gets out of the way the moment that work finishes.

⚠️ Privileged sleep control. Overriding clamshell sleep requires root. Adrafinil isolates that in a tiny, audited helper that only exposes setSleepBlocked(Bool) — all policy lives in an unprivileged daemon. It holds a standard IOPMAssertion for idle sleep and uses pmset disablesleep for clamshell (lid-closed) sleep, after verifying on-device that the cleaner private IOPMrootDomain paths don't keep a displayless lid-closed Mac awake. See Docs/ARCHITECTURE.md §2.

  • Agent-aware, not always-on. Sleep is blocked only while ≥1 agent session holds an assertion. Zero sessions → normal sleep, including lid-close.
  • Hook integration for 9 agents. One-click installer wires Adrafinil into the hook systems of Claude Code, Codex, Cursor, Gemini CLI, Aider, Hermes, OpenCode, Cline, and Pi.
  • Sub-50ms CLI. adrafinil acquire / release are called from agent hooks and round-trip to the daemon in under 50ms, so they never stall an agent's workflow.
  • Reference-counted assertions. Overlapping sessions stack cleanly; sleep unblocks only when the last one releases.
  • Thermal cutout. If skin/CPU temperature crosses threshold while the lid is closed, all assertions are force-released so a bag-bound Mac can't cook itself.
  • Idle release. Assertions whose owning process has died or gone CPU-idle for N minutes are dropped automatically.
  • Process sniffing (optional). The daemon can auto-acquire when it sees a known agent binary running, even without hooks installed.
  • Lid-close audio + lid-open summary. A chime confirms an assertion is held when you close the lid (the screen is off, so no notification); reopening shows what ran while you were away, peak temperature, and whether the thermal cutout fired.
  • Clean uninstall. Removes every hook entry it added across all agent configs.
  • macOS Tahoe 26.4. That's what I build and test on; it likely runs on earlier 26.x, but I haven't tested it there.
  • Xcode 26+ to build, with Swift 6 strict concurrency enabled.
  • Admin rights for the standard install (the privileged helper installs via SMAppService). A non-admin install path drops the CLI in ~/.local/bin instead of /usr/local/bin.

Download Adrafinil — a signed, notarized disk image. Open it, drag Adrafinil to Applications, and launch. The first launch asks for admin rights once to register the privileged helper. Requires macOS 26.4 or later.

Prefer to build it yourself? See Building.

git clone https://github.com/kageroumado/adrafinil.git
cd adrafinil
open Adrafinil.xcodeproj

In Xcode, select the Adrafinil scheme and Run. You'll need to set a development team for code signing — the daemon (LaunchAgent) and helper (LaunchDaemon) are embedded into the app bundle and registered with the system when the app launches. (No Team ID is baked into the source; the XPC caller check reads your own signing team at runtime, so a rebuild under any Developer ID authorizes its own components without code changes.)

For a headless compile check without local signing identities:

xcodebuild -project Adrafinil.xcodeproj -scheme Adrafinil -configuration Debug \
  -destination 'generic/platform=macOS' \
  CODE_SIGNING_ALLOWED=NO CODE_SIGNING_REQUIRED=NO CODE_SIGN_IDENTITY='' build

The shared logic builds and tests standalone as a Swift package:

cd AdrafinilShared
swift test

Agents don't talk to Adrafinil directly. Each agent's hook system calls the bundled CLI:

adrafinil acquire <session-key> --tool claude-code --reason "long build"   # when a turn starts
adrafinil release <session-key>                                            # when the agent goes idle

Holds are activity-scoped, not session-scoped: Claude Code acquires on UserPromptSubmit and releases on Stop, so the Mac is only kept awake while the agent is actually working — an open-but-idle session at the prompt lets it sleep normally.

The daemon refcounts by session key and asks the helper to block sleep while the count is non-zero.

An agent can also keep the Mac awake for a background task that outlives its reply (a long build or deploy) with a time-boxed hold — either by calling adrafinil hold directly or, for MCP-capable agents, through the bundled MCP tool that adrafinil mcp serves:

adrafinil hold --for 30m --reason "deploy"   # keep awake up to 30 min, then auto-release
adrafinil mcp                                 # speak the Model Context Protocol on stdio (for agents)

Other subcommands: status, install-hooks, uninstall-hooks, daemon-status, version.

Four products across three privilege tiers (full detail, including the Xcode project layout, in Docs/ARCHITECTURE.md):

┌──────────────────────────────────────────────────────────────┐
│  Adrafinil.app   (menu bar app, user-facing)                 │
│  • Status item, settings, installer GUI, lid-open summary    │
└─────────────────────────────┬────────────────────────────────┘
                              │ XPC
                              ▼
┌──────────────────────────────────────────────────────────────┐
│  AdrafinilDaemon  (LaunchAgent, runs as user, always-on)     │
│  • Reference-counted assertion registry                      │
│  • Process watchers (kqueue NOTE_EXIT + periodic sweep)      │
│  • Thermal monitor (SMC)  • Lid-state monitor (IORegistry)   │
│  • Lid-close chime  • CLI socket at …/Adrafinil/cli.sock     │
└─────────────────────────────┬────────────────────────────────┘
                              │ XPC (privileged Mach service)
                              ▼
┌──────────────────────────────────────────────────────────────┐
│  AdrafinilHelper  (SMAppService LaunchDaemon, root)          │
│  • The ONLY component that touches sleep-blocking APIs       │
│  • setSleepBlocked(Bool) + read-only state/version           │
│  • Verifies caller's code-signing requirement                │
└──────────────────────────────────────────────────────────────┘

  adrafinil  (CLI, ships inside the .app, symlinked onto PATH)
  • acquire / release / hold / mcp / status / install-hooks / uninstall-hooks
  • Connects to the daemon socket; <50ms round-trip
  • AdrafinilShared — a Swift package shared across every target: data models (AgentKind, Assertion), the IPC wire formats, AssertionRegistry, CallerVerifier, the hook-install specs, and the CLI argument parser. This is where the unit tests live.
  • Helper stays trivial to audit. It holds no policy — ref counting, thermal, idle, and lid logic all live in the daemon. The privileged surface is a single mutating endpoint plus read-only introspection.
  • Daemon is the source of truth. The app is a pure view layer; it can quit and relaunch freely without affecting held assertions.
  • Public IOPM assertions don't beat clamshell sleep. IOPMAssertionCreateWithName with the public types (and therefore caffeinate) will not keep a lid-closed Mac awake. Adrafinil's v1 uses pmset disablesleep 1, which is blunt (it also disables idle sleep) and must be cleared on shutdown or it leaks — the helper resets to disablesleep 0 on respawn before re-applying state.
  • Daemon handlers run on arbitrary queues. XPC and socket callbacks can arrive on any dispatch queue, so the assertion registry and shared state are synchronized accordingly. Tread carefully around concurrency when modifying the daemon.
  • The CLI is on a latency budget. acquire/release are in the hot path of every agent session, hence static lookups (e.g. AgentKind.allBinaryNames) and a thin socket protocol instead of full XPC for the CLI ↔ daemon hop.

MIT. Do whatever you want, no warranty.

Built by @kageroumado, dispensed at kagerou.glass. The name is a nod to adrafinil — a wakefulness-promoting prodrug — because the app keeps your machine awake only when it actually has work to do.

联系我们 contact @ memedata.com