Files
thoughtsync/core
bvandeusenandClaude Opus 5 f90b9203a7
Desktop (Tauri) / Windows installer (cross-compiled) (push) Successful in 2m9s
Desktop (Tauri) / Tauri desktop (Linux) (push) Successful in 3m49s
Desktop (Tauri) / Update manifest (push) Successful in 6s
android: bind the core to Kotlin through uniffi (M12 step 4)
`android/ffi` is to Android what `desktop/src-tauri/src/commands/` is to the
desktop: a shim over the shared core holding no logic of its own. Third workspace
member, so the desktop lane's `cargo clippy --all-targets` compiles and lints it
— which until the Android lane lands (step 5) is the only thing that does.

Three decisions worth stating.

MIRRORED RECORDS, NOT DERIVES ON THE CORE. The core's model structs are serde
shapes contracted with the shared Vue frontend, and one of them holds a
serde_json::Value, which has no uniffi representation. Hanging uniffi derives on
them would couple two unrelated consumers to one definition. The cost of
mirroring is drift — an Android client quietly missing a field the desktop
gained — so every conversion destructures the core struct exhaustively. Add a
field to core::local::models::Note and this crate stops compiling until Android
is told what to do with it.

NoteEdit IS A LIST, NOT A STRUCT OF NULLABLE FIELDS. The store's patch format
distinguishes three states: leave alone, set, and clear to null. Kotlin cannot
express the third with a nullable field — `title = null` in a data class is
indistinguishable from `title` unset — so the editor could never clear a title.
Explicit Clear* variants say it out loud and give Kotlin a sealed class.

ASYNC IS TOKIO-BACKED, AND CANCELLATION ALREADY WORKED. Exported async methods
become Kotlin suspend functions. When a coroutine is cancelled uniffi drops the
future, and no async path in the core holds the store lock across an await —
a std MutexGuard isn't Send, so the compiler has been enforcing that all along.
A cancelled sync leaves the store consistent and simply hasn't stamped
last_sync_at, which is only written after both halves of a cycle succeed.

Also here:

  * core gains Db::conn(). Every consumer was writing
    `db.0.lock().map_err(|e| e.to_string())?` by hand, and worse, any helper
    returning the guard had to NAME rusqlite::Connection — which would have made
    rusqlite a dependency of a layer whose whole point is not knowing what the
    store is made of. Same trap as the update.rs test module in step 1.
  * The uniffi `cli` feature is gated behind our own `bindgen` feature. It drags
    in clap, askama and goblin for a three-line binary, and the desktop lane
    should not compile a code generator it never runs.
  * The bindgen binary lives in this workspace on purpose: generated bindings and
    the linked uniffi runtime are two halves of one ABI, and compiling the
    generator against the same dependency keeps them in step by construction.
    That is why ci-rust-android ships no uniffi-bindgen.

Tests cover the round trip the Android skeleton needs (open a store in a
directory that does not exist yet, write a note, read it back), that a body-only
note still has a display_title, that set and clear are genuinely different
edits, and that an unlinked app reports NotLinked rather than an error.

Known and deliberate: the workspace sets panic = "abort", so a panic crossing the
FFI aborts instead of arriving in Kotlin as an exception. Same behaviour the
desktop already has; noted in the crate header rather than silently changed.

Scribe #2733.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-18 11:09:52 -04:00
..