Search and contents

Retrieval quality gate: strong / broadened / incomplete

RetrievalQualityGate in engine/retrieval_gate.py decides what to do with a query's retrieval, on top of EmbeddingStore.similarity_search (docs/operations/retrieval-embedding-infra.md). The mechanism at the core of the module is:

  • a strong match proceeds;
  • a weak match broadens and retries;
  • no match flags incomplete, rather than fabricating an answer.

EmbeddingStore.similarity_search only ever reports what it found. It never decides what a caller should do about an empty or thin result. RetrievalQualityGate is that decision, so a caller (PlaybookAnswerer in engine/playbook_answer.py, or a future consumer) doesn't have to invent its own strong/weak/none policy from scratch.

1. Build the gate over an already-constructed client/store

from engine.retrieval_gate import RetrievalQualityGate

gate = RetrievalQualityGate(
    embedding_client,  # engine.embedding.EmbeddingClient
    embedding_store,  # engine.embedding_store.EmbeddingStore
    top_k=5,
    strong_threshold=0.75,
    weak_threshold=0.65,
    broaden_similarity_step=0.05,
    broaden_top_k_multiplier=2,  # the retry asks for top_k * this many
    exclude_document_types=None,  # e.g. the case record types, for the SOP lookup
)

exclude_document_types leaves the named document types out of every search, the first pass and the broadened retry alike. It does so in the same SQL as the ranking, so an unwanted type cannot use up a place a wanted chunk would have taken (case-precedent-retrieval.md uses it to keep case records out of an SOP answer). Broadening loosens the score bar and the count, never this.

The two bars

strong_threshold and weak_threshold are both similarity-score bars, not a hardcoded trichotomy.

  • The first pass always searches at weak_threshold, the widest bar the gate accepts without broadening. Whether that pass's top hit clears strong_threshold is what decides STRONG versus everything else.
  • The broadened retry searches at weak_threshold - broaden_similarity_step (0.60 by default). That lower bar is the one below which a hit is not an answer: INCOMPLETE means nothing reached it.

The defaults are the values shown. They were measured with nomic-embed-text on a small set of documents:

  • a question the documents answer scored at least 0.645;
  • a question about something else entirely scored at most 0.572;
  • a question that only touches the same subject scored 0.60 to 0.69, so it is treated as answerable.

The bars were measured on one embedding model, nomic-embed-text with 768 dimensions, and the gate records that profile as RetrievalQualityGate.BARS_MEASURED_ON. The same model gave the precedent relevance threshold of 0.65 (case-precedent-retrieval.md).

A different embedding model scores on a different scale. On one, the bars may refuse an answerable question or let through one the documents do not answer. A Node whose embedding section names another model, or the same model at another width, says so:

  • it logs a warning when it starts;
  • deployment/doctor.sh prints a WARNING retrieval-bars line for the staged configuration.

Neither stops the Node, and neither changes a bar. The comparison is of the model's name (case and a :latest tag aside) and its width, so another build or quantization under the same name is not seen. The warning is there so that nobody trusts a bar that was measured for another model.

Measuring the bars again

Measure on the Node's own embedding model and the site's own documents, after they are ingested. Write three short lists of questions: ones the documents answer, ones about something else entirely, and ones that only touch the same subject. For each question read the top similarity the store reports:

from engine.embedding import EmbeddingClient, EmbeddingProfile
from engine.embedding_store import EmbeddingStore

profile = EmbeddingProfile(
    endpoint="http://embed.example.local:11434", model="the-model-you-use", dimensions=768
)
client = EmbeddingClient(profile)
store = EmbeddingStore("MES", profile.dimensions, embedding_model=profile.model)


def top_similarity(question: str) -> float:
    hits = store.similarity_search(client.embed([question])[0], top_k=1, min_similarity=0.0)
    return hits[0].similarity if hits else 0.0

The bar below which a hit is not an answer (weak_threshold less broaden_similarity_step, 0.60 by default) belongs between the lowest score of the answered questions and the highest of the unrelated ones. The scores of the questions that only touch the subject show how many of them the bar lets through.

The precedent threshold is measured the same way, with the text of a window's case summaries as the query. Keep the measurements: they are what a later change of model is compared with.

The Node does not take the bars from its configuration. It builds the gate with the defaults above. What a measurement tells a site is whether those defaults suit its model. Where they do not, the fix is a change to the engine's defaults, which means changing MEASURED_BARS and BARS_MEASURED_ON together (a test holds the record to the defaults).

2. Retrieve

result = gate.retrieve("What's the procedure for calibrating a torque wrench?")
result.outcome  # RetrievalOutcome.STRONG | BROADENED | INCOMPLETE
result.chunks  # list[RetrievedChunk] - [] only for INCOMPLETE
result.attempts  # 1 (no retry) or 2 (the broadened retry ran)

Pass context (an ApplicabilityContext) exactly the way you would to similarity_search directly. It is forwarded unchanged to both the first pass and the broadened retry:

from engine.embedding_store import ApplicabilityContext

result = gate.retrieve(
    question, context=ApplicabilityContext(product_id="widget-a", unit_number=4812)
)

The three outcomes

  • STRONG: the first pass's top hit already cleared strong_threshold. There is no retry, and attempts=1.
  • BROADENED: the first pass wasn't strong, because either its top hit was below strong_threshold or the pass returned nothing at all. The gate therefore retried once at a lower similarity bar (weak_threshold - broaden_similarity_step, default 0.05 lower) and a wider top_k (default doubled). That retry found something, and attempts=2.
  • INCOMPLETE: even the broadened retry came up empty. chunks is []: there is nothing to proceed with, and nothing fabricated to paper over it.

Why "no matches" and "weak matches" both get one retry

STRONG, BROADENED and INCOMPLETE are three possible outcomes, not three hard-separated code paths keyed off the first pass alone. A first pass with a merely weak hit and a first pass with literally zero hits are both just "not strong yet". Both get the same one broadened retry, and INCOMPLETE only fires when that retry also finds nothing.

Denying a query any second look purely because its first, narrower pass returned nothing is a worse failure mode than the bounded, one-shot cost of trying once more with a wider net.

Applicability never broadens

context is passed unchanged to both passes. The broadened retry widens the similarity bar and top_k, never the applicability filter.

A wrong-product or wrong-Mfg-Order instruction doesn't become correct by lowering the similarity bar. Relaxing that filter to manufacture a result would trade an INCOMPLETE (honest) failure for a wrong-context one (actively worse), so the gate's broadening step is not allowed to touch it.

An older revision with no range yields to a newer one

Both passes also ask the store to leave out an older revision of a kept document that has no range of its own, when a newer active revision of the same document applies to the same context (see A newer revision with no effective range). This is applied in the same query as the ranking, so a revision left out never uses up one of the top_k places.

The result's superseded list names each revision left out and the revision that replaced it. It is empty when none was, or when context is not given. PlaybookAnswerer ends its answer with a code-written Source note for each.

The gate has no setting to turn this off. A caller that wants every revision calls EmbeddingStore.similarity_search itself.

Wired into PlaybookAnswerer

PlaybookAnswerer in engine/playbook_answer.py takes a RetrievalQualityGate in its constructor and calls retrieve() instead of EmbeddingStore.similarity_search directly (see docs/operations/playbook-answer-synthesis.md).

  • grounded=False with NOT_ENOUGH_INFORMATION maps onto INCOMPLETE. grounded is also False, without the gate being consulted, when the answerer is built with inference_enabled=False.
  • STRONG and BROADENED both proceed to synthesis.

PlaybookAnswer doesn't surface the gate's full outcome. A caller that needs to know whether an answer came from a STRONG or a BROADENED retrieval should call RetrievalQualityGate.retrieve itself rather than go through PlaybookAnswerer.

Pitfalls

  • A BROADENED result isn't necessarily lower quality; it is lower confidence. The retry might land on a genuinely strong hit. The outcome still reports BROADENED because reaching it required widening the net, which is signal worth keeping even when the eventual chunk was fine.
  • attempts counts retrieval passes, not model calls. This gate never calls an inference model. It is a decision over what similarity_search returns, nothing more.
  • Tune strong_threshold and weak_threshold against real embeddings, not the synthetic unit vectors this module's own tests use. nomic-embed-text cosine similarities for genuinely related text don't necessarily land in the same numeric range as hand-constructed test vectors. Verify these defaults against real Playbook content before trusting them at a real site.

Release: v0.11.0