agentty 0.14.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
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
use ag_forge::ReviewCommentSnapshot;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::layout::Rect;

use crate::app::App;
use crate::app::prompt_intent::ReviewCommentResolutionOutcome;
use crate::presentation::app_mode::{AppMode, ReviewCommentAction, ReviewCommentActionSelection};
use crate::presentation::review_comment;
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 exit_review_comments(app, &key) {
        return EventResult::Continue;
    }

    let mode = std::mem::replace(&mut app.mode, AppMode::List);
    let AppMode::ReviewComments {
        mut comment_actions,
        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 can_reply = session_allows_review_comment_reply(app, session_id.as_str());
    let item_count = page::review_comment::review_comment_item_count(comment_snapshot.as_ref());
    if key.code == KeyCode::Enter
        && key.modifiers == KeyModifiers::NONE
        && !comment_actions.is_empty()
        && let Some(snapshot) = comment_snapshot.as_ref()
    {
        let snapshot = snapshot.clone();
        let submitted_actions = comment_actions.clone();
        app.mode = AppMode::ReviewComments {
            comment_actions,
            comment_error,
            comment_snapshot,
            diff,
            is_loading_comments,
            selected_comment_index,
            session_id: session_id.clone(),
            scroll_offset,
        };
        let outcome = app
            .resolve_session_review_comments(&session_id, &snapshot, &submitted_actions)
            .await;
        apply_review_comment_resolution_outcome(app, outcome);

        return EventResult::Continue;
    }

    if can_reply {
        toggle_selected_comment_action(
            &key,
            comment_snapshot.as_ref(),
            selected_comment_index,
            &mut comment_actions,
        );
    }
    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_actions,
        comment_error,
        comment_snapshot,
        diff,
        is_loading_comments,
        selected_comment_index,
        session_id,
        scroll_offset,
    };

    EventResult::Continue
}

/// Returns whether the review-comments page still belongs to a session that
/// may accept direct user-driven comment work.
fn session_allows_review_comment_reply(app: &App, session_id: &str) -> bool {
    app.sessions
        .sessions()
        .iter()
        .find(|session| session.id == session_id)
        .is_some_and(crate::domain::session::Session::allows_review_comment_reply)
}

/// Restores session view when the review-comments exit key is pressed.
fn exit_review_comments(app: &mut App, key: &KeyEvent) -> bool {
    if !matches!(key.code, KeyCode::Char('q') | KeyCode::Esc) {
        return false;
    }

    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;
    }

    true
}

/// Applies an address or deny toggle to the selected actionable thread.
fn toggle_selected_comment_action(
    key: &KeyEvent,
    comment_snapshot: Option<&ReviewCommentSnapshot>,
    selected_comment_index: usize,
    comment_actions: &mut Vec<ReviewCommentActionSelection>,
) {
    let action = match key.code {
        KeyCode::Char('a') if key.modifiers == KeyModifiers::NONE => ReviewCommentAction::Address,
        KeyCode::Char('d') if key.modifiers == KeyModifiers::NONE => ReviewCommentAction::Deny,
        _ => return,
    };
    let Some(thread_id) = comment_snapshot.and_then(|snapshot| {
        review_comment::selected_actionable_thread_id(snapshot, selected_comment_index)
    }) else {
        return;
    };

    review_comment::toggle_action(comment_actions, thread_id, action);
}

/// Applies presentation navigation returned by the review-comment workflow.
fn apply_review_comment_resolution_outcome(app: &mut App, outcome: ReviewCommentResolutionOutcome) {
    match outcome {
        ReviewCommentResolutionOutcome::KeepReviewComments => {}
        ReviewCommentResolutionOutcome::ShowSession { session_id } => {
            app.mode = AppMode::View {
                session_id,
                scroll_offset: None,
            };
        }
    }
}

/// 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::*;
    use crate::domain::session::{SessionId, SessionRole, Status};
    use crate::test_support::SessionFixtureBuilder;

    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_marks_address_replaces_with_deny_and_toggles_off() {
        // Arrange
        let mut app = crate::test_support::new_test_app_without_retained_base_dir().await;
        app.sessions.push_session(
            SessionFixtureBuilder::new()
                .id("session-id")
                .status(Status::Review)
                .build(),
        );
        app.mode = AppMode::ReviewComments {
            comment_actions: Vec::new(),
            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
        for key_code in [KeyCode::Char('a'), KeyCode::Char('d'), KeyCode::Char('d')] {
            handle_with_cache(
                &mut app,
                &RenderCacheStore::default(),
                Rect::new(0, 0, 80, 24),
                KeyEvent::new(key_code, KeyModifiers::NONE),
            )
            .await;
        }

        // Assert
        assert!(matches!(
            app.mode,
            AppMode::ReviewComments {
                ref comment_actions,
                ..
            } if comment_actions.is_empty()
        ));
    }

    #[tokio::test]
    async fn managed_session_cannot_mark_review_comments() {
        // Arrange
        let mut app = crate::test_support::new_test_app_without_retained_base_dir().await;
        app.sessions.push_session(
            SessionFixtureBuilder::new()
                .id("session-id")
                .role(SessionRole::OrchestrationWorker)
                .status(Status::Review)
                .build(),
        );
        app.mode = AppMode::ReviewComments {
            comment_actions: Vec::new(),
            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, 24),
            KeyEvent::new(KeyCode::Char('a'), KeyModifiers::NONE),
        )
        .await;

        // Assert
        assert!(matches!(
            app.mode,
            AppMode::ReviewComments {
                ref comment_actions,
                ..
            } if comment_actions.is_empty()
        ));
    }

    #[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_actions: Vec::new(),
            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_actions: Vec::new(),
            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_actions: Vec::new(),
            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_actions: Vec::new(),
            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_actions: Vec::new(),
            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_batch_keys_reject_read_only_rows_and_preserve_failed_submission() {
        // Arrange
        let mut selected_app = crate::test_support::new_test_app_without_retained_base_dir().await;
        selected_app.mode = AppMode::ReviewComments {
            comment_actions: Vec::new(),
            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 submit_app = crate::test_support::new_test_app_without_retained_base_dir().await;
        submit_app.mode = AppMode::ReviewComments {
            comment_actions: vec![ReviewCommentActionSelection {
                action: ReviewCommentAction::Address,
                thread_id: "thread-id".to_string(),
            }],
            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 submit_app,
            &RenderCacheStore::default(),
            Rect::new(0, 0, 80, 24),
            KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE),
        )
        .await;

        // Assert
        assert!(matches!(
            selected_app.mode,
            AppMode::ReviewComments {
                ref comment_actions,
                selected_comment_index: 1,
                scroll_offset: 3,
                ..
            } if comment_actions.is_empty()
        ));
        assert!(matches!(
            submit_app.mode,
            AppMode::ReviewComments {
                ref comment_actions,
                selected_comment_index: 1,
                scroll_offset: 3,
                ..
            } if comment_actions.len() == 1
        ));
    }

    #[tokio::test]
    async fn test_apply_review_comment_resolution_outcome_shows_session() {
        // Arrange
        let mut app = crate::test_support::new_test_app_without_retained_base_dir().await;
        let session_id = SessionId::from("session-id");

        // Act
        apply_review_comment_resolution_outcome(
            &mut app,
            ReviewCommentResolutionOutcome::ShowSession {
                session_id: session_id.clone(),
            },
        );

        // Assert
        assert!(matches!(
            app.mode,
            AppMode::View {
                session_id: viewed_session_id,
                scroll_offset: None,
            } if viewed_session_id == session_id
        ));
    }

    #[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);
    }
}