koda-core 0.2.23

Core engine for the Koda AI coding agent (macOS and Linux only)
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
//! Single source of truth for "what does a tool call display?".
//!
//! Pre-#1100 (this module), every rendering surface — TUI live header,
//! transcript export, history replay — re-parsed the raw tool-call
//! JSON args independently. Each surface had its own
//! `first_string(args, &["file_path", "path", "directory"])` lookup
//! list, and they drifted twice in two days:
//!
//! - **#1094 → #1096**: the `List` LLM-facing body lost its
//!   `Listing: <path>` header, leaving the model unable to tell
//!   parallel `List` calls apart.
//! - **#1099**: the TUI header read the wrong arg keys for
//!   `List`/`Grep`/`Glob`, rendering every call as `● List .`
//!   regardless of the actual path. This *hid* the loop-spin bug
//!   #1102 for 8 days because every iteration's `List
//!   /Users/lijun/repo` looked identical to `List .`.
//!
//! Both bugs had the same shape — "a layer that displays a tool call
//! looked at the wrong key in the args JSON" — and required separate
//! fixes in separate files. The root cause was duplication: each
//! rendering layer owned its own JSON parser.
//!
//! ## What this module owns
//!
//! `ToolCallSummary` is a pure data struct that captures everything
//! a renderer needs to know about a tool call:
//!
//! - The tool's name (e.g. `"List"`, `"Grep"`).
//! - The structured payload, in `ToolCallKind` — one variant per
//!   tool family that has its own display shape.
//!
//! `ToolCallSummary::from_call` is the **only** place that knows
//! which arg keys mean "the path" or "the search pattern" for each
//! tool. If a tool's schema renames `file_path` → `target` tomorrow,
//! one constructor changes and every renderer follows.
//!
//! ## What this module does NOT own
//!
//! No rendering knowledge — no `ratatui::Span`, no ANSI colors, no
//! truncation. Renderers (currently `koda-cli/src/tool_header.rs`)
//! pattern-match on the `ToolCallKind` enum and produce their own
//! medium-specific output. This keeps `koda-core` headless and means
//! a future ACP/JSON renderer can plug in without touching this
//! module at all.

use serde_json::Value;

/// A renderer-agnostic description of a tool call's display payload.
///
/// Built once by [`ToolCallSummary::from_call`] from the raw JSON
/// args; consumed by every rendering surface (TUI header, transcript,
/// history replay) via pattern matching on [`Self::kind`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToolCallSummary {
    /// Tool name as the dispatcher sees it (e.g. `"List"`, `"Grep"`).
    pub name: String,
    /// Structured per-tool payload.
    pub kind: ToolCallKind,
}

/// Per-tool display payload — one variant per shape, not per tool.
///
/// `Read`/`Write`/`Edit`/`Delete` all share `Path` because they all
/// display "one file path." `Grep` gets its own variant because it
/// has both a pattern and a directory. Variants are added only when
/// a tool's display shape genuinely differs; otherwise we reuse.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ToolCallKind {
    /// `Bash` — a single command string.
    Bash {
        /// The shell command to execute. Empty string means the args
        /// didn't carry a recognized command/cmd key.
        command: String,
    },
    /// `Read` / `Write` / `Edit` / `Delete` — a single file path.
    /// Empty string means the args didn't carry a recognized key.
    Path {
        /// The file path the operation targets.
        path: String,
    },
    /// `Grep` — a search pattern in a directory.
    /// `dir` defaults to `"."` when the args omit it.
    Grep {
        /// The search pattern (regex or literal, tool-defined).
        pattern: String,
        /// The directory to search in; `"."` when omitted.
        dir: String,
    },
    /// `Glob` — a glob pattern, optionally rooted at `base`.
    /// `base` is `None` when the call uses the project root default.
    Glob {
        /// The glob pattern (e.g. `"**/*.rs"`).
        pattern: String,
        /// Optional base directory to root the glob at.
        base: Option<String>,
    },
    /// `List` — a directory path. Defaults to `"."` when omitted.
    List {
        /// The directory to list; `"."` when omitted from args.
        dir: String,
    },
    /// `WebFetch` — a URL.
    WebFetch {
        /// The URL to fetch.
        url: String,
    },
    /// Fallback for tools without a specialized shape: the first
    /// string-valued argument (object iteration order), or `None`
    /// if the args have no string values.
    Generic {
        /// First string-valued argument in object-iteration order,
        /// or `None` if no such value exists.
        value: Option<String>,
    },
}

impl ToolCallSummary {
    /// Parse a tool call into its display payload.
    ///
    /// **This is the only function that knows arg-key conventions.**
    /// Adding a new path-bearing tool means adding one match arm
    /// here; renderers pick it up automatically.
    ///
    /// Key-list conventions (must match the dispatcher's lookup
    /// order in `koda-core/src/tools/*.rs`):
    ///
    /// | Tool                        | Path keys                              | Pattern keys                  |
    /// |-----------------------------|----------------------------------------|-------------------------------|
    /// | `Read`/`Write`/`Edit`/`Delete` | `file_path`, `path`                 | —                             |
    /// | `Grep`                      | `file_path`, `path`, `directory`       | `search_string`, `pattern`    |
    /// | `Glob`                      | `file_path`, `path`, `directory`       | `pattern`                     |
    /// | `List`                      | `file_path`, `path`, `directory`       | —                             |
    /// | `Bash`                      | —                                      | `command`, `cmd`              |
    /// | `WebFetch`                  | —                                      | `url`                         |
    pub fn from_call(name: &str, args: &Value) -> Self {
        let kind = match name {
            "Bash" => ToolCallKind::Bash {
                command: first_string(args, &["command", "cmd"]).unwrap_or_default(),
            },
            "Read" | "Write" | "Edit" | "Delete" => ToolCallKind::Path {
                path: first_string(args, &["file_path", "path"]).unwrap_or_default(),
            },
            "Grep" => ToolCallKind::Grep {
                pattern: first_string(args, &["search_string", "pattern"]).unwrap_or_default(),
                dir: first_string(args, &["file_path", "path", "directory"])
                    .unwrap_or_else(|| ".".to_string()),
            },
            "Glob" => ToolCallKind::Glob {
                pattern: first_string(args, &["pattern"]).unwrap_or_default(),
                base: first_string(args, &["file_path", "path", "directory"]),
            },
            "List" => ToolCallKind::List {
                dir: first_string(args, &["file_path", "path", "directory"])
                    .unwrap_or_else(|| ".".to_string()),
            },
            "WebFetch" => ToolCallKind::WebFetch {
                url: first_string(args, &["url"]).unwrap_or_default(),
            },
            _ => ToolCallKind::Generic {
                value: first_string_in_object(args),
            },
        };
        Self {
            name: name.to_string(),
            kind,
        }
    }
}

/// Return the first present string value among the candidate keys.
fn first_string(args: &Value, keys: &[&str]) -> Option<String> {
    keys.iter()
        .find_map(|k| args.get(k).and_then(|v| v.as_str()).map(|s| s.to_string()))
}

/// Return the first string value in object-iteration order, or `None`
/// if the args aren't an object or have no string values.
///
/// Used as the `Generic` fallback for tools we don't have a
/// specialized shape for. Object-iteration order is `serde_json`'s
/// insertion-preserving order, which means the first key the tool's
/// schema declares wins — usually the most informative one.
fn first_string_in_object(args: &Value) -> Option<String> {
    args.as_object()?
        .iter()
        .find_map(|(_, v)| v.as_str().map(|s| s.to_string()))
}

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

    // ── Bash ──────────────────────────────────────────────────────────

    #[test]
    fn bash_reads_command_key() {
        let s = ToolCallSummary::from_call("Bash", &json!({ "command": "ls -la" }));
        assert_eq!(
            s.kind,
            ToolCallKind::Bash {
                command: "ls -la".into()
            }
        );
    }

    #[test]
    fn bash_falls_back_to_cmd_alias() {
        let s = ToolCallSummary::from_call("Bash", &json!({ "cmd": "echo hi" }));
        assert_eq!(
            s.kind,
            ToolCallKind::Bash {
                command: "echo hi".into()
            }
        );
    }

    #[test]
    fn bash_with_no_command_yields_empty() {
        let s = ToolCallSummary::from_call("Bash", &json!({}));
        assert_eq!(
            s.kind,
            ToolCallKind::Bash {
                command: String::new()
            }
        );
    }

    // ── Path family (Read/Write/Edit/Delete) ──────────────────────────

    #[test]
    fn read_write_edit_delete_share_path_shape() {
        for name in ["Read", "Write", "Edit", "Delete"] {
            let s = ToolCallSummary::from_call(name, &json!({ "file_path": "src/foo.rs" }));
            assert_eq!(
                s.kind,
                ToolCallKind::Path {
                    path: "src/foo.rs".into()
                },
                "{name} should produce ToolCallKind::Path"
            );
        }
    }

    #[test]
    fn path_falls_back_to_path_alias() {
        let s = ToolCallSummary::from_call("Read", &json!({ "path": "legacy.rs" }));
        assert_eq!(
            s.kind,
            ToolCallKind::Path {
                path: "legacy.rs".into()
            }
        );
    }

    // ── Grep ──────────────────────────────────────────────────────────

    #[test]
    fn grep_reads_search_string_and_file_path() {
        let s = ToolCallSummary::from_call(
            "Grep",
            &json!({ "search_string": "TODO", "file_path": "src/" }),
        );
        assert_eq!(
            s.kind,
            ToolCallKind::Grep {
                pattern: "TODO".into(),
                dir: "src/".into()
            }
        );
    }

    #[test]
    fn grep_pattern_alias_works_for_legacy_callers() {
        let s = ToolCallSummary::from_call(
            "Grep",
            &json!({ "pattern": "fn main", "directory": "src/" }),
        );
        assert_eq!(
            s.kind,
            ToolCallKind::Grep {
                pattern: "fn main".into(),
                dir: "src/".into()
            }
        );
    }

    #[test]
    fn grep_default_directory_is_dot() {
        let s = ToolCallSummary::from_call("Grep", &json!({ "search_string": "x" }));
        assert_eq!(
            s.kind,
            ToolCallKind::Grep {
                pattern: "x".into(),
                dir: ".".into()
            }
        );
    }

    // ── Glob ──────────────────────────────────────────────────────────

    #[test]
    fn glob_with_no_base_leaves_base_none() {
        let s = ToolCallSummary::from_call("Glob", &json!({ "pattern": "*.rs" }));
        assert_eq!(
            s.kind,
            ToolCallKind::Glob {
                pattern: "*.rs".into(),
                base: None
            }
        );
    }

    #[test]
    fn glob_surfaces_file_path_as_base_when_present() {
        let s = ToolCallSummary::from_call(
            "Glob",
            &json!({ "pattern": "*.toml", "file_path": "koda-cli/" }),
        );
        assert_eq!(
            s.kind,
            ToolCallKind::Glob {
                pattern: "*.toml".into(),
                base: Some("koda-cli/".into()),
            }
        );
    }

    // ── List ──────────────────────────────────────────────────────────

    #[test]
    fn list_default_directory_is_dot() {
        let s = ToolCallSummary::from_call("List", &json!({}));
        assert_eq!(s.kind, ToolCallKind::List { dir: ".".into() });
    }

    #[test]
    fn list_uses_file_path_key_from_schema() {
        let s = ToolCallSummary::from_call("List", &json!({ "file_path": "koda-core/src/" }));
        assert_eq!(
            s.kind,
            ToolCallKind::List {
                dir: "koda-core/src/".into()
            }
        );
    }

    // ── WebFetch ──────────────────────────────────────────────────────

    #[test]
    fn webfetch_reads_url() {
        let s = ToolCallSummary::from_call("WebFetch", &json!({ "url": "https://example.com" }));
        assert_eq!(
            s.kind,
            ToolCallKind::WebFetch {
                url: "https://example.com".into()
            }
        );
    }

    // ── Generic fallback ──────────────────────────────────────────────

    #[test]
    fn generic_picks_first_string_value_in_object_order() {
        let s = ToolCallSummary::from_call("UnknownTool", &json!({ "a": "first", "b": "second" }));
        assert_eq!(
            s.kind,
            ToolCallKind::Generic {
                value: Some("first".into())
            }
        );
    }

    #[test]
    fn generic_with_no_string_values_yields_none() {
        let s = ToolCallSummary::from_call("UnknownTool", &json!({ "n": 42 }));
        assert_eq!(s.kind, ToolCallKind::Generic { value: None });
    }

    #[test]
    fn generic_with_non_object_args_yields_none() {
        let s = ToolCallSummary::from_call("UnknownTool", &json!("just a string"));
        assert_eq!(s.kind, ToolCallKind::Generic { value: None });
    }

    // ── Pinning tests for the bug class this module exists to prevent ─

    /// Regression test for the #1099 bug class: every path-bearing
    /// tool MUST honor `file_path` (the schema-blessed key the
    /// dispatcher actually reads) before any legacy alias. Pre-fix
    /// the renderers checked obsolete keys first and silently
    /// rendered `.` for every call.
    ///
    /// This is the structural equivalent of
    /// `path_bearing_tools_render_actual_dispatch_key` in
    /// `tool_header.rs` — that test pins the renderer's output;
    /// this one pins the data layer the renderer reads from.
    #[test]
    fn path_bearing_tools_honor_file_path_key() {
        let cases = [
            (
                "List",
                json!({ "file_path": "alpha", "path": "WRONG", "directory": "WRONG" }),
                "alpha",
            ),
            (
                "Grep",
                json!({
                    "search_string": "x",
                    "file_path": "bravo",
                    "path": "WRONG",
                    "directory": "WRONG",
                }),
                "bravo",
            ),
            (
                "Glob",
                json!({ "pattern": "*", "file_path": "charlie", "path": "WRONG" }),
                "charlie",
            ),
            (
                "Read",
                json!({ "file_path": "delta", "path": "WRONG" }),
                "delta",
            ),
        ];

        for (name, args, expected) in cases {
            let s = ToolCallSummary::from_call(name, &args);
            let actual = match &s.kind {
                ToolCallKind::List { dir } => dir.clone(),
                ToolCallKind::Grep { dir, .. } => dir.clone(),
                ToolCallKind::Glob { base, .. } => base.clone().unwrap_or_default(),
                ToolCallKind::Path { path } => path.clone(),
                other => panic!("{name} produced unexpected kind {other:?}"),
            };
            assert_eq!(
                actual, expected,
                "{name}: must read `file_path` first — that's the key the dispatcher reads. \
                 Pre-#1099, renderers checked obsolete keys first and silently rendered \
                 wrong paths for every call."
            );
        }
    }
}