Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(profiles): let a profile named outside ASCII be saved

Nguyen Thanh Datb4c2da3ee056
diff --git a/src/openhuman/agent/profiles/store.rs b/src/openhuman/agent/profiles/store.rs
index ed32e394ed..1b4eec9b2d 100644
--- a/src/openhuman/agent/profiles/store.rs
+++ b/src/openhuman/agent/profiles/store.rs
@@ -548,6 +548,9 @@ fn normalise_profile(mut profile: AgentProfile) -> AgentProfile {
if profile.id.is_empty() {
profile.id = slugify_profile_id(&profile.name);
}
+ if profile.id.is_empty() {
+ profile.id = profile_id_from_name_digest(&profile.name);
+ }
profile.name = profile.name.trim().to_string();
if profile.name.is_empty() {
profile.name = profile.id.clone();
@@ -620,6 +623,33 @@ pub(crate) fn normalise_profile_id(input: &str) -> String {
slugify_profile_id(input)
}
+/// Profile id for a name that slugifies to nothing.
+///
+/// `slugify_profile_id` keeps only ASCII alphanumerics, so a name written
+/// entirely outside that range - Japanese, Chinese, Greek, Cyrillic, Arabic -
+/// reduces to the empty string and `validate_profile_id` rejects the upsert
+/// with "profile id must not be empty". The user supplied a name; the error
+/// blames an id they never typed.
+///
+/// Derive one from the name instead. A digest keeps it deterministic, so
+/// saving the same profile twice updates it rather than creating a second one,
+/// and two different names do not land on the same id.
+fn profile_id_from_name_digest(name: &str) -> String {
+ use sha2::{Digest, Sha256};
+
+ let trimmed = name.trim();
+ if trimmed.is_empty() {
+ return String::new();
+ }
+ let digest = Sha256::digest(trimmed.as_bytes());
+ let short: String = digest
+ .iter()
+ .take(4)
+ .map(|byte| format!("{byte:02x}"))
+ .collect();
+ format!("profile-{short}")
+}
+
fn slugify_profile_id(input: &str) -> String {
let mut out = String::new();
let mut last_was_sep = false;
diff --git a/src/openhuman/agent/profiles/store_tests.rs b/src/openhuman/agent/profiles/store_tests.rs
index a82a2f9734..4da50738ce 100644
--- a/src/openhuman/agent/profiles/store_tests.rs
+++ b/src/openhuman/agent/profiles/store_tests.rs
@@ -335,3 +335,78 @@ fn default_profile_has_master_and_memory_suffix() {
assert_eq!(default.memory_dir_suffix.as_deref(), Some(""));
assert!(default.include_agent_conversations);
}
+
+/// A name written entirely outside ASCII slugifies to nothing, and the
+/// upsert used to fail with "profile id must not be empty" - an error about
+/// a field the user never filled in.
+#[test]
+fn a_profile_named_only_in_non_ascii_can_be_saved() {
+ let dir = tempdir().expect("tempdir");
+ let store = AgentProfileStore::new(dir.path().to_path_buf());
+ let name = "\u{7814}\u{7a76}\u{30a2}\u{30b7}\u{30b9}\u{30bf}\u{30f3}\u{30c8}";
+
+ let state = store
+ .upsert(custom("", name, "orchestrator"))
+ .expect("a profile named in Japanese must be saveable");
+
+ let saved = state
+ .profiles
+ .iter()
+ .find(|p| p.name == name)
+ .expect("the profile is in the list");
+ assert!(
+ super::super::home::validate_profile_id(&saved.id).is_ok(),
+ "derived id must be a valid one: {}",
+ saved.id
+ );
+}
+
+/// The id comes from a digest of the name, so re-saving the same profile
+/// edits it instead of adding a second copy.
+#[test]
+fn re_saving_a_non_ascii_profile_updates_it() {
+ let dir = tempdir().expect("tempdir");
+ let store = AgentProfileStore::new(dir.path().to_path_buf());
+ let name = "\u{7814}\u{7a76}\u{30a2}\u{30b7}\u{30b9}\u{30bf}\u{30f3}\u{30c8}";
+
+ store
+ .upsert(custom("", name, "orchestrator"))
+ .expect("first");
+ let state = store.upsert(custom("", name, "planner")).expect("second");
+
+ let matching: Vec<_> = state.profiles.iter().filter(|p| p.name == name).collect();
+ assert_eq!(
+ matching.len(),
+ 1,
+ "re-saving must not duplicate the profile"
+ );
+ assert_eq!(matching[0].agent_id, "planner", "the edit must be applied");
+}
+
+/// Two different non-ASCII names must not collapse onto one profile.
+#[test]
+fn two_non_ascii_profiles_keep_separate_ids() {
+ let dir = tempdir().expect("tempdir");
+ let store = AgentProfileStore::new(dir.path().to_path_buf());
+
+ store
+ .upsert(custom("", "\u{7814}\u{7a76}", "orchestrator"))
+ .expect("first");
+ let state = store
+ .upsert(custom("", "\u{5206}\u{6790}", "orchestrator"))
+ .expect("second");
+
+ assert!(state.profiles.iter().any(|p| p.name == "\u{7814}\u{7a76}"));
+ assert!(state.profiles.iter().any(|p| p.name == "\u{5206}\u{6790}"));
+}
+
+/// An ASCII name still gets the readable slug it always had.
+#[test]
+fn an_ascii_name_is_unaffected() {
+ let dir = tempdir().expect("tempdir");
+ let store = AgentProfileStore::new(dir.path().to_path_buf());
+ let state = store
+ .upsert(custom("", "Research Buddy", "orchestrator"))
+ .expect("upsert");
+ assert!(state.profiles.iter().any(|p| p.id == "research-buddy"));
+}