agentty 0.8.10

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
use ratatui::Frame;
use ratatui::layout::{Constraint, Layout, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Cell, Paragraph, Row, Table, TableState, Wrap};
use time::OffsetDateTime;

use crate::domain::project::ProjectListItem;
use crate::ui::state::help_action;
use crate::ui::{Page, layout, style};

/// Uses row-background highlighting without a textual cursor glyph.
const ROW_HIGHLIGHT_SYMBOL: &str = "";
const ACTIVE_PROJECT_MARKER: &str = "* ";
/// Horizontal spacing between project-table columns.
const TABLE_COLUMN_SPACING: u16 = 2;
/// Fixed height reserved for the top Agentty info panel.
const AGENTTY_INFO_PANEL_HEIGHT: u16 = 9;
/// Percentage of the top-panel width reserved for the ASCII logo.
const AGENTTY_INFO_ASCII_WIDTH_PERCENT: u16 = 58;
/// Number of lines in the Agentty ASCII art banner.
const AGENTTY_ASCII_ART_LINE_COUNT: u16 = 5;
/// Maximum visible width of the Agentty ASCII art banner.
const AGENTTY_ASCII_ART_WIDTH: u16 = 45;
/// Compile-time version text shown in the projects info panel.
const AGENTTY_VERSION: &str = concat!("v", env!("CARGO_PKG_VERSION"));
/// Short overview text shown alongside the Agentty version.
const AGENTTY_SHORT_DESCRIPTION: &str = "Agentty is an ADE (Agentic Development Environment) for \
                                         structured, controllable AI-assisted software \
                                         development.";
/// ASCII logo shown in the projects info panel.
const AGENTTY_ASCII_ART: &str = r"    _    ____ _____ _   _ _____ _____ __   __
   / \  / ___| ____| \ | |_   _|_   _|\ \ / /
  / _ \| |  _|  _| |  \| | | |   | |   \ V /
 / ___ \ |_| | |___| |\  | | |   | |    | |
/_/   \_\____|_____|_| \_| |_|   |_|    |_|";

/// Projects tab renderer showing saved repositories and quick metadata.
pub struct ProjectListPage<'a> {
    /// Identifier for the currently active project.
    pub active_project_id: i64,
    /// Project rows displayed in the table.
    pub projects: &'a [ProjectListItem],
    /// Stateful cursor position for the project table.
    pub table_state: &'a mut TableState,
}

impl<'a> ProjectListPage<'a> {
    /// Creates a project-list page renderer with active-project highlighting.
    pub fn new(
        projects: &'a [ProjectListItem],
        table_state: &'a mut TableState,
        active_project_id: i64,
    ) -> Self {
        Self {
            active_project_id,
            projects,
            table_state,
        }
    }
}

impl Page for ProjectListPage<'_> {
    fn render(&mut self, f: &mut Frame, area: Rect) {
        let chunks = Layout::default()
            .constraints([Constraint::Min(0), Constraint::Length(1)])
            .margin(1)
            .split(area);

        let main_area = chunks[0];
        let footer_area = chunks[1];
        let content_chunks = Layout::vertical([
            Constraint::Length(AGENTTY_INFO_PANEL_HEIGHT),
            Constraint::Min(0),
        ])
        .split(main_area);
        let info_area = content_chunks[0];
        let project_area = content_chunks[1];
        let info_panel_block = Block::default()
            .borders(Borders::ALL)
            .title("Agentty")
            .border_style(style::border_style());
        let info_panel_inner_area = info_panel_block.inner(info_area);
        let info_panel_chunks = Layout::horizontal([
            Constraint::Percentage(AGENTTY_INFO_ASCII_WIDTH_PERCENT),
            Constraint::Percentage(100 - AGENTTY_INFO_ASCII_WIDTH_PERCENT),
        ])
        .split(info_panel_inner_area);
        let logo_area = info_panel_chunks[0];
        let details_area = info_panel_chunks[1];
        let centered_logo_area = layout::centered_content_rect(
            logo_area,
            AGENTTY_ASCII_ART_WIDTH,
            AGENTTY_ASCII_ART_LINE_COUNT,
        );
        let logo_panel = Paragraph::new(AGENTTY_ASCII_ART)
            .style(Style::default().fg(style::palette::text()))
            .wrap(Wrap { trim: false });
        let details_panel = Paragraph::new(agentty_info_details_text())
            .style(Style::default().fg(style::palette::text()))
            .wrap(Wrap { trim: true });

        let selected_style = Style::default().bg(style::palette::surface());
        let header = Row::new(["Project", "Branch", "Sessions", "Last Opened", "Path"])
            .style(
                Style::default()
                    .bg(style::palette::surface())
                    .fg(style::palette::text_muted())
                    .add_modifier(Modifier::BOLD),
            )
            .height(1)
            .bottom_margin(1);
        let active_project_id = self.active_project_id;
        let rows = self
            .projects
            .iter()
            .map(|project_item| render_project_row(project_item, active_project_id));
        let table = Table::new(
            rows,
            [
                Constraint::Length(20),
                Constraint::Length(12),
                Constraint::Length(8),
                Constraint::Length(12),
                Constraint::Fill(1),
            ],
        )
        .column_spacing(TABLE_COLUMN_SPACING)
        .header(header)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .title("Projects")
                .border_style(style::border_style()),
        )
        .row_highlight_style(selected_style)
        .highlight_symbol(ROW_HIGHLIGHT_SYMBOL);

        f.render_stateful_widget(table, project_area, self.table_state);
        f.render_widget(info_panel_block, info_area);
        f.render_widget(logo_panel, centered_logo_area);
        f.render_widget(details_panel, details_area);

        let help_message = Paragraph::new(project_list_footer_line());
        f.render_widget(help_message, footer_area);
    }
}

/// Renders one project metadata row.
fn render_project_row(project_item: &ProjectListItem, active_project_id: i64) -> Row<'static> {
    let (title, branch, last_opened, path) = project_row_values(project_item, active_project_id);

    Row::new(vec![
        Cell::from(title),
        Cell::from(branch),
        Cell::from(session_count_line(
            project_item.session_count,
            project_item.active_session_count,
        )),
        Cell::from(last_opened),
        Cell::from(path),
    ])
    .style(project_row_style(project_item, active_project_id))
}

/// Builds top-panel Agentty metadata text shown to the right of the logo.
fn agentty_info_details_text() -> String {
    format!(
        "Version: {AGENTTY_VERSION}\n\n{AGENTTY_SHORT_DESCRIPTION}\n\nDocs: https://agentty.xyz/docs"
    )
}

/// Returns the footer help content rendered below the projects table.
fn project_list_footer_line() -> Line<'static> {
    help_action::footer_line(&help_action::project_list_footer_actions())
}

/// Returns project row display values for reuse and testing.
fn project_row_values(
    project_item: &ProjectListItem,
    active_project_id: i64,
) -> (String, String, String, String) {
    let project = &project_item.project;
    let title = project_title(project_item, active_project_id);
    let branch = project.git_branch.as_deref().unwrap_or("-");
    let last_opened = format_last_opened(project.last_opened_at);
    let path = project.path.to_string_lossy().to_string();

    (title, branch.to_string(), last_opened, path)
}

/// Returns style for one project row, emphasizing the active project.
fn project_row_style(project_item: &ProjectListItem, active_project_id: i64) -> Style {
    if project_item.project.id == active_project_id {
        return Style::default().fg(style::palette::accent_soft());
    }

    Style::default().fg(style::palette::text())
}

/// Returns the visible project title, marking the active project in the list.
fn project_title(project_item: &ProjectListItem, active_project_id: i64) -> String {
    let display_label = project_item.project.display_label();
    if project_item.project.id == active_project_id {
        return format!("{ACTIVE_PROJECT_MARKER}{display_label}");
    }

    display_label
}

/// Builds a styled line for the session count column, coloring the active
/// indicator in yellow when active sessions exist.
fn session_count_line(total: u32, active: u32) -> Line<'static> {
    if active > 0 {
        return Line::from(vec![
            Span::raw(format!("{total} ")),
            Span::styled(
                format!("â–¶ {active}"),
                Style::default().fg(style::palette::warning()),
            ),
        ]);
    }

    Line::from(total.to_string())
}

/// Formats the project last-opened timestamp for table display.
fn format_last_opened(last_opened_at: Option<i64>) -> String {
    let Some(last_opened_at) = last_opened_at else {
        return "Never".to_string();
    };
    let Ok(last_opened_datetime) = OffsetDateTime::from_unix_timestamp(last_opened_at) else {
        return "Unknown".to_string();
    };

    let year = last_opened_datetime.year();
    let month = u8::from(last_opened_datetime.month());
    let day = last_opened_datetime.day();

    format!("{year:04}-{month:02}-{day:02}")
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use super::*;
    use crate::domain::project::Project;
    use crate::domain::theme::ColorTheme;

    #[test]
    fn test_row_highlight_symbol_uses_background_only_selection() {
        // Arrange
        let highlight_symbol = ROW_HIGHLIGHT_SYMBOL;

        // Act
        let is_empty_symbol = highlight_symbol.is_empty();

        // Assert
        assert!(is_empty_symbol);
    }

    #[test]
    fn test_project_table_column_spacing_is_wider_for_readability() {
        // Arrange
        let expected_spacing = 2;

        // Act
        let spacing = TABLE_COLUMN_SPACING;

        // Assert
        assert_eq!(spacing, expected_spacing);
    }

    #[test]
    fn test_render_uses_palette_border_for_projects_table() {
        // Arrange
        let _theme_scope = style::scoped_active_theme(ColorTheme::Current);
        let projects = vec![ProjectListItem {
            active_session_count: 0,
            last_session_updated_at: None,
            project: Project {
                created_at: 1,
                display_name: Some("agentty".to_string()),
                git_branch: Some("main".to_string()),
                id: 42,
                is_favorite: false,
                last_opened_at: None,
                path: PathBuf::from("/tmp/agentty"),
                updated_at: 2,
            },
            session_count: 0,
        }];
        let mut table_state = TableState::default();
        table_state.select(Some(0));
        let backend = ratatui::backend::TestBackend::new(100, 30);
        let mut terminal = ratatui::Terminal::new(backend).expect("failed to create terminal");

        // Act
        terminal
            .draw(|frame| {
                ProjectListPage::new(&projects, &mut table_state, 42).render(frame, frame.area());
            })
            .expect("failed to draw projects page");

        // Assert
        let border_cell_count = foreground_symbol_cell_count(terminal.backend().buffer(), "┌");
        assert!(
            border_cell_count >= 2,
            "expected both project page panels to use palette border color"
        );
    }

    #[test]
    fn test_format_last_opened_uses_iso_like_date() {
        // Arrange
        let last_opened_at = Some(1_700_000_000);

        // Act
        let formatted = format_last_opened(last_opened_at);

        // Assert
        assert_eq!(formatted, "2023-11-14");
    }

    #[test]
    fn test_format_last_opened_returns_never_without_timestamp() {
        // Arrange
        let last_opened_at = None;

        // Act
        let formatted = format_last_opened(last_opened_at);

        // Assert
        assert_eq!(formatted, "Never");
    }

    #[test]
    fn test_format_last_opened_returns_unknown_for_invalid_timestamp() {
        // Arrange
        let last_opened_at = Some(i64::MAX);

        // Act
        let formatted = format_last_opened(last_opened_at);

        // Assert
        assert_eq!(formatted, "Unknown");
    }

    #[test]
    fn test_project_row_values_show_metadata() {
        // Arrange
        let project_item = ProjectListItem {
            active_session_count: 0,
            last_session_updated_at: Some(20),
            project: Project {
                created_at: 1,
                display_name: Some("agentty".to_string()),
                git_branch: Some("main".to_string()),
                id: 1,
                is_favorite: true,
                last_opened_at: Some(1_700_000_000),
                path: PathBuf::from("/tmp/agentty"),
                updated_at: 2,
            },
            session_count: 3,
        };

        // Act
        let values = project_row_values(&project_item, 99);

        // Assert
        assert_eq!(values.0, "agentty");
        assert_eq!(values.1, "main");
        assert_eq!(values.2, "2023-11-14");
        assert_eq!(values.3, "/tmp/agentty");
    }

    #[test]
    fn test_project_row_values_use_fallbacks_for_missing_branch_and_timestamp() {
        // Arrange
        let project_item = ProjectListItem {
            active_session_count: 0,
            last_session_updated_at: None,
            project: Project {
                created_at: 1,
                display_name: None,
                git_branch: None,
                id: 1,
                is_favorite: false,
                last_opened_at: None,
                path: PathBuf::from("/tmp/agentty"),
                updated_at: 2,
            },
            session_count: 0,
        };

        // Act
        let values = project_row_values(&project_item, 99);

        // Assert
        assert_eq!(values.0, "agentty");
        assert_eq!(values.1, "-");
        assert_eq!(values.2, "Never");
    }

    #[test]
    fn test_session_count_line_shows_plain_total_without_active() {
        // Arrange & Act
        let line = session_count_line(7, 0);

        // Assert
        assert_eq!(line.to_string(), "7");
        assert_eq!(line.spans.len(), 1);
    }

    #[test]
    fn test_session_count_line_colors_active_indicator_yellow() {
        // Arrange & Act
        let line = session_count_line(5, 2);

        // Assert
        assert_eq!(line.spans.len(), 2);
        assert_eq!(line.spans[0].content.as_ref(), "5 ");
        assert_eq!(line.spans[1].content.as_ref(), "â–¶ 2");
        assert_eq!(line.spans[1].style.fg, Some(style::palette::warning()));
    }

    #[test]
    fn test_project_row_values_mark_active_project_title() {
        // Arrange
        let project_item = ProjectListItem {
            active_session_count: 0,
            last_session_updated_at: Some(20),
            project: Project {
                created_at: 1,
                display_name: Some("agentty".to_string()),
                git_branch: Some("main".to_string()),
                id: 42,
                is_favorite: true,
                last_opened_at: Some(1_700_000_000),
                path: PathBuf::from("/tmp/agentty"),
                updated_at: 2,
            },
            session_count: 3,
        };

        // Act
        let values = project_row_values(&project_item, 42);

        // Assert
        assert_eq!(values.0, "* agentty");
    }

    #[test]
    fn test_project_row_style_uses_accent_for_active_project() {
        // Arrange
        let project_item = ProjectListItem {
            active_session_count: 0,
            last_session_updated_at: None,
            project: Project {
                created_at: 1,
                display_name: Some("agentty".to_string()),
                git_branch: Some("main".to_string()),
                id: 42,
                is_favorite: false,
                last_opened_at: None,
                path: PathBuf::from("/tmp/agentty"),
                updated_at: 2,
            },
            session_count: 0,
        };

        // Act
        let style = project_row_style(&project_item, 42);

        // Assert
        assert_eq!(style.fg, Some(style::palette::accent_soft()));
    }

    #[test]
    fn test_project_row_style_uses_text_color_for_inactive_project() {
        // Arrange
        let project_item = ProjectListItem {
            active_session_count: 0,
            last_session_updated_at: None,
            project: Project {
                created_at: 1,
                display_name: Some("agentty".to_string()),
                git_branch: Some("main".to_string()),
                id: 42,
                is_favorite: false,
                last_opened_at: None,
                path: PathBuf::from("/tmp/agentty"),
                updated_at: 2,
            },
            session_count: 0,
        };

        // Act
        let style = project_row_style(&project_item, 7);

        // Assert
        assert_eq!(style.fg, Some(style::palette::text()));
    }

    #[test]
    fn test_project_list_footer_line_matches_project_shortcuts() {
        // Arrange
        let expected_line = help_action::footer_line(&help_action::project_list_footer_actions());

        // Act
        let footer_line = project_list_footer_line();

        // Assert
        assert_eq!(footer_line, expected_line);
    }

    #[test]
    fn test_agentty_info_details_text_includes_version_and_description() {
        // Arrange
        let expected_version = AGENTTY_VERSION;
        let expected_description = AGENTTY_SHORT_DESCRIPTION;

        // Act
        let info_text = agentty_info_details_text();

        // Assert
        assert!(info_text.contains(expected_version));
        assert!(info_text.contains(expected_description));
    }

    #[test]
    fn test_agentty_ascii_art_banner_matches_reference_header() {
        // Arrange
        let expected_banner_header = "    _    ____ _____ _   _ _____ _____ __   __";

        // Assert
        assert!(AGENTTY_ASCII_ART.starts_with(expected_banner_header));
    }

    #[test]
    fn test_agentty_ascii_art_line_count_matches_banner() {
        // Arrange & Act
        let actual_line_count = AGENTTY_ASCII_ART.lines().count();

        // Assert
        assert_eq!(actual_line_count, usize::from(AGENTTY_ASCII_ART_LINE_COUNT));
    }

    #[test]
    fn test_agentty_ascii_art_width_matches_banner() {
        // Arrange & Act
        let actual_width = AGENTTY_ASCII_ART
            .lines()
            .map(str::len)
            .max()
            .unwrap_or_default();

        // Assert
        assert_eq!(actual_width, usize::from(AGENTTY_ASCII_ART_WIDTH));
    }

    /// Counts cells matching a rendered symbol and the active palette border
    /// color.
    fn foreground_symbol_cell_count(buffer: &ratatui::buffer::Buffer, symbol: &str) -> usize {
        buffer
            .content()
            .iter()
            .filter(|cell| cell.symbol() == symbol && cell.fg == style::palette::border())
            .count()
    }
}