Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(filesystem): handle create edits in apply_patch

Steven Enamakelee205e143aca
diff --git a/crates/openhuman-core/src/tools/impl/filesystem/apply_patch.rs b/crates/openhuman-core/src/tools/impl/filesystem/apply_patch.rs
index 69122a6f95..1ba9a7b9e6 100644
--- a/crates/openhuman-core/src/tools/impl/filesystem/apply_patch.rs
+++ b/crates/openhuman-core/src/tools/impl/filesystem/apply_patch.rs
@@ -257,13 +257,46 @@ impl ApplyPatchTool {
}
}
- // Security check: validate path string, resolve symlinks, confirm workspace containment.
- let resolved = match path_policy.validate_path(&edit.path).await {
- Ok(p) => p,
- Err(msg) => {
- return Ok(ToolResult::error(format!("edit[{}]: {msg}", edit.index)));
+ // Security check: validate path string, resolve symlinks, confirm
+ // workspace containment. A create has no file to canonicalize,
+ // so it resolves through the parent — the same gate
+ // `file_write` uses, which walks up to the deepest existing
+ // ancestor and still checks it for symlink escapes.
+ let resolved = if edit.create {
+ match path_policy.validate_parent_path(&edit.path).await {
+ Ok(p) => p,
+ Err(msg) => {
+ return Ok(ToolResult::error(format!("edit[{}]: {msg}", edit.index)));
+ }
+ }
+ } else {
+ match path_policy.validate_path(&edit.path).await {
+ Ok(p) => p,
+ Err(msg) => {
+ return Ok(ToolResult::error(format!("edit[{}]: {msg}", edit.index)));
+ }
}
};
+ if edit.create {
+ if let Some(parent) = resolved.parent() {
+ if let Err(e) = tokio::fs::create_dir_all(parent).await {
+ return Ok(ToolResult::error(format!(
+ "edit[{}]: failed to create parent of {}: {e}",
+ edit.index, edit.path
+ )));
+ }
+ }
+ buffers.insert(
+ edit.path.clone(),
+ FileBuffer {
+ resolved,
+ original: None,
+ contents: edit.new_string.clone(),
+ edit_count: 1,
+ },
+ );
+ continue;
+ }
if let Ok(meta) = tokio::fs::metadata(&resolved).await {
if meta.len() > MAX_FILE_BYTES {
return Ok(ToolResult::error(format!(
@@ -286,13 +319,23 @@ impl ApplyPatchTool {
edit.path.clone(),
FileBuffer {
resolved,
- original: contents.clone(),
+ original: Some(contents.clone()),
contents,
edit_count: 0,
},
);
}
+ if edit.create {
+ // A second create for the same path in one batch. The first one
+ // already populated the buffer; a duplicate would silently
+ // discard one of the two bodies, so say so.
+ return Ok(ToolResult::error(format!(
+ "edit[{}]: {} is created earlier in this batch; only one create per path",
+ edit.index, edit.path
+ )));
+ }
+
let buf = buffers.get_mut(&edit.path).unwrap();
let count = buf.contents.matches(&edit.old_string).count();
if count == 0 {