Make file subtasks return #main value and expose it as plan.result.
Quiet spawn still hides child stdout; workbooks solidify to **returns**, agent-kb lives in the plugin, and multi-step follows OKF file relay rather than print-based answers. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
fbece725e1
commit
d1b33c78c6
55 files changed
+3498
-169
No files matched your search
+185
@@ -106,3 +106,188 @@ pub fn remove(ctx: &HostContext, path: &Value) -> Result<Value, String> {
|
||||
}
|
||||
Ok(Value::None)
|
||||
}
|
||||
|
||||
/// Exact UTF-8 FIND→REPLACE on a file. `find` must occur exactly once.
|
||||
pub fn text_patch(ctx: &HostContext, path: &Value, find: &Value, replace: &Value) -> Result<Value, String> {
|
||||
if !ctx.allow_fs_write() {
|
||||
return Err("text_patch denied by host policy".into());
|
||||
}
|
||||
let p = ctx.resolve_path(text_path(path)?)?;
|
||||
let find = text_body(find)?;
|
||||
let replace = text_body(replace)?;
|
||||
if find.is_empty() {
|
||||
return Err("text_patch: find must be non-empty".into());
|
||||
}
|
||||
let src = fs::read_to_string(&p).map_err(|e| format!("text_patch {}: {e}", p.display()))?;
|
||||
let count = src.matches(find).count();
|
||||
if count == 0 {
|
||||
return Err(format!(
|
||||
"text_patch: find not found in {}",
|
||||
p.display()
|
||||
));
|
||||
}
|
||||
if count > 1 {
|
||||
return Err(format!(
|
||||
"text_patch: find matched {count} times in {} (must be unique)",
|
||||
p.display()
|
||||
));
|
||||
}
|
||||
let next = src.replacen(find, replace, 1);
|
||||
fs::write(&p, next).map_err(|e| format!("text_patch write {}: {e}", p.display()))?;
|
||||
Ok(Value::Int(1))
|
||||
}
|
||||
|
||||
/// Apply one or more FIND/REPLACE blocks from plan-reply text.
|
||||
///
|
||||
/// Blocks:
|
||||
/// ```text
|
||||
/// <<<
|
||||
/// FIND
|
||||
/// <old>
|
||||
/// ===
|
||||
/// REPLACE
|
||||
/// <new>
|
||||
/// >>>
|
||||
/// ```
|
||||
pub fn apply_patch_blocks(ctx: &HostContext, path: &Value, text: &Value) -> Result<Value, String> {
|
||||
if !ctx.allow_fs_write() {
|
||||
return Err("apply_patch_blocks denied by host policy".into());
|
||||
}
|
||||
let blocks = parse_patch_blocks(text_body(text)?)?;
|
||||
if blocks.is_empty() {
|
||||
return Ok(Value::Int(0));
|
||||
}
|
||||
let p = ctx.resolve_path(text_path(path)?)?;
|
||||
let mut src = fs::read_to_string(&p).map_err(|e| format!("apply_patch_blocks {}: {e}", p.display()))?;
|
||||
let mut applied = 0i64;
|
||||
for (find, replace) in blocks {
|
||||
if find.is_empty() {
|
||||
return Err("apply_patch_blocks: empty FIND".into());
|
||||
}
|
||||
let count = src.matches(&find).count();
|
||||
if count == 0 {
|
||||
return Err(format!(
|
||||
"apply_patch_blocks: FIND not found in {}",
|
||||
p.display()
|
||||
));
|
||||
}
|
||||
if count > 1 {
|
||||
return Err(format!(
|
||||
"apply_patch_blocks: FIND matched {count} times in {} (must be unique)",
|
||||
p.display()
|
||||
));
|
||||
}
|
||||
src = src.replacen(&find, &replace, 1);
|
||||
applied += 1;
|
||||
}
|
||||
fs::write(&p, src).map_err(|e| format!("apply_patch_blocks write {}: {e}", p.display()))?;
|
||||
Ok(Value::Int(applied))
|
||||
}
|
||||
|
||||
fn parse_patch_blocks(text: &str) -> Result<Vec<(String, String)>, String> {
|
||||
let mut out = Vec::new();
|
||||
let mut rest = text;
|
||||
while let Some(start) = rest.find("<<<") {
|
||||
let after = &rest[start + 3..];
|
||||
let end = after
|
||||
.find(">>>")
|
||||
.ok_or_else(|| "apply_patch_blocks: unclosed <<< block".to_string())?;
|
||||
let body = after[..end].trim();
|
||||
rest = &after[end + 3..];
|
||||
let (find_part, replace_part) = body
|
||||
.split_once("===")
|
||||
.ok_or_else(|| "apply_patch_blocks: block missing ===".to_string())?;
|
||||
let find = strip_labeled(find_part, "FIND")?;
|
||||
let replace = strip_labeled(replace_part, "REPLACE")?;
|
||||
out.push((find, replace));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn strip_labeled(chunk: &str, label: &str) -> Result<String, String> {
|
||||
let t = chunk.trim();
|
||||
let upper = t.to_ascii_uppercase();
|
||||
let label_u = label.to_ascii_uppercase();
|
||||
if upper.starts_with(&label_u) {
|
||||
let after = t[label.len()..].trim_start_matches([':', ' ', '\t']);
|
||||
let after = after.strip_prefix('\n').unwrap_or(after);
|
||||
Ok(after.to_string())
|
||||
} else {
|
||||
Ok(t.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::host::{HostCaps, HostContext};
|
||||
use std::fs;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
fn write_ctx(dir: &std::path::Path) -> HostContext {
|
||||
let entry = dir.join("entry.mq.md");
|
||||
let _ = fs::write(&entry, "# main\n");
|
||||
let mut ctx = HostContext::for_run(Some(&entry), HostCaps::default(), vec![]);
|
||||
ctx.cwd = dir.to_path_buf();
|
||||
ctx.fs_root = Some(dir.to_path_buf());
|
||||
ctx
|
||||
}
|
||||
|
||||
fn temp_workspace(tag: &str) -> std::path::PathBuf {
|
||||
let nanos = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos();
|
||||
let dir = std::env::temp_dir().join(format!("marqdo-fs-{tag}-{nanos}"));
|
||||
fs::create_dir_all(&dir).unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_patch_unique() {
|
||||
let dir = temp_workspace("patch");
|
||||
let path = dir.join("a.md");
|
||||
fs::write(&path, "hello OLD world").unwrap();
|
||||
let ctx = write_ctx(&dir);
|
||||
let n = text_patch(
|
||||
&ctx,
|
||||
&Value::Text("a.md".into()),
|
||||
&Value::Text("OLD".into()),
|
||||
&Value::Text("NEW".into()),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(n, Value::Int(1));
|
||||
assert_eq!(fs::read_to_string(&path).unwrap(), "hello NEW world");
|
||||
let _ = fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn apply_patch_blocks_two() {
|
||||
let dir = temp_workspace("blocks");
|
||||
let path = dir.join("a.md");
|
||||
fs::write(&path, "aa bb cc").unwrap();
|
||||
let ctx = write_ctx(&dir);
|
||||
let reply = r#"
|
||||
DECISION: CONTINUE
|
||||
PATCH:
|
||||
<<<
|
||||
FIND
|
||||
aa
|
||||
===
|
||||
REPLACE
|
||||
AA
|
||||
>>>
|
||||
<<<
|
||||
FIND
|
||||
cc
|
||||
===
|
||||
REPLACE
|
||||
CC
|
||||
>>>
|
||||
"#;
|
||||
let n = apply_patch_blocks(&ctx, &Value::Text("a.md".into()), &Value::Text(reply.into())).unwrap();
|
||||
assert_eq!(n, Value::Int(2));
|
||||
assert_eq!(fs::read_to_string(&path).unwrap(), "AA bb CC");
|
||||
let _ = fs::remove_dir_all(&dir);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user