Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.
Rust apple-notesapple-reminderscloudkitcrdtdesktop-appicloudicloud-calendaricloud-driveicloud-notesicloud-photosiphonelinuxlinux-desktoplocal-firstoffline-firstprivacyrusttauritauri-appwindows
Refreshing reminders drops from 52 seconds to under half a second
Yashwanth Kamireddi8dcffeb7478a
diff --git a/crates/quince-core/src/reminders.rs b/crates/quince-core/src/reminders.rsindex 5ef7143..bfce699 100644--- a/crates/quince-core/src/reminders.rs+++ b/crates/quince-core/src/reminders.rs@@ -47,6 +47,74 @@ fn now() -> i64 {}impl Library {+ /// Upsert lists without removing anything.+ ///+ /// An incremental sync returns only what changed, so absence means "unchanged", not+ /// "deleted". Using the replacing variant there would wipe the whole library on the+ /// first incremental refresh.+ pub fn merge_reminder_lists(&mut self, lists: &[ReminderList]) -> Result<()> {+ let tx = self.conn.transaction()?;+ {+ let mut stmt = tx.prepare(+ "INSERT INTO reminder_lists (guid, title, colour, ctag, sort_order, enabled, updated_at)+ VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)+ ON CONFLICT(guid) DO UPDATE SET+ title = excluded.title, colour = excluded.colour, ctag = excluded.ctag,+ sort_order = excluded.sort_order, enabled = excluded.enabled,+ updated_at = excluded.updated_at",+ )?;+ let stamp = now();+ for l in lists {+ stmt.execute(params![+ l.guid,+ l.title,+ l.colour,+ l.ctag,+ l.sort_order,+ i64::from(l.enabled),+ stamp+ ])?;+ }+ }+ tx.commit()?;+ Ok(())+ }++ /// Remove reminders and lists the server reports as gone.+ pub fn forget_reminders(+ &mut self,+ reminder_guids: &[String],+ list_guids: &[String],+ ) -> Result<()> {+ let tx = self.conn.transaction()?;+ {+ let mut drop_reminder = tx.prepare("DELETE FROM reminders WHERE guid = ?1")?;+ for guid in reminder_guids {+ drop_reminder.execute(params![guid])?;+ }+ // Removing a list takes its reminders with it, by foreign key.+ let mut drop_list = tx.prepare("DELETE FROM reminder_lists WHERE guid = ?1")?;+ for guid in list_guids {+ drop_list.execute(params![guid])?;+ }+ }+ tx.commit()?;+ Ok(())+ }++ /// Where a service's last sync got to, for resuming.+ pub fn sync_token(&self, service: &str) -> Result<Option<String>> {+ Ok(self+ .conn+ .query_row(+ "SELECT token FROM sync_state WHERE service = ?1",+ params![service],+ |row| row.get::<_, Option<String>>(0),+ )+ .ok()+ .flatten())+ }+/// Replace the set of lists, dropping any Apple no longer reports.pub fn put_reminder_lists(&mut self, lists: &[ReminderList]) -> Result<()> {let tx = self.conn.transaction()?;@@ -229,6 +297,59 @@ mod tests {);}+ /// The bug this guards: an incremental sync returns only what changed, so treating+ /// absence as deletion would wipe everything already stored.+ #[test]+ fn merging_does_not_remove_what_was_not_mentioned() {+ let mut lib = seeded();+ lib.put_reminders(&[reminder("r1", None, None)]).unwrap();++ // A later sync mentions a different list entirely.+ lib.merge_reminder_lists(&[list("l2")]).unwrap();++ assert_eq!(+ lib.reminder_lists().unwrap().len(),+ 2,+ "the original list must survive"+ );+ assert_eq!(+ lib.reminders_in("l1", true).unwrap().len(),+ 1,+ "and so must its reminders"+ );+ }++ #[test]+ fn forgetting_removes_only_what_was_named() {+ let mut lib = seeded();+ lib.put_reminder_lists(&[list("l1"), list("l2")]).unwrap();+ lib.put_reminders(&[reminder("r1", None, None), reminder("r2", None, None)])+ .unwrap();++ lib.forget_reminders(&["r1".to_string()], &[]).unwrap();+ let left = lib.reminders_in("l1", true).unwrap();+ assert_eq!(left.len(), 1);+ assert_eq!(left[0].guid, "r2");++ lib.forget_reminders(&[], &["l1".to_string()]).unwrap();+ assert!(+ lib.reminders_in("l1", true).unwrap().is_empty(),+ "its reminders go too"+ );+ assert_eq!(lib.reminder_lists().unwrap().len(), 1);+ }++ #[test]+ fn a_sync_token_round_trips() {+ let lib = seeded();+ assert!(lib.sync_token("reminders").unwrap().is_none());+ lib.mark_synced("reminders", Some("token-abc")).unwrap();+ assert_eq!(+ lib.sync_token("reminders").unwrap().as_deref(),+ Some("token-abc")+ );+ }+#[test]fn deleting_a_list_takes_its_reminders() {let mut lib = seeded();diff --git a/crates/quince-icloud/src/reminders.rs b/crates/quince-icloud/src/reminders.rsindex 179ad9b..220db44 100644--- a/crates/quince-icloud/src/reminders.rs+++ b/crates/quince-icloud/src/reminders.rs@@ -440,7 +440,12 @@ fn list_from_record(record: &Value) -> Option<ReminderList> {/// Read reminders from CloudKit into the local library.///-/// Returns the number of lists and reminders stored, plus the sync token to resume from.+/// Pass the previous `since` token to fetch only what changed. That distinction matters for+/// more than speed: an incremental response contains only changes, so absence means+/// "unchanged", not "deleted" — treating it otherwise would empty the library on the first+/// incremental refresh.+///+/// Returns the number of lists and reminders written, plus the token to resume from.pub async fn sync_cloudkit(http: &reqwest::Client,ckdatabasews_root: &str,@@ -448,28 +453,63 @@ pub async fn sync_cloudkit(library: &mut quince_core::Library,since: Option<&str>,) -> Result<(usize, usize, Option<String>)> {+ let incremental = since.is_some();let client = ZoneClient::new(http, ckdatabasews_root, CONTAINER, params);let (records, token) = client.all_records(ZONE, since).await?;let mut lists: Vec<ReminderList> = Vec::new();let mut reminders: Vec<Reminder> = Vec::new();+ let mut gone_reminders: Vec<String> = Vec::new();+ let mut gone_lists: Vec<String> = Vec::new();for record in &records {- match field::record_type(record).as_deref() {- Some("Reminder") => reminders.extend(reminder_from_record(record)),+ let Some(kind) = field::record_type(record) else {+ continue; // tombstone with no type; nothing to act on+ };+ let Some(name) = field::record_name(record) else {+ continue;+ };+ // CloudKit reports a removal either as a top-level flag or a Deleted field.+ let removed = record+ .get("deleted")+ .and_then(Value::as_bool)+ .unwrap_or(false)+ || field::flag(record, "Deleted");++ match kind.as_str() {+ "Reminder" => {+ if removed {+ gone_reminders.push(name);+ } else {+ reminders.extend(reminder_from_record(record));+ }+ }// Only List records are the lists people made. ListSection is a heading inside// one, and SmartList is a built-in filter — Today, Scheduled, Flagged — which// carries a SmartListType instead of a Name and appeared as phantom entries// called "Reminders" that held nothing.- Some("List") => lists.extend(list_from_record(record)),+ "List" => {+ if removed {+ gone_lists.push(name);+ } else {+ lists.extend(list_from_record(record));+ }+ }_ => {}}}// Every list a reminder points at must exist, or storing it fails the foreign key.- // Apple does not always return the list record in the same page as its reminders.+ // On an incremental pass the list is usually already stored, so check before inventing.+ let known_locally: std::collections::HashSet<String> = library+ .reminder_lists()?+ .into_iter()+ .map(|l| l.guid)+ .collect();for reminder in &reminders {- if !lists.iter().any(|l| l.guid == reminder.list_guid) {+ let missing = !lists.iter().any(|l| l.guid == reminder.list_guid)+ && !known_locally.contains(&reminder.list_guid);+ if missing {lists.push(ReminderList {guid: reminder.list_guid.clone(),title: "Reminders".into(),@@ -481,8 +521,15 @@ pub async fn sync_cloudkit(}}- library.put_reminder_lists(&lists)?;+ if incremental {+ library.merge_reminder_lists(&lists)?;+ } else {+ library.put_reminder_lists(&lists)?;+ }library.put_reminders(&reminders)?;+ if !gone_reminders.is_empty() || !gone_lists.is_empty() {+ library.forget_reminders(&gone_reminders, &gone_lists)?;+ }library.mark_synced("reminders", token.as_deref())?;Ok((lists.len(), reminders.len(), token))}@@ -646,3 +693,161 @@ mod list_kind_tests {assert_eq!(l.guid, "List/abc");}}++#[cfg(test)]+mod incremental_tests {+ use super::*;+ use quince_core::Library;+ use serde_json::json;+ use wiremock::matchers::{method, path_regex};+ use wiremock::{Mock, MockServer, ResponseTemplate};++ fn doc(text: &str) -> String {+ use base64::{engine::general_purpose::STANDARD as B64, Engine};+ use std::io::Write;+ fn f(tag: u8, payload: &[u8]) -> Vec<u8> {+ let mut out = vec![(tag << 3) | 2];+ let mut len = payload.len();+ loop {+ let mut b = (len & 0x7F) as u8;+ len >>= 7;+ if len != 0 {+ b |= 0x80;+ }+ out.push(b);+ if len == 0 {+ break;+ }+ }+ out.extend_from_slice(payload);+ out+ }+ let proto = f(2, &f(3, &f(2, text.as_bytes())));+ let mut gz = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());+ gz.write_all(&proto).unwrap();+ B64.encode(gz.finish().unwrap())+ }++ fn reminder(name: &str, title: &str) -> Value {+ json!({+ "recordName": name, "recordType": "Reminder",+ "fields": {+ "TitleDocument": { "type": "ENCRYPTED_BYTES", "value": doc(title) },+ "List": { "type": "REFERENCE", "value": { "recordName": "List/a" } }+ }+ })+ }++ fn list(name: &str, title: &str) -> Value {+ json!({+ "recordName": name, "recordType": "List",+ "fields": { "Name": { "type": "STRING", "value": title } }+ })+ }++ async fn serve(body: Value) -> MockServer {+ let server = MockServer::start().await;+ Mock::given(method("POST"))+ .and(path_regex(r".*/changes/zone$"))+ .respond_with(ResponseTemplate::new(200).set_body_json(body))+ .mount(&server)+ .await;+ server+ }++ /// The bug worth guarding: an incremental response mentions one changed record, and+ /// everything else must survive. Replacing on a delta would empty the library.+ #[tokio::test]+ async fn an_incremental_sync_keeps_what_it_did_not_mention() {+ let http = reqwest::Client::new();+ let mut library = Library::in_memory().unwrap();++ let full = serve(json!({ "zones": [{+ "records": [list("List/a", "GYM"), reminder("Reminder/1", "Squats"),+ reminder("Reminder/2", "Deadlifts")],+ "syncToken": "t1", "moreComing": false }] }))+ .await;+ sync_cloudkit(&http, &full.uri(), vec![], &mut library, None)+ .await+ .unwrap();+ assert_eq!(library.reminders_in("List/a", true).unwrap().len(), 2);++ // A delta carrying only one changed reminder, and no list at all.+ let delta = serve(json!({ "zones": [{+ "records": [reminder("Reminder/1", "Squats (heavier)")],+ "syncToken": "t2", "moreComing": false }] }))+ .await;+ sync_cloudkit(&http, &delta.uri(), vec![], &mut library, Some("t1"))+ .await+ .unwrap();++ let after = library.reminders_in("List/a", true).unwrap();+ assert_eq!(+ after.len(),+ 2,+ "the unmentioned reminder must survive a delta"+ );+ assert_eq!(+ library.reminder_lists().unwrap().len(),+ 1,+ "and so must the list, which the delta never mentioned"+ );+ assert!(+ after.iter().any(|r| r.title == "Squats (heavier)"),+ "the changed one should be updated"+ );+ }++ /// A full sync still drops what Apple stopped reporting.+ #[tokio::test]+ async fn a_full_sync_still_removes_vanished_lists() {+ let http = reqwest::Client::new();+ let mut library = Library::in_memory().unwrap();++ let first = serve(json!({ "zones": [{+ "records": [list("List/a", "GYM"), list("List/b", "Study")],+ "syncToken": "t1", "moreComing": false }] }))+ .await;+ sync_cloudkit(&http, &first.uri(), vec![], &mut library, None)+ .await+ .unwrap();+ assert_eq!(library.reminder_lists().unwrap().len(), 2);++ let second = serve(json!({ "zones": [{+ "records": [list("List/a", "GYM")], "syncToken": "t2", "moreComing": false }] }))+ .await;+ sync_cloudkit(&http, &second.uri(), vec![], &mut library, None)+ .await+ .unwrap();+ assert_eq!(+ library.reminder_lists().unwrap().len(),+ 1,+ "a full pass is authoritative"+ );+ }++ /// A reminder deleted on the phone must disappear here.+ #[tokio::test]+ async fn a_deleted_reminder_is_removed() {+ let http = reqwest::Client::new();+ let mut library = Library::in_memory().unwrap();++ let first = serve(json!({ "zones": [{+ "records": [list("List/a", "GYM"), reminder("Reminder/1", "Squats")],+ "syncToken": "t1", "moreComing": false }] }))+ .await;+ sync_cloudkit(&http, &first.uri(), vec![], &mut library, None)+ .await+ .unwrap();+ assert_eq!(library.reminders_in("List/a", true).unwrap().len(), 1);++ let delta = serve(json!({ "zones": [{+ "records": [{ "recordName": "Reminder/1", "recordType": "Reminder", "deleted": true }],+ "syncToken": "t2", "moreComing": false }] }))+ .await;+ sync_cloudkit(&http, &delta.uri(), vec![], &mut library, Some("t1"))+ .await+ .unwrap();+ assert!(library.reminders_in("List/a", true).unwrap().is_empty());+ }+}diff --git a/probe/src/main.rs b/probe/src/main.rsindex 3373e25..245c310 100644--- a/probe/src/main.rs+++ b/probe/src/main.rs@@ -502,38 +502,6 @@ async fn main() -> Result<()> {// than against an assumption about the shape.// Scan month by month with the exact parameters the client uses, so "no events"// is a measurement rather than an assumption.- println!(" \x1b[1mmonth-by-month scan\x1b[0m");- let months = [- ("2026-01-01", "2026-01-31"),- ("2026-03-01", "2026-03-31"),- ("2026-05-01", "2026-05-31"),- ("2026-07-01", "2026-07-31"),- ("2026-08-01", "2026-08-31"),- ("2026-09-01", "2026-09-30"),- ("2026-10-01", "2026-10-31"),- ("2026-12-01", "2026-12-31"),- ("2027-01-01", "2027-01-31"),- ];- let scan_client =- quince_icloud::calendar::CalendarClient::new(&http, &root, params.clone());- let mut total = 0usize;- for (a, b) in months {- let via_startup = scan_client.startup(a, b, zone).await;- let via_events = scan_client.events(a, b, zone).await;- let s_count = via_startup.as_ref().map(|s| s.events.len()).unwrap_or(0);- let e_count = via_events.as_ref().map(|s| s.events.len()).unwrap_or(0);- total += s_count.max(e_count);- println!(- " {a} startup={s_count:<4} events={e_count:<4}{}",- if via_startup.is_err() || via_events.is_err() {- " (an endpoint errored)"- } else {- ""- }- );- }- println!(" total events seen across the scan: {total}");-let (window_start, window_end) = ("2026-06-01", "2027-03-31");match quince_core::Library::open(&dir) {@@ -592,15 +560,28 @@ async fn main() -> Result<()> {match quince_core::Library::open(&dir) {Err(e) => println!(" \x1b[31mcouldn't open the local library:\x1b[0m {e}"),Ok(mut library) => {- match quince_icloud::reminders::sync_cloudkit(+ // Resume from the stored token, so this measures what a real refresh costs+ // rather than repeating a cold walk every time.+ let since = library.sync_token("reminders").ok().flatten();+ let started = std::time::Instant::now();+ println!(+ " {} sync…",+ if since.is_some() {+ "incremental"+ } else {+ "first (full)"+ }+ );+ let outcome = quince_icloud::reminders::sync_cloudkit(&http,&ck,params,&mut library,- None,+ since.as_deref(),)- .await- {+ .await;+ println!(" took {:.1}s", started.elapsed().as_secs_f64());+ match outcome {Err(e) => println!(" \x1b[31mFAIL\x1b[0m {e}"),Ok((lists, count, token)) => {println!(@@ -628,58 +609,6 @@ async fn main() -> Result<()> {}}- // ---- where do List records live? --------------------------------------------- // Reminders reference List/<uuid>, but the Reminders zone returns none. Census every- // zone in the container rather than guess.- println!("\n\x1b[1m── Hunting the List records ──\x1b[0m");- if let Some(ck) = session.service_url("ckdatabasews") {- for zone_name in ["Reminders", "RemindersMigration", "_defaultZone"] {- let zc = quince_icloud::cloudkit::ZoneClient::new(- client.http(),- &ck,- "com.apple.reminders",- client.service_params(),- );- match zc.all_records(zone_name, None).await {- Err(e) => println!(- " {zone_name:<20} error: {}",- e.to_string().chars().take(70).collect::<String>()- ),- Ok((records, _)) => {- let mut census: std::collections::BTreeMap<String, usize> =- std::collections::BTreeMap::new();- for rec in &records {- let kind = rec- .get("recordType")- .and_then(|t| t.as_str())- .unwrap_or("(tombstone)");- *census.entry(kind.to_string()).or_default() += 1;- }- let summary: Vec<String> =- census.iter().map(|(k, n)| format!("{k}={n}")).collect();- println!(- " {zone_name:<20} {} record(s): {}",- records.len(),- summary.join(" ")- );-- // Show any List record's fields, wherever it turns up.- for rec in &records {- if rec.get("recordType").and_then(|t| t.as_str()) == Some("List") {- let fields: Vec<String> = rec- .get("fields")- .and_then(|f| f.as_object())- .map(|o| o.keys().cloned().collect())- .unwrap_or_default();- println!(" List fields: {}", fields.join(", "));- break;- }- }- }- }- }- }-// ---- CloudKit containers ----------------------------------------------------// Apple's legacy /rd and /ca endpoints answer with a placeholder once data has been// upgraded to the modern format. The real records live in CloudKit, so find out which@@ -740,117 +669,6 @@ async fn main() -> Result<()> {}}- // ---- what a CloudKit reminder actually looks like ---------------------------- println!("\n\x1b[1m── CloudKit reminders: record shapes ──\x1b[0m");- if let Some(ck) = session.service_url("ckdatabasews") {- // The Reminder type is not indexable, so records/query is refused. changes/zone- // walks the whole zone instead, which is also how incremental sync works.- // Omit desiredKeys entirely: an empty array asks Apple for no fields at all.- let url = format!("{ck}/database/1/com.apple.reminders/production/private/changes/zone");- let mut q = client.service_params();- q.push(("ckjsBuildVersion".into(), "17DProjectDev77".into()));- q.push(("ckjsVersion".into(), "2.6.1".into()));-- let mut sync_token: Option<String> = None;- let mut types: std::collections::BTreeMap<String, usize> =- std::collections::BTreeMap::new();- let mut samples: Vec<(String, Vec<String>, Option<String>)> = Vec::new();-- for page in 0..12 {- let mut zone = serde_json::json!({- "zoneID": { "zoneName": "Reminders", "zoneType": "REGULAR_CUSTOM_ZONE" },- "maxChanges": 200,- });- if let Some(token) = &sync_token {- zone["syncToken"] = serde_json::Value::String(token.clone());- }- let body = serde_json::json!({ "zones": [zone] });-- let Ok(r) = client.http().post(&url).query(&q).json(&body).send().await else {- println!(" transport error on page {page}");- break;- };- let text = r.text().await.unwrap_or_default();- let v: serde_json::Value =- serde_json::from_str(&text).unwrap_or(serde_json::Value::Null);- let Some(zone_result) = v- .get("zones")- .and_then(|z| z.as_array())- .and_then(|a| a.first())- else {- println!(- " unexpected: {}",- text.chars().take(200).collect::<String>()- );- break;- };-- let records = zone_result- .get("records")- .and_then(|r| r.as_array())- .cloned()- .unwrap_or_default();- for rec in &records {- let rt = rec- .get("recordType")- .and_then(|t| t.as_str())- .unwrap_or("?");- *types.entry(rt.to_string()).or_default() += 1;- if samples.iter().all(|(t, _, _)| t != rt) {- let fields: Vec<String> = rec- .get("fields")- .and_then(|f| f.as_object())- .map(|o| o.keys().cloned().collect())- .unwrap_or_default();- // Any field that looks like a title tells us where the text lives.- // Dump a whole Reminder so the shape of TitleDocument is visible.- let title = if rt == "Reminder" {- // Only the fields whose encoding is still unknown.- rec.get("fields").and_then(|f| {- let picked = serde_json::json!({- "TitleDocument": f.get("TitleDocument"),- "NotesDocument": f.get("NotesDocument"),- "Priority": f.get("Priority"),- "List": f.get("List"),- });- serde_json::to_string_pretty(&picked)- .ok()- .map(|s| s.chars().take(1600).collect::<String>())- })- } else {- None- };- samples.push((rt.to_string(), fields, title));- }- }-- sync_token = zone_result- .get("syncToken")- .and_then(|t| t.as_str())- .map(ToString::to_string);- if !zone_result- .get("moreComing")- .and_then(|m| m.as_bool())- .unwrap_or(false)- {- break;- }- }-- let total: usize = types.values().sum();- println!(" {total} record(s) across the zone");- for (rt, n) in &types {- println!(" {rt}: {n}");- }- for (rt, fields, title) in &samples {- if let Some(dump) = title {- println!(" --- full {rt} record ---\n{dump}");- } else {- println!(" {rt} fields: {}", fields.join(", "));- }- }- }-// ---- 5. verdict -------------------------------------------------------------let passed = results.iter().filter(|r| r.ok).count();println!("\n\x1b[1m── Verdict ──\x1b[0m");diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rsindex cc71ed4..0aa0dd7 100644--- a/src-tauri/src/main.rs+++ b/src-tauri/src/main.rs@@ -235,7 +235,10 @@ async fn calendar_list(data: State<'_, Data>) -> UiResult<Vec<Calendar>> {async fn reminders_sync(auth: State<'_, Auth>, data: State<'_, Data>) -> UiResult<(usize, usize)> {let (http, root, params) = cloudkit_context(&auth).await?;let mut library = data.0.lock().await;- quince_icloud::reminders::sync_cloudkit(&http, &root, params, &mut library, None)+ // Resume from where the last sync got to. A full walk of this zone is thousands of+ // records; a delta is usually a handful.+ let since = library.sync_token("reminders").map_err(|e| e.to_string())?;+ quince_icloud::reminders::sync_cloudkit(&http, &root, params, &mut library, since.as_deref()).await.map(|(lists, count, _)| (lists, count)).map_err(|e| e.to_string())@@ -288,7 +291,8 @@ async fn reminder_set_done(.map_err(|e| e.to_string())?;let mut library = data.0.lock().await;- quince_icloud::reminders::sync_cloudkit(&http, &root, params, &mut library, None)+ let since = library.sync_token("reminders").map_err(|e| e.to_string())?;+ quince_icloud::reminders::sync_cloudkit(&http, &root, params, &mut library, since.as_deref()).await.map_err(|e| e.to_string())?;Ok(())