Review the prerequisites
Suitable for: Backend developers learning Agent applications.
- Entity disambiguation and constraint preservation in query rewriting
- The purpose of rewriting is to express the known intention more retrievably and not to create missing facts. Entity, negation, time, and scope must have a source, and ambiguities affecting the answer need to be preserved or clarified.
How does the mechanism work?
Step-by-step explanation
- What should I do if the two candidates are very similar?
Move from resolving references to ambiguities that can change the answer.
- How can a user retain an entity that he just rejected in the last round?
Multi-turn state must handle corrections, rather than merely accumulate related terms.
- How to deal with different definitions brought by query expansion?
Even when broader coverage improves, prevent different business definitions from being merged.
Debugging · Diagnose failures
Diagnose failures · Entity disambiguation and constraint preservation in query rewriting
Objectives of this level: Locate guessing and constraint-loss faults before retrieval tuning.
Visual guide · Resolve intent without adding facts
This function validates an already extracted intent, not a model rewrite. Preserve exclusions and dates; current access must be checked again at retrieval. Select the figure to open it at full size.
Same after-sales project
The after-sales user asks “Can it be returned, excluding gifts, during October?” after comparing two orders. This exercise validates a structured intent envelope from already extracted inputs; it does not parse natural language, generate a model rewrite or measure retrieval quality. The server’s current allowed_entities narrows candidates but mentioning an order does not grant access.
The existing capstone main-return creates a return application; it does not execute a refund. These manifests are independent fictional extensions or policy/evidence/counting exercises. They do not add real refunds, publication, model calls or Java execution to the capstone runtime.
Same-input fault reproduction
Change the ambiguous branch in your copy to pick the first candidate. On the same ambiguous payload, the first failure is entity resolution, before vector search. Retain unauthorized-choice as a separate guard and preserve-negation as a rewrite control; repair both resolution and constraint copying without changing the case.
{
"text": "Can it be returned, excluding gifts, during October?",
"candidates": [
"order-a",
"order-b"
],
"allowed_entities": [
"order-a",
"order-b"
],
"selected_entity": null,
"constraints": {
"exclude": [
"gift"
],
"from_date": "2026-10-01",
"to_date": "2026-10-31"
}
}
Use this expected result from the foundation sample, unchanged:
{
"expected": {
"status": "clarify",
"entities": [
"order-a",
"order-b"
],
"text": "Can it be returned, excluding gifts, during October?",
"constraints": {
"exclude": [
"gift"
],
"from_date": "2026-10-01",
"to_date": "2026-10-31"
}
},
"error": null
}
Evidence before repair
After implementing, preserve a faulty student-before.py. Run the before command, inspect its first actual mismatch, then repair student.py and rerun the after command. Keep both reports, source hashes/diff, an evidence→hypothesis→distinguishing-evidence note and the named negative control. Reports may contain an earlier first mismatch than the named probe; explain both. The public suggested fault is a reproducible exercise, not an unannounced incident or unseen test.
python3 lab_runner.py grade --course rag-query-rewrite --module student-before.py --out before-report.json
python3 lab_runner.py grade --course rag-query-rewrite --module student.py --out report.json
import json
from pathlib import Path
report = json.loads(Path("before-report.json").read_text())
first = next((row for row in report["checks"] if not row["passed"]), None)
print(json.dumps(first, indent=2, ensure_ascii=False))
Optional retrospective after evidence
No. Locate constraint loss before retrieval and preserve the original exclusion evidence.
code example
Preserve the same-input red/green repair
Change the ambiguous branch in your copy to pick the first candidate. On the same ambiguous payload, the first failure is entity resolution, before vector search. Retain unauthorized-choice as a separate guard and preserve-negation as a rewrite control; repair both resolution and constraint copying without changing the case.
Acceptance task for this level
Freeze a faulty copy, submit evidence and competing hypotheses, reproduce the same-input failure, repair it and preserve red/green reports plus a negative control. Contract: build_query_plan(payload).
Reading this browser’s record…
Hide the answer and check your understanding
A rewrite dropped “excluding gifts”; is a new embedding model the first fix?
Expand reference derivation
No. Locate constraint loss before retrieval and preserve the original exclusion evidence.
Further explanations and practice
When encountering unfamiliar principles, first read the implementation, continuous questioning and migration cases, and then independently explain the premise and boundaries. Answers and notes are saved to the original account record.
All linked explanations and exercises (1 )
Sources and verification scope
The principles are based on public information; the numbers, cases and tasks are the teaching design of this website. Offline experiments verify the range noted on this page, and the learning effect still needs to be judged through independent tasks and feedback.