Skip to content

Commit ce4e453

Browse files
wesbillmanPinky
andcommitted
refactor(desktop): split boot-time event sync out of migration.rs
migration.rs crossed the file-size guard (1577 > 1575) when the managed-agent reconcile call site landed. Rather than bump the limit, extract the post-identity event-sync leg (run_event_sync + migrate_personas_to_events/_in_dir + migrate_teams_to_events/_in_dir) into a new event_sync.rs with its tests, and ratchet the guard down: migration.rs 1575 -> 1310, migration_tests.rs 1410 -> 1110. Pure code move — no behavior change; the full test set is unchanged (verified by name against the pre-split list). Co-authored-by: Pinky <44b8e82baa6e0e254e0208d68f335c283c94e7b78dd1fa10d5a49d3f13dd0435@sprout-oss.stage.blox.sqprod.co> Signed-off-by: Wes <[email protected]>
1 parent 23f3f2f commit ce4e453

8 files changed

Lines changed: 596 additions & 576 deletions

File tree

‎desktop/scripts/check-file-sizes.mjs‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,16 @@ const overrides = new Map([
153153
// migration_tests.rs carries the harness-sync migration coverage plus the
154154
// patch_json_records owner-only writeback regression test (SECURITY.md:90
155155
// crash-safe 0o600 fallback). Load-bearing security + feature coverage, not
156-
// generic debt growth. Approved override; still queued to split.
157-
["src-tauri/src/migration_tests.rs", 1410],
156+
// generic debt growth. Approved override; still queued to split. Event-sync
157+
// (persona/team event reconcile) tests were split out to event_sync_tests.rs
158+
// and the limit ratcheted 1410 → 1110.
159+
["src-tauri/src/migration_tests.rs", 1110],
158160
["src-tauri/src/nostr_convert.rs", 1126],
159161
["src/shared/api/relayClientSession.ts", 1022],
160-
["src-tauri/src/migration.rs", 1575],
162+
// Boot-time event sync (persona/team/agent event reconcile) was split out
163+
// to event_sync.rs, ratcheting this limit 1575 → 1310. Remaining content is
164+
// the pre-identity data migrations; still queued to split further.
165+
["src-tauri/src/migration.rs", 1310],
161166
// onMarkRead + isUnread prop threading (mirrors the onMarkUnread prop
162167
// already here) for the single-toggle mark-read/unread menu item — a small
163168
// overage from load-bearing per-message plumbing, not generic debt growth.
Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
//! Boot-time disk→relay event reconcile ("event sync").
2+
//!
3+
//! Reconciles the on-disk JSON stores (`personas.json`, `teams.json`,
4+
//! `managed-agents.json`) into signed retention events queued for relay
5+
//! publish. Runs after identity resolution (event signing needs the owner
6+
//! keys), unlike the pre-identity migrations in [`crate::migration`].
7+
8+
use std::path::Path;
9+
10+
/// Reconcile personas, teams, and managed agents into signed retention
11+
/// events. All readers consume the already-synced
12+
/// `personas.json`/`teams.json`/`managed-agents.json` that
13+
/// `sync_team_personas` wrote in [`crate::migration::run_boot_migrations`]
14+
/// (see its `# Ordering` guard). Event signing needs the resolved owner keys,
15+
/// so this runs after identity resolution, not in the boot migrations.
16+
pub fn run_event_sync(app: &tauri::AppHandle, owner_keys: &nostr::Keys) {
17+
migrate_personas_to_events(app, owner_keys);
18+
migrate_teams_to_events(app, owner_keys);
19+
crate::managed_agents::reconcile::reconcile_agents_to_events(app, owner_keys);
20+
}
21+
22+
/// Reconcile `personas.json` into the persona-event retention store.
23+
///
24+
/// Must run AFTER `migrate_packs_to_teams` (depends on field renames being
25+
/// complete) and AFTER the persisted identity is resolved (it signs every
26+
/// retained event with the owner's keys).
27+
///
28+
/// Per-record reconcile: for each non-builtin persona it compares the freshly
29+
/// serialized event content against the retained row at the same coordinate
30+
/// and re-retains (marking `pending_sync = 1`) only when the row is absent or
31+
/// its content differs. An unchanged persona is left untouched, so a launch
32+
/// after a no-op edit does not churn `pending_sync`; a persona added or edited
33+
/// on disk between launches is picked up and republished. There is no
34+
/// whole-store sentinel — comparing per coordinate is what lets newly added
35+
/// personas reach the relay.
36+
///
37+
/// Strategy: write to local SQLite retention first (durable copy), mark as
38+
/// `pending_sync = 1` for later relay publish. Migration succeeds on local
39+
/// write, not relay acknowledgment. Every retained row is a real signed
40+
/// event — there is no placeholder path.
41+
pub fn migrate_personas_to_events(app: &tauri::AppHandle, keys: &nostr::Keys) {
42+
use crate::managed_agents::managed_agents_base_dir;
43+
44+
let Ok(base_dir) = managed_agents_base_dir(app) else {
45+
return;
46+
};
47+
48+
match migrate_personas_in_dir(&base_dir, keys) {
49+
Ok(0) => {}
50+
Ok(migrated) => {
51+
eprintln!(
52+
"buzz-desktop: persona-event-migration: {migrated} personas migrated to retention"
53+
);
54+
}
55+
Err(e) => {
56+
eprintln!("buzz-desktop: persona-event-migration: {e}");
57+
}
58+
}
59+
}
60+
61+
/// Core reconcile logic, decoupled from the Tauri `AppHandle` for testing.
62+
///
63+
/// Returns the number of personas (re)written to the retention store. Returns
64+
/// `Ok(0)` when every non-builtin persona already has a matching retained row
65+
/// (or there are none to reconcile).
66+
fn migrate_personas_in_dir(base_dir: &Path, keys: &nostr::Keys) -> Result<u32, String> {
67+
use crate::managed_agents::{
68+
persona_events::{build_persona_event, monotonic_created_at, persona_d_tag},
69+
retention::{get_retained_event, open_retention_db, retain_event, RetainedEvent},
70+
PersonaRecord,
71+
};
72+
use buzz_core_pkg::kind::KIND_PERSONA;
73+
use nostr::JsonUtil;
74+
75+
let pubkey = keys.public_key().to_hex();
76+
77+
// Read personas.json fresh at reconcile time. Nothing to do if absent.
78+
let personas_path = base_dir.join("personas.json");
79+
if !personas_path.exists() {
80+
return Ok(0);
81+
}
82+
83+
let content = std::fs::read_to_string(&personas_path)
84+
.map_err(|e| format!("failed to read personas.json: {e}"))?;
85+
86+
let records: Vec<PersonaRecord> = serde_json::from_str(&content)
87+
.map_err(|e| format!("failed to parse personas.json: {e}"))?;
88+
89+
if records.is_empty() {
90+
return Ok(0);
91+
}
92+
93+
// Open (or create) the retention database.
94+
let db_path = base_dir.join("retention.db");
95+
let conn =
96+
open_retention_db(&db_path).map_err(|e| format!("failed to open retention db: {e}"))?;
97+
98+
let mut migrated = 0u32;
99+
100+
for record in &records {
101+
// Skip built-in personas — they're always available from code.
102+
if record.is_builtin {
103+
continue;
104+
}
105+
106+
let d_tag = persona_d_tag(record);
107+
108+
// Fetch the retained head first so the rebuilt event can supersede it:
109+
// build at the default `now` and a future-dated head (clock skew, or an
110+
// interactive same-second `max(now, head+1)` bump) would make
111+
// `retain_event`'s `created_at >= ...` guard SILENTLY skip the UPDATE
112+
// while `migrated` over-reports. Mirror the interactive sites' monotonic
113+
// bump (F1) so a changed body always lands.
114+
let existing = get_retained_event(&conn, KIND_PERSONA, &pubkey, &d_tag)?;
115+
116+
let event = build_persona_event(record)
117+
.map_err(|e| format!("failed to build event for '{}': {e}", record.display_name))?
118+
.custom_created_at(monotonic_created_at(
119+
existing.as_ref().map(|row| row.created_at),
120+
))
121+
.sign_with_keys(keys)
122+
.map_err(|e| format!("failed to sign event for '{}': {e}", record.display_name))?;
123+
124+
// Per-coordinate reconcile: skip when an identical body is already
125+
// retained, so an unchanged persona doesn't reset `pending_sync`.
126+
// Content is timestamp-independent, so the monotonic bump above never
127+
// forces a spurious republish.
128+
let event_content = event.content.to_string();
129+
if existing
130+
.as_ref()
131+
.is_some_and(|row| row.content == event_content)
132+
{
133+
continue;
134+
}
135+
136+
let retained = RetainedEvent {
137+
kind: KIND_PERSONA,
138+
pubkey: pubkey.clone(),
139+
d_tag,
140+
content: event_content,
141+
// Safety: nostr timestamps are seconds and stay below i64::MAX
142+
// until year 2262.
143+
created_at: event.created_at.as_secs() as i64,
144+
raw_event: event.as_json(),
145+
pending_sync: true,
146+
};
147+
148+
// The monotonic bump guarantees `created_at > head`, so the upsert's
149+
// `>=` guard always lands the UPDATE — `migrated` counts only real,
150+
// retained republishes.
151+
retain_event(&conn, &retained)
152+
.map_err(|e| format!("failed to retain '{}': {e}", record.display_name))?;
153+
migrated += 1;
154+
}
155+
156+
Ok(migrated)
157+
}
158+
159+
/// Reconcile `teams.json` into kind:30176 team events in the retention store.
160+
///
161+
/// Mirrors [`migrate_personas_to_events`] for teams: it picks up team metadata
162+
/// edits (name/description/persona_ids) made on disk between launches and
163+
/// queues them for relay publish. Managed agents (kind:30177) are deliberately
164+
/// NOT reconciled here — they have no pack/dir source and are backfilled from
165+
/// `managed-agents.json` elsewhere.
166+
///
167+
/// Must run after the persisted identity is resolved (it signs each event with
168+
/// the owner's keys).
169+
pub fn migrate_teams_to_events(app: &tauri::AppHandle, keys: &nostr::Keys) {
170+
use crate::managed_agents::managed_agents_base_dir;
171+
172+
let Ok(base_dir) = managed_agents_base_dir(app) else {
173+
return;
174+
};
175+
176+
match migrate_teams_in_dir(&base_dir, keys) {
177+
Ok(0) => {}
178+
Ok(migrated) => {
179+
eprintln!("buzz-desktop: team-event-migration: {migrated} teams migrated to retention");
180+
}
181+
Err(e) => {
182+
eprintln!("buzz-desktop: team-event-migration: {e}");
183+
}
184+
}
185+
}
186+
187+
/// Core team reconcile logic, decoupled from the Tauri `AppHandle` for testing.
188+
///
189+
/// Returns the number of teams (re)written to the retention store. The
190+
/// per-coordinate content compare matches [`migrate_personas_in_dir`]: an
191+
/// unchanged team is skipped so a launch does not churn `pending_sync`.
192+
fn migrate_teams_in_dir(base_dir: &Path, keys: &nostr::Keys) -> Result<u32, String> {
193+
use crate::managed_agents::{
194+
persona_events::monotonic_created_at,
195+
retention::{get_retained_event, open_retention_db, retain_event, RetainedEvent},
196+
team_events::build_team_event,
197+
TeamRecord,
198+
};
199+
use buzz_core_pkg::kind::KIND_TEAM;
200+
use nostr::JsonUtil;
201+
202+
let pubkey = keys.public_key().to_hex();
203+
204+
let teams_path = base_dir.join("teams.json");
205+
if !teams_path.exists() {
206+
return Ok(0);
207+
}
208+
209+
let content = std::fs::read_to_string(&teams_path)
210+
.map_err(|e| format!("failed to read teams.json: {e}"))?;
211+
212+
let records: Vec<TeamRecord> =
213+
serde_json::from_str(&content).map_err(|e| format!("failed to parse teams.json: {e}"))?;
214+
215+
if records.is_empty() {
216+
return Ok(0);
217+
}
218+
219+
let db_path = base_dir.join("retention.db");
220+
let conn =
221+
open_retention_db(&db_path).map_err(|e| format!("failed to open retention db: {e}"))?;
222+
223+
let mut migrated = 0u32;
224+
225+
for record in &records {
226+
// Skip built-in teams — they're always available from code.
227+
if record.is_builtin {
228+
continue;
229+
}
230+
231+
// Team d-tag is the team id (team_events.rs: no slug fallback).
232+
let d_tag = record.id.clone();
233+
234+
// Fetch the head first so the monotonic bump can supersede a
235+
// future-dated head — see migrate_personas_in_dir (F1/F8).
236+
let existing = get_retained_event(&conn, KIND_TEAM, &pubkey, &d_tag)?;
237+
238+
let event = build_team_event(record)
239+
.map_err(|e| format!("failed to build event for team '{}': {e}", record.name))?
240+
.custom_created_at(monotonic_created_at(
241+
existing.as_ref().map(|row| row.created_at),
242+
))
243+
.sign_with_keys(keys)
244+
.map_err(|e| format!("failed to sign event for team '{}': {e}", record.name))?;
245+
246+
let event_content = event.content.to_string();
247+
if existing
248+
.as_ref()
249+
.is_some_and(|row| row.content == event_content)
250+
{
251+
continue;
252+
}
253+
254+
let retained = RetainedEvent {
255+
kind: KIND_TEAM,
256+
pubkey: pubkey.clone(),
257+
d_tag,
258+
content: event_content,
259+
created_at: event.created_at.as_secs() as i64,
260+
raw_event: event.as_json(),
261+
pending_sync: true,
262+
};
263+
264+
// Monotonic bump guarantees the upsert UPDATE lands — `migrated` counts
265+
// only real republishes.
266+
retain_event(&conn, &retained)
267+
.map_err(|e| format!("failed to retain team '{}': {e}", record.name))?;
268+
migrated += 1;
269+
}
270+
271+
Ok(migrated)
272+
}
273+
274+
#[cfg(test)]
275+
#[path = "event_sync_tests.rs"]
276+
mod tests;
277+
278+
#[cfg(test)]
279+
#[path = "event_sync_team_events_tests.rs"]
280+
mod team_events_tests;
File renamed without changes.

0 commit comments

Comments
 (0)