Inside the Query Harness: One Cheap Model Call Before Retrieval
How 3arif turns a raw question into a typed query, routes it to the right mode, and degrades to plain search when anything goes wrong.
The shape of the problem
A user types "هذا صراط علي مستقيم", or "what is the ruling on combining prayers while travelling?", or "Q 2:255". These need different retrieval. The first is a verse-meaning question in Arabic, the second is jurisprudence, the third is an explicit reference that needs no search at all.
The query harness is the layer in front of retrieval that tells them apart.
The pipeline
- Fast path. Heuristics catch explicit verse or ḥadīth references and trivial queries and skip the model entirely. The reference is resolved straight from the database.
- Understand. One call to a small, fast model (temperature 0) returns a structured object: intent, slots such as verse or narrator, sub-queries, and plan hints. The JSON parse is deliberately tolerant, because small models wrap JSON in prose.
- Route. A pure function maps intent and confidence to a mode. Below a confidence threshold, it keeps the default.
- Reformulate. Per-intent rewrites run, for example Arabic query expansion so an English question can match Arabic text.
- Retrieve. The plan feeds the normal retriever. The answer path is unchanged.
Design rules that mattered
Additive and fail-safe. Any failure, whether a timeout, a bad parse or an exception, degrades to retrieval on the raw query. The harness can make answers better; it is not allowed to make them fail.
The user's choice wins. If you pick a mode, routing short-circuits. Auto mode exists for people who do not want to choose.
Pure routing. The router is a plain function with unit tests, not another model call. That keeps routing decisions reproducible and debuggable.
One model call, not a chain. Each extra call adds latency and a new way to fail. One structured call plus deterministic stages gave most of the benefit.
How we know it helps
A retrieval evaluation runner scores recall@k and MRR on a golden set and can compare modes side by side (for example fast against auto). Changes to the harness are judged on those numbers, not on a few good-looking examples.
What is next
Per-mode profiles are already configurable, and the harness feeds the same signals into the tafsīr and istidlāl features. The honest limit today is the size of the golden set, and growing it is the highest-value work on the list.