agent-code-lib 0.16.1

Agent engine library: LLM providers, tools, query loop, memory
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
//! Shell passthrough: capture subprocess output and build context messages.
//!
//! The `!` prefix in the REPL runs a shell command directly, streams output
//! to the terminal, captures it into a buffer, and injects the result into
//! conversation history as a `UserMessage` with `is_meta: true`.
//!
//! This module extracts the capture and message-building logic so it can be
//! tested independently of the REPL event loop.

use crate::llm::message::{ContentBlock, Message, UserMessage};
use std::io::{BufRead, BufReader, Read};
use std::path::Path;
use std::process::{Command, Stdio};
use uuid::Uuid;

/// Maximum bytes captured from shell output before truncation.
pub const MAX_CAPTURE_BYTES: usize = 50 * 1024; // 50KB

/// Result of capturing shell command output.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CapturedOutput {
    /// The captured text (may be truncated).
    pub text: String,
    /// Whether the output was truncated at `MAX_CAPTURE_BYTES`.
    pub truncated: bool,
    /// The exit code of the process, if available.
    pub exit_code: Option<i32>,
}

/// Capture text into a buffer with a size limit.
///
/// Reads lines from `reader`, calls `on_line` for each (e.g., to print to
/// the terminal), and appends to the buffer until `MAX_CAPTURE_BYTES` is reached.
pub fn capture_lines<R: Read>(
    reader: R,
    buffer: &mut String,
    truncated: &mut bool,
    mut on_line: impl FnMut(&str),
) {
    for line in BufReader::new(reader).lines() {
        match line {
            Ok(line) => {
                on_line(&line);
                if !*truncated {
                    if buffer.len() + line.len() + 1 > MAX_CAPTURE_BYTES {
                        *truncated = true;
                    } else {
                        buffer.push_str(&line);
                        buffer.push('\n');
                    }
                }
            }
            Err(_) => break,
        }
    }
}

/// Run a shell command, capture its output, and return the result.
///
/// Output is captured up to `MAX_CAPTURE_BYTES`. The `on_stdout` and
/// `on_stderr` callbacks are called for each line as it arrives (for
/// real-time terminal streaming).
pub fn run_and_capture(
    cmd: &str,
    cwd: &Path,
    mut on_stdout: impl FnMut(&str),
    mut on_stderr: impl FnMut(&str),
) -> Result<CapturedOutput, String> {
    let mut child = Command::new("bash")
        .arg("-c")
        .arg(cmd)
        .current_dir(cwd)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .map_err(|e| format!("bash error: {e}"))?;

    let mut captured = String::new();
    let mut truncated = false;

    if let Some(stdout) = child.stdout.take() {
        capture_lines(stdout, &mut captured, &mut truncated, &mut on_stdout);
    }

    if let Some(stderr) = child.stderr.take() {
        capture_lines(stderr, &mut captured, &mut truncated, &mut on_stderr);
    }

    let exit_code = child.wait().ok().and_then(|s| s.code());

    Ok(CapturedOutput {
        text: captured,
        truncated,
        exit_code,
    })
}

/// Build a context injection message from captured shell output.
///
/// Returns `None` if the captured text is empty (nothing to inject).
pub fn build_context_message(cmd: &str, output: &CapturedOutput) -> Option<Message> {
    if output.text.is_empty() {
        return None;
    }

    let suffix = if output.truncated {
        "\n[output truncated at 50KB]"
    } else {
        ""
    };
    let context_text = format!("[Shell output from: {cmd}]\n{}{suffix}", output.text);

    Some(Message::User(UserMessage {
        uuid: Uuid::new_v4(),
        timestamp: chrono::Utc::now().to_rfc3339(),
        content: vec![ContentBlock::Text { text: context_text }],
        is_meta: true,
        is_compact_summary: false,
    }))
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Cursor;

    // ── capture_lines tests ──────────────────────────────────────────

    #[test]
    fn captures_all_lines_under_limit() {
        let input = "line one\nline two\nline three\n";
        let reader = Cursor::new(input);
        let mut buffer = String::new();
        let mut truncated = false;
        let mut printed = Vec::new();

        capture_lines(reader, &mut buffer, &mut truncated, |line| {
            printed.push(line.to_string());
        });

        assert_eq!(buffer, "line one\nline two\nline three\n");
        assert!(!truncated);
        assert_eq!(printed, vec!["line one", "line two", "line three"]);
    }

    #[test]
    fn truncates_at_limit() {
        // Create input that exceeds MAX_CAPTURE_BYTES.
        let long_line = "x".repeat(1024);
        let lines: Vec<String> = (0..60).map(|_| long_line.clone()).collect();
        let input = lines.join("\n") + "\n";
        let reader = Cursor::new(input);
        let mut buffer = String::new();
        let mut truncated = false;

        capture_lines(reader, &mut buffer, &mut truncated, |_| {});

        assert!(truncated);
        assert!(buffer.len() <= MAX_CAPTURE_BYTES);
        // Should have captured some lines but not all 60.
        let captured_lines: Vec<&str> = buffer.lines().collect();
        assert!(captured_lines.len() < 60);
        assert!(!captured_lines.is_empty());
    }

    #[test]
    fn empty_input_produces_empty_buffer() {
        let reader = Cursor::new("");
        let mut buffer = String::new();
        let mut truncated = false;

        capture_lines(reader, &mut buffer, &mut truncated, |_| {});

        assert!(buffer.is_empty());
        assert!(!truncated);
    }

    #[test]
    fn single_line_no_newline() {
        let reader = Cursor::new("just one line");
        let mut buffer = String::new();
        let mut truncated = false;

        capture_lines(reader, &mut buffer, &mut truncated, |_| {});

        assert_eq!(buffer, "just one line\n");
        assert!(!truncated);
    }

    #[test]
    fn on_line_called_for_every_line_even_after_truncation() {
        // Verify that the callback is always called (for terminal streaming)
        // even after truncation kicks in.
        let long_line = "x".repeat(MAX_CAPTURE_BYTES + 100);
        let input = format!("{long_line}\nsecond line\n");
        let reader = Cursor::new(input);
        let mut buffer = String::new();
        let mut truncated = false;
        let mut callback_count = 0;

        capture_lines(reader, &mut buffer, &mut truncated, |_| {
            callback_count += 1;
        });

        // Both lines should trigger the callback for terminal streaming.
        assert_eq!(callback_count, 2);
        assert!(truncated);
    }

    #[test]
    fn truncation_boundary_exact() {
        // Fill buffer to exactly MAX_CAPTURE_BYTES - 1, then add a line
        // that would push it over.
        let fill_len = MAX_CAPTURE_BYTES - 10;
        let fill = "a".repeat(fill_len);
        let overflow = "b".repeat(20);
        let input = format!("{fill}\n{overflow}\n");
        let reader = Cursor::new(input);
        let mut buffer = String::new();
        let mut truncated = false;

        capture_lines(reader, &mut buffer, &mut truncated, |_| {});

        // First line should be captured (fill_len + 1 newline = MAX - 9).
        // Second line (20 + 1) would exceed MAX, so truncation should fire.
        assert!(truncated);
        assert!(buffer.contains(&fill));
        assert!(!buffer.contains(&overflow));
    }

    // ── build_context_message tests ──────────────────────────────────

    #[test]
    fn builds_message_with_header() {
        let output = CapturedOutput {
            text: "hello world\n".to_string(),
            truncated: false,
            exit_code: Some(0),
        };

        let msg = build_context_message("echo hello", &output).unwrap();
        if let Message::User(u) = &msg {
            assert!(u.is_meta);
            assert!(!u.is_compact_summary);
            assert_eq!(u.content.len(), 1);
            if let ContentBlock::Text { text } = &u.content[0] {
                assert!(text.starts_with("[Shell output from: echo hello]\n"));
                assert!(text.contains("hello world"));
                assert!(!text.contains("[output truncated"));
            } else {
                panic!("Expected Text block");
            }
        } else {
            panic!("Expected User message");
        }
    }

    #[test]
    fn builds_message_with_truncation_suffix() {
        let output = CapturedOutput {
            text: "partial output\n".to_string(),
            truncated: true,
            exit_code: Some(0),
        };

        let msg = build_context_message("big-cmd", &output).unwrap();
        if let Message::User(u) = &msg {
            if let ContentBlock::Text { text } = &u.content[0] {
                assert!(text.contains("[output truncated at 50KB]"));
                assert!(text.starts_with("[Shell output from: big-cmd]\n"));
            } else {
                panic!("Expected Text block");
            }
        } else {
            panic!("Expected User message");
        }
    }

    #[test]
    fn empty_output_returns_none() {
        let output = CapturedOutput {
            text: String::new(),
            truncated: false,
            exit_code: Some(0),
        };

        assert!(build_context_message("true", &output).is_none());
    }

    #[test]
    fn preserves_multiline_output() {
        let output = CapturedOutput {
            text: "line 1\nline 2\nline 3\n".to_string(),
            truncated: false,
            exit_code: Some(0),
        };

        let msg = build_context_message("seq 3", &output).unwrap();
        if let Message::User(u) = &msg {
            if let ContentBlock::Text { text } = &u.content[0] {
                assert!(text.contains("line 1\nline 2\nline 3\n"));
            } else {
                panic!("Expected Text block");
            }
        } else {
            panic!("Expected User message");
        }
    }

    #[test]
    fn special_characters_in_command_preserved() {
        let output = CapturedOutput {
            text: "ok\n".to_string(),
            truncated: false,
            exit_code: Some(0),
        };

        let msg = build_context_message("grep -r 'foo bar' .", &output).unwrap();
        if let Message::User(u) = &msg {
            if let ContentBlock::Text { text } = &u.content[0] {
                assert!(text.contains("[Shell output from: grep -r 'foo bar' .]"));
            } else {
                panic!("Expected Text block");
            }
        } else {
            panic!("Expected User message");
        }
    }

    // ── run_and_capture tests (real subprocess) ─────────────────────

    #[test]
    fn captures_stdout_from_echo() {
        let dir = std::env::temp_dir();
        let result = run_and_capture("echo hello_e2e", &dir, |_| {}, |_| {}).unwrap();

        assert_eq!(result.text, "hello_e2e\n");
        assert!(!result.truncated);
        assert_eq!(result.exit_code, Some(0));
    }

    #[test]
    fn captures_stderr() {
        let dir = std::env::temp_dir();
        let result = run_and_capture("echo err_msg >&2", &dir, |_| {}, |_| {}).unwrap();

        assert!(result.text.contains("err_msg"));
        assert!(!result.truncated);
    }

    #[test]
    fn captures_mixed_stdout_stderr() {
        let dir = std::env::temp_dir();
        let result = run_and_capture(
            "echo stdout_line && echo stderr_line >&2",
            &dir,
            |_| {},
            |_| {},
        )
        .unwrap();

        assert!(result.text.contains("stdout_line"));
        assert!(result.text.contains("stderr_line"));
    }

    #[test]
    fn captures_multiline_output() {
        let dir = std::env::temp_dir();
        let result = run_and_capture("printf 'a\\nb\\nc\\n'", &dir, |_| {}, |_| {}).unwrap();

        assert_eq!(result.text, "a\nb\nc\n");
        assert!(!result.truncated);
    }

    #[test]
    fn handles_empty_output_command() {
        let dir = std::env::temp_dir();
        let result = run_and_capture("true", &dir, |_| {}, |_| {}).unwrap();

        assert!(result.text.is_empty());
        assert!(!result.truncated);
        assert_eq!(result.exit_code, Some(0));
    }

    #[test]
    fn captures_nonzero_exit_code() {
        let dir = std::env::temp_dir();
        let result = run_and_capture("exit 42", &dir, |_| {}, |_| {}).unwrap();

        assert_eq!(result.exit_code, Some(42));
    }

    #[test]
    fn respects_cwd() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(tmp.path().join("marker.txt"), "found_it").unwrap();

        let result = run_and_capture("cat marker.txt", tmp.path(), |_| {}, |_| {}).unwrap();

        assert!(result.text.contains("found_it"));
    }

    #[test]
    fn callbacks_receive_all_lines() {
        let dir = std::env::temp_dir();
        let mut stdout_lines = Vec::new();
        let mut stderr_lines = Vec::new();

        let _ = run_and_capture(
            "echo out1 && echo out2 && echo err1 >&2",
            &dir,
            |line| stdout_lines.push(line.to_string()),
            |line| stderr_lines.push(line.to_string()),
        );

        assert_eq!(stdout_lines, vec!["out1", "out2"]);
        assert_eq!(stderr_lines, vec!["err1"]);
    }

    #[test]
    fn truncates_large_output() {
        let dir = std::env::temp_dir();
        // Generate >50KB of output: 1000 lines of 100 chars each = 100KB.
        let result = run_and_capture(
            "for i in $(seq 1 1000); do printf '%0100d\\n' $i; done",
            &dir,
            |_| {},
            |_| {},
        )
        .unwrap();

        assert!(result.truncated);
        assert!(result.text.len() <= MAX_CAPTURE_BYTES);
        // Should have captured some but not all 1000 lines.
        let line_count = result.text.lines().count();
        assert!(line_count > 100);
        assert!(line_count < 1000);
    }

    #[test]
    fn invalid_command_returns_error_output() {
        let dir = std::env::temp_dir();
        let result = run_and_capture(
            "nonexistent_command_xyz123 2>&1 || true",
            &dir,
            |_| {},
            |_| {},
        )
        .unwrap();

        // The command fails but bash itself runs fine.
        // stderr should contain the error message.
        assert!(
            result.text.contains("not found") || result.text.contains("No such file"),
            "Expected error message, got: {}",
            result.text
        );
    }

    // ── End-to-end: run_and_capture + build_context_message ──────────

    #[test]
    fn full_pipeline_echo_to_context_message() {
        let dir = std::env::temp_dir();
        let result = run_and_capture("echo integration_test_marker", &dir, |_| {}, |_| {}).unwrap();
        let msg = build_context_message("echo integration_test_marker", &result).unwrap();

        if let Message::User(u) = &msg {
            assert!(u.is_meta);
            if let ContentBlock::Text { text } = &u.content[0] {
                assert!(text.starts_with("[Shell output from: echo integration_test_marker]"));
                assert!(text.contains("integration_test_marker"));
            } else {
                panic!("Expected Text block");
            }
        } else {
            panic!("Expected User message");
        }
    }

    #[test]
    fn full_pipeline_empty_command_no_message() {
        let dir = std::env::temp_dir();
        let result = run_and_capture("true", &dir, |_| {}, |_| {}).unwrap();
        let msg = build_context_message("true", &result);

        assert!(msg.is_none());
    }

    #[test]
    fn full_pipeline_truncated_output_has_suffix() {
        let dir = std::env::temp_dir();
        let result = run_and_capture(
            "for i in $(seq 1 2000); do printf '%0100d\\n' $i; done",
            &dir,
            |_| {},
            |_| {},
        )
        .unwrap();

        assert!(result.truncated);

        let msg = build_context_message("big-output", &result).unwrap();
        if let Message::User(u) = &msg {
            if let ContentBlock::Text { text } = &u.content[0] {
                assert!(text.contains("[output truncated at 50KB]"));
                assert!(text.starts_with("[Shell output from: big-output]"));
            } else {
                panic!("Expected Text block");
            }
        } else {
            panic!("Expected User message");
        }
    }
}