# capri-perception Spec 06 (perception, awareness, initiative, resources) in pure Rust, plus the Linux platform layer shared with `capri-computer`: KWin scripting over D-Bus, i3 IPC, window/output listing, pixel capture. | Module | What | |---|---| | `platform` | `Platform::detect` (KDE Wayland vs X11/i3), `Desktop` (window list + activation via KWin script or i3 IPC + XRes), `process` identity helpers | | `kwin`, `i3` | KWin one-shot and persistent scripts reporting to Capri's own D-Bus object; i3 IPC codec, tree leaves | | `capture` | KWin `ScreenShot2` (raw pipe), Spectacle fallback, X11 `GetImage`; `active_display`, `frame(source)` (<= 1280x720, mosaic for all monitors) | | `policy` | consent state machine: opaque source tokens, `UserGesture`, armed 10 s single-use grant, watch lease, revocation, no fallback | | `screen` | `ScreenController`: snapshot previews, observation loop (2/5 s ticks, 64x36 change detection, 12 s background analysis), per-turn foreground analysis, clips | | `observation`, `visual`, `identity`, `llm` | scene-summary request, observation text, one-shot visual turn prompt/budget/failure texts/commit pair, `CAPRI_SCREEN_SELF`, loopback-only JSON model calls | | `clip` | `is_clip_command`, private screenshot files, last-screenshot receipt, shutter asset lookup + procedural shutter | | `awareness` | metadata-only events: foreground app (KWin persistent script / i3 events + `_NET_ACTIVE_WINDOW`), lock, suspend, shutdown, idle (ScreenSaver ms / MIT-SCREEN-SAVER) | | `initiative` | settings, gates in Capri's order, budget persistence, scheduler service, generation request/parse | | `resources` | /proc CPU and process-tree load, hwmon temperature, meminfo, `nvidia-smi`, AMD sysfs, warning hysteresis | | `settings`, `ui` | `Section`s and plain egui pages | ## Integration ### Startup ```rust let desktop = capri_perception::platform::Desktop::connect.await?; // KDE or X11/i3 let screen = ScreenController::new(Arc::new(desktop.clone), host_flags, paths.data.clone, paths.state); screen.set_model(LocalModel::new(&settings.ollama_base_url, &model, capabilities, options).ok); // None = vision off ``` `host_flags: Arc HostState>` reports busy/typing/speaking/user speech/offline/hidden. ### Per chat turn (engine) 1. Before the model call: `let ctx = screen.prepare_turn(&user_text, &turn_cancel).await;` (fresh frame + analysis while observing; never fails the turn). 2. Append `screen.observation_context(&user_text)` to the turn context (empty when off). 3. `StateProvider`: merge `screen.state_json` → `screenObservation`, `screenVisionAvailable` (only when false), `previousScreenshotSaved`. 4. Clip: if `clip::is_clip_command(text, Some(&wake_finder))` → `screen.clip(turn_uuid, locked).await` (18 s overall deadline is the caller's), say `clip::CLIPPED`, play `clip::shutter_sound(..)` or `clip::procedural_shutter(rate)` with rodio. Failure texts: `clip::CANCELLED` / `clip::FAILED`. 5. Snapshot "Ask Capri": `screen.take_preview(id)` → `visual::messages(&VisualPrompt{..})` → stream with `capri_llm::Ollama::chat` (`num_predict = visual::output_budget(..)`), then store `visual::commit_pair(..)` (text only). ### UI - Buttons create `UserGesture::owner_clicked` inside the click handler and call `screen.discover`, `capture_snapshot(token, gesture)`, `start_observation(token, interval, duration, gesture)`, `stop`. `ui::settings_ui` returns these as `ScreenRequest`s. - `screen.status` (`watch`) carries `{capture, observation}` for the REC/OBS indicator; `screen.indicator` gives kind/label/watch. - Hide, lock, suspend, model sleep, quit: `screen.discard(StopReason::…)`. Display change: `screen.topology_changed`. Owner activity: `screen.activity`. ### Awareness and initiative ```rust let (aw, mut events) = Awareness::start(desktop.clone, vec![]).await?; // only when initiative level != off and Capri is visible let ini = InitiativeService::new(host, store.section, saved_budget.as_ref); ini.spawn_scheduler; while let Some(e) = events.recv.await { match e { Some(ev) => ini.observe(&ev), None => screen.topology_changed } } ``` `InitiativeHost`: `flags`, `generate` (use `initiative::generation_body` + `generate_with(&model, …)`), `commit` (append an assistant message labelled with `Comment.label`, before delivery), `deliver` (UI + voice), `persist_budget` → `/initiative-state.json`. Call `ini.activity(3000)` on keystrokes/mic/send, `ini.activity(45_000)` after a completed turn, `ini.cancel` on hide/sleep/lock, `ini.configure(new)` on settings change, `ini.tick(true)` for "Try initiative", `ini.dismiss/restore`. ### Resources `resources::memory_status(&mut cpu_sampler, Some(&mut process_load)).await` every 3/5/10 s while the HUD is visible; `ResourceWarnings::update(device, metric, value, sampled_at)`. ### Settings sections `screenObservation` (`intervalMs`, `durationMs`), `initiative` (Capri's nine keys; Linux process names added to `excludedApps`), `resources` (`memoryMonitor`, `processGraphs`, `memoryStyle`, `memoryIntervalSeconds`). ### Permissions (owner, once) - **KDE window capture**: KWin only serves `ScreenShot2` to binaries whose installed desktop file declares it. Install `capture::kwin_desktop_entry(¤t_exe)` as `~/.local/share/applications/capri.desktop` (`Exec` = the real binary path; run `kbuildsycoca6` if KWin still refuses). Without it, monitor captures fall back to `spectacle` (installed with Plasma) and window sources fail closed. - **i3/X11**: nothing (X11 `GetImage`, XRes and MIT-SCREEN-SAVER are standard Xorg extensions). Lock events need a locker that uses logind (`xss-lock`/`loginctl lock-session`). - No uinput, no udev rule, no Python. ### Not done Time service/reminders (..42, owned by `capri-tools`), mosaic captions use a 5x7 bitmap font, KDE awareness has no 1 s fallback poll (the KWin script pushes changes), Capri process load does not count an Ollama started by systemd.