Land release blocker fixes and workspace cleanup
Some checks failed
check / cargo check (macos-latest) (push) Has been cancelled
check / cargo check (ubuntu-22.04) (push) Has been cancelled
check / cargo check (windows-latest) (push) Has been cancelled
check / svelte build + lint (push) Has been cancelled

This commit is contained in:
2026-04-23 00:16:09 +01:00
parent d7363cc913
commit 9b0067b4c0
36 changed files with 1529 additions and 418 deletions

View File

@@ -21,7 +21,17 @@
//! may still decide to idle us. We log when that happens so the
//! diagnostics bundle has a breadcrumb.
use std::collections::HashMap;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Mutex, OnceLock};
#[derive(Debug, Clone)]
pub struct PowerAssertionSnapshot {
pub id: usize,
pub reason: &'static str,
pub backend: &'static str,
pub acquired: bool,
}
/// Handle for a single power assertion. Dropping it releases the
/// assertion. Holders are expected to keep it alive in a field for
@@ -32,12 +42,30 @@ pub struct PowerAssertion {
#[allow(dead_code)]
id: usize,
reason: &'static str,
backend: &'static str,
acquired: bool,
#[cfg(target_os = "macos")]
activity: Option<objc_bridge::ActivityHandle>,
}
static NEXT_ID: AtomicUsize = AtomicUsize::new(1);
fn assertion_registry() -> &'static Mutex<HashMap<usize, PowerAssertionSnapshot>> {
static REGISTRY: OnceLock<Mutex<HashMap<usize, PowerAssertionSnapshot>>> = OnceLock::new();
REGISTRY.get_or_init(|| Mutex::new(HashMap::new()))
}
pub fn active_assertions_snapshot() -> Vec<PowerAssertionSnapshot> {
let mut snapshots = assertion_registry()
.lock()
.unwrap()
.values()
.cloned()
.collect::<Vec<_>>();
snapshots.sort_by_key(|snapshot| snapshot.id);
snapshots
}
impl PowerAssertion {
/// Begin a power assertion for the given reason. On macOS this
/// pins beginActivityWithOptions; on Linux/Windows it logs only
@@ -47,12 +75,16 @@ impl PowerAssertion {
#[cfg(target_os = "macos")]
let activity = objc_bridge::begin_activity(reason).ok();
#[cfg(target_os = "macos")]
let backend = "macos";
#[cfg(target_os = "macos")]
let acquired = activity.is_some();
#[cfg(target_os = "macos")]
if activity.is_none() {
eprintln!(
"[power] macOS App Nap guard could not begin activity for reason '{reason}'"
);
if acquired {
eprintln!("[power] began macOS App Nap guard #{id} for reason '{reason}'");
} else {
eprintln!("[power] macOS App Nap guard could not begin activity for reason '{reason}'");
}
#[cfg(not(target_os = "macos"))]
@@ -63,9 +95,26 @@ impl PowerAssertion {
let _ = reason;
}
#[cfg(not(target_os = "macos"))]
let backend = "noop";
#[cfg(not(target_os = "macos"))]
let acquired = false;
assertion_registry().lock().unwrap().insert(
id,
PowerAssertionSnapshot {
id,
reason,
backend,
acquired,
},
);
Self {
id,
reason,
backend,
acquired,
#[cfg(target_os = "macos")]
activity,
}
@@ -77,64 +126,83 @@ impl Drop for PowerAssertion {
#[cfg(target_os = "macos")]
if let Some(handle) = self.activity.take() {
objc_bridge::end_activity(handle);
eprintln!(
"[power] ended macOS App Nap guard #{} for reason '{}'",
self.id, self.reason
);
}
assertion_registry().lock().unwrap().remove(&self.id);
let _ = (self.reason, self.id);
let _ = (self.backend, self.acquired);
}
}
#[cfg(target_os = "macos")]
mod objc_bridge {
//! Placeholder for the NSProcessInfo App-Nap bridge.
//!
//! A proper implementation calls:
//! `NSProcessInfo *info = [NSProcessInfo processInfo];`
//! `id activity = [info beginActivityWithOptions:
//! (NSActivityUserInitiated | NSActivityLatencyCritical)
//! reason:reasonNSString];`
//! and retains the returned object until `end_activity`.
//!
//! This workstream ships the PowerAssertion RAII guard + wiring
//! so `commands/live.rs` and `commands/llm.rs` can adopt it today
//! (matters on macOS, no-op elsewhere). The actual `objc2` bridge
//! lands in a follow-up commit that can introduce `objc2` +
//! `objc2-foundation` without touching the rest of the workspace
//! in the same change.
//!
//! Until then, `begin_activity` returns Err; callers (`begin()`)
//! log a warning but keep running, so recording continues to work
//! as today — the gap is just the App-Nap protection, not the
//! recording itself.
use objc2::rc::Retained;
use objc2::runtime::ProtocolObject;
use objc2_foundation::{NSActivityOptions, NSObjectProtocol, NSProcessInfo, NSString};
pub struct ActivityHandle {
#[allow(dead_code)]
retained: *mut std::ffi::c_void,
activity: Retained<ProtocolObject<dyn NSObjectProtocol>>,
}
// SAFETY: The pointer is opaque to Rust; Foundation manages its
// lifetime via retain/release. We never dereference it directly.
unsafe impl Send for ActivityHandle {}
pub fn begin_activity(_reason: &str) -> Result<ActivityHandle, String> {
Err("macOS App Nap bridge not yet wired — objc2 integration tracked for a follow-up".into())
pub fn begin_activity(reason: &str) -> Result<ActivityHandle, String> {
let process_info = NSProcessInfo::processInfo();
let reason = NSString::from_str(reason);
let options = NSActivityOptions::UserInitiated | NSActivityOptions::LatencyCritical;
let activity = process_info.beginActivityWithOptions_reason(options, &reason);
Ok(ActivityHandle { activity })
}
pub fn end_activity(_handle: ActivityHandle) {}
pub fn end_activity(handle: ActivityHandle) {
let process_info = NSProcessInfo::processInfo();
unsafe {
process_info.endActivity(&handle.activity);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::{Mutex, MutexGuard, OnceLock};
fn power_test_guard() -> MutexGuard<'static, ()> {
static TEST_GUARD: OnceLock<Mutex<()>> = OnceLock::new();
TEST_GUARD.get_or_init(|| Mutex::new(())).lock().unwrap()
}
fn clear_assertion_registry() {
assertion_registry().lock().unwrap().clear();
}
#[test]
fn power_assertion_is_a_no_op_drop() {
let _guard = power_test_guard();
clear_assertion_registry();
let guard = PowerAssertion::begin("test-reason");
let snapshots = active_assertions_snapshot();
assert!(snapshots.iter().any(|snapshot| snapshot.id == guard.id));
drop(guard);
assert!(active_assertions_snapshot().is_empty());
}
#[test]
fn multiple_assertions_get_unique_ids() {
let _guard = power_test_guard();
clear_assertion_registry();
let a = PowerAssertion::begin("a");
let b = PowerAssertion::begin("b");
assert_ne!(a.id, b.id);
let snapshots = active_assertions_snapshot();
assert_eq!(snapshots.len(), 2);
assert_eq!(snapshots[0].reason, "a");
assert_eq!(snapshots[1].reason, "b");
drop(a);
drop(b);
assert!(active_assertions_snapshot().is_empty());
}
}