agd 0.1.0

Local-first CLI and stdio MCP server for org-mode files
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
use std::collections::BTreeSet;

use anyhow::Result;
use chrono::{Datelike, Duration, NaiveDate};
use org_core::{
    AgendaEngine, AgendaEntry, AgendaQuery, AgendaSpan, Item, JournalRecord, JournalStatus,
    OccurrenceType, WorkspaceIndex, WriteEngine,
};

use super::{App, DOING, DUE, DisplayRow, INBOX, SCHEDULE, SLIPPED, Screen, SelectionAnchor};

/// Rows shown under SLIPPED on Today; the rest wait in Triage.
const SLIPPED_ON_TODAY: usize = 3;
/// Agent review shows this many recent changes.
const REVIEW_LIMIT: usize = 200;

impl App {
    /// Rebuilds the visible rows and keeps the cursor on the same heading
    /// (by ID or file, line and outline path); if it left the list, the
    /// cursor stays at the same position, which is the next row.
    pub(super) fn reload_rows(&mut self) -> Result<()> {
        let previous = self.selected;
        let anchor = self
            .selected_row()
            .map(|row| SelectionAnchor::from_item(&row.item));
        self.rebuild_rows()?;
        self.selected = anchor
            .and_then(|anchor| anchor.find(&self.rows))
            .unwrap_or(previous)
            .min(self.row_count().saturating_sub(1));
        Ok(())
    }

    /// Changes screen with the cursor on the first row; esc comes back.
    pub(super) fn switch_screen(&mut self, screen: Screen) -> Result<()> {
        if matches!(screen, Screen::Triage | Screen::Review | Screen::View(_))
            && !matches!(self.screen, Screen::Triage | Screen::Review)
        {
            self.back = self.screen.clone();
        }
        self.screen = screen;
        self.rows.clear();
        self.selected = 0;
        self.marks.clear();
        self.reload_rows()
    }

    pub(super) fn selected_row(&self) -> Option<&DisplayRow> {
        self.rows.get(self.selected)
    }

    pub(super) fn row_count(&self) -> usize {
        if self.screen == Screen::Review {
            self.journals.len()
        } else {
            self.rows.len()
        }
    }

    pub(super) fn move_selection(&mut self, delta: isize) {
        let count = self.row_count();
        self.selected = if count == 0 {
            0
        } else {
            (self.selected as isize + delta).clamp(0, count as isize - 1) as usize
        };
    }

    pub(super) fn toggle_mark(&mut self) {
        let Some(item_ref) = self.selected_row().map(|row| row.item.item_ref.clone()) else {
            return;
        };
        if !self.marks.remove(&item_ref) {
            self.marks.insert(item_ref);
        }
        self.move_selection(1);
    }

    /// Marked rows when any are marked (bulk mode), else the selected row.
    pub(super) fn targets(&self) -> Vec<String> {
        if self.marks.is_empty() {
            self.selected_row()
                .map(|row| vec![row.item.item_ref.clone()])
                .unwrap_or_default()
        } else {
            self.marks.iter().cloned().collect()
        }
    }

    /// Categories in the order they first appear, for the `<` context filter.
    pub(super) fn categories(&self) -> Vec<String> {
        let mut seen = BTreeSet::new();
        self.index
            .items()
            .filter(|item| item.todo.is_some())
            .map(|item| item.category.clone())
            .filter(|category| seen.insert(category.clone()))
            .collect()
    }

    fn rebuild_rows(&mut self) -> Result<()> {
        let journals = WriteEngine::with_default_journal(self.settings.clone())?.journal()?;
        self.provenance.clear();
        for record in &journals {
            if record.diff.lines().any(|line| line.starts_with("+*"))
                && let Some(item_ref) = &record.item_ref
            {
                let actor = record
                    .actor
                    .strip_prefix("mcp:")
                    .unwrap_or("you")
                    .to_string();
                let time = record.timestamp.get(11..16).unwrap_or_default().to_string();
                self.provenance.insert(item_ref.clone(), (actor, time));
            }
        }
        self.counts.agents = journals
            .iter()
            .filter(|record| is_agent(record) && record.status == JournalStatus::Committed)
            .count();
        let engine = AgendaEngine::new(&self.index);
        let today_entries = engine.agenda(&self.query(self.today, self.today))?;
        self.counts.slipped = today_entries
            .iter()
            .filter(|entry| is_slipped(entry))
            .count();
        let inbox = engine.inbox();
        self.counts.inbox = inbox.len();

        let mut headers: Vec<String> = Vec::new();
        let mut rows = match &self.screen {
            Screen::Review => {
                self.journals = review_records(journals, self.agent_filter.as_deref());
                self.rows.clear();
                return Ok(());
            }
            Screen::Today if self.pane => {
                pane_rows(engine.agenda(&self.query(self.today, self.today))?)
            }
            Screen::Today if self.classic => {
                headers.extend(day_labels(self.day, self.day));
                self.day_rows(&engine)?
            }
            Screen::Today => today_rows(
                engine.agenda(&self.query(self.day, self.day))?,
                &engine.todo(None)?,
                &inbox,
                &self.index,
            ),
            Screen::Week => {
                let (from, to) = AgendaSpan::Week.bounds(self.day, &self.settings.week_start);
                headers.extend(day_labels(from, to));
                day_grouped(engine.agenda(&self.query(from, to))?)
            }
            Screen::Inbox => inbox
                .into_iter()
                .map(|item| DisplayRow {
                    section: INBOX.to_string(),
                    item,
                    occurrence: None,
                })
                .collect(),
            Screen::View(key) => {
                let mut rows = Vec::new();
                if let Some(view) = self.settings.view(key) {
                    for section in engine.view(view, self.day, self.today, self.log_mode)? {
                        // Agenda blocks list every day under their title, as
                        // an Emacs block agenda does; list blocks use theirs.
                        if let (Some(from), Some(to)) = (section.from, section.to) {
                            headers.push(if section.header.is_empty() {
                                agenda_block_title(from, to)
                            } else {
                                section.header.clone()
                            });
                            headers.extend(day_labels(from, to));
                            rows.extend(day_grouped(section.entries));
                        }
                        if section.from.is_none() && !section.header.is_empty() {
                            headers.push(section.header.clone());
                        }
                        rows.extend(section.items.into_iter().map(|item| DisplayRow {
                            section: section.header.clone(),
                            item,
                            occurrence: None,
                        }));
                    }
                }
                rows
            }
            Screen::Triage => {
                self.reschedules = reschedule_counts(&journals, &self.index);
                let someday = self.settings.someday_tag.clone();
                let mut rows = triage_rows(today_entries, inbox);
                // Demoted tasks are done being triaged.
                rows.retain(|row| !row.item.tags.contains(&someday));
                rows
            }
        };
        rows.retain(|row| self.visible(&row.item));
        // Sections that only exist through their rows (Today, Inbox).
        for row in &rows {
            if !headers.contains(&row.section) {
                headers.push(row.section.clone());
            }
        }
        self.headers = headers;
        self.rows = rows;
        Ok(())
    }

    fn query(&self, from: NaiveDate, to: NaiveDate) -> AgendaQuery {
        AgendaQuery {
            from,
            to,
            today: self.today,
            match_expression: None,
            log_mode: self.log_mode,
        }
    }

    /// Classic view: the plain org-agenda day list for `day`.
    fn day_rows(&self, engine: &AgendaEngine<'_>) -> Result<Vec<DisplayRow>> {
        Ok(day_grouped(engine.agenda(&self.query(self.day, self.day))?))
    }

    /// Context (`<`) and text/tag (`/`) filters.
    fn visible(&self, item: &Item) -> bool {
        let context = self
            .context
            .as_ref()
            .is_none_or(|context| item.category.eq_ignore_ascii_case(context));
        let filter = self.filter.to_lowercase();
        context
            && (filter.is_empty()
                || item.title.to_lowercase().contains(&filter)
                || item
                    .tags
                    .iter()
                    .chain(&item.inherited_tags)
                    .any(|tag| tag.to_lowercase() == filter.trim_start_matches('+')))
    }
}

/// Today's sections in the order you act on them. SLIPPED shows only the
/// first few; DOING lists in-progress TODOs without a date today.
fn today_rows(
    entries: Vec<AgendaEntry>,
    todos: &[Item],
    inbox: &[Item],
    index: &WorkspaceIndex,
) -> Vec<DisplayRow> {
    let mut sections: [(&str, Vec<DisplayRow>); 5] = [
        (SLIPPED, Vec::new()),
        (SCHEDULE, Vec::new()),
        (DUE, Vec::new()),
        (DOING, Vec::new()),
        (INBOX, Vec::new()),
    ];
    let mut shown = BTreeSet::new();
    for entry in entries {
        let slot = if is_slipped(&entry) {
            0
        } else if matches!(
            entry.occurrence.kind,
            OccurrenceType::Deadline | OccurrenceType::UpcomingDeadline
        ) {
            2
        } else {
            1
        };
        shown.insert(entry.item.item_ref.clone());
        sections[slot].1.push(DisplayRow {
            section: sections[slot].0.to_string(),
            item: entry.item,
            occurrence: Some(entry.occurrence),
        });
    }
    sections[0].1.truncate(SLIPPED_ON_TODAY);
    for item in todos {
        if !shown.contains(&item.item_ref) && is_in_progress(item, index) {
            shown.insert(item.item_ref.clone());
            sections[3].1.push(DisplayRow {
                section: DOING.to_string(),
                item: item.clone(),
                occurrence: None,
            });
        }
    }
    for item in inbox {
        if shown.insert(item.item_ref.clone()) {
            sections[4].1.push(DisplayRow {
                section: INBOX.to_string(),
                item: item.clone(),
                occurrence: None,
            });
        }
    }
    sections.into_iter().flat_map(|(_, rows)| rows).collect()
}

/// The pane: today's dated rows, timed ones by clock time, then untimed,
/// with done rows last; overdue rows are only counted.
fn pane_rows(entries: Vec<AgendaEntry>) -> Vec<DisplayRow> {
    let mut rows: Vec<DisplayRow> = entries
        .into_iter()
        .filter(|entry| !is_slipped(entry))
        .map(|entry| DisplayRow {
            section: SCHEDULE.to_string(),
            item: entry.item,
            occurrence: Some(entry.occurrence),
        })
        .collect();
    rows.sort_by_key(|row| {
        (
            row.item.done,
            row.occurrence
                .as_ref()
                .and_then(|occurrence| occurrence.time.clone())
                .is_none(),
            row.occurrence
                .as_ref()
                .and_then(|occurrence| occurrence.time.clone()),
        )
    });
    rows
}

/// Reschedules per item: Org's own "Rescheduled from" LOGBOOK notes when
/// org-log-reschedule wrote them, otherwise agd journal entries that moved
/// an existing SCHEDULED date.
fn reschedule_counts(
    journals: &[JournalRecord],
    index: &WorkspaceIndex,
) -> std::collections::BTreeMap<String, usize> {
    let mut counts = std::collections::BTreeMap::new();
    for record in journals {
        let moved = record
            .diff
            .lines()
            .any(|line| line.starts_with('-') && line.contains("SCHEDULED:"))
            && record
                .diff
                .lines()
                .any(|line| line.starts_with('+') && line.contains("SCHEDULED:"));
        if moved && let Some(item_ref) = &record.item_ref {
            *counts.entry(item_ref.clone()).or_default() += 1;
        }
    }
    for document in index.documents() {
        for headline in document.headlines() {
            let section =
                &document.source()[headline.section_span.start..headline.section_span.end];
            let logged = section.matches("- Rescheduled from").count();
            if logged > 0 {
                counts.insert(headline.item.item_ref.clone(), logged);
            }
        }
    }
    counts
}

/// org-agenda's day header: `Wednesday  14 October 2026`.
pub(super) fn day_label(date: NaiveDate) -> String {
    format!(
        "{:<10} {:>2} {} {}",
        date.format("%A"),
        date.day(),
        date.format("%B"),
        date.year()
    )
}

/// Emacs's title for an agenda block without an overriding header.
fn agenda_block_title(from: NaiveDate, to: NaiveDate) -> String {
    let days = (to - from).num_days() + 1;
    let span = match days {
        1 => "Day".to_string(),
        7 => "Week".to_string(),
        days => format!("{days}-day"),
    };
    format!("{span}-agenda (W{:02}):", from.iso_week().week())
}

fn day_labels(from: NaiveDate, to: NaiveDate) -> Vec<String> {
    let mut labels = Vec::new();
    let mut day = from;
    while day <= to {
        labels.push(day_label(day));
        day += Duration::days(1);
    }
    labels
}

/// org-agenda rows grouped under one header per day, as in Emacs.
fn day_grouped(entries: Vec<AgendaEntry>) -> Vec<DisplayRow> {
    entries
        .into_iter()
        .map(|entry| {
            let section = entry
                .occurrence
                .date
                .parse::<NaiveDate>()
                .map(day_label)
                .unwrap_or_else(|_| entry.occurrence.date.clone());
            DisplayRow {
                section,
                item: entry.item,
                occurrence: Some(entry.occurrence),
            }
        })
        .collect()
}

/// Overdue items first (most days late first), then the inbox.
fn triage_rows(entries: Vec<AgendaEntry>, inbox: Vec<Item>) -> Vec<DisplayRow> {
    let mut seen = BTreeSet::new();
    let mut rows: Vec<DisplayRow> = entries
        .into_iter()
        .filter(|entry| is_slipped(entry) && !entry.item.done)
        .filter(|entry| seen.insert(entry.item.item_ref.clone()))
        .map(|entry| DisplayRow {
            section: SLIPPED.to_string(),
            item: entry.item,
            occurrence: Some(entry.occurrence),
        })
        .collect();
    rows.extend(
        inbox
            .into_iter()
            .filter(|item| seen.insert(item.item_ref.clone()))
            .map(|item| DisplayRow {
                section: INBOX.to_string(),
                item,
                occurrence: None,
            }),
    );
    rows
}

/// Past-scheduled reminders and overdue deadlines.
pub(super) fn is_slipped(entry: &AgendaEntry) -> bool {
    match entry.occurrence.kind {
        OccurrenceType::PastScheduled => true,
        OccurrenceType::Deadline => entry.occurrence.extra.ends_with("ago:"),
        _ => false,
    }
}

/// A TODO in a middle state of its sequence (NEXT, INPROGRESS, WAIT…):
/// not the first keyword and not done.
fn is_in_progress(item: &Item, index: &WorkspaceIndex) -> bool {
    let Some(todo) = item.todo.as_deref() else {
        return false;
    };
    let Some(document) = index.document(std::path::Path::new(&item.file)) else {
        return false;
    };
    let Some(keyword) = document.todo_keyword(todo) else {
        return false;
    };
    let head = document
        .todo_keywords()
        .iter()
        .find(|candidate| candidate.sequence == keyword.sequence);
    !item.done && head.is_some_and(|head| head.name != todo)
}

fn is_agent(record: &JournalRecord) -> bool {
    record.actor.starts_with("mcp:")
}

/// Recent agent changes, unreviewed first, newest first within each group.
fn review_records(journals: Vec<JournalRecord>, agent: Option<&str>) -> Vec<JournalRecord> {
    let mut records: Vec<JournalRecord> = journals
        .into_iter()
        .filter(|record| {
            is_agent(record)
                && matches!(
                    record.status,
                    JournalStatus::Committed | JournalStatus::Reviewed
                )
                && agent.is_none_or(|agent| record.actor.strip_prefix("mcp:") == Some(agent))
        })
        .collect();
    records.sort_by(|left, right| {
        (left.status != JournalStatus::Committed)
            .cmp(&(right.status != JournalStatus::Committed))
            .then_with(|| right.timestamp.cmp(&left.timestamp))
    });
    records.truncate(REVIEW_LIMIT);
    records
}

/// Days since a date, for "waiting 3d" and triage ages.
pub(super) fn days_since(date: &str, today: NaiveDate) -> Option<i64> {
    let date = date.get(..10)?.parse::<NaiveDate>().ok()?;
    Some((today - date).num_days().max(0))
}

/// Next occurrence of a repeating SCHEDULED/DEADLINE after marking it DONE,
/// as org-auto-repeat-maybe computes it, for the peek preview.
pub(super) fn next_repeat(date: &str, repeater: &str, today: NaiveDate) -> Option<NaiveDate> {
    let base = date.parse::<NaiveDate>().ok()?;
    let (kind, value) = if let Some(value) = repeater.strip_prefix("++") {
        ("++", value)
    } else if let Some(value) = repeater.strip_prefix(".+") {
        (".+", value)
    } else {
        ("+", repeater.strip_prefix('+')?)
    };
    let value = value.split('/').next()?;
    let unit = value.chars().last()?;
    let amount: i64 = value[..value.len() - 1].parse().ok()?;
    let step = |date: NaiveDate| -> Option<NaiveDate> {
        match unit {
            'd' => Some(date + Duration::days(amount)),
            'w' => Some(date + Duration::weeks(amount)),
            'm' => date.checked_add_months(chrono::Months::new(amount as u32)),
            'y' => date.checked_add_months(chrono::Months::new(12 * amount as u32)),
            _ => None,
        }
    };
    match kind {
        ".+" => step(today),
        "++" => {
            let mut next = step(base)?;
            while next <= today {
                next = step(next)?;
            }
            Some(next)
        }
        _ => step(base),
    }
}