feat(analysis): consume the current public Analysis v3 contract

- add the process boundary for the core CLI: explicit MARQDO_BIN/--marqdo-bin
  only (never PATH), argv-only invocation of check ENTRY --emit, bounded
  timeout, private temp artifact, exit checks and absolute-path sanitisation
- deserialize every Analysis record with closed serde shapes and validate
  semantic invariants that JSON Schema cannot express: identities, source
  digests, UTF-8 spans, unit tree/order/mode ancestry, anchors, functions and
  signatures, references states, calls, resources, diagnostics and budgets
- validate statement/expression trees structurally with depth and span checks
- convert accepted modules into DocumentFacts, preserving honest upstream
  reference states and one node per Exec unit/function
- merge modules across entry contexts only when identity and source agree,
  otherwise report knowledge.analysis_context_conflict
- add the real rc.4 Analysis positive fixture (with provenance and regeneration
  script), tamper negative tests and optional real-binary integration tests
This commit is contained in:
marqdo-knowledge committed 2026-11-26 00:29:19 +08:00
1 parent 682717a610
commit 59d5afe10b
14 files changed
+3474 -57

No files matched your search

+98
View File
@@ -0,0 +1,98 @@
//! Integration tests for the core CLI boundary and Analysis adapter.
//!
//! These tests need a real `marqdo` binary. Set `MARQDO_TEST_BIN` to run them;
//! without it they report a skip and do not claim evidence. The repository
//! keeps real captured artefacts under `tests/fixtures/analysis/` for the
//! offline contract tests.
use marqdo_knowledge::document::analysis;
use marqdo_knowledge::document::core_cli::CoreCli;
use std::path::{Path, PathBuf};
fn core_bin() -> Option<PathBuf> {
let value = std::env::var_os("MARQDO_TEST_BIN")?;
let path = PathBuf::from(value);
if path.is_file() {
Some(path)
} else {
None
}
}
fn write_project(root: &Path) {
std::fs::create_dir_all(root.join("physics")).unwrap();
std::fs::write(
root.join("marqdo.yaml"),
"project:\n name: demo\n version: 0.1.0\n marqdo: \">=3.0.0-beta.1,<4.0.0\"\ndependencies: {}\n",
)
.unwrap();
std::fs::write(
root.join("physics/mechanics.mq.md"),
"---\ntype: Theory\ntitle: 经典力学\n---\n\n# 经典力学\n\n## @knowledge 牛顿第二定律\n\n合外力等于质量与加速度的乘积。\n\n计算入口:[计算力](#计算力)。\n\n## @exec 计算力\n\n`params(m, a)`\n\n依据[牛顿第二定律](#牛顿第二定律)。\n\n`return m * a`\n",
)
.unwrap();
std::fs::write(
root.join("bad.mq.md"),
"# bad\n\n## @exec broken\n\n`return this is not valid ))`\n",
)
.unwrap();
}
#[test]
fn real_core_cli_emits_a_valid_analysis_artifact() {
let Some(bin) = core_bin() else {
eprintln!("skip: MARQDO_TEST_BIN is not set");
return;
};
let temp = tempfile::tempdir().unwrap();
write_project(temp.path());
let core = CoreCli::locate(Some(&bin)).unwrap();
assert!(core.identity.version.starts_with('3'));
let emitted = core
.emit_analysis(temp.path(), "physics/mechanics.mq.md")
.unwrap();
let artifact = analysis::parse_and_validate(&emitted.bytes).unwrap();
assert_eq!(artifact.schema_version, 3);
assert!(artifact
.modules
.iter()
.any(|module| module.relative_path == "physics/mechanics.mq.md"));
let batch =
analysis::analyze_core(temp.path(), &["physics/mechanics.mq.md".to_string()], &core);
assert!(!batch.diagnostics.has_errors(), "{:?}", batch.diagnostics);
let module = batch
.module_for_path("physics/mechanics.mq.md")
.expect("valid module facts");
assert!(module
.facts
.functions
.iter()
.any(|function| function.local_name == "计算力"));
}
#[test]
fn invalid_marqdo_document_fails_analysis_without_an_artifact() {
let Some(bin) = core_bin() else {
eprintln!("skip: MARQDO_TEST_BIN is not set");
return;
};
let temp = tempfile::tempdir().unwrap();
write_project(temp.path());
let core = CoreCli::locate(Some(&bin)).unwrap();
let batch = analysis::analyze_core(temp.path(), &["bad.mq.md".to_string()], &core);
assert!(batch.diagnostics.has_errors());
assert_eq!(batch.modules.len(), 0);
assert_eq!(batch.failed_entries, vec!["bad.mq.md".to_string()]);
}
#[test]
fn analysis_core_cli_identity_is_recorded() {
let Some(bin) = core_bin() else {
eprintln!("skip: MARQDO_TEST_BIN is not set");
return;
};
let core = CoreCli::locate(Some(&bin)).unwrap();
assert!(core.identity.version.starts_with('3'));
assert!(core.identity.binary_digest.starts_with("sha256:"));
assert_eq!(core.identity.binary_digest.len(), 71);
}
+23
View File
@@ -0,0 +1,23 @@
# Analysis fixture provenance
`mechanics.json` is a real `marqdo.analysis` revision-3 artefact captured from
the upstream release candidate, not a hand-written example. Tests use it as the
positive fixture for the closed-shape and semantic validator and for the
`DocumentFacts` conversion.
- Producer: `marqdo 3.0.0-rc.4` (`4bbefc92449ee480c8a2c89b0308feb4cda8d0e7`),
Linux x86_64.
- Command (captured from the project directory):
```sh
"$MARQDO_BIN" check physics/mechanics.mq.md --emit analysis.json
```
- Source project: a minimal `marqdo.yaml` plus one document with a namespace,
a Knowledge H2, an Exec H2 with parameters, a Markdown link to an anchor, a
formula, a table and a front matter block.
- Fixture SHA-256: see `mechanics.json.sha256`.
- Regenerate with `scripts/regenerate-analysis-fixture.sh SHA256_OF_CORE_BINARY`
and record the new producer version and binary digest if the contract changes.
A revision that cannot be consumed by the current validator must be treated as
a broken producer/consumer pair, never patched into the tests.
+647
View File
@@ -0,0 +1,647 @@
{
"schema": "marqdo.analysis",
"schema_version": 3,
"producer": {
"name": "marqdo",
"version": "3.0.0-rc.4",
"markdown_profile": "marqdo.md.v1"
},
"project": {
"root_id": "root:demo@0.1.0",
"identity_kind": "manifest",
"entry_module_id": "root:demo@0.1.0::physics/mechanics.mq.md",
"lock_digest": "",
"manifest_digest": "sha256:ee4f20fb5156741127f6a4d947b73b9a8f9700a368992974601a958861939a82",
"stdlib": null,
"extensions": []
},
"modules": [
{
"id": "root:demo@0.1.0::physics/mechanics.mq.md",
"package_instance_id": "root:demo@0.1.0",
"relative_path": "physics/mechanics.mq.md",
"source_digest": "27815c2e64ca85343ac4e43213e5c842e458a0dd2ee98e499965fb9da2b37607",
"source": "---\ntype: Theory\ntitle: 经典力学\ntags: [physics, mechanics]\n---\n\n# 经典力学\n\n## @knowledge 牛顿第二定律\n\n合外力等于质量与加速度的乘积。\n\n$$\nF = ma\n$$\n\n| 符号 | 含义 |\n| --- | --- |\n| m | 质量 |\n| a | 加速度 |\n\n计算入口:[计算力](#计算力)。\n\n## @exec 计算力\n\n`params(m, a)`\n\n依据[牛顿第二定律](#牛顿第二定律)。\n\n`return m * a`\n",
"document": {
"children": [
{
"children": [],
"kind": "front_matter",
"literal": "---\ntype: Theory\ntitle: 经典力学\ntags: [physics, mechanics]\n---\n\n",
"span": {
"end": 67,
"start": 0
}
},
{
"children": [
{
"children": [],
"kind": "text",
"literal": "经典力学",
"span": {
"end": 83,
"start": 71
}
}
],
"kind": "heading",
"level": 1,
"setext": false,
"span": {
"end": 83,
"start": 69
}
},
{
"children": [
{
"children": [],
"kind": "text",
"literal": "@knowledge 牛顿第二定律",
"span": {
"end": 117,
"start": 88
}
}
],
"kind": "heading",
"level": 2,
"setext": false,
"span": {
"end": 117,
"start": 85
}
},
{
"children": [
{
"children": [],
"kind": "text",
"literal": "合外力等于质量与加速度的乘积。",
"span": {
"end": 164,
"start": 119
}
}
],
"kind": "paragraph",
"span": {
"end": 164,
"start": 119
}
},
{
"children": [],
"kind": "math_block",
"literal": "F = ma\n",
"span": {
"end": 178,
"start": 166
}
},
{
"alignments": [
"none",
"none"
],
"children": [
{
"children": [
{
"children": [
{
"children": [],
"kind": "text",
"literal": "符号",
"span": {
"end": 188,
"start": 182
}
}
],
"kind": "table_cell",
"span": {
"end": 189,
"start": 181
}
},
{
"children": [
{
"children": [],
"kind": "text",
"literal": "含义",
"span": {
"end": 197,
"start": 191
}
}
],
"kind": "table_cell",
"span": {
"end": 198,
"start": 190
}
}
],
"header": true,
"kind": "table_row",
"span": {
"end": 199,
"start": 180
}
},
{
"children": [
{
"children": [
{
"children": [],
"kind": "text",
"literal": "m",
"span": {
"end": 217,
"start": 216
}
}
],
"kind": "table_cell",
"span": {
"end": 218,
"start": 215
}
},
{
"children": [
{
"children": [],
"kind": "text",
"literal": "质量",
"span": {
"end": 226,
"start": 220
}
}
],
"kind": "table_cell",
"span": {
"end": 227,
"start": 219
}
}
],
"header": false,
"kind": "table_row",
"span": {
"end": 228,
"start": 214
}
},
{
"children": [
{
"children": [
{
"children": [],
"kind": "text",
"literal": "a",
"span": {
"end": 232,
"start": 231
}
}
],
"kind": "table_cell",
"span": {
"end": 233,
"start": 230
}
},
{
"children": [
{
"children": [],
"kind": "text",
"literal": "加速度",
"span": {
"end": 244,
"start": 235
}
}
],
"kind": "table_cell",
"span": {
"end": 245,
"start": 234
}
}
],
"header": false,
"kind": "table_row",
"span": {
"end": 246,
"start": 229
}
}
],
"kind": "table",
"span": {
"end": 246,
"start": 180
}
},
{
"children": [
{
"children": [],
"kind": "text",
"literal": "计算入口:",
"span": {
"end": 263,
"start": 248
}
},
{
"children": [
{
"children": [],
"kind": "text",
"literal": "计算力",
"span": {
"end": 273,
"start": 264
}
}
],
"kind": "link",
"span": {
"end": 286,
"start": 263
},
"title": "",
"url": "#计算力"
},
{
"children": [],
"kind": "text",
"literal": "。",
"span": {
"end": 289,
"start": 286
}
}
],
"kind": "paragraph",
"span": {
"end": 289,
"start": 248
}
},
{
"children": [
{
"children": [],
"kind": "text",
"literal": "@exec 计算力",
"span": {
"end": 309,
"start": 294
}
}
],
"kind": "heading",
"level": 2,
"setext": false,
"span": {
"end": 309,
"start": 291
}
},
{
"children": [
{
"children": [],
"kind": "code",
"literal": "params(m, a)",
"span": {
"end": 325,
"start": 311
},
"ticks": 1
}
],
"kind": "paragraph",
"span": {
"end": 325,
"start": 311
}
},
{
"children": [
{
"children": [],
"kind": "text",
"literal": "依据",
"span": {
"end": 333,
"start": 327
}
},
{
"children": [
{
"children": [],
"kind": "text",
"literal": "牛顿第二定律",
"span": {
"end": 352,
"start": 334
}
}
],
"kind": "link",
"span": {
"end": 374,
"start": 333
},
"title": "",
"url": "#牛顿第二定律"
},
{
"children": [],
"kind": "text",
"literal": "。",
"span": {
"end": 377,
"start": 374
}
}
],
"kind": "paragraph",
"span": {
"end": 377,
"start": 327
}
},
{
"children": [
{
"children": [],
"kind": "code",
"literal": "return m * a",
"span": {
"end": 393,
"start": 379
},
"ticks": 1
}
],
"kind": "paragraph",
"span": {
"end": 393,
"start": 379
}
}
],
"kind": "document",
"span": {
"end": 394,
"start": 0
}
},
"units": [
{
"id": "root:demo@0.1.0::physics/mechanics.mq.md#",
"path": [],
"parent_id": null,
"kind": "root",
"name": "",
"level": 0,
"mode": "knowledge",
"mode_origin": "default",
"mode_source_unit_id": "root:demo@0.1.0::physics/mechanics.mq.md#",
"heading_span": null,
"span": {
"start": 0,
"end": 394
},
"function": null
},
{
"id": "root:demo@0.1.0::physics/mechanics.mq.md#经典力学",
"path": [
"经典力学"
],
"parent_id": "root:demo@0.1.0::physics/mechanics.mq.md#",
"kind": "namespace",
"name": "经典力学",
"level": 1,
"mode": "knowledge",
"mode_origin": "inherited",
"mode_source_unit_id": "root:demo@0.1.0::physics/mechanics.mq.md#",
"heading_span": {
"start": 69,
"end": 83
},
"span": {
"start": 69,
"end": 394
},
"function": null
},
{
"id": "root:demo@0.1.0::physics/mechanics.mq.md#经典力学/牛顿第二定律",
"path": [
"经典力学",
"牛顿第二定律"
],
"parent_id": "root:demo@0.1.0::physics/mechanics.mq.md#经典力学",
"kind": "knowledge",
"name": "牛顿第二定律",
"level": 2,
"mode": "knowledge",
"mode_origin": "explicit",
"mode_source_unit_id": "root:demo@0.1.0::physics/mechanics.mq.md#经典力学/牛顿第二定律",
"heading_span": {
"start": 85,
"end": 117
},
"span": {
"start": 85,
"end": 291
},
"function": null
},
{
"id": "root:demo@0.1.0::physics/mechanics.mq.md#经典力学/计算力",
"path": [
"经典力学",
"计算力"
],
"parent_id": "root:demo@0.1.0::physics/mechanics.mq.md#经典力学",
"kind": "function",
"name": "计算力",
"level": 2,
"mode": "exec",
"mode_origin": "explicit",
"mode_source_unit_id": "root:demo@0.1.0::physics/mechanics.mq.md#经典力学/计算力",
"heading_span": {
"start": 291,
"end": 309
},
"span": {
"start": 291,
"end": 394
},
"function": "root:demo@0.1.0::physics/mechanics.mq.md::经典力学.计算力"
}
],
"anchors": [
{
"anchor": "牛顿第二定律",
"unit_id": "root:demo@0.1.0::physics/mechanics.mq.md#经典力学/牛顿第二定律"
},
{
"anchor": "经典力学",
"unit_id": "root:demo@0.1.0::physics/mechanics.mq.md#经典力学"
},
{
"anchor": "计算力",
"unit_id": "root:demo@0.1.0::physics/mechanics.mq.md#经典力学/计算力"
}
],
"functions": [
{
"id": "root:demo@0.1.0::physics/mechanics.mq.md::经典力学.计算力",
"unit_id": "root:demo@0.1.0::physics/mechanics.mq.md#经典力学/计算力",
"local_name": "计算力",
"namespace": "经典力学",
"span": {
"start": 291,
"end": 393
},
"heading_span": {
"start": 291,
"end": 309
},
"signature": {
"kind": "explicit",
"params": [
{
"name": "m",
"required": true,
"type": null,
"default": null,
"span": {
"start": 315,
"end": 316
}
},
{
"name": "a",
"required": true,
"type": null,
"default": null,
"span": {
"start": 320,
"end": 322
}
}
]
},
"effects": [
"unknown"
],
"body": [
{
"kind": "return",
"span": {
"end": 393,
"start": 379
},
"value": {
"kind": "binary",
"left": {
"kind": "variable",
"name": "m",
"span": {
"end": 388,
"start": 387
}
},
"op": "mul",
"right": {
"kind": "variable",
"name": "a",
"span": {
"end": 392,
"start": 391
}
},
"span": {
"end": 392,
"start": 387
}
}
}
]
}
],
"imports": [],
"references": [
{
"source": "root:demo@0.1.0::physics/mechanics.mq.md",
"source_unit": "root:demo@0.1.0::physics/mechanics.mq.md#经典力学/牛顿第二定律",
"url": "#计算力",
"span": {
"start": 263,
"end": 286
},
"state": "resolved",
"target_kind": "module",
"target_module": "root:demo@0.1.0::physics/mechanics.mq.md",
"target_unit": "root:demo@0.1.0::physics/mechanics.mq.md#经典力学/计算力",
"candidate_package_instance_id": null,
"candidate_path": null,
"fragment": "计算力",
"candidates": []
},
{
"source": "root:demo@0.1.0::physics/mechanics.mq.md",
"source_unit": "root:demo@0.1.0::physics/mechanics.mq.md#经典力学/计算力",
"url": "#牛顿第二定律",
"span": {
"start": 333,
"end": 374
},
"state": "resolved",
"target_kind": "module",
"target_module": "root:demo@0.1.0::physics/mechanics.mq.md",
"target_unit": "root:demo@0.1.0::physics/mechanics.mq.md#经典力学/牛顿第二定律",
"candidate_package_instance_id": null,
"candidate_path": null,
"fragment": "牛顿第二定律",
"candidates": []
}
],
"calls": [],
"resources": [
{
"id": "root:demo@0.1.0::physics/mechanics.mq.md#resource:formula-1",
"module_id": "root:demo@0.1.0::physics/mechanics.mq.md",
"unit_id": "root:demo@0.1.0::physics/mechanics.mq.md#经典力学/牛顿第二定律",
"kind": "formula",
"span": {
"start": 166,
"end": 178
},
"content_digest": "377d9d34e57777c470a2b3e3ddea6839c9d257cafab6df1640b4d9598d840df6",
"language": null,
"display": true
}
]
}
],
"diagnostics": []
}
+1
View File
@@ -0,0 +1 @@
37e8049262f119388d7934190814b117a33820782547fd9ea70f988818ea9577 tests/fixtures/analysis/mechanics.json