fix(agent): harden workbook patches against whole-file rewrites
Reject large whole-file FIND spans, narrow soft=True to miss-only, check decompose CONTINUE patch counts, and unit-test workbook_solidify. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
c36383bf28
commit
d0b184939f
6 files changed
+146
-6
No files matched your search
+84
-1
@@ -174,11 +174,30 @@ pub fn apply_patch_blocks(
|
||||
};
|
||||
match apply_patch_blocks_inner(ctx, path, text) {
|
||||
Ok(n) => Ok(n),
|
||||
Err(_e) if soft => Ok(Value::Int(0)),
|
||||
// soft only swallows unique-FIND miss — never parse / multi-match / whole-file.
|
||||
Err(e) if soft && e.contains("FIND not found") => Ok(Value::Int(0)),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Reject FIND that rewrites a non-trivial file wholesale (exact or ≥90% span).
|
||||
/// Tiny files may be replaced entirely (e.g. two-line import rewrites).
|
||||
fn find_spans_whole_file(src: &str, find: &str) -> bool {
|
||||
const MIN_BYTES: usize = 800;
|
||||
let st = src.trim();
|
||||
let ft = find.trim();
|
||||
if ft.is_empty() || src.len() < MIN_BYTES {
|
||||
return false;
|
||||
}
|
||||
if st == ft {
|
||||
return true;
|
||||
}
|
||||
if !src.contains(find) {
|
||||
return false;
|
||||
}
|
||||
find.len().saturating_mul(10) >= src.len().saturating_mul(9)
|
||||
}
|
||||
|
||||
fn apply_patch_blocks_inner(
|
||||
ctx: &HostContext,
|
||||
path: &Value,
|
||||
@@ -195,6 +214,12 @@ fn apply_patch_blocks_inner(
|
||||
if find.is_empty() {
|
||||
return Err("apply_patch_blocks: empty FIND".into());
|
||||
}
|
||||
if find_spans_whole_file(&src, &find) {
|
||||
return Err(
|
||||
"apply_patch_blocks: FIND spans entire file (whole-file rewrite forbidden)"
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
let count = src.matches(&find).count();
|
||||
if count == 0 {
|
||||
return Err(format!(
|
||||
@@ -527,4 +552,62 @@ PATCH:
|
||||
assert_eq!(fs::read_to_string(&path).unwrap(), "hello");
|
||||
let _ = fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn apply_patch_blocks_rejects_whole_file_find() {
|
||||
let dir = temp_workspace("whole");
|
||||
let path = dir.join("a.md");
|
||||
let body = format!(
|
||||
"---\ntitle: workbook\n---\n\n# main\n\n{}",
|
||||
"line padding for size threshold\n".repeat(40)
|
||||
);
|
||||
assert!(body.len() >= 800, "fixture too small: {}", body.len());
|
||||
fs::write(&path, &body).unwrap();
|
||||
let ctx = write_ctx(&dir);
|
||||
let reply = format!(
|
||||
"<<<\nFIND\n{body}===\nREPLACE\n# main\n\n> print text=bye\n>>>\n"
|
||||
);
|
||||
let err = apply_patch_blocks(
|
||||
&ctx,
|
||||
&Value::Text("a.md".into()),
|
||||
&Value::Text(reply),
|
||||
None,
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(
|
||||
err.contains("whole-file rewrite forbidden"),
|
||||
"unexpected err={err}"
|
||||
);
|
||||
assert_eq!(fs::read_to_string(&path).unwrap(), body);
|
||||
let soft_err = apply_patch_blocks(
|
||||
&ctx,
|
||||
&Value::Text("a.md".into()),
|
||||
&Value::Text(format!(
|
||||
"<<<\nFIND\n{body}===\nREPLACE\nx\n>>>\n"
|
||||
)),
|
||||
Some(&Value::Bool(true)),
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(soft_err.contains("whole-file rewrite forbidden"));
|
||||
let _ = fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn apply_patch_blocks_soft_keeps_multi_match_hard() {
|
||||
let dir = temp_workspace("multisoft");
|
||||
let path = dir.join("a.md");
|
||||
fs::write(&path, "xx xx").unwrap();
|
||||
let ctx = write_ctx(&dir);
|
||||
let reply = "<<<\nFIND\nxx\n===\nREPLACE\nYY\n>>>\n";
|
||||
let err = apply_patch_blocks(
|
||||
&ctx,
|
||||
&Value::Text("a.md".into()),
|
||||
&Value::Text(reply.into()),
|
||||
Some(&Value::Bool(true)),
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(err.contains("matched 2 times"), "unexpected err={err}");
|
||||
assert_eq!(fs::read_to_string(&path).unwrap(), "xx xx");
|
||||
let _ = fs::remove_dir_all(&dir);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user