feat: complete BoundCall dual-backend and reuse bind gate (G02)

Share resolve_for_exec across Tree/Bytecode and semantic projections,
emit bound_calls/dynamic_calls, and require eng_reuse bind_targets before
execute so discovery scores cannot be treated as handles.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-10-23 23:06:34 +08:00
1 parent e9ae7ff1ae
commit 0e1bfb3359
12 files changed
+925 -154

No files matched your search

+120 -3
View File
@@ -1,8 +1,7 @@
//! S0 executable-knowledge baseline gate (offline).
//! Executable-knowledge gate (offline): S0 baseline + S1/S2 BoundCall/effects.
//!
//! See `tests/executable_knowledge/README.md`. Design-status samples may run
//! but do not prove source/applicability. Expected failures are asserted;
//! the bytecode ambiguous-name defect is fingerprinted until S2 BoundCall.
//! but do not prove source/applicability. Expected failures are asserted.
use std::process::Command;
@@ -417,3 +416,121 @@ fn s0_06b_ambiguous_bare_name_rejected_both_backends() {
);
}
}
#[test]
fn s2_g02_boundcall_path_and_dynamic_projections() {
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let out = std::env::temp_dir().join(format!(
"marqdo-ek-g02-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
let _ = std::fs::remove_dir_all(&out);
let status = Command::new(env!("CARGO_BIN_EXE_marqdo"))
.args([
"knowledge",
"tests/executable_knowledge/baseline/10-boundcall",
"-o",
out.to_str().unwrap(),
])
.current_dir(&root)
.output()
.expect("knowledge compile");
assert_eq!(
status.status.code(),
Some(0),
"stderr={}",
String::from_utf8_lossy(&status.stderr)
);
let bound: serde_json::Value = serde_json::from_str(
&std::fs::read_to_string(out.join("bound_calls.json")).unwrap(),
)
.unwrap();
let dynamic: serde_json::Value = serde_json::from_str(
&std::fs::read_to_string(out.join("dynamic_calls.json")).unwrap(),
)
.unwrap();
let bounds = bound["bound_calls"].as_array().expect("bound_calls");
assert!(
bounds.iter().any(|c| {
c["binding"] == "BoundCall"
&& c["call_site"].as_str().unwrap_or("").contains("pricing.商品小计")
&& c["target"].as_str().is_some_and(|t| !t.is_empty())
}),
"import path must be BoundCall with SymbolId: {bound}"
);
let dyns = dynamic["dynamic_calls"].as_array().expect("dynamic_calls");
assert!(
dyns.iter().any(|c| {
c["binding"] == "DynamicCall" && c["reason"] == "call_fn"
}),
"call_fn must be DynamicCall: {dynamic}"
);
// Edges from caller must include the BoundCall target SymbolId.
let edges: serde_json::Value = serde_json::from_str(
&std::fs::read_to_string(out.join("graph/edges.json")).unwrap(),
)
.unwrap();
let target = bounds
.iter()
.find(|c| c["call_site"].as_str().unwrap_or("").contains("pricing.商品小计"))
.unwrap()["target"]
.as_str()
.unwrap();
let edge_txt = edges.to_string();
assert!(
edge_txt.contains(target),
"graph edges must mention BoundCall target {target}: {edges}"
);
let _ = std::fs::remove_dir_all(&out);
}
#[test]
fn s2_g02_boundcall_runs_both_backends() {
assert_out_both(
"tests/executable_knowledge/baseline/10-boundcall/main.mq.md",
"hi\n60",
);
}
#[test]
fn s2_g07_eng_reuse_requires_bind_before_execute() {
// Discovery scores must not be treated as executable handles.
use marqdo::knowledge::ir::{ReuseCandidate, ReuseDecision, ReuseResult};
use marqdo::knowledge::reuse::reuse_json;
let result = ReuseResult {
decision: ReuseDecision::Reuse,
confidence: 0.9,
capability: Some("定价".into()),
symbol: Some("商品小计".into()),
source: None,
reason: vec!["score meets REUSE threshold".into()],
candidates: vec![ReuseCandidate {
id: "capability:定价".into(),
kind: "Capability".into(),
title: "定价".into(),
score: 0.9,
resource: None,
reason: vec![],
}],
constraints: vec![],
decisions: vec![],
failures: vec![],
create_allowed: false,
recommended: vec!["REUSE 定价".into()],
};
let v = reuse_json("商品小计", &result);
assert_eq!(v["discovery_only"], true, "{v}");
assert_eq!(v["bind_required"], true, "{v}");
assert!(
v["bind_targets"]
.as_array()
.unwrap()
.iter()
.any(|t| t["query"] == "商品小计"),
"REUSE must expose bind_targets for MLSP bind: {v}"
);
}
+5 -2
View File
@@ -17,11 +17,14 @@
| 05 | `baseline/05-policy-draft/` | **design** | 可试跑 → `113`;**不**宣称税率事实已证实 | 叙述中的来源意图(尚未有强制 schema) |
| 06a | `baseline/06-reject/missing-import.mq.md` | expected_failure | 双后端均非 0 | 0(正确拒绝) |
| 06b | `baseline/06-reject/ambiguous.mq.md` | expected_failure | **双后端均拒绝**裸名同名(G02) | — |
| 10 | `baseline/10-boundcall/` | implemented | BoundCall 路径 SymbolId + DynamicCall(`call_fn`);双后端 `hi`/`60` | import 一行 |
### 同名裸调用(G02)
`06-reject/ambiguous.mq.md` 同时 `import` 了 `a` 与 `b`(同名 `商品小计`),正文用**裸名**调用。双后端均须拒绝;正确写法是 `a.商品小计` / `b.商品小计` 等显式路径。
`10-boundcall`:显式 `pricing.商品小计` 写入 `bound_calls.json`(`binding: BoundCall` + SymbolId);`call_fn` 写入 `dynamic_calls.json`(不得假装已绑定)。
## 不计为成功的项
- `status: design` 样例通过运行 ≠ 来源/适用性/ready 已实现(G05/G07)
@@ -38,9 +41,9 @@
| S1 同名 | **done** | `08-same-name` resolve→A + 运行 `60` |
| S2/G06 | **done** | 包装继承 `fs.write`;未知不标 pure |
| S2/G07 子集 | **done** | MLSP bind 含 `symbol_id`;execute 按源行绑定;pure 拒非纯 |
| S2/G02 BoundCall | **done(作用域子集)** | Bytecode 裸名解析不跨 import scope;`06b` 双后端拒绝 |
| S2/G02 BoundCall | **done** | 共享 `resolve_for_exec`;Tree/Bytecode 静态路径绑定;`bound_calls.json` / `dynamic_calls.json`;`10-boundcall` + `06b` |
| S2/G07 eng_reuse | **done(子集)** | `reuse_json` / `agent_eng_reuse`:`discovery_only` + `bind_required` + `bind_targets`;执行仍须 MLSP bind |
## 与后续切片的关系
- S2 后续:sema 路径 BoundCall 记录、Agent eng_reuse 强制 bind
- S3+:局部编辑、来源适用性、整代快照 — 见缺口路线图
@@ -0,0 +1,21 @@
---
markup: occlusion
title: S2 G02 BoundCall 路径绑定与动态标记
status: implemented
import:
pricing: pricing.mq.md
---
## helper
动态目标演示用无参函数。
> print text=hi
# main
显式 import 路径必须进 BoundCall;`call_fn` 必须进 DynamicCall。
**小计 = [两件](pricing.商品小计(数量=2, 单价=30))**
> call_fn name=helper
> print text=`小计`
@@ -0,0 +1,11 @@
---
markup: occlusion
title: BoundCall 目标定价
status: implemented
---
## 商品小计
根据购买数量`数量`和商品单价`单价`计算小计。
*[按数量与单价计算](数量 * 单价)*