| 1 | //! Pure compatibility projections for the pre-Work-Graph Plan and To-do wire |
| 2 | //! formats. |
| 3 | //! |
| 4 | //! The graph owns the ordering, metadata, titles, and states. These functions |
| 5 | //! only derive owned snapshots; they never receive mutable graph access (V10). |
| 6 | |
| 7 | use crate::tools::plan::{PlanItemArg, PlanSnapshot, StepStatus}; |
| 8 | use crate::tools::todo::{TodoItem, TodoListSnapshot, TodoStatus}; |
| 9 | |
| 10 | use super::model::{CompatPlanMetadata, NodeState, WorkGraphSnapshot, WorkNode}; |
| 11 | |
| 12 | pub type PlanProjection = PlanSnapshot; |
| 13 | pub type TodoProjection = TodoListSnapshot; |
| 14 | |
| 15 | impl CompatPlanMetadata { |
| 16 | #[must_use] |
| 17 | pub fn from_plan_snapshot(snapshot: &PlanSnapshot) -> Self { |
| 18 | Self { |
| 19 | title: snapshot.title.clone(), |
| 20 | objective: snapshot.objective.clone(), |
| 21 | context_summary: snapshot.context_summary.clone(), |
| 22 | explanation: snapshot.explanation.clone(), |
| 23 | sources_used: snapshot.sources_used.clone(), |
| 24 | critical_files: snapshot.critical_files.clone(), |
| 25 | constraints: snapshot.constraints.clone(), |
| 26 | recommended_approach: snapshot.recommended_approach.clone(), |
| 27 | verification_plan: snapshot.verification_plan.clone(), |
| 28 | risks_and_unknowns: snapshot.risks_and_unknowns.clone(), |
| 29 | handoff_packet: snapshot.handoff_packet.clone(), |
| 30 | replan_if: snapshot.replan_if.clone(), |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | #[must_use] |
| 35 | fn to_plan_snapshot(&self) -> PlanSnapshot { |
| 36 | PlanSnapshot { |
| 37 | title: self.title.clone(), |
| 38 | objective: self.objective.clone(), |
| 39 | context_summary: self.context_summary.clone(), |
| 40 | explanation: self.explanation.clone(), |
| 41 | sources_used: self.sources_used.clone(), |
| 42 | critical_files: self.critical_files.clone(), |
| 43 | constraints: self.constraints.clone(), |
| 44 | recommended_approach: self.recommended_approach.clone(), |
| 45 | verification_plan: self.verification_plan.clone(), |
| 46 | risks_and_unknowns: self.risks_and_unknowns.clone(), |
| 47 | handoff_packet: self.handoff_packet.clone(), |
| 48 | replan_if: self.replan_if.clone(), |
| 49 | items: Vec::new(), |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /// Derive the complete legacy Strategy/Plan snapshot. |
| 55 | #[must_use] |
| 56 | pub fn project_plan(snapshot: &WorkGraphSnapshot) -> PlanProjection { |
| 57 | let mut plan = snapshot.compat.plan.to_plan_snapshot(); |
| 58 | plan.items = snapshot |
| 59 | .compat |
| 60 | .plan_order |
| 61 | .iter() |
| 62 | .filter_map(|id| snapshot.node(id)) |
| 63 | .map(|node| PlanItemArg { |
| 64 | step: node.title.clone(), |
| 65 | status: plan_status(node), |
| 66 | }) |
| 67 | .collect(); |
| 68 | plan |
| 69 | } |
| 70 | |
| 71 | /// Derive the complete legacy To-do snapshot. Migration provenance stays in |
| 72 | /// the graph; old readers receive clean, user-visible content. |
| 73 | #[must_use] |
| 74 | pub fn project_todos(snapshot: &WorkGraphSnapshot) -> TodoProjection { |
| 75 | let items = snapshot |
| 76 | .compat |
| 77 | .todos |
| 78 | .iter() |
| 79 | .filter_map(|binding| { |
| 80 | let node = snapshot.node(&binding.node)?; |
| 81 | Some(TodoItem { |
| 82 | id: binding.legacy_id, |
| 83 | content: node.title.clone(), |
| 84 | status: todo_status(node), |
| 85 | }) |
| 86 | }) |
| 87 | .collect::<Vec<_>>(); |
| 88 | let settled = items.iter().filter(|item| item.status.is_settled()).count(); |
| 89 | let completion_pct = if items.is_empty() { |
| 90 | 0 |
| 91 | } else { |
| 92 | let rounded = (settled.saturating_mul(100) + items.len() / 2) / items.len(); |
| 93 | u8::try_from(rounded).unwrap_or(u8::MAX) |
| 94 | }; |
| 95 | let in_progress_id = items |
| 96 | .iter() |
| 97 | .find(|item| item.status == TodoStatus::InProgress) |
| 98 | .map(|item| item.id); |
| 99 | TodoListSnapshot { |
| 100 | items, |
| 101 | completion_pct, |
| 102 | in_progress_id, |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | fn plan_status(node: &WorkNode) -> StepStatus { |
| 107 | match node.state { |
| 108 | NodeState::Initializing | NodeState::Active => StepStatus::InProgress, |
| 109 | NodeState::Completed | NodeState::Verified => StepStatus::Completed, |
| 110 | _ => StepStatus::Pending, |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | fn todo_status(node: &WorkNode) -> TodoStatus { |
| 115 | match node.state { |
| 116 | NodeState::Initializing | NodeState::Active => TodoStatus::InProgress, |
| 117 | NodeState::Completed | NodeState::Verified => TodoStatus::Completed, |
| 118 | NodeState::Cancelled | NodeState::Superseded => TodoStatus::Cancelled, |
| 119 | _ => TodoStatus::Pending, |
| 120 | } |
| 121 | } |
| 122 |