Skip to content

Commit 6f486e8

Browse files
SamiTyler Longwell
andcommitted
fix(agents): write self-authored config back to the private-config overlay
The overlay only ever learned config from events this device RECEIVED. Both fill paths are inbound-only: `insert_patch` on an `Applied` inbound event (`personas/inbound.rs:228`) and boot hydration (`hydrate_from_retention`). Neither fires for an event this device authored -- the relay's echo of our own event dedupes to `Skipped` in `retain_inbound_event`, because the row is already retained. So the overlay stays pinned at the last received generation for the whole session. `update_managed_agent` resolves that patch onto the disk record, applies the user's edit, saves, and retains -- correct for ONE edit. The SECOND edit in the same session resolves the same stale patch onto the now fresher disk record, reverting the first edit, and publishes the reversion as an audit-clean gen+1 successor that wins LWW. The resolve result is written back to disk, so the revert is durable, not just in-flight. That is Max's live gate red on a single backend: gen 3 published parallelism 19, the following rename returned 17 and published 17 as gen 4. It stands independent of the Device-A/B attribution he retracted. Fix: after `retain_agent_record` commits, read the just-retained kind:30179 head back out of the same connection and `insert_patch` it. One seam -- `retain_managed_agent_pending` -- covers all five writers (create, settings, edit/rename, start, rename-rollback) with no per-caller copies and no new-writer trap. `reconcile.rs` is untouched: `retain_agent_record` takes `conn`+`keys` and threading `AppState` through it would drag the boot reconcile into the diff for nothing. No new lock edge: callers already hold `managed_agents_store_lock` and the overlay lock is taken under it, the same order `resolved_local_record` uses. Decode is factored into `patch_from_retained_row`, shared with boot hydration, so both learn config through exactly one path. Absorbing unconditionally (not gated on the retain reporting a change) keeps the overlay from ever running ahead of retention: every insert comes from a row read back out of the database. A missing or undecodable head leaves the current entry alone rather than clearing it. Coverage. `sami_second_edit_in_one_session_preserves_the_first_edit` runs the two-edit sequence through the real retention engine, seeding gen 2 via `retain_inbound_event` + `hydrate_from_retention` so the overlay is populated exactly as boot populates it. Its negative control runs the same sequence without the write-through and asserts the revert to 17, so the main assertion cannot pass vacuously. `absorb_retained_head_leaves_the_overlay_alone_when_ there_is_no_head` pins the no-clear contract with a positive control proving the same call DOES update on a present head. Both behavioural tests model the helper body -- every caller is inside a `#[tauri::command]` needing a live `AppHandle`, so deleting the production call leaves them green (the failure mode already documented on `write_site_resolve_guard`). Three source guards close that: exact call count of 1, source order retain-before-absorb, and a negative control proving the searched literals are load-bearing. Mutants, 4/4 killed: (1) `absorb_retained_head` body no-oped -> the red test fails with exactly the live symptom, `left: Some(17) right: Some(19)`; (2) production call deleted -> both source guards fail; (3) production call moved before the retain -> the order guard fails; (4) test helper's order swapped -> the behavioural test fails. Gate at this tree: desktop lib suite 2272 passed / 0 failed / 14 ignored, `cargo clippy --workspace --all-targets` clean, `cargo fmt --check` clean, `check-file-sizes` rc=0 with CHECK_FILE_SIZES_BASE pinned to the merge-base with origin/main. Co-authored-by: Tyler Longwell <[email protected]> Signed-off-by: Tyler Longwell <[email protected]>
1 parent aa39d72 commit 6f486e8

4 files changed

Lines changed: 371 additions & 11 deletions

File tree

‎desktop/src-tauri/src/commands/agents.rs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ pub(super) fn workspace_owner_hex(state: &AppState) -> Result<String, String> {
3939
/// only runtime fields produces an identical row and never re-enqueues a
4040
/// publish. Best-effort: a failure here is logged and swallowed so a retention
4141
/// hiccup never blocks the disk-authoritative write.
42+
///
43+
/// Also writes the just-retained kind:30179 head back through to the in-memory
44+
/// overlay. This is the ONLY path by which the overlay learns config this
45+
/// device authored — inbound `insert_patch` never fires for our own event
46+
/// (the relay echo dedupes to `Skipped`) and boot hydration runs once per
47+
/// launch — so without it a second edit in the same session resolves the
48+
/// stale patch onto the fresher disk record and publishes a silent revert of
49+
/// the first edit. Overlay lock is taken UNDER `managed_agents_store_lock`,
50+
/// which every caller already holds: the established order, same as
51+
/// `resolved_local_record`.
4252
pub(super) fn retain_managed_agent_pending(
4353
app: &AppHandle,
4454
state: &AppState,
@@ -52,7 +62,12 @@ pub(super) fn retain_managed_agent_pending(
5262
// Shared engine with the boot-time reconcile: projection content diff
5363
// (no republish for runtime-only churn) + monotonic created_at bump
5464
// past the retained head (NIP-AP step 3).
55-
retain_agent_record(&conn, &scope.owner_keys, record).map(|_| ())
65+
retain_agent_record(&conn, &scope.owner_keys, record)?;
66+
state
67+
.private_managed_agent_overlay
68+
.lock()
69+
.map_err(|error| error.to_string())?
70+
.absorb_retained_head(&conn, &scope.owner_keys, &record.pubkey)
5671
})();
5772
if let Err(e) = result {
5873
eprintln!("buzz-desktop: agent-retain: {e}");

‎desktop/src-tauri/src/managed_agents/private_config_overlay.rs‎

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,44 @@ impl PrivateConfigOverlay {
213213
self.0.remove(pubkey);
214214
}
215215

216+
/// Write-through for a SELF-AUTHORED retain: adopt the kind:30179 head this
217+
/// device just wrote as the config it is following.
218+
///
219+
/// Both existing fill paths are inbound-only — `insert_patch` on an
220+
/// `Applied` inbound event (`personas/inbound.rs`) and boot hydration
221+
/// below — and neither fires for an event this device authored: the
222+
/// relay's echo of our own event dedupes to `Skipped` against the row we
223+
/// already retained. So without this the overlay stays pinned at the last
224+
/// *received* generation, and the NEXT edit resolves that stale patch on
225+
/// top of the fresher disk record, silently reverting the previous edit
226+
/// and publishing the reversion as a valid successor.
227+
///
228+
/// Absorbing unconditionally (not only when the retain reported a change)
229+
/// is safe and strictly convergent: the overlay is never ahead of
230+
/// retention — every insert either comes from a row written in the same
231+
/// step or is read back out of retention. A missing/undecodable head
232+
/// leaves the current entry alone rather than clearing it.
233+
pub(crate) fn absorb_retained_head(
234+
&mut self,
235+
conn: &rusqlite::Connection,
236+
owner_keys: &nostr::Keys,
237+
agent_pubkey: &str,
238+
) -> Result<(), String> {
239+
let row = crate::managed_agents::retention::get_retained_event(
240+
conn,
241+
buzz_core_pkg::kind::KIND_PRIVATE_MANAGED_AGENT,
242+
&owner_keys.public_key().to_hex(),
243+
agent_pubkey,
244+
)?;
245+
if let Some(patch) = row
246+
.as_ref()
247+
.and_then(|row| patch_from_retained_row(&row.raw_event, owner_keys))
248+
{
249+
self.insert_patch(patch);
250+
}
251+
Ok(())
252+
}
253+
216254
pub(crate) fn resolve_local_record(&self, record: &ManagedAgentRecord) -> ManagedAgentRecord {
217255
let mut resolved = record.clone();
218256
if let Some(patch) = self.0.get(&record.pubkey) {
@@ -437,6 +475,23 @@ mod tests {
437475
}
438476
}
439477

478+
/// Decode one retained kind:30179 row into a patch. Shared by boot hydration
479+
/// and the self-authored write-through so both learn config through exactly
480+
/// one decode path. Best-effort: a row that fails to parse, decrypt, or
481+
/// validate yields `None` rather than an error, matching the inbound path's
482+
/// per-record reject.
483+
fn patch_from_retained_row(
484+
raw_event: &str,
485+
owner_keys: &nostr::Keys,
486+
) -> Option<PrivateConfigPatch> {
487+
use buzz_core_pkg::private_managed_agent;
488+
use nostr::JsonUtil;
489+
490+
let event = nostr::Event::from_json(raw_event).ok()?;
491+
let (_, payload) = private_managed_agent::validate_and_decrypt(&event, owner_keys).ok()?;
492+
PrivateConfigPatch::from_payload(payload).ok()
493+
}
494+
440495
/// Rebuild the in-memory overlay from the retained kind:30179 rows.
441496
///
442497
/// The inbound path only calls `insert_patch` when `retain_inbound_event`
@@ -454,8 +509,7 @@ pub(crate) fn hydrate_from_retention(
454509
conn: &rusqlite::Connection,
455510
owner_keys: &nostr::Keys,
456511
) -> Result<PrivateConfigOverlay, String> {
457-
use buzz_core_pkg::{kind::KIND_PRIVATE_MANAGED_AGENT, private_managed_agent};
458-
use nostr::JsonUtil;
512+
use buzz_core_pkg::kind::KIND_PRIVATE_MANAGED_AGENT;
459513

460514
let rows = crate::managed_agents::retention::get_retained_events_of_kind(
461515
conn,
@@ -465,14 +519,7 @@ pub(crate) fn hydrate_from_retention(
465519

466520
let mut overlay = PrivateConfigOverlay::default();
467521
for row in rows {
468-
let Ok(event) = nostr::Event::from_json(&row.raw_event) else {
469-
continue;
470-
};
471-
let Ok((_, payload)) = private_managed_agent::validate_and_decrypt(&event, owner_keys)
472-
else {
473-
continue;
474-
};
475-
if let Ok(patch) = PrivateConfigPatch::from_payload(payload) {
522+
if let Some(patch) = patch_from_retained_row(&row.raw_event, owner_keys) {
476523
overlay.insert_patch(patch);
477524
}
478525
}

‎desktop/src-tauri/src/managed_agents/reconcile/tests.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,4 +555,5 @@ fn retain_agent_record_is_noop_when_unchanged() {
555555
);
556556
}
557557

558+
mod self_authored_overlay_tests;
558559
mod stale_republish_tests;

0 commit comments

Comments
 (0)