1mod anchors;
18mod apply;
19mod metering;
20mod output;
21mod symbol;
22#[cfg(test)]
23mod tests;
24
25pub use anchors::AnchorOp;
26pub(crate) use symbol::{build_refactor_args, is_replace_symbol};
27
28use std::path::{Path, PathBuf};
29
30use crate::core::cache::SessionCache;
31use crate::core::tokens::count_tokens;
32use crate::tools::ctx_edit::{CacheEffect, apply_cache_effect, build_diff_evidence};
33use crate::tools::edit_io::{
34 default_backup_path, ensure_preimage_still_matches, read_preimage,
35 write_atomic_bytes_with_permissions,
36};
37
38pub struct PatchParams {
42 pub path: String,
43 pub ops: Vec<AnchorOp>,
44 pub expected_md5: Option<String>,
47 pub backup: bool,
48 pub backup_path: Option<String>,
49 pub evidence: bool,
50 pub diff_max_lines: usize,
51 pub allow_lossy_utf8: bool,
52 pub validate_syntax: bool,
56}
57
58pub fn parse_ops(
60 args: &serde_json::Map<String, serde_json::Value>,
61) -> Result<Vec<AnchorOp>, String> {
62 anchors::parse_ops(args)
63}
64
65pub fn handle(cache: &mut SessionCache, params: &PatchParams) -> String {
68 let last_mode = cache
69 .get(¶ms.path)
70 .map(|e| e.last_mode.clone())
71 .unwrap_or_default();
72 let (text, effect) = run_io(params, &last_mode);
73 record_outcome(params, &last_mode, &text, &effect);
74 apply_cache_effect(cache, ¶ms.path, effect);
75 text
76}
77
78pub fn record_outcome(params: &PatchParams, last_mode: &str, text: &str, effect: &CacheEffect) {
85 let success = matches!(effect, CacheEffect::Invalidate);
86 let conflict = matches!(effect, CacheEffect::None) && text.starts_with("CONFLICT:");
87 if success || conflict {
88 crate::core::edit_quality::record_anchored_edit_outcome(¶ms.path, last_mode, success);
89 }
90}
91
92pub fn run_io(params: &PatchParams, _last_mode: &str) -> (String, CacheEffect) {
96 let file_path = ¶ms.path;
97 let path = Path::new(file_path);
98 let cap = crate::core::limits::max_read_bytes();
99
100 if let Some(content) = single_create_op(¶ms.ops) {
103 return match content {
104 Ok(text) => handle_create(params, path, text),
105 Err(e) => (e, CacheEffect::None),
106 };
107 }
108
109 let pre = match read_preimage(path, cap, params.allow_lossy_utf8) {
110 Ok(p) => p,
111 Err(e) => {
112 if !path.exists() {
113 let hint = crate::tools::edit_recovery::moved_or_deleted_hint(path);
114 return (format!("{e}{hint}"), CacheEffect::None);
115 }
116 return (e, CacheEffect::None);
117 }
118 };
119
120 if let Some(expected) = params.expected_md5.as_deref()
121 && expected != pre.fp.md5
122 {
123 return (
124 format!(
125 "ERROR: preimage mismatch for {file_path}: expected_md5={expected}, actual_md5={}",
126 pre.fp.md5
127 ),
128 CacheEffect::None,
129 );
130 }
131
132 if params.ops.is_empty() {
133 return (
134 "ERROR: no edits provided (pass an op or ops:[…])".to_string(),
135 CacheEffect::None,
136 );
137 }
138
139 let (bom, body) = match pre.text.strip_prefix('\u{feff}') {
145 Some(rest) => ("\u{feff}", rest),
146 None => ("", pre.text.as_str()),
147 };
148 let (lines, sep, trailing) = apply::split_lines(body);
149
150 let edits = match apply::resolve_ops(&lines, ¶ms.ops) {
151 Ok(e) => e,
152 Err(apply::ResolveError::Conflict(misses)) => {
153 crate::core::edit_metering::record_anchored_conflict();
156 return (
157 output::render_conflict(file_path, &lines, &misses),
158 CacheEffect::None,
159 );
160 }
161 Err(apply::ResolveError::Invalid(msg)) => {
162 return (format!("ERROR: {msg}"), CacheEffect::None);
163 }
164 };
165
166 let avoided_tokens = metering::avoided_output_tokens(&lines, ¶ms.ops);
169
170 let n_edits = edits.len();
171 let lines_before = lines.len();
172 let new_lines = apply::apply_edits(lines.clone(), edits);
173 let new_content = format!("{bom}{}", apply::join_lines(&new_lines, sep, trailing));
174
175 if new_content == pre.text {
176 return (
177 "ERROR: edits produced no change to the file".to_string(),
178 CacheEffect::None,
179 );
180 }
181
182 let ext = Path::new(file_path)
183 .extension()
184 .and_then(|e| e.to_str())
185 .unwrap_or("");
186
187 if params.validate_syntax
190 && let Some(reason) = crate::core::syntax_validate::gate_edit(ext, &pre.text, &new_content)
191 {
192 return (reason, CacheEffect::None);
193 }
194
195 let health_notice = match crate::core::code_health::gate::evaluate(&pre.text, &new_content, ext)
197 {
198 crate::core::code_health::gate::GateOutcome::Block(reason) => {
199 return (
200 format!("ERROR: code-health gate: {reason}"),
201 CacheEffect::None,
202 );
203 }
204 crate::core::code_health::gate::GateOutcome::Allow(notice) => notice,
205 };
206
207 if let Err(e) = ensure_preimage_still_matches(path, &pre.fp, cap) {
209 return (e, CacheEffect::None);
210 }
211
212 let backup_path = match make_backup(params, path, &pre.bytes, &pre.permissions) {
213 Ok(bp) => bp,
214 Err(e) => return (e, CacheEffect::None),
215 };
216
217 if let Err(e) =
218 write_atomic_bytes_with_permissions(path, new_content.as_bytes(), Some(&pre.permissions))
219 {
220 return (e, CacheEffect::None);
221 }
222
223 if let Ok(mut bt) = crate::core::bounce_tracker::global().lock() {
224 bt.record_edit(file_path);
225 }
226
227 crate::core::edit_metering::record_anchored_success(n_edits as u64, avoided_tokens);
229
230 let mut out = render_success(
231 params,
232 &pre.text,
233 &new_content,
234 pre.fp.size,
235 pre.fp.mtime_ms,
236 &pre.fp.md5,
237 lines_before,
238 new_lines.len(),
239 n_edits,
240 backup_path,
241 );
242 if let Some(notice) = health_notice {
243 out.push_str("\n\n");
244 out.push_str(¬ice);
245 }
246 (out, CacheEffect::Invalidate)
247}
248
249fn single_create_op(ops: &[AnchorOp]) -> Option<Result<&str, String>> {
253 let create = ops.iter().find_map(|op| match op {
254 AnchorOp::Create { new_text } => Some(new_text.as_str()),
255 _ => None,
256 })?;
257 if ops.len() > 1 {
258 return Some(Err(
259 "ERROR: create cannot be batched with anchored ops — a new file has no \
260 preimage to anchor against; send create as a single op"
261 .to_string(),
262 ));
263 }
264 Some(Ok(create))
265}
266
267fn handle_create(params: &PatchParams, path: &Path, content: &str) -> (String, CacheEffect) {
271 if path.exists() {
272 return (
273 format!(
274 "ERROR: {} already exists — create is for new files only. \
275 Use anchored ops (ctx_read mode=\"anchored\" → set_line/replace_lines) to modify it.",
276 params.path
277 ),
278 CacheEffect::None,
279 );
280 }
281
282 if let Err(e) = crate::core::pathjail::enforce_writable(path) {
285 return (format!("ERROR: {e}"), CacheEffect::None);
286 }
287
288 if let Some(parent) = path.parent()
289 && !parent.exists()
290 && let Err(e) = std::fs::create_dir_all(parent)
291 {
292 return (
293 format!("ERROR: cannot create directory {}: {e}", parent.display()),
294 CacheEffect::None,
295 );
296 }
297
298 if let Err(e) = write_atomic_bytes_with_permissions(path, content.as_bytes(), None) {
299 return (e, CacheEffect::None);
300 }
301
302 if let Ok(mut bt) = crate::core::bounce_tracker::global().lock() {
303 bt.record_edit(¶ms.path);
304 }
305
306 let lines = content.lines().count();
307 let tokens = count_tokens(content);
308 let short = output::short_name(¶ms.path);
309 let post_md5 = crate::core::hasher::hash_hex(content.as_bytes());
310 let mut out = format!(
311 "✓ created {short}: {lines} lines, {tokens} tok\npostimage: bytes={}, md5={post_md5}",
312 content.len()
313 );
314 if params.evidence {
315 let diff = build_diff_evidence("", content, &short, params.diff_max_lines);
316 out.push_str("\n\nevidence (diff, redacted, bounded):\n```diff\n");
317 out.push_str(&diff);
318 out.push_str("\n```");
319 }
320 (out, CacheEffect::Invalidate)
321}
322
323fn make_backup(
325 params: &PatchParams,
326 path: &Path,
327 bytes: &[u8],
328 permissions: &std::fs::Permissions,
329) -> Result<Option<String>, String> {
330 if !params.backup {
331 return Ok(None);
332 }
333 let bp = params
334 .backup_path
335 .as_deref()
336 .map(PathBuf::from)
337 .or_else(|| default_backup_path(path))
338 .ok_or_else(|| format!("ERROR: cannot compute backup path for {}", path.display()))?;
339 write_atomic_bytes_with_permissions(&bp, bytes, Some(permissions))
340 .map_err(|e| format!("ERROR: cannot create backup {}: {e}", bp.display()))?;
341 Ok(Some(bp.to_string_lossy().to_string()))
342}
343
344#[allow(clippy::too_many_arguments)]
345fn render_success(
346 params: &PatchParams,
347 old_content: &str,
348 new_content: &str,
349 pre_size: u64,
350 pre_mtime_ms: u64,
351 pre_md5: &str,
352 lines_before: usize,
353 lines_after: usize,
354 n_edits: usize,
355 backup_path: Option<String>,
356) -> String {
357 let short = output::short_name(¶ms.path);
358 let line_delta = lines_after as i64 - lines_before as i64;
359 let delta_str = if line_delta >= 0 {
360 format!("+{line_delta}")
361 } else {
362 format!("{line_delta}")
363 };
364 let old_tokens = count_tokens(old_content);
365 let new_tokens = count_tokens(new_content);
366
367 let post_mtime_ms = std::fs::metadata(¶ms.path)
368 .ok()
369 .and_then(|m| m.modified().ok())
370 .map_or(0, crate::tools::edit_io::system_time_to_millis);
371 let post_md5 = crate::core::hasher::hash_hex(new_content.as_bytes());
372
373 let edit_word = if n_edits == 1 { "edit" } else { "edits" };
374 let mut out = format!(
375 "✓ {short}: {n_edits} anchored {edit_word}, {delta_str} lines ({old_tokens}→{new_tokens} tok)\n\
376preimage: bytes={pre_size}, mtime_ms={pre_mtime_ms}, md5={pre_md5}\n\
377postimage: bytes={}, mtime_ms={post_mtime_ms}, md5={post_md5}",
378 new_content.len()
379 );
380 if let Some(bp) = backup_path {
381 out.push_str(&format!("\nbackup: {bp}"));
382 }
383 if params.evidence {
384 let diff = build_diff_evidence(old_content, new_content, &short, params.diff_max_lines);
385 out.push_str("\n\nevidence (diff, redacted, bounded):\n```diff\n");
386 out.push_str(&diff);
387 out.push_str("\n```");
388 let balance = brace_balance(new_content);
389 if balance == 0 {
390 out.push_str("\nbrace-balance: ok (matched)");
391 } else {
392 out.push_str(&format!(
393 "\n⚠ brace-balance: {} unmatched '{{' — verify file integrity",
394 balance.abs()
395 ));
396 }
397 }
398 out
399}
400
401fn brace_balance(content: &str) -> i64 {
404 content.chars().fold(0i64, |acc, c| match c {
405 '{' => acc + 1,
406 '}' => acc - 1,
407 _ => acc,
408 })
409}