summaryrefslogtreecommitdiff
path: root/makima/src/daemon/task/progress_log.rs
blob: 394a055f896728b2e65c0b12f5b6524e1288628b (plain) (blame)
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
//! Structured progress logging for task execution.
//!
//! This module provides an append-only progress log file system that persists
//! learnings, patterns, and context across task iterations. The log is stored
//! in the task's worktree directory as `progress.log`.
//!
//! Format:
//! ```markdown
//! # progress.log
//! # Auto-generated by Makima - DO NOT EDIT MANUALLY
//!
//! ## [2024-01-15T10:30:00Z] - Task [fc09b908-...]: Implement user authentication
//! - Status: done
//! - Files changed: src/auth.rs, src/lib.rs
//! - **Learnings:**
//!   - Pattern: Use bcrypt for password hashing
//!   - Gotcha: Need to handle expired tokens gracefully
//! ---
//! ```

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::fs::{self, OpenOptions};
use std::io::{self, BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
use uuid::Uuid;

/// Status of a completed task entry.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ProgressEntryStatus {
    /// Task completed successfully.
    Done,
    /// Task failed.
    Failed,
}

impl std::fmt::Display for ProgressEntryStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ProgressEntryStatus::Done => write!(f, "done"),
            ProgressEntryStatus::Failed => write!(f, "failed"),
        }
    }
}

impl std::str::FromStr for ProgressEntryStatus {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "done" | "completed" => Ok(ProgressEntryStatus::Done),
            "failed" | "error" => Ok(ProgressEntryStatus::Failed),
            _ => Err(format!("Unknown status: {}", s)),
        }
    }
}

/// A single progress log entry representing a completed task iteration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProgressEntry {
    /// Timestamp when the entry was created.
    pub timestamp: DateTime<Utc>,
    /// Task ID.
    pub task_id: Uuid,
    /// Human-readable task name.
    pub task_name: String,
    /// Completion status (done/failed).
    pub status: ProgressEntryStatus,
    /// List of files that were changed.
    pub files_changed: Vec<String>,
    /// Learnings discovered during task execution.
    pub learnings: Vec<String>,
}

impl ProgressEntry {
    /// Create a new progress entry.
    pub fn new(
        task_id: Uuid,
        task_name: String,
        status: ProgressEntryStatus,
        files_changed: Vec<String>,
        learnings: Vec<String>,
    ) -> Self {
        Self {
            timestamp: Utc::now(),
            task_id,
            task_name,
            status,
            files_changed,
            learnings,
        }
    }

    /// Format the entry as human-readable markdown.
    pub fn to_markdown(&self) -> String {
        let mut output = String::new();

        // Header with timestamp, task ID, and name
        output.push_str(&format!(
            "## [{}] - Task [{}]: {}\n",
            self.timestamp.format("%Y-%m-%dT%H:%M:%SZ"),
            &self.task_id.to_string()[..8], // Short task ID
            self.task_name
        ));

        // Status
        output.push_str(&format!("- Status: {}\n", self.status));

        // Files changed
        if self.files_changed.is_empty() {
            output.push_str("- Files changed: (none)\n");
        } else {
            output.push_str(&format!(
                "- Files changed: {}\n",
                self.files_changed.join(", ")
            ));
        }

        // Learnings
        output.push_str("- **Learnings:**\n");
        if self.learnings.is_empty() {
            output.push_str("  - (none recorded)\n");
        } else {
            for learning in &self.learnings {
                output.push_str(&format!("  - {}\n", learning));
            }
        }

        // Separator
        output.push_str("---\n");

        output
    }

    /// Parse a progress entry from markdown text.
    ///
    /// Returns None if the text doesn't contain a valid progress entry.
    pub fn from_markdown(text: &str) -> Option<Self> {
        let lines: Vec<&str> = text.lines().collect();

        // Parse header: ## [timestamp] - Task [id]: name
        let header = lines.first()?;
        if !header.starts_with("## [") {
            return None;
        }

        // Extract timestamp
        let timestamp_end = header.find(']')?;
        let timestamp_str = &header[4..timestamp_end];
        let timestamp = DateTime::parse_from_rfc3339(timestamp_str)
            .ok()?
            .with_timezone(&Utc);

        // Extract task ID
        let task_start = header.find("Task [")? + 6;
        let task_end = header[task_start..].find(']')? + task_start;
        let task_id_short = &header[task_start..task_end];

        // Try to parse as UUID (we stored short version, need to handle that)
        // For now, we'll generate a placeholder UUID if we can't parse the short version
        let task_id = Uuid::parse_str(task_id_short)
            .or_else(|_| {
                // Try to parse with padding (short IDs are just first 8 chars)
                Uuid::parse_str(&format!("{}-0000-0000-0000-000000000000", task_id_short))
            })
            .ok()?;

        // Extract task name
        let name_start = header.find("]: ")? + 3;
        let task_name = header[name_start..].to_string();

        // Parse remaining fields
        let mut status = ProgressEntryStatus::Done;
        let mut files_changed = Vec::new();
        let mut learnings = Vec::new();
        let mut in_learnings = false;

        for line in &lines[1..] {
            let line = line.trim();

            if line.starts_with("- Status:") {
                let status_str = line.trim_start_matches("- Status:").trim();
                status = status_str.parse().unwrap_or(ProgressEntryStatus::Done);
                in_learnings = false;
            } else if line.starts_with("- Files changed:") {
                let files_str = line.trim_start_matches("- Files changed:").trim();
                if files_str != "(none)" {
                    files_changed = files_str
                        .split(',')
                        .map(|s| s.trim().to_string())
                        .filter(|s| !s.is_empty())
                        .collect();
                }
                in_learnings = false;
            } else if line.starts_with("- **Learnings:**") {
                in_learnings = true;
            } else if in_learnings && line.starts_with("- ") {
                let learning = line.trim_start_matches("- ").trim();
                if learning != "(none recorded)" {
                    learnings.push(learning.to_string());
                }
            } else if line == "---" {
                break;
            }
        }

        Some(ProgressEntry {
            timestamp,
            task_id,
            task_name,
            status,
            files_changed,
            learnings,
        })
    }
}

/// Progress log manager for a task's worktree.
pub struct ProgressLog {
    /// Path to the progress.log file.
    log_path: PathBuf,
}

/// Default file name for the progress log.
pub const PROGRESS_LOG_FILENAME: &str = "progress.log";

/// Default maximum number of entries to inject into prompts.
pub const DEFAULT_MAX_ENTRIES_INJECTED: usize = 20;

impl ProgressLog {
    /// Create a new ProgressLog for a worktree directory.
    pub fn new(worktree_path: &Path) -> Self {
        Self {
            log_path: Self::get_log_path(worktree_path),
        }
    }

    /// Get the path to the progress log file for a worktree.
    pub fn get_log_path(worktree_path: &Path) -> PathBuf {
        worktree_path.join(PROGRESS_LOG_FILENAME)
    }

    /// Get the path to this progress log file.
    pub fn path(&self) -> &Path {
        &self.log_path
    }

    /// Check if the progress log file exists.
    pub fn exists(&self) -> bool {
        self.log_path.exists()
    }

    /// Append a progress entry to the log file.
    ///
    /// Creates the file with a header if it doesn't exist.
    pub fn append_entry(&self, entry: &ProgressEntry) -> io::Result<()> {
        let file_exists = self.log_path.exists();

        let mut file = OpenOptions::new()
            .create(true)
            .append(true)
            .open(&self.log_path)?;

        // Write header if this is a new file
        if !file_exists {
            writeln!(file, "# progress.log")?;
            writeln!(file, "# Auto-generated by Makima - DO NOT EDIT MANUALLY")?;
            writeln!(file)?;
        }

        // Append the entry
        write!(file, "{}", entry.to_markdown())?;
        writeln!(file)?;

        Ok(())
    }

    /// Read all entries from the progress log.
    pub fn read_all_entries(&self) -> io::Result<Vec<ProgressEntry>> {
        if !self.log_path.exists() {
            return Ok(Vec::new());
        }

        let file = fs::File::open(&self.log_path)?;
        let reader = BufReader::new(file);

        let mut entries = Vec::new();
        let mut current_entry = String::new();

        for line in reader.lines() {
            let line = line?;

            // Start of a new entry
            if line.starts_with("## [") {
                // Process previous entry if any
                if !current_entry.is_empty() {
                    if let Some(entry) = ProgressEntry::from_markdown(&current_entry) {
                        entries.push(entry);
                    }
                    current_entry.clear();
                }
            }

            // Skip header lines
            if line.starts_with("# ") || line.is_empty() {
                continue;
            }

            current_entry.push_str(&line);
            current_entry.push('\n');
        }

        // Process last entry
        if !current_entry.is_empty() {
            if let Some(entry) = ProgressEntry::from_markdown(&current_entry) {
                entries.push(entry);
            }
        }

        Ok(entries)
    }

    /// Read the most recent entries from the progress log.
    ///
    /// Returns up to `max` entries, starting from the most recent.
    pub fn read_recent_entries(&self, max: usize) -> io::Result<Vec<ProgressEntry>> {
        let mut entries = self.read_all_entries()?;

        // Return the last `max` entries (most recent)
        if entries.len() > max {
            entries = entries.split_off(entries.len() - max);
        }

        Ok(entries)
    }

    /// Format recent entries for injection into a prompt.
    ///
    /// Returns a formatted string containing the recent progress entries
    /// suitable for including in a Claude prompt.
    pub fn format_for_prompt(&self, max_entries: usize) -> io::Result<String> {
        let entries = self.read_recent_entries(max_entries)?;

        if entries.is_empty() {
            return Ok(String::new());
        }

        let mut output = String::new();
        output.push_str("## Previous Task Progress\n\n");
        output.push_str("The following entries show recent task completions and learnings:\n\n");

        for entry in &entries {
            output.push_str(&entry.to_markdown());
            output.push('\n');
        }

        Ok(output)
    }

    /// Get the total number of entries in the log.
    pub fn entry_count(&self) -> io::Result<usize> {
        Ok(self.read_all_entries()?.len())
    }
}

/// Helper function to create a progress entry and append it to a log.
///
/// This is a convenience function for the common case of appending a single entry.
pub fn append_progress_entry(
    worktree_path: &Path,
    task_id: Uuid,
    task_name: String,
    status: ProgressEntryStatus,
    files_changed: Vec<String>,
    learnings: Vec<String>,
) -> io::Result<()> {
    let log = ProgressLog::new(worktree_path);
    let entry = ProgressEntry::new(task_id, task_name, status, files_changed, learnings);
    log.append_entry(&entry)
}

/// Get the list of files changed in a git worktree.
///
/// This runs `git diff --name-only HEAD~1` to get files changed in the last commit,
/// falling back to `git diff --name-only` for uncommitted changes if no commits exist.
pub async fn get_git_changed_files(worktree_path: &Path) -> Vec<String> {
    // Try to get files from the last commit first
    let output = tokio::process::Command::new("git")
        .args(["diff", "--name-only", "HEAD~1"])
        .current_dir(worktree_path)
        .output()
        .await;

    if let Ok(output) = output {
        if output.status.success() {
            let files: Vec<String> = String::from_utf8_lossy(&output.stdout)
                .lines()
                .filter(|s| !s.is_empty())
                .map(String::from)
                .collect();
            if !files.is_empty() {
                return files;
            }
        }
    }

    // Fall back to uncommitted changes
    let output = tokio::process::Command::new("git")
        .args(["diff", "--name-only", "HEAD"])
        .current_dir(worktree_path)
        .output()
        .await;

    if let Ok(output) = output {
        if output.status.success() {
            return String::from_utf8_lossy(&output.stdout)
                .lines()
                .filter(|s| !s.is_empty())
                .map(String::from)
                .collect();
        }
    }

    // Try to get all tracked files that have been modified
    let output = tokio::process::Command::new("git")
        .args(["status", "--porcelain"])
        .current_dir(worktree_path)
        .output()
        .await;

    if let Ok(output) = output {
        if output.status.success() {
            return String::from_utf8_lossy(&output.stdout)
                .lines()
                .filter(|s| !s.is_empty())
                .filter_map(|line| {
                    // Format: "XY filename" where XY is the status
                    if line.len() > 3 {
                        Some(line[3..].to_string())
                    } else {
                        None
                    }
                })
                .collect();
        }
    }

    Vec::new()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::tempdir;

    #[test]
    fn test_progress_entry_to_markdown() {
        let entry = ProgressEntry {
            timestamp: DateTime::parse_from_rfc3339("2024-01-15T10:30:00Z")
                .unwrap()
                .with_timezone(&Utc),
            task_id: Uuid::parse_str("fc09b908-1234-5678-abcd-ef1234567890").unwrap(),
            task_name: "Implement user authentication".to_string(),
            status: ProgressEntryStatus::Done,
            files_changed: vec!["src/auth.rs".to_string(), "src/lib.rs".to_string()],
            learnings: vec![
                "Pattern: Use bcrypt for password hashing".to_string(),
                "Gotcha: Need to handle expired tokens gracefully".to_string(),
            ],
        };

        let markdown = entry.to_markdown();
        assert!(markdown.contains("## [2024-01-15T10:30:00Z]"));
        assert!(markdown.contains("Task [fc09b908]"));
        assert!(markdown.contains("Implement user authentication"));
        assert!(markdown.contains("Status: done"));
        assert!(markdown.contains("src/auth.rs, src/lib.rs"));
        assert!(markdown.contains("Use bcrypt for password hashing"));
        assert!(markdown.contains("---"));
    }

    #[test]
    fn test_progress_entry_roundtrip() {
        let entry = ProgressEntry {
            timestamp: DateTime::parse_from_rfc3339("2024-01-15T10:30:00Z")
                .unwrap()
                .with_timezone(&Utc),
            task_id: Uuid::parse_str("fc09b908-0000-0000-0000-000000000000").unwrap(),
            task_name: "Test task".to_string(),
            status: ProgressEntryStatus::Failed,
            files_changed: vec!["test.rs".to_string()],
            learnings: vec!["Learning 1".to_string()],
        };

        let markdown = entry.to_markdown();
        let parsed = ProgressEntry::from_markdown(&markdown).unwrap();

        assert_eq!(parsed.timestamp, entry.timestamp);
        assert_eq!(parsed.task_name, entry.task_name);
        assert_eq!(parsed.status, entry.status);
        assert_eq!(parsed.files_changed, entry.files_changed);
        assert_eq!(parsed.learnings, entry.learnings);
    }

    #[test]
    fn test_progress_log_append_and_read() {
        let dir = tempdir().unwrap();
        let log = ProgressLog::new(dir.path());

        // Initially no entries
        assert!(!log.exists());
        assert_eq!(log.read_all_entries().unwrap().len(), 0);

        // Append first entry
        let entry1 = ProgressEntry::new(
            Uuid::new_v4(),
            "Task 1".to_string(),
            ProgressEntryStatus::Done,
            vec!["file1.rs".to_string()],
            vec!["Learning A".to_string()],
        );
        log.append_entry(&entry1).unwrap();

        assert!(log.exists());
        let entries = log.read_all_entries().unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].task_name, "Task 1");

        // Append second entry
        let entry2 = ProgressEntry::new(
            Uuid::new_v4(),
            "Task 2".to_string(),
            ProgressEntryStatus::Failed,
            vec!["file2.rs".to_string()],
            vec!["Learning B".to_string()],
        );
        log.append_entry(&entry2).unwrap();

        let entries = log.read_all_entries().unwrap();
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[1].task_name, "Task 2");
    }

    #[test]
    fn test_progress_log_read_recent() {
        let dir = tempdir().unwrap();
        let log = ProgressLog::new(dir.path());

        // Append 5 entries
        for i in 1..=5 {
            let entry = ProgressEntry::new(
                Uuid::new_v4(),
                format!("Task {}", i),
                ProgressEntryStatus::Done,
                vec![],
                vec![],
            );
            log.append_entry(&entry).unwrap();
        }

        // Read only last 3
        let recent = log.read_recent_entries(3).unwrap();
        assert_eq!(recent.len(), 3);
        assert_eq!(recent[0].task_name, "Task 3");
        assert_eq!(recent[1].task_name, "Task 4");
        assert_eq!(recent[2].task_name, "Task 5");
    }

    #[test]
    fn test_progress_log_format_for_prompt() {
        let dir = tempdir().unwrap();
        let log = ProgressLog::new(dir.path());

        let entry = ProgressEntry::new(
            Uuid::new_v4(),
            "Important task".to_string(),
            ProgressEntryStatus::Done,
            vec!["src/main.rs".to_string()],
            vec!["Key insight".to_string()],
        );
        log.append_entry(&entry).unwrap();

        let prompt = log.format_for_prompt(10).unwrap();
        assert!(prompt.contains("## Previous Task Progress"));
        assert!(prompt.contains("Important task"));
        assert!(prompt.contains("Key insight"));
    }

    #[test]
    fn test_progress_entry_status_display() {
        assert_eq!(format!("{}", ProgressEntryStatus::Done), "done");
        assert_eq!(format!("{}", ProgressEntryStatus::Failed), "failed");
    }

    #[test]
    fn test_progress_entry_status_parse() {
        assert_eq!(
            "done".parse::<ProgressEntryStatus>().unwrap(),
            ProgressEntryStatus::Done
        );
        assert_eq!(
            "completed".parse::<ProgressEntryStatus>().unwrap(),
            ProgressEntryStatus::Done
        );
        assert_eq!(
            "failed".parse::<ProgressEntryStatus>().unwrap(),
            ProgressEntryStatus::Failed
        );
        assert_eq!(
            "error".parse::<ProgressEntryStatus>().unwrap(),
            ProgressEntryStatus::Failed
        );
        assert!("unknown".parse::<ProgressEntryStatus>().is_err());
    }

    #[test]
    fn test_empty_files_and_learnings() {
        let entry = ProgressEntry::new(
            Uuid::new_v4(),
            "Empty task".to_string(),
            ProgressEntryStatus::Done,
            vec![],
            vec![],
        );

        let markdown = entry.to_markdown();
        assert!(markdown.contains("Files changed: (none)"));
        assert!(markdown.contains("(none recorded)"));
    }

    #[test]
    fn test_progress_log_file_header() {
        let dir = tempdir().unwrap();
        let log = ProgressLog::new(dir.path());

        let entry = ProgressEntry::new(
            Uuid::new_v4(),
            "Test".to_string(),
            ProgressEntryStatus::Done,
            vec![],
            vec![],
        );
        log.append_entry(&entry).unwrap();

        let content = fs::read_to_string(log.path()).unwrap();
        assert!(content.starts_with("# progress.log\n"));
        assert!(content.contains("Auto-generated by Makima"));
    }

    #[test]
    fn test_append_progress_entry_helper() {
        let dir = tempdir().unwrap();

        append_progress_entry(
            dir.path(),
            Uuid::new_v4(),
            "Helper test".to_string(),
            ProgressEntryStatus::Done,
            vec!["file.rs".to_string()],
            vec!["A learning".to_string()],
        )
        .unwrap();

        let log = ProgressLog::new(dir.path());
        let entries = log.read_all_entries().unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].task_name, "Helper test");
    }
}