release: v2.2.0 call one function in any .mq.md
The same page is the document, the knowledge and the program. marqdo call runs that function with arguments, so reuse, tools and tests do not need a second skill system. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
4018e9928a
commit
1730f26fc7
42 files changed
+1917
-116
No files matched your search
@@ -0,0 +1,11 @@
|
||||
# 甲
|
||||
|
||||
## 加法
|
||||
|
||||
*[计算结果](1)*
|
||||
|
||||
# 乙
|
||||
|
||||
## 加法
|
||||
|
||||
*[计算结果](2)*
|
||||
@@ -0,0 +1,11 @@
|
||||
# 校验
|
||||
|
||||
## 加一
|
||||
|
||||
对于输入变量`n`,期望是数字。
|
||||
|
||||
| 参数 | 类型 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| n | 数值 | 输入值 |
|
||||
|
||||
*[结果](n + 1)*
|
||||
@@ -0,0 +1,13 @@
|
||||
# 危险
|
||||
|
||||
## 写入
|
||||
|
||||
> fs.write_text path=sent.txt text=no
|
||||
|
||||
*None*
|
||||
|
||||
## 联网
|
||||
|
||||
> net.http_post url="http://127.0.0.1:9/nope" body="{}"
|
||||
|
||||
*None*
|
||||
@@ -0,0 +1,5 @@
|
||||
## 加一
|
||||
|
||||
给定`n`。
|
||||
|
||||
*[结果](n + 1)*
|
||||
@@ -0,0 +1,17 @@
|
||||
---
|
||||
import:
|
||||
inc: inc.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
*0*
|
||||
|
||||
# 包装
|
||||
|
||||
## 再加一
|
||||
|
||||
给定`n`。
|
||||
|
||||
**结果 = [调用增量](inc.加一(n=n))**
|
||||
*结果*
|
||||
@@ -0,0 +1,11 @@
|
||||
# main
|
||||
|
||||
*0*
|
||||
|
||||
# 计算器
|
||||
|
||||
## 加法
|
||||
|
||||
计算两个数字`a`和`b`的和。
|
||||
|
||||
*[计算结果](a + b)*
|
||||
@@ -0,0 +1,14 @@
|
||||
---
|
||||
runtime: markdown
|
||||
tools: 计算器.加法
|
||||
---
|
||||
|
||||
# 计算器
|
||||
|
||||
这份文件不是 SKILL。`runtime` 和 `tools` 不参与调用。
|
||||
|
||||
## 加法
|
||||
|
||||
计算两个数字`a`和`b`的和。
|
||||
|
||||
*[计算结果](a + b)*
|
||||
@@ -0,0 +1,7 @@
|
||||
# 点
|
||||
|
||||
## 横坐标
|
||||
|
||||
读取`self`。
|
||||
|
||||
*[结果](self)*
|
||||
@@ -0,0 +1,219 @@
|
||||
//! `marqdo call` runs one function. These tests start the real binary.
|
||||
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Command;
|
||||
|
||||
fn bin() -> &'static str {
|
||||
env!("CARGO_BIN_EXE_marqdo")
|
||||
}
|
||||
|
||||
fn root() -> PathBuf {
|
||||
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
}
|
||||
|
||||
fn run(args: &[&str]) -> std::process::Output {
|
||||
Command::new(bin())
|
||||
.args(args)
|
||||
.current_dir(root())
|
||||
.output()
|
||||
.expect("spawn marqdo")
|
||||
}
|
||||
|
||||
fn text(bytes: &[u8]) -> String {
|
||||
String::from_utf8_lossy(bytes).trim().to_string()
|
||||
}
|
||||
|
||||
fn assert_value(file: &Path, function: &str, args: &[&str], backend: &str, expect: &str) {
|
||||
let file = file.to_str().unwrap();
|
||||
let mut cmd = vec!["call", "--backend", backend, file, function];
|
||||
cmd.extend_from_slice(args);
|
||||
let out = run(&cmd);
|
||||
assert_eq!(
|
||||
out.status.code(),
|
||||
Some(0),
|
||||
"backend={backend} stdout={} stderr={}",
|
||||
text(&out.stdout),
|
||||
text(&out.stderr)
|
||||
);
|
||||
assert_eq!(text(&out.stdout), expect, "backend={backend}");
|
||||
}
|
||||
|
||||
fn assert_code(file: &Path, function: &str, args: &[&str], code: &str) {
|
||||
let file = file.to_str().unwrap();
|
||||
let mut cmd = vec!["call", "--json", file, function];
|
||||
cmd.extend_from_slice(args);
|
||||
let out = run(&cmd);
|
||||
assert_ne!(out.status.code(), Some(0), "stdout={}", text(&out.stdout));
|
||||
let body: serde_json::Value = serde_json::from_str(&text(&out.stdout)).unwrap_or_else(|e| {
|
||||
panic!("json stdout: {e}\n{}", text(&out.stdout));
|
||||
});
|
||||
assert_eq!(body["ok"], false, "{body}");
|
||||
assert_eq!(body["error"]["code"], code, "{body}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn calculator_matches_the_documented_examples() {
|
||||
let file = root().join("examples/calculator.mq.md");
|
||||
for backend in ["tree", "bytecode"] {
|
||||
assert_value(&file, "计算器.加法", &["a=2", "b=3"], backend, "5");
|
||||
assert_value(&file, "计算器.加法", &["a=-2", "b=3"], backend, "1");
|
||||
assert_value(&file, "计算器.乘法", &["a=4", "b=6"], backend, "24");
|
||||
assert_value(&file, "计算器.乘法", &["a=0", "b=100"], backend, "0");
|
||||
assert_value(&file, "加法", &["a=2", "b=3"], backend, "5");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn symbol_id_selects_the_same_function_on_both_backends() {
|
||||
let file = root().join("examples/calculator.mq.md");
|
||||
let compiled = marqdo::sema::compile_file_closure(&file).expect("compile calculator");
|
||||
let artifact = compiled
|
||||
.semantics
|
||||
.artifacts
|
||||
.iter()
|
||||
.find(|artifact| artifact.display_name == "计算器.加法")
|
||||
.expect("加法 artifact");
|
||||
let id = artifact
|
||||
.symbol_id
|
||||
.as_ref()
|
||||
.expect("symbol id")
|
||||
.as_str()
|
||||
.to_string();
|
||||
let args = vec![
|
||||
("a".to_string(), marqdo::value::Value::Int(2)),
|
||||
("b".to_string(), marqdo::value::Value::Int(3)),
|
||||
];
|
||||
for backend in [marqdo::Backend::Tree, marqdo::Backend::Bytecode] {
|
||||
let value = marqdo::call::call_file(&file, &id, &args, backend).unwrap_or_else(|err| {
|
||||
panic!("symbol id call failed on {backend:?}: {err:#}")
|
||||
});
|
||||
assert_eq!(value, marqdo::value::Value::Int(5));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn json_envelope_and_metadata_are_not_a_skill_gate() {
|
||||
let file = root().join("tests/call/metadata.mq.md");
|
||||
let out = run(&[
|
||||
"call",
|
||||
"--json",
|
||||
file.to_str().unwrap(),
|
||||
"计算器.加法",
|
||||
"a=2",
|
||||
"b=3",
|
||||
]);
|
||||
assert_eq!(out.status.code(), Some(0), "{}", text(&out.stderr));
|
||||
let body: serde_json::Value = serde_json::from_str(&text(&out.stdout)).unwrap();
|
||||
assert_eq!(body, serde_json::json!({"ok": true, "result": 5}));
|
||||
|
||||
let trailing = run(&[
|
||||
"call",
|
||||
file.to_str().unwrap(),
|
||||
"计算器.加法",
|
||||
"a=2",
|
||||
"b=3",
|
||||
"--backend",
|
||||
"bytecode",
|
||||
"--json",
|
||||
]);
|
||||
assert_eq!(trailing.status.code(), Some(0), "{}", text(&trailing.stderr));
|
||||
let body: serde_json::Value = serde_json::from_str(&text(&trailing.stdout)).unwrap();
|
||||
assert_eq!(body, serde_json::json!({"ok": true, "result": 5}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bytecode_does_not_run_main() {
|
||||
let file = root().join("tests/call/main-split.mq.md");
|
||||
for backend in ["tree", "bytecode"] {
|
||||
assert_value(&file, "计算器.加法", &["a=2", "b=3"], backend, "5");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_unknown_and_contract_args_are_structured() {
|
||||
let calc = root().join("examples/calculator.mq.md");
|
||||
assert_code(&calc, "没有这个函数", &[], "artifact.unresolved");
|
||||
assert_code(&calc, "计算器.加法", &["a=2"], "call.missing_arg");
|
||||
assert_code(
|
||||
&calc,
|
||||
"计算器.加法",
|
||||
&["a=2", "b=3", "c=1"],
|
||||
"call.unknown_arg",
|
||||
);
|
||||
let contract = root().join("tests/call/contract.mq.md");
|
||||
assert_code(&contract, "校验.加一", &["n=True"], "contract.arg_mismatch");
|
||||
assert_value(&contract, "校验.加一", &["n=3"], "tree", "4");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn same_short_name_is_ambiguous() {
|
||||
let file = root().join("tests/call/ambiguous.mq.md");
|
||||
assert_code(&file, "加法", &[], "artifact.ambiguous");
|
||||
assert_value(&file, "甲.加法", &[], "tree", "1");
|
||||
assert_value(&file, "乙.加法", &[], "bytecode", "2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn instance_method_is_refused() {
|
||||
let file = root().join("tests/call/method.mq.md");
|
||||
assert_code(&file, "点.横坐标", &[], "artifact.not_executable");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_is_executed_and_call_lib_path_stays_on_bytecode() {
|
||||
let site = root().join("tests/call/import/site.mq.md");
|
||||
for backend in ["tree", "bytecode"] {
|
||||
assert_value(&site, "包装.再加一", &["n=4"], backend, "5");
|
||||
}
|
||||
let tmp = std::env::temp_dir().join(format!("mq-call-lib-{}", std::process::id()));
|
||||
let _ = fs::remove_dir_all(&tmp);
|
||||
fs::create_dir_all(&tmp).unwrap();
|
||||
let args = tmp.join("args.json");
|
||||
let emit = tmp.join("out.json");
|
||||
fs::write(&args, r#"{"n":4}"#).unwrap();
|
||||
let out = Command::new(bin())
|
||||
.args([
|
||||
"run",
|
||||
site.to_str().unwrap(),
|
||||
"--backend",
|
||||
"bytecode",
|
||||
"--call-lib-path",
|
||||
"inc.加一",
|
||||
"--args-json",
|
||||
args.to_str().unwrap(),
|
||||
"--emit-result",
|
||||
emit.to_str().unwrap(),
|
||||
])
|
||||
.current_dir(root())
|
||||
.output()
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
out.status.code(),
|
||||
Some(0),
|
||||
"stderr={} stdout={}",
|
||||
text(&out.stderr),
|
||||
text(&out.stdout)
|
||||
);
|
||||
assert_eq!(fs::read_to_string(&emit).unwrap().trim(), "5");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn impure_calls_do_not_run() {
|
||||
let src = root().join("tests/call/effects.mq.md");
|
||||
let dir = std::env::temp_dir().join(format!("mq-call-fx-{}", std::process::id()));
|
||||
let _ = fs::remove_dir_all(&dir);
|
||||
fs::create_dir_all(&dir).unwrap();
|
||||
let file = dir.join("effects.mq.md");
|
||||
fs::copy(&src, &file).unwrap();
|
||||
for function in ["危险.写入", "危险.联网"] {
|
||||
let out = run(&["call", "--json", file.to_str().unwrap(), function]);
|
||||
assert_ne!(out.status.code(), Some(0), "{function} {}", text(&out.stdout));
|
||||
let body: serde_json::Value = serde_json::from_str(&text(&out.stdout)).unwrap();
|
||||
assert_eq!(body["error"]["code"], "artifact.effect_denied", "{body}");
|
||||
}
|
||||
assert!(
|
||||
!dir.join("sent.txt").exists(),
|
||||
"denied write must not create sent.txt"
|
||||
);
|
||||
}
|
||||
@@ -1417,6 +1417,56 @@ fn m05_skill_front_has_occlusion_core_not_classic_index() {
|
||||
root.join("skills/marqdo/migration-classic.md").is_file(),
|
||||
"Classic rules must live in migration-classic.md"
|
||||
);
|
||||
for rel in ["skills/marqdo/SKILL.md", ".cursor/skills/marqdo/SKILL.md"] {
|
||||
let text = std::fs::read_to_string(root.join(rel)).unwrap();
|
||||
let core = text.split("## Minimal program").next().unwrap_or(&text);
|
||||
assert!(
|
||||
core.contains("| `**…**` | Statement") && core.contains("| `*…*` | Return |"),
|
||||
"{rel} marker table must match the parser: bold statement, italic return"
|
||||
);
|
||||
assert!(
|
||||
!core.contains("= code / return expression") && !core.contains("| `*…*` | Statement"),
|
||||
"{rel} still teaches the inverted marker polarity"
|
||||
);
|
||||
}
|
||||
let skill = std::fs::read_to_string(root.join("skills/marqdo/SKILL.md")).unwrap();
|
||||
let golden = fence_after(&skill, "## Call one function");
|
||||
let dir = std::env::temp_dir().join(format!("mq-skill-call-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let file = dir.join("calculator.mq.md");
|
||||
std::fs::write(&file, golden).unwrap();
|
||||
let out = std::process::Command::new(env!("CARGO_BIN_EXE_marqdo"))
|
||||
.args([
|
||||
"call",
|
||||
file.to_str().unwrap(),
|
||||
"计算器.加法",
|
||||
"a=2",
|
||||
"b=3",
|
||||
])
|
||||
.output()
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
out.status.code(),
|
||||
Some(0),
|
||||
"skill golden failed\nstderr={}\nstdout={}\nsource=\n{golden}",
|
||||
String::from_utf8_lossy(&out.stderr),
|
||||
String::from_utf8_lossy(&out.stdout)
|
||||
);
|
||||
assert_eq!(String::from_utf8_lossy(&out.stdout).trim(), "5");
|
||||
}
|
||||
|
||||
fn fence_after<'a>(body: &'a str, heading: &str) -> &'a str {
|
||||
let rest = body
|
||||
.split_once(heading)
|
||||
.unwrap_or_else(|| panic!("missing {heading}"))
|
||||
.1;
|
||||
let start = rest
|
||||
.find("```markdown\n")
|
||||
.unwrap_or_else(|| panic!("{heading} has no markdown fence"));
|
||||
let rest = &rest[start + "```markdown\n".len()..];
|
||||
let end = rest.find("\n```").expect("fence end");
|
||||
rest[..end].trim()
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1643,6 +1693,14 @@ fn m05_skill_core_card_has_access_not_classic_index() {
|
||||
agent.contains("## skill_core_card") && agent.contains("access(coll, key)"),
|
||||
"skill_core_card must teach access()"
|
||||
);
|
||||
assert!(
|
||||
agent.contains("Bold markers = statement; italic markers = return."),
|
||||
"skill card must match parser polarity"
|
||||
);
|
||||
assert!(
|
||||
!agent.contains("italic markers = statement"),
|
||||
"skill card still teaches inverted markers"
|
||||
);
|
||||
assert!(
|
||||
!agent.contains("never **bold** to print") || agent.contains("never use bold markers"),
|
||||
"core card must not rely on conflicting bold-as-print Classic wording"
|
||||
|
||||
@@ -124,6 +124,7 @@ fn mr07_supervisor_dry_run_slots_and_publish_recover() {
|
||||
selection_ref: None,
|
||||
capabilities_ref: None,
|
||||
deadline_at: "2099-01-01T00:00:00Z".into(),
|
||||
fs_root: Some(root.display().to_string()),
|
||||
},
|
||||
};
|
||||
let a = sup.dispatch(req("a")).unwrap();
|
||||
|
||||
Reference in new issue
Block a user