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
use ag_forge::{AssignedIssue, IssueDetail};
use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Paragraph};

use crate::presentation::help_action;
use crate::ui::{Page, help_format, layout, markdown, style};

/// Page renderer for one selected GitHub issue, its base details, and the
/// shortcut for starting an issue-addressing session.
pub struct IssueDetailPage<'a> {
    action_error: Option<&'a str>,
    detail: Option<&'a IssueDetail>,
    error: Option<&'a str>,
    issue: &'a AssignedIssue,
    markdown_render_cache: &'a markdown::MarkdownRenderCache,
    scroll_offset: u16,
}

impl<'a> IssueDetailPage<'a> {
    /// Creates an issue-detail renderer for a selected list row and its
    /// asynchronous detail state.
    pub fn new(
        issue: &'a AssignedIssue,
        detail: Option<&'a IssueDetail>,
        error: Option<&'a str>,
        action_error: Option<&'a str>,
        markdown_render_cache: &'a markdown::MarkdownRenderCache,
        scroll_offset: u16,
    ) -> Self {
        Self {
            action_error,
            detail,
            error,
            issue,
            markdown_render_cache,
            scroll_offset,
        }
    }
}

impl Page for IssueDetailPage<'_> {
    fn render(&mut self, frame: &mut Frame, area: Rect) {
        let areas = layout::tab_page_areas(area);
        let content_area = issue_detail_content_area(area, self.issue);
        let paragraph = Paragraph::new(issue_detail_lines(
            self.issue,
            self.detail,
            self.error,
            self.action_error,
            self.markdown_render_cache,
            usize::from(content_area.width),
        ))
        .block(issue_detail_block(self.issue))
        .style(Style::default().fg(style::palette::text()))
        .scroll((self.scroll_offset, 0));

        frame.render_widget(paragraph, areas.main_area);
        frame.render_widget(
            Paragraph::new(help_format::footer_line(
                &help_action::issue_detail_actions(),
            )),
            areas.footer_area,
        );
    }
}

/// Returns the largest valid vertical scroll offset for an issue detail page.
pub(crate) fn issue_detail_max_scroll_offset(
    issue: &AssignedIssue,
    detail: Option<&IssueDetail>,
    error: Option<&str>,
    action_error: Option<&str>,
    area: Rect,
    markdown_render_cache: &markdown::MarkdownRenderCache,
) -> u16 {
    let content_area = issue_detail_content_area(area, issue);
    let viewport_height = content_area.height;
    if viewport_height == 0 {
        return 0;
    }

    let line_count = issue_detail_lines(
        issue,
        detail,
        error,
        action_error,
        markdown_render_cache,
        usize::from(content_area.width),
    )
    .len();

    u16::try_from(line_count.saturating_sub(usize::from(viewport_height))).unwrap_or(u16::MAX)
}

/// Builds visible metadata and markdown description lines without comments.
fn issue_detail_lines(
    issue: &AssignedIssue,
    detail: Option<&IssueDetail>,
    error: Option<&str>,
    action_error: Option<&str>,
    markdown_render_cache: &markdown::MarkdownRenderCache,
    width: usize,
) -> Vec<Line<'static>> {
    let mut lines = action_error.map_or_else(Vec::new, |message| {
        vec![error_line(message), Line::from("")]
    });
    let Some(detail) = detail else {
        lines.push(Line::from(
            error.unwrap_or("Loading issue details...").to_string(),
        ));

        return lines;
    };
    if issue.display_id != detail.display_id {
        return vec![Line::from(
            "Issue details do not match the selected issue. Return to the list and reopen it.",
        )];
    }

    let assignees = list_text(&detail.assignees);
    let labels = list_text(&detail.labels);
    let created_at = timestamp_text(detail.created_at.as_deref());
    let updated_at = timestamp_text(detail.updated_at.as_deref());
    let description = detail
        .body
        .as_deref()
        .map(str::trim)
        .filter(|body| !body.is_empty())
        .unwrap_or("No description provided.");
    lines.extend([
        field_line("Title", &detail.title),
        field_line("State", &detail.state),
        field_line("Author", &detail.author),
        field_line("Assignees", &assignees),
        field_line("Labels", &labels),
        field_line("Created", created_at),
        field_line("Updated", updated_at),
        field_line("URL", &detail.web_url),
        Line::from(""),
        section_label("Description"),
    ]);
    lines.extend(
        markdown_render_cache
            .render_html(description, width)
            .iter()
            .cloned(),
    );

    lines
}

/// Formats a user-facing issue-detail failure.
fn error_line(text: &str) -> Line<'static> {
    Line::from(Span::styled(
        text.to_string(),
        Style::default().fg(style::palette::danger()),
    ))
}

/// Formats one metadata field as a bold label followed by its value.
fn field_line(label: &'static str, value: &str) -> Line<'static> {
    Line::from(vec![
        Span::styled(
            format!("{label}: "),
            Style::default()
                .fg(style::palette::text_muted())
                .add_modifier(Modifier::BOLD),
        ),
        Span::raw(value.to_string()),
    ])
}

/// Formats a section label for the issue description.
fn section_label(label: &'static str) -> Line<'static> {
    Line::from(Span::styled(
        label,
        Style::default()
            .fg(style::palette::text_muted())
            .add_modifier(Modifier::BOLD),
    ))
}

/// Formats a provider string collection or an explicit empty-state label.
fn list_text(items: &[String]) -> String {
    if items.is_empty() {
        "None".to_string()
    } else {
        items.join(", ")
    }
}

/// Returns the first 10 bytes of an ISO 8601 timestamp, or `"Unknown"` when
/// the value is absent or shorter than 10 bytes.
fn timestamp_text(timestamp: Option<&str>) -> &str {
    timestamp
        .and_then(|value| value.get(..10))
        .unwrap_or("Unknown")
}

/// Returns the inner content area of the selected issue detail frame.
fn issue_detail_content_area(area: Rect, issue: &AssignedIssue) -> Rect {
    let areas = layout::tab_page_areas(area);

    issue_detail_block(issue).inner(areas.main_area)
}

/// Builds the selected issue detail block.
fn issue_detail_block(issue: &AssignedIssue) -> Block<'static> {
    Block::default()
        .borders(Borders::ALL)
        .title(format!("Issue {} · {}", issue.display_id, issue.repository))
        .border_style(style::border_style())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_list_text_formats_empty_and_populated_values() {
        // Arrange
        let populated = vec!["bug".to_string(), "ui".to_string()];

        // Act
        let empty_text = list_text(&[]);
        let populated_text = list_text(&populated);

        // Assert
        assert_eq!(empty_text, "None");
        assert_eq!(populated_text, "bug, ui");
    }

    #[test]
    fn test_issue_detail_lines_rejects_mismatched_issue_details() {
        // Arrange
        let issue = assigned_issue();
        let mut detail = issue_detail();
        detail.display_id = "#125".to_string();
        let markdown_render_cache = markdown::MarkdownRenderCache::default();

        // Act
        let lines = issue_detail_lines(
            &issue,
            Some(&detail),
            None,
            None,
            &markdown_render_cache,
            80,
        );

        // Assert
        assert_eq!(lines.len(), 1);
        assert!(lines[0].to_string().contains("do not match"));
    }

    #[test]
    fn test_issue_detail_lines_preserves_loaded_detail_with_action_error() {
        // Arrange
        let issue = assigned_issue();
        let detail = issue_detail();
        let markdown_render_cache = markdown::MarkdownRenderCache::default();

        // Act
        let lines = issue_detail_lines(
            &issue,
            Some(&detail),
            None,
            Some("Failed to start issue session: worktree unavailable"),
            &markdown_render_cache,
            80,
        );
        let rendered_text = lines
            .iter()
            .map(ToString::to_string)
            .collect::<Vec<_>>()
            .join("\n");

        // Assert
        assert!(rendered_text.contains("Failed to start issue session: worktree unavailable"));
        assert!(rendered_text.contains("Title: Keep issue details reachable"));
        assert!(rendered_text.contains("One description line."));
    }

    #[test]
    fn test_issue_detail_lines_renders_embedded_html() {
        // Arrange
        let issue = assigned_issue();
        let mut detail = issue_detail();
        detail.body = Some(
            "<!-- hidden template --><h2>Details</h2><ul><li>Use <code>shared</code> \
             rendering.</li></ul>"
                .to_string(),
        );
        let markdown_render_cache = markdown::MarkdownRenderCache::default();

        // Act
        let lines = issue_detail_lines(
            &issue,
            Some(&detail),
            None,
            None,
            &markdown_render_cache,
            80,
        );
        let rendered_text = lines
            .iter()
            .map(ToString::to_string)
            .collect::<Vec<_>>()
            .join("\n");

        // Assert
        assert!(rendered_text.contains("Details"));
        assert!(rendered_text.contains("- Use shared rendering."));
        assert!(!rendered_text.contains("hidden template"));
        assert!(!rendered_text.contains("<h2>"));
        assert!(!rendered_text.contains("<code>"));
    }

    #[test]
    fn test_issue_detail_lines_shows_action_error_while_details_load() {
        // Arrange
        let issue = assigned_issue();
        let markdown_render_cache = markdown::MarkdownRenderCache::default();

        // Act
        let lines = issue_detail_lines(
            &issue,
            None,
            None,
            Some("Failed to start issue session: worktree unavailable"),
            &markdown_render_cache,
            80,
        );

        // Assert
        assert_eq!(lines.len(), 3);
        assert!(
            lines[0]
                .to_string()
                .contains("Failed to start issue session")
        );
        assert_eq!(lines[2].to_string(), "Loading issue details...");
    }

    #[test]
    fn test_timestamp_text_handles_missing_short_and_iso_values() {
        // Arrange
        let iso_timestamp = Some("2026-07-12T07:04:38Z");
        let short_timestamp = Some("2026-07");

        // Act
        let iso_date = timestamp_text(iso_timestamp);
        let short_date = timestamp_text(short_timestamp);
        let missing_date = timestamp_text(None);

        // Assert
        assert_eq!(iso_date, "2026-07-12");
        assert_eq!(short_date, "Unknown");
        assert_eq!(missing_date, "Unknown");
    }

    #[test]
    fn test_issue_detail_content_area_excludes_footer_and_border() {
        // Arrange
        let area = Rect::new(0, 0, 80, 20);
        let issue = assigned_issue();

        // Act
        let content_area = issue_detail_content_area(area, &issue);

        // Assert
        assert_eq!(content_area, Rect::new(2, 1, 76, 16));
    }

    #[test]
    fn test_issue_detail_max_scroll_offset_uses_inner_viewport_height() {
        // Arrange
        let area = Rect::new(0, 0, 40, 8);
        let issue = assigned_issue();
        let detail = issue_detail();
        let markdown_render_cache = markdown::MarkdownRenderCache::default();

        // Act
        let max_scroll_offset = issue_detail_max_scroll_offset(
            &issue,
            Some(&detail),
            None,
            None,
            area,
            &markdown_render_cache,
        );

        // Assert
        assert_eq!(max_scroll_offset, 7);
    }

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

    /// Builds one loaded issue detail for viewport tests.
    fn issue_detail() -> IssueDetail {
        IssueDetail {
            assignees: Vec::new(),
            author: "octocat".to_string(),
            body: Some("One description line.".to_string()),
            created_at: None,
            display_id: "#124".to_string(),
            labels: Vec::new(),
            repository: "agentty-xyz/agentty".to_string(),
            state: "OPEN".to_string(),
            title: "Keep issue details reachable".to_string(),
            updated_at: None,
            web_url: "https://github.com/agentty-xyz/agentty/issues/124".to_string(),
        }
    }
}