|
7 | 7 | //! instances share the same physical files — edits in any worktree are |
8 | 8 | //! immediately visible to all others. |
9 | 9 | //! |
| 10 | +//! **Command reconciliation** (`reconcile_legacy_command_names`): Per-launch |
| 11 | +//! fix-up of persisted built-in command names from the Sprout→Buzz rename. |
| 12 | +//! |
10 | 13 | //! **Provider reconciliation** (`reconcile_provider_mcp_commands`): Per-launch |
11 | 14 | //! fix-up of `mcp_command` values in `managed-agents.json` against the |
12 | 15 | //! discovery table. Ensures known providers always have their canonical |
@@ -596,6 +599,191 @@ fn reconcile_mcp_commands_in_file(path: &Path) { |
596 | 599 | }); |
597 | 600 | } |
598 | 601 |
|
| 602 | +fn replace_command_field( |
| 603 | + obj: &mut serde_json::Map<String, serde_json::Value>, |
| 604 | + field: &str, |
| 605 | + replacement: String, |
| 606 | +) -> bool { |
| 607 | + let Some(current) = obj.get(field).and_then(|v| v.as_str()) else { |
| 608 | + return false; |
| 609 | + }; |
| 610 | + if current == replacement { |
| 611 | + return false; |
| 612 | + } |
| 613 | + eprintln!( |
| 614 | + "buzz-desktop: command-rename-reconcile: {:?}: {field} {:?} → {:?}", |
| 615 | + obj.get("name").and_then(|v| v.as_str()).unwrap_or("?"), |
| 616 | + current, |
| 617 | + replacement, |
| 618 | + ); |
| 619 | + obj.insert(field.to_string(), serde_json::Value::String(replacement)); |
| 620 | + true |
| 621 | +} |
| 622 | + |
| 623 | +fn reconcile_legacy_command_names_in_file(path: &Path) { |
| 624 | + patch_json_records(path, |obj| { |
| 625 | + let mut changed = false; |
| 626 | + |
| 627 | + if let Some(acp_command) = obj |
| 628 | + .get("acp_command") |
| 629 | + .and_then(|v| v.as_str()) |
| 630 | + .map(str::to_string) |
| 631 | + { |
| 632 | + if acp_command == "sprout-acp" { |
| 633 | + changed |= replace_command_field(obj, "acp_command", "buzz-acp".to_string()); |
| 634 | + } |
| 635 | + } |
| 636 | + |
| 637 | + let mut agent_command = obj |
| 638 | + .get("agent_command") |
| 639 | + .and_then(|v| v.as_str()) |
| 640 | + .unwrap_or("") |
| 641 | + .to_string(); |
| 642 | + if agent_command == "sprout-agent" { |
| 643 | + agent_command = "buzz-agent".to_string(); |
| 644 | + changed |= replace_command_field(obj, "agent_command", agent_command.clone()); |
| 645 | + } |
| 646 | + |
| 647 | + if let Some(mcp_command) = obj |
| 648 | + .get("mcp_command") |
| 649 | + .and_then(|v| v.as_str()) |
| 650 | + .map(str::to_string) |
| 651 | + { |
| 652 | + match mcp_command.as_str() { |
| 653 | + "sprout-dev-mcp" => { |
| 654 | + changed |= |
| 655 | + replace_command_field(obj, "mcp_command", "buzz-dev-mcp".to_string()); |
| 656 | + } |
| 657 | + "sprout-mcp" | "sprout-mcp-server" | "buzz-mcp-server" => { |
| 658 | + let replacement = if agent_command == "buzz-agent" { |
| 659 | + "buzz-dev-mcp" |
| 660 | + } else { |
| 661 | + "" |
| 662 | + }; |
| 663 | + changed |= replace_command_field(obj, "mcp_command", replacement.to_string()); |
| 664 | + } |
| 665 | + _ => {} |
| 666 | + } |
| 667 | + } |
| 668 | + |
| 669 | + changed |
| 670 | + }); |
| 671 | +} |
| 672 | + |
| 673 | +fn reconcile_legacy_persona_runtimes_in_file(path: &Path) { |
| 674 | + patch_json_records(path, |obj| { |
| 675 | + let Some(runtime) = obj.get("runtime").and_then(|v| v.as_str()) else { |
| 676 | + return false; |
| 677 | + }; |
| 678 | + if runtime != "sprout-agent" { |
| 679 | + return false; |
| 680 | + } |
| 681 | + eprintln!( |
| 682 | + "buzz-desktop: command-rename-reconcile: persona {:?}: runtime {:?} → {:?}", |
| 683 | + obj.get("display_name") |
| 684 | + .or_else(|| obj.get("displayName")) |
| 685 | + .and_then(|v| v.as_str()) |
| 686 | + .unwrap_or("?"), |
| 687 | + runtime, |
| 688 | + "buzz-agent", |
| 689 | + ); |
| 690 | + obj.insert( |
| 691 | + "runtime".to_string(), |
| 692 | + serde_json::Value::String("buzz-agent".to_string()), |
| 693 | + ); |
| 694 | + true |
| 695 | + }); |
| 696 | +} |
| 697 | + |
| 698 | +fn rewrite_legacy_persona_md_runtime(content: &str) -> Option<String> { |
| 699 | + let (frontmatter, body) = buzz_persona_pkg::persona::split_frontmatter(content).ok()?; |
| 700 | + let mut value = serde_yaml::from_str::<serde_yaml::Value>(frontmatter).ok()?; |
| 701 | + let mapping = value.as_mapping_mut()?; |
| 702 | + let runtime = mapping.get_mut(serde_yaml::Value::String("runtime".to_string()))?; |
| 703 | + if runtime.as_str()? != "sprout-agent" { |
| 704 | + return None; |
| 705 | + } |
| 706 | + *runtime = serde_yaml::Value::String("buzz-agent".to_string()); |
| 707 | + let frontmatter = serde_yaml::to_string(&value).ok()?; |
| 708 | + Some(format!("---\n{frontmatter}---\n{body}")) |
| 709 | +} |
| 710 | + |
| 711 | +fn reconcile_legacy_team_persona_runtime_files(dir: &Path) { |
| 712 | + let Ok(entries) = std::fs::read_dir(dir) else { |
| 713 | + return; |
| 714 | + }; |
| 715 | + for entry in entries.flatten() { |
| 716 | + let path = entry.path(); |
| 717 | + let Ok(file_type) = entry.file_type() else { |
| 718 | + continue; |
| 719 | + }; |
| 720 | + if file_type.is_dir() { |
| 721 | + reconcile_legacy_team_persona_runtime_files(&path); |
| 722 | + continue; |
| 723 | + } |
| 724 | + if !file_type.is_file() { |
| 725 | + continue; |
| 726 | + } |
| 727 | + let Some(name) = path.file_name().and_then(|name| name.to_str()) else { |
| 728 | + continue; |
| 729 | + }; |
| 730 | + if !name.ends_with(".persona.md") { |
| 731 | + continue; |
| 732 | + } |
| 733 | + let Ok(content) = std::fs::read_to_string(&path) else { |
| 734 | + continue; |
| 735 | + }; |
| 736 | + let Some(updated) = rewrite_legacy_persona_md_runtime(&content) else { |
| 737 | + continue; |
| 738 | + }; |
| 739 | + if updated == content { |
| 740 | + continue; |
| 741 | + } |
| 742 | + match std::fs::write(&path, updated) { |
| 743 | + Ok(()) => { |
| 744 | + eprintln!( |
| 745 | + "buzz-desktop: command-rename-reconcile: updated {}", |
| 746 | + path.display() |
| 747 | + ); |
| 748 | + } |
| 749 | + Err(error) => { |
| 750 | + eprintln!( |
| 751 | + "buzz-desktop: command-rename-reconcile: failed to update {}: {error}", |
| 752 | + path.display() |
| 753 | + ); |
| 754 | + } |
| 755 | + } |
| 756 | + } |
| 757 | +} |
| 758 | + |
| 759 | +/// Reconcile exact built-in command values persisted before the Sprout→Buzz |
| 760 | +/// rename. Custom commands and explicit paths are left untouched. |
| 761 | +pub fn reconcile_legacy_command_names(app: &tauri::AppHandle) { |
| 762 | + let Ok(current_dir) = app.path().app_data_dir() else { |
| 763 | + return; |
| 764 | + }; |
| 765 | + let mut dirs = vec![current_dir.clone()]; |
| 766 | + if let Some(canonical) = canonical_dev_data_dir(¤t_dir) { |
| 767 | + if canonical.exists() && canonical != current_dir { |
| 768 | + dirs.push(canonical); |
| 769 | + } |
| 770 | + } |
| 771 | + for dir in dirs { |
| 772 | + let path = dir.join("agents/managed-agents.json"); |
| 773 | + if path.exists() { |
| 774 | + reconcile_legacy_command_names_in_file(&path); |
| 775 | + } |
| 776 | + let personas_path = dir.join("agents/personas.json"); |
| 777 | + if personas_path.exists() { |
| 778 | + reconcile_legacy_persona_runtimes_in_file(&personas_path); |
| 779 | + } |
| 780 | + let teams_dir = dir.join("agents/teams"); |
| 781 | + if teams_dir.exists() && !teams_dir.is_symlink() { |
| 782 | + reconcile_legacy_team_persona_runtime_files(&teams_dir); |
| 783 | + } |
| 784 | + } |
| 785 | +} |
| 786 | + |
599 | 787 | /// Reconcile `mcp_command` values in managed-agents.json against the |
600 | 788 | /// discovery table. Known runtimes get their canonical mcp_command; |
601 | 789 | /// unknown/custom agents are left untouched. Covers both the current |
@@ -646,3 +834,7 @@ pub fn migrate_persona_provider_to_runtime(app: &tauri::AppHandle) { |
646 | 834 | #[cfg(test)] |
647 | 835 | #[path = "migration_tests.rs"] |
648 | 836 | mod tests; |
| 837 | + |
| 838 | +#[cfg(test)] |
| 839 | +#[path = "migration_command_tests.rs"] |
| 840 | +mod command_tests; |
0 commit comments