analysis: publish one schema for every project
This commit is contained in:
1 parent
8bd33a4615
commit
8bfe0c36ee
19 files changed
+93
-500
No files matched your search
@@ -1,4 +1,4 @@
|
||||
//! Public Analysis projection (spec/analysis-v2.md and spec/analysis-v3.md).
|
||||
//! Public Analysis projection (spec/analysis.md).
|
||||
//!
|
||||
//! This is deliberately not a serde dump of `marqdo-ir`: the schema and node
|
||||
//! kinds are the public contract, while internal Rust types may change. The
|
||||
@@ -17,24 +17,13 @@ use crate::loader::LoadedProject;
|
||||
|
||||
/// Schema name and version.
|
||||
pub const SCHEMA: &str = "marqdo.analysis";
|
||||
pub const BASE_VERSION: u32 = 2;
|
||||
/// First schema version with structured loop transfers.
|
||||
pub const LOOP_CONTROL_VERSION: u32 = 3;
|
||||
pub const SCHEMA_VERSION: u32 = 3;
|
||||
|
||||
/// Build the deterministic public projection.
|
||||
pub fn build(project: &LoadedProject) -> Value {
|
||||
let mut modules = Vec::new();
|
||||
let mut version = BASE_VERSION;
|
||||
for (module_id, file) in &project.files {
|
||||
let module = &file.analysis.module;
|
||||
if version == BASE_VERSION
|
||||
&& module
|
||||
.functions
|
||||
.iter()
|
||||
.any(|function| has_loop_control(&function.body))
|
||||
{
|
||||
version = LOOP_CONTROL_VERSION;
|
||||
}
|
||||
modules.push(json!({
|
||||
"id": module_id.to_string(),
|
||||
"package_instance_id": module_id.instance,
|
||||
@@ -60,7 +49,7 @@ pub fn build(project: &LoadedProject) -> Value {
|
||||
});
|
||||
json!({
|
||||
"schema": SCHEMA,
|
||||
"schema_version": version,
|
||||
"schema_version": SCHEMA_VERSION,
|
||||
"producer": {
|
||||
"name": "marqdo",
|
||||
"version": env!("CARGO_PKG_VERSION"),
|
||||
@@ -81,15 +70,6 @@ pub fn build(project: &LoadedProject) -> Value {
|
||||
})
|
||||
}
|
||||
|
||||
fn has_loop_control(body: &[Stmt]) -> bool {
|
||||
body.iter().any(|stmt| match stmt {
|
||||
Stmt::Break { .. } | Stmt::Continue { .. } => true,
|
||||
Stmt::Branch { arms, .. } => arms.iter().any(|arm| has_loop_control(&arm.body)),
|
||||
Stmt::Loop { body, .. } => has_loop_control(body),
|
||||
Stmt::Assign { .. } | Stmt::Eval { .. } | Stmt::Return { .. } => false,
|
||||
})
|
||||
}
|
||||
|
||||
/// Encode exactly the bytes written by `check --emit`.
|
||||
fn encode(project: &LoadedProject) -> Result<Vec<u8>, String> {
|
||||
serde_json::to_vec_pretty(&build(project))
|
||||
|
||||
@@ -314,7 +314,7 @@ struct CheckArgs {
|
||||
/// Declare a plugin manifest for static export-signature checking.
|
||||
#[arg(long = "plugin", value_name = "PATH")]
|
||||
plugin: Vec<PathBuf>,
|
||||
/// Write public Analysis JSON, using v3 for projects with loop transfers.
|
||||
/// Write the current public Analysis JSON for the checked project.
|
||||
#[arg(long, value_name = "PATH")]
|
||||
emit: Option<PathBuf>,
|
||||
/// Machine readable JSON output.
|
||||
|
||||
@@ -31,10 +31,7 @@ struct SupportedProtocols {
|
||||
|
||||
const SUPPORTED: SupportedProtocols = SupportedProtocols {
|
||||
tool: PROTOCOL_VERSION,
|
||||
analysis: &[
|
||||
crate::analysis::BASE_VERSION,
|
||||
crate::analysis::LOOP_CONTROL_VERSION,
|
||||
],
|
||||
analysis: &[crate::analysis::SCHEMA_VERSION],
|
||||
trace: &[2],
|
||||
};
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! Public Analysis/Trace v1 black-box contract (spec/analysis-v1.md,
|
||||
//! Public Analysis and Trace black-box contracts (spec/analysis.md,
|
||||
//! spec/trace-v1.md).
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
@@ -96,7 +96,7 @@ fn analysis_is_deterministic_and_has_stable_semantic_nodes() {
|
||||
|
||||
let analysis: serde_json::Value = serde_json::from_slice(&first_bytes).unwrap();
|
||||
assert_eq!(analysis["schema"], "marqdo.analysis");
|
||||
assert_eq!(analysis["schema_version"], 2);
|
||||
assert_eq!(analysis["schema_version"], 3);
|
||||
let module = &analysis["modules"][0];
|
||||
assert_eq!(module["id"], "root:default::index.mq.md");
|
||||
assert_eq!(module["package_instance_id"], "root:default");
|
||||
@@ -140,7 +140,7 @@ fn analysis_is_deterministic_and_has_stable_semantic_nodes() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn analysis_v2_identity_is_location_independent() {
|
||||
fn analysis_identity_is_location_independent() {
|
||||
let first = TempDir::new("analysis-identity-a");
|
||||
let second = TempDir::new("analysis-identity-b");
|
||||
let source = "## @exec main\n\n`x = 1`\n\n`return x`\n";
|
||||
@@ -182,7 +182,7 @@ fn analysis_v2_identity_is_location_independent() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn analysis_v2_projects_memory_place_targets_additively() {
|
||||
fn analysis_projects_memory_place_targets_additively() {
|
||||
let dir = TempDir::new("analysis-place");
|
||||
let source =
|
||||
"## @exec main\n\n`内存 = memory.new(1, 2)`\n\n`内存[0] = 7`\n\n`return 内存[0]`\n";
|
||||
@@ -211,7 +211,7 @@ fn analysis_v2_projects_memory_place_targets_additively() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn analysis_v2_projects_addressable_formula_and_code_resources() {
|
||||
fn analysis_projects_addressable_formula_and_code_resources() {
|
||||
let dir = TempDir::new("analysis-resources");
|
||||
let source = r#"## @exec calc
|
||||
|
||||
@@ -395,7 +395,7 @@ fn trace_records_calls_effects_and_status_without_secrets() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multi_arm_branch_preserves_analysis_v2_and_carrier_positions() {
|
||||
fn multi_arm_branch_preserves_analysis_and_carrier_positions() {
|
||||
let dir = TempDir::new("else-if");
|
||||
let source = r#"## @exec 分类
|
||||
|
||||
@@ -427,7 +427,7 @@ fn multi_arm_branch_preserves_analysis_v2_and_carrier_positions() {
|
||||
assert!(output.status.success(), "{output:?}");
|
||||
let analysis: serde_json::Value =
|
||||
serde_json::from_slice(&std::fs::read(dir.path().join("analysis.json")).unwrap()).unwrap();
|
||||
assert_eq!(analysis["schema_version"], 2);
|
||||
assert_eq!(analysis["schema_version"], 3);
|
||||
assert_eq!(analysis["producer"]["markdown_profile"], "marqdo.md.v1");
|
||||
let branch = &analysis["modules"][0]["functions"][0]["body"][0];
|
||||
assert_eq!(branch["kind"], "branch");
|
||||
@@ -463,7 +463,7 @@ fn multi_arm_branch_preserves_analysis_v2_and_carrier_positions() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_imported_loop_transfer_selects_v3_and_keeps_the_else_if_call_edge() {
|
||||
fn an_imported_loop_transfer_uses_current_schema_and_keeps_the_else_if_call_edge() {
|
||||
let dir = TempDir::new("imported-loop");
|
||||
write(
|
||||
dir.path(),
|
||||
|
||||
@@ -133,7 +133,7 @@ fn unknown_tool_name_installs_and_dispatches() {
|
||||
|
||||
#[test]
|
||||
fn public_schemas_are_valid_json_and_versioned() {
|
||||
for name in ["analysis-v1", "trace-v1", "tool-v1", "execution-events-v1"] {
|
||||
for name in ["analysis", "trace-v1", "tool-v1", "execution-events-v1"] {
|
||||
let path = repo()
|
||||
.join("spec/schemas")
|
||||
.join(format!("{name}.schema.json"));
|
||||
|
||||
Reference in new issue
Block a user