winx-code-agent 0.2.349

Remote-first Rust MCP runtime with Streamable HTTP, durable PTYs, and guarded agent tools
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
use schemars::JsonSchema;
use serde::Deserialize;
use serde_json::Value;

use super::domain::{
    validate_aggregate, EditChange, EditCommand, EditMode, EditOperation, EditSurface,
    EditVerification, NormalizedEditCall,
};
use crate::errors::{Result, WinxError};
use crate::tools::file_write_or_edit::uses_search_replace;
use crate::types::{ApplyPatch, FileWriteOrEdit, LinePatch, MultiFileEdit, UndoEdit};

#[derive(Clone, Debug, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct EditFilesWire {
    /// Optional explicit operation. Omit it for normal edits; exactly one `mode=undo`
    /// entry infers `undo`. An explicit value must agree with the listed modes.
    #[serde(default)]
    pub operation: Option<EditOperation>,
    /// One unique target per entry. All apply entries are validated before writing begins.
    #[schemars(length(min = 1, max = 100))]
    pub files: Vec<EditFileWire>,
    /// Optional finite foreground check run once after a successful apply.
    #[serde(default)]
    pub verify_command: Option<String>,
    /// Seconds to wait inline for `verify_command`, from 0 through 60.
    #[serde(default)]
    pub verify_wait_for_seconds: Option<f32>,
    /// Exact project-session identifier returned by `Initialize`.
    pub thread_id: String,
    /// Exact project identity returned alongside `thread_id` by `Initialize`.
    #[serde(default)]
    pub workspace_root: Option<String>,
}

#[derive(Clone, Debug, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct EditFileWire {
    /// Target path. Relative paths resolve from the session's current shell directory.
    pub file_path: String,
    /// `line_patch` is the default for an existing file after `ReadFiles`; copy its
    /// revision and visible coordinates. Reserve `search_replace` for intentional
    /// text anchoring copied exactly from the current read, use `replace` for new
    /// files or deliberate whole-file rewrites, and `undo` only as the sole entry
    /// with its exact `undo_id`.
    pub mode: EditMode,
    /// Required by `replace` and `search_replace`; forbidden by other modes. For
    /// `search_replace`, use one or more exact
    /// `<<<<<<< SEARCH\nold\n=======\nnew\n>>>>>>> REPLACE` blocks. An optional
    /// `@line` or `@start-end` may follow `SEARCH` to disambiguate repeated text.
    #[serde(default)]
    pub content: Option<String>,
    /// Exact `ReadFiles.files[].revision`; required only by `line_patch`.
    #[serde(default)]
    pub expected_revision: Option<String>,
    /// Ordered, non-overlapping, one-based changes; required only by `line_patch`.
    #[serde(default)]
    pub patches: Option<Vec<LinePatch>>,
    /// Exact receipt returned by the edit being undone; required only by `undo`.
    #[serde(default)]
    pub undo_id: Option<String>,
}

pub fn normalize_edit_call(surface: EditSurface, arguments: Value) -> Result<NormalizedEditCall> {
    let original_arguments = arguments.clone();
    if surface == EditSurface::EditFiles {
        let request: EditFilesWire = deserialize(arguments, surface.tool_name())?;
        return normalize_new_wire(request, original_arguments);
    }
    let workspace_root =
        arguments.get("workspace_root").and_then(Value::as_str).map(str::to_string);
    let verification = parse_verification(&arguments)?;
    let mut domain_arguments = arguments;
    if let Some(map) = domain_arguments.as_object_mut() {
        map.remove("workspace_root");
        map.remove("verify_command");
        map.remove("verify_wait_for_seconds");
    }

    let (surface, thread_id, command) =
        normalize_legacy_wire(surface, domain_arguments, verification.is_some())?;
    let changes = match &command {
        EditCommand::Apply { changes } => changes.as_slice(),
        EditCommand::Undo { .. } => &[],
    };
    validate_aggregate(changes)?;
    Ok(NormalizedEditCall {
        surface,
        command,
        verification,
        thread_id,
        workspace_root,
        original_arguments,
    })
}

fn normalize_legacy_wire(
    surface: EditSurface,
    arguments: Value,
    has_verification: bool,
) -> Result<(EditSurface, String, EditCommand)> {
    Ok(match surface {
        EditSurface::FileWriteOrEdit => {
            let request: FileWriteOrEdit = deserialize(arguments, surface.tool_name())?;
            let mode = if uses_search_replace(
                request.percentage_to_change,
                &request.text_or_search_replace_blocks,
            ) {
                EditMode::SearchReplace
            } else {
                EditMode::Replace
            };
            let change =
                text_change(mode, request.file_path, request.text_or_search_replace_blocks)?;
            (
                EditSurface::FileWriteOrEdit,
                request.thread_id,
                EditCommand::Apply { changes: vec![change] },
            )
        }
        EditSurface::MultiFileEdit => {
            let request: MultiFileEdit = deserialize(arguments, surface.tool_name())?;
            if request.files.len() < 2 {
                return Err(WinxError::ArgumentParseError(
                    "MultiFileEdit needs at least 2 files; use FileWriteOrEdit for a single file."
                        .to_string(),
                ));
            }
            if request.files.len() > 100 {
                return Err(WinxError::ArgumentParseError(format!(
                    "MultiFileEdit is limited to 100 files per batch (got {}); split the change into smaller batches.",
                    request.files.len()
                )));
            }
            let changes = request
                .files
                .into_iter()
                .map(|entry| {
                    let mode = if uses_search_replace(
                        entry.percentage_to_change,
                        &entry.text_or_search_replace_blocks,
                    ) {
                        EditMode::SearchReplace
                    } else {
                        EditMode::Replace
                    };
                    text_change(mode, entry.file_path, entry.text_or_search_replace_blocks)
                })
                .collect::<Result<Vec<_>>>()?;
            (EditSurface::MultiFileEdit, request.thread_id, EditCommand::Apply { changes })
        }
        EditSurface::ApplyPatch => {
            let request: ApplyPatch = deserialize(arguments, surface.tool_name())?;
            crate::tools::apply_patch::validate_revision(&request.expected_revision)?;
            crate::tools::apply_patch::validate_patch_count(&request.patches)?;
            let change = EditChange::LinePatch {
                file_path: request.file_path,
                expected_revision: request.expected_revision,
                patches: request.patches,
            };
            (
                EditSurface::ApplyPatch,
                request.thread_id,
                EditCommand::Apply { changes: vec![change] },
            )
        }
        EditSurface::UndoEdit => {
            if has_verification {
                return Err(WinxError::InvalidInput(
                    "UndoEdit does not accept verification fields".to_string(),
                ));
            }
            let request: UndoEdit = deserialize(arguments, surface.tool_name())?;
            (
                EditSurface::UndoEdit,
                request.thread_id,
                EditCommand::Undo {
                    file_path: request.file_path,
                    undo_id: None,
                    legacy_lifo: true,
                },
            )
        }
        EditSurface::EditFiles => unreachable!("EditFiles is normalized before legacy lowering"),
    })
}

fn normalize_new_wire(
    request: EditFilesWire,
    original_arguments: Value,
) -> Result<NormalizedEditCall> {
    if request.files.is_empty() || request.files.len() > 100 {
        return Err(WinxError::InvalidInput(
            "EditFiles files must contain between 1 and 100 entries".to_string(),
        ));
    }
    let verification =
        parse_verification_fields(request.verify_command, request.verify_wait_for_seconds)?;
    let operation = request.operation.unwrap_or_else(|| {
        if request.files.len() == 1 && request.files[0].mode == EditMode::Undo {
            EditOperation::Undo
        } else {
            EditOperation::Apply
        }
    });
    let command = match operation {
        EditOperation::Apply => {
            let changes = request
                .files
                .into_iter()
                .enumerate()
                .map(|(index, file)| apply_wire_change(index, file))
                .collect::<Result<Vec<_>>>()?;
            validate_aggregate(&changes)?;
            EditCommand::Apply { changes }
        }
        EditOperation::Undo => {
            if verification.is_some() {
                return Err(WinxError::InvalidInput(
                    "EditFiles verification fields are valid only for operation=apply".to_string(),
                ));
            }
            if request.files.len() != 1 {
                return Err(WinxError::InvalidInput(
                    "EditFiles operation=undo requires exactly one file".to_string(),
                ));
            }
            let file = request.files.into_iter().next().ok_or_else(|| {
                WinxError::InvalidInput("EditFiles undo target is missing".to_string())
            })?;
            if file.mode != EditMode::Undo {
                return Err(WinxError::InvalidInput(
                    "EditFiles operation=undo requires files[0].mode=undo".to_string(),
                ));
            }
            reject_payload_fields(&file, "undo")?;
            let undo_id = file.undo_id.filter(|id| !id.trim().is_empty()).ok_or_else(|| {
                WinxError::InvalidInput(
                    "EditFiles operation=undo requires the exact non-empty undo_id returned by the edit"
                        .to_string(),
                )
            })?;
            EditCommand::Undo {
                file_path: file.file_path,
                undo_id: Some(undo_id),
                legacy_lifo: false,
            }
        }
    };
    Ok(NormalizedEditCall {
        surface: EditSurface::EditFiles,
        command,
        verification,
        thread_id: request.thread_id,
        workspace_root: request.workspace_root,
        original_arguments,
    })
}

fn apply_wire_change(index: usize, file: EditFileWire) -> Result<EditChange> {
    if file.mode == EditMode::Undo {
        return Err(WinxError::InvalidInput(format!(
            "files[{index}].mode=undo requires operation=undo and cannot be mixed with apply"
        )));
    }
    if file.undo_id.is_some() {
        return Err(WinxError::InvalidInput(format!(
            "files[{index}].undo_id is valid only for mode=undo"
        )));
    }
    match file.mode {
        EditMode::Replace | EditMode::SearchReplace => {
            if file.expected_revision.is_some() || file.patches.is_some() {
                return Err(WinxError::InvalidInput(format!(
                    "files[{index}] text modes accept content only"
                )));
            }
            let content = file.content.ok_or_else(|| {
                WinxError::InvalidInput(format!("files[{index}].content is required"))
            })?;
            text_change(file.mode, file.file_path, content)
        }
        EditMode::LinePatch => {
            if file.content.is_some() {
                return Err(WinxError::InvalidInput(format!(
                    "files[{index}].content is not valid for line_patch"
                )));
            }
            let expected_revision = file.expected_revision.ok_or_else(|| {
                WinxError::InvalidInput(format!(
                    "files[{index}].expected_revision is required for line_patch"
                ))
            })?;
            let patches = file.patches.ok_or_else(|| {
                WinxError::InvalidInput(format!(
                    "files[{index}].patches is required for line_patch"
                ))
            })?;
            crate::tools::apply_patch::validate_revision(&expected_revision)?;
            crate::tools::apply_patch::validate_patch_count(&patches)?;
            Ok(EditChange::LinePatch { file_path: file.file_path, expected_revision, patches })
        }
        EditMode::Undo => unreachable!("handled above"),
    }
}

fn reject_payload_fields(file: &EditFileWire, mode: &str) -> Result<()> {
    if file.content.is_some() || file.expected_revision.is_some() || file.patches.is_some() {
        return Err(WinxError::InvalidInput(format!(
            "mode={mode} does not accept content, expected_revision, or patches"
        )));
    }
    Ok(())
}

fn text_change(mode: EditMode, file_path: String, content: String) -> Result<EditChange> {
    match mode {
        EditMode::Replace => Ok(EditChange::Replace { file_path, content }),
        EditMode::SearchReplace => Ok(EditChange::SearchReplace { file_path, content }),
        _ => Err(WinxError::InvalidInput("invalid text edit mode".to_string())),
    }
}

fn parse_verification(arguments: &Value) -> Result<Option<EditVerification>> {
    let command = arguments
        .get("verify_command")
        .filter(|value| !value.is_null())
        .map(|value| {
            value.as_str().map(str::to_string).ok_or_else(|| {
                WinxError::InvalidInput("verify_command must be a string".to_string())
            })
        })
        .transpose()?;
    let wait = arguments
        .get("verify_wait_for_seconds")
        .filter(|value| !value.is_null())
        .map(|value| {
            serde_json::from_value::<f32>(value.clone()).map_err(|error| {
                WinxError::InvalidInput(format!("invalid verify_wait_for_seconds: {error}"))
            })
        })
        .transpose()?;
    parse_verification_fields(command, wait)
}

fn parse_verification_fields(
    command: Option<String>,
    wait_for_seconds: Option<f32>,
) -> Result<Option<EditVerification>> {
    let command = command.map(|command| command.trim().to_string());
    match command {
        Some(command) if command.is_empty() => {
            Err(WinxError::InvalidInput("verify_command must not be empty".to_string()))
        }
        Some(_command)
            if wait_for_seconds
                .is_some_and(|wait| !wait.is_finite() || !(0.0..=60.0).contains(&wait)) =>
        {
            Err(WinxError::InvalidInput(
                "verify_wait_for_seconds must be between 0 and 60".to_string(),
            ))
        }
        Some(command) => Ok(Some(EditVerification { command, wait_for_seconds })),
        None if wait_for_seconds.is_some() => Err(WinxError::InvalidInput(
            "verify_wait_for_seconds requires verify_command".to_string(),
        )),
        None => Ok(None),
    }
}

fn deserialize<T: serde::de::DeserializeOwned>(value: Value, tool: &str) -> Result<T> {
    serde_json::from_value(value)
        .map_err(|error| WinxError::InvalidInput(format!("Invalid {tool} parameters: {error}")))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn unified_wire_keeps_explicit_workspace_and_verification_fields() -> Result<()> {
        let normalized = normalize_edit_call(
            EditSurface::EditFiles,
            serde_json::json!({
                "operation": "apply",
                "files": [{
                    "file_path": "/workspace/file.rs",
                    "mode": "replace",
                    "content": "fn main() {}"
                }],
                "verify_command": " cargo check ",
                "verify_wait_for_seconds": 12.5,
                "thread_id": "thread",
                "workspace_root": "/workspace"
            }),
        )?;
        assert_eq!(normalized.workspace_root.as_deref(), Some("/workspace"));
        let verification = normalized.verification.ok_or_else(|| {
            WinxError::InvalidInput("expected normalized verification".to_string())
        })?;
        assert_eq!(verification.command, "cargo check");
        assert_eq!(verification.wait_for_seconds, Some(12.5));
        Ok(())
    }

    #[test]
    fn unified_wire_rejects_unknown_entry_fields_and_mixed_undo() {
        let unknown = serde_json::json!({
            "operation": "apply",
            "files": [{
                "file_path": "/workspace/file.rs",
                "mode": "replace",
                "content": "content",
                "percentage_to_change": 100
            }],
            "thread_id": "thread"
        });
        assert!(normalize_edit_call(EditSurface::EditFiles, unknown).is_err());

        let mixed = serde_json::json!({
            "operation": "apply",
            "files": [{
                "file_path": "/workspace/file.rs",
                "mode": "undo",
                "undo_id": "undo_123"
            }],
            "thread_id": "thread"
        });
        assert!(normalize_edit_call(EditSurface::EditFiles, mixed).is_err());
    }

    #[test]
    fn unified_wire_infers_common_apply_and_single_undo_operations() -> Result<()> {
        let apply = normalize_edit_call(
            EditSurface::EditFiles,
            serde_json::json!({
                "files": [{
                    "file_path": "/workspace/new.rs",
                    "mode": "replace",
                    "content": "fn main() {}"
                }],
                "thread_id": "thread"
            }),
        )?;
        assert_eq!(apply.command.operation(), EditOperation::Apply);

        let undo = normalize_edit_call(
            EditSurface::EditFiles,
            serde_json::json!({
                "files": [{
                    "file_path": "/workspace/new.rs",
                    "mode": "undo",
                    "undo_id": "undo_123"
                }],
                "thread_id": "thread"
            }),
        )?;
        assert_eq!(undo.command.operation(), EditOperation::Undo);
        Ok(())
    }

    #[test]
    fn legacy_marker_heuristic_does_not_leak_into_explicit_modes() -> Result<()> {
        let content = "<<<<<<< SEARCH\nold\n=======\nnew\n>>>>>>> REPLACE\n";
        let legacy = normalize_edit_call(
            EditSurface::FileWriteOrEdit,
            serde_json::json!({
                "file_path": "/workspace/file.rs",
                "percentage_to_change": 100,
                "text_or_search_replace_blocks": content,
                "thread_id": "thread"
            }),
        )?;
        let explicit = normalize_edit_call(
            EditSurface::EditFiles,
            serde_json::json!({
                "operation": "apply",
                "files": [{
                    "file_path": "/workspace/file.rs",
                    "mode": "replace",
                    "content": content
                }],
                "thread_id": "thread"
            }),
        )?;
        assert_eq!(legacy.command.modes(), vec![EditMode::SearchReplace]);
        assert_eq!(explicit.command.modes(), vec![EditMode::Replace]);
        Ok(())
    }
}