fix(pmset): Skip unsupported settings based on hardware capabilities - #5
Conversation
Query `pmset -g cap` to determine which settings each power source actually supports, then filter out unsupported keys during both check and apply. This prevents false drift detection and avoids passing invalid arguments on machines that lack certain power features (e.g., desktops with no battery or hardware without autopoweroff).
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📜 Recent review details🔇 Additional comments (11)
📝 WalkthroughWalkthrough
ChangesPMSet Capability-Aware Filtering
🎯 3 (Moderate) | ⏱️ ~20 minutes
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Pull request overview
This PR updates PmsetSetting to probe pmset -g cap and use those per-power-source capabilities to ignore unsupported pmset keys during drift checks and when applying settings, preventing perpetual “drift” and avoiding attempts to set keys that aren’t supported on the current hardware/power configuration.
Changes:
- Parse
pmset -g capinto a per-section capabilities map and use it to filter settings. - Refactor drift detection into a helper that skips unsupported keys.
- Add tests covering capability parsing and supported/unsupported drift + apply filtering behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
Sources/Astrolabe/Steps/Sys/PmsetSetting.swift |
Adds capability probing + helpers; filters unsupported keys during check()/apply(). |
Tests/AstrolabeTests/AstrolabeTests.swift |
Adds unit tests for capability parsing and filtering behavior. |
Comments suppressed due to low confidence (1)
Sources/Astrolabe/Steps/Sys/PmsetSetting.swift:44
- When
source == .all,apply()still invokespmsetwith-a, but the capability filtering is per power-source section. A key that is supported in only one section (e.g. Battery Power) will still be included insettingsToApply, and passing it via-acan reintroduce the exact “not supported on this machine” warnings/errors this PR aims to avoid for the other sections. Consider applying per power source when.all(run separatepmset -b/-c/-ucommands with per-section filtered settings), or otherwise change the filtering/command strategy so unsupported-by-section keys are never sent topmsetfor that section.
public func apply() async throws {
let capabilitiesOutput = try? await captureOutput("/usr/bin/pmset", ["-g", "cap"])
let capabilities = capabilitiesOutput.map(Self.parseCapabilities) ?? [:]
let settingsToApply = Self.supportedSettings(settings, for: source, capabilities: capabilities)
guard !settingsToApply.isEmpty else { return }
var arguments = [source.rawValue]
for setting in settingsToApply {
arguments.append(setting.key)
arguments.append(String(setting.intValue))
}
try await run("/usr/bin/pmset", arguments)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
pmset -g capper source and filter the desired settings down to those the hardware actually supports before checking or applying.check()no longer reports drift for unsupported keys (e.g.autopoweroffon Apple Silicon,standbyon hardware that doesn't expose it).apply()no longer feeds unsupported keys topmset, which would otherwise fail or surface "not supported on this machine" warnings.Motivation
pmsetexposes a different capability set per power source and per machine. Asking it to set a key that isn't listed underpmset -g capproduces a no-op (best case) or a hard error (worst case), and the nextcheck()would re-read the original value and report drift forever — re-triggeringapply()on every loop tick. Filtering by the live capability set keepsPmsetSettingconverged on heterogeneous hardware (Apple Silicon laptops, Intel desktops, machines on UPS, etc.) without per-target conditionals in user code.Changes
Sources/Astrolabe/Steps/Sys/PmsetSetting.swiftpmset -g capinto[section: Set<key>]. ExtractsettingsAreSatisfied,supportedSettings,targetSections,targetSectionNames, andisSupportedhelpers.check()ignores unsupported keys;apply()filters them out and short-circuits if nothing remains. Fall back to the original behavior when the capability probe fails.Tests/AstrolabeTests/AstrolabeTests.swiftparseCapabilities, drift filtering when a setting is unsupported, drift still firing when a supported key is missing, andapplyfiltering down to supported settings.Test plan
swift buildsucceeds.swift testpasses (newpmsetParseCapabilities,pmsetUnsupportedSettingsDoNotForceDrift,pmsetSupportedMissingSettingStillDrifts,pmsetApplyFiltersUnsupportedSettings).autopoweroff/standbyis silently skipped instead of producing recurring drift.Need help on this PR? Tag
@codesmithwith what you need. Autofix is disabled.Summary by CodeRabbit
Bug Fixes
Tests