Repository navigation
feat: add low-battery auto-off guardrail - #5
Merged
Merged
Conversation
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
nosleep onnow restores sleep when the charge drops to/below 20% while running on battery, not just when the window ends. On AC power a low reading is ignored, since it's charging.--battery=N(1–99) moves the threshold;--no-batteryopts out and auto-offs on DURATION alone.This is on by default: an unplugged Mac left awake in clamshell mode would otherwise drain flat waiting out its window, and the old behavior is one flag away.
Usage
The threshold is only read from
--battery=N, never a separate--battery Ntoken:DURATIONaccepts a bare number of hours, so the two would be ambiguous —nosleep on --battery 3means three hours at the default threshold.Example
statuswith the guardrail armed:Why
An unplugged Mac running lid-closed with sleep disabled could drain its battery flat instead of falling back to normal sleep. 20% restores sleep with reserve to spare, ahead of macOS's own low-battery warning (~10%) and emergency sleep (~2–3%) — a graceful sleep rather than a scramble at the bottom.
How
_guardrailsubcommand. With--no-batteryit just sleeps out DURATION (byte-for-byte the previous behavior). Otherwise the same root process pollspmset -g battevery 30s and, on a low-while-unplugged read, tears downcaffeinateand the lid watcher (which restores brightness) beforepmset -a disablesleep 0.cmd_onhands it the resolved state dir explicitly (NOSLEEP_STATE_DIR, alongsideNOSLEEP_PMSET_BIN/NOSLEEP_POLL_INTERVAL).sudo'senv_resetdropsXDG_STATE_HOME, so a guardrail left to re-derive its own paths would operate on a different directory and silently skip the teardown.min(poll, remaining)so the timed auto-off can't land after the momentstatusadvertised.08and quietly turn010into 8), and armed-ness is tracked separately from the value so that no input a user can type —0, empty — disarms the guardrail silently.is_our_timernow matches the_guardrailcommand line;battery_readdoes onepmsetcall and one parse, shared by the poll loop andstatus.statusshows the armed threshold and current charge; state adds abattery_thresholdfile, cleaned up alongside the rest.