Repository navigation
Guard battery sysfs rate against bogus EC readings - #13009
Kristijan-K wants to merge 15 commits into
Conversation
|
No actionable defects found in the reviewed scope at Verified: With synthetic sysfs inputs and stubbed UPower reporting 7.3 W, the actual base and PR scripts produced:
Source inspection also confirms that idle detection uses magnitude, and that omitting an unavailable zero cycle count reaches the panel's missing-value fallback. Review informationTest scope: Targeted synthetic base/PR comparisons and source inspection, not execution of the stock test suite. Real battery hardware, rendered UI and UPower refresh timing were not tested. The ratio guard can also reject genuine rapid load increases while UPower lags; the practical frequency and duration of that trade-off remain unverified. Community review: Independent automated community review, unaffiliated with the Omarchy team, intended to help prepare PRs for their review. Automated AI review: Astra Medium initial inspection, independent Opus 5.5 High and GPT 6 Sol Xhigh technical assessments, then Astra synthesis against source and targeted evidence. Opus ran the synthetic comparisons; Sol's assessment was source-only. |
Automated AI review
Follow-up to the earlier automated review of Outcome: The earlier review's verified results still hold at Verified:
New: The estimate window restarts on every call
Predicted panel values (synthetic arithmetic: 12 V, constant load, 1 mAh steps, exact 5 s polling; not measured on hardware):
A gap under 5 s, including an apparent 4 s gap from whole-second timestamps, gives no estimate but still replaces the reference sample. Impact: On the targeted hardware, the open panel is likely to jump between Suggested change: Keep the reference sample until a minimum window has passed (for example 30 to 60 s), or keep a small ring of samples and use the oldest one within 600 s. UPower's own estimator requires at least 15 s of history and looks back over several samples. New: The fallback replaces an accepted sysfs readingThe fallback only checks that UPower reports 0.5 W or less (
Impact: The UPower v1.91.4 source reports 0 W for about 10 s after AC plug or unplug and after resume, so on healthy hardware a live reading can briefly give way to an older average. On the bogus-current hardware this PR targets, sysfs is rejected anyway. Suggested change: Run the estimate only when the sysfs reading was absent or rejected, as the "last resort" comment describes. New: The cached sample has no battery or state identityThe cache line holds only
Impact: It needs a multi-battery or hot-swap machine, or a charge/discharge change inside the window. The sign is never wrong, and the effect usually lasts one refresh. After the panel is reopened it can use a sample up to 600 s old. Suggested change: Write Optional test improvement: The wedge test writes the cache as Note: When Review informationTest scope: Source inspection of AI process: Opus 5.5 Medium coordination and synthesis. Independent Opus 5.5 Xhigh and GPT 6 Sol Xhigh technical assessments of this follow-up, each followed by one targeted cross-check. Opus 5.5 Medium editorial check. Opt out: To stop receiving these reviews, reply to this comment saying so. |
|
Addressed in
Full UPower version tested: |
|
Follow-up (commit Hit a second symptom of the same wedge on this machine. When The script only ever sourced time from UPower, so I added a fallback: when UPower gives no time estimate but a usable rate is available, derive it from energy over that rate — Tests cover both shapes: time derived while UPower is wedged ( Root cause note: the EC reports |
Automated AI review
Follow-up to the earlier automated review of Outcome: Three of the earlier findings are resolved at Earlier findings
Verified:
Not rechecked: The replies' statement that the full New: The 10-minute window spans suspend, AC holds and same-slot swapsThe window uses wall-clock timestamps (
A window that includes little or no drain underestimates the rate, and the new time derivation divides energy by that rate, so the remaining time is inflated by the same factor. A swapped pack instead gives a rate that is far too high. Synthetic timelines (10 W actual load, 0.5 W while suspended, 5 s polling while the panel is open; "settles" means first within 15% of the actual rate):
UPower's own estimator discards its history on resume and on AC changes. The code comment at Impact: On the hardware this PR targets (where the UPower wedge persists across resume), opening the lid after a suspend, or unplugging after a hold at a charge limit, can show a rate several times too low with a remaining time several times too long, for several minutes. At Suggested change: A shorter maximum window, for example Optional test improvement: No test checks which sample is used as the reference. A variant that uses the youngest sample at least 30 s old passes the current test file. New: Cached charge and voltage fields are not validated before arithmeticThe cache reader checks only the timestamp (
Impact: The cache normally sits in the user's own Suggested change: Add New: Time to full uses the truncated capacityThe charging branch computes Impact: Near full charge, the time to full is too short. It appears in the Suggested change: Parse energy-full separately, without Note (panel, base behavior): While charging below 99% with UPower's rate at 0 W, the power panel does not show the script's estimated rate or derived time to full. Note: Open PR #13111 addresses the same bogus-current symptom with a different policy. It accepts a plausible sysfs rate far above UPower's (60 W against 8 W in its test), where this PR's 3x guard keeps UPower's 8 W. Both add a guard in the same sysfs-rate block and change the same test file. Note (unchanged): When Review informationTest scope: Source inspection of AI process: Opus 5.5 Medium coordination and synthesis, Opus 5.5 Xhigh technical review and final fact check, GPT 6 Sol Xhigh search for related issues, Opus 5.5 Medium editorial check. Opt out: To stop receiving these reviews, reply to this comment saying so. |
|
All three new findings addressed in 1. 10-minute window spanned suspend, AC holds and same-slot swaps
2. Cache fields reached shell arithmetic unvalidated
3. Time to full used the truncated capacity
Tests. Added coverage for the oldest-sample reference, gap reset, non-numeric cache fields and untruncated capacity. I checked each new assertion is load-bearing by removing the guard it covers and confirming the test goes red — the first attempt at the cache-field test passed without the guard because the 60s gap was dropping the line via the gap reset, so that sample is now recent enough to isolate the validation.
On #13111: agreed it overlaps — same sysfs-rate block and same test file, different policy on plausible-but-high sysfs values (its 60W vs UPower 8W case, where the 3x guard here keeps UPower). Happy to rebase on whichever lands first so the other only has to reconcile the policy difference. |
Automated AI review
Outcome: The 45 s reset also fires after the panel is closed for over 45 s, so quick glances show no estimate. Earlier findings (review, rechecked at Reopening the panel after 45 s restarts the estimateWhen UPower is wedged and sysfs rejected, a reopen after 45 s shows Suggested change: Keep history across ordinary closes (for example resetting on resume, not elapsed time), or document this cost at Reproducer (sandboxed run at cd81d77 and 05f1edc)Reproducer: the 45 s reset also discards history between panel openingsReviewed head: This is the behaviour the author accepted ("0W for the first 30s after any PR-style snippetAppended to # Steady 7.5 W (19 mAh in 110 s), last refresh 60 s ago, panel reopened now.
printf 'BAT0 discharging 1799999950 4619000 12000000\n' >"$tmp_dir/reopen.cache"
printf 'BAT0 discharging 1800000000 4610000 12000000\n' >>"$tmp_dir/reopen.cache"
reopen_output=$(OMARCHY_POWER_SUPPLY_PATH="$tmp_dir/wedge/power" OMARCHY_BATTERY_CACHE="$tmp_dir/reopen.cache" PATH="$tmp_dir/wedge/bin:$PATH" "$ROOT/bin/omarchy-battery-status" --shell)
printf 'REPRO reopen: %s | %s\n' "$(grep $'^rate\t' <<<"$reopen_output")" "$(grep $'^time\t' <<<"$reopen_output")"Actual output (sandboxed run): Timelines (actual script, stubbed UPower at 0 W and clock, 12 V, 1 mAh steps, 7.5 W load)Panel open 20 s (refresh every 5 s), closed, then reopened:
Single invocations (the
Periodic caller, 600 s at 7.5 W:
The base branch never estimates. With the same fixture (UPower 0 W, sysfs DetailsVerified:
Limit: Interruptions of 45 s or less are still measured across (40 s suspend at 10 W: Suggested change, other options: A background sampler would also keep history across closes. An AC hold or pack swap while the panel is closed leaves nothing a non-running script can observe, so each option has its own cost. Optional test improvement: The identity and stale-cache tests seed samples older than 45 s, which the reset would discard anyway. Mutants without the identity check, with a 600 s window or without the voltage check pass the head test file; the block below catches each. Reproducer: tests no longer isolate three cache guards at cd81d77Reviewed head: 1. Mutants that pass the unmodified
|
| Mutant | Change | Result |
|---|---|---|
| no identity check | delete line 135 ([[ $sample_battery == "$native_path" && $sample_state == "$state" ]] || continue) |
exit 0, ok - battery status owns capacity and remaining calculations |
| 600 s window | maximum_window=120 becomes maximum_window=600 |
exit 0, same ok line |
| no window filter | delete line 138 ((( now_ts - sample_ts <= maximum_window )) || continue) |
exit 0, same ok line |
| charge-only check | line 137 checks only $sample_charge |
exit 0, same ok line |
The same identity mutant applied to the 05f1edc script, run against the
05f1edc test file, fails:
not ok - battery status ignores samples from another battery
not ok - battery status ignores samples from another state
Why: the identity and stale-cache tests seed samples 60 s and 1060 s old
(1800000000, 1799999000 with the clock stubbed at 1800000060). The new
reset_gap=45 (line 148) discards any history whose newest sample is older
than 45 s, so these samples would be discarded even without the identity or
window check (at cd81d77 those checks, lines 135 and 138, still run first).
The non-numeric test (line 127) seeds a sample 5 s old, below the 30 s
minimum_window, so the estimate loop never evaluates it. Without the numeric
guard, its rate 0W assertion (line 129) still passes; only the carry-forward
check (line 130) fails:
not ok - battery status drops non-numeric cache samples
2. Test additions that do isolate them
Insert before the final rg check in test/shell.d/battery-status-test.sh
(reuses the file's wedge fixture: UPower 0 W, sysfs current -65 A,
charge_now 4600000, 12 V, date +%s = 1800000060):
# Identity samples 35s old, so the 45s gap reset cannot be what drops them.
printf 'BAT1 discharging 1800000025 4609000 12000000\n' >"$tmp_dir/other35.cache"
other35_output=$(OMARCHY_POWER_SUPPLY_PATH="$tmp_dir/wedge/power" OMARCHY_BATTERY_CACHE="$tmp_dir/other35.cache" PATH="$tmp_dir/wedge/bin:$PATH" "$ROOT/bin/omarchy-battery-status" --shell)
grep -Fx $'rate\t0W' <<<"$other35_output" >/dev/null || fail "battery status ignores a 35s-old sample from another battery" "$other35_output"
printf 'BAT0 charging 1800000025 4609000 12000000\n' >"$tmp_dir/state35.cache"
state35_output=$(OMARCHY_POWER_SUPPLY_PATH="$tmp_dir/wedge/power" OMARCHY_BATTERY_CACHE="$tmp_dir/state35.cache" PATH="$tmp_dir/wedge/bin:$PATH" "$ROOT/bin/omarchy-battery-status" --shell)
grep -Fx $'rate\t0W' <<<"$state35_output" >/dev/null || fail "battery status ignores a 35s-old sample from another state" "$state35_output"
# A sample chain longer than 120s; the reference must be the oldest sample inside 120s.
printf 'BAT0 discharging 1799999920 4620000 12000000\n' >"$tmp_dir/window.cache"
printf 'BAT0 discharging 1799999960 4610000 12000000\n' >>"$tmp_dir/window.cache"
printf 'BAT0 discharging 1800000000 4606000 12000000\n' >>"$tmp_dir/window.cache"
printf 'BAT0 discharging 1800000040 4602000 12000000\n' >>"$tmp_dir/window.cache"
window_output=$(OMARCHY_POWER_SUPPLY_PATH="$tmp_dir/wedge/power" OMARCHY_BATTERY_CACHE="$tmp_dir/window.cache" PATH="$tmp_dir/wedge/bin:$PATH" "$ROOT/bin/omarchy-battery-status" --shell)
grep -Fx $'rate\t4.3W' <<<"$window_output" >/dev/null || fail "battery status measures within the 120s maximum window" "$window_output"
# A sample old enough to reach the arithmetic, with its voltage missing.
printf 'BAT0 discharging 1800000025 4609000\n' >"$tmp_dir/novolt.cache"
novolt_output=$(OMARCHY_POWER_SUPPLY_PATH="$tmp_dir/wedge/power" OMARCHY_BATTERY_CACHE="$tmp_dir/novolt.cache" PATH="$tmp_dir/wedge/bin:$PATH" "$ROOT/bin/omarchy-battery-status" --shell)
grep -Fx $'rate\t0W' <<<"$novolt_output" >/dev/null || fail "battery status ignores a sample without a voltage" "$novolt_output"3. Actual results (sandboxed runs, continue-on-failure copy of the test)
| Script | Failing assertions (rate printed) |
|---|---|
cd81d77 unchanged |
none; stock run exits 0 |
| no identity check | 35s-old sample from another battery (rate 11.1W); from another state (rate 11.1W) |
| 600 s window | 120s maximum window (rate 6.2W) |
| no window filter | 120s maximum window (rate 6.2W) |
| charge-only check | sample without a voltage (rate 5.6W) |
| no numeric check | drops non-numeric cache samples; sample without a voltage (rate 5.6W) |
Expected values: 9000 uAh over 35 s at 12 V is 11.1 W; 10000 uAh over 100 s
is 4.3 W, 20000 uAh over 140 s is 6.2 W; a missing voltage halves the average
voltage, giving 5.6 W.
Note: Open PR #13111 still conflicts in both files.
Review information
Test scope: Source and sandbox tests (stubbed UPower and clock) at cd81d77; no hardware, suspend or rendered panel.
AI process: Opus 5.5 Medium coordination and synthesis, Opus 5.5 Xhigh technical review and final fact check, GPT 6 Sol Xhigh search for related issues, Opus 5.5 Medium editorial check.
Opt out: To stop receiving these reviews, reply to this comment saying so.
|
Both findings valid, addressed in 1. The 45s reset also fired on ordinary panel closes The critique was right, and my "0W for the first 30s after any gap" framing understated it: a script that only runs while the panel is open cannot tell a closed panel from a suspend, so a normal close counted as a gap. Reproduced at Fix: removed
2. Three tests no longer isolated their guards Confirmed: removing the identity check left the suite passing. The identity/state seeds were 60s old and the stale seed 1060s old, so the gap reset discarded them before the guards mattered. Now that the reset is gone they isolate again, and I hardened them anyway:
Verified every guard is load-bearing with six mutants — identity removed,
#13111 still conflicts in both files; the offer to rebase on whichever lands first stands. |
[Medium impact] The PR appears safe to merge; no new actionable issues remain. SummaryGuards live battery readings and adds a charge-history fallback when UPower cannot supply a useful rate.
No new actionable issues found. The earlier threads are addressed by separate temporary files, future-date checks, cycle-count tests, clearing history during observed holds, and a scratch cache for every test run.
|
|
Thanks — all three greptile findings were valid, addressed in 1. Concurrent cache writes can collide Fair point: the panel polls every 5s and the low-battery notification can invoke the script independently, so two runs could interleave on the shared 2. Future samples never expire Correct — a backwards wall-clock correction made 3. Cycle suppression lacks test coverage Added both cases: sysfs Both new guards were verified load-bearing by removing them and confirming the named assertions go red. |
|
Linking related work so this can be compared:
The estimator adds a fair amount to |
|
Looked at this at Two models classified it separately: Claude Opus 5.5 read it as Two other things stand regardless of the classification: GitHub reports the branch as conflicting with This waits on the maintainer to decide what it is. |
…s-sysfs-rate # Conflicts: # bin/omarchy-battery-status
|
Merged Dropped as duplicate
Kept and reshaped
Tests rebuilt on upstream's fixtures. The old test file was written against fixtures The PR body now covers the estimator, which it never did. If the estimator is what makes this hard to classify, say so and I will split it into its own PR; the guard and the cycle display stand alone as the bug fix. On #13111: still the same overlap in both files, and I will rebase on whichever lands first. |
|
Valid, and fixed in The earlier attempt at this reset on elapsed time, which also fired when the panel was simply closed for more than the window, so a quick glance showed
Test added: seed two discharge samples, run once with
|
|
Classified again at Claude Opus 5.5 read it as The merge cleared the conflict with This waits on the maintainer: to decide what it is, whether to split it, and which of this and #13111 carries the guard. |
…s-sysfs-rate # Conflicts: # bin/omarchy-battery-status # test/shell.d/battery-status-test.sh
|
Merged The test file needed a rebuild rather than a merge: #16272 added a full-battery case in the same place as the guard cases, so the two fixtures interleaved. The file is now upstream's at
Nothing else changed: this still waits on the maintainer for what it is, whether to split it, and which of this and #13111 carries the guard. |
|
Right, and worse than cosmetic: fixed in The suite now exports a scratch That also removes the trap for future fixtures: any new test that hits a
|
|
Classified again at Claude Opus 5.5 read it as #13111 is still open against the same sysfs-rate block. This still waits on the maintainer: to decide what it is, whether to split the estimator out as offered, and which of this and #13111 carries the guard. |
…s-sysfs-rate # Conflicts: # test/shell.d/battery-status-test.sh
|
Merged
|
|
Classified again at Claude Opus 5.5 read it as What is new: the branch conflicts with This waits on the author to resolve the conflict on top of |
…s-sysfs-rate # Conflicts: # bin/omarchy-battery-status
|
Merged Conflict was in
Verified:
Leaving the decision to the maintainer as requested: #13111 overlap in the sysfs-rate block stands, and I can split the guard+magnitude display from the estimator into its own PR if that makes classification easier. |
|
Want your agent to iterate on Greptile's feedback? Try greploops. |
On some machines the EC reports a bogus but numeric current. My MSI Prestige 14 Flip AI+ (BAT1 BIF0_9) reports current_now=-65A for a ~7.5W discharge, so the panel showed
Discharging -1124Wwhile UPower correctly reported 7.5W.The sysfs override only guarded readability, so any numeric value clobbered the good UPower rate. This now:
current_now, the normal discharging sign),-1124W),cycle_count0 when UPower reportscharge-cyclesN/A so the panel shows — instead of a misleading 0.Fallback when UPower wedges entirely. On the same hardware UPower can sit at
energy-rate: 0 Wfor hours, and it then drops itstime to emptyline as well.charge_nowstill moves, so the script estimates the rate from charge deltas across a 30–120s history kept in$XDG_RUNTIME_DIR(orOMARCHY_BATTERY_CACHE). Each cache line records the battery and state, so a sample from another battery, another state, the future, or outside the window is never measured against the current one. The estimate only runs when neither sysfs nor UPower supplied a usable rate. Time left comes from the rate the script settled on, so the two always agree. Batteries that exposeenergybut nochargecarry no sample and keep UPower's rate.Scope. Rebasing onto current
quattrodropped the parts this PR had duplicated:#15285now derives time left from the rate shown and#15326formats durations throughomarchy-i18n, so the branch's own time derivation and its untruncated-capacity fix are gone. What remains is the plausibility guard, the cycle-count suppression and the charge-delta estimator. If the estimator makes this hard to classify, say so and I will split it into its own PR — the guard and cycle display stand alone as a bug fix.Complements #12649/#12755 (those cover failed/empty reads); this covers bogus-but-numeric reads. Overlaps #13111: same sysfs block and same test file, with a different policy for high-but-plausible sysfs values (it accepts a plausible 60W against UPower's 8W; the 3x guard here keeps UPower's 8W). Happy to rebase on whichever lands first.
Verified with the extended
test/shell.d/battery-status-test.sh(bogus -65A, non-numeric, sane negative discharge, plausible-but-high, estimator window/identity/validation, cycle suppression) plus live hardware: bogus -1113W discharge now shows 7.5W, and a sane 29.1W charge still uses the live sysfs value.