feat(nudges): Phase 6 — Margot soft-touch nudges via frontend nudge bus
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

Frontend-owned nudge bus that consumes in-app signals Corbie already
produces, applies suppression, and fans out to OS notification + an
optional TTS read-aloud. OS-wide keyboard/window activity detection
stays deferred per the revised roadmap — the plan before rewrite
would have been brittle on Wayland, permission-heavy on macOS, and
low-quality everywhere.

Triggers (v1, all in-app signals):
- inactivity_with_active_timer — timer running, window blurred ≥ 90 s,
  at least 60 s into the timer.
- pending_morning_triage — ritual enabled, past 10:00 local, last
  shown ≠ today. Polls every 5 min while focused.
- micro_step_idle — micro-step decomposition created, no child step
  or parent task completed within 15 min.

Suppression:
- Respects nudgesEnabled + nudgesMuted.
- No nudge while the app has focus (document.hasFocus).
- Hard cap 3 per rolling hour.
- Permission requested via @tauri-apps/plugin-notification on first
  delivery; denial is silently respected.

Rust side:
- tauri-plugin-notification registered + ACL entries on the main-
  window capability only (secondary windows can't fire nudges).
- commands::nudges::deliver_nudge — thin wrapper, security-guarded
  via ensure_main_window, delegates to the plugin. No DB writes —
  the roadmap's nudges-audit table is deferred until a concrete need
  emerges.

Frontend glue:
- nudgeBus.svelte.ts — subscribes to window events, applies
  suppression, calls deliver_nudge (+ tts_speak when speakAloud is
  on).
- kon:task-completed now dispatched on complete_task_cmd success.
- kon:microstep-generated + kon:step-completed dispatched from
  MicroSteps so the idle trigger can clear itself on any engagement.
- kon:focus-timer-cancelled added to focusTimer so the bus can reset
  its inactivity state on cancel, not only on natural completion.
- nudgeBus started from +layout.svelte onMount, stopped on destroy.

Settings:
- New "Nudges" section with three toggles: Enable nudges, Mute for
  now (separate so a hard mute doesn't lose preferences),
  Speak nudges aloud (reuses Phase 4 TTS).
- All default OFF. No first-run prompt — nudges are a Settings-found
  feature rather than a walkthrough step.

Out of scope per the revised Phase 6 spec: OS-wide keyboard/window
hooks, biometric signals, custom trigger editor (Phase 7), notification
sound (platform variance too high for Layer-1 — revisit in Phase 9
polish with a bundled .wav).

Gates: fmt clean, clippy -D warnings clean, cargo test 262/0 (4
existing + 262 current), npm run check 0/0, npm run build green.
This commit is contained in:
2026-04-24 19:05:21 +01:00
parent b333c6229e
commit eebea8cb9a
19 changed files with 980 additions and 152 deletions

View File

@@ -8,6 +8,7 @@ pub mod live;
pub mod llm;
pub mod meeting;
pub mod models;
pub mod nudges;
pub mod paste;
pub mod power;
pub mod profiles;

View File

@@ -0,0 +1,63 @@
//! Phase 6 of the feature-complete roadmap: Margot soft-touch nudges.
//!
//! The nudge bus lives in the frontend (`nudgeBus.svelte.ts`) — it
//! owns trigger subscription, suppression rules, and the hourly cap.
//! This module is the thin Rust side that:
//!
//! - Guards delivery to the main window only (secondary windows like
//! the task float can't fire notifications at the user — they're
//! already beside the user's attention).
//! - Delegates to `tauri-plugin-notification` for cross-platform OS
//! delivery. The plugin handles the macOS Notification Center /
//! Linux org.freedesktop.Notifications / Windows toast plumbing.
//!
//! Permission handling happens on the JS side via the plugin's
//! `isPermissionGranted` / `requestPermission` API — before the first
//! `deliver_nudge` call, the nudge bus prompts the user. If denied,
//! subsequent calls return an error that the bus logs and swallows.
//!
//! No persistence for v1. Cooldown state is ephemeral; the roadmap's
//! nudge-audit table is deferred until a real need emerges.
use serde::Deserialize;
use tauri_plugin_notification::NotificationExt;
use crate::commands::security::ensure_main_window;
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DeliverNudgeInput {
pub title: String,
pub body: String,
}
/// Deliver a single OS notification. Does not apply suppression
/// logic — the frontend nudge bus is responsible for cadence and
/// cooldown, so this command is a blunt "push it now" primitive.
///
/// Returns an error if the notification plugin refuses the call
/// (typically because permission was denied by the user at the OS
/// level). The nudge bus logs and swallows these — nudges are
/// fire-and-forget and must never surface errors to the user.
#[tauri::command]
pub fn deliver_nudge(
app: tauri::AppHandle,
window: tauri::WebviewWindow,
input: DeliverNudgeInput,
) -> Result<(), String> {
ensure_main_window(&window)?;
let title = input.title.trim();
let body = input.body.trim();
if title.is_empty() && body.is_empty() {
// A blank nudge is worse than no nudge — stay quiet.
return Ok(());
}
app.notification()
.builder()
.title(if title.is_empty() { "Corbie" } else { title })
.body(body)
.show()
.map_err(|e| format!("notification delivery failed: {e}"))
}

View File

@@ -138,6 +138,12 @@ pub fn run() {
tauri_plugin_autostart::MacosLauncher::LaunchAgent,
None,
))
// Phase 6 nudges: OS-native notifications. The plugin exposes
// isPermissionGranted / requestPermission / sendNotification on
// the JS side; our deliver_nudge wrapper guards those calls
// against the nudge-bus suppression rules and restricts
// invocation to the main window via ensure_main_window.
.plugin(tauri_plugin_notification::init())
// Remember size + position of every window across app restarts.
// Without this, secondary windows (preview overlay, task float,
// transcript viewer) open at whatever spot the compositor picks,
@@ -321,6 +327,8 @@ pub fn run() {
// Rituals (Phase 5 roadmap)
commands::rituals::get_last_morning_triage,
commands::rituals::mark_morning_triage_shown,
// Nudges (Phase 6 roadmap)
commands::nudges::deliver_nudge,
// Profiles + profile terms (canonical SQLite-backed profile CRUD) — Task 12
commands::profiles::list_profiles_cmd,
commands::profiles::get_profile_cmd,