test: first-round knowledge quality metrics (#14 P3) #17
No files matched your search
@@ -0,0 +1,36 @@
|
||||
# 检索质量口径(首轮,issue #14 P3)
|
||||
|
||||
本文件定义 `marqdo-knowledge` 的**首轮可执行质量口径**。它不是生产基准,也不承诺
|
||||
召回/延迟/RSS 门槛;数字只适用于固定 fixture 与真实候选制品。
|
||||
|
||||
## 1. 固定 fixture 与查询集
|
||||
|
||||
`tests/fixtures` 之外,首轮口径使用 `tests/quality.rs` 内的 5 文档确定性 fixture:
|
||||
一个 `index.md` 导航入口 + 4 个领域知识文档(归一化、矩阵分解、缓存失效、
|
||||
时区转换)。查询集为 5 个与标题唯一匹配的问题,期望目标按源路径精确判定。
|
||||
|
||||
## 2. 指标
|
||||
|
||||
| 指标 | 定义 | 首轮取值(固定 fixture) |
|
||||
|---|---|---|
|
||||
| hit@1 | 期望目标位于第 1 条命中的查询比例 | 5/5 = 1.0 |
|
||||
| hit@3 | 期望目标位于前 3 条命中的查询比例 | 5/5 = 1.0 |
|
||||
| MRR | 期望目标排名的倒数均值 | 1.0 |
|
||||
| navigation role | `index.md` 命中携带 `role: index` | PASS |
|
||||
| stale 拒绝 | 源变更后普通 query 返回 `knowledge.database_stale` | PASS |
|
||||
| stale 标记 | `--snapshot` 查询返回 `current:false` + `knowledge.snapshot_stale` | PASS |
|
||||
|
||||
执行入口:
|
||||
|
||||
```sh
|
||||
cargo test --locked --test quality -- --nocapture
|
||||
# 输出一行 QUALITY_METRICS {json},CI 日志可归档
|
||||
```
|
||||
|
||||
## 3. 边界(不宣称)
|
||||
|
||||
- 固定 fixture 只有 5 个文档/5 个查询,不能代表真实语料;数字不是 Recall@10/MRR
|
||||
产品门槛,也没有延迟/RSS/大规模性能数据。
|
||||
- 指标只覆盖 plain Markdown 命中与导航/新鲜度;.mq.md Analysis 索引、语义通道、
|
||||
跨域候选排序由各自测试与 `marqdo#55 §15` 冷启动评估另行覆盖。
|
||||
- 修改 fixture/查询集视为改动本口径;应在对应 PR 记录新数字,不沿用旧值。
|
||||
@@ -52,6 +52,7 @@ must not be claimed as passing.
|
||||
| Exact / lexical / fuzzy / graph query, weighted RRF, bounded expansion | implemented | `src/query/*` tests, `tests/query.rs` |
|
||||
| Candidate location + evidence status (module/unit/anchor/span/digest, current/stale, `nature: unspecified`) | implemented | `tests/query.rs`, `spec/knowledge.md` §3.1.1/§4.1 |
|
||||
| Document role in query hits (`entry`/`index`/`log`/`document`/`concept`, additive) | implemented | `tests/query.rs` (`hits_expose_entry_navigation_and_concept_roles`), `spec/knowledge.md` §3.1.1/§4.1 |
|
||||
| First-round deterministic quality metrics (5-doc fixture: hit@1/hit@3/MRR, navigation role, stale refusal) | implemented | `tests/quality.rs`, `docs/QUALITY.md`(固定 fixture;非生产基准) |
|
||||
| Document / execution / hybrid slices with 32 KiB budget | implemented | `tests/query.rs`, `src/query/slice.rs` |
|
||||
| OKF v0.3 metadata recognition, reserved roles, concept aliases, `check --okf` conformance | implemented | `tests/okf.rs` |
|
||||
| Explicit OpenAI-compatible semantic channel with all-or-nothing indexing | implemented | `tests/embedding.rs` (local mock protocol server) |
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
# Knowledge 首轮检索质量口径(issue #14 P3)
|
||||
|
||||
Date: 2026-11-30. Branch: `codex/issue-14-quality-metrics`.
|
||||
Baseline: master `90d5d3d2fb11f598c898ed536b86e6d064d0b878`(PR #16)。
|
||||
|
||||
## 交付
|
||||
|
||||
- `tests/quality.rs`:固定 5 文档 fixture + 5 个唯一标题查询,计算 hit@1、hit@3、
|
||||
MRR、navigation role、stale 拒绝/快照标记,并输出一行
|
||||
`QUALITY_METRICS {json}` 供 CI 日志归档。
|
||||
- `docs/QUALITY.md`:指标定义、首轮取值、边界(非生产基准、不宣称召回/延迟门槛)。
|
||||
- `docs/STATUS.md` 记录实现与证据位置。
|
||||
|
||||
## 真实执行(Linux x86_64 / glibc 2.43)
|
||||
|
||||
```text
|
||||
$ cargo test --locked --test quality -- --nocapture
|
||||
QUALITY_METRICS {"fixture_documents":5,"hit_at_1":5,"hit_at_3":5,"mrr":1.0,
|
||||
"navigation_role":"index","queries":5,"stale_marked_with_snapshot":true,
|
||||
"stale_refused":true}
|
||||
test first_round_quality_metrics_on_the_fixed_fixture ... ok
|
||||
```
|
||||
|
||||
指标只对固定 fixture 生效;没有声称生产语料召回、延迟、RSS 或大规模基准。
|
||||
`.mq.md` Analysis 索引与语义通道质量仍由各自测试/#55 §15 冷启动评估跟踪。
|
||||
未创建 release;未改 CLI/其它组件。
|
||||
@@ -0,0 +1,186 @@
|
||||
//! First-round retrieval/navigation quality口径 over a fixed plain-Markdown
|
||||
//! fixture (issue #14 P3).
|
||||
//!
|
||||
//! This is a small deterministic regression set, not a benchmark claim: it
|
||||
//! records the executable metrics (hit@1/hit@3/MRR, navigation role hit, stale
|
||||
//! rejection) for the current compiler/query implementation on the same
|
||||
//! fixture in CI. Real recall/latency/RSS thresholds remain unclaimed until a
|
||||
//! larger, representative corpus and real measurements exist.
|
||||
|
||||
use marqdo_knowledge::cli::{CompileArgs, EmbeddingArgs, OutputFormat, QueryArgs};
|
||||
use marqdo_knowledge::compiler;
|
||||
use marqdo_knowledge::query;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
fn project(files: &[(&str, &str)]) -> PathBuf {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let root = temp.path().to_path_buf();
|
||||
for (path, content) in files {
|
||||
let absolute = root.join(path);
|
||||
if let Some(parent) = absolute.parent() {
|
||||
std::fs::create_dir_all(parent).unwrap();
|
||||
}
|
||||
std::fs::write(absolute, content).unwrap();
|
||||
}
|
||||
std::mem::forget(temp);
|
||||
root
|
||||
}
|
||||
|
||||
fn compile(root: &Path) {
|
||||
compiler::compile(
|
||||
&CompileArgs {
|
||||
root: root.to_path_buf(),
|
||||
exclude: Vec::new(),
|
||||
rebuild: false,
|
||||
json: true,
|
||||
embedding: EmbeddingArgs::default(),
|
||||
},
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn query_args(root: &Path, text: &str) -> QueryArgs {
|
||||
QueryArgs {
|
||||
query: text.to_string(),
|
||||
root: root.to_path_buf(),
|
||||
exact: false,
|
||||
kind: None,
|
||||
limit: Some(10),
|
||||
depth: Some(0),
|
||||
relation: Vec::new(),
|
||||
reverse: false,
|
||||
slice: None,
|
||||
format: OutputFormat::Json,
|
||||
snapshot: false,
|
||||
semantic: false,
|
||||
embedding: EmbeddingArgs::default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn run(root: &Path, text: &str) -> serde_json::Value {
|
||||
let output = query::run_query(&query_args(root, text), None).unwrap();
|
||||
assert_eq!(output.exit_code, 0, "{}", output.stderr);
|
||||
serde_json::from_str(&output.stdout).unwrap()
|
||||
}
|
||||
|
||||
fn quality_project() -> PathBuf {
|
||||
project(&[
|
||||
(
|
||||
"index.md",
|
||||
"# 知识入口\n\n导航:[归一化](math/normalization.md) · [矩阵](math/matrix.md) · [缓存](systems/cache.md) · [时区](time/timezone.md)。\n",
|
||||
),
|
||||
(
|
||||
"math/normalization.md",
|
||||
"# 归一化\n\n## 归一化动机\n\n消除向量模长影响,归一化是常见操作。\n",
|
||||
),
|
||||
(
|
||||
"math/matrix.md",
|
||||
"# 线性代数\n\n## 矩阵分解\n\n把矩阵分解为若干结构化的因子。\n",
|
||||
),
|
||||
(
|
||||
"systems/cache.md",
|
||||
"# 缓存\n\n## 缓存失效\n\nTTL 到期或显式失效都会删除条目。\n",
|
||||
),
|
||||
(
|
||||
"time/timezone.md",
|
||||
"# 时间\n\n## 时区转换\n\nIANA 时区之间的转换需要处理夏令时。\n",
|
||||
),
|
||||
])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn first_round_quality_metrics_on_the_fixed_fixture() {
|
||||
let root = quality_project();
|
||||
compile(&root);
|
||||
|
||||
// (query, expected source path) — unique titles make the target checkable.
|
||||
let cases = [
|
||||
("归一化动机", "math/normalization.md"),
|
||||
("矩阵分解", "math/matrix.md"),
|
||||
("缓存失效", "systems/cache.md"),
|
||||
("时区转换", "time/timezone.md"),
|
||||
("知识入口", "index.md"),
|
||||
];
|
||||
let mut hit_at_1 = 0usize;
|
||||
let mut hit_at_3 = 0usize;
|
||||
let mut reciprocal_rank = 0.0f64;
|
||||
for (text, expected) in cases {
|
||||
let value = run(&root, text);
|
||||
let hits = value["hits"].as_array().cloned().unwrap_or_default();
|
||||
let rank = hits
|
||||
.iter()
|
||||
.position(|hit| hit["source"]["path"] == expected)
|
||||
.map(|index| index + 1);
|
||||
match rank {
|
||||
Some(1) => {
|
||||
hit_at_1 += 1;
|
||||
hit_at_3 += 1;
|
||||
reciprocal_rank += 1.0;
|
||||
}
|
||||
Some(rank) if rank <= 3 => {
|
||||
hit_at_3 += 1;
|
||||
reciprocal_rank += 1.0 / rank as f64;
|
||||
}
|
||||
Some(rank) => reciprocal_rank += 1.0 / rank as f64,
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
let total = cases.len();
|
||||
let mrr = reciprocal_rank / total as f64;
|
||||
// The navigation document must be identifiable as an index role.
|
||||
let navigation = run(&root, "知识入口");
|
||||
let navigation_role = navigation["hits"]
|
||||
.as_array()
|
||||
.and_then(|hits| {
|
||||
hits.iter()
|
||||
.find(|hit| hit["source"]["path"] == "index.md")
|
||||
.map(|hit| hit["role"].as_str().unwrap_or("unknown").to_string())
|
||||
})
|
||||
.unwrap_or_else(|| "missing".to_string());
|
||||
|
||||
// Stale source must be refused unless the caller explicitly permits it.
|
||||
std::fs::write(
|
||||
root.join("math/normalization.md"),
|
||||
"# 归一化\n\n## 归一化动机\n\n内容已变化。\n",
|
||||
)
|
||||
.unwrap();
|
||||
let stale_error = query::run_query(&query_args(&root, "归一化动机"), None).unwrap_err();
|
||||
assert_eq!(stale_error.code, "knowledge.database_stale");
|
||||
let mut snapshot_args = query_args(&root, "归一化动机");
|
||||
snapshot_args.snapshot = true;
|
||||
let snapshot = query::run_query(&snapshot_args, None).unwrap();
|
||||
let snapshot: serde_json::Value = serde_json::from_str(&snapshot.stdout).unwrap();
|
||||
let stale_marked = snapshot["current"] == serde_json::Value::Bool(false)
|
||||
&& snapshot["diagnostics"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.any(|diagnostic| diagnostic["code"] == "knowledge.snapshot_stale");
|
||||
|
||||
println!(
|
||||
"QUALITY_METRICS {}",
|
||||
serde_json::json!({
|
||||
"fixture_documents": 5,
|
||||
"queries": total,
|
||||
"hit_at_1": hit_at_1,
|
||||
"hit_at_3": hit_at_3,
|
||||
"mrr": (mrr * 1000.0).round() / 1000.0,
|
||||
"navigation_role": navigation_role,
|
||||
"stale_refused": stale_error.code == "knowledge.database_stale",
|
||||
"stale_marked_with_snapshot": stale_marked,
|
||||
})
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
hit_at_3, total,
|
||||
"every query must find its target in the top 3"
|
||||
);
|
||||
assert_eq!(
|
||||
hit_at_1, total,
|
||||
"unique titles must rank first on this fixture"
|
||||
);
|
||||
assert_eq!(navigation_role, "index");
|
||||
assert!(stale_error.code == "knowledge.database_stale");
|
||||
assert!(stale_marked);
|
||||
}
|
||||
Reference in new issue
Block a user