Files
marqdo/src/sema/ir.rs
T

2256 lines
90 KiB
Rust
Executable File

//! Versioned semantic artifacts for executable knowledge.
//!
//! This is deliberately a *projection* of the existing syntax AST. It gives
//! every consumer one deterministic place to obtain identity, contracts,
//! effects, provenance and hashes without changing the classic runtime AST.
use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::path::{Path, PathBuf};
use anyhow::{bail, Context, Result};
use serde_json::{json, Value};
use sha2::{Digest as _, Sha256};
use crate::ast::{Arg, CallExpr, Expr, Function, Module, Stmt};
use crate::contract::Ty;
use crate::diagnostics::{Diagnostic, Severity, Span};
use crate::project::ProjectRoot;
use super::bind::{call_display, resolve_for_exec, DynamicReason, ExecTarget};
use super::{CallableKind, ModuleId, ProjectIndex, SymbolDecl, SymbolId, SymbolKind};
pub const SCHEMA_VERSION: u32 = 1;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ArtifactId(pub String);
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Digest(pub String);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LockMode {
Off,
Check,
Strict,
}
#[derive(Debug, Clone)]
pub struct CompileOptions {
pub project_root: PathBuf,
pub entry: Option<PathBuf>,
pub lock_mode: LockMode,
}
impl CompileOptions {
pub fn for_path(path: &Path) -> Result<Self> {
let project = if path.is_dir() {
ProjectRoot::new(path)?
} else {
ProjectRoot::discover(path)?
};
Ok(Self {
project_root: project.path().to_path_buf(),
entry: path.is_file().then(|| path.to_path_buf()),
lock_mode: LockMode::Off,
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct EffectSet(BTreeSet<String>);
impl EffectSet {
pub fn pure() -> Self {
Self(BTreeSet::from(["pure".to_string()]))
}
/// Effect is not yet known (dynamic target, missing metadata, or unresolved name).
/// Must never be treated as `pure` for policy checks (G06).
pub fn unknown() -> Self {
let mut set = Self::default();
set.add("unknown");
set
}
pub fn add(&mut self, effect: impl Into<String>) {
let effect = effect.into();
if effect != "pure" {
self.0.remove("pure");
}
self.0.insert(effect);
}
pub fn extend(&mut self, other: &Self) {
for effect in &other.0 {
self.add(effect.clone());
}
}
pub fn is_pure(&self) -> bool {
self.0.len() == 1 && self.0.contains("pure")
}
pub fn contains(&self, effect: &str) -> bool {
self.0.contains(effect)
}
pub fn values(&self) -> Vec<String> {
self.0.iter().cloned().collect()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SourceLocation {
pub file: String,
pub line: u32,
pub end_line: u32,
}
#[derive(Debug, Clone)]
pub struct CanonicalParam {
pub name: String,
pub ty: String,
pub required: bool,
pub default_digest: Option<Digest>,
pub unit: Option<String>,
}
#[derive(Debug, Clone)]
pub struct CanonicalSignature {
pub receiver: String,
pub params: Vec<CanonicalParam>,
pub returns: String,
pub effects: EffectSet,
}
#[derive(Debug, Clone)]
pub struct ContractSet {
pub params: Vec<String>,
pub returns: Option<String>,
pub fields: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct SourceRef {
pub uri: String,
pub locator: Option<String>,
pub content_digest: Option<Digest>,
pub relation: String,
}
#[derive(Debug, Clone)]
pub struct VerificationRecord {
pub parse: String,
pub resolve: String,
pub contract: String,
pub execution: String,
pub tests: String,
pub evidence: String,
pub domain: String,
pub methods: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct ArtifactHashes {
pub interface: Digest,
pub implementation: Digest,
pub evidence: Digest,
pub artifact: Digest,
/// Transitive dependency closure over call edges (G08).
pub closure: Digest,
}
#[derive(Debug, Clone)]
pub struct SemanticArtifact {
pub artifact_id: ArtifactId,
pub symbol_id: Option<SymbolId>,
pub display_name: String,
pub kind: String,
pub callable: Option<String>,
pub source: SourceLocation,
pub signature: Option<CanonicalSignature>,
pub contract: ContractSet,
pub effects: EffectSet,
pub dependencies: Vec<ArtifactId>,
pub provenance: Vec<SourceRef>,
pub applicability: String,
pub hashes: ArtifactHashes,
pub verification: VerificationRecord,
pub lifecycle: String,
}
#[derive(Debug, Clone)]
pub struct KnowledgeRef {
pub id: String,
/// Stable public address of the marked step, absent for ambiguous labels.
pub anchor_id: Option<String>,
/// Original Markdown target when this reference occurs in prose.
pub link_target: Option<String>,
/// Resolved stable anchor target; never an executable call.
pub target_anchor: Option<String>,
/// Reference to a theorem declaration, not a proof certificate.
pub target_claim: Option<String>,
pub label: String,
pub context: String,
pub source: SourceLocation,
pub resolution: String,
pub target: Option<SymbolId>,
}
/// Stable identity and unproved declaration of a v2.8.0 function theorem.
#[derive(Debug, Clone)]
pub struct KnowledgeClaim {
pub anchor_id: String,
pub owner: SymbolId,
pub label: String,
pub conclusion: String,
pub premise: Option<String>,
pub line: u32,
pub source: SourceLocation,
}
/// Declared operation assumption, not an accepted proof or runtime binding.
#[derive(Debug, Clone)]
pub struct KnowledgeAssumption {
pub id: String,
pub owner: SymbolId,
pub operation: String,
pub premise: Option<String>,
pub postcondition: String,
pub source: SourceLocation,
}
#[derive(Debug, Clone)]
pub struct KnowledgeStep {
pub id: ArtifactId,
/// Stable public anchor; legacy #step-N is kept as an internal step identity.
pub anchor_id: Option<String>,
/// Hash of the authored source line (or fallback label when line unavailable).
pub definition_digest: Digest,
pub reference: String,
pub kind: String,
pub calls: Vec<SymbolId>,
pub effects: EffectSet,
pub source: SourceLocation,
}
/// Fully static i64 call-site argument. Non-constant expressions are kept
/// as unresolved proof obligations rather than guessed values.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StaticCallArg {
pub name: Option<String>,
pub integer: Option<i64>,
pub expression_digest: Digest,
/// Only a direct identifier; arbitrary expressions do not become symbolic identities.
pub symbol: Option<String>,
}
fn checked_constant_int(expr: &Expr, depth: usize) -> Option<i64> {
if depth > 32 { return None; }
match expr {
Expr::Literal(crate::ast::Literal::Int(n)) => Some(*n),
Expr::Occluded { payload: Some(inner), .. } =>
checked_constant_int(inner, depth+1),
Expr::Unary {op: crate::ast::UnaryOp::Neg, expr} =>
checked_constant_int(expr,depth+1)?.checked_neg(),
Expr::Binary {op,left,right} => {
let a=checked_constant_int(left,depth+1)?;
let b=checked_constant_int(right,depth+1)?;
match op {
crate::ast::BinaryOp::Add=>a.checked_add(b),
crate::ast::BinaryOp::Sub=>a.checked_sub(b),
crate::ast::BinaryOp::Mul=>a.checked_mul(b),
// Division and its rounding/coercion are not yet proven
// equivalent to the Lean abstract numeric model.
_=>None
}
},
_=>None
}
}
fn static_call_args(call:&CallExpr)->Vec<StaticCallArg>{
// The exact same alias/modifier expansion used by runtime resolution
// must determine the proof's argument list. An invalid normalization
// must never yield a recoverable proof argument.
let Ok(normalized)=call.for_evaluation() else {return Vec::new()};
normalized.args.iter().map(|a|{
let (name,expr)=match a {
crate::ast::Arg::Positional(expr)=>(None,expr),
crate::ast::Arg::Named{name,value}=>(Some(name.clone()),value)
};
StaticCallArg {
name, integer:checked_constant_int(expr,0),
expression_digest:digest(format!("{expr:?}")),
symbol:match expr {Expr::Var(v)=>Some(v.clone()),_=>None}
}
}).collect()
}
/// Static call site bound to a SymbolId (G02).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BoundCall {
/// Exact owning function symbol at the AST callsite.
pub caller: SymbolId,
pub target: SymbolId,
pub call_site: String,
pub source: SourceLocation,
/// Exact, ordered actual arguments with conservative closed-int decoding.
pub args: Vec<StaticCallArg>,
/// Guard predicates are source-derived and never independently trusted.
pub path_guards: Vec<String>,
/// Loop, unknown guard, etc: no modular proof may pass this callsite.
pub control_blocker: Option<String>,
pub required_effects: EffectSet,
}
/// Call site that must not pretend to be statically bound (G02).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DynamicCall {
pub reason: String,
pub call_site: String,
pub source: SourceLocation,
}
#[derive(Debug, Clone)]
pub struct SemanticProject {
pub schema_version: u32,
pub project_id: String,
pub root: PathBuf,
pub artifacts: Vec<SemanticArtifact>,
pub refs: Vec<KnowledgeRef>,
pub steps: Vec<KnowledgeStep>,
pub claims: Vec<KnowledgeClaim>,
pub assumptions: Vec<KnowledgeAssumption>,
pub bound_calls: Vec<BoundCall>,
pub dynamic_calls: Vec<DynamicCall>,
}
#[derive(Debug, Clone)]
pub struct Compilation {
pub index: ProjectIndex,
pub semantics: SemanticProject,
pub diagnostics: Vec<Diagnostic>,
}
/// Compile every project source once into stable semantic facts. This is
/// side-effect free: it never loads plugins, evaluates expressions or fetches
/// sources.
pub fn compile_project(options: &CompileOptions) -> Result<Compilation> {
let root = ProjectRoot::new(&options.project_root)?;
compile_index(ProjectIndex::build(root)?)
}
/// Compile `entry` and the modules it imports. Sibling files in the directory are not included.
pub fn compile_file_closure(entry: &Path) -> Result<Compilation> {
let files = import_closure(entry)?;
let ancestor = common_ancestor(&files)?;
let root = ProjectRoot::new(&ancestor)?;
compile_index(ProjectIndex::from_files(root, files)?)
}
fn compile_index(index: ProjectIndex) -> Result<Compilation> {
let root = index.root().clone();
// Reuse parsed modules from ProjectIndex — same input snapshot, no second parse (M-R5b).
let mut files = BTreeMap::new();
let mut diagnostics = Vec::new();
for (module_id, parsed) in index.parsed_modules() {
files.insert(
module_id.clone(),
(
parsed.path.clone(),
parsed.text.clone(),
parsed.module.clone(),
),
);
}
// Wire import_modules from already-parsed project files so BoundCall path
// resolve shares `resolve_for_exec` (no plugin load / no re-parse).
attach_project_imports(&mut files);
let project_id = index.project_id().to_string();
let mut artifacts = Vec::new();
let mut refs = Vec::new();
let mut steps = Vec::new();
let mut claims = Vec::new();
let mut assumptions = Vec::new();
let mut bound_calls = Vec::new();
let mut dynamic_calls = Vec::new();
let mut local_by_name: BTreeMap<(ModuleId, String), SymbolId> = BTreeMap::new();
for symbol in index.symbols() {
local_by_name.insert((symbol.module.clone(), symbol.name.clone()), symbol.id.clone());
}
for (module_id, (path, text, module)) in &files {
artifacts.push(module_artifact(&index, module_id, path, text));
let mut by_path = BTreeMap::new();
collect_functions(&module.functions, &mut Vec::new(), &mut by_path);
let narrative_links = crate::parse::scan_narrative_links(text);
let module_prov = super::provenance::parse_module_provenance(text);
let module_symbols = index.symbols_in_module(module_id);
for symbol in index.symbols_in_module(module_id) {
let function = by_path.get(&symbol.declaration_path).expect("symbol collected from module");
let mut claim_counts=BTreeMap::new();
for claim in &function.claims {
*claim_counts.entry(claim.id.as_str()).or_insert(0usize)+=1;
}
for claim in &function.claims {
if claim_counts.get(claim.id.as_str())!=Some(&1) {
diagnostics.push(Diagnostic::new(Some(path.as_path()),
Span::new(claim.line,1),
format!("ambiguous theorem declaration: {}",claim.id))
.with_code("knowledge.claim_duplicate")
.with_severity(Severity::Warning));
continue;
}
claims.push(KnowledgeClaim {
anchor_id: format!("{}#claim-{}",symbol.id.as_str(),
digest(claim.id.as_bytes()).0.trim_start_matches("sha256:")),
owner:symbol.id.clone(),
label:claim.id.clone(),
conclusion:claim.conclusion.clone(),
premise:claim.premise.clone(),
line:claim.line,
source:source_location(index.root().path(),path,claim.line,claim.line),
});
}
let mut assumption_counts=BTreeMap::new();
for assumption in &function.assumptions {
*assumption_counts.entry(assumption.op.as_str()).or_insert(0usize)+=1;
}
for assumption in &function.assumptions {
if assumption_counts.get(assumption.op.as_str())!=Some(&1) {
diagnostics.push(Diagnostic::new(Some(path.as_path()),
Span::new(assumption.line,1),
format!("ambiguous assumption operation: {}",assumption.op))
.with_code("proof.assumption_duplicate")
.with_severity(Severity::Warning));
continue;
}
assumptions.push(KnowledgeAssumption{
id:format!("{}#assumption-{}",symbol.id.as_str(),
digest(assumption.op.as_bytes()).0.trim_start_matches("sha256:")),
owner:symbol.id.clone(),
operation:assumption.op.clone(),
premise:assumption.premise.clone(),
postcondition:assumption.postcondition.clone(),
source:source_location(index.root().path(),path,assumption.line,assumption.line)
});
}
let owned_links: Vec<_> = narrative_links
.iter()
.filter(|link| enclosing_symbol(&module_symbols, link.line).is_some_and(|s| s.id == symbol.id))
.cloned()
.collect();
let (artifact, mut artifact_refs, mut artifact_steps, mut bounds, mut dynamics, prov_diags) =
artifact_for_function(
&index,
module_id,
module,
path,
function,
symbol,
text,
&local_by_name,
&owned_links,
&module_prov,
);
for diag in prov_diags {
diagnostics.push(
Diagnostic::new(Some(path), Span::new(1, 1), diag.message())
.with_code(diag.code())
.with_severity(Severity::Warning),
);
}
refs.append(&mut artifact_refs);
steps.append(&mut artifact_steps);
bound_calls.append(&mut bounds);
dynamic_calls.append(&mut dynamics);
artifacts.push(artifact);
}
}
close_effect_dependencies(&mut artifacts);
resolve_anchor_references(&mut refs, &claims);
for reference in refs.iter().filter(|r|
r.resolution=="unresolved_anchor" || r.resolution=="ambiguous_anchor") {
let source_path=root.path().join(&reference.source.file);
diagnostics.push(Diagnostic::new(
Some(source_path.as_path()),
Span::new(reference.source.line, 1),
format!("knowledge target cannot be uniquely resolved: {}",
reference.link_target.as_deref().unwrap_or("<missing>")))
.with_code(if reference.resolution=="ambiguous_anchor" {
"knowledge.anchor_ambiguous"
} else {"knowledge.anchor_unresolved"})
.with_severity(Severity::Warning));
}
let resolved_effects: BTreeMap<_,_> = artifacts.iter()
.map(|artifact|(artifact.artifact_id.clone(),artifact.effects.clone())).collect();
for bound in &mut bound_calls {
if let Some(effects) = resolved_effects.get(&ArtifactId(bound.target.as_str().into())) {
bound.required_effects = effects.clone();
}
}
let mut callsite_effects: BTreeMap<(String,u32),EffectSet> = BTreeMap::new();
for bound in &bound_calls {
callsite_effects.entry((bound.source.file.clone(),bound.source.line))
.or_default().extend(&bound.required_effects);
}
for step in &mut steps {
if let Some(effects) = callsite_effects.get(&(step.source.file.clone(),step.source.line)) {
step.effects.extend(effects);
}
if step.effects.0.is_empty() { step.effects = EffectSet::pure(); }
}
close_closure_digests(&mut artifacts);
artifacts.sort_by(|a, b| a.artifact_id.0.cmp(&b.artifact_id.0));
refs.sort_by(|a, b| a.id.cmp(&b.id));
steps.sort_by(|a, b| a.id.0.cmp(&b.id.0));
claims.sort_by(|a,b| a.anchor_id.cmp(&b.anchor_id));
assumptions.sort_by(|a,b|a.id.cmp(&b.id));
bound_calls.sort_by(|a, b| {
a.source
.file
.cmp(&b.source.file)
.then(a.source.line.cmp(&b.source.line))
.then(a.call_site.cmp(&b.call_site))
});
dynamic_calls.sort_by(|a, b| {
a.source
.file
.cmp(&b.source.file)
.then(a.source.line.cmp(&b.source.line))
.then(a.call_site.cmp(&b.call_site))
});
Ok(Compilation {
index,
semantics: SemanticProject {
schema_version: SCHEMA_VERSION,
project_id,
root: root.path().to_path_buf(),
artifacts,
refs,
steps,
claims,
assumptions,
bound_calls,
dynamic_calls,
},
diagnostics,
})
}
fn import_closure(entry: &Path) -> Result<Vec<PathBuf>> {
let mut out = Vec::new();
let mut seen = std::collections::HashSet::new();
fn walk(path: &Path, out: &mut Vec<PathBuf>, seen: &mut std::collections::HashSet<PathBuf>) -> Result<()> {
if !path.is_file() {
return Ok(());
}
let canon = path.canonicalize().with_context(|| format!("resolve {}", path.display()))?;
if !seen.insert(canon.clone()) {
return Ok(());
}
out.push(canon.clone());
let source = fs::read_to_string(&canon).with_context(|| format!("read {}", canon.display()))?;
let module = crate::load::parse_source_auto(&source)
.with_context(|| format!("parse {}", canon.display()))?;
let base = canon.parent().unwrap_or_else(|| Path::new("."));
for imp in &module.imports {
let dep = crate::load::resolve_import(base, &imp.path)
.with_context(|| format!("import `{}` from {}", imp.path, canon.display()))?;
walk(&dep, out, seen)?;
}
Ok(())
}
if !entry.is_file() {
bail!("call target {} is not a file", entry.display());
}
walk(entry, &mut out, &mut seen)?;
out.sort();
Ok(out)
}
fn common_ancestor(files: &[PathBuf]) -> Result<PathBuf> {
let Some(first) = files.first() else {
bail!("call target has no source files");
};
let mut ancestor = first
.parent()
.map(|p| p.to_path_buf())
.unwrap_or_else(|| PathBuf::from("."));
for file in files.iter().skip(1) {
while !file.starts_with(&ancestor) {
ancestor = ancestor
.parent()
.map(|p| p.to_path_buf())
.ok_or_else(|| anyhow::anyhow!("source files have no common directory"))?;
}
}
Ok(ancestor)
}
fn module_artifact(index: &ProjectIndex, module: &ModuleId, path: &Path, text: &str) -> SemanticArtifact {
let implementation = digest(normalize_source(text));
let interface = digest(module.as_str());
let evidence = digest("");
let artifact_id = ArtifactId(format!("mq://{}/{}/module", percent(index.project_id()), percent(module.as_str())));
semantic_artifact(
artifact_id,
None,
module.as_str().to_string(),
"module".to_string(),
None,
source_location(index.root().path(), path, 1, text.lines().count() as u32),
None,
ContractSet { params: vec![], returns: None, fields: vec![] },
EffectSet::pure(),
vec![],
vec![SourceRef { uri: rel_uri(index.root().path(), path), locator: None, content_digest: Some(implementation.clone()), relation: "defines".into() }],
"project-local module".into(),
interface,
implementation,
evidence,
)
}
fn path_key(path: &Path) -> PathBuf {
path.canonicalize().unwrap_or_else(|_| path.to_path_buf())
}
/// Clone imported modules already present in `files` into each Module's
/// `import_modules` map (G02 BoundCall for explicit import paths).
fn attach_project_imports(files: &mut BTreeMap<ModuleId, (PathBuf, String, Module)>) {
let by_path: BTreeMap<PathBuf, Module> = files
.values()
.map(|(path, _, module)| (path_key(path), module.clone()))
.collect();
for (path, _, module) in files.values_mut() {
let base = path
.parent()
.map(|p| p.to_path_buf())
.unwrap_or_else(|| path.to_path_buf());
let mut import_modules = std::collections::HashMap::new();
for imp in &module.imports {
let Ok(resolved) = crate::load::resolve_import(&base, &imp.path) else {
continue;
};
let key = path_key(&resolved);
if let Some(dep) = by_path.get(&key) {
import_modules.insert(imp.bind.clone(), dep.clone());
} else if let Ok(dep) = crate::load::load_module(&resolved) {
// External standard libraries are outside a project-only index,
// but their resolved bodies still determine effects.
import_modules.insert(imp.bind.clone(), dep);
}
}
module.import_modules = import_modules;
}
}
fn enclosing_symbol<'a>(symbols: &[&'a SymbolDecl], line: u32) -> Option<&'a SymbolDecl> {
symbols
.iter()
.copied()
.filter(|s| s.line <= line)
.max_by_key(|s| s.line)
}
fn resolve_narrative_target(
index: &ProjectIndex,
module_id: &ModuleId,
local_by_name: &BTreeMap<(ModuleId, String), SymbolId>,
target: &str,
) -> Option<SymbolId> {
let t = target.trim().trim_start_matches('#');
if t.is_empty() {
return None;
}
if let Some(id) = local_by_name.get(&(module_id.clone(), t.to_string())) {
return Some(id.clone());
}
// Path-like `bind.member` via display name suffix match within project.
index.symbols().iter().find_map(|s| {
if s.declaration_path.join(".") == t || s.name == t {
Some(s.id.clone())
} else {
None
}
})
}
/// The label is a human-facing name, not an execution permission.
fn normalize_anchor_label(label: &str) -> String {
label.split_whitespace().collect::<Vec<_>>().join(" ")
}
fn public_anchor(owner: &SymbolId, label: &str, counts: &BTreeMap<String, usize>) -> Option<String> {
let normalized = normalize_anchor_label(label);
if normalized.is_empty() || counts.get(&normalized) != Some(&1) {
return None;
}
// Full SHA-256 avoids truncation collisions; unrelated step insertions
// do not change the address, while changing the label intentionally does.
Some(format!("{}#anchor-{}", owner.as_str(), digest(normalized.as_bytes()).0.trim_start_matches("sha256:")))
}
fn anchor_definition_digest(source: &str, line: u32, label: &str, kind: &str) -> Digest {
let body = if line > 0 { source.lines().nth(line as usize - 1) } else { None };
digest(format!("{kind}\0{label}\0{}", body.unwrap_or("<unknown-source-line>")))
}
/// Explicit knowledge references are inert: resolving a link never invokes a
/// function. Short #@label binds ONLY inside the current function. Full
/// mq://...#anchor-<sha256> targets support cross-module references.
fn resolve_anchor_references(refs: &mut [KnowledgeRef], claims: &[KnowledgeClaim]) {
let declared: BTreeSet<String> = refs.iter()
.filter_map(|r| r.anchor_id.clone()).collect();
let theorem_ids: BTreeSet<String> = claims.iter().map(|c|c.anchor_id.clone()).collect();
for reference in refs {
let Some(raw) = reference.link_target.as_deref() else {continue};
let sought = if let Some(label) = raw.trim().strip_prefix("#@") {
let Some(owner) = reference.id.split("#ref-").next() else {continue};
if label.is_empty() {continue}
Some((format!("{}#anchor-{}", owner,
digest(normalize_anchor_label(label).as_bytes()).0.trim_start_matches("sha256:")),
format!("{}#claim-{}", owner,
digest(label.as_bytes()).0.trim_start_matches("sha256:"))))
} else if raw.starts_with("mq://")
&& (raw.contains("#anchor-") || raw.contains("#claim-")) {
Some((raw.trim().to_string(), raw.trim().to_string()))
} else { None };
if let Some((anchor, claim)) = sought {
let step_match=declared.contains(&anchor);
let theorem_match=theorem_ids.contains(&claim);
match (step_match,theorem_match) {
(true,false)=>{
reference.target_anchor=Some(anchor);
reference.resolution="anchor".into();
}
(false,true)=>{
reference.target_claim=Some(claim);
reference.resolution="claim".into();
}
(true,true)=>reference.resolution="ambiguous_anchor".into(),
_=>reference.resolution="unresolved_anchor".into()
}
reference.target=None;
}
}
}
#[derive(Clone,Default)]
struct CallsiteControl {
guards: Vec<String>,
blocker: Option<String>,
}
/// Only side-effect-free boolean and integer-comparison guards are projected.
/// Unknown guards and any loops remain explicit unresolved obligations.
fn guard_text(expr:&Expr)->Option<String>{
match expr {
Expr::Var(v)=>Some(v.clone()),
Expr::Literal(crate::ast::Literal::Int(n))=>Some(n.to_string()),
Expr::Literal(crate::ast::Literal::Bool(v))=>Some(
if *v {"真"}else{"假"}.into()),
Expr::Occluded {payload:Some(e),..}=>guard_text(e),
Expr::Unary {op:crate::ast::UnaryOp::Not,expr}=>
Some(format!("非 ({})",guard_text(expr)?)),
Expr::Binary {op,left,right}=>{
let symbol=match op {
crate::ast::BinaryOp::Eq=>"=",
crate::ast::BinaryOp::Ne=>"≠",
crate::ast::BinaryOp::Lt=>"<",
crate::ast::BinaryOp::Le=>"≤",
crate::ast::BinaryOp::Gt=>">",
crate::ast::BinaryOp::Ge=>"≥",
crate::ast::BinaryOp::And=>"且",
crate::ast::BinaryOp::Or=>"或",
_=>return None
};
Some(format!("({} {} {})",
guard_text(left)?,symbol,guard_text(right)?))
},
_=>None
}
}
fn callsite_control(fun:&Function)->BTreeMap<u32,CallsiteControl>{
fn walk(items:&[Stmt],conditions:&[String],block:Option<String>,
output:&mut BTreeMap<u32,CallsiteControl>){
for stmt in items{
match stmt {
Stmt::Branch{arms,span}=>{
// Calls within branch test expressions require separate
// evaluation-order reasoning and must not borrow arm facts.
output.entry(span.line).or_insert_with(||CallsiteControl{
guards:conditions.to_vec(),
blocker:Some("call_in_condition_not_modeled".into())
});
let mut negated=Vec::new();
for arm in arms{
let mut path=conditions.to_vec();
path.extend(negated.iter().cloned());
let mut arm_block=block.clone();
if let Some(cond)=&arm.condition{
match guard_text(cond){
Some(guard)=>{
path.push(guard.clone());
negated.push(format!("非 ({guard})"));
},
None=>arm_block=Some("non_pure_branch_guard".into())
}
}
walk(&arm.body,&path,arm_block,output);
}
},
Stmt::While{body,span,..}|Stmt::ForEach{body,span,..}=>{
output.insert(span.line,CallsiteControl{
guards:conditions.to_vec(),
blocker:Some("loop_or_iterator_requires_invariant".into())
});
walk(body,conditions,
Some("loop_or_iterator_requires_invariant".into()),output);
},
_=>{
let line=crate::ast::stmt_line(stmt);
output.insert(line,CallsiteControl{
guards:conditions.to_vec(),blocker:block.clone()
});
}
}
}
}
let mut found=BTreeMap::new();
walk(&fun.body,&[],None,&mut found);
found
}
fn artifact_for_function(
index: &ProjectIndex,
module_id: &ModuleId,
ast_module: &Module,
path: &Path,
function: &Function,
symbol: &SymbolDecl,
source_text: &str,
local_by_name: &BTreeMap<(ModuleId, String), SymbolId>,
narrative_links: &[crate::parse::NarrativeLink],
module_prov: &super::provenance::ModuleProvenance,
) -> (
SemanticArtifact,
Vec<KnowledgeRef>,
Vec<KnowledgeStep>,
Vec<BoundCall>,
Vec<DynamicCall>,
Vec<super::provenance::ProvenanceDiag>,
) {
let calls = collect_call_sites(function);
let controls=callsite_control(function);
let mut effects = direct_effects(index, ast_module, function, &calls);
let mut dependencies = Vec::new();
let mut bound_calls = Vec::new();
let mut dynamic_calls = Vec::new();
for (_, call, line) in &calls {
let source = source_location(index.root().path(), path, *line, *line);
match classify_call(index, module_id, ast_module, function, call, local_by_name) {
CallClass::Bound { target, effects: call_effects } => {
dependencies.push(ArtifactId(target.as_str().to_string()));
bound_calls.push(BoundCall {
caller:symbol.id.clone(),
target,
call_site: call_display(call),
source,
args:static_call_args(call),
path_guards:controls.get(line).map(|c|c.guards.clone()).unwrap_or_default(),
control_blocker:controls.get(line).and_then(|c|c.blocker.clone()),
required_effects: call_effects,
});
}
CallClass::Dynamic { reason } => {
dynamic_calls.push(DynamicCall {
reason: reason.as_str().into(),
call_site: call_display(call),
source,
});
}
CallClass::Other => {}
}
}
let named = collect_named_steps(function);
// Only a unique author-visible label in this function receives a public
// address. Duplicate names are deliberately not resolved by source order.
let mut label_counts = BTreeMap::new();
for site in &named {
*label_counts.entry(normalize_anchor_label(&site.label)).or_insert(0usize) += 1;
}
let mut refs = Vec::new();
let mut steps = Vec::new();
let mut step_n = 0u32;
for named in named.iter() {
step_n += 1;
let target = named.call.as_ref().and_then(|call| {
match classify_call(index, module_id, ast_module, function, call, local_by_name) {
CallClass::Bound { target, .. } => Some(target),
_ => None,
}
});
let ref_id = format!("{}#ref-{}", symbol.id.as_str(), step_n);
let anchor = public_anchor(&symbol.id, &named.label, &label_counts);
let definition_digest = anchor_definition_digest(source_text, named.line, &named.label, &named.kind);
refs.push(KnowledgeRef {
anchor_id: anchor.clone(),
link_target: None,
target_anchor: None,
target_claim: None,
id: ref_id.clone(),
label: named.label.clone(),
context: named.context.clone(),
source: source_location(index.root().path(), path, named.line, named.line),
resolution: target
.as_ref()
.map(|_| "symbol".to_string())
.unwrap_or_else(|| named.resolution.clone()),
target: target.clone(),
});
let mut step_effects = EffectSet::default();
for (_, call, line) in &calls {
if *line == named.line { step_effects.extend(&effects_for_call(ast_module,function,call,Some(index))); }
}
steps.push(KnowledgeStep {
id: ArtifactId(format!("{}#step-{}", symbol.id.as_str(), step_n)),
anchor_id: anchor,
definition_digest,
reference: ref_id,
kind: named.kind.clone(),
calls: target.into_iter().collect(),
effects: step_effects,
source: source_location(index.root().path(), path, named.line, named.line),
});
}
// G03 NarrativeRef: comment-line []() — never BoundCall, never effects.
for link in narrative_links {
step_n += 1;
let target = match link.kind {
crate::parse::NarrativeLinkKind::Narrate => {
resolve_narrative_target(index, module_id, local_by_name, &link.target)
}
crate::parse::NarrativeLinkKind::Navigation => None,
};
let ref_id = format!("{}#ref-{}", symbol.id.as_str(), step_n);
// A narrative link is a reference, not a definition. It never creates
// a public executable anchor of its own.
refs.push(KnowledgeRef {
anchor_id: None,
link_target: Some(link.target.clone()),
target_anchor: None,
target_claim: None,
id: ref_id.clone(),
label: link.label.clone(),
context: "narrative".into(),
source: source_location(index.root().path(), path, link.line, link.line),
resolution: match link.kind {
crate::parse::NarrativeLinkKind::Navigation => "navigation".into(),
crate::parse::NarrativeLinkKind::Narrate if target.is_some() => "symbol".into(),
crate::parse::NarrativeLinkKind::Narrate => "unresolved".into(),
},
target: target.clone(),
});
steps.push(KnowledgeStep {
id: ArtifactId(format!("{}#step-{}", symbol.id.as_str(), step_n)),
anchor_id: None,
definition_digest: anchor_definition_digest(source_text, link.line, &link.label, link.kind.as_str()),
reference: ref_id,
kind: link.kind.as_str().into(),
calls: target.into_iter().collect(),
effects: EffectSet::pure(),
source: source_location(index.root().path(), path, link.line, link.line),
});
}
if effects.0.is_empty() {
effects = EffectSet::pure();
}
let (signature, contract) = signature(function, symbol.callable, &effects);
let interface = digest(signature_json(signature.as_ref()));
let implementation = digest(format!("{:?}", function.body));
let mut provenance = vec![SourceRef {
uri: rel_uri(index.root().path(), path),
locator: Some(format!("L{}", function.span.line)),
content_digest: Some(implementation.clone()),
relation: "defines".into(),
}];
let fn_sources = super::provenance::sources_for_function(module_prov, &function.name);
let mut fn_prov = module_prov.clone();
fn_prov.sources = fn_sources;
let resolved = super::provenance::resolve_provenance_sources(path, &fn_prov, &fn_prov.sources);
provenance.extend(resolved.refs.clone());
// M02: evidence digest includes contract + canonical provenance (URI/locator/content/applicability).
let evidence = digest(format!(
"{}|{}",
contract_summary(&contract),
resolved.evidence_record
));
let kind = match symbol.kind {
SymbolKind::Type => "type",
SymbolKind::Function => "function",
}.to_string();
let mut artifact = semantic_artifact(
ArtifactId(symbol.id.as_str().to_string()),
Some(symbol.id.clone()),
symbol.declaration_path.join("."),
kind,
symbol.callable.map(|kind| kind.as_str().to_string()),
source_location(index.root().path(), path, function.span.line, function.span.line),
signature,
contract,
effects,
dependencies,
provenance,
resolved.applicability,
interface,
implementation,
evidence,
);
artifact.verification.evidence = resolved.evidence_status;
(
artifact,
refs,
steps,
bound_calls,
dynamic_calls,
resolved.diagnostics,
)
}
enum CallClass {
Bound {
target: SymbolId,
effects: EffectSet,
},
Dynamic {
reason: DynamicReason,
},
Other,
}
fn declaration_path_of(module: &Module, target: &Function) -> Option<Vec<String>> {
fn walk(funs: &[Function], target: &Function, prefix: &mut Vec<String>) -> Option<Vec<String>> {
for fun in funs {
prefix.push(fun.name.clone());
if std::ptr::eq(fun, target) {
return Some(prefix.clone());
}
if let Some(found) = walk(&fun.children, target, prefix) {
return Some(found);
}
prefix.pop();
}
None
}
walk(&module.functions, target, &mut Vec::new())
}
fn symbol_for_path(
index: &ProjectIndex,
module_id: &ModuleId,
decl_path: &[String],
) -> Option<SymbolId> {
index
.symbols()
.iter()
.find(|s| &s.module == module_id && s.declaration_path == decl_path)
.map(|s| s.id.clone())
}
fn classify_call(
index: &ProjectIndex,
module_id: &ModuleId,
ast_module: &Module,
current: &Function,
call: &CallExpr,
_local_by_name: &BTreeMap<(ModuleId, String), SymbolId>,
) -> CallClass {
match resolve_for_exec(ast_module, current, call) {
ExecTarget::Local(fun) => {
let Some(decl) = declaration_path_of(ast_module, fun) else {
return CallClass::Other;
};
let Some(target) = symbol_for_path(index, module_id, &decl) else {
return CallClass::Other;
};
CallClass::Bound {
target,
effects: EffectSet::default(),
}
}
ExecTarget::Import { bind, lib, fun, path } => {
let Some(imp) = ast_module.imports.iter().find(|i| i.bind == bind) else {
return CallClass::Other;
};
let base = index
.modules()
.get(module_id)
.and_then(|p| p.parent().map(|d| d.to_path_buf()))
.unwrap_or_else(|| index.root().path().to_path_buf());
let Ok(imported_path) = crate::load::resolve_import(&base, &imp.path) else {
return CallClass::Other;
};
let imported_path = imported_path
.canonicalize()
.unwrap_or(imported_path);
let Some((imported_id, _)) = index.modules().iter().find(|(_, p)| {
p.canonicalize().unwrap_or_else(|_| (*p).clone()) == imported_path
}) else {
return CallClass::Other;
};
// Resolve the *declaration* path in the imported module, not
// the syntactic call path: `rates.lookup()` may refer to a
// nested `# rates / ## lookup` function. Losing the parent
// would silently break transitive proof invalidation.
let decl = if let Some(d) = declaration_path_of(lib, fun) {
d
} else if path.len() >= 2 {
path[1..].to_vec()
} else {
return CallClass::Other;
};
let Some(target) = symbol_for_path(index, imported_id, &decl) else {
return CallClass::Other;
};
CallClass::Bound {
target,
effects: EffectSet::default(),
}
}
ExecTarget::Builtin(_) | ExecTarget::Host(_) => CallClass::Other,
ExecTarget::Dynamic { reason, .. } => CallClass::Dynamic { reason },
ExecTarget::Unresolved { .. } => CallClass::Other,
}
}
#[allow(clippy::too_many_arguments)]
fn semantic_artifact(
artifact_id: ArtifactId, symbol_id: Option<SymbolId>, display_name: String, kind: String,
callable: Option<String>, source: SourceLocation, signature: Option<CanonicalSignature>,
contract: ContractSet, effects: EffectSet, mut dependencies: Vec<ArtifactId>,
provenance: Vec<SourceRef>, applicability: String, interface: Digest, implementation: Digest,
evidence: Digest,
) -> SemanticArtifact {
dependencies.sort_by(|a, b| a.0.cmp(&b.0));
dependencies.dedup();
let artifact = digest(format!("{}:{}:{}", interface.0, implementation.0, evidence.0));
// Placeholder; replaced by `close_closure_digests` after the full graph exists.
let closure = artifact.clone();
SemanticArtifact {
artifact_id, symbol_id, display_name, kind, callable, source, signature, contract,
effects, dependencies, provenance, applicability, hashes: ArtifactHashes {
interface, implementation, evidence, artifact, closure,
},
verification: VerificationRecord {
parse: "passed".into(), resolve: "passed".into(), contract: "unchecked".into(),
execution: "not_run".into(), tests: "unknown".into(), evidence: "unverified".into(),
domain: "unreviewed".into(), methods: vec!["static_project_compile".into()],
},
lifecycle: "draft".into(),
}
}
fn signature(function: &Function, callable: Option<CallableKind>, effects: &EffectSet) -> (Option<CanonicalSignature>, ContractSet) {
if function.is_object() {
let fields = function.contract.as_ref().map(|c| c.fields.iter().map(|f| format!("{}:{}", f.name, ty_name(&f.ty))).collect()).unwrap_or_default();
return (None, ContractSet { params: vec![], returns: None, fields });
}
let contract = function.contract.as_ref();
let params = function.params.iter().map(|param| {
let cp = contract.and_then(|c| c.params.iter().find(|item| item.name == param.name));
CanonicalParam {
name: param.name.clone(), ty: cp.map(|p| ty_name(&p.ty)).unwrap_or_else(|| "any".into()),
required: param.default.is_none(), default_digest: param.default.as_ref().map(|value| digest(format!("{:?}", value))), unit: None,
}
}).collect::<Vec<_>>();
let set = ContractSet {
params: contract.map(|c| c.params.iter().map(|p| format!("{}:{}", p.name, ty_name(&p.ty))).collect()).unwrap_or_default(),
returns: contract.and_then(|c| c.returns.as_ref().map(|r| ty_name(&r.ty))),
fields: contract.map(|c| c.fields.iter().map(|f| format!("{}:{}", f.name, ty_name(&f.ty))).collect()).unwrap_or_default(),
};
let receiver = match callable {
Some(CallableKind::InstanceMethod) => "instance",
Some(CallableKind::AssociatedFunction) => "none",
_ => "none",
}.to_string();
(Some(CanonicalSignature {
receiver, params, returns: set.returns.clone().unwrap_or_else(|| "any".into()), effects: effects.clone(),
}), set)
}
fn ty_name(ty: &Ty) -> String { ty.name() }
fn collect_functions<'a>(functions: &'a [Function], path: &mut Vec<String>, out: &mut BTreeMap<Vec<String>, &'a Function>) {
for function in functions {
path.push(function.name.clone());
out.insert(path.clone(), function);
collect_functions(&function.children, path, out);
path.pop();
}
}
fn collect_call_sites(function: &Function) -> Vec<(usize, CallExpr, u32)> {
let mut calls = Vec::new();
fn expr(value: &Expr, line: u32, calls: &mut Vec<(usize, CallExpr, u32)>) {
match value {
Expr::Call(call) => {
calls.push((calls.len(), call.clone(), line));
for arg in &call.args { match arg { Arg::Positional(value) | Arg::Named { value, .. } => expr(value, line, calls) } }
}
Expr::Unary { expr: value, .. } => expr(value, line, calls),
Expr::Binary { left, right, .. } => { expr(left, line, calls); expr(right, line, calls); }
Expr::List(items) => for item in items { expr(item, line, calls); },
Expr::Map(items) => for (_, item) in items { expr(item, line, calls); },
Expr::Index { base, .. } => expr(base, line, calls),
Expr::Occluded { payload: Some(payload), .. } => expr(payload, line, calls),
_ => {}
}
}
fn statements(items: &[Stmt], calls: &mut Vec<(usize, CallExpr, u32)>) {
for item in items {
match item {
Stmt::Call { call, span } => { calls.push((calls.len(), call.clone(), span.line)); for arg in &call.args { match arg { Arg::Positional(value) | Arg::Named { value, .. } => expr(value, span.line, calls) } } }
Stmt::Assign { value, span, .. } | Stmt::Return { value, span } | Stmt::Expr { value, span } => expr(value, span.line, calls),
Stmt::Branch { arms, .. } => for arm in arms { if let Some(condition) = &arm.condition { expr(condition, 0, calls) }; statements(&arm.body, calls); },
Stmt::While { condition, body, .. } => { expr(condition, 0, calls); statements(body, calls); }
Stmt::ForEach { body, .. } => statements(body, calls),
}
}
}
// Omitted arguments execute these expressions before the body. Leaving
// them out lets the pure-call policy miss both direct and transitive effects.
for param in &function.params {
if let Some(default) = &param.default { expr(default, function.span.line, &mut calls); }
}
statements(&function.body, &mut calls);
calls
}
/// One author-visible knowledge step extracted from occlusion / labeled loop / call stmt.
struct NamedStepSite {
label: String,
kind: String,
context: String,
resolution: String,
line: u32,
call: Option<CallExpr>,
}
fn is_observe_call(call: &CallExpr) -> bool {
matches!(call.callee.as_str(), "access" | "取" | "取值" | "at" | "get")
&& call.path.is_none()
&& call.receiver.is_none()
}
fn classify_payload_kind(payload: &Expr) -> (&'static str, Option<CallExpr>) {
match payload {
Expr::Call(call) if is_observe_call(call) => ("observe", Some(call.clone())),
Expr::Call(call) => ("call", Some(call.clone())),
Expr::Index { .. } => ("observe", None),
Expr::Binary { .. } | Expr::Unary { .. } => ("derive", None),
Expr::Literal(_) | Expr::Var(_) => ("derive", None),
// Nested occlusion is its own step; outer stays a wrapper derive.
Expr::Occluded { .. } => ("derive", None),
_ => ("derive", None),
}
}
fn collect_named_steps(function: &Function) -> Vec<NamedStepSite> {
let mut out = Vec::new();
fn push_occluded(label: &str, payload: Option<&Expr>, line: u32, out: &mut Vec<NamedStepSite>) {
let label = label.trim();
if label.is_empty() {
return;
}
let (kind, call) = match payload {
Some(p) => classify_payload_kind(p),
None => ("derive", None),
};
out.push(NamedStepSite {
label: label.to_string(),
kind: kind.into(),
context: "executable".into(),
resolution: if call.is_some() {
"external_or_dynamic".into()
} else {
"payload".into()
},
line,
call,
});
if let Some(p) = payload {
walk_expr_for_nested(p, line, out);
}
}
fn walk_expr_for_nested(value: &Expr, line: u32, out: &mut Vec<NamedStepSite>) {
match value {
Expr::Occluded { label, payload } => {
// Nested occlusions are separate steps; outer already recorded.
push_occluded(label, payload.as_deref(), line, out);
}
Expr::Call(call) => {
for arg in &call.args {
match arg {
Arg::Positional(value) | Arg::Named { value, .. } => {
walk_expr_for_nested(value, line, out)
}
}
}
}
Expr::Unary { expr: value, .. } => walk_expr_for_nested(value, line, out),
Expr::Binary { left, right, .. } => {
walk_expr_for_nested(left, line, out);
walk_expr_for_nested(right, line, out);
}
Expr::List(items) => {
for item in items {
walk_expr_for_nested(item, line, out);
}
}
Expr::Map(items) => {
for (_, item) in items {
walk_expr_for_nested(item, line, out);
}
}
Expr::Index { base, .. } => walk_expr_for_nested(base, line, out),
_ => {}
}
}
fn walk_expr_top(value: &Expr, line: u32, out: &mut Vec<NamedStepSite>) {
match value {
Expr::Occluded { label, payload } => {
push_occluded(label, payload.as_deref(), line, out);
}
other => walk_expr_for_nested(other, line, out),
}
}
fn statements(items: &[Stmt], out: &mut Vec<NamedStepSite>) {
for item in items {
match item {
Stmt::Call { call, span } => {
out.push(NamedStepSite {
label: call_name(call),
kind: if is_observe_call(call) {
"observe".into()
} else {
"call".into()
},
context: "executable".into(),
resolution: "external_or_dynamic".into(),
line: span.line,
call: Some(call.clone()),
});
for arg in &call.args {
match arg {
Arg::Positional(value) | Arg::Named { value, .. } => {
walk_expr_top(value, span.line, out)
}
}
}
}
Stmt::Assign { value, span, .. }
| Stmt::Return { value, span }
| Stmt::Expr { value, span } => walk_expr_top(value, span.line, out),
Stmt::Branch { arms, .. } => {
for arm in arms {
if let Some(condition) = &arm.condition {
walk_expr_top(condition, 0, out);
}
statements(&arm.body, out);
}
}
Stmt::While { condition, body, span, .. } => {
walk_expr_top(condition, span.line, out);
statements(body, out);
}
Stmt::ForEach {
item,
collection,
label,
body,
span,
} => {
let shown = label
.as_deref()
.map(str::trim)
.filter(|s| !s.is_empty())
.map(|s| s.to_string())
.unwrap_or_else(|| format!("遍历 {collection} 为 {item}"));
out.push(NamedStepSite {
label: shown,
kind: "iterate".into(),
context: "executable".into(),
resolution: "payload".into(),
line: span.line,
call: None,
});
statements(body, out);
}
}
}
}
statements(&function.body, &mut out);
out
}
fn call_name(call: &CallExpr) -> String {
call_display(call)
}
fn effects_for_call(module: &Module, current: &Function, call: &CallExpr, index: Option<&ProjectIndex>) -> EffectSet {
let mut effects = EffectSet::default();
match resolve_for_exec(module, current, call) {
ExecTarget::Host(host) => {
for effect in host.effects() { effects.add(*effect); }
}
ExecTarget::Builtin("print") => effects.add("output.write"),
ExecTarget::Builtin("input") => effects.add("user.input"),
ExecTarget::Builtin(_) => return EffectSet::pure(),
ExecTarget::Local(_) => {},
ExecTarget::Import { lib, fun, .. } => {
if index.is_some_and(|index| lib.source.path.as_ref().is_some_and(|path| index.module_id_for_path(path).is_some())) {
// Project-indexed dependencies close by edges, not shallow AST clones.
return EffectSet::default();
}
return imported_effects(lib, fun, &mut std::collections::HashSet::new());
},
ExecTarget::Dynamic { reason: DynamicReason::Plugin, .. } => effects.add("plugin.call"),
ExecTarget::Dynamic { .. } | ExecTarget::Unresolved { .. } => return EffectSet::unknown(),
}
effects
}
fn imported_effects(
module: &Module, function: &Function,
visiting: &mut std::collections::HashSet<(Option<PathBuf>, u32)>,
) -> EffectSet {
let identity = (module.source.path.clone(), function.span.line);
if !visiting.insert(identity.clone()) { return EffectSet::default(); }
let mut effects = EffectSet::default();
for (_, call, _) in collect_call_sites(function) {
match resolve_for_exec(module, function, &call) {
ExecTarget::Local(fun) => effects.extend(&imported_effects(module, fun, visiting)),
ExecTarget::Import { lib, fun, .. } => effects.extend(&imported_effects(lib, fun, visiting)),
_ => effects.extend(&effects_for_call(module, function, &call, None)),
}
}
visiting.remove(&identity);
effects
}
fn direct_effects(index: &ProjectIndex, module: &Module, current: &Function, calls: &[(usize, CallExpr, u32)]) -> EffectSet {
let mut effects = EffectSet::default();
for (_, call, _) in calls { effects.extend(&effects_for_call(module, current, call, Some(index))); }
effects
}
/// Propagate callee effects along local `dependencies` edges until fixpoint.
/// Empty effect sets become `pure` only after closure (no unknown remains).
///
/// Deterministic SCC closure digests (M02/G08):
/// - Member seed = sorted `(artifact_id, interface, implementation)` within the SCC
/// - External = sorted closures of dependencies outside the SCC
/// - All members of an SCC share one closure digest
fn close_closure_digests(artifacts: &mut [SemanticArtifact]) {
let n = artifacts.len();
if n == 0 {
return;
}
let index: BTreeMap<String, usize> = artifacts
.iter()
.enumerate()
.map(|(i, a)| (a.artifact_id.0.clone(), i))
.collect();
// Tarjan SCC
let mut stack: Vec<usize> = Vec::new();
let mut on_stack = vec![false; n];
let mut indices = vec![-1i32; n];
let mut lowlink = vec![0i32; n];
let mut index_counter = 0i32;
let mut sccs: Vec<Vec<usize>> = Vec::new();
fn strongconnect(
v: usize,
artifacts: &[SemanticArtifact],
index_map: &BTreeMap<String, usize>,
stack: &mut Vec<usize>,
on_stack: &mut [bool],
indices: &mut [i32],
lowlink: &mut [i32],
index_counter: &mut i32,
sccs: &mut Vec<Vec<usize>>,
) {
indices[v] = *index_counter;
lowlink[v] = *index_counter;
*index_counter += 1;
stack.push(v);
on_stack[v] = true;
for dep in &artifacts[v].dependencies {
if let Some(&w) = index_map.get(&dep.0) {
if indices[w] < 0 {
strongconnect(
w,
artifacts,
index_map,
stack,
on_stack,
indices,
lowlink,
index_counter,
sccs,
);
lowlink[v] = lowlink[v].min(lowlink[w]);
} else if on_stack[w] {
lowlink[v] = lowlink[v].min(indices[w]);
}
}
}
if lowlink[v] == indices[v] {
let mut scc = Vec::new();
loop {
let w = stack.pop().expect("tarjan stack");
on_stack[w] = false;
scc.push(w);
if w == v {
break;
}
}
scc.sort_unstable();
sccs.push(scc);
}
}
for v in 0..n {
if indices[v] < 0 {
strongconnect(
v,
artifacts,
&index,
&mut stack,
&mut on_stack,
&mut indices,
&mut lowlink,
&mut index_counter,
&mut sccs,
);
}
}
// sccs are in reverse topological order (callees before callers for DAGs).
let mut scc_of = vec![0usize; n];
for (si, scc) in sccs.iter().enumerate() {
for &v in scc {
scc_of[v] = si;
}
}
let mut closure_done = vec![false; n];
for scc in &sccs {
let mut member_parts = Vec::new();
for &v in scc {
let a = &artifacts[v];
member_parts.push(format!(
"{}|{}|{}",
a.artifact_id.0, a.hashes.interface.0, a.hashes.implementation.0
));
}
member_parts.sort();
member_parts.dedup();
let mut external = Vec::new();
for &v in scc {
for dep in &artifacts[v].dependencies {
if let Some(&w) = index.get(&dep.0) {
if scc_of[w] != scc_of[v] && closure_done[w] {
external.push(artifacts[w].hashes.closure.0.clone());
} else if scc_of[w] != scc_of[v] {
// External not yet closed — use artifact hash as stable seed.
external.push(artifacts[w].hashes.artifact.0.clone());
}
} else {
external.push(dep.0.clone());
}
}
}
external.sort();
external.dedup();
let mut parts = member_parts;
parts.extend(external);
let scc_digest = digest(parts.join("|"));
for &v in scc {
artifacts[v].hashes.closure = scc_digest.clone();
closure_done[v] = true;
}
}
}
fn close_effect_dependencies(artifacts: &mut [SemanticArtifact]) {
let index: BTreeMap<String, usize> = artifacts
.iter()
.enumerate()
.map(|(i, a)| (a.artifact_id.0.clone(), i))
.collect();
for _ in 0..artifacts.len().saturating_add(1) {
let mut changed = false;
let snapshot: Vec<EffectSet> = artifacts.iter().map(|a| a.effects.clone()).collect();
for artifact in artifacts.iter_mut() {
for dep in &artifact.dependencies {
if let Some(&i) = index.get(&dep.0) {
let before = artifact.effects.values();
artifact.effects.extend(&snapshot[i]);
if artifact.effects.values() != before {
changed = true;
}
}
}
}
if !changed {
break;
}
}
for artifact in artifacts.iter_mut() {
if artifact.effects.contains("unknown")
&& artifact
.effects
.values()
.iter()
.any(|e| e != "unknown" && e != "pure")
{
// Concrete effects from BoundCall closure supersede unknown.
let mut cleaned = EffectSet::default();
for e in artifact.effects.values() {
if e != "unknown" {
cleaned.add(e);
}
}
artifact.effects = cleaned;
}
if artifact.effects.values().is_empty() {
artifact.effects = EffectSet::pure();
}
if let Some(sig) = artifact.signature.as_mut() {
sig.effects = artifact.effects.clone();
}
}
}
fn source_location(root: &Path, path: &Path, line: u32, end_line: u32) -> SourceLocation {
SourceLocation { file: rel_uri(root, path), line, end_line }
}
fn rel_uri(root: &Path, path: &Path) -> String { path.strip_prefix(root).unwrap_or(path).to_string_lossy().replace('\\', "/") }
fn digest(value: impl AsRef<[u8]>) -> Digest { let mut hash = Sha256::new(); hash.update(value.as_ref()); Digest(format!("sha256:{:x}", hash.finalize())) }
fn normalize_source(source: &str) -> String { source.lines().map(str::trim).filter(|line| !line.is_empty()).collect::<Vec<_>>().join("\n") }
fn percent(value: &str) -> String { value.bytes().map(|byte| match byte { b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' => (byte as char).to_string(), _ => format!("%{byte:02X}") }).collect() }
fn contract_summary(contract: &ContractSet) -> String { format!("{}|{:?}|{}", contract.params.join(","), contract.returns, contract.fields.join(",")) }
fn signature_json(signature: Option<&CanonicalSignature>) -> String {
match signature { Some(signature) => format!("{}|{}|{}|{}", signature.receiver, signature.params.iter().map(|p| format!("{}:{}:{}", p.name, p.ty, p.required)).collect::<Vec<_>>().join(","), signature.returns, signature.effects.values().join(",")), None => "type".into() }
}
impl SemanticProject {
pub fn to_json(&self) -> Value {
json!({
"schema_version": self.schema_version, "project": {"id": self.project_id, "root": self.root},
"artifacts": self.artifacts.iter().map(artifact_json).collect::<Vec<_>>(),
"refs": self.refs.iter().map(ref_json).collect::<Vec<_>>(),
"steps": self.steps.iter().map(step_json).collect::<Vec<_>>(),
"claims": self.claims.iter().map(claim_json).collect::<Vec<_>>(),
"assumptions": self.assumptions.iter().map(assumption_json).collect::<Vec<_>>(),
"bound_calls": self.bound_calls.iter().map(bound_call_json).collect::<Vec<_>>(),
"dynamic_calls": self.dynamic_calls.iter().map(dynamic_call_json).collect::<Vec<_>>(),
})
}
}
pub fn artifact_json(artifact: &SemanticArtifact) -> Value {
json!({"schema_version": SCHEMA_VERSION, "artifact_id": artifact.artifact_id.0, "symbol_id": artifact.symbol_id.as_ref().map(SymbolId::as_str), "display_name": artifact.display_name, "kind": artifact.kind, "callable": artifact.callable, "source": {"file": artifact.source.file, "line": artifact.source.line, "end_line": artifact.source.end_line}, "signature": artifact.signature.as_ref().map(|s| json!({"receiver":s.receiver,"params":s.params.iter().map(|p|json!({"name":p.name,"type":p.ty,"required":p.required,"default_digest":p.default_digest.as_ref().map(|d|&d.0),"unit":p.unit})).collect::<Vec<_>>(),"returns":s.returns,"effects":s.effects.values()})), "contract":{"params":artifact.contract.params,"returns":artifact.contract.returns,"fields":artifact.contract.fields}, "effects":artifact.effects.values(), "dependencies":artifact.dependencies.iter().map(|d|&d.0).collect::<Vec<_>>(), "provenance":artifact.provenance.iter().map(|p|json!({"uri":p.uri,"locator":p.locator,"content_digest":p.content_digest.as_ref().map(|d|&d.0),"relation":p.relation})).collect::<Vec<_>>(), "applicability":artifact.applicability, "hashes":{"interface":artifact.hashes.interface.0,"implementation":artifact.hashes.implementation.0,"evidence":artifact.hashes.evidence.0,"artifact":artifact.hashes.artifact.0,"closure":artifact.hashes.closure.0}, "verification":{"parse":artifact.verification.parse,"resolve":artifact.verification.resolve,"contract":artifact.verification.contract,"execution":artifact.verification.execution,"tests":artifact.verification.tests,"evidence":artifact.verification.evidence,"domain":artifact.verification.domain,"methods":artifact.verification.methods}, "lifecycle":artifact.lifecycle})
}
fn ref_json(reference: &KnowledgeRef) -> Value { json!({"id":reference.id,"anchor_id":reference.anchor_id,"link_target":reference.link_target,"target_anchor":reference.target_anchor,"target_claim":reference.target_claim,"label":reference.label,"context":reference.context,"source":{"file":reference.source.file,"line":reference.source.line,"end_line":reference.source.end_line},"resolution":reference.resolution,"target":reference.target.as_ref().map(SymbolId::as_str)}) }
fn assumption_json(a:&KnowledgeAssumption)->Value{
json!({"id":a.id,"owner":a.owner.as_str(),"operation":a.operation,
"premise":a.premise,"postcondition":a.postcondition,
"verification":"declared_not_proved",
"source":{"file":a.source.file,"line":a.source.line}})
}
fn claim_json(claim: &KnowledgeClaim) -> Value {
json!({"anchor_id":claim.anchor_id,"owner":claim.owner.as_str(),
"label":claim.label,"conclusion":claim.conclusion,"premise":claim.premise,
"verification":"declared_unverified",
"source":{"file":claim.source.file,"line":claim.line}})
}
fn step_json(step: &KnowledgeStep) -> Value { json!({"id":step.id.0,"anchor_id":step.anchor_id,"definition_digest":step.definition_digest.0,"reference":step.reference,"kind":step.kind,"calls":step.calls.iter().map(SymbolId::as_str).collect::<Vec<_>>(),"effects":step.effects.values(),"source":{"file":step.source.file,"line":step.source.line,"end_line":step.source.end_line}}) }
fn bound_call_json(call: &BoundCall) -> Value {
json!({
"caller":call.caller.as_str(),
"target": call.target.as_str(),
"call_site": call.call_site,
"args":call.args.iter().map(|a|json!({
"name":a.name,"integer":a.integer,"symbol":a.symbol,
"expression_digest":a.expression_digest.0
})).collect::<Vec<_>>(),
"path_guards":call.path_guards,
"control_blocker":call.control_blocker,
"source": {"file": call.source.file, "line": call.source.line, "end_line": call.source.end_line},
"required_effects": call.required_effects.values(),
"binding": "BoundCall",
})
}
fn dynamic_call_json(call: &DynamicCall) -> Value {
json!({
"reason": call.reason,
"call_site": call.call_site,
"source": {"file": call.source.file, "line": call.source.line, "end_line": call.source.end_line},
"binding": "DynamicCall",
})
}
const SEMANTIC_NAMES: &[&str] = &[
"project.json",
"symbols.json",
"refs.json",
"steps.json",
"anchors.json",
"claims.json",
"assumptions.json",
"proof-index.json",
"proof-obligations.json",
"bound_calls.json",
"dynamic_calls.json",
"artifacts",
"graph",
"verification",
];
/// Resolve the active semantic generation directory (G04).
/// Prefers `CURRENT` → `generations/<id>/`; falls back to `out_dir` itself.
pub fn semantic_generation_dir(out_dir: &Path) -> PathBuf {
let current = out_dir.join("CURRENT");
if let Ok(id) = fs::read_to_string(&current) {
let id = id.trim();
if !id.is_empty() {
let gen = out_dir.join("generations").join(id);
if gen.join("project.json").is_file() {
return gen;
}
}
}
out_dir.to_path_buf()
}
/// Atomically write the semantic portion of `.marqdo/` as a whole generation
/// (G04). Readers that open via [`semantic_generation_dir`] never see a mix.
pub fn write_semantic_projections(out_dir: &Path, compilation: &Compilation) -> Result<()> {
fs::create_dir_all(out_dir).with_context(|| format!("create {}", out_dir.display()))?;
let build_id = {
let mut hasher = Sha256::new();
hasher.update(compilation.semantics.project_id.as_bytes());
for a in &compilation.semantics.artifacts {
hasher.update(a.hashes.artifact.0.as_bytes());
}
hasher.update(
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_nanos()
.to_le_bytes(),
);
format!("{:x}", hasher.finalize())[..16].to_string()
};
let gens = out_dir.join("generations");
fs::create_dir_all(&gens)?;
let gen_dir = gens.join(&build_id);
if gen_dir.exists() {
fs::remove_dir_all(&gen_dir)?;
}
fs::create_dir_all(gen_dir.join("artifacts"))?;
fs::create_dir_all(gen_dir.join("graph"))?;
fs::create_dir_all(gen_dir.join("verification"))?;
write_generation_tree(&gen_dir, compilation, &out_dir.join("prove"))?;
// Publish journal + CAS (M-R6): record expected CURRENT before switching.
let expected = fs::read_to_string(out_dir.join("CURRENT"))
.ok()
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty());
let source_digest = {
let mut h = Sha256::new();
for a in &compilation.semantics.artifacts {
h.update(a.hashes.artifact.0.as_bytes());
}
format!("sha256:{:x}", h.finalize())
};
// Staging already written under gen_dir; journal tracks CURRENT switch.
let mut journal = crate::runtime::PublishJournal::begin(
out_dir,
&compilation.semantics.project_id,
expected,
&build_id,
&source_digest,
)
.map_err(|e| anyhow::anyhow!("publish journal: {e}"))?;
// Point journal staging at the generation we just wrote (already complete).
journal.staging_dir = gen_dir.clone();
journal.generation_dir = gen_dir.clone();
journal
.mark(out_dir, crate::runtime::publish::PublishPhase::Validated)
.map_err(|e| anyhow::anyhow!("publish journal mark: {e}"))?;
// Atomic generation pointer: only publish CURRENT after the tree is complete.
let current_tmp = out_dir.join("CURRENT.tmp");
fs::write(&current_tmp, format!("{build_id}\n"))
.with_context(|| format!("write {}", current_tmp.display()))?;
fs::rename(&current_tmp, out_dir.join("CURRENT"))
.with_context(|| format!("publish CURRENT under {}", out_dir.display()))?;
journal
.mark(out_dir, crate::runtime::publish::PublishPhase::CurrentSwitched)
.map_err(|e| anyhow::anyhow!("publish journal current: {e}"))?;
let _ = fs::remove_file(out_dir.join("publish_journal.json"));
// Compatibility root: stage a full mirror, then swap names (best-effort for
// legacy readers that do not follow CURRENT).
let stage = out_dir.join(".tmp-sema-publish");
if stage.exists() {
fs::remove_dir_all(&stage)?;
}
copy_dir_recursive(&gen_dir, &stage)?;
for name in SEMANTIC_NAMES {
let target = out_dir.join(name);
if target.exists() {
if target.is_dir() {
fs::remove_dir_all(&target)?;
} else {
fs::remove_file(&target)?;
}
}
fs::rename(stage.join(name), &target)?;
}
let _ = fs::remove_dir_all(&stage);
Ok(())
}
fn write_generation_tree(stage: &Path, compilation: &Compilation, prove_root: &Path) -> Result<()> {
let project = compilation.semantics.to_json();
write_json(
&stage.join("project.json"),
&json!({
"schema_version": SCHEMA_VERSION,
"project": project["project"],
"artifact_count": compilation.semantics.artifacts.len(),
"generation": stage.file_name().and_then(|s| s.to_str()),
}),
)?;
write_json(&stage.join("symbols.json"), &json!({"schema_version":SCHEMA_VERSION,"symbols":compilation.index.symbols().iter().map(|s|json!({"id":s.id.as_str(),"module":s.module.as_str(),"kind":s.kind.as_str(),"callable":s.callable.map(|c|c.as_str()),"name":s.name,"declaration_path":s.declaration_path,"source":{"file":rel_uri(compilation.index.root().path(),&s.source),"line":s.line}})).collect::<Vec<_>>() }))?;
write_json(&stage.join("refs.json"), &json!({"schema_version":SCHEMA_VERSION,"refs":project["refs"]}))?;
write_json(&stage.join("steps.json"), &json!({"schema_version":SCHEMA_VERSION,"steps":project["steps"]}))?;
write_json(&stage.join("claims.json"), &json!({
"schema_version":SCHEMA_VERSION,
"role":"theorem_declarations_not_proof_results",
"claims":project["claims"]
}))?;
let proofs=crate::knowledge::proof::index_links(prove_root, &compilation.semantics);
write_json(&stage.join("assumptions.json"), &json!({
"schema_version":SCHEMA_VERSION,
"role":"declared_modular_contracts_require_callsite_proof",
"assumptions":project["assumptions"]
}))?;
write_json(&stage.join("proof-index.json"), &proofs)?;
write_json(&stage.join("proof-obligations.json"),
&crate::knowledge::modular::analyze(&compilation.semantics, &proofs))?;
write_json(&stage.join("anchors.json"), &json!({
"schema_version":SCHEMA_VERSION,
"role":"stable_public_knowledge_addresses",
"anchors": compilation.semantics.steps.iter()
.filter(|step| step.anchor_id.is_some())
.map(|step| json!({
"anchor_id":step.anchor_id,
"owner":step.id.0.split('#').next(),
"label":compilation.semantics.refs.iter()
.find(|r| r.id == step.reference).map(|r| r.label.as_str()),
"step_id":step.id.0,
"kind":step.kind,
"source":step_json(step)["source"],
"definition_digest":step.definition_digest.0,
"effects":step.effects.values(),
})).collect::<Vec<_>>()
}))?;
write_json(&stage.join("bound_calls.json"), &json!({"schema_version":SCHEMA_VERSION,"bound_calls":project["bound_calls"]}))?;
write_json(&stage.join("dynamic_calls.json"), &json!({"schema_version":SCHEMA_VERSION,"dynamic_calls":project["dynamic_calls"]}))?;
write_json(&stage.join("graph/nodes.json"), &json!({"schema_version":SCHEMA_VERSION,"nodes":project["artifacts"]}))?;
let mut edges: Vec<Value> = compilation
.semantics
.artifacts
.iter()
.flat_map(|a| {
a.dependencies.iter().map(move |d| {
json!({"from": a.artifact_id.0, "to": d.0, "kind": "calls"})
})
})
.collect();
for assumption in &compilation.semantics.assumptions {
edges.push(json!({"from":assumption.owner.as_str(),
"to":assumption.id,"kind":"declares_assumption"}));
}
for claim in &compilation.semantics.claims {
edges.push(json!({"from":claim.owner.as_str(),
"to":claim.anchor_id,"kind":"declares_claim"}));
}
for reference in &compilation.semantics.refs {
if let Some(claim) = &reference.target_claim {
let owner=reference.id.split("#ref-").next().unwrap_or("");
edges.push(json!({"from":owner,"to":claim,"kind":"references_claim"}));
}
if let Some(target) = &reference.target_anchor {
let owner = reference.id.split("#ref-").next().unwrap_or("");
edges.push(json!({"from":owner,"to":target,"kind":"references_anchor"}));
}
}
for step in &compilation.semantics.steps {
if let Some(anchor) = &step.anchor_id {
if let Some(owner) = step.id.0.split('#').next() {
edges.push(json!({"from":owner,"to":anchor,"kind":"defines_anchor"}));
}
for target in &step.calls {
edges.push(json!({"from":anchor,"to":target.as_str(),"kind":"calls"}));
}
}
if step.kind == "narrate" {
for target in &step.calls {
edges.push(json!({
"from": step.id.0,
"to": target.as_str(),
"kind": "narrates",
}));
}
}
}
write_json(
&stage.join("graph/edges.json"),
&json!({
"schema_version": SCHEMA_VERSION,
"authority": "SemanticProject",
"edges": edges,
}),
)?;
for artifact in &compilation.semantics.artifacts {
let name = hex_name(&artifact.artifact_id.0);
write_json(
&stage.join("artifacts").join(format!("{name}.json")),
&artifact_json(artifact),
)?;
write_json(
&stage.join("verification").join(format!("{name}.json")),
&artifact_json(artifact)["verification"],
)?;
}
Ok(())
}
fn copy_dir_recursive(src: &Path, dst: &Path) -> Result<()> {
fs::create_dir_all(dst)?;
for ent in fs::read_dir(src).with_context(|| format!("read {}", src.display()))? {
let ent = ent?;
let from = ent.path();
let to = dst.join(ent.file_name());
if from.is_dir() {
copy_dir_recursive(&from, &to)?;
} else {
fs::copy(&from, &to)
.with_context(|| format!("copy {} → {}", from.display(), to.display()))?;
}
}
Ok(())
}
fn write_json(path: &Path, value: &Value) -> Result<()> { fs::write(path, serde_json::to_vec_pretty(value)?).with_context(|| format!("write {}", path.display())) }
fn hex_name(value: &str) -> String { digest(value).0.trim_start_matches("sha256:").to_string() }
#[cfg(test)]
mod tests {
use super::*;
use std::time::{SystemTime, UNIX_EPOCH};
fn root() -> PathBuf { let id = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_nanos(); let root = std::env::temp_dir().join(format!("marqdo-sema-ir-{id}")); fs::create_dir_all(&root).unwrap(); root }
#[test]
fn compilation_is_deterministic_and_preserves_contract_effects() {
let root = root();
fs::write(root.join("main.mq.md"), "---\nimport:\n fs: lib/fs.mq.md\n---\n# main\n\n| 参数 | 类型 |\n| --- | --- |\n| path | text |\n\n## save\n\n> fs.write_text `path` ok\n\n*1*").unwrap();
let options = CompileOptions::for_path(&root).unwrap();
let first = compile_project(&options).unwrap();
let second = compile_project(&options).unwrap();
assert_eq!(first.semantics.to_json(), second.semantics.to_json());
let save = first.semantics.artifacts.iter().find(|a| a.display_name.ends_with("save")).unwrap();
assert!(save.effects.values().contains(&"fs.write".to_string()));
assert_eq!(save.hashes.implementation.0, first.semantics.artifacts.iter().find(|a| a.display_name.ends_with("save")).unwrap().hashes.implementation.0);
fs::remove_dir_all(root).unwrap();
}
#[test]
fn semantic_files_have_schema_versions() {
let root = root(); fs::write(root.join("a.mq.md"), "# a\n\n*1*").unwrap();
let out = root.join(".marqdo"); let compilation = compile_project(&CompileOptions::for_path(&root).unwrap()).unwrap();
write_semantic_projections(&out, &compilation).unwrap();
let project: Value = serde_json::from_str(&fs::read_to_string(out.join("project.json")).unwrap()).unwrap();
assert_eq!(project["schema_version"], SCHEMA_VERSION);
assert!(out.join("artifacts").read_dir().unwrap().next().is_some());
fs::remove_dir_all(root).unwrap();
}
#[test]
fn unknown_call_is_not_pure_and_wrapper_inherits_fs_write() {
let root = root();
fs::write(
root.join("main.mq.md"),
r#"---
markup: occlusion
import:
fs: lib/fs.mq.md
---
## save
> fs.write_text path=out.txt text=ok
*1*
## wrap
> save
*None*
## mystery
> weird_plugin_thing x=1
*None*
# main
> wrap
> print text=ok
"#,
)
.unwrap();
let compilation = compile_project(&CompileOptions::for_path(&root).unwrap()).unwrap();
let wrap = compilation
.semantics
.artifacts
.iter()
.find(|a| a.display_name.ends_with("wrap"))
.unwrap();
assert!(
wrap.effects.values().contains(&"fs.write".to_string()),
"wrapper must inherit fs.write, got {:?}",
wrap.effects.values()
);
assert!(!wrap.effects.is_pure());
let mystery = compilation
.semantics
.artifacts
.iter()
.find(|a| a.display_name.ends_with("mystery"))
.unwrap();
// bare local unresolved name → empty then closed; without a symbol it stays
// as empty→pure OR if treated as call site with no resolve, empty→pure.
// Path-like / unknown plugin style: use a dotted name.
let _ = mystery;
fs::write(
root.join("dyn.mq.md"),
"# dyn\n\n## go\n\n> plugin.do_stuff x=1\n\n*None*\n",
)
.unwrap();
let dyn_comp = compile_project(&CompileOptions::for_path(&root).unwrap()).unwrap();
let go = dyn_comp
.semantics
.artifacts
.iter()
.find(|a| a.display_name.ends_with("go"))
.unwrap();
assert!(
go.effects.contains("plugin.call") || go.effects.contains("unknown"),
"plugin/dynamic must not be pure: {:?}",
go.effects.values()
);
assert!(!go.effects.is_pure());
fs::remove_dir_all(root).unwrap();
}
#[test]
fn public_anchors_survive_unrelated_insertions_and_track_definition_changes() {
let root = root();
let file = root.join("main.mq.md");
let source = |prefix: &str, amount: i32| format!(
"# main\n\n## calculate\n\n{prefix}**x = [计算税额]({amount} * 2)**\n*x*\n"
);
fs::write(&file, source("", 3)).unwrap();
let one = compile_project(&CompileOptions::for_path(&root).unwrap()).unwrap();
let anchor1 = one.semantics.steps.iter().find(|s| s.anchor_id.is_some()).unwrap();
let identity = anchor1.anchor_id.clone();
let digest1 = anchor1.definition_digest.clone();
fs::write(&file, source("**other = [准备参数](1)**\n", 3)).unwrap();
let two = compile_project(&CompileOptions::for_path(&root).unwrap()).unwrap();
let anchor2 = two.semantics.steps.iter()
.find(|s| s.anchor_id == identity).unwrap();
assert_eq!(anchor1.anchor_id, anchor2.anchor_id);
assert_eq!(digest1, anchor2.definition_digest);
assert_ne!(anchor1.id, anchor2.id, "legacy ordinal step id may move");
fs::write(&file, source("**other = [准备参数](1)**\n", 4)).unwrap();
let three = compile_project(&CompileOptions::for_path(&root).unwrap()).unwrap();
let anchor3 = three.semantics.steps.iter()
.find(|s| s.anchor_id == identity).unwrap();
assert_ne!(digest1, anchor3.definition_digest);
fs::remove_dir_all(root).unwrap();
}
#[test]
fn markdown_knowledge_links_bind_only_to_registered_anchors() {
let root=root();
fs::write(root.join("main.mq.md"),
"# main\n\n## f\n\n[说明](#@演算)\n[不存在](#@没有)\n**x = [演算](2 * 3)**\n*x*\n"
).unwrap();
let c=compile_project(&CompileOptions::for_path(&root).unwrap()).unwrap();
let target=c.semantics.steps.iter().find_map(|s|s.anchor_id.as_deref()).unwrap();
let link=c.semantics.refs.iter().find(|r|r.label=="说明").unwrap();
assert_eq!(link.target_anchor.as_deref(),Some(target));
assert_eq!(link.resolution,"anchor");
assert!(link.target.is_none(),"narrative links must never become executable calls");
let bad=c.semantics.refs.iter().find(|r|r.label=="不存在").unwrap();
assert_eq!(bad.resolution,"unresolved_anchor");
assert!(bad.target_anchor.is_none());
fs::remove_dir_all(root).unwrap();
}
#[test]
fn canonical_mq_uri_resolves_across_functions_without_calling_knowledge() {
let root=root();
let src=root.join("main.mq.md");
fs::write(&src,"# main\n\n## source\n\n**x = [演算](2 * 3)**\n*x*\n").unwrap();
let initial=compile_project(&CompileOptions::for_path(&root).unwrap()).unwrap();
let anchor=initial.semantics.steps.iter().find_map(|s|s.anchor_id.clone()).unwrap();
let updated=format!(
"# main\n\n## source\n\n**x = [演算](2 * 3)**\n*x*\n\n## consumer\n\n[阅读计算知识]({anchor})\n*None*\n"
);
fs::write(&src,updated).unwrap();
let parsed=compile_project(&CompileOptions::for_path(&root).unwrap()).unwrap();
let read=parsed.semantics.refs.iter().find(|r|r.label=="阅读计算知识").unwrap();
assert_eq!(read.target_anchor.as_deref(),Some(anchor.as_str()));
assert_eq!(read.resolution,"anchor");
assert!(read.target.is_none());
assert!(parsed.semantics.bound_calls.iter().all(|c|c.call_site!="阅读计算知识"));
fs::remove_dir_all(root).unwrap();
}
#[test]
fn theorem_claims_are_addressable_but_never_implicitly_proved() {
let root=root();
fs::write(root.join("main.mq.md"),
"# main\n\n## f\n\n| 定理 | 结论 | 前提 | 策略 |\n|---|---|---|---|\n| 安全性质 | 长度(轨迹) = 0 | | auto |\n\n[阅读定理](#@安全性质)\n**x = [演算](2 * 3)**\n*x*\n").unwrap();
let c=compile_project(&CompileOptions::for_path(&root).unwrap()).unwrap();
assert_eq!(c.semantics.claims.len(),1);
let claim=&c.semantics.claims[0];
let link=c.semantics.refs.iter().find(|r|r.label=="阅读定理").unwrap();
assert_eq!(link.target_claim.as_deref(),Some(claim.anchor_id.as_str()));
assert_eq!(link.resolution,"claim");
assert!(link.target.is_none());
let serialized=c.semantics.to_json();
assert_eq!(serialized["claims"][0]["verification"],"declared_unverified");
fs::remove_dir_all(root).unwrap();
}
#[test]
fn duplicate_labels_fail_closed_in_public_anchor_projection() {
let root = root();
fs::write(root.join("main.mq.md"),
"# main\n\n## f\n\n**x = [重复](1)**\n**y = [重复](2)**\n*y*\n").unwrap();
let compilation = compile_project(&CompileOptions::for_path(&root).unwrap()).unwrap();
let duplicated: Vec<_> = compilation.semantics.refs.iter()
.filter(|r| r.label == "重复").collect();
assert_eq!(duplicated.len(), 2);
assert!(duplicated.iter().all(|r| r.anchor_id.is_none()));
let out = root.join(".marqdo");
write_semantic_projections(&out, &compilation).unwrap();
let anchors: Value = serde_json::from_str(
&fs::read_to_string(out.join("anchors.json")).unwrap()).unwrap();
assert!(anchors["anchors"].as_array().unwrap().is_empty());
fs::remove_dir_all(root).unwrap();
}
#[test]
fn named_steps_classify_observe_derive_iterate_call() {
let root = root();
fs::write(
root.join("main.mq.md"),
r#"---
markup: occlusion
import:
json: lib/json.mq.md
---
## 加倍
根据输入`n`返回两倍。
*[两倍](n * 2)*
# main
`列` = > json.parse text=["一","二"]
**a = [取表项](access(`列`, 1))**
**b = [两件小计](2 * 30)**
**c = [调用加倍](加倍(n=3))**
- [篮子里的每个果子](遍历 `列` 为 `果`)
> print text=`果`
> print text=`c`
"#,
)
.unwrap();
let compilation = compile_project(&CompileOptions::for_path(&root).unwrap()).unwrap();
let main_steps: Vec<&KnowledgeStep> = compilation
.semantics
.steps
.iter()
.filter(|s| s.id.0.contains("main"))
.collect();
let kinds: Vec<&str> = main_steps.iter().map(|s| s.kind.as_str()).collect();
assert!(
kinds.contains(&"observe"),
"expected observe step, got {kinds:?}"
);
assert!(
kinds.contains(&"derive"),
"expected derive step, got {kinds:?}"
);
assert!(
kinds.contains(&"iterate"),
"expected iterate step, got {kinds:?}"
);
assert!(
kinds.iter().any(|k| *k == "call"),
"expected call step, got {kinds:?}"
);
assert!(
!kinds.iter().all(|k| *k == "call"),
"steps must not all be forged as call: {kinds:?}"
);
let labels: Vec<String> = main_steps
.iter()
.filter_map(|s| {
compilation
.semantics
.refs
.iter()
.find(|r| r.id == s.reference)
.map(|r| r.label.clone())
})
.collect();
assert!(labels.iter().any(|l| l.contains("取表项")), "{labels:?}");
assert!(labels.iter().any(|l| l.contains("两件小计")), "{labels:?}");
assert!(labels.iter().any(|l| l.contains("篮子")), "{labels:?}");
assert!(labels.iter().any(|l| l.contains("调用加倍")), "{labels:?}");
fs::remove_dir_all(root).unwrap();
}
}