Why the LLM Controller Is Allowed at Most Two Retrieval Rounds
System Shadow starts from the original question; the LLM may rerank or request one supplementary retrieval, but it cannot override deterministic evidence checks or search without bound.
On this page · 12 sections
Fully deterministic retrieval is stable, but it is poor at understanding that a “configuration label” and LaunchBadge in the code may refer to the same business concept. A fully agentic search loop is more flexible, but it can rewrite the query over and over, drive up call costs, and produce an increasingly confident story without any new evidence.
The trade-off in mt_rag is to let the LLM own semantic judgment and hand rounds, actions, evidence, and output identity to deterministic code.
The Short Version
- The LLM owns semantic judgment only; deterministic code controls rounds, actions, evidence, and output identity.
- The controller has only four actions (
answer,rerank,retrieve_again,insufficient) and at most two retrieval rounds; a retrieval request in the second round is rejected and falls into the deterministic fallback. - An
answerfrom the LLM does not have the final say: without a verified path it is rejected, and the final organizer cannot modify the call chain, confidence, or citations. - The current Codex path is configured and has passed code-level verification, but live end-to-end validation is not complete.
Knowledge Map for This Article
The core concepts include role separation, the bounded controller, structured output validation, deterministic fallback, the least-privilege bridge, and the cost boundary.
Division of Labor: Codex Is Not the Embedding Model
The current role assignment is below. Look for which capabilities go to Codex and which stay with deterministic code:
| Capability | Current model/program |
|---|---|
| Query embedding | Local Qwen3 Embedding 0.6B, 1024 dimensions |
| System Shadow first query | Deterministic code, using the original question directly |
| External API rewrite / source classification | Codex CLI bridge |
| Controller decision | Codex CLI bridge + deterministic policy |
| Candidate rerank | Codex CLI bridge, or the deterministic rerank fallback |
| Business summary / final organization | Codex CLI bridge |
| Verified chain / confidence / citation whitelist | Deterministic Python |
| Manual fallback | Gemma, only on explicit switch; not the default path |
The old retrieval-usage.md still describes Gemma rewrite/answer, but the current README.md, local-llm-service.md, rag-runtime.json, and code are all Codex-first. This article follows the current code and active config and does not treat a historical runbook as a runtime fact.
More importantly, the first round of run_grounded_system_query in System Shadow performs no rewrite. Rewrite belongs to a different query entry point, such as the external Answer API, and the two entry points must not be stitched together into one pipeline that merely looks more complete.
Why the Bridge Can Only Be a Restricted Inference Worker
The Codex bridge adapts a saved Codex CLI login into a loopback OpenAI-compatible endpoint, that is, a compatible API served only on the local loopback address. Its default behavior includes:
- Listening only on
127.0.0.1:18089. A non-loopback host exits at startup, because the bridge itself has no network authentication. codex exec --ephemeral --sandbox read-only --ignore-user-config.- Reasoning effort fixed at
none, preserving the low-latency behavior of a structured worker. - Default concurrency of 1, to prevent unbounded fan-out across the controller, rerank, and renderer.
- Removing
OPENAI_API_KEYandCODEX_API_KEYfrom the child-process environment by default. - A request body limit of 1 MiB, with both the model name and message roles validated against a whitelist.
- A prompt that explicitly forbids reading files, running commands, using tools, browsing, or modifying state.
These parameters narrow the worker's privileges; they do not mean a call has no resource cost. A real Codex completion may consume account usage, so health checks and unit tests verify only the bridge and mocks. Without explicit authorization, live queries should not be sent in bulk just to “see how it does.”
The Controller Has Only Four Actions
The GroundedQueryController in System Shadow accepts only:
answer: the current evidence is sufficient;rerank: candidates exist, but their order or coverage needs improving;retrieve_again: the first round produces one concrete refined query;insufficient: the current evidence cannot support an answer.
retrieve_again must carry a non-empty refined_query. MAX_RETRIEVAL_ROUNDS=2 is a code constant: requesting retrieval again in the second round triggers a policy rejection and falls into the deterministic fallback instead of starting a third round.
Why two rounds? The first round exposes the real gap, and the second allows one targeted correction. Beyond that, the marginal gain in recall usually falls, while latency, tokens, failure surface, and the risk of “finding similar material for the sake of finding an answer” keep rising.
Two rounds is not an optimal constant from a paper. It is this engineering system's explicit budget and stopping condition.
The LLM's answer Does Not Have the Final Say Either
After the controller returns answer, the program still checks the verified path, that is, a call path whose every edge has verified evidence:
- Path confidence must be
VERIFIED. - The edge count must equal the node count minus one.
- Every edge must have an evidence ID.
- Required evidence must be fully loaded with
verification=verified. - Only the highest-priority verified path enters the developer answer.
When the LLM requests answer without a verified path, the request is rejected and converted to refined retrieval, rerank, or insufficient. The LLM cannot rewrite an AMBIGUOUS edge, Jira background, or similar text into a call fact.
The Final Organizer Cannot Modify the Canonical Answer
Deterministic code first produces DeveloperAnswerV1: status, call chain, code, overall confidence, citations, limitations, and summary facts. Codex receives only this canonical answer to organize into human-readable form, and must satisfy the following:
- The first section is
业务 Summary(business summary) and the second is简约调用链(compact call chain). - The call-chain order is entirely unchanged.
- The overall confidence is entirely unchanged.
- Citation IDs must come from the whitelist, with at most 10 in System Shadow.
- When a chain exists, every identifier must appear in the visible answer.
- When insufficient, it refuses explicitly and does not fill in parameters, return values, or business rules.
If any one of these fails, the output falls back to the deterministic renderer. Fluency may degrade; the evidence contract may not.
The Boundary Between the External API and System Shadow
The current external /v1/answer and run_grounded_system_query are two query entry points. Both use Codex for semantic roles and keep the deterministic guard, but their candidate profiles, graph depth, citation configuration, and hybrid switches are not identical.
The two-round controller in System Shadow, and the at-most-10 citations in DeveloperAnswerV1, are the strict contract this chapter discusses. The external API's top_n, code_deep profile, and answer review have their own limits; a number from one contract cannot be applied to every entry point. Before production cutover, evaluations for the two paths should also be recorded separately.
Current Validation Evidence Cannot Be Borrowed Across Runtimes
The grounded query in the historical Phase 10 completed real shadow queries using the local model of that time. The current Codex controller and final organizer, for their part, have active config, contract tests, and a deterministic smoke test. Both are real evidence, but they prove different versions.
A new live Codex query would consume account usage and hand the retrieved evidence to the current bridge, so it requires separate authorization. Until that validation is actually completed, the accurate statement is “the current Codex path is configured and has passed code-level verification,” not “live end-to-end validation is complete.”
An engineering article should obey the same provenance discipline as the system: the success of an old runtime cannot be used to supply the new runtime with a validation edge that does not exist.
Failure Modes and Trade-offs
Each row below is one failure. Look at the last column: what each protection costs.
| Failure mode | Current behavior | Trade-off |
|---|---|---|
| Bridge unavailable/timeout | Deterministic decision/answer fallback | Semantic adaptability drops, but the query remains explainable |
| LLM returns invalid JSON | The parser tries to extract a valid object; on failure, fallback | No guessing the action from free text |
| Requests answer without a verified path | Policy reject; retrieve again/rerank/insufficient | May refuse an answer a human would consider “very likely correct” |
| Requests retrieval again in the second round | Hard stop; no third round | Controls cost and latency; may miss extremely deep questions |
| LLM changes the chain/confidence | Output rejected; deterministic render | Less natural phrasing; facts are not overwritten by the model |
| More concurrent queries | Bridge semaphore defaults to 1; returns 429 when busy | Throughput yields to controllable local resources |
| API key inherited by accident | Removed from the child environment by default | An API-key-backed mode must be enabled explicitly and take on the cost gate |
Verifiable Project Evidence
src/mt_rag/service/codex_bridge.py: loopback, the CLI command, the environment filter, concurrency, and the JSONL parser.src/mt_rag/query/controller.py: the four-action state machine, the highest-hit anchor, and the two-round limit.src/mt_rag/query/runtime.py: Codex decision/answer, policy rejection, and deterministic fallback.src/mt_rag/query/answer.py: the canonical chain, confidence, citations, and insufficient.llm,model_profiles, andretrieval.controllerinconfig/rag-runtime.json: the current roles and timeouts.docs/02-runbooks/local-llm-service.md: the current boundary between Codex as primary and Gemma as manual fallback.
References
- OpenAI Codex Non-interactive Mode
- OpenAI Codex Configuration Reference
- OpenAI Codex Security
- Self-RAG
- FLARE: Active Retrieval Augmented Generation
- Corrective Retrieval Augmented Generation
The point of a controlled LLM is not to reduce what the model can do. It is to place the semantic judgment the model is good at inside a system that is observable, can say no, and has a cap on both cost and rounds.
Related notes
Local RAG Design: Store the Evidence First, Then Retrieve
How mt_rag separates replayable evidence, rebuildable query views, verified graph paths, and bounded generation—and why current source still wins.
Why a Code Hit Is Not Yet an Answer
Engineering retrieval has to bind results to a repository, a commit, and a symbol; a hit that is missing, stale, or from the wrong repository is only a locating lead.
Working on something similar?
I'm always happy to compare notes on distributed systems, delivery, and applied AI.
[email protected]