\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{array}
\usepackage{booktabs}
\usepackage{enumitem}
\usepackage{hyperref}

\setlength{\parindent}{0pt}
\setlength{\parskip}{0.82\baselineskip}

\title{BetterQuery Intention Bridge:\\
\textit{Architectural Foundations, Operational Guarantees, and Validation Plan}}
\author{CDLI Lab\\\texttt{research@cdli.ai}}
\date{}

\begin{document}
\maketitle

\section*{Abstract}
BetterQuery began in late 2023 as an intent-to-enhancement experiment.
This document formalizes the production architecture that has emerged:
intent-aware routing before generation, explicit fallback behavior, quota
telemetry, and distribution across API, MCP, and browser surfaces.

The design goal is simple: improve reliability and usefulness without
increasing unpredictability. The Intention Bridge makes routing explicit,
applies policy controls under uncertainty, and exposes the signals needed for
iterative calibration.

\section{Problem Definition}
Production prompt enhancement pipelines commonly fail in four modes:
\begin{enumerate}[leftmargin=*]
\item wrong intent assignment;
\item unsafe or low-value generation;
\item uncontrollable latency;
\item opaque failure behavior.
\end{enumerate}

BetterQuery addresses these with a staged architecture:
\begin{itemize}[leftmargin=*]
\item decide intent before generation,
\item apply quality gates around output,
\item enforce bounded rollout behavior under uncertainty,
\item persist structured telemetry for tuning.
\end{itemize}

\section{End-to-End Flow}
Request flow in production is:
\[
\text{sanitize}\rightarrow\text{route}\rightarrow\text{cache}\rightarrow\text{generate}\rightarrow\text{gate}\rightarrow\text{persist/emit}\rightarrow\text{response}
\]

\begin{center}
\begin{tabular}{@{}>{\raggedright}p{0.34\linewidth}>{\raggedright\arraybackslash}p{0.62\linewidth}@{}}
\toprule
\textbf{Subsystem} & \textbf{Role}\\
\midrule
Input boundary & Auth (JWT/API key), request validation, rate limits, quota checks \\
\addlinespace
Intention router & Rule checks, semantic retrieval, optional LLM arbitration, fallback \\
\addlinespace
Session layer & Encrypted context and tiered retention windows \\
\addlinespace
Enhancement engine & Architect + refiner prompt stages with circuit breakers \\
\addlinespace
Caching layer & Exact and semantic cache, similarity thresholding \\
\addlinespace
Safety/controls & Output guards, fallback and degraded-path signaling \\
\addlinespace
Observability & Per-call events, skill usage, latency, ratings, admin stats \\
\bottomrule
\end{tabular}
\end{center}

\section{Intention Bridge Formalization}
Let request embedding be \(q\in\mathbb{R}^d\), skill set \(\mathcal{S}\), and \(e_s\) the embedding for skill \(s\in\mathcal{S}\).
\[
\mathrm{sim}(q,s)=\frac{q\cdot e_s}{\|q\|\,\|e_s\|}.
\]

Let \(C_k(q)\) be the top-\(k\) candidates by \(\mathrm{sim}\), and \(a(s\mid q)\) be arbitration confidence.

\[
\pi(q)=
\begin{cases}
\text{passthrough}, & q\in\mathcal{R}_{\text{rule}}\\
\arg\max_{s\in C_k(q)}\mathrm{sim}(q,s), & \max_{s\in C_k(q)}\mathrm{sim}(q,s)\ge\tau_{\text{sim}}\\
\arg\max_{s\in C_k(q)}a(s\mid q), & \max_{s\in C_k(q)}a(s\mid q)\ge\tau_{\text{llm}}\\
\text{fallback}, & \text{otherwise}
\end{cases}
\]

This policy prefers cheap deterministic routing when confidence is high and
shifts to controlled arbitration only on ambiguous inputs.

\section{Quota Window Semantics}
Each account has rolling reset horizon of \(D\) days (default \(D=30\)):
\[
\text{Reset}(t)=t+D,\quad \text{Remaining}=\max(0,L-u_t),
\]
where \(L\) is plan quota and \(u_t\) is usage within the current window.

If stored horizon values are malformed, stale, or legacy boundary values,
the system normalizes them to a fresh rolling window. This guarantees consistent
quota behavior and avoids legacy month-based resets.

\section{Caching and Latency}
For request \(i\) with cache hit \(H_i\), cache latency \(L_c\), generation latency \(L_g\):
\[
\mathbb{E}[L_i]=P(H_i=1)L_c+(1-P(H_i=1))L_g.
\]
Since \(L_c\ll L_g\), even moderate hit rates reduce p95 significantly.

\section{Quality Control}
Enhancement is generated by a two-step pipeline:
\begin{itemize}[leftmargin=*]
\item architecture pass (intent framing and structure),
\item refine pass (clarity, actionability, tone),
\item output gate (length, identity preservation, fallback detection),
\item circuit breaker for repeated failures.
\end{itemize}

When a gate rejects, behavior remains safe and explicit rather than silently
degrading into ambiguous output.

\section{Observability}
Admin analytics currently include:
\begin{itemize}[leftmargin=*]
\item top skills by usage,
\item fallback rate,
\item average latency,
\item daily active enhancers,
\item total enhancements and active users.
\end{itemize}

User pages also persist ratings, forming explicit preference signals for
future calibration.

\section{Distribution Surfaces}
The same intention-aware policy engine serves:
\begin{enumerate}[leftmargin=*]
\item REST integration,
\item MCP clients (Claude Code, Cursor, Gemini CLI, etc.),
\item Browser extension overlay in chat and agent UIs.
\end{enumerate}

The design reduces duplicated logic while increasing accessibility.

\section{Evaluation Plan}
Benchmarking is organized as:
\begin{enumerate}[leftmargin=*]
\item \textbf{Offline}: intent stratification, date-split leakage controls, stress subsets for ambiguous prompts.
\item \textbf{Ablation}: retrieval-only, arbitration-only, no-cache, one-stage baseline, full pipeline.
\item \textbf{Online}: staged threshold experiments with fallback, latency, and preference outcomes.
\end{enumerate}

Primary metrics: intent precision/recall, fallback ratio, p50/p95 latency,
cache hit ratio, and user preference agreement.

\section{Threats and Limits}
Current risks are short-input ambiguity, label sparsity, and threshold drift.
Operational risk is mitigated through periodic calibration and conservative policy
defaults.

\section{Conclusion}
Intention-first architecture is the central control layer in BetterQuery.
It improves behavior under uncertainty, supports safe product scaling, and enables
continuous improvement from production telemetry.

\section*{References}
\begin{thebibliography}{99}
\bibitem{BERT} Devlin, J. et al. ``BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding.'' NAACL, 2019.
\bibitem{SENTBERT} Reimers, N. and Gurevych, I. ``Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks.'' ACL, 2019.
\bibitem{RAG} Lewis, P. et al. ``Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks.'' NeurIPS, 2020.
\bibitem{COT} Wei, J. et al. ``Chain-of-Thought Prompting Elicits Reasoning in LLMs.'' NeurIPS, 2022.
\bibitem{REACT} Yao, S. et al. ``ReAct: Synergizing Reasoning and Acting in Language Models.'' 2022.
\end{thebibliography}

\end{document}
