From 98ed8e5c49676f55d92abf1afd59116a33b3dfdc Mon Sep 17 00:00:00 2001 From: cflmy Date: Mon, 30 Nov 2026 17:01:54 +0800 Subject: [PATCH] feat: expose document role in query hits Continue marqdo-knowledge#14 P2 with one additive, path-convention-derived field on every query hit: role = entry (index.mq.md), index/log (OKF reserved files), document (other .mq.md), concept (other .md), or unknown without a source path. This is navigation information for agents, not execution or authority: no new metadata syntax, no extra source read, no second index. spec/knowledge.md records the field and the path mapping; tests/query.rs compiles a real project with all four roles; STATUS records implementation and test. No release; schema_version stays 1 (additive optional-aware field). --- docs/STATUS.md | 1 + docs/evidence/2026-11-30-document-role.md | 35 +++++++++++++++++++++++ spec/knowledge.md | 8 ++++++ src/query/mod.rs | 26 +++++++++++++++++ tests/query.rs | 29 +++++++++++++++++++ 5 files changed, 99 insertions(+) create mode 100644 docs/evidence/2026-11-30-document-role.md diff --git a/docs/STATUS.md b/docs/STATUS.md index a7233ff..19447bd 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -51,6 +51,7 @@ must not be claimed as passing. | Check command (sources, references, database structure/currentness) | implemented | `tests/compile.rs`, `tests/query.rs` | | 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 | | 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) | diff --git a/docs/evidence/2026-11-30-document-role.md b/docs/evidence/2026-11-30-document-role.md new file mode 100644 index 0000000..5bd8e17 --- /dev/null +++ b/docs/evidence/2026-11-30-document-role.md @@ -0,0 +1,35 @@ +# Knowledge 候选文档角色(issue #14 P2) + +Date: 2026-11-30. Branch: `codex/issue-14-document-role`. +Baseline: master `ff6dad4bc2336ff4fee8316d0557d41af4fcff2f`(PR #15)。 + +## 交付 + +查询命中新增 additive 字段 `role`,只从现有公开路径约定推导: + +| path | role | +|---|---| +| `index.mq.md` | `entry` | +| `index.md` | `index` | +| `log.md` | `log` | +| 其它 `*.mq.md` | `document` | +| 其它 `*.md` | `concept` | +| 无 source path | `unknown` | + +- 不新增元数据语法、不读取源码正文、不新增索引;`nature` 仍为 `unspecified`。 +- `spec/knowledge.md` §3.1.1 记录 role 语义,§4.1 映射表新增 path→role 行; + `docs/STATUS.md` 记录实现与测试。 +- 测试:`tests/query.rs::hits_expose_entry_navigation_and_concept_roles` + (index.md / log.md / .md / .mq.md 四类真实 compile/query)。 + +## 验证(真实执行,Linux x86_64 / glibc 2.43) + +输入:CLI 3.0.0-rc.6 merge `afea4f947473ae4cd5688e53a6545cce7f327b2f`, +Bundle `marqdo-stdlib 0.3.0`(tree `sha256:4e47b0ff…`)。 + +```text +$ MARQDO_BIN= MARQDO_STDLIB_DIR= cargo test --locked --test query +13 passed; 0 failed +``` + +完整门禁见 PR CI;未创建 release。role 是 agent 导航信息,不是执行/权威性声明。 diff --git a/spec/knowledge.md b/spec/knowledge.md index a22b812..0e3facd 100644 --- a/spec/knowledge.md +++ b/spec/knowledge.md @@ -90,6 +90,7 @@ stream and no per-query bespoke format. { "id": "opaque-canonical-id", "kind": "function", + "role": "concept", "title": "归一化检查", "source": {"path": "state.mq.md", "digest": "sha256:...", "span": {"start": 100, "end": 180}}, "snippet": "原文片段(非生成文本)", @@ -148,6 +149,12 @@ their absence as an error (older databases predate them). matches the source snapshot. A `--snapshot` query or a stale database reports `stale`; the result envelope's `current` flag is authoritative for the whole result. +* `role` is the document role derived from existing public path conventions: + `entry` for `index.mq.md`, `index`/`log` for the OKF reserved files at any + level, `document` for other `.mq.md` files, `concept` for other `.md` files + and `unknown` when no source path exists. It is information for agents (entry + and navigation versus ordinary documents), not an execution or authority + claim. * `source`/`source.span` remain the exact snippet location; `location` is the structured candidate address. Neither is a second knowledge body: the authoritative text is always the source file, read through the CLI's public @@ -209,6 +216,7 @@ one. The mapping from public facts to indexed objects is: | `calls[]` | `calls` edges (origin `analysis`) | Static call graph; unresolved/dynamic calls stay explicitly unresolved. | | `resources[]` | resource nodes | Inert formula/code payloads; no execution. | | structure / OKF metadata | `nature` in the candidate location | `unspecified` when the inputs do not declare a nature. | +| path convention | hit `role` | `entry` (`index.mq.md`), `index`/`log` (OKF reserved), `document` (other `.mq.md`), `concept` (other `.md`); no new metadata syntax. | Consequences: an entry/navigation document and a knowledge unit are distinct concepts; a resolved link does not make its target executable; and a candidate diff --git a/src/query/mod.rs b/src/query/mod.rs index cd357f8..9be112e 100644 --- a/src/query/mod.rs +++ b/src/query/mod.rs @@ -203,6 +203,7 @@ struct Hit { matches: Vec, score: f64, callable: Option, + role: String, location: Option, evidence: serde_json::Value, related: Vec, @@ -661,6 +662,11 @@ impl<'a> Engine<'a> { "source_digest": source_digest, "snapshot_digest": self.snapshot_digest, }); + let role = source_path + .as_deref() + .map(document_role) + .unwrap_or("unknown") + .to_string(); Ok(Hit { id: node.key.clone(), kind: node.kind, @@ -677,6 +683,7 @@ impl<'a> Engine<'a> { matches: final_hit.matches.clone(), score: (final_hit.score * 1_000_000.0).round() / 1_000_000.0, callable, + role, location, evidence, related, @@ -724,6 +731,7 @@ impl<'a> Engine<'a> { if let Some(callable) = &hit.callable { object.insert("callable".into(), callable.clone()); } + object.insert("role".into(), serde_json::Value::String(hit.role.clone())); if let Some(location) = &hit.location { object.insert("location".into(), location.clone()); } @@ -892,6 +900,24 @@ pub fn canonical_document_key(path: &str) -> String { identity::document_key(path).into_string() } +/// Document role from the existing public path conventions. +/// +/// `index.mq.md` is the project/module entry convention; OKF reserves +/// `index.md`/`log.md` at every level; other `.mq.md`/`.md` files are ordinary +/// documents. The role is information for agents, not an execution or +/// authority claim. +fn document_role(path: &str) -> &'static str { + let file = path.rsplit('/').next().unwrap_or(path); + match file { + "index.mq.md" => "entry", + "index.md" => "index", + "log.md" => "log", + name if name.ends_with(".mq.md") => "document", + name if name.ends_with(".md") => "concept", + _ => "unknown", + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/tests/query.rs b/tests/query.rs index c0fb4be..992ba70 100644 --- a/tests/query.rs +++ b/tests/query.rs @@ -296,3 +296,32 @@ fn check_sees_the_database_as_current_after_compile() { let value: serde_json::Value = serde_json::from_str(&output.stdout).unwrap(); assert_eq!(value["payload"]["database"], "current"); } + +#[test] +fn hits_expose_entry_navigation_and_concept_roles() { + let root = project(&[ + ("index.md", "# 知识入口\n\n导航到 [机制](mechanics.md)。\n"), + ("mechanics.md", "# 机制\n\n## 说明\n\n机制正文。\n"), + ("log.md", "# 变更日志\n\n## 记录\n\n历史。\n"), + ( + "compute.mq.md", + "# @knowledge 计算\n\n## 说明\n\n可执行文稿。\n", + ), + ]); + compile(&root); + for (query, expected) in [ + ("知识入口", "index"), + ("机制", "concept"), + ("变更日志", "log"), + ("计算", "document"), + ] { + let value = run(&root, query_args(&root, query)); + let hit = value["hits"] + .as_array() + .unwrap() + .iter() + .find(|hit| hit["role"] == expected) + .unwrap_or_else(|| panic!("missing role {expected} for query {query}: {value}")); + assert!(hit["role"].is_string()); + } +} -- 2.54.1