| 1 | //! Plan hand-off: ask how to continue a successfully completed Plan response. |
| 2 | //! |
| 3 | //! The exact completed text is frozen for one session and turn. A prose plan |
| 4 | //! seeds one bounded pending item; it is not parsed into English-shaped steps. |
| 5 | //! Checkpoint admission uses the incumbent queued boundary, not a disk-write |
| 6 | //! acknowledgement. Failed preparation removes only its unchanged seed from |
| 7 | //! the current To-do projection; the graph retains unprojected history. That |
| 8 | //! repair cannot recall a checkpoint already accepted by the persistence actor. |
| 9 | //! If cleanup refuses because the owned item changed or Work validation fails, |
| 10 | //! retain its ownership and refuse a new dispatch rather than erase other work. |
| 11 | //! |
| 12 | //! The question uses the modal `request_user_input` already draws. It is |
| 13 | //! host-owned: no tool call is waiting in the engine, so the answer is applied |
| 14 | //! by the TUI (permission, mode, follow-up message) instead of being returned |
| 15 | //! as a tool result. |
| 16 | |
| 17 | use codewhale_config::AppMode; |
| 18 | use codewhale_execpolicy::ApprovalMode; |
| 19 | use codewhale_localization::{Locale, MessageId, tr}; |
| 20 | |
| 21 | use codewhale_engine::core::events::TurnOutcomeStatus; |
| 22 | use codewhale_engine::tools::user_input::{ |
| 23 | UserInputOption, UserInputQuestion, UserInputRequest, UserInputResponse, |
| 24 | }; |
| 25 | |
| 26 | /// Request id of the hand-off question on the view stack. |
| 27 | pub(crate) const REQUEST_ID: &str = "codewhale:plan-handoff"; |
| 28 | const QUESTION_ID: &str = "plan_handoff"; |
| 29 | |
| 30 | /// What the person chose to do with a finished plan. |
| 31 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 32 | pub(crate) enum PlanHandoffChoice { |
| 33 | /// Leave Plan for Work with this permission and start on the plan. |
| 34 | Work(ApprovalMode), |
| 35 | /// Stay in Plan and send this feedback as the next message. |
| 36 | Revise(String), |
| 37 | /// Stay in Plan; nothing is sent. |
| 38 | KeepPlanning, |
| 39 | } |
| 40 | |
| 41 | /// One completed Plan response. The question id prevents a delayed answer |
| 42 | /// from approving a newer plan, even in the same session. |
| 43 | #[derive(Debug, Clone)] |
| 44 | pub(crate) struct PendingPlanHandoff { |
| 45 | pub request_id: String, |
| 46 | pub session_id: String, |
| 47 | pub turn_id: String, |
| 48 | pub history_start: usize, |
| 49 | pub transcript_epoch: u64, |
| 50 | pub text: String, |
| 51 | pub has_current_checklist: bool, |
| 52 | /// Retained only if failed-preparation cleanup cannot safely remove it. |
| 53 | pub seeded_todo_id: Option<u32>, |
| 54 | } |
| 55 | |
| 56 | /// Only typed, successful, nonempty output from the current Plan turn qualifies. |
| 57 | pub(crate) fn plan_ready(mode: AppMode, status: TurnOutcomeStatus, output: Option<&str>) -> bool { |
| 58 | mode == AppMode::Plan |
| 59 | && status == TurnOutcomeStatus::Completed |
| 60 | && output.is_some_and(|text| !text.trim().is_empty()) |
| 61 | } |
| 62 | |
| 63 | pub(crate) fn auto_save_plan_if_ready( |
| 64 | session_id: &str, |
| 65 | mode: AppMode, |
| 66 | status: TurnOutcomeStatus, |
| 67 | output: Option<&str>, |
| 68 | ) -> Option<std::path::PathBuf> { |
| 69 | if !plan_ready(mode, status, output) { |
| 70 | return None; |
| 71 | } |
| 72 | crate::tui::plan_artifact::save_plan_markdown(session_id, output?).ok() |
| 73 | } |
| 74 | |
| 75 | /// The hand-off question, in the person's language. |
| 76 | pub(crate) fn request(locale: Locale) -> UserInputRequest { |
| 77 | let option = |label: MessageId, description: MessageId| UserInputOption { |
| 78 | label: tr(locale, label).into_owned(), |
| 79 | description: tr(locale, description).into_owned(), |
| 80 | }; |
| 81 | UserInputRequest { |
| 82 | require_typed_answers: false, |
| 83 | questions: vec![UserInputQuestion { |
| 84 | detail: None, |
| 85 | intent: None, |
| 86 | header: tr(locale, MessageId::PlanHandoffHeader).into_owned(), |
| 87 | id: QUESTION_ID.to_string(), |
| 88 | question: tr(locale, MessageId::PlanHandoffQuestion).into_owned(), |
| 89 | options: vec![ |
| 90 | option( |
| 91 | MessageId::PlanHandoffWorkAsk, |
| 92 | MessageId::PlanHandoffWorkAskDetail, |
| 93 | ), |
| 94 | option( |
| 95 | MessageId::PlanHandoffWorkAcceptEdits, |
| 96 | MessageId::PlanHandoffWorkAcceptEditsDetail, |
| 97 | ), |
| 98 | option( |
| 99 | MessageId::PlanHandoffWorkAuto, |
| 100 | MessageId::PlanHandoffWorkAutoDetail, |
| 101 | ), |
| 102 | option( |
| 103 | MessageId::PlanHandoffKeepPlanning, |
| 104 | MessageId::PlanHandoffKeepPlanningDetail, |
| 105 | ), |
| 106 | ], |
| 107 | allow_free_text: true, |
| 108 | multi_select: false, |
| 109 | }], |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /// Read the answer to [`request`]. A typed response is feedback on the plan; |
| 114 | /// anything unrecognized or empty keeps planning, the choice that changes |
| 115 | /// nothing. |
| 116 | pub(crate) fn choice(locale: Locale, response: &UserInputResponse) -> PlanHandoffChoice { |
| 117 | let Some(answer) = response.answers.first() else { |
| 118 | return PlanHandoffChoice::KeepPlanning; |
| 119 | }; |
| 120 | let picked = |id: MessageId| answer.label == tr(locale, id); |
| 121 | if picked(MessageId::PlanHandoffWorkAsk) { |
| 122 | PlanHandoffChoice::Work(ApprovalMode::Suggest) |
| 123 | } else if picked(MessageId::PlanHandoffWorkAcceptEdits) { |
| 124 | PlanHandoffChoice::Work(ApprovalMode::AcceptEdits) |
| 125 | } else if picked(MessageId::PlanHandoffWorkAuto) { |
| 126 | PlanHandoffChoice::Work(ApprovalMode::Auto) |
| 127 | } else if picked(MessageId::PlanHandoffKeepPlanning) { |
| 128 | PlanHandoffChoice::KeepPlanning |
| 129 | } else { |
| 130 | let feedback = answer.value.trim(); |
| 131 | if feedback.is_empty() { |
| 132 | PlanHandoffChoice::KeepPlanning |
| 133 | } else { |
| 134 | PlanHandoffChoice::Revise(feedback.to_string()) |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | #[cfg(test)] |
| 140 | mod tests { |
| 141 | use super::*; |
| 142 | use codewhale_engine::tools::user_input::UserInputAnswer; |
| 143 | |
| 144 | fn answer(label: &str, value: &str) -> UserInputResponse { |
| 145 | UserInputResponse { |
| 146 | answers: vec![UserInputAnswer { |
| 147 | id: QUESTION_ID.to_string(), |
| 148 | label: label.to_string(), |
| 149 | value: value.to_string(), |
| 150 | kind: None, |
| 151 | }], |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | #[test] |
| 156 | fn only_successful_nonempty_plan_output_is_ready() { |
| 157 | let prose = Some("先检查数据。\n\nThen build the report."); |
| 158 | assert!(plan_ready( |
| 159 | AppMode::Plan, |
| 160 | TurnOutcomeStatus::Completed, |
| 161 | prose |
| 162 | )); |
| 163 | assert!(!plan_ready( |
| 164 | AppMode::Agent, |
| 165 | TurnOutcomeStatus::Completed, |
| 166 | prose |
| 167 | )); |
| 168 | for status in [TurnOutcomeStatus::Interrupted, TurnOutcomeStatus::Failed] { |
| 169 | assert!(!plan_ready(AppMode::Plan, status, prose)); |
| 170 | } |
| 171 | for output in [None, Some(""), Some(" \n ")] { |
| 172 | assert!(!plan_ready( |
| 173 | AppMode::Plan, |
| 174 | TurnOutcomeStatus::Completed, |
| 175 | output |
| 176 | )); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | #[test] |
| 181 | fn every_locale_asks_a_valid_question_and_reads_its_own_answers() { |
| 182 | for locale in Locale::shipped_complete() { |
| 183 | let asked = request(*locale); |
| 184 | asked |
| 185 | .validate() |
| 186 | .unwrap_or_else(|error| panic!("{}: {error:?}", locale.tag())); |
| 187 | let labels: Vec<&str> = asked.questions[0] |
| 188 | .options |
| 189 | .iter() |
| 190 | .map(|option| option.label.as_str()) |
| 191 | .collect(); |
| 192 | let expected = [ |
| 193 | PlanHandoffChoice::Work(ApprovalMode::Suggest), |
| 194 | PlanHandoffChoice::Work(ApprovalMode::AcceptEdits), |
| 195 | PlanHandoffChoice::Work(ApprovalMode::Auto), |
| 196 | PlanHandoffChoice::KeepPlanning, |
| 197 | ]; |
| 198 | assert_eq!(labels.len(), expected.len(), "{}", locale.tag()); |
| 199 | for (label, expected) in labels.iter().zip(expected) { |
| 200 | // The modal answers an option with its label as the value. |
| 201 | assert_eq!( |
| 202 | choice(*locale, &answer(label, label)), |
| 203 | expected, |
| 204 | "{} {label}", |
| 205 | locale.tag() |
| 206 | ); |
| 207 | // "Other" is the modal's own free-text row. |
| 208 | assert_ne!(*label, "Other", "{}", locale.tag()); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | #[test] |
| 214 | fn a_typed_response_is_feedback_and_an_empty_one_keeps_planning() { |
| 215 | assert_eq!( |
| 216 | choice(Locale::En, &answer("Other", " cover the tests too ")), |
| 217 | PlanHandoffChoice::Revise("cover the tests too".to_string()) |
| 218 | ); |
| 219 | assert_eq!( |
| 220 | choice(Locale::En, &answer("Other", " ")), |
| 221 | PlanHandoffChoice::KeepPlanning |
| 222 | ); |
| 223 | assert_eq!( |
| 224 | choice(Locale::En, &UserInputResponse { answers: vec![] }), |
| 225 | PlanHandoffChoice::KeepPlanning |
| 226 | ); |
| 227 | } |
| 228 | } |
| 229 |