Files
codex/codex-rs/utils/sleep-inhibitor/src/lib.rs
Michael Bolin 61dfe0b86c chore: clean up argument-comment lint and roll out all-target CI on macOS (#16054)
## Why

`argument-comment-lint` was green in CI even though the repo still had
many uncommented literal arguments. The main gap was target coverage:
the repo wrapper did not force Cargo to inspect test-only call sites, so
examples like the `latest_session_lookup_params(true, ...)` tests in
`codex-rs/tui_app_server/src/lib.rs` never entered the blocking CI path.

This change cleans up the existing backlog, makes the default repo lint
path cover all Cargo targets, and starts rolling that stricter CI
enforcement out on the platform where it is currently validated.

## What changed

- mechanically fixed existing `argument-comment-lint` violations across
the `codex-rs` workspace, including tests, examples, and benches
- updated `tools/argument-comment-lint/run-prebuilt-linter.sh` and
`tools/argument-comment-lint/run.sh` so non-`--fix` runs default to
`--all-targets` unless the caller explicitly narrows the target set
- fixed both wrappers so forwarded cargo arguments after `--` are
preserved with a single separator
- documented the new default behavior in
`tools/argument-comment-lint/README.md`
- updated `rust-ci` so the macOS lint lane keeps the plain wrapper
invocation and therefore enforces `--all-targets`, while Linux and
Windows temporarily pass `-- --lib --bins`

That temporary CI split keeps the stricter all-targets check where it is
already cleaned up, while leaving room to finish the remaining Linux-
and Windows-specific target-gated cleanup before enabling
`--all-targets` on those runners. The Linux and Windows failures on the
intermediate revision were caused by the wrapper forwarding bug, not by
additional lint findings in those lanes.

## Validation

- `bash -n tools/argument-comment-lint/run.sh`
- `bash -n tools/argument-comment-lint/run-prebuilt-linter.sh`
- shell-level wrapper forwarding check for `-- --lib --bins`
- shell-level wrapper forwarding check for `-- --tests`
- `just argument-comment-lint`
- `cargo test` in `tools/argument-comment-lint`
- `cargo test -p codex-terminal-detection`

## Follow-up

- Clean up remaining Linux-only target-gated callsites, then switch the
Linux lint lane back to the plain wrapper invocation.
- Clean up remaining Windows-only target-gated callsites, then switch
the Windows lint lane back to the plain wrapper invocation.
2026-03-27 19:00:44 -07:00

114 lines
3.4 KiB
Rust

//! Cross-platform helper for preventing idle sleep while a turn is running.
//!
//! Platform-specific behavior:
//! - macOS: Uses native IOKit power assertions instead of spawning `caffeinate`.
//! - Linux: Spawns `systemd-inhibit` or `gnome-session-inhibit` while active.
//! - Windows: Uses `PowerCreateRequest` + `PowerSetRequest` with
//! `PowerRequestSystemRequired`.
//! - Other platforms: No-op backend.
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
mod dummy;
#[cfg(target_os = "linux")]
mod linux_inhibitor;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "windows")]
mod windows_inhibitor;
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
use dummy as imp;
#[cfg(target_os = "linux")]
use linux_inhibitor as imp;
#[cfg(target_os = "macos")]
use macos as imp;
#[cfg(target_os = "windows")]
use windows_inhibitor as imp;
/// Keeps the machine awake while a turn is in progress when enabled.
#[derive(Debug)]
pub struct SleepInhibitor {
enabled: bool,
turn_running: bool,
platform: imp::SleepInhibitor,
}
impl SleepInhibitor {
pub fn new(enabled: bool) -> Self {
Self {
enabled,
turn_running: false,
platform: imp::SleepInhibitor::new(),
}
}
/// Update the active turn state; turns sleep prevention on/off as needed.
pub fn set_turn_running(&mut self, turn_running: bool) {
self.turn_running = turn_running;
if !self.enabled {
self.release();
return;
}
if turn_running {
self.acquire();
} else {
self.release();
}
}
fn acquire(&mut self) {
self.platform.acquire();
}
fn release(&mut self) {
self.platform.release();
}
/// Return the latest turn-running state requested by the caller.
pub fn is_turn_running(&self) -> bool {
self.turn_running
}
}
#[cfg(test)]
mod tests {
use super::SleepInhibitor;
#[test]
fn sleep_inhibitor_toggles_without_panicking() {
let mut inhibitor = SleepInhibitor::new(/*enabled*/ true);
inhibitor.set_turn_running(/*turn_running*/ true);
assert!(inhibitor.is_turn_running());
inhibitor.set_turn_running(/*turn_running*/ false);
assert!(!inhibitor.is_turn_running());
}
#[test]
fn sleep_inhibitor_disabled_does_not_panic() {
let mut inhibitor = SleepInhibitor::new(/*enabled*/ false);
inhibitor.set_turn_running(/*turn_running*/ true);
assert!(inhibitor.is_turn_running());
inhibitor.set_turn_running(/*turn_running*/ false);
assert!(!inhibitor.is_turn_running());
}
#[test]
fn sleep_inhibitor_multiple_true_calls_are_idempotent() {
let mut inhibitor = SleepInhibitor::new(/*enabled*/ true);
inhibitor.set_turn_running(/*turn_running*/ true);
inhibitor.set_turn_running(/*turn_running*/ true);
inhibitor.set_turn_running(/*turn_running*/ true);
inhibitor.set_turn_running(/*turn_running*/ false);
}
#[test]
fn sleep_inhibitor_can_toggle_multiple_times() {
let mut inhibitor = SleepInhibitor::new(/*enabled*/ true);
inhibitor.set_turn_running(/*turn_running*/ true);
inhibitor.set_turn_running(/*turn_running*/ false);
inhibitor.set_turn_running(/*turn_running*/ true);
inhibitor.set_turn_running(/*turn_running*/ false);
}
}