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
|
//! Phase guidance and deliverables tracking for contract management.
//!
//! This module provides structured guidance for each contract phase, tracking
//! expected deliverables and completion criteria.
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use uuid::Uuid;
/// Priority level for recommended deliverables
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "lowercase")]
pub enum FilePriority {
/// Must exist before advancing phase
Required,
/// Strongly suggested for phase completion
Recommended,
/// Nice to have, not blocking
Optional,
}
/// A recommended file for a phase
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecommendedFile {
/// Template ID to create from
pub template_id: String,
/// Suggested file name
pub name_suggestion: String,
/// Priority level
pub priority: FilePriority,
/// Brief description of purpose
pub description: String,
}
/// Expected deliverables for a phase
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PhaseDeliverables {
/// Phase name
pub phase: String,
/// Recommended files to create
pub recommended_files: Vec<RecommendedFile>,
/// Whether a repository is required for this phase
pub requires_repository: bool,
/// Whether tasks should exist in this phase
pub requires_tasks: bool,
/// Guidance text for this phase
pub guidance: String,
}
/// Status of a deliverable item
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct DeliverableStatus {
/// Template ID
pub template_id: String,
/// Expected name
pub name: String,
/// Priority
pub priority: FilePriority,
/// Whether it has been created
pub completed: bool,
/// File ID if created
pub file_id: Option<Uuid>,
/// Actual file name if created
pub actual_name: Option<String>,
}
/// Checklist for phase completion
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct PhaseChecklist {
/// Current phase
pub phase: String,
/// File deliverables status
pub file_deliverables: Vec<DeliverableStatus>,
/// Whether repository is configured
pub has_repository: bool,
/// Whether repository was required
pub repository_required: bool,
/// Task statistics (for execute phase)
pub task_stats: Option<TaskStats>,
/// Overall completion percentage (0-100)
pub completion_percentage: u8,
/// Summary message
pub summary: String,
/// Suggestions for next actions
pub suggestions: Vec<String>,
}
/// Task statistics for execute phase
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct TaskStats {
pub total: usize,
pub pending: usize,
pub running: usize,
pub done: usize,
pub failed: usize,
}
/// Minimal file info for checklist building
pub struct FileInfo {
pub id: Uuid,
pub name: String,
pub contract_phase: Option<String>,
}
/// Minimal task info for checklist building
pub struct TaskInfo {
pub id: Uuid,
pub name: String,
pub status: String,
}
/// Get phase deliverables configuration
pub fn get_phase_deliverables(phase: &str) -> PhaseDeliverables {
match phase {
"research" => PhaseDeliverables {
phase: "research".to_string(),
recommended_files: vec![
RecommendedFile {
template_id: "research-notes".to_string(),
name_suggestion: "Research Notes".to_string(),
priority: FilePriority::Recommended,
description: "Document findings and insights during research".to_string(),
},
RecommendedFile {
template_id: "competitor-analysis".to_string(),
name_suggestion: "Competitor Analysis".to_string(),
priority: FilePriority::Recommended,
description: "Analyze competitors and market positioning".to_string(),
},
RecommendedFile {
template_id: "user-research".to_string(),
name_suggestion: "User Research".to_string(),
priority: FilePriority::Optional,
description: "Document user interviews and persona insights".to_string(),
},
],
requires_repository: false,
requires_tasks: false,
guidance: "Focus on understanding the problem space, gathering information, and documenting findings. Create at least one research document before moving to Specify phase.".to_string(),
},
"specify" => PhaseDeliverables {
phase: "specify".to_string(),
recommended_files: vec![
RecommendedFile {
template_id: "requirements".to_string(),
name_suggestion: "Requirements Document".to_string(),
priority: FilePriority::Required,
description: "Define functional and non-functional requirements".to_string(),
},
RecommendedFile {
template_id: "user-stories".to_string(),
name_suggestion: "User Stories".to_string(),
priority: FilePriority::Recommended,
description: "Define features from the user's perspective".to_string(),
},
RecommendedFile {
template_id: "acceptance-criteria".to_string(),
name_suggestion: "Acceptance Criteria".to_string(),
priority: FilePriority::Recommended,
description: "Define testable conditions for completion".to_string(),
},
],
requires_repository: false,
requires_tasks: false,
guidance: "Define what needs to be built with clear requirements and acceptance criteria. Ensure specifications are detailed enough for planning.".to_string(),
},
"plan" => PhaseDeliverables {
phase: "plan".to_string(),
recommended_files: vec![
RecommendedFile {
template_id: "architecture".to_string(),
name_suggestion: "Architecture Document".to_string(),
priority: FilePriority::Recommended,
description: "Document system architecture and design decisions".to_string(),
},
RecommendedFile {
template_id: "task-breakdown".to_string(),
name_suggestion: "Task Breakdown".to_string(),
priority: FilePriority::Required,
description: "Break down work into implementable tasks".to_string(),
},
RecommendedFile {
template_id: "technical-design".to_string(),
name_suggestion: "Technical Design".to_string(),
priority: FilePriority::Optional,
description: "Detailed technical specification".to_string(),
},
],
requires_repository: true,
requires_tasks: false,
guidance: "Design the solution and break down work into tasks. A repository must be configured before moving to Execute phase.".to_string(),
},
"execute" => PhaseDeliverables {
phase: "execute".to_string(),
recommended_files: vec![
RecommendedFile {
template_id: "dev-notes".to_string(),
name_suggestion: "Development Notes".to_string(),
priority: FilePriority::Recommended,
description: "Track implementation details and decisions".to_string(),
},
RecommendedFile {
template_id: "test-plan".to_string(),
name_suggestion: "Test Plan".to_string(),
priority: FilePriority::Optional,
description: "Document testing strategy and test cases".to_string(),
},
RecommendedFile {
template_id: "implementation-log".to_string(),
name_suggestion: "Implementation Log".to_string(),
priority: FilePriority::Optional,
description: "Chronological log of implementation progress".to_string(),
},
],
requires_repository: true,
requires_tasks: true,
guidance: "Execute the planned tasks, implement features, and track progress. Complete all tasks before moving to Review phase.".to_string(),
},
"review" => PhaseDeliverables {
phase: "review".to_string(),
recommended_files: vec![
RecommendedFile {
template_id: "release-notes".to_string(),
name_suggestion: "Release Notes".to_string(),
priority: FilePriority::Required,
description: "Document changes for release communication".to_string(),
},
RecommendedFile {
template_id: "review-checklist".to_string(),
name_suggestion: "Review Checklist".to_string(),
priority: FilePriority::Recommended,
description: "Comprehensive checklist for code and feature review".to_string(),
},
RecommendedFile {
template_id: "retrospective".to_string(),
name_suggestion: "Retrospective".to_string(),
priority: FilePriority::Optional,
description: "Reflect on the project and capture learnings".to_string(),
},
],
requires_repository: false,
requires_tasks: false,
guidance: "Review completed work, document the release, and conduct a retrospective. The contract can be completed after review.".to_string(),
},
_ => PhaseDeliverables {
phase: phase.to_string(),
recommended_files: vec![],
requires_repository: false,
requires_tasks: false,
guidance: "Unknown phase".to_string(),
},
}
}
/// Build a phase checklist comparing expected vs actual deliverables
pub fn get_phase_checklist(
phase: &str,
files: &[FileInfo],
tasks: &[TaskInfo],
has_repository: bool,
) -> PhaseChecklist {
let deliverables = get_phase_deliverables(phase);
// Match files to expected deliverables
let file_deliverables: Vec<DeliverableStatus> = deliverables
.recommended_files
.iter()
.map(|rec| {
// Check if a file with matching template ID or similar name exists
let matched_file = files.iter().find(|f| {
// Match by phase first
f.contract_phase.as_deref() == Some(phase) &&
// Then by name similarity (case-insensitive contains)
(f.name.to_lowercase().contains(&rec.name_suggestion.to_lowercase()) ||
rec.name_suggestion.to_lowercase().contains(&f.name.to_lowercase()) ||
f.name.to_lowercase().contains(&rec.template_id.replace("-", " ")))
});
DeliverableStatus {
template_id: rec.template_id.clone(),
name: rec.name_suggestion.clone(),
priority: rec.priority,
completed: matched_file.is_some(),
file_id: matched_file.map(|f| f.id),
actual_name: matched_file.map(|f| f.name.clone()),
}
})
.collect();
// Calculate task stats for execute phase
let task_stats = if phase == "execute" {
let total = tasks.len();
let pending = tasks.iter().filter(|t| t.status == "pending").count();
let running = tasks.iter().filter(|t| t.status == "running").count();
let done = tasks.iter().filter(|t| t.status == "done").count();
let failed = tasks.iter().filter(|t| t.status == "failed" || t.status == "error").count();
Some(TaskStats { total, pending, running, done, failed })
} else {
None
};
// Calculate completion percentage
let mut completed_items = 0;
let mut total_items = 0;
// Count required and recommended files (not optional)
for status in &file_deliverables {
if status.priority != FilePriority::Optional {
total_items += 1;
if status.completed {
completed_items += 1;
}
}
}
// Count repository if required
if deliverables.requires_repository {
total_items += 1;
if has_repository {
completed_items += 1;
}
}
// Count tasks if in execute phase
if let Some(ref stats) = task_stats {
if stats.total > 0 {
total_items += 1;
if stats.done == stats.total && stats.total > 0 {
completed_items += 1;
}
}
}
let completion_percentage = if total_items > 0 {
((completed_items as f64 / total_items as f64) * 100.0) as u8
} else {
100 // No requirements means complete
};
// Generate suggestions
let mut suggestions = Vec::new();
// Suggest missing required files
for status in &file_deliverables {
if !status.completed {
match status.priority {
FilePriority::Required => {
suggestions.push(format!("Create {} (required)", status.name));
}
FilePriority::Recommended => {
suggestions.push(format!("Consider creating {} (recommended)", status.name));
}
FilePriority::Optional => {
// Don't suggest optional items
}
}
}
}
// Suggest repository if needed
if deliverables.requires_repository && !has_repository {
suggestions.push("Configure a repository for task execution".to_string());
}
// Suggest task actions for execute phase
if let Some(ref stats) = task_stats {
if stats.total == 0 {
suggestions.push("Create tasks from the Task Breakdown document".to_string());
} else if stats.pending > 0 {
suggestions.push(format!("Run {} pending task(s)", stats.pending));
} else if stats.running > 0 {
suggestions.push(format!("Wait for {} running task(s) to complete", stats.running));
} else if stats.failed > 0 {
suggestions.push(format!("Address {} failed task(s)", stats.failed));
} else if stats.done == stats.total && stats.total > 0 {
// All tasks complete - for simple contracts, this is the terminal phase
// For specification contracts, they should advance to review phase
suggestions.push("Mark the contract as completed (for simple contracts) or advance to Review phase".to_string());
}
}
// Suggest completion for review phase (terminal for specification contracts)
if phase == "review" {
let has_release_notes = file_deliverables.iter()
.any(|d| d.template_id == "release-notes" && d.completed);
if has_release_notes {
suggestions.push("Mark the contract as completed".to_string());
}
}
// Generate summary
let summary = generate_phase_summary(phase, &file_deliverables, has_repository, &task_stats, completion_percentage);
PhaseChecklist {
phase: phase.to_string(),
file_deliverables,
has_repository,
repository_required: deliverables.requires_repository,
task_stats,
completion_percentage,
summary,
suggestions,
}
}
fn generate_phase_summary(
phase: &str,
deliverables: &[DeliverableStatus],
has_repository: bool,
task_stats: &Option<TaskStats>,
completion_percentage: u8,
) -> String {
let completed_count = deliverables.iter().filter(|d| d.completed).count();
let total_count = deliverables.len();
match phase {
"research" => {
if completed_count == 0 {
"Research phase needs documentation. Create research notes or competitor analysis.".to_string()
} else {
format!("{}/{} research documents created. Consider transitioning to Specify phase.", completed_count, total_count)
}
}
"specify" => {
let has_required = deliverables.iter()
.filter(|d| d.priority == FilePriority::Required)
.all(|d| d.completed);
if !has_required {
"Specify phase requires a Requirements Document before transitioning.".to_string()
} else if completion_percentage >= 66 {
"Specifications are ready. Consider transitioning to Plan phase.".to_string()
} else {
format!("{}/{} specification documents created.", completed_count, total_count)
}
}
"plan" => {
let has_task_breakdown = deliverables.iter()
.any(|d| d.template_id == "task-breakdown" && d.completed);
if !has_task_breakdown {
"Plan phase requires a Task Breakdown document.".to_string()
} else if !has_repository {
"Repository not configured. Configure a repository before Execute phase.".to_string()
} else {
"Planning complete. Ready to transition to Execute phase.".to_string()
}
}
"execute" => {
if let Some(stats) = task_stats {
if stats.total == 0 {
"No tasks created. Create tasks from the Task Breakdown document.".to_string()
} else if stats.done == stats.total {
"All tasks complete! Ready for Review phase.".to_string()
} else {
format!("{}/{} tasks completed ({}% done)", stats.done, stats.total,
if stats.total > 0 { (stats.done * 100) / stats.total } else { 0 })
}
} else {
"Execute phase in progress.".to_string()
}
}
"review" => {
let has_release_notes = deliverables.iter()
.any(|d| d.template_id == "release-notes" && d.completed);
if !has_release_notes {
"Review phase requires Release Notes before completion.".to_string()
} else {
"Review documentation complete. Contract can be marked as done.".to_string()
}
}
_ => format!("Phase {} - {}% complete", phase, completion_percentage),
}
}
/// Check if phase targets are met for transition
pub fn check_phase_completion(
phase: &str,
files: &[FileInfo],
tasks: &[TaskInfo],
has_repository: bool,
) -> bool {
let checklist = get_phase_checklist(phase, files, tasks, has_repository);
// Check required files are complete
let required_files_complete = checklist.file_deliverables.iter()
.filter(|d| d.priority == FilePriority::Required)
.all(|d| d.completed);
// Check repository if required
let repository_ok = !checklist.repository_required || checklist.has_repository;
// Check tasks if in execute phase
let tasks_ok = if let Some(stats) = &checklist.task_stats {
stats.total > 0 && stats.done == stats.total
} else {
true
};
required_files_complete && repository_ok && tasks_ok
}
/// Format checklist as markdown for LLM context
pub fn format_checklist_markdown(checklist: &PhaseChecklist) -> String {
let mut md = format!("## Phase Progress ({} Phase)\n\n", capitalize(&checklist.phase));
// File deliverables
md.push_str("### Deliverables\n");
for status in &checklist.file_deliverables {
let check = if status.completed { "+" } else { "-" };
let priority_label = match status.priority {
FilePriority::Required => " (required)",
FilePriority::Recommended => " (recommended)",
FilePriority::Optional => " (optional)",
};
if status.completed {
md.push_str(&format!("[{}] {} - \"{}\"\n", check, status.name, status.actual_name.as_deref().unwrap_or("created")));
} else {
md.push_str(&format!("[{}] {}{}\n", check, status.name, priority_label));
}
}
// Repository status
if checklist.repository_required {
let check = if checklist.has_repository { "+" } else { "-" };
md.push_str(&format!("[{}] Repository configured (required)\n", check));
}
// Task stats for execute phase
if let Some(ref stats) = checklist.task_stats {
md.push_str(&format!("\n### Task Progress\n"));
md.push_str(&format!("- Total: {}\n", stats.total));
md.push_str(&format!("- Done: {}\n", stats.done));
if stats.pending > 0 {
md.push_str(&format!("- Pending: {}\n", stats.pending));
}
if stats.running > 0 {
md.push_str(&format!("- Running: {}\n", stats.running));
}
if stats.failed > 0 {
md.push_str(&format!("- Failed: {}\n", stats.failed));
}
}
// Summary
md.push_str(&format!("\n**Status**: {} ({}% complete)\n", checklist.summary, checklist.completion_percentage));
// Suggestions
if !checklist.suggestions.is_empty() {
md.push_str("\n**Next Steps**:\n");
for suggestion in &checklist.suggestions {
md.push_str(&format!("- {}\n", suggestion));
}
}
md
}
fn capitalize(s: &str) -> String {
let mut chars = s.chars();
match chars.next() {
None => String::new(),
Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_phase_deliverables() {
let research = get_phase_deliverables("research");
assert_eq!(research.phase, "research");
assert!(!research.requires_repository);
assert_eq!(research.recommended_files.len(), 3);
let plan = get_phase_deliverables("plan");
assert!(plan.requires_repository);
assert!(plan.recommended_files.iter().any(|f| f.template_id == "task-breakdown"));
}
#[test]
fn test_phase_checklist_empty() {
let checklist = get_phase_checklist("research", &[], &[], false);
assert_eq!(checklist.completion_percentage, 0);
assert!(!checklist.suggestions.is_empty());
}
#[test]
fn test_check_phase_completion() {
let files = vec![
FileInfo {
id: Uuid::new_v4(),
name: "Requirements Document".to_string(),
contract_phase: Some("specify".to_string()),
},
];
// Specify phase has required file
let complete = check_phase_completion("specify", &files, &[], false);
assert!(complete);
}
}
|