Skip to content

Fix caffeinate -disu: POSIX-style bundled flags support#2

Merged
VertigoRay merged 2 commits into
mainfrom
fix/bundled-flags-wrapper
May 9, 2026
Merged

Fix caffeinate -disu: POSIX-style bundled flags support#2
VertigoRay merged 2 commits into
mainfrom
fix/bundled-flags-wrapper

Conversation

@VertigoRay

@VertigoRay VertigoRay commented May 9, 2026

Copy link
Copy Markdown
Owner

Summary

  • Replace [Alias('caffeinate')] with a standalone caffeinate wrapper function that supports POSIX-style bundled flags (-disu, -di, -ds, etc.) via hashtable splatting with parameter metadata introspection
  • Export caffeinate as a function instead of an alias in the manifest
  • Fix ES_USER_PRESENT (0x04) causing SetThreadExecutionState to silently return 0 (failure), leaving the system completely unprotected. -u now substitutes ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED
  • Add Set-SleepAssertion helper that checks the API return value and warns on failure
  • Re-assert execution state every 30s in all wait modes (timeout, WaitPid, indefinite) to guard against thread-state loss
  • WaitPid mode now polls with WaitForExit(30000) instead of blocking indefinitely

Fixes #1, fixes #3

Test plan

  • caffeinate -disu -t 1 -- bundled flags with timeout
  • caffeinate -di -t 2 -- partial bundled flags with timeout
  • caffeinate -d -i -s -u -t 2 -- individual switches (no longer returns 0)
  • caffeinate -d -t 1 -Verbose -- verbose pass-through
  • caffeinate -t 1 -WhatIf -- WhatIf pass-through
  • Invoke-Caffeinate -u -Timeout 2 -Verbose -- shows ES_USER_PRESENT substitution message
  • caffeinate cmd.exe /c echo hello -- command mode pass-through
  • All 46 Pester tests pass
  • PSScriptAnalyzer reports no warnings or errors
  • Manual testing confirms system stays awake

VertigoRay added 2 commits May 9, 2026 10:29
…tyle bundled flags

PowerShell does not support POSIX-style flag bundling natively, so
-disu was treated as a single unknown parameter name. Replace the
[Alias('caffeinate')] with a standalone caffeinate wrapper function
that parses bundled flags, builds a hashtable using Invoke-Caffeinate
parameter metadata to distinguish switches from value-taking params,
and splats correctly.

Fixes #1
SetThreadExecutionState returns 0 (failure) when ES_USER_PRESENT
(0x04) is included in any flag combination on modern Windows. This
caused ALL assertions to silently fail, so the system was never
actually prevented from sleeping.

Changes:
- Never pass ES_USER_PRESENT to the API; -u now substitutes
  ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED instead
- Add Set-SleepAssertion helper that checks the return value and
  warns on failure
- Re-assert execution state every 30s in all wait modes to guard
  against thread-state loss
- WaitPid mode now polls with WaitForExit(30000) instead of blocking
  indefinitely, enabling periodic re-assertion
@VertigoRay VertigoRay merged commit 7066c2a into main May 9, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant