rho-coding-agent 1.40.0

A lightweight agent harness inspired by Pi
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
//! Claude Code tool names as first-class [`ToolCard`]s.
//!
//! Keep Claude verbs (`Read`, `Bash`, `Glob`). Populate the same family,
//! header dialect, facts, and body types native Rho cards use. Unknown and
//! MCP tools degrade to a named generic card.

use std::path::Path;

use serde_json::Value;

use rho_sdk::floor_char_boundary;

use rho_tools::{
    tool::compact_display_path,
    tool_card::{
        DiffRow, DiffRowKind, ToolBody, ToolCard, ToolFact, ToolFamily, ToolHeader, ToolStatus,
    },
};

use super::format::{truncate_payload_lines, MAX_TOOL_BODY_LINES};
use super::types::MAX_TOOL_PAYLOAD_CHARS;

/// Raw `input_json_delta` assembly budget. Larger than the presentation cap
/// so a complete oversized object can be parsed, then reduced by
/// [`bounded_input`].
const MAX_INPUT_JSON_CHARS: usize = MAX_TOOL_PAYLOAD_CHARS.saturating_mul(16);

/// Claude tool identity parsed once from the wire name.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ClaudeTool {
    Bash,
    Read,
    Write,
    Edit,
    NotebookEdit,
    Glob,
    Grep,
    Ls,
    WebSearch,
    WebFetch,
    Skill,
    Task,
    TodoWrite,
    AskUserQuestion,
    ExitPlanMode,
    EnterPlanMode,
    Other,
}

impl ClaudeTool {
    fn from_name(name: &str) -> Self {
        match name {
            "Bash" => Self::Bash,
            "Read" => Self::Read,
            "Write" => Self::Write,
            "Edit" => Self::Edit,
            "NotebookEdit" => Self::NotebookEdit,
            "Glob" => Self::Glob,
            "Grep" => Self::Grep,
            "LS" => Self::Ls,
            "WebSearch" => Self::WebSearch,
            "WebFetch" => Self::WebFetch,
            "Skill" => Self::Skill,
            "Task" => Self::Task,
            "TodoWrite" => Self::TodoWrite,
            "AskUserQuestion" => Self::AskUserQuestion,
            "ExitPlanMode" => Self::ExitPlanMode,
            "EnterPlanMode" => Self::EnterPlanMode,
            _ => Self::Other,
        }
    }

    fn family(self) -> ToolFamily {
        match self {
            Self::Bash | Self::Read | Self::Glob | Self::Grep | Self::Ls => ToolFamily::FileCommand,
            Self::Edit | Self::Write | Self::NotebookEdit => ToolFamily::FileDiff,
            Self::WebSearch | Self::WebFetch => ToolFamily::Web,
            Self::Skill => ToolFamily::Skill,
            Self::Task => ToolFamily::Agent,
            Self::AskUserQuestion | Self::ExitPlanMode | Self::EnterPlanMode => ToolFamily::Form,
            Self::TodoWrite | Self::Other => ToolFamily::Default,
        }
    }
}

/// A Claude `tool_use` remembered until its `tool_result` arrives.
#[derive(Debug, Clone, PartialEq)]
pub(super) struct StartedClaudeTool {
    kind: ClaudeTool,
    name: String,
    pub(super) input: Option<Value>,
    /// Concatenated `input_json_delta` fragments for this tool.
    input_json: String,
}

impl StartedClaudeTool {
    pub(super) fn from_name_input(name: Option<&str>, input: Option<&Value>) -> Self {
        let name = clean_name(name);
        Self {
            kind: ClaudeTool::from_name(&name),
            name,
            input: bounded_input(input),
            input_json: String::new(),
        }
    }

    pub(super) fn from_block(block: &Value) -> Self {
        let name = block
            .get("name")
            .and_then(Value::as_str)
            .or_else(|| block.get("tool_name").and_then(Value::as_str));
        Self::from_name_input(name, block.get("input"))
    }

    pub(super) fn name(&self) -> &str {
        &self.name
    }

    /// Fill input only when it is still missing. Later snapshots must not
    /// clobber a payload assembled from `input_json_delta`.
    pub(super) fn apply_input(&mut self, input: Option<&Value>) -> bool {
        if self.input.is_some() {
            return false;
        }
        let Some(input) = bounded_input(input) else {
            return false;
        };
        self.input = Some(input);
        true
    }

    /// Append a JSON fragment. Returns true when parsed input changed.
    pub(super) fn push_input_json(&mut self, fragment: &str) -> bool {
        if fragment.is_empty() {
            return false;
        }
        let room = MAX_INPUT_JSON_CHARS.saturating_sub(self.input_json.len());
        if room == 0 {
            return false;
        }
        if fragment.len() <= room {
            self.input_json.push_str(fragment);
        } else {
            let end = floor_char_boundary(fragment, room);
            if end == 0 {
                return false;
            }
            self.input_json.push_str(&fragment[..end]);
        }
        let Some(value) = parse_assembled_input(&self.input_json) else {
            return false;
        };
        let Some(input) = bounded_input(Some(&value)) else {
            return false;
        };
        if self.input.as_ref() == Some(&input) {
            return false;
        }
        self.input = Some(input);
        true
    }

    fn header(&self, cwd: Option<&Path>) -> ToolHeader {
        match self.kind {
            ClaudeTool::Bash => {
                ToolHeader::shell("$", string_field(self.input.as_ref(), &["command", "cmd"]))
            }
            ClaudeTool::Task => {
                let identity = string_field(self.input.as_ref(), &["subagent_type", "agent"])
                    .filter(|value| !value.is_empty())
                    .unwrap_or_else(|| "Task".into());
                let detail = string_field(self.input.as_ref(), &["description", "prompt"])
                    .unwrap_or_default();
                ToolHeader::status_first(identity, detail)
            }
            _ => ToolHeader::call(self.name(), self.primary(cwd)),
        }
    }

    fn primary(&self, cwd: Option<&Path>) -> Option<String> {
        let input = self.input.as_ref();
        let primary = match self.kind {
            ClaudeTool::Read | ClaudeTool::Write | ClaudeTool::Edit => {
                display_path_field(input, &["file_path", "path"], cwd)
            }
            ClaudeTool::NotebookEdit => {
                display_path_field(input, &["notebook_path", "file_path"], cwd)
            }
            ClaudeTool::Ls => display_path_field(input, &["path"], cwd),
            ClaudeTool::Glob => string_field(input, &["pattern"]),
            ClaudeTool::Grep => grep_primary(input, cwd),
            ClaudeTool::WebSearch => {
                string_field(input, &["query"]).map(|query| quoted(&query, 80))
            }
            ClaudeTool::WebFetch => string_field(input, &["url"]).map(|url| truncate(&url, 80)),
            ClaudeTool::Skill => string_field(input, &["skill", "command", "name"]),
            _ => None,
        };
        primary.filter(|value| !value.is_empty())
    }

    fn populate_running(&self, card: &mut ToolCard) {
        match self.kind {
            ClaudeTool::Bash => push_bash_meta(card, self.input.as_ref()),
            ClaudeTool::TodoWrite => push_todo_facts(card, self.input.as_ref()),
            ClaudeTool::Read | ClaudeTool::Write | ClaudeTool::Edit => {
                if let Some(range) = read_range_fact(self.input.as_ref()) {
                    card.push_fact(range);
                }
            }
            _ => {}
        }
    }

    fn populate_finished(
        &self,
        card: &mut ToolCard,
        content_text: &str,
        tool_use_result: Option<&Value>,
    ) {
        let input = self.input.as_ref();
        match self.kind {
            ClaudeTool::Bash => {
                push_bash_meta(card, input);
                set_lines_body(card, content_text);
            }
            ClaudeTool::Read => {
                if let Some(range) = read_range_fact(input) {
                    card.push_fact(range);
                }
                let lines =
                    file_line_count(tool_use_result).or_else(|| count_nonempty_lines(content_text));
                if let Some(value) = lines {
                    card.push_fact(count_fact("line", "lines", value, None));
                }
            }
            ClaudeTool::Glob => {
                let files = filenames_from_result(tool_use_result);
                let value = u64_field(tool_use_result, &["numFiles", "num_files"])
                    .or_else(|| files.as_ref().map(|names| names.len() as u64))
                    .or_else(|| count_nonempty_lines(content_text));
                if let Some(value) = value {
                    card.push_fact(count_fact("file", "files", value, None));
                }
                if let Some(files) = files.filter(|names| !names.is_empty()) {
                    set_lines_body(card, &files.join("\n"));
                } else {
                    set_lines_body(card, content_text);
                }
            }
            ClaudeTool::Grep => push_grep_result(card, input, content_text, tool_use_result),
            ClaudeTool::Edit | ClaudeTool::Write | ClaudeTool::NotebookEdit => {
                push_diff_result(card, self.kind, input, content_text, tool_use_result);
            }
            ClaudeTool::WebSearch | ClaudeTool::WebFetch => {
                if let Some(value) = u64_field(tool_use_result, &["resultCount", "numResults"])
                    .or_else(|| count_nonempty_lines(content_text))
                {
                    card.push_fact(count_fact("result", "results", value, None));
                }
                set_lines_body(card, content_text);
            }
            ClaudeTool::TodoWrite => push_todo_facts(card, input),
            _ => set_lines_body(card, content_text),
        }
    }
}

pub(super) fn started_card(tool: &StartedClaudeTool, cwd: Option<&Path>) -> ToolCard {
    build_card(tool, ToolStatus::Running, "", None, cwd)
}

pub(super) fn finished_card(
    tool: Option<&StartedClaudeTool>,
    ok: bool,
    content_text: &str,
    tool_use_result: Option<&Value>,
    cwd: Option<&Path>,
) -> ToolCard {
    let fallback = StartedClaudeTool::from_name_input(None, None);
    let tool = tool.unwrap_or(&fallback);
    build_card(
        tool,
        ToolStatus::from_finished(ok),
        content_text,
        tool_use_result,
        cwd,
    )
}

fn build_card(
    tool: &StartedClaudeTool,
    status: ToolStatus,
    content_text: &str,
    tool_use_result: Option<&Value>,
    cwd: Option<&Path>,
) -> ToolCard {
    let mut card = ToolCard::new(status, tool.kind.family(), tool.header(cwd));
    match status {
        ToolStatus::Error => push_error_output(&mut card, content_text),
        ToolStatus::Running => tool.populate_running(&mut card),
        ToolStatus::Ok | ToolStatus::Interrupted => {
            tool.populate_finished(&mut card, content_text, tool_use_result);
        }
    }
    card
}

fn push_bash_meta(card: &mut ToolCard, input: Option<&Value>) {
    if let Some(description) = string_field(input, &["description"]) {
        card.push_fact(ToolFact::Meta { text: description });
    }
    if let Some(timeout_ms) = u64_field(input, &["timeout"]) {
        card.push_fact(ToolFact::Timeout {
            seconds: Some(timeout_ms.saturating_add(999) / 1000),
        });
    }
    if input
        .and_then(|value| value.get("run_in_background"))
        .and_then(Value::as_bool)
        == Some(true)
    {
        card.push_fact(ToolFact::Meta {
            text: "background".into(),
        });
    }
}

fn push_grep_result(
    card: &mut ToolCard,
    input: Option<&Value>,
    content_text: &str,
    tool_use_result: Option<&Value>,
) {
    let match_lines = u64_field(tool_use_result, &["numMatches", "matchCount"])
        .or_else(|| count_nonempty_lines(content_text))
        .unwrap_or(0);
    let file_count = u64_field(tool_use_result, &["numFiles", "fileCount"]);
    let detail =
        file_count.map(|files| format!("in {files} {}", if files == 1 { "file" } else { "files" }));
    card.push_fact(count_fact("match", "matches", match_lines, detail));
    set_lines_body(card, content_text);
    if let Some(pattern) = string_field(input, &["pattern"]) {
        let case_sensitive = !input
            .and_then(|value| {
                value
                    .get("-i")
                    .or_else(|| value.get("case_insensitive"))
                    .and_then(Value::as_bool)
            })
            .unwrap_or(false);
        card.match_pattern = Some(pattern);
        card.match_literal = false;
        card.match_case_sensitive = case_sensitive;
    }
}

fn push_diff_result(
    card: &mut ToolCard,
    kind: ClaudeTool,
    input: Option<&Value>,
    content_text: &str,
    tool_use_result: Option<&Value>,
) {
    if let Some(rows) = diff_rows_from_result(tool_use_result, input, kind) {
        let (added, removed) = diff_row_stats(&rows);
        card.push_fact(ToolFact::DiffStat {
            added,
            removed,
            path: None,
        });
        if !rows.is_empty() {
            card.body = ToolBody::Diff(rows);
        }
        return;
    }
    set_lines_body(card, content_text);
}

fn diff_rows_from_result(
    tool_use_result: Option<&Value>,
    input: Option<&Value>,
    kind: ClaudeTool,
) -> Option<Vec<DiffRow>> {
    if let Some(rows) = structured_patch_rows(tool_use_result) {
        return Some(rows);
    }
    if matches!(kind, ClaudeTool::Write) {
        return write_result_rows(tool_use_result, input);
    }
    old_new_string_rows(input)
}

fn write_result_is_create(tool_use_result: Option<&Value>) -> bool {
    matches!(
        string_field(tool_use_result, &["type"]).as_deref(),
        Some("create")
    )
}

fn write_result_is_update(tool_use_result: Option<&Value>) -> bool {
    matches!(
        string_field(tool_use_result, &["type"]).as_deref(),
        Some("update")
    ) || string_field(tool_use_result, &["originalFile", "original_file"]).is_some()
}

fn write_result_rows(
    tool_use_result: Option<&Value>,
    input: Option<&Value>,
) -> Option<Vec<DiffRow>> {
    if write_result_is_update(tool_use_result) {
        return write_update_rows(tool_use_result, input);
    }
    if !write_result_is_create(tool_use_result) {
        return None;
    }
    let content = string_field(input, &["content"])
        .or_else(|| string_field(tool_use_result, &["content"]))?;
    let rows = line_rows(&content, DiffRowKind::Added);
    (!rows.is_empty()).then_some(rows)
}

fn write_update_rows(
    tool_use_result: Option<&Value>,
    input: Option<&Value>,
) -> Option<Vec<DiffRow>> {
    let old = string_field(tool_use_result, &["originalFile", "original_file"])?;
    let new = string_field(tool_use_result, &["content"])
        .or_else(|| string_field(input, &["content"]))?;
    replace_rows(&old, &new)
}

fn structured_patch_rows(tool_use_result: Option<&Value>) -> Option<Vec<DiffRow>> {
    let patch = tool_use_result
        .and_then(|value| value.get("structuredPatch"))
        .or_else(|| tool_use_result.and_then(|value| value.get("structured_patch")))?;
    let hunks = patch.as_array()?;
    if hunks.is_empty() {
        return None;
    }
    let mut rows = Vec::new();
    for (index, hunk) in hunks.iter().enumerate() {
        if index > 0 {
            rows.push(DiffRow::new(DiffRowKind::Skip, None, ""));
        }
        let lines = hunk.get("lines").and_then(Value::as_array)?;
        let mut old_line = u32_field(hunk, "oldStart").unwrap_or(1);
        let mut new_line = u32_field(hunk, "newStart").unwrap_or(1);
        for line in lines {
            let Some(text) = line.as_str() else {
                continue;
            };
            let (kind, content, line_no) = match text.as_bytes().first().copied() {
                Some(b'+') => (DiffRowKind::Added, tail_after_marker(text), new_line),
                Some(b'-') => (DiffRowKind::Removed, tail_after_marker(text), old_line),
                Some(b' ') => (DiffRowKind::Context, tail_after_marker(text), new_line),
                _ => continue,
            };
            match kind {
                DiffRowKind::Added => new_line = new_line.saturating_add(1),
                DiffRowKind::Removed => old_line = old_line.saturating_add(1),
                DiffRowKind::Context => {
                    old_line = old_line.saturating_add(1);
                    new_line = new_line.saturating_add(1);
                }
                _ => {}
            }
            rows.push(DiffRow::new(kind, Some(line_no), content));
        }
    }
    (!rows.is_empty()).then_some(rows)
}

fn old_new_string_rows(input: Option<&Value>) -> Option<Vec<DiffRow>> {
    let old = string_field(input, &["old_string", "oldString"])?;
    let new = string_field(input, &["new_string", "newString"])?;
    replace_rows(&old, &new)
}

fn replace_rows(old: &str, new: &str) -> Option<Vec<DiffRow>> {
    let mut rows = line_rows(old, DiffRowKind::Removed);
    rows.extend(line_rows(new, DiffRowKind::Added));
    (!rows.is_empty()).then_some(rows)
}

fn line_rows(text: &str, kind: DiffRowKind) -> Vec<DiffRow> {
    text.lines()
        .enumerate()
        .map(|(index, line)| {
            DiffRow::new(
                kind,
                Some(u32::try_from(index + 1).unwrap_or(u32::MAX)),
                line,
            )
        })
        .collect()
}

fn tail_after_marker(text: &str) -> &str {
    text.get(1..).unwrap_or_default()
}

fn u32_field(value: &Value, key: &str) -> Option<u32> {
    value
        .get(key)
        .and_then(Value::as_u64)
        .and_then(|value| u32::try_from(value).ok())
}

fn diff_row_stats(rows: &[DiffRow]) -> (u64, u64) {
    let mut added = 0;
    let mut removed = 0;
    for row in rows {
        match row.kind {
            DiffRowKind::Added => added += 1,
            DiffRowKind::Removed => removed += 1,
            _ => {}
        }
    }
    (added, removed)
}

fn push_todo_facts(card: &mut ToolCard, input: Option<&Value>) {
    let Some(todos) = input
        .and_then(|value| value.get("todos"))
        .and_then(Value::as_array)
    else {
        return;
    };
    /// Visible todo rows on the card. Extra items collapse to a count.
    const MAX_TODO_FACTS: usize = 10;
    for todo in todos.iter().take(MAX_TODO_FACTS) {
        let text = string_field(Some(todo), &["content", "activeForm"]).unwrap_or_default();
        if text.is_empty() {
            continue;
        }
        let status = string_field(Some(todo), &["status"]).unwrap_or_default();
        let marker = match status.as_str() {
            "completed" | "complete" => "",
            "in_progress" | "in-progress" => "",
            _ => "",
        };
        card.push_fact(ToolFact::Text {
            text: format!("{marker} {text}"),
        });
    }
    if todos.len() > MAX_TODO_FACTS {
        card.push_fact(ToolFact::Meta {
            text: format!("{} more", todos.len() - MAX_TODO_FACTS),
        });
    }
}

fn read_range_fact(input: Option<&Value>) -> Option<ToolFact> {
    let offset = u64_field(input, &["offset"]);
    let limit = u64_field(input, &["limit"]);
    match (offset, limit) {
        (None, None) => None,
        (offset, limit) => {
            let start = offset.unwrap_or(1);
            let text = match limit {
                Some(limit) => format!("{start}-{}", start.saturating_add(limit).saturating_sub(1)),
                None => format!("{start}-"),
            };
            Some(ToolFact::Meta { text })
        }
    }
}

fn file_line_count(tool_use_result: Option<&Value>) -> Option<u64> {
    tool_use_result
        .and_then(|value| value.get("file"))
        .and_then(|file| {
            file.get("numLines")
                .or_else(|| file.get("totalLines"))
                .or_else(|| file.get("num_lines"))
        })
        .and_then(Value::as_u64)
}

fn filenames_from_result(tool_use_result: Option<&Value>) -> Option<Vec<String>> {
    let names = tool_use_result
        .and_then(|value| value.get("filenames"))
        .or_else(|| tool_use_result.and_then(|value| value.get("files")))?
        .as_array()?;
    let files = names
        .iter()
        .filter_map(|value| value.as_str().map(str::to_string))
        .filter(|name| !name.is_empty())
        .collect::<Vec<_>>();
    Some(files)
}

fn set_lines_body(card: &mut ToolCard, content_text: &str) {
    if content_text.trim().is_empty() {
        return;
    }
    card.body = ToolBody::Lines(truncate_payload_lines(content_text, MAX_TOOL_BODY_LINES));
}

fn push_error_output(card: &mut ToolCard, content: &str) {
    let trimmed = content.trim();
    if trimmed.is_empty() {
        return;
    }
    let lines = truncate_payload_lines(trimmed, MAX_TOOL_BODY_LINES);
    let Some(first) = lines.first() else {
        return;
    };
    let summary = truncate(first.trim(), 160);
    card.push_fact(ToolFact::Error {
        text: summary.clone(),
    });
    if lines.len() > 1 {
        card.body = ToolBody::Lines(lines[1..].to_vec());
    }
}

fn grep_primary(input: Option<&Value>, cwd: Option<&Path>) -> Option<String> {
    let pattern = string_field(input, &["pattern"])?;
    match display_path_field(input, &["path"], cwd) {
        Some(path) => Some(format!("{pattern}, {path}")),
        None => Some(pattern),
    }
}

fn display_path_field(input: Option<&Value>, keys: &[&str], cwd: Option<&Path>) -> Option<String> {
    let path = string_field(input, keys)?;
    Some(match cwd {
        Some(cwd) => compact_display_path(cwd, &path),
        None => path,
    })
}

fn string_field(input: Option<&Value>, keys: &[&str]) -> Option<String> {
    let input = input?;
    keys.iter().find_map(|key| {
        input
            .get(*key)
            .and_then(Value::as_str)
            .map(str::trim)
            .filter(|value| !value.is_empty())
            .map(str::to_string)
    })
}

fn u64_field(input: Option<&Value>, keys: &[&str]) -> Option<u64> {
    let input = input?;
    keys.iter()
        .find_map(|key| input.get(*key).and_then(Value::as_u64))
}

fn count_fact(singular: &str, plural: &str, value: u64, detail: Option<String>) -> ToolFact {
    ToolFact::Count {
        label: if value == 1 {
            singular.into()
        } else {
            plural.into()
        },
        value,
        detail,
    }
}

fn count_nonempty_lines(content: &str) -> Option<u64> {
    let count = content
        .lines()
        .filter(|line| !line.trim().is_empty())
        .count() as u64;
    (count > 0).then_some(count)
}

/// Parse assembled `input_json_delta` text. Truncated objects keep leading
/// keys when a small closer produces valid JSON.
fn parse_assembled_input(raw: &str) -> Option<Value> {
    if let Ok(value) = serde_json::from_str(raw) {
        return Some(value);
    }
    for suffix in ["}", "\"}"] {
        if let Ok(value) = serde_json::from_str::<Value>(&format!("{raw}{suffix}")) {
            if value.is_object() {
                return Some(value);
            }
        }
    }
    None
}

/// Fields at or under this encoded size are presentation metadata and are
/// kept even when a sibling body field overflows the payload budget.
const SMALL_INPUT_FIELD_CHARS: usize = 512;

fn bounded_input(input: Option<&Value>) -> Option<Value> {
    let value = input.filter(|value| !value.is_null())?;
    if value.as_object().is_some_and(serde_json::Map::is_empty) {
        return None;
    }
    let serialized = serde_json::to_string(value).ok()?;
    if serialized.len() <= MAX_TOOL_PAYLOAD_CHARS {
        return Some(value.clone());
    }
    bound_oversized_object(value)
}

fn bound_oversized_object(value: &Value) -> Option<Value> {
    let object = value.as_object()?;
    let mut kept = serde_json::Map::new();
    let mut large_strings = Vec::new();
    for (key, field) in object {
        if let Value::String(text) = field {
            if encoded_len(field) > SMALL_INPUT_FIELD_CHARS {
                large_strings.push((key.clone(), text.clone()));
                continue;
            }
        }
        if encoded_len(field) <= SMALL_INPUT_FIELD_CHARS && object_fits(&kept, key, field) {
            kept.insert(key.clone(), field.clone());
        }
    }
    for (key, text) in large_strings {
        if let Some(field) = largest_fitting_string(&kept, &key, &text) {
            kept.insert(key, field);
        }
    }
    (!kept.is_empty()).then_some(Value::Object(kept))
}

/// Longest prefix of `text` whose JSON object still fits the payload budget.
fn largest_fitting_string(
    kept: &serde_json::Map<String, Value>,
    key: &str,
    text: &str,
) -> Option<Value> {
    let full = Value::String(text.to_string());
    if object_fits(kept, key, &full) {
        return Some(full);
    }
    let chars = text.chars().collect::<Vec<_>>();
    let mut lo = 0;
    let mut hi = chars.len();
    let mut best = None;
    while lo < hi {
        let mid = lo + (hi - lo).div_ceil(2);
        let prefix = chars[..mid].iter().collect::<String>();
        let field = Value::String(prefix);
        if object_fits(kept, key, &field) {
            best = Some(field);
            lo = mid;
        } else {
            hi = mid - 1;
        }
    }
    best
}

fn encoded_len(value: &Value) -> usize {
    serde_json::to_string(value)
        .map(|encoded| encoded.len())
        .unwrap_or(usize::MAX)
}

fn encoded_object_len(object: &serde_json::Map<String, Value>) -> usize {
    encoded_len(&Value::Object(object.clone()))
}

fn object_fits(kept: &serde_json::Map<String, Value>, key: &str, field: &Value) -> bool {
    let mut probe = kept.clone();
    probe.insert(key.to_string(), field.clone());
    encoded_object_len(&probe) <= MAX_TOOL_PAYLOAD_CHARS
}

fn clean_name(name: Option<&str>) -> String {
    name.map(str::trim)
        .filter(|name| !name.is_empty())
        .unwrap_or("tool")
        .to_string()
}

fn quoted(text: &str, max_chars: usize) -> String {
    format!("\"{}\"", truncate(text, max_chars.saturating_sub(2)))
}

fn truncate(text: &str, max_chars: usize) -> String {
    if text.chars().count() <= max_chars {
        return text.to_string();
    }
    let mut out = text
        .chars()
        .take(max_chars.saturating_sub(1))
        .collect::<String>();
    out.push('');
    out
}

#[cfg(test)]
#[path = "tool_cards_tests.rs"]
mod tests;