| 1 | //! `/goal` — codex-style thread goals: set, inspect, pause, resume, and close |
| 2 | //! a durable objective. The engine owns the goal: setting or resuming one |
| 3 | //! starts work through the runtime's continuation steering, never by echoing |
| 4 | //! the objective back as a user message. |
| 5 | //! |
| 6 | //! FEAT-021 converts this handler to the portable command contract: it |
| 7 | //! consumes the typed project goal projection and the presentation facet only. |
| 8 | //! `CommandResult` and the emitted `AppAction` variants (`SetGoalStatus`, |
| 9 | //! `SetGoalObjective`, `SendMessage`) remain temporary TUI-owned data-only |
| 10 | //! references until FEAT-037. |
| 11 | |
| 12 | use codewhale_command_contract::facets::{ |
| 13 | CommandPresentationContext, ProjectGoalState, ProjectGoalStatus, |
| 14 | }; |
| 15 | use codewhale_command_contract::handler::{CommandContexts, CommandHandler}; |
| 16 | use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand}; |
| 17 | |
| 18 | use crate::tui::app::AppAction; |
| 19 | |
| 20 | use crate::commands::CommandResult; |
| 21 | |
| 22 | /// Translate one stable project key through the presentation facet. |
| 23 | fn translate(presentation: &mut dyn CommandPresentationContext, key: &str) -> String { |
| 24 | presentation.translate(key, &[]).unwrap_or_default() |
| 25 | } |
| 26 | |
| 27 | /// Map the portable goal status onto the TUI-owned action payload status. |
| 28 | /// |
| 29 | /// This is the bounded FEAT-037 temporary action reference: the handler only |
| 30 | /// constructs data-only `AppAction` payloads and never touches the goal |
| 31 | /// service, config, or session manager. |
| 32 | fn to_action_status(status: ProjectGoalStatus) -> codewhale_engine::tools::goal::GoalStatus { |
| 33 | match status { |
| 34 | ProjectGoalStatus::Active => codewhale_engine::tools::goal::GoalStatus::Active, |
| 35 | ProjectGoalStatus::Paused => codewhale_engine::tools::goal::GoalStatus::Paused, |
| 36 | ProjectGoalStatus::Complete => codewhale_engine::tools::goal::GoalStatus::Complete, |
| 37 | ProjectGoalStatus::Blocked => codewhale_engine::tools::goal::GoalStatus::Blocked, |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /// Declare, show, pause, resume, or close a goal. |
| 42 | fn goal_command( |
| 43 | goal: &ProjectGoalState, |
| 44 | presentation: &mut dyn CommandPresentationContext, |
| 45 | arg: Option<&str>, |
| 46 | ) -> CommandResult { |
| 47 | match arg { |
| 48 | Some("clear") | Some("reset") => CommandResult::action(AppAction::SetGoalStatus { |
| 49 | status: codewhale_engine::tools::goal::GoalStatus::Active, |
| 50 | clear: true, |
| 51 | }), |
| 52 | Some("done") | Some("complete") => { |
| 53 | close_goal(goal, presentation, ProjectGoalStatus::Complete) |
| 54 | } |
| 55 | Some("pause") | Some("paused") => close_goal(goal, presentation, ProjectGoalStatus::Paused), |
| 56 | Some("resume") | Some("continue") => resume_goal(goal, presentation), |
| 57 | Some("help") | Some("?") | Some("usage") => CommandResult::message(goal_usage()), |
| 58 | Some("status") | Some("show") => goal_status(goal, presentation), |
| 59 | Some("block") | Some("blocked") => { |
| 60 | close_goal(goal, presentation, ProjectGoalStatus::Blocked) |
| 61 | } |
| 62 | Some(text) if !text.is_empty() => { |
| 63 | let (objective, budget) = parse_goal_budget(text); |
| 64 | if objective.is_empty() || objective.chars().all(|c| c == '|') { |
| 65 | return CommandResult::error(goal_usage()); |
| 66 | } |
| 67 | // The command layer never mutates the visible projection. The UI |
| 68 | // first persists and accepts this typed intent; only the engine's |
| 69 | // authoritative GoalUpdated event may change what the user sees. |
| 70 | CommandResult::action(AppAction::SetGoalObjective { |
| 71 | objective, |
| 72 | token_budget: budget, |
| 73 | }) |
| 74 | } |
| 75 | _ => { |
| 76 | if goal.pending_controls { |
| 77 | CommandResult::message(translate(presentation, "goal_control_accepted")) |
| 78 | } else if goal.objective.is_some() { |
| 79 | goal_status(goal, presentation) |
| 80 | } else if !goal.conversation_present { |
| 81 | // Nothing has happened yet: there is no context to derive an |
| 82 | // objective from, so answer with usage instead of spending a |
| 83 | // model turn on a question we already know the answer to. |
| 84 | CommandResult::message(goal_usage()) |
| 85 | } else { |
| 86 | // Context-dependent bare /goal: with no active goal, the |
| 87 | // invocation itself is the ask — derive the objective from |
| 88 | // the conversation instead of demanding a restatement |
| 89 | // (mirrors bare /workflow). The end-of-turn GoalUpdated |
| 90 | // snapshot syncs the created goal into the sidebar. |
| 91 | let message = BARE_GOAL_BRIEF.to_string(); |
| 92 | CommandResult::with_message_and_action( |
| 93 | "Declaring a goal from the current context...", |
| 94 | AppAction::SendMessage(message), |
| 95 | ) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /// Model-facing brief for a context-dependent bare `/goal` (#6747). |
| 102 | pub(crate) const BARE_GOAL_BRIEF: &str = "The user invoked /goal with no objective — declare a goal for the \ |
| 103 | CURRENT work. Synthesize the objective from the conversation context (the \ |
| 104 | task in flight, recent findings, open items) and set it by calling \ |
| 105 | `create_goal` with the full objective (and a token_budget only if one was \ |
| 106 | discussed). Then continue working toward it. Only if the conversation \ |
| 107 | genuinely contains no work yet, ask the user what the goal should be."; |
| 108 | |
| 109 | /// Plain status line: objective, state, elapsed, budget, continuations, and |
| 110 | /// — for an active goal that no turn is driving right now — how to continue. |
| 111 | fn goal_status( |
| 112 | goal: &ProjectGoalState, |
| 113 | presentation: &mut dyn CommandPresentationContext, |
| 114 | ) -> CommandResult { |
| 115 | let Some(obj) = goal.objective.as_deref() else { |
| 116 | return CommandResult::message(goal_usage()); |
| 117 | }; |
| 118 | let elapsed = if goal.time_used_seconds > 0 { |
| 119 | codewhale_command_contract::elapsed::format_elapsed_secs(goal.time_used_seconds) |
| 120 | } else if let Some(secs) = goal.started_at_elapsed_seconds { |
| 121 | codewhale_command_contract::elapsed::format_elapsed_secs(secs) |
| 122 | } else { |
| 123 | "unknown".to_string() |
| 124 | }; |
| 125 | let budget_str = goal |
| 126 | .token_budget |
| 127 | .map(|b| { |
| 128 | let used = if goal.tokens_used > 0 { |
| 129 | goal.tokens_used |
| 130 | } else { |
| 131 | u64::from(goal.session_total_tokens) |
| 132 | }; |
| 133 | let pct = if b > 0 { |
| 134 | (used as f64 / f64::from(b) * 100.0).min(100.0) |
| 135 | } else { |
| 136 | 0.0 |
| 137 | }; |
| 138 | format!(" · tokens {used}/{b} ({pct:.0}%)") |
| 139 | }) |
| 140 | .unwrap_or_default(); |
| 141 | let mut state = goal_status_label(goal.status).to_string(); |
| 142 | if let (ProjectGoalStatus::Paused, Some(reason)) = (goal.status, goal.pause_reason.as_deref()) { |
| 143 | state = format!("{state} ({reason})"); |
| 144 | } |
| 145 | let mut line = format!( |
| 146 | "Goal {state}: \"{obj}\" · elapsed {elapsed}{budget_str} · continuations {}", |
| 147 | goal.continuation_count |
| 148 | ); |
| 149 | if goal.status == ProjectGoalStatus::Active |
| 150 | && !goal.is_loading |
| 151 | && !goal.goal_continuation_waiting |
| 152 | { |
| 153 | line.push_str(" · "); |
| 154 | line.push_str(&translate(presentation, "goal_status_idle_hint")); |
| 155 | } |
| 156 | CommandResult::message(line) |
| 157 | } |
| 158 | |
| 159 | /// Close out the goal at `status`. Pure control plane: the engine stops (or |
| 160 | /// re-arms) the continuation loop from the `SetGoalStatus` op; no model turn |
| 161 | /// is dispatched. |
| 162 | fn close_goal( |
| 163 | goal: &ProjectGoalState, |
| 164 | presentation: &mut dyn CommandPresentationContext, |
| 165 | status: ProjectGoalStatus, |
| 166 | ) -> CommandResult { |
| 167 | if effective_goal_objective(goal).is_none_or(str::is_empty) { |
| 168 | return CommandResult::error("No goal set. Use /goal <objective> [budget: N] first."); |
| 169 | } |
| 170 | if effective_goal_status(goal) == status { |
| 171 | if goal.pending_controls { |
| 172 | return CommandResult::message(translate(presentation, "goal_control_accepted")); |
| 173 | } |
| 174 | return goal_status(goal, presentation); |
| 175 | } |
| 176 | |
| 177 | CommandResult::action(AppAction::SetGoalStatus { |
| 178 | status: to_action_status(status), |
| 179 | clear: false, |
| 180 | }) |
| 181 | } |
| 182 | |
| 183 | /// Resume a paused goal. The engine restarts the continuation loop itself |
| 184 | /// (`SetGoalStatus` → schedule kickoff); the objective is never re-sent as a |
| 185 | /// user message. |
| 186 | fn resume_goal( |
| 187 | goal: &ProjectGoalState, |
| 188 | presentation: &mut dyn CommandPresentationContext, |
| 189 | ) -> CommandResult { |
| 190 | if effective_goal_objective(goal) |
| 191 | .map(str::trim) |
| 192 | .is_none_or(str::is_empty) |
| 193 | { |
| 194 | return CommandResult::error("No paused goal set. Use /goal <objective> first."); |
| 195 | } |
| 196 | |
| 197 | if effective_goal_status(goal) == ProjectGoalStatus::Active { |
| 198 | if goal.pending_controls { |
| 199 | return CommandResult::message(translate(presentation, "goal_control_accepted")); |
| 200 | } |
| 201 | if goal.is_loading || goal.goal_continuation_waiting { |
| 202 | return goal_status(goal, presentation); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | CommandResult::action(AppAction::SetGoalStatus { |
| 207 | status: codewhale_engine::tools::goal::GoalStatus::Active, |
| 208 | clear: false, |
| 209 | }) |
| 210 | } |
| 211 | |
| 212 | fn effective_goal_objective(goal: &ProjectGoalState) -> Option<&str> { |
| 213 | if goal.pending_controls { |
| 214 | goal.last_known_objective.as_deref() |
| 215 | } else { |
| 216 | goal.objective.as_deref() |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | fn effective_goal_status(goal: &ProjectGoalState) -> ProjectGoalStatus { |
| 221 | if goal.pending_controls { |
| 222 | goal.last_known_status.unwrap_or(ProjectGoalStatus::Active) |
| 223 | } else { |
| 224 | goal.status |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | fn goal_usage() -> &'static str { |
| 229 | "No goal set. /goal <objective> [budget: N] starts one; the agent works toward it \ |
| 230 | across turns until it is verified complete, blocked, or you stop it.\n\ |
| 231 | /goal — progress of the current goal\n\ |
| 232 | /goal pause — pause without continuing\n\ |
| 233 | /goal resume — resume and continue\n\ |
| 234 | /goal done — mark complete (skips the model's verification)\n\ |
| 235 | /goal blocked — mark blocked\n\ |
| 236 | /goal clear — remove the current goal." |
| 237 | } |
| 238 | |
| 239 | fn goal_status_label(status: ProjectGoalStatus) -> &'static str { |
| 240 | match status { |
| 241 | ProjectGoalStatus::Active => "active", |
| 242 | ProjectGoalStatus::Complete => "complete", |
| 243 | ProjectGoalStatus::Paused => "paused", |
| 244 | ProjectGoalStatus::Blocked => "blocked", |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /// Parse text like "Implement login | budget: 50000" into (objective, budget). |
| 249 | fn parse_goal_budget(text: &str) -> (String, Option<u32>) { |
| 250 | // Only an explicit, well-formed budget suffix splits the objective. |
| 251 | // `budget:` followed by something that is not a number is prose that |
| 252 | // belongs to the objective — truncating it would silently rewrite what |
| 253 | // the user asked for. |
| 254 | if let Some((objective, rest)) = text.rsplit_once("budget:") |
| 255 | && let Ok(budget) = rest.trim().parse::<u32>() |
| 256 | { |
| 257 | let objective = objective.trim_end(); |
| 258 | let objective = objective.strip_suffix('|').unwrap_or(objective); |
| 259 | return (objective.trim().to_string(), Some(budget)); |
| 260 | } |
| 261 | (text.trim().to_string(), None) |
| 262 | } |
| 263 | |
| 264 | pub(in crate::commands) const GOAL_INFO: CommandInfo = CommandInfo { |
| 265 | name: "goal", |
| 266 | aliases: &[], |
| 267 | usage: "/goal [objective|status|pause|resume|done|blocked|clear] [budget: N]", |
| 268 | description_key: "cmd_goal_description", |
| 269 | }; |
| 270 | |
| 271 | pub(in crate::commands) struct GoalCmd; |
| 272 | |
| 273 | impl RegisterCommand<CommandResult> for GoalCmd { |
| 274 | fn info() -> &'static CommandInfo { |
| 275 | &GOAL_INFO |
| 276 | } |
| 277 | |
| 278 | fn handler() -> CommandHandler<CommandResult> { |
| 279 | CommandHandler::Contextual { |
| 280 | capabilities: codewhale_command_contract::handler::CommandCapabilities::PROJECT |
| 281 | .union(codewhale_command_contract::handler::CommandCapabilities::PRESENTATION), |
| 282 | handler: goal_contextual, |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /// Contextual `/goal` dispatch (FEAT-021 Phase 4). |
| 288 | /// |
| 289 | /// Destructures the declared `PROJECT | PRESENTATION` facets with safe |
| 290 | /// missing-facet errors; the portable handler never panics on absent |
| 291 | /// capabilities and never emits a partial action. |
| 292 | fn goal_contextual(contexts: CommandContexts<'_>, arg: Option<&str>) -> CommandResult { |
| 293 | let mut parts = contexts.into_parts(); |
| 294 | let Some(project) = parts.project.as_deref_mut() else { |
| 295 | return CommandResult::error("Command capability unavailable: project"); |
| 296 | }; |
| 297 | let Some(presentation) = parts.presentation.as_deref_mut() else { |
| 298 | return CommandResult::error("Command capability unavailable: presentation"); |
| 299 | }; |
| 300 | let goal = project.goal_state(); |
| 301 | goal_command(&goal, presentation, arg) |
| 302 | } |
| 303 | |
| 304 | #[cfg(test)] |
| 305 | mod tests { |
| 306 | /// #6747: the bare /goal brief names only callable tools. |
| 307 | #[test] |
| 308 | fn bare_goal_brief_names_only_callable_tools() { |
| 309 | codewhale_engine::tools::canonical_action_test_support::assert_text_names_only_callable_tools( |
| 310 | "bare /goal brief", |
| 311 | BARE_GOAL_BRIEF, |
| 312 | ); |
| 313 | } |
| 314 | |
| 315 | use super::*; |
| 316 | use codewhale_command_contract::facets::CommandProjectContext; |
| 317 | |
| 318 | /// Deterministic fake project facet over portable values only. |
| 319 | struct FakeProject; |
| 320 | |
| 321 | impl FakeProject { |
| 322 | fn new() -> Self { |
| 323 | Self |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | impl CommandProjectContext for FakeProject { |
| 328 | fn lsp_status(&self) -> String { |
| 329 | String::new() |
| 330 | } |
| 331 | |
| 332 | fn lsp_approve(&mut self, _names: &[&str]) -> Result<String, String> { |
| 333 | Ok(String::new()) |
| 334 | } |
| 335 | |
| 336 | fn lsp_revoke(&mut self, _names: &[&str]) -> Result<String, String> { |
| 337 | Ok(String::new()) |
| 338 | } |
| 339 | |
| 340 | fn goal_state(&self) -> ProjectGoalState { |
| 341 | goal_state() |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /// Deterministic fake presentation facet over portable values only. |
| 346 | struct FakePresentation; |
| 347 | |
| 348 | impl CommandPresentationContext for FakePresentation { |
| 349 | fn translate(&self, key: &str, _replacements: &[(&str, &str)]) -> Result<String, String> { |
| 350 | match key { |
| 351 | "goal_control_accepted" => { |
| 352 | Ok("Goal control saved; applying at the next safe boundary.".to_string()) |
| 353 | } |
| 354 | "goal_status_idle_hint" => { |
| 355 | Ok("not running now — send a message or /goal resume to continue".to_string()) |
| 356 | } |
| 357 | other => Err(format!("unknown key {other}")), |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | fn goal_state() -> ProjectGoalState { |
| 363 | ProjectGoalState { |
| 364 | objective: None, |
| 365 | status: ProjectGoalStatus::Active, |
| 366 | pause_reason: None, |
| 367 | started_at_elapsed_seconds: None, |
| 368 | time_used_seconds: 0, |
| 369 | token_budget: None, |
| 370 | tokens_used: 0, |
| 371 | session_total_tokens: 0, |
| 372 | continuation_count: 0, |
| 373 | pending_controls: false, |
| 374 | last_known_objective: None, |
| 375 | last_known_status: None, |
| 376 | conversation_present: false, |
| 377 | is_loading: false, |
| 378 | goal_continuation_waiting: false, |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | fn run(goal: &ProjectGoalState, arg: Option<&str>) -> CommandResult { |
| 383 | let mut presentation = FakePresentation; |
| 384 | goal_command(goal, &mut presentation, arg) |
| 385 | } |
| 386 | |
| 387 | #[test] |
| 388 | fn test_set_goal_dispatches_control_plane_not_user_echo() { |
| 389 | let goal = goal_state(); |
| 390 | let result = run(&goal, Some("Fix the login bug")); |
| 391 | assert!(result.message.is_none()); |
| 392 | // The engine owns the kickoff: the objective must reach it as a |
| 393 | // SetGoalObjective control op, never as a SendMessage user echo. |
| 394 | assert!(matches!( |
| 395 | result.action, |
| 396 | Some(AppAction::SetGoalObjective { ref objective, token_budget: None }) |
| 397 | if objective == "Fix the login bug" |
| 398 | )); |
| 399 | } |
| 400 | |
| 401 | #[test] |
| 402 | fn test_goal_budget_parsing_reaches_the_op() { |
| 403 | let goal = goal_state(); |
| 404 | let result = run(&goal, Some("Ship 0.9.10 | budget: 5000")); |
| 405 | assert!(matches!( |
| 406 | result.action, |
| 407 | Some(AppAction::SetGoalObjective { ref objective, token_budget: Some(5000) }) |
| 408 | if objective == "Ship 0.9.10" |
| 409 | )); |
| 410 | } |
| 411 | |
| 412 | #[test] |
| 413 | fn pause_and_clear_are_control_ops_without_optimistic_state() { |
| 414 | let mut goal = goal_state(); |
| 415 | goal.objective = Some("Keep the build green".to_string()); |
| 416 | goal.status = ProjectGoalStatus::Active; |
| 417 | let paused = run(&goal, Some("pause")); |
| 418 | assert!(paused.message.is_none()); |
| 419 | assert!(matches!( |
| 420 | paused.action, |
| 421 | Some(AppAction::SetGoalStatus { |
| 422 | status: codewhale_engine::tools::goal::GoalStatus::Paused, |
| 423 | clear: false |
| 424 | }) |
| 425 | )); |
| 426 | |
| 427 | let cleared = run(&goal, Some("clear")); |
| 428 | assert!(cleared.message.is_none()); |
| 429 | assert!(matches!( |
| 430 | cleared.action, |
| 431 | Some(AppAction::SetGoalStatus { |
| 432 | status: codewhale_engine::tools::goal::GoalStatus::Active, |
| 433 | clear: true |
| 434 | }) |
| 435 | )); |
| 436 | } |
| 437 | |
| 438 | #[test] |
| 439 | fn test_goal_without_argument_synthesizes_goal_from_context() { |
| 440 | // Bare /goal with no active goal is context-dependent: the model |
| 441 | // derives the objective from the conversation and sets it via |
| 442 | // create_goal — it must not error with a usage demand. |
| 443 | let mut goal = goal_state(); |
| 444 | goal.conversation_present = true; |
| 445 | let result = run(&goal, None); |
| 446 | assert!(!result.is_error); |
| 447 | let Some(AppAction::SendMessage(message)) = result.action else { |
| 448 | panic!("expected SendMessage action"); |
| 449 | }; |
| 450 | assert!(message.contains("Synthesize the objective from the conversation")); |
| 451 | assert!(message.contains("`create_goal`")); |
| 452 | } |
| 453 | |
| 454 | #[test] |
| 455 | fn bare_goal_on_an_empty_session_prints_usage_without_a_model_turn() { |
| 456 | // No conversation yet: there is nothing to derive an objective from, |
| 457 | // so the answer is usage — free, and not a question to the model. |
| 458 | let goal = goal_state(); |
| 459 | let result = run(&goal, None); |
| 460 | assert!(!result.is_error); |
| 461 | assert!(result.action.is_none()); |
| 462 | assert!(result.message.unwrap().contains("/goal <objective>")); |
| 463 | } |
| 464 | |
| 465 | #[test] |
| 466 | fn goal_status_reports_objective_and_state() { |
| 467 | let mut goal = goal_state(); |
| 468 | goal.objective = Some("Make the suite green".to_string()); |
| 469 | goal.token_budget = Some(100); |
| 470 | goal.status = ProjectGoalStatus::Active; |
| 471 | let result = run(&goal, Some("status")); |
| 472 | let line = result.message.unwrap(); |
| 473 | assert!(line.contains("Make the suite green")); |
| 474 | assert!(line.contains("active")); |
| 475 | assert!(line.contains("100")); |
| 476 | } |
| 477 | |
| 478 | #[test] |
| 479 | fn goal_status_includes_elapsed_and_continuations() { |
| 480 | let mut goal = goal_state(); |
| 481 | goal.objective = Some("Ship it".to_string()); |
| 482 | goal.status = ProjectGoalStatus::Active; |
| 483 | goal.time_used_seconds = 125; |
| 484 | goal.continuation_count = 3; |
| 485 | let result = run(&goal, Some("status")); |
| 486 | let line = result.message.unwrap(); |
| 487 | assert!(line.contains("elapsed 2m 05s"), "line: {line}"); |
| 488 | assert!(line.contains("continuations 3"), "line: {line}"); |
| 489 | } |
| 490 | |
| 491 | #[test] |
| 492 | fn paused_goal_status_shows_reason() { |
| 493 | let mut goal = goal_state(); |
| 494 | goal.objective = Some("Ship it".to_string()); |
| 495 | goal.status = ProjectGoalStatus::Paused; |
| 496 | goal.pause_reason = Some("usage limit".to_string()); |
| 497 | let result = run(&goal, Some("status")); |
| 498 | assert!(result.message.unwrap().contains("paused (usage limit)")); |
| 499 | } |
| 500 | |
| 501 | #[test] |
| 502 | fn resume_on_an_active_goal_only_kicks_when_idle() { |
| 503 | let mut goal = goal_state(); |
| 504 | goal.objective = Some("Keep the build green".to_string()); |
| 505 | goal.status = ProjectGoalStatus::Active; |
| 506 | for (is_loading, waiting) in [(false, false), (true, false), (false, true)] { |
| 507 | goal.is_loading = is_loading; |
| 508 | goal.goal_continuation_waiting = waiting; |
| 509 | let resumed = run(&goal, Some("resume")); |
| 510 | assert!(!resumed.is_error); |
| 511 | if is_loading || waiting { |
| 512 | assert!(resumed.action.is_none()); |
| 513 | assert!(resumed.message.unwrap().contains("Keep the build green")); |
| 514 | } else { |
| 515 | assert!(matches!( |
| 516 | resumed.action, |
| 517 | Some(AppAction::SetGoalStatus { |
| 518 | status: codewhale_engine::tools::goal::GoalStatus::Active, |
| 519 | clear: false |
| 520 | }) |
| 521 | )); |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | #[test] |
| 527 | fn only_a_whole_numeric_final_budget_suffix_is_a_budget() { |
| 528 | for (input, expected_objective, expected_budget) in [ |
| 529 | ( |
| 530 | "Keep the budget: 5 engineers and ship login", |
| 531 | "Keep the budget: 5 engineers and ship login", |
| 532 | None, |
| 533 | ), |
| 534 | ( |
| 535 | "Fix budget: label budget: 5000", |
| 536 | "Fix budget: label", |
| 537 | Some(5000), |
| 538 | ), |
| 539 | ( |
| 540 | "Ship it budget: 99999999999", |
| 541 | "Ship it budget: 99999999999", |
| 542 | None, |
| 543 | ), |
| 544 | ("Ship 0.9.10 | budget:5000 ", "Ship 0.9.10", Some(5000)), |
| 545 | ] { |
| 546 | assert_eq!( |
| 547 | parse_goal_budget(input), |
| 548 | (expected_objective.to_string(), expected_budget), |
| 549 | "{input:?}" |
| 550 | ); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | #[test] |
| 555 | fn invalid_budget_suffix_stays_part_of_the_objective() { |
| 556 | let goal = goal_state(); |
| 557 | let result = run(&goal, Some("Fix budget: handling in settings")); |
| 558 | assert!(matches!( |
| 559 | result.action, |
| 560 | Some(AppAction::SetGoalObjective { ref objective, token_budget: None }) |
| 561 | if objective == "Fix budget: handling in settings" |
| 562 | )); |
| 563 | } |
| 564 | |
| 565 | #[test] |
| 566 | fn completing_a_goal_without_one_is_an_error() { |
| 567 | let goal = goal_state(); |
| 568 | let result = run(&goal, Some("done")); |
| 569 | assert!(result.is_error); |
| 570 | } |
| 571 | |
| 572 | #[test] |
| 573 | fn goal_help_route_returns_usage() { |
| 574 | let goal = goal_state(); |
| 575 | for arg in ["help", "?", "usage"] { |
| 576 | let result = run(&goal, Some(arg)); |
| 577 | assert!(!result.is_error); |
| 578 | assert!(result.message.unwrap().contains("/goal <objective>")); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | #[test] |
| 583 | fn missing_project_facet_fails_safely() { |
| 584 | let result = goal_contextual(CommandContexts::empty(), Some("status")); |
| 585 | assert!(result.is_error); |
| 586 | assert!( |
| 587 | result |
| 588 | .message |
| 589 | .unwrap() |
| 590 | .contains("Command capability unavailable: project") |
| 591 | ); |
| 592 | } |
| 593 | |
| 594 | #[test] |
| 595 | fn missing_presentation_facet_fails_safely() { |
| 596 | // PROJECT present but PRESENTATION absent: safe error, no partial action. |
| 597 | let mut project = FakeProject::new(); |
| 598 | let contexts = CommandContexts::empty().with_project(&mut project); |
| 599 | let result = goal_contextual(contexts, Some("status")); |
| 600 | assert!(result.is_error); |
| 601 | assert!( |
| 602 | result.action.is_none(), |
| 603 | "no partial action on missing facet" |
| 604 | ); |
| 605 | assert!( |
| 606 | result |
| 607 | .message |
| 608 | .unwrap() |
| 609 | .contains("Command capability unavailable: presentation") |
| 610 | ); |
| 611 | } |
| 612 | |
| 613 | #[test] |
| 614 | fn goal_control_accepted_translates_through_fake_presentation() { |
| 615 | // Pending controls route to the translated message, not the raw key. |
| 616 | let mut goal = goal_state(); |
| 617 | goal.objective = Some("Keep the build green".to_string()); |
| 618 | goal.status = ProjectGoalStatus::Active; |
| 619 | goal.pending_controls = true; |
| 620 | goal.last_known_objective = Some("Keep the build green".to_string()); |
| 621 | goal.last_known_status = Some(ProjectGoalStatus::Paused); |
| 622 | let result = run(&goal, Some("pause")); |
| 623 | assert!(!result.is_error); |
| 624 | let msg = result.message.expect("pending-control message"); |
| 625 | assert!(msg.contains("Goal control saved")); |
| 626 | assert!( |
| 627 | !msg.contains("goal_control_accepted"), |
| 628 | "raw key must not leak" |
| 629 | ); |
| 630 | } |
| 631 | |
| 632 | #[test] |
| 633 | fn idle_hint_translates_through_fake_presentation() { |
| 634 | // Active goal not being driven: the idle hint is the translated text. |
| 635 | let mut goal = goal_state(); |
| 636 | goal.objective = Some("Ship it".to_string()); |
| 637 | goal.status = ProjectGoalStatus::Active; |
| 638 | goal.is_loading = false; |
| 639 | goal.goal_continuation_waiting = false; |
| 640 | let result = run(&goal, Some("status")); |
| 641 | let line = result.message.unwrap(); |
| 642 | assert!( |
| 643 | line.contains("not running now"), |
| 644 | "idle hint must be translated: {line}" |
| 645 | ); |
| 646 | assert!( |
| 647 | !line.contains("goal_status_idle_hint"), |
| 648 | "raw key must not leak" |
| 649 | ); |
| 650 | } |
| 651 | |
| 652 | #[test] |
| 653 | fn goal_token_fallback_uses_session_total() { |
| 654 | // tokens_used == 0 falls back to the session conversation-token total. |
| 655 | let mut goal = goal_state(); |
| 656 | goal.objective = Some("Ship it".to_string()); |
| 657 | goal.token_budget = Some(100); |
| 658 | goal.tokens_used = 0; |
| 659 | goal.session_total_tokens = 42; |
| 660 | let result = run(&goal, Some("status")); |
| 661 | let line = result.message.unwrap(); |
| 662 | assert!(line.contains("tokens 42/100 (42%)"), "line: {line}"); |
| 663 | } |
| 664 | |
| 665 | #[test] |
| 666 | fn goal_token_uses_engine_count_when_nonzero() { |
| 667 | let mut goal = goal_state(); |
| 668 | goal.objective = Some("Ship it".to_string()); |
| 669 | goal.token_budget = Some(100); |
| 670 | goal.tokens_used = 10; |
| 671 | goal.session_total_tokens = 42; |
| 672 | let result = run(&goal, Some("status")); |
| 673 | let line = result.message.unwrap(); |
| 674 | assert!(line.contains("tokens 10/100 (10%)"), "line: {line}"); |
| 675 | } |
| 676 | } |
| 677 |