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
Notes and reminders written here stop arriving blank
Yashwanth Kamireddi82382dbd491a
diff --git a/crates/quince-icloud/src/ckdocument.rs b/crates/quince-icloud/src/ckdocument.rsindex 0a337d5..2a00292 100644--- a/crates/quince-icloud/src/ckdocument.rs+++ b/crates/quince-icloud/src/ckdocument.rs@@ -223,9 +223,40 @@ fn write_varint(mut value: u64, out: &mut Vec<u8>) {}}-/// The minimal document Apple accepts: the text nested at [`TEXT_PATH`].+/// Which field of the note holds an attribute run.+const ATTRIBUTE_RUN: u64 = 5;++/// A protobuf varint field: `(tag << 3) | 0`, then the value.+fn varint_field(tag: u64, value: u64) -> Vec<u8> {+ let mut out = Vec::with_capacity(8);+ write_varint(tag << 3, &mut out);+ write_varint(value, &mut out);+ out+}++/// The length Apple's attribute runs are measured in.+///+/// Not bytes and not characters: UTF-16 code units, which is what an emoji makes differ+/// from both. Measured against real notes, where the runs summed to exactly this.+fn utf16_len(text: &str) -> u64 {+ text.encode_utf16().count() as u64+}++/// The document Apple accepts: the text nested at [`TEXT_PATH`], with an attribute run+/// covering it.+///+/// The run is the part that was missing. Apple's clients map styling onto the text through+/// these runs, and a note or reminder written without one arrives with nothing to render —+/// which is exactly how everything created from here showed up blank on the phone. A bare+/// `{ length }` is a complete run; Apple writes exactly that for unstyled text.fn document_bytes(text: &str) -> Vec<u8> {- let mut nested = length_delimited(TEXT_PATH[2], text.as_bytes());+ let mut note = length_delimited(TEXT_PATH[2], text.as_bytes());+ note.extend(length_delimited(+ ATTRIBUTE_RUN,+ &varint_field(1, utf16_len(text)),+ ));++ let mut nested = note;for tag in TEXT_PATH[..2].iter().rev() {nested = length_delimited(*tag, &nested);}@@ -240,6 +271,214 @@ fn gzip(bytes: &[u8]) -> Vec<u8> {encoder.finish().unwrap_or_default()}+/// What a text edit did, in the UTF-16 units attribute runs are measured in.+///+/// Derived from the common prefix and suffix rather than a full diff: an edit is almost+/// always one contiguous change, and this describes exactly that — `removed` units at+/// `from` became `inserted` units.+fn utf16_change(old: &str, new: &str) -> (u64, u64, u64) {+ let old: Vec<u16> = old.encode_utf16().collect();+ let new: Vec<u16> = new.encode_utf16().collect();++ let prefix = old+ .iter()+ .zip(&new)+ .take_while(|(a, b)| a == b)+ .count()+ .min(old.len())+ .min(new.len());++ // The suffix may not run back past the prefix on either side.+ let max_suffix = (old.len() - prefix).min(new.len() - prefix);+ let suffix = old+ .iter()+ .rev()+ .zip(new.iter().rev())+ .take_while(|(a, b)| a == b)+ .count()+ .min(max_suffix);++ (+ prefix as u64,+ (old.len() - prefix - suffix) as u64,+ (new.len() - prefix - suffix) as u64,+ )+}++/// Apply that change to a set of run lengths, the way an attributed string does it.+///+/// Runs entirely before the change keep their length; runs entirely after keep theirs; the+/// ones the change overlaps shrink by what it removed, and whichever run the change starts+/// in grows by what it inserted. A run left covering nothing is dropped.+fn adjust_run_lengths(lengths: &[u64], from: u64, removed: u64, inserted: u64) -> Vec<u64> {+ let removal_end = from + removed;+ let mut adjusted = Vec::with_capacity(lengths.len());+ let mut cursor = 0u64;+ let mut inserted_into = false;++ for &length in lengths {+ let start = cursor;+ let end = cursor + length;+ cursor = end;++ let overlap = removal_end.min(end).saturating_sub(from.max(start));+ let mut next = length - overlap;++ // Whichever run the change begins inside takes the new text. `end > from` rather+ // than `>=` so an insertion at a boundary lands in the run it is extending.+ if !inserted_into && (end > from || (end == from && start <= from)) {+ next += inserted;+ inserted_into = true;+ }+ adjusted.push(next);+ }++ // An edit past the end of every run — appending to a note whose runs were already short+ // — belongs to the last one.+ if !inserted_into && inserted > 0 {+ if let Some(last) = adjusted.last_mut() {+ *last += inserted;+ } else {+ adjusted.push(inserted);+ }+ }++ adjusted.retain(|&n| n > 0);+ adjusted+}++/// Replace a varint field's value, copying every other field through untouched.+fn set_varint_field(message: &[u8], tag: u64, value: u64) -> Option<Vec<u8>> {+ let mut reader = Reader::new(message);+ let mut out = Vec::with_capacity(message.len() + 8);+ let mut replaced = false;++ while !reader.done() {+ let start = reader.at;+ let key = reader.varint()?;+ let (field, wire) = (key >> 3, key & 7);+ if field == tag && wire == WIRE_VARINT {+ reader.varint()?;+ out.extend_from_slice(&varint_field(tag, value));+ replaced = true;+ } else {+ reader.skip(wire)?;+ out.extend_from_slice(message.get(start..reader.at)?);+ }+ }++ if !replaced {+ // A run with styling but no length is still a run; give it one.+ let mut with_length = varint_field(tag, value);+ with_length.extend_from_slice(&out);+ return Some(with_length);+ }+ Some(out)+}++/// Rebuild a note with new text and runs that still describe it.+///+/// Every field other than the text and the run lengths is copied through byte for byte, so+/// each run keeps whatever styling it carried — the paragraph style, the font, the+/// identifiers Apple uses to merge edits. Only the lengths move.+fn rebuild_note(note: &[u8], old_text: &str, new_text: &str) -> Option<Vec<u8>> {+ let (from, removed, inserted) = utf16_change(old_text, new_text);++ // Collect the existing runs, whole.+ let mut runs: Vec<Vec<u8>> = Vec::new();+ let mut reader = Reader::new(note);+ while !reader.done() {+ let key = reader.varint()?;+ let (field, wire) = (key >> 3, key & 7);+ if field == ATTRIBUTE_RUN && wire == WIRE_LEN {+ let n = usize::try_from(reader.varint()?).ok()?;+ runs.push(reader.take(n)?.to_vec());+ } else {+ reader.skip(wire)?;+ }+ }++ let lengths: Vec<u64> = runs+ .iter()+ .map(|run| {+ let mut inner = Reader::new(run);+ while !inner.done() {+ let Some(key) = inner.varint() else { break };+ if key >> 3 == 1 && key & 7 == WIRE_VARINT {+ return inner.varint().unwrap_or(0);+ }+ if inner.skip(key & 7).is_none() {+ break;+ }+ }+ 0+ })+ .collect();++ let adjusted = adjust_run_lengths(&lengths, from, removed, inserted);+ let wanted = utf16_len(new_text);++ // The runs have to add up to the text, or Apple has styling pointing at units that are+ // not there. If the arithmetic did not come out — or there were no runs to begin with —+ // one run covering everything is correct, just unstyled.+ let rebuilt_runs: Vec<Vec<u8>> =+ if adjusted.iter().sum::<u64>() == wanted && !adjusted.is_empty() {+ let mut kept = Vec::with_capacity(adjusted.len());+ let mut source = runs.iter();+ for length in &adjusted {+ // Runs are consumed in order; a dropped one takes its styling with it.+ let template = source.next().cloned().unwrap_or_default();+ kept.push(set_varint_field(&template, 1, *length)?);+ }+ kept+ } else {+ vec![varint_field(1, wanted)]+ };++ // Now the note itself: the text replaced, the runs replaced, everything else copied.+ let mut out = Vec::with_capacity(note.len() + new_text.len());+ let mut reader = Reader::new(note);+ let mut wrote_text = false;+ let mut wrote_runs = false;++ while !reader.done() {+ let start = reader.at;+ let key = reader.varint()?;+ let (field, wire) = (key >> 3, key & 7);++ if field == TEXT_PATH[2] && wire == WIRE_LEN && !wrote_text {+ let n = usize::try_from(reader.varint()?).ok()?;+ reader.take(n)?;+ out.extend_from_slice(&length_delimited(field, new_text.as_bytes()));+ wrote_text = true;+ } else if field == ATTRIBUTE_RUN && wire == WIRE_LEN {+ let n = usize::try_from(reader.varint()?).ok()?;+ reader.take(n)?;+ // All of them go back in the place the first one held.+ if !wrote_runs {+ for run in &rebuilt_runs {+ out.extend_from_slice(&length_delimited(ATTRIBUTE_RUN, run));+ }+ wrote_runs = true;+ }+ } else {+ reader.skip(wire)?;+ out.extend_from_slice(note.get(start..reader.at)?);+ }+ }++ if !wrote_text {+ return None;+ }+ // A note that had no runs at all still needs them now.+ if !wrote_runs {+ for run in &rebuilt_runs {+ out.extend_from_slice(&length_delimited(ATTRIBUTE_RUN, run));+ }+ }+ Some(out)+}+/// Rewrite the text inside an existing document, leaving every other field exactly as it/// was.///@@ -267,7 +506,15 @@ pub fn replace_text(original: &[u8], text: &str) -> Option<Vec<u8>> {return None;}- rewrite(&plain, TEXT_PATH, text.as_bytes())+ // Swapping the text alone is not enough: the note's attribute runs describe how long+ // the text is, and Apple's clients map styling onto it through them. Left stale they+ // point at units that are no longer there, and the edit does not appear at all on a+ // phone — which is exactly what happened. The note is rebuilt so the runs still+ // describe the text, keeping every other byte of each run intact.+ let old_text = std::str::from_utf8(current).ok()?;+ let note = field_at(&plain, &TEXT_PATH[..2])?;+ let rebuilt_note = rebuild_note(note, old_text, text)?;+ rewrite(&plain, &TEXT_PATH[..2], &rebuilt_note)}/// Whether [`replace_text`] would accept this document, without building the result.@@ -342,6 +589,79 @@ pub fn debug_inflate(value: &str) -> Option<Vec<u8>> {inflate(&B64.decode(value.trim()).ok()?)}+/// The `length` of every attribute run inside a note document.+///+/// Apple's note is `NoteStoreProto{2: Document{3: Note}}`, and the Note carries its text at+/// field 2 and a repeated AttributeRun at field 5. Each run's own field 1 is a length. If+/// those lengths do not add up to the text, Apple's own clients have runs that no longer+/// describe what is there — which is what a note arriving blank on a phone looks like.+///+/// Returns the lengths in order, for comparing against the text they claim to cover.+#[must_use]+pub fn debug_attribute_lengths(bytes: &[u8]) -> Vec<u64> {+ let Some(plain) = inflate(bytes) else {+ return Vec::new();+ };+ // Down to the Note itself: NoteStoreProto -> Document -> Note is 2 -> 3.+ let Some(note) = field_at(&plain, &[2, 3]) else {+ return Vec::new();+ };++ let mut lengths = Vec::new();+ let mut reader = Reader::new(note);+ while !reader.done() {+ let Some(key) = reader.varint() else { break };+ let (field, wire) = (key >> 3, key & 7);+ if field == 5 && wire == WIRE_LEN {+ let Some(n) = reader.varint().and_then(|n| usize::try_from(n).ok()) else {+ break;+ };+ let Some(run) = reader.take(n) else { break };+ // The run's first field is its length.+ let mut inner = Reader::new(run);+ if let Some(inner_key) = inner.varint() {+ if inner_key >> 3 == 1 && inner_key & 7 == WIRE_VARINT {+ if let Some(length) = inner.varint() {+ lengths.push(length);+ }+ }+ }+ } else if reader.skip(wire).is_none() {+ break;+ }+ }+ lengths+}++/// The raw bytes of every attribute run in a note document, for seeing exactly what Apple+/// puts in one before writing one ourselves.+#[must_use]+pub fn debug_attribute_runs(bytes: &[u8]) -> Vec<Vec<u8>> {+ let Some(plain) = inflate(bytes) else {+ return Vec::new();+ };+ let Some(note) = field_at(&plain, &[2, 3]) else {+ return Vec::new();+ };++ let mut runs = Vec::new();+ let mut reader = Reader::new(note);+ while !reader.done() {+ let Some(key) = reader.varint() else { break };+ let (field, wire) = (key >> 3, key & 7);+ if field == 5 && wire == WIRE_LEN {+ let Some(n) = reader.varint().and_then(|n| usize::try_from(n).ok()) else {+ break;+ };+ let Some(run) = reader.take(n) else { break };+ runs.push(run.to_vec());+ } else if reader.skip(wire).is_none() {+ break;+ }+ }+ runs+}+/// A readable outline of a protobuf message: one line per field, nested.////// Rewriting a note body means preserving every field that is not the text, so the shape@@ -673,6 +993,16 @@ mod rewrite_tests {note.extend(length_delimited(3, &run));}note.extend(length_delimited(4, &length_delimited(1, &[0xAB; 28])));+ // The attribute run that makes the text renderable, carrying styling alongside its+ // length exactly as Apple's own runs do.+ note.extend(length_delimited(+ 5,+ &[+ varint(1, utf16_len(text)),+ length_delimited(2, &[0x4a, 0x02, 0xAA, 0xBB]),+ ]+ .concat(),+ ));let mut inner = Vec::new();inner.extend(varint(1, 0));@@ -701,6 +1031,80 @@ mod rewrite_tests {);}+ /// The bug both of these fix: Apple's clients map styling onto the text through+ /// attribute runs, and a run whose length no longer matches leaves the note unrenderable+ /// — a note edited here simply never changed on the phone.+ #[test]+ fn the_runs_always_add_up_to_the_text_they_describe() {+ for (from, to) in [+ ("short", "a considerably longer body"),+ ("a considerably longer body", "short"),+ ("same size", "SAME SIZE"),+ ("plain", "with an emoji 🔴 in it"),+ ("with an emoji 🔴 in it", "plain"),+ ("anything", ""),+ ] {+ let after = replace_text(&document(from), to).unwrap();+ let lengths = debug_attribute_lengths(&after);+ let text = text_from_bytes(&after).unwrap_or_default();+ assert_eq!(+ lengths.iter().sum::<u64>(),+ text.encode_utf16().count() as u64,+ "{from:?} -> {to:?}: runs {lengths:?} must cover the text exactly"+ );+ }+ }++ /// A document written from scratch needs a run too — without one, everything created+ /// from here arrived blank.+ #[test]+ fn a_created_document_carries_a_run_covering_its_text() {+ for text in ["hello", "🔴 emoji counts as two", ""] {+ let encoded = text_to_base64(text);+ let raw = B64.decode(&encoded).unwrap();+ let lengths = debug_attribute_lengths(&raw);+ assert_eq!(+ lengths.iter().sum::<u64>(),+ text.encode_utf16().count() as u64,+ "{text:?} must be covered by its runs"+ );+ assert_eq!(text_from_base64(&encoded).as_deref().unwrap_or(""), text);+ }+ }++ /// An edit in the middle must leave the runs on either side of it alone — that is what+ /// keeps a heading still a heading after you change a word underneath it.+ #[test]+ fn an_edit_only_disturbs_the_runs_it_touches() {+ // Three runs of ten units each.+ let lengths = [10, 10, 10];+ // Replace two units in the middle run with five.+ let adjusted = adjust_run_lengths(&lengths, 14, 2, 5);+ assert_eq!(adjusted, vec![10, 13, 10], "only the middle run moves");+ assert_eq!(adjusted.iter().sum::<u64>(), 33);+ }++ #[test]+ fn a_run_left_covering_nothing_is_dropped() {+ // Remove the whole middle run.+ assert_eq!(adjust_run_lengths(&[10, 10, 10], 10, 10, 0), vec![10, 10]);+ }++ #[test]+ fn appending_extends_the_last_run() {+ assert_eq!(adjust_run_lengths(&[10, 10], 20, 0, 5), vec![10, 15]);+ }++ /// The change is described in UTF-16 units, because that is what the runs count in.+ #[test]+ fn a_change_is_measured_the_way_the_runs_measure() {+ // An emoji is one char but two UTF-16 units.+ assert_eq!(utf16_change("ab", "a🔴b"), (1, 0, 2));+ assert_eq!(utf16_change("a🔴b", "ab"), (1, 2, 0));+ assert_eq!(utf16_change("same", "same"), (4, 0, 0));+ assert_eq!(utf16_change("", "new"), (0, 0, 3));+ }+/// The whole point of rewriting in place rather than re-encoding. An edit of the same/// length must leave the document byte-identical apart from the text itself, which is/// the strongest statement available that nothing else was touched.@@ -717,32 +1121,42 @@ mod rewrite_tests {.filter_map(|(i, (a, b))| (a != b).then_some(i)).collect();assert_eq!(differing.len(), 5, "only the five text bytes moved");+ // And the run still covers it, unchanged, because the length did not change.+ assert_eq!(debug_attribute_lengths(&after), vec![5]);}-- /// And when the length does change, the only fields that change are the lengths on the- /// path to the text. The version vectors keep their exact values.+ /// And when the length does change, the merge records Apple uses to reconcile edits+ /// across devices must come through byte for byte. Those are what a phone consults to+ /// decide whether our version is newer than its own.#[test]fn the_version_vectors_survive_a_save() {let before = document("first");let after = replace_text(&before, "second").unwrap();- // Sizes shift on the path by construction; the fields and their values must not.- let shape = |bytes: &[u8]| {- debug_tree(bytes, 6)- .into_iter()- .filter(|line| !line.contains("text "))- .map(|line| {- line.split_whitespace()- .filter(|token| !token.ends_with('B'))- .collect::<Vec<_>>()- .join(" ")- })- .collect::<Vec<_>>()++ // Field 3 of the note is Apple's merge state. Pull it out of both and compare.+ let merge_records = |bytes: &[u8]| -> Vec<Vec<u8>> {+ let plain = inflate(bytes).unwrap();+ let note = field_at(&plain, &TEXT_PATH[..2]).unwrap();+ let mut found = Vec::new();+ let mut reader = Reader::new(note);+ while !reader.done() {+ let key = reader.varint().unwrap();+ let (field, wire) = (key >> 3, key & 7);+ if field == 3 && wire == WIRE_LEN {+ let n = usize::try_from(reader.varint().unwrap()).unwrap();+ found.push(reader.take(n).unwrap().to_vec());+ } else {+ reader.skip(wire).unwrap();+ }+ }+ found};+assert_eq!(- shape(&after),- shape(&before),- "every field but the text must survive unchanged"+ merge_records(&after),+ merge_records(&before),+ "the merge state must survive an edit untouched");+ assert!(!merge_records(&before).is_empty(), "the fixture has some");}#[test]diff --git a/probe/src/main.rs b/probe/src/main.rsindex 1fb9e18..3f1abff 100644--- a/probe/src/main.rs+++ b/probe/src/main.rs@@ -2349,6 +2349,325 @@ async fn main() -> Result<()> {}}+ // ---- how does uploading a file actually work? ----------------------------------+ // pyicloud's upload is a two-step dance: ask docws for a signed upload URL, PUT the+ // bytes there, then tell drivews what was uploaded so it becomes a real Drive item.+ // Measure each step against the real service before writing any of it. --write-test+ // only, and it cleans up after itself.+ if std::env::args().any(|a| a == "--write-test") {+ println!("\n\x1b[1m── Drive: upload capability probe ──\x1b[0m");+ if let (Some(drivews), Some(docws)) =+ (session.service_url("drivews"), session.service_url("docws"))+ {+ let params = client.service_params();+ let http = client.http().clone();+ let bytes = b"Quince upload test - safe to delete\n";+ let name = "quince-upload-test.txt";++ // Step 1: ask for somewhere to put the bytes. This is a JSON POST, not query+ // params — sending it as query params is what "Unexpected input at column 0"+ // turned out to mean: the server tried to parse an empty body as JSON.+ let url = format!(+ "{}/ws/com.apple.CloudDocs/upload/web",+ docws.trim_end_matches('/')+ );+ let request_body = serde_json::json!({+ "filename": name,+ "type": "FILE",+ "content_type": "text/plain",+ "size": bytes.len(),+ });+ let response = http+ .post(&url)+ .query(¶ms)+ .json(&request_body)+ .send()+ .await;+ match response {+ Err(e) => println!(" upload/web request failed: {e}"),+ Ok(r) => {+ let status = r.status();+ let text = r.text().await.unwrap_or_default();+ println!(" upload/web -> {status}: {text}");+ let Ok(body) = serde_json::from_str::<serde_json::Value>(&text) else {+ println!(" not JSON, stopping here");+ return Ok(());+ };+ // Print the whole shape rather than guess a path — the response nests+ // this differently across Apple's own client versions.+ println!(+ " full parsed shape:\n {}",+ serde_json::to_string_pretty(&body)+ .unwrap_or_default()+ .lines()+ .collect::<Vec<_>>()+ .join("\n ")+ );+ let upload_url = [+ body.pointer("/url"),+ body.pointer("/data/upload_url"),+ body.pointer("/0/url"),+ ]+ .into_iter()+ .flatten()+ .find_map(|v| v.as_str())+ .map(ToString::to_string);+ let Some(upload_url) = upload_url else {+ println!(" no upload URL found at any guessed path");+ return Ok(());+ };++ // Step 2: PUT the bytes there.+ let put = http.put(&upload_url).body(bytes.to_vec()).send().await;+ match put {+ Err(e) => println!(" PUT to the upload URL failed: {e}"),+ Ok(r) => {+ let status = r.status();+ let text = r.text().await.unwrap_or_default();+ println!(+ " PUT bytes -> {status}: {}",+ text.chars().take(300).collect::<String>()+ );+ let Ok(receipt) = serde_json::from_str::<serde_json::Value>(&text)+ else {+ println!(" not JSON, stopping here");+ return Ok(());+ };++ // Step 3: tell drivews this document now belongs in a folder.+ let single_file = receipt+ .pointer("/singleFile")+ .cloned()+ .unwrap_or(receipt.clone());+ let commit_url = format!(+ "{}/ws/com.apple.CloudDocs/update/documents",+ docws.trim_end_matches('/')+ );+ let commit_body = serde_json::json!({+ "data": {+ "signature": single_file.get("fileChecksum"),+ "wrapping_key": single_file.get("wrappingKey"),+ "reference_signature": single_file.get("referenceChecksum"),+ "receipt": single_file.get("receipt"),+ "size": bytes.len(),+ },+ "command": "add_file",+ "document_id": single_file.get("document_id"),+ "path": {+ "starting_document_id": "FOLDER::com.apple.CloudDocs::root",+ "path": name,+ },+ "allow_conflict": true,+ "file_flags": { "is_writable": true, "is_executable": false, "is_hidden": false },+ "mtime": std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0),+ "btime": std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0),+ });+ let commit = http+ .post(&commit_url)+ .query(¶ms)+ .json(&commit_body)+ .send()+ .await;+ match commit {+ Err(e) => println!(" commit request failed: {e}"),+ Ok(r) => {+ let status = r.status();+ let text = r.text().await.unwrap_or_default();+ println!(+ " commit -> {status}: {}",+ text.chars().take(400).collect::<String>()+ );+ }+ }++ // Clean up whatever landed, whether or not the commit worked.+ let _ = quince_core::Library::open(&dir).ok();+ let cleanup = quince_icloud::drive::sync_folder(+ &http,+ &drivews,+ client.service_params(),+ &mut quince_core::Library::open(&dir)?,+ quince_core::drive::ROOT,+ )+ .await;+ if cleanup.is_ok() {+ let library = quince_core::Library::open(&dir)?;+ if let Some(uploaded) = library+ .drive_children(quince_core::drive::ROOT)?+ .into_iter()+ .find(|i| i.name.starts_with("quince-upload-test"))+ {+ match quince_icloud::drive::delete(+ &http,+ &drivews,+ client.service_params(),+ &uploaded,+ )+ .await+ {+ Ok(()) => println!(" cleaned up the uploaded test file"),+ Err(e) => println!(+ " \x1b[33mcouldn't clean up: {e} — {} may still be there\x1b[0m",+ uploaded.name+ ),+ }+ }+ }+ }+ }+ }+ }+ }+ }++ // ---- why does anything we write show up blank? ---------------------------------+ // Apple's note is NoteStoreProto{2: Document{3: Note}}; the Note holds its text at+ // field 2 and a repeated AttributeRun at field 5, each run's field 1 being a length.+ // If those lengths are the text measured in UTF-16 code units, then writing text+ // without them — or without updating them — leaves every Apple client with runs that+ // no longer describe what is there, which is exactly what a blank note looks like.+ println!("\n\x1b[1m── Do attribute runs carry the text length? ──\x1b[0m");+ if let Some(ck) = session.service_url("ckdatabasews") {+ let zc = quince_icloud::cloudkit::ZoneClient::new(+ client.http(),+ &ck,+ "com.apple.notes",+ client.service_params(),+ );+ if let Ok(quince_icloud::cloudkit::ZoneWalk { records, .. }) =+ zc.all_records("Notes", None).await+ {+ for rec in records+ .iter()+ .filter(|r| r.get("recordType").and_then(|t| t.as_str()) == Some("Note"))+ {+ let Some(encoded) = rec+ .pointer("/fields/TextDataEncrypted/value")+ .and_then(|v| v.as_str())+ else {+ continue;+ };+ let Ok(raw) = base64::Engine::decode(+ &base64::engine::general_purpose::STANDARD,+ encoded.trim(),+ ) else {+ continue;+ };+ let Some(text) = quince_icloud::ckdocument::text_from_bytes(&raw) else {+ continue;+ };+ let title = quince_icloud::cloudkit::field::document_text(rec, "TitleEncrypted")+ .unwrap_or_default();+ let lengths = quince_icloud::ckdocument::debug_attribute_lengths(&raw);+ let total: u64 = lengths.iter().sum();+ let utf16 = text.encode_utf16().count() as u64;++ println!(+ " {:<34} chars {:<6} utf16 {:<6} runs {:?} total {} {}",+ title.chars().take(32).collect::<String>(),+ text.chars().count(),+ utf16,+ lengths,+ total,+ if total == utf16 {+ "\x1b[32m← matches UTF-16 exactly\x1b[0m"+ } else {+ "\x1b[33m← does not match\x1b[0m"+ }+ );+ }+ }++ // What is actually inside one run? Writing one from scratch means knowing whether a+ // bare length is enough or whether Apple expects styling fields alongside it.+ println!("\n \x1b[1mThe raw bytes of real attribute runs\x1b[0m");+ if let Ok(quince_icloud::cloudkit::ZoneWalk { records, .. }) =+ zc.all_records("Notes", None).await+ {+ for rec in records+ .iter()+ .filter(|r| r.get("recordType").and_then(|t| t.as_str()) == Some("Note"))+ {+ let Some(encoded) = rec+ .pointer("/fields/TextDataEncrypted/value")+ .and_then(|v| v.as_str())+ else {+ continue;+ };+ let Ok(raw) = base64::Engine::decode(+ &base64::engine::general_purpose::STANDARD,+ encoded.trim(),+ ) else {+ continue;+ };+ let runs = quince_icloud::ckdocument::debug_attribute_runs(&raw);+ if runs.is_empty() {+ continue;+ }+ let title = quince_icloud::cloudkit::field::document_text(rec, "TitleEncrypted")+ .unwrap_or_default();+ println!(" {}", title.chars().take(30).collect::<String>());+ for run in runs.iter().take(3) {+ println!(+ " {} byte(s): {}",+ run.len(),+ run.iter()+ .map(|b| format!("{b:02x}"))+ .collect::<Vec<_>>()+ .join(" ")+ );+ }+ }+ }++ // A reminder's title uses the same document format, and also comes back blank when+ // written from here.+ println!("\n \x1b[1mReminder titles, same format\x1b[0m");+ let zr = quince_icloud::cloudkit::ZoneClient::new(+ client.http(),+ &ck,+ "com.apple.reminders",+ client.service_params(),+ );+ if let Ok(quince_icloud::cloudkit::ZoneWalk { records, .. }) =+ zr.all_records("Reminders", None).await+ {+ for rec in records+ .iter()+ .filter(|r| r.get("recordType").and_then(|t| t.as_str()) == Some("Reminder"))+ .take(4)+ {+ let Some(encoded) = rec+ .pointer("/fields/TitleDocument/value")+ .and_then(|v| v.as_str())+ else {+ continue;+ };+ let Ok(raw) = base64::Engine::decode(+ &base64::engine::general_purpose::STANDARD,+ encoded.trim(),+ ) else {+ continue;+ };+ let text = quince_icloud::ckdocument::text_from_bytes(&raw).unwrap_or_default();+ let lengths = quince_icloud::ckdocument::debug_attribute_lengths(&raw);+ let total: u64 = lengths.iter().sum();+ let utf16 = text.encode_utf16().count() as u64;+ println!(+ " {:<34} utf16 {:<5} runs {:?} total {} {}",+ text.chars().take(32).collect::<String>(),+ utf16,+ lengths,+ total,+ if total == utf16 {+ "\x1b[32m← matches\x1b[0m"+ } else {+ "\x1b[33m← does not match\x1b[0m"+ }+ );+ }+ }+ }// ---- 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 whichdiff --git a/src/components/Glyph.tsx b/src/components/Glyph.tsxnew file mode 100644index 0000000..996aaa8--- /dev/null+++ b/src/components/Glyph.tsx@@ -0,0 +1,65 @@+/**+ * The small icons the Reminders sidebar marks its built-in views with.+ *+ * Drawn rather than typed: the glyphs here used to be a mix of emoji and dingbat+ * characters — a literal 📅 next to a ☀ next to a ≡ — which every font renders at a+ * different weight, size and baseline, so the row of them never lined up and never looked+ * like it came from the same hand. These are one stroke weight on one grid.+ */+export type GlyphName = "today" | "scheduled" | "all" | "flagged" | "list";++const PATHS: Record<GlyphName, React.ReactNode> = {+ // A calendar page with the day marked — what Today means.+ today: (+ <>+ <rect x="2.5" y="3.5" width="11" height="10" rx="2.2" />+ <path d="M2.5 6.5h11" />+ <circle cx="8" cy="10" r="1.6" fill="currentColor" stroke="none" />+ </>+ ),+ // The same page, with days on it rather than one.+ scheduled: (+ <>+ <rect x="2.5" y="3.5" width="11" height="10" rx="2.2" />+ <path d="M2.5 6.5h11M5.5 2.5v2M10.5 2.5v2" />+ <path d="M5.2 9.3h1.4M9.4 9.3h1.4M5.2 11.4h1.4M9.4 11.4h1.4" />+ </>+ ),+ // Everything, as a list.+ all: (+ <>+ <path d="M5.5 4.6h8M5.5 8h8M5.5 11.4h8" />+ <circle cx="2.9" cy="4.6" r="0.9" fill="currentColor" stroke="none" />+ <circle cx="2.9" cy="8" r="0.9" fill="currentColor" stroke="none" />+ <circle cx="2.9" cy="11.4" r="0.9" fill="currentColor" stroke="none" />+ </>+ ),+ flagged: (+ <>+ <path d="M4.2 13.5V3.2" />+ <path d="M4.2 3.6h7.2l-1.7 2.6 1.7 2.6H4.2z" fill="currentColor" stroke="none" />+ </>+ ),+ // A list you made yourself, in its own colour.+ list: <circle cx="8" cy="8" r="4.4" fill="currentColor" stroke="none" />,+};++export function Glyph({ name, className }: { name: GlyphName; className?: string }) {+ return (+ <svg+ className={className}+ viewBox="0 0 16 16"+ width="16"+ height="16"+ fill="none"+ stroke="currentColor"+ strokeWidth="1.4"+ strokeLinecap="round"+ strokeLinejoin="round"+ aria-hidden="true"+ focusable="false"+ >+ {PATHS[name]}+ </svg>+ );+}diff --git a/src/screens/RemindersView.tsx b/src/screens/RemindersView.tsxindex 28829af..ee0a5e4 100644--- a/src/screens/RemindersView.tsx+++ b/src/screens/RemindersView.tsx@@ -9,14 +9,15 @@ import {} from "../lib/ipc";import { useSync } from "../lib/sync";import { Picker } from "../components/Picker";+import { Glyph, type GlyphName } from "../components/Glyph";/** Apple's built-in views, which is how most people enter Reminders. *//** Apple's built-in views, in Apple's order, each with the glyph and colour its app uses. */-const SMART: { kind: SmartList; title: string; glyph: string; colour: string }[] = [- { kind: "today", title: "Today", glyph: "\u2600", colour: "#0a84ff" },- { kind: "scheduled", title: "Scheduled", glyph: "\ud83d\udcc5", colour: "#ff453a" },- { kind: "all", title: "All", glyph: "\u2261", colour: "#8e8e93" },- { kind: "flagged", title: "Flagged", glyph: "\u2691", colour: "#ff9f0a" },+const SMART: { kind: SmartList; title: string; glyph: GlyphName; colour: string }[] = [+ { kind: "today", title: "Today", glyph: "today", colour: "#0a84ff" },+ { kind: "scheduled", title: "Scheduled", glyph: "scheduled", colour: "#ff453a" },+ { kind: "all", title: "All", glyph: "all", colour: "#8e8e93" },+ { kind: "flagged", title: "Flagged", glyph: "flagged", colour: "#ff9f0a" },];/** End of today in the viewer's own timezone — the interface knows it, the backend doesn't. */@@ -295,12 +296,36 @@ export function RemindersView() {}/**- * A smart list is a view, not somewhere to keep things — but Apple still lets you add- * from Today or All, and quietly files the new reminder in a real list. The first list- * is the closest thing this account has to a default one, which is what a smart view- * targets.+ * A smart list is a view, not somewhere to keep things, so adding from Today or All has+ * to file the reminder in a real list — as Apple's own app does.+ *+ * Which one was previously just "the first one", which is why everything landed in+ * Assignments with no explanation. Apple's default-list setting is a client preference it+ * does not publish anywhere this app can read, so rather than guess at it, the choice is+ * shown and it is yours: whichever list you last added to, changeable right there.*/- const targetListGuid = listGuid ?? lists[0]?.guid ?? null;+ const [preferredList, setPreferredList] = useState<string | null>(() => {+ try {+ return window.localStorage.getItem("quince.reminders.defaultList");+ } catch {+ return null;+ }+ });++ const chooseList = useCallback((guid: string) => {+ setPreferredList(guid);+ try {+ window.localStorage.setItem("quince.reminders.defaultList", guid);+ } catch {+ // A browser with storage switched off still works; it just forgets the choice.+ }+ }, []);++ const targetListGuid =+ listGuid ??+ (preferredList && lists.some((l) => l.guid === preferredList)+ ? preferredList+ : (lists[0]?.guid ?? null));const targetList = lists.find((l) => l.guid === targetListGuid) ?? null;async function add(e: React.FormEvent) {@@ -377,7 +402,7 @@ export function RemindersView() {><span className="rem-smart-top"><span className="rem-smart-glyph" style={{ background: smart.colour }}>- {smart.glyph}+ <Glyph name={smart.glyph} /></span><span className="rem-smart-count">{counts?.[smart.kind] ?? 0}</span></span>@@ -423,9 +448,11 @@ export function RemindersView() {onDoubleClick={() => setRenaming(list.guid)}><span- className="dot"- style={{ background: list.colour || "var(--gold)" }}- />+ className="rem-list-glyph"+ style={{ color: list.colour || "var(--gold)" }}+ >+ <Glyph name="list" />+ </span><span className="rem-list-name">{list.title}</span><span className="rem-list-count">{counts?.lists.find(([guid]) => guid === list.guid)?.[1] ?? 0}@@ -514,12 +541,16 @@ export function RemindersView() {}disabled={syncing}/>- {/* Only said out loud when it isn't obvious — inside a list, of course it goes- there. From a smart view, where it lands is worth naming. */}- {!inList && targetList && (- <span className="rem-add-target" aria-hidden="true">- {targetList.title}- </span>+ {/* Inside a list it obviously goes there. From a smart view, where it lands is+ a real choice, so it is made here rather than decided for you. */}+ {!inList && targetList && lists.length > 0 && (+ <Picker+ className="rem-add-target"+ label="List to add to"+ value={targetListGuid ?? ""}+ onChange={chooseList}+ options={lists.map((l) => ({ value: l.guid, label: l.title }))}+ />)}</form>)}diff --git a/src/styles/app.css b/src/styles/app.cssindex e0de791..59227eb 100644--- a/src/styles/app.css+++ b/src/styles/app.css@@ -735,11 +735,21 @@border-radius: var(--r-pill);display: grid;place-items: center;- font-size: 11px;- line-height: 1;color: #fff;}+/* Drawn at 16 and shown at 12, so the stroke stays crisp rather than soft. */+.rem-smart-glyph svg { width: 12px; height: 12px; }++/* A list's own colour, on the same 16px grid the heading aligns to. */+.rem-list-glyph {+ flex: none;+ width: 16px;+ height: 16px;+ display: grid;+ place-items: center;+}+.rem-smart-count {font-size: var(--t-22);font-weight: 620;@@ -886,14 +896,10 @@.rem-add input::placeholder { color: var(--text-3); }-.rem-add-target {- white-space: nowrap;- font-size: var(--t-11);- color: var(--text-3);- padding: 2px 7px;- border-radius: var(--r-pill);- background: color-mix(in srgb, var(--text) 6%, transparent);-}+/* The destination list, chosen rather than assumed. Sized to sit quietly in the add row+ rather than compete with the field it follows. */+.rem-add-target { max-width: 150px; }+.rem-add-target .picker-button { font-size: var(--t-11); padding: 3px 8px; }.rem-add:focus-within { background: color-mix(in srgb, var(--text) 4%, transparent); }/* A ring outline that is clearly not actionable, so the row reads as a draft. */@@ -1378,8 +1384,12 @@ textarea.note-text:disabled {/* ---- reminders: smart lists, inline editing, flags ------------------------------ */+/* Lines up with the list names beneath it, not with their icons — the heading used to sit+ nineteen pixels to their left, which read as a column that had slipped. 8px of row+ padding, a 16px icon, and the 8px gap between them is where the names begin. */.rem-lists-heading {- margin: 14px 8px 4px;+ margin: 16px 8px 6px;+ padding-left: 24px;font-size: var(--t-11);font-weight: 600;color: var(--text-3);