fix custom world agent draft profile identity

This commit is contained in:
2026-04-23 13:54:38 +08:00
parent 1e200ec5ba
commit da7c1ff0c5
8 changed files with 356 additions and 6 deletions

View File

@@ -993,7 +993,18 @@ fn upsert_custom_world_profile_record(
.custom_world_profile()
.profile_id()
.find(&input.profile_id)
.filter(|row| row.owner_user_id == input.owner_user_id);
.filter(|row| row.owner_user_id == input.owner_user_id)
.or_else(|| {
input.source_agent_session_id.as_ref().and_then(|session_id| {
ctx.db.custom_world_profile().iter().find(|row| {
is_same_agent_draft_profile_candidate(
row,
&input.owner_user_id,
session_id,
)
})
})
});
let next_row = match current {
Some(existing) => {
@@ -1798,11 +1809,16 @@ fn execute_sync_result_profile_action(
payload: &JsonMap<String, JsonValue>,
) -> Result<CustomWorldAgentOperationSnapshot, String> {
ensure_refining_stage(session.stage, "sync_result_profile")?;
let profile = payload
let mut profile = payload
.get("profile")
.and_then(JsonValue::as_object)
.cloned()
.ok_or_else(|| "sync_result_profile requires profile".to_string())?;
if let Some(stable_profile_id) = resolve_stable_agent_draft_profile_id(session) {
// 结果页回写时必须沿用当前草稿的稳定身份,避免把同一草稿写成新条目。
profile.insert("id".to_string(), JsonValue::String(stable_profile_id.clone()));
upsert_nested_result_profile_id(&mut profile, &stable_profile_id);
}
let draft_profile = ensure_minimal_draft_profile(profile, &session.seed_text);
let gate = summarize_publish_gate_from_json(
&session.session_id,
@@ -1854,6 +1870,35 @@ fn execute_sync_result_profile_action(
Ok(build_custom_world_agent_operation_snapshot(&operation))
}
fn resolve_stable_agent_draft_profile_id(session: &CustomWorldAgentSession) -> Option<String> {
parse_optional_session_object(session.draft_profile_json.as_deref()).and_then(|profile| {
read_optional_text_field(&profile, &["legacyResultProfile.id", "id"])
})
}
fn upsert_nested_result_profile_id(profile: &mut JsonMap<String, JsonValue>, stable_profile_id: &str) {
let legacy_result_profile = profile
.entry("legacyResultProfile".to_string())
.or_insert_with(|| JsonValue::Object(JsonMap::new()));
if let Some(object) = legacy_result_profile.as_object_mut() {
object.insert(
"id".to_string(),
JsonValue::String(stable_profile_id.to_string()),
);
}
}
fn is_same_agent_draft_profile_candidate(
row: &CustomWorldProfile,
owner_user_id: &str,
source_agent_session_id: &str,
) -> bool {
row.owner_user_id == owner_user_id
&& row.deleted_at.is_none()
&& row.publication_status == CustomWorldPublicationStatus::Draft
&& row.source_agent_session_id.as_deref() == Some(source_agent_session_id)
}
fn execute_publish_world_action(
ctx: &ReducerContext,
session: &CustomWorldAgentSession,