winx-code-agent 0.2.338

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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
use rmcp::model::{CallToolResult, ContentBlock};
use rmcp::ErrorData as McpError;
use schemars::{JsonSchema, Schema, SchemaGenerator};
use serde::Serialize;
use serde_json::{json, Map, Value};

use crate::errors::WinxError;
use crate::runtime::BashCommandRuntimeResult;
use crate::state::turn::TurnState;
use crate::tools::bash_command::BashCommandState;

/// Machine-readable state of a completed Winx tool invocation.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub(super) enum ToolResultStatus {
    Completed,
    Running,
    AwaitingInput,
    AwaitingApproval,
    NeedsRead,
    NeedsInitialize,
    Conflict,
    NotFound,
    Denied,
    InvalidInput,
    Failed,
}

/// A concrete next tool call, or an instruction when arguments cannot be safely inferred.
#[derive(Clone, Debug, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub(super) struct ToolNextAction {
    pub tool: String,
    pub instruction: String,
    #[schemars(schema_with = "open_object_schema")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub arguments: Option<Value>,
}

/// File coverage that must be refreshed before retrying an edit.
#[derive(Clone, Debug, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub(super) struct RequiredRead {
    pub path: String,
    pub ranges: Vec<String>,
}

/// Shared structured output advertised by Winx tools.
///
/// Text content remains the backwards-compatible human-readable result. This
/// object gives an LLM an unambiguous state transition and safe next action.
#[derive(Clone, Debug, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub(super) struct ToolResultEnvelope {
    pub status: ToolResultStatus,
    pub tool: String,
    pub message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_code: Option<String>,
    pub retryable: bool,
    pub retry_same_call: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub retry_after_ms: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next_action: Option<ToolNextAction>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub required_reads: Vec<RequiredRead>,
    #[schemars(schema_with = "open_object_schema")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<Value>,
}

/// Winx only emits object payloads in these extensible result fields. Avoid
/// schemars' boolean `true` schema for `serde_json::Value`: it is valid JSON
/// Schema, but some MCP clients reject boolean schemas while compiling tools.
fn open_object_schema(_: &mut SchemaGenerator) -> Schema {
    schemars::json_schema!({ "type": "object" })
}

/// `CodeMap` keeps its established top-level fields while adding the shared
/// orchestration metadata. Every code-navigation field is optional because the
/// same schema must also describe a tool-level error result.
#[allow(dead_code)] // schema-only shape consumed by schemars in the MCP catalog
#[derive(Clone, Debug, JsonSchema)]
pub(super) struct CodeMapToolResultEnvelope {
    #[schemars(flatten)]
    pub result: ToolResultEnvelope,
    pub truncated: Option<bool>,
    pub mode: Option<String>,
    pub files_shown: Option<usize>,
    pub files: Option<Vec<crate::types::OutlineFile>>,
    pub name: Option<String>,
    pub definitions: Option<usize>,
    pub references: Option<usize>,
    pub hits: Option<Vec<crate::types::ReferenceHit>>,
}

/// Convert a domain execution failure into a caller-visible MCP tool error.
/// Only failures that prevent the server from producing a valid tool result use
/// JSON-RPC errors; normal execution failures belong in `CallToolResult.isError`.
pub(super) fn tool_failure(
    tool: &str,
    error: &WinxError,
    arguments: Option<&Value>,
) -> Result<CallToolResult, McpError> {
    if matches!(error, WinxError::SerializationError(_) | WinxError::BashStateLockError(_)) {
        return Err(McpError::internal_error(format!("{tool} failed: {error}"), None));
    }

    let text = format!("{tool} failed: {error}");
    let envelope = error_envelope(tool, error, arguments, text.clone());
    let mut result = CallToolResult::error(vec![ContentBlock::text(text)]);
    result.structured_content = serde_json::to_value(envelope).ok();
    Ok(result)
}

/// Attach orchestration metadata to a successful non-Bash result without
/// changing its existing text/image content. `BashCommand` is decorated separately
/// from its runtime-owned typed state.
pub(super) fn decorate_success(tool: &str, arguments: Option<&Value>, result: &mut CallToolResult) {
    if result.is_error == Some(true) || tool == "BashCommand" {
        return;
    }
    if result.structured_content.as_ref().is_some_and(|structured| {
        structured.get("tool").and_then(Value::as_str) == Some(tool)
            && structured.get("status").and_then(Value::as_str).is_some()
    }) {
        return;
    }

    let existing = result.structured_content.take();
    let text = result_text(result);
    let mut data = safe_success_data(tool, arguments, &text, false);
    if let Some(existing) = existing.as_ref() {
        if tool == "Initialize" {
            if let Value::Object(metadata) = existing {
                for (key, value) in metadata {
                    data.entry(key.clone()).or_insert_with(|| value.clone());
                }
            } else {
                data.insert("result".to_string(), existing.clone());
            }
        } else {
            data.insert("result".to_string(), existing.clone());
        }
    }

    let envelope = ToolResultEnvelope {
        status: ToolResultStatus::Completed,
        tool: tool.to_string(),
        message: success_message(tool, ToolResultStatus::Completed),
        error_code: None,
        retryable: false,
        retry_same_call: false,
        retry_after_ms: None,
        next_action: None,
        required_reads: Vec::new(),
        data: (!data.is_empty()).then_some(Value::Object(data)),
    };

    let Ok(mut value) = serde_json::to_value(envelope) else {
        result.structured_content = existing;
        return;
    };
    if tool == "CodeMap" {
        if let (Value::Object(target), Some(Value::Object(source))) = (&mut value, existing) {
            for (key, item) in source {
                target.entry(key).or_insert(item);
            }
        }
    }
    result.structured_content = Some(value);
}

/// Combine an already-applied edit with its optional foreground verification.
/// The command remains a nested Bash result, while the outer status makes it
/// explicit that a failed verification does not roll the edit back.
pub(super) fn edit_verification_result(
    tool: &str,
    arguments: Option<&Value>,
    edit_text: &str,
    verification: CallToolResult,
) -> CallToolResult {
    let verification_text = result_text(&verification);
    let verification_is_error = verification.is_error == Some(true);
    let nested = verification.structured_content.unwrap_or_else(|| {
        json!({
            "status": if verification_is_error { "failed" } else { "completed" },
            "tool": "BashCommand",
            "message": "Verification returned no structured result."
        })
    });
    let nested_status = nested.get("status").and_then(Value::as_str).unwrap_or("failed");
    let exit_code =
        nested.get("data").and_then(|data| data.get("exit_code")).and_then(Value::as_i64);
    let nonzero_exit = exit_code.is_some_and(|code| code != 0);
    let verification_error = verification_is_error || nonzero_exit;
    let active = matches!(nested_status, "running" | "awaiting_input" | "awaiting_approval");
    let outer_status = if nonzero_exit {
        "failed"
    } else if active || verification_error {
        nested_status
    } else {
        "completed"
    };
    let message = if verification_error {
        format!("{tool} applied the edit, but verification failed; the edit was not rolled back.")
    } else if active {
        format!("{tool} applied the edit; verification is still {outer_status}.")
    } else {
        format!("{tool} applied the edit and verification completed.")
    };
    let verification_summary = if verification_error {
        exit_code.map_or_else(
            || "Verification failed; the edit remains applied.".to_string(),
            |code| format!("Verification failed with exit code {code}; the edit remains applied."),
        )
    } else if active {
        format!("Verification is still {outer_status}.")
    } else {
        exit_code.map_or_else(
            || "Verification completed.".to_string(),
            |code| format!("Verification completed with exit code {code}."),
        )
    };
    let combined_text = if verification_text.trim().is_empty() {
        format!("{edit_text}\n\n{verification_summary}")
    } else {
        format!("{edit_text}\n\n{verification_summary}\n{verification_text}")
    };

    let mut data = safe_success_data(tool, arguments, &combined_text, false);
    data.insert("edit_applied".to_string(), Value::Bool(true));
    data.insert("verification".to_string(), nested.clone());
    if let Some(exit_code) = exit_code {
        data.insert("verification_exit_code".to_string(), json!(exit_code));
    }

    let mut envelope = json!({
        "status": outer_status,
        "tool": tool,
        "message": message,
        "retryable": nested.get("retryable").and_then(Value::as_bool).unwrap_or(false),
        "retrySameCall": false,
        "requiredReads": [],
        "data": data,
    });
    if verification_error {
        let nested_code =
            nested.get("errorCode").and_then(Value::as_str).unwrap_or("execution_failed");
        envelope["errorCode"] = Value::String(if nonzero_exit {
            "verification_failed".to_string()
        } else {
            format!("verification_{nested_code}")
        });
    }
    for key in ["retryAfterMs", "nextAction"] {
        if let Some(value) = nested.get(key) {
            envelope[key] = value.clone();
        }
    }

    let mut result = if verification_error {
        CallToolResult::error(vec![ContentBlock::text(combined_text)])
    } else {
        CallToolResult::success(vec![ContentBlock::text(combined_text)])
    };
    result.structured_content = Some(envelope);
    result
}

/// Build a `BashCommand` `CallToolResult` exclusively from runtime-owned state.
/// Terminal text is preserved for humans but never parsed for orchestration.
pub(super) fn bash_success_result(
    arguments: Option<&Value>,
    outcome: BashCommandRuntimeResult,
    compact_output: bool,
) -> Result<CallToolResult, McpError> {
    let status = bash_success_status(&outcome.result.state);
    let output_truncated = outcome.output_truncated;
    let next_action = bash_next_action(status, arguments, &outcome.result.state);
    let compact_rendered = compact_output && outcome.compact_output.is_some();
    let rendered_output = if compact_output {
        outcome.compact_output.unwrap_or(outcome.result.output)
    } else {
        outcome.result.output
    };
    let mut data = safe_success_data("BashCommand", arguments, &rendered_output, output_truncated);
    if compact_rendered {
        data.insert("output_format".to_string(), Value::String("compact".to_string()));
    }
    data.insert("cwd".to_string(), Value::String(outcome.result.state.cwd.display().to_string()));
    if let Some(value) = outcome.result.state.exit_code {
        data.insert("exit_code".to_string(), json!(value));
    }
    if let Some(value) = outcome.result.state.background_id.as_ref() {
        data.insert("bg_command_id".to_string(), Value::String(value.clone()));
    }
    if let Some(value) = outcome.result.state.running_for_seconds {
        data.insert("running_for_seconds".to_string(), json!(value));
        data.insert("running_for".to_string(), Value::String(format!("{value} seconds")));
    }
    if let Some(value) = outcome.result.state.turn_state {
        data.insert("turn_state".to_string(), Value::String(value.as_str().to_string()));
    }
    let envelope = ToolResultEnvelope {
        status,
        tool: "BashCommand".to_string(),
        message: success_message("BashCommand", status),
        error_code: None,
        retryable: false,
        retry_same_call: false,
        retry_after_ms: (status == ToolResultStatus::Running).then_some(1_000),
        next_action,
        required_reads: Vec::new(),
        data: (!data.is_empty()).then_some(Value::Object(data)),
    };
    let structured_content = serde_json::to_value(envelope).map_err(|error| {
        McpError::internal_error(
            format!("failed to serialize typed BashCommand result: {error}"),
            None,
        )
    })?;
    let mut result = CallToolResult::success(vec![ContentBlock::text(rendered_output)]);
    result.structured_content = Some(structured_content);
    Ok(result)
}

pub(super) fn result_status(result: &CallToolResult) -> String {
    result
        .structured_content
        .as_ref()
        .and_then(|value| value.get("status"))
        .and_then(Value::as_str)
        .unwrap_or(if result.is_error == Some(true) { "failed" } else { "completed" })
        .to_string()
}

pub(super) fn result_size_bytes(result: &CallToolResult) -> usize {
    let content = result
        .content
        .iter()
        .map(|block| {
            block
                .as_text()
                .map_or(0, |text| text.text.len())
                .saturating_add(block.as_image().map_or(0, |image| image.data.len()))
        })
        .fold(0usize, usize::saturating_add);
    content.saturating_add(
        result
            .structured_content
            .as_ref()
            .and_then(|value| serde_json::to_vec(value).ok())
            .map_or(0, |value| value.len()),
    )
}

fn result_text(result: &CallToolResult) -> String {
    result
        .content
        .iter()
        .filter_map(|content| content.as_text().map(|text| text.text.as_str()))
        .collect::<Vec<_>>()
        .join("\n")
}

fn bash_success_status(state: &BashCommandState) -> ToolResultStatus {
    if !state.is_running() {
        return ToolResultStatus::Completed;
    }
    match state.turn_state {
        Some(TurnState::AwaitingApproval) => ToolResultStatus::AwaitingApproval,
        Some(TurnState::AwaitingInput) => ToolResultStatus::AwaitingInput,
        Some(TurnState::Busy | TurnState::Unknown) | None => ToolResultStatus::Running,
    }
}

fn success_message(tool: &str, status: ToolResultStatus) -> String {
    match status {
        ToolResultStatus::Running => format!("{tool} is still running."),
        ToolResultStatus::AwaitingInput => format!("{tool} is waiting for input."),
        ToolResultStatus::AwaitingApproval => format!("{tool} is waiting for approval."),
        _ => format!("{tool} completed."),
    }
}

fn bash_next_action(
    status: ToolResultStatus,
    arguments: Option<&Value>,
    bash_state: &BashCommandState,
) -> Option<ToolNextAction> {
    let thread_id = string_argument(arguments, "thread_id");
    match status {
        ToolResultStatus::Running => {
            let mut action = json!({
                "action_json": {
                    "type": "status_check",
                    "status_check": true
                }
            });
            if let Some(thread_id) = thread_id {
                action["thread_id"] = Value::String(thread_id);
            }
            if let Some(background_id) = bash_state.background_id.as_ref() {
                action["action_json"]["bg_command_id"] = Value::String(background_id.clone());
            }
            Some(ToolNextAction {
                tool: "BashCommand".to_string(),
                instruction: "Wait for retry_after_ms, then check status. Do not repeat the original command."
                    .to_string(),
                arguments: Some(action),
            })
        }
        ToolResultStatus::AwaitingInput => Some(ToolNextAction {
            tool: "BashCommand".to_string(),
            instruction: "Inspect the returned screen, then use send_text or send_specials with the required input."
                .to_string(),
            arguments: None,
        }),
        ToolResultStatus::AwaitingApproval => Some(ToolNextAction {
            tool: "BashCommand".to_string(),
            instruction: "Do not approve automatically. Ask the user when approval changes permissions, data, or system state."
                .to_string(),
            arguments: None,
        }),
        _ => None,
    }
}

fn safe_success_data(
    tool: &str,
    arguments: Option<&Value>,
    text: &str,
    output_truncated: bool,
) -> Map<String, Value> {
    let mut data = Map::new();
    data.insert("output_bytes".to_string(), json!(text.len()));
    data.insert("output_truncated".to_string(), json!(output_truncated));

    for key in [
        "thread_id",
        "file_path",
        "operation",
        "path",
        "name",
        "id",
        "mode_name",
        "any_workspace_path",
    ] {
        if let Some(value) = string_argument(arguments, key) {
            data.insert(key.to_string(), Value::String(value));
        }
    }
    if let Some(files) = arguments
        .and_then(Value::as_object)
        .and_then(|arguments| arguments.get("file_paths"))
        .and_then(Value::as_array)
    {
        data.insert(
            "file_paths".to_string(),
            Value::Array(
                files.iter().filter_map(Value::as_str).map(|value| json!(value)).collect(),
            ),
        );
    }
    if tool == "MultiFileEdit" {
        if let Some(files) = arguments
            .and_then(Value::as_object)
            .and_then(|arguments| arguments.get("files"))
            .and_then(Value::as_array)
        {
            data.insert(
                "file_paths".to_string(),
                Value::Array(
                    files
                        .iter()
                        .filter_map(|file| file.get("file_path"))
                        .filter_map(Value::as_str)
                        .map(|value| json!(value))
                        .collect(),
                ),
            );
        }
    }
    if tool == "BashCommand" {
        if let Some(action) = bash_action(arguments) {
            data.insert("action".to_string(), Value::String(action));
        }
    }
    data
}

#[allow(clippy::too_many_lines)] // exhaustive error taxonomy is clearer in one match
fn error_envelope(
    tool: &str,
    error: &WinxError,
    arguments: Option<&Value>,
    text: String,
) -> ToolResultEnvelope {
    let mut status = ToolResultStatus::Failed;
    let mut error_code = "execution_failed".to_string();
    let mut retryable = false;
    let mut retry_after_ms = None;
    let mut next_action = None;
    let mut required_reads = Vec::new();

    match error {
        WinxError::BashStateNotInitialized => {
            status = ToolResultStatus::NeedsInitialize;
            error_code = "not_initialized".to_string();
            retryable = true;
            next_action = Some(ToolNextAction {
                tool: "Initialize".to_string(),
                instruction: "Initialize the intended workspace once, preserve the returned thread_id, then retry."
                    .to_string(),
                arguments: None,
            });
        }
        WinxError::CommandAlreadyRunning { .. } => {
            status = ToolResultStatus::Running;
            error_code = "command_already_running".to_string();
            retryable = true;
            retry_after_ms = Some(1_000);
            next_action = Some(status_check_action(arguments));
        }
        WinxError::FileAccessError { path, message } => {
            classify_file_error(
                path.to_string_lossy().as_ref(),
                message,
                arguments,
                &mut status,
                &mut error_code,
                &mut retryable,
                &mut next_action,
                &mut required_reads,
            );
        }
        WinxError::SearchBlockNotFound(_) | WinxError::SearchBlockAmbiguous { .. } => {
            status = ToolResultStatus::Conflict;
            error_code = if matches!(error, WinxError::SearchBlockAmbiguous { .. }) {
                "search_block_ambiguous".to_string()
            } else {
                "search_block_not_found".to_string()
            };
            retryable = true;
            if let Some(path) = string_argument(arguments, "file_path") {
                required_reads.push(RequiredRead { path: path.clone(), ranges: Vec::new() });
                next_action = read_action(arguments, &path, &[]);
            }
        }
        WinxError::MultiFilePlanError { path, source, .. } => {
            status = ToolResultStatus::Conflict;
            error_code = if matches!(source.as_ref(), WinxError::SearchBlockAmbiguous { .. }) {
                "search_block_ambiguous".to_string()
            } else {
                "search_block_not_found".to_string()
            };
            retryable = true;
            let path = path.to_string_lossy().into_owned();
            required_reads.push(RequiredRead { path: path.clone(), ranges: Vec::new() });
            next_action = read_action(arguments, &path, &[]);
        }
        WinxError::PathSecurityError { .. } | WinxError::CommandNotAllowed(_) => {
            status = ToolResultStatus::Denied;
            error_code = "operation_denied".to_string();
        }
        WinxError::WorkspacePathError(_) | WinxError::BackgroundSessionNotFound(_) => {
            status = ToolResultStatus::NotFound;
            error_code = "target_not_found".to_string();
        }
        WinxError::ThreadIdMismatch(_) => {
            status = ToolResultStatus::Conflict;
            error_code = "thread_id_mismatch".to_string();
        }
        WinxError::ParameterValidationError { .. }
        | WinxError::MissingParameterError { .. }
        | WinxError::NullValueError { .. }
        | WinxError::ArgumentParseError(_)
        | WinxError::JsonParseError(_)
        | WinxError::DeserializationError(_)
        | WinxError::InvalidInput(_)
        | WinxError::ParseError(_)
        | WinxError::SearchReplaceSyntaxError(_)
        | WinxError::SearchReplaceSyntaxErrorDetailed { .. }
        | WinxError::EmptyInteractiveInput { .. }
        | WinxError::InteractiveTargetNotRunning(_)
        | WinxError::NoActiveCommand(_) => {
            status = ToolResultStatus::InvalidInput;
            error_code = "invalid_tool_input".to_string();
            retryable = true;
        }
        WinxError::RecoverableSuggestionError { .. } => {
            retryable = true;
            error_code = "recoverable_failure".to_string();
        }
        WinxError::CommandTimeout { timeout_seconds, .. } => {
            error_code = "command_timeout".to_string();
            retryable = true;
            retry_after_ms = Some(timeout_seconds.saturating_mul(1_000));
        }
        _ => {}
    }

    let mut data = Map::new();
    if let Some(thread_id) = string_argument(arguments, "thread_id") {
        data.insert("thread_id".to_string(), Value::String(thread_id));
    }
    ToolResultEnvelope {
        status,
        tool: tool.to_string(),
        message: text,
        error_code: Some(error_code),
        retryable,
        retry_same_call: false,
        retry_after_ms,
        next_action,
        required_reads,
        data: (!data.is_empty()).then_some(Value::Object(data)),
    }
}

#[allow(clippy::too_many_arguments)]
fn classify_file_error(
    path: &str,
    message: &str,
    arguments: Option<&Value>,
    status: &mut ToolResultStatus,
    error_code: &mut String,
    retryable: &mut bool,
    next_action: &mut Option<ToolNextAction>,
    required_reads: &mut Vec<RequiredRead>,
) {
    let lower = message.to_ascii_lowercase();
    if lower.contains("hasn't been read")
        || lower.contains("changed on disk since you last read")
        || lower.contains("read more of the file")
    {
        *status = ToolResultStatus::NeedsRead;
        *error_code = "read_required".to_string();
        *retryable = true;
        let ranges = unread_ranges(message);
        required_reads.push(RequiredRead { path: path.to_string(), ranges: ranges.clone() });
        *next_action = read_action(arguments, path, &ranges);
    } else if lower.contains("changed since its last winx edit") {
        *status = ToolResultStatus::Conflict;
        *error_code = "file_changed_after_edit".to_string();
        *retryable = true;
        required_reads.push(RequiredRead { path: path.to_string(), ranges: Vec::new() });
        *next_action = read_action(arguments, path, &[]);
    } else if lower.contains("does not exist")
        || lower.contains("path not found")
        || lower.contains("no undo checkpoint")
    {
        *status = ToolResultStatus::NotFound;
        *error_code = "file_not_found".to_string();
    } else if lower.contains("not allowed") || lower.contains("permission denied") {
        *status = ToolResultStatus::Denied;
        *error_code = "file_operation_denied".to_string();
    }
}

fn status_check_action(arguments: Option<&Value>) -> ToolNextAction {
    let mut value = json!({
        "action_json": {
            "type": "status_check",
            "status_check": true
        }
    });
    if let Some(thread_id) = string_argument(arguments, "thread_id") {
        value["thread_id"] = Value::String(thread_id);
    }
    ToolNextAction {
        tool: "BashCommand".to_string(),
        instruction: "Check the running command instead of submitting the original command again."
            .to_string(),
        arguments: Some(value),
    }
}

fn read_action(arguments: Option<&Value>, path: &str, ranges: &[String]) -> Option<ToolNextAction> {
    if path.is_empty() {
        return None;
    }
    let file_paths = if ranges.is_empty() {
        vec![Value::String(path.to_string())]
    } else {
        ranges.iter().map(|range| Value::String(format!("{path}:{range}"))).collect()
    };
    let mut value = json!({"file_paths": file_paths});
    if let Some(thread_id) = string_argument(arguments, "thread_id") {
        value["thread_id"] = Value::String(thread_id);
    }
    Some(ToolNextAction {
        tool: "ReadFiles".to_string(),
        instruction: "Perform every required read before retrying the edit. Do not retry the same edit unchanged first."
            .to_string(),
        arguments: Some(value),
    })
}

fn unread_ranges(message: &str) -> Vec<String> {
    message
        .split_once("Unread line ranges:")
        .map(|(_, ranges)| {
            ranges
                .split(',')
                .map(str::trim)
                .filter(|range| !range.is_empty())
                .map(ToString::to_string)
                .collect()
        })
        .unwrap_or_default()
}

fn bash_action(arguments: Option<&Value>) -> Option<String> {
    let arguments = arguments?.as_object()?;
    let fallback = Value::Object(arguments.clone());
    let action = arguments.get("action_json").unwrap_or(&fallback);
    match action {
        Value::Object(action) => {
            action.get("type").and_then(Value::as_str).map(ToString::to_string).or_else(|| {
                [
                    "command",
                    "status_check",
                    "send_text",
                    "send_specials",
                    "send_ascii",
                    "screen",
                    "wait_for_turn",
                ]
                .into_iter()
                .find(|kind| action.contains_key(*kind))
                .map(ToString::to_string)
            })
        }
        Value::String(_) => Some("command".to_string()),
        _ => None,
    }
}

fn string_argument(arguments: Option<&Value>, key: &str) -> Option<String> {
    arguments?
        .as_object()?
        .get(key)?
        .as_str()
        .filter(|value| !value.is_empty())
        .map(ToString::to_string)
}

#[cfg(test)]
mod tests {
    #![allow(clippy::expect_used)]
    use super::*;

    #[test]
    fn initialize_metadata_is_flattened_into_the_shared_data_envelope() {
        let mut result = CallToolResult::success(vec![ContentBlock::text("attached")]);
        result.structured_content = Some(json!({
            "initialize_transition": "attached_existing",
            "initialize_reused": true,
            "initialize_response_mode": "compact",
            "context_bytes": 0
        }));
        decorate_success(
            "Initialize",
            Some(&json!({"thread_id":"thread","mode_name":"wcgw"})),
            &mut result,
        );

        let structured = result.structured_content.expect("structured Initialize result");
        assert_eq!(structured["status"], "completed");
        assert_eq!(structured["data"]["initialize_transition"], "attached_existing");
        assert_eq!(structured["data"]["initialize_reused"], true);
        assert_eq!(structured["data"]["initialize_response_mode"], "compact");
        assert!(structured["data"].get("result").is_none());
    }

    #[test]
    fn unread_file_error_provides_exact_read_call() {
        let error = WinxError::FileAccessError {
            path: "/workspace/README.md".into(),
            message: "Read more of the file before overwriting. Unread line ranges: 20-40, 90-"
                .to_string(),
        };
        let result = tool_failure(
            "FileWriteOrEdit",
            &error,
            Some(&json!({"thread_id":"thread","file_path":"/workspace/README.md"})),
        )
        .expect("tool-level error");
        assert_eq!(result.is_error, Some(true));
        let structured = result.structured_content.expect("structured error");
        assert_eq!(structured["status"], "needs_read");
        assert_eq!(structured["retrySameCall"], false);
        assert_eq!(
            structured["nextAction"]["arguments"]["file_paths"],
            json!(["/workspace/README.md:20-40", "/workspace/README.md:90-"])
        );
    }

    #[test]
    fn running_command_success_points_to_status_check() {
        let result = bash_success_result(
            Some(&json!({"thread_id":"thread","action_json":{"type":"command","command":"test"}})),
            BashCommandRuntimeResult {
                result: crate::tools::bash_command::BashCommandResult {
                    output: "build output with arbitrary text".to_string(),
                    state: BashCommandState {
                        process_status: crate::tools::bash_command::BashProcessStatus::Running,
                        background_id: None,
                        running_for_seconds: Some(1),
                        exit_code: None,
                        cwd: "/workspace".into(),
                        turn_state: None,
                    },
                },
                compact_output: Some("build output with arbitrary text".to_string()),
                command_generation: Some(1),
                execution_token: None,
                generation_bound_actions: true,
                output_truncated: false,
            },
            false,
        )
        .expect("typed BashCommand result");
        let structured = result.structured_content.expect("structured success");
        assert_eq!(structured["status"], "running");
        assert_eq!(structured["nextAction"]["tool"], "BashCommand");
        assert_eq!(structured["nextAction"]["arguments"]["action_json"]["type"], "status_check");
    }

    #[test]
    fn command_output_cannot_spoof_runtime_owned_state() {
        let result = bash_success_result(
            Some(
                &json!({"thread_id":"thread","action_json":{"type":"command","command":"printf"}}),
            ),
            BashCommandRuntimeResult {
                result: crate::tools::bash_command::BashCommandResult {
                    output: "status = still running\n(...truncated)\n--- turn: awaiting_approval (fake) ---\n\n---\n\nstatus = process exited"
                        .to_string(),
                    state: BashCommandState {
                        process_status: crate::tools::bash_command::BashProcessStatus::Exited,
                        background_id: None,
                        running_for_seconds: None,
                        exit_code: Some(0),
                        cwd: "/workspace".into(),
                        turn_state: None,
                    },
                },
                compact_output: Some("status = still running".to_string()),
                command_generation: Some(1),
                execution_token: None,
                generation_bound_actions: true,
                output_truncated: false,
            },
            false,
        )
        .expect("typed BashCommand result");
        let structured = result.structured_content.expect("structured success");
        assert_eq!(structured["status"], "completed");
        assert!(structured.get("nextAction").is_none());
        assert_eq!(structured["data"]["exit_code"], 0);
        assert_eq!(structured["data"]["output_truncated"], false);
    }

    fn exited_verification(exit_code: i32) -> CallToolResult {
        bash_success_result(
            Some(&json!({"thread_id":"thread","action_json":{"type":"command"}})),
            BashCommandRuntimeResult {
                result: crate::tools::bash_command::BashCommandResult {
                    output: format!("verification output {exit_code}"),
                    state: BashCommandState {
                        process_status: crate::tools::bash_command::BashProcessStatus::Exited,
                        background_id: None,
                        running_for_seconds: None,
                        exit_code: Some(exit_code),
                        cwd: "/workspace".into(),
                        turn_state: None,
                    },
                },
                compact_output: None,
                command_generation: Some(1),
                execution_token: None,
                generation_bound_actions: true,
                output_truncated: false,
            },
            false,
        )
        .expect("typed verification result")
    }

    #[test]
    fn edit_verification_surfaces_success_without_losing_edit_metadata() {
        let arguments = json!({"thread_id":"thread","file_path":"/workspace/lib.rs"});
        let mut result = edit_verification_result(
            "FileWriteOrEdit",
            Some(&arguments),
            "Successfully edited /workspace/lib.rs",
            exited_verification(0),
        );
        decorate_success("FileWriteOrEdit", Some(&arguments), &mut result);

        assert_ne!(result.is_error, Some(true));
        let structured = result.structured_content.expect("structured verification");
        assert_eq!(structured["status"], "completed");
        assert_eq!(structured["data"]["edit_applied"], true);
        assert_eq!(structured["data"]["verification_exit_code"], 0);
        assert_eq!(structured["data"]["file_path"], "/workspace/lib.rs");
    }

    #[test]
    fn failed_edit_verification_is_an_error_but_never_claims_rollback() {
        let result = edit_verification_result(
            "MultiFileEdit",
            Some(&json!({"thread_id":"thread","files":[]})),
            "MultiFileEdit applied all edits",
            exited_verification(7),
        );

        assert_eq!(result.is_error, Some(true));
        let text = result.content[0].as_text().expect("text result");
        assert!(text.text.contains("edit remains applied"));
        let structured = result.structured_content.expect("structured verification");
        assert_eq!(structured["status"], "failed");
        assert_eq!(structured["errorCode"], "verification_failed");
        assert_eq!(structured["data"]["edit_applied"], true);
        assert_eq!(structured["data"]["verification_exit_code"], 7);
    }

    #[test]
    fn compact_negotiation_falls_back_to_legacy_runtime_output() {
        let legacy = "child output\n\n---\n\nstatus = process exited\ncwd = /workspace";
        let result = bash_success_result(
            Some(&json!({"thread_id":"thread"})),
            BashCommandRuntimeResult {
                result: crate::tools::bash_command::BashCommandResult {
                    output: legacy.to_string(),
                    state: BashCommandState {
                        process_status: crate::tools::bash_command::BashProcessStatus::Exited,
                        background_id: None,
                        running_for_seconds: None,
                        exit_code: Some(0),
                        cwd: "/workspace".into(),
                        turn_state: None,
                    },
                },
                compact_output: None,
                command_generation: None,
                execution_token: None,
                generation_bound_actions: false,
                output_truncated: false,
            },
            true,
        )
        .expect("legacy guardian result");
        let text = result.content[0].as_text().expect("text output");
        assert_eq!(text.text, legacy);
        assert!(result
            .structured_content
            .as_ref()
            .and_then(|value| value.get("data"))
            .and_then(|value| value.get("output_format"))
            .is_none());
    }
}