//! `capri diagnostics export`: a ZIP to attach to a bug report (spec 09 RT-34/38). It holds only //! allow-listed facts — versions, desktop, hardware, the doctor checks and a settings summary — //! never conversations, memory, personality, file paths of documents, keys or screenshots. use std::io::Write; use std::path::{Path, PathBuf}; use anyhow::{Context, Result}; use capri_core::{Paths, Settings}; use serde_json::{json, Value}; /// Settings that help debugging and say nothing about the owner. fn settings_summary(settings: &Settings) -> Value { let inference: capri_llm::sections::InferenceSection = settings.section(); let api: capri_llm::sections::ApiSection = settings.section(); let router: capri_llm::sections::RouterSection = settings.section(); json!({ "model": settings.model, "upstreamModel": settings.upstream_model, "contextTokens": settings.context_tokens, "keepAlive": settings.keep_alive, "acceleration": settings.acceleration, "device": inference.device, "externalOllama": settings.ollama_url.is_some(), "apiMode": api.enabled, "apiConnections": api.connections.len(), "requestRouter": router.enabled, "language": {"interface": settings.language.interface, "conversation": settings.language.conversation}, }) } fn system() -> Value { let read = |p: &str| std::fs::read_to_string(p).ok().map(|s| s.trim().to_string()); let os = read("/etc/os-release").and_then(|t| t.lines().find_map(|l| l.strip_prefix("PRETTY_NAME=").map(|v| v.trim_matches('"').to_string()))); let var = |k: &str| std::env::var(k).ok(); json!({ "capri": env!("CARGO_PKG_VERSION"), "os": os, "kernel": read("/proc/sys/kernel/osrelease"), "sessionType": var("XDG_SESSION_TYPE"), "desktop": var("XDG_CURRENT_DESKTOP"), "wayland": var("WAYLAND_DISPLAY").is_some(), "x11": var("DISPLAY").is_some(), }) } /// Default report location: `~/capri-diagnostics-.zip`. pub fn default_output() -> PathBuf { let name = format!("capri-diagnostics-{}.zip", chrono::Utc::now().format("%Y%m%d-%H%M%S")); std::env::var_os("HOME").map_or_else(|| PathBuf::from(&name), |h| PathBuf::from(h).join(&name)) } pub async fn export(paths: &Paths, settings: &Settings, output: &Path) -> Result<()> { let checks = crate::doctor::checks(paths, settings).await; let running = crate::instance::running(paths); let status = if running { crate::instance::request(paths, "status").await.ok() } else { None }; // The running companion's status, reduced to non-private fields. let companion = status.map(|s| json!({"core": s["core"], "busy": s["busy"], "backend": s["backend"].as_str().map(|b| b.split(" · ").next().unwrap_or_default().to_string()), "naturalVoice": s["naturalVoice"]})); let files: [(&str, Value); 4] = [ ("system.json", system()), ("doctor.json", crate::doctor::to_json(&checks)), ("settings-summary.json", settings_summary(settings)), ("session.json", json!({"desktop": if running { "running" } else { "stopped" }, "companion": companion})), ]; let file = std::fs::OpenOptions::new().write(true).create_new(true).open(output).with_context(|| format!("cannot create {}", output.display()))?; let mut zip = zip::ZipWriter::new(file); let options = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated).unix_permissions(0o600); for (name, value) in files { zip.start_file(name, options)?; zip.write_all(serde_json::to_string_pretty(&value)?.as_bytes())?; } zip.start_file("README.txt", options)?; zip.write_all(b"Capri diagnostics: versions, desktop, hardware, setup checks and a settings summary.\nNo conversations, memory, personality, keys, screenshots or logs are included.\n")?; zip.finish()?; Ok(()) } #[cfg(test)] mod tests { use super::*; #[test] fn summary_has_no_private_values() { let mut settings = Settings::default(); settings.set_section(&capri_llm::sections::ApiSection { enabled: true, active: Some("c".into()), connections: vec![capri_llm::openai::ApiConnection { id: "c".into(), base_url: "https://secret.example/v1".into(), model: "m".into(), ..Default::default() }] }); let text = settings_summary(&settings).to_string(); assert!(!text.contains("secret.example") && text.contains("\"apiConnections\":1")); } }