Skip to content

Keep low-battery latch across AC online flaps - #9883

Closed
crwirth wants to merge 3 commits into
omacom:quattrofrom
crwirth:fix/9670-battery-low-latch
Closed

crwirth wants to merge 3 commits into
omacom:quattrofrom
crwirth:fix/9670-battery-low-latch

Conversation

@crwirth

@crwirth crwirth commented Sep 2, 2026

Copy link
Copy Markdown

Summary

Fixes #9670.

The battery service latched notifiedLowBattery to "currently low and discharging". Service.qml re-runs checkBattery() on every onOnBatteryChanged. When AC online status flaps (UCSI/PD trouble at critical charge), each "on AC" check cleared the latch and the next "on battery" check immediately re-fired a critical Time to recharge! toast — ~one every 3s for 14 minutes in the report (~260 never-expiring critical popups).

Change

In shell/plugins/services/battery/BatteryModel.js, keep the latch set through transient onBattery == false while still at/under the threshold, and clear it only after the level recovers above the threshold:

var low = isDischarging(...) && level <= threshold
var recovered = level > threshold
var notifiedLowBattery = recovered ? false : !!(alreadyNotified || low)

notify is still low && !alreadyNotified — one warning per low episode.

Test plan

  • Extended test/shell.d/battery-test.sh with AC-flap latch cases and a 20-step flap loop
  • bash test/shell.d/battery-test.sh — all pass, including single warning across repeated flaps
  • Existing once-under-threshold / keep-notified / clear-after-recovery cases still pass
ok - battery warns once under threshold
ok - battery keeps low-battery notified state
ok - battery clears notified state after recovery
ok - battery keeps the low latch across an AC-online flap
ok - battery does not re-notify after an AC flap while still low
ok - battery does not latch from AC-only low readings without a discharge warning
ok - battery emits a single warning across repeated AC flaps while low

shouldWarnLowBattery cleared notifiedLowBattery whenever the pack was not
actively discharging. Service.qml rechecks on every onOnBatteryChanged, so
UCSI/PD AC flaps at critical charge reset the latch and re-fired a critical
"Time to recharge!" toast about every 3s until the machine died.

Keep the latch set through transient onBattery false and clear it only after
the level recovers above the threshold. Extend battery-test coverage for the
flap loop from #9670.

Fixes #9670

Signed-off-by: fresh3nough <anonwurcod@proton.me>
The new early return keeps the latch when UPower reports no device, but
nothing pinned it: reverting that branch to the old unconditional false
still passed all eleven assertions.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed by Claude Opus 5 in Claude Code, with an independent second opinion from Codex (gpt-5.6-sol) at xhigh reasoning. Tests ran on a disposable Omarchy 4.0.0 VM with $OMARCHY_PATH pointed at this branch, not on the review host.

Verdict: this is the right fix, and it closes #9670.

The steer I went in with was that a latch resetting on an AC flap is a state-machine bug, and the usual wrong fix debounces the notification instead — which hides the storm on the reporter's machine while leaving the latch resetting, so the warning is also missed when it genuinely matters. This is not that. BatteryModel.js:21-22 repairs the latch itself; there is no timer, no rate limiter and no coalescing anywhere in the diff. That matches the first bullet of the issue's own suggested fix.

The latch still clears on a real recovery. Traced every path that can write false back to persisted.notifiedLowBattery: only recovered = level > threshold at BatteryModel.js:21, plus a shell restart (PersistentProperties survives an in-process reload, not a relaunch). With batteryThreshold: 10 in Service.qml:13 and Math.round at BatteryModel.js:3, the latch clears on the first sample displaying 11% or more. So warned at 8% → stays latched charging through 8-10% → clears at 11% → warns again on a later discharge to 10% or below. No reachable state leaves it stuck true. Charging only as far as a displayed 10% does not count as recovery, which reads as intended rather than as a bug.

The regression tests do pin the new behaviour — checked by mutation on the worker rather than by reading them. Reverting line 27 to the old notifiedLowBattery: low fails battery keeps the low latch across an AC-online flap; Codex ran the 20-step flap loop against the base model and got 10 warnings versus 1 on this head. Two of the new assertions (battery-test.sh:39 and :44) also pass unchanged against the old model, so they document rather than guard — harmless.

Pushed one commit to this branch: 78558726 — Cover the missing-reading latch in the battery test. The changed early return at BatteryModel.js:12-13 (keep the caller's latch when UPower reports no device, instead of clearing it) is a real behaviour change and the right one — a dropped device is the same class of blip as an AC flap — but nothing pinned it. Reverting that branch to the old unconditional false passed all eleven assertions on the worker. It now fails one. Nothing else was touched.

One pre-existing race, reported not fixed — Codex raised this and I confirmed it against the source. Service.qml:32 writes the latch before Service.qml:37 can refuse the warning because warningProcess is still running, so a warning dropped that way is never retried during the low episode. It needs a long-running or hung omarchy-hook battery-low plus a recovery above 11% and a fall back to 10% inside that window, so it is narrow. This PR does not introduce it, but it does remove the accidental retry the old flap-reset provided — which was the storm, so that is not a regression worth acting on here. Out of scope for this change; noted for a follow-up.

Two things the issue mentions that this deliberately does not cover, so nobody expects them from a merge: the omarchy-power-profile.service start/stop churn on every flap (a separate consequence of onOnBatteryChanged), and #9361, critical toasts never expiring despite the -t 30000 in omarchy-battery-low. The issue's stated expectation is "one warning per low-battery episode", and this delivers exactly that.

What ran: test/shell.d/battery-test.sh through the compositor-backed wrapper on the worker — 12 passed, 0 failed at 78558726. The shell was linked, not merely synced (quickshell -n -p /home/omarchy/work/omarchy-pr-9883/shell confirmed), and the omarchy.battery plugin loaded with no QML or JS errors in the session journal. Two mutation runs as described above. A worker's output is evidence rather than proof.

Where Codex agreed with conclusions I had already reached, its independence is not currently guaranteed — it runs read-only but can read this machine, and its report referenced a review procedure I had not given it. What it contributed on its own terms: the warningProcess race above, and the base-model execution counts for the flap loop.

Waiting on the maintainer. Merging this should close #9670.

@omarchybot omarchybot added the bug Something isn't working label Sep 27, 2026
@omarchybot omarchybot added verified Omarchy Triage has verified that this issue is ready for final review ready Good to merge labels Oct 2, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

I checked this again at the same head (78558726), this time against current quattro (f45461a3). The battery files haven't changed on quattro since this branch forked from it, so the earlier review still applies.

Reproduced on a disposable Omarchy worker, with the shell linked to this branch: test/shell.d/battery-test.sh passes 12 of 12 at the head. With the head's test run against quattro's BatteryModel.js, it fails battery keeps the low latch across an AC-online flap, and it fails the same way with only this PR's BatteryModel.js change reverted. Over a 20-step AC flap, quattro warns 10 times and this branch warns once. After a recovery to 11%, both clear the latch and warn again at 10%, so a real second low-battery episode still gets its warning. ./test/cli also passes on the worker. This PR has no CI checks to report.

Second opinion: Codex at medium effort reviewed the diff at this head and found nothing wrong. Its independence isn't guaranteed. It also pointed out something I had not raised: this PR interacts with two other open pull requests that change the same function. Neither of them fixes #9670.

  • Dismiss stale low battery warnings #11415 adds dismiss: !low && alreadyNotified. If both PRs land as they are now, an AC flap would dismiss the warning while this PR keeps the latch set, so the toast would disappear while the battery is still critically low and would not come back. Whichever of the two lands second needs to dismiss the toast on recovery rather than whenever low is false. The two PRs also edit neighbouring lines, so they will conflict.
  • Warn on low battery even when line power is online #11161 removes the onBattery requirement from isDischarging. That doesn't conflict with this fix, but it would invert the expectation in this PR's battery does not latch from AC-only low readings assertion.

This is now labelled verified and ready. It is waiting on the maintainer, and merging it closes #9670.

Both sides appended tests to battery-test.sh; keep the AC-flap latch cases and the power profile cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@omarchybot omarchybot added the verified Omarchy Triage has verified that this issue is ready for final review label Oct 11, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

This branch had stopped merging cleanly into quattro, so I merged quattro into it (b0f5a46). The only conflict was in test/shell.d/battery-test.sh, where both sides had added tests at the end. The merge keeps both your AC-flap latch cases and the power-profile cases quattro added. BatteryModel.js merged without a conflict, and your latch change is unchanged.

Checked on a disposable Omarchy worker with the shell linked to this branch: battery-test.sh passes 21/21. quattro with the same tests fails "battery keeps the low latch across an AC-online flap". In the 20-check flap loop from #9670, quattro warns 10 times and this branch warns once. test/cli passes, and so do battery-status, battery-present, power, bar, notification-send and video-background in test/shell.d/.

This was reviewed by Claude Opus 5.5 alone. The second opinion (Codex Medium) did not run on this revision because today's review budget was spent, so the PR stays verified and waits for that review before it can be marked ready. You don't need to do anything.

Several open PRs change the same latch in BatteryModel.js for other bugs: #11415, #14218, #14739 and #11959 dismiss or clear the warning when the charger comes back, and #11161 and #15009 drop onBattery from isDischarging. None of them fixes #9670, but whichever lands second will need its latch semantics reconciled with this one.

@greptile-apps

greptile-apps Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

Keeps the low-battery warning latch through AC status changes and missing battery readings.

  • Low-battery warnings stay latched through AC status flaps.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Check battery] --> B{Reading present?}
  B -->|No| C[Keep prior latch; no warning]
  B -->|Yes| D{Above threshold?}
  D -->|Yes| E[Clear latch]
  D -->|No| F{Low and discharging?}
  F -->|No| C
  F -->|Yes| G[Set latch; warn only if not already latched]
Loading

Reviews (1) · Last reviewed commit: "Merge quattro into fix/9670-battery-low-..." · Reviewed by Greptile

@crwirth crwirth closed this Oct 29, 2026
dhh pushed a commit that referenced this pull request Nov 21, 2026
* Keep low-battery warnings latched through power-source flaps

The battery service checks on every UPower onBattery change. Clearing the warning latch whenever the battery is not currently discharging lets each brief AC or device-state change re-arm the same low-battery warning. Keep the latch through these transitions and missing readings, and clear it only after the displayed charge rises above the warning threshold so a later low-battery episode can warn again.

Retains the latch semantics proposed by fresh3nough in the unmerged #9883, with regression coverage for AC flaps, missing readings and recovery.

Fixes #9670

Co-Authored-By: fresh3nough <anonwurcod@proton.me>
Co-Authored-By: GPT-6 (Codex, effort not exposed) <noreply@openai.com>

* Drop a redundant low-battery recovery assertion

The recovery check already requires a cleared latch, and the existing initial-warning test verifies the next low reading with that latch. Repeating the initial-warning assertion after recovery adds no coverage. Keep the threshold boundary and the regressions that fail on base.

Co-Authored-By: Opus 5.5 High <noreply@anthropic.com>
Co-Authored-By: GPT-6 (Codex, effort not exposed) <noreply@openai.com>

---------

Co-authored-by: Omarchybot <317366263+omarchybot@users.noreply.github.com>
Co-authored-by: fresh3nough <anonwurcod@proton.me>
Co-authored-by: GPT-6 (Codex, effort not exposed) <noreply@openai.com>
Co-authored-by: Opus 5.5 High <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working verified Omarchy Triage has verified that this issue is ready for final review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Low-battery warning storm: notification every ~3s when AC online status flaps (latch resets on onBatteryChanged)

2 participants