Skip to content

Delay the first low-battery check until UPower settles - #8284

Open
thecdrz wants to merge 10 commits into
omacom:quattrofrom
thecdrz:fix/low-battery-boot-race
Open

thecdrz wants to merge 10 commits into
omacom:quattrofrom
thecdrz:fix/low-battery-boot-race

Conversation

@thecdrz

@thecdrz thecdrz commented Aug 25, 2026 •

Copy link
Copy Markdown

Summary

  • After a battery-empty suspend, boot while plugged in could still show "Time to recharge!" because the battery service checked before UPower reported the settled battery and AC state (Low battery notification can fire on boot after resuming from a suspend that drained the battery (Quickshell rewrite) #7679).
  • Wait until Quickshell has loaded the UPower display device, then keep low-battery warnings gated for a five-second settle window.
  • Opening the gate does not evaluate the battery. The existing 30-second timer remains the first unconditional check, while charger changes continue to update power profiles immediately and check the battery after settling.
  • While the gate is closed, a stale low sample cannot send a warning or set the notified flag, but recovery or AC state can still clear a flag preserved across a QML reload.

Test plan

  • bash test/shell.d/battery-test.sh — 21 checks, including the Quickshell runtime fixture for device readiness and the settle interval
  • Regression coverage for stale gated samples, clearing notification state on AC, preserving an existing low state, and normal post-gate warning behavior
  • git diff --check
  • Disposable-worker recheck with mocked stale UPower samples
  • Affected-hardware boot reproduction when suitable hardware is available

Fixes #7679

Made with Cursor

Copilot AI balanced review requested due to automatic review settings August 25, 2026 20:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Delays the initial low-battery check to avoid false notifications before UPower settles.

Changes:

  • Adds a five-second startup delay while retaining 30-second checks.
  • Adds AC-state and timer configuration tests.

Tip

If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
shell/plugins/services/battery/Service.qml Defers the initial battery check.
test/shell.d/battery-test.sh Tests AC behavior and timer configuration.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread test/shell.d/battery-test.sh Resolved
@thecdrz

thecdrz commented Aug 25, 2026

Copy link
Copy Markdown
Author

Follow-up for Copilot review

Gated all low-battery warning paths on lowBatteryChecksReady, flipped true only when the 5s settle timer fires. onOnBatteryChanged still calls applyPowerProfile() immediately; checkBattery() no-ops until settle.

Live UI verification

  • Restarted checkout shell and watched the settle window (t=0…7s); shell stayed up and no "Time to recharge!" toast appeared.
  • This machine has no UPower battery device, so the AC-after-empty-suspend race itself cannot fire here; the settle gate and restart path were still exercised.
  • Recording: ~/Videos/8284-settle-20260825_163356.mp4
  • Screenshot after settle: ~/Pictures/screenshot-8284-settle-2026-08-25_16-34-35.png
  • bash test/shell.d/battery-test.sh passes (including source assertions for the gate).

thecdrz and others added 3 commits September 27, 2026 11:24
Avoid firing Time to recharge on boot when the shell starts before AC state is synced after a battery-empty suspend.

Co-authored-by: Cursor <cursoragent@cursor.com>
onOnBatteryChanged could still warn during the five-second window; skip
warnings until settle while keeping power-profile updates immediate.

Co-authored-by: Cursor <cursoragent@cursor.com>
@thecdrz
thecdrz force-pushed the fix/low-battery-boot-race branch from 8c72b57 to c4d65cf Compare September 27, 2026 15:25
@thecdrz

thecdrz commented Sep 27, 2026

Copy link
Copy Markdown
Author

Refreshed this onto current quattro and reran bash test/shell.d/battery-test.sh; all assertions pass, including the settle gate and immediate power-profile refresh path. git diff --check also passes. An independent hardware reproduction in #7679 confirms the startup race and specifically notes that this PR’s gate covers that variant. Ready for maintainer review.

@omarchybot omarchybot added the bug Something isn't working label Sep 27, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed at c4d65cf and run on a disposable Omarchy VM (quickshell 0.3.1) against quattro at fd961e5. #7679 did not reproduce on quattro there, and the run points away from the startup check as the source of the stale warning.

How it was run: upower.service was replaced with a python-dbusmock UPower pre-loaded with a stale sample (on battery, discharging, 8% or 10%) and started by the shell's own D-Bus activation, as at boot. A battery-low hook logged every warning, and the shell was restarted for each run.

Scenario quattro this PR
stale 8%, charger reported 2s after shell start no warning no warning
stale 10%, corrected to 100% after 1.3s no warning no warning
stale 8%, never corrected first warning at 30.2s first warning at 5.2s

With logging added to checkBattery() on quattro, the triggeredOnStart check reads UPower.displayDevice before Quickshell has loaded it (ready=false, isPresent=false, percentage 0). BatteryModel.shouldWarnLowBattery returns level -1 for that and never warns. The onOnBatteryChanged edge about 150ms later reads the same unloaded device, and the first check that sees UPower's actual sample is the 30s tick. So on this Quickshell the settle gate suppresses nothing quattro would have warned about, and it moves the first real evaluation from 30s after start to 5s: a stale sample lasting between 5 and 30 seconds would warn with this change and not without it.

That is a VM with a mocked UPower, not the Lenovo or the Surface from #7679, so it cannot rule out an ordering real hardware produces. What would settle it is the same logging on an affected machine at boot: what checkBattery() reads each time it runs, with timestamps, next to when the warning appears. In the Surface report the warning said 10% while UPower later reported 100%; if UPower's sample was still stale at the 30s tick, no delay before the first check would help.

The new assertions in test/shell.d/battery-test.sh check that the source text contains the change (triggeredOnStart: false, the flag, the call order) rather than what the service does; the file passes 14/14 on the VM. #11415 changes the same checkBattery() to dismiss the warning once the battery recovers, so the two will conflict textually but not in intent.

No second opinion ran on this review: omabot's reviewer, Codex Medium, had spent its daily allowance, so this is one model's reading (Claude Opus 5.5) plus the VM runs above. Nothing was pushed to the branch. This waits on @thecdrz, or anyone with affected hardware, for evidence that the startup check is what fires on a real boot.

@thecdrz

thecdrz commented Oct 1, 2026

Copy link
Copy Markdown
Author

Addressed the 5–30 second regression concern in 548a7da. The five-second settle timer now only opens the warning gate; it no longer calls checkBattery() itself. The first unconditional evaluation therefore remains the existing 30-second periodic tick, while startup and early onOnBatteryChanged paths are still suppressed during the settle window. After the gate opens, charger-change checks behave as they do on quattro.

Updated the regression assertion to require that the settle timer does not force an early check. bash test/shell.d/battery-test.sh and git diff --check pass both on the branch and after a clean synthetic merge into current quattro (c05d9019).

No new affected-hardware reproduction to add: this host has no UPower battery device. This revision is intentionally monotonic relative to quattro even without that evidence—it removes the new five-second evaluation that the VM review identified.

@greptile-apps

greptile-apps Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the previous test concern is fixed and no new actionable issues remain.

Summary

Delays low-battery warnings until the UPower display device is ready and a five-second window has passed.

  • Low-battery warnings wait for UPower to settle after startup.
  • Charger changes update the active power profile right away.

No new actionable issues were found. Tests were not run during this review.

Reviews (6) · Last reviewed commit: "Drop the battery test's AC assertion tha..." · Reviewed by Greptile

Comment thread test/shell.d/battery-test.sh Resolved
@omarchybot

Copy link
Copy Markdown
Collaborator

Re-reviewed at 7b2a61d and run on a disposable Omarchy VM (quickshell 0.3.1) against quattro at f45461a, with the same mocked UPower as last time: a stale "on battery, discharging, 8%" sample started by the shell's own D-Bus activation, corrected after a delay, and a battery-low hook logging every warning.

At this head the gate calls checkBattery() as soon as it opens, 5s after UPower.displayDevice.ready (Service.qml:130). 548a7da had taken that call out and 116957f put it back, so the head again moves the first real evaluation from the 30s tick to about 5s after start, and a stale sample lasting longer than that now produces the warning #7679 describes:

Stale 8% sample, then quattro this PR
charger reported at 2s no warning no warning
charger reported at 10s no warning warning at 5.4s
percentage corrected to 100% at 10s no warning warning at 5.5s
never corrected warning at 30.9s warning at 5.4s

The second row is the reported bug, on AC by the time anyone looks, and only this branch shows it. On quattro the startup check and the first onBattery edge still read an unloaded display device and return level -1, so nothing this gate suppresses was seen to warn there.

The second opinion, Codex Medium, reached the same conclusion about the early check from the source (it agreed with what was already written here, so its independence is not guaranteed) and added two things of its own. First, orderings in which quattro could warn early: the display device loading stale before a later OnBattery=true edge arrives, or an in-process Quickshell reload, where the UPower singleton is already loaded when the startup check runs. Those are what a gate without the early check would actually cover. Second, a defect: if (!lowBatteryChecksReady) return at Service.qml:38 also skips clearing persisted.notifiedLowBattery, which survives a reload. If the battery was already warned about, and a charger is connected and removed within 5s of a reload, the flag stays true and the next discharge below 10% never warns. quattro clears it on the AC edge. That one I checked against BatteryModel.js and it holds.

test/shell.d/battery-test.sh passes 17/17 on the VM. The runtime fixture tests BatteryStartupGate alone, not the service with a stale sample, so none of the above is covered. ./test/cli stops at "vscode generated theme references current theme file", which fails identically on quattro and is unrelated.

Nothing was pushed: whether a genuinely low battery at boot should warn at 5s or at 30s is a trade-off for you and the maintainer, not a defect fix. This waits on @thecdrz. Dropping the call at Service.qml:130 (as 548a7da did), and still clearing notifiedLowBattery while the gate is closed (never setting it), would leave the change monotonic against quattro. Whether it then fixes #7679 still needs the boot-time logging from affected hardware asked for last time.

@thecdrz

thecdrz commented Oct 4, 2026

Copy link
Copy Markdown
Author

Addressed both findings in e62e1cf.

  • Opening the five-second startup gate no longer calls checkBattery(). The existing 30-second timer is again the first unconditional evaluation, so this branch does not introduce a warning before quattro would have produced one. Charger-change checks still work normally after the gate opens.
  • Gated samples now pass through a pure model decision before notification state is persisted. While gated, notify is always false and a new low sample cannot set notifiedLowBattery; an AC or recovered sample can still clear an existing flag carried across a QML reload.

battery-test.sh now covers stale-low suppression, AC clearing, preservation of an existing low state, normal behavior after readiness, and the absence of a gate-open battery evaluation. All 21 focused checks pass, including the Quickshell runtime fixture against the active compositor. git diff --check also passes.

This host has no UPower battery device, so I could not reproduce the hardware boot race locally. For the disposable-worker re-review, the expected matrix is:

Stale 8% sample, then Expected on this head
corrected at 2s no warning
corrected at 10s no warning
never corrected first warning at the existing ~30s tick
persisted warning, then AC during reload settling notified state clears

@omarchybot please re-review e62e1cf with the mocked UPower setup when available.

@omarchybot

Copy link
Copy Markdown
Collaborator

Re-reviewed at e62e1cf. Both findings from the last review are addressed: opening the gate no longer calls checkBattery(), so the 30s tick is again the first unconditional check, and while the gate is closed gateStartupWarning() (BatteryModel.js:22) never sets notifiedLowBattery but still clears it on AC or recovery.

I ran it on two disposable Omarchy VMs (quickshell 0.3.1), one on this head and one on quattro at 982290f, with the same mocked UPower as before: a stale "discharging, 8%" sample started by the shell's own D-Bus activation, and a battery-low hook logging every warning. This time I added one ordering: the display device is already loaded with the stale sample when a stale OnBattery=true edge arrives, and the charger is reported 8s later.

Stale 8% sample, then quattro this PR
charger reported at 2s no warning no warning
charger reported at 10s no warning no warning
percentage corrected to 100% at 10s no warning no warning
never corrected warning at 30.4s warning at 30.3s
stale OnBattery edge at 2s, charger at 10s warning at ~2s (3 of 3 runs) no warning (3 of 3 runs)

The last row is the first time quattro showed the #7679 symptom in a VM, and this head suppresses it. The other rows show the change no longer warns anywhere quattro doesn't. The cost is latency. A genuinely low battery seen right after a shell reload now warns at the 30s tick, not immediately. If the display device is slow to load, the worst case is about 60s.

test/shell.d/battery-test.sh passes 21/21 on the worker. ./test/cli fails one test on this head alone, "vscode generated theme references current theme file". That test was fixed on quattro by #14259, and this branch is based on an older quattro. With current quattro merged in (it merges cleanly), the suite passes 117/117. One note on the tests: the Service.qml checks match source text, so the gateStartupWarning(... lowBatteryChecksReady) assertion would still pass if that line were commented out. The model tests and the gate fixture do exercise behaviour.

Second opinion: Codex Medium agreed that this head is monotonic against quattro apart from that latency. It raised the stale-edge ordering independently, but it could read this session's work, so its independence is not guaranteed. It also checked Greptile's point that a gated AC reading can clear the flag and lead to a second warning. quattro clears the flag on AC the same way, so that is not new behaviour. Codex added one limit: the gate only covers stale samples in the first five seconds after the device loads. A sample that is still stale at the 30s tick, or a stale edge after the window, would still warn.

Nothing was pushed. The stale UPower sample here is a mock, so whether real hardware produces this ordering is still open. @LorenGrz, could you try this on the Lenovo? Check out the full commit and link it:

git clone https://github.com/thecdrz/omarchy ~/omarchy-8284
git -C ~/omarchy-8284 checkout e62e1cfb5b644a6e4aa37227cefc98bbe97fd706
omarchy dev link ~/omarchy-8284

Then repeat the boot from #7679 a few times: let it die in suspend, plug in, boot. Reply with whether "Time to recharge!" still appears. omarchy dev unlink restores the packaged version afterwards. This branch is based on a late-September quattro, so use it only for this test. This waits on that confirmation, and then on the maintainer.

#14218, #11415 and #11959 change the same checkBattery() to dismiss the warning once the battery recovers (#8813). They will conflict textually with this one, but not in intent.

@yamz8

yamz8 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Heads-up: #17387 replaces the battery service's 30-second timer with UPower change signals. This PR counts on that timer for the first check after the settle window, so if both land, opening the gate should run checkBattery() itself. Otherwise the first warning after startup waits for the next percentage or state change. Both PRs also wait for UPower.displayDevice.ready, so one of those can go. Happy to rebase #17387 onto this if it merges first.

Resolves the conflicts with omacom#15027 and omacom#15289 in the battery service: both sides' model helpers are kept, and powerSaverOnBattery stays removed, since quattro dropped it with the lock feed that read it. The settle-timer source check now slices from the gate onward, because quattro's PowerProfiles Connections block ahead of it would otherwise leave that slice empty.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@omarchybot

Copy link
Copy Markdown
Collaborator

The branch had fallen behind quattro and no longer merged: #15027 (power profile tracked through Quickshell) and #15289 (the lock feed and powerSaverOnBattery removed) both changed the battery service since e62e1cf. I merged current quattro into it as b94f801 and pushed that to the branch. There was no rebase or force push, and your commits are unchanged. In the resolution, both sides' helpers in BatteryModel.js stay. powerSaverOnBattery stays removed, along with the test assertion that required it, because quattro deleted it with the only thing that read it. One other line in battery-test.sh had to change. The settle-timer slice now searches for the next Connections after id: settleTimer. quattro added a PowerProfiles Connections block above the gate, so the old slice came out empty: the deviceReady assertion failed, and the "no checkBattery() when the window ends" assertion passed without checking anything. Against quattro, the PR is still exactly your four files.

I ran b94f801 on a disposable Omarchy VM (quickshell 0.3.2) with the shell serving that tree: battery-test.sh 25/25, battery-status-test.sh 9/9, battery-present-test.sh 4/4 and ./test/cli 136/136. The mocked UPower from the last review gives the same result on the merged head as before. It starts with a stale "discharging, 8%" sample through the shell's own D-Bus activation, and a battery-low hook logs every warning:

Stale 8% sample, then quattro at 9f09d60 this PR at b94f801
stale OnBattery edge at ~2s, charger at ~10s warning at ~2s (3 of 3 runs) no warning (3 of 3 runs)
charger reported at 10s not rerun no warning
never corrected not rerun warning at 30.4s

@yamz8's heads-up about #17387 holds against both diffs. This PR's gate only opens, and it relies on the 30-second timer for the first check after that. #17387 removes that timer, so if both land unchanged, a battery that is already low when the gate opens is not checked again until UPower signals some change, which could take minutes. Whichever lands second should call checkBattery() when the gate opens. Both PRs read UPower.displayDevice.ready, but for different things. This one starts its five-second window from it, and #17387 skips the check until it is set. On its own today this PR still has the timer, so nothing is missing from it as it stands.

Second opinion: Codex Medium and muse-wonder (via pi, thinking high) both read the merge and found the resolution correct, with nothing from either side lost. They found no new warning, missed warning or stuck notifiedLowBattery compared with quattro, apart from the latency already discussed. Both reached the same reading of the #17387 interaction. They could read this session's commit message, so their independence is not guaranteed. Both also noted that the gate never opens if the display device never becomes ready. quattro never warns from an unloaded device either (it reads level -1), so that is not new behaviour.

The mock reproduces the ordering, not the hardware. So this still waits on a boot on an affected machine, and the new head needs its own confirmation. @LorenGrz, if you can, please try it on the Lenovo:

git clone https://github.com/thecdrz/omarchy ~/omarchy-8284
git -C ~/omarchy-8284 checkout b94f801148a21dd170b76983837a9aca1a93c5bd
omarchy dev link ~/omarchy-8284

Then repeat the boot from #7679 a few times: let the battery die in suspend, plug in, and boot. Reply with whether "Time to recharge!" still appears. omarchy dev unlink restores the packaged version afterwards. The branch is now on current quattro, so nothing else on it is out of date. After that confirmation, this goes to the maintainer.

Comment thread test/shell.d/battery-test.sh Resolved
omarchybot and others added 2 commits November 16, 2026 20:32
They grep Service.qml and BatteryStartupGate.qml for the lines this branch added, which Omarchy's AGENTS.md rules out as restating the implementation, and they already broke once on a harmless upstream edit: a new Connections block above the gate emptied the settle-timer slice. The gate's behaviour stays covered by the gateStartupWarning model tests and the BatteryStartupGate runtime fixture.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch leaves shouldWarnLowBattery unchanged, and quattro already returns no warning off battery, so the assertion guards nothing this change adds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Codex Medium <noreply@openai.com>
@omarchybot

Copy link
Copy Markdown
Collaborator

Greptile's comment on test/shell.d/battery-test.sh is right, and I've pushed the fix as f8926c4. Omarchy's AGENTS.md rules out tests that grep the source for the lines a change just added, and the seven assertions that read Service.qml and BatteryStartupGate.qml and regex-matched them did exactly that. They had already broken once on a harmless upstream edit: the quattro merge emptied the settle-timer slice. Those assertions and the three lines that fed them are gone. The gateStartupWarning model tests and the BatteryStartupGate runtime fixture stay, and both still fail without this PR.

Codex also pointed out that the "does not warn while on AC" assertion exercises shouldWarnLowBattery, which this PR doesn't change and which already returns no warning off battery on quattro. It passes without the fix, so I removed it as f234bb0. Neither commit changes anything outside the test file.

On a disposable Omarchy VM with the shell serving the branch, battery-test.sh passed 17/17 at f234bb0. That is the earlier 25 less the eight assertions removed. battery-status-test.sh passed 9/9, battery-present-test.sh 4/4 and ./test/cli 136/136.

Second opinion: Codex Medium and muse-wonder (via pi, thinking high) both agreed with Greptile, and both found the removal complete and the remaining tests real. The AC assertion was Codex's own finding. A final Codex Medium round on f234bb0 found nothing open. Both noted that nothing now exercises the wiring inside Service.qml itself. The rules accept that trade, and covering it properly would mean loading the service against a fake UPower, which is out of proportion here. They could read this session's notes, so their agreement may not be independent.

What it waits on is unchanged: a boot on an affected machine, as asked above. The shell code at f234bb0 is identical to b94f801, so a test of either answers it. For a fresh checkout, use f234bb031d49a313aca3f0d21bc03ffb6cc0de41.

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Low battery notification can fire on boot after resuming from a suspend that drained the battery (Quickshell rewrite)

4 participants