Search and contents

Precedent retrieval: ranking past cases for a current situation

Precedent Retrieval means surfacing relevant past cases for a current situation. This guide covers how CasePrecedentRetriever in engine/case_precedent_retrieval.py implements it over the Playbook.

CasePrecedentRetriever is a thin wrapper over EmbeddingStore.similarity_search (see retrieval-embedding-infra.md). It searches the Node Type's one embeddings table, limited to the document types the site's catalogue marks as case records.

It takes already-constructed collaborators, the same split RetrievalQualityGate in engine/retrieval_gate.py uses for its EmbeddingClient and EmbeddingStore. The caller owns which store, and so which physical table, is searched.

1. Build the retriever over an already-constructed client and store

from engine.embedding_store import EmbeddingStore
from engine.case_precedent_retrieval import CasePrecedentRetriever

# the Node Type's own table, the one every document is ingested into
playbook_store = EmbeddingStore(node_type_name, dimensions)
playbook_store.ensure_schema()

retriever = CasePrecedentRetriever(
    embedding_client,  # engine.embedding.EmbeddingClient
    playbook_store,
    catalogue.case_record_types,  # the starter's three: deviation, nonconformance, approved summary
    top_k=5,
    relevance_threshold=0.65,
    inference_enabled=True,  # false: retrieve() returns [] without an embedding call
)

Case record types

case_record_types are the document types that count as past cases. They are the entries of the document_types catalogue marked "case_record": true (DocumentTypeCatalogue.case_record_types).

The starter catalogue marks three:

  • deviation_report
  • nonconformance_record
  • approved_summary: an approved case summary that a person submitted with a disposition they wrote

Only chunks of those types are ranked, in the same query as the ranking. A procedure that scores higher than every case is therefore never offered as a precedent, and cannot take a place from one.

When retrieve returns [] without an embedding call

  • The catalogue marks no case record types, so there is no precedent to offer.
  • inference_enabled=False (see inference-operating-mode.md). This is the same empty result an unconfigured retriever gives.

2. Retrieve

results = retriever.retrieve("a torque reading fell outside spec on the same fixture")
# list[RetrievedChunk], ranked by similarity descending, [] if nothing clears the threshold

The full signature is retrieve(situation, context=None, *, include_inactive=False):

  • context is an ApplicabilityContext, passed exactly as you would to similarity_search directly. It is forwarded unchanged as a hard pre-filter, as in RetrievalQualityGate.retrieve.
  • include_inactive defaults to False, which searches only active documents. True also searches deactivated ones, for an analysis of what was on file.
from engine.embedding_store import ApplicabilityContext

results = retriever.retrieve(situation, context=ApplicabilityContext(product_id="widget-a"))

Deliberately simpler than Retrieval Quality Gate

RetrievalQualityGate in engine/retrieval_gate.py adds a broaden-and-retry policy on top of similarity_search. That is worth it for a point-of-use SOP question, where a thin first pass deserves a second, wider look before giving up.

Precedent Retrieval needs something narrower:

  • A known case ranks above a relevance threshold.
  • Ranking degrades gracefully (empty, not wrong) when nothing relevant exists.

A plain single-pass threshold filter gives both. similarity_search's min_similarity parameter is the relevance threshold, and the method "only ever reports what it found" (that module's own docstring). An empty result is therefore already the graceful-degradation case, and nothing needs adding on top.

Pitfalls

  • This module never fabricates a result or loosens the bar to manufacture one. Unlike RetrievalQualityGate's broadened retry, there is no second, wider pass. A situation with no genuinely relevant precedent on file returns [].
  • The default threshold of 0.65 was measured with nomic-embed-text, on the query the shift handoff builds (the text of the window's case summaries, then its activity).
    • A query with no case content scored at most 0.625 against everything stored.
    • A model-drafted summary scored at least 0.707 against its own topic.
    • Cases of another topic written in the same structured-facts shape as the query can still score above 0.8. The threshold therefore keeps the ranking honest but does not replace it: the matching topic ranks first, and the licensed prompt tells the model to use a precedent only when it matches.
    • Another embedding model needs its own measurement (how to measure again). A Node on another model is warned at start.
  • relevance_threshold and top_k need tuning against real embeddings. retrieval-quality-gate.md notes the same caveat for its own thresholds: nomic-embed-text cosine similarities for genuinely related case text don't necessarily land in the same numeric range as the hand-constructed unit vectors this module's tests use.
  • retrieve still raises when the embedding call or the database fails, so an outage is never read as "no precedent exists". A caller that can do without precedents catches that. The shift handoff does, and records the draft's precedent_retrieval as unavailable with the failure class (see Shift-handoff live license gating).
  • A past case has to be ingested as a case record type. A deviation report ingested as sop is an SOP excerpt to this class and is never a precedent. Give the document the type the catalogue marks as a case record, or mark the type.
  • The split follows the catalogue as it is now, not as the chunks were ingested.
    • Marking a type case_record (or clearing the mark) moves the chunks already stored under it between precedent retrieval and the SOP lookup when the Node is next restarted, with no re-ingest.
    • Chunks stored under a type the catalogue no longer defines count as no case record: the SOP lookup can return them and precedent retrieval never does.
    • Keep a type in the catalogue for as long as documents are stored under it, or withdraw them first with python3 -m engine.document_tools deactivate --id <document id> (see Document file sources).
  • The point-of-use SOP lookup leaves these types out, so a deviation report does not appear in an SOP answer. RetrievalQualityGate takes exclude_document_types for that, in both of its passes.

Release: v0.11.0