code-mesh-core 0.1.0

High-performance, WASM-powered distributed swarm intelligence core library for concurrent code execution and neural mesh computing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! Multi-edit tool implementation for batch file editing with atomic transactions

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::path::PathBuf;
use tokio::fs;
use similar::TextDiff;
use uuid::Uuid;

use super::{Tool, ToolContext, ToolResult, ToolError};
use super::edit::{SimpleReplacer, LineTrimmedReplacer, WhitespaceNormalizedReplacer, IndentationFlexibleReplacer, ReplacementStrategy};

/// Tool for performing multiple file edits in a single atomic operation
pub struct MultiEditTool;

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct EditOperation {
    pub old_string: String,
    pub new_string: String,
    #[serde(default)]
    pub replace_all: bool,
}

#[derive(Debug, Deserialize)]
struct MultiEditParams {
    file_path: String,
    edits: Vec<EditOperation>,
}

#[derive(Debug)]
struct FileBackup {
    backup_id: String,
    original_content: String,
    backup_path: PathBuf,
}

#[derive(Debug)]
struct EditResult {
    operation_index: usize,
    replacements: usize,
    strategy_used: String,
    content_after: String,
}

#[async_trait]
impl Tool for MultiEditTool {
    fn id(&self) -> &str {
        "multiedit"
    }
    
    fn description(&self) -> &str {
        "Perform multiple file edits in a single atomic operation with rollback support"
    }
    
    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "file_path": {
                    "type": "string",
                    "description": "Path to the file to edit"
                },
                "edits": {
                    "type": "array",
                    "description": "Array of edit operations to perform sequentially",
                    "items": {
                        "type": "object",
                        "properties": {
                            "old_string": {
                                "type": "string",
                                "description": "Text to find and replace"
                            },
                            "new_string": {
                                "type": "string",
                                "description": "Replacement text"
                            },
                            "replace_all": {
                                "type": "boolean",
                                "description": "Replace all occurrences (default: false)",
                                "default": false
                            }
                        },
                        "required": ["old_string", "new_string"]
                    },
                    "minItems": 1
                }
            },
            "required": ["file_path", "edits"]
        })
    }
    
    async fn execute(
        &self,
        args: Value,
        ctx: ToolContext,
    ) -> Result<ToolResult, ToolError> {
        let params: MultiEditParams = serde_json::from_value(args)
            .map_err(|e| ToolError::InvalidParameters(e.to_string()))?;
        
        // Validate parameters
        if params.edits.is_empty() {
            return Err(ToolError::InvalidParameters(
                "At least one edit operation is required".to_string()
            ));
        }
        
        // Check for invalid edits
        for (i, edit) in params.edits.iter().enumerate() {
            if edit.old_string == edit.new_string {
                return Err(ToolError::InvalidParameters(format!(
                    "Edit operation {} has identical old_string and new_string", i
                )));
            }
        }
        
        // Resolve file path
        let path = if PathBuf::from(&params.file_path).is_absolute() {
            PathBuf::from(&params.file_path)
        } else {
            ctx.working_directory.join(&params.file_path)
        };
        
        // Create backup before any modifications
        let backup = self.create_backup(&path).await?;
        
        // Attempt to apply all edits
        match self.apply_edits_atomic(&path, &params.edits, &ctx).await {
            Ok(results) => {
                // Success - clean up backup and return results
                self.cleanup_backup(&backup).await.ok(); // Don't fail if cleanup fails
                self.format_success_result(&params.file_path, &backup.original_content, &path, results).await
            }
            Err(error) => {
                // Failure - restore from backup
                if let Err(restore_error) = self.restore_backup(&backup, &path).await {
                    return Err(ToolError::ExecutionFailed(format!(
                        "Edit failed: {}. Backup restoration also failed: {}", 
                        error, restore_error
                    )));
                }
                self.cleanup_backup(&backup).await.ok();
                Err(error)
            }
        }
    }
}

impl MultiEditTool {
    /// Create a backup of the file before modifications
    async fn create_backup(&self, path: &PathBuf) -> Result<FileBackup, ToolError> {
        let original_content = fs::read_to_string(path).await?;
        let backup_id = Uuid::new_v4().to_string();
        let backup_path = path.with_extension(format!("backup.{}", backup_id));
        
        // Write backup file
        fs::write(&backup_path, &original_content).await?;
        
        Ok(FileBackup {
            backup_id,
            original_content,
            backup_path,
        })
    }
    
    /// Apply all edits atomically - if any fail, return error
    async fn apply_edits_atomic(
        &self,
        path: &PathBuf,
        edits: &[EditOperation],
        ctx: &ToolContext,
    ) -> Result<Vec<EditResult>, ToolError> {
        let mut current_content = fs::read_to_string(path).await?;
        let mut results = Vec::new();
        
        // Replacement strategies to try
        let strategies: Vec<(&str, Box<dyn ReplacementStrategy + Send + Sync>)> = vec![
            ("simple", Box::new(SimpleReplacer)),
            ("line_trimmed", Box::new(LineTrimmedReplacer)),
            ("whitespace_normalized", Box::new(WhitespaceNormalizedReplacer)),
            ("indentation_flexible", Box::new(IndentationFlexibleReplacer)),
        ];
        
        // Apply each edit sequentially
        for (i, edit) in edits.iter().enumerate() {
            // Check for cancellation
            if *ctx.abort_signal.borrow() {
                return Err(ToolError::Aborted);
            }
            
            let mut found_replacement = false;
            let mut replacements = 0;
            let mut strategy_used = String::new();
            
            // Try each replacement strategy
            for (strategy_name, strategy) in &strategies {
                let result = strategy.replace(&current_content, &edit.old_string, &edit.new_string, edit.replace_all);
                if result.count > 0 {
                    current_content = result.content;
                    replacements = result.count;
                    strategy_used = strategy_name.to_string();
                    found_replacement = true;
                    break;
                }
            }
            
            if !found_replacement {
                return Err(ToolError::ExecutionFailed(format!(
                    "Edit operation {} failed: Could not find '{}' in file after {} previous edit(s)",
                    i,
                    edit.old_string.chars().take(100).collect::<String>(),
                    i
                )));
            }
            
            results.push(EditResult {
                operation_index: i,
                replacements,
                strategy_used,
                content_after: current_content.clone(),
            });
        }
        
        // All edits successful - write final content
        fs::write(path, &current_content).await?;
        
        Ok(results)
    }
    
    /// Restore file from backup
    async fn restore_backup(&self, backup: &FileBackup, path: &PathBuf) -> Result<(), ToolError> {
        fs::write(path, &backup.original_content).await?;
        Ok(())
    }
    
    /// Clean up backup file
    async fn cleanup_backup(&self, backup: &FileBackup) -> Result<(), ToolError> {
        if backup.backup_path.exists() {
            fs::remove_file(&backup.backup_path).await?;
        }
        Ok(())
    }
    
    /// Format successful result with comprehensive information
    async fn format_success_result(
        &self,
        file_path: &str,
        original_content: &str,
        final_path: &PathBuf,
        results: Vec<EditResult>,
    ) -> Result<ToolResult, ToolError> {
        let final_content = fs::read_to_string(final_path).await?;
        
        // Calculate total replacements
        let total_replacements: usize = results.iter().map(|r| r.replacements).sum();
        
        // Generate comprehensive diff
        let diff = TextDiff::from_lines(original_content, &final_content);
        let mut diff_output = String::new();
        let mut changes_count = 0;
        
        for change in diff.iter_all_changes() {
            match change.tag() {
                similar::ChangeTag::Delete => {
                    diff_output.push_str(&format!("- {}", change));
                    changes_count += 1;
                }
                similar::ChangeTag::Insert => {
                    diff_output.push_str(&format!("+ {}", change));
                    changes_count += 1;
                }
                similar::ChangeTag::Equal => {},
            }
        }
        
        // Create detailed metadata
        let edit_details: Vec<Value> = results.iter().map(|result| {
            json!({
                "operation_index": result.operation_index,
                "replacements": result.replacements,
                "strategy_used": result.strategy_used
            })
        }).collect();
        
        let metadata = json!({
            "path": final_path.to_string_lossy(),
            "total_operations": results.len(),
            "total_replacements": total_replacements,
            "operations_details": edit_details,
            "diff": diff_output,
            "diff_changes": changes_count,
            "atomic_transaction": true
        });
        
        let operations_summary = results.iter()
            .map(|r| format!("Op {}: {} replacement{} ({})", 
                r.operation_index, 
                r.replacements,
                if r.replacements == 1 { "" } else { "s" },
                r.strategy_used
            ))
            .collect::<Vec<_>>()
            .join(", ");
        
        Ok(ToolResult {
            title: format!(
                "Successfully completed {} edit operation{} with {} total replacement{} in {}",
                results.len(),
                if results.len() == 1 { "" } else { "s" },
                total_replacements,
                if total_replacements == 1 { "" } else { "s" },
                file_path
            ),
            metadata,
            output: format!(
                "Multi-edit completed successfully:\n\
                - File: {}\n\
                - Total operations: {}\n\
                - Total replacements: {}\n\
                - Operations: {}\n\
                - Atomic transaction: All edits applied successfully or rolled back on failure",
                file_path,
                results.len(),
                total_replacements,
                operations_summary
            ),
        })
    }
}

// Note: Replacement strategies are imported at the top of the file

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::NamedTempFile;
    use std::io::Write;
    
    #[tokio::test]
    async fn test_multiedit_atomic_success() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "Hello world\nThis is a test\nEnd of file").unwrap();
        let temp_path = temp_file.path().to_path_buf();
        
        let tool = MultiEditTool;
        let params = json!({
            "file_path": temp_path.to_string_lossy(),
            "edits": [
                {
                    "old_string": "Hello",
                    "new_string": "Hi",
                    "replace_all": false
                },
                {
                    "old_string": "test",
                    "new_string": "example",
                    "replace_all": false
                }
            ]
        });
        
        let ctx = ToolContext {
            session_id: "test".to_string(),
            message_id: "test".to_string(),
            abort_signal: tokio::sync::watch::channel(false).1,
            working_directory: std::env::current_dir().unwrap(),
        };
        
        let result = tool.execute(params, ctx).await.unwrap();
        assert!(result.title.contains("2 edit operation"));
        assert!(result.title.contains("2 total replacement"));
        
        let content = fs::read_to_string(&temp_path).await.unwrap();
        assert!(content.contains("Hi world"));
        assert!(content.contains("This is a example"));
    }
    
    #[tokio::test]
    async fn test_multiedit_atomic_failure_rollback() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "Hello world\nThis is a test\nEnd of file").unwrap();
        let temp_path = temp_file.path().to_path_buf();
        let original_content = fs::read_to_string(&temp_path).await.unwrap();
        
        let tool = MultiEditTool;
        let params = json!({
            "file_path": temp_path.to_string_lossy(),
            "edits": [
                {
                    "old_string": "Hello",
                    "new_string": "Hi",
                    "replace_all": false
                },
                {
                    "old_string": "nonexistent",
                    "new_string": "replacement",
                    "replace_all": false
                }
            ]
        });
        
        let ctx = ToolContext {
            session_id: "test".to_string(),
            message_id: "test".to_string(),
            abort_signal: tokio::sync::watch::channel(false).1,
            working_directory: std::env::current_dir().unwrap(),
        };
        
        let result = tool.execute(params, ctx).await;
        assert!(result.is_err());
        
        // Verify rollback - content should be unchanged
        let final_content = fs::read_to_string(&temp_path).await.unwrap();
        assert_eq!(original_content, final_content);
    }
    
    #[tokio::test]
    async fn test_multiedit_replace_all() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "test test test\nAnother test line").unwrap();
        let temp_path = temp_file.path().to_path_buf();
        
        let tool = MultiEditTool;
        let params = json!({
            "file_path": temp_path.to_string_lossy(),
            "edits": [
                {
                    "old_string": "test",
                    "new_string": "example",
                    "replace_all": true
                }
            ]
        });
        
        let ctx = ToolContext {
            session_id: "test".to_string(),
            message_id: "test".to_string(),
            abort_signal: tokio::sync::watch::channel(false).1,
            working_directory: std::env::current_dir().unwrap(),
        };
        
        let result = tool.execute(params, ctx).await.unwrap();
        assert!(result.title.contains("4 total replacement"));
        
        let content = fs::read_to_string(&temp_path).await.unwrap();
        assert_eq!(content, "example example example\nAnother example line\n");
    }
}