- src/mlsp.rs: 行分隔 JSON 协议 + 五 handler;语法唯一事实来源 = CORE_CONSTRUCTS(只加检索别名) - repair_targets 带有界护栏策略(anchored-local / 越界必拒 abstain / max_attempts=2) - schema 契约导出(T2.6):形参/返回/字段契约 + 升参清单 + 锚点 - marqdo mlsp 子命令:stdio 行分隔 JSON 服务 - tests/mlsp.rs 7 例全绿;全量回归零失败(含 262 gold) Co-authored-by: Cursor <cursoragent@cursor.com>
104 lines
4.1 KiB
Rust
104 lines
4.1 KiB
Rust
//! MLSP for AI(T3.1/T3.2 + T2.6):`locate` / `syntax` / `validate` / `repair_targets` / `schema`。
|
||
//!
|
||
//! 验收对照 `doc/roadmap/three-problems-plan.md` Phase 3:
|
||
//! AI 不必背语法细节(syntax/locate 可查)、schema 可导出、修复靶点有界(越界必拒)。
|
||
|
||
use marqdo::mlsp::handle_line;
|
||
|
||
fn call(req: &str) -> serde_json::Value {
|
||
serde_json::from_str(&handle_line(req)).expect("MLSP 响应必须是合法 JSON")
|
||
}
|
||
|
||
#[test]
|
||
fn syntax_query_returns_construct_cards() {
|
||
let resp = call(r#"{"id":1,"method":"syntax","params":{"query":"返回"}}"#);
|
||
assert_eq!(resp["ok"], true, "{resp}");
|
||
let ids: Vec<&str> = resp["result"]["cards"]
|
||
.as_array()
|
||
.unwrap()
|
||
.iter()
|
||
.map(|c| c["id"].as_str().unwrap())
|
||
.collect();
|
||
assert!(ids.contains(&"italic-return"), "「返回」应命中斜体返回卡片: {resp}");
|
||
assert!(resp["result"]["cards"][0]["manual"]
|
||
.as_str()
|
||
.unwrap()
|
||
.contains("core-surface"));
|
||
}
|
||
|
||
#[test]
|
||
fn syntax_unknown_query_structured_error() {
|
||
let resp = call(r#"{"id":2,"method":"syntax","params":{"query":"量子纠缠语法"}}"#);
|
||
assert_eq!(resp["ok"], false);
|
||
assert_eq!(resp["error"]["code"], "mlsp.no_match");
|
||
}
|
||
|
||
#[test]
|
||
fn locate_symbol_returns_span_and_contract() {
|
||
let src = "# main\n\n## 加一\n\n对于输入变量`n`,执行加一。\n\n| 参数 | 类型 | 说明 |\n|------|------|------|\n| n | number | 输入值 |\n\n*n+1*\n";
|
||
let req = serde_json::json!({"id":3,"method":"locate","params":{"source": src, "symbol": "加一"}});
|
||
let resp = call(&req.to_string());
|
||
assert_eq!(resp["ok"], true, "{resp}");
|
||
assert_eq!(resp["result"]["unit"], "加一");
|
||
assert_eq!(resp["result"]["contract"]["params"][0]["type"], "number");
|
||
assert!(resp["result"]["span"]["line"].as_u64().unwrap() >= 1);
|
||
}
|
||
|
||
#[test]
|
||
fn validate_reports_contract_drift() {
|
||
let src = "# main\n\n## 加一\n\n对于输入变量`n`,执行加一。\n\n| 参数 | 类型 | 说明 |\n|------|------|------|\n| nn | number | 输入值 |\n\n*n+1*\n";
|
||
let req = serde_json::json!({"id":4,"method":"validate","params":{"source": src}});
|
||
let resp = call(&req.to_string());
|
||
assert_eq!(resp["ok"], true, "{resp}");
|
||
assert_eq!(resp["result"]["ok"], false, "漂移必须让 validate 不通过: {resp}");
|
||
let codes: Vec<&str> = resp["result"]["diagnostics"]
|
||
.as_array()
|
||
.unwrap()
|
||
.iter()
|
||
.map(|d| d["code"].as_str().unwrap())
|
||
.collect();
|
||
assert!(codes.contains(&"contract.param_drift"), "{resp}");
|
||
}
|
||
|
||
#[test]
|
||
fn schema_exports_progressive_contract() {
|
||
let req = serde_json::json!({
|
||
"id": 5,
|
||
"method": "schema",
|
||
"params": {"path": concat!(env!("CARGO_MANIFEST_DIR"), "/tests/contracts/ok_contract.mq.md"), "unit": "加一"}
|
||
});
|
||
let resp = call(&req.to_string());
|
||
assert_eq!(resp["ok"], true, "{resp}");
|
||
assert_eq!(resp["result"]["contract"]["params"][0]["name"], "n");
|
||
assert_eq!(resp["result"]["contract"]["params"][0]["type"], "number");
|
||
assert_eq!(resp["result"]["contract"]["returns"]["type"], "number");
|
||
assert!(resp["result"]["contract"]["doc_anchor"]
|
||
.as_str()
|
||
.unwrap()
|
||
.contains("#L"));
|
||
}
|
||
|
||
#[test]
|
||
fn repair_targets_are_bounded() {
|
||
let resp = call(
|
||
r#"{"id":6,"method":"repair_targets","params":{"code":"contract.arg_mismatch","doc_anchor":"a.mq.md#L9-L15","suggestion":"把实参改为 number"}}"#,
|
||
);
|
||
assert_eq!(resp["ok"], true, "{resp}");
|
||
let repair = &resp["result"]["repair"];
|
||
assert_eq!(repair["strategy"], "anchored-local");
|
||
assert_eq!(repair["bounded"], true);
|
||
assert_eq!(repair["on_violation"], "abstain");
|
||
assert_eq!(resp["result"]["targets"][0]["doc_anchor"], "a.mq.md#L9-L15");
|
||
}
|
||
|
||
#[test]
|
||
fn unknown_method_and_bad_json_are_structured() {
|
||
let resp = call(r#"{"id":7,"method":"divine","params":{}}"#);
|
||
assert_eq!(resp["ok"], false);
|
||
assert_eq!(resp["error"]["code"], "mlsp.unknown_method");
|
||
|
||
let resp = call("not json at all");
|
||
assert_eq!(resp["ok"], false);
|
||
assert_eq!(resp["error"]["code"], "mlsp.bad_request");
|
||
}
|