coding_agent_tools 0.4.0

Coding agent tools (CLI + MCP). First tool: ls.
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
//! Recipe execution logic.
//!
//! Handles recipe resolution, ambiguity detection, argument mapping, and execution.

use super::cache::JustRegistry;
use super::parser::ParamKind;
use super::security::SecurityValidator;
use super::types::ExecuteOutput;
use crate::paths;
use agentic_tools_core::ToolContext;
use serde_json::Value;
use std::collections::HashMap;
use tokio::io::AsyncReadExt;
use tokio::process::Command;

/// Execute a recipe by name, with optional directory disambiguation and arguments.
///
/// When no dir is specified, defaults to the root repository's justfile if the recipe exists there.
/// Only errors for ambiguity if recipe is not in root AND exists in multiple subdirectories.
#[expect(
    clippy::implicit_hasher,
    reason = "HashMap with default hasher is simpler for MCP tool API"
)]
#[expect(
    clippy::similar_names,
    reason = "args and argv are distinct: args is input, argv is CLI"
)]
pub async fn execute_recipe(
    registry: &JustRegistry,
    recipe_name: &str,
    dir_opt: Option<String>,
    args_opt: Option<HashMap<String, Value>>,
    repo_root: &str,
    ctx: &ToolContext,
) -> Result<ExecuteOutput, String> {
    // Canonicalize to resolve symlinks (e.g., /var -> /private/var on macOS)
    let repo_root = paths::to_abs_string(repo_root)?;
    let all = registry.get_all_recipes(&repo_root).await?;

    // Filter by name and visibility
    let mut candidates: Vec<_> = all
        .into_iter()
        .filter(|(_, r)| r.name == recipe_name && !r.is_private && !r.is_mcp_hidden)
        .collect();

    // Apply dir filter if provided
    if let Some(ref dir) = dir_opt {
        let abs_dir = paths::to_abs_string(dir)?;
        candidates.retain(|(d, _)| d == &abs_dir);
    }

    if candidates.is_empty() {
        return Err(format!(
            "Recipe '{recipe_name}' not found or not exposed. Use just_search(query='{recipe_name}') to discover available recipes."
        ));
    }

    // Select recipe: prefer root dir if no dir specified and multiple exist
    let unique_dirs: std::collections::HashSet<_> =
        candidates.iter().map(|(d, _)| d.as_str()).collect();

    let chosen_idx = if unique_dirs.len() > 1 && dir_opt.is_none() {
        // No dir specified and multiple candidates - prefer root repo justfile
        if let Some(idx) = candidates.iter().position(|(d, _)| d == &repo_root) {
            idx
        } else {
            // Recipe not in root, genuine ambiguity
            let dirs_list = unique_dirs
                .into_iter()
                .map(|d| format!("  - {d}"))
                .collect::<Vec<_>>()
                .join("\n");
            return Err(format!(
                "Recipe '{recipe_name}' not in root justfile and exists in multiple directories:\n{dirs_list}\nSpecify dir parameter to disambiguate."
            ));
        }
    } else {
        0
    };

    let (chosen_dir, recipe) = candidates.swap_remove(chosen_idx);

    // Validate args
    let args = args_opt.unwrap_or_default();
    SecurityValidator::default().validate(&args)?;

    // Compute expected param names and find unused arg keys for better error messages
    let expected_params: std::collections::HashSet<&str> =
        recipe.params.iter().map(|p| p.name.as_str()).collect();
    let unused_keys: Vec<&str> = args
        .keys()
        .filter(|k| !expected_params.contains(k.as_str()))
        .map(std::string::String::as_str)
        .collect();

    // Build argv
    let mut argv = vec![recipe_name.to_string()];
    for p in &recipe.params {
        if let Some(val) = args.get(&p.name) {
            match p.kind {
                ParamKind::Star => {
                    if let Value::Array(items) = val {
                        for item in items {
                            argv.push(value_to_arg(item)?);
                        }
                    } else {
                        argv.push(value_to_arg(val)?);
                    }
                }
                ParamKind::Singular => {
                    argv.push(value_to_arg(val)?);
                }
            }
        } else if !p.has_default {
            use std::fmt::Write;
            let mut err_msg = format!(
                "Missing required argument '{}' for recipe '{}'.",
                p.name, recipe_name
            );
            if !unused_keys.is_empty() {
                let _ = write!(
                    err_msg,
                    " You provided key(s) {unused_keys:?} which didn't match any parameter."
                );
            }
            let param_names: Vec<&str> = recipe.params.iter().map(|p| p.name.as_str()).collect();
            let _ = write!(err_msg, " Expected parameter(s): {param_names:?}");
            return Err(err_msg);
        }
    }

    let mut child = Command::new("just")
        .args(&argv)
        .current_dir(&chosen_dir)
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .kill_on_drop(true)
        .spawn()
        .map_err(|e| format!("Failed to execute just: {e}"))?;

    let stdout = child
        .stdout
        .take()
        .ok_or_else(|| "Failed to capture just stdout".to_string())?;
    let stderr = child
        .stderr
        .take()
        .ok_or_else(|| "Failed to capture just stderr".to_string())?;

    let stdout_task = tokio::spawn(async move {
        let mut stdout = stdout;
        let mut bytes = Vec::new();
        stdout.read_to_end(&mut bytes).await.map(|_| bytes)
    });
    let stderr_task = tokio::spawn(async move {
        let mut stderr = stderr;
        let mut bytes = Vec::new();
        stderr.read_to_end(&mut bytes).await.map(|_| bytes)
    });

    let status = tokio::select! {
        () = ctx.cancelled() => {
            let _ = child.kill().await;
            let _ = child.wait().await;
            stdout_task.abort();
            stderr_task.abort();
            return Err("Just execution cancelled".to_string());
        }
        status = child.wait() => status.map_err(|e| format!("Failed to execute just: {e}"))?,
    };

    let stdout = stdout_task
        .await
        .map_err(|e| format!("Failed to join stdout reader: {e}"))?
        .map_err(|e| format!("Failed to read just stdout: {e}"))?;
    let stderr = stderr_task
        .await
        .map_err(|e| format!("Failed to join stderr reader: {e}"))?
        .map_err(|e| format!("Failed to read just stderr: {e}"))?;

    Ok(ExecuteOutput {
        dir: chosen_dir,
        recipe: recipe_name.to_string(),
        success: status.success(),
        exit_code: status.code(),
        stdout: String::from_utf8_lossy(&stdout).to_string(),
        stderr: String::from_utf8_lossy(&stderr).to_string(),
    })
}

fn value_to_arg(v: &Value) -> Result<String, String> {
    match v {
        Value::String(s) => Ok(s.clone()),
        Value::Number(n) => Ok(n.to_string()),
        Value::Bool(b) => Ok(b.to_string()),
        Value::Array(_) => Err("Arrays are only supported for variadic (*) parameters".to_string()),
        Value::Null => Err("Null values are not supported; use empty string instead".to_string()),
        Value::Object(_) => Err("Object arguments are not supported".to_string()),
    }
}

#[cfg(test)]
#[expect(clippy::unwrap_used)]
mod tests {
    use super::*;
    use agentic_tools_core::ToolContext;
    use serde_json::json;
    use std::fs;
    use tempfile::TempDir;

    /// Skip test if `just` command is not available
    macro_rules! skip_if_just_unavailable {
        () => {
            if tokio::process::Command::new("just")
                .arg("--version")
                .output()
                .await
                .is_err()
            {
                eprintln!("Skipping test: just not installed");
                return;
            }
        };
    }

    #[test]
    fn value_to_arg_string() {
        assert_eq!(value_to_arg(&json!("hello")).unwrap(), "hello");
    }

    #[test]
    fn value_to_arg_number() {
        assert_eq!(value_to_arg(&json!(42)).unwrap(), "42");
        assert_eq!(value_to_arg(&json!(3.5)).unwrap(), "3.5");
    }

    #[test]
    fn value_to_arg_bool() {
        assert_eq!(value_to_arg(&json!(true)).unwrap(), "true");
        assert_eq!(value_to_arg(&json!(false)).unwrap(), "false");
    }

    #[test]
    fn value_to_arg_rejects_complex_with_clear_messages() {
        let obj_err = value_to_arg(&json!({"key": "value"})).unwrap_err();
        assert!(obj_err.contains("Object"));

        let arr_err = value_to_arg(&json!(["a", "b"])).unwrap_err();
        assert!(arr_err.contains("variadic"));

        let null_err = value_to_arg(&json!(null)).unwrap_err();
        assert!(null_err.contains("empty string"));
    }

    #[tokio::test]
    async fn recipe_not_found_error() {
        skip_if_just_unavailable!();

        let tmp = TempDir::new().unwrap();
        let root = tmp.path();
        fs::write(root.join("justfile"), "build:\n    echo building").unwrap();

        let registry = JustRegistry::new();
        let result = execute_recipe(
            &registry,
            "nonexistent",
            None,
            None,
            root.to_str().unwrap(),
            &ToolContext::default(),
        )
        .await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.contains("not found"));
        assert!(err.contains("nonexistent"));
    }

    #[tokio::test]
    async fn defaults_to_root_when_recipe_in_multiple_dirs() {
        skip_if_just_unavailable!();

        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        // Create same recipe in root and subdirectory
        fs::write(root.join("justfile"), "check:\n    echo root").unwrap();
        fs::create_dir_all(root.join("sub")).unwrap();
        fs::write(root.join("sub/justfile"), "check:\n    echo sub").unwrap();

        let registry = JustRegistry::new();
        // No dir specified - should default to root
        let result = execute_recipe(
            &registry,
            "check",
            None,
            None,
            root.to_str().unwrap(),
            &ToolContext::default(),
        )
        .await;

        assert!(result.is_ok());
        let output = result.unwrap();
        assert!(output.success);
        assert!(output.stdout.contains("root"));
    }

    #[tokio::test]
    async fn ambiguous_recipe_not_in_root_errors() {
        skip_if_just_unavailable!();

        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        // Create recipe only in subdirectories, NOT in root
        fs::write(root.join("justfile"), "other:\n    echo other").unwrap();
        fs::create_dir_all(root.join("sub1")).unwrap();
        fs::write(root.join("sub1/justfile"), "check:\n    echo sub1").unwrap();
        fs::create_dir_all(root.join("sub2")).unwrap();
        fs::write(root.join("sub2/justfile"), "check:\n    echo sub2").unwrap();

        let registry = JustRegistry::new();
        // No dir specified, recipe not in root - should error
        let result = execute_recipe(
            &registry,
            "check",
            None,
            None,
            root.to_str().unwrap(),
            &ToolContext::default(),
        )
        .await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.contains("not in root"));
        assert!(err.contains("multiple directories"));
    }

    #[tokio::test]
    async fn missing_required_arg_error() {
        skip_if_just_unavailable!();

        let tmp = TempDir::new().unwrap();
        let root = tmp.path();
        // Recipe with required parameter
        fs::write(
            root.join("justfile"),
            "greet name:\n    echo hello {{name}}",
        )
        .unwrap();

        let registry = JustRegistry::new();
        // Call without providing the required arg
        let result = execute_recipe(
            &registry,
            "greet",
            None,
            None,
            root.to_str().unwrap(),
            &ToolContext::default(),
        )
        .await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.contains("Missing required argument"));
        assert!(err.contains("name"));
    }

    #[tokio::test]
    async fn successful_execution() {
        skip_if_just_unavailable!();

        let tmp = TempDir::new().unwrap();
        let root = tmp.path();
        fs::write(root.join("justfile"), "hello:\n    echo hello world").unwrap();

        let registry = JustRegistry::new();
        let result = execute_recipe(
            &registry,
            "hello",
            None,
            None,
            root.to_str().unwrap(),
            &ToolContext::default(),
        )
        .await;

        assert!(result.is_ok());
        let output = result.unwrap();
        assert!(output.success);
        assert_eq!(output.exit_code, Some(0));
        assert!(output.stdout.contains("hello world"));
    }

    #[tokio::test]
    async fn non_zero_exit_code() {
        skip_if_just_unavailable!();

        let tmp = TempDir::new().unwrap();
        let root = tmp.path();
        fs::write(root.join("justfile"), "fail:\n    exit 42").unwrap();

        let registry = JustRegistry::new();
        let result = execute_recipe(
            &registry,
            "fail",
            None,
            None,
            root.to_str().unwrap(),
            &ToolContext::default(),
        )
        .await;

        assert!(result.is_ok());
        let output = result.unwrap();
        assert!(!output.success);
        // just wraps the exit code, so we check it's non-zero
        assert!(output.exit_code != Some(0));
    }

    #[tokio::test]
    async fn disambiguate_with_dir() {
        skip_if_just_unavailable!();

        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        // Create same recipe in two directories
        fs::write(root.join("justfile"), "check:\n    echo root").unwrap();
        fs::create_dir_all(root.join("sub")).unwrap();
        fs::write(root.join("sub/justfile"), "check:\n    echo sub").unwrap();

        let registry = JustRegistry::new();

        // Specify dir to disambiguate
        let sub_dir = root.join("sub").to_string_lossy().to_string();
        let result = execute_recipe(
            &registry,
            "check",
            Some(sub_dir),
            None,
            root.to_str().unwrap(),
            &ToolContext::default(),
        )
        .await;

        assert!(result.is_ok());
        let output = result.unwrap();
        assert!(output.success);
        assert!(output.stdout.contains("sub"));
    }

    #[tokio::test]
    async fn missing_arg_error_shows_unused_keys() {
        skip_if_just_unavailable!();

        let tmp = TempDir::new().unwrap();
        let root = tmp.path();
        // Recipe with required parameter named 'target'
        fs::write(
            root.join("justfile"),
            "build target:\n    echo building {{target}}",
        )
        .unwrap();

        let registry = JustRegistry::new();
        // Call with wrong key name 'tgt' instead of 'target'
        let mut wrong_args = HashMap::new();
        wrong_args.insert("tgt".to_string(), json!("x86_64"));

        let result = execute_recipe(
            &registry,
            "build",
            None,
            Some(wrong_args),
            root.to_str().unwrap(),
            &ToolContext::default(),
        )
        .await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        // Should mention the missing required arg
        assert!(err.contains("Missing required argument"));
        assert!(err.contains("target"));
        // Should mention the unused key the user provided
        assert!(err.contains("tgt"));
        // Should list expected parameters
        assert!(err.contains("Expected parameter"));
    }

    #[tokio::test]
    async fn cancellation_stops_recipe_execution() {
        skip_if_just_unavailable!();

        let tmp = TempDir::new().unwrap();
        let root = tmp.path();
        fs::write(root.join("justfile"), "hang:\n    sleep 30").unwrap();

        let registry = JustRegistry::new();
        let ctx = ToolContext::default();
        let cancel = ctx.cancellation_token();

        let handle = tokio::spawn({
            let repo_root = root.to_str().unwrap().to_string();
            async move { execute_recipe(&registry, "hang", None, None, &repo_root, &ctx).await }
        });

        tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        cancel.cancel();

        let result = handle.await.unwrap();
        assert_eq!(result.unwrap_err(), "Just execution cancelled");
    }
}