agentty 0.13.5

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

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

/// Non-content rows excluded from issue-detail half-page scrolling.
const ISSUE_DETAIL_PAGE_INSET: u16 = 3;
/// Non-content rows excluded from review-detail half-page scrolling.
const REVIEW_DETAIL_PAGE_INSET: u16 = 5;

/// Handles key input while an issue or requested-review detail page is visible.
pub(crate) async fn handle_with_cache(
    app: &mut App,
    render_cache_store: &RenderCacheStore,
    content_area: Rect,
    key: KeyEvent,
) -> EventResult {
    if key.code == KeyCode::Char('a')
        && key.modifiers == KeyModifiers::NONE
        && let AppMode::IssueDetail {
            detail,
            error,
            issue,
            scroll_offset,
            ..
        } = &app.mode
    {
        let detail = detail.clone();
        let detail_error = error.clone();
        let issue = issue.clone();
        let issue_url = issue.web_url.clone();
        let scroll_offset = *scroll_offset;
        if let Err(error) = app.start_issue_session(&issue_url).await {
            app.mode = AppMode::IssueDetail {
                action_error: Some(format!("Failed to start issue session: {error}")),
                detail,
                error: detail_error,
                issue,
                scroll_offset,
            };
        }

        return EventResult::Continue;
    }

    handle_detail_with_cache(app, render_cache_store, content_area, key)
}

/// Applies read-only navigation and scrolling for issue and review details.
fn handle_detail_with_cache(
    app: &mut App,
    render_cache_store: &RenderCacheStore,
    content_area: Rect,
    key: KeyEvent,
) -> EventResult {
    if matches!(key.code, KeyCode::Esc | KeyCode::Char('q')) {
        app.mode = AppMode::List;

        return EventResult::Continue;
    }

    let (max_scroll_offset, half_page_inset) = match &app.mode {
        AppMode::IssueDetail {
            action_error,
            detail,
            error,
            issue,
            ..
        } => (
            page::issue_detail::issue_detail_max_scroll_offset(
                issue,
                detail.as_ref(),
                error.as_deref(),
                action_error.as_deref(),
                content_area,
                render_cache_store.markdown_render_cache(),
            ),
            ISSUE_DETAIL_PAGE_INSET,
        ),
        AppMode::ReviewDetail {
            comment_error,
            is_loading_comments,
            review,
            ..
        } => (
            page::review_detail::review_detail_max_scroll_offset(
                review,
                comment_error.as_deref(),
                *is_loading_comments,
                content_area,
                render_cache_store.markdown_render_cache(),
            ),
            REVIEW_DETAIL_PAGE_INSET,
        ),
        _ => (0, 0),
    };

    if let AppMode::IssueDetail { scroll_offset, .. }
    | AppMode::ReviewDetail { scroll_offset, .. } = &mut app.mode
    {
        let clamped_scroll_offset = (*scroll_offset).min(max_scroll_offset);
        *scroll_offset = match key.code {
            KeyCode::Down | KeyCode::Char('j') if key.modifiers == KeyModifiers::NONE => {
                clamped_scroll_offset
                    .saturating_add(1)
                    .min(max_scroll_offset)
            }
            KeyCode::Up | KeyCode::Char('k') if key.modifiers == KeyModifiers::NONE => {
                clamped_scroll_offset.saturating_sub(1)
            }
            KeyCode::Char('d') if key.modifiers == KeyModifiers::CONTROL => clamped_scroll_offset
                .saturating_add(detail_half_page_step(content_area, half_page_inset))
                .min(max_scroll_offset),
            KeyCode::Char('u') if key.modifiers == KeyModifiers::CONTROL => clamped_scroll_offset
                .saturating_sub(detail_half_page_step(content_area, half_page_inset)),
            KeyCode::Char('g') if key.modifiers == KeyModifiers::NONE => 0,
            KeyCode::Char('G') if key.modifiers == KeyModifiers::SHIFT => max_scroll_offset,
            KeyCode::Char('G') if key.modifiers == KeyModifiers::NONE => max_scroll_offset,
            _ => clamped_scroll_offset,
        };
    }

    EventResult::Continue
}

#[cfg(test)]
fn handle(app: &mut App, content_area: Rect, key: KeyEvent) -> EventResult {
    handle_detail_with_cache(app, &RenderCacheStore::default(), content_area, key)
}

/// Returns the half-page scroll step for one detail viewport.
fn detail_half_page_step(content_area: Rect, page_inset: u16) -> u16 {
    content_area
        .height
        .saturating_sub(page_inset)
        .saturating_div(2)
        .max(1)
}

#[cfg(test)]
mod tests {
    use ag_forge::{
        AssignedIssue, ForgeKind, IssueDetail, RequestedReview, RequestedReviewAudience,
    };
    use crossterm::event::KeyModifiers;

    use super::*;

    #[tokio::test]
    async fn test_handle_q_returns_from_issue_detail_to_list_mode() {
        // Arrange
        let mut app = crate::test_support::new_test_app_without_retained_base_dir().await;
        app.mode = AppMode::IssueDetail {
            action_error: None,
            detail: None,
            error: None,
            issue: assigned_issue(),
            scroll_offset: 0,
        };

        // Act
        let event_result = handle(
            &mut app,
            Rect::new(0, 0, 80, 20),
            KeyEvent::new(KeyCode::Char('q'), KeyModifiers::NONE),
        );

        // Assert
        assert!(matches!(event_result, EventResult::Continue));
        assert!(matches!(app.mode, AppMode::List));
    }

    #[tokio::test]
    async fn test_handle_a_starts_session_for_issue_url() {
        // Arrange
        let (mut app, _base_dir) =
            crate::test_support::new_git_test_app_with_mock_tmux_client().await;
        let issue = assigned_issue();
        let expected_prompt = format!("Address this issue: {}", issue.web_url);
        app.mode = AppMode::IssueDetail {
            action_error: None,
            detail: None,
            error: None,
            issue,
            scroll_offset: 0,
        };

        // Act
        let event_result = handle_with_cache(
            &mut app,
            &RenderCacheStore::default(),
            Rect::new(0, 0, 80, 20),
            KeyEvent::new(KeyCode::Char('a'), KeyModifiers::NONE),
        )
        .await;

        // Assert
        assert!(matches!(event_result, EventResult::Continue));
        assert_eq!(app.sessions.sessions().len(), 1);
        assert_eq!(app.sessions.sessions()[0].prompt, expected_prompt);
        assert!(matches!(app.mode, AppMode::View { .. }));
    }

    #[tokio::test]
    async fn test_handle_a_preserves_issue_detail_when_session_creation_fails() {
        // Arrange
        let mut app = crate::test_support::new_test_app_without_retained_base_dir().await;
        let issue = assigned_issue();
        let detail = issue_detail("Issue description remains visible.");
        app.mode = AppMode::IssueDetail {
            action_error: None,
            detail: Some(detail),
            error: None,
            issue,
            scroll_offset: 2,
        };

        // Act
        let event_result = handle_with_cache(
            &mut app,
            &RenderCacheStore::default(),
            Rect::new(0, 0, 80, 20),
            KeyEvent::new(KeyCode::Char('a'), KeyModifiers::NONE),
        )
        .await;

        // Assert
        assert!(matches!(event_result, EventResult::Continue));
        assert!(app.sessions.sessions().is_empty());
        assert!(matches!(
            app.mode,
            AppMode::IssueDetail {
                action_error: Some(ref error),
                detail: Some(_),
                error: None,
                scroll_offset: 2,
                ..
            } if error.contains("Failed to start issue session:")
        ));
    }

    #[tokio::test]
    async fn test_handle_scrolls_issue_detail_page_to_bottom() {
        // Arrange
        let mut app = crate::test_support::new_test_app_without_retained_base_dir().await;
        let content_area = Rect::new(0, 0, 80, 8);
        let issue = assigned_issue();
        let detail = issue_detail("line 1\nline 2\nline 3\nline 4\nline 5\nline 6");
        let render_cache_store = RenderCacheStore::default();
        let expected_scroll_offset = page::issue_detail::issue_detail_max_scroll_offset(
            &issue,
            Some(&detail),
            None,
            None,
            content_area,
            render_cache_store.markdown_render_cache(),
        );
        app.mode = AppMode::IssueDetail {
            action_error: None,
            detail: Some(detail),
            error: None,
            issue,
            scroll_offset: 0,
        };

        // Act
        handle(
            &mut app,
            content_area,
            KeyEvent::new(KeyCode::Char('G'), KeyModifiers::SHIFT),
        );

        // Assert
        assert!(expected_scroll_offset > 0);
        assert!(matches!(
            app.mode,
            AppMode::IssueDetail {
                scroll_offset,
                ..
            } if scroll_offset == expected_scroll_offset
        ));
    }

    #[tokio::test]
    async fn test_handle_q_returns_from_review_detail_to_list_mode() {
        // Arrange
        let mut app = new_test_app().await;

        // Act
        let event_result = handle(
            &mut app,
            Rect::new(0, 0, 80, 20),
            KeyEvent::new(KeyCode::Char('q'), KeyModifiers::NONE),
        );

        // Assert
        assert!(matches!(event_result, EventResult::Continue));
        assert!(matches!(app.mode, AppMode::List));
    }

    #[tokio::test]
    async fn test_handle_scrolls_detail_page_down_and_up() {
        // Arrange
        let mut app = new_test_app().await;
        app.mode = AppMode::ReviewDetail {
            comment_error: None,
            is_loading_comments: false,
            review: requested_review("line 1\nline 2\nline 3\nline 4\nline 5\nline 6"),
            scroll_offset: 0,
        };

        // Act
        handle(
            &mut app,
            Rect::new(0, 0, 80, 8),
            KeyEvent::new(KeyCode::Char('j'), KeyModifiers::NONE),
        );
        handle(
            &mut app,
            Rect::new(0, 0, 80, 8),
            KeyEvent::new(KeyCode::Char('k'), KeyModifiers::NONE),
        );

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

    /// Verifies `G` moves the requested-review detail page to its rendered
    /// bottom offset.
    #[tokio::test]
    async fn test_handle_scrolls_detail_page_to_bottom() {
        // Arrange
        let mut app = new_test_app().await;
        let content_area = Rect::new(0, 0, 80, 8);
        let review = requested_review("line 1\nline 2\nline 3\nline 4\nline 5\nline 6");
        let render_cache_store = RenderCacheStore::default();
        let expected_scroll_offset = page::review_detail::review_detail_max_scroll_offset(
            &review,
            None,
            false,
            content_area,
            render_cache_store.markdown_render_cache(),
        );
        app.mode = AppMode::ReviewDetail {
            comment_error: None,
            is_loading_comments: false,
            review,
            scroll_offset: 0,
        };

        // Act
        handle(
            &mut app,
            content_area,
            KeyEvent::new(KeyCode::Char('G'), KeyModifiers::SHIFT),
        );

        // Assert
        assert!(expected_scroll_offset > 0);
        assert!(matches!(
            app.mode,
            AppMode::ReviewDetail {
                scroll_offset,
                ..
            } if scroll_offset == expected_scroll_offset
        ));
    }

    /// Builds one assigned-issue row for shared detail navigation tests.
    fn assigned_issue() -> AssignedIssue {
        AssignedIssue {
            display_id: "#42".to_string(),
            repository: "agentty-xyz/agentty".to_string(),
            title: "Load issue details".to_string(),
            updated_at: None,
            web_url: "https://github.com/agentty-xyz/agentty/issues/42".to_string(),
        }
    }

    /// Builds one loaded issue detail for shared navigation tests.
    fn issue_detail(body: &str) -> IssueDetail {
        IssueDetail {
            assignees: Vec::new(),
            author: "octocat".to_string(),
            body: Some(body.to_string()),
            created_at: None,
            display_id: "#42".to_string(),
            labels: Vec::new(),
            repository: "agentty-xyz/agentty".to_string(),
            state: "OPEN".to_string(),
            title: "Load issue details".to_string(),
            updated_at: None,
            web_url: "https://github.com/agentty-xyz/agentty/issues/42".to_string(),
        }
    }

    /// Builds a minimal app in review-detail mode for key handling tests.
    async fn new_test_app() -> App {
        let mut app = crate::test_support::new_test_app_without_retained_base_dir().await;
        crate::test_support::set_review_detail_mode(&mut app, requested_review("Detail body"));

        app
    }

    /// Builds one requested review snapshot for detail-mode tests.
    fn requested_review(body: &str) -> RequestedReview {
        RequestedReview {
            audience: RequestedReviewAudience::Personal,
            author: "octocat".to_string(),
            body: Some(body.to_string()),
            comment_snapshot: None,
            display_id: "#42".to_string(),
            forge_kind: ForgeKind::GitHub,
            repository: "agentty-xyz/agentty".to_string(),
            status_summary: None,
            title: "Add review detail page".to_string(),
            updated_at: Some("2026-04-27T21:30:00Z".to_string()),
            web_url: "https://example.com/42".to_string(),
        }
    }
}