summaryrefslogtreecommitdiff
path: root/makima/src/llm/task_output.rs
blob: c7f699095d3c1fa8e450a68aec0b0ad437385355 (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
//! Task output processing and task derivation utilities.
//!
//! This module provides utilities for:
//! - Parsing task lists from markdown documents
//! - Analyzing completed task outputs
//! - Suggesting follow-up actions based on task results

use regex::Regex;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// A parsed task from a markdown document
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParsedTask {
    /// Task name/title
    pub name: String,
    /// Task description or plan
    pub description: Option<String>,
    /// Group/phase this task belongs to
    pub group: Option<String>,
    /// Order within the group (0-indexed)
    pub order: usize,
    /// Whether this task was marked as completed in source
    pub completed: bool,
    /// Dependencies (names of other tasks)
    pub dependencies: Vec<String>,
}

/// Result of parsing tasks from a document
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskParseResult {
    /// Successfully parsed tasks
    pub tasks: Vec<ParsedTask>,
    /// Groups/phases found
    pub groups: Vec<String>,
    /// Total tasks found
    pub total: usize,
    /// Any parsing warnings
    pub warnings: Vec<String>,
}

/// Impact on contract phase
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PhaseImpact {
    /// Current phase
    pub phase: String,
    /// Whether phase targets are now met
    pub targets_met: bool,
    /// Tasks remaining in phase
    pub tasks_remaining: usize,
    /// Suggestion for phase transition
    pub transition_suggestion: Option<String>,
}

/// Suggested action based on task output
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum SuggestedAction {
    /// Create a follow-up task
    CreateTask {
        name: String,
        plan: String,
        chain_from: Option<Uuid>,
    },
    /// Create a new file from template
    CreateFile {
        template_id: String,
        name: String,
        seed_content: Option<String>,
    },
    /// Update an existing file
    UpdateFile {
        file_id: Uuid,
        file_name: String,
        additions: String,
    },
    /// Advance to next phase
    AdvancePhase {
        to_phase: String,
    },
    /// Run the next chained task
    RunNextTask {
        task_id: Uuid,
        task_name: String,
    },
    /// Mark the contract as completed
    MarkContractComplete {
        contract_id: Uuid,
    },
}

/// Analysis of a completed task's output
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskOutputAnalysis {
    /// Summary of what was accomplished
    pub summary: String,
    /// Files that were created/modified (from diff)
    pub files_affected: Vec<String>,
    /// Suggested next actions
    pub next_steps: Vec<SuggestedAction>,
    /// Impact on contract phase
    pub phase_impact: Option<PhaseImpact>,
}

/// Parse tasks from a markdown task breakdown document
///
/// Supports formats like:
/// - `[ ] Task name`
/// - `[x] Completed task`
/// - `1. Task name`
/// - `- Task name`
///
/// Groups are detected from `## Phase/Section` headings.
pub fn parse_tasks_from_breakdown(content: &str) -> TaskParseResult {
    let mut tasks = Vec::new();
    let mut groups = Vec::new();
    let mut warnings = Vec::new();
    let mut current_group: Option<String> = None;
    let mut task_order = 0;

    // Patterns for task items
    let checkbox_pattern = Regex::new(r"^\s*[-*]\s*\[([ xX])\]\s*(.+)$").unwrap();
    let numbered_checkbox = Regex::new(r"^\s*\d+\.\s*\[([ xX])\]\s*(.+)$").unwrap();
    let numbered_pattern = Regex::new(r"^\s*\d+\.\s+(.+)$").unwrap();
    let bullet_pattern = Regex::new(r"^\s*[-*]\s+(.+)$").unwrap();
    let heading_pattern = Regex::new(r"^##\s+(?:Phase\s*\d*:?\s*)?(.+)$").unwrap();

    // Patterns for dependencies (inline)
    let depends_pattern = Regex::new(r"(?i)\(?\s*(?:depends on|after|requires):?\s*([^)]+)\)?").unwrap();

    for line in content.lines() {
        let trimmed = line.trim();

        // Skip empty lines
        if trimmed.is_empty() {
            continue;
        }

        // Check for section headings
        if let Some(caps) = heading_pattern.captures(trimmed) {
            let group_name = caps[1].trim().to_string();
            if !groups.contains(&group_name) {
                groups.push(group_name.clone());
            }
            current_group = Some(group_name);
            task_order = 0;
            continue;
        }

        // Try to parse as a task
        let mut task_name: Option<String> = None;
        let mut completed = false;

        // Try checkbox patterns first (more specific)
        if let Some(caps) = checkbox_pattern.captures(trimmed) {
            completed = &caps[1] != " ";
            task_name = Some(caps[2].trim().to_string());
        } else if let Some(caps) = numbered_checkbox.captures(trimmed) {
            completed = &caps[1] != " ";
            task_name = Some(caps[2].trim().to_string());
        } else if let Some(caps) = numbered_pattern.captures(trimmed) {
            task_name = Some(caps[1].trim().to_string());
        } else if let Some(caps) = bullet_pattern.captures(trimmed) {
            // Only treat as task if it looks like a task (has actionable verbs)
            let text = caps[1].trim();
            if looks_like_task(text) {
                task_name = Some(text.to_string());
            }
        }

        if let Some(name) = task_name {
            // Skip items that are clearly not tasks
            if name.to_lowercase().starts_with("note:") ||
               name.to_lowercase().starts_with("todo:") && name.len() < 10 ||
               name.starts_with('#') {
                continue;
            }

            // Extract dependencies if present
            let dependencies = if let Some(dep_caps) = depends_pattern.captures(&name) {
                dep_caps[1]
                    .split(',')
                    .map(|s| s.trim().to_string())
                    .filter(|s| !s.is_empty())
                    .collect()
            } else {
                Vec::new()
            };

            // Clean task name (remove dependency info)
            let clean_name = depends_pattern.replace(&name, "").trim().to_string();

            // Extract description if there's a colon
            let (final_name, description) = if let Some(idx) = clean_name.find(':') {
                let (n, d) = clean_name.split_at(idx);
                (n.trim().to_string(), Some(d[1..].trim().to_string()))
            } else {
                (clean_name, None)
            };

            tasks.push(ParsedTask {
                name: final_name,
                description,
                group: current_group.clone(),
                order: task_order,
                completed,
                dependencies,
            });

            task_order += 1;
        }
    }

    let total = tasks.len();

    // Add warnings
    if tasks.is_empty() {
        warnings.push("No tasks found in document. Ensure tasks are formatted as checkbox items (- [ ] Task) or numbered lists (1. Task).".to_string());
    }

    TaskParseResult {
        tasks,
        groups,
        total,
        warnings,
    }
}

/// Check if text looks like a task (has action verbs at word boundaries)
fn looks_like_task(text: &str) -> bool {
    let lower = text.to_lowercase();
    let action_verbs = [
        "add", "create", "implement", "build", "write", "fix", "update",
        "refactor", "test", "configure", "set up", "setup", "deploy",
        "integrate", "migrate", "design", "review", "document", "remove",
        "delete", "modify", "change", "improve", "optimize", "enable",
        "disable", "install", "initialize", "define", "extend", "extract",
    ];

    // Check if text starts with an action verb (followed by space or end)
    for verb in &action_verbs {
        if lower.starts_with(verb) {
            // Check for word boundary after verb
            let after = &lower[verb.len()..];
            if after.is_empty() || after.starts_with(' ') || after.starts_with('_') {
                return true;
            }
        }
        // Check if verb appears after space with word boundary
        let pattern = format!(" {} ", verb);
        let pattern_end = format!(" {}", verb);
        if lower.contains(&pattern) {
            return true;
        }
        // Check if verb is at the end of string after a space
        if lower.ends_with(&pattern_end) && lower.len() > pattern_end.len() {
            return true;
        }
    }
    false
}

/// Analyze a completed task's output to suggest next actions
pub fn analyze_task_output(
    _task_id: Uuid,
    task_name: &str,
    task_result: Option<&str>,
    task_diff: Option<&str>,
    contract_phase: &str,
    total_tasks: usize,
    completed_tasks: usize,
    next_task: Option<(Uuid, String)>,
    dev_notes_file: Option<(Uuid, String)>,
) -> TaskOutputAnalysis {
    let mut next_steps = Vec::new();
    let mut files_affected = Vec::new();

    // Parse files from diff if available
    if let Some(diff) = task_diff {
        files_affected = extract_files_from_diff(diff);
    }

    // Generate summary
    let summary = if let Some(result) = task_result {
        if result.len() > 200 {
            format!("{}...", &result[..200])
        } else {
            result.to_string()
        }
    } else {
        format!("Task '{}' completed", task_name)
    };

    // If there's a next chained task, suggest running it
    if let Some((next_id, next_name)) = next_task {
        next_steps.push(SuggestedAction::RunNextTask {
            task_id: next_id,
            task_name: next_name,
        });
    }

    // Suggest updating Dev Notes if in execute phase and file exists
    if contract_phase == "execute" {
        if let Some((file_id, file_name)) = dev_notes_file {
            let additions = format!(
                "\n## Task: {}\n\n{}\n\n### Files Modified\n{}\n",
                task_name,
                summary,
                files_affected.iter()
                    .map(|f| format!("- {}", f))
                    .collect::<Vec<_>>()
                    .join("\n")
            );

            next_steps.push(SuggestedAction::UpdateFile {
                file_id,
                file_name,
                additions,
            });
        } else {
            // Suggest creating Dev Notes
            next_steps.push(SuggestedAction::CreateFile {
                template_id: "dev-notes".to_string(),
                name: "Development Notes".to_string(),
                seed_content: Some(format!(
                    "# Development Notes\n\n## Task: {}\n\n{}\n",
                    task_name, summary
                )),
            });
        }
    }

    // Calculate phase impact
    let new_completed = completed_tasks + 1;
    let targets_met = new_completed >= total_tasks && total_tasks > 0;
    let tasks_remaining = total_tasks.saturating_sub(new_completed);

    let transition_suggestion = if targets_met && contract_phase == "execute" {
        Some("All tasks complete. Ready to advance to Review phase.".to_string())
    } else {
        None
    };

    // If targets are met, suggest phase transition
    if targets_met && contract_phase == "execute" {
        next_steps.push(SuggestedAction::AdvancePhase {
            to_phase: "review".to_string(),
        });
    }

    let phase_impact = Some(PhaseImpact {
        phase: contract_phase.to_string(),
        targets_met,
        tasks_remaining,
        transition_suggestion,
    });

    TaskOutputAnalysis {
        summary,
        files_affected,
        next_steps,
        phase_impact,
    }
}

/// Extract file paths from a git diff
fn extract_files_from_diff(diff: &str) -> Vec<String> {
    let mut files = Vec::new();
    let file_pattern = Regex::new(r"^(?:diff --git a/|[+]{3} b/|[-]{3} a/)(.+)$").unwrap();

    for line in diff.lines() {
        if let Some(caps) = file_pattern.captures(line) {
            let path = caps[1].trim().to_string();
            // Skip /dev/null and duplicates
            if path != "/dev/null" && !files.contains(&path) {
                // Clean up path (remove a/ or b/ prefix from git diff)
                let clean_path = path.trim_start_matches("a/").trim_start_matches("b/").to_string();
                if !files.contains(&clean_path) {
                    files.push(clean_path);
                }
            }
        }
    }

    files
}

/// Format parsed tasks for display
pub fn format_parsed_tasks(result: &TaskParseResult) -> String {
    let mut output = String::new();

    if result.tasks.is_empty() {
        output.push_str("No tasks found in the document.\n");
        for warning in &result.warnings {
            output.push_str(&format!("Warning: {}\n", warning));
        }
        return output;
    }

    output.push_str(&format!("Found {} task(s)", result.total));
    if !result.groups.is_empty() {
        output.push_str(&format!(" in {} group(s)", result.groups.len()));
    }
    output.push_str(":\n\n");

    let mut current_group: Option<&str> = None;
    for (i, task) in result.tasks.iter().enumerate() {
        // Print group header if changed
        if task.group.as_deref() != current_group {
            current_group = task.group.as_deref();
            if let Some(group) = current_group {
                output.push_str(&format!("**{}**\n", group));
            }
        }

        let status = if task.completed { "[x]" } else { "[ ]" };
        output.push_str(&format!("{}. {} {}", i + 1, status, task.name));

        if !task.dependencies.is_empty() {
            output.push_str(&format!(" (depends on: {})", task.dependencies.join(", ")));
        }

        output.push('\n');
    }

    output
}

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

    #[test]
    fn test_parse_checkbox_tasks() {
        let content = r#"
## Phase 1: Setup
- [ ] Set up project structure
- [x] Configure dev environment

## Phase 2: Features
1. [ ] Implement authentication
2. [ ] Add user dashboard
"#;

        let result = parse_tasks_from_breakdown(content);
        assert_eq!(result.total, 4);
        assert_eq!(result.groups.len(), 2);
        assert!(!result.tasks[0].completed);
        assert!(result.tasks[1].completed);
    }

    #[test]
    fn test_parse_with_dependencies() {
        let content = r#"
- [ ] Task A
- [ ] Task B (depends on: Task A)
"#;

        let result = parse_tasks_from_breakdown(content);
        assert_eq!(result.tasks[1].dependencies, vec!["Task A"]);
    }

    #[test]
    fn test_extract_files_from_diff() {
        let diff = r#"
diff --git a/src/main.rs b/src/main.rs
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,4 @@
+fn new_function() {}
"#;

        let files = extract_files_from_diff(diff);
        assert!(files.contains(&"src/main.rs".to_string()));
    }

    #[test]
    fn test_looks_like_task() {
        assert!(looks_like_task("Add authentication"));
        assert!(looks_like_task("Create user model"));
        assert!(looks_like_task("implement feature X"));
        assert!(!looks_like_task("This is a note"));
        assert!(!looks_like_task("Summary of changes"));
    }
}