Inspiration
Every week, freelance developers, tenants, and small business owners sign binding legal agreements like residential tenancy contracts, software development NDAs, and vendor SOWs without $400/hour legal counsel. When people paste those contracts into standard cloud AI chatbots for a quick review, two things go wrong:
- Zero Privacy: Sensitive personal records Malaysian MyKad IC numbers (
XXXXXX-XX-XXXX), passport numbers, bank account details, phone numbers, and home addresses get uploaded straight to third-party servers. - No Actionable Redlines: Generic chatbots flag scary clauses without providing balanced, jurisdiction-grounded wording that users can actually send back in negotiations.
I built ClauseGuard as a solo project for #BuildHybridAI 2026 to prove that contract intelligence can be both zero-leakage and negotiation-ready by combining client-side deterministic redaction, on-device Gemini Nano, and Cloud Gemini Flash.
What it does
ClauseGuard is a privacy-first hybrid AI contract auditing and negotiation workbench:
- 100% Client-Side PII Shield: Before a single byte leaves the browser, deterministic redactors mask MyKad ICs, passport numbers, emails, phone numbers, bank accounts, payment cards, registration IDs, and street addresses locally with a live Raw Input vs. Redacted Preview toggle so users can verify zero personal data ever leaves their machine.
- Complexity-Aware Hybrid Risk Audit: Splits contracts (pasted text, local browser-extracted PDFs via
pdfjs-dist, or photographed pages) into individual clauses and routes routine clauses locally to Chrome Built-in AI (Gemini Nano) at $0.00 marginal cost while escalating complex multi-condition clauses and unsupported/offline browsers seamlessly to Cloud Gemini Flash. - One-Click Jurisdiction-Aware Redlines: For any
HIGHorMEDIUMrisk clause, one click drafts a side-by-side Original Clause (Redacted) vs. Recommended Fair Clause grounded in local statutory protections (such as Section 28 of the Malaysian Contracts Act 1950), complete with one-click copy, Markdown/SVG report export, and encrypted Cloud Firestore audit persistence.
How I built it
- Frontend & Client-Side Privacy Pipeline: Built with React 19, TypeScript, Vite 8, and Tailwind CSS v4. PDF parsing runs 100% in-browser via
pdfjs-dist, followed immediately by deterministic regex PII masking (piiRedactor.ts) before clause segmentation (clauseSplitter.ts). - Split-Compute Hybrid AI Engine: Integrated Firebase AI Logic (
InferenceMode.PREFER_ON_DEVICE) alongside Chrome'swindow.LanguageModel(Gemini Nano) API with strict JSON schema constraints (responseConstraint), serialized session cloning, and a 12-second timeout guard that automatically escalates dense or timed-out clauses to Cloud Gemini Flash (gemini-3.5-flash/gemini-2.5-flash-literolling fallback). - Backend & Defense-in-Depth Security: Built a Node.js + Express server (
server.ts) and parallel Python FastAPI microservice (backend/) that verify Firebase ID Tokens and Firebase App Check headers, enforce host-matched CORS and popup-safe COOP headers, and re-run server-side PII scrubbing before any cloud model call. - Auth & Persistence: Configured Firebase Authentication (Google Sign-In with automatic mobile/Safari redirect fallback, Email/Password, and Guest Reviewer anonymous sessions) and Cloud Firestore security rules to store strictly redacted audit trails.
Challenges I ran into
- CPU-Bound Gemini Nano Latency & JSON Truncation: On hardware without discrete GPUs (
Very Lowperformance class in Chrome), running 15+ clauses sequentially through local Gemini Nano caused multi-minute queues and occasional truncated JSON. I solved this with complexity-aware routing (<= 220characters locally, capped at 3 local clauses per audit) paired with a 12-second timeout guard and strict JSON validation that escalates immediately to Cloud Gemini Flash in parallel. - Cloud Counter-Clause Latency & Quota Limits: Early cloud redline calls suffered from high latency and free-tier rate limits on newer preview models. Setting
thinkingBudget: 0, lowering temperature to0.2, tuning output token limits, and implementing a rolling model fallback (gemini-3.5-flash$\rightarrow$gemini-2.5-flash$\rightarrow$gemini-2.5-flash-lite) cut counter-clause drafting to ~1.5 seconds without JSON truncation. - Mobile OAuth Popups & App Check Enforcement: iOS Safari blocked
signInWithPopup()and production App Check headers initially rejected unconfigured origins. Adding automaticsignInWithRedirectfallback and environment-aware App Check validation made both desktop and mobile sign-in seamless.
Accomplishments that I'm proud of
- True Zero-Leakage Guarantee: In my live 2-page Malaysian software NDA walkthrough, ClauseGuard masked 12 sensitive PII entities in the browser, audited all 15 clauses (catching 90-day payment traps, unpaid scope creep, background IP seizure, and Section 28 restraint-of-trade violations), and generated 7 balanced counter-clauses for under $0.06 total with the server confirming
0 server PII scrubbedbecause nothing ever leaked from the client. - Resilient Hybrid Fallback: Whether a judge tests on an Apple Silicon Mac with Gemini Nano enabled, a standard desktop browser, or an iPhone over mobile data, the hybrid router adapts automatically without breaking the user experience.
What I learned
- Hybrid AI needs smart routing, not blind routing: On-device models shine on short, structured classification tasks at zero cost, while cloud models excel at nuanced legal drafting and multi-condition reasoning combining both with strict timeouts gives users the best of both worlds.
- Privacy invariants belong at every boundary: Running deterministic PII masking both before local/cloud client calls and as a server-side guardrail guarantees that even accidental misconfigurations cannot expose user identities.
What's next for ClauseGuard
- DOCX Tracked-Changes Redline Export: Export accepted counter-clauses directly into
.docxfiles with Word-compatible<w:ins>/<w:del>tracked revisions for seamless legal redlining. - Offline IndexedDB Sync Queue: Persist offline Gemini Nano audits in encrypted browser storage and auto-sync redacted summaries to Firestore once connectivity returns.
- Client-Side WebAssembly NER & Bilingual Support: Add a lightweight local ONNX Named-Entity Recognition pass for free-form personal names alongside native Bahasa Melayu / English clause normalization for Malaysian statutory contracts.
Built With
- chrome-built-in-ai
- cloud-firestore
- express.js
- fastapi
- firebase
- firebase-ai-logic
- firebase-app-check
- firebase-auth
- gemini
- gemini-flash
- gemini-nano
- node.js
- python
- react
- render
- tailwindcss
- typescript
- vite