Corrective Retrieval
Level 2. Rewrite the question with the conversation in view, search semantic and keyword, fuse by rank, and grade again. One pass, no new infrastructure.
Level 2 runs when Level 1 falls short and the grader did not call the message conversation. It rewrites the question, searches again two ways, fuses the results, and grades them. The nodes are rewrite and hybrid_retrieve in agent/templates/documents.py, followed by regrade; the search is retrieve() in retrieval/search.py.
Level 1 asks the question as the user typed it. Level 2 asks it the way the documentation would answer it.
The flow
rewrite structured output · prompts/<client_id>/rewrite.md
│ in: recent conversation · the message · why Level 1 fell short
│ out: queries (≤ corrective.query_rewrites) · keywords
▼
hybrid_retrieve → retrieve() retrieval/search.py
├─ embed_queries() [message, *queries] in one Voyage call
├─ _dense() per vector pgvector cosine · drop floor · top k
├─ _keyword(keywords) websearch_to_tsquery over chunks.text_search (if retrieval.keyword.enabled)
└─ rrf_merge() each list adds 1 / (rrf_k + rank) per chunk → top_k
▼
regrade → decide_level2 see Grading and The Guardrail
The rewrite returns two different things for two different searches:
queries: standalone questions phrased in documentation vocabulary, with references like “that” resolved. Each is embedded and searched.keywords: one to four exact terms that would appear verbatim in the right page: an error code, a permission name, an annotation. Matched with Postgres full-text search.
A worked example
Two turns from a real run.
“so is that the same as the health check timing out?” (following a question about the PORT error)
- Level 1 found two passages at
0.36, and the grader returned0.15. rewriteresolved “that” from the conversation and returned three queries:- Cloud Run health check timeout and container startup failure
- Cloud Run container failed to start listening on PORT environment variable versus health check failure
- how health check timeout differs from container PORT environment variable error in Cloud Run
- It also returned the keywords
PORT health check. hybrid_retrievefused four dense searches and one keyword search into five passages.regradereturned0.95, and the turn was answered with three citations.- Level 2 took 8 s: rewrite 3.6 s, hybrid search 377 ms, regrade 4.1 s. The turn cost $0.039, against $0.026 for the Level 1 answer before it.
“How do I configure a Cloud SQL read replica to fail over automatically?”
- Level 1 found one passage at
0.33, from a Cloud Run page. rewritereturned three Cloud SQL queries and the keywordsCloud SQL read replica failover.- The keyword search matched nothing, and the dense searches returned two Cloud Run and GKE autoscaling passages.
regradereturned0.05, and the turn escalated. Level 2 cannot invent coverage the corpus does not have.
Design decisions
Rewrite plus hybrid search, not web search
The textbook corrective tier reaches for the web. For this corpus it would add latency before the same ticket.
| Web search | Rewrite + hybrid search (built) | |
|---|---|---|
| The questions that reach Level 3 | Depend on the user’s own project, which no public page describes | Same, and it doesn’t pretend otherwise |
| What it fixes | Gaps in the corpus | Vocabulary mismatch, vague follow-ups, exact identifiers |
| Citations | A different block shape, from sources nobody curated | The same search_result blocks, from the corpus |
| Eval determinism | Changes with the web | Changes with the corpus |
| New infrastructure | A search API and its key | One generated column and a GIN index |
Fuse ranks, not scores
- Cosine similarity and
ts_rank_cdare on unrelated scales. Adding them would be meaningless. Positions are comparable. - Reciprocal rank fusion. Each list adds
1 / (rrf_k + rank)for every chunk it contains.rrf_kdefaults to 60, which is the constant from the original RRF paper, and it damps the gap between rank 1 and rank 2 so no single list dominates. - Agreement rises. A chunk that several query variants and the keyword search all found outranks one found once.
- Keyword hits carry
similarity: 0.0. They have no cosine similarity, and the code says so rather than inventing one. That is also why Level 2 has no cosine floor.
The keyword half
- A generated column.
chunks.text_searchisto_tsvector('english', text), generated by Postgres (migration0005). Ingest never writes it, so it cannot drift from the chunk. websearch_to_tsquery, notto_tsquery. It never raises on user-shaped input.- Every keyword must match. The query ANDs its terms, so the prompt asks for one to four precise ones.
iam.serviceAccounts.actAsmatches exactly two chunks in this corpus. - The original message is always searched too. A rewrite can only add recall, never lose the Level 1 hits.
- One embedding call. The message and every rewrite are embedded together.
One pass
- No loop from
regradeback torewrite. Level 2 runs once, so its cost is bounded and its trail has a fixed shape. - No multi-hop. A question that needs one search to inform the next wants a model holding a tool. The loop engine that did that was retired.
Configuration
| Key | Default | What it does |
|---|---|---|
agent.corrective.enabled | true | Off sends a turn that fails Level 1 straight to Level 3 |
agent.corrective.query_rewrites | 3 | Query variants the rewrite returns, each embedded and searched. Bounded 1 to 5 |
agent.retrieval_drop_floor | 0.3 | Applied to the dense half. The keyword half has no floor |
retrieval.top_k | 5 | Chunks kept per list and after fusion |
retrieval.rrf_k | 60 | The fusion constant |
retrieval.keyword.enabled | true | Off skips the keyword search, even when the rewrite returned keywords |
The rewrite prompt is rewrite.md, beside the answer prompt.
Next: Answering, which both levels hand their passages to.