|
| 1 | +# Evaluation harness (Phase 8) — plan v2.4 §9.1–§9.6, §15 row 8 |
| 2 | + |
| 3 | +**Added 2026-10-21.** This document describes the frozen-corpus evaluation |
| 4 | +workflow exactly as implemented and verified. The canonical requirements live in |
| 5 | +`RAG_Platform_Master_Plan_v2.4.md`; this file documents behavior, not new rules. |
| 6 | +Every number below is a measured value from a recorded run or a fresh test run |
| 7 | +of 2026-10-21; nothing is inferred, rounded up, or borrowed from another |
| 8 | +configuration (Z8). |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +## 1. Safety envelope (what an eval run is allowed to do) |
| 13 | + |
| 14 | +- **Generation is disabled for every run.** `local_generation_enabled=False`, |
| 15 | + `generation_model=None`, `retrieval_score_floor=None` — the shipped defaults |
| 16 | + are the only configuration the harness accepts. If settings ever carry |
| 17 | + `local_generation_enabled=True`, `run_eval` raises `EvalConfigError` and the |
| 18 | + CLI exits `2`; no question is asked, no file is ingested, no report is |
| 19 | + written. There is deliberately **no CLI flag that enables generation** (the |
| 20 | + argparse destination set is exactly `dataset, split, tuning, top_k, out`). |
| 21 | +- **Offline by construction.** An eval run performs zero socket operations of |
| 22 | + any kind — proven under a total socket guard (`connect()` *and* |
| 23 | + `getaddrinfo()`, loopback included) in |
| 24 | + `tests/egress/test_eval_no_outbound.py`, covering the harness run, the CLI |
| 25 | + command, a tampered-corpus refusal, and two back-to-back runs. |
| 26 | +- **No model loads.** A run never imports `sentence_transformers` or `torch` |
| 27 | + (the lazy MiniLM loader is never triggered). |
| 28 | +- **Read-only corpus.** The frozen dataset is only read; a full byte-snapshot |
| 29 | + (SHA-256 per file, before/after) proves verification changes nothing |
| 30 | + (`tests/eval/test_frozen_corpus.py`). |
| 31 | +- **Isolated per-run storage.** Ingestion and evaluation happen in a fresh |
| 32 | + SQLite database per run under `data\eval_runs\<run_id>\` (D:, gitignored). |
| 33 | + The application database is never touched by `eval` (only the explicit, |
| 34 | + manifest-confined `ingest` subcommand writes to a database of its choosing). |
| 35 | + |
| 36 | +## 2. Frozen dataset contract |
| 37 | + |
| 38 | +Layout: `corpora\<dataset_id>\` with `manifest.json`, `documents\…`, |
| 39 | +`questions\questions_dev.jsonl`, `questions\questions_test.jsonl`. |
| 40 | + |
| 41 | +**Manifest** (`schema = rag-platform-eval-manifest/1`) — verified fully before |
| 42 | +anything else happens; any failure aborts the run (`DatasetIntegrityError`, |
| 43 | +CLI exit `3`): |
| 44 | + |
| 45 | +- required keys, `dataset_id` (must match `\A[a-z0-9][a-z0-9_-]*\Z`, at most 32 |
| 46 | + characters — never normalized/stripped; enforced during verification), chunk |
| 47 | + parameters (`chunk_size=1000`, |
| 48 | + `chunk_overlap=150` — must equal the chunker's configured values); |
| 49 | +- exactly the two splits present (`dev`, `test`), each question file with its |
| 50 | + SHA-256 and size; |
| 51 | +- per-file SHA-256 + size for every listed document; |
| 52 | +- **path confinement**: absolute paths, drive letters, backslashes, `..` |
| 53 | + traversal, leading dots, symlinks, and duplicate paths are rejected; |
| 54 | +- documents may not carry a split; question files must declare one. |
| 55 | + |
| 56 | +**Question records** (JSONL) — keys `id`, `type`, `expected_outcome`, |
| 57 | +`question`, `gold_chunk_ids`, `gold_answer`, `gold_answer_span`, `structures`: |
| 58 | + |
| 59 | +- `type` ∈ {single-hop, multi-hop, unanswerable, conflict-aware, |
| 60 | + injection-adjacent}; `expected_outcome` ∈ {answer, abstain, flag-conflict}; |
| 61 | +- label rules: `answer` ⇒ non-empty gold set + gold answer; `abstain` and |
| 62 | + `unanswerable` ⇒ no gold; `gold_answer_span` must reference a gold chunk with |
| 63 | + `0 ≤ char_start < char_end`; IDs unique per file; ≤ 2000 chars per question; |
| 64 | + ≤ 500 questions per file. |
| 65 | + |
| 66 | +**The frozen corpus (`corpora\synthetic_v1`, locally authored):** 24 documents |
| 67 | +— 10 TXT / 8 MD / 6 PDF, 63,090–84,641 bytes each (within the plan's |
| 68 | +50–150 KB band) — plus 114 questions: **dev 70** (27 single-hop, 24 multi-hop, |
| 69 | +9 unanswerable, 3 conflict-aware, 7 injection-adjacent) and **test 44** (13/16/ |
| 70 | +7/3/5), i.e. 40 multi-hop and 16 unanswerable overall. Gold spans and gold |
| 71 | +chunk IDs were computed with the production chunker (`load_document` / |
| 72 | +`chunk_text` / `document_id` / `chunk_id`), not by hand. |
| 73 | + |
| 74 | +> **Storage status (owner decision pending).** `.gitignore` line 20 states |
| 75 | +> "frozen eval datasets ARE committed; raw corpora are not", yet line 21 |
| 76 | +> ignores `corpora/` entirely — the frozen corpus therefore cannot be staged |
| 77 | +> as-is. Per constraints, `.gitignore` was **not** changed and the corpus is |
| 78 | +> **not** staged. The precise conflict and the minimal proposed exception are |
| 79 | +> recorded in `RAG_Platform_Plan_Change_Note_v2.4.md` §12 for the owner. |
| 80 | +
|
| 81 | +## 3. Splits and tuning protection |
| 82 | + |
| 83 | +- Thresholds tune on **dev** only; **test** is evaluated once per candidate |
| 84 | + configuration. |
| 85 | +- `load_questions(..., tuning=True)` refuses the test split **before the file |
| 86 | + is opened** — proven by deleting the test file: the tuning refusal message |
| 87 | + differs from the ordinary "unreadable" error, which demonstrates the |
| 88 | + ordering. |
| 89 | +- Every run records `prior_test_split_evaluations` — a dataset-wide count of |
| 90 | + prior test-split `run.json` files — in its report (surfacing |
| 91 | + overfitting-by-repetition). **Limitation:** the counter reflects what is on |
| 92 | + disk; deleting `data\eval_runs\` resets it. |
| 93 | +- No test-split evaluation has been burned: the recorded runs are dev-only |
| 94 | + (`prior_test_split_evaluations = 0`). |
| 95 | +- No external grading, downloaded judge, or comparison against other systems |
| 96 | + exists anywhere (author–evaluator risk disclosed per R19; see §9). |
| 97 | + |
| 98 | +## 4. Running an evaluation (CLI) |
| 99 | + |
| 100 | +```powershell |
| 101 | +$env:PYTHONPATH = "src" # once per session (README §7) |
| 102 | +.venv\Scripts\python.exe -m rag_platform.cli eval --dataset corpora\synthetic_v1 --split dev |
| 103 | +.venv\Scripts\python.exe -m rag_platform.cli eval --dataset corpora\synthetic_v1 --split dev --tuning |
| 104 | +.venv\Scripts\python.exe -m rag_platform.cli ingest --dataset corpora\synthetic_v1 |
| 105 | +``` |
| 106 | + |
| 107 | +- `eval` options: `--split {dev,test}` (default `dev`), `--tuning`, |
| 108 | + `--top-k` (1–20, default 10), `--out` (default `<project>\reports`). |
| 109 | + Both `--dataset` and `--out` must resolve inside the project root. |
| 110 | +- `ingest` ingests **only** manifest-listed documents, after full manifest |
| 111 | + verification, into a SQLite database; ambient files in the same folders are |
| 112 | + ignored (tested). Question labels are ignored by ingest — it never reads |
| 113 | + gold into the index. |
| 114 | +- **Exit codes:** `0` clean run; `2` invalid input (bad top-k, missing/outside |
| 115 | + dataset, `--out` outside the project, `--tuning` with `--split test`, |
| 116 | + `EvalConfigError`); `3` integrity failure (tampered manifest/file, gold |
| 117 | + mismatch) or an unexpected internal error (exception *class name* only — |
| 118 | + never a traceback or message); `4` the run completed but some questions |
| 119 | + raised typed errors — per-query errors stay in the denominators, the report |
| 120 | + is written with `error_classes` and `retrieval_error_count`. |
| 121 | +- Reports print a JSON summary to stdout (report paths included); raw queries |
| 122 | + are **not** dumped by default. The advisory quarantine notices you may see |
| 123 | + (`quarantined_chunks=1`) are metadata-only findings on planted injection |
| 124 | + documents — content is preserved, chunks excluded from use. |
| 125 | + |
| 126 | +## 5. Metrics (stdlib-only; definitions frozen in `eval/metrics.py`) |
| 127 | + |
| 128 | +| Metric | Definition | Denominator / honesty rule | |
| 129 | +|---|---|---| |
| 130 | +| Recall@5 / @10 | \|gold ∩ top-k\| / \|gold\| | macro over answer-labeled questions with a non-empty gold set; a retrieval error scores 0 for that question — **errors never leave the denominator** | |
| 131 | +| Precision@5 / @10 | \|gold ∩ top-k\| / k | missing slots count non-relevant (denominator is k) | |
| 132 | +| MRR | 1 / rank of first gold hit (0 if none) | same answerable set; errors → 0 | |
| 133 | +| nDCG@10 | binary gains: Σ 1/log₂(rank+1) over gold in top 10, normalized against the ideal ordering of min(\|gold\|, 10) | same answerable set; errors → 0 | |
| 134 | +| Abstention correctness | evidence-level abstentions (`no_candidates`, `insufficient_evidence`, `no_usable_evidence`) among **abstain-labeled** questions / abstain-labeled count | `generation_unavailable` counts **incorrect** (disabled generation is not a correct refusal); the count is printed in the metric note | |
| 135 | +| False-refusal rate | answer-labeled questions that received an evidence-level abstention / answer-labeled count | flag-conflict questions are a separate class — no conflict metric at M1, none invented | |
| 136 | +| Retrieval-only rate | responses with `generation_outcome == "unavailable"` / all | reflects configuration, not quality | |
| 137 | +| Citation validity | citations whose span resolves inside a served chunk / total citations | only when citations exist; otherwise `not_measured` | |
| 138 | +| Token-F1 | lexical overlap, predicted vs gold answer | only over pairs where **both** an actual answer and a gold answer exist; with generation disabled there are none → `not_measured` (never 0) | |
| 139 | +| Latency p50/p95 | nearest-rank percentiles of per-query wall time | volatile; single machine; no cross-machine claim | |
| 140 | +| Stage timings | p50/p95 of recorded `trace_stages` timings per stage | volatile; only stages a query actually reached are counted | |
| 141 | + |
| 142 | +**Status vocabulary.** Metric rows carry `measured` or `not_measured` |
| 143 | +(`value=None` when unmeasured — never zero-filled, never 100%). Plan §7.1/§9.1 |
| 144 | +gate-level statuses (`passed/failed/skipped/blocked/not-measured`) are used only |
| 145 | +in the Gate M1 checklist, never to relabel a metric: **M1 defines no numeric |
| 146 | +thresholds, so a measured value is evidence, not a pass** — that limitation is |
| 147 | +printed in every report header (§9.4). |
| 148 | + |
| 149 | +## 6. Reports |
| 150 | + |
| 151 | +- Files: `reports\eval_<dataset>_<split>_<UTCstamp>_<runhex>.json` and `.md` |
| 152 | + (gitignored). Existing reports are never overwritten — same-second runs get |
| 153 | + distinct run-hex suffixes; re-writing the same result yields byte-identical |
| 154 | + output. |
| 155 | +- Structure (`schema: rag-platform-eval-report/1`): a **deterministic** section |
| 156 | + (dataset/split/top-k config labels, manifest/question-file/lockfile SHA-256, |
| 157 | + `metric_counts`, all non-latency metric rows, outcome/abstention/served-by |
| 158 | + distributions, `error_classes`, `ingest_summary`, and ID-only per-question |
| 159 | + records) separated from a **volatile** section (run id, timestamps, wall |
| 160 | + time, latency rows, stage timings, `prior_test_split_evaluations`, |
| 161 | + environment). Two runs over the same inputs produce identical |
| 162 | + `deterministic` bytes. |
| 163 | +- The Markdown renderer prints the §9.4 known-metric limitations in every |
| 164 | + header, plus the run's outcomes, distributions, stage-timing table, and |
| 165 | + per-question rows. |
| 166 | +- No raw question text, answer text, passage text, absolute paths, or exception |
| 167 | + messages appear in either format (tested). |
| 168 | + |
| 169 | +## 7. Trace linkage |
| 170 | + |
| 171 | +- Deterministic request IDs: `eval:<dataset>:<split>:<question_id>` — every |
| 172 | + report row links to its `traces`/`trace_stages` rows in the run database. |
| 173 | +- Stage vocabulary (frozen in `observability.py`): retrieval ∈ {ok, |
| 174 | + no_candidates, insufficient_evidence}; context_build ∈ {ok, budget_invalid, |
| 175 | + budget_exhausted}. A query that abstains before context building honestly has |
| 176 | + no `context_build` stage. |
| 177 | +- The harness attaches an **observer** to the shared `run_query` — the only |
| 178 | + addition it makes to the pipeline. With the default `observer=None` the trace |
| 179 | + keeps its exact Phase 7 shape (three stages, retrieval timing NULL); with an |
| 180 | + observer, the measured retrieval timing and the `context_build` row reach |
| 181 | + both the callback and the database (regression-tested in |
| 182 | + `tests/test_observer_trace.py`, including a raising observer, which is |
| 183 | + contained without affecting the response or the trace). |
| 184 | +- Content rules unchanged: digests, enums, counts, timings only; `excerpt` |
| 185 | + always NULL; `trace_authz` empty; no queries/prompts/passages/paths/keys. |
| 186 | + |
| 187 | +## 8. Recorded dev run (2026-10-21) |
| 188 | + |
| 189 | +`reports\eval_synthetic_v1_dev_20261021T075202Z_dc9ab460.{json,md}` — 70 dev |
| 190 | +questions, 0 errors, exit 0: |
| 191 | + |
| 192 | +| | | | | |
| 193 | +|---|---|---|---| |
| 194 | +| recall@5 **0.8934** | recall@10 **0.9754** | precision@5 **0.2492** | precision@10 **0.1393** | |
| 195 | +| MRR **0.9145** | nDCG@10 **0.8938** | abstention correctness **0.0** | false-refusal **0.0** | |
| 196 | +| retrieval-only **1.0** | latency p50 **11.23 ms** | latency p95 **15.28 ms** | token-F1 / citation validity **not_measured** | |
| 197 | + |
| 198 | +- Counts: 61 answerable / 9 unanswerable / 70 generation-unavailable / |
| 199 | + 0 retrieval errors; retrieval outcomes `{ok: 70}`; abstention reasons |
| 200 | + `{generation_unavailable: 70}`; `served_by {retrieval_only: 70}`. |
| 201 | +- Stage timings recorded for all four stages (70 samples each). |
| 202 | +- **Determinism:** these values are byte-identical to the earlier independent |
| 203 | + dev run of the same day (`…T062124Z`), across separate harness invocations. |
| 204 | +- `prior_test_split_evaluations = 0` — the test split has never been run. |
| 205 | + |
| 206 | +## 9. Findings from the first runs (honest, recorded) |
| 207 | + |
| 208 | +1. **`no_candidates` is unreachable for grammatical questions with the default |
| 209 | + retriever.** The lexical retriever OR-joins unfiltered query tokens, so any |
| 210 | + question that shares a token with the corpus returns candidates — |
| 211 | + evidenced by `{ok: 70}` on the real corpus. The fixture dataset still |
| 212 | + exercises the path (a gibberish question yields `no_candidates`), so the |
| 213 | + abstention plumbing is tested end to end. |
| 214 | +2. **Abstention correctness on the real dev split is 0.0 (measured).** With |
| 215 | + the score floor unset (`retrieval_score_floor=None`) and generation |
| 216 | + disabled, every abstain-labeled question answered `generation_unavailable`, |
| 217 | + which is *not* a correct abstention. This is exactly what the frozen |
| 218 | + specification predicts: correct evidence-level refusal requires an approved |
| 219 | + floor/D3 verdict (both gated, untouched). The plan's ~90 % abstention target |
| 220 | + belongs to **M3 (D11)** — it is not an M1 criterion, and Gate M1 criterion 6 |
| 221 | + (which only requires producing the metrics with a status column) is |
| 222 | + unaffected. |
| 223 | +3. **Fixture coverage proves both refusal paths.** The test fixture dataset |
| 224 | + (2 documents, 4 questions) shows `no_candidates` (gibberish) and |
| 225 | + `generation_unavailable` (in-domain) abstentions with abstention |
| 226 | + correctness 0.5 (1/2) — spec-honest and covered by tests. |
| 227 | + |
| 228 | +## 10. Known limitations (printed in every report header) |
| 229 | + |
| 230 | +- Recall/precision/MRR/nDCG measure retrieval overlap with gold chunk IDs |
| 231 | + only; they do not measure answer correctness. |
| 232 | +- Token-F1 is lexical overlap with gold answer spans; `not_measured` until a |
| 233 | + generation-enabled run is authorized. |
| 234 | +- Citation validity counts citations whose span resolves inside a served chunk; |
| 235 | + it does not verify that the cited span supports the claim. |
| 236 | +- Abstention correctness is judged against authored expectations in the frozen |
| 237 | + corpus; it is not a human preference judgment. |
| 238 | +- Latency p50/p95 are nearest-rank percentiles from a single machine; no |
| 239 | + cross-machine generalization is claimed. |
| 240 | +- M1 has no frozen numeric thresholds (§7.1): a measured value is evidence, |
| 241 | + not a pass — threshold decisions are the owner's. |
| 242 | +- Same-author corpus and gold labels (R19): mitigations are the frozen hashed |
| 243 | + splits, published item lists, and the ban on external-comparison claims. |
| 244 | + |
| 245 | +## 11. Gate M1 |
| 246 | + |
| 247 | +Evidence checklist: `reports\gate_m1_evidence_checklist_20261021.md` — |
| 248 | +criterion 6 (eval harness end-to-end) is **passed**; criteria 1, 5, 7 and the |
| 249 | +gate-9 spike are **blocked** with recorded reasons (generation-enabled run / |
| 250 | +BG-1a verdict / score-floor approval / memory-spike measurement all pending |
| 251 | +owner decisions). No gate is claimed fully passed while blocked items remain |
| 252 | +(§9.1 status semantics). |
0 commit comments