A message’s path
Every message takes the cheapest path that can answer it.
- The signature scan.
clarity-corenormalises the message and checks it against the definitions (the scam families it knows), the risk signals, and your allowlist. Amediumorhighfamily match settles the message as FAKE with no model. - Gemini Nano 4 Fast, on the phone. When the scan cannot settle it, the app asks Gemini Nano 4 Fast through Firebase AI Logic (
PREVIEW_FAST,ONLY_ON_DEVICE). A rejected or failed answer gets one retry on the stable model (STABLE,PREFER_ON_DEVICE): Gemini Nano 3 on the test phone. gemini-4-hydrogen, in the cloud. Only when the phone has no on-device model, or its model fails, does the app call the cloud model through Firebase AI Logic (Vertex AI backend,global), with a prompt built from the redacted message.
Rust decides what to ask the model and whether to trust the answer; Kotlin makes the call. The model must answer with one JSON object (verdict, family, reason, lang), and the core rejects anything else.
Timings and cost
From the signed release build, in the last live run on the test phones.
| Step | Where | Time | Cost |
|---|---|---|---|
| Signature scan | phone, offline | 0 s | free |
| Gemini Nano 4 Fast | phone | about 15 s | free |
| Nano 4 Fast, then the Nano 3 retry | phone | about 30 s for the whole message | free |
gemini-4-hydrogen, thinking LOW | cloud | about 4.7 s for the whole message | US$0.004688 for the one measured call, at list price |
The phone times are from a Pixel 9 Pro, where Nano 4 Fast runs on the CPU; the 30 s includes a first Nano 4 try that failed. The cloud time is from a Pixel 7 Pro, which has no on-device model, so the message went straight to the cloud. Across 20 test messages, a debug build saw Nano 4 Fast accepted on 16 first tries (median 23.0 s, maximum 41.9 s), and Nano 3 accepted all 4 retries.
The safety rule
A message that asks for money, an OTP, credentials, or urgency is never REAL, whatever a model says. The rule is one tested function, invariant::enforce in core/src/invariant.rs. The phone, the test tools, the scan in the browser, and the brain all use it. A model can move a verdict from REAL to ASK or FAKE, or from ASK to FAKE, and never back.
How it learns
A person who meets an unknown scam can send a report, with consent and with numbers and codes removed. Reports wait in Cloud Firestore. The brain (brain/, a Cloud Run job that runs per batch, never per message) sets aside what the definitions already catch, groups the rest with gemini-4-hydrogen, and has gemini-4-carbon write new scam families. A gate then runs the engine over the labelled test set: no REAL message may be called FAKE, and scam recall may not drop. A version that passes goes to every phone through Firebase Remote Config, with the previous version kept for rollback. Each run has a spending ceiling, checked before every model call.
In the live run, one phone’s report took the definitions from version 1 (34 families) to version 2 (36 families). The same scam went from a 35.5 s on-device model answer to a 50 ms scan answer, both FAKE. The brain can add or change families; it cannot touch the risk signals or the safety rule.
Accuracy
On a held-out slice of 60 invented English messages, scored once at the end with the final prompt on gemini-4-hydrogen, Clarity kept 20 of 20 REAL messages REAL and caught 20 of 20 FAKE ones; the 20 ASK messages came out 13 ASK, 5 FAKE, and 2 REAL. No REAL message was called FAKE. The on-device models were not scored on this slice. The test set holds 392 labelled messages, in English, Hindi, and Malayalam. See the tuning record.
Where it is in the code
| Path | What it holds |
|---|---|
core/ | the engine: routing, the prompt, the answer check, the safety rule |
ffi/ | the core for Kotlin, through uniffi |
android/ | the app; the hybrid call is in app/src/main/java/app/clarity/android/model/ |
brain/ | the hosted job that writes new definitions |
lab/ | the test tools and the gate |
wasm/ | the core for the browser, used by the scan on the home page |
definitions/, testset/ | the scam families and the labelled test messages |
Build from source
Run every command from the repository root. Nothing here spends money, deploys, or calls a model.
| Tool | Version |
|---|---|
| Rust | pinned by rust-toolchain.toml (rustup installs it) |
| JDK | 21, at $JAVA_HOME |
| Android SDK | platforms/android-36 and build-tools, at $ANDROID_HOME |
| Android NDK | r30 (30.0.16248370) |
| cargo-ndk | 4.1.2, with the aarch64-linux-android Rust target |
The gate runs cargo fmt --check, clippy with -D warnings, the workspace tests, the brain’s deploy dry run, the Android debug build with its unit tests, and a secret scan. It needs no credential, and its last line is GATE PASSED.
export JAVA_HOME=/path/to/jdk-21 ANDROID_HOME=/path/to/android-sdk
./tools/check.shTo build the app and install it on a device:
tools/build-android-lib.sh # the core as libclarity_ffi.so, for arm64-v8a
cd android
./gradlew :app:assembleDebug :app:testDebugUnitTest
adb install -r app/build/outputs/apk/debug/app-debug.apkWithout a Firebase client config file, the app still builds and runs: the scan, the allowlist, and the safety rule work, and no model is called. To add one, and for the release build and the brain, see the README’s build section, the Android README, and the brain document.
Read the contracts
- The verdict contract: the model’s answer, the prompt, and its token budget.
- The definitions file: the schema, the matching rules, and the gate.
- The brain: the run, the spend guard, publishing, and rollback.
Licence and credits
Clarity is open source under the Apache License, Version 2.0. Its dependencies are declared in the Cargo manifests and in android/gradle/libs.versions.toml, each under its own licence. Google platforms used: Android, Firebase AI Logic, Gemini Nano, Firebase App Check, Firebase Remote Config, Cloud Firestore, Firebase Authentication, Firebase Analytics, Cloud Run, Vertex AI, and ML Kit. This site is built with Astro, in Atkinson Hyperlegible Next and Mono from Google Fonts. Every test message is invented: no real numbers, names, or brands.