hex/src/desktop/activity.rs
Dax 341046f8d6
macos 27.3: require keystroke permission for paste, request it, and fail visibly (#2757)
* macos 27.3: require keystroke permission for paste, request it, and fail visibly

macOS 27.3 splits the old Accessibility grant into Accessibility (assistive
technologies) and System & App Protection Override (apps that control other
apps, posting ⌘V included). The Accessibility grant no longer proves Hex may
post a keystroke, so setup read Ready from AXIsProcessTrusted alone, never
asked macOS, and CGEventPost silently dropped every paste while Hex logged a
successful delivery and wrote History.

- Setup counts only the post-event preflight on 27.3+, reusing the native
  27.3 version check already used for the pane's name; below 27.3 nothing
  changes. Allow now calls CGRequestPostEventAccess so macOS can list Hex and
  post its Privacy & Security notification, during setup and in the "fix it"
  flow.
- The post-event preflight keeps its launch-time answer, so a row that stays
  untrusted after a grant reads "Needs a restart" and offers Restart Hex in
  the tour, and the Settings row does the same instead of running a tccutil
  reset that would drop the grant the person just made.
- A paste that would post a keystroke refuses before touching the clipboard
  when 27.3+ has not granted posting, with a HUD error naming System & App
  Protection Override. The shared delivery path already keeps the text for
  Paste last dictation and writes no History entry on a failed insert.

Docs note the 27.3 behavior and that no 27.3 device has verified it.

* macos 27.3: restart once per grant, explain a refused paste, and guard captures

Review fixes for the 27.3 keystroke grant.

- Restart Hex was a dead end: every stale Accessibility row on 27.3 offered
  it, so a grant that never landed looped between "Needs a restart" and a
  launch, and the tour row had no way back to the guide. The one restart is
  now recorded in onboarding-accessibility-restart (cleared when
  Accessibility reads Ready, and by a setup reset or identity change). After
  that restart a stale row falls back to the existing "Still grey? Fix it"
  reset-and-prompt flow, in the tour and in Settings > Permissions needed.
- A refused paste's HUD notice was gated by Labs > Failure notices (off by
  default), so the one failure that needs a person to act showed nothing. Its
  message is now a shared constant that the HUD explains regardless of the
  lab, through tr() with the six main languages and the four extra ones.
- Captures take the same check before touching the clipboard: Paster's
  paste_capture and the capture hand-off. The capture helper's Allow asks
  with CGRequestPostEventAccess so 27.3 can list Hex.
- The keystroke check runs before Labs > Return to app brings another app
  forward, so a refusal doesn't switch apps for a paste that cannot land.
- CGRequestPostEventAccess is a safe fn; request_post_event_access returns
  () and logs its answer instead of returning a bool no caller could use.
  The reset flow's request stays: after the restart fallback it is reachable
  again.

Docs now say what the person sees, which tests cover it, and that the
refusal itself has not run on a 27.3 device.

* macos 27.3: re-arm the restart after a reset, and explain a refused paste last

Second review fixes.

- After the one Restart Hex, "Still grey? Fix it" could never succeed: the
  marker was only cleared on Ready/setup reset/identity change, so reset ->
  grant still showed the reset row and Restart never came back. The reset
  dispatch now clears the one-restart marker for Accessibility on 27.3, so
  every cycle is reset -> grant -> one restart, user-driven. The poll re-reads
  it; Flow and Settings tests cover restart -> stale -> reset -> stale ->
  Restart again.
- A refused Paste Last only played the error tone. It now sends the same
  capture-level failure the dictation path uses, so the HUD notice explains it
  even with Labs > Failure notices off; other Paste Last failures keep the tone
  and log only. A refused capture hand-off still only logs, and the docs now
  say so instead of claiming every path shows the notice.
- Record the one restart only for the Accessibility row: the tour's
  restarts_for_post_event and the Settings stale row's restart. A Screen
  Recording or microphone Restart no longer spends it.
- Clear the marker only when accessibility_restart_done is set, and skip the
  marker stat entirely below 27.3.
2026-11-24 16:00:56 -05:00

407 lines
16 KiB
Rust

use crate::desktop::events::{
CommandOutcome, DictationPhase, EventReader, TranscriptPhase, VoiceEvent, VoiceState,
};
use crate::desktop::host::ListenerStatus;
#[derive(Clone, Debug, Default, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
pub(crate) struct DesktopActivity {
pub(crate) session_started_at: Option<u64>,
pub(crate) state: Option<VoiceState>,
pub(crate) device: Option<String>,
pub(crate) error: Option<String>,
pub(crate) last_failure: Option<(u64, String)>,
}
impl DesktopActivity {
pub(crate) fn refresh(&mut self, reader: &mut EventReader) {
if let Err(error) = reader.refresh() {
tracing::warn!(%error, path = %reader.path().display(), "could not refresh desktop activity");
self.error = Some(error.to_string());
return;
}
*self = Self::from_events(reader.events().iter().rev());
}
pub(crate) fn state_label(&self) -> Option<ListenerStatus> {
self.state.map(|state| match state {
VoiceState::Listening => ListenerStatus::Enabled,
VoiceState::Sleeping => ListenerStatus::Sleeping,
VoiceState::Dictating => ListenerStatus::Recording,
VoiceState::Transcribing => ListenerStatus::Transcribing,
VoiceState::Stopping => ListenerStatus::Disabling,
})
}
/// The newest failure at or after `started_at`, hidden once a later
/// dictation completed. Startup reads an event log that may still end in
/// the previous session, so its own start time filters that out.
#[cfg_attr(not(any(target_os = "macos", target_os = "windows")), allow(dead_code))]
pub(crate) fn visible_failure_since(
&self,
started_at: u64,
last_completed_at: Option<u64>,
) -> Option<String> {
self.last_failure
.as_ref()
.filter(|(failed_at, _)| {
*failed_at >= started_at
&& last_completed_at.is_none_or(|completed| completed < *failed_at)
})
.map(|(_, message)| message.clone())
}
fn from_events<'a>(events: impl Iterator<Item = &'a VoiceEvent>) -> Self {
let mut activity = Self::default();
for event in events {
match event {
VoiceEvent::SessionStarted { timestamp_ms } => {
activity.session_started_at = Some(*timestamp_ms);
break;
}
VoiceEvent::State { state, device, .. } if activity.state.is_none() => {
activity.state = Some(*state);
activity.device = Some(device.clone());
}
VoiceEvent::Dictation {
timestamp_ms,
phase: DictationPhase::Failed(message),
..
} if activity.last_failure.is_none() => {
activity.last_failure = Some((*timestamp_ms, message.clone()));
}
_ => {}
}
}
activity
}
}
/// When the current session's newest dictation completed. `events` is oldest
/// first, as `EventReader` keeps them.
#[cfg_attr(not(any(target_os = "macos", target_os = "windows")), allow(dead_code))]
pub(crate) fn last_completed_at<'a>(
events: impl DoubleEndedIterator<Item = &'a VoiceEvent>,
) -> Option<u64> {
for event in events.rev() {
match event {
VoiceEvent::SessionStarted { .. } => return None,
VoiceEvent::Transcript {
timestamp_ms,
phase: TranscriptPhase::Completed,
..
} => return Some(*timestamp_ms),
_ => {}
}
}
None
}
/// A known reason a dictation failed, in words a person can act on. Unknown
/// failures stay as they were logged.
pub(crate) fn failure_explanation(message: &str) -> String {
if message.contains(crate::audio::LID_CLOSED_ERROR) {
crate::desktop::i18n::tr(
"Built-in mic is off with the lid closed. Open the lid or connect a mic.",
)
.to_owned()
} else if message.contains(crate::audio::NO_MICROPHONE_ERROR) {
crate::desktop::i18n::tr("No microphone is available. Connect a microphone.").to_owned()
} else if crate::paste::refused_by_macos(message) {
crate::desktop::i18n::tr(crate::paste::POST_EVENT_REFUSED).to_owned()
} else {
message.to_owned()
}
}
/// One line describing `event` for the Activity pane.
pub(crate) fn event_summary(event: &VoiceEvent) -> String {
match event {
VoiceEvent::SessionStarted { .. } => "Hex session started".into(),
VoiceEvent::State { state, device, .. } => match state {
VoiceState::Sleeping => "Voice commands went to sleep".into(),
VoiceState::Listening => format!("Voice commands resumed on {device}"),
VoiceState::Dictating => format!("Dictation started on {device}"),
VoiceState::Transcribing => "Dictation is being transcribed".into(),
VoiceState::Stopping => "Hex is stopping".into(),
},
VoiceEvent::Transcript {
phase,
latency_ms,
text,
..
} => match phase {
TranscriptPhase::Started => "Speech recognition started".into(),
TranscriptPhase::Updated if text.is_empty() => "Heard speech so far".into(),
TranscriptPhase::Updated => format!("Heard \"{text}\" so far"),
TranscriptPhase::Completed if text.is_empty() => {
format!("Transcribed in {latency_ms} ms")
}
TranscriptPhase::Completed => format!("Heard \"{text}\" in {latency_ms} ms"),
},
VoiceEvent::Command {
heard,
command,
outcome,
..
} => {
let command = command.as_deref().unwrap_or("an unmatched command");
let heard = if heard.is_empty() { "speech" } else { heard };
match outcome {
CommandOutcome::Ignored => format!("Ignored \"{heard}\""),
CommandOutcome::Woke => format!("\"{heard}\" woke Hex"),
CommandOutcome::Slept => format!("\"{heard}\" put voice commands to sleep"),
CommandOutcome::Submitted => format!("Sent {command} for \"{heard}\""),
CommandOutcome::Executed => format!("Ran {command} for \"{heard}\""),
CommandOutcome::Failed(error) => format!("Could not run {command}: {error}"),
}
}
VoiceEvent::Dictation {
phase,
text,
processing,
reason,
..
} => {
let description = match phase {
DictationPhase::Started => "Dictation started".into(),
DictationPhase::Discarded
if reason.as_deref()
== Some(crate::desktop::events::VOICE_PROMPT_NOTHING_REASON) =>
{
crate::desktop::i18n::tr("The voice prompt asked for nothing").to_owned()
}
DictationPhase::Discarded => "A brief dictation was discarded".into(),
DictationPhase::Cancelled => "Dictation was cancelled".into(),
DictationPhase::Transcribing => "Dictation is being transcribed".into(),
DictationPhase::Pasted => text_or("Dictation paste sent", text),
DictationPhase::Copied => text_or("Dictation copied to the clipboard", text),
DictationPhase::Printed => text_or("Dictation was printed", text),
DictationPhase::Edited => text_or("Selected text was edited", text),
DictationPhase::VoiceAction => text_or("Voice Action completed", text),
DictationPhase::Logged => text_or("Journal entry was saved", text),
DictationPhase::Repasted => text_or("The last dictation was pasted again", text),
DictationPhase::MeetingPasted => text_or("New meeting transcript was pasted", text),
DictationPhase::Failed(error) => format!("Dictation failed: {error}"),
};
processing
.as_ref()
.map_or(description.clone(), |processing| {
processing.fallback.as_ref().map_or_else(
|| {
format!(
"{description} · {} profile · {} ms",
processing.profile, processing.latency_ms
)
},
|error| {
format!(
"{description} · {} profile fell back to raw after {} ms: {}",
processing.profile, processing.latency_ms, error
)
},
)
})
}
VoiceEvent::Context { application, .. } => match application {
Some(application) => format!("Context changed to {application}"),
None => "Foreground context became unavailable".into(),
},
VoiceEvent::ApiServerStarted { port, .. } => {
format!("Local API started on port {port}")
}
VoiceEvent::ApiServerStopped { .. } => "Local API stopped".into(),
VoiceEvent::ApiAuthFailed { method, path, .. } => {
format!("Rejected unauthenticated {method} {path}")
}
}
}
fn text_or(prefix: &str, text: &str) -> String {
if text.is_empty() {
prefix.into()
} else {
format!("{prefix}: \"{text}\"")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_voice_prompt_nothing_discard_says_so() {
let event = VoiceEvent::Dictation {
timestamp_ms: 1,
phase: DictationPhase::Discarded,
text: String::new(),
processing: None,
reason: Some(crate::desktop::events::VOICE_PROMPT_NOTHING_REASON.into()),
};
assert_eq!(
event_summary(&event),
crate::desktop::i18n::tr("The voice prompt asked for nothing")
);
}
#[test]
fn projects_latest_state_and_device() {
let events = [
VoiceEvent::SessionStarted { timestamp_ms: 1 },
VoiceEvent::State {
timestamp_ms: 2,
state: VoiceState::Listening,
device: "Old microphone".into(),
},
VoiceEvent::State {
timestamp_ms: 3,
state: VoiceState::Dictating,
device: "Current microphone".into(),
},
];
let activity = DesktopActivity::from_events(events.iter().rev());
assert_eq!(activity.session_started_at, Some(1));
assert_eq!(activity.state, Some(VoiceState::Dictating));
assert_eq!(activity.device.as_deref(), Some("Current microphone"));
}
#[test]
fn session_boundary_excludes_older_state_and_device() {
let mut events = vec![
VoiceEvent::State {
timestamp_ms: 1,
state: VoiceState::Dictating,
device: "Old microphone".into(),
},
VoiceEvent::SessionStarted { timestamp_ms: 2 },
];
let activity = DesktopActivity::from_events(events.iter().rev());
assert_eq!(activity.session_started_at, Some(2));
assert_eq!(activity.state, None);
assert_eq!(activity.device, None);
events.push(VoiceEvent::State {
timestamp_ms: 3,
state: VoiceState::Listening,
device: "Current microphone".into(),
});
let activity = DesktopActivity::from_events(events.iter().rev());
assert_eq!(activity.state, Some(VoiceState::Listening));
assert_eq!(activity.device.as_deref(), Some("Current microphone"));
}
#[test]
fn projects_the_latest_failure_only_from_the_current_session() {
let mut events = vec![
VoiceEvent::SessionStarted { timestamp_ms: 1 },
VoiceEvent::Dictation {
timestamp_ms: 2,
phase: DictationPhase::Failed("first".into()),
text: String::new(),
processing: None,
reason: None,
},
VoiceEvent::Dictation {
timestamp_ms: 3,
phase: DictationPhase::Failed("second".into()),
text: String::new(),
processing: None,
reason: None,
},
];
assert_eq!(
DesktopActivity::from_events(events.iter().rev()).last_failure,
Some((3, "second".into()))
);
events.push(VoiceEvent::SessionStarted { timestamp_ms: 4 });
assert!(
DesktopActivity::from_events(events.iter().rev())
.last_failure
.is_none()
);
}
#[test]
fn newest_failure_shows_until_a_later_transcript_completes() {
let activity = DesktopActivity {
last_failure: Some((5, "OpenCode generation failed".into())),
..DesktopActivity::default()
};
assert_eq!(
activity.visible_failure_since(0, Some(4)).as_deref(),
Some("OpenCode generation failed")
);
assert_eq!(activity.visible_failure_since(0, Some(6)), None);
assert_eq!(
activity.visible_failure_since(0, None).as_deref(),
Some("OpenCode generation failed")
);
}
#[test]
fn the_newest_completion_is_scoped_to_the_current_session() {
let transcript = |timestamp_ms, text| VoiceEvent::Transcript {
timestamp_ms,
phase: TranscriptPhase::Completed,
latency_ms: 10,
text,
};
let events = [
transcript(1, "Old session".into()),
VoiceEvent::SessionStarted { timestamp_ms: 2 },
];
assert_eq!(last_completed_at(events.iter()), None);
let events = [
VoiceEvent::SessionStarted { timestamp_ms: 2 },
transcript(3, "First".into()),
transcript(4, "Second".into()),
];
assert_eq!(last_completed_at(events.iter()), Some(4));
}
#[test]
fn a_startup_reader_ignores_the_previous_sessions_failure() {
let activity = DesktopActivity {
last_failure: Some((5, "Could not open microphone".into())),
..DesktopActivity::default()
};
assert_eq!(
activity.visible_failure_since(6, None),
None,
"a failure before this runtime started is the previous session's"
);
assert_eq!(
activity.visible_failure_since(5, None).as_deref(),
Some("Could not open microphone")
);
assert_eq!(
activity.visible_failure_since(5, Some(7)),
None,
"a later completed dictation still clears it"
);
}
#[test]
fn known_microphone_failures_get_an_actionable_explanation() {
assert_eq!(
failure_explanation(
"Could not open microphone: the built-in microphone is off while the lid is closed"
),
"Built-in mic is off with the lid closed. Open the lid or connect a mic."
);
assert_eq!(
failure_explanation("Could not open microphone: no microphone is available"),
"No microphone is available. Connect a microphone."
);
assert_eq!(
failure_explanation("OpenCode generation failed"),
"OpenCode generation failed"
);
assert_eq!(
failure_explanation(crate::paste::POST_EVENT_REFUSED),
crate::desktop::i18n::tr(crate::paste::POST_EVENT_REFUSED),
"a refused paste names the pane to add Hex to"
);
}
}