feat(labels): M9 两级词表检索 — labels CLI + MLSP labels.search

第一级(内置,数据常量):六组关键词 —— 未完成/假数据/示例/实验/废弃/待校验,
pending = 未完成 ∪ 假数据。作用是默认查询、LLM 范例、跨项目可比。
第二级(参数):--grep 替换,--also-builtin 并集,--group 选组。
**词表是数据不是语法**:扩充它不改任何解析规则。

CLI:
  marqdo labels .                              # pending 口径
  marqdo labels . --grep "联调"                 # 自定义替换
  marqdo labels . --grep "联调" --also-builtin   # 并集
  marqdo labels . --group 示例,实验
  marqdo labels . --count / --json

MLSP:{"method":"labels.search","params":{path|source, grep?, also_builtin?, group?}}
→ {"ok":true,"words":[…],"count":N,"hits":[{line,unit,label,words,has_payload}]}
与 CLI 共用同一份 label::search。

检索语义 v1:子串匹配(CJK 无词边界)、多词 OR、英文大小写不敏感;
误判只影响计数,输出带描述原文可人工判断;不做否定语法(留 v1.1)。

测试:4 个集成(pending=2 / grep 替换=1 / also-builtin 并集=3 / MLSP 返回 hits);
全量 cargo test --release:587 passed / 0 failed(M4 后 583 + 4)。

实测:
  pending → 2 命中("待开发"、"模拟"两处)
  --grep 联调 → 1 命中
  --grep 联调 --also-builtin → 3 命中

文档:doc/design/change-m9-label-search.md
This commit is contained in:
cflmy committed 2026-10-22 15:16:39 +08:00
1 parent 6dd0ce47b1
commit fd0cfcb566
6 files changed
+468

No files matched your search

+47
View File
@@ -57,6 +57,8 @@ fn dispatch(req: &Value) -> Result<Value, Value> {
Some("repair_targets") => repair_targets(&params),
Some("repair_apply") => repair_apply(&params),
Some("schema") => schema(&params),
// `<TAB>` M9: occlusion label search (two-tier vocabulary).
Some("labels.search") => labels_search(&params),
other => Err(err(
"mlsp.unknown_method",
format!("未知方法 {other:?};可用:locate / syntax / validate / repair_targets / repair_apply / schema"),
@@ -331,6 +333,51 @@ fn schema(params: &Value) -> Result<Value, Value> {
// ---------- 公共小工具 ----------
/// `labels.search` — occlusion label query. `<TAB>` M9
///
/// params: `{ source | path, grep?: [..] | "a,b", also_builtin?: bool,
/// group?: "未完成,假数据" }`
fn labels_search(params: &Value) -> Result<Value, Value> {
let (module, _path) = module_from_params(params)?;
let mut words: Vec<String> = match params.get("grep") {
Some(Value::String(s)) => crate::label::parse_words(s),
Some(Value::Array(a)) => a
.iter()
.filter_map(|v| v.as_str())
.map(|s| s.to_string())
.collect(),
_ => Vec::new(),
};
let groups: Vec<String> = match params.get("group").and_then(|g| g.as_str()) {
Some(spec) => crate::label::parse_words(spec),
None => crate::label::PENDING_GROUPS
.iter()
.map(|s| s.to_string())
.collect(),
};
let group_refs: Vec<&str> = groups.iter().map(|s| s.as_str()).collect();
if words.is_empty() || params.get("also_builtin").and_then(|b| b.as_bool()) == Some(true) {
for w in crate::label::group_words(&group_refs) {
if !words.contains(&w) {
words.push(w);
}
}
}
let hits: Vec<Value> = crate::label::search(&module, &words)
.into_iter()
.map(|h| {
json!({
"line": h.site.line,
"unit": h.site.unit,
"label": h.site.label,
"words": h.words,
"has_payload": h.site.has_payload,
})
})
.collect();
Ok(json!({"ok": true, "words": words, "count": hits.len(), "hits": hits}))
}
fn module_from_params(params: &Value) -> Result<(Module, Option<PathBuf>), Value> {
if let Some(src) = params.get("source").and_then(|s| s.as_str()) {
let module = load::load_module_from_source(src)