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

+127
View File
@@ -97,6 +97,32 @@ enum Commands {
#[arg(long)]
json: bool,
},
/// Occlusion label queries: pending stubs (built-in words) or keyword search
Labels {
/// File or directory (recursively scans `*.mq.md`)
#[arg(value_name = "PATH", default_value = ".")]
path: PathBuf,
/// Keywords (comma separated). Replaces the built-in pending set.
#[arg(long, value_name = "WORDS")]
grep: Option<String>,
/// Union with the built-in groups instead of replacing them
#[arg(long)]
also_builtin: bool,
/// Built-in group names, comma separated (default: pending = 未完成,假数据)
#[arg(long, value_name = "GROUPS")]
group: Option<String>,
/// One JSON array on stdout
#[arg(long)]
json: bool,
/// Print only the hit count
#[arg(long)]
count: bool,
},
/// MLSP for AI: line-delimited JSON on stdio (locate / syntax / validate / repair_targets / schema)
Mlsp,
/// Browse `.mq.md` structure + execution (live server or static output)
@@ -426,6 +452,32 @@ fn main() -> ExitCode {
}
}
/// Recursively collect `*.mq.md` under `path` (file → itself).
fn collect_mq_files(path: &std::path::Path) -> Result<Vec<std::path::PathBuf>> {
fn walk(dir: &std::path::Path, out: &mut Vec<std::path::PathBuf>) {
let Ok(rd) = std::fs::read_dir(dir) else { return };
for e in rd.flatten() {
let p = e.path();
let name = p.file_name().and_then(|n| n.to_str()).unwrap_or("");
if p.is_dir() {
if matches!(name, "target" | "dist" | "node_modules" | ".git" | ".marqdo") {
continue;
}
walk(&p, out);
} else if name.ends_with(".mq.md") {
out.push(p);
}
}
}
if path.is_file() {
return Ok(vec![path.to_path_buf()]);
}
let mut out = Vec::new();
walk(path, &mut out);
out.sort();
Ok(out)
}
fn try_main(cli: Cli) -> Result<i32> {
match cli.command {
Commands::Run {
@@ -507,6 +559,81 @@ fn try_main(cli: Cli) -> Result<i32> {
}
Ok(errors.min(1))
}
Commands::Labels {
path,
grep,
also_builtin,
group,
json,
count,
} => {
let mut words: Vec<String> = match &grep {
Some(spec) => marqdo::label::parse_words(spec),
None => {
let groups: Vec<&str> = match &group {
Some(g) => g.split(',').map(|s| s.trim()).filter(|s| !s.is_empty()).collect(),
None => marqdo::label::PENDING_GROUPS.to_vec(),
};
marqdo::label::group_words(&groups)
}
};
if also_builtin {
let groups: Vec<&str> = match &group {
Some(g) => g.split(',').map(|s| s.trim()).filter(|s| !s.is_empty()).collect(),
None => marqdo::label::PENDING_GROUPS.to_vec(),
};
for w in marqdo::label::group_words(&groups) {
if !words.contains(&w) {
words.push(w);
}
}
}
let files = collect_mq_files(&path)?;
let mut rows: Vec<(String, u32, String, String, Vec<String>, bool)> = Vec::new();
for f in &files {
let module = match marqdo::load::load_module(f) {
Ok(m) => m,
Err(_) => continue, // unparsable files are `check`'s problem, not ours
};
for hit in marqdo::label::search(&module, &words) {
rows.push((
f.display().to_string(),
hit.site.line,
hit.site.unit.clone(),
hit.site.label.clone(),
hit.words.clone(),
hit.site.has_payload,
));
}
}
if json {
let arr: Vec<serde_json::Value> = rows
.iter()
.map(|(file, line, unit, label, words, has_payload)| {
serde_json::json!({
"file": file, "line": line, "unit": unit,
"label": label, "words": words, "has_payload": has_payload,
})
})
.collect();
println!("{}", serde_json::Value::Array(arr));
} else if count {
println!("{}", rows.len());
} else {
for (file, line, unit, label, words, _) in &rows {
println!("{file}:{line} [{unit}] {label} ← {}", words.join(","));
}
println!(
"label search: {} 命中({} 文件,词={:?})",
rows.len(),
files.len(),
words
);
}
Ok(0)
}
Commands::Mlsp => {
use std::io::{BufRead, Write};
let stdin = std::io::stdin();