rpi-tools 0.30.0

Built-in agent tools (read/write/edit/bash/grep/find/ls) + ExecutionEnv, Rust port of pi harness tools
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Mirrors `packages/agent/test/harness/tools.test.ts` "bash" describe block —
//! non-zero exit → Err, "(no output)", truncation temp-file, oversized-final-line
//! size report, command prefix, and output combination. The TS runs against a
//! real `NodeExecutionEnv`+bash; the Rust port runs the SAME invariants against
//! `InMemoryExecutionEnv` with canned `ShellScript`s (deterministic, no-platform-
//! bash dependency) plus the `OsExecutionEnv` for a couple of real-shell smoke
//! checks gated on bash availability.
//!
//! See the initial port notes (retired) for the divergence note on the timeout-truncated
//! output case (the TS `TimeoutOutputExecutionEnv` cannot be expressed against
//! `InMemoryExecutionEnv` without a blocking-override env like `abort_bracketing`).

use std::sync::Arc;
use std::time::Duration;

use rpi_agent::agent_tool::AgentTool;
use rpi_agent::error::AgentError;
use rpi_tools::{
    BashToolOptions, ExecutionToolContext, FileSystem, InMemoryExecutionEnv, ShellScript,
    DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES,
};
use tokio_util::sync::CancellationToken;

fn fresh_context() -> (Arc<InMemoryExecutionEnv>, ExecutionToolContext) {
    let env = Arc::new(InMemoryExecutionEnv::with_cwd("/tmp/work".into()));
    let env_dyn: Arc<dyn rpi_tools::ExecutionEnv> = env.clone();
    let mut_env: Arc<dyn rpi_tools::MutatingEnv> = env.clone();
    let ctx = ExecutionToolContext::new(env_dyn, Some(mut_env));
    (env, ctx)
}

async fn run_bash(
    tool: Arc<dyn AgentTool>,
    params: serde_json::Value,
) -> Result<rpi_agent::types::AgentToolResult, AgentError> {
    let signal = CancellationToken::new();
    let on_update = Arc::new(|_p| ());
    let prepared = tool
        .prepare_arguments(params.clone())
        .unwrap_or_else(|_| params);
    tool.execute("bash-x", prepared, signal, on_update).await
}

fn text_output(r: &rpi_agent::types::AgentToolResult) -> String {
    use rpi_agent::types::TextContentOrImage;
    r.content
        .iter()
        .filter_map(|c| match c {
            TextContentOrImage::Text(t) => Some(t.text.clone()),
            _ => None,
        })
        .collect::<Vec<_>>()
        .join("\n")
}

fn err_text(e: &AgentError) -> String {
    match e {
        AgentError::Tool(m) => m.clone(),
        // The bash tool now reports failed commands as StructuredTool so the
        // tool result can carry exit_code / failure_kind. The message lives in
        // content, not in the error's Display.
        AgentError::StructuredTool(r) => text_output(r),
        other => format!("{other:?}"),
    }
}

#[tokio::test]
async fn no_output_renders_placeholder() {
    let (env, ctx) = fresh_context();
    env.register_shell("noop", ShellScript::success("")).await;
    let tool = rpi_tools::create_bash_tool(&ctx, None);
    let r = run_bash(tool, serde_json::json!({ "command": "noop" }))
        .await
        .expect("ok");
    assert_eq!(text_output(&r), "(no output)");
}

#[tokio::test]
async fn combines_stdout_and_stderr() {
    let (env, ctx) = fresh_context();
    // ShellScript carries separate stdout/stderr but the capture layer merges
    // them into one rolling buffer. Seed a script with both non-empty.
    env.register_shell(
        "both",
        ShellScript {
            stdout: "out".into(),
            stderr: "err".into(),
            exit_code: 0,
            delay_ms: None,
        },
    )
    .await;
    let tool = rpi_tools::create_bash_tool(&ctx, None);
    let r = run_bash(tool, serde_json::json!({ "command": "both" }))
        .await
        .expect("ok");
    let out = text_output(&r);
    assert!(out.contains("out"), "{out}");
    assert!(out.contains("err"), "{out}");
}

#[tokio::test]
async fn nonzero_exit_returns_error_with_output() {
    let (env, ctx) = fresh_context();
    env.register_shell(
        "fail",
        ShellScript {
            stdout: "failed".into(),
            stderr: String::new(),
            exit_code: 7,
            delay_ms: None,
        },
    )
    .await;
    let tool = rpi_tools::create_bash_tool(&ctx, None);
    let err = run_bash(tool, serde_json::json!({ "command": "fail" }))
        .await
        .expect_err("nonzero exit → Err");
    let msg = err_text(&err);
    assert!(msg.contains("failed"), "{msg}");
    assert!(msg.contains("Command exited with code 7"), "{msg}");
    // PR-B (fork issue #14): the failure stays isError=true in the agent loop,
    // but details now says this was a command result, not a broken tool.
    let AgentError::StructuredTool(result) = &err else {
        panic!("bash failures must carry structured details: {err:?}");
    };
    assert_eq!(result.details["exit_code"], 7);
    assert_eq!(result.details["failure_kind"], "command_failed");
}

#[tokio::test]
async fn abort_returns_error() {
    let (env, ctx) = fresh_context();
    env.register_shell(
        "slow",
        ShellScript {
            stdout: "x".into(),
            stderr: String::new(),
            exit_code: 0,
            delay_ms: Some(1000),
        },
    )
    .await;
    let tool = rpi_tools::create_bash_tool(&ctx, None);
    let signal = CancellationToken::new();
    let on_update = Arc::new(|_p| ());
    let handle = {
        let tool = tool.clone();
        let sig = signal.clone();
        tokio::spawn(async move {
            tool.execute(
                "bash-abort",
                serde_json::json!({ "command": "slow" }),
                sig,
                on_update,
            )
            .await
        })
    };
    // Let the command enter its delay, then abort.
    tokio::time::sleep(Duration::from_millis(20)).await;
    signal.cancel();
    let err = handle.await.expect("join").expect_err("abort → Err");
    let msg = err_text(&err);
    assert!(msg.contains("aborted") || msg.contains("Aborted"), "{msg}");
}

#[tokio::test]
async fn persists_truncated_full_output_to_temp_file() {
    let (env, ctx) = fresh_context();
    // Emit DEFAULT_MAX_LINES + 1 lines — exceeds the line cap, triggers the
    // post-stream temp-file spill.
    let lines: String = (0..(DEFAULT_MAX_LINES + 1))
        .map(|i| format!("line-{}", i + 1))
        .collect::<Vec<_>>()
        .join("\n")
        + "\n";
    env.register_shell("big", ShellScript::success(lines)).await;

    let tool = rpi_tools::create_bash_tool(&ctx, None);
    let r = run_bash(tool, serde_json::json!({ "command": "big" }))
        .await
        .expect("ok");
    // Truncation metadata + a fullOutputPath.
    let trunc = r.details.get("truncation").expect("truncation present");
    assert_eq!(trunc["truncated"], true);
    assert_eq!(trunc["truncated_by"], "lines");
    assert_eq!(trunc["total_lines"], DEFAULT_MAX_LINES + 1);
    assert_eq!(trunc["output_lines"], DEFAULT_MAX_LINES);
    let full_path = r
        .details
        .get("full_output_path")
        .and_then(|v| v.as_str())
        .expect("full_output_path present");
    let full = env
        .read_text_file(full_path, None)
        .await
        .expect("read full output");
    assert!(full.contains("line-1\nline-2"), "{full}");
    assert!(
        full.contains(&format!(
            "line-{}\nline-{}",
            DEFAULT_MAX_LINES,
            DEFAULT_MAX_LINES + 1
        )),
        "{full}"
    );
}

#[tokio::test]
async fn reports_oversized_final_line_size() {
    let (env, ctx) = fresh_context();
    // One line that exceeds the 50KB byte cap → last_line_partial path.
    let big_line: String = "0".repeat(DEFAULT_MAX_BYTES + 5000);
    env.register_shell("oneline", ShellScript::success(big_line))
        .await;

    let tool = rpi_tools::create_bash_tool(&ctx, None);
    let r = run_bash(tool, serde_json::json!({ "command": "oneline" }))
        .await
        .expect("ok");
    let out = text_output(&r);
    // The TS expects "Showing last <size> of line 1 (line is <size>). Full output:".
    assert!(out.contains("Showing last"), "{out}");
    assert!(out.contains("line 1"), "{out}");
}

#[tokio::test]
async fn supports_command_prefix() {
    let (env, ctx) = fresh_context();
    // Prefix sets a variable; the command echoes it. The InMemory shell matches
    // by longest prefix, so register a script for the FULL composed command the
    // tool emits (prefix + "\n" + command).
    env.register_shell("value=hello\necho hello", ShellScript::success("hello"))
        .await;
    let tool = rpi_tools::create_bash_tool(
        &ctx,
        Some(BashToolOptions {
            command_prefix: Some("value=hello".into()),
            default_timeout: None,
            prepare: None,
        }),
    );
    // The shell_script prefix-matches on the composed command start; echo is the
    // user command. Register a broader prefix to be safe.
    env.register_shell("value=hello", ShellScript::success("hello"))
        .await;
    let r = run_bash(tool, serde_json::json!({ "command": "echo ignored" }))
        .await
        .expect("ok");
    assert_eq!(text_output(&r), "hello");
}

#[test]
fn schema_describes_effective_default_timeout() {
    let (_, ctx) = fresh_context();
    for (options, expected) in [
        (None, 120.0),
        (Some(BashToolOptions::default()), 120.0),
        (
            Some(BashToolOptions {
                default_timeout: Some(30.0),
                ..Default::default()
            }),
            30.0,
        ),
    ] {
        let tool = rpi_tools::create_bash_tool(&ctx, options);
        assert!(tool
            .schema()
            .description
            .contains(&format!("{expected}-second timeout")));
        let params = serde_json::to_value(&tool.schema().parameters).unwrap();
        assert!(params["properties"]["timeout"]["description"]
            .as_str()
            .unwrap()
            .contains(&format!("defaults to {expected} seconds")));
    }
}

#[tokio::test]
async fn real_shell_honors_fallback_and_explicit_timeouts() {
    use rpi_tools::{ExecutionErrorCode, OsExecutionEnv, Shell, ShellExecOptions};

    let tmp = tempfile::tempdir().unwrap();
    let env = Arc::new(OsExecutionEnv::with_cwd(tmp.path().to_path_buf()));
    match env
        .exec(
            "echo ready",
            ShellExecOptions {
                timeout: Some(5.0),
                ..Default::default()
            },
        )
        .await
    {
        Err(error) if error.code == ExecutionErrorCode::ShellUnavailable => {
            eprintln!("skipping real-shell timeout test: {error}");
            return;
        }
        result => {
            result.expect("shell is available");
        }
    }
    let ctx = ExecutionToolContext::new(env, None);
    for (default_timeout, params) in [
        (0.1, serde_json::json!({ "command": "sleep 30" })),
        (
            0.1,
            serde_json::json!({ "command": "sleep 30", "timeout": null }),
        ),
        (
            30.0,
            serde_json::json!({ "command": "sleep 30", "timeout": 0.1 }),
        ),
    ] {
        let tool = rpi_tools::create_bash_tool(
            &ctx,
            Some(BashToolOptions {
                default_timeout: Some(default_timeout),
                ..Default::default()
            }),
        );
        let result = tokio::time::timeout(Duration::from_secs(10), run_bash(tool, params))
            .await
            .expect("bash must return within its deadline");
        let msg = err_text(&result.expect_err("slow command must time out"));
        assert!(msg.contains("Command timed out after 0.1s"), "{msg}");
    }
}

/// Fork issue #14 PR-B, compatibility version: keep `isError` semantics but
/// classify the failure in `details`. Pins the whole "how did the call end"
/// axis: command failed / cancelled / timed out / shell transport failed, plus
/// the success cell (no failure_kind).
#[tokio::test]
async fn review_issue14_failure_kind_axis_and_success_have_no_classification() {
    let (env, ctx) = fresh_context();
    env.register_shell(
        "fail",
        ShellScript {
            stdout: "grep: no match".into(),
            stderr: String::new(),
            exit_code: 1,
            delay_ms: None,
        },
    )
    .await;
    env.register_shell("ok", ShellScript::success("done")).await;
    let tool = rpi_tools::create_bash_tool(&ctx, None);

    let ok = run_bash(tool.clone(), serde_json::json!({ "command": "ok" }))
        .await
        .expect("success");
    assert!(ok.details.get("failure_kind").is_none(), "{:?}", ok.details);
    assert_eq!(ok.details["exit_code"], 0);

    let err = run_bash(tool.clone(), serde_json::json!({ "command": "fail" }))
        .await
        .expect_err("nonzero exit stays an error");
    let AgentError::StructuredTool(result) = &err else {
        panic!("expected StructuredTool: {err:?}");
    };
    assert_eq!(result.details["exit_code"], 1);
    assert_eq!(result.details["failure_kind"], "command_failed");
    // A probe command's output must still be visible: the classification is
    // additive, it must not swallow what the command said.
    assert!(text_output(result).contains("grep: no match"));

    // Timeout cell: InMemoryExecutionEnv does not model a deadline, so use the
    // real shell (same setup as `real_shell_honors_fallback_and_explicit_timeouts`).
    use rpi_tools::{ExecutionErrorCode, OsExecutionEnv, Shell, ShellExecOptions};
    let tmp = tempfile::tempdir().unwrap();
    let os_env = Arc::new(OsExecutionEnv::with_cwd(tmp.path().to_path_buf()));
    match os_env
        .exec(
            "echo ready",
            ShellExecOptions {
                timeout: Some(5.0),
                ..Default::default()
            },
        )
        .await
    {
        Err(error) if error.code == ExecutionErrorCode::ShellUnavailable => {
            eprintln!("skipping timeout cell: no bash on this host");
        }
        result => {
            result.expect("shell is available");
            let os_ctx =
                ExecutionToolContext::new(os_env.clone() as Arc<dyn rpi_tools::ExecutionEnv>, None);
            let slow_tool = rpi_tools::create_bash_tool(&os_ctx, None);
            let err = run_bash(
                slow_tool,
                serde_json::json!({ "command": "sleep 30", "timeout": 0.1 }),
            )
            .await
            .expect_err("timeout stays an error");
            let AgentError::StructuredTool(result) = &err else {
                panic!("expected StructuredTool: {err:?}");
            };
            assert_eq!(result.details["failure_kind"], "timeout");
        }
    }

    // Cancellation cell.
    let (env3, ctx3) = fresh_context();
    env3.register_shell(
        "slow3",
        ShellScript {
            stdout: String::new(),
            stderr: String::new(),
            exit_code: 0,
            delay_ms: Some(5000),
        },
    )
    .await;
    let cancel_tool = rpi_tools::create_bash_tool(&ctx3, None);
    let signal = CancellationToken::new();
    let on_update = Arc::new(|_p| ());
    let cancel = signal.clone();
    tokio::spawn(async move {
        tokio::time::sleep(Duration::from_millis(30)).await;
        cancel.cancel();
    });
    let err = cancel_tool
        .execute(
            "bash-x",
            serde_json::json!({ "command": "slow3" }),
            signal,
            on_update,
        )
        .await
        .expect_err("cancel stays an error");
    let AgentError::StructuredTool(result) = &err else {
        panic!("expected StructuredTool: {err:?}");
    };
    assert_eq!(result.details["failure_kind"], "cancelled");
}