eggsact 1.2.7

Deterministic MCP and in-process utility tools for coding agents
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
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
use regex;
use serde::Serialize;
use std::sync::LazyLock;

pub fn escape_ascii_json(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    for c in s.chars() {
        if c.is_ascii() {
            result.push(c);
        } else {
            let mut utf16 = [0u16; 2];
            for unit in c.encode_utf16(&mut utf16).iter() {
                result.push_str(&format!("\\u{:04x}", unit));
            }
        }
    }
    result
}

pub fn python_json_dumps<T: Serialize>(value: &T) -> String {
    struct PythonStyleFormatter;

    impl serde_json::ser::Formatter for PythonStyleFormatter {
        fn begin_array_value<W: std::io::Write + ?Sized>(
            &mut self,
            writer: &mut W,
            first: bool,
        ) -> std::io::Result<()> {
            if first {
                Ok(())
            } else {
                writer.write_all(b", ")
            }
        }

        fn begin_object_key<W: std::io::Write + ?Sized>(
            &mut self,
            writer: &mut W,
            first: bool,
        ) -> std::io::Result<()> {
            if first {
                Ok(())
            } else {
                writer.write_all(b", ")
            }
        }

        fn begin_object_value<W: std::io::Write + ?Sized>(
            &mut self,
            writer: &mut W,
        ) -> std::io::Result<()> {
            writer.write_all(b": ")
        }
    }

    let mut buf = Vec::new();
    {
        let mut serializer = serde_json::Serializer::with_formatter(&mut buf, PythonStyleFormatter);
        if value.serialize(&mut serializer).is_err() {
            return String::new();
        }
    }
    // serde_json emits UTF-8 directly for non-ASCII strings.  The common MCP
    // case is already ASCII, so avoid converting to a String and walking the
    // whole result a second time.  Keep the Python-compatible escaping path
    // byte-for-byte identical for Unicode output.
    if buf.is_ascii() {
        String::from_utf8(buf).unwrap_or_default()
    } else {
        let serialized = String::from_utf8(buf).unwrap_or_default();
        escape_ascii_json(&serialized)
    }
}

pub fn wrap_tool_response(tool_response: &ToolResponse) -> serde_json::Value {
    let text = python_json_dumps(tool_response);
    wrap_tool_response_with_text(tool_response, text)
}

pub(crate) fn wrap_tool_response_with_text(
    tool_response: &ToolResponse,
    text: String,
) -> serde_json::Value {
    if tool_response.ok {
        serde_json::json!({
            "content": [{"type": "text", "text": text}],
        })
    } else {
        serde_json::json!({
            "content": [{"type": "text", "text": text}],
            "isError": true,
        })
    }
}

/// Modern (2026-07-28) `tools/call` result shape.
///
/// Successful responses expose `structuredContent` that conforms to the tool's
/// `outputSchema` (which describes `ToolResponse.result`), plus the legacy
/// text JSON fallback for backward compatibility. Route-critical `verdict`
/// stays inside `result`; outer envelope fields remain in the text fallback.
/// Tool-level errors retain `isError:true` and omit `structuredContent`.
/// Every modern result carries `resultType:"complete"` (eggsact has no MRTR
/// workflow) and reserved `_meta` server identity.
pub fn wrap_tool_response_modern(tool_response: &ToolResponse) -> serde_json::Value {
    let text = python_json_dumps(tool_response);
    wrap_tool_response_modern_with_text(tool_response, text)
}

pub(crate) fn wrap_tool_response_modern_with_text(
    tool_response: &ToolResponse,
    text: String,
) -> serde_json::Value {
    let meta = crate::mcp::protocol::server_info_meta();
    if tool_response.ok {
        match &tool_response.result {
            Some(result) => serde_json::json!({
                "resultType": "complete",
                "content": [{"type": "text", "text": text}],
                "structuredContent": result,
                "_meta": meta,
            }),
            None => serde_json::json!({
                "resultType": "complete",
                "content": [{"type": "text", "text": text}],
                "_meta": meta,
            }),
        }
    } else {
        serde_json::json!({
            "resultType": "complete",
            "content": [{"type": "text", "text": text}],
            "isError": true,
            "_meta": meta,
        })
    }
}

static SANITIZE_REGEXES: LazyLock<Vec<(&'static str, regex::Regex, &'static str)>> =
    LazyLock::new(|| {
        vec![
            (
                "file_line",
                regex::Regex::new(r#"File\s+["\'][^"\']*["\'],\s*line\s+\d+"#).unwrap(),
                r#"File "<redacted>", line <redacted>"#,
            ),
            (
                "module_ref",
                regex::Regex::new(r"(?:in\s+)<[^>]+>").unwrap(),
                "in <module>",
            ),
            (
                "var_assign",
                regex::Regex::new(r#"(?m)^\s*[A-Za-z_]\w*\s*=\s*["'][^"']*["']"#).unwrap(),
                "<var>=<redacted>",
            ),
            (
                "system_path",
                regex::Regex::new(
                    r"/(?:etc|proc|dev|sys|run|tmp|var|usr|lib|bin|sbin)(?:/[\w.-]+)+",
                )
                .unwrap(),
                "<path>",
            ),
            (
                "win_path",
                regex::Regex::new(r"[A-Za-z]:\\(?:[\w.-]+\\)+\w+\.\w+").unwrap(),
                "<path>",
            ),
            (
                "no_such_file",
                regex::Regex::new(r#"No such file or directory:\s*['"][^'"]*['"]"#).unwrap(),
                "No such file or directory: '<redacted>'",
            ),
            (
                "mem_addr",
                regex::Regex::new(r"0x[0-9a-fA-F]{8,}").unwrap(),
                "<address>",
            ),
            (
                "json_pos",
                regex::Regex::new(r"(?i)\bline\s+\d+\s+column\s+\d+\b").unwrap(),
                "line <redacted> column <redacted>",
            ),
        ]
    });

static BARE_PATH_REGEX: LazyLock<regex::Regex> =
    LazyLock::new(|| regex::Regex::new(r"/[\w./-]+\.\w{1,10}").unwrap());

pub fn sanitize_error(msg: &str) -> String {
    // Single pass: cap length and fold non-ASCII to '?' in the initial collect.
    let mut result: String = msg
        .chars()
        .take(8192)
        .map(|c| if c.is_ascii() { c } else { '?' })
        .collect();
    for (_name, re, replacement) in SANITIZE_REGEXES.iter() {
        result = re.replace_all(&result, *replacement).into_owned();
    }
    // Clone for the BARE_PATH closure: `replace_all` borrows the haystack and
    // invokes the closure per match; the closure must not borrow `result` while
    // it is being reassigned. Using a snapshot makes the borrow explicit and
    // avoids relying on `regex` cloning the haystack internally.
    let snapshot = result.clone();
    result = BARE_PATH_REGEX
        .replace_all(&snapshot, |captures: &regex::Captures<'_>| {
            let path = captures.get(0).expect("regex match has group 0");
            let before = snapshot[..path.start()].chars().next_back();
            let after = snapshot[path.end()..].chars().next();
            let valid_before =
                before.is_none_or(|c| !c.is_alphanumeric() && c != '_' && c != '.' && c != '/');
            let valid_after = after.is_none_or(|c| !c.is_alphanumeric() && c != '_' && c != '/');
            if valid_before && valid_after {
                "<path>".to_string()
            } else {
                path.as_str().to_string()
            }
        })
        .into_owned();
    result
}

// Canonical severity, disposition, and verdict constants live in
// `machine_codes`. Re-export here so existing `use crate::mcp::response::{severity, ...}`
// paths continue to work.
pub use crate::mcp::machine_codes::{disposition, severity, verdict};

/// Create a structured finding as a `serde_json::Value`.
///
/// Findings are serialized as JSON objects with standard fields:
/// `code`, `severity`, `message`, and optionally `disposition`, `location`, `details`.
///
/// # Arguments
///
/// * `code` — Machine code constant (e.g. `AMBIGUOUS_REPLACEMENT`).
/// * `severity` — Severity level (`severity::INFO`, etc.).
/// * `message` — Human-readable description of the finding.
/// * `disposition` — Optional disposition (`disposition::BLOCKING`, etc.).
/// * `details` — Optional additional structured data.
pub fn finding(
    code: &str,
    severity: &str,
    message: &str,
    disposition: Option<&str>,
    details: Option<serde_json::Value>,
) -> serde_json::Value {
    let mut f = serde_json::json!({
        "code": code,
        "severity": severity,
        "message": message,
    });
    if let Some(d) = disposition {
        f["disposition"] = serde_json::json!(d);
    }
    if let Some(d) = details {
        f["details"] = d;
    }
    f
}

/// Create a structured finding with a source location.
///
/// # Arguments
///
/// * `code` — Machine code constant.
/// * `severity` — Severity level.
/// * `message` — Human-readable description.
/// * `disposition` — Optional disposition.
/// * `line` — 1-indexed line number.
/// * `column` — 1-indexed column number (optional).
pub fn finding_with_location(
    code: &str,
    severity: &str,
    message: &str,
    disposition: Option<&str>,
    line: usize,
    column: Option<usize>,
) -> serde_json::Value {
    let mut loc = serde_json::json!({
        "line": line,
    });
    if let Some(c) = column {
        loc["column"] = serde_json::json!(c);
    }
    let mut f = serde_json::json!({
        "code": code,
        "severity": severity,
        "message": message,
        "location": loc,
    });
    if let Some(d) = disposition {
        f["disposition"] = serde_json::json!(d);
    }
    f
}

/// Create a finding for a prompt input inspection check.
///
/// These findings have a `span` field instead of `location`.
pub fn prompt_finding(
    code: &str,
    severity: &str,
    message: &str,
    disposition: Option<&str>,
    byte_offset: usize,
    end_byte_offset: usize,
    details: Option<serde_json::Value>,
) -> serde_json::Value {
    let mut f = serde_json::json!({
        "code": code,
        "severity": severity,
        "message": message,
        "span": {
            "byte_offset": byte_offset,
            "end_byte_offset": end_byte_offset,
        },
    });
    if let Some(d) = disposition {
        f["disposition"] = serde_json::json!(d);
    }
    if let Some(d) = details {
        f["details"] = d;
    }
    f
}

#[derive(Serialize, Debug)]
pub struct ToolResponse {
    pub ok: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hints: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub warnings: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limits_applied: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub findings: Option<Vec<serde_json::Value>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub machine_code: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub recommended_next_tool: Option<serde_json::Value>,
}

impl ToolResponse {
    pub fn success(result: serde_json::Value, tool: Option<&str>) -> Self {
        Self {
            ok: true,
            tool: tool.map(String::from),
            result: Some(result),
            error_type: None,
            error: None,
            hints: None,
            warnings: None,
            limits_applied: None,
            findings: None,
            machine_code: None,
            recommended_next_tool: None,
        }
    }

    #[cfg(test)]
    #[deprecated(
        since = "1.1.4",
        note = "Use `error_with_code` instead. Non-OK tool responses must carry a machine_code."
    )]
    #[doc(hidden)]
    pub(crate) fn error_without_code_for_legacy_tests_only(
        error_type: &str,
        error: &str,
        hints: Option<Vec<String>>,
        tool: Option<&str>,
    ) -> Self {
        Self {
            ok: false,
            tool: tool.map(String::from),
            result: None,
            error_type: Some(error_type.to_string()),
            error: Some(sanitize_error(error)),
            hints: Some(
                hints
                    .unwrap_or_default()
                    .into_iter()
                    .map(|h| sanitize_error(&h))
                    .collect(),
            ),
            warnings: Some(vec![]),
            limits_applied: None,
            findings: None,
            machine_code: None,
            recommended_next_tool: None,
        }
    }

    /// Create an error response with a machine code.
    ///
    /// This is the preferred constructor for non-OK tool responses. It ensures
    /// every error carries a stable machine-readable code.
    ///
    /// # Arguments
    ///
    /// * `error_type` — Coarse error category (legacy, kept for compat).
    /// * `machine_code` — Stable machine code from `machine_codes` module.
    /// * `error` — Human-readable error message.
    /// * `hints` — Optional help text for the caller.
    /// * `tool` — Optional tool name.
    pub fn error_with_code(
        error_type: &str,
        machine_code: &str,
        error: &str,
        hints: Option<Vec<String>>,
        tool: Option<&str>,
    ) -> Self {
        Self {
            ok: false,
            tool: tool.map(String::from),
            result: None,
            error_type: Some(error_type.to_string()),
            error: Some(sanitize_error(error)),
            hints: Some(
                hints
                    .unwrap_or_default()
                    .into_iter()
                    .map(|h| sanitize_error(&h))
                    .collect(),
            ),
            warnings: Some(vec![]),
            limits_applied: None,
            findings: None,
            machine_code: Some(machine_code.to_string()),
            recommended_next_tool: None,
        }
    }

    /// Create a success response with a machine code.
    pub fn success_with_machine_code(
        result: serde_json::Value,
        tool: Option<&str>,
        machine_code: &str,
    ) -> Self {
        Self {
            ok: true,
            tool: tool.map(String::from),
            result: Some(result),
            error_type: None,
            error: None,
            hints: None,
            warnings: None,
            limits_applied: None,
            findings: None,
            machine_code: Some(machine_code.to_string()),
            recommended_next_tool: None,
        }
    }

    pub fn with_tool(mut self, tool: &str) -> Self {
        self.tool = Some(tool.to_string());
        self
    }

    pub fn with_warnings(mut self, warnings: Vec<String>) -> Self {
        self.warnings = Some(warnings);
        self
    }

    pub fn with_limits_applied(mut self, limits: Vec<String>) -> Self {
        self.limits_applied = Some(limits);
        self
    }

    pub fn with_findings(mut self, findings: Vec<serde_json::Value>) -> Self {
        self.findings = Some(findings);
        self
    }

    pub fn with_machine_code(mut self, code: &str) -> Self {
        self.machine_code = Some(code.to_string());
        self
    }

    pub fn with_recommended_next_tool(mut self, tool: serde_json::Value) -> Self {
        self.recommended_next_tool = Some(tool);
        self
    }

    /// Build a structured `recommended_next_tool` value.
    ///
    /// Returns a JSON object with `name`, `reason`, and optionally `arguments_hint`.
    pub fn next_tool(
        name: &str,
        reason: &str,
        arguments_hint: Option<serde_json::Value>,
    ) -> serde_json::Value {
        let mut obj = serde_json::json!({
            "name": name,
            "reason": reason,
        });
        if let Some(hint) = arguments_hint {
            obj["arguments_hint"] = hint;
        }
        obj
    }

    /// Set the `verdict` field inside `result`.
    ///
    /// If `result` is `None`, it is initialized to an empty object first.
    pub fn with_verdict(mut self, verdict: &str) -> Self {
        let result = self.result.get_or_insert_with(|| serde_json::json!({}));
        result["verdict"] = serde_json::json!(verdict);
        self
    }
}

/// Build a preflight "allow" response.
///
/// Sets `ok=true`, verdict to `allow`, machine_code, and optionally findings and next tool.
pub fn preflight_allow(
    tool: &str,
    machine_code: &str,
    result: serde_json::Value,
    findings: Vec<serde_json::Value>,
    next_tool: Option<serde_json::Value>,
) -> ToolResponse {
    let mut resp = ToolResponse::success(result, Some(tool))
        .with_machine_code(machine_code)
        .with_verdict(verdict::ALLOW);
    if !findings.is_empty() {
        resp = resp.with_findings(findings);
    }
    if let Some(nt) = next_tool {
        resp = resp.with_recommended_next_tool(nt);
    }
    resp
}

/// Build a preflight "review" response.
///
/// Sets `ok=true`, verdict to `review`, machine_code, findings, and optionally next tool.
pub fn preflight_review(
    tool: &str,
    machine_code: &str,
    result: serde_json::Value,
    findings: Vec<serde_json::Value>,
    next_tool: Option<serde_json::Value>,
) -> ToolResponse {
    let mut resp = ToolResponse::success(result, Some(tool))
        .with_machine_code(machine_code)
        .with_verdict(verdict::REVIEW);
    if !findings.is_empty() {
        resp = resp.with_findings(findings);
    }
    if let Some(nt) = next_tool {
        resp = resp.with_recommended_next_tool(nt);
    }
    resp
}

/// Build a preflight "block" response.
///
/// Sets `ok=true`, verdict to `block`, machine_code, and findings.
pub fn preflight_block(
    tool: &str,
    machine_code: &str,
    result: serde_json::Value,
    findings: Vec<serde_json::Value>,
) -> ToolResponse {
    let mut resp = ToolResponse::success(result, Some(tool))
        .with_machine_code(machine_code)
        .with_verdict(verdict::BLOCK);
    if !findings.is_empty() {
        resp = resp.with_findings(findings);
    }
    resp
}

/// Truncate a `ToolResponse` to fit within the given budget limits.
///
/// This applies deterministic truncation rules:
/// - `findings` array is capped at `budget.max_findings` (highest-severity first).
/// - `result` string representation is capped at `budget.max_output_bytes`.
/// - `limits_applied` is populated with any truncation that occurred.
///
/// The tool's `machine_code` is set to `OUTPUT_TOO_LARGE` only when truncation
/// changes the routing verdict (e.g., a route-critical tool loses all findings).
pub fn truncate_response(response: &mut ToolResponse, budget: &crate::mcp::budget::ToolBudget) {
    let mut limits: Vec<String> = Vec::new();

    // Truncate findings array — keep total length <= max_findings.
    // We reserve one slot for the synthetic truncation notice so the
    // observed cap is `max_findings - 1` real findings + 1 notice,
    // never exceeding `max_findings` total.
    if let Some(ref mut findings) = response.findings {
        if findings.len() > budget.max_findings {
            // Sort by severity (highest first) before truncating. This
            // intentionally changes the ordering of the truncated response.
            let severity_order = |s: &str| match s {
                "critical" => 0,
                "high" => 1,
                "medium" => 2,
                "low" => 3,
                "info" => 4,
                _ => 5,
            };
            findings.sort_by(|a, b| {
                let sev_a = a.get("severity").and_then(|v| v.as_str()).unwrap_or("info");
                let sev_b = b.get("severity").and_then(|v| v.as_str()).unwrap_or("info");
                severity_order(sev_a).cmp(&severity_order(sev_b))
            });
            let original_len = findings.len();
            // When max_findings >= 1, reserve one slot for the truncation
            // notice; real findings are capped at max_findings - 1.
            // When max_findings == 0, no findings or notice are retained.
            let real_cap = budget.max_findings.saturating_sub(1);
            findings.truncate(real_cap);
            if budget.max_findings >= 1 {
                let omitted = original_len.saturating_sub(real_cap);
                findings.push(serde_json::json!({
                    "code": "OUTPUT_TOO_LARGE",
                    "severity": "info",
                    "message": format!("{} findings omitted due to output budget", omitted),
                    "disposition": "informational",
                }));
                limits.push(format!("findings_truncated:{}", omitted));
            } else {
                limits.push(format!("findings_truncated:{}", original_len));
            }
        }
    }

    // Truncate result string if it exists and is too large. We replace
    // oversized result with a summary object that preserves route-critical
    // top-level keys (machine_code, verdict, ok, summary) so callers can
    // still route on the response. The existing user-supplied `summary`
    // field is kept; only if absent do we emit a default placeholder.
    if let Some(ref mut result) = response.result {
        let original_len = serialized_size(result);
        if original_len > budget.max_output_bytes {
            // Build a small summary and use it as the new result.
            let summary = serde_json::json!({
                "truncated": true,
                "original_size_bytes": original_len,
                "max_output_bytes": budget.max_output_bytes,
            });
            // Preserve route-critical keys at the top level if present.
            let mut merged = serde_json::Map::new();
            if let Some(obj) = result.as_object() {
                for key in &["machine_code", "verdict", "summary", "ok"] {
                    if let Some(v) = obj.get(*key) {
                        merged.insert((*key).to_string(), v.clone());
                    }
                }
            }
            for (k, v) in summary.as_object().unwrap() {
                merged.insert(k.clone(), v.clone());
            }
            // Inject default truncation message only when caller didn't
            // supply a `summary` field — caller-supplied summaries are
            // more informative for harnesses than our boilerplate.
            if !merged.contains_key("summary") {
                merged.insert(
                    "summary".to_string(),
                    serde_json::Value::String(
                        "Result exceeded output budget; full payload suppressed.".to_string(),
                    ),
                );
            }
            *result = serde_json::Value::Object(merged);
            limits.push(format!("result_truncated:{}", original_len));
        }
    }

    if !limits.is_empty() {
        let existing = response.limits_applied.take().unwrap_or_default();
        let mut all_limits = existing;
        all_limits.extend(limits);
        response.limits_applied = Some(all_limits);
    }
}

/// Count serialized JSON bytes without allocating the serialized payload.
fn serialized_size<T: Serialize>(value: &T) -> usize {
    struct Counter(usize);
    impl std::io::Write for Counter {
        fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
            self.0 = self.0.saturating_add(bytes.len());
            Ok(bytes.len())
        }

        fn flush(&mut self) -> std::io::Result<()> {
            Ok(())
        }
    }

    let mut counter = Counter(0);
    serde_json::to_writer(&mut counter, value)
        .map(|_| counter.0)
        .unwrap_or(0)
}

#[derive(Serialize, Debug, Default, Clone)]
pub struct CallMetrics {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub elapsed_ms: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub input_bytes: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output_bytes_before_truncation: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output_bytes_after_truncation: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub budget_tier: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limits_applied_count: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sub_tool_count: Option<usize>,
}

impl ToolResponse {
    pub fn with_metrics(mut self, metrics: CallMetrics) -> Self {
        if let Some(ref mut result) = self.result {
            if let Ok(metrics_val) = serde_json::to_value(&metrics) {
                result["_metrics"] = metrics_val;
            }
        }
        self
    }
}

pub struct CallMetricsBuilder {
    metrics: CallMetrics,
}

impl Default for CallMetricsBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl CallMetricsBuilder {
    pub fn new() -> Self {
        Self {
            metrics: CallMetrics::default(),
        }
    }

    pub fn elapsed_ms(mut self, ms: u64) -> Self {
        self.metrics.elapsed_ms = Some(ms);
        self
    }

    pub fn input_bytes(mut self, bytes: usize) -> Self {
        self.metrics.input_bytes = Some(bytes);
        self
    }

    pub fn output_bytes_before(mut self, bytes: usize) -> Self {
        self.metrics.output_bytes_before_truncation = Some(bytes);
        self
    }

    pub fn output_bytes_after(mut self, bytes: usize) -> Self {
        self.metrics.output_bytes_after_truncation = Some(bytes);
        self
    }

    pub fn budget_tier(mut self, tier: &str) -> Self {
        self.metrics.budget_tier = Some(tier.to_string());
        self
    }

    pub fn limits_applied_count(mut self, count: usize) -> Self {
        self.metrics.limits_applied_count = Some(count);
        self
    }

    pub fn sub_tool_count(mut self, count: usize) -> Self {
        self.metrics.sub_tool_count = Some(count);
        self
    }

    pub fn build(self) -> CallMetrics {
        self.metrics
    }
}

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

    #[test]
    #[allow(deprecated)]
    fn legacy_error_constructor_only_in_tests() {
        let _ = ToolResponse::error_without_code_for_legacy_tests_only("test", "test", None, None);
    }

    #[test]
    fn finding_with_disposition() {
        let f = finding(
            "TEST_CODE",
            severity::MEDIUM,
            "test message",
            Some(disposition::BLOCKING),
            None,
        );
        assert_eq!(f["code"], "TEST_CODE");
        assert_eq!(f["severity"], "medium");
        assert_eq!(f["disposition"], "blocking");
        assert!(f.get("location").is_none());
    }

    #[test]
    fn finding_without_disposition() {
        let f = finding("TEST_CODE", severity::LOW, "test message", None, None);
        assert_eq!(f["code"], "TEST_CODE");
        assert!(f.get("disposition").is_none());
    }

    #[test]
    fn next_tool_structured() {
        let nt = ToolResponse::next_tool("text_diff_explain", "ambiguous replacement", None);
        assert_eq!(nt["name"], "text_diff_explain");
        assert_eq!(nt["reason"], "ambiguous replacement");
        assert!(nt.get("arguments_hint").is_none());
    }

    #[test]
    fn next_tool_with_arguments_hint() {
        let hint = serde_json::json!({"old_text": "foo"});
        let nt = ToolResponse::next_tool("text_diff_explain", "ambiguous", Some(hint));
        assert_eq!(nt["arguments_hint"]["old_text"], "foo");
    }

    #[test]
    fn with_verdict_sets_result_field() {
        let resp = ToolResponse::success(serde_json::json!({}), None).with_verdict(verdict::ALLOW);
        assert_eq!(resp.result.as_ref().unwrap()["verdict"], "allow");
    }

    #[test]
    fn preflight_allow_builder() {
        let resp = preflight_allow(
            "edit_preflight",
            machine_codes::EDIT_OK,
            serde_json::json!({"ok_to_apply": true}),
            vec![],
            None,
        );
        assert!(resp.ok);
        assert_eq!(resp.machine_code.as_deref(), Some("EDIT_OK"));
        assert_eq!(resp.result.as_ref().unwrap()["verdict"], "allow");
    }

    #[test]
    fn preflight_block_builder() {
        let findings = vec![finding(
            "PATCH_FAILED",
            severity::HIGH,
            "patch does not apply",
            Some(disposition::BLOCKING),
            None,
        )];
        let resp = preflight_block(
            "edit_preflight",
            machine_codes::PATCH_FAILED,
            serde_json::json!({"ok_to_apply": false}),
            findings,
        );
        assert!(resp.ok);
        assert_eq!(resp.result.as_ref().unwrap()["verdict"], "block");
        assert_eq!(resp.findings.as_ref().unwrap().len(), 1);
    }

    #[test]
    fn truncate_findings_within_budget() {
        let budget = crate::mcp::budget::ToolBudget::CHEAP.with_max_findings(3);
        let mut resp = ToolResponse::success(serde_json::json!({}), Some("test_tool"))
            .with_findings(vec![
                finding("A", severity::LOW, "low sev", None, None),
                finding("B", severity::HIGH, "high sev", None, None),
                finding("C", severity::MEDIUM, "medium sev", None, None),
                finding("D", severity::CRITICAL, "critical sev", None, None),
            ]);
        truncate_response(&mut resp, &budget);
        let findings = resp.findings.as_ref().unwrap();
        // Total findings length must not exceed max_findings: keep
        // highest-severity (critical, high) + truncation notice.
        assert_eq!(findings.len(), 3);
        assert_eq!(findings[0]["severity"], "critical");
        assert_eq!(findings[1]["severity"], "high");
        assert_eq!(findings[2]["code"], "OUTPUT_TOO_LARGE");
    }

    #[test]
    fn truncate_findings_populates_limits_applied() {
        let budget = crate::mcp::budget::ToolBudget::CHEAP.with_max_findings(2);
        let mut resp = ToolResponse::success(serde_json::json!({}), Some("test_tool"))
            .with_findings(vec![
                finding("A", severity::LOW, "a", None, None),
                finding("B", severity::LOW, "b", None, None),
                finding("C", severity::LOW, "c", None, None),
                finding("D", severity::LOW, "d", None, None),
            ]);
        truncate_response(&mut resp, &budget);
        assert!(resp.limits_applied.is_some());
        let limits = resp.limits_applied.as_ref().unwrap();
        assert!(limits.iter().any(|l| l.starts_with("findings_truncated:")));
    }

    #[test]
    fn truncate_noop_when_within_budget() {
        let budget = crate::mcp::budget::ToolBudget::CHEAP;
        let mut resp = ToolResponse::success(serde_json::json!({}), Some("test_tool"))
            .with_findings(vec![finding("A", severity::LOW, "a", None, None)]);
        truncate_response(&mut resp, &budget);
        assert_eq!(resp.findings.as_ref().unwrap().len(), 1);
        assert!(resp.limits_applied.is_none());
    }

    #[test]
    fn truncate_findings_total_never_exceeds_cap() {
        // Exact-length contract: total findings length must be <= max_findings
        // regardless of how many findings the source produced.
        for max in 1usize..=10 {
            let budget = crate::mcp::budget::ToolBudget::CHEAP.with_max_findings(max);
            // Produce 5x the cap to force truncation.
            let many: Vec<_> = (0..max * 5)
                .map(|i| {
                    let s = format!("F{}", i);
                    finding(&s, severity::LOW, "msg", None, None)
                })
                .collect();
            let mut resp =
                ToolResponse::success(serde_json::json!({}), Some("t")).with_findings(many);
            truncate_response(&mut resp, &budget);
            let findings = resp.findings.as_ref().unwrap();
            assert!(
                findings.len() <= max,
                "max_findings={} but total findings len={}",
                max,
                findings.len()
            );
            // Last entry should be the truncation notice.
            let last = findings.last().unwrap();
            assert_eq!(last["code"], "OUTPUT_TOO_LARGE");
        }
    }

    #[test]
    fn truncate_result_replaces_oversized_payload() {
        // Result truncation is REAL: replaces oversized result with a
        // summary object that preserves route-critical keys.
        let budget = crate::mcp::budget::ToolBudget::CHEAP.with_max_output_bytes(50);
        // Build a large result that's guaranteed to exceed 50 bytes.
        let large_text = "x".repeat(500);
        let mut resp = ToolResponse::success(
            serde_json::json!({
                "machine_code": "EDIT_OK",
                "verdict": "allow",
                "summary": "all good",
                "data": large_text,
            }),
            Some("test_tool"),
        )
        .with_machine_code(machine_codes::EDIT_OK)
        .with_verdict(verdict::ALLOW);
        truncate_response(&mut resp, &budget);
        // Serialized result must now be small enough to fit.
        let result = resp.result.as_ref().unwrap();
        let result_str = serde_json::to_string(result).unwrap();
        assert!(
            result_str.len() <= 200,
            "result should have been replaced; len={}",
            result_str.len()
        );
        // Route-critical keys preserved.
        assert_eq!(result["machine_code"], "EDIT_OK");
        assert_eq!(result["verdict"], "allow");
        // Truncation marker set.
        assert_eq!(result["truncated"], true);
        assert!(result["original_size_bytes"].as_u64().unwrap() > 50);
        assert_eq!(result["max_output_bytes"], 50);
        // limits_applied records the truncation.
        let limits = resp.limits_applied.as_ref().unwrap();
        assert!(limits.iter().any(|l| l.starts_with("result_truncated:")));
    }

    #[test]
    fn truncate_result_preserves_summary_when_present() {
        // Existing top-level `summary` string should be kept alongside the
        // truncation metadata so callers can show a human message.
        let budget = crate::mcp::budget::ToolBudget::CHEAP.with_max_output_bytes(40);
        let mut resp = ToolResponse::success(
            serde_json::json!({
                "machine_code": "EDIT_OK",
                "summary": "edited 3 lines",
                "ok": true,
                "big_blob": "y".repeat(500),
            }),
            Some("test_tool"),
        )
        .with_machine_code(machine_codes::EDIT_OK);
        truncate_response(&mut resp, &budget);
        let result = resp.result.as_ref().unwrap();
        assert_eq!(result["summary"], "edited 3 lines");
        assert_eq!(result["ok"], true);
        assert_eq!(result["truncated"], true);
    }

    #[test]
    fn truncate_under_max_does_not_mutate_result() {
        // When result fits within budget, it must be left intact.
        let budget = crate::mcp::budget::ToolBudget::CHEAP; // 1 MB cap
        let original = serde_json::json!({"k": "v", "n": 42});
        let mut resp = ToolResponse::success(original.clone(), Some("test_tool"));
        truncate_response(&mut resp, &budget);
        assert_eq!(resp.result.as_ref().unwrap(), &original);
        assert!(resp.limits_applied.is_none());
    }

    #[test]
    fn call_metrics_default_all_none() {
        let m = CallMetrics::default();
        let val = serde_json::to_value(&m).unwrap();
        assert_eq!(val, serde_json::json!({}));
    }

    #[test]
    fn call_metrics_builder_sets_fields() {
        let m = CallMetricsBuilder::new()
            .elapsed_ms(42)
            .input_bytes(128)
            .output_bytes_before(256)
            .output_bytes_after(200)
            .budget_tier("standard")
            .limits_applied_count(2)
            .sub_tool_count(3)
            .build();
        assert_eq!(m.elapsed_ms, Some(42));
        assert_eq!(m.input_bytes, Some(128));
        assert_eq!(m.output_bytes_before_truncation, Some(256));
        assert_eq!(m.output_bytes_after_truncation, Some(200));
        assert_eq!(m.budget_tier.as_deref(), Some("standard"));
        assert_eq!(m.limits_applied_count, Some(2));
        assert_eq!(m.sub_tool_count, Some(3));
    }

    #[test]
    fn with_metrics_attaches_to_result() {
        let metrics = CallMetricsBuilder::new().elapsed_ms(10).build();
        let resp =
            ToolResponse::success(serde_json::json!({"key": "val"}), None).with_metrics(metrics);
        let result = resp.result.unwrap();
        assert_eq!(result["_metrics"]["elapsed_ms"], 10);
        assert_eq!(result["key"], "val");
    }

    #[test]
    fn with_metrics_no_result_noop() {
        let metrics = CallMetricsBuilder::new().elapsed_ms(10).build();
        let resp = ToolResponse {
            ok: false,
            tool: None,
            result: None,
            error_type: Some("test".into()),
            error: Some("test".into()),
            hints: None,
            warnings: None,
            limits_applied: None,
            findings: None,
            machine_code: None,
            recommended_next_tool: None,
        }
        .with_metrics(metrics);
        assert!(resp.result.is_none());
    }

    #[test]
    fn truncate_findings_cap_zero_returns_no_findings() {
        let budget = crate::mcp::budget::ToolBudget::CHEAP.with_max_findings(0);
        let mut resp = ToolResponse::success(serde_json::json!({}), Some("test_tool"))
            .with_findings(vec![
                finding("A", severity::LOW, "a", None, None),
                finding("B", severity::HIGH, "b", None, None),
            ]);
        truncate_response(&mut resp, &budget);
        let findings = resp.findings.as_ref().unwrap();
        assert_eq!(findings.len(), 0);
        let limits = resp.limits_applied.as_ref().unwrap();
        assert!(limits.iter().any(|l| l.starts_with("findings_truncated:2")));
    }

    #[test]
    fn truncate_findings_cap_one_keeps_notice_only() {
        let budget = crate::mcp::budget::ToolBudget::CHEAP.with_max_findings(1);
        let mut resp = ToolResponse::success(serde_json::json!({}), Some("test_tool"))
            .with_findings(vec![
                finding("A", severity::LOW, "a", None, None),
                finding("B", severity::HIGH, "b", None, None),
                finding("C", severity::MEDIUM, "c", None, None),
            ]);
        truncate_response(&mut resp, &budget);
        let findings = resp.findings.as_ref().unwrap();
        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0]["code"], "OUTPUT_TOO_LARGE");
        // All 3 real findings were omitted
        assert_eq!(
            findings[0]["message"],
            "3 findings omitted due to output budget"
        );
    }

    #[test]
    fn truncate_findings_cap_two_keeps_one_real_and_notice() {
        let budget = crate::mcp::budget::ToolBudget::CHEAP.with_max_findings(2);
        let mut resp = ToolResponse::success(serde_json::json!({}), Some("test_tool"))
            .with_findings(vec![
                finding("A", severity::LOW, "a", None, None),
                finding("B", severity::HIGH, "b", None, None),
                finding("C", severity::MEDIUM, "c", None, None),
                finding("D", severity::CRITICAL, "d", None, None),
            ]);
        truncate_response(&mut resp, &budget);
        let findings = resp.findings.as_ref().unwrap();
        assert_eq!(findings.len(), 2);
        assert_eq!(findings[0]["severity"], "critical");
        assert_eq!(findings[1]["code"], "OUTPUT_TOO_LARGE");
        // 4 original - 1 real cap = 3 omitted
        assert_eq!(
            findings[1]["message"],
            "3 findings omitted due to output budget"
        );
    }

    #[test]
    fn truncate_findings_omitted_count_exact() {
        // Regression: omitted count should be original_len - real_cap, not
        // original_len - max_findings (which would be off by one).
        let budget = crate::mcp::budget::ToolBudget::CHEAP.with_max_findings(5);
        let mut resp = ToolResponse::success(serde_json::json!({}), Some("test_tool"))
            .with_findings(vec![
                finding("A", severity::LOW, "a", None, None),
                finding("B", severity::HIGH, "b", None, None),
                finding("C", severity::MEDIUM, "c", None, None),
            ]);
        // 3 findings within cap of 5: no truncation
        truncate_response(&mut resp, &budget);
        let findings = resp.findings.as_ref().unwrap();
        assert_eq!(findings.len(), 3);
        assert!(resp.limits_applied.is_none());
    }

    #[test]
    fn call_metrics_serialization_skips_none() {
        let m = CallMetrics {
            elapsed_ms: Some(5),
            input_bytes: None,
            output_bytes_before_truncation: None,
            output_bytes_after_truncation: None,
            budget_tier: None,
            limits_applied_count: None,
            sub_tool_count: None,
        };
        let val = serde_json::to_value(&m).unwrap();
        let obj = val.as_object().unwrap();
        assert_eq!(obj.len(), 1);
        assert_eq!(obj["elapsed_ms"], 5);
    }
}