Update an intent's fields.
(
&self,
intent_id: &str,
options: IntentUpdateOptions,
)
| 477 | |
| 478 | /// Update an intent's fields. |
| 479 | pub fn vault_intent_update( |
| 480 | &self, |
| 481 | intent_id: &str, |
| 482 | options: IntentUpdateOptions, |
| 483 | ) -> Result<IntentInfo, RepositoryError> { |
| 484 | let full_id = self.normalize_intent_id(intent_id)?; |
| 485 | let intent_file = |
| 486 | self.find_intent_path(&full_id)? |
| 487 | .ok_or_else(|| RepositoryError::InvalidOperation { |
| 488 | message: format!("Intent '{}' not found", full_id), |
| 489 | })?; |
| 490 | |
| 491 | // Read from disk first so we pick up any user/agent edits to the |
| 492 | // markdown body. If the file doesn't exist on disk, fall back to |
| 493 | // the redb entry. |
| 494 | let disk_path = self.vault_dir().join(&intent_file); |
| 495 | let (content_bytes, frontmatter_json) = if disk_path.exists() { |
| 496 | let file_content = std::fs::read_to_string(&disk_path)?; |
| 497 | let (fm_json, body) = crate::repository::vault::parse_vault_frontmatter(&file_content); |
| 498 | (body.into_bytes(), fm_json) |
| 499 | } else { |
| 500 | let entry = self.vault_retrieve(&intent_file)?.ok_or_else(|| { |
| 501 | RepositoryError::InvalidOperation { |
| 502 | message: format!("Intent '{}' not found", full_id), |
| 503 | } |
| 504 | })?; |
| 505 | (entry.content_bytes.clone(), entry.frontmatter_json.clone()) |
| 506 | }; |
| 507 | |
| 508 | // Update frontmatter |
| 509 | let mut fm: serde_json::Map<String, serde_json::Value> = |
| 510 | serde_json::from_str(&frontmatter_json).unwrap_or_default(); |
| 511 | |
| 512 | if options.content.is_some() && !options.force { |
| 513 | let manifest = self.vault_manifest()?; |
| 514 | let summary = manifest.intents.get(&full_id); |
| 515 | let current_status = summary |
| 516 | .map(|intent| intent.status.as_str()) |
| 517 | .or_else(|| fm.get("status").and_then(|value| value.as_str())) |
| 518 | .unwrap_or("unknown"); |
| 519 | let has_linked_goal = summary.is_some_and(|intent| intent.goals > 0) |
| 520 | || manifest.goals.values().any(|goal| { |
| 521 | goal.intent |
| 522 | .as_deref() |
| 523 | .is_some_and(|id| id.eq_ignore_ascii_case(&full_id)) |
| 524 | }); |
| 525 | |
| 526 | if current_status != "backlog" || has_linked_goal { |
| 527 | return Err(RepositoryError::InvalidOperation { |
| 528 | message: format!( |
| 529 | "Intent '{}' has started or is linked to a goal; rewriting its body would \ |
| 530 | change the execution context. Retry with force enabled.", |
| 531 | full_id |
| 532 | ), |
| 533 | }); |
| 534 | } |
| 535 | } |
| 536 |