Clarity

For developers

How Clarity works

Clarity is an Android app in Kotlin around a deterministic engine in Rust, clarity-core. The engine decides first. A model reads only what the engine cannot settle, and it can only move an answer towards suspicion. The code is open source under Apache-2.0.

How Clarity checks a message, and how it learns new scamsA message first goes to the signature scan, in Rust on the phone (0 s, free). If the scan cannot settle it, Gemini Nano 4 Fast reads it on the phone (about 15 s, free), with one retry on Nano 3. If the phone cannot answer, gemini-4-hydrogen reads it in the cloud through Firebase AI Logic (about 4.7 s, US$0.0047). The safety rule in clarity-core checks every answer from all three: a message that asks for money, a code, a password, or urgency is never REAL, and a model can only move an answer towards suspicion. The answer is REAL, ASK SOMEONE, or FAKE. A report of a new scam, sent only with consent and with numbers and codes removed, waits in Cloud Firestore. The brain, a Cloud Run job, groups reports with gemini-4-hydrogen and has gemini-4-carbon write new scam families; a gate tests every new version. Remote Config sends the new definitions to every phone's signature scan.A MESSAGE'S PATHHOW IT LEARNSsettledansweransweryou report a new scamnew scamdefinitionsfor the scanA messageshared, pasted, orfrom a notificationON THE PHONE1Signature scanRust, clarity-corethe scams it knows0 s · freeON THE PHONE2Gemini Nano 4 Fastif the scan cannot settle it;one retry on Nano 3about 15 s · freeIN THE CLOUD3gemini-4-hydrogenthrough Firebase AI Logic,if the phone cannot answerabout 4.7 s · US$0.0047ON THE PHONEA reportonly with your consent,numbers and codes outIN THE CLOUDCloud Firestorereports wait here,with no senderIN THE CLOUDThe brain, a Cloud Run jobgroups: gemini-4-hydrogenwrites: gemini-4-carbona gate tests every new versionIN THE CLOUDRemote Confignew definitions,for every phoneThe safety rule, in clarity-core, checks every answerA message that asks for money, a code, a password, or urgency is never REAL.A model can only move an answer towards suspicion, never back towards REAL.Your answerREALASK SOMEONEFAKEHow Clarity checks a message, and how it learns new scamsA message first goes to the signature scan, in Rust on the phone (0 s, free). If the scan cannot settle it, Gemini Nano 4 Fast reads it on the phone (about 15 s, free), with one retry on Nano 3. If the phone cannot answer, gemini-4-hydrogen reads it in the cloud through Firebase AI Logic (about 4.7 s, US$0.0047). The safety rule in clarity-core checks every answer from all three: a message that asks for money, a code, a password, or urgency is never REAL, and a model can only move an answer towards suspicion. The answer is REAL, ASK SOMEONE, or FAKE. A report of a new scam, sent only with consent and with numbers and codes removed, waits in Cloud Firestore. The brain, a Cloud Run job, groups reports with gemini-4-hydrogen and has gemini-4-carbon write new scam families; a gate tests every new version. Remote Config sends the new definitions to every phone's signature scan.A MESSAGE'S PATHHOW IT LEARNS NEW SCAMSyou report a new scamevery answerA messageshared, pasted, or from a notificationON THE PHONE1Signature scanRust, clarity-corethe scams it knows0 s · freeON THE PHONE2Gemini Nano 4 Fastif the scan cannot settle it;one retry on Nano 3about 15 s · freeIN THE CLOUD3gemini-4-hydrogenthrough Firebase AI Logic,if the phone cannot answerabout 4.7 s · US$0.0047ON THE PHONEA reportonly with your consent,numbers and codes taken outIN THE CLOUDCloud Firestorereports wait here,with no sender and no accountIN THE CLOUDThe brain, a Cloud Run jobgroups: gemini-4-hydrogenwrites: gemini-4-carbona gate tests every new versionIN THE CLOUDRemote Confignew definitions forevery phone's scanThe safety rule, inclarity-core, checks every answerA message that asks for money, a code,a password, or urgency is never REAL.A model can only move an answertowards suspicion, never towards REAL.REALASK SOMEONEFAKE

A message’s path

Every message takes the cheapest path that can answer it.

  1. The signature scan. clarity-core normalises the message and checks it against the definitions (the scam families it knows), the risk signals, and your allowlist. Amedium or high family match settles the message as FAKE with no model.
  2. 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.
  3. 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.

StepWhereTimeCost
Signature scanphone, offline0 sfree
Gemini Nano 4 Fastphoneabout 15 sfree
Nano 4 Fast, then the Nano 3 retryphoneabout 30 s for the whole messagefree
gemini-4-hydrogen, thinking LOWcloudabout 4.7 s for the whole messageUS$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

PathWhat 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.

ToolVersion
Rustpinned by rust-toolchain.toml (rustup installs it)
JDK21, at $JAVA_HOME
Android SDKplatforms/android-36 and build-tools, at $ANDROID_HOME
Android NDKr30 (30.0.16248370)
cargo-ndk4.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.sh

To 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.apk

Without 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

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.