feat: consume Analysis v3 alongside v2

This commit is contained in:
cflmy committed 2026-11-24 13:08:16 +08:00
1 parent 27532e7b4c
commit 4cf500fb58
6 files changed
+31 -16

No files matched your search

+3 -2
View File
@@ -696,7 +696,7 @@ pub fn verify_command(opts: &RuntimeOpts, args: &VerifyArgs) -> ViewResult<i32>
}, },
"analysis": { "analysis": {
"schema": "marqdo.analysis", "schema": "marqdo.analysis",
"schema_version": tool_v1::ANALYSIS_PROTOCOL, "schema_version": projection.schema_version,
"digest": projection.analysis_digest, "digest": projection.analysis_digest,
"producer_version": projection.producer_version, "producer_version": projection.producer_version,
"markdown_profile": projection.markdown_profile, "markdown_profile": projection.markdown_profile,
@@ -736,7 +736,8 @@ pub fn verify_command(opts: &RuntimeOpts, args: &VerifyArgs) -> ViewResult<i32>
); );
println!("document: {entry}"); println!("document: {entry}");
println!( println!(
"analysis: schema=marqdo.analysis v2 modules={} functions={} digest={}", "analysis: schema=marqdo.analysis v{} modules={} functions={} digest={}",
projection.schema_version,
projection.modules.len(), projection.modules.len(),
projection.functions.len(), projection.functions.len(),
projection.analysis_digest projection.analysis_digest
+21 -9
View File
@@ -73,6 +73,8 @@ pub enum StatementKind {
Branch, Branch,
Loop, Loop,
Return, Return,
Break,
Continue,
Other, Other,
} }
@@ -84,6 +86,8 @@ impl StatementKind {
StatementKind::Branch => "条件分支", StatementKind::Branch => "条件分支",
StatementKind::Loop => "循环", StatementKind::Loop => "循环",
StatementKind::Return => "返回", StatementKind::Return => "返回",
StatementKind::Break => "跳出循环",
StatementKind::Continue => "继续循环",
StatementKind::Other => "语句", StatementKind::Other => "语句",
} }
} }
@@ -95,6 +99,8 @@ impl StatementKind {
StatementKind::Branch => "branch", StatementKind::Branch => "branch",
StatementKind::Loop => "loop", StatementKind::Loop => "loop",
StatementKind::Return => "return", StatementKind::Return => "return",
StatementKind::Break => "break",
StatementKind::Continue => "continue",
StatementKind::Other => "other", StatementKind::Other => "other",
} }
} }
@@ -210,6 +216,7 @@ pub struct ModuleSummary {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct SemanticProjection { pub struct SemanticProjection {
pub schema_version: u32,
pub analysis_sha256: String, pub analysis_sha256: String,
pub analysis_digest: String, pub analysis_digest: String,
pub producer_version: String, pub producer_version: String,
@@ -541,19 +548,18 @@ pub fn verify_analysis(
) -> ViewResult<VerifiedAnalysis> { ) -> ViewResult<VerifiedAnalysis> {
let raw: RawAnalysis = let raw: RawAnalysis =
serde_json::from_slice(analysis_bytes).map_err(|err| ViewError::AnalysisFailed { serde_json::from_slice(analysis_bytes).map_err(|err| ViewError::AnalysisFailed {
detail: format!("Analysis v2 is not valid JSON: {err}"), detail: format!("Analysis is not valid JSON: {err}"),
})?; })?;
if raw.schema != "marqdo.analysis" { if raw.schema != "marqdo.analysis" {
return Err(ViewError::ProtocolIncompatible { return Err(ViewError::ProtocolIncompatible {
detail: format!("unexpected Analysis schema {:?}", raw.schema), detail: format!("unexpected Analysis schema {:?}", raw.schema),
}); });
} }
if raw.schema_version != crate::protocol::tool_v1::ANALYSIS_PROTOCOL { if !crate::protocol::tool_v1::ANALYSIS_PROTOCOLS.contains(&raw.schema_version) {
return Err(ViewError::ProtocolIncompatible { return Err(ViewError::ProtocolIncompatible {
detail: format!( detail: format!(
"Analysis schema_version {} is not supported (View implements v{})", "Analysis schema_version {} is not supported (View implements v2/v3)",
raw.schema_version, raw.schema_version
crate::protocol::tool_v1::ANALYSIS_PROTOCOL
), ),
}); });
} }
@@ -641,7 +647,9 @@ pub fn verify_analysis(
let statements = function let statements = function
.body .body
.iter() .iter()
.filter_map(|statement| convert_statement(statement, &mut valid_span)) .filter_map(|statement| {
convert_statement(statement, raw.schema_version, &mut valid_span)
})
.collect(); .collect();
Some(SemanticFunction { Some(SemanticFunction {
id: function.id.clone(), id: function.id.clone(),
@@ -777,6 +785,7 @@ pub fn verify_analysis(
Ok(VerifiedAnalysis { Ok(VerifiedAnalysis {
projection: SemanticProjection { projection: SemanticProjection {
schema_version: raw.schema_version,
analysis_sha256: sha256_hex(analysis_bytes), analysis_sha256: sha256_hex(analysis_bytes),
analysis_digest: sha256_prefixed(analysis_bytes), analysis_digest: sha256_prefixed(analysis_bytes),
producer_version: raw.producer.version.clone(), producer_version: raw.producer.version.clone(),
@@ -804,6 +813,7 @@ pub fn verify_analysis(
fn convert_statement( fn convert_statement(
raw: &RawStatement, raw: &RawStatement,
schema_version: u32,
valid_span: &mut impl FnMut(Option<SourceSpan>, &str) -> Option<SourceSpan>, valid_span: &mut impl FnMut(Option<SourceSpan>, &str) -> Option<SourceSpan>,
) -> Option<SemanticStatement> { ) -> Option<SemanticStatement> {
let span = valid_span(raw.span, "语句")?; let span = valid_span(raw.span, "语句")?;
@@ -813,12 +823,14 @@ fn convert_statement(
"branch" => StatementKind::Branch, "branch" => StatementKind::Branch,
"loop" => StatementKind::Loop, "loop" => StatementKind::Loop,
"return" => StatementKind::Return, "return" => StatementKind::Return,
"break" if schema_version == 3 => StatementKind::Break,
"continue" if schema_version == 3 => StatementKind::Continue,
_ => StatementKind::Other, _ => StatementKind::Other,
}; };
let body = raw let body = raw
.body .body
.iter() .iter()
.filter_map(|child| convert_statement(child, valid_span)) .filter_map(|child| convert_statement(child, schema_version, valid_span))
.collect(); .collect();
let arms = raw let arms = raw
.arms .arms
@@ -829,7 +841,7 @@ fn convert_statement(
body: arm body: arm
.body .body
.iter() .iter()
.filter_map(|child| convert_statement(child, valid_span)) .filter_map(|child| convert_statement(child, schema_version, valid_span))
.collect(), .collect(),
}) })
.collect(); .collect();
@@ -905,7 +917,7 @@ mod tests {
let index = SourceIndex::new(source); let index = SourceIndex::new(source);
let digest = sha256_hex(source.as_bytes()); let digest = sha256_hex(source.as_bytes());
let err = let err =
verify_analysis(&analysis(3, &digest), source, &index, "index.mq.md").unwrap_err(); verify_analysis(&analysis(4, &digest), source, &index, "index.mq.md").unwrap_err();
assert_eq!(err.code(), "view.protocol_incompatible"); assert_eq!(err.code(), "view.protocol_incompatible");
} }
+4 -2
View File
@@ -15,6 +15,7 @@ pub const MIN_PRODUCT: &str = "3.0.0-beta.1";
pub const TOOL_PROTOCOL: u32 = 1; pub const TOOL_PROTOCOL: u32 = 1;
pub const ANALYSIS_PROTOCOL: u32 = 2; pub const ANALYSIS_PROTOCOL: u32 = 2;
pub const ANALYSIS_PROTOCOLS: [u32; 2] = [2, 3];
pub const TRACE_PROTOCOL: u32 = 2; pub const TRACE_PROTOCOL: u32 = 2;
/// Environment variables injected by CLI Tool Protocol v1. /// Environment variables injected by CLI Tool Protocol v1.
@@ -83,10 +84,10 @@ pub fn negotiate_protocol_env(
} }
if let Some(raw) = analysis_protocol { if let Some(raw) = analysis_protocol {
let value = parse_env_u32(ENV_PROTOCOL_ANALYSIS, raw)?; let value = parse_env_u32(ENV_PROTOCOL_ANALYSIS, raw)?;
if value != ANALYSIS_PROTOCOL { if !ANALYSIS_PROTOCOLS.contains(&value) {
return Err(ViewError::ProtocolIncompatible { return Err(ViewError::ProtocolIncompatible {
detail: format!( detail: format!(
"{ENV_PROTOCOL_ANALYSIS}={value} is not supported (this View requires Analysis v{ANALYSIS_PROTOCOL})" "{ENV_PROTOCOL_ANALYSIS}={value} is not supported (this View implements Analysis v2/v3)"
), ),
}); });
} }
@@ -141,6 +142,7 @@ mod tests {
fn negotiates_known_protocols_only() { fn negotiates_known_protocols_only() {
assert!(negotiate_protocol_env(Some("1"), Some("2"), Some("2")).is_ok()); assert!(negotiate_protocol_env(Some("1"), Some("2"), Some("2")).is_ok());
assert!(negotiate_protocol_env(None, None, None).is_ok()); assert!(negotiate_protocol_env(None, None, None).is_ok());
assert!(negotiate_protocol_env(Some("1"), Some("3"), Some("2")).is_ok());
assert!(negotiate_protocol_env(Some("2"), None, None).is_err()); assert!(negotiate_protocol_env(Some("2"), None, None).is_err());
assert!(negotiate_protocol_env(None, Some("1"), None).is_err()); assert!(negotiate_protocol_env(None, Some("1"), None).is_err());
assert!(negotiate_protocol_env(None, None, Some("3")).is_err()); assert!(negotiate_protocol_env(None, None, Some("3")).is_err());
+1 -1
View File
@@ -288,7 +288,7 @@ fn serve_documents(state: &ServerState, request: Request, query: &str) -> ViewRe
"source_status": model.source_status.data_value(), "source_status": model.source_status.data_value(),
"semantics_usable": model.source_status.semantics_usable(), "semantics_usable": model.source_status.semantics_usable(),
"analysis": semantic.map(|projection| serde_json::json!({ "analysis": semantic.map(|projection| serde_json::json!({
"schema_version": crate::protocol::tool_v1::ANALYSIS_PROTOCOL, "schema_version": projection.schema_version,
"digest": projection.analysis_digest, "digest": projection.analysis_digest,
"producer_version": projection.producer_version, "producer_version": projection.producer_version,
"project_root_id": projection.project_root_id, "project_root_id": projection.project_root_id,
+1 -1
View File
@@ -182,7 +182,7 @@ fn tool_manifest_matches_package_and_protocols() {
"tool.json version must match Cargo package version" "tool.json version must match Cargo package version"
); );
assert_eq!(tool["compatibility"]["marqdo"], ">=3.0.0-beta.1,<4.0.0"); assert_eq!(tool["compatibility"]["marqdo"], ">=3.0.0-beta.1,<4.0.0");
assert_eq!(tool["compatibility"]["analysis"], serde_json::json!([2])); assert_eq!(tool["compatibility"]["analysis"], serde_json::json!([2, 3]));
assert_eq!(tool["compatibility"]["trace"], serde_json::json!([2])); assert_eq!(tool["compatibility"]["trace"], serde_json::json!([2]));
} }
+1 -1
View File
@@ -7,7 +7,7 @@
"entrypoint": "bin/marqdo-view", "entrypoint": "bin/marqdo-view",
"compatibility": { "compatibility": {
"marqdo": ">=3.0.0-beta.1,<4.0.0", "marqdo": ">=3.0.0-beta.1,<4.0.0",
"analysis": [2], "analysis": [2, 3],
"trace": [2] "trace": [2]
} }
} }