I put a night-watch soldier on my Mac for AI agents
说明与用途
开发者为 macOS 构建了夜间巡逻程序,仅在 AI 代理工作时保持机器清醒,退出时恢复睡眠设置,并发送通知。
问题
Mac sleeps too well while AI agents run, causing frozen processes, screen on all night, or battery drain.
结果
All 70 unit tests green; real‑world incidents resolved: watchdog restores sleep setting within 3s, watch time survives restarts, notifications work after permission granted.
工作流
- Detect agent activity via caffeinate child process or >2% CPU over 60s
- Run DutyEngine state machine to decide sleep or wake
- If agent active, keep Mac awake; else allow sleep
- Use pmset -a disablesleep 1 for lid‑closed mode, with watchdog to restore setting on app exit
- Persist remaining watch time across restarts
- Send notifications via macOS notification center or osascript when shift ends or conditions change
证据
My Mac had the opposite problem. It slept too well.
The duty logic lives in a pure state machine (DutyEngine) with no system calls. With a fake clock I wrote 70 scenario tests: "quiet for 5 minutes → hand over", "battery under 20% → stand down", "overheating → rest, then resume".
Final measurement: even after kill -9 on the app, the sleep setting is restored within 3 seconds.
macOS doesn’t grant notification permission at all to ad-hoc signed apps without a Developer ID (Team ID).
- 记录日期: 2026-10-14T09:41:00.063Z