ag-agent 0.18.1

Agentty is an ADE (Agentic Development Environment) for structured, controllable AI-assisted software development.
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
use std::io::Write as _;

use ag_telemetry::KeyValue;
use serde_json::{Value, json};

use crate::agent::tool_trace::{PREVIEW_BYTES, Preview, ToolMetadata, safe_identity};

#[test]
fn default_metadata_excludes_content_and_project_paths() {
    // Arrange
    let input = json!({"command": "cargo test", "password": "hidden"});
    let output = json!("test failed");
    let metadata = ToolMetadata {
        command: Some("/project/private/bin/cargo test"),
        input: Some(&input),
        name: Some("Bash"),
        output: Some(&output),
        stderr: Some(&output),
        stdout: Some(&output),
    };

    // Act
    let attributes = metadata.attributes(false);

    // Assert
    assert_eq!(
        attributes,
        vec![
            KeyValue::new("gen_ai.tool.name", "Bash"),
            KeyValue::new("process.executable.name", "cargo"),
            KeyValue::new("agentty.tool.output.bytes", 11_i64),
            KeyValue::new("agentty.tool.stdout.bytes", 11_i64),
            KeyValue::new("agentty.tool.stderr.bytes", 11_i64),
        ]
    );
    assert!(!format!("{attributes:?}").contains("private"));
}

#[test]
fn opt_in_exports_commands_arguments_and_separate_output_previews() {
    // Arrange
    let input = json!({"command": "cargo test", "count": 2, "enabled": true, "extra": null});
    let output = json!([{"text": "test failed"}]);
    let stderr = json!("compilation failed");
    let stdout = json!("");
    let metadata = ToolMetadata {
        command: Some("cargo test"),
        input: Some(&input),
        name: Some("shell"),
        output: Some(&output),
        stderr: Some(&stderr),
        stdout: Some(&stdout),
    };

    // Act
    let attributes = metadata.attributes(true);

    // Assert
    for attribute in [
        KeyValue::new("agentty.tool.command", "cargo test"),
        KeyValue::new("gen_ai.tool.call.arguments", input.to_string()),
        KeyValue::new("gen_ai.tool.call.result", output.to_string()),
        KeyValue::new("agentty.tool.stderr", "compilation failed"),
        KeyValue::new("agentty.tool.stdout", ""),
        KeyValue::new(
            "agentty.tool.output.bytes",
            i64::try_from(output.to_string().len()).expect("bounded fixture"),
        ),
        KeyValue::new("agentty.tool.input.truncated", false),
    ] {
        assert!(attributes.contains(&attribute), "{attribute:?}");
    }
}

#[test]
fn credential_markers_suppress_whole_values_before_truncation() {
    // Arrange
    let values = [
        json!({"password": "hidden", "command": "cargo test"}),
        json!([{"nested": "Authorization: Basic hidden"}]),
        json!({"api_key": "hidden"}),
        json!("curl --token hidden"),
        json!("PASSWORD='hidden with spaces' cargo test"),
        json!("https://user:hidden@host/path"),
        json!("http://user:hidden@host"),
        json!(format!("{}\nBearer hidden", "x".repeat(PREVIEW_BYTES))),
    ];

    // Act / Assert
    for output in &values {
        let attributes = ToolMetadata {
            output: Some(output),
            ..ToolMetadata::default()
        }
        .attributes(true);
        let redacted = if output.is_string() {
            "[REDACTED]"
        } else {
            r#""[REDACTED]""#
        };
        assert!(attributes.contains(&KeyValue::new("gen_ai.tool.call.result", redacted)));
        assert!(attributes.contains(&KeyValue::new("agentty.tool.output.truncated", false)));
        assert!(!format!("{attributes:?}").contains("hidden"));
        if !output.is_string() {
            assert_eq!(
                serde_json::from_str::<Value>(redacted).expect("valid redaction JSON"),
                json!("[REDACTED]")
            );
        }
    }
}

#[test]
fn previews_limit_utf8_bytes_and_report_full_output_size() {
    // Arrange
    let values = [
        json!("x".repeat(PREVIEW_BYTES)),
        json!(format!("{}é", "x".repeat(PREVIEW_BYTES - 1))),
    ];

    // Act / Assert
    for value in &values {
        let attributes = ToolMetadata {
            output: Some(value),
            ..ToolMetadata::default()
        }
        .attributes(true);
        let size = value
            .as_str()
            .map_or_else(|| value.to_string().len(), str::len);
        assert!(attributes.contains(&KeyValue::new(
            "agentty.tool.output.bytes",
            i64::try_from(size).expect("bounded fixture")
        )));
        assert!(attributes.contains(&KeyValue::new(
            "agentty.tool.output.truncated",
            size > PREVIEW_BYTES
        )));
        let preview = attributes
            .iter()
            .find(|attribute| attribute.key.as_str() == "gen_ai.tool.call.result")
            .expect("preview");
        assert!(preview.value.to_string().len() <= PREVIEW_BYTES);
        assert!(!preview.value.to_string().contains('�'));
    }
}

#[test]
fn metadata_only_counts_compact_json_without_retaining_serialized_content() {
    // Arrange
    let controls: String = (0_u8..=31).map(char::from).collect();
    let values = [
        json!(null),
        json!(true),
        json!(false),
        json!(0),
        json!(-42),
        json!(1.25),
        json!(1e-30),
        json!([]),
        json!({}),
        json!("raw text"),
        json!([null, true, false, 12, -7, {"quoted\"key\n": ["é🦀", "\\", controls]}]),
        json!({"stdout": "x".repeat(1_000_000), "stderr": "compilation failed"}),
    ];

    // Act / Assert
    for value in &values {
        let expected = value
            .as_str()
            .map_or_else(|| value.to_string().len(), str::len);
        let preview = Preview::new(value, false);
        assert_eq!(preview.bytes, expected);
        assert_eq!(preview.head, [] as [u8; 0]);
        assert!(preview.tail.is_empty());
        assert!(!preview.retain);
    }
}

#[test]
fn oversized_structured_values_use_text_previews_instead_of_partial_json() {
    // Arrange
    let values = [
        json!({"message": "é".repeat(PREVIEW_BYTES)}),
        json!(["\"\\\n".repeat(PREVIEW_BYTES)]),
    ];

    // Act / Assert
    for value in &values {
        let attributes = ToolMetadata {
            input: Some(value),
            output: Some(value),
            stdout: Some(value),
            stderr: Some(value),
            ..ToolMetadata::default()
        }
        .attributes(true);
        for (json_key, preview_key, truncated_key) in [
            (
                "gen_ai.tool.call.arguments",
                "agentty.tool.input.preview",
                "agentty.tool.input.truncated",
            ),
            (
                "gen_ai.tool.call.result",
                "agentty.tool.output.preview",
                "agentty.tool.output.truncated",
            ),
            (
                "agentty.tool.stdout",
                "agentty.tool.stdout.preview",
                "agentty.tool.stdout.truncated",
            ),
            (
                "agentty.tool.stderr",
                "agentty.tool.stderr.preview",
                "agentty.tool.stderr.truncated",
            ),
        ] {
            assert!(
                !attributes
                    .iter()
                    .any(|attribute| attribute.key.as_str() == json_key)
            );
            let preview = attributes
                .iter()
                .find(|attribute| attribute.key.as_str() == preview_key)
                .expect("text preview");
            assert!(preview.value.to_string().len() <= PREVIEW_BYTES);
            assert!(!preview.value.to_string().contains('�'));
            assert!(attributes.contains(&KeyValue::new(truncated_key, true)));
        }
        assert!(attributes.contains(&KeyValue::new(
            "agentty.tool.output.bytes",
            i64::try_from(value.to_string().len()).expect("fixture size")
        )));
    }
}

#[test]
fn structured_values_at_the_limit_remain_complete_parseable_json() {
    // Arrange
    let value = json!({"message": "x".repeat(PREVIEW_BYTES - r#"{"message":""}"#.len())});
    assert_eq!(value.to_string().len(), PREVIEW_BYTES);

    // Act
    let attributes = ToolMetadata {
        input: Some(&value),
        output: Some(&value),
        ..ToolMetadata::default()
    }
    .attributes(true);

    // Assert
    for key in ["gen_ai.tool.call.arguments", "gen_ai.tool.call.result"] {
        let content = attributes
            .iter()
            .find(|attribute| attribute.key.as_str() == key)
            .expect("complete JSON")
            .value
            .to_string();
        assert_eq!(
            serde_json::from_str::<Value>(&content).expect("parseable JSON"),
            value
        );
    }
    assert!(attributes.contains(&KeyValue::new("agentty.tool.input.truncated", false)));
    assert!(attributes.contains(&KeyValue::new("agentty.tool.output.truncated", false)));
}

#[test]
fn missing_null_and_unsafe_identities_are_omitted() {
    // Arrange
    let null = json!(null);
    let metadata = ToolMetadata {
        command: Some(""),
        input: Some(&null),
        name: Some("tool /private/path"),
        output: Some(&null),
        stderr: Some(&null),
        stdout: Some(&null),
    };

    // Act / Assert
    assert_eq!(metadata.attributes(true), []);
    assert_eq!(ToolMetadata::default().attributes(false), []);
    for name in [
        "",
        "some tool",
        "api_token",
        "ghp_hidden",
        "/project/path",
        &"a".repeat(129),
    ] {
        assert!(safe_identity(name).is_none());
    }
    for command in [
        "TOKEN=hidden cargo test",
        "'C:\\bin\\cargo' test",
        "\"/usr/bin/cargo\" test",
    ] {
        let attributes = ToolMetadata {
            command: Some(command),
            ..ToolMetadata::default()
        }
        .attributes(false);
        assert_eq!(attributes.is_empty(), command.starts_with("TOKEN"));
    }
    for command in [
        "VAR=/project/path cargo test",
        "'/project with spaces/cargo' test",
        "\"/project with spaces/cargo\" test",
    ] {
        assert_eq!(
            ToolMetadata {
                command: Some(command),
                ..ToolMetadata::default()
            }
            .attributes(false),
            []
        );
    }
}

#[test]
fn writer_counts_without_retaining_disabled_content_and_flushes() {
    // Arrange
    let mut preview = Preview::new(&json!({"data": "content"}), false);
    let initial_bytes = preview.bytes;

    // Act
    assert_eq!(preview.write(b"abc").expect("write"), 3);
    preview.flush().expect("flush");

    // Assert
    assert_eq!(preview.bytes, initial_bytes + 3);
    assert_eq!(preview.head, [] as [u8; 0]);
    assert!(preview.tail.is_empty());
}

#[test]
fn long_output_previews_keep_the_beginning_and_the_final_failure_summary() {
    // Arrange
    let output = json!(format!(
        "Compiling agentty\n{}\nFAILED: 2 tests failed",
        "warning: noisy build line\n".repeat(1_000)
    ));
    let total = output.as_str().expect("text fixture").len();

    // Act
    let attributes = ToolMetadata {
        output: Some(&output),
        ..ToolMetadata::default()
    }
    .attributes(true);

    // Assert
    let preview = attributes
        .iter()
        .find(|attribute| attribute.key.as_str() == "gen_ai.tool.call.result")
        .expect("preview")
        .value
        .to_string();
    let (head, tail, omitted) = excerpt_parts(&preview);
    assert!(head.starts_with("Compiling agentty\n"));
    assert!(tail.ends_with("\nFAILED: 2 tests failed"));
    assert_eq!(head.len() + omitted + tail.len(), total);
    assert!(preview.len() <= PREVIEW_BYTES);
    assert!(attributes.contains(&KeyValue::new("agentty.tool.output.truncated", true)));
    assert!(attributes.contains(&KeyValue::new(
        "agentty.tool.output.bytes",
        i64::try_from(total).expect("bounded fixture")
    )));
}

#[test]
fn excerpts_split_multibyte_characters_only_at_utf8_boundaries() {
    // Arrange
    let output = json!(format!("x{}x", "é".repeat(PREVIEW_BYTES)));

    // Act
    let attributes = ToolMetadata {
        output: Some(&output),
        ..ToolMetadata::default()
    }
    .attributes(true);

    // Assert
    let preview = attributes
        .iter()
        .find(|attribute| attribute.key.as_str() == "gen_ai.tool.call.result")
        .expect("preview")
        .value
        .to_string();
    let (head, tail, omitted) = excerpt_parts(&preview);
    assert!(!preview.contains('�'));
    assert_eq!(head.len(), 1_023);
    assert!(tail.starts_with('é') && tail.ends_with('x'));
    assert_eq!(
        head.len() + omitted + tail.len(),
        output.as_str().expect("text fixture").len()
    );
}

/// Splits an excerpt into its head, tail, and reported omitted byte count.
fn excerpt_parts(preview: &str) -> (&str, &str, usize) {
    let (head, rest) = preview.split_once("\n[... ").expect("marker start");
    let (marker, tail) = rest.split_once(" ...]\n").expect("marker end");
    let omitted = marker
        .strip_suffix(" bytes omitted")
        .and_then(|count| count.parse::<usize>().ok())
        .expect("omitted byte count");

    (head, tail, omitted)
}