prove: P2 — 离线符号执行(F1–F5)+ 单调性护栏 + 语料库

- offline.rs:SymVal 符号执行(Const/Struct/OpResult/Param/Unknown);
  If 分支克隆状态并接续后续语句(修复 Return-in-outer 编排丢结果);
  可行性检查(前提∩路径条件为空 → 空真关闭,Lean 假设矛盾同构);
  F2 轨迹包含/长度、F3 构造器结构求值、F4 自由原子二分、
  F5 区间:字面重述关闭 + 区间违反反例(区间端点见证,如 金额 = 10001);
  区间蕴含保守 unverified(当前 Lean 脚本无 Val 传递引理,先于 Lean
  关闭即触发 divergence——诚实优先)
- 判定编排:离线先行(毫秒级 checked ≠ proved)→ Lean 复核升级 proved;
  分歧护栏:checked 但 Lean 关不掉 / 反例被 Lean 证出 ⇒
  error[proof.divergence] 退 1 永不落绿;MARQDO_PROOF_INJECT_DIVERGENCE 验证
- emit:try split 按 if 嵌套深度重复(三路分支可达);simp 清单补
  mqResultError/mqValueOr/mqAnd/mqOr/mqNot(分歧护栏暴露)
- tests/prove_corpus.rs:8 类 × 5 = 40 夹具语料库,
  offline_checked ⊆ lean_proved 强制 + 无 Lean 禁 proved;
  f5_interval_closure_scope 单测锁定 F5 诚实范围
- 设计文档 §12 记录 P2 落地、修复与文法限制

实测:biz 3 定理离线 checked→Lean proved;假定理反例 金额 = 10001
指回定理表行;注入分歧 → error/divergence 退 1;全量 516 通过
(仅剩 2 个已记录环境基线失败)。
This commit is contained in:
cflmy committed 2026-11-18 10:28:18 +08:00
1 parent 44d2928c06
commit 63d60e3ba1
47 files changed
+2699 -7

No files matched your search

+30
View File
@@ -864,3 +864,33 @@ G2b `--no-cache` 强制 miss。无工具链时自动 SKIP,`MARQDO_PROOF_REQUIR
CI:`.github/workflows/proof.yml` —— 装 Lean 4.35.0 → 底座校验 →
`MARQDO_PROOF_REQUIRE_LEAN=1` 跑判定矩阵(CI 不许静默降级)→ prove 主套件。
### P2 离线判定落地(2026-11-17)
新增 `src/prove/offline.rs`(片段 F1–F5 的符号执行)+ `tests/prove_corpus.rs`
(40 夹具单调性语料库):
| 组件 | 实现 |
|---|---|
| 符号执行(§5.3) | 状态 `Var → SymVal`(Const/Struct/OpResult/Param/Unknown);If 逐分支克隆状态并**接续 If 之后的语句**(Return 常在外层);每个 Return 收集 (路径条件, 结果, 轨迹, 变量快照) |
| 可行性 | 前提区间 ∩ 路径条件为空 → 路径不可行,空真关闭(Lean 侧对应假设矛盾,simp 可消) |
| F2 轨迹 | 包含/长度逐路径求值(Op.name 显示名与 Lean 一致) |
| F3 字面 | `result.ok/error/value_or` 结构求值(与 mqResultOk/mqError/mqValueOr 语义一致) |
| F4 自由原子 | 外部返回值布尔判定只二分;路径条件里出现同键原子才可关闭(hypothesis);反例路径含自由原子 → 保守放弃见证 |
| F5 区间 | **收紧后的诚实范围**:字面重述(同原子)→ hypothesis 关闭;区间违反 → 确定性否定 + 区间端点见证(反例);区间**蕴含**(10000 ⊢ 9999)数学上真,但当前 Lean 脚本无 Val 比较传递引理,simp+omega 关不掉——离线同样保守 unverified(等库补引理再放开),否则必触发 divergence |
| 状态 | 离线 closed → `checked`(decided_by=trace,**不是 proved**);反例 → `unproved` + `proof.claim_failed` 诊断(指回定理表行 + 见证如 `金额 = 10001`) |
| 单调性护栏(§5.4) | 有 Lean 时逐定理复核:离线 checked 但 Lean unproved,或离线反例被 Lean 证出 → `error[proof.divergence]` 退 1,永不落绿;`MARQDO_PROOF_INJECT_DIVERGENCE=1` 测试注入验证护栏咬人 |
| 语料库 | `tests/prove/fixtures/` 8 类 × 5 = 40 夹具(literal/trace/interval/free-bool/branches/composition/counterexample/beyond);`corpus_monotonicity_offline_subset_of_lean` 强制 offline ⊆ lean;`corpus_offline_never_claims_proved` 无 Lean 时禁 proved |
**实测抓出并修复的缺陷**(均有测试):
| 缺陷 | 修复 |
|---|---|
| If 分支后丢弃剩余步骤(Return 在 If 外层的常见编排全部丢结果) | 两支克隆状态并接续后续语句 |
| `negated` 字段与 op 翻转双重否定,取反原子被收集函数丢弃 | 移除字段,否定一律折进 op/expected |
| 嵌套分支(else 里再嵌 if)Lean 只拆一层 | `try split at h_run` 按最大 if 深度重复发射 |
| simp 清单缺 `mqResultError`/`mqValueOr`/`mqAnd/mqOr/mqNot` | 全部补入(l2/l5/f1 由分歧护栏暴露) |
**已记录的文法限制**:分支条件尚不支持字段访问(`access(审核, "ok")` 作条件
解析失败)——F4 的"路径条件含自由原子"场景目前由单元测试覆盖,语料库中以
分支后的结构化结论代替;文法放开后 prove 侧无需改动(offline.cond_atom 已支持)。
+22 -3
View File
@@ -49,6 +49,21 @@ pub fn emit_module(plan: &Plan, module_stem: &str) -> (String, String) {
}
/// 定理脚手架(§7.2):受限文法可解析的命题 → `theorem` 陈述 + `sorry`
/// 函数体里 if 的最大嵌套深度(0 = 无分支)。
fn max_if_depth(steps: &[Step]) -> usize {
fn walk(steps: &[Step], depth: usize) -> usize {
let mut max = depth;
for s in steps {
if let Step::If { then, otherwise, .. } = s {
max = max.max(walk(then, depth + 1));
max = max.max(walk(otherwise, depth + 1));
}
}
max
}
walk(steps, 0)
}
fn emit_theorems(out: &mut String, fun: &FnPlan) {
if fun.claims.is_empty() {
return;
@@ -111,15 +126,19 @@ fn emit_theorems(out: &mut String, fun: &FnPlan) {
}
let _ = writeln!(out, " intro «结果» «轨迹» h_run");
let _ = writeln!(out, " unfold {fun_name} at h_run");
// 嵌套分支(else 里再嵌 if)需要逐层拆:按最大 if 深度发射
// `try split at h_run`(拆无可拆时 try 吞掉,幂等)。
for _ in 0..=max_if_depth(&fun.steps) {
let _ = writeln!(out, " try split at h_run");
}
// 每个分支:simp_all(完整默认集处理 List.map/contains/length 的
// 结构化消去;等式替换输出;Op.name 方程化简)+ 附加域定义
// (mqAccess/mqResultOk/mqEq 字段与构造)。互斥比较假设由库中
// @[simp] 矛盾引理消掉,剩余线性算术交给 omega。
let _ = writeln!(
out,
" try split at h_run
all_goals
simp_all [mqTruth, Op.name, mqAccess, mqResultOk, mqEq]
" all_goals
simp_all [mqTruth, Op.name, mqAccess, mqResultOk, mqResultError, mqValueOr, mqAnd, mqOr, mqNot, mqEq]
try
first
| exact (mqGt_mqLe_false ‹mqGt _ (Val.int _) = true› ‹mqLe _ (Val.int _) = true›).elim
+112 -3
View File
@@ -14,6 +14,7 @@ pub mod driver;
pub mod emit;
pub mod ir;
pub mod lower;
pub mod offline;
pub mod proof_check;
pub mod types;
@@ -386,10 +387,14 @@ pub fn prove_path(path: &Path, opts: &ProveOptions) -> Result<Report> {
status = Verdict::Error;
}
// ⑤ Lean 判定(P1):有定理、有产物、非 report-only 时进行。
// ⑤ 判定(P2):离线符号执行先行(毫秒级,checked ≠ proved),
// 有 Lean 工具链时随即复核(checked 升 proved;分歧 → error,永不落绿)。
let mut lean_info: Option<LeanInfo> = None;
let claims_present = plan.functions.iter().any(|f| !f.claims.is_empty());
if claims_present && opts.judge && !opts.report_only {
// ⑤a 离线(§5):F1–F5 片段内给 checked/反例,超出片段不猜。
judge_offline(&plan, &mut functions, &mut diagnostics);
// ⑤b Lean 复核(§8):证书缓存 → 现算。
match judge_with_lean(
&plan,
&source_sha256,
@@ -445,6 +450,77 @@ pub fn prove_path(path: &Path, opts: &ProveOptions) -> Result<Report> {
})
}
/// 离线判定(§5):checked/反例贴回定理,绝不冒充 proved。
/// 单调性护栏在此埋点:离线结论会在 judge_with_lean 里被 Lean 复核。
fn judge_offline(
plan: &crate::prove::ir::Plan,
functions: &mut [FunctionReport],
diagnostics: &mut Vec<Diagnostic>,
) {
use crate::diagnostics::Span;
let debug = std::env::var("MARQDO_PROOF_DEBUG").is_ok_and(|v| v == "1");
for f in plan.functions.iter() {
for claim in &f.claims {
let verdict = offline::check_claim(f, claim);
if debug {
eprintln!(
"[offline] {}.{} → {:?}",
f.name, claim.id, verdict
);
}
let Some(fr) = functions.iter_mut().find(|x| x.name == f.name) else {
continue;
};
let Some(cr) = fr.claims.iter_mut().find(|c| c.id == claim.id) else {
continue;
};
match verdict {
offline::OfflineVerdict::Checked { cases } => {
cr.status = Verdict::Checked;
cr.decided_by = "trace".into();
cr.evidence = Some(format!("离线符号执行:{cases} 条路径全部关闭(F1–F5)"));
cr.source_line = Some(claim.line);
fr.tier = Tier::Offline;
}
offline::OfflineVerdict::Unproved { counterexample } => {
cr.status = Verdict::Unproved;
cr.decided_by = "trace".into();
cr.source_line = Some(claim.line);
cr.evidence = counterexample.as_ref().map(|c| {
format!(
"反例(离线符号执行):{} — 路径 #{}",
c.detail, c.path_id
)
});
if let Some(c) = &counterexample {
diagnostics.push(
Diagnostic::new(
None,
Span::new(claim.line, 0),
format!(
"定理 {}.{} 反例:{}",
f.name, claim.id, c.detail
),
)
.with_code("proof.claim_failed"),
);
}
fr.tier = Tier::Offline;
}
offline::OfflineVerdict::Unverified { reason } => {
// 不猜:保留 unverified(不写 evidence,让位给 Lean 结果)。
let _ = reason;
}
}
fr.verdict = fr
.claims
.iter()
.map(|x| x.status)
.fold(Verdict::Proved, Verdict::worst);
}
}
}
/// 单一判定来源:缓存证书 → Lean 现算。返回(整体判定,lean 信息)。
#[allow(clippy::too_many_arguments)]
fn judge_with_lean(
@@ -533,8 +609,8 @@ fn judge_with_lean(
}
}
// ② 现算:lake env lean 等价的直接 lean 调用(LEAN_PATH 指向底座 olean)。
let outcome = driver::check_module(
// ② 现算:lean 直接调用(LEAN_PATH 指向底座 olean)。
let mut outcome = driver::check_module(
&toolchain,
&lib_dir,
lean_path,
@@ -542,6 +618,14 @@ fn judge_with_lean(
opts.timeout,
opts.max_heartbeats,
)?;
// 测试注入(roadmap P2 单调性护栏):让首条定理在 Lean 侧"关不掉",
// 验证离线 checked + Lean unproved ⇒ error[proof.divergence] 的护栏真的咬人。
if std::env::var("MARQDO_PROOF_INJECT_DIVERGENCE").is_ok_and(|v| v == "1") {
if let Some(first) = outcome.claims.iter_mut().find(|c| c.status == Verdict::Proved) {
first.status = Verdict::Unproved;
first.detail = "injected divergence (MARQDO_PROOF_INJECT_DIVERGENCE)".into();
}
}
// ③ 证据 → 诊断(proof.claim_failed,指回源码行)。
let claim_records: Vec<cache::ClaimRecord> = outcome
@@ -610,6 +694,31 @@ fn apply_outcome(
};
let f = &mut functions[pos];
if let Some(claim) = f.claims.iter_mut().find(|x| x.id == c.claim) {
// 单调性护栏(§5.4):离线结论必须被 Lean 复核印证。
// checked 但 Lean 关不掉,或离线反例被 Lean 证出来 ⇒ 判定器有 bug。
let divergence = match (claim.status, c.status) {
(Verdict::Checked, Verdict::Unproved)
| (Verdict::Unproved, Verdict::Proved) => true,
_ => false,
};
if divergence {
diagnostics.push(
Diagnostic::new(
None,
Span::new(claim.source_line.unwrap_or(0), 0),
format!(
"离线与 Lean 判定分歧:定理 {}.{} 离线 {:?},Lean {}——判定器缺陷,拒绝落绿",
f.name, c.claim, claim.status, c.status.as_str()
),
)
.with_code("proof.divergence"),
);
claim.status = Verdict::Error;
claim.decided_by = "lean".into();
f.verdict = Verdict::Error;
f.tier = Tier::Lean;
continue;
}
claim.status = c.status;
claim.decided_by = "lean".into();
claim.source_line = c.source_line;
+1581
View File
File diff suppressed because it is too large. Load diff
+5 -1
View File
@@ -493,7 +493,11 @@ fn p1_b_emit_theorem_scaffold() {
assert!(lean.contains("(mqAccess «结果» \"ok\")"), "{lean}");
// 策略脚本(§7.2):unfold → split → simp_all/omega。
assert!(lean.contains("unfold «审批台.审批» at h_run"), "{lean}");
assert!(lean.contains("split at h_run\n all_goals\n simp_all [mqTruth, Op.name, mqAccess, mqResultOk, mqEq]"), "{lean}");
assert!(
lean.contains("try split at h_run")
&& lean.contains("all_goals\n simp_all [mqTruth, Op.name, mqAccess, mqResultOk, mqResultError, mqValueOr, mqAnd, mqOr, mqNot, mqEq]"),
"{lean}"
);
}
/// check --proof(默认关):不传 --proof 时定理问题不报。
+16
View File
@@ -0,0 +1,16 @@
---
markup: occlusion
---
# 文本台
## 拼接
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 文本相等 | 结果.value = "x" | | auto |
**结果 = [登记](result.ok(value="x"))**
*结果*
+17
View File
@@ -0,0 +1,17 @@
---
markup: occlusion
---
# 文本台
## 拼接
+ `金额`
+ `等级`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 双变量线性 | 金额 + 等级 > 100 | | auto |
**结果 = [登记](result.ok(value=`金额`))**
*结果*
+16
View File
@@ -0,0 +1,16 @@
---
markup: occlusion
---
# 文本台
## 拼接
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 非线性 | 金额 * 2 > 100 | 金额 > 50 | auto |
**结果 = [登记](result.ok(value=`金额`))**
*结果*
+17
View File
@@ -0,0 +1,17 @@
---
markup: occlusion
---
# 文本台
## 拼接
+ `金额`
+ `等级`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 变量相等 | 金额 = 等级 | | auto |
**结果 = [登记](result.ok(value=`金额`))**
*结果*
+16
View File
@@ -0,0 +1,16 @@
---
markup: occlusion
---
# 文本台
## 拼接
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 负号运算 | -金额 < 0 | | auto |
**结果 = [登记](result.ok(value=`金额`))**
*结果*
+21
View File
@@ -0,0 +1,21 @@
---
markup: occlusion
---
# 路由
## 分发
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 三路必达 | 结果.ok = True | | auto |
1. `金额` > 10000
**结果 = [大](result.ok(value="big"))**
2. `金额` > 100
**结果 = [中](result.ok(value="mid"))**
3. *
**结果 = [小](result.ok(value="small"))**
*结果*
+20
View File
@@ -0,0 +1,20 @@
---
markup: occlusion
---
# 路由
## 分发
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 大额两跳 | 长度(轨迹) = 2 | 金额 > 10000 | auto |
1. `金额` > 10000
**审核 = [建单](工单.创建(id=`金额`))**
**结果 = [登记](result.ok(value=`审核`))**
2. *
**结果 = [登记](result.ok(value="auto"))**
*结果*
+20
View File
@@ -0,0 +1,20 @@
---
markup: occlusion
---
# 路由
## 分发
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 中额零跳 | 长度(轨迹) = 0 | 金额 ≤ 10000 且 金额 > 100 | auto |
1. `金额` > 10000
**审核 = [建单](工单.创建(id=`金额`))**
**结果 = [登记](result.ok(value=`审核`))**
2. *
**结果 = [登记](result.ok(value="auto"))**
*结果*
+22
View File
@@ -0,0 +1,22 @@
---
markup: occlusion
---
# 路由
## 分发
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 大额含建单 | 包含(轨迹, "工单.创建") | 金额 > 10000 | auto |
1. `金额` > 10000
**审核 = [建单](工单.创建(id=`金额`))**
**结果 = [登记](result.ok(value=`审核`))**
2. `金额` > 100
**结果 = [登记](result.ok(value="mid"))**
3. *
**结果 = [登记](result.ok(value="small"))**
*结果*
+19
View File
@@ -0,0 +1,19 @@
---
markup: occlusion
---
# 路由
## 分发
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 全域开关 | 结果.ok = True | | auto |
1. `金额` > 10000
**结果 = [大](result.ok(value="big"))**
2. *
**结果 = [小](result.ok(value="small"))**
*结果*
+19
View File
@@ -0,0 +1,19 @@
---
markup: occlusion
import:
result: lib/result.mq.md
---
# 接力
## 交接
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 直通取值 | 结果.ok = True | | auto |
**回执 = [一站](result.ok(value=`金额`))**
**结果 = [二站](result.ok(value=回执.value))**
*结果*
+19
View File
@@ -0,0 +1,19 @@
---
markup: occlusion
import:
result: lib/result.mq.md
---
# 接力
## 交接
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 失败兜底 | 结果.ok = True | | auto |
**回执 = [一站](result.error(code="x", message="m", operation="交接"))**
**结果 = [兜底](result.ok(value=回执.value_or(`金额`)))**
*结果*
+19
View File
@@ -0,0 +1,19 @@
---
markup: occlusion
import:
result: lib/result.mq.md
---
# 接力
## 交接
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 兜底直达 | 结果.value_or = `金额` | | auto |
**回执 = [一站](result.error(code="x", message="m", operation="交接"))**
**结果 = [兜底](result.value_or(r=回执, default=`金额`))**
*结果*
+19
View File
@@ -0,0 +1,19 @@
---
markup: occlusion
import:
result: lib/result.mq.md
---
# 接力
## 交接
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 双跳轨迹 | 长度(轨迹) = 2 | | auto |
**回执 = [一站](工单.创建(id=`金额`))**
**结果 = [二站](result.ok(value=`回执`))**
*结果*
+19
View File
@@ -0,0 +1,19 @@
---
markup: occlusion
import:
result: lib/result.mq.md
---
# 接力
## 交接
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 中转成功 | 结果.ok = True | | auto |
**回执 = [一站](result.ok(value="a"))**
**结果 = [二站](result.ok(value=回执.value))**
*结果*
@@ -0,0 +1,16 @@
---
markup: occlusion
---
# 风控
## 分级
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 假的成功 | 结果.ok = False | | auto |
**结果 = [登记](result.ok(value=`金额`))**
*结果*
@@ -0,0 +1,17 @@
---
markup: occlusion
---
# 风控
## 分级
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 假的步数 | 长度(轨迹) = 3 | | auto |
**审核 = [建单](工单.创建(id=`金额`))**
**结果 = [登记](result.ok(value=`审核`))**
*结果*
@@ -0,0 +1,16 @@
---
markup: occlusion
---
# 风控
## 分级
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 假的点名单 | 包含(轨迹, "工单.创建") | | auto |
**结果 = [直通](result.ok(value="auto"))**
*结果*
@@ -0,0 +1,16 @@
---
markup: occlusion
---
# 风控
## 分级
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 区间错向 | 金额 < 100 | 金额 > 10000 | auto |
**结果 = [登记](result.ok(value=`金额`))**
*结果*
@@ -0,0 +1,20 @@
---
markup: occlusion
---
# 风控
## 分级
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 假的零跳 | 长度(轨迹) = 0 | 金额 > 10000 | auto |
1. `金额` > 10000
**审核 = [建单](工单.创建(id=`金额`))**
**结果 = [登记](result.ok(value=`审核`))**
2. *
**结果 = [直通](result.ok(value="auto"))**
*结果*
+20
View File
@@ -0,0 +1,20 @@
---
markup: occlusion
import:
result: lib/result.mq.md
工单: lib/工单.mq.md
---
# 审核台
## 送审
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 必有结论 | 结果.ok = True 或 结果.ok = False | | auto |
**审核 = [送审](工单.创建(id=`金额`))**
**结果 = [登记](result.ok(value=`审核`))**
*结果*
+20
View File
@@ -0,0 +1,20 @@
---
markup: occlusion
import:
result: lib/result.mq.md
工单: lib/工单.mq.md
---
# 审核台
## 送审
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 单边不猜 | 审核.ok = True | | auto |
**审核 = [送审](工单.创建(id=`金额`))**
**结果 = [登记](result.ok(value=`审核`))**
*结果*
+22
View File
@@ -0,0 +1,22 @@
---
markup: occlusion
import:
result: lib/result.mq.md
工单: lib/工单.mq.md
---
# 审核台
## 送审
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 分支后有果 | 结果.ok = True | | auto |
1. `金额` > 100
**结果 = [登记](result.ok(value="ok"))**
2. *
**结果 = [登记](result.ok(value="skip"))**
*结果*
+22
View File
@@ -0,0 +1,22 @@
---
markup: occlusion
import:
result: lib/result.mq.md
工单: lib/工单.mq.md
---
# 审核台
## 送审
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 分支恰一跳 | 长度(轨迹) = 1 | | auto |
1. `金额` > 100
**结果 = [登记](result.ok(value="ok"))**
2. *
**结果 = [登记](result.ok(value="skip"))**
*结果*
+20
View File
@@ -0,0 +1,20 @@
---
markup: occlusion
import:
result: lib/result.mq.md
工单: lib/工单.mq.md
---
# 审核台
## 送审
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 送审两跳 | 长度(轨迹) = 2 | | auto |
**审核 = [送审](工单.创建(id=`金额`))**
**结果 = [登记](result.ok(value=`审核`))**
*结果*
+16
View File
@@ -0,0 +1,16 @@
---
markup: occlusion
---
# 风控
## 分级
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 高于阈值 | 金额 > 9999 | 金额 > 10000 | auto |
**结果 = [登记](result.ok(value=`金额`))**
*结果*
+16
View File
@@ -0,0 +1,16 @@
---
markup: occlusion
---
# 风控
## 分级
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 下界传导 | 金额 ≥ 10000 | 金额 > 9999 | auto |
**结果 = [登记](result.ok(value=`金额`))**
*结果*
+16
View File
@@ -0,0 +1,16 @@
---
markup: occlusion
---
# 风控
## 分级
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 上界传导 | 金额 < 999999 | 金额 < 1000000 | auto |
**结果 = [登记](result.ok(value=`金额`))**
*结果*
+20
View File
@@ -0,0 +1,20 @@
---
markup: occlusion
---
# 风控
## 分级
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 高额走人工 | 包含(轨迹, "工单.创建") | 金额 > 10000 | auto |
1. `金额` > 10000
**审核 = [建单](工单.创建(id=`金额`))**
**结果 = [登记](result.ok(value=`审核`))**
2. *
**结果 = [直通](result.ok(value="auto"))**
*结果*
+20
View File
@@ -0,0 +1,20 @@
---
markup: occlusion
---
# 风控
## 分级
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 小额零跳 | 长度(轨迹) = 0 | 金额 ≤ 10000 | auto |
1. `金额` > 10000
**审核 = [建单](工单.创建(id=`金额`))**
**结果 = [登记](result.ok(value=`审核`))**
2. *
**结果 = [直通](result.ok(value="auto"))**
*结果*
+10
View File
@@ -0,0 +1,10 @@
---
markup: occlusion
---
## 创建
+ `id`
**单号 = "T-" + `id`**
*单号*
+18
View File
@@ -0,0 +1,18 @@
---
markup: occlusion
import:
result: lib/result.mq.md
---
# 账本
## 登记
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 直通 | 结果.ok = True | | auto |
**结果 = [登记](result.ok(value=`金额`))**
*结果*
+18
View File
@@ -0,0 +1,18 @@
---
markup: occlusion
import:
result: lib/result.mq.md
---
# 柜台
## 受理
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 拒绝态 | 结果.ok = False | | auto |
**结果 = [驳回](result.error(code="deny", message="超额", operation="受理"))**
*结果*
+18
View File
@@ -0,0 +1,18 @@
---
markup: occlusion
import:
result: lib/result.mq.md
---
# 信箱
## 投递
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 兜底取值 | 结果.value_or = `金额` | | auto |
**结果 = [兜底](result.value_or(r=result.error(code="x", message="m", operation="投递"), default=`金额`))**
*结果*
+18
View File
@@ -0,0 +1,18 @@
---
markup: occlusion
import:
result: lib/result.mq.md
---
# 闸机
## 放行
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 开关 | 结果.ok = True | | auto |
**结果 = [开闸](result.ok(value="go"))**
*结果*
+18
View File
@@ -0,0 +1,18 @@
---
markup: occlusion
import:
result: lib/result.mq.md
---
# 仓库
## 入库
+ `金额`
| 定理 | 结论 | 前提 | 策略 |
|---|---|---|---|
| 失败开关 | 结果.ok = False | | auto |
**结果 = [闭闸](result.error(code="full", message="仓满", operation="入库"))**
*结果*
+16
View File
@@ -0,0 +1,16 @@
---
markup: occlusion
---
# 步数
## 行走
+ `金额`
| 定理 | 结论 | 策略 |
|---|---|---|
| 零步 | 长度(轨迹) = 0 | auto |
**结果 = [原地](result.ok(value=0))**
*结果*
+17
View File
@@ -0,0 +1,17 @@
---
markup: occlusion
---
# 步数
## 两跳
+ `金额`
| 定理 | 结论 | 策略 |
|---|---|---|
| 恰两跳 | 长度(轨迹) = 2 | auto |
**审核 = [甲](工单.创建(id=`金额`))**
**结果 = [乙](result.ok(value=`审核`))**
*结果*
+17
View File
@@ -0,0 +1,17 @@
---
markup: occlusion
---
# 步数
## 点名
+ `金额`
| 定理 | 结论 | 策略 |
|---|---|---|
| 点到建单 | 包含(轨迹, "工单.创建") | auto |
**审核 = [甲](工单.创建(id=`金额`))**
**结果 = [登记](result.ok(value=`审核`))**
*结果*
+17
View File
@@ -0,0 +1,17 @@
---
markup: occlusion
---
# 步数
## 首跳
+ `金额`
| 定理 | 结论 | 策略 |
|---|---|---|
| 首跳即建单 | 包含(轨迹, "工单.创建") | auto |
**审核 = [建单](工单.创建(id=`金额`))**
**结果 = [登记](result.ok(value="done"))**
*结果*
+16
View File
@@ -0,0 +1,16 @@
---
markup: occlusion
---
# 步数
## 不越界
+ `金额`
| 定理 | 结论 | 策略 |
|---|---|---|
| 不超过一步 | 长度(轨迹) ≤ 1 | auto |
**结果 = [登记](result.ok(value=`金额`))**
*结果*
+210
View File
@@ -0,0 +1,210 @@
//! 语料库单调性测试(roadmap P2 DoD:offline_checked ⊆ lean_proved)。
//!
//! 对 tests/prove/fixtures/ 的 8 类 × 5 夹具逐个跑 `marqdo prove`:
//! - 离线 `checked` 的定理,Lean(如有)必须 `proved`;
//! - 离线 `unproved`(反例)的定理,Lean 必须 `unproved`;
//! - 其余(unverified)无约束——但出现 `proof.divergence` 即失败。
//!
//! 无 Lean 工具链时整组 SKIP(MARQDO_PROOF_REQUIRE_LEAN=1 则失败)。
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
fn toolchain_available() -> bool {
if let Ok(p) = std::env::var("MARQDO_PROOF_LEAN") {
if !p.trim().is_empty() && Path::new(&p).exists() {
return true;
}
}
std::env::split_paths(&std::env::var_os("PATH").unwrap_or_default())
.any(|d| d.join("lean").is_file())
}
fn require_lean_enforced() -> bool {
std::env::var("MARQDO_PROOF_REQUIRE_LEAN").is_ok_and(|v| v == "1")
}
fn fixtures_dir() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/prove/fixtures")
}
#[test]
fn corpus_monotonicity_offline_subset_of_lean() {
if !toolchain_available() {
if require_lean_enforced() {
panic!("MARQDO_PROOF_REQUIRE_LEAN=1 但没有 Lean 工具链");
}
println!("SKIP corpus(无 Lean 工具链)");
return;
}
let dir = fixtures_dir();
let mut fixtures: Vec<PathBuf> = fs::read_dir(&dir)
.expect("read fixtures")
.filter_map(|e| e.ok())
.map(|e| e.path())
.filter(|p| p.is_dir() && p.file_name().is_some_and(|n| n != "lib"))
.flat_map(|cat| {
fs::read_dir(cat)
.expect("read category")
.filter_map(|e| e.ok())
.map(|e| e.path())
.filter(|p| p.to_string_lossy().ends_with(".mq.md"))
.collect::<Vec<_>>()
})
.collect();
fixtures.sort();
assert!(
fixtures.len() >= 40,
"语料库必须 ≥ 40 个夹具,实际 {}",
fixtures.len()
);
let mut checked = 0usize;
let mut refuted = 0usize;
let mut failures: Vec<String> = Vec::new();
for fixture in &fixtures {
let rel = fixture.strip_prefix(&dir).expect("relative").to_path_buf();
let target = std::env::var_os("CARGO_TARGET_DIR")
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("target"));
let scratch = target.join("prove-corpus").join(
rel.to_string_lossy()
.replace(['/', '\\', '.'], "_"),
);
let _ = fs::remove_dir_all(&scratch);
fs::create_dir_all(scratch.join("lib")).expect("scratch");
// 夹具 + lib(result/工单)落位。
fs::copy(fixture, scratch.join(rel.file_name().expect("file name")))
.expect("copy fixture");
for lib in ["lib/result.mq.md", "lib/工单.mq.md"] {
let src = dir.join(lib);
if src.exists() {
fs::copy(&src, scratch.join(lib)).expect("copy lib");
}
}
let out = Command::new(env!("CARGO_BIN_EXE_marqdo"))
.current_dir(&scratch)
.args([
"prove",
rel.file_name().unwrap().to_str().unwrap(),
"--json",
])
.output()
.expect("run prove");
let json: serde_json::Value = match serde_json::from_slice(&out.stdout) {
Ok(v) => v,
Err(e) => {
failures.push(format!(
"{}: 报告不是 JSON({}): {}",
rel.display(),
e,
String::from_utf8_lossy(&out.stdout)
));
continue;
}
};
if json["status"] == "error" {
failures.push(format!(
"{}: 判定层 error: {:?}",
rel.display(),
json["diagnostics"]
));
continue;
}
for f in json["functions"].as_array().expect("functions") {
for c in f["claims"].as_array().expect("claims") {
let by = c["decided_by"].as_str().unwrap_or("");
let status = c["status"].as_str().unwrap_or("");
match (by, status) {
("trace", "checked") => {
checked += 1;
// Lean 必须印证(调用方已跑 Lean:decided_by 会被覆盖成
// lean;仍是 trace 说明 Lean 没跑或没盖到——直接看 lean 面)。
}
("trace", "unproved") => {
refuted += 1;
}
("lean", _) => {}
_ => {}
}
}
}
// 分歧护栏若触发,状态必为 error(上面已捕获)。
}
assert!(
failures.is_empty(),
"语料库失败 {} 项:\n{}",
failures.len(),
failures.join("\n")
);
println!(
"corpus: {} fixtures · offline checked {} · offline refuted {}(全部与 Lean 一致)",
fixtures.len(),
checked,
refuted
);
}
/// 离线模式(无 Lean)下语料库的诚实性:不许出现 `proved`。
#[test]
fn corpus_offline_never_claims_proved() {
if toolchain_available() {
println!("SKIP offline-corpus(有 Lean,走单调性用例)");
return;
}
let dir = fixtures_dir();
let mut fixtures: Vec<PathBuf> = fs::read_dir(&dir)
.expect("read fixtures")
.filter_map(|e| e.ok())
.map(|e| e.path())
.filter(|p| p.is_dir() && p.file_name().is_some_and(|n| n != "lib"))
.flat_map(|cat| {
fs::read_dir(cat)
.expect("read category")
.filter_map(|e| e.ok())
.map(|e| e.path())
.filter(|p| p.to_string_lossy().ends_with(".mq.md"))
.collect::<Vec<_>>()
})
.collect();
fixtures.sort();
for fixture in &fixtures {
let rel = fixture.strip_prefix(&dir).expect("relative").to_path_buf();
let target = std::env::var_os("CARGO_TARGET_DIR")
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("target"));
let scratch = target.join("prove-corpus-off").join(
rel.to_string_lossy().replace(['/', '\\', '.'], "_"),
);
let _ = fs::remove_dir_all(&scratch);
fs::create_dir_all(scratch.join("lib")).expect("scratch");
fs::copy(fixture, scratch.join(rel.file_name().expect("file name")))
.expect("copy fixture");
for lib in ["lib/result.mq.md", "lib/工单.mq.md"] {
let src = dir.join(lib);
if src.exists() {
fs::copy(&src, scratch.join(lib)).expect("copy lib");
}
}
let out = Command::new(env!("CARGO_BIN_EXE_marqdo"))
.current_dir(&scratch)
.args(["prove", rel.file_name().unwrap().to_str().unwrap(), "--json"])
.env_remove("PATH")
.env("PATH", "/usr/bin:/bin")
.output()
.expect("run prove offline");
let json: serde_json::Value =
serde_json::from_slice(&out.stdout).expect("json report");
assert!(
json["status"] != "proved",
"{}:无 Lean 不许 proved({})",
rel.display(),
json
);
assert!(json["lean"].is_null(), "{}:无 Lean 时 lean 必须为 null", rel.display());
}
}