Skip to content

Commit a6cfd65

Browse files
wpfleger96npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7
andauthored
fix(snapshot): send Buzz shares as PNG avatar cards (#1811)
Signed-off-by: Will Pfleger <[email protected]> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co>
1 parent d41b4c3 commit a6cfd65

19 files changed

Lines changed: 561 additions & 250 deletions

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ async fn fetch_blob_bytes_with_cap(
281281
pub(crate) enum SnapshotFileKind {
282282
/// `.agent.json` — plaintext JSON; accepts memory; 5 MiB cap.
283283
AgentJson,
284-
/// `.agent.png` — PNG with embedded metadata; no memory; 10 MiB cap.
284+
/// `.agent.png` — PNG with embedded metadata; 10 MiB cap.
285285
AgentPng,
286286
/// `.team.json` — team template; 25 MiB cap.
287287
TeamJson,
@@ -443,9 +443,8 @@ pub async fn fetch_snapshot_bytes(
443443
return Err("hash mismatch: fetched bytes do not match the declared SHA-256".to_string());
444444
}
445445

446-
// 3. Byte magic must match the expected kind (filename → format).
447-
// This prevents .agent.png delivering JSON bytes (including memory-bearing
448-
// JSON) and .agent.json delivering PNG bytes.
446+
// 3. Byte magic must match the expected kind (filename → format), so a
447+
// filename cannot select a JSON or PNG parser for the other format.
449448
ensure_bytes_match_kind(&bytes, kind)?;
450449

451450
// 4. Bytes must parse as the snapshot type selected by the filename.

‎desktop/src-tauri/src/commands/personas/snapshot.rs‎

Lines changed: 91 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ fn parse_format_is_png(s: &str) -> Result<bool, String> {
173173
///
174174
/// **Invariants preserved (identical for both callers):**
175175
/// - JSON/PNG format selection from the parsed `is_png` flag (magic-byte sniffing is import-only)
176-
/// - PNG + memory hard rejection
177176
/// - Memory-source pubkey validation
178177
/// - Secret exclusion (env_vars never enter the manifest via `build_snapshot`)
179178
/// - Output filename derived from the agent display name
@@ -182,18 +181,10 @@ pub(crate) async fn materialize_snapshot_bytes(
182181
memory_source_pubkey: Option<String>,
183182
memory_level: MemoryLevel,
184183
is_png: bool,
184+
avatar_png_data_url: Option<String>,
185185
app: AppHandle,
186186
state: State<'_, AppState>,
187187
) -> Result<SnapshotPayload, String> {
188-
// Eagerly reject PNG + memory — avoid an unnecessary relay round-trip.
189-
if is_png && memory_level != MemoryLevel::None {
190-
return Err(
191-
"Cannot export memory to .agent.png — use JSON format for memory-bearing \
192-
snapshots."
193-
.to_string(),
194-
);
195-
}
196-
197188
// ── Load definition record and memory-source instance under lock ─────────
198189
let (record, memory_pubkey) = {
199190
let _store_guard = state
@@ -274,7 +265,9 @@ pub(crate) async fn materialize_snapshot_bytes(
274265
let slug = crate::util::slugify(&display_name, "agent", 50);
275266

276267
if is_png {
277-
let png_bytes = encode_snapshot_png(&snapshot, avatar_bytes.as_deref())
268+
let png_body_avatar_bytes =
269+
resolve_png_body_avatar_bytes(avatar_png_data_url.as_deref(), avatar_bytes);
270+
let png_bytes = encode_snapshot_png(&snapshot, png_body_avatar_bytes.as_deref())
278271
.map_err(|e| format!("Failed to encode .agent.png: {e}"))?;
279272
validate_snapshot_encode_size(png_bytes.len(), true)?;
280273
Ok(SnapshotPayload {
@@ -292,6 +285,17 @@ pub(crate) async fn materialize_snapshot_bytes(
292285
}
293286
}
294287

288+
/// Choose bytes for the PNG image body without changing the source avatar the
289+
/// manifest preserves for import.
290+
fn resolve_png_body_avatar_bytes(
291+
avatar_png_data_url: Option<&str>,
292+
store_avatar_bytes: Option<Vec<u8>>,
293+
) -> Option<Vec<u8>> {
294+
avatar_png_data_url
295+
.and_then(crate::managed_agents::agent_snapshot::decode_avatar_data_url)
296+
.or(store_avatar_bytes)
297+
}
298+
295299
/// Export an agent definition as a `buzz-agent-snapshot v1` file.
296300
///
297301
/// `id` is a definition slug or a keyed-instance pubkey.
@@ -309,6 +313,7 @@ pub async fn export_agent_snapshot(
309313
memory_source_pubkey: Option<String>,
310314
memory_level: String,
311315
format: String,
316+
avatar_png_data_url: Option<String>,
312317
app: AppHandle,
313318
state: State<'_, AppState>,
314319
) -> Result<bool, String> {
@@ -320,6 +325,7 @@ pub async fn export_agent_snapshot(
320325
memory_source_pubkey,
321326
memory_level,
322327
is_png,
328+
avatar_png_data_url,
323329
app.clone(),
324330
state,
325331
)
@@ -370,15 +376,23 @@ pub async fn encode_agent_snapshot_for_send(
370376
memory_source_pubkey: Option<String>,
371377
memory_level: String,
372378
format: String,
379+
avatar_png_data_url: Option<String>,
373380
app: AppHandle,
374381
state: State<'_, AppState>,
375382
) -> Result<EncodedSnapshotPayload, String> {
376383
let memory_level = parse_memory_level(&memory_level)?;
377384
let is_png = parse_format_is_png(&format)?;
378385

379-
let payload =
380-
materialize_snapshot_bytes(id, memory_source_pubkey, memory_level, is_png, app, state)
381-
.await?;
386+
let payload = materialize_snapshot_bytes(
387+
id,
388+
memory_source_pubkey,
389+
memory_level,
390+
is_png,
391+
avatar_png_data_url,
392+
app,
393+
state,
394+
)
395+
.await?;
382396

383397
Ok(EncodedSnapshotPayload {
384398
file_bytes: payload.bytes,
@@ -388,3 +402,66 @@ pub async fn encode_agent_snapshot_for_send(
388402

389403
#[cfg(test)]
390404
mod tests;
405+
406+
#[cfg(test)]
407+
mod png_body_tests {
408+
use super::*;
409+
use base64::Engine as _;
410+
use png::Decoder;
411+
412+
#[test]
413+
fn frontend_raster_becomes_png_body_without_replacing_manifest_source() {
414+
let avatar = image::DynamicImage::ImageRgb8(image::RgbImage::from_pixel(
415+
3,
416+
2,
417+
image::Rgb([0x12, 0x34, 0x56]),
418+
));
419+
let mut source_png = Vec::new();
420+
avatar
421+
.write_to(
422+
&mut std::io::Cursor::new(&mut source_png),
423+
image::ImageFormat::Png,
424+
)
425+
.unwrap();
426+
let avatar_data_url = format!(
427+
"data:image/png;base64,{}",
428+
base64::engine::general_purpose::STANDARD.encode(&source_png)
429+
);
430+
let snapshot = crate::managed_agents::agent_snapshot::AgentSnapshot {
431+
format: crate::managed_agents::agent_snapshot::FORMAT_DISCRIMINATOR.to_string(),
432+
version: crate::managed_agents::agent_snapshot::FORMAT_VERSION,
433+
definition: crate::managed_agents::agent_snapshot::AgentSnapshotDefinition {
434+
name: "Agent".to_string(),
435+
system_prompt: None,
436+
runtime: None,
437+
model: None,
438+
provider: None,
439+
parallelism: None,
440+
respond_to: None,
441+
respond_to_allowlist: vec![],
442+
name_pool: vec![],
443+
idle_timeout_seconds: None,
444+
max_turn_duration_seconds: None,
445+
},
446+
profile: crate::managed_agents::agent_snapshot::AgentSnapshotProfile {
447+
display_name: "Agent".to_string(),
448+
about: None,
449+
avatar_data_url: None,
450+
avatar_url: Some("https://relay.example/media/avatar.png".to_string()),
451+
},
452+
memory: crate::managed_agents::agent_snapshot::AgentSnapshotMemory {
453+
level: MemoryLevel::None,
454+
entries: vec![],
455+
},
456+
};
457+
let body_bytes = resolve_png_body_avatar_bytes(Some(&avatar_data_url), None);
458+
let png_bytes = encode_snapshot_png(&snapshot, body_bytes.as_deref()).unwrap();
459+
let reader = Decoder::new(std::io::Cursor::new(&png_bytes))
460+
.read_info()
461+
.unwrap();
462+
let decoded = import::decode_snapshot_from_bytes(&png_bytes).unwrap();
463+
464+
assert_eq!((reader.info().width, reader.info().height), (3, 2));
465+
assert_eq!(decoded.profile.avatar_url, snapshot.profile.avatar_url);
466+
}
467+
}

‎desktop/src-tauri/src/commands/personas/snapshot/import.rs‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,9 @@ const PNG_MAGIC: [u8; 4] = [0x89, 0x50, 0x4e, 0x47];
197197
/// Fails closed on malformed content, wrong format, or unsupported version.
198198
/// Never trusts the file extension — only the bytes.
199199
///
200-
/// **PNG memory policy:** any PNG manifest whose `memory.level` is not `None`,
201-
/// or whose `memory.entries` is non-empty despite `level == None`, is rejected
202-
/// before any write. Plaintext memory is accepted only from `.agent.json`.
200+
/// **Memory consistency:** any manifest whose `memory.entries` is non-empty
201+
/// despite `memory.level == None` is rejected before any write, regardless of
202+
/// the enclosing format.
203203
///
204204
/// **Size cap:** PNG inputs over 10 MiB and JSON inputs over 5 MiB are rejected
205205
/// before allocation to avoid avoidable large-input work.
@@ -214,18 +214,9 @@ pub(crate) fn decode_snapshot_from_bytes(
214214
));
215215
}
216216
let snapshot = decode_snapshot_png(file_bytes)?;
217-
// Hard reject: PNG must never carry memory.
218-
if snapshot.memory.level != crate::managed_agents::agent_snapshot::MemoryLevel::None {
217+
if snapshot.memory.level == MemoryLevel::None && !snapshot.memory.entries.is_empty() {
219218
return Err(
220-
"Cannot import a memory-bearing .agent.png — use .agent.json for \
221-
snapshots that include memory."
222-
.to_string(),
223-
);
224-
}
225-
if !snapshot.memory.entries.is_empty() {
226-
return Err(
227-
"Snapshot is malformed: .agent.png carries memory entries despite \
228-
memory.level being 'none'."
219+
"Snapshot is malformed: memory.level is 'none' but entries are present."
229220
.to_string(),
230221
);
231222
}

‎desktop/src-tauri/src/commands/personas/snapshot/tests.rs‎

Lines changed: 51 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -368,80 +368,71 @@ fn import_avatar_url_fallback_is_used_when_no_data_url() {
368368
);
369369
}
370370

371-
// ── Import: PNG memory policy ─────────────────────────────────────────────
372-
373-
/// PNG with memory level `core` is rejected before any write.
374-
#[test]
375-
fn import_png_with_core_memory_is_rejected() {
376-
// encode_snapshot_png refuses to encode memory-bearing snapshots —
377-
// that guard on the EXPORT side is correct. On the IMPORT side,
378-
// decode_snapshot_from_bytes adds a matching guard so that a
379-
// foreign-built PNG carrying a core/everything memory level is also
380-
// rejected. We verify the guard condition here.
381-
let level = MemoryLevel::Core;
382-
let entries = vec![AgentSnapshotMemoryEntry {
383-
slug: "core".to_string(),
384-
body: "Secret.".to_string(),
385-
}];
386-
// Simulate what decode_snapshot_from_bytes checks after PNG decode:
387-
let would_reject = level != MemoryLevel::None;
388-
assert!(
389-
would_reject,
390-
"PNG with core memory level must be rejected by the import guard"
391-
);
392-
// Also verify that the encoder itself refuses this on the export side.
371+
// ── Import: PNG memory parity ─────────────────────────────────────────────
372+
373+
/// PNG with core memory round-trips through the production import decoder.
374+
#[test]
375+
fn import_png_with_core_memory_preserves_entries() {
393376
use crate::managed_agents::agent_snapshot::encode_snapshot_png;
394-
let snapshot = make_snapshot(MemoryLevel::Core, entries);
395-
assert!(
396-
encode_snapshot_png(&snapshot, None).is_err(),
397-
"encode_snapshot_png must also refuse memory-bearing snapshots"
377+
let snapshot = make_snapshot(
378+
MemoryLevel::Core,
379+
vec![AgentSnapshotMemoryEntry {
380+
slug: "core".to_string(),
381+
body: "Remember this.".to_string(),
382+
}],
398383
);
384+
385+
let png_bytes = encode_snapshot_png(&snapshot, None).unwrap();
386+
let decoded = decode_snapshot_from_bytes(&png_bytes).unwrap();
387+
388+
assert_eq!(decoded.memory, snapshot.memory);
399389
}
400390

401-
/// PNG with memory level `everything` is rejected before any write.
391+
/// PNG with all memory round-trips through the production import decoder.
402392
#[test]
403-
fn import_png_with_everything_memory_is_rejected() {
404-
// Same as core: both the encoder guard and the decoder guard fire.
405-
let level = MemoryLevel::Everything;
406-
let would_reject = level != MemoryLevel::None;
407-
assert!(
408-
would_reject,
409-
"PNG with everything memory level must be rejected by the import guard"
410-
);
393+
fn import_png_with_everything_memory_preserves_entries() {
411394
use crate::managed_agents::agent_snapshot::encode_snapshot_png;
412395
let snapshot = make_snapshot(
413396
MemoryLevel::Everything,
414-
vec![AgentSnapshotMemoryEntry {
415-
slug: "mem/research".to_string(),
416-
body: "Notes.".to_string(),
417-
}],
418-
);
419-
assert!(
420-
encode_snapshot_png(&snapshot, None).is_err(),
421-
"encode_snapshot_png must also refuse memory-bearing snapshots"
397+
vec![
398+
AgentSnapshotMemoryEntry {
399+
slug: "core".to_string(),
400+
body: "Remember this.".to_string(),
401+
},
402+
AgentSnapshotMemoryEntry {
403+
slug: "mem/research".to_string(),
404+
body: "Notes.".to_string(),
405+
},
406+
],
422407
);
408+
409+
let png_bytes = encode_snapshot_png(&snapshot, None).unwrap();
410+
let decoded = decode_snapshot_from_bytes(&png_bytes).unwrap();
411+
412+
assert_eq!(decoded.memory, snapshot.memory);
423413
}
424414

425-
/// PNG with `none` level but non-empty entries is rejected (malformed).
426-
/// We test this by verifying the guard condition directly, since
427-
/// encode_snapshot_png correctly refuses to produce such a PNG.
415+
/// PNG with `none` level but non-empty entries is rejected as malformed.
428416
#[test]
429417
fn import_png_with_none_level_and_entries_is_rejected() {
430-
// This guard is enforced in decode_snapshot_from_bytes after the PNG
431-
// path resolves. A PNG with none + entries cannot be produced by
432-
// encode_snapshot_png (it already guards that), but could arrive from
433-
// a foreign tool. We verify the guard condition matches the spec:
434-
let level = MemoryLevel::None;
435-
let entries = [AgentSnapshotMemoryEntry {
436-
slug: "core".to_string(),
437-
body: "Leak.".to_string(),
438-
}];
439-
// The guard in decode_snapshot_from_bytes:
440-
let would_reject = !entries.is_empty() && level == MemoryLevel::None;
441-
assert!(
442-
would_reject,
443-
"none level + non-empty entries must trigger the guard"
418+
use crate::managed_agents::agent_snapshot::{
419+
encode_snapshot_json, encode_snapshot_png, make_png_with_text, PNG_CHUNK_KEYWORD,
420+
};
421+
use base64::{engine::general_purpose::STANDARD, Engine as _};
422+
423+
let snapshot = make_snapshot(
424+
MemoryLevel::None,
425+
vec![AgentSnapshotMemoryEntry {
426+
slug: "core".to_string(),
427+
body: "Leak.".to_string(),
428+
}],
444429
);
430+
let json = encode_snapshot_json(&snapshot).unwrap();
431+
let png_bytes = make_png_with_text(PNG_CHUNK_KEYWORD, &STANDARD.encode(json)).unwrap();
432+
433+
assert!(encode_snapshot_png(&snapshot, None).is_err());
434+
let error = decode_snapshot_from_bytes(&png_bytes).unwrap_err();
435+
assert!(error.contains("'none' but entries are present"));
445436
}
446437

447438
/// PNG with `none` level and no entries imports normally.

0 commit comments

Comments
 (0)