//! The log of import runs — one `cotton_imports` row per run that changed a//! database, which the Imports tool lists as "past imports" (Spec 9.3).//!//! Every importer writes its row through here, so the five of them agree on//! the one shape: where it came from (`source`), what it was in words//! (`title`), how many records it added, how many it skipped because the//! database already had them, the device that ran it and when it finished. A//! run that added nothing and skipped nothing writes no row: there is nothing//! to show for it, and a list of empty runs would bury the ones that mattered.//!//! Two doors, one row. A tool running over a [`Ctx`] (contacts, passwords, a//! recording) writes through [`record`]; an importer that mints its own//! `LocalChange`s (a chat export on the phone, the Mac's ingest) builds the//! the same patch with [`row`].use serde_json::{Map, Value};use cotton_store::Driver;usecrate::mcp::dispatch::Ctx;usecrate::mcp::handlers::common::{insert_row, now_iso};usecrate::mcp::protocol::ToolFailure;/// The table.pubconst TABLE: &str = "cotton_imports";/// Where an import came from — the table's `source` CHECK, spelled once./// The CHECK still admits `instagram_export` for the runs Instagram's export/// left (Instagram left, 22 Nov 2026); nothing writes a new one.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pubenumImportSource {/// The phone's address book, into People.Contacts,/// A WhatsApp chat export shared to the phone.ChatExport,/// A recording brought in as a meeting.Recording,/// A password manager's export (Dashlane), into Passwords.Passwords,/// The Mac's Messages library.Messages,/// The Mac's call history.CallHistory,/// A WhatsApp backup the Mac reads.WhatsappBackup,/// The Mac's Photos library.Photos,}implImportSource {pubconstfn as_str(self) -> &'static str {matchself {ImportSource::Contacts => "contacts",ImportSource::ChatExport => "chat_export",ImportSource::Recording => "recording",ImportSource::Passwords => "passwords",ImportSource::Messages => "messages",ImportSource::CallHistory => "call_history",ImportSource::WhatsappBackup => "whatsapp_backup",ImportSource::Photos => "photos", } }}/// Whether a run is worth a row: it added or skipped something.pubconstfn changed(added: i64, skipped: i64) -> bool { added > 0 || skipped > 0}/// The device that ran the import, as this file names its own device/// (`_meta.device_id`, the replica's author device — never the credential/// key). `None` when the file has no `_meta` row.pubfn this_device(db: &Driver) -> Option<String> { db.get("SELECT device_id FROM _meta LIMIT 1", &[]) .ok() .flatten() .and_then(|row| row.text("device_id").map(str::to_string)) .filter(|id| !id.is_empty())}/// The row's columns. `workspace_id` is in the patch because a `LocalChange`/// minted outside a [`Ctx`] carries no workspace of its own; `title` is/// trimmed, and an empty one says what the source was instead, so the CHECK/// never refuses a run that did import something.#[allow(clippy::too_many_arguments)]pubfn row( workspace_id: &str, source: ImportSource, title: &str, added: i64, skipped: i64, device_id: Option<&str>, finished_at: &str,) -> Map<String, Value> {let title = title.trim();let title = if title.is_empty() { default_title(source) } else { title };letmut patch = Map::new(); patch.insert("workspace_id".into(), Value::from(workspace_id)); patch.insert("source".into(), Value::from(source.as_str())); patch.insert("title".into(), Value::from(title)); patch.insert("added".into(), Value::from(added.max(0))); patch.insert("skipped".into(), Value::from(skipped.max(0))); patch.insert("device_id".into(), device_id.map_or(Value::Null, Value::from), ); patch.insert("finished_at".into(), Value::from(finished_at)); patch.insert("created_at".into(), Value::from(finished_at)); patch}/// What a run is called when its importer has no better words — and what a/// Mac ingest source's run is always called: the library it read.pubfn default_title(source: ImportSource) -> &'static str {match source {ImportSource::Contacts => "Phone contacts",ImportSource::ChatExport => "A chat export",ImportSource::Recording => "A recording",ImportSource::Passwords => "A password export",ImportSource::Messages => "Messages on this Mac",ImportSource::CallHistory => "Call history",ImportSource::WhatsappBackup => "WhatsApp backup",ImportSource::Photos => "Photos library", }}/// Write one run's row through a tool's [`Ctx`], before the caller's commit./// Nothing when the run changed nothing.pubfn record( ctx: &mutCtx<'_>, source: ImportSource, title: &str, added: i64, skipped: i64,) -> Result<(), ToolFailure> {if !changed(added, skipped) {returnOk(()); }let device = this_device(ctx.db());let finished_at = now_iso(ctx);let patch = row( ctx.workspace_id, source, title, added, skipped, device.as_deref(), &finished_at, );let id = ctx.new_uuid(); insert_row(ctx, TABLE, &id, patch)}