Offline GCP IAM Assistant With Local Gemma 4 and SQLite: Five Benchmarks and What Actually Broke
Regulated Canadian teams keep asking whether a useful AI assistant can run completely offline with zero external API calls. To test that on a domain with zero tolerance for invented strings, I benchmarked a local 4-bit Gemma 4 E4B model (4.8 GB Metal VRAM, 5.4 GB RAM on an Apple M5 Pro) against 14,435 canonical GCP IAM permissions and 2,420 predefined roles stored in SQLite.
Across five controlled runs on a 45-question golden set, fixing retrieval (structure-aware chunking plus dual SQLite FTS5) drove overall hard fails to 0.00% and role recall to 93.33%, while base Gemma took 13.71s median latency. A behavioral LoRA that preserved a short 1 to 2 sentence thought block cut median latency 6.8x down to 2.02s and held exact permission recall at 90.00%, at the cost of a 4.44% residual hard-fail rate.
The Setup: Zero-Tolerance Offline Eval
Everything ran locally on an Apple M5 Pro (48 GB unified memory) with zero external API calls. The source corpus is an 8.0 GiB local SQLite crawl (315,051 URLs, 296,974 completed), filtered to 988 IAM pages (42.07 MB) and deduplicated to 901 unique documents in the hybrid index, including 356 /iam/docs/roles-permissions/* service pages. From those tables, a deterministic extractor built the ground-truth catalog: 14,435 canonical permissions, 2,420 predefined roles, and 14,274 custom-role support entries.
All five runs used the exact same model (mlx-community/gemma-4-E4B-it-4bit, temp=0.0, max_tokens=1536), the same embedding model (BAAI/bge-small-en-v1.5, 384-dim on MPS), top_k = 5, the same SYSTEM_PROMPT, and the same 45-question golden set (15 exact permission lookups, 15 least-privilege role selections, and 15 unanswerable/fake-entity refusal checks). A programmatic scorer verified every emitted permission, roles/* ID, and citation against the ground-truth catalog.
[User Question]
│
├──▶ [chunks_fts_exact: per-token quoted FTS5 (tokenchars '._/-')]
├──▶ [chunks_fts_text: Porter BM25 + authoritative table slice]
└──▶ [BAAI/bge-small-en-v1.5: 384-dim cosine similarity on MPS]
│
▼
[Weighted RRF Fusion (rrf_k=60, top_k=5)]
│
▼
[Local Gemma 4 E4B 4-bit via MLX (Base or LoRA Adapter)]
│
▼
[Zero-Tolerance Scorer vs. 14,435 Permissions / 2,420 Roles]
Five Controlled Runs: The Numbers
| Metric | Exp A: Naive Vector | Exp B: Hybrid v1 | Exp B2: Hybrid v2 | Exp C: LoRA (iter 100 / 200) | Exp C2: Thought LoRA (iter 80) |
|---|---|---|---|---|---|
| Exact Permission Recall (%) | 78.33 | 90.00 | 90.00 | 81.67 / 71.67 | 90.00 |
| Exact Permission Match (%) | 66.67 | 76.67 | 86.67 | 66.67 / 56.67 | 80.00 |
| Role Recall (%) | 70.00 | 86.67 | 93.33 | 86.67 / 86.67 | 86.67 |
| Forbidden Perm Violation (%) | 6.67 | 6.67 | 6.67 | 0.00 / 0.00 | 3.33 |
| Correct Refusal Rate (%) | 100.0 | 100.0 | 100.0 | 93.33 / 93.33 | 93.33 |
| Doc Citation Accuracy (%) | 53.33 | 76.67 | 90.00 | 76.67 / 63.33 | 76.67 |
| Overall Hard Fail Rate (%) | 2.22 | 2.22 | 0.00 | 8.89 / 6.67 | 4.44 |
| Latency P50 / P95 (s) | 11.49 / 18.87 | 11.59 / 15.94 | 13.71 / 27.42 | 1.33 / 5.73 | 1.22 / 2.03 | 2.02 / 8.48 |
| Training Time (s) | 0 | 0 | 0 | ~315s / ~627s | ~413s |
Two patterns stand out immediately:
- Retrieval sets the hard ceiling (
Exp A -> B -> B2): Structure-aware chunking plus stripping repetitiveService agent roles Warningboilerplate cut total chunks by 54% (42,349 -> 19,480) and cut MPS index build time 3.6x (362.87s -> 99.96s). Upgrading retrieval scoring alone in Experiment B2 (zero index rebuild) drove Overall Hard Fail Rate to 0.00%, Role Recall to 93.33%, and Doc Citation Accuracy to 90.00%. - Fine-tuning buys latency, not missing facts (
Exp C -> C2): Experiment C (360zero-leakage synthetic examples,0.047%trainable params,rank=8,200iters) suppressed Gemma 4's spontaneous thinking completely for a 9.5x speedup (11.59s -> 1.22sP50) and0.00%forbidden violations, but caused role-vs-permission conflation and template leakage (6.67%to8.89%hard fail). Experiment C2 (rank=8,scale=4.0,16layers,80iters,~413s) preserved a 1 to 2 sentence<|channel>thought(~20 to 50 tokens), recovering 90.00% Exact Permission Recall and 80.00% Exact Match at 2.02s P50 (6.8x faster than B2), while leaving2/45(4.44%) residual hard fails (perm-04,trick-11).
Three Real Failure Modes (What Actually Broke)
- Mid-row Markdown table slicing (
role-01): In Cloud Storage IAM docs, each predefined role and its entire permission list sit on a single Markdown table row (median2,517chars, max18,936chars). Experiment A's 1,200-char windows sliced right through the middle of a row, severing the role header from its permissions and causing Gemma to bindstorage.objects.getandstorage.objects.listtoroles/storage.expressModeServiceOutputinstead ofroles/storage.objectViewer. Experiment B2 fixed it (100%recall and match). - Duplicate REST
/deletepages drowning canonical roles (perm-08): Asked for the permission to delete a GCP project, pure dense vectors rankeddocs.cloud.google.com/iam/docs/reference/rest/v1/organizations.roles/deleteandcloud.google.com/iam/reference/rest/v1/organizations.roles/deletein Top-5 slots 1 and 2, pushingresourcemanager.projects.deleteout of context. Experiment B2'scontent_hashdedup and authoritative table BM25 slice fixed it. - Service-agent mega-roles hijacking multi-permission FTS5 (
role-12): In Experiment B, joining two exact permissions withOR("bigquery.jobs.create" OR "bigquery.tables.getData") caused giant cross-service roles that happen to hold both strings (roles/looker.serviceAgent,roles/enterpriseknowledgegraph.serviceAgent,roles/dlp.projectdriver) to outrankroles/bigquery.jobUserandroles/bigquery.dataViewer. Experiment B2 eliminated service-agent hijacking with per-token FTS5 fan-out and a0.35downweight onserviceAgentandjobfunctionsroles.
When to Use Each Stack
| Stack | Best For | Measured Accuracy / Hard Fails | Latency | Infrastructure Footprint | Main Risk |
|---|---|---|---|---|---|
| Base Gemma 4 + Hybrid SQLite v2 | Air-gapped security audits and compliance checks where correctness beats speed | 0.00% hard fail, 93.33% role recall, 90.00% doc citations | 13.71s P50 / 27.42s P95 | 4.8 GB VRAM, 5.4 GB RAM, 63 MiB index | Slow multi-second thinking traces on every turn |
| Concise-Thought LoRA (Exp C2) + Hybrid v2 | Interactive local developer assistants with strict latency budgets | 4.44% hard fail, 90.00% exact perm recall, 80.00% exact match | 2.02s P50 / 8.48s P95 | Same footprint + tiny rank=8 LoRA (~6m 53s to train) | Residual 2/45 slips on trick refusals and rare role echoes |
| Regional Canadian Cloud API + Hybrid RAG | Multi-user enterprise teams needing frontier reasoning inside Canadian residency | Not benchmarked here; depends on host model | Network-bound | Regional VPC + private endpoints (northamerica-northeast1 / Canada Central) | Egress governance, per-token spend, and cloud ops |
Appendix: Under-the-Hood Mechanics
- Cross-host 1-byte diffs defeating
content_hashdedup:cloud.google.comanddocs.cloud.google.comreturned1,901raw/iam/rows that differed by roughly 1 byte, bypassing pure hash deduplication until lowercase URL-path dedup ran first (973unique/iam/docs,988with product IAM pages), followed bycontent_hashdedup (901unique docs) to strip REST path aliases. - SQLite FTS5 dot-quoting trap: Default
unicode61splitsstorage.objects.geton.and/, blurring it withstorage.objects.list. Switching tounicode61 tokenchars '._/-'fixes tokenization, but unquotedstorage.objects.getthen raisesfts5: syntax error near ".", requiring double-quoted tokens across dual FTS5 tables (exact+porter). - Gemma 4's spontaneous
<|channel>thoughtchannel: Even withenable_thinking=Falseand no<|think|>token injected,gemma-4-E4B-it-4bitentered<|channel>thoughton 44 of 45 multi-source RAG prompts. Atmax_tokens=384,0/44finished thinking; prefilling<|channel>thought\n<channel|>failed too (Gemma printed a plain-textThought Process:plus a stray<channel|>). Allowing native generation up tomax_tokens=1536and parsing past<channel|>fixed truncation (0/45empty answers). - Gemma 4
chat_template.jinjasilently stripping training thoughts: Gemma 4's Jinja template runsstrip_thinking()on everyrole == 'model'message withouttool_calls, somlx_lm.lora's built-inChatDatasetsilently deleted all<|channel>thought...<channel|>tokens before training. Preserving concise thoughts in Experiment C2 required a customThoughtChatDatasetthat renders the prompt prefix and directly encodes the completion with special token IDs (<|channel>=100,<channel|>=101,<turn|>=106). - Checkpoint overfit past iter 100 and
scale: 20.0repetition loops: In Experiment C, training from iter 100 to iter 200 dropped Exact Permission Recall by 10 percentage points (81.67% -> 71.67%) even thoughval_losslooked flat (0.011 -> 0.007), and drove template-echo garbling ("is not present"loops ontrick-11). In the first C2 attempt, repeated imperative checklist lines in thoughts plusscale: 20.0across 8 layers triggered a 70x greedy repetition loop that never closed<channel|>. Switching to varied 1 to 2 sentence natural thoughts andscale: 4.0across 16 layers eliminated loops (0/45). - Authoritative-table boost trade-off (
role_defvsproduct_iam): Boostingrole_def(1.25) overproduct_iam(1.00) in Experiment B2 eliminated hard fails across the board, with one subtle side effect onperm-01andperm-03:roles/storage.legacyBucketReaderdisplaced/storage/docs/access-control/iam-permissions, because role tables list permissions without explaining action-level preconditions (such as object overwrite requiring bothcreateanddelete).