Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

I put a night-watch soldier on my Mac for AI agents

Description and use

A developer built a night‑watch soldier for macOS that keeps the machine awake only while AI agents are working, restores sleep settings on app exit, and sends notifications.

Problem

Mac sleeps too well while AI agents run, causing frozen processes, screen on all night, or battery drain.

Outcome

All 70 unit tests green; real‑world incidents resolved: watchdog restores sleep setting within 3s, watch time survives restarts, notifications work after permission granted.

Workflow

  • 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

Evidence

My Mac had the opposite problem. It slept too well.

Source

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".

Source

Final measurement: even after kill -9 on the app, the sleep setting is restored within 3 seconds.

Source

macOS doesn’t grant notification permission at all to ad-hoc signed apps without a Developer ID (Team ID).

Source

  • Record date: 2026-10-14T09:41:00.063Z