feat(labels): M8 债务棘轮 — --baseline + --fail-on-increase
规则:未完成项只许减少,不许增加。
- 台账不存在 → 建立(写入当前计数)并提示,exit 0
- 当前 > 台账 → 失败并提示"未完成项增加了 K",exit 1
- 当前 ≤ 台账 → 通过;若减少则**收紧**台账(N → M)
为什么减少即收紧:否则债务可先降后升,棘轮形同虚设;
收紧后回退到旧数量同样失败(测试第 4 步验证)。
台账格式 {"count":N,"words":[…]},记录词集使"口径变更"可审计。
为什么用棘轮而非硬门禁:硬门禁(必须为 0)会让存量债立刻红一片、逼人关检查;
棘轮做到"老账不追、新账必拦",与防疲劳机制的结论一致。
呈现面复核:expr_display / view 把遮蔽渲染为 [描述](实现)、桩为 [描述](),
实测 view output 页面含 access(;未做桩徽章——桩已是显眼形状,再加装饰是噪音。
测试:四步集成用例(建账 → 增加失败 → 减少收紧 → 回退再次失败);
全量 605 passed / 0 failed(M7 后 604 + 1)。
文档:doc/design/change-m8-ratchet.md
This commit is contained in:
1 parent
4488a3cb70
commit
34b0911218
3 files changed
+214
No files matched your search
+60
@@ -142,6 +142,14 @@ enum Commands {
|
||||
/// Print only the hit count
|
||||
#[arg(long)]
|
||||
count: bool,
|
||||
|
||||
/// Debt ledger (JSON) for the ratchet: seed it, then only allow decreases
|
||||
#[arg(long, value_name = "FILE")]
|
||||
baseline: Option<PathBuf>,
|
||||
|
||||
/// Fail (exit 1) when the hit count exceeds the baseline (`<TAB>` M8)
|
||||
#[arg(long)]
|
||||
fail_on_increase: bool,
|
||||
},
|
||||
/// Migrate legacy syntax to the purity forms (`<TAB>` M7)
|
||||
Migrate {
|
||||
@@ -595,6 +603,8 @@ fn try_main(cli: Cli) -> Result<i32> {
|
||||
group,
|
||||
json,
|
||||
count,
|
||||
baseline,
|
||||
fail_on_increase,
|
||||
} => {
|
||||
let mut words: Vec<String> = match &grep {
|
||||
Some(spec) => marqdo::label::parse_words(spec),
|
||||
@@ -661,6 +671,56 @@ fn try_main(cli: Cli) -> Result<i32> {
|
||||
words
|
||||
);
|
||||
}
|
||||
|
||||
// `<TAB>` M8: ratchet — debt may shrink, never grow.
|
||||
let current = rows.len();
|
||||
let mut violation = false;
|
||||
if let Some(ledger) = &baseline {
|
||||
let prior = std::fs::read_to_string(ledger)
|
||||
.ok()
|
||||
.and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok())
|
||||
.and_then(|v| v.get("count").and_then(|c| c.as_u64()))
|
||||
.map(|n| n as usize);
|
||||
match prior {
|
||||
None => {
|
||||
let body = serde_json::json!({"count": current, "words": words});
|
||||
if let Some(dir) = ledger.parent() {
|
||||
if !dir.as_os_str().is_empty() {
|
||||
let _ = std::fs::create_dir_all(dir);
|
||||
}
|
||||
}
|
||||
std::fs::write(ledger, format!("{body}\n"))?;
|
||||
if !json && !count {
|
||||
println!("baseline: 已建立 {current}({})", ledger.display());
|
||||
}
|
||||
}
|
||||
Some(prior) if current > prior => {
|
||||
violation = true;
|
||||
if !count {
|
||||
println!(
|
||||
"baseline: {current} > {prior} —— 未完成项增加了 {}({})",
|
||||
current - prior,
|
||||
ledger.display()
|
||||
);
|
||||
}
|
||||
}
|
||||
Some(prior) => {
|
||||
if current < prior || std::fs::read_to_string(ledger).is_err() {
|
||||
let body = serde_json::json!({"count": current, "words": words});
|
||||
std::fs::write(ledger, format!("{body}\n"))?;
|
||||
if !json && !count {
|
||||
println!(
|
||||
"baseline: {prior} → {current}(棘轮收紧,{})",
|
||||
ledger.display()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if violation && fail_on_increase {
|
||||
return Ok(1);
|
||||
}
|
||||
Ok(0)
|
||||
}
|
||||
Commands::Migrate { action } => match action {
|
||||
|
||||
Reference in new issue
Block a user