Skip to content

Preserve keyboard brightness across hardware timeouts - #6760

Open
yashranaway wants to merge 17 commits into
omacom:quattrofrom
yashranaway:fix/preserve-timed-out-keyboard-backlight
Open

yashranaway wants to merge 17 commits into
omacom:quattrofrom
yashranaway:fix/preserve-timed-out-keyboard-backlight

Conversation

@yashranaway

@yashranaway yashranaway commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Remembers the keyboard brightness selected with up/down/cycle, including an intentional off, so hardware timeouts don't erase the level to restore after unlock. Addresses #6667.

<<< AI wording below >>>

Problem

A zero reading at lock time cannot distinguish a hardware timeout from a manual off. Saving that zero loses the user's lit setting; skipping it without tracking manual changes can restore an older non-zero setting after the user turned the light off. Relying on a previous lock-time save also misses timeouts before the first lock.

Fix

Remember successful manual keyboard brightness selections, including zero, under the hardware lock. Restore only when the saved level differs from the current one, avoiding redundant zero writes that disrupt hardware wake triggers. Share a persistent user state directory between TTY and compositor commands. If saving a manual adjustment fails, try to restore a positive prior hardware brightness and report failure.

Verification

bash test/shell.d/brightness-keyboard-test.sh passes. Changed Bash files pass syntax checks and git diff --check passes. Tests simulate brightness hardware; no Dell firmware test was available.

Review follow-up, 1 October

Normal TTY and compositor commands share a persistent preference. If persistent storage is unavailable, a writable runtime fallback stays authoritative for the rest of that session, including after storage recovers. A lit lock records the current level over stale prior-session state. If a replacement save fails but an older preference exists, blanking still works and warns that unlock will use the older level. Manual failed-save rollback only restores positive prior levels. Permission-dependent fixtures skip explicitly under root.

Focused regressions, Bash syntax, and diff checks pass. Dell firmware was not exercised on this host.

Fixes #6667

Copilot AI balanced review requested due to automatic review settings August 12, 2026 16:49

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

Preserves keyboard-backlight restore state when hardware idle timeouts set brightness to zero.

Changes:

  • Checks current brightness before saving and disabling the backlight.
  • Adds a configurable LED path and regression 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 1 out of 2 changed files in this pull request and generated no comments.

File Description
bin/omarchy-brightness-keyboard Avoids saving zero after hardware timeouts.
test/shell.d/brightness-keyboard-test.sh Tests save, timeout, and restore behavior.

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

@mmahut

mmahut commented Sep 12, 2026

Copy link
Copy Markdown

Looks like a duplicate of #10364, which fixes the same double-save in off and also covers a manually disabled backlight.

@yashranaway

ghost commented Sep 12, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for flagging this. This PR targets the hardware timeout happening before the first blank call. #10364 appears to save zero in that case. Does it also cover that scenario?

@mmahut

ghost commented Sep 13, 2026

Copy link
Copy Markdown

You're right, #10364 snapshots 0 in the timeout case and restores 0, so it doesn't cover #6667. My mistake.

Your approach has a gap the other way though: if I turn the keyboard off manually with down and then lock, off skips the save and restore brings back a stale non-zero level. And it only works at all if some earlier lock saved a non-zero value.

At lock time a reading of 0 can't tell "hardware timed out" from "user turned it off". Only the up/down/cycle path knows.

I'd rather fix it as #6667 suggests: record the last user-set level in a runtime state file on manual changes, skip set 0 when already at zero, and restore the recorded level. Would you be up for reworking this in that direction, with the manual-off and repeated-blank cases in the test too?

@yashranaway

ghost commented Sep 24, 2026

Copy link
Copy Markdown
Contributor Author

Good catch. I reworked this to remember successful manual brightness changes, including off, and added regressions for manual off and a timeout before first lock.

@greptile-apps

ghost commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no outstanding findings or new actionable issues remain.

Summary

The PR records keyboard brightness selections, including intentional off, so hardware timeouts do not erase the level to restore. It coordinates persistent and runtime state with hardware changes and adds regression tests. Since the previous review, the permission-dependent read-only-lock test has been changed to skip under root.

Reviews (12) · Last reviewed commit: "Skip runtime lock permission test as roo..." · Reviewed by Greptile

Comment thread bin/omarchy-brightness-keyboard Resolved
Comment thread bin/omarchy-brightness-keyboard Resolved
Comment thread bin/omarchy-brightness-keyboard Resolved
Comment thread bin/omarchy-brightness-keyboard Resolved
Comment thread bin/omarchy-brightness-keyboard Resolved
Comment thread bin/omarchy-brightness-keyboard Resolved
Comment thread bin/omarchy-brightness-keyboard Resolved
Comment thread bin/omarchy-brightness-keyboard Resolved
Comment thread bin/omarchy-brightness-keyboard Resolved
Comment thread bin/omarchy-brightness-keyboard Resolved
Comment thread bin/omarchy-brightness-keyboard Resolved
@omarchybot omarchybot added bug Something isn't working verified Omarchy Triage has verified that this issue is ready for final review labels Oct 13, 2026
@omarchybot

ghost commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

Reviewed at d280924 by Claude Opus 5.5, with Codex Medium as a second opinion. Codex read this pull request and its competitors and raised the firmware-key case below as a finding of its own; where it otherwise agreed with this review, that agreement is not guaranteed to be independent.

The fix works. I ran it on a disposable Omarchy worker against the real brightnessctl, driving a userspace LED (uleds) named omabot::kbd_backlight, with root writes to sysfs standing in for the firmware. On quattro, both reported paths leave the keyboard dark after unlock: a level set with the brightness keys and then timed out before the first blank, and two blanks with no wake between them. On this head, both bring back the saved level. A level turned off through down or cycle stays off. test/shell.d/brightness-keyboard-test.sh, brightness-keys-test.sh and bin-style-test.sh pass on the worker. Nothing here exercises Dell firmware, so the claim that a redundant set 0 disables Dell's wake triggers is untested.

One regression, which the maintainer should weigh. A light turned off with a key the firmware handles itself (Fn+Space on a ThinkPad, the backlight key on many Dells) never runs omarchy-brightness-keyboard, so nothing records that choice. If an earlier lock saved a lit level, the next unlock turns the keyboard back on: on the worker, light at 2, lock, unlock, firmware sets 0, lock, unlock now ends at 2, where quattro leaves it at 0. Because the preference is stored under ~/.local/state, a level from an earlier session can come back the same way. This is the cost of reading a 0 at blank time as "don't know". Whether that trade is right is the maintainer's call, not a defect to patch here. Separately, a keyboard that times out before any level has been recorded still stays dark, as the earlier review on #6667 noted.

Size. The core is proportionate: record successful up/down/cycle levels, skip a blank or restore that would only write the same 0, and restore from that record. The persistent-versus-runtime fallback in lines 29–41, and the rollback and warning paths for a failed save, exist for an unwritable state directory or a full disk. A single state file would cover the bug, and those paths are where Codex's remaining findings sit: for example, a failed save while an older, unreadable file exists blanks the keyboard and then cannot restore it.

Competing fixes. #8119, #10364, #12502 and #12960 change the same behaviour. This one is the oldest and the only one that handles a timeout before the first blank, so it is the one carried forward. #12502's choice to leave a positive brightness alone at restore would also avoid the case where a level changed by a firmware key is overwritten on wake.

./test/cli fails one test, "vscode generated theme references current theme file". That test and the code behind it are identical to the older quattro this branch forked from, and current quattro passes it on the same worker, so merging quattro in should clear it. I've linked #6667. Nothing was pushed. Labelled verified, and this now waits on the maintainer to decide on the firmware-key behaviour.

Comment thread bin/omarchy-brightness-keyboard Resolved
@yashranaway

ghost commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

I fixed the two state and failed-save findings in 999fd18 and merged current quattro. A newer TTY choice now wins over an old runtime fallback, and a failed lock-time save keeps a mismatched lit level on instead of losing it. The focused keyboard tests pass. The firmware-handled Fn-key case remains a tradeoff: a zero reading cannot distinguish an intentional off from a hardware timeout.

Comment thread bin/omarchy-brightness-keyboard Resolved
@omarchybot omarchybot removed the verified Omarchy Triage has verified that this issue is ready for final review label Oct 15, 2026
Comment thread bin/omarchy-brightness-keyboard Resolved
@omarchybot omarchybot added the verified Omarchy Triage has verified that this issue is ready for final review label Oct 15, 2026
@omarchybot

ghost commented Oct 15, 2026

Copy link
Copy Markdown
Collaborator

Re-reviewed at f13119f by Claude Opus 5.5, with Codex Medium as a second opinion over two rounds. Where Codex agreed with this review, the agreement is not guaranteed to be independent; the TTY race below is its own finding.

The fix still works. I ran it on a disposable Omarchy worker against the real brightnessctl, driving a userspace LED (uleds). A level set with the keys and timed out before the first blank, and two blanks with no wake between them, both leave the keyboard dark on current quattro and come back to the saved level on this head. A light turned off through down stays off. ./test/cli, brightness-keyboard-test.sh, brightness-keys-test.sh and bin-style-test.sh pass on the worker, so the vscode theme failure from the last review is gone with the quattro merge.

What the new commits fix. A failed lock-time save now blanks only when the saved level is readable and matches the lit one, which closes the case where unlock restored the wrong level or nothing at all. An unopenable runtime lock no longer stops the keys: on the worker, with XDG_RUNTIME_DIR read-only, up worked at f13119f and exited with Permission denied at ab5069e. The concurrency test now proves the second command is waiting on the lock before it releases the first.

Two defects remain, both in the persistent-versus-runtime fallback.

  • After storage recovers, a newer runtime file becomes the file every later choice is saved to (bin/omarchy-brightness-keyboard:54), so the persistent file never catches up. On the worker: persistent level 2, an outage records 1 in /run, storage recovers, down chooses 0; the persistent file still says 2, and after a reboot clears /run the next unlock turns the light back on at 2.
  • A command with no XDG_RUNTIME_DIR, such as one from a bare TTY, skips the runtime lock (line 47). If a fallback command is still holding that lock mid-adjustment when storage recovers, the TTY choice is written and then overwritten by the fallback's newer runtime file. This one was found by reading, not run.

I have not pushed fixes for these. Both disappear with one state file and one lock, and the rest of the change is right: record up, down and cycle levels, including zero, under a lock; skip a blank or restore that would only write the same 0; save atomically; restore from the record. The fallback serves a home directory that cannot be written. Patching it adds more code to the part that already exceeds what #6667 needs, and removing it is a design decision for you or the maintainer, not a defect fix.

Unchanged from the last review: a light turned off by a key the firmware handles itself still comes back on at unlock if an earlier lock saved a lit level. That trade-off is the maintainer's call.

Labelled verified at this head. It is not ready, because the second opinion's last round still has the two findings above. This now waits on the maintainer to decide on the firmware-key behaviour and whether the fallback stays.

Comment thread bin/omarchy-brightness-keyboard Resolved
@omarchybot omarchybot removed the verified Omarchy Triage has verified that this issue is ready for final review label Oct 15, 2026
@omarchybot omarchybot added the verified Omarchy Triage has verified that this issue is ready for final review label Oct 15, 2026
@omarchybot

ghost commented Oct 15, 2026

Copy link
Copy Markdown
Collaborator

Re-reviewed at 23fd875 by Claude Opus 5.5, with Codex Medium as a second opinion. Codex agreed with this review on every point below, and that agreement is not guaranteed to be independent.

The fix still works. On a disposable Omarchy worker, with the real brightnessctl driving a userspace LED (uleds), two cases leave the keyboard dark after unlock on current quattro but bring back the saved level on this head: a level set with the keys that times out before the first blank, and two blanks with no wake between them. A light turned off through down stays off. ./test/cli, brightness-keyboard-test.sh (36 passed), brightness-keys-test.sh and bin-style-test.sh pass on the worker.

The two new commits. Opening an existing runtime lock read-only is sound, because flock on Linux takes an exclusive lock whatever mode the file was opened in. The refusal in lines 64–67 can leave the light on at lock or unchanged at unlock, but only when a newer runtime file exists and its lock cannot be opened at all. A fallback command creates that lock before it writes the file, so neither of us found a normal session that gets there. Greptile's P1 about an inaccessible lock blocking the hooks does not hold at this head.

The two earlier fallback defects are still open:

  • Once a newer runtime file is picked at line 62, every later choice is saved to it, so the persistent file never catches up. I reproduced this on the worker. With persistent level 2, an outage that records 1 in /run, storage recovering and down choosing 0, the persistent file still says 2. Once /run is cleared, the next lock and unlock turns the light on at 2.
  • A command with no XDG_RUNTIME_DIR still skips the runtime lock (line 47), so it can race a fallback command that is in flight while storage recovers. This one was found by reading, not run.

These two commits add more handling to the fallback without settling either of them. As in the last review, one state file and one lock would remove both. Whether the fallback stays is a design question for the maintainer. It is not a defect to patch here.

Unchanged: a light turned off by a key the firmware handles itself still comes back on at unlock if an earlier lock saved a lit level. A keyboard that times out before any level was recorded still stays dark.

Nothing was pushed. Labelled verified at this head. It is not ready, because the second opinion's last round still has the two findings above and the fallback exceeds what #6667 needs. This waits on the maintainer to decide on the firmware-key behaviour and on whether the fallback stays.

@omarchybot omarchybot added verified Omarchy Triage has verified that this issue is ready for final review and removed verified Omarchy Triage has verified that this issue is ready for final review labels Oct 15, 2026
@omarchybot

ghost commented Oct 15, 2026

Copy link
Copy Markdown
Collaborator

Re-reviewed at dbf80c7 by Claude Opus 5.5, with Codex Medium as a second opinion. Codex agreed with this review on every point below, and that agreement is not guaranteed to be independent.

The new commit is sound. It changes only test/shell.d/brightness-keyboard-test.sh: the read-only runtime lock case is skipped as root, where mode 400 cannot deny a write, and otherwise it first proves the fixture really refuses a write-open. Cleanup still runs on both paths. bin/omarchy-brightness-keyboard is unchanged from 23fd875.

The fix still works. On a disposable Omarchy worker, with the real brightnessctl driving a userspace LED (uleds), a level set with the keys that times out before the first blank, and two blanks with no wake between them, leave the keyboard dark on current quattro and come back to the saved level on this head. A light turned off through down stays off. ./test/cli, brightness-keyboard-test.sh (36 passed), brightness-keys-test.sh and bin-style-test.sh pass on the worker, and brightness-keyboard-test.sh run as root now passes with that one case skipped.

Unchanged from the last review. The two fallback defects are still open: after storage recovers, a newer runtime file at line 62 keeps receiving every choice, so the persistent file never catches up and a reboot brings back the old level; and a command without XDG_RUNTIME_DIR still skips the runtime lock at line 47. A light turned off by a key the firmware handles itself still comes back on at unlock if an earlier lock saved a lit level.

Nothing was pushed. Labelled verified at this head. It is not ready, because the second opinion's last round still has the two fallback findings and the fallback goes beyond what #6667 needs. This waits on the maintainer to decide on the firmware-key behaviour and on whether the fallback stays.

@omarchybot

ghost commented Oct 31, 2026

Copy link
Copy Markdown
Collaborator

Re-checked at dbf80c7 against current quattro (e4fffc6) by Claude Opus 5.5, with Codex Medium and muse-wonder (via pi, thinking high) as second opinions.

This now conflicts with quattro, and the conflict is a design choice rather than a mechanical merge. #15908 (0c29fa5) has merged and rewrote off and restore in bin/omarchy-brightness-keyboard and test/shell.d/brightness-keyboard-test.sh, the same two files this changes. The two keep different records. On quattro, off writes a runtime record only when none exists and restore consumes it. Here, every manual change writes a lasting preference that restore keeps. Resolving the conflict means picking one of those models, or keeping both with explicit precedence. Both second opinions read it the same way. Nothing was pushed.

What #15908 covers, and what it leaves. On a disposable Omarchy worker, with the real brightnessctl driving a userspace LED (uleds), I ran the same scenarios against quattro and this head:

  • Two blanks with no wake between them: fixed on both. That half of this pull request is now on quattro.
  • A level set with the keys, then the firmware timing the light out to 0 before the lock blanks: dark on quattro, restored on this head. That is Keyboard backlight restore fails after lock (brightnessctl saves 0 after Dell stop_timeout) #6667, and quattro still does not fix it, because a manual change records nothing there and off saves the firmware's 0.
  • A light turned off by a firmware-handled key after an earlier lit lock: stays off on quattro, comes back on at unlock on this head. That regression is unchanged from the last review.

One new defect, found by both second opinions and confirmed in the source. If the persistent state directory stops being writable, lines 30-39 switch to the runtime file without reading the persistent one, and the check at line 47 is skipped in that case. So a level saved earlier with the keys is lost for as long as storage stays read-only: off after a timeout saves nothing, and restore finds no runtime record and leaves the keyboard dark. The read-only tests always seed the runtime file first, so they miss it. The two earlier fallback findings, the newer runtime file shadowing the persistent one and the missing runtime lock without XDG_RUNTIME_DIR, are still open.

verified stays off because this head no longer merges cleanly. This waits on the maintainer to decide whether #6667's timeout case should be fixed on top of #15908's record, and how. After that, the branch needs reworking onto quattro. The older competing pull requests (#8119, #10364, #12502) are still open and overlap #15908 as well.

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.

Keyboard backlight restore fails after lock (brightnessctl saves 0 after Dell stop_timeout)

4 participants