Skip to content

Restore owned sleep state on unplug, quit and unexpected termination - #12

Open
future-gareth wants to merge 1 commit into
Aboudjem:mainfrom
future-gareth:improvements/watchdog-safety
Open

future-gareth wants to merge 1 commit into
Aboudjem:mainfrom
future-gareth:improvements/watchdog-safety

Conversation

@future-gareth

Copy link
Copy Markdown

What does this change?

Fixes #8. Quitting while ON previously left the global sleep-disabled flag set after the app's battery floor and timer stopped. Normal quit now restores Sleepless-owned state; crashes and force-quits are handled independently.

  • Add default-on Turn off when unplugged, using native IOKit notifications. Reconnecting power never re-arms.
  • Add an unprivileged safety helper and per-user LaunchAgent, with a 45-second boot-scoped lease, 10-second renewal/ticks, and a shared kernel transaction lock. The watchdog never enables keep-awake.
  • Refuse arming without power monitoring, both exact passwordless pmset permissions and a healthy watchdog. Preserve pre-existing unowned state; report failed commands/read-back explicitly and retain recovery after failed clearing.
  • Fence installation, upgrades and removal. Verify normal sleep before replacing/removing recovery; abort removal on failure. Preserve the native popover, existing controls, optional login setting and .app/ZIP distribution.
  • Update build/CI, safety/deployment documentation and translation notices. Include the unchanged MIT licence in built bundles.

The lease/watchdog design is adapted from mmavko/Sleepless. No wholesale merge or unrelated CLI, activity tracking or thermal features are included. Both repositories carry the same MIT licence and Adam Boudjemaa copyright notice, retained unchanged.

Why the watchdog must be independent

Issue #8 concerns a system-global setting whose lifetime exceeds the GUI process. A normal applicationShouldTerminate / applicationWillTerminate handler covers orderly Quit, but cannot run after SIGKILL/Force Quit, and cannot be relied on after a crash. An in-process timer, signal handler or shutdown callback therefore cannot provide the required unexpected-termination recovery; checking only on the next launch would also leave sleep disabled until someone reopens the app.

The separate per-user LaunchAgent runs a copied helper independently of the GUI. The GUI must continually renew its short ownership lease; when renewal stops, the watchdog restores recorded Sleepless-owned state without waiting for another app launch. The copied helper also survives app-bundle deletion. Arming is blocked until the watchdog has demonstrated health and both exact sleep permissions are available. This complements immediate normal-quit cleanup; it does not replace it or guarantee recovery after permission revocation, logout or agent failure.

Maintainer action required: Homebrew uninstall integration

Before distributing this version through Homebrew, update the external tap's uninstall action to invoke Sleepless.app/Contents/Resources/uninstall.sh as the logged-in user before deleting the bundle. If restoration fails, abort removal and retain the watchdog and grant. A cask action that only quits/deletes the app does not remove the separately copied watchdog or scoped sudoers grant. The uninstaller fences new arms, restores owned state and verifies normal sleep before removing recovery and privileges. Only its fixed grant-removal command requires native administrator authorization; do not run the entire helper/uninstaller as root.

The tap is outside this repository, so this PR does not implement that hook. This is a distribution prerequisite for complete Homebrew removal and needs coordinated maintainer review. The retained watchdog provides lease recovery after bundle deletion while its permissions and login session remain available; it is not a substitute for complete uninstall integration.

How was it tested?

  • bash tests/run.sh: 104 safety assertions, 5 lifecycle integration scenarios and 3 native read-only checks passed. Covers power removal/return/opt-out, normal exit, expiry, process death, real-process concurrency, ownership, failed permissions/commands/read-back, and failure-preserving lifecycle behavior.
  • bash build.sh: zero-warning arm64/macOS 26 build passed. App and helper strict signature checks passed; both Mach-O minimums and Info.plist are 26.0. Required resources and licence verified.
  • TARGET=arm64-apple-macos13.0 bash build.sh build/compatibility: build and strict app/helper signature checks passed; minimum 13.0 verified. Compile validation only; no macOS 13 runtime claim.
  • Previously authorized development hardware tests on MacBook Air (Mac16,12), Apple M4, macOS 26.5.2: physical unplug/reconnect, normal Quit, SIGKILL recovery (49.3 seconds), SIGABRT recovery (43.7 seconds to flag OFF), missing/revoked grant, recovery after grant repair, owned-state upgrade, failed removal, complete removal/reinstall and unplug opt-out passed. Flag OFF/lease cleanup verified after each case, including failed tests once permission was repaired.
  • A user on a second Apple Silicon MacBook/macOS 26.6.2 confirmed launch, powered lid-closed operation, unplug auto-off, and OFF/no error after Force Quit, a 75-second wait and reopening. Second-machine timing and lease state were not directly measured.

No new privileged installation or sleep-setting mutation was performed during contribution preparation. These final checks build and test without modifying the installed app.

Remaining limitations / maintainer review

  • macOS exposes a global Boolean without an ownership API. An external ON during Sleepless's own ON cannot be distinguished; competing tools and concurrent logged-in keep-awake sessions are unsupported.
  • Revoked permissions, a disabled/stalled agent or logout can prevent recovery. Per-user launchd recovery is not a privileged system-wide service; the nominal recovery interval is not a hard deadline.
  • Actual physical sleep was not demonstrated on the development Mac because essential Screen Sharing asserted PreventSystemSleep. Second-machine normal quit, reconnection, failed permissions and actual physical sleep were not separately confirmed. Intel and macOS 13 runtime remain untested.
  • The external Homebrew tap must invoke the bundled uninstaller before deleting the app to achieve complete removal. App deletion alone leaves the copied watchdog and scoped grant; the copied helper can still expire an owned lease. No tap change or release is included.
  • Builds remain ad-hoc signed and unnotarized. Translation notices link to current English safety instructions; full translations remain pending.
  • This introduces an independent unprivileged user LaunchAgent/helper, an explicit exception to the repository template's “no helper daemons” preference. No root daemon, kext, new dependency or broader sudo command grant is added.

Checklist

  • Clean build and automated checks above; app launch confirmed on both tested macOS 26 machines before contribution preparation.
  • Real on/off behavior verified during authorized hardware validation.
  • Removal restores sleep before deleting recovery/grant; failed restoration aborts. Revocation is not tested by executing another sleep-changing command.
  • No personal paths/usernames in production scripts or install commands; no Context files or generated artifacts included.
  • README/security/deployment/audit/contributor docs updated; original licence retained.
  • Contribution is under the existing MIT licence.

@future-gareth
future-gareth marked this pull request as ready for review October 22, 2026 16:52

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Safety: quitting while ON leaves disablesleep=1 with no watchdog (battery floor + auto-off die with the process)

2 participants