feat: pure extract, bookstore story subset, and project scan caps
Add artifact.extract_pure for restricted expression lifting, a runnable bookstore tax story fixture, and G12 discovery limits so oversized or deep trees fail with explicit diagnostics instead of hanging. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
dcd3d329d4
commit
8a6d09d582
10 files changed
+339
-11
No files matched your search
+26
-1
@@ -65,17 +65,42 @@ fn dispatch(req: &Value) -> Result<Value, Value> {
|
||||
Some("artifact.bind") => artifact_bind(¶ms),
|
||||
Some("artifact.execute") => artifact_execute(¶ms),
|
||||
Some("artifact.patch") => artifact_patch(¶ms),
|
||||
Some("artifact.extract_pure") => artifact_extract_pure(¶ms),
|
||||
// `<TAB>` M9: occlusion label search (two-tier vocabulary).
|
||||
Some("labels.search") => labels_search(¶ms),
|
||||
// `<TAB>` M6: one-shot session summary (T1 staleness seen/fixed).
|
||||
Some("session.summary") => session_summary(),
|
||||
other => Err(err(
|
||||
"mlsp.unknown_method",
|
||||
format!("未知方法 {other:?};可用:locate / syntax / validate / repair_targets / repair_apply / schema / artifact.find / artifact.inspect / link.resolve / artifact.verify / artifact.impact / artifact.bind / artifact.execute / artifact.patch"),
|
||||
format!("未知方法 {other:?};可用:locate / syntax / validate / repair_targets / repair_apply / schema / artifact.find / artifact.inspect / link.resolve / artifact.verify / artifact.impact / artifact.bind / artifact.execute / artifact.patch / artifact.extract_pure"),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn artifact_extract_pure(params: &Value) -> Result<Value, Value> {
|
||||
let name = params
|
||||
.get("name")
|
||||
.and_then(Value::as_str)
|
||||
.ok_or_else(|| err("mlsp.bad_request", "artifact.extract_pure 需要 name"))?;
|
||||
let expression = params
|
||||
.get("expression")
|
||||
.and_then(Value::as_str)
|
||||
.ok_or_else(|| err("mlsp.bad_request", "artifact.extract_pure 需要 expression"))?;
|
||||
match crate::sema::plan_pure_extract(name, expression) {
|
||||
Ok(plan) => Ok(json!({
|
||||
"schema_version": 1,
|
||||
"name": plan.name,
|
||||
"params": plan.params,
|
||||
"expression": plan.expression,
|
||||
"heading": plan.heading,
|
||||
"call": plan.call,
|
||||
"effects": ["pure"],
|
||||
"note": "预览补丁;写入请配合 artifact.patch / 人工审阅",
|
||||
})),
|
||||
Err(e) => Err(json!({"code": e.code, "message": e.message})),
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- Executable-knowledge artifact protocol ----------
|
||||
|
||||
/// All artifact methods compile the same semantic project as the CLI. They
|
||||
|
||||
+52
-4
@@ -81,7 +81,22 @@ const EXCLUDED_DIRS: &[&str] = &[
|
||||
".agents",
|
||||
];
|
||||
|
||||
/// Soft limits so discovery fails with a clear diagnosis instead of hanging (G12).
|
||||
pub const MAX_SOURCE_DEPTH: u32 = 32;
|
||||
pub const MAX_SOURCE_FILE_BYTES: u64 = 2 * 1024 * 1024;
|
||||
pub const MAX_SOURCE_FILES: usize = 10_000;
|
||||
|
||||
fn collect_sources(root: &Path, dir: &Path, out: &mut Vec<PathBuf>) -> Result<()> {
|
||||
collect_sources_at(root, dir, out, 0)
|
||||
}
|
||||
|
||||
fn collect_sources_at(root: &Path, dir: &Path, out: &mut Vec<PathBuf>, depth: u32) -> Result<()> {
|
||||
if depth > MAX_SOURCE_DEPTH {
|
||||
bail!(
|
||||
"project.scan_depth_exceeded: directory depth > {MAX_SOURCE_DEPTH} under {}",
|
||||
dir.display()
|
||||
);
|
||||
}
|
||||
let mut entries: Vec<_> = fs::read_dir(dir)
|
||||
.with_context(|| format!("read project directory {}", dir.display()))?
|
||||
.collect::<std::result::Result<Vec<_>, _>>()?;
|
||||
@@ -100,9 +115,9 @@ fn collect_sources(root: &Path, dir: &Path, out: &mut Vec<PathBuf>) -> Result<()
|
||||
continue;
|
||||
}
|
||||
if canonical.is_dir() {
|
||||
collect_sources(root, &canonical, out)?;
|
||||
collect_sources_at(root, &canonical, out, depth + 1)?;
|
||||
} else if is_mq_md(&canonical) {
|
||||
out.push(canonical);
|
||||
push_source(out, &canonical)?;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -111,14 +126,33 @@ fn collect_sources(root: &Path, dir: &Path, out: &mut Vec<PathBuf>) -> Result<()
|
||||
if EXCLUDED_DIRS.iter().any(|excluded| name == *excluded) {
|
||||
continue;
|
||||
}
|
||||
collect_sources(root, &path, out)?;
|
||||
collect_sources_at(root, &path, out, depth + 1)?;
|
||||
} else if file_type.is_file() && is_mq_md(&path) {
|
||||
out.push(path);
|
||||
push_source(out, &path)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn push_source(out: &mut Vec<PathBuf>, path: &Path) -> Result<()> {
|
||||
if out.len() >= MAX_SOURCE_FILES {
|
||||
bail!(
|
||||
"project.scan_count_exceeded: more than {MAX_SOURCE_FILES} `.mq.md` sources"
|
||||
);
|
||||
}
|
||||
let meta = fs::metadata(path)
|
||||
.with_context(|| format!("stat {}", path.display()))?;
|
||||
if meta.len() > MAX_SOURCE_FILE_BYTES {
|
||||
bail!(
|
||||
"project.scan_file_too_large: {} is {} bytes (max {MAX_SOURCE_FILE_BYTES})",
|
||||
path.display(),
|
||||
meta.len()
|
||||
);
|
||||
}
|
||||
out.push(path.to_path_buf());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn is_mq_md(path: &Path) -> bool {
|
||||
path.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
@@ -173,6 +207,20 @@ mod tests {
|
||||
fs::remove_dir_all(root).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oversized_source_is_diagnosed() {
|
||||
let root = fixture_root("too-large");
|
||||
let big = "x".repeat((MAX_SOURCE_FILE_BYTES as usize) + 8);
|
||||
fs::write(root.join("big.mq.md"), format!("# main\n\n{big}\n")).unwrap();
|
||||
let project = ProjectRoot::new(&root).unwrap();
|
||||
let err = project.source_files().unwrap_err().to_string();
|
||||
assert!(
|
||||
err.contains("project.scan_file_too_large"),
|
||||
"expected size diagnostic, got {err}"
|
||||
);
|
||||
fs::remove_dir_all(root).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nearest_marker_wins() {
|
||||
let root = fixture_root("discover");
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
//! Restricted pure-expression extract (S3).
|
||||
//!
|
||||
//! Only Literal / Var / Unary / Binary / List / Map / Index payloads may be
|
||||
//! lifted. Calls, host, and plugins are refused so effect counts stay zero.
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use crate::ast::Expr;
|
||||
use crate::parse::expr::parse_expr_prefer_var;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ExtractPlan {
|
||||
pub name: String,
|
||||
pub params: Vec<String>,
|
||||
pub expression: String,
|
||||
pub heading: String,
|
||||
pub call: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ExtractError {
|
||||
pub code: &'static str,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
/// Build an extract plan from a pure expression string (no file I/O).
|
||||
pub fn plan_pure_extract(name: &str, expression: &str) -> Result<ExtractPlan, ExtractError> {
|
||||
let name = name.trim();
|
||||
if name.is_empty()
|
||||
|| name.contains(|c: char| c.is_whitespace() || c == '.' || c == '/')
|
||||
{
|
||||
return Err(ExtractError {
|
||||
code: "extract.bad_name",
|
||||
message: "提取目标名必须是单个标识符".into(),
|
||||
});
|
||||
}
|
||||
let expr = parse_expr_prefer_var(expression).map_err(|e| ExtractError {
|
||||
code: "extract.parse",
|
||||
message: format!("无法解析表达式: {e}"),
|
||||
})?;
|
||||
if !is_pure_payload(&expr) {
|
||||
return Err(ExtractError {
|
||||
code: "extract.not_pure",
|
||||
message: "仅允许字面量/变量/算术/列表等纯载荷;拒绝调用与宿主效应".into(),
|
||||
});
|
||||
}
|
||||
let params: Vec<String> = free_vars(&expr).into_iter().collect();
|
||||
let call = if params.is_empty() {
|
||||
format!("{name}()")
|
||||
} else {
|
||||
let args = params
|
||||
.iter()
|
||||
.map(|p| format!("{p}=`{p}`"))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
format!("{name}({args})")
|
||||
};
|
||||
let heading = if params.is_empty() {
|
||||
format!("## {name}\n\n*{expression}*\n")
|
||||
} else {
|
||||
let decls = params
|
||||
.iter()
|
||||
.map(|p| format!("`{p}`"))
|
||||
.collect::<Vec<_>>()
|
||||
.join("、");
|
||||
format!("## {name}\n\n参数:{decls}\n\n*{expression}*\n")
|
||||
};
|
||||
Ok(ExtractPlan {
|
||||
name: name.into(),
|
||||
params,
|
||||
expression: expression.trim().into(),
|
||||
heading,
|
||||
call,
|
||||
})
|
||||
}
|
||||
|
||||
fn is_pure_payload(expr: &Expr) -> bool {
|
||||
match expr {
|
||||
Expr::Literal(_) | Expr::Var(_) => true,
|
||||
Expr::Unary { expr, .. } => is_pure_payload(expr),
|
||||
Expr::Binary { left, right, .. } => is_pure_payload(left) && is_pure_payload(right),
|
||||
Expr::List(items) => items.iter().all(is_pure_payload),
|
||||
Expr::Map(items) => items.iter().all(|(_, v)| is_pure_payload(v)),
|
||||
Expr::Index { base, .. } => is_pure_payload(base),
|
||||
Expr::Occluded { payload, .. } => payload
|
||||
.as_ref()
|
||||
.map(|p| is_pure_payload(p))
|
||||
.unwrap_or(true),
|
||||
Expr::Call(_) | Expr::Interp(_) | Expr::Formula(_) | Expr::Code(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn free_vars(expr: &Expr) -> BTreeSet<String> {
|
||||
let mut out = BTreeSet::new();
|
||||
walk(expr, &mut out);
|
||||
out
|
||||
}
|
||||
|
||||
fn walk(expr: &Expr, out: &mut BTreeSet<String>) {
|
||||
match expr {
|
||||
Expr::Var(name) => {
|
||||
out.insert(name.clone());
|
||||
}
|
||||
Expr::Unary { expr, .. } => walk(expr, out),
|
||||
Expr::Binary { left, right, .. } => {
|
||||
walk(left, out);
|
||||
walk(right, out);
|
||||
}
|
||||
Expr::List(items) => {
|
||||
for item in items {
|
||||
walk(item, out);
|
||||
}
|
||||
}
|
||||
Expr::Map(items) => {
|
||||
for (_, item) in items {
|
||||
walk(item, out);
|
||||
}
|
||||
}
|
||||
Expr::Index { base, .. } => walk(base, out),
|
||||
Expr::Occluded { payload, .. } => {
|
||||
if let Some(p) = payload {
|
||||
walk(p, out);
|
||||
}
|
||||
}
|
||||
Expr::Literal(_)
|
||||
| Expr::Call(_)
|
||||
| Expr::Interp(_)
|
||||
| Expr::Formula(_)
|
||||
| Expr::Code(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn extracts_product() {
|
||||
let plan = plan_pure_extract("商品小计", "数量 * 单价").unwrap();
|
||||
assert_eq!(plan.params, vec!["单价".to_string(), "数量".to_string()]);
|
||||
assert!(plan.heading.contains("## 商品小计"));
|
||||
assert!(plan.call.contains("商品小计("));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_call() {
|
||||
let err = plan_pure_extract("x", "save()").unwrap_err();
|
||||
assert_eq!(err.code, "extract.not_pure");
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
//! the meaning of the same source text.
|
||||
|
||||
pub mod bind;
|
||||
pub mod extract;
|
||||
pub mod ir;
|
||||
pub mod lock;
|
||||
pub mod provenance;
|
||||
@@ -13,6 +14,7 @@ pub mod symbols;
|
||||
pub use bind::{
|
||||
call_display, is_static_bound, lookup_local, resolve_for_exec, DynamicReason, ExecTarget,
|
||||
};
|
||||
pub use extract::{plan_pure_extract, ExtractError, ExtractPlan};
|
||||
pub use ir::{
|
||||
artifact_json, compile_project, semantic_generation_dir, write_semantic_projections, ArtifactId,
|
||||
BoundCall, CompileOptions, Compilation, Digest, DynamicCall, EffectSet, LockMode,
|
||||
|
||||
Reference in new issue
Block a user