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