agentty 0.13.3

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
442
443
444
445
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::layout::Rect;

use crate::app::App;
use crate::app::prompt_intent::ReviewCommentSelection;
use crate::presentation::app_mode::AppMode;
use crate::runtime::EventResult;
use crate::ui::{RenderCacheStore, page};

/// Handles agent-resolution, selection, detail scrolling, and exit keys for
/// the session review-comment page.
pub(crate) async fn handle_with_cache(
    app: &mut App,
    render_cache_store: &RenderCacheStore,
    content_area: Rect,
    key: KeyEvent,
) -> EventResult {
    if matches!(key.code, KeyCode::Char('q') | KeyCode::Esc) {
        let mode = std::mem::replace(&mut app.mode, AppMode::List);
        if let AppMode::ReviewComments { session_id, .. } = mode {
            app.mode = AppMode::View {
                session_id,
                scroll_offset: None,
            };
        } else {
            app.mode = mode;
        }

        return EventResult::Continue;
    }

    let mode = std::mem::replace(&mut app.mode, AppMode::List);
    let AppMode::ReviewComments {
        comment_error,
        comment_snapshot,
        diff,
        is_loading_comments,
        mut selected_comment_index,
        session_id,
        mut scroll_offset,
    } = mode
    else {
        app.mode = mode;

        return EventResult::Continue;
    };
    let item_count = page::review_comment::review_comment_item_count(comment_snapshot.as_ref());
    let resolution_selection = match key.code {
        KeyCode::Char('a') if key.modifiers == KeyModifiers::NONE => {
            Some(ReviewCommentSelection::Selected(selected_comment_index))
        }
        KeyCode::Char('A') if key.modifiers == KeyModifiers::SHIFT => {
            Some(ReviewCommentSelection::AllUnresolved)
        }
        _ => None,
    };
    if let (Some(selection), Some(snapshot)) = (resolution_selection, comment_snapshot.as_ref()) {
        let snapshot = snapshot.clone();
        app.mode = AppMode::ReviewComments {
            comment_error,
            comment_snapshot,
            diff,
            is_loading_comments,
            selected_comment_index,
            session_id: session_id.clone(),
            scroll_offset,
        };
        app.resolve_session_review_comments(&session_id, &snapshot, selection)
            .await;

        return EventResult::Continue;
    }

    match key.code {
        KeyCode::Char('j') if key.modifiers == KeyModifiers::NONE => {
            let next_index = next_selected_index(selected_comment_index, item_count);
            if next_index != selected_comment_index {
                selected_comment_index = next_index;
                scroll_offset = 0;
            }
        }
        KeyCode::Char('k') if key.modifiers == KeyModifiers::NONE => {
            let previous_index = previous_selected_index(selected_comment_index, item_count);
            if previous_index != selected_comment_index {
                selected_comment_index = previous_index;
                scroll_offset = 0;
            }
        }
        KeyCode::Down => {
            let max_scroll_offset = page::review_comment::review_comment_view_max_scroll_offset(
                comment_snapshot.as_ref(),
                comment_error.as_deref(),
                is_loading_comments,
                &diff,
                selected_comment_index,
                content_area,
                render_cache_store.markdown_render_cache(),
            );
            scroll_offset = scroll_offset
                .min(max_scroll_offset)
                .saturating_add(1)
                .min(max_scroll_offset);
        }
        KeyCode::Up => {
            scroll_offset = scroll_offset.saturating_sub(1);
        }
        _ => {}
    }

    app.mode = AppMode::ReviewComments {
        comment_error,
        comment_snapshot,
        diff,
        is_loading_comments,
        selected_comment_index,
        session_id,
        scroll_offset,
    };

    EventResult::Continue
}

/// Returns the next wrapped selection index.
fn next_selected_index(selected_index: usize, item_count: usize) -> usize {
    if item_count == 0 {
        return selected_index;
    }

    (selected_index.min(item_count - 1) + 1) % item_count
}

/// Returns the previous wrapped selection index.
fn previous_selected_index(selected_index: usize, item_count: usize) -> usize {
    if item_count == 0 {
        return selected_index;
    }
    let selected_index = selected_index.min(item_count - 1);
    if selected_index == 0 {
        return item_count - 1;
    }

    selected_index - 1
}

#[cfg(test)]
mod tests {
    use ag_forge::{
        ReviewComment, ReviewCommentAnchorSide, ReviewCommentSnapshot, ReviewCommentThread,
    };

    use super::*;

    fn comment_snapshot() -> ReviewCommentSnapshot {
        ReviewCommentSnapshot {
            pr_level_comments: vec![ReviewComment {
                author: "alice".to_string(),
                body: "General comment".to_string(),
            }],
            threads: vec![ReviewCommentThread {
                anchor_side: ReviewCommentAnchorSide::New,
                comments: vec![ReviewComment {
                    author: "bob".to_string(),
                    body: "Inline comment".to_string(),
                }],
                id: "thread-id".to_string(),
                is_outdated: Some(false),
                is_resolved: false,
                line: Some(2),
                path: "src/main.rs".to_string(),
                start_line: None,
            }],
        }
    }

    #[tokio::test]
    async fn test_handle_selects_next_comment_and_resets_detail_scroll() {
        // Arrange
        let mut app = crate::test_support::new_test_app_without_retained_base_dir().await;
        app.mode = AppMode::ReviewComments {
            comment_error: None,
            comment_snapshot: Some(comment_snapshot()),
            diff: String::new(),
            is_loading_comments: false,
            selected_comment_index: 0,
            session_id: "session-id".into(),
            scroll_offset: 4,
        };

        // Act
        handle_with_cache(
            &mut app,
            &RenderCacheStore::default(),
            Rect::new(0, 0, 80, 24),
            KeyEvent::new(KeyCode::Char('j'), KeyModifiers::NONE),
        )
        .await;

        // Assert
        assert!(matches!(
            app.mode,
            AppMode::ReviewComments {
                selected_comment_index: 1,
                scroll_offset: 0,
                ..
            }
        ));
    }

    #[tokio::test]
    async fn test_handle_q_restores_session_view() {
        // Arrange
        let mut app = crate::test_support::new_test_app_without_retained_base_dir().await;
        app.mode = AppMode::ReviewComments {
            comment_error: None,
            comment_snapshot: None,
            diff: String::new(),
            is_loading_comments: true,
            selected_comment_index: 0,
            session_id: "session-id".into(),
            scroll_offset: 0,
        };

        // Act
        handle_with_cache(
            &mut app,
            &RenderCacheStore::default(),
            Rect::new(0, 0, 80, 24),
            KeyEvent::new(KeyCode::Char('q'), KeyModifiers::NONE),
        )
        .await;

        // Assert
        assert!(matches!(
            app.mode,
            AppMode::View {
                ref session_id,
                scroll_offset: None,
            } if session_id == "session-id"
        ));
    }

    #[tokio::test]
    async fn test_handle_selects_previous_comment_and_resets_detail_scroll() {
        // Arrange
        let mut app = crate::test_support::new_test_app_without_retained_base_dir().await;
        app.mode = AppMode::ReviewComments {
            comment_error: None,
            comment_snapshot: Some(comment_snapshot()),
            diff: String::new(),
            is_loading_comments: false,
            selected_comment_index: 1,
            session_id: "session-id".into(),
            scroll_offset: 4,
        };

        // Act
        handle_with_cache(
            &mut app,
            &RenderCacheStore::default(),
            Rect::new(0, 0, 80, 24),
            KeyEvent::new(KeyCode::Char('k'), KeyModifiers::NONE),
        )
        .await;

        // Assert
        assert!(matches!(
            app.mode,
            AppMode::ReviewComments {
                selected_comment_index: 0,
                scroll_offset: 0,
                ..
            }
        ));
    }

    #[tokio::test]
    async fn test_handle_down_scrolls_within_rendered_detail() {
        // Arrange
        let mut app = crate::test_support::new_test_app_without_retained_base_dir().await;
        app.mode = AppMode::ReviewComments {
            comment_error: None,
            comment_snapshot: Some(comment_snapshot()),
            diff: String::new(),
            is_loading_comments: false,
            selected_comment_index: 0,
            session_id: "session-id".into(),
            scroll_offset: 0,
        };

        // Act
        handle_with_cache(
            &mut app,
            &RenderCacheStore::default(),
            Rect::new(0, 0, 80, 8),
            KeyEvent::new(KeyCode::Down, KeyModifiers::NONE),
        )
        .await;

        // Assert
        assert!(matches!(
            app.mode,
            AppMode::ReviewComments {
                scroll_offset: 1,
                ..
            }
        ));
    }

    #[tokio::test]
    async fn test_handle_up_decrements_scroll_and_other_keys_preserve_mode() {
        // Arrange
        let mut app = crate::test_support::new_test_app_without_retained_base_dir().await;
        app.mode = AppMode::ReviewComments {
            comment_error: None,
            comment_snapshot: Some(comment_snapshot()),
            diff: String::new(),
            is_loading_comments: false,
            selected_comment_index: 0,
            session_id: "session-id".into(),
            scroll_offset: 2,
        };

        // Act
        handle_with_cache(
            &mut app,
            &RenderCacheStore::default(),
            Rect::new(0, 0, 80, 24),
            KeyEvent::new(KeyCode::Up, KeyModifiers::NONE),
        )
        .await;
        handle_with_cache(
            &mut app,
            &RenderCacheStore::default(),
            Rect::new(0, 0, 80, 24),
            KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE),
        )
        .await;

        // Assert
        assert!(matches!(
            app.mode,
            AppMode::ReviewComments {
                scroll_offset: 1,
                ..
            }
        ));
    }

    #[tokio::test]
    async fn test_handle_agent_resolution_keys_preserve_page_when_session_cannot_reply() {
        // Arrange
        let mut selected_app = crate::test_support::new_test_app_without_retained_base_dir().await;
        selected_app.mode = AppMode::ReviewComments {
            comment_error: None,
            comment_snapshot: Some(comment_snapshot()),
            diff: String::new(),
            is_loading_comments: false,
            selected_comment_index: 1,
            session_id: "missing-session".into(),
            scroll_offset: 3,
        };
        let mut all_app = crate::test_support::new_test_app_without_retained_base_dir().await;
        all_app.mode = AppMode::ReviewComments {
            comment_error: None,
            comment_snapshot: Some(comment_snapshot()),
            diff: String::new(),
            is_loading_comments: false,
            selected_comment_index: 1,
            session_id: "missing-session".into(),
            scroll_offset: 3,
        };

        // Act
        handle_with_cache(
            &mut selected_app,
            &RenderCacheStore::default(),
            Rect::new(0, 0, 80, 24),
            KeyEvent::new(KeyCode::Char('a'), KeyModifiers::NONE),
        )
        .await;
        handle_with_cache(
            &mut all_app,
            &RenderCacheStore::default(),
            Rect::new(0, 0, 80, 24),
            KeyEvent::new(KeyCode::Char('A'), KeyModifiers::SHIFT),
        )
        .await;

        // Assert
        assert!(matches!(
            selected_app.mode,
            AppMode::ReviewComments {
                selected_comment_index: 1,
                scroll_offset: 3,
                ..
            }
        ));
        assert!(matches!(
            all_app.mode,
            AppMode::ReviewComments {
                selected_comment_index: 1,
                scroll_offset: 3,
                ..
            }
        ));
    }

    #[tokio::test]
    async fn test_handle_preserves_non_review_comment_modes() {
        // Arrange
        let mut exit_app = crate::test_support::new_test_app_without_retained_base_dir().await;
        let mut other_app = crate::test_support::new_test_app_without_retained_base_dir().await;

        // Act
        handle_with_cache(
            &mut exit_app,
            &RenderCacheStore::default(),
            Rect::new(0, 0, 80, 24),
            KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE),
        )
        .await;
        handle_with_cache(
            &mut other_app,
            &RenderCacheStore::default(),
            Rect::new(0, 0, 80, 24),
            KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE),
        )
        .await;

        // Assert
        assert!(matches!(exit_app.mode, AppMode::List));
        assert!(matches!(other_app.mode, AppMode::List));
    }

    #[test]
    fn test_selection_helpers_wrap_clamp_and_preserve_empty_selection() {
        // Arrange, Act, Assert
        assert_eq!(next_selected_index(0, 0), 0);
        assert_eq!(next_selected_index(1, 2), 0);
        assert_eq!(next_selected_index(usize::MAX, 2), 0);
        assert_eq!(previous_selected_index(0, 0), 0);
        assert_eq!(previous_selected_index(0, 2), 1);
        assert_eq!(previous_selected_index(usize::MAX, 2), 0);
    }
}