Skip to main content

winx_code_agent/tools/
multi_file_edit.rs

1//! Implementation of the `MultiFileEdit` tool.
2//!
3//! Applies edits across several files all-or-nothing at the COMPUTE stage: every
4//! file is validated and its new content computed in memory first (reusing
5//! `file_write_or_edit::plan_edit`), and only if ALL succeed is anything written.
6//! So a SEARCH block that fails to match in the last file leaves the earlier
7//! files untouched, instead of the half-edited tree N separate `FileWriteOrEdit`
8//! calls would leave.
9//!
10//! The write stage is a sequence of individually-atomic single-file renames
11//! (`commit_edit` -> `write_no_follow`). It stops at the first I/O failure and
12//! reports which files were already written; it does NOT roll them back (each is
13//! already crash-safe on its own, and a second write pass could fail and corrupt
14//! more state).
15
16use std::collections::HashSet;
17use std::path::{Path, PathBuf};
18use std::sync::Arc;
19
20use tokio::sync::Mutex;
21use tracing::instrument;
22
23use crate::errors::{Result, WinxError};
24use crate::state::bash_state::BashState;
25use crate::tools::file_write_or_edit::{commit_edit, plan_edit};
26use crate::types::{normalize_thread_id, FileEditEntry, MultiFileEdit};
27
28/// Upper bound on files per batch. The whole batch holds the `bash_state` lock
29/// across its (synchronous) file IO, so a huge batch would block the executor
30/// and other sessions for a long time. A real multi-file refactor is well under
31/// this; the cap is a guard against a pathological request.
32const MAX_FILES_PER_BATCH: usize = 100;
33
34#[instrument(level = "info", skip(bash_state_arc, multi))]
35pub async fn handle_tool_call(
36    bash_state_arc: &Arc<Mutex<Option<BashState>>>,
37    multi: MultiFileEdit,
38) -> Result<String> {
39    let mut bash_state_guard = bash_state_arc.lock().await;
40
41    // Cheap validation up front (needs only the current thread id).
42    {
43        let bash_state = bash_state_guard.as_ref().ok_or(WinxError::BashStateNotInitialized)?;
44        let thread_id = normalize_thread_id(&multi.thread_id);
45        if thread_id != bash_state.current_thread_id {
46            return Err(WinxError::ThreadIdMismatch(thread_id));
47        }
48    }
49    if multi.files.len() < 2 {
50        return Err(WinxError::ArgumentParseError(
51            "MultiFileEdit needs at least 2 files; use FileWriteOrEdit for a single file."
52                .to_string(),
53        ));
54    }
55    if multi.files.len() > MAX_FILES_PER_BATCH {
56        return Err(WinxError::ArgumentParseError(format!(
57            "MultiFileEdit is limited to {MAX_FILES_PER_BATCH} files per batch (got {}); split the \
58             change into smaller batches.",
59            multi.files.len()
60        )));
61    }
62
63    // Move the state onto the blocking pool for the synchronous plan+commit IO
64    // (reading every file, then writing every file). The guard is held throughout,
65    // so the slot stays locked — mutual exclusion is preserved; `take` just lets us
66    // own the value across spawn_blocking. This frees the tokio worker instead of
67    // pinning it on up to MAX_FILES_PER_BATCH file reads/writes.
68    let mut state = bash_state_guard.take().ok_or(WinxError::BashStateNotInitialized)?;
69    let recovery_state = state.clone();
70    let files = multi.files;
71    let joined = tokio::task::spawn_blocking(move || {
72        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
73            apply_batch(&mut state, &files)
74        }))
75        .unwrap_or_else(|_| {
76            Err(WinxError::CommandExecutionError(
77                "MultiFileEdit panicked on the blocking worker".to_string(),
78            ))
79        });
80        (state, result)
81    })
82    .await;
83
84    match joined {
85        Ok((state, result)) => {
86            *bash_state_guard = Some(state);
87            result
88        }
89        Err(error) => {
90            *bash_state_guard = Some(recovery_state);
91            Err(WinxError::CommandExecutionError(format!(
92                "MultiFileEdit blocking task failed: {error}"
93            )))
94        }
95    }
96}
97
98/// Plan every file (all-or-nothing at the compute stage), reject duplicate
99/// targets, then commit sequentially. Synchronous (file IO) — runs on the
100/// blocking pool, never on a tokio worker.
101fn apply_batch(bash_state: &mut BashState, files: &[FileEditEntry]) -> Result<String> {
102    // PHASE 1: plan every file (validate + compute new content) with NO writes.
103    // Any failure aborts the whole batch having touched nothing on disk.
104    let mut planned = Vec::with_capacity(files.len());
105    for (index, entry) in files.iter().enumerate() {
106        let edit = plan_edit(
107            bash_state,
108            &entry.file_path,
109            entry.percentage_to_change,
110            &entry.text_or_search_replace_blocks,
111        )
112        .map_err(|error| contextualize_plan_error(bash_state, index, entry, error))?;
113        planned.push(edit);
114    }
115
116    // Reject duplicate targets, checked on the RESOLVED path (so `a.txt` and its
117    // absolute form can't both slip through and clobber each other): two entries
118    // for the same file don't compose - the second is computed from the original,
119    // not the first's result, so it would silently overwrite the first.
120    let mut seen = HashSet::with_capacity(planned.len());
121    for edit in &planned {
122        if !seen.insert(edit.target()) {
123            return Err(WinxError::ArgumentParseError(format!(
124                "MultiFileEdit targets '{}' more than once; edits to the same file don't compose - \
125                 combine them into a single entry.",
126                edit.target()
127            )));
128        }
129    }
130
131    // PHASE 2: commit sequentially. Each write is individually atomic (temp +
132    // rename). On the first failure, stop and report honestly without rolling
133    // back already-written files.
134    let total = planned.len();
135    let mut summaries = Vec::with_capacity(total);
136    for (committed, edit) in planned.into_iter().enumerate() {
137        let target = edit.target().to_string();
138        match commit_edit(bash_state, edit) {
139            Ok(summary) => summaries.push(format!("[{target}]\n{summary}")),
140            Err(e) => {
141                return Err(WinxError::CommandExecutionError(format!(
142                    "MultiFileEdit: committed {committed} of {total} files, then failed writing \
143                     {target}: {e}\nThe {committed} already-written file(s) were NOT rolled back. \
144                     Re-read the affected files and retry the rest."
145                )));
146            }
147        }
148    }
149
150    Ok(format!("MultiFileEdit applied all {total} edits:\n\n{}", summaries.join("\n\n")))
151}
152
153/// Preserve the original failure class so the MCP orchestration layer can emit
154/// `needs_read`, stale-file recovery, or SEARCH conflict guidance. SEARCH errors
155/// do not carry a path themselves, so wrap them in path-aware
156/// `MultiFilePlanError` context without flattening the source into a string.
157fn contextualize_plan_error(
158    bash_state: &BashState,
159    index: usize,
160    entry: &FileEditEntry,
161    error: WinxError,
162) -> WinxError {
163    let context = format!(
164        "MultiFileEdit aborted before writing anything - file {} ({}) failed validation",
165        index + 1,
166        entry.file_path
167    );
168    match error {
169        WinxError::FileAccessError { path, message } => {
170            WinxError::FileAccessError { path, message: format!("{context}: {message}") }
171        }
172        source @ (WinxError::SearchBlockNotFound(_) | WinxError::SearchBlockAmbiguous { .. }) => {
173            WinxError::MultiFilePlanError {
174                index: index + 1,
175                path: resolved_entry_path(bash_state, &entry.file_path),
176                source: Box::new(source),
177            }
178        }
179        WinxError::SearchReplaceSyntaxError(message) => {
180            WinxError::SearchReplaceSyntaxError(format!("{context}: {message}"))
181        }
182        WinxError::SearchReplaceSyntaxErrorDetailed {
183            message,
184            line_number,
185            block_type,
186            suggestions,
187        } => WinxError::SearchReplaceSyntaxErrorDetailed {
188            message: format!("{context}: {message}"),
189            line_number,
190            block_type,
191            suggestions,
192        },
193        other => other,
194    }
195}
196
197fn resolved_entry_path(bash_state: &BashState, value: &str) -> PathBuf {
198    let expanded = crate::utils::path::expand_user(value);
199    let path = if Path::new(&expanded).is_absolute() {
200        PathBuf::from(expanded)
201    } else {
202        bash_state.cwd.join(expanded)
203    };
204    path.canonicalize().unwrap_or(path)
205}