The first plan for Companion OS was semantic-first: embed every chunk, search by vector similarity, and add keyword search later “if users complain”. I reversed that decision before writing the search code, and wrote down why in ADR-0024.
What engineers actually type
Look at what goes into a search box at work. An error code: E_CONN_REFUSED. A function name:
WorkspaceScope. A config key, a ticket number, a flag. These are rare, literal tokens, and they’re the
queries where getting the exact match matters most.
Dense embeddings are systematically weak on rare literal tokens. A model compresses a chunk into a point that represents what the text is about. Two passages about connection failures land close together whether or not either contains the exact error code you pasted. That’s the strength of semantic search, and it’s exactly the wrong behaviour when you’re hunting for one specific identifier.
“Add lexical later if users complain” sounds pragmatic, but it means users have to notice a missing result, a failure that is invisible by definition. In a product whose whole promise is to cite its sources or say “I don’t know”, a retrieval miss turns into a confident “I don’t know” about something that is right there in the corpus.
The design
Every query runs two retrievers concurrently, both inside Postgres:
- Dense: pgvector with an HNSW index.
- Lexical: Postgres full-text search with a GIN index.
The two ranked lists are merged with reciprocal rank fusion:
score(d) = Σ 1 / (k + rank_r(d)) over retrievers r, with k = 60
RRF only looks at ranks, never at raw scores. That matters because cosine similarity and ts_rank live on
unrelated scales. Weighted score fusion would need calibration, and the calibration would drift every time the embedding
model or the corpus changed. With ranks, a document that sits near the top of either list surfaces; a document near the
top of both wins.
What I rejected
- Semantic-only, lexical later. Fails silently on the queries that matter most.
- Weighted score fusion. Needs calibration that doesn’t survive model changes.
- A separate engine (Elasticsearch, Meilisearch, Tantivy). Another system to run, back up and keep in sync, and another place where workspace isolation has to be enforced. Postgres FTS is good enough until a measurement says otherwise.
- Query-time routing. Guessing whether a query is “keyword-like” or “semantic” adds a classifier that can be wrong, in exchange for saving one cheap query.
How I’ll know if it works
The evaluation set has 30 golden queries over the project’s own documentation. Some are exact-token lookups, some conceptual, some multi-hop, and six can’t be answered from the corpus at all, to check that the system refuses instead of improvising. The thresholds the eval gate will enforce: recall@8 ≥ 0.80, MRR ≥ 0.60, groundedness ≥ 0.95.