|
2 | 2 | //! guard). Owns the reconcile data carrier, the legacy-avatar backfill, and |
3 | 3 | //! the needs-sync predicate. |
4 | 4 |
|
5 | | -use tauri::AppHandle; |
| 5 | +use std::collections::HashSet; |
| 6 | +use tauri::{AppHandle, Manager}; |
6 | 7 |
|
7 | 8 | use crate::app_state::AppState; |
8 | 9 | use crate::managed_agents::managed_agent_avatar_url; |
@@ -49,6 +50,78 @@ pub(super) fn resolve_legacy_avatar( |
49 | 50 | .unwrap_or_default() |
50 | 51 | } |
51 | 52 |
|
| 53 | +pub(crate) fn profile_reconcile_data( |
| 54 | + record: &crate::managed_agents::ManagedAgentRecord, |
| 55 | + personas: &[crate::managed_agents::AgentDefinition], |
| 56 | +) -> ProfileReconcileData { |
| 57 | + ProfileReconcileData { |
| 58 | + private_key_nsec: record.private_key_nsec.clone(), |
| 59 | + name: record.name.clone(), |
| 60 | + relay_url: record.relay_url.clone(), |
| 61 | + avatar_url: record.avatar_url.clone(), |
| 62 | + auth_tag: record.auth_tag.clone(), |
| 63 | + pubkey: record.pubkey.clone(), |
| 64 | + agent_command: crate::managed_agents::record_agent_command(record, personas), |
| 65 | + persona_id: record.persona_id.clone(), |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +pub(crate) fn load_pending_profile_reconciliations( |
| 70 | + app: &AppHandle, |
| 71 | +) -> Result<Vec<(String, ProfileReconcileData)>, String> { |
| 72 | + let state = app.state::<AppState>(); |
| 73 | + let _store_guard = state |
| 74 | + .managed_agents_store_lock |
| 75 | + .lock() |
| 76 | + .map_err(|error| error.to_string())?; |
| 77 | + let store_path = crate::managed_agents::managed_agents_store_path(app)?; |
| 78 | + let queue_path = crate::migration::profile_reconcile_queue_path(&store_path); |
| 79 | + if !queue_path.exists() { |
| 80 | + return Ok(Vec::new()); |
| 81 | + } |
| 82 | + |
| 83 | + let pending: HashSet<String> = crate::migration::read_profile_reconcile_queue(&queue_path)? |
| 84 | + .into_iter() |
| 85 | + .collect(); |
| 86 | + let records = crate::managed_agents::load_managed_agents(app)?; |
| 87 | + let personas = crate::managed_agents::load_personas(app).unwrap_or_default(); |
| 88 | + let workspace_relay = relay_ws_url_with_override(&state); |
| 89 | + Ok(records |
| 90 | + .iter() |
| 91 | + // A queue write deliberately precedes the migrated agent-store write. |
| 92 | + // If the process dies between them, retain (but do not execute) the |
| 93 | + // stale item until the next boot finishes renaming the record. |
| 94 | + .filter(|record| { |
| 95 | + pending.contains(&record.pubkey) |
| 96 | + && record.name == crate::managed_agents::POLLEN_DISPLAY_NAME |
| 97 | + }) |
| 98 | + .map(|record| { |
| 99 | + let mut data = profile_reconcile_data(record, &personas); |
| 100 | + // Pin the effective relay while this workspace is active. Otherwise |
| 101 | + // a fast community switch could make a queued task for A run on B. |
| 102 | + data.relay_url = |
| 103 | + crate::relay::effective_agent_relay_url(&data.relay_url, &workspace_relay); |
| 104 | + (record.pubkey.clone(), data) |
| 105 | + }) |
| 106 | + .collect()) |
| 107 | +} |
| 108 | + |
| 109 | +pub(crate) fn mark_profile_reconciled(app: &AppHandle, pubkey: &str) -> Result<(), String> { |
| 110 | + let state = app.state::<AppState>(); |
| 111 | + let _store_guard = state |
| 112 | + .managed_agents_store_lock |
| 113 | + .lock() |
| 114 | + .map_err(|error| error.to_string())?; |
| 115 | + let store_path = crate::managed_agents::managed_agents_store_path(app)?; |
| 116 | + let queue_path = crate::migration::profile_reconcile_queue_path(&store_path); |
| 117 | + if !queue_path.exists() { |
| 118 | + return Ok(()); |
| 119 | + } |
| 120 | + let mut pending = crate::migration::read_profile_reconcile_queue(&queue_path)?; |
| 121 | + pending.retain(|queued| queued != pubkey); |
| 122 | + crate::migration::write_profile_reconcile_queue(&queue_path, &pending) |
| 123 | +} |
| 124 | + |
52 | 125 | /// Reconcile an agent's kind:0 profile on the relay. |
53 | 126 | /// |
54 | 127 | /// Queries the relay for the agent's existing profile and re-publishes if missing |
|
0 commit comments