fix: GAP-06–10 parser, invoke Null, exec capture, SSE headers
Recover ## bodies after bad *…* terminators; bracket-aware call args;
invoke Null is 500 not fake ok; sys.exec capture={code,stdout,stderr};
proxy stream sets anti-buffer headers.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
68c4f8c822
commit
1637acce6e
14 files changed
+389
-45
No files matched your search
@@ -87,6 +87,7 @@ pub fn canonical_param(builtin: &str, param: &str) -> String {
|
||||
("host_dotenv_load" | "load_dotenv", "path" | "路径") => "path".into(),
|
||||
("host_exec" | "exec", "cmd" | "命令") => "cmd".into(),
|
||||
("host_exec" | "exec", "args" | "参数") => "args".into(),
|
||||
("host_exec" | "exec", "capture" | "捕获") => "capture".into(),
|
||||
(
|
||||
"host_stream_publish" | "stream_publish",
|
||||
"event" | "事件",
|
||||
|
||||
@@ -910,7 +910,7 @@ impl HostFn {
|
||||
Self::OpenAiSseParse => &["echo"],
|
||||
Self::HttpRequest => &["body", "content_type", "headers"],
|
||||
Self::DotenvLoad => &["path"],
|
||||
Self::Exec => &["args"],
|
||||
Self::Exec => &["args", "capture"],
|
||||
Self::Solve => &["min", "max"],
|
||||
Self::Plot => &["steps", "path", "derivative", "grid"],
|
||||
Self::PlotPoints => &["path", "grid"],
|
||||
@@ -981,7 +981,12 @@ pub fn call_host(
|
||||
HostFn::Args => sys::args(ctx),
|
||||
HostFn::Cwd => sys::cwd(ctx),
|
||||
HostFn::Exit => sys::exit(ctx, require(bound, "code")?),
|
||||
HostFn::Exec => sys::exec(ctx, require(bound, "cmd")?, bound.get("args")),
|
||||
HostFn::Exec => sys::exec(
|
||||
ctx,
|
||||
require(bound, "cmd")?,
|
||||
bound.get("args"),
|
||||
bound.get("capture"),
|
||||
),
|
||||
HostFn::JsonParse => json::parse(require(bound, "text")?),
|
||||
HostFn::JsonStringify => {
|
||||
json::stringify(require(bound, "value")?, bound.get("indent"))
|
||||
|
||||
+44
-6
@@ -108,18 +108,28 @@ pub fn exit(ctx: &HostContext, code: &Value) -> Result<Value, String> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn exec(ctx: &HostContext, cmd: &Value, args: Option<&Value>) -> Result<Value, String> {
|
||||
pub fn exec(
|
||||
ctx: &HostContext,
|
||||
cmd: &Value,
|
||||
args: Option<&Value>,
|
||||
capture: Option<&Value>,
|
||||
) -> Result<Value, String> {
|
||||
#[cfg(not(feature = "exec-host"))]
|
||||
{
|
||||
let _ = (ctx, cmd, args);
|
||||
let _ = (ctx, cmd, args, capture);
|
||||
return Err("exec unavailable in browser wasm".into());
|
||||
}
|
||||
#[cfg(feature = "exec-host")]
|
||||
{
|
||||
use std::process::Stdio;
|
||||
if !ctx.allow_exec() {
|
||||
return Err("exec denied by host policy".into());
|
||||
}
|
||||
let cmd = as_text(cmd, "cmd")?;
|
||||
let capture = match capture {
|
||||
None | Some(Value::None) => false,
|
||||
Some(v) => crate::host::math::as_bool(v),
|
||||
};
|
||||
let mut command = Command::new(cmd);
|
||||
command.current_dir(&ctx.cwd);
|
||||
match args {
|
||||
@@ -134,10 +144,38 @@ pub fn exec(ctx: &HostContext, cmd: &Value, args: Option<&Value>) -> Result<Valu
|
||||
}
|
||||
Some(_) => return Err("exec args must be list or text".into()),
|
||||
}
|
||||
let status = command
|
||||
.status()
|
||||
.map_err(|e| format!("exec `{cmd}`: {e}"))?;
|
||||
Ok(Value::Int(status.code().unwrap_or(1) as i64))
|
||||
if capture {
|
||||
command.stdout(Stdio::piped());
|
||||
command.stderr(Stdio::piped());
|
||||
let output = command
|
||||
.output()
|
||||
.map_err(|e| format!("exec `{cmd}`: {e}"))?;
|
||||
let code = output.status.code().unwrap_or(1) as i64;
|
||||
let stdout = String::from_utf8_lossy(&output.stdout).into_owned();
|
||||
let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
|
||||
Ok(Value::Map(vec![
|
||||
("code".into(), Value::Int(code)),
|
||||
("stdout".into(), Value::Text(clip_exec_io(&stdout))),
|
||||
("stderr".into(), Value::Text(clip_exec_io(&stderr))),
|
||||
]))
|
||||
} else {
|
||||
let status = command
|
||||
.status()
|
||||
.map_err(|e| format!("exec `{cmd}`: {e}"))?;
|
||||
Ok(Value::Int(status.code().unwrap_or(1) as i64))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const EXEC_IO_CAP: usize = 256 * 1024;
|
||||
|
||||
fn clip_exec_io(s: &str) -> String {
|
||||
let mut it = s.chars();
|
||||
let head: String = it.by_ref().take(EXEC_IO_CAP).collect();
|
||||
if it.next().is_some() {
|
||||
format!("{head}\n…(truncated)")
|
||||
} else {
|
||||
head
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+142
-11
@@ -44,6 +44,8 @@ fn is_code_starter(c: char) -> bool {
|
||||
}
|
||||
|
||||
/// `*` / `**` lines that are Marqdo executable (statement / return / else), not narrative Markdown.
|
||||
///
|
||||
/// Closing `*` / `**` must not be taken from inside `` `…` `` or `"…"` (GAP-06/07 nested markers).
|
||||
fn is_marqdo_star_line(trimmed: &str) -> bool {
|
||||
if trimmed == "*" || trimmed == "****" {
|
||||
return true;
|
||||
@@ -52,14 +54,126 @@ fn is_marqdo_star_line(trimmed: &str) -> bool {
|
||||
if trimmed.trim() == "** **" {
|
||||
return true;
|
||||
}
|
||||
if let Some(rest) = trimmed.strip_prefix("**") {
|
||||
if let Some(pos) = rest.find("**") {
|
||||
return rest[pos + 2..].trim().is_empty();
|
||||
}
|
||||
}
|
||||
return matching_bold_close(trimmed).is_some();
|
||||
}
|
||||
if !trimmed.starts_with('*') || trimmed.len() < 2 {
|
||||
return false;
|
||||
}
|
||||
trimmed.starts_with('*') && trimmed.ends_with('*') && trimmed.len() >= 2
|
||||
matching_italic_close(trimmed).is_some()
|
||||
}
|
||||
|
||||
/// Index of the closing `*` for a single-star statement, skipping strings and backticks.
|
||||
fn matching_italic_close(trimmed: &str) -> Option<usize> {
|
||||
if trimmed.starts_with("**") {
|
||||
return None;
|
||||
}
|
||||
let bytes = trimmed.as_bytes();
|
||||
let mut i = 1usize; // skip opening *
|
||||
let mut in_bt = false;
|
||||
let mut in_str = false;
|
||||
while i < bytes.len() {
|
||||
let c = bytes[i];
|
||||
if in_str {
|
||||
if c == b'\\' && i + 1 < bytes.len() {
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
if c == b'"' {
|
||||
in_str = false;
|
||||
}
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
if in_bt {
|
||||
if c == b'`' {
|
||||
in_bt = false;
|
||||
}
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
match c {
|
||||
b'`' => {
|
||||
in_bt = true;
|
||||
i += 1;
|
||||
}
|
||||
b'"' => {
|
||||
in_str = true;
|
||||
i += 1;
|
||||
}
|
||||
b'*' => {
|
||||
// Closing * must be the last non-ws on the line.
|
||||
if trimmed[i + 1..].trim().is_empty() {
|
||||
return Some(i);
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
_ => i += 1,
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Closing `**` for a bold return, skipping strings and backticks.
|
||||
fn matching_bold_close(trimmed: &str) -> Option<usize> {
|
||||
let Some(rest) = trimmed.strip_prefix("**") else {
|
||||
return None;
|
||||
};
|
||||
let bytes = rest.as_bytes();
|
||||
let mut i = 0usize;
|
||||
let mut in_bt = false;
|
||||
let mut in_str = false;
|
||||
while i + 1 < bytes.len() {
|
||||
let c = bytes[i];
|
||||
if in_str {
|
||||
if c == b'\\' && i + 1 < bytes.len() {
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
if c == b'"' {
|
||||
in_str = false;
|
||||
}
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
if in_bt {
|
||||
if c == b'`' {
|
||||
in_bt = false;
|
||||
}
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
match c {
|
||||
b'`' => {
|
||||
in_bt = true;
|
||||
i += 1;
|
||||
}
|
||||
b'"' => {
|
||||
in_str = true;
|
||||
i += 1;
|
||||
}
|
||||
b'*' if bytes[i + 1] == b'*' => {
|
||||
let after = i + 2;
|
||||
if rest[after..].trim().is_empty() {
|
||||
// Position relative to full `trimmed` (after opening `**`).
|
||||
return Some(2 + i);
|
||||
}
|
||||
i += 2;
|
||||
}
|
||||
_ => i += 1,
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Headings / calls / finished star lines should escape a narrative paragraph (GAP-06/07 recovery).
|
||||
fn breaks_comment_paragraph(trimmed: &str) -> bool {
|
||||
if is_structural_code_line(trimmed) {
|
||||
return true;
|
||||
}
|
||||
if trimmed.starts_with('#') || trimmed.starts_with('>') {
|
||||
return true;
|
||||
}
|
||||
is_marqdo_star_line(trimmed)
|
||||
}
|
||||
|
||||
/// Classify a single logical line (no trailing newline required).
|
||||
@@ -141,7 +255,7 @@ pub fn classify_source(source: &str) -> Vec<ClassifiedLine> {
|
||||
in_comment_paragraph = true;
|
||||
LineKind::Comment
|
||||
}
|
||||
LineKind::Code if structural => {
|
||||
LineKind::Code if structural || breaks_comment_paragraph(trimmed) => {
|
||||
in_comment_paragraph = false;
|
||||
LineKind::Code
|
||||
}
|
||||
@@ -298,8 +412,8 @@ mod tests {
|
||||
fn star_line_classification_is_star_terminated() {
|
||||
// A `*…*` Marqdo statement line must END with `*` to be treated as code.
|
||||
// If it accidentally ends with a backtick (e.g. `table="t"`), the line
|
||||
// falls back to narrative Comment and swallows the following paragraph.
|
||||
// Regression: `store.count` calls after a blank line were being dropped.
|
||||
// falls back to narrative Comment — but later executable lines recover
|
||||
// (GAP-06/07: do not swallow the rest of a `##` body).
|
||||
let star_term = "*c = > `store`.count table=\"t\"*";
|
||||
assert!(
|
||||
is_marqdo_star_line(star_term),
|
||||
@@ -307,6 +421,21 @@ mod tests {
|
||||
);
|
||||
assert_eq!(classify_line(star_term), LineKind::Code);
|
||||
|
||||
let nested_bt = "*code = > sys.exec cmd=\"python3\" args=`args`*";
|
||||
assert!(
|
||||
is_marqdo_star_line(nested_bt),
|
||||
"args=`args`* must still be a star line"
|
||||
);
|
||||
let nested_json = "*args = > json.parse text=[\"scripts/legacy/web_search.py\"]*";
|
||||
assert!(
|
||||
is_marqdo_star_line(nested_json),
|
||||
"text=[…] * must still be a star line"
|
||||
);
|
||||
assert!(
|
||||
is_marqdo_star_line("*x = > f text=`a*b`*"),
|
||||
"* inside backticks must not steal the closer"
|
||||
);
|
||||
|
||||
let backtick_term = "*c = > `store`.count table=\"t\"`";
|
||||
assert!(
|
||||
!is_marqdo_star_line(backtick_term),
|
||||
@@ -314,9 +443,11 @@ mod tests {
|
||||
);
|
||||
assert_eq!(classify_line(backtick_term), LineKind::Comment);
|
||||
|
||||
// In paragraph context the misplaced line must not stay Code.
|
||||
let src = "> `store`.insert table=t rows=`数据`\n\n*c = > `store`.count table=\"t\"`\n> print text=count1-done\n";
|
||||
// Bad terminator → comment, but following `>` / `*…*` escape the paragraph.
|
||||
let src = "> `store`.insert table=t rows=`数据`\n\n*c = > `store`.count table=\"t\"`\n> print text=count1-done\n*y = 1*\n";
|
||||
let lines = classify_source(src);
|
||||
assert_eq!(lines[2].kind, LineKind::Comment, "bad terminator → comment");
|
||||
assert_eq!(lines[3].kind, LineKind::Code, "call recovers after bad line");
|
||||
assert_eq!(lines[4].kind, LineKind::Code, "assign recovers after bad line");
|
||||
}
|
||||
}
|
||||
+31
-6
@@ -924,9 +924,12 @@ fn split_first_token(s: &str) -> (&str, &str) {
|
||||
}
|
||||
|
||||
fn find_next_arg_boundary(after_eq: &str) -> usize {
|
||||
// Look for ` <ident>=` pattern not inside backticks or quoted strings.
|
||||
// Look for ` <ident>=` pattern not inside backticks, quoted strings, or
|
||||
// JSON-ish `[…]` / `{…}` (GAP-07: `text=["a"] other=…`).
|
||||
let mut in_bt = false;
|
||||
let mut in_str = false;
|
||||
let mut bracket_depth: i32 = 0;
|
||||
let mut brace_depth: i32 = 0;
|
||||
let mut chars = after_eq.char_indices().peekable();
|
||||
while let Some((i, c)) = chars.next() {
|
||||
if in_str {
|
||||
@@ -945,12 +948,34 @@ fn find_next_arg_boundary(after_eq: &str) -> usize {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if c == '`' {
|
||||
in_bt = true;
|
||||
continue;
|
||||
match c {
|
||||
'`' => {
|
||||
in_bt = true;
|
||||
continue;
|
||||
}
|
||||
'"' => {
|
||||
in_str = true;
|
||||
continue;
|
||||
}
|
||||
'[' => {
|
||||
bracket_depth += 1;
|
||||
continue;
|
||||
}
|
||||
']' => {
|
||||
bracket_depth = bracket_depth.saturating_sub(1);
|
||||
continue;
|
||||
}
|
||||
'{' => {
|
||||
brace_depth += 1;
|
||||
continue;
|
||||
}
|
||||
'}' => {
|
||||
brace_depth = brace_depth.saturating_sub(1);
|
||||
continue;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if c == '"' {
|
||||
in_str = true;
|
||||
if bracket_depth > 0 || brace_depth > 0 {
|
||||
continue;
|
||||
}
|
||||
if c.is_whitespace() {
|
||||
|
||||
Reference in new issue
Block a user