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

+101
View File
@@ -336,3 +336,104 @@ mod tests {
assert!(refs("空 `` 引号").is_empty());
}
}
// ---------------------------------------------------------------------------
// M9: 两级词表检索(built-in groups = 默认/范例;`--grep` = 调用方自定义)
// ---------------------------------------------------------------------------
/// One built-in keyword group. `<TAB>` M9
///
/// The table is **data**, not syntax: it seeds the default query
/// (`marqdo labels pending`) and gives LLMs a stable vocabulary; any project can
/// override or extend it at the call site with `--grep`.
pub struct WordGroup {
pub name: &'static str,
pub words: &'static [&'static str],
}
/// Built-in groups (v1). Extending this table never changes the grammar.
pub const BUILTIN_GROUPS: &[WordGroup] = &[
WordGroup {
name: "未完成",
words: &[
"待开发", "未开发", "开发中", "未实现", "待实现", "未接", "接口未接", "TODO",
],
},
WordGroup {
name: "假数据",
words: &["模拟", "假数据", "桩", "占位", "stub", "mock"],
},
WordGroup {
name: "示例",
words: &["示例", "样例", "demo"],
},
WordGroup {
name: "实验",
words: &["实验", "试验", "experimental"],
},
WordGroup {
name: "废弃",
words: &["废弃", "弃用", "deprecated"],
},
WordGroup {
name: "待校验",
words: &["待校验", "未验证", "待确认"],
},
];
/// Groups that count as "pending" (default query for `labels pending`).
pub const PENDING_GROUPS: &[&str] = &["未完成", "假数据"];
/// Expand group names into their words; unknown names are ignored.
pub fn group_words(names: &[&str]) -> Vec<String> {
let mut out = Vec::new();
for g in BUILTIN_GROUPS {
if names.iter().any(|n| *n == g.name) {
out.extend(g.words.iter().map(|w| w.to_string()));
}
}
out
}
/// Split a `--grep` value (`a,b` / `a,b`) into words.
pub fn parse_words(spec: &str) -> Vec<String> {
spec.split([',', ','])
.map(|w| w.trim())
.filter(|w| !w.is_empty())
.map(|w| w.to_string())
.collect()
}
/// Words that hit `label` (substring, ASCII case-insensitive). `<TAB>` M9
pub fn matched_words(label: &str, words: &[String]) -> Vec<String> {
let hay = label.to_lowercase();
let mut out = Vec::new();
for w in words {
if w.is_empty() {
continue;
}
if hay.contains(&w.to_lowercase()) {
out.push(w.clone());
}
}
out
}
/// One search hit: where, what the label says, which words matched.
#[derive(Debug, Clone)]
pub struct Hit {
pub site: OcclusionSite,
pub words: Vec<String>,
}
/// Search every occlusion whose label matches any of `words`.
pub fn search(module: &Module, words: &[String]) -> Vec<Hit> {
let mut out = Vec::new();
for site in collect(module) {
let words = matched_words(&site.label, words);
if !words.is_empty() {
out.push(Hit { site, words });
}
}
out
}
+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();
+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)