Ship oracle journal values, bytecode/frontend hardening, and replay-policy Lean.

Host exposes oracle journals as language values with refuse/copy gates; bytecode and foreign session paths get modularized; applications and verification replay-policy sources land with Lake builds ignored.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-11-06 15:04:27 +08:00
1 parent e3d9021bb6
commit 8f1010f967
101 files changed
+17962 -1727

No files matched your search

+79 -85
View File
@@ -1,107 +1,101 @@
//! Object inheritance: `# Child = > Parent` validation and helpers.
use std::collections::{HashMap, HashSet};
use std::collections::HashSet;
use anyhow::Result;
use crate::ast::{Function, Module};
use crate::diagnostics::{Diagnostic, Span};
use crate::diagnostics::Diagnostic;
/// Import binding paths identify module instances, including repeated imports
/// of the same file under different bindings. The tree remains immutable.
pub(crate) fn module_namespace(root:&Module, wanted:&Module) -> Option<Vec<String>> {
if std::ptr::eq(root,wanted) { return Some(Vec::new()); }
let mut imports=root.import_modules.iter().collect::<Vec<_>>();
imports.sort_by_key(|(name,_)| *name);
for (bind,module) in imports {
if let Some(mut path)=module_namespace(module,wanted) {
path.insert(0,bind.clone()); return Some(path);
}
}
None
}
pub(crate) fn object_at_namespace<'a>(root:&'a Module,path:&[String],name:&str) -> Option<(&'a Module,&'a Function)> {
let mut owner=root;
for segment in path { owner=owner.import_modules.get(segment)?; }
let object=owner.functions.iter().find(|fun|fun.is_object() && fun.name==name)?;
Some((owner,object))
}
/// Bare types prefer their defining module. An imported short-name binding
/// resolves its explicit path; a legacy imported bare name must be unique.
pub(crate) fn resolve_object<'a>(owner:&'a Module,name:&str) -> Result<(&'a Module,&'a Function),String> {
if let Some(alias)=owner.uses.iter().find(|binding|binding.bind==name) {
let Some((class,path))=alias.path.split_last() else { return Err("empty object alias".into()); };
return object_at_namespace(owner,path,class).ok_or_else(||format!("unknown object type `{name}`"));
}
if let Some(object)=owner.functions.iter().find(|fun|fun.is_object() && fun.name==name) { return Ok((owner,object)); }
fn imported<'a>(module:&'a Module,name:&str,out:&mut Vec<(&'a Module,&'a Function)>) {
let mut imports=module.import_modules.iter().collect::<Vec<_>>();imports.sort_by_key(|(name,_)|*name);
for (_,child) in imports {
out.extend(child.functions.iter().filter(|fun|fun.is_object() && fun.name==name).map(|fun|(child,fun)));
imported(child,name,out);
}
}
let mut candidates=Vec::new(); imported(owner,name,&mut candidates);
match candidates.as_slice() {
[only]=>Ok(*only),
[]=>Err(format!("unknown object type `{name}`")),
_=>Err(format!("ambiguous object type `{name}`; use an explicit import binding")),
}
}
/// Validate that every object's `base` resolves and that the inheritance graph is acyclic.
pub fn validate_inheritance(module: &Module) -> Result<()> {
let mut by_name: HashMap<String, (Span, Option<String>)> = HashMap::new();
collect_objects(module, &mut by_name);
// Deterministic order: source line, then name (stable gold diagnostics).
let mut names: Vec<String> = by_name.keys().cloned().collect();
names.sort_by(|a, b| {
let (sa, _) = &by_name[a];
let (sb, _) = &by_name[b];
sa.line.cmp(&sb.line).then_with(|| a.cmp(b))
});
for name in &names {
let (span, base) = &by_name[name];
let Some(base) = base else { continue };
if !by_name.contains_key(base) {
return Err(Diagnostic::new(
None,
*span,
format!("unknown base type `{base}` for object `{name}`"),
)
.into());
}
fn modules<'a>(module: &'a Module, out: &mut Vec<&'a Module>) {
out.push(module);
let mut imports=module.import_modules.iter().collect::<Vec<_>>();
imports.sort_by_key(|(name,_)| *name);
for (_, child) in imports { modules(child,out); }
}
for name in &names {
let (span, base) = &by_name[name];
if base.is_none() {
continue;
}
let mut path = Vec::new();
let mut cur = name.clone();
loop {
if path.iter().any(|p| p == &cur) {
path.push(cur);
return Err(Diagnostic::new(
None,
*span,
format!(
"cyclic inheritance involving `{name}` ({})",
path.join(" → ")
),
)
.into());
let mut all=Vec::new(); modules(module,&mut all);
for owner in all {
for object in owner.functions.iter().filter(|fun|fun.is_object()) {
let mut current_owner=owner;
let mut current=object;
let mut seen=HashSet::new();
let mut chain=Vec::new();
loop {
chain.push(current.name.clone());
if !seen.insert((current_owner as *const Module,current as *const Function)) {
return Err(Diagnostic::new(owner.source.path.as_deref(),object.span,
format!("cyclic inheritance involving `{}` ({})",object.name,chain.join(" → ")))
.with_source(&owner.source).into());
}
let Some(base)=&current.base else { break; };
(current_owner,current)=resolve_object(current_owner,base).map_err(|message| {
Diagnostic::new(current_owner.source.path.as_deref(),current.span,
format!("{} base type `{base}` for object `{}`: {message}",if message.starts_with("ambiguous") { "ambiguous" } else { "unknown" },current.name))
.with_source(&current_owner.source)
})?;
}
path.push(cur.clone());
let Some((_, Some(next))) = by_name.get(&cur) else {
break;
};
cur = next.clone();
}
}
Ok(())
}
fn collect_objects(module: &Module, out: &mut HashMap<String, (Span, Option<String>)>) {
for fun in &module.functions {
if fun.is_object() {
out.entry(fun.name.clone())
.or_insert((fun.span, fun.base.clone()));
}
}
for lib in module.import_modules.values() {
collect_objects(lib, out);
}
}
/// Walk the base chain starting at `type_name` (inclusive).
pub fn inheritance_chain<'a>(module: &'a Module, type_name: &str) -> Vec<&'a Function> {
let mut out = Vec::new();
let mut cur = type_name.to_string();
let mut seen = HashSet::new();
while seen.insert(cur.clone()) {
let Some(obj) = find_object(module, &cur) else {
break;
};
out.push(obj);
match &obj.base {
Some(b) => cur = b.clone(),
None => break,
}
let mut out=Vec::new();
let Ok((mut owner,mut object))=resolve_object(module,type_name) else { return out; };
let mut seen=HashSet::new();
while seen.insert((owner as *const Module,object as *const Function)) {
out.push(object);
let Some(base)=&object.base else { break; };
let Ok(next)=resolve_object(owner,base) else { break; };
(owner,object)=next;
}
out
}
fn find_object<'a>(module: &'a Module, name: &str) -> Option<&'a Function> {
module
.functions
.iter()
.find(|f| f.name == name && f.is_object())
.or_else(|| {
module.import_modules.values().find_map(|lib| {
lib.functions
.iter()
.find(|f| f.name == name && f.is_object())
})
})
}