ag-agent 0.21.0

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
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
//! Agent CLI stdin payload writer unit tests.

use std::io;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use ag_protocol::TurnPrompt;
use tokio::io::{AsyncReadExt as _, AsyncWrite};
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;

use crate::agent::AgentBackendError;
use crate::agent::cli::stdin::{
    StdinSteering, await_optional_stdin_write, is_broken_pipe_error, spawn_optional_stdin_write,
    spawn_steering_stdin_write, write_optional_stdin, write_steering_stdin,
};
use crate::app_server::TurnSteeringInbox;

/// Test-only error type used to verify generic error formatting.
#[derive(Debug, PartialEq)]
struct TestError(String);

/// Converts a string message into `TestError` for use as the
/// `format_error` callback.
fn make_test_error(message: String) -> TestError {
    TestError(message)
}

/// Stdin stand-in whose writes and shutdown fail with configured error kinds.
struct FailingStdin {
    shutdown_error: Option<io::ErrorKind>,
    write_error: Option<io::ErrorKind>,
}

impl AsyncWrite for FailingStdin {
    fn poll_write(
        self: Pin<&mut Self>,
        _context: &mut Context<'_>,
        buffer: &[u8],
    ) -> Poll<io::Result<usize>> {
        Poll::Ready(
            self.write_error
                .map_or(Ok(buffer.len()), |kind| Err(kind.into())),
        )
    }

    fn poll_flush(self: Pin<&mut Self>, _context: &mut Context<'_>) -> Poll<io::Result<()>> {
        Poll::Ready(Ok(()))
    }

    fn poll_shutdown(self: Pin<&mut Self>, _context: &mut Context<'_>) -> Poll<io::Result<()>> {
        Poll::Ready(self.shutdown_error.map_or(Ok(()), |kind| Err(kind.into())))
    }
}

// --- write_optional_stdin ---

#[tokio::test]
async fn test_write_tolerates_closed_pipes_and_reports_other_failures() {
    // Arrange
    let write_cases = [
        (
            Some(io::ErrorKind::BrokenPipe),
            Some(io::ErrorKind::BrokenPipe),
            None,
        ),
        (
            Some(io::ErrorKind::PermissionDenied),
            None,
            Some("Failed to write stdin payload: permission denied"),
        ),
        (
            None,
            Some(io::ErrorKind::PermissionDenied),
            Some("Failed to close stdin payload: permission denied"),
        ),
    ];

    for (write_error, shutdown_error, expected_error) in write_cases {
        let child_stdin = FailingStdin {
            shutdown_error,
            write_error,
        };

        // Act
        let result = write_optional_stdin(
            Some(child_stdin),
            b"prompt".to_vec(),
            "stdin unavailable",
            make_test_error,
        )
        .await;

        // Assert
        assert_eq!(
            result,
            expected_error.map_or(Ok(()), |message| Err(TestError(message.to_string())))
        );
    }
}

// --- spawn_optional_stdin_write ---

#[test]
fn test_spawn_returns_none_when_no_payload() {
    // Arrange / Act
    let result =
        spawn_optional_stdin_write::<TestError>(None, None, "stdin unavailable", make_test_error);

    // Assert
    assert!(result.is_none());
}

#[tokio::test]
async fn test_spawn_returns_error_when_stdin_unavailable() {
    // Arrange
    let payload = Some(b"prompt".to_vec());

    // Act
    let handle = spawn_optional_stdin_write::<TestError>(
        None,
        payload,
        "stdin unavailable",
        make_test_error,
    );
    let join_result = handle
        .expect("expected a task handle")
        .await
        .expect("task should not panic");

    // Assert
    assert_eq!(join_result, Err(TestError("stdin unavailable".to_string())));
}

#[tokio::test]
async fn test_spawn_writes_payload_to_child_stdin() {
    // Arrange
    let mut child_command = tokio::process::Command::new("cat");
    child_command.stdin(std::process::Stdio::piped());
    child_command.stdout(std::process::Stdio::piped());
    let mut child = child_command.spawn().expect("failed to spawn cat");
    let child_stdin = child.stdin.take();
    let payload = Some(b"hello world".to_vec());

    // Act
    let handle = spawn_optional_stdin_write::<TestError>(
        child_stdin,
        payload,
        "stdin unavailable",
        make_test_error,
    );
    let write_result = handle
        .expect("expected a task handle")
        .await
        .expect("task should not panic");

    // Assert
    assert!(write_result.is_ok());
    let output = child
        .wait_with_output()
        .await
        .expect("failed to read cat output");
    assert_eq!(output.stdout, b"hello world");
}

// --- await_optional_stdin_write ---

#[tokio::test]
async fn test_await_returns_ok_when_no_task() {
    // Arrange / Act
    let result =
        await_optional_stdin_write::<TestError>(None, "join failed", make_test_error).await;

    // Assert
    assert!(result.is_ok());
}

#[tokio::test]
async fn test_await_propagates_inner_error() {
    // Arrange
    let handle = tokio::spawn(async { Err(TestError("write failed".to_string())) });

    // Act
    let result = await_optional_stdin_write(Some(handle), "join failed", make_test_error).await;

    // Assert
    assert_eq!(result, Err(TestError("write failed".to_string())));
}

#[tokio::test]
async fn test_await_returns_join_error_when_task_is_aborted() {
    // Arrange
    let handle: JoinHandle<Result<(), TestError>> = tokio::spawn(async {
        tokio::time::sleep(std::time::Duration::from_mins(1)).await;

        Ok(())
    });
    handle.abort();

    // Act
    let result = await_optional_stdin_write(Some(handle), "join failed", make_test_error).await;

    // Assert
    let error = result.expect_err("expected a join error");
    assert!(error.0.starts_with("join failed:"));
}

// --- is_broken_pipe_error ---

#[test]
fn test_broken_pipe_returns_true() {
    // Arrange
    let error = io::Error::new(io::ErrorKind::BrokenPipe, "broken pipe");

    // Act / Assert
    assert!(is_broken_pipe_error(&error));
}

#[test]
fn test_non_broken_pipe_returns_false() {
    // Arrange
    let error = io::Error::new(io::ErrorKind::PermissionDenied, "denied");

    // Act / Assert
    assert!(!is_broken_pipe_error(&error));
}

// --- write_steering_stdin ---

/// Encodes one steering prompt as its text followed by a newline.
const ENCODE_LINE: fn(&TurnPrompt) -> Result<Vec<u8>, AgentBackendError> =
    |prompt| Ok(format!("{}\n", prompt.text).into_bytes());

/// Rejects every steering prompt as unencodable.
fn reject_prompt(_prompt: &TurnPrompt) -> Result<Vec<u8>, AgentBackendError> {
    Err(AgentBackendError::command_build("unencodable prompt"))
}

/// Builds steering over a fresh inbox with the given encoder.
fn stdin_steering(
    encode_prompt: fn(&TurnPrompt) -> Result<Vec<u8>, AgentBackendError>,
) -> StdinSteering {
    StdinSteering {
        encode_prompt,
        inbox: Arc::new(TurnSteeringInbox::new()),
    }
}

#[tokio::test]
async fn steering_writer_delivers_prompts_until_the_turn_ends() {
    // Arrange
    let steering = stdin_steering(ENCODE_LINE);
    let turn_ended = CancellationToken::new();
    let acknowledgement = steering
        .inbox
        .submit("also add tests".into())
        .expect("open inbox");
    let (writer, mut reader) = tokio::io::duplex(1024);
    let writing = tokio::spawn({
        let turn_ended = turn_ended.clone();

        async move {
            write_steering_stdin(
                writer,
                b"initial\n".to_vec(),
                &steering,
                &turn_ended,
                make_test_error,
            )
            .await
        }
    });

    // Act
    let delivered = acknowledgement.await.expect("reply");
    turn_ended.cancel();
    let result = writing.await.expect("writer task");
    let mut written = String::new();
    reader
        .read_to_string(&mut written)
        .await
        .expect("closed stdin");

    // Assert
    assert!(delivered.is_ok());
    assert_eq!(result, Ok(()));
    assert_eq!(written, "initial\nalso add tests\n");
}

#[tokio::test]
async fn steering_writer_rejects_unencodable_and_refused_prompts() {
    // Arrange
    let unencodable = stdin_steering(reject_prompt);
    let unencodable_ack = unencodable.inbox.submit("image".into()).expect("open");
    let refused = stdin_steering(ENCODE_LINE);
    let refused_ack = refused.inbox.submit("late".into()).expect("open");
    let turn_ended = CancellationToken::new();
    let (writer, _reader) = tokio::io::duplex(1024);
    let unencodable_writing = tokio::spawn({
        let turn_ended = turn_ended.clone();

        async move {
            write_steering_stdin(
                writer,
                Vec::new(),
                &unencodable,
                &turn_ended,
                make_test_error,
            )
            .await
        }
    });
    let refusing_stdin = FailingStdin {
        shutdown_error: None,
        write_error: None,
    };
    let refusing_writer = RefuseAfterFirstWrite {
        inner: refusing_stdin,
        writes: 0,
    };

    // Act
    let unencodable_reply = unencodable_ack.await.expect("reply");
    turn_ended.cancel();
    let unencodable_result = unencodable_writing.await.expect("writer task");
    let refused_result = write_steering_stdin(
        refusing_writer,
        b"initial\n".to_vec(),
        &refused,
        &CancellationToken::new(),
        make_test_error,
    )
    .await;
    let refused_reply = refused_ack.await.expect("reply");

    // Assert
    assert_eq!(
        unencodable_reply.expect_err("unencodable").to_string(),
        "unencodable prompt"
    );
    assert_eq!(unencodable_result, Ok(()));
    assert!(refused_reply.is_err());
    assert_eq!(refused_result, Ok(()));
}

#[tokio::test]
async fn steering_writer_tolerates_closed_pipes_and_reports_other_failures() {
    // Arrange
    let cases = [
        (Some(io::ErrorKind::BrokenPipe), None, None),
        (
            Some(io::ErrorKind::PermissionDenied),
            None,
            Some("Failed to write stdin payload: permission denied"),
        ),
        (None, Some(io::ErrorKind::BrokenPipe), None),
        (
            None,
            Some(io::ErrorKind::PermissionDenied),
            Some("Failed to close stdin payload: permission denied"),
        ),
    ];
    let turn_ended = CancellationToken::new();
    turn_ended.cancel();

    for (write_error, shutdown_error, expected_error) in cases {
        let child_stdin = FailingStdin {
            shutdown_error,
            write_error,
        };

        // Act
        let result = write_steering_stdin(
            child_stdin,
            b"prompt".to_vec(),
            &stdin_steering(ENCODE_LINE),
            &turn_ended,
            make_test_error,
        )
        .await;

        // Assert
        assert_eq!(
            result,
            expected_error.map_or(Ok(()), |message| Err(TestError(message.to_string())))
        );
    }
}

#[tokio::test]
async fn spawned_steering_writer_closes_the_inbox_when_stdin_is_unavailable() {
    // Arrange
    let steering = stdin_steering(ENCODE_LINE);
    let inbox = Arc::clone(&steering.inbox);

    // Act
    let result = spawn_steering_stdin_write::<TestError>(
        None,
        b"prompt".to_vec(),
        steering,
        CancellationToken::new(),
        "stdin unavailable",
        make_test_error,
    )
    .await
    .expect("writer task");

    // Assert
    assert_eq!(result, Err(TestError("stdin unavailable".to_string())));
    assert!(inbox.submit("late".into()).is_err());
}

/// Stdin stand-in that accepts the first write and refuses later ones.
struct RefuseAfterFirstWrite {
    inner: FailingStdin,
    writes: usize,
}

impl AsyncWrite for RefuseAfterFirstWrite {
    fn poll_write(
        mut self: Pin<&mut Self>,
        context: &mut Context<'_>,
        buffer: &[u8],
    ) -> Poll<io::Result<usize>> {
        self.writes += 1;
        if self.writes > 1 {
            return Poll::Ready(Err(io::ErrorKind::BrokenPipe.into()));
        }

        Pin::new(&mut self.inner).poll_write(context, buffer)
    }

    fn poll_flush(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<io::Result<()>> {
        Pin::new(&mut self.inner).poll_flush(context)
    }

    fn poll_shutdown(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<io::Result<()>> {
        Pin::new(&mut self.inner).poll_shutdown(context)
    }
}