| 1 | //! Plan tool implementation with step tracking and validation |
| 2 | |
| 3 | use std::sync::Arc; |
| 4 | use std::time::Instant; |
| 5 | use tokio::sync::Mutex; |
| 6 | |
| 7 | use serde::{Deserialize, Serialize}; |
| 8 | |
| 9 | // === Types === |
| 10 | |
| 11 | /// Status of a plan step. |
| 12 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] |
| 13 | #[serde(rename_all = "snake_case")] |
| 14 | pub enum StepStatus { |
| 15 | Pending, |
| 16 | InProgress, |
| 17 | Completed, |
| 18 | } |
| 19 | |
| 20 | impl StepStatus { |
| 21 | #[must_use] |
| 22 | pub(crate) fn from_str(value: &str) -> Option<Self> { |
| 23 | match value.trim().to_lowercase().as_str() { |
| 24 | "pending" => Some(StepStatus::Pending), |
| 25 | "in_progress" | "inprogress" => Some(StepStatus::InProgress), |
| 26 | "completed" | "done" => Some(StepStatus::Completed), |
| 27 | _ => None, |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /// Input representation for a plan item. |
| 33 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] |
| 34 | pub struct PlanItemArg { |
| 35 | pub step: String, |
| 36 | pub status: StepStatus, |
| 37 | } |
| 38 | |
| 39 | /// Update payload used by the plan tool. |
| 40 | #[derive(Debug, Clone, Default, Serialize, Deserialize)] |
| 41 | pub struct UpdatePlanArgs { |
| 42 | #[serde(default)] |
| 43 | pub title: Option<String>, |
| 44 | #[serde(default)] |
| 45 | pub objective: Option<String>, |
| 46 | #[serde(default)] |
| 47 | pub context_summary: Option<String>, |
| 48 | #[serde(default)] |
| 49 | pub explanation: Option<String>, |
| 50 | #[serde(default)] |
| 51 | pub sources_used: Vec<String>, |
| 52 | #[serde(default)] |
| 53 | pub critical_files: Vec<String>, |
| 54 | #[serde(default)] |
| 55 | pub constraints: Vec<String>, |
| 56 | #[serde(default)] |
| 57 | pub recommended_approach: Option<String>, |
| 58 | #[serde(default)] |
| 59 | pub verification_plan: Option<String>, |
| 60 | #[serde(default)] |
| 61 | pub risks_and_unknowns: Option<String>, |
| 62 | #[serde(default)] |
| 63 | pub handoff_packet: Option<String>, |
| 64 | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 65 | pub replan_if: Vec<String>, |
| 66 | #[serde(default)] |
| 67 | pub plan: Vec<PlanItemArg>, |
| 68 | } |
| 69 | |
| 70 | // === Plan State === |
| 71 | |
| 72 | /// A plan step with timing information |
| 73 | #[derive(Debug, Clone)] |
| 74 | pub struct PlanStep { |
| 75 | pub text: String, |
| 76 | pub status: StepStatus, |
| 77 | /// When the step was started (transitioned to `InProgress`) |
| 78 | pub started_at: Option<Instant>, |
| 79 | /// When the step was completed |
| 80 | pub completed_at: Option<Instant>, |
| 81 | } |
| 82 | |
| 83 | impl PlanStep { |
| 84 | /// Create a new plan step. |
| 85 | pub fn new(text: String, status: StepStatus) -> Self { |
| 86 | Self { |
| 87 | text, |
| 88 | status, |
| 89 | started_at: None, |
| 90 | completed_at: None, |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /// Serializable snapshot for display |
| 96 | #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)] |
| 97 | pub struct PlanSnapshot { |
| 98 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 99 | pub title: Option<String>, |
| 100 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 101 | pub objective: Option<String>, |
| 102 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 103 | pub context_summary: Option<String>, |
| 104 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 105 | pub explanation: Option<String>, |
| 106 | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 107 | pub sources_used: Vec<String>, |
| 108 | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 109 | pub critical_files: Vec<String>, |
| 110 | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 111 | pub constraints: Vec<String>, |
| 112 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 113 | pub recommended_approach: Option<String>, |
| 114 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 115 | pub verification_plan: Option<String>, |
| 116 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 117 | pub risks_and_unknowns: Option<String>, |
| 118 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 119 | pub handoff_packet: Option<String>, |
| 120 | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 121 | pub replan_if: Vec<String>, |
| 122 | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 123 | pub items: Vec<PlanItemArg>, |
| 124 | } |
| 125 | |
| 126 | impl PlanSnapshot { |
| 127 | #[must_use] |
| 128 | pub fn is_empty(&self) -> bool { |
| 129 | self.title.is_none() |
| 130 | && self.objective.is_none() |
| 131 | && self.context_summary.is_none() |
| 132 | && self.explanation.is_none() |
| 133 | && self.sources_used.is_empty() |
| 134 | && self.critical_files.is_empty() |
| 135 | && self.constraints.is_empty() |
| 136 | && self.recommended_approach.is_none() |
| 137 | && self.verification_plan.is_none() |
| 138 | && self.risks_and_unknowns.is_none() |
| 139 | && self.handoff_packet.is_none() |
| 140 | && self.replan_if.is_empty() |
| 141 | && self.items.is_empty() |
| 142 | } |
| 143 | |
| 144 | /// Parse the user/model-facing `update_plan` payload into a displayable |
| 145 | /// snapshot. This is intentionally tolerant so saved transcript replay can |
| 146 | /// keep legacy and partially streamed payloads visible. |
| 147 | #[must_use] |
| 148 | pub fn from_tool_input(input: &serde_json::Value) -> Self { |
| 149 | let mut items = Vec::new(); |
| 150 | if let Some(plan_items) = input.get("plan").and_then(|v| v.as_array()) { |
| 151 | for item in plan_items { |
| 152 | let step = item |
| 153 | .get("step") |
| 154 | .and_then(|v| v.as_str()) |
| 155 | .map(str::trim) |
| 156 | .unwrap_or(""); |
| 157 | if step.is_empty() { |
| 158 | continue; |
| 159 | } |
| 160 | let status = item |
| 161 | .get("status") |
| 162 | .and_then(|v| v.as_str()) |
| 163 | .and_then(StepStatus::from_str) |
| 164 | .unwrap_or(StepStatus::Pending); |
| 165 | items.push(PlanItemArg { |
| 166 | step: step.to_string(), |
| 167 | status, |
| 168 | }); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | Self { |
| 173 | title: clean_optional(string_field(input, "title")), |
| 174 | objective: clean_optional(string_field(input, "objective")), |
| 175 | context_summary: clean_optional(string_field(input, "context_summary")), |
| 176 | explanation: clean_optional(string_field(input, "explanation")), |
| 177 | sources_used: clean_list(string_vec_field(input, "sources_used")), |
| 178 | critical_files: clean_list(string_vec_field(input, "critical_files")), |
| 179 | constraints: clean_list(string_vec_field(input, "constraints")), |
| 180 | recommended_approach: clean_optional(string_field(input, "recommended_approach")), |
| 181 | verification_plan: clean_optional(string_field(input, "verification_plan")), |
| 182 | risks_and_unknowns: clean_optional(string_field(input, "risks_and_unknowns")), |
| 183 | handoff_packet: clean_optional(string_field(input, "handoff_packet")), |
| 184 | replan_if: clean_list(string_vec_field(input, "replan_if")), |
| 185 | items, |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /// State tracking for the current plan |
| 191 | #[derive(Debug, Clone, Default)] |
| 192 | pub struct PlanState { |
| 193 | title: Option<String>, |
| 194 | objective: Option<String>, |
| 195 | context_summary: Option<String>, |
| 196 | explanation: Option<String>, |
| 197 | sources_used: Vec<String>, |
| 198 | critical_files: Vec<String>, |
| 199 | constraints: Vec<String>, |
| 200 | recommended_approach: Option<String>, |
| 201 | verification_plan: Option<String>, |
| 202 | risks_and_unknowns: Option<String>, |
| 203 | handoff_packet: Option<String>, |
| 204 | replan_if: Vec<String>, |
| 205 | steps: Vec<PlanStep>, |
| 206 | } |
| 207 | |
| 208 | impl PlanState { |
| 209 | pub fn update(&mut self, args: UpdatePlanArgs) { |
| 210 | self.title = clean_optional(args.title); |
| 211 | self.objective = clean_optional(args.objective); |
| 212 | self.context_summary = clean_optional(args.context_summary); |
| 213 | self.explanation = clean_optional(args.explanation); |
| 214 | self.sources_used = clean_list(args.sources_used); |
| 215 | self.critical_files = clean_list(args.critical_files); |
| 216 | self.constraints = clean_list(args.constraints); |
| 217 | self.recommended_approach = clean_optional(args.recommended_approach); |
| 218 | self.verification_plan = clean_optional(args.verification_plan); |
| 219 | self.risks_and_unknowns = clean_optional(args.risks_and_unknowns); |
| 220 | self.handoff_packet = clean_optional(args.handoff_packet); |
| 221 | self.replan_if = clean_list(args.replan_if); |
| 222 | |
| 223 | let now = Instant::now(); |
| 224 | let mut new_steps = Vec::new(); |
| 225 | let mut in_progress_seen = false; |
| 226 | |
| 227 | for item in args.plan { |
| 228 | let step_text = item.step.trim(); |
| 229 | if step_text.is_empty() { |
| 230 | continue; |
| 231 | } |
| 232 | // Try to find existing step to preserve timing |
| 233 | let existing = self.steps.iter().find(|s| s.text == step_text); |
| 234 | |
| 235 | let mut status = item.status; |
| 236 | // Enforce single in_progress |
| 237 | if status == StepStatus::InProgress { |
| 238 | if in_progress_seen { |
| 239 | status = StepStatus::Pending; |
| 240 | } else { |
| 241 | in_progress_seen = true; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | let step = if let Some(old) = existing { |
| 246 | let mut s = old.clone(); |
| 247 | let old_status = s.status.clone(); |
| 248 | s.status = status.clone(); |
| 249 | |
| 250 | // Track timing transitions |
| 251 | if old_status == StepStatus::Pending && status == StepStatus::InProgress { |
| 252 | s.started_at = Some(now); |
| 253 | } |
| 254 | if old_status == StepStatus::InProgress && status == StepStatus::Completed { |
| 255 | s.completed_at = Some(now); |
| 256 | } |
| 257 | |
| 258 | s |
| 259 | } else { |
| 260 | let mut s = PlanStep::new(step_text.to_string(), status.clone()); |
| 261 | if status == StepStatus::InProgress { |
| 262 | s.started_at = Some(now); |
| 263 | } |
| 264 | s |
| 265 | }; |
| 266 | |
| 267 | new_steps.push(step); |
| 268 | } |
| 269 | |
| 270 | self.steps = new_steps; |
| 271 | } |
| 272 | |
| 273 | pub fn snapshot(&self) -> PlanSnapshot { |
| 274 | PlanSnapshot { |
| 275 | title: self.title.clone(), |
| 276 | objective: self.objective.clone(), |
| 277 | context_summary: self.context_summary.clone(), |
| 278 | explanation: self.explanation.clone(), |
| 279 | sources_used: self.sources_used.clone(), |
| 280 | critical_files: self.critical_files.clone(), |
| 281 | constraints: self.constraints.clone(), |
| 282 | recommended_approach: self.recommended_approach.clone(), |
| 283 | verification_plan: self.verification_plan.clone(), |
| 284 | risks_and_unknowns: self.risks_and_unknowns.clone(), |
| 285 | handoff_packet: self.handoff_packet.clone(), |
| 286 | replan_if: self.replan_if.clone(), |
| 287 | items: self |
| 288 | .steps |
| 289 | .iter() |
| 290 | .map(|s| PlanItemArg { |
| 291 | step: s.text.clone(), |
| 292 | status: s.status.clone(), |
| 293 | }) |
| 294 | .collect(), |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /// Restore persisted plan data through the same normalization path used by |
| 299 | /// `update_plan`. Timing is intentionally session-local and starts fresh. |
| 300 | #[must_use] |
| 301 | pub fn from_snapshot(snapshot: &PlanSnapshot) -> Self { |
| 302 | let mut state = Self::default(); |
| 303 | state.update(UpdatePlanArgs { |
| 304 | title: snapshot.title.clone(), |
| 305 | objective: snapshot.objective.clone(), |
| 306 | context_summary: snapshot.context_summary.clone(), |
| 307 | explanation: snapshot.explanation.clone(), |
| 308 | sources_used: snapshot.sources_used.clone(), |
| 309 | critical_files: snapshot.critical_files.clone(), |
| 310 | constraints: snapshot.constraints.clone(), |
| 311 | recommended_approach: snapshot.recommended_approach.clone(), |
| 312 | verification_plan: snapshot.verification_plan.clone(), |
| 313 | risks_and_unknowns: snapshot.risks_and_unknowns.clone(), |
| 314 | handoff_packet: snapshot.handoff_packet.clone(), |
| 315 | replan_if: snapshot.replan_if.clone(), |
| 316 | plan: snapshot.items.clone(), |
| 317 | }); |
| 318 | state |
| 319 | } |
| 320 | |
| 321 | /// Get counts of steps by status |
| 322 | pub fn counts(&self) -> (usize, usize, usize) { |
| 323 | let mut pending = 0; |
| 324 | let mut in_progress = 0; |
| 325 | let mut completed = 0; |
| 326 | for s in &self.steps { |
| 327 | match s.status { |
| 328 | StepStatus::Pending => pending += 1, |
| 329 | StepStatus::InProgress => in_progress += 1, |
| 330 | StepStatus::Completed => completed += 1, |
| 331 | } |
| 332 | } |
| 333 | (pending, in_progress, completed) |
| 334 | } |
| 335 | |
| 336 | /// Get progress as a percentage |
| 337 | pub fn progress_percent(&self) -> u8 { |
| 338 | if self.steps.is_empty() { |
| 339 | return 0; |
| 340 | } |
| 341 | let completed = self |
| 342 | .steps |
| 343 | .iter() |
| 344 | .filter(|s| s.status == StepStatus::Completed) |
| 345 | .count(); |
| 346 | let percent = completed.saturating_mul(100) / self.steps.len(); |
| 347 | u8::try_from(percent).unwrap_or(u8::MAX) |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | fn clean_optional(value: Option<String>) -> Option<String> { |
| 352 | value |
| 353 | .map(|s| s.trim().to_string()) |
| 354 | .filter(|s| !s.is_empty()) |
| 355 | } |
| 356 | |
| 357 | fn clean_list(values: Vec<String>) -> Vec<String> { |
| 358 | values |
| 359 | .into_iter() |
| 360 | .map(|value| value.trim().to_string()) |
| 361 | .filter(|value| !value.is_empty()) |
| 362 | .collect() |
| 363 | } |
| 364 | |
| 365 | /// Shared reference to `PlanState` for use across runtime and history views. |
| 366 | pub type SharedPlanState = Arc<Mutex<PlanState>>; |
| 367 | |
| 368 | /// Create a new shared `PlanState` |
| 369 | pub fn new_shared_plan_state() -> SharedPlanState { |
| 370 | Arc::new(Mutex::new(PlanState::default())) |
| 371 | } |
| 372 | |
| 373 | fn string_field(input: &serde_json::Value, field: &str) -> Option<String> { |
| 374 | input |
| 375 | .get(field) |
| 376 | .and_then(|v| v.as_str()) |
| 377 | .map(std::string::ToString::to_string) |
| 378 | } |
| 379 | |
| 380 | fn string_vec_field(input: &serde_json::Value, field: &str) -> Vec<String> { |
| 381 | input |
| 382 | .get(field) |
| 383 | .and_then(|v| v.as_array()) |
| 384 | .map(|values| { |
| 385 | values |
| 386 | .iter() |
| 387 | .filter_map(|value| value.as_str().map(std::string::ToString::to_string)) |
| 388 | .collect() |
| 389 | }) |
| 390 | .unwrap_or_default() |
| 391 | } |
| 392 | |
| 393 | #[cfg(test)] |
| 394 | mod tests { |
| 395 | use super::*; |
| 396 | use serde_json::json; |
| 397 | |
| 398 | #[test] |
| 399 | fn plan_state_treats_every_artifact_field_as_non_empty() { |
| 400 | let cases = vec![ |
| 401 | UpdatePlanArgs { |
| 402 | title: Some("Title".to_string()), |
| 403 | ..UpdatePlanArgs::default() |
| 404 | }, |
| 405 | UpdatePlanArgs { |
| 406 | objective: Some("Objective".to_string()), |
| 407 | ..UpdatePlanArgs::default() |
| 408 | }, |
| 409 | UpdatePlanArgs { |
| 410 | context_summary: Some("Context".to_string()), |
| 411 | ..UpdatePlanArgs::default() |
| 412 | }, |
| 413 | UpdatePlanArgs { |
| 414 | explanation: Some("Explanation".to_string()), |
| 415 | ..UpdatePlanArgs::default() |
| 416 | }, |
| 417 | UpdatePlanArgs { |
| 418 | sources_used: vec!["gh issue view 2691".to_string()], |
| 419 | ..UpdatePlanArgs::default() |
| 420 | }, |
| 421 | UpdatePlanArgs { |
| 422 | critical_files: vec!["crates/engine/src/tools/plan.rs".to_string()], |
| 423 | ..UpdatePlanArgs::default() |
| 424 | }, |
| 425 | UpdatePlanArgs { |
| 426 | constraints: vec!["Preserve legacy payloads".to_string()], |
| 427 | ..UpdatePlanArgs::default() |
| 428 | }, |
| 429 | UpdatePlanArgs { |
| 430 | recommended_approach: Some("Do the narrow slice".to_string()), |
| 431 | ..UpdatePlanArgs::default() |
| 432 | }, |
| 433 | UpdatePlanArgs { |
| 434 | verification_plan: Some("Run focused tests".to_string()), |
| 435 | ..UpdatePlanArgs::default() |
| 436 | }, |
| 437 | UpdatePlanArgs { |
| 438 | risks_and_unknowns: Some("Replay may drift".to_string()), |
| 439 | ..UpdatePlanArgs::default() |
| 440 | }, |
| 441 | UpdatePlanArgs { |
| 442 | handoff_packet: Some("Next agent should inspect rendering".to_string()), |
| 443 | ..UpdatePlanArgs::default() |
| 444 | }, |
| 445 | ]; |
| 446 | |
| 447 | for args in cases { |
| 448 | let mut state = PlanState::default(); |
| 449 | state.update(args); |
| 450 | assert!( |
| 451 | !state.snapshot().is_empty(), |
| 452 | "artifact metadata must keep plan state visible" |
| 453 | ); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | #[test] |
| 458 | fn plan_state_snapshot_trims_blank_artifact_values() { |
| 459 | let mut state = PlanState::default(); |
| 460 | state.update(UpdatePlanArgs { |
| 461 | title: Some(" Rich plan ".to_string()), |
| 462 | sources_used: vec![" ".to_string(), " gh issue view 2691 ".to_string()], |
| 463 | critical_files: vec![" crates/engine/src/tools/plan.rs ".to_string()], |
| 464 | constraints: vec!["".to_string(), " no secrets ".to_string()], |
| 465 | plan: vec![ |
| 466 | PlanItemArg { |
| 467 | step: " ".to_string(), |
| 468 | status: StepStatus::Pending, |
| 469 | }, |
| 470 | PlanItemArg { |
| 471 | step: " render sections ".to_string(), |
| 472 | status: StepStatus::InProgress, |
| 473 | }, |
| 474 | ], |
| 475 | ..UpdatePlanArgs::default() |
| 476 | }); |
| 477 | |
| 478 | let snapshot = state.snapshot(); |
| 479 | assert_eq!(snapshot.title.as_deref(), Some("Rich plan")); |
| 480 | assert_eq!(snapshot.sources_used, vec!["gh issue view 2691"]); |
| 481 | assert_eq!( |
| 482 | snapshot.critical_files, |
| 483 | vec!["crates/engine/src/tools/plan.rs"] |
| 484 | ); |
| 485 | assert_eq!(snapshot.constraints, vec!["no secrets"]); |
| 486 | assert_eq!(snapshot.items.len(), 1); |
| 487 | assert_eq!(snapshot.items[0].step, "render sections"); |
| 488 | assert_eq!(snapshot.items[0].status, StepStatus::InProgress); |
| 489 | } |
| 490 | |
| 491 | #[test] |
| 492 | fn plan_state_restores_from_persisted_snapshot() { |
| 493 | let snapshot = PlanSnapshot { |
| 494 | objective: Some("Restore Work state".to_string()), |
| 495 | items: vec![ |
| 496 | PlanItemArg { |
| 497 | step: "inspect".to_string(), |
| 498 | status: StepStatus::Completed, |
| 499 | }, |
| 500 | PlanItemArg { |
| 501 | step: "verify".to_string(), |
| 502 | status: StepStatus::InProgress, |
| 503 | }, |
| 504 | ], |
| 505 | ..PlanSnapshot::default() |
| 506 | }; |
| 507 | |
| 508 | let restored = PlanState::from_snapshot(&snapshot); |
| 509 | assert_eq!(restored.snapshot(), snapshot); |
| 510 | } |
| 511 | |
| 512 | #[test] |
| 513 | fn snapshot_serde_skips_empty_fields_and_deserializes_legacy() { |
| 514 | let snapshot = PlanSnapshot { |
| 515 | objective: Some("Ship PlanArtifact".to_string()), |
| 516 | items: vec![PlanItemArg { |
| 517 | step: "keep legacy replay working".to_string(), |
| 518 | status: StepStatus::Completed, |
| 519 | }], |
| 520 | ..PlanSnapshot::default() |
| 521 | }; |
| 522 | |
| 523 | let value = serde_json::to_value(&snapshot).expect("serialize snapshot"); |
| 524 | assert!(value.get("objective").is_some()); |
| 525 | assert!(value.get("title").is_none()); |
| 526 | assert!(value.get("sources_used").is_none()); |
| 527 | assert!(value.get("constraints").is_none()); |
| 528 | |
| 529 | let legacy: PlanSnapshot = serde_json::from_value(json!({ |
| 530 | "explanation": "Legacy explanation", |
| 531 | "items": [ |
| 532 | { "step": "legacy step", "status": "pending" } |
| 533 | ] |
| 534 | })) |
| 535 | .expect("legacy snapshot should deserialize"); |
| 536 | assert_eq!(legacy.explanation.as_deref(), Some("Legacy explanation")); |
| 537 | assert_eq!(legacy.items.len(), 1); |
| 538 | assert!(legacy.sources_used.is_empty()); |
| 539 | } |
| 540 | |
| 541 | #[tokio::test] |
| 542 | async fn replan_if_round_trips_through_state_serde_and_work_graph() { |
| 543 | let state = new_shared_plan_state(); |
| 544 | let todos = crate::tools::todo::new_shared_todo_list(); |
| 545 | let work = crate::work_graph::new_shared_work_runtime(todos, state); |
| 546 | |
| 547 | let empty_json = serde_json::to_value(PlanSnapshot::default()).unwrap(); |
| 548 | assert!(empty_json.get("replan_if").is_none()); |
| 549 | |
| 550 | let from_input = PlanSnapshot::from_tool_input(&json!({ |
| 551 | "replan_if": [" api schema changes ", " "] |
| 552 | })); |
| 553 | assert_eq!(from_input.replan_if, vec!["api schema changes"]); |
| 554 | assert!(!from_input.is_empty()); |
| 555 | |
| 556 | let args = UpdatePlanArgs { |
| 557 | replan_if: vec![" auth scope narrows ".to_string(), "".to_string()], |
| 558 | ..UpdatePlanArgs::default() |
| 559 | }; |
| 560 | let mut isolated = PlanState::default(); |
| 561 | isolated.update(args); |
| 562 | let isolated_snap = isolated.snapshot(); |
| 563 | assert_eq!(isolated_snap.replan_if, vec!["auth scope narrows"]); |
| 564 | assert_eq!( |
| 565 | PlanState::from_snapshot(&isolated_snap).snapshot(), |
| 566 | isolated_snap |
| 567 | ); |
| 568 | |
| 569 | let graph_snap = PlanSnapshot::from_tool_input(&json!({ |
| 570 | "replan_if": [" latency regresses above 1s ", " "] |
| 571 | })); |
| 572 | work.apply_plan_update("session", "plan", &graph_snap) |
| 573 | .await |
| 574 | .expect("replan_if update through work_graph should succeed"); |
| 575 | |
| 576 | let captured = work |
| 577 | .capture(Some("session")) |
| 578 | .expect("capture") |
| 579 | .expect("graph state"); |
| 580 | assert_eq!(captured.plan.replan_if, vec!["latency regresses above 1s"]); |
| 581 | let serialized = serde_json::to_value(&captured.plan).unwrap(); |
| 582 | assert_eq!( |
| 583 | serialized["replan_if"], |
| 584 | json!(["latency regresses above 1s"]) |
| 585 | ); |
| 586 | } |
| 587 | } |
| 588 |