Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions desktop/src-tauri/src/managed_agents/personas.rs

Large diffs are not rendered by default.

74 changes: 74 additions & 0 deletions desktop/src-tauri/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//! discovery table. Ensures known providers always have their canonical
//! `mcp_command`; unknown/custom agents are left untouched.

use sha2::{Digest, Sha256};
use std::path::{Path, PathBuf};
use tauri::Manager;

Expand Down Expand Up @@ -175,6 +176,7 @@ fn run_boot_migrations_inner(app: &tauri::AppHandle, reset_completed: bool) {
// Post-fold readers of the runtime map (`load_persona_runtimes`) fall
// back to the unified store's definitions.
fold_personas_into_agent_store(app);
refresh_builtin_agent_avatars(app);
// B5: manufacture definitions for standalone agents AFTER the fold (so
// pre-existing definition slugs are present for collision checks) and
// before event sync republishes — the backfilled link is what flips the
Expand Down Expand Up @@ -511,6 +513,78 @@ fn patch_json_records(
}
}

const LEGACY_BUILTIN_AVATAR_HASHES: &[(&str, &str)] = &[
(
"builtin:fizz",
"2771b8c9c46aa3c8ac1c4d2acfa23fa9ba35b79c4b1694554e923081e3b8b4d0",
),
(
"builtin:honey",
"1979e54ef77fc94ec688170bd74dade35c563e7fcc82bb0714c672dfb018eab9",
),
(
"builtin:bumble",
"c08cf3b8b4c3f8721df6143367ababdebae8f913b9c654401ba74bb3d233655b",
),
];

/// Refresh the prior seeded avatar on built-in definitions and linked agent
/// instances while preserving any avatar the user customized. Matching by the
/// full data URL digest makes the migration idempotent and avoids relying on
/// timestamps or other persona fields the user may also have edited.
fn refresh_builtin_agent_avatars(app: &tauri::AppHandle) {
let Ok(dir) = app.path().app_data_dir() else {
return;
};
let path = dir.join("agents/managed-agents.json");
if path.exists() {
refresh_builtin_agent_avatars_in_file(
&path,
LEGACY_BUILTIN_AVATAR_HASHES,
&crate::util::now_iso(),
);
}
}

fn refresh_builtin_agent_avatars_in_file(path: &Path, legacy_hashes: &[(&str, &str)], now: &str) {
patch_json_records(path, |record| {
let persona_id = record
.get("persona_id")
.and_then(serde_json::Value::as_str)
.or_else(|| record.get("slug").and_then(serde_json::Value::as_str));
let Some(persona_id) = persona_id else {
return false;
};
let Some(legacy_hash) = legacy_hashes
.iter()
.find_map(|(id, hash)| (*id == persona_id).then_some(*hash))
else {
return false;
};
let Some(current_avatar) = record.get("avatar_url").and_then(serde_json::Value::as_str)
else {
return false;
};
if hex::encode(Sha256::digest(current_avatar.as_bytes())) != legacy_hash {
return false;
}
let Some(replacement) = crate::managed_agents::built_in_persona_avatar_url(persona_id)
else {
return false;
};

record.insert(
"avatar_url".to_string(),
serde_json::Value::String(replacement.to_string()),
);
Comment thread
klopez4212 marked this conversation as resolved.
Resolved
record.insert(
"updated_at".to_string(),
serde_json::Value::String(now.to_string()),
);
true
});
}

/// Create symlinks for shared agent data files from the current (worktree)
/// data directory to the canonical dev data directory.
///
Expand Down
78 changes: 78 additions & 0 deletions desktop/src-tauri/src/migration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -991,3 +991,81 @@ fn migrate_legacy_nest_preserves_user_edited_agents_md() {
"a user-edited live AGENTS.md must never be clobbered"
);
}

#[test]
fn refresh_builtin_agent_avatars_updates_seeded_values_and_preserves_customizations() {
use sha2::{Digest as _, Sha256};

let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("managed-agents.json");
let old_fizz = "data:image/png;base64,old-fizz";
let old_honey = "data:image/png;base64,old-honey";
let fizz_hash = hex::encode(Sha256::digest(old_fizz.as_bytes()));
let honey_hash = hex::encode(Sha256::digest(old_honey.as_bytes()));
let legacy_hashes = [
("builtin:fizz", fizz_hash.as_str()),
("builtin:honey", honey_hash.as_str()),
];
let records = serde_json::json!([
{
"pubkey": "",
"slug": "builtin:fizz",
"persona_id": null,
"avatar_url": old_fizz,
"updated_at": "before"
},
{
"pubkey": "fizz-instance",
"persona_id": "builtin:fizz",
"avatar_url": old_fizz,
"updated_at": "before"
},
{
"pubkey": "honey-instance",
"persona_id": "builtin:honey",
"avatar_url": "data:image/png;base64,user-customized",
"updated_at": "before"
},
{
"pubkey": "custom-instance",
"persona_id": "custom:fizz",
"avatar_url": old_fizz,
"updated_at": "before"
}
]);
std::fs::write(&path, serde_json::to_vec_pretty(&records).unwrap()).unwrap();

refresh_builtin_agent_avatars_in_file(&path, &legacy_hashes, "after");

let migrated: Vec<serde_json::Value> =
serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
let new_fizz = crate::managed_agents::built_in_persona_avatar_url("builtin:fizz").unwrap();
assert_eq!(migrated[0]["avatar_url"], new_fizz);
assert_eq!(migrated[0]["updated_at"], "after");
assert_eq!(migrated[1]["avatar_url"], new_fizz);
assert_eq!(migrated[1]["updated_at"], "after");
assert_eq!(
migrated[2]["avatar_url"],
"data:image/png;base64,user-customized"
);
assert_eq!(migrated[2]["updated_at"], "before");
assert_eq!(migrated[3]["avatar_url"], old_fizz);
assert_eq!(migrated[3]["updated_at"], "before");

let once = std::fs::read(&path).unwrap();
refresh_builtin_agent_avatars_in_file(&path, &legacy_hashes, "later");
assert_eq!(std::fs::read(&path).unwrap(), once);
}

#[test]
fn current_builtin_agent_avatars_do_not_match_legacy_hashes() {
use sha2::{Digest as _, Sha256};

for (id, legacy_hash) in LEGACY_BUILTIN_AVATAR_HASHES {
let current = crate::managed_agents::built_in_persona_avatar_url(id).unwrap();
assert_ne!(
hex::encode(Sha256::digest(current.as_bytes())),
*legacy_hash
);
}
}
Loading