@@ -142,7 +142,9 @@ fn patch_json_records(
142142 }
143143 if changed {
144144 if let Ok ( bytes) = serde_json:: to_vec_pretty ( & records) {
145- let _ = std:: fs:: write ( path, bytes) ;
145+ if let Err ( e) = crate :: managed_agents:: atomic_write_json ( path, & bytes) {
146+ eprintln ! ( "buzz-desktop: patch-json-records: {e}" ) ;
147+ }
146148 }
147149 }
148150}
@@ -343,8 +345,8 @@ pub fn sync_shared_agent_data(app: &tauri::AppHandle) {
343345 }
344346}
345347
346- fn reconcile_team_dirs_in_file ( path : & Path , canonical_dir : & Path ) {
347- let canonical_teams = canonical_dir . join ( "agents/teams" ) ;
348+ fn reconcile_team_dirs_in_file ( path : & Path , target_dir : & Path ) {
349+ let target_teams = target_dir . join ( "agents/teams" ) ;
348350 patch_json_records ( path, |obj| {
349351 // Handle both old field name and new field name
350352 let field_name = if obj. contains_key ( "persona_team_dir" ) {
@@ -374,43 +376,111 @@ fn reconcile_team_dirs_in_file(path: &Path, canonical_dir: &Path) {
374376 let Some ( id) = team_id else {
375377 return false ;
376378 } ;
377- let expected = canonical_teams . join ( id) ;
379+ let expected = target_teams . join ( id) ;
378380 if team_path == expected {
381+ // Value already correct — still normalize the legacy field name so
382+ // stores converge on `persona_team_dir` (runtime reads either via
383+ // serde alias).
384+ if field_name == "persona_pack_path" {
385+ if let Some ( val) = obj. remove ( "persona_pack_path" ) {
386+ obj. insert ( "persona_team_dir" . to_string ( ) , val) ;
387+ return true ;
388+ }
389+ }
379390 return false ;
380391 }
392+ // Rewriting to a path that does not exist on disk makes things worse
393+ // than leaving a stale-but-working path in place. fs::metadata follows
394+ // symlinks, so a valid symlinked install passes; a dangling symlink
395+ // fails with NotFound.
396+ if let Err ( e) = std:: fs:: metadata ( & expected) {
397+ eprintln ! (
398+ "buzz-desktop: team-dir-reconcile: {:?}: {:?} expected at {:?} — {e}, leaving as-is" ,
399+ obj. get( "name" ) . and_then( |v| v. as_str( ) ) . unwrap_or( "?" ) ,
400+ team_path,
401+ expected,
402+ ) ;
403+ return false ;
404+ }
405+ let Some ( expected_str) = expected. to_str ( ) else {
406+ eprintln ! (
407+ "buzz-desktop: team-dir-reconcile: {:?}: expected path {:?} is not valid UTF-8, leaving as-is" ,
408+ obj. get( "name" ) . and_then( |v| v. as_str( ) ) . unwrap_or( "?" ) ,
409+ expected,
410+ ) ;
411+ return false ;
412+ } ;
381413 eprintln ! (
382414 "buzz-desktop: team-dir-reconcile: {:?}: {:?} → {:?}" ,
383415 obj. get( "name" ) . and_then( |v| v. as_str( ) ) . unwrap_or( "?" ) ,
384416 team_path,
385417 expected,
386418 ) ;
387- // Always write the canonical new field name
419+ // Always write the new field name
388420 obj. remove ( "persona_pack_path" ) ;
389421 obj. insert (
390422 "persona_team_dir" . to_string ( ) ,
391- serde_json:: Value :: String ( expected . to_string_lossy ( ) . into_owned ( ) ) ,
423+ serde_json:: Value :: String ( expected_str . to_owned ( ) ) ,
392424 ) ;
393425 true
394426 } ) ;
395427}
396428
429+ /// Select the data directory to reconcile against.
430+ ///
431+ /// Dev instances — identified by the data-dir name starting with
432+ /// `CANONICAL_DEV_IDENTIFIER` (covers the canonical dir itself and any
433+ /// worktree variant like `xyz.block.buzz.app.dev.mybranch`) — share
434+ /// `agents/managed-agents.json` and `agents/teams` via symlinks to the
435+ /// canonical dev dir, so they should normalize against that canonical dir.
436+ ///
437+ /// Release builds must reconcile their own data dir — keying off the canonical
438+ /// dev dir's mere existence would leave release records permanently stale on
439+ /// developer machines, where that dir is always present.
440+ fn reconcile_target_dir ( current_dir : & Path ) -> PathBuf {
441+ let is_dev_instance = current_dir
442+ . file_name ( )
443+ . and_then ( |n| n. to_str ( ) )
444+ . is_some_and ( |n| n. starts_with ( CANONICAL_DEV_IDENTIFIER ) ) ;
445+ if is_dev_instance {
446+ match canonical_dev_data_dir ( current_dir) {
447+ Some ( dir) if dir. exists ( ) => dir,
448+ _ => current_dir. to_path_buf ( ) ,
449+ }
450+ } else {
451+ current_dir. to_path_buf ( )
452+ }
453+ }
454+
397455/// Reconcile `persona_team_dir` (and legacy `persona_pack_path`) values in
398- /// managed-agents.json to point to the canonical dev data directory's
399- /// `agents/teams/` prefix. Fixes stale paths left when agents were created
400- /// from worktree instances whose data directories don't have local team copies.
456+ /// managed-agents.json to point to the correct `agents/teams/` prefix.
457+ ///
458+ /// Fixes two classes of stale paths:
459+ /// - Worktree dev instances whose records point at a sibling data dir rather
460+ /// than the canonical dev dir (dev instances share managed-agents.json and
461+ /// agents/teams via symlinks, so they all reconcile against the canonical dir).
462+ /// - Legacy paths left by historical renames: `agents/packs/` → `agents/teams/`
463+ /// (the packs→teams consolidation) and bundle-id `xyz.block.sprout.app` →
464+ /// `xyz.block.buzz.app` (the sprout→buzz rename, which moved the app data dir).
465+ ///
466+ /// Release builds reconcile their own data dir — choosing the canonical dev dir
467+ /// whenever it exists would leave release files permanently stale on developer
468+ /// machines.
401469pub fn reconcile_persona_team_dirs ( app : & tauri:: AppHandle ) {
402470 let Ok ( current_dir) = app. path ( ) . app_data_dir ( ) else {
403471 return ;
404472 } ;
405- let canonical_dir = match canonical_dev_data_dir ( & current_dir) {
406- Some ( dir) if dir. exists ( ) => dir,
407- _ => current_dir,
408- } ;
409- let path = canonical_dir. join ( "agents/managed-agents.json" ) ;
473+ // Single-dir on purpose: unlike reconcile_legacy_command_names and
474+ // reconcile_provider_mcp_commands, which patch both [current, canonical],
475+ // path rewrites are target-dependent — a dual pass through a dev
476+ // instance's symlinked store would write worktree-local paths into the
477+ // shared canonical file.
478+ let target_dir = reconcile_target_dir ( & current_dir) ;
479+ let path = target_dir. join ( "agents/managed-agents.json" ) ;
410480 if !path. exists ( ) {
411481 return ;
412482 }
413- reconcile_team_dirs_in_file ( & path, & canonical_dir ) ;
483+ reconcile_team_dirs_in_file ( & path, & target_dir ) ;
414484}
415485
416486/// One-time migration from packs to teams.
@@ -428,15 +498,12 @@ pub fn migrate_packs_to_teams(app: &tauri::AppHandle) {
428498 let Ok ( current_dir) = app. path ( ) . app_data_dir ( ) else {
429499 return ;
430500 } ;
431- let canonical_dir = match canonical_dev_data_dir ( & current_dir) {
432- Some ( dir) if dir. exists ( ) => dir,
433- _ => current_dir,
434- } ;
501+ let target_dir = reconcile_target_dir ( & current_dir) ;
435502
436- let packs_dir = canonical_dir . join ( "agents/packs" ) ;
437- let teams_dir = canonical_dir . join ( "agents/teams" ) ;
438- let personas_path = canonical_dir . join ( "agents/personas.json" ) ;
439- let agents_path = canonical_dir . join ( "agents/managed-agents.json" ) ;
503+ let packs_dir = target_dir . join ( "agents/packs" ) ;
504+ let teams_dir = target_dir . join ( "agents/teams" ) ;
505+ let personas_path = target_dir . join ( "agents/personas.json" ) ;
506+ let agents_path = target_dir . join ( "agents/managed-agents.json" ) ;
440507
441508 // Check if migration is needed: packs dir exists OR agents JSON has old field names
442509 let packs_dir_exists = packs_dir. exists ( ) && !packs_dir. is_symlink ( ) ;
@@ -831,10 +898,18 @@ pub fn migrate_persona_provider_to_runtime(app: &tauri::AppHandle) {
831898 rename_provider_to_runtime_in_personas ( & path) ;
832899}
833900
901+ #[ cfg( test) ]
902+ #[ path = "migration_test_support.rs" ]
903+ mod test_support;
904+
834905#[ cfg( test) ]
835906#[ path = "migration_tests.rs" ]
836907mod tests;
837908
838909#[ cfg( test) ]
839910#[ path = "migration_command_tests.rs" ]
840911mod command_tests;
912+
913+ #[ cfg( test) ]
914+ #[ path = "migration_team_dir_tests.rs" ]
915+ mod team_dir_tests;
0 commit comments